blob: b1e70c6fd2251ca5d904ff7c4481221f9d65ffb7 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Code to handle user-settable options. This is all pretty much table-
12 * driven. Checklist for adding a new option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000013 * - Put it in the options array in optiondefs.h (copy an existing entry).
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 * - For a global option: Add a variable for it in option.h.
15 * - For a buffer or window local option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000016 * - Add a PV_XX macro definition to the optiondefs.h file.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017 * - Add a variable to the window or buffer struct in structs.h.
18 * - For a window option, add some code to copy_winopt().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000019 * - For a window string option, add code to check_win_options() and
20 * clear_winopt().
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 * - For a buffer option, add some code to buf_copy_options().
22 * - For a buffer string option, add code to check_buf_options().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000023 * - If it's a numeric option, add any necessary bounds checks to
24 * set_num_option().
25 * - If it's a list of flags, add some code in did_set_string_option(), search
26 * for WW_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027 * - When adding an option with expansion (P_EXPAND), but with a different
28 * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
Bram Moolenaarcea912a2016-10-12 14:20:24 +020029 * - Add documentation! One line in doc/quickref.txt, full description in
Bram Moolenaar071d4272004-06-13 20:20:40 +000030 * options.txt, and any other related places.
31 * - Add an entry in runtime/optwin.vim.
32 * When making changes:
33 * - Adjust the help for the option in doc/option.txt.
34 * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
35 * comment at the help for the 'compatible' option.
36 */
37
38#define IN_OPTION_C
39#include "vim.h"
Bram Moolenaar0eddca42019-09-12 22:26:43 +020040#include "optiondefs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010042static void set_options_default(int opt_flags);
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +010043static void set_string_default_esc(char *name, char_u *val, int escape);
Bram Moolenaarb00ef052020-09-12 14:53:53 +020044static char_u *find_dup_item(char_u *origval, char_u *newval, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010045static char_u *option_expand(int opt_idx, char_u *val);
46static void didset_options(void);
47static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000048#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010049static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000050#else
51# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
52#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010053static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
54static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, size_t errbuflen, int opt_flags);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020055static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020057static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010058static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010059static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010060static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
61static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020062static int istermoption(struct vimoption *p);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000063static char_u *get_varp_scope(struct vimoption *p, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020065static void check_win_options(win_T *win);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000066static void option_value2string(struct vimoption *, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010067static void check_winopt(winopt_T *wop);
68static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010069static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Christian Brabandt757593c2023-08-22 21:44:10 +020071#if defined(FEAT_EVAL) || defined(PROTO)
72static char *(p_bin_dep_opts[]) = {"textwidth", "wrapmargin", "modeline", "expandtab", NULL};
73static char *(p_paste_dep_opts[]) = {"autoindent", "expandtab", "ruler", "showmatch", "smarttab",
74 "softtabstop", "textwidth", "wrapmargin",
75#ifdef FEAT_RIGHTLEFT
76 "hkmap", "revins",
77#endif
78#ifdef FEAT_VARTABS
79 "varsofttabstop",
80#endif
81 NULL};
82static void didset_options_sctx(int opt_flags, char **buf);
83#endif
84
85
Bram Moolenaar071d4272004-06-13 20:20:40 +000086/*
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000087 * Initialize the 'shell' option to a default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 */
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000089 static void
90set_init_default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000091{
92 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000093
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000094 // Find default value for 'shell' option.
95 // Don't use it if it is empty.
Bram Moolenaar7c626922005-02-07 22:01:03 +000096 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010097#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +000098 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010099 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +0000101 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200102#if defined(MSWIN)
103 {
104 // For MS-Windows put the path in quotes instead of escaping spaces.
105 char_u *cmd;
106 size_t len;
107
108 if (vim_strchr(p, ' ') != NULL)
109 {
110 len = STRLEN(p) + 3; // two quotes and a trailing NUL
111 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +0200112 if (cmd != NULL)
113 {
114 vim_snprintf((char *)cmd, len, "\"%s\"", p);
115 set_string_default("sh", cmd);
116 vim_free(cmd);
117 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200118 }
119 else
120 set_string_default("sh", p);
121 }
122#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100123 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200124#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000125}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000127/*
128 * Set the default for 'backupskip' to include environment variables for
129 * temp files.
130 */
131 static void
132set_init_default_backupskip(void)
133{
134 int opt_idx;
135 long_u n;
136 char_u *p;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100137#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000138 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100139#else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000140 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100141#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000142 int len;
143 garray_T ga;
Bram Moolenaar14338022023-03-15 22:05:44 +0000144 char_u *item;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200145
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000146 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000148 ga_init2(&ga, 1, 100);
149 for (n = 0; n < (long)ARRAY_LENGTH(names); ++n)
150 {
Bram Moolenaar14338022023-03-15 22:05:44 +0000151 int mustfree = FALSE;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100152#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000153 if (*names[n] == NUL)
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100154# ifdef MACOS_X
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000155 p = (char_u *)"/private/tmp";
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100156# else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000157 p = (char_u *)"/tmp";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000159 else
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100160#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000161 p = vim_getenv((char_u *)names[n], &mustfree);
162 if (p != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000164 // First time count the NUL, otherwise count the ','.
165 len = (int)STRLEN(p) + 3;
166 item = alloc(len);
Bram Moolenaar14338022023-03-15 22:05:44 +0000167 if (item != NULL)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000168 {
Bram Moolenaar14338022023-03-15 22:05:44 +0000169 STRCPY(item, p);
170 add_pathsep(item);
171 STRCAT(item, "*");
172 if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
173 == NULL
174 && ga_grow(&ga, len) == OK)
175 {
176 if (ga.ga_len > 0)
177 STRCAT(ga.ga_data, ",");
178 STRCAT(ga.ga_data, item);
179 ga.ga_len += len;
180 }
181 vim_free(item);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000184 if (mustfree)
185 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000187 if (ga.ga_data != NULL)
188 {
189 set_string_default("bsk", ga.ga_data);
190 vim_free(ga.ga_data);
191 }
192}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000194/*
195 * Initialize the 'maxmemtot' and 'maxmem' options to a default value.
196 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory.
197 */
198 static void
199set_init_default_maxmemtot(void)
200{
201 int opt_idx;
202 long_u n;
203
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 opt_idx = findoption((char_u *)"maxmemtot");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000205 if (opt_idx < 0)
206 return;
207
208#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
209 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 {
ichizok02560422022-04-05 14:18:44 +0100212#if defined(HAVE_AVAIL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000213 // Use amount of memory available at this moment.
214 n = (mch_avail_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100215#elif defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000216 // Use amount of memory available to Vim.
217 n = (mch_total_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100218#else
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000219 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000221 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
222 opt_idx = findoption((char_u *)"maxmem");
223 if (opt_idx >= 0)
224 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000225#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000226 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
227 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000228#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000229 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000232}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000234/*
235 * Initialize the 'cdpath' option to a default value.
236 */
237 static void
238set_init_default_cdpath(void)
239{
240 int opt_idx;
241 char_u *cdpath;
242 char_u *buf;
243 int i;
244 int j;
245 int mustfree = FALSE;
246
247 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
248 if (cdpath == NULL)
249 return;
250
251 buf = alloc((STRLEN(cdpath) << 1) + 2);
252 if (buf != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000254 buf[0] = ','; // start with ",", current dir first
255 j = 1;
256 for (i = 0; cdpath[i] != NUL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000258 if (vim_ispathlistsep(cdpath[i]))
259 buf[j++] = ',';
260 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000262 if (cdpath[i] == ' ' || cdpath[i] == ',')
263 buf[j++] = '\\';
264 buf[j++] = cdpath[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 }
266 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000267 buf[j] = NUL;
268 opt_idx = findoption((char_u *)"cdpath");
269 if (opt_idx >= 0)
270 {
271 options[opt_idx].def_val[VI_DEFAULT] = buf;
272 options[opt_idx].flags |= P_DEF_ALLOCED;
273 }
274 else
275 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000277 if (mustfree)
278 vim_free(cdpath);
279}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000281/*
282 * Initialize the 'printencoding' option to a default value.
283 */
284 static void
285set_init_default_printencoding(void)
286{
ichizok02560422022-04-05 14:18:44 +0100287#if defined(FEAT_POSTSCRIPT) && \
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000288 (defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100289 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100291# if defined(MSWIN)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000292 (char_u *)"cp1252"
ichizok02560422022-04-05 14:18:44 +0100293# elif defined(VMS)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000294 (char_u *)"dec-mcs"
ichizok02560422022-04-05 14:18:44 +0100295# elif defined(MAC)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000296 (char_u *)"mac-roman"
ichizok02560422022-04-05 14:18:44 +0100297# else // HPUX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000298 (char_u *)"hp-roman8"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000300 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000302}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303
304#ifdef FEAT_POSTSCRIPT
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000305/*
306 * Initialize the 'printexpr' option to a default value.
307 */
308 static void
309set_init_default_printexpr(void)
310{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100311 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100313# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000314 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
ichizok02560422022-04-05 14:18:44 +0100315# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
317
ichizok02560422022-04-05 14:18:44 +0100318# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320# endif
321 );
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000322}
323#endif
324
325#ifdef UNIX
326/*
327 * Force restricted-mode on for "nologin" or "false" $SHELL
328 */
329 static void
330set_init_restricted_mode(void)
331{
332 char_u *p;
333
334 p = get_isolated_shell_name();
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000335 if (p == NULL)
336 return;
337 if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
338 restricted = TRUE;
339 vim_free(p);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000340}
341#endif
342
343#ifdef CLEAN_RUNTIMEPATH
344/*
345 * When Vim is started with the "--clean" argument, set the default value
346 * for the 'runtimepath' and 'packpath' options.
347 */
348 static void
349set_init_clean_rtp(void)
350{
351 int opt_idx;
352
353 opt_idx = findoption((char_u *)"runtimepath");
354 if (opt_idx >= 0)
355 {
356 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
357 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
358 }
359 opt_idx = findoption((char_u *)"packpath");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000360 if (opt_idx < 0)
361 return;
362 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
363 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000364}
365#endif
366
367/*
368 * Expand environment variables and things like "~" for the defaults.
369 * If option_expand() returns non-NULL the variable is expanded. This can
370 * only happen for non-indirect options.
371 * Also set the default to the expanded value, so ":set" does not list
372 * them.
373 * Don't set the P_ALLOCED flag, because we don't want to free the
374 * default.
375 */
376 static void
377set_init_expand_env(void)
378{
379 int opt_idx;
380 char_u *p;
381
382 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
383 {
384 if ((options[opt_idx].flags & P_GETTEXT)
385 && options[opt_idx].var != NULL)
386 p = (char_u *)_(*(char **)options[opt_idx].var);
387 else
388 p = option_expand(opt_idx, NULL);
389 if (p != NULL && (p = vim_strsave(p)) != NULL)
390 {
391 *(char_u **)options[opt_idx].var = p;
392 // VIMEXP
393 // Defaults for all expanded options are currently the same for Vi
394 // and Vim. When this changes, add some code here! Also need to
395 // split P_DEF_ALLOCED in two.
396 if (options[opt_idx].flags & P_DEF_ALLOCED)
397 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
398 options[opt_idx].def_val[VI_DEFAULT] = p;
399 options[opt_idx].flags |= P_DEF_ALLOCED;
400 }
401 }
402}
403
404/*
405 * Initialize the 'LANG' environment variable to a default value.
406 */
407 static void
408set_init_lang_env(void)
409{
410#if defined(MSWIN) && defined(FEAT_GETTEXT)
411 // If $LANG isn't set, try to get a good value for it. This makes the
412 // right language be used automatically. Don't do this for English.
413 if (mch_getenv((char_u *)"LANG") == NULL)
414 {
415 char buf[20];
416 long_u n;
417
418 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
419 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
420 // only the first two.
421 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
422 (LPTSTR)buf, 20);
423 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
424 {
425 // There are a few exceptions (probably more)
426 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
427 STRCPY(buf, "zh_TW");
428 else if (STRNICMP(buf, "chs", 3) == 0
429 || STRNICMP(buf, "zhc", 3) == 0)
430 STRCPY(buf, "zh_CN");
431 else if (STRNICMP(buf, "jp", 2) == 0)
432 STRCPY(buf, "ja");
433 else
434 buf[2] = NUL; // truncate to two-letter code
435 vim_setenv((char_u *)"LANG", (char_u *)buf);
436 }
437 }
438#elif defined(MACOS_CONVERT)
439 // Moved to os_mac_conv.c to avoid dependency problems.
440 mac_lang_init();
441#endif
442}
443
444/*
445 * Initialize the 'encoding' option to a default value.
446 */
447 static void
448set_init_default_encoding(void)
449{
450 char_u *p;
451 int opt_idx;
452
453# ifdef MSWIN
454 // MS-Windows has builtin support for conversion to and from Unicode, using
455 // "utf-8" for 'encoding' should work best for most users.
456 p = vim_strsave((char_u *)ENC_DFLT);
457# else
458 // enc_locale() will try to find the encoding of the current locale.
459 // This works best for properly configured systems, old and new.
460 p = enc_locale();
461# endif
462 if (p == NULL)
463 return;
464
465 // Try setting 'encoding' and check if the value is valid.
466 // If not, go back to the default encoding.
467 char_u *save_enc = p_enc;
468 p_enc = p;
469 if (STRCMP(p_enc, "gb18030") == 0)
470 {
471 // We don't support "gb18030", but "cp936" is a good substitute
472 // for practical purposes, thus use that. It's not an alias to
473 // still support conversion between gb18030 and utf-8.
474 p_enc = vim_strsave((char_u *)"cp936");
475 vim_free(p);
476 }
477 if (mb_init() == NULL)
478 {
479 opt_idx = findoption((char_u *)"encoding");
480 if (opt_idx >= 0)
481 {
482 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
483 options[opt_idx].flags |= P_DEF_ALLOCED;
484 }
485
486#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
487 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
488 {
489 // Adjust the default for 'isprint' and 'iskeyword' to match
490 // latin1. Also set the defaults for when 'nocompatible' is
491 // set.
492 set_string_option_direct((char_u *)"isp", -1,
493 ISP_LATIN1, OPT_FREE, SID_NONE);
494 set_string_option_direct((char_u *)"isk", -1,
495 ISK_LATIN1, OPT_FREE, SID_NONE);
496 opt_idx = findoption((char_u *)"isp");
497 if (opt_idx >= 0)
498 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
499 opt_idx = findoption((char_u *)"isk");
500 if (opt_idx >= 0)
501 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
502 (void)init_chartab();
503 }
504#endif
505
506#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
507 // Win32 console: When GetACP() returns a different value from
508 // GetConsoleCP() set 'termencoding'.
509 if (
510# ifdef VIMDLL
511 (!gui.in_use && !gui.starting) &&
512# endif
513 GetACP() != GetConsoleCP())
514 {
515 char buf[50];
516
517 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
518 // Use an alternative value.
519 if (GetConsoleCP() == 0)
520 sprintf(buf, "cp%ld", (long)GetACP());
521 else
522 sprintf(buf, "cp%ld", (long)GetConsoleCP());
523 p_tenc = vim_strsave((char_u *)buf);
524 if (p_tenc != NULL)
525 {
526 opt_idx = findoption((char_u *)"termencoding");
527 if (opt_idx >= 0)
528 {
529 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
530 options[opt_idx].flags |= P_DEF_ALLOCED;
531 }
532 convert_setup(&input_conv, p_tenc, p_enc);
533 convert_setup(&output_conv, p_enc, p_tenc);
534 }
535 else
536 p_tenc = empty_option;
537 }
538#endif
539#if defined(MSWIN)
540 // $HOME may have characters in active code page.
541 init_homedir();
542#endif
543 }
544 else
545 {
546 vim_free(p_enc);
547 p_enc = save_enc;
548 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000549}
550
551/*
552 * Initialize the options, first part.
553 *
554 * Called only once from main(), just after creating the first buffer.
555 * If "clean_arg" is TRUE Vim was started with --clean.
556 */
557 void
558set_init_1(int clean_arg)
559{
560#ifdef FEAT_LANGMAP
561 langmap_init();
562#endif
563
564 // Be Vi compatible by default
565 p_cp = TRUE;
566
567 // Use POSIX compatibility when $VIM_POSIX is set.
568 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
569 {
570 set_string_default("cpo", (char_u *)CPO_ALL);
571 set_string_default("shm", (char_u *)SHM_POSIX);
572 }
573
574 set_init_default_shell();
575 set_init_default_backupskip();
576 set_init_default_maxmemtot();
577 set_init_default_cdpath();
578 set_init_default_printencoding();
579#ifdef FEAT_POSTSCRIPT
580 set_init_default_printexpr();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581#endif
582
583 /*
584 * Set all the options (except the terminal options) to their default
585 * value. Also set the global value for local options.
586 */
587 set_options_default(0);
588
matveytadbb1bf2022-02-01 17:26:12 +0000589#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000590 set_init_restricted_mode();
matveytadbb1bf2022-02-01 17:26:12 +0000591#endif
592
Bram Moolenaar07268702018-03-01 21:57:32 +0100593#ifdef CLEAN_RUNTIMEPATH
594 if (clean_arg)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000595 set_init_clean_rtp();
Bram Moolenaar07268702018-03-01 21:57:32 +0100596#endif
597
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598#ifdef FEAT_GUI
599 if (found_reverse_arg)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100600 set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601#endif
602
603 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100604 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100605 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 check_buf_options(curbuf);
607 check_win_options(curwin);
608 check_options();
609
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100610 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 didset_options();
612
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000613#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100614 // Use the current chartab for the generic chartab. This is not in
615 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000616 init_spell_chartab();
617#endif
618
K.Takatace3189d2023-02-15 19:13:43 +0000619 set_init_default_encoding();
620
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000621 // Expand environment variables and things like "~" for the defaults.
622 set_init_expand_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100624 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100627 // Detect use of mlterm.
628 // Mlterm is a terminal emulator akin to xterm that has some special
629 // abilities (bidi namely).
630 // NOTE: mlterm's author is being asked to 'set' a variable
631 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 if (mch_getenv((char_u *)"MLTERM") != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100633 set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634#endif
635
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200636 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000638 set_init_lang_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
640#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100641 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 set_helplang_default(get_mess_lang());
643#endif
644}
645
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200646static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
647
648/*
649 * Set the "fileencodings" option to the default value for when 'encoding' is
650 * utf-8.
651 */
652 void
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +0000653set_fencs_unicode(void)
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200654{
655 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
656 OPT_FREE, 0);
657}
658
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659/*
660 * Set an option to its default value.
661 * This does not take care of side effects!
662 */
663 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100664set_option_default(
665 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100666 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
667 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100669 char_u *varp; // pointer to variable for current option
670 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000672 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
674
675 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
676 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100677 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 {
679 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
680 if (flags & P_STRING)
681 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200682 // 'fencs' default value depends on 'encoding'
683 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
684 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100685 // Use set_string_option_direct() for local options to handle
686 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200687 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200688 set_string_option_direct(NULL, opt_idx,
689 options[opt_idx].def_val[dvi], opt_flags, 0);
690 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200692 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
693 free_string_option(*(char_u **)(varp));
694 *(char_u **)varp = options[opt_idx].def_val[dvi];
695 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 }
697 }
698 else if (flags & P_NUM)
699 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000700 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 win_comp_scroll(curwin);
702 else
703 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100704 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
705
706 if ((long *)varp == &curwin->w_p_so
707 || (long *)varp == &curwin->w_p_siso)
708 // 'scrolloff' and 'sidescrolloff' local values have a
709 // different default value than the global default.
710 *(long *)varp = -1;
711 else
712 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100713 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 if (both)
715 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100716 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 }
718 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100719 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100721 // the cast to long is required for Manx C, long_i is needed for
722 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000723 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000724#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100725 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000726 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
727 *(int *)varp = FALSE;
728#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100729 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 if (both)
731 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
732 *(int *)varp;
733 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000734
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100735 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000736 flagsp = insecure_flag(opt_idx, opt_flags);
737 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 }
739
740#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200741 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742#endif
743}
744
745/*
746 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200747 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 */
749 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100750set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100751 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752{
753 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000755 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756
Bram Moolenaardac13472019-09-16 21:06:21 +0200757 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200758 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200759 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100760 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200761# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200762 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200763 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200764# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100765 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 set_option_default(i, opt_flags, p_cp);
767
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100768 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000769 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200771 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772}
773
774/*
775 * Set the Vi-default value of a string option.
776 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100777 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100779 static void
780set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781{
782 char_u *p;
783 int opt_idx;
784
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100785 if (escape && vim_strchr(val, ' ') != NULL)
786 p = vim_strsave_escaped(val, (char_u *)" ");
787 else
788 p = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000789 if (p == NULL) // we don't want a NULL
790 return;
791
792 opt_idx = findoption((char_u *)name);
793 if (opt_idx < 0)
794 return;
795
796 if (options[opt_idx].flags & P_DEF_ALLOCED)
797 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
798 options[opt_idx].def_val[VI_DEFAULT] = p;
799 options[opt_idx].flags |= P_DEF_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800}
801
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100802 void
803set_string_default(char *name, char_u *val)
804{
805 set_string_default_esc(name, val, FALSE);
806}
807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200809 * For an option value that contains comma separated items, find "newval" in
810 * "origval". Return NULL if not found.
811 */
812 static char_u *
813find_dup_item(char_u *origval, char_u *newval, long_u flags)
814{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200815 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200816 size_t newlen;
817 char_u *s;
818
819 if (origval == NULL)
820 return NULL;
821
822 newlen = STRLEN(newval);
823 for (s = origval; *s != NUL; ++s)
824 {
825 if ((!(flags & P_COMMA)
826 || s == origval
827 || (s[-1] == ',' && !(bs & 1)))
828 && STRNCMP(s, newval, newlen) == 0
829 && (!(flags & P_COMMA)
830 || s[newlen] == ','
831 || s[newlen] == NUL))
832 return s;
833 // Count backslashes. Only a comma with an even number of backslashes
834 // or a single backslash preceded by a comma before it is recognized as
835 // a separator.
836 if ((s > origval + 1
837 && s[-1] == '\\'
838 && s[-2] != ',')
839 || (s == origval + 1
840 && s[-1] == '\\'))
841 ++bs;
842 else
843 bs = 0;
844 }
845 return NULL;
846}
847
848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 * Set the Vi-default value of a number option.
850 * Used for 'lines' and 'columns'.
851 */
852 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100853set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000855 int opt_idx;
856
857 opt_idx = findoption((char_u *)name);
858 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000859 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860}
861
Dominique Pelle748b3082022-01-08 12:41:16 +0000862#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200863/*
864 * Set all window-local and buffer-local options to the Vim default.
865 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200866 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200867 */
868 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200869set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200870{
871 win_T *save_curwin = curwin;
872 int i;
873
874 curwin = wp;
875 curbuf = curwin->w_buffer;
876 block_autocmds();
877
Bram Moolenaardac13472019-09-16 21:06:21 +0200878 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200879 {
880 struct vimoption *p = &(options[i]);
881 char_u *varp = get_varp_scope(p, OPT_LOCAL);
882
883 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200884 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200885 && !(options[i].flags & P_NODEFAULT)
886 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200887 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200888 }
889
890 unblock_autocmds();
891 curwin = save_curwin;
892 curbuf = curwin->w_buffer;
893}
Dominique Pelle748b3082022-01-08 12:41:16 +0000894#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200895
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000896#if defined(EXITFREE) || defined(PROTO)
897/*
898 * Free all options.
899 */
900 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100901free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000902{
903 int i;
904
Bram Moolenaardac13472019-09-16 21:06:21 +0200905 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000906 {
907 if (options[i].indir == PV_NONE)
908 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100909 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100910 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000911 free_string_option(*(char_u **)options[i].var);
912 if (options[i].flags & P_DEF_ALLOCED)
913 free_string_option(options[i].def_val[VI_DEFAULT]);
914 }
915 else if (options[i].var != VAR_WIN
916 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100917 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200918 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000919 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000920 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000921 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000922}
923#endif
924
925
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926/*
927 * Initialize the options, part two: After getting Rows and Columns and
928 * setting 'term'.
929 */
930 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100931set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000933 int idx;
934
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000935 // 'scroll' defaults to half the window height. The stored default is zero,
936 // which results in the actual value computed from the window height.
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000937 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000938 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000939 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 comp_col();
941
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000942 // 'window' is only for backwards compatibility with Vi.
943 // Default is Rows - 1.
Bram Moolenaard68071d2006-05-02 22:08:30 +0000944 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000945 p_window = Rows - 1;
946 set_number_default("window", Rows - 1);
947
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100948 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100949#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000950 // If 'background' wasn't set by the user, try guessing the value,
951 // depending on the terminal name. Only need to check for terminals
952 // with a dark background, that can handle color.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000953 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000954 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
955 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000957 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100958 // don't mark it as set, when starting the GUI it may be
959 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000960 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 }
962#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000963
964#ifdef CURSOR_SHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000965 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000966#endif
967#ifdef FEAT_MOUSESHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000968 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000969#endif
970#ifdef FEAT_PRINTER
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000971 (void)parse_printoptions(NULL); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/*
976 * Initialize the options, part three: After reading the .vimrc
977 */
978 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100979set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100981#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982/*
983 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
984 * This is done after other initializations, where 'shell' might have been
985 * set, but only if they have not been set before.
986 */
987 char_u *p;
988 int idx_srr;
989 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100990# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 int idx_sp;
992 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100993# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994
995 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000996 if (idx_srr < 0)
997 do_srr = FALSE;
998 else
999 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001000# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001002 if (idx_sp < 0)
1003 do_sp = FALSE;
1004 else
1005 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001006# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001007 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 if (p != NULL)
1009 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001010 // Default for p_sp is "| tee", for p_srr is ">".
1011 // For known shells it is changed here to include stderr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 if ( fnamecmp(p, "csh") == 0
1013 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001014# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 || fnamecmp(p, "csh.exe") == 0
1016 || fnamecmp(p, "tcsh.exe") == 0
1017# endif
1018 )
1019 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001020# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 if (do_sp)
1022 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001023# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001025# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001027# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1029 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001030# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 if (do_srr)
1032 {
1033 p_srr = (char_u *)">&";
1034 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1035 }
1036 }
Mike Williams12795022021-06-28 20:53:58 +02001037# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +02001038 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
1039 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +02001040 else if ( fnamecmp(p, "powershell") == 0
1041 || fnamecmp(p, "powershell.exe") == 0
1042 )
1043 {
1044# if defined(FEAT_QUICKFIX)
1045 if (do_sp)
1046 {
1047 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
1048 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1049 }
1050# endif
1051 if (do_srr)
1052 {
1053 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
1054 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1055 }
1056 }
1057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 else
Natanael Copa56318362021-05-06 18:46:35 +02001059 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 if ( fnamecmp(p, "sh") == 0
1061 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001062 || fnamecmp(p, "mksh") == 0
1063 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001065 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001067 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001068 || fnamecmp(p, "ash") == 0
1069 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001070 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001071# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 || fnamecmp(p, "cmd") == 0
1073 || fnamecmp(p, "sh.exe") == 0
1074 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001075 || fnamecmp(p, "mksh.exe") == 0
1076 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001078 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 || fnamecmp(p, "bash.exe") == 0
1080 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001081 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001082 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001084 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001086# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 if (do_sp)
1088 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001089# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001091# else
Mike Williamsa3d1b292021-06-30 20:56:00 +02001092 if (fnamecmp(p, "pwsh") == 0)
1093 p_sp = (char_u *)">%s 2>&1";
1094 else
1095 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001096# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1098 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001099# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 if (do_srr)
1101 {
1102 p_srr = (char_u *)">%s 2>&1";
1103 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1104 }
1105 }
1106 vim_free(p);
1107 }
1108#endif
1109
Bram Moolenaar4f974752019-02-17 17:44:42 +01001110#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001112 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1113 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001115 * set, but only if they have not been set before.
1116 * Default values depend on shell (cmd.exe is default shell):
1117 *
1118 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001119 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001120 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001121 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001122 * "sh" like shells - "-c" "\""
1123 *
1124 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 */
Mike Williams12795022021-06-28 20:53:58 +02001126 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1127 {
1128 int idx_opt;
1129
1130 idx_opt = findoption((char_u *)"shcf");
1131 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1132 {
1133 p_shcf = (char_u*)"-Command";
1134 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1135 }
1136
1137 idx_opt = findoption((char_u *)"sxq");
1138 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1139 {
1140 p_sxq = (char_u*)"\"";
1141 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1142 }
1143 }
1144 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 {
1146 int idx3;
1147
1148 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001149 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 {
1151 p_shcf = (char_u *)"-c";
1152 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1153 }
1154
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001155 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001157 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 {
1159 p_sxq = (char_u *)"\"";
1160 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001163 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1164 {
1165 int idx3;
1166
1167 /*
1168 * cmd.exe on Windows will strip the first and last double quote given
1169 * on the command line, e.g. most of the time things like:
1170 * cmd /c "my path/to/echo" "my args to echo"
1171 * become:
1172 * my path/to/echo" "my args to echo
1173 * when executed.
1174 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001175 * To avoid this, set shellxquote to surround the command in
1176 * parenthesis. This appears to make most commands work, without
1177 * breaking commands that worked previously, such as
1178 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001179 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001180 idx3 = findoption((char_u *)"sxq");
1181 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1182 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001183 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001184 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1185 }
1186
Bram Moolenaara64ba222012-02-12 23:23:31 +01001187 idx3 = findoption((char_u *)"shcf");
1188 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1189 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001190 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001191 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1192 }
1193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194#endif
1195
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001196 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001197 {
1198 int idx_ffs = findoption((char_u *)"ffs");
1199
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001200 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001201 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1202 set_fileformat(default_fileformat(), OPT_LOCAL);
1203 }
1204
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206}
1207
1208#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1209/*
1210 * When 'helplang' is still at its default value, set it to "lang".
1211 * Only the first two characters of "lang" are used.
1212 */
1213 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001214set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215{
1216 int idx;
1217
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001218 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 return;
1220 idx = findoption((char_u *)"hlg");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001221 if (idx < 0 || (options[idx].flags & P_WAS_SET))
1222 return;
1223
1224 if (options[idx].flags & P_ALLOCED)
1225 free_string_option(p_hlg);
1226 p_hlg = vim_strsave(lang);
1227 if (p_hlg == NULL)
1228 p_hlg = empty_option;
1229 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001231 // zh_CN becomes "cn", zh_TW becomes "tw"
1232 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001233 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001234 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1235 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001236 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001237 // any C like setting, such as C.UTF-8, becomes "en"
1238 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1239 {
1240 p_hlg[0] = 'e';
1241 p_hlg[1] = 'n';
1242 }
1243 p_hlg[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001245 options[idx].flags |= P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246}
1247#endif
1248
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249/*
1250 * 'title' and 'icon' only default to true if they have not been set or reset
1251 * in .vimrc and we can read the old value.
1252 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1253 * they can be reset. This reduces startup time when using X on a remote
1254 * machine.
1255 */
1256 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001257set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258{
1259 int idx1;
1260 long val;
1261
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001262 // If GUI is (going to be) used, we can always set the window title and
1263 // icon name. Saves a bit of time, because the X11 display server does
1264 // not need to be contacted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001266 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 {
1268#ifdef FEAT_GUI
1269 if (gui.starting || gui.in_use)
1270 val = TRUE;
1271 else
1272#endif
1273 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001274 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 p_title = val;
1276 }
1277 idx1 = findoption((char_u *)"icon");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001278 if (idx1 < 0 || (options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001280 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001282
1283#ifdef FEAT_GUI
1284 if (gui.starting || gui.in_use)
1285 val = TRUE;
1286 else
1287#endif
1288 val = mch_can_restore_icon();
1289 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
1290 p_icon = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001293 void
1294ex_set(exarg_T *eap)
1295{
1296 int flags = 0;
1297
1298 if (eap->cmdidx == CMD_setlocal)
1299 flags = OPT_LOCAL;
1300 else if (eap->cmdidx == CMD_setglobal)
1301 flags = OPT_GLOBAL;
1302#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001303 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001304 ex_options(eap);
1305 else
1306#endif
1307 {
1308 if (eap->forceit)
1309 flags |= OPT_ONECOLUMN;
1310 (void)do_set(eap->arg, flags);
1311 }
1312}
1313
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001314/*
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001315 * :set boolean option prefix
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001316 */
Bram Moolenaar47403942022-09-21 21:12:53 +01001317typedef enum {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001318 PREFIX_NO = 0, // "no" prefix
1319 PREFIX_NONE, // no prefix
1320 PREFIX_INV, // "inv" prefix
1321} set_prefix_T;
1322
1323/*
1324 * Return the prefix type for the option name in *argp.
1325 */
1326 static set_prefix_T
1327get_option_prefix(char_u **argp)
1328{
1329 int prefix = PREFIX_NONE;
1330 char_u *arg = *argp;
1331
1332 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
1333 {
1334 prefix = PREFIX_NO;
1335 arg += 2;
1336 }
1337 else if (STRNCMP(arg, "inv", 3) == 0)
1338 {
1339 prefix = PREFIX_INV;
1340 arg += 3;
1341 }
1342
1343 *argp = arg;
1344 return prefix;
1345}
1346
1347/*
1348 * Parse the option name in "arg" and return the option index in "*opt_idxp",
1349 * and the option name length in "*lenp". For a <t_xx> option, return the key
1350 * number in "*keyp".
1351 *
1352 * Returns FAIL if an option starting with "<" doesn't end with a ">",
1353 * otherwise returns OK.
1354 */
1355 static int
1356parse_option_name(char_u *arg, int *opt_idxp, int *lenp, int *keyp)
1357{
1358 int key = 0;
1359 int len;
1360 int opt_idx;
1361
1362 if (*arg == '<')
1363 {
1364 opt_idx = -1;
1365 // look out for <t_>;>
1366 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1367 len = 5;
1368 else
1369 {
1370 len = 1;
1371 while (arg[len] != NUL && arg[len] != '>')
1372 ++len;
1373 }
1374 if (arg[len] != '>')
1375 return FAIL;
1376
1377 arg[len] = NUL; // put NUL after name
1378 if (arg[1] == 't' && arg[2] == '_') // could be term code
1379 opt_idx = findoption(arg + 1);
1380 arg[len++] = '>'; // restore '>'
1381 if (opt_idx == -1)
1382 key = find_key_option(arg + 1, TRUE);
1383 }
1384 else
1385 {
1386 int nextchar; // next non-white char after option name
1387
1388 len = 0;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001389 // The two characters after "t_" may not be alphanumeric.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001390 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1391 len = 4;
1392 else
1393 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1394 ++len;
1395 nextchar = arg[len];
1396 arg[len] = NUL; // put NUL after name
1397 opt_idx = findoption(arg);
1398 arg[len] = nextchar; // restore nextchar
1399 if (opt_idx == -1)
1400 key = find_key_option(arg, FALSE);
1401 }
1402
1403 *keyp = key;
1404 *lenp = len;
1405 *opt_idxp = opt_idx;
1406
1407 return OK;
1408}
1409
1410/*
1411 * Get the option operator (+=, ^=, -=).
1412 */
1413 static set_op_T
1414get_opt_op(char_u *arg)
1415{
1416 set_op_T op = OP_NONE;
1417
1418 if (*arg != NUL && *(arg + 1) == '=')
1419 {
1420 if (*arg == '+')
1421 op = OP_ADDING; // "+="
1422 else if (*arg == '^')
1423 op = OP_PREPENDING; // "^="
1424 else if (*arg == '-')
1425 op = OP_REMOVING; // "-="
1426 }
1427
1428 return op;
1429}
1430
1431/*
1432 * Validate whether the value of the option in "opt_idx" can be changed.
1433 * Returns FAIL if the option can be skipped or cannot be changed. Returns OK
1434 * if it can be changed.
1435 */
1436 static int
1437validate_opt_idx(int opt_idx, int opt_flags, long_u flags, char **errmsg)
1438{
1439 // Skip all options that are not window-local (used when showing
1440 // an already loaded buffer in a window).
1441 if ((opt_flags & OPT_WINONLY)
1442 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1443 return FAIL;
1444
1445 // Skip all options that are window-local (used for :vimgrep).
1446 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1447 && options[opt_idx].var == VAR_WIN)
1448 return FAIL;
1449
1450 // Disallow changing some options from modelines.
1451 if (opt_flags & OPT_MODELINE)
1452 {
1453 if (flags & (P_SECURE | P_NO_ML))
1454 {
1455 *errmsg = e_not_allowed_in_modeline;
1456 return FAIL;
1457 }
1458 if ((flags & P_MLE) && !p_mle)
1459 {
1460 *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
1461 return FAIL;
1462 }
1463#ifdef FEAT_DIFF
1464 // In diff mode some options are overruled. This avoids that
1465 // 'foldmethod' becomes "marker" instead of "diff" and that
1466 // "wrap" gets set.
1467 if (curwin->w_p_diff
1468 && opt_idx >= 0 // shut up coverity warning
1469 && (
1470# ifdef FEAT_FOLDING
1471 options[opt_idx].indir == PV_FDM ||
1472# endif
1473 options[opt_idx].indir == PV_WRAP))
1474 return FAIL;
1475#endif
1476 }
1477
1478#ifdef HAVE_SANDBOX
1479 // Disallow changing some options in the sandbox
1480 if (sandbox != 0 && (flags & P_SECURE))
1481 {
1482 *errmsg = e_not_allowed_in_sandbox;
1483 return FAIL;
1484 }
1485#endif
1486
1487 return OK;
1488}
1489
Bram Moolenaar47403942022-09-21 21:12:53 +01001490/*
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001491 * Get the Vim/Vi default value for a string option.
1492 */
1493 static char_u *
1494stropt_get_default_val(
1495 int opt_idx,
1496 char_u *varp,
1497 int flags,
1498 int cp_val)
1499{
1500 char_u *newval;
1501
1502 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1503 ? VI_DEFAULT : VIM_DEFAULT];
1504 if ((char_u **)varp == &p_bg)
1505 {
1506 // guess the value of 'background'
1507#ifdef FEAT_GUI
1508 if (gui.in_use)
1509 newval = gui_bg_default();
1510 else
1511#endif
1512 newval = term_bg_default();
1513 }
1514 else if ((char_u **)varp == &p_fencs && enc_utf8)
1515 newval = fencs_utf8_default;
1516
1517 // expand environment variables and ~ since the default value was
1518 // already expanded, only required when an environment variable was set
1519 // later
1520 if (newval == NULL)
1521 newval = empty_option;
1522 else
1523 {
1524 char_u *s = option_expand(opt_idx, newval);
1525 if (s == NULL)
1526 s = newval;
1527 newval = vim_strsave(s);
1528 }
1529
1530 return newval;
1531}
1532
1533/*
1534 * Convert the 'backspace' option number value to a string: for adding,
1535 * prepending and removing string.
1536 */
1537 static void
1538opt_backspace_nr2str(
1539 char_u *varp,
1540 char_u **origval_p,
1541 char_u **origval_l_p,
1542 char_u **origval_g_p,
1543 char_u **oldval_p)
1544{
1545 int i = getdigits((char_u **)varp);
1546
1547 switch (i)
1548 {
1549 case 0:
1550 *(char_u **)varp = empty_option;
1551 break;
1552 case 1:
1553 *(char_u **)varp = vim_strsave((char_u *)"indent,eol");
1554 break;
1555 case 2:
1556 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,start");
1557 break;
1558 case 3:
1559 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,nostop");
1560 break;
1561 }
1562 vim_free(*oldval_p);
1563 if (*origval_p == *oldval_p)
1564 *origval_p = *(char_u **)varp;
1565 if (*origval_l_p == *oldval_p)
1566 *origval_l_p = *(char_u **)varp;
1567 if (*origval_g_p == *oldval_p)
1568 *origval_g_p = *(char_u **)varp;
1569 *oldval_p = *(char_u **)varp;
1570}
1571
1572/*
1573 * Convert the 'whichwrap' option number value to a string, for backwards
1574 * compatibility with Vim 3.0.
1575 * Note: 'argp' is a pointer to a char_u pointer and is updated.
1576 */
1577 static char_u *
1578opt_whichwrap_nr2str(char_u **argp, char_u *whichwrap)
1579{
1580 *whichwrap = NUL;
1581 int i = getdigits(argp);
1582 if (i & 1)
1583 STRCAT(whichwrap, "b,");
1584 if (i & 2)
1585 STRCAT(whichwrap, "s,");
1586 if (i & 4)
1587 STRCAT(whichwrap, "h,l,");
1588 if (i & 8)
1589 STRCAT(whichwrap, "<,>,");
1590 if (i & 16)
1591 STRCAT(whichwrap, "[,],");
1592 if (*whichwrap != NUL) // remove trailing ,
1593 whichwrap[STRLEN(whichwrap) - 1] = NUL;
1594
1595 return whichwrap;
1596}
1597
1598/*
1599 * Copy the new string value into allocated memory for the option.
1600 * Can't use set_string_option_direct(), because we need to remove the
1601 * backslashes.
1602 */
1603 static char_u *
1604stropt_copy_value(
1605 char_u *origval,
1606 char_u **argp,
1607 set_op_T op,
1608 int flags UNUSED)
1609{
1610 char_u *arg = *argp;
1611 unsigned newlen;
1612 char_u *newval;
1613 char_u *s = NULL;
1614
1615 // get a bit too much
1616 newlen = (unsigned)STRLEN(arg) + 1;
1617 if (op != OP_NONE)
1618 newlen += (unsigned)STRLEN(origval) + 1;
1619 newval = alloc(newlen);
1620 if (newval == NULL) // out of mem, don't change
1621 return NULL;
1622 s = newval;
1623
1624 // Copy the string, skip over escaped chars.
1625 // For MS-DOS and WIN32 backslashes before normal file name characters
1626 // are not removed, and keep backslash at start, for "\\machine\path",
1627 // but do remove it for "\\\\machine\\path".
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001628 // The reverse is found in escape_option_str_cmdline().
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001629 while (*arg != NUL && !VIM_ISWHITE(*arg))
1630 {
1631 int i;
1632
1633 if (*arg == '\\' && arg[1] != NUL
1634#ifdef BACKSLASH_IN_FILENAME
1635 && !((flags & P_EXPAND)
1636 && vim_isfilec(arg[1])
1637 && !VIM_ISWHITE(arg[1])
1638 && (arg[1] != '\\'
1639 || (s == newval && arg[2] != '\\')))
1640#endif
1641 )
1642 ++arg; // remove backslash
1643 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
1644 {
1645 // copy multibyte char
1646 mch_memmove(s, arg, (size_t)i);
1647 arg += i;
1648 s += i;
1649 }
1650 else
1651 *s++ = *arg++;
1652 }
1653 *s = NUL;
1654
1655 *argp = arg;
1656 return newval;
1657}
1658
1659/*
1660 * Expand environment variables and ~ in string option value 'newval'.
1661 */
1662 static char_u *
1663stropt_expand_envvar(
1664 int opt_idx,
1665 char_u *origval,
1666 char_u *newval,
1667 set_op_T op)
1668{
1669 char_u *s = option_expand(opt_idx, newval);
1670 if (s == NULL)
1671 return newval;
1672
1673 vim_free(newval);
1674 unsigned newlen = (unsigned)STRLEN(s) + 1;
1675 if (op != OP_NONE)
1676 newlen += (unsigned)STRLEN(origval) + 1;
1677
1678 newval = alloc(newlen);
1679 if (newval == NULL)
1680 return NULL;
1681
1682 STRCPY(newval, s);
1683
1684 return newval;
1685}
1686
1687/*
1688 * Concatenate the original and new values of a string option, adding a "," if
1689 * needed.
1690 */
1691 static void
1692stropt_concat_with_comma(
1693 char_u *origval,
1694 char_u *newval,
1695 set_op_T op,
1696 int flags)
1697{
1698 int len = 0;
1699
1700 int comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1701 if (op == OP_ADDING)
1702 {
1703 len = (int)STRLEN(origval);
1704 // strip a trailing comma, would get 2
1705 if (comma && len > 1
1706 && (flags & P_ONECOMMA) == P_ONECOMMA
1707 && origval[len - 1] == ','
1708 && origval[len - 2] != '\\')
1709 len--;
1710 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1711 mch_memmove(newval, origval, (size_t)len);
1712 }
1713 else
1714 {
1715 len = (int)STRLEN(newval);
1716 STRMOVE(newval + len + comma, origval);
1717 }
1718 if (comma)
1719 newval[len] = ',';
1720}
1721
1722/*
1723 * Remove a value from a string option. Copy string option value in "origval"
1724 * to "newval" and then remove the string "strval" of length "len".
1725 */
1726 static void
1727stropt_remove_val(
1728 char_u *origval,
1729 char_u *newval,
1730 int flags,
1731 char_u *strval,
1732 int len)
1733{
1734 // Remove newval[] from origval[]. (Note: "len" has been set above
1735 // and is used here).
1736 STRCPY(newval, origval);
1737 if (*strval)
1738 {
1739 // may need to remove a comma
1740 if (flags & P_COMMA)
1741 {
1742 if (strval == origval)
1743 {
1744 // include comma after string
1745 if (strval[len] == ',')
1746 ++len;
1747 }
1748 else
1749 {
1750 // include comma before string
1751 --strval;
1752 ++len;
1753 }
1754 }
1755 STRMOVE(newval + (strval - origval), strval + len);
1756 }
1757}
1758
1759/*
1760 * Remove flags that appear twice in the string option value 'newval'.
1761 */
1762 static void
1763stropt_remove_dupflags(char_u *newval, int flags)
1764{
1765 char_u *s = newval;
1766
1767 // Remove flags that appear twice.
1768 while (*s)
1769 {
1770 // if options have P_FLAGLIST and P_ONECOMMA such as 'whichwrap'
1771 if (flags & P_ONECOMMA)
1772 {
1773 if (*s != ',' && *(s + 1) == ',' && vim_strchr(s + 2, *s) != NULL)
1774 {
1775 // Remove the duplicated value and the next comma.
1776 STRMOVE(s, s + 2);
1777 continue;
1778 }
1779 }
1780 else
1781 {
1782 if ((!(flags & P_COMMA) || *s != ',')
1783 && vim_strchr(s + 1, *s) != NULL)
1784 {
1785 STRMOVE(s, s + 1);
1786 continue;
1787 }
1788 }
1789 ++s;
1790 }
1791}
1792
1793/*
1794 * Get the string value specified for a ":set" command. The following set
1795 * options are supported:
1796 * set {opt}&
1797 * set {opt}<
1798 * set {opt}={val}
1799 * set {opt}:{val}
1800 */
1801 static char_u *
1802stropt_get_newval(
1803 int nextchar,
1804 int opt_idx,
1805 char_u **argp,
1806 char_u *varp,
1807 char_u **origval_arg,
1808 char_u **origval_l_arg,
1809 char_u **origval_g_arg,
1810 char_u **oldval_arg,
1811 set_op_T *op_arg,
1812 int flags,
1813 int cp_val)
1814{
1815 char_u *arg = *argp;
1816 char_u *origval = *origval_arg;
1817 char_u *origval_l = *origval_l_arg;
1818 char_u *origval_g = *origval_g_arg;
1819 char_u *oldval = *oldval_arg;
1820 set_op_T op = *op_arg;
1821 char_u *save_arg = NULL;
1822 char_u *newval;
1823 char_u *s = NULL;
1824 char_u whichwrap[80];
1825
1826 if (nextchar == '&') // set to default val
1827 newval = stropt_get_default_val(opt_idx, varp, flags, cp_val);
1828 else if (nextchar == '<') // set to global val
1829 newval = vim_strsave(*(char_u **)get_varp_scope(
1830 &(options[opt_idx]), OPT_GLOBAL));
1831 else
1832 {
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001833 ++arg; // jump to after the '=' or ':'
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001834
1835 // Set 'keywordprg' to ":help" if an empty
1836 // value was passed to :set by the user.
1837 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
1838 {
1839 save_arg = arg;
1840 arg = (char_u *)":help";
1841 }
1842 // Convert 'backspace' number to string
1843 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1844 opt_backspace_nr2str(varp, &origval, &origval_l, &origval_g,
1845 &oldval);
1846 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
1847 {
1848 // Convert 'whichwrap' number to string, for backwards
1849 // compatibility with Vim 3.0.
1850 char_u *t = opt_whichwrap_nr2str(&arg, whichwrap);
1851 save_arg = arg;
1852 arg = t;
1853 }
1854 // Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1855 // version 3.0
1856 else if (*arg == '>' && (varp == (char_u *)&p_dir
1857 || varp == (char_u *)&p_bdir))
1858 ++arg;
1859
1860 // Copy the new string into allocated memory.
1861 newval = stropt_copy_value(origval, &arg, op, flags);
1862 if (newval == NULL)
1863 goto done;
1864
1865 // Expand environment variables and ~.
1866 // Don't do it when adding without inserting a comma.
1867 if (op == OP_NONE || (flags & P_COMMA))
1868 {
1869 newval = stropt_expand_envvar(opt_idx, origval, newval, op);
1870 if (newval == NULL)
1871 goto done;
1872 }
1873
1874 // locate newval[] in origval[] when removing it and when adding to
1875 // avoid duplicates
1876 int len = 0;
1877 if (op == OP_REMOVING || (flags & P_NODUP))
1878 {
1879 len = (int)STRLEN(newval);
1880 s = find_dup_item(origval, newval, flags);
1881
1882 // do not add if already there
1883 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
1884 {
1885 op = OP_NONE;
1886 STRCPY(newval, origval);
1887 }
1888
1889 // if no duplicate, move pointer to end of original value
1890 if (s == NULL)
1891 s = origval + (int)STRLEN(origval);
1892 }
1893
1894 // concatenate the two strings; add a ',' if needed
1895 if (op == OP_ADDING || op == OP_PREPENDING)
1896 stropt_concat_with_comma(origval, newval, op, flags);
1897 else if (op == OP_REMOVING)
1898 // Remove newval[] from origval[]. (Note: "len" has been set above
1899 // and is used here).
1900 stropt_remove_val(origval, newval, flags, s, len);
1901
1902 if (flags & P_FLAGLIST)
1903 // Remove flags that appear twice.
1904 stropt_remove_dupflags(newval, flags);
1905 }
1906
1907done:
1908 if (save_arg != NULL)
1909 arg = save_arg; // arg was temporarily changed, restore it
1910 *argp = arg;
1911 *origval_arg = origval;
1912 *origval_l_arg = origval_l;
1913 *origval_g_arg = origval_g;
1914 *oldval_arg = oldval;
1915 *op_arg = op;
1916
1917 return newval;
1918}
1919
1920/*
Bram Moolenaar47403942022-09-21 21:12:53 +01001921 * Part of do_set() for string options.
1922 * Returns FAIL on failure, do not process further options.
1923 */
1924 static int
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001925do_set_option_string(
Bram Moolenaar47403942022-09-21 21:12:53 +01001926 int opt_idx,
1927 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01001928 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01001929 int nextchar,
1930 set_op_T op_arg,
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001931 long_u flags,
Bram Moolenaar47403942022-09-21 21:12:53 +01001932 int cp_val,
1933 char_u *varp_arg,
1934 char *errbuf,
1935 int *value_checked,
1936 char **errmsg)
1937{
Bram Moolenaar6f981142022-09-22 12:48:58 +01001938 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01001939 set_op_T op = op_arg;
1940 char_u *varp = varp_arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01001941 char_u *oldval = NULL; // previous value if *varp
1942 char_u *newval;
1943 char_u *origval = NULL;
1944 char_u *origval_l = NULL;
1945 char_u *origval_g = NULL;
1946#if defined(FEAT_EVAL)
1947 char_u *saved_origval = NULL;
1948 char_u *saved_origval_l = NULL;
1949 char_u *saved_origval_g = NULL;
1950 char_u *saved_newval = NULL;
1951#endif
Bram Moolenaar47403942022-09-21 21:12:53 +01001952
1953 // When using ":set opt=val" for a global option
1954 // with a local value the local value will be
1955 // reset, use the global value here.
1956 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
1957 && ((int)options[opt_idx].indir & PV_BOTH))
1958 varp = options[opt_idx].var;
1959
1960 // The old value is kept until we are sure that the new value is valid.
1961 oldval = *(char_u **)varp;
1962
1963 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1964 {
1965 origval_l = *(char_u **)get_varp_scope(
1966 &(options[opt_idx]), OPT_LOCAL);
1967 origval_g = *(char_u **)get_varp_scope(
1968 &(options[opt_idx]), OPT_GLOBAL);
1969
1970 // A global-local string option might have an empty option as value to
1971 // indicate that the global value should be used.
1972 if (((int)options[opt_idx].indir & PV_BOTH)
1973 && origval_l == empty_option)
1974 origval_l = origval_g;
1975 }
1976
1977 // When setting the local value of a global option, the old value may be
1978 // the global value.
1979 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
1980 origval = *(char_u **)get_varp(&options[opt_idx]);
1981 else
1982 origval = oldval;
1983
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001984 // Get the new value for the option
1985 newval = stropt_get_newval(nextchar, opt_idx, &arg, varp, &origval,
1986 &origval_l, &origval_g, &oldval, &op, flags,
1987 cp_val);
Bram Moolenaar47403942022-09-21 21:12:53 +01001988
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001989 // Set the new value.
Bram Moolenaar47403942022-09-21 21:12:53 +01001990 *(char_u **)(varp) = newval;
Bram Moolenaar339e1142023-02-15 14:26:25 +00001991 if (newval == NULL)
1992 *(char_u **)(varp) = empty_option;
Bram Moolenaar47403942022-09-21 21:12:53 +01001993
1994#if defined(FEAT_EVAL)
1995 if (!starting
1996# ifdef FEAT_CRYPT
1997 && options[opt_idx].indir != PV_KEY
1998# endif
1999 && origval != NULL && newval != NULL)
2000 {
2001 // origval may be freed by did_set_string_option(), make a copy.
2002 saved_origval = vim_strsave(origval);
2003 // newval (and varp) may become invalid if the buffer is closed by
2004 // autocommands.
2005 saved_newval = vim_strsave(newval);
2006 if (origval_l != NULL)
2007 saved_origval_l = vim_strsave(origval_l);
2008 if (origval_g != NULL)
2009 saved_origval_g = vim_strsave(origval_g);
2010 }
2011#endif
2012
2013 {
2014 long_u *p = insecure_flag(opt_idx, opt_flags);
2015 int secure_saved = secure;
2016
2017 // When an option is set in the sandbox, from a modeline or in secure
2018 // mode, then deal with side effects in secure mode. Also when the
2019 // value was set with the P_INSECURE flag and is not completely
2020 // replaced.
2021 if ((opt_flags & OPT_MODELINE)
2022#ifdef HAVE_SANDBOX
2023 || sandbox != 0
2024#endif
2025 || (op != OP_NONE && (*p & P_INSECURE)))
2026 secure = 1;
2027
2028 // Handle side effects, and set the global value for ":set" on local
2029 // options. Note: when setting 'syntax' or 'filetype' autocommands may
2030 // be triggered that can cause havoc.
2031 *errmsg = did_set_string_option(
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002032 opt_idx, (char_u **)varp, oldval, newval, errbuf,
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02002033 opt_flags, op, value_checked);
Bram Moolenaar47403942022-09-21 21:12:53 +01002034
2035 secure = secure_saved;
2036 }
2037
2038#if defined(FEAT_EVAL)
2039 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00002040 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01002041 saved_origval_l, saved_origval_g, saved_newval);
2042 vim_free(saved_origval);
2043 vim_free(saved_origval_l);
2044 vim_free(saved_origval_g);
2045 vim_free(saved_newval);
2046#endif
2047
Bram Moolenaar6f981142022-09-22 12:48:58 +01002048 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002049 return *errmsg == NULL ? OK : FAIL;
2050}
2051
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002053 * Set a boolean option.
2054 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002055 */
2056 static char *
2057do_set_option_bool(
2058 int opt_idx,
2059 int opt_flags,
2060 set_prefix_T prefix,
2061 long_u flags,
2062 char_u *varp,
2063 int nextchar,
2064 int afterchar,
2065 int cp_val)
2066
2067{
2068 varnumber_T value;
2069
2070 if (nextchar == '=' || nextchar == ':')
2071 return e_invalid_argument;
Bram Moolenaar546933f2023-02-06 16:40:49 +00002072 if (opt_idx < 0 || varp == NULL)
2073 return NULL; // "cannot happen"
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002074
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002075 // ":set opt!": invert
2076 // ":set opt&": reset to default value
2077 // ":set opt<": reset to global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002078 if (nextchar == '!')
2079 value = *(int *)(varp) ^ 1;
2080 else if (nextchar == '&')
2081 value = (int)(long)(long_i)options[opt_idx].def_val[
2082 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2083 else if (nextchar == '<')
2084 {
2085 // For 'autoread' -1 means to use global value.
2086 if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL)
2087 value = -1;
2088 else
2089 value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2090 }
2091 else
2092 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002093 // ":set invopt": invert
2094 // ":set opt" or ":set noopt": set or reset
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002095 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
2096 return e_trailing_characters;
2097 if (prefix == PREFIX_INV)
2098 value = *(int *)(varp) ^ 1;
2099 else
2100 value = prefix == PREFIX_NO ? 0 : 1;
2101 }
2102
2103 return set_bool_option(opt_idx, varp, (int)value, opt_flags);
2104}
2105
2106/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002107 * Set a numeric option.
2108 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002109 */
2110 static char *
2111do_set_option_numeric(
2112 int opt_idx,
2113 int opt_flags,
2114 char_u **argp,
2115 int nextchar,
2116 set_op_T op,
2117 long_u flags,
2118 int cp_val,
2119 char_u *varp,
2120 char *errbuf,
2121 size_t errbuflen)
2122{
2123 char_u *arg = *argp;
2124 varnumber_T value;
2125 int i;
2126 char *errmsg = NULL;
2127
Bram Moolenaar546933f2023-02-06 16:40:49 +00002128 if (opt_idx < 0 || varp == NULL)
2129 return NULL; // "cannot happen"
2130 //
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002131 /*
2132 * Different ways to set a number option:
2133 * & set to default value
2134 * < set to global value
2135 * <xx> accept special key codes for 'wildchar'
2136 * c accept any non-digit for 'wildchar'
2137 * [-]0-9 set number
2138 * other error
2139 */
2140 ++arg;
2141 if (nextchar == '&')
2142 value = (long)(long_i)options[opt_idx].def_val[
2143 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2144 else if (nextchar == '<')
2145 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002146 if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL)
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002147 // for 'undolevels' NO_LOCAL_UNDOLEVEL means using the global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002148 value = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002149 else if (opt_flags == OPT_LOCAL
2150 && ((long *)varp == &curwin->w_p_siso
2151 || (long *)varp == &curwin->w_p_so))
2152 // for 'scrolloff'/'sidescrolloff' -1 means using the global value
2153 value = -1;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002154 else
2155 value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2156 }
2157 else if (((long *)varp == &p_wc || (long *)varp == &p_wcm)
2158 && (*arg == '<'
2159 || *arg == '^'
2160 || (*arg != NUL
2161 && (!arg[1] || VIM_ISWHITE(arg[1]))
2162 && !VIM_ISDIGIT(*arg))))
2163 {
2164 value = string_to_key(arg, FALSE);
2165 if (value == 0 && (long *)varp != &p_wcm)
2166 {
2167 errmsg = e_invalid_argument;
2168 goto skip;
2169 }
2170 }
2171 else if (*arg == '-' || VIM_ISDIGIT(*arg))
2172 {
2173 // Allow negative (for 'undolevels'), octal and hex numbers.
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002174 vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE, NULL);
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002175 if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i])))
2176 {
2177 errmsg = e_number_required_after_equal;
2178 goto skip;
2179 }
2180 }
2181 else
2182 {
2183 errmsg = e_number_required_after_equal;
2184 goto skip;
2185 }
2186
2187 if (op == OP_ADDING)
2188 value = *(long *)varp + value;
2189 else if (op == OP_PREPENDING)
2190 value = *(long *)varp * value;
2191 else if (op == OP_REMOVING)
2192 value = *(long *)varp - value;
2193
2194 errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen,
2195 opt_flags);
2196
2197skip:
2198 *argp = arg;
2199 return errmsg;
2200}
2201
2202/*
2203 * Set a key code (t_xx) option
2204 */
2205 static char *
2206do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar)
2207{
2208 char_u *arg = *argp;
2209 char_u *p;
2210
2211 if (nextchar == '&')
2212 {
2213 if (add_termcap_entry(key_name, TRUE) == FAIL)
2214 return e_not_found_in_termcap;
2215 }
2216 else
2217 {
2218 ++arg; // jump to after the '=' or ':'
2219 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
2220 if (*p == '\\' && p[1] != NUL)
2221 ++p;
2222 nextchar = *p;
2223 *p = NUL;
2224 add_termcode(key_name, arg, FALSE);
2225 *p = nextchar;
2226 }
2227 if (full_screen)
2228 ttest(FALSE);
2229 redraw_all_later(UPD_CLEAR);
2230
2231 *argp = arg;
2232 return NULL;
2233}
2234
2235/*
2236 * Set an option to a new value.
2237 */
2238 static char *
2239do_set_option_value(
2240 int opt_idx,
2241 int opt_flags,
2242 char_u **argp,
2243 set_prefix_T prefix,
2244 set_op_T op,
2245 long_u flags,
2246 char_u *varp,
2247 char_u *key_name,
2248 int nextchar,
2249 int afterchar,
2250 int cp_val,
2251 int *stopopteval,
2252 char *errbuf,
2253 size_t errbuflen)
2254{
2255 int value_checked = FALSE;
2256 char *errmsg = NULL;
2257 char_u *arg = *argp;
2258
2259 if (flags & P_BOOL)
2260 {
2261 // boolean option
2262 errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp,
2263 nextchar, afterchar, cp_val);
2264 if (errmsg != NULL)
2265 goto skip;
2266 }
2267 else
2268 {
2269 // numeric or string option
2270 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2271 || prefix != PREFIX_NONE)
2272 {
2273 errmsg = e_invalid_argument;
2274 goto skip;
2275 }
2276
2277 if (flags & P_NUM)
2278 {
2279 // numeric option
2280 errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar,
2281 op, flags, cp_val, varp,
2282 errbuf, errbuflen);
2283 if (errmsg != NULL)
2284 goto skip;
2285 }
2286 else if (opt_idx >= 0)
2287 {
2288 // string option
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002289 if (do_set_option_string(opt_idx, opt_flags, &arg, nextchar, op,
2290 flags, cp_val, varp, errbuf,
2291 &value_checked, &errmsg) == FAIL)
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002292 {
2293 if (errmsg != NULL)
2294 goto skip;
2295 *stopopteval = TRUE;
2296 goto skip;
2297 }
2298 }
2299 else
2300 {
2301 // key code option
2302 errmsg = do_set_option_keycode(&arg, key_name, nextchar);
2303 if (errmsg != NULL)
2304 goto skip;
2305 }
2306 }
2307
2308 if (opt_idx >= 0)
2309 did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked);
2310
2311skip:
2312 *argp = arg;
2313 return errmsg;
2314}
2315
2316/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002317 * Set an option to a new value.
2318 * Return NULL if OK, return an untranslated error message when something is
2319 * wrong. "errbuf[errbuflen]" can be used to create the error message.
2320 */
2321 static char *
2322do_set_option(
2323 int opt_flags,
2324 char_u **argp,
2325 char_u *arg_start,
2326 char_u **startarg,
2327 int *did_show,
2328 int *stopopteval,
2329 char *errbuf,
2330 size_t errbuflen)
2331{
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002332 int opt_idx;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002333 char_u *arg;
2334 set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix
2335 set_op_T op;
2336 long_u flags; // flags for current option
2337 char_u *varp; // pointer to variable for current option
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002338 char_u key_name[2];
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002339 int nextchar; // next non-white char after option name
2340 int afterchar; // character just after option name
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002341 char *errmsg = NULL;
2342 int key;
2343 int len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002344
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002345 prefix = get_option_prefix(argp);
2346 arg = *argp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002347
2348 // find end of name
2349 key = 0;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002350 if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL)
2351 return e_invalid_argument;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002352
2353 // remember character after option name
2354 afterchar = arg[len];
2355
2356 if (in_vim9script())
2357 {
2358 char_u *p = skipwhite(arg + len);
2359
2360 // disallow white space before =val, +=val, -=val, ^=val
2361 if (p > arg + len && (p[0] == '='
2362 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
2363 && p[1] == '=')))
2364 {
2365 errmsg = e_no_white_space_allowed_between_option_and;
2366 arg = p;
2367 *startarg = p;
2368 goto skip;
2369 }
2370 }
2371 else
2372 // skip white space, allow ":set ai ?", ":set hlsearch !"
2373 while (VIM_ISWHITE(arg[len]))
2374 ++len;
2375
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002376 op = get_opt_op(arg + len);
2377 if (op != OP_NONE)
2378 len++;
2379
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002380 nextchar = arg[len];
2381
2382 if (opt_idx == -1 && key == 0) // found a mismatch: skip
2383 {
2384 if (in_vim9script() && arg > arg_start
2385 && vim_strchr((char_u *)"!&<", *arg) != NULL)
2386 errmsg = e_no_white_space_allowed_between_option_and;
2387 else
2388 errmsg = e_unknown_option;
2389 goto skip;
2390 }
2391
2392 if (opt_idx >= 0)
2393 {
2394 if (options[opt_idx].var == NULL) // hidden option: skip
2395 {
2396 // Only give an error message when requesting the value of
2397 // a hidden option, ignore setting it.
2398 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
2399 && (!(options[opt_idx].flags & P_BOOL)
2400 || nextchar == '?'))
2401 errmsg = e_option_not_supported;
2402 goto skip;
2403 }
2404
2405 flags = options[opt_idx].flags;
2406 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
2407 }
2408 else
2409 {
2410 flags = P_STRING;
Bram Moolenaar40b48722023-02-05 17:04:50 +00002411 varp = NULL;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002412 if (key < 0)
2413 {
2414 key_name[0] = KEY2TERMCAP0(key);
2415 key_name[1] = KEY2TERMCAP1(key);
2416 }
2417 else
2418 {
2419 key_name[0] = KS_KEY;
2420 key_name[1] = (key & 0xff);
2421 }
2422 }
2423
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002424 // Make sure the option value can be changed.
2425 if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002426 goto skip;
2427
Bram Moolenaar40b48722023-02-05 17:04:50 +00002428 int cp_val = p_cp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002429 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
2430 {
2431 arg += len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002432 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
2433 {
2434 if (arg[3] == 'm') // "opt&vim": set to Vim default
2435 {
2436 cp_val = FALSE;
2437 arg += 3;
2438 }
2439 else // "opt&vi": set to Vi default
2440 {
2441 cp_val = TRUE;
2442 arg += 2;
2443 }
2444 }
2445 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
2446 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
2447 {
2448 errmsg = e_trailing_characters;
2449 goto skip;
2450 }
2451 }
2452
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002453 // Allow '=' and ':' for historical reasons (MSDOS command.com).
2454 // Allows only one '=' character per "set" command line. grrr. (jw)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002455 if (nextchar == '?'
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002456 || (prefix == PREFIX_NONE
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002457 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
2458 && !(flags & P_BOOL)))
2459 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002460 // print value
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002461 if (*did_show)
2462 msg_putchar('\n'); // cursor below last one
2463 else
2464 {
2465 gotocmdline(TRUE); // cursor at status line
2466 *did_show = TRUE; // remember that we did a line
2467 }
2468 if (opt_idx >= 0)
2469 {
2470 showoneopt(&options[opt_idx], opt_flags);
2471#ifdef FEAT_EVAL
2472 if (p_verbose > 0)
2473 {
2474 // Mention where the option was last set.
2475 if (varp == options[opt_idx].var)
2476 last_set_msg(options[opt_idx].script_ctx);
2477 else if ((int)options[opt_idx].indir & PV_WIN)
2478 last_set_msg(curwin->w_p_script_ctx[
2479 (int)options[opt_idx].indir & PV_MASK]);
2480 else if ((int)options[opt_idx].indir & PV_BUF)
2481 last_set_msg(curbuf->b_p_script_ctx[
2482 (int)options[opt_idx].indir & PV_MASK]);
2483 }
2484#endif
2485 }
2486 else
2487 {
2488 char_u *p;
2489
2490 p = find_termcode(key_name);
2491 if (p == NULL)
2492 {
2493 errmsg = e_key_code_not_set;
2494 goto skip;
2495 }
2496 else
2497 (void)show_one_termcode(key_name, p, TRUE);
2498 }
2499 if (nextchar != '?'
2500 && nextchar != NUL && !VIM_ISWHITE(afterchar))
2501 errmsg = e_trailing_characters;
2502 }
2503 else
2504 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002505 errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op,
2506 flags, varp, key_name, nextchar,
2507 afterchar, cp_val, stopopteval, errbuf,
2508 errbuflen);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002509 }
2510
2511skip:
2512 *argp = arg;
2513 return errmsg;
2514}
2515
2516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 * Parse 'arg' for option settings.
2518 *
2519 * 'arg' may be IObuff, but only when no errors can be present and option
2520 * does not need to be expanded with option_expand().
2521 * "opt_flags":
2522 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002523 * OPT_GLOBAL for ":setglobal"
2524 * OPT_LOCAL for ":setlocal" and a modeline
2525 * OPT_MODELINE for a modeline
2526 * OPT_WINONLY to only set window-local options
2527 * OPT_NOWIN to skip setting window-local options
2528 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002530 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 */
2532 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002533do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002534 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002535 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002537 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002539 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540
2541 if (*arg == NUL)
2542 {
2543 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002544 did_show = TRUE;
2545 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 }
2547
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002548 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002550 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002551 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002553 // ":set all" show all options.
2554 // ":set all&" set all options to their default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 arg += 3;
2556 if (*arg == '&')
2557 {
2558 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002559 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002561 didset_options();
2562 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002563 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 }
2565 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002566 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002568 did_show = TRUE;
2569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002571 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 {
2573 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002574 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002575 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 arg += 7;
2577 }
2578 else
2579 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002580 int stopopteval = FALSE;
2581 char *errmsg = NULL;
2582 char errbuf[80];
2583 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002585 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2586 &did_show, &stopopteval, errbuf,
2587 sizeof(errbuf));
2588 if (stopopteval)
2589 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002591 // Advance to next argument.
2592 // - skip until a blank found, taking care of backslashes
2593 // - skip blanks
2594 // - skip one "=val" argument (for hidden options ":set gfn =xx")
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 for (i = 0; i < 2 ; ++i)
2596 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002597 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 if (*arg++ == '\\' && *arg != NUL)
2599 ++arg;
2600 arg = skipwhite(arg);
2601 if (*arg != '=')
2602 break;
2603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002605 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002607 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
2608 i = (int)STRLEN(IObuff) + 2;
2609 if (i + (arg - startarg) < IOSIZE)
2610 {
2611 // append the argument with the error
2612 STRCAT(IObuff, ": ");
2613 mch_memmove(IObuff + i, startarg, (arg - startarg));
2614 IObuff[i + (arg - startarg)] = NUL;
2615 }
2616 // make sure all characters are printable
2617 trans_characters(IObuff, IOSIZE);
2618
2619 ++no_wait_return; // wait_return() done later
2620 emsg((char *)IObuff); // show error highlighted
2621 --no_wait_return;
2622
2623 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 }
2626
2627 arg = skipwhite(arg);
2628 }
2629
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002630theend:
2631 if (silent_mode && did_show)
2632 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002633 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002634 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002635 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002636 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002637 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002638 out_flush();
2639 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002640 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002641 }
2642
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 return OK;
2644}
2645
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002646/*
2647 * Call this when an option has been given a new value through a user command.
2648 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2649 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002650 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002651did_set_option(
2652 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002653 int opt_flags, // possibly with OPT_MODELINE
2654 int new_value, // value was replaced completely
2655 int value_checked) // value was checked to be safe, no need to set the
2656 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002657{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002658 long_u *p;
2659
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002660 options[opt_idx].flags |= P_WAS_SET;
2661
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002662 // When an option is set in the sandbox, from a modeline or in secure mode
2663 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2664 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002665 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002666 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002667#ifdef HAVE_SANDBOX
2668 || sandbox != 0
2669#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002670 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002671 *p = *p | P_INSECURE;
2672 else if (new_value)
2673 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002674}
2675
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676/*
2677 * Convert a key name or string into a key value.
2678 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002679 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002681 int
2682string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683{
2684 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002685 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 if (*arg == '^')
2687 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002688 if (multi_byte)
2689 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 return *arg;
2691}
2692
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693/*
2694 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2695 * maketitle() to create and display it.
2696 * When switching the title or icon off, call mch_restore_title() to get
2697 * the old value back.
2698 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002699 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002700did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701{
2702 if (starting != NO_SCREEN
2703#ifdef FEAT_GUI
2704 && !gui.starting
2705#endif
2706 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709
2710/*
2711 * set_options_bin - called when 'bin' changes value.
2712 */
2713 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002714set_options_bin(
2715 int oldval,
2716 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002717 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002719 // The option values that are changed when 'bin' changes are
2720 // copied when 'bin is set and restored when 'bin' is reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 if (newval)
2722 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002723 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 {
2725 if (!(opt_flags & OPT_GLOBAL))
2726 {
2727 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2728 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2729 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2730 curbuf->b_p_et_nobin = curbuf->b_p_et;
2731 }
2732 if (!(opt_flags & OPT_LOCAL))
2733 {
2734 p_tw_nobin = p_tw;
2735 p_wm_nobin = p_wm;
2736 p_ml_nobin = p_ml;
2737 p_et_nobin = p_et;
2738 }
2739 }
2740
2741 if (!(opt_flags & OPT_GLOBAL))
2742 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002743 curbuf->b_p_tw = 0; // no automatic line wrap
2744 curbuf->b_p_wm = 0; // no automatic line wrap
2745 curbuf->b_p_ml = 0; // no modelines
2746 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 }
2748 if (!(opt_flags & OPT_LOCAL))
2749 {
2750 p_tw = 0;
2751 p_wm = 0;
2752 p_ml = FALSE;
2753 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002754 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 }
2756 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002757 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 {
2759 if (!(opt_flags & OPT_GLOBAL))
2760 {
2761 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2762 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2763 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2764 curbuf->b_p_et = curbuf->b_p_et_nobin;
2765 }
2766 if (!(opt_flags & OPT_LOCAL))
2767 {
2768 p_tw = p_tw_nobin;
2769 p_wm = p_wm_nobin;
2770 p_ml = p_ml_nobin;
2771 p_et = p_et_nobin;
2772 }
2773 }
Christian Brabandt757593c2023-08-22 21:44:10 +02002774#if defined(FEAT_EVAL) || defined(PROTO)
2775 // Remember where the dependent option were reset
2776 didset_options_sctx(opt_flags, p_bin_dep_opts);
2777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778}
2779
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780/*
2781 * Expand environment variables for some string options.
2782 * These string options cannot be indirect!
2783 * If "val" is NULL expand the current value of the option.
2784 * Return pointer to NameBuff, or NULL when not expanded.
2785 */
2786 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002787option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002789 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2791 return NULL;
2792
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002793 // If val is longer than MAXPATHL no meaningful expansion can be done,
2794 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 if (val != NULL && STRLEN(val) > MAXPATHL)
2796 return NULL;
2797
2798 if (val == NULL)
2799 val = *(char_u **)options[opt_idx].var;
2800
2801 /*
2802 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2803 * Escape spaces when expanding 'tags', they are used to separate file
2804 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002805 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 */
2807 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002808 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002809#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002810 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2811#endif
2812 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002813 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 return NULL;
2815
2816 return NameBuff;
2817}
2818
2819/*
2820 * After setting various option values: recompute variables that depend on
2821 * option values.
2822 */
2823 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002824didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002826 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 (void)init_chartab();
2828
Bram Moolenaardac13472019-09-16 21:06:21 +02002829 didset_string_options();
2830
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002831#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002832 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002833 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002834 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002835 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002836#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002837 // set cedit_key
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002838 (void)did_set_cedit(NULL);
Bram Moolenaar597a4222014-06-25 14:39:50 +02002839#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002840 // initialize the table for 'breakat'.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002841 did_set_breakat(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002842#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002843 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002844}
2845
2846/*
2847 * More side effects of setting options.
2848 */
2849 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002850didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002851{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002852 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002853 (void)highlight_changed();
2854
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002855 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002856 check_opt_wim();
2857
Bram Moolenaareed9d462021-02-15 20:38:25 +01002858 // Parse default for 'listchars'.
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002859 (void)set_listchars_option(curwin, curwin->w_p_lcs, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002860
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002861 // Parse default for 'fillchars'.
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002862 (void)set_fillchars_option(curwin, curwin->w_p_fcs, TRUE);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002863
2864#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002865 // Parse default for 'clipboard'
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002866 (void)did_set_clipboard(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002867#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002868#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002869 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002870 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002871 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002872 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874}
2875
2876/*
2877 * Check for string options that are NULL (normally only termcap options).
2878 */
2879 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002880check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881{
2882 int opt_idx;
2883
2884 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2885 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2886 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2887}
2888
2889/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002890 * Return the option index found by a pointer into term_strings[].
2891 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002893 int
2894get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002896 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897
2898 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2899 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002900 return opt_idx;
2901 return -1; // cannot happen: didn't find it!
2902}
2903
2904/*
2905 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2906 * Return the option index or -1 if not found.
2907 */
2908 int
2909set_term_option_alloced(char_u **p)
2910{
2911 int opt_idx = get_term_opt_idx(p);
2912
2913 if (opt_idx >= 0)
2914 options[opt_idx].flags |= P_ALLOCED;
2915 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916}
2917
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002918#if defined(FEAT_EVAL) || defined(PROTO)
2919/*
2920 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2921 * Return FALSE when it wasn't.
2922 * Return -1 for an unknown option.
2923 */
2924 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002925was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002926{
2927 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002928 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002929
2930 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002931 {
2932 flagp = insecure_flag(idx, opt_flags);
2933 return (*flagp & P_INSECURE) != 0;
2934 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002935 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002936 return -1;
2937}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002938
2939/*
2940 * Get a pointer to the flags used for the P_INSECURE flag of option
2941 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002942 * NOTE: Caller must make sure that "curwin" is set to the window from which
2943 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002944 */
2945 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002946insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002947{
2948 if (opt_flags & OPT_LOCAL)
2949 switch ((int)options[opt_idx].indir)
2950 {
2951#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002952 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002953#endif
2954#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002955# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002956 case PV_FDE: return &curwin->w_p_fde_flags;
2957 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002958# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002959# ifdef FEAT_BEVAL
2960 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2961# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002962 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002963 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002964# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002965 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002966# endif
2967#endif
2968 }
2969
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002970 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002971 return &options[opt_idx].flags;
2972}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002973#endif
2974
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002975/*
2976 * Redraw the window title and/or tab page text later.
2977 */
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02002978 void
2979redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002980{
2981 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002982 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002983}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002984
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002986 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2987 * characters or characters in "allowed".
2988 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002989 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002990valid_name(char_u *val, char *allowed)
2991{
2992 char_u *s;
2993
2994 for (s = val; *s != NUL; ++s)
2995 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2996 return FALSE;
2997 return TRUE;
2998}
2999
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003000#if defined(FEAT_EVAL) || defined(PROTO)
3001/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003002 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003003 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003004 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003005 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003006set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003007{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003008 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
3009 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003010 sctx_T new_script_ctx = script_ctx;
3011
Bram Moolenaar51258742020-05-03 17:19:33 +02003012 // Modeline already has the line number set.
3013 if (!(opt_flags & OPT_MODELINE))
3014 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003015
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003016 // Remember where the option was set. For local options need to do that
3017 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003018 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003019 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003020 if (both || (opt_flags & OPT_LOCAL))
3021 {
3022 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003023 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003024 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00003025 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003026 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00003027 if (both)
3028 // also setting the "all buffers" value
3029 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
3030 new_script_ctx;
3031 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003032 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003033}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003034
3035/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00003036 * Get the script context of global option "name".
3037 *
3038 */
3039 sctx_T *
3040get_option_sctx(char *name)
3041{
3042 int idx = findoption((char_u *)name);
3043
3044 if (idx >= 0)
3045 return &options[idx].script_ctx;
3046 siemsg("no such option: %s", name);
3047 return NULL;
3048}
3049
3050/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003051 * Set the script_ctx for a termcap option.
3052 * "name" must be the two character code, e.g. "RV".
3053 * When "name" is NULL use "opt_idx".
3054 */
3055 void
3056set_term_option_sctx_idx(char *name, int opt_idx)
3057{
3058 char_u buf[5];
3059 int idx;
3060
3061 if (name == NULL)
3062 idx = opt_idx;
3063 else
3064 {
3065 buf[0] = 't';
3066 buf[1] = '_';
3067 buf[2] = name[0];
3068 buf[3] = name[1];
3069 buf[4] = 0;
3070 idx = findoption(buf);
3071 }
3072 if (idx >= 0)
3073 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
3074}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003075#endif
3076
Bram Moolenaarf4140482020-02-15 23:06:45 +01003077#if defined(FEAT_EVAL)
3078/*
3079 * Apply the OptionSet autocommand.
3080 */
3081 static void
3082apply_optionset_autocmd(
3083 int opt_idx,
3084 long opt_flags,
3085 long oldval,
3086 long oldval_g,
3087 long newval,
3088 char *errmsg)
3089{
3090 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
3091
3092 // Don't do this while starting up, failure or recursively.
3093 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
3094 return;
3095
3096 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
3097 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
3098 oldval_g);
3099 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
3100 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
3101 (opt_flags & OPT_LOCAL) ? "local" : "global");
3102 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3103 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3104 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
3105 if (opt_flags & OPT_LOCAL)
3106 {
3107 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3108 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3109 }
3110 if (opt_flags & OPT_GLOBAL)
3111 {
3112 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3113 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3114 }
3115 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3116 {
3117 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3118 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3119 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3120 }
3121 if (opt_flags & OPT_MODELINE)
3122 {
3123 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3124 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3125 }
3126 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3127 NULL, FALSE, NULL);
3128 reset_v_option_vars();
3129}
3130#endif
3131
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003132#if defined(FEAT_ARABIC) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003133/*
3134 * Process the updated 'arabic' option value.
3135 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003136 char *
3137did_set_arabic(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003138{
3139 char *errmsg = NULL;
3140
3141 if (curwin->w_p_arab)
3142 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003143 // 'arabic' is set, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003144 if (!p_tbidi)
3145 {
3146 // set rightleft mode
3147 if (!curwin->w_p_rl)
3148 {
3149 curwin->w_p_rl = TRUE;
3150 changed_window_setting();
3151 }
3152
3153 // Enable Arabic shaping (major part of what Arabic requires)
3154 if (!p_arshape)
3155 {
3156 p_arshape = TRUE;
3157 redraw_later_clear();
3158 }
3159 }
3160
3161 // Arabic requires a utf-8 encoding, inform the user if it's not
3162 // set.
3163 if (STRCMP(p_enc, "utf-8") != 0)
3164 {
3165 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3166
3167 msg_source(HL_ATTR(HLF_W));
3168 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
3169#ifdef FEAT_EVAL
3170 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3171#endif
3172 }
3173
3174 // set 'delcombine'
3175 p_deco = TRUE;
3176
3177# ifdef FEAT_KEYMAP
3178 // Force-set the necessary keymap for arabic
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003179 errmsg = set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3180 OPT_LOCAL);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003181# endif
3182 }
3183 else
3184 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003185 // 'arabic' is reset, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003186 if (!p_tbidi)
3187 {
3188 // reset rightleft mode
3189 if (curwin->w_p_rl)
3190 {
3191 curwin->w_p_rl = FALSE;
3192 changed_window_setting();
3193 }
3194
3195 // 'arabicshape' isn't reset, it is a global option and
3196 // another window may still need it "on".
3197 }
3198
3199 // 'delcombine' isn't reset, it is a global option and another
3200 // window may still want it "on".
3201
3202# ifdef FEAT_KEYMAP
3203 // Revert to the default keymap
3204 curbuf->b_p_iminsert = B_IMODE_NONE;
3205 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3206# endif
3207 }
3208
3209 return errmsg;
3210}
3211#endif
3212
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003213#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
3214/*
3215 * Process the updated 'autochdir' option value.
3216 */
3217 char *
3218did_set_autochdir(optset_T *args UNUSED)
3219{
3220 // Change directories when the 'acd' option is set now.
3221 DO_AUTOCHDIR;
3222 return NULL;
3223}
3224#endif
3225
3226#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
3227/*
3228 * Process the updated 'ballooneval' option value.
3229 */
3230 char *
3231did_set_ballooneval(optset_T *args)
3232{
3233 if (balloonEvalForTerm)
3234 return NULL;
3235
3236 if (p_beval && !args->os_oldval.boolean)
3237 gui_mch_enable_beval_area(balloonEval);
3238 else if (!p_beval && args->os_oldval.boolean)
3239 gui_mch_disable_beval_area(balloonEval);
3240
3241 return NULL;
3242}
3243#endif
3244
3245#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
3246/*
3247 * Process the updated 'balloonevalterm' option value.
3248 */
3249 char *
3250did_set_balloonevalterm(optset_T *args UNUSED)
3251{
3252 mch_bevalterm_changed();
3253 return NULL;
3254}
3255#endif
3256
3257/*
3258 * Process the updated 'binary' option value.
3259 */
3260 char *
3261did_set_binary(optset_T *args)
3262{
3263 // when 'bin' is set also set some other options
3264 set_options_bin(args->os_oldval.boolean, curbuf->b_p_bin, args->os_flags);
3265 redraw_titles();
3266
3267 return NULL;
3268}
3269
3270#if defined(FEAT_LINEBREAK) || defined(PROTO)
3271/*
3272 * Called when the 'breakat' option changes value.
3273 */
3274 char *
3275did_set_breakat(optset_T *args UNUSED)
3276{
3277 char_u *p;
3278 int i;
3279
3280 for (i = 0; i < 256; i++)
3281 breakat_flags[i] = FALSE;
3282
3283 if (p_breakat != NULL)
3284 for (p = p_breakat; *p; p++)
3285 breakat_flags[*p] = TRUE;
3286
3287 return NULL;
3288}
3289#endif
3290
3291/*
3292 * Process the updated 'buflisted' option value.
3293 */
3294 char *
3295did_set_buflisted(optset_T *args)
3296{
3297 // when 'buflisted' changes, trigger autocommands
3298 if (args->os_oldval.boolean != curbuf->b_p_bl)
3299 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3300 NULL, NULL, TRUE, curbuf);
3301 return NULL;
3302}
3303
3304/*
3305 * Process the new 'cmdheight' option value.
3306 */
3307 char *
3308did_set_cmdheight(optset_T *args)
3309{
3310 long old_value = args->os_oldval.number;
3311 char *errmsg = NULL;
3312
3313 // if p_ch changed value, change the command line height
3314 if (p_ch < 1)
3315 {
3316 errmsg = e_argument_must_be_positive;
3317 p_ch = 1;
3318 }
3319 if (p_ch > Rows - min_rows() + 1)
3320 p_ch = Rows - min_rows() + 1;
3321
3322 // Only compute the new window layout when startup has been
3323 // completed. Otherwise the frame sizes may be wrong.
3324 if ((p_ch != old_value
3325 || tabline_height() + topframe->fr_height != Rows - p_ch)
3326 && full_screen
3327#ifdef FEAT_GUI
3328 && !gui.starting
3329#endif
3330 )
3331 command_height();
3332
3333 return errmsg;
3334}
3335
3336/*
3337 * Process the updated 'compatible' option value.
3338 */
3339 char *
3340did_set_compatible(optset_T *args UNUSED)
3341{
3342 compatible_set();
3343 return NULL;
3344}
3345
3346#if defined(FEAT_CONCEAL) || defined(PROTO)
3347/*
3348 * Process the new 'conceallevel' option value.
3349 */
3350 char *
3351did_set_conceallevel(optset_T *args UNUSED)
3352{
3353 char *errmsg = NULL;
3354
3355 if (curwin->w_p_cole < 0)
3356 {
3357 errmsg = e_argument_must_be_positive;
3358 curwin->w_p_cole = 0;
3359 }
3360 else if (curwin->w_p_cole > 3)
3361 {
3362 errmsg = e_invalid_argument;
3363 curwin->w_p_cole = 3;
3364 }
3365
3366 return errmsg;
3367}
3368#endif
3369
3370#if defined(FEAT_DIFF) || defined(PROTO)
3371/*
3372 * Process the updated 'diff' option value.
3373 */
3374 char *
3375did_set_diff(optset_T *args UNUSED)
3376{
3377 // May add or remove the buffer from the list of diff buffers.
3378 diff_buf_adjust(curwin);
3379# ifdef FEAT_FOLDING
3380 if (foldmethodIsDiff(curwin))
3381 foldUpdateAll(curwin);
3382# endif
3383 return NULL;
3384}
3385#endif
3386
3387/*
3388 * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb'
3389 * option value.
3390 */
3391 char *
3392did_set_eof_eol_fixeol_bomb(optset_T *args UNUSED)
3393{
3394 // redraw the window title and tab page text
3395 redraw_titles();
3396 return NULL;
3397}
3398
3399/*
3400 * Process the updated 'equalalways' option value.
3401 */
3402 char *
3403did_set_equalalways(optset_T *args)
3404{
3405 if (p_ea && !args->os_oldval.boolean)
3406 win_equal(curwin, FALSE, 0);
3407
3408 return NULL;
3409}
3410
3411#if defined(FEAT_FOLDING) || defined(PROTO)
3412/*
3413 * Process the new 'foldcolumn' option value.
3414 */
3415 char *
3416did_set_foldcolumn(optset_T *args UNUSED)
3417{
3418 char *errmsg = NULL;
3419
3420 if (curwin->w_p_fdc < 0)
3421 {
3422 errmsg = e_argument_must_be_positive;
3423 curwin->w_p_fdc = 0;
3424 }
3425 else if (curwin->w_p_fdc > 12)
3426 {
3427 errmsg = e_invalid_argument;
3428 curwin->w_p_fdc = 12;
3429 }
3430
3431 return errmsg;
3432}
3433
3434/*
3435 * Process the new 'foldlevel' option value.
3436 */
3437 char *
3438did_set_foldlevel(optset_T *args UNUSED)
3439{
3440 if (curwin->w_p_fdl < 0)
3441 curwin->w_p_fdl = 0;
3442 newFoldLevel();
3443 return NULL;
3444}
3445
3446/*
3447 * Process the new 'foldminlines' option value.
3448 */
3449 char *
3450did_set_foldminlines(optset_T *args UNUSED)
3451{
3452 foldUpdateAll(curwin);
3453 return NULL;
3454}
3455
3456/*
3457 * Process the new 'foldnestmax' option value.
3458 */
3459 char *
3460did_set_foldnestmax(optset_T *args UNUSED)
3461{
3462 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3463 foldUpdateAll(curwin);
3464 return NULL;
3465}
3466#endif
3467
3468#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
3469/*
3470 * Process the updated 'hlsearch' option value.
3471 */
3472 char *
3473did_set_hlsearch(optset_T *args UNUSED)
3474{
3475 // when 'hlsearch' is set or reset: reset no_hlsearch
3476 set_no_hlsearch(FALSE);
3477 return NULL;
3478}
3479#endif
3480
3481/*
3482 * Process the updated 'ignorecase' option value.
3483 */
3484 char *
3485did_set_ignorecase(optset_T *args UNUSED)
3486{
3487 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
3488 if (p_hls)
3489 redraw_all_later(UPD_SOME_VALID);
3490 return NULL;
3491}
3492
3493#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
3494/*
3495 * Process the updated 'imdisable' option value.
3496 */
3497 char *
3498did_set_imdisable(optset_T *args UNUSED)
3499{
3500 // Only de-activate it here, it will be enabled when changing mode.
3501 if (p_imdisable)
3502 im_set_active(FALSE);
3503 else if (State & MODE_INSERT)
3504 // When the option is set from an autocommand, it may need to take
3505 // effect right away.
3506 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
3507 return NULL;
3508}
3509#endif
3510
3511/*
3512 * Process the new 'iminsert' option value.
3513 */
3514 char *
3515did_set_iminsert(optset_T *args UNUSED)
3516{
3517 char *errmsg = NULL;
3518
3519 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3520 {
3521 errmsg = e_invalid_argument;
3522 curbuf->b_p_iminsert = B_IMODE_NONE;
3523 }
3524 p_iminsert = curbuf->b_p_iminsert;
3525 if (termcap_active) // don't do this in the alternate screen
3526 showmode();
3527#if defined(FEAT_KEYMAP)
3528 // Show/unshow value of 'keymap' in status lines.
3529 status_redraw_curbuf();
3530#endif
3531
3532 return errmsg;
3533}
3534
3535/*
3536 * Process the new 'imsearch' option value.
3537 */
3538 char *
3539did_set_imsearch(optset_T *args UNUSED)
3540{
3541 char *errmsg = NULL;
3542
3543 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3544 {
3545 errmsg = e_invalid_argument;
3546 curbuf->b_p_imsearch = B_IMODE_NONE;
3547 }
3548 p_imsearch = curbuf->b_p_imsearch;
3549
3550 return errmsg;
3551}
3552
3553#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3554/*
3555 * Process the new 'imstyle' option value.
3556 */
3557 char *
3558did_set_imstyle(optset_T *args UNUSED)
3559{
3560 char *errmsg = NULL;
3561
3562 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3563 errmsg = e_invalid_argument;
3564
3565 return errmsg;
3566}
3567#endif
3568
3569/*
3570 * Process the updated 'insertmode' option value.
3571 */
3572 char *
3573did_set_insertmode(optset_T *args)
3574{
3575 // when 'insertmode' is set from an autocommand need to do work here
3576 if (p_im)
3577 {
3578 if ((State & MODE_INSERT) == 0)
3579 need_start_insertmode = TRUE;
3580 stop_insert_mode = FALSE;
3581 }
3582 // only reset if it was set previously
3583 else if (args->os_oldval.boolean)
3584 {
3585 need_start_insertmode = FALSE;
3586 stop_insert_mode = TRUE;
3587 if (restart_edit != 0 && mode_displayed)
3588 clear_cmdline = TRUE; // remove "(insert)"
3589 restart_edit = 0;
3590 }
3591
3592 return NULL;
3593}
3594
3595#if defined(FEAT_LANGMAP) || defined(PROTO)
3596/*
3597 * Process the updated 'langnoremap' option value.
3598 */
3599 char *
3600did_set_langnoremap(optset_T *args UNUSED)
3601{
3602 // 'langnoremap' -> !'langremap'
3603 p_lrm = !p_lnr;
3604 return NULL;
3605}
3606
3607/*
3608 * Process the updated 'langremap' option value.
3609 */
3610 char *
3611did_set_langremap(optset_T *args UNUSED)
3612{
3613 // 'langremap' -> !'langnoremap'
3614 p_lnr = !p_lrm;
3615 return NULL;
3616}
3617#endif
3618
3619/*
3620 * Process the new 'laststatus' option value.
3621 */
3622 char *
3623did_set_laststatus(optset_T *args UNUSED)
3624{
3625 last_status(FALSE); // (re)set last window status line
3626 return NULL;
3627}
3628
3629#if defined(FEAT_GUI) || defined(PROTO)
3630/*
3631 * Process the new 'linespace' option value.
3632 */
3633 char *
3634did_set_linespace(optset_T *args UNUSED)
3635{
3636 // Recompute gui.char_height and resize the Vim window to keep the
3637 // same number of lines.
3638 if (gui.in_use && gui_mch_adjust_charheight() == OK)
3639 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
3640 return NULL;
3641}
3642#endif
3643
3644/*
3645 * Process the updated 'lisp' option value.
3646 */
3647 char *
3648did_set_lisp(optset_T *args UNUSED)
3649{
3650 // When 'lisp' option changes include/exclude '-' in keyword characters.
3651 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
3652 return NULL;
3653}
3654
3655/*
3656 * Process the new 'maxcombine' option value.
3657 */
3658 char *
3659did_set_maxcombine(optset_T *args UNUSED)
3660{
3661 if (p_mco > MAX_MCO)
3662 p_mco = MAX_MCO;
3663 else if (p_mco < 0)
3664 p_mco = 0;
3665 screenclear(); // will re-allocate the screen
3666 return NULL;
3667}
3668
3669/*
3670 * Process the updated 'modifiable' option value.
3671 */
3672 char *
3673did_set_modifiable(optset_T *args UNUSED)
3674{
3675 // when 'modifiable' is changed, redraw the window title
3676
3677# ifdef FEAT_TERMINAL
3678 // Cannot set 'modifiable' when in Terminal mode.
3679 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3680 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
3681 {
3682 curbuf->b_p_ma = FALSE;
3683 args->os_doskip = TRUE;
3684 return e_cannot_make_terminal_with_running_job_modifiable;
3685 }
3686# endif
3687 redraw_titles();
3688
3689 return NULL;
3690}
3691
3692/*
3693 * Process the updated 'modified' option value.
3694 */
3695 char *
3696did_set_modified(optset_T *args)
3697{
3698 if (!args->os_newval.boolean)
3699 save_file_ff(curbuf); // Buffer is unchanged
3700 redraw_titles();
3701 modified_was_set = args->os_newval.boolean;
3702 return NULL;
3703}
3704
3705#if defined(FEAT_GUI) || defined(PROTO)
3706/*
3707 * Process the updated 'mousehide' option value.
3708 */
3709 char *
3710did_set_mousehide(optset_T *args UNUSED)
3711{
3712 if (!p_mh)
3713 gui_mch_mousehide(FALSE);
3714 return NULL;
3715}
3716#endif
3717
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003718/*
3719 * Process the updated 'number' or 'relativenumber' option value.
3720 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003721 char *
3722did_set_number_relativenumber(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003723{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003724#if (defined(FEAT_SIGNS) && defined(FEAT_GUI)) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003725 if (gui.in_use
3726 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3727 && curbuf->b_signlist != NULL)
3728 {
3729 // If the 'number' or 'relativenumber' options are modified and
3730 // 'signcolumn' is set to 'number', then clear the screen for a full
3731 // refresh. Otherwise the sign icons are not displayed properly in the
3732 // number column. If the 'number' option is set and only the
3733 // 'relativenumber' option is toggled, then don't refresh the screen
3734 // (optimization).
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003735 if (!(curwin->w_p_nu && ((int *)args->os_varp == &curwin->w_p_rnu)))
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003736 redraw_all_later(UPD_CLEAR);
3737 }
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003738#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003739 return NULL;
3740}
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003741
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003742#if defined(FEAT_LINEBREAK) || defined(PROTO)
3743/*
3744 * Process the new 'numberwidth' option value.
3745 */
3746 char *
3747did_set_numberwidth(optset_T *args UNUSED)
3748{
3749 char *errmsg = NULL;
3750
3751 // 'numberwidth' must be positive
3752 if (curwin->w_p_nuw < 1)
3753 {
3754 errmsg = e_argument_must_be_positive;
3755 curwin->w_p_nuw = 1;
3756 }
3757 if (curwin->w_p_nuw > 20)
3758 {
3759 errmsg = e_invalid_argument;
3760 curwin->w_p_nuw = 20;
3761 }
3762 curwin->w_nrwidth_line_count = 0; // trigger a redraw
3763
3764 return errmsg;
3765}
3766#endif
3767
3768/*
3769 * Process the updated 'paste' option value. Called after p_paste was set or
3770 * reset. When 'paste' is set or reset also change other options.
3771 */
3772 char *
3773did_set_paste(optset_T *args UNUSED)
3774{
3775 static int old_p_paste = FALSE;
3776 static int save_sm = 0;
3777 static int save_sta = 0;
3778 static int save_ru = 0;
3779#ifdef FEAT_RIGHTLEFT
3780 static int save_ri = 0;
3781 static int save_hkmap = 0;
3782#endif
3783 buf_T *buf;
3784
3785 if (p_paste)
3786 {
3787 // Paste switched from off to on.
3788 // Save the current values, so they can be restored later.
3789 if (!old_p_paste)
3790 {
3791 // save options for each buffer
3792 FOR_ALL_BUFFERS(buf)
3793 {
3794 buf->b_p_tw_nopaste = buf->b_p_tw;
3795 buf->b_p_wm_nopaste = buf->b_p_wm;
3796 buf->b_p_sts_nopaste = buf->b_p_sts;
3797 buf->b_p_ai_nopaste = buf->b_p_ai;
3798 buf->b_p_et_nopaste = buf->b_p_et;
3799#ifdef FEAT_VARTABS
3800 if (buf->b_p_vsts_nopaste)
3801 vim_free(buf->b_p_vsts_nopaste);
3802 buf->b_p_vsts_nopaste =
3803 buf->b_p_vsts && buf->b_p_vsts != empty_option
3804 ? vim_strsave(buf->b_p_vsts) : NULL;
3805#endif
3806 }
3807
3808 // save global options
3809 save_sm = p_sm;
3810 save_sta = p_sta;
3811 save_ru = p_ru;
3812#ifdef FEAT_RIGHTLEFT
3813 save_ri = p_ri;
3814 save_hkmap = p_hkmap;
3815#endif
3816 // save global values for local buffer options
3817 p_ai_nopaste = p_ai;
3818 p_et_nopaste = p_et;
3819 p_sts_nopaste = p_sts;
3820 p_tw_nopaste = p_tw;
3821 p_wm_nopaste = p_wm;
3822#ifdef FEAT_VARTABS
3823 if (p_vsts_nopaste)
3824 vim_free(p_vsts_nopaste);
3825 p_vsts_nopaste = p_vsts && p_vsts != empty_option
3826 ? vim_strsave(p_vsts) : NULL;
3827#endif
3828 }
3829
3830 // Always set the option values, also when 'paste' is set when it is
3831 // already on. Set options for each buffer.
3832 FOR_ALL_BUFFERS(buf)
3833 {
3834 buf->b_p_tw = 0; // textwidth is 0
3835 buf->b_p_wm = 0; // wrapmargin is 0
3836 buf->b_p_sts = 0; // softtabstop is 0
3837 buf->b_p_ai = 0; // no auto-indent
3838 buf->b_p_et = 0; // no expandtab
3839#ifdef FEAT_VARTABS
3840 if (buf->b_p_vsts)
3841 free_string_option(buf->b_p_vsts);
3842 buf->b_p_vsts = empty_option;
3843 VIM_CLEAR(buf->b_p_vsts_array);
3844#endif
3845 }
3846
3847 // set global options
3848 p_sm = 0; // no showmatch
3849 p_sta = 0; // no smarttab
3850 if (p_ru)
3851 status_redraw_all(); // redraw to remove the ruler
3852 p_ru = 0; // no ruler
3853#ifdef FEAT_RIGHTLEFT
3854 p_ri = 0; // no reverse insert
3855 p_hkmap = 0; // no Hebrew keyboard
3856#endif
3857 // set global values for local buffer options
3858 p_tw = 0;
3859 p_wm = 0;
3860 p_sts = 0;
3861 p_ai = 0;
Christian Brabandt757593c2023-08-22 21:44:10 +02003862 p_et = 0;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003863#ifdef FEAT_VARTABS
3864 if (p_vsts)
3865 free_string_option(p_vsts);
3866 p_vsts = empty_option;
3867#endif
3868 }
3869
3870 // Paste switched from on to off: Restore saved values.
3871 else if (old_p_paste)
3872 {
3873 // restore options for each buffer
3874 FOR_ALL_BUFFERS(buf)
3875 {
3876 buf->b_p_tw = buf->b_p_tw_nopaste;
3877 buf->b_p_wm = buf->b_p_wm_nopaste;
3878 buf->b_p_sts = buf->b_p_sts_nopaste;
3879 buf->b_p_ai = buf->b_p_ai_nopaste;
3880 buf->b_p_et = buf->b_p_et_nopaste;
3881#ifdef FEAT_VARTABS
3882 if (buf->b_p_vsts)
3883 free_string_option(buf->b_p_vsts);
3884 buf->b_p_vsts = buf->b_p_vsts_nopaste
3885 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
3886 vim_free(buf->b_p_vsts_array);
3887 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
3888 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
3889 else
3890 buf->b_p_vsts_array = NULL;
3891#endif
3892 }
3893
3894 // restore global options
3895 p_sm = save_sm;
3896 p_sta = save_sta;
3897 if (p_ru != save_ru)
3898 status_redraw_all(); // redraw to draw the ruler
3899 p_ru = save_ru;
3900#ifdef FEAT_RIGHTLEFT
3901 p_ri = save_ri;
3902 p_hkmap = save_hkmap;
3903#endif
3904 // set global values for local buffer options
3905 p_ai = p_ai_nopaste;
3906 p_et = p_et_nopaste;
3907 p_sts = p_sts_nopaste;
3908 p_tw = p_tw_nopaste;
3909 p_wm = p_wm_nopaste;
3910#ifdef FEAT_VARTABS
3911 if (p_vsts)
3912 free_string_option(p_vsts);
3913 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
3914#endif
3915 }
3916
3917 old_p_paste = p_paste;
3918
Christian Brabandt757593c2023-08-22 21:44:10 +02003919#if defined(FEAT_EVAL) || defined(PROTO)
3920 // Remember where the dependent options were reset
3921 didset_options_sctx((OPT_LOCAL | OPT_GLOBAL), p_paste_dep_opts);
3922#endif
3923
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003924 return NULL;
3925}
3926
3927
3928#ifdef FEAT_QUICKFIX
3929/*
3930 * Process the updated 'previewwindow' option value.
3931 */
3932 char *
3933did_set_previewwindow(optset_T *args)
3934{
3935 if (!curwin->w_p_pvw)
3936 return NULL;
3937
3938 // There can be only one window with 'previewwindow' set.
3939 win_T *win;
3940
3941 FOR_ALL_WINDOWS(win)
3942 if (win->w_p_pvw && win != curwin)
3943 {
3944 curwin->w_p_pvw = FALSE;
3945 args->os_doskip = TRUE;
3946 return e_preview_window_already_exists;
3947 }
3948
3949 return NULL;
3950}
3951#endif
3952
3953#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
3954/*
3955 * Process the new 'pyxversion' option value.
3956 */
3957 char *
3958did_set_pyxversion(optset_T *args UNUSED)
3959{
3960 char *errmsg = NULL;
3961
3962 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3963 errmsg = e_invalid_argument;
3964
3965 return errmsg;
3966}
3967#endif
3968
3969/*
3970 * Process the updated 'readonly' option value.
3971 */
3972 char *
3973did_set_readonly(optset_T *args)
3974{
3975 // when 'readonly' is reset globally, also reset readonlymode
3976 if (!curbuf->b_p_ro && (args->os_flags & OPT_LOCAL) == 0)
3977 readonlymode = FALSE;
3978
3979 // when 'readonly' is set may give W10 again
3980 if (curbuf->b_p_ro)
3981 curbuf->b_did_warn = FALSE;
3982
3983 redraw_titles();
3984
3985 return NULL;
3986}
3987
3988/*
3989 * Process the updated 'scrollbind' option value.
3990 */
3991 char *
3992did_set_scrollbind(optset_T *args UNUSED)
3993{
3994 // when 'scrollbind' is set: snapshot the current position to avoid a jump
3995 // at the end of normal_cmd()
3996 if (!curwin->w_p_scb)
3997 return NULL;
3998
3999 do_check_scrollbind(FALSE);
4000 curwin->w_scbind_pos = curwin->w_topline;
4001 return NULL;
4002}
4003
4004#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
4005/*
4006 * Process the updated 'shellslash' option value.
4007 */
4008 char *
4009did_set_shellslash(optset_T *args UNUSED)
4010{
4011 if (p_ssl)
4012 {
4013 psepc = '/';
4014 psepcN = '\\';
4015 pseps[0] = '/';
4016 }
4017 else
4018 {
4019 psepc = '\\';
4020 psepcN = '/';
4021 pseps[0] = '\\';
4022 }
4023
4024 // need to adjust the file name arguments and buffer names.
4025 buflist_slash_adjust();
4026 alist_slash_adjust();
4027# ifdef FEAT_EVAL
4028 scriptnames_slash_adjust();
4029# endif
4030 return NULL;
4031}
4032#endif
4033
4034/*
4035 * Process the new 'shiftwidth' or the 'tabstop' option value.
4036 */
4037 char *
4038did_set_shiftwidth_tabstop(optset_T *args)
4039{
4040 long *pp = (long *)args->os_varp;
4041 char *errmsg = NULL;
4042
4043 if (curbuf->b_p_sw < 0)
4044 {
4045 errmsg = e_argument_must_be_positive;
4046#ifdef FEAT_VARTABS
4047 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4048 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4049 ? tabstop_first(curbuf->b_p_vts_array)
4050 : curbuf->b_p_ts;
4051#else
4052 curbuf->b_p_sw = curbuf->b_p_ts;
4053#endif
4054 }
4055
4056#ifdef FEAT_FOLDING
4057 if (foldmethodIsIndent(curwin))
4058 foldUpdateAll(curwin);
4059#endif
4060 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
4061 // parse 'cinoptions'.
4062 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
4063 parse_cino(curbuf);
4064
4065 return errmsg;
4066}
4067
4068/*
4069 * Process the new 'showtabline' option value.
4070 */
4071 char *
4072did_set_showtabline(optset_T *args UNUSED)
4073{
4074 shell_new_rows(); // recompute window positions and heights
4075 return NULL;
4076}
4077
4078/*
4079 * Process the updated 'smoothscroll' option value.
4080 */
4081 char *
4082did_set_smoothscroll(optset_T *args UNUSED)
4083{
4084 if (curwin->w_p_sms)
4085 return NULL;
4086
4087 curwin->w_skipcol = 0;
4088 changed_line_abv_curs();
4089 return NULL;
4090}
4091
4092#if defined(FEAT_SPELL) || defined(PROTO)
4093/*
4094 * Process the updated 'spell' option value.
4095 */
4096 char *
4097did_set_spell(optset_T *args UNUSED)
4098{
4099 if (curwin->w_p_spell)
4100 return parse_spelllang(curwin);
4101
4102 return NULL;
4103}
4104#endif
4105
4106/*
4107 * Process the updated 'swapfile' option value.
4108 */
4109 char *
4110did_set_swapfile(optset_T *args UNUSED)
4111{
4112 // when 'swf' is set, create swapfile, when reset remove swapfile
4113 if (curbuf->b_p_swf && p_uc)
4114 ml_open_file(curbuf); // create the swap file
4115 else
4116 // no need to reset curbuf->b_may_swap, ml_open_file() will check
4117 // buf->b_p_swf
4118 mf_close_file(curbuf, TRUE); // remove the swap file
4119 return NULL;
4120}
4121
4122#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004123 char *
4124did_set_termguicolors(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004125{
4126# ifdef FEAT_VTP
4127 // Do not turn on 'tgc' when 24-bit colors are not supported.
4128 if (
4129# ifdef VIMDLL
4130 !gui.in_use && !gui.starting &&
4131# endif
4132 !has_vtp_working())
4133 {
4134 p_tgc = 0;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004135 args->os_doskip = TRUE;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004136 return e_24_bit_colors_are_not_supported_on_this_environment;
4137 }
4138 if (is_term_win32())
4139 swap_tcap();
4140# endif
4141# ifdef FEAT_GUI
4142 if (!gui.in_use && !gui.starting)
4143# endif
4144 highlight_gui_started();
4145# ifdef FEAT_VTP
4146 // reset t_Co
4147 if (is_term_win32())
4148 {
4149 control_console_color_rgb();
4150 set_termname(T_NAME);
4151 init_highlight(TRUE, FALSE);
4152 }
4153# endif
4154# ifdef FEAT_TERMINAL
4155 term_update_colors_all();
4156 term_update_palette_all();
4157 term_update_wincolor_all();
4158# endif
4159
4160 return NULL;
4161}
4162#endif
4163
4164/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004165 * Process the updated 'terse' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004167 char *
4168did_set_terse(optset_T *args UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004170 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004172 // when 'terse' is set change 'shortmess'
4173 p = vim_strchr(p_shm, SHM_SEARCH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004175 // insert 's' in p_shm
4176 if (p_terse && p == NULL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004177 {
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004178 STRCPY(IObuff, p_shm);
4179 STRCAT(IObuff, "s");
4180 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004181 }
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004182 // remove 's' from p_shm
4183 else if (!p_terse && p != NULL)
4184 STRMOVE(p, p + 1);
4185 return NULL;
4186}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004188/*
4189 * Process the updated 'textauto' option value.
4190 */
4191 char *
4192did_set_textauto(optset_T *args)
4193{
4194 // when 'textauto' is set or reset also change 'fileformats'
4195 set_string_option_direct((char_u *)"ffs", -1,
4196 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
4197 OPT_FREE | args->os_flags, 0);
Bram Moolenaar53744302015-07-17 17:38:22 +02004198
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004199 return NULL;
4200}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004202/*
4203 * Process the updated 'textmode' option value.
4204 */
4205 char *
4206did_set_textmode(optset_T *args)
4207{
4208 // when 'textmode' is set or reset also change 'fileformat'
4209 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, args->os_flags);
4210
4211 return NULL;
4212}
4213
4214/*
4215 * Process the new 'textwidth' option value.
4216 */
4217 char *
4218did_set_textwidth(optset_T *args UNUSED)
4219{
4220 char *errmsg = NULL;
4221
4222 if (curbuf->b_p_tw < 0)
4223 {
4224 errmsg = e_argument_must_be_positive;
4225 curbuf->b_p_tw = 0;
4226 }
4227#ifdef FEAT_SYN_HL
4228 {
4229 win_T *wp;
4230 tabpage_T *tp;
4231
4232 FOR_ALL_TAB_WINDOWS(tp, wp)
4233 check_colorcolumn(wp);
4234 }
Bram Moolenaar53744302015-07-17 17:38:22 +02004235#endif
4236
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004237 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238}
4239
4240/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004241 * Process the updated 'title' or the 'icon' option value.
4242 */
4243 char *
4244did_set_title_icon(optset_T *args UNUSED)
4245{
4246 // when 'title' changed, may need to change the title; same for 'icon'
4247 did_set_title();
4248 return NULL;
4249}
4250
4251/*
4252 * Process the new 'titlelen' option value.
4253 */
4254 char *
4255did_set_titlelen(optset_T *args)
4256{
4257 long old_value = args->os_oldval.number;
4258 char *errmsg = NULL;
4259
4260 // if 'titlelen' has changed, redraw the title
4261 if (p_titlelen < 0)
4262 {
4263 errmsg = e_argument_must_be_positive;
4264 p_titlelen = 85;
4265 }
4266 if (starting != NO_SCREEN && old_value != p_titlelen)
4267 need_maketitle = TRUE;
4268
4269 return errmsg;
4270}
4271
4272#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
4273/*
4274 * Process the updated 'undofile' option value.
4275 */
4276 char *
4277did_set_undofile(optset_T *args)
4278{
4279 // Only take action when the option was set.
4280 if (!curbuf->b_p_udf && !p_udf)
4281 return NULL;
4282
4283 // When reset we do not delete the undo file, the option may be set again
4284 // without making any changes in between.
4285 char_u hash[UNDO_HASH_SIZE];
4286 buf_T *save_curbuf = curbuf;
4287
4288 FOR_ALL_BUFFERS(curbuf)
4289 {
4290 // When 'undofile' is set globally: for every buffer, otherwise
4291 // only for the current buffer: Try to read in the undofile,
4292 // if one exists, the buffer wasn't changed and the buffer was
4293 // loaded
4294 if ((curbuf == save_curbuf
4295 || (args->os_flags & OPT_GLOBAL)
4296 || args->os_flags == 0)
4297 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
4298 {
4299#ifdef FEAT_CRYPT
Christian Brabandtaae58342023-04-23 17:50:22 +01004300 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004301 continue;
4302#endif
4303 u_compute_hash(hash);
4304 u_read_undo(NULL, hash, curbuf->b_fname);
4305 }
4306 }
4307 curbuf = save_curbuf;
4308
4309 return NULL;
4310}
4311#endif
4312
4313/*
4314 * Process the new global 'undolevels' option value.
4315 */
4316 static void
4317update_global_undolevels(long value, long old_value)
4318{
4319 // sync undo before 'undolevels' changes
4320
4321 // use the old value, otherwise u_sync() may not work properly
4322 p_ul = old_value;
4323 u_sync(TRUE);
4324 p_ul = value;
4325}
4326
4327/*
4328 * Process the new buffer local 'undolevels' option value.
4329 */
4330 static void
4331update_buflocal_undolevels(long value, long old_value)
4332{
4333 // use the old value, otherwise u_sync() may not work properly
4334 curbuf->b_p_ul = old_value;
4335 u_sync(TRUE);
4336 curbuf->b_p_ul = value;
4337}
4338
4339/*
4340 * Process the new 'undolevels' option value.
4341 */
4342 char *
4343did_set_undolevels(optset_T *args)
4344{
4345 long *pp = (long *)args->os_varp;
4346
4347 if (pp == &p_ul) // global 'undolevels'
4348 update_global_undolevels(args->os_newval.number,
4349 args->os_oldval.number);
4350 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4351 update_buflocal_undolevels(args->os_newval.number,
4352 args->os_oldval.number);
4353
4354 return NULL;
4355}
4356
4357/*
4358 * Process the new 'updatecount' option value.
4359 */
4360 char *
4361did_set_updatecount(optset_T *args)
4362{
4363 long old_value = args->os_oldval.number;
4364 char *errmsg = NULL;
4365
4366 // when 'updatecount' changes from zero to non-zero, open swap files
4367 if (p_uc < 0)
4368 {
4369 errmsg = e_argument_must_be_positive;
4370 p_uc = 100;
4371 }
4372 if (p_uc && !old_value)
4373 ml_open_files();
4374
4375 return errmsg;
4376}
4377
4378/*
4379 * Process the updated 'weirdinvert' option value.
4380 */
4381 char *
4382did_set_weirdinvert(optset_T *args)
4383{
4384 // When 'weirdinvert' changed, set/reset 't_xs'.
4385 // Then set 'weirdinvert' according to value of 't_xs'.
4386 if (p_wiv && !args->os_oldval.boolean)
4387 T_XS = (char_u *)"y";
4388 else if (!p_wiv && args->os_oldval.boolean)
4389 T_XS = empty_option;
4390 p_wiv = (*T_XS != NUL);
4391
4392 return NULL;
4393}
4394
4395/*
4396 * Process the new 'window' option value.
4397 */
4398 char *
4399did_set_window(optset_T *args UNUSED)
4400{
4401 if (p_window < 1)
4402 p_window = 1;
4403 else if (p_window >= Rows)
4404 p_window = Rows - 1;
4405 return NULL;
4406}
4407
4408/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004409 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004411 char *
4412did_set_winheight_helpheight(optset_T *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004414 long *pp = (long *)args->os_varp;
4415 char *errmsg = NULL;
4416
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004417 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004419 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004420 p_wh = 1;
4421 }
4422 if (p_wmh > p_wh)
4423 {
4424 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4425 p_wh = p_wmh;
4426 }
4427 if (p_hh < 0)
4428 {
4429 errmsg = e_argument_must_be_positive;
4430 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 }
4432
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004433 // Change window height NOW
4434 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004436 if (pp == &p_wh && curwin->w_height < p_wh)
4437 win_setheight((int)p_wh);
4438 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
4439 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004442 return errmsg;
4443}
4444
4445/*
4446 * Process the new 'winminheight' option value.
4447 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004448 char *
4449did_set_winminheight(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004450{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004451 char *errmsg = NULL;
4452
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004453 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004455 errmsg = e_argument_must_be_positive;
4456 p_wmh = 0;
4457 }
4458 if (p_wmh > p_wh)
4459 {
4460 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4461 p_wmh = p_wh;
4462 }
4463 win_setminheight();
4464
4465 return errmsg;
4466}
4467
4468/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004469 * Process the new 'winminwidth' option value.
4470 */
4471 char *
4472did_set_winminwidth(optset_T *args UNUSED)
4473{
4474 char *errmsg = NULL;
4475
4476 if (p_wmw < 0)
4477 {
4478 errmsg = e_argument_must_be_positive;
4479 p_wmw = 0;
4480 }
4481 if (p_wmw > p_wiw)
4482 {
4483 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4484 p_wmw = p_wiw;
4485 }
4486 win_setminwidth();
4487
4488 return errmsg;
4489}
4490
4491/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004492 * Process the new 'winwidth' option value.
4493 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004494 char *
4495did_set_winwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004496{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004497 char *errmsg = NULL;
4498
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004499 if (p_wiw < 1)
4500 {
4501 errmsg = e_argument_must_be_positive;
4502 p_wiw = 1;
4503 }
4504 if (p_wmw > p_wiw)
4505 {
4506 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4507 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004509
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004510 // Change window width NOW
4511 if (!ONE_WINDOW && curwin->w_width < p_wiw)
4512 win_setwidth((int)p_wiw);
4513
4514 return errmsg;
4515}
4516
4517/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004518 * Process the updated 'wrap' option value.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004519 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004520 char *
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004521did_set_wrap(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004522{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004523 // If 'wrap' is set, set w_leftcol to zero.
4524 if (curwin->w_p_wrap)
4525 curwin->w_leftcol = 0;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004526 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004527}
4528
4529/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004530 * Set the value of a boolean option, and take care of side effects.
4531 * Returns NULL for success, or an error message for an error.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004532 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004533 static char *
4534set_bool_option(
4535 int opt_idx, // index in options[] table
4536 char_u *varp, // pointer to the option variable
4537 int value, // new value
4538 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004539{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004540 int old_value = *(int *)varp;
4541#if defined(FEAT_EVAL)
4542 int old_global_value = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004544 char *errmsg = NULL;
4545
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004546 // Disallow changing some options from secure mode
4547 if ((secure
4548#ifdef HAVE_SANDBOX
4549 || sandbox != 0
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004550#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004551 ) && (options[opt_idx].flags & P_SECURE))
4552 return e_not_allowed_here;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004553
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004554#if defined(FEAT_EVAL)
4555 // Save the global value before changing anything. This is needed as for
4556 // a global-only option setting the "local value" in fact sets the global
4557 // value (since there is only one value).
4558 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4559 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
4560 OPT_GLOBAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004562
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004563 *(int *)varp = value; // set the new value
4564#ifdef FEAT_EVAL
4565 // Remember where the option was set.
4566 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004567#endif
4568
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004570 need_mouse_correct = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004573 // May set global value for local option.
4574 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4575 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004576
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004577 // Handle side effects of changing a bool option.
4578 if (options[opt_idx].opt_did_set_cb != NULL)
4579 {
4580 optset_T args;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004581
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004582 CLEAR_FIELD(args);
4583 args.os_varp = varp;
4584 args.os_flags = opt_flags;
4585 args.os_oldval.boolean = old_value;
4586 args.os_newval.boolean = value;
4587 args.os_errbuf = NULL;
4588 errmsg = options[opt_idx].opt_did_set_cb(&args);
4589 if (args.os_doskip)
4590 return errmsg;
4591 }
4592
4593 // after handling side effects, call autocommand
4594
4595 options[opt_idx].flags |= P_WAS_SET;
4596
4597#if defined(FEAT_EVAL)
4598 apply_optionset_autocmd(opt_idx, opt_flags,
4599 (long)(old_value ? TRUE : FALSE),
4600 (long)(old_global_value ? TRUE : FALSE),
4601 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004602#endif
4603
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004604 comp_col(); // in case 'ruler' or 'showcmd' changed
4605 if (curwin->w_curswant != MAXCOL
4606 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
4607 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004608
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004609 if ((opt_flags & OPT_NO_REDRAW) == 0)
4610 check_redraw(options[opt_idx].flags);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004611
4612 return errmsg;
4613}
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004614
4615/*
4616 * Check the bounds of numeric options.
4617 */
4618 static char *
4619check_num_option_bounds(
4620 long *pp,
4621 long old_value,
4622 long old_Rows,
4623 long old_Columns,
4624 char *errbuf,
4625 size_t errbuflen,
4626 char *errmsg)
4627{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 if (Rows < min_rows() && full_screen)
4629 {
4630 if (errbuf != NULL)
4631 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004632 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004633 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 errmsg = errbuf;
4635 }
4636 Rows = min_rows();
4637 }
4638 if (Columns < MIN_COLUMNS && full_screen)
4639 {
4640 if (errbuf != NULL)
4641 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004642 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004643 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 errmsg = errbuf;
4645 }
4646 Columns = MIN_COLUMNS;
4647 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004648 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004650 // If the screen (shell) height has been changed, assume it is the
4651 // physical screenheight.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 if (old_Rows != Rows || old_Columns != Columns)
4653 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004654 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 if (updating_screen)
4656 *pp = old_value;
4657 else if (full_screen
4658#ifdef FEAT_GUI
4659 && !gui.starting
4660#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004661 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 set_shellsize((int)Columns, (int)Rows, TRUE);
4663 else
4664 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004665 // Postpone the resizing; check the size and cmdline position for
4666 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 check_shellsize();
4668 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4669 cmdline_row = Rows - p_ch;
4670 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004671 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004672 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 }
4674
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 if (curbuf->b_p_ts <= 0)
4676 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004677 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 curbuf->b_p_ts = 8;
4679 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004680 else if (curbuf->b_p_ts > TABSTOP_MAX)
4681 {
4682 errmsg = e_invalid_argument;
4683 curbuf->b_p_ts = 8;
4684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 if (p_tm < 0)
4686 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004687 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 p_tm = 0;
4689 }
4690 if ((curwin->w_p_scr <= 0
4691 || (curwin->w_p_scr > curwin->w_height
4692 && curwin->w_height > 0))
4693 && full_screen)
4694 {
4695 if (pp == &(curwin->w_p_scr))
4696 {
4697 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004698 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 win_comp_scroll(curwin);
4700 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004701 // If 'scroll' became invalid because of a side effect silently adjust
4702 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 else if (curwin->w_p_scr <= 0)
4704 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004705 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 curwin->w_p_scr = curwin->w_height;
4707 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004708 if (p_hi < 0)
4709 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004710 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004711 p_hi = 0;
4712 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004713 else if (p_hi > 10000)
4714 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004715 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004716 p_hi = 10000;
4717 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004718 if (p_re < 0 || p_re > 2)
4719 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004720 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004721 p_re = 0;
4722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 if (p_report < 0)
4724 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004725 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 p_report = 1;
4727 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004728 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004730 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 p_sj = Rows / 2;
4732 else
4733 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004734 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 p_sj = 1;
4736 }
4737 }
4738 if (p_so < 0 && full_screen)
4739 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004740 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 p_so = 0;
4742 }
4743 if (p_siso < 0 && full_screen)
4744 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004745 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 p_siso = 0;
4747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 if (p_cwh < 1)
4749 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004750 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 p_cwh = 1;
4752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 if (p_ut < 0)
4754 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004755 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 p_ut = 2000;
4757 }
4758 if (p_ss < 0)
4759 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004760 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 p_ss = 0;
4762 }
4763
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004764 return errmsg;
4765}
4766
4767/*
4768 * Set the value of a number option, and take care of side effects.
4769 * Returns NULL for success, or an error message for an error.
4770 */
4771 static char *
4772set_num_option(
4773 int opt_idx, // index in options[] table
4774 char_u *varp, // pointer to the option variable
4775 long value, // new value
4776 char *errbuf, // buffer for error messages
4777 size_t errbuflen, // length of "errbuf"
4778 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4779 // OPT_MODELINE, etc.
4780{
4781 char *errmsg = NULL;
4782 long old_value = *(long *)varp;
4783#if defined(FEAT_EVAL)
4784 long old_global_value = 0; // only used when setting a local and
4785 // global option
4786#endif
4787 long old_Rows = Rows; // remember old Rows
4788 long old_Columns = Columns; // remember old Columns
4789 long *pp = (long *)varp;
4790
4791 // Disallow changing some options from secure mode.
4792 if ((secure
4793#ifdef HAVE_SANDBOX
4794 || sandbox != 0
4795#endif
4796 ) && (options[opt_idx].flags & P_SECURE))
4797 return e_not_allowed_here;
4798
4799#if defined(FEAT_EVAL)
4800 // Save the global value before changing anything. This is needed as for
4801 // a global-only option setting the "local value" in fact sets the global
4802 // value (since there is only one value).
4803 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4804 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4805 OPT_GLOBAL);
4806#endif
4807
4808 *pp = value;
4809#ifdef FEAT_EVAL
4810 // Remember where the option was set.
4811 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4812#endif
4813#ifdef FEAT_GUI
4814 need_mouse_correct = TRUE;
4815#endif
4816
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004817 // Invoke the option specific callback function to validate and apply the
4818 // new value.
4819 if (options[opt_idx].opt_did_set_cb != NULL)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004820 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004821 optset_T args;
4822
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004823 CLEAR_FIELD(args);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004824 args.os_varp = varp;
4825 args.os_flags = opt_flags;
4826 args.os_oldval.number = old_value;
4827 args.os_newval.number = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004828 args.os_errbuf = NULL;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004829 errmsg = options[opt_idx].opt_did_set_cb(&args);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004830 }
4831
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004832 // Check the bounds for numeric options here
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004833 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4834 errbuf, errbuflen, errmsg);
4835
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004836 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4838 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4839
4840 options[opt_idx].flags |= P_WAS_SET;
4841
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004842#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004843 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4844 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004845#endif
4846
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004847 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02004848 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01004849 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004850 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004851 if ((opt_flags & OPT_NO_REDRAW) == 0)
4852 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853
4854 return errmsg;
4855}
4856
4857/*
4858 * Called after an option changed: check if something needs to be redrawn.
4859 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004860 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004861check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004863 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004864 int doclear = (flags & P_RCLR) == P_RCLR;
4865 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004867 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869
4870 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
4871 changed_window_setting();
4872 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004873 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01004874 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004875 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004876 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004877 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004879 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880}
4881
4882/*
4883 * Find index for option 'arg'.
4884 * Return -1 if not found.
4885 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004886 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004887findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888{
4889 int opt_idx;
4890 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004891 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 int is_term_opt;
4893
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004894 // For first call: Initialize the quick-access table.
4895 // It contains the index for the first option that starts with a certain
4896 // letter. There are 26 letters, plus the first "t_" option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 if (quick_tab[1] == 0)
4898 {
4899 p = options[0].fullname;
4900 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4901 {
4902 if (s[0] != p[0])
4903 {
4904 if (s[0] == 't' && s[1] == '_')
4905 quick_tab[26] = opt_idx;
4906 else
4907 quick_tab[CharOrdLow(s[0])] = opt_idx;
4908 }
4909 p = s;
4910 }
4911 }
4912
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004913 // Check for name starting with an illegal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 return -1;
4916
4917 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4918 if (is_term_opt)
4919 opt_idx = quick_tab[26];
4920 else
4921 opt_idx = quick_tab[CharOrdLow(arg[0])];
4922 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4923 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004924 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 break;
4926 }
4927 if (s == NULL && !is_term_opt)
4928 {
4929 opt_idx = quick_tab[CharOrdLow(arg[0])];
4930 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
4931 {
4932 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004933 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 break;
4935 s = NULL;
4936 }
4937 }
4938 if (s == NULL)
4939 opt_idx = -1;
4940 return opt_idx;
4941}
4942
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004943#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
4944 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945/*
4946 * Get the value for an option.
4947 *
4948 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004949 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01004950 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004951 * String option: gov_string, *stringval gets allocated string.
4952 * Hidden Number option: gov_hidden_number.
4953 * Hidden Toggle option: gov_hidden_bool.
4954 * Hidden String option: gov_hidden_string.
4955 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004956 *
4957 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004959 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01004960get_option_value(
4961 char_u *name,
4962 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004963 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004964 int *flagsp,
4965 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966{
4967 int opt_idx;
4968 char_u *varp;
4969
4970 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004971 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01004972 {
4973 int key;
4974
4975 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004976 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004977 {
4978 char_u key_name[2];
4979 char_u *p;
4980
Bram Moolenaar10c75c42021-12-28 20:53:30 +00004981 if (flagsp != NULL)
4982 *flagsp = 0; // terminal option has no flags
4983
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004984 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01004985 if (key < 0)
4986 {
4987 key_name[0] = KEY2TERMCAP0(key);
4988 key_name[1] = KEY2TERMCAP1(key);
4989 }
4990 else
4991 {
4992 key_name[0] = KS_KEY;
4993 key_name[1] = (key & 0xff);
4994 }
4995 p = find_termcode(key_name);
4996 if (p != NULL)
4997 {
4998 if (stringval != NULL)
4999 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005000 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01005001 }
5002 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005003 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01005004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005006 varp = get_varp_scope(&(options[opt_idx]), scope);
5007
5008 if (flagsp != NULL)
5009 // Return the P_xxxx option flags.
5010 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011
5012 if (options[opt_idx].flags & P_STRING)
5013 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005014 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005015 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 if (stringval != NULL)
5017 {
zeertzjq51f0bc32022-05-09 13:33:39 +01005018 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01005019 *stringval = str2special_save(*(char_u **)(varp), FALSE,
5020 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005022 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01005023 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00005024 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01005027 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 *stringval = vim_strsave(*(char_u **)(varp));
5029 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005030 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 }
5032
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005033 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005034 return (options[opt_idx].flags & P_NUM)
5035 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 if (options[opt_idx].flags & P_NUM)
5037 *numval = *(long *)varp;
5038 else
5039 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005040 // Special case: 'modified' is b_changed, but we also want to consider
5041 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 if ((int *)varp == &curbuf->b_changed)
5043 *numval = curbufIsChanged();
5044 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02005045 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005047 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048}
5049#endif
5050
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005051#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005052/*
5053 * Returns the option attributes and its value. Unlike the above function it
5054 * will return either global value or local value of the option depending on
5055 * what was requested, but it will never return global value if it was
5056 * requested to return local one and vice versa. Neither it will return
5057 * buffer-local value if it was requested to return window-local one.
5058 *
5059 * Pretends that option is absent if it is not present in the requested scope
5060 * (i.e. has no global, window-local or buffer-local value depending on
5061 * opt_type). Uses
5062 *
5063 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02005064 * 0 hidden or unknown option, also option that does not have requested
5065 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005066 * see SOPT_* in vim.h for other flags
5067 *
5068 * Possible opt_type values: see SREQ_* in vim.h
5069 */
5070 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005071get_option_value_strict(
5072 char_u *name,
5073 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005074 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01005075 int opt_type,
5076 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005077{
5078 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02005079 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005080 struct vimoption *p;
5081 int r = 0;
5082
5083 opt_idx = findoption(name);
5084 if (opt_idx < 0)
5085 return 0;
5086
5087 p = &(options[opt_idx]);
5088
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005089 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005090 if (p->var == NULL)
5091 return 0;
5092
5093 if (p->flags & P_BOOL)
5094 r |= SOPT_BOOL;
5095 else if (p->flags & P_NUM)
5096 r |= SOPT_NUM;
5097 else if (p->flags & P_STRING)
5098 r |= SOPT_STRING;
5099
5100 if (p->indir == PV_NONE)
5101 {
5102 if (opt_type == SREQ_GLOBAL)
5103 r |= SOPT_GLOBAL;
5104 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005105 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005106 }
5107 else
5108 {
5109 if (p->indir & PV_BOTH)
5110 r |= SOPT_GLOBAL;
5111 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005112 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005113
5114 if (p->indir & PV_WIN)
5115 {
5116 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005117 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005118 else
5119 r |= SOPT_WIN;
5120 }
5121 else if (p->indir & PV_BUF)
5122 {
5123 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005124 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005125 else
5126 r |= SOPT_BUF;
5127 }
5128 }
5129
5130 if (stringval == NULL)
5131 return r;
5132
5133 if (opt_type == SREQ_GLOBAL)
5134 varp = p->var;
5135 else
5136 {
5137 if (opt_type == SREQ_BUF)
5138 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005139 // Special case: 'modified' is b_changed, but we also want to
5140 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005141 if (p->indir == PV_MOD)
5142 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005143 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005144 varp = NULL;
5145 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005146# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005147 else if (p->indir == PV_KEY)
5148 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005149 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005150 *stringval = NULL;
5151 varp = NULL;
5152 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005153# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005154 else
5155 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005156 buf_T *save_curbuf = curbuf;
5157
5158 // only getting a pointer, no need to use aucmd_prepbuf()
5159 curbuf = (buf_T *)from;
5160 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005161 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02005162 curbuf = save_curbuf;
5163 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005164 }
5165 }
5166 else if (opt_type == SREQ_WIN)
5167 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005168 win_T *save_curwin = curwin;
5169
5170 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005171 curbuf = curwin->w_buffer;
5172 varp = get_varp(p);
5173 curwin = save_curwin;
5174 curbuf = curwin->w_buffer;
5175 }
5176 if (varp == p->var)
5177 return (r | SOPT_UNSET);
5178 }
5179
5180 if (varp != NULL)
5181 {
5182 if (p->flags & P_STRING)
5183 *stringval = vim_strsave(*(char_u **)(varp));
5184 else if (p->flags & P_NUM)
5185 *numval = *(long *) varp;
5186 else
5187 *numval = *(int *)varp;
5188 }
5189
5190 return r;
5191}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005192
5193/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005194 * Iterate over options. First argument is a pointer to a pointer to a
5195 * structure inside options[] array, second is option type like in the above
5196 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005197 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005198 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005199 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005200 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005201 * first argument to NULL.
5202 *
5203 * Returns full option name for current option on each call.
5204 */
5205 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005206option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005207{
5208 struct vimoption *ret = NULL;
5209 do
5210 {
5211 if (*option == NULL)
5212 *option = (void *) options;
5213 else if (((struct vimoption *) (*option))->fullname == NULL)
5214 {
5215 *option = NULL;
5216 return NULL;
5217 }
5218 else
5219 *option = (void *) (((struct vimoption *) (*option)) + 1);
5220
5221 ret = ((struct vimoption *) (*option));
5222
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005223 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005224 if (ret->var == NULL)
5225 {
5226 ret = NULL;
5227 continue;
5228 }
5229
5230 switch (opt_type)
5231 {
5232 case SREQ_GLOBAL:
5233 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
5234 ret = NULL;
5235 break;
5236 case SREQ_BUF:
5237 if (!(ret->indir & PV_BUF))
5238 ret = NULL;
5239 break;
5240 case SREQ_WIN:
5241 if (!(ret->indir & PV_WIN))
5242 ret = NULL;
5243 break;
5244 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005245 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005246 return NULL;
5247 }
5248 }
5249 while (ret == NULL);
5250
5251 return (char_u *)ret->fullname;
5252}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005253#endif
5254
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005256 * Return the flags for the option at 'opt_idx'.
5257 */
5258 long_u
5259get_option_flags(int opt_idx)
5260{
5261 return options[opt_idx].flags;
5262}
5263
5264/*
5265 * Set a flag for the option at 'opt_idx'.
5266 */
5267 void
5268set_option_flag(int opt_idx, long_u flag)
5269{
5270 options[opt_idx].flags |= flag;
5271}
5272
5273/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005274 * Returns TRUE if the option at 'opt_idx' is a global option
5275 */
5276 int
5277is_global_option(int opt_idx)
5278{
5279 return options[opt_idx].indir == PV_NONE;
5280}
5281
5282/*
5283 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5284 * local value.
5285 */
5286 int
5287is_global_local_option(int opt_idx)
5288{
5289 return options[opt_idx].indir & PV_BOTH;
5290}
5291
5292/*
5293 * Returns TRUE if the option at 'opt_idx' is a window-local option
5294 */
5295 int
5296is_window_local_option(int opt_idx)
5297{
5298 return options[opt_idx].var == VAR_WIN;
5299}
5300
5301/*
5302 * Returns TRUE if the option at 'opt_idx' is a hidden option
5303 */
5304 int
5305is_hidden_option(int opt_idx)
5306{
5307 return options[opt_idx].var == NULL;
5308}
5309
5310#if defined(FEAT_CRYPT) || defined(PROTO)
5311/*
5312 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5313 */
5314 int
5315is_crypt_key_option(int opt_idx)
5316{
5317 return options[opt_idx].indir == PV_KEY;
5318}
5319#endif
5320
5321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 * Set the value of option "name".
5323 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005324 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005325 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005327 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005328set_option_value(
5329 char_u *name,
5330 long number,
5331 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005332 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333{
5334 int opt_idx;
5335 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005336 long_u flags;
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005337 static char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338
5339 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005340 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005341 {
5342 int key;
5343
5344 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005345 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005346 {
5347 char_u key_name[2];
5348
5349 if (key < 0)
5350 {
5351 key_name[0] = KEY2TERMCAP0(key);
5352 key_name[1] = KEY2TERMCAP1(key);
5353 }
5354 else
5355 {
5356 key_name[0] = KS_KEY;
5357 key_name[1] = (key & 0xff);
5358 }
5359 add_termcode(key_name, string, FALSE);
5360 if (full_screen)
5361 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005362 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005363 return NULL;
5364 }
5365
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005366 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 else
5369 {
5370 flags = options[opt_idx].flags;
5371#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005372 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005374 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005375 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005376 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005379 if (flags & P_STRING)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005380 return set_string_option(opt_idx, string, opt_flags, errbuf);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005381
5382 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5383 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005385 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005387 int idx;
5388
5389 // Either we are given a string or we are setting option
5390 // to zero.
5391 for (idx = 0; string[idx] == '0'; ++idx)
5392 ;
5393 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005394 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005395 // There's another character after zeros or the string
5396 // is empty. In both cases, we are trying to set a
5397 // num option using a string.
5398 semsg(_(e_number_required_after_str_equal_str),
5399 name, string);
5400 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005401
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005404 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005405 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005406 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005407 errbuf, sizeof(errbuf), opt_flags);
5408 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005409 else
5410 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005412
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005414 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415}
5416
5417/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005418 * Call set_option_value() and when an error is returned report it.
5419 */
5420 void
5421set_option_value_give_err(
5422 char_u *name,
5423 long number,
5424 char_u *string,
5425 int opt_flags) // OPT_LOCAL or 0 (both)
5426{
5427 char *errmsg = set_option_value(name, number, string, opt_flags);
5428
5429 if (errmsg != NULL)
5430 emsg(_(errmsg));
5431}
5432
5433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 * Get the terminal code for a terminal option.
5435 * Returns NULL when not found.
5436 */
5437 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005438get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439{
5440 int opt_idx;
5441 char_u *varp;
5442
5443 if (tname[0] != 't' || tname[1] != '_' ||
5444 tname[2] == NUL || tname[3] == NUL)
5445 return NULL;
5446 if ((opt_idx = findoption(tname)) >= 0)
5447 {
5448 varp = get_varp(&(options[opt_idx]));
5449 if (varp != NULL)
5450 varp = *(char_u **)(varp);
5451 return varp;
5452 }
5453 return find_termcode(tname + 2);
5454}
5455
5456 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005457get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458{
5459 int i;
5460
5461 i = findoption((char_u *)"hl");
5462 if (i >= 0)
5463 return options[i].def_val[VI_DEFAULT];
5464 return (char_u *)NULL;
5465}
5466
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005467 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005468get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005469{
5470 int i;
5471
5472 i = findoption((char_u *)"enc");
5473 if (i >= 0)
5474 return options[i].def_val[VI_DEFAULT];
5475 return (char_u *)NULL;
5476}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005477
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005478#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005479 int
5480is_option_allocated(char *name)
5481{
5482 int idx = findoption((char_u *)name);
5483
5484 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5485}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005486#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005487
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488/*
5489 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005490 * When "has_lt" is true there is a '<' before "*arg_arg".
5491 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 */
5493 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005494find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005496 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005498 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005500 // Don't use get_special_key_code() for t_xx, we don't want it to call
5501 // add_termcap_entry().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5503 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005504 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005506 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005508 key = find_special_key(&arg, &modifiers,
5509 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005510 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 key = 0;
5512 }
5513 return key;
5514}
5515
5516/*
5517 * if 'all' == 0: show changed options
5518 * if 'all' == 1: show all normal options
5519 * if 'all' == 2: show all terminal options
5520 */
5521 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005522showoptions(
5523 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005524 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525{
5526 struct vimoption *p;
5527 int col;
5528 int isterm;
5529 char_u *varp;
5530 struct vimoption **items;
5531 int item_count;
5532 int run;
5533 int row, rows;
5534 int cols;
5535 int i;
5536 int len;
5537
5538#define INC 20
5539#define GAP 3
5540
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005541 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 if (items == NULL)
5543 return;
5544
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005545 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005547 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005549 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005551 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005553 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005555 // Do the loop two times:
5556 // 1. display the short items
5557 // 2. display the long items (only strings and numbers)
5558 // When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 for (run = 1; run <= 2 && !got_int; ++run)
5560 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005561 // collect the items in items[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 item_count = 0;
5563 for (p = &options[0]; p->fullname != NULL; p++)
5564 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005565 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005566 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005567 continue;
5568
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 varp = NULL;
5570 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005571 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 {
5573 if (p->indir != PV_NONE && !isterm)
5574 varp = get_varp_scope(p, opt_flags);
5575 }
5576 else
5577 varp = get_varp(p);
5578 if (varp != NULL
5579 && ((all == 2 && isterm)
5580 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005581 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005583 if (opt_flags & OPT_ONECOLUMN)
5584 len = Columns;
5585 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005586 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 else
5588 {
5589 option_value2string(p, opt_flags);
5590 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5591 }
5592 if ((len <= INC - GAP && run == 1) ||
5593 (len > INC - GAP && run == 2))
5594 items[item_count++] = p;
5595 }
5596 }
5597
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005598 // display the items
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 if (run == 1)
5600 {
5601 cols = (Columns + GAP - 3) / INC;
5602 if (cols == 0)
5603 cols = 1;
5604 rows = (item_count + cols - 1) / cols;
5605 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005606 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 rows = item_count;
5608 for (row = 0; row < rows && !got_int; ++row)
5609 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005610 msg_putchar('\n'); // go to next line
5611 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 break;
5613 col = 0;
5614 for (i = row; i < item_count; i += rows)
5615 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005616 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 showoneopt(items[i], opt_flags);
5618 col += INC;
5619 }
5620 out_flush();
5621 ui_breakcheck();
5622 }
5623 }
5624 vim_free(items);
5625}
5626
5627/*
5628 * Return TRUE if option "p" has its default value.
5629 */
5630 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005631optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632{
5633 int dvi;
5634
5635 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005636 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005637 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005639 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005641 // the cast to long is required for Manx C, long_i is
5642 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005643 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005644 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5646}
5647
5648/*
5649 * showoneopt: show the value of one option
5650 * must not be called with a hidden option!
5651 */
5652 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005653showoneopt(
5654 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005655 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005657 char_u *varp;
5658 int save_silent = silent_mode;
5659
5660 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005661 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662
5663 varp = get_varp_scope(p, opt_flags);
5664
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005665 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5667 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005668 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005670 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005672 msg_puts(" ");
5673 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 if (!(p->flags & P_BOOL))
5675 {
5676 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005677 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 option_value2string(p, opt_flags);
5679 msg_outtrans(NameBuff);
5680 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005681
5682 silent_mode = save_silent;
5683 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684}
5685
5686/*
5687 * Write modified options as ":set" commands to a file.
5688 *
5689 * There are three values for "opt_flags":
5690 * OPT_GLOBAL: Write global option values and fresh values of
5691 * buffer-local options (used for start of a session
5692 * file).
5693 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5694 * curwin (used for a vimrc file).
5695 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5696 * and local values for window-local options of
5697 * curwin. Local values are also written when at the
5698 * default value, because a modeline or autocommand
5699 * may have set them when doing ":edit file" and the
5700 * user has set them back at the default or fresh
5701 * value.
5702 * When "local_only" is TRUE, don't write fresh
5703 * values, only local values (for ":mkview").
5704 * (fresh value = value used for a new buffer or window for a local option).
5705 *
5706 * Return FAIL on error, OK otherwise.
5707 */
5708 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005709makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710{
5711 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005712 char_u *varp; // currently used value
5713 char_u *varp_fresh; // local value
5714 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 char *cmd;
5716 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005717 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718
5719 /*
5720 * The options that don't have a default (terminal name, columns, lines)
5721 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005722 * Do the loop over "options[]" twice: once for options with the
5723 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005725 for (pri = 1; pri >= 0; --pri)
5726 {
5727 for (p = &options[0]; !istermoption(p); p++)
5728 if (!(p->flags & P_NO_MKRC)
5729 && !istermoption(p)
5730 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005732 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5734 continue;
5735
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005736 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5737 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5739 continue;
5740
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005741 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005743 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 continue;
5745
Bram Moolenaard23b7142021-04-17 21:04:34 +02005746 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5747 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005748 continue;
5749
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 round = 2;
5751 if (p->indir != PV_NONE)
5752 {
5753 if (p->var == VAR_WIN)
5754 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005755 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 if (!(opt_flags & OPT_LOCAL))
5757 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005758 // When fresh value of window-local option is not at the
5759 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5761 {
5762 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005763 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 {
5765 round = 1;
5766 varp_local = varp;
5767 varp = varp_fresh;
5768 }
5769 }
5770 }
5771 }
5772
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005773 // Round 1: fresh value for window-local options.
5774 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 for ( ; round <= 2; varp = varp_local, ++round)
5776 {
5777 if (round == 1 || (opt_flags & OPT_GLOBAL))
5778 cmd = "set";
5779 else
5780 cmd = "setlocal";
5781
5782 if (p->flags & P_BOOL)
5783 {
5784 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5785 return FAIL;
5786 }
5787 else if (p->flags & P_NUM)
5788 {
5789 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5790 return FAIL;
5791 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005792 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005794 int do_endif = FALSE;
5795
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005796 // Don't set 'syntax' and 'filetype' again if the value is
5797 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005798 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005799#if defined(FEAT_SYN_HL)
5800 p->indir == PV_SYN ||
5801#endif
5802 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 {
5804 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5805 *(char_u **)(varp)) < 0
5806 || put_eol(fd) < 0)
5807 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005808 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 }
5810 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005811 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005813 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 {
5815 if (put_line(fd, "endif") == FAIL)
5816 return FAIL;
5817 }
5818 }
5819 }
5820 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 return OK;
5823}
5824
5825#if defined(FEAT_FOLDING) || defined(PROTO)
5826/*
5827 * Generate set commands for the local fold options only. Used when
5828 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5829 */
5830 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005831makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005833 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005835 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 == FAIL
5837# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005838 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005840 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 == FAIL
5842 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5843 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5844 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5845 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5846 )
5847 return FAIL;
5848
5849 return OK;
5850}
5851#endif
5852
5853 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005854put_setstring(
5855 FILE *fd,
5856 char *cmd,
5857 char *name,
5858 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005859 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860{
5861 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005862 char_u *buf = NULL;
5863 char_u *part = NULL;
5864 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865
5866 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5867 return FAIL;
5868 if (*valuep != NULL)
5869 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005870 // Output 'pastetoggle' as key names. For other
5871 // options some characters have to be escaped with
5872 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 if (valuep == &p_pt)
5874 {
5875 s = *valuep;
5876 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005877 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 return FAIL;
5879 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005880 // expand the option value, replace $HOME by ~
5881 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005883 int size = (int)STRLEN(*valuep) + 1;
5884
5885 // replace home directory in the whole option value into "buf"
5886 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005887 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005888 goto fail;
5889 home_replace(NULL, *valuep, buf, size, FALSE);
5890
5891 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005892 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005893 // can be expanded when read back.
5894 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5895 && vim_strchr(*valuep, ',') != NULL)
5896 {
5897 part = alloc(size);
5898 if (part == NULL)
5899 goto fail;
5900
5901 // write line break to clear the option, e.g. ':set rtp='
5902 if (put_eol(fd) == FAIL)
5903 goto fail;
5904
5905 p = buf;
5906 while (*p != NUL)
5907 {
5908 // for each comma separated option part, append value to
5909 // the option, :set rtp+=value
5910 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5911 goto fail;
5912 (void)copy_option_part(&p, part, size, ",");
5913 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5914 goto fail;
5915 }
5916 vim_free(buf);
5917 vim_free(part);
5918 return OK;
5919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005921 {
5922 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005924 }
5925 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 }
5927 else if (put_escstr(fd, *valuep, 2) == FAIL)
5928 return FAIL;
5929 }
5930 if (put_eol(fd) < 0)
5931 return FAIL;
5932 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005933fail:
5934 vim_free(buf);
5935 vim_free(part);
5936 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937}
5938
5939 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005940put_setnum(
5941 FILE *fd,
5942 char *cmd,
5943 char *name,
5944 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945{
5946 long wc;
5947
5948 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5949 return FAIL;
5950 if (wc_use_keyname((char_u *)valuep, &wc))
5951 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005952 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5954 return FAIL;
5955 }
5956 else if (fprintf(fd, "%ld", *valuep) < 0)
5957 return FAIL;
5958 if (put_eol(fd) < 0)
5959 return FAIL;
5960 return OK;
5961}
5962
5963 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005964put_setbool(
5965 FILE *fd,
5966 char *cmd,
5967 char *name,
5968 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005970 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005971 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5973 || put_eol(fd) < 0)
5974 return FAIL;
5975 return OK;
5976}
5977
5978/*
5979 * Clear all the terminal options.
5980 * If the option has been allocated, free the memory.
5981 * Terminal options are never hidden or indirect.
5982 */
5983 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005984clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005986 // Reset a few things before clearing the old options. This may cause
5987 // outputting a few things that the terminal doesn't understand, but the
5988 // screen will be cleared later, so this is OK.
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005989 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005990 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005992 // When starting the GUI close the display opened for the clipboard.
5993 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 if (gui.starting)
5995 clear_xterm_clip();
5996#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005997 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005999 free_termoptions();
6000}
6001
6002 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006003free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006004{
6005 struct vimoption *p;
6006
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006007 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 if (istermoption(p))
6009 {
6010 if (p->flags & P_ALLOCED)
6011 free_string_option(*(char_u **)(p->var));
6012 if (p->flags & P_DEF_ALLOCED)
6013 free_string_option(p->def_val[VI_DEFAULT]);
6014 *(char_u **)(p->var) = empty_option;
6015 p->def_val[VI_DEFAULT] = empty_option;
6016 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006017#ifdef FEAT_EVAL
6018 // remember where the option was cleared
6019 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
6020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 }
6022 clear_termcodes();
6023}
6024
6025/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00006026 * Free the string for one term option, if it was allocated.
6027 * Set the string to empty_option and clear allocated flag.
6028 * "var" points to the option value.
6029 */
6030 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006031free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00006032{
6033 struct vimoption *p;
6034
6035 for (p = &options[0]; p->fullname != NULL; p++)
6036 if (p->var == var)
6037 {
6038 if (p->flags & P_ALLOCED)
6039 free_string_option(*(char_u **)(p->var));
6040 *(char_u **)(p->var) = empty_option;
6041 p->flags &= ~P_ALLOCED;
6042 break;
6043 }
6044}
6045
6046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 * Set the terminal option defaults to the current value.
6048 * Used after setting the terminal name.
6049 */
6050 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006051set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052{
6053 struct vimoption *p;
6054
6055 for (p = &options[0]; p->fullname != NULL; p++)
6056 {
6057 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
6058 {
6059 if (p->flags & P_DEF_ALLOCED)
6060 {
6061 free_string_option(p->def_val[VI_DEFAULT]);
6062 p->flags &= ~P_DEF_ALLOCED;
6063 }
6064 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
6065 if (p->flags & P_ALLOCED)
6066 {
6067 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006068 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 }
6070 }
6071 }
6072}
6073
6074/*
6075 * return TRUE if 'p' starts with 't_'
6076 */
6077 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006078istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079{
6080 return (p->fullname[0] == 't' && p->fullname[1] == '_');
6081}
6082
Bram Moolenaardac13472019-09-16 21:06:21 +02006083/*
6084 * Returns TRUE if the option at 'opt_idx' starts with 't_'
6085 */
6086 int
6087istermoption_idx(int opt_idx)
6088{
6089 return istermoption(&options[opt_idx]);
6090}
6091
Bram Moolenaar113e1072019-01-20 15:30:40 +01006092#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006094 * Unset local option value, similar to ":set opt<".
6095 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006096 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006097unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006098{
6099 struct vimoption *p;
6100 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006101 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006102
6103 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02006104 if (opt_idx < 0)
6105 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006106 p = &(options[opt_idx]);
6107
6108 switch ((int)p->indir)
6109 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006110 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006111 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006112 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006113 break;
6114 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006115 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006116 break;
6117 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006118 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006119 break;
6120 case PV_AR:
6121 buf->b_p_ar = -1;
6122 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006123 case PV_BKC:
6124 clear_string_option(&buf->b_p_bkc);
6125 buf->b_bkc_flags = 0;
6126 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006127 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006128 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006129 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006130 case PV_TC:
6131 clear_string_option(&buf->b_p_tc);
6132 buf->b_tc_flags = 0;
6133 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006134 case PV_SISO:
6135 curwin->w_p_siso = -1;
6136 break;
6137 case PV_SO:
6138 curwin->w_p_so = -1;
6139 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006140# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006141 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006142 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006143 break;
6144 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006145 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006146 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006147# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006148 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006149 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006150 break;
6151 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006152 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006153 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006154# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006155 case PV_TSRFU:
6156 clear_string_option(&buf->b_p_tsrfu);
6157 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006158# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006159 case PV_FP:
6160 clear_string_option(&buf->b_p_fp);
6161 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006162# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006163 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006164 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006165 break;
6166 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006167 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006168 break;
6169 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006170 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006171 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006172# endif
6173# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006174 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006175 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006176 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006177# endif
6178# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006179 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006180 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006181 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006182# endif
6183# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006184 case PV_SBR:
6185 clear_string_option(&((win_T *)from)->w_p_sbr);
6186 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006187# endif
6188# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006189 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006190 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006191 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006192# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006193 case PV_UL:
6194 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6195 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006196 case PV_LW:
6197 clear_string_option(&buf->b_p_lw);
6198 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006199 case PV_MENC:
6200 clear_string_option(&buf->b_p_menc);
6201 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006202 case PV_LCS:
6203 clear_string_option(&((win_T *)from)->w_p_lcs);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006204 set_listchars_option((win_T *)from, ((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006205 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006206 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006207 case PV_FCS:
6208 clear_string_option(&((win_T *)from)->w_p_fcs);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006209 set_fillchars_option((win_T *)from, ((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006210 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006211 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006212 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006213 clear_string_option(&((win_T *)from)->w_p_ve);
6214 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006215 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006216 }
6217}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006218#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006219
6220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006222 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 */
6224 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006225get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006227 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 {
6229 if (p->var == VAR_WIN)
6230 return (char_u *)GLOBAL_WO(get_varp(p));
6231 return p->var;
6232 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006233 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 {
6235 switch ((int)p->indir)
6236 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006237 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006239 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
6240 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6241 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006243 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6244 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6245 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006246 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006247 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006248 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006249 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6250 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006252 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6253 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006255 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6256 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006257#ifdef FEAT_COMPL_FUNC
6258 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6259#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006260#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6261 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6262#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006263#if defined(FEAT_CRYPT)
6264 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6265#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006266#ifdef FEAT_LINEBREAK
6267 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6268#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006269#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006270 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006271#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006272 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006273 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006274 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006275 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006276 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006277 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006278 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006279
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006281 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 }
6283 return get_varp(p);
6284}
6285
6286/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006287 * Get pointer to option variable at 'opt_idx', depending on local or global
6288 * scope.
6289 */
6290 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006291get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006292{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006293 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006294}
6295
6296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 * Get pointer to option variable.
6298 */
6299 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006300get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006302 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 if (p->var == NULL)
6304 return NULL;
6305
6306 switch ((int)p->indir)
6307 {
6308 case PV_NONE: return p->var;
6309
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006310 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006311 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006313 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006315 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006317 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006319 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006321 case PV_TC: return *curbuf->b_p_tc != NUL
6322 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006323 case PV_BKC: return *curbuf->b_p_bkc != NUL
6324 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006325 case PV_SISO: return curwin->w_p_siso >= 0
6326 ? (char_u *)&(curwin->w_p_siso) : p->var;
6327 case PV_SO: return curwin->w_p_so >= 0
6328 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006330 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006332 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6334#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006335 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006337 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006339#ifdef FEAT_COMPL_FUNC
6340 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6341 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6342#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006343 case PV_FP: return *curbuf->b_p_fp != NUL
6344 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006346 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006348 case PV_GP: return *curbuf->b_p_gp != NUL
6349 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6350 case PV_MP: return *curbuf->b_p_mp != NUL
6351 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006353#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6354 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6355 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6356#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006357#if defined(FEAT_CRYPT)
6358 case PV_CM: return *curbuf->b_p_cm != NUL
6359 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6360#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006361#ifdef FEAT_LINEBREAK
6362 case PV_SBR: return *curwin->w_p_sbr != NUL
6363 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6364#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006365#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006366 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006367 ? (char_u *)&(curwin->w_p_stl) : p->var;
6368#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006369 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6370 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006371 case PV_LW: return *curbuf->b_p_lw != NUL
6372 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006373 case PV_MENC: return *curbuf->b_p_menc != NUL
6374 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375#ifdef FEAT_ARABIC
6376 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6377#endif
6378 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006379 case PV_LCS: return *curwin->w_p_lcs != NUL
6380 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006381 case PV_FCS: return *curwin->w_p_fcs != NUL
6382 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006383 case PV_VE: return *curwin->w_p_ve != NUL
6384 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006385#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006386 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6387#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006388#ifdef FEAT_SYN_HL
6389 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6390 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006391 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006392 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394#ifdef FEAT_DIFF
6395 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6396#endif
6397#ifdef FEAT_FOLDING
6398 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6399 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6400 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6401 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6402 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6403 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6404 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6405# ifdef FEAT_EVAL
6406 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6407 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6408# endif
6409 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6410#endif
6411 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006412 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006413#ifdef FEAT_LINEBREAK
6414 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006417 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006418#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6420#endif
6421#ifdef FEAT_RIGHTLEFT
6422 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6423 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6424#endif
6425 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006426 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6428#ifdef FEAT_LINEBREAK
6429 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006430 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6431 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006433 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006435 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006436#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006437 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6438 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6439#endif
6440#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006441 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6442 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6443 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445
6446 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6447 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6450 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6452 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6454 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6455 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006456 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459#ifdef FEAT_FOLDING
6460 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006463#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006464 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006466#ifdef FEAT_COMPL_FUNC
6467 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006468 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006469#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006470#ifdef FEAT_EVAL
6471 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6472#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006473 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006475 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006481 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6483 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6484 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6485 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6486#ifdef FEAT_FIND_ID
6487# ifdef FEAT_EVAL
6488 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6489# endif
6490#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006491#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6493 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6494#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006495#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006496 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498#ifdef FEAT_CRYPT
6499 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006502 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6504 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6505 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6506 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6507 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006509 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6516#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006517 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006518 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6519#endif
6520#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006521 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6522 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6523 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006524 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525#endif
6526 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6527 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6528 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6529 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006530#ifdef FEAT_PERSISTENT_UNDO
6531 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6534#ifdef FEAT_KEYMAP
6535 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6536#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006537#ifdef FEAT_SIGNS
6538 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6539#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006540#ifdef FEAT_VARTABS
6541 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6542 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6543#endif
RestorerZ68ebcee2023-05-31 17:12:14 +01006544 default: iemsg(e_get_varp_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006546 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 return (char_u *)&(curbuf->b_p_wm);
6548}
6549
6550/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006551 * Return a pointer to the variable for option at 'opt_idx'
6552 */
6553 char_u *
6554get_option_var(int opt_idx)
6555{
6556 return options[opt_idx].var;
6557}
6558
Dominique Pelle748b3082022-01-08 12:41:16 +00006559#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006560/*
6561 * Return the full name of the option at 'opt_idx'
6562 */
6563 char_u *
6564get_option_fullname(int opt_idx)
6565{
6566 return (char_u *)options[opt_idx].fullname;
6567}
Dominique Pelle748b3082022-01-08 12:41:16 +00006568#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006569
6570/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00006571 * Return the did_set callback function for the option at 'opt_idx'
6572 */
6573 opt_did_set_cb_T
6574get_option_did_set_cb(int opt_idx)
6575{
6576 return options[opt_idx].opt_did_set_cb;
6577}
6578
6579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 * Get the value of 'equalprg', either the buffer-local one or the global one.
6581 */
6582 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006583get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584{
6585 if (*curbuf->b_p_ep == NUL)
6586 return p_ep;
6587 return curbuf->b_p_ep;
6588}
6589
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590/*
6591 * Copy options from one window to another.
6592 * Used when splitting a window.
6593 */
6594 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006595win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596{
6597 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6598 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006599 after_copy_winopt(wp_to);
6600}
6601
6602/*
6603 * After copying window options: update variables depending on options.
6604 */
6605 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006606after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006607{
6608#ifdef FEAT_LINEBREAK
6609 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006610#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006611#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006612 fill_culopt_flags(NULL, wp);
6613 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006614#endif
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006615 set_listchars_option(wp, wp->w_p_lcs, TRUE);
6616 set_fillchars_option(wp, wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006617}
6618
6619 static char_u *
6620copy_option_val(char_u *val)
6621{
6622 if (val == empty_option)
6623 return empty_option; // no need to allocate memory
6624 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626
6627/*
6628 * Copy the options from one winopt_T to another.
6629 * Doesn't free the old option values in "to", use clear_winopt() for that.
6630 * The 'scroll' option is not copied, because it depends on the window height.
6631 * The 'previewwindow' option is reset, there can be only one preview window.
6632 */
6633 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006634copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635{
6636#ifdef FEAT_ARABIC
6637 to->wo_arab = from->wo_arab;
6638#endif
6639 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006640 to->wo_lcs = copy_option_val(from->wo_lcs);
6641 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006643 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006644 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006645 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006646#ifdef FEAT_LINEBREAK
6647 to->wo_nuw = from->wo_nuw;
6648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649#ifdef FEAT_RIGHTLEFT
6650 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006651 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006653#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006654 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006655#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006656#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006657 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006660#ifdef FEAT_DIFF
6661 to->wo_wrap_save = from->wo_wrap_save;
6662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663#ifdef FEAT_LINEBREAK
6664 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006665 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006666 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006668 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006670 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006671 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006672 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006673 to->wo_crb_save = from->wo_crb_save;
Christian Brabandt4a8eb6e2023-08-13 19:43:42 +02006674 to->wo_siso = from->wo_siso;
6675 to->wo_so = from->wo_so;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006676#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006677 to->wo_spell = from->wo_spell;
6678#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006679#ifdef FEAT_SYN_HL
6680 to->wo_cuc = from->wo_cuc;
6681 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006682 to->wo_culopt = copy_option_val(from->wo_culopt);
6683 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685#ifdef FEAT_DIFF
6686 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006687 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006689#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006690 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006691 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006692#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006693#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006694 to->wo_twk = copy_option_val(from->wo_twk);
6695 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697#ifdef FEAT_FOLDING
6698 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006699 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006701 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006702 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 to->wo_fml = from->wo_fml;
6704 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006705 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006706 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006707 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006708 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 to->wo_fdn = from->wo_fdn;
6710# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006711 to->wo_fde = copy_option_val(from->wo_fde);
6712 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006714 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006716#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006717 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006718#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006719
6720#ifdef FEAT_EVAL
6721 // Copy the script context so that we know where the value was last set.
6722 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6723 sizeof(to->wo_script_ctx));
6724#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006725 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726}
6727
6728/*
6729 * Check string options in a window for a NULL value.
6730 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006731 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006732check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733{
6734 check_winopt(&win->w_onebuf_opt);
6735 check_winopt(&win->w_allbuf_opt);
6736}
6737
6738/*
6739 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6740 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006741 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006742check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743{
6744#ifdef FEAT_FOLDING
6745 check_string_option(&wop->wo_fdi);
6746 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006747 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748# ifdef FEAT_EVAL
6749 check_string_option(&wop->wo_fde);
6750 check_string_option(&wop->wo_fdt);
6751# endif
6752 check_string_option(&wop->wo_fmr);
6753#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006754#ifdef FEAT_SIGNS
6755 check_string_option(&wop->wo_scl);
6756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757#ifdef FEAT_RIGHTLEFT
6758 check_string_option(&wop->wo_rlc);
6759#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006760#ifdef FEAT_LINEBREAK
6761 check_string_option(&wop->wo_sbr);
6762#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006763#ifdef FEAT_STL_OPT
6764 check_string_option(&wop->wo_stl);
6765#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006766#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006767 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006768 check_string_option(&wop->wo_cc);
6769#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006770#ifdef FEAT_CONCEAL
6771 check_string_option(&wop->wo_cocu);
6772#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006773#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006774 check_string_option(&wop->wo_twk);
6775 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006776#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006777#ifdef FEAT_LINEBREAK
6778 check_string_option(&wop->wo_briopt);
6779#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006780 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006781 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006782 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006783 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784}
6785
6786/*
6787 * Free the allocated memory inside a winopt_T.
6788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006790clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791{
6792#ifdef FEAT_FOLDING
6793 clear_string_option(&wop->wo_fdi);
6794 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006795 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796# ifdef FEAT_EVAL
6797 clear_string_option(&wop->wo_fde);
6798 clear_string_option(&wop->wo_fdt);
6799# endif
6800 clear_string_option(&wop->wo_fmr);
6801#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006802#ifdef FEAT_SIGNS
6803 clear_string_option(&wop->wo_scl);
6804#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006805#ifdef FEAT_LINEBREAK
6806 clear_string_option(&wop->wo_briopt);
6807#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006808 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809#ifdef FEAT_RIGHTLEFT
6810 clear_string_option(&wop->wo_rlc);
6811#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006812#ifdef FEAT_LINEBREAK
6813 clear_string_option(&wop->wo_sbr);
6814#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006815#ifdef FEAT_STL_OPT
6816 clear_string_option(&wop->wo_stl);
6817#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006818#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006819 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006820 clear_string_option(&wop->wo_cc);
6821#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006822#ifdef FEAT_CONCEAL
6823 clear_string_option(&wop->wo_cocu);
6824#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006825#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006826 clear_string_option(&wop->wo_twk);
6827 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006828#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006829 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006830 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006831 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832}
6833
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006834#ifdef FEAT_EVAL
6835// Index into the options table for a buffer-local option enum.
6836static int buf_opt_idx[BV_COUNT];
6837# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6838
6839/*
6840 * Initialize buf_opt_idx[] if not done already.
6841 */
6842 static void
6843init_buf_opt_idx(void)
6844{
6845 static int did_init_buf_opt_idx = FALSE;
6846 int i;
6847
6848 if (did_init_buf_opt_idx)
6849 return;
6850 did_init_buf_opt_idx = TRUE;
6851 for (i = 0; !istermoption_idx(i); i++)
6852 if (options[i].indir & PV_BUF)
6853 buf_opt_idx[options[i].indir & PV_MASK] = i;
6854}
6855#else
6856# define COPY_OPT_SCTX(buf, bv)
6857#endif
6858
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859/*
6860 * Copy global option values to local options for one buffer.
6861 * Used when creating a new buffer and sometimes when entering a buffer.
6862 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006863 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6865 * appropriate.
6866 * BCO_NOHELP Don't copy the values to a help buffer.
6867 */
6868 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006869buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870{
6871 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006872 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 int dont_do_help;
6874 int did_isk = FALSE;
6875
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006876 // Skip this when the option defaults have not been set yet. Happens when
6877 // main() allocates the first buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 if (p_cpo != NULL)
6879 {
6880 /*
6881 * Always copy when entering and 'cpo' contains 'S'.
6882 * Don't copy when already initialized.
6883 * Don't copy when 'cpo' contains 's' and not entering.
6884 * 'S' BCO_ENTER initialized 's' should_copy
6885 * yes yes X X TRUE
6886 * yes no yes X FALSE
6887 * no X yes X FALSE
6888 * X no no yes FALSE
6889 * X no no no TRUE
6890 * no yes no X TRUE
6891 */
6892 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6893 && (buf->b_p_initialized
6894 || (!(flags & BCO_ENTER)
6895 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6896 should_copy = FALSE;
6897
6898 if (should_copy || (flags & BCO_ALWAYS))
6899 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006900#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006901 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006902 init_buf_opt_idx();
6903#endif
6904 // Don't copy the options specific to a help buffer when
6905 // BCO_NOHELP is given or the options were initialized already
6906 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6908 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006909 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 {
6911 save_p_isk = buf->b_p_isk;
6912 buf->b_p_isk = NULL;
6913 }
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006914 // Always free the allocated strings. If not already initialized,
6915 // reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 if (!buf->b_p_initialized)
6917 {
6918 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006919 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006922 switch (*p_ffs)
6923 {
6924 case 'm':
6925 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6926 case 'd':
6927 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6928 case 'u':
6929 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6930 default:
6931 buf->b_p_ff = vim_strsave(p_ff);
6932 }
6933 if (buf->b_p_ff != NULL)
6934 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 buf->b_p_bh = empty_option;
6936 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 }
6938 else
6939 free_buf_options(buf, FALSE);
6940
6941 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006942 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 buf->b_p_ai_nopaste = p_ai_nopaste;
6944 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006945 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006947 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 buf->b_p_tw_nopaste = p_tw_nopaste;
6949 buf->b_p_tw_nobin = p_tw_nobin;
6950 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006951 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 buf->b_p_wm_nopaste = p_wm_nopaste;
6953 buf->b_p_wm_nobin = p_wm_nobin;
6954 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006955 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006956 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006957 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006958 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006959 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006961 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006963 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006965 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 buf->b_p_ml_nobin = p_ml_nobin;
6967 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006968 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006969 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006970 buf->b_p_swf = FALSE;
6971 else
6972 {
6973 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006974 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006977 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006978#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006979 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006980 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006982#ifdef FEAT_COMPL_FUNC
6983 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006984 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006985 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006986 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006987 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006988 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006989#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006990#ifdef FEAT_EVAL
6991 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006992 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006993 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006996 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006998#ifdef FEAT_VARTABS
6999 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007000 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007001 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007002 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007003 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007004 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007005 buf->b_p_vsts_nopaste = p_vsts_nopaste
7006 ? vim_strsave(p_vsts_nopaste) : NULL;
7007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007009 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007011 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012#ifdef FEAT_FOLDING
7013 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007014 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015#endif
7016 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007017 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007018 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007019 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02007020 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
7021 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007023 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007025 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007027 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007029 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007030
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007032 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007034 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007036 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01007037 buf->b_p_cinsd = vim_strsave(p_cinsd);
7038 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01007039 buf->b_p_lop = vim_strsave(p_lop);
7040 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007041
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007042 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007045 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007047 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007049 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007051 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00007053 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007054 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01007055 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007056#endif
7057#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02007058 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007059 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007060 (void)compile_cap_prog(&buf->b_s);
7061 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007062 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007063 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007064 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02007065 buf->b_s.b_p_spo = vim_strsave(p_spo);
7066 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007068#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007070 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007072 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01007074 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007075#if defined(FEAT_EVAL)
7076 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007077 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079#ifdef FEAT_CRYPT
7080 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007081 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007084 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085#ifdef FEAT_KEYMAP
7086 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007087 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 buf->b_kmap_state |= KEYMAP_INIT;
7089#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007090#ifdef FEAT_TERMINAL
7091 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007092 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007093#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007094 // This isn't really an option, but copying the langmap and IME
7095 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007097 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007099 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007101 // options that are normally global but also have a local value
7102 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01007104 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007105 buf->b_p_bkc = empty_option;
7106 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107#ifdef FEAT_QUICKFIX
7108 buf->b_p_gp = empty_option;
7109 buf->b_p_mp = empty_option;
7110 buf->b_p_efm = empty_option;
7111#endif
7112 buf->b_p_ep = empty_option;
7113 buf->b_p_kp = empty_option;
7114 buf->b_p_path = empty_option;
7115 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007116 buf->b_p_tc = empty_option;
7117 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118#ifdef FEAT_FIND_ID
7119 buf->b_p_def = empty_option;
7120 buf->b_p_inc = empty_option;
7121# ifdef FEAT_EVAL
7122 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007123 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124# endif
7125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 buf->b_p_dict = empty_option;
7127 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01007128#ifdef FEAT_COMPL_FUNC
7129 buf->b_p_tsrfu = empty_option;
7130#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007131 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007132 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007133#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7134 buf->b_p_bexpr = empty_option;
7135#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007136#if defined(FEAT_CRYPT)
7137 buf->b_p_cm = empty_option;
7138#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007139#ifdef FEAT_PERSISTENT_UNDO
7140 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007141 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007142#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007143 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007144 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007146 // Don't copy the options set by ex_help(), use the saved values,
7147 // when going from a help buffer to a non-help buffer.
7148 // Don't touch these at all when BCO_NOHELP is used and going from
7149 // or to a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007151 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007153#ifdef FEAT_VARTABS
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007154 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007155 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007156 else
7157 buf->b_p_vts_array = NULL;
7158#endif
7159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 else
7161 {
7162 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007163 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 did_isk = TRUE;
7165 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00007166 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007167#ifdef FEAT_VARTABS
7168 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007169 COPY_OPT_SCTX(buf, BV_VTS);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007170 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007171 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007172 else
7173 buf->b_p_vts_array = NULL;
7174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 if (buf->b_p_bt[0] == 'h')
7177 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007179 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 }
7181 }
7182
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007183 // When the options should be copied (ignoring BCO_ALWAYS), set the
7184 // flag that indicates that the options have been initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 if (should_copy)
7186 buf->b_p_initialized = TRUE;
7187 }
7188
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007189 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 if (did_isk)
7191 (void)buf_init_chartab(buf, FALSE);
7192}
7193
7194/*
7195 * Reset the 'modifiable' option and its default value.
7196 */
7197 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007198reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199{
7200 int opt_idx;
7201
7202 curbuf->b_p_ma = FALSE;
7203 p_ma = FALSE;
7204 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007205 if (opt_idx >= 0)
7206 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207}
7208
7209/*
7210 * Set the global value for 'iminsert' to the local value.
7211 */
7212 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007213set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214{
7215 p_iminsert = curbuf->b_p_iminsert;
7216}
7217
7218/*
7219 * Set the global value for 'imsearch' to the local value.
7220 */
7221 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007222set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223{
7224 p_imsearch = curbuf->b_p_imsearch;
7225}
7226
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227static int expand_option_idx = -1;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007228static int expand_option_start_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7230static int expand_option_flags = 0;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007231static int expand_option_append = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232
7233 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007234set_context_in_set_cmd(
7235 expand_T *xp,
7236 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007237 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238{
7239 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007240 long_u flags = 0; // init for GCC
7241 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 char_u *p;
7243 char_u *s;
7244 int is_term_option = FALSE;
7245 int key;
7246
7247 expand_option_flags = opt_flags;
7248
7249 xp->xp_context = EXPAND_SETTINGS;
7250 if (*arg == NUL)
7251 {
7252 xp->xp_pattern = arg;
7253 return;
7254 }
7255 p = arg + STRLEN(arg) - 1;
7256 if (*p == ' ' && *(p - 1) != '\\')
7257 {
7258 xp->xp_pattern = p + 1;
7259 return;
7260 }
7261 while (p > arg)
7262 {
7263 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007264 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 if (*p == ' ' || *p == ',')
7266 {
7267 while (s > arg && *(s - 1) == '\\')
7268 --s;
7269 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007270 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 if (*p == ' ' && ((p - s) & 1) == 0)
7272 {
7273 ++p;
7274 break;
7275 }
7276 --p;
7277 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007278 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 {
7280 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007281 xp->xp_prefix = XP_PREFIX_NO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 p += 2;
7283 }
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007284 else if (STRNCMP(p, "inv", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 {
7286 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007287 xp->xp_prefix = XP_PREFIX_INV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 p += 3;
7289 }
7290 xp->xp_pattern = arg = p;
7291 if (*arg == '<')
7292 {
7293 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007294 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 return;
7296 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007297 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 {
7299 xp->xp_context = EXPAND_NOTHING;
7300 return;
7301 }
7302 nextchar = *++p;
7303 is_term_option = TRUE;
7304 expand_option_name[2] = KEY2TERMCAP0(key);
7305 expand_option_name[3] = KEY2TERMCAP1(key);
7306 }
7307 else
7308 {
7309 if (p[0] == 't' && p[1] == '_')
7310 {
7311 p += 2;
7312 if (*p != NUL)
7313 ++p;
7314 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007315 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 nextchar = *++p;
7317 is_term_option = TRUE;
7318 expand_option_name[2] = p[-2];
7319 expand_option_name[3] = p[-1];
7320 }
7321 else
7322 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007323 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7325 p++;
7326 if (*p == NUL)
7327 return;
7328 nextchar = *p;
7329 *p = NUL;
7330 opt_idx = findoption(arg);
7331 *p = nextchar;
7332 if (opt_idx == -1 || options[opt_idx].var == NULL)
7333 {
7334 xp->xp_context = EXPAND_NOTHING;
7335 return;
7336 }
7337 flags = options[opt_idx].flags;
7338 if (flags & P_BOOL)
7339 {
7340 xp->xp_context = EXPAND_NOTHING;
7341 return;
7342 }
7343 }
7344 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007345 // handle "-=" and "+="
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007346 expand_option_append = FALSE;
7347 int expand_option_subtract = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7349 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007350 if (nextchar == '-')
7351 expand_option_subtract = TRUE;
7352 if (nextchar == '+' || nextchar == '^')
7353 expand_option_append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 ++p;
7355 nextchar = '=';
7356 }
7357 if ((nextchar != '=' && nextchar != ':')
7358 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7359 {
7360 xp->xp_context = EXPAND_UNSUCCESSFUL;
7361 return;
7362 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007363
7364 // Below are for handling expanding a specific option's value after the '='
7365 // or ':'
7366
7367 if (is_term_option)
7368 expand_option_idx = -1;
7369 else
7370 expand_option_idx = opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02007372 if (!is_term_option)
7373 {
7374 if (options[opt_idx].flags & P_NO_CMD_EXPAND)
7375 {
7376 xp->xp_context=EXPAND_UNSUCCESSFUL;
7377 return;
7378 }
7379 }
7380
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 xp->xp_pattern = p + 1;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007382 expand_option_start_col = (int)(p + 1 - xp->xp_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007384 // Certain options currently have special case handling to reuse the
7385 // expansion logic with other commands.
Doug Kearns6dfdff32023-08-27 18:48:51 +02007386#ifdef FEAT_SYN_HL
7387 if (options[opt_idx].var == (char_u *)&p_syn)
7388 {
7389 xp->xp_context = EXPAND_OWNSYNTAX;
7390 return;
7391 }
7392#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007393 if (options[opt_idx].var == (char_u *)&p_ft)
7394 {
7395 xp->xp_context = EXPAND_FILETYPE;
7396 return;
7397 }
Doug Kearns6dfdff32023-08-27 18:48:51 +02007398
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007399 // Now pick. If the option has a custom expander, use that. Otherwise, just
7400 // fill with the existing option value.
7401 if (expand_option_subtract)
7402 {
7403 xp->xp_context = EXPAND_SETTING_SUBTRACT;
7404 return;
7405 }
7406 else if (expand_option_idx >= 0 &&
7407 options[expand_option_idx].opt_expand_cb != NULL)
7408 {
7409 xp->xp_context = EXPAND_STRING_SETTING;
7410 }
7411 else if (*xp->xp_pattern == NUL)
7412 {
7413 xp->xp_context = EXPAND_OLD_SETTING;
7414 return;
7415 }
7416 else
7417 xp->xp_context = EXPAND_NOTHING;
7418
7419 if (is_term_option || (flags & P_NUM))
7420 return;
7421
7422 // Only string options below
7423
7424 // Options that have P_EXPAND are considered to all use file/dir expansion.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 if (flags & P_EXPAND)
7426 {
7427 p = options[opt_idx].var;
7428 if (p == (char_u *)&p_bdir
7429 || p == (char_u *)&p_dir
7430 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007431 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434#ifdef FEAT_SESSION
7435 || p == (char_u *)&p_vdir
7436#endif
7437 )
7438 {
7439 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007440 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 xp->xp_backslash = XP_BS_THREE;
7442 else
7443 xp->xp_backslash = XP_BS_ONE;
7444 }
7445 else
7446 {
7447 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007448 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 if (p == (char_u *)&p_tags)
7450 xp->xp_backslash = XP_BS_THREE;
7451 else
7452 xp->xp_backslash = XP_BS_ONE;
7453 }
Yee Cheng Chin54844852023-10-09 18:12:31 +02007454 if (flags & P_COMMA)
7455 xp->xp_backslash |= XP_BS_COMMA;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 }
7457
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007458 // For an option that is a list of file names, or comma/colon-separated
7459 // values, split it by the delimiter and find the start of the current
7460 // pattern, while accounting for backslash-escaped space/commas/colons.
7461 // Triple-backslashed escaped file names (e.g. 'path') can also be
7462 // delimited by space.
7463 if ((flags & P_EXPAND) || (flags & P_COMMA) || (flags & P_COLON))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007465 for (p = arg + STRLEN(arg) - 1; p >= xp->xp_pattern; --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007467 // count number of backslashes before ' ' or ',' or ':'
7468 if (*p == ' ' || *p == ',' ||
7469 (*p == ':' && (flags & P_COLON)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007471 s = p;
7472 while (s > xp->xp_pattern && *(s - 1) == '\\')
7473 --s;
Yee Cheng Chin54844852023-10-09 18:12:31 +02007474 if ((*p == ' ' && ((xp->xp_backslash & XP_BS_THREE) && (p - s) < 3))
7475#if defined(BACKSLASH_IN_FILENAME)
7476 || (*p == ',' && (flags & P_COMMA) && (p - s) < 1)
7477#else
7478 || (*p == ',' && (flags & P_COMMA) && (p - s) < 2)
7479#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007480 || (*p == ':' && (flags & P_COLON)))
7481 {
7482 xp->xp_pattern = p + 1;
7483 break;
7484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 }
7486 }
7487 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007488
7489 // An option that is a list of single-character flags should always start
7490 // at the end as we don't complete words.
7491 if (flags & P_FLAGLIST)
7492 xp->xp_pattern = arg + STRLEN(arg);
7493
7494 // Some options can either be using file/dir expansions, or custom value
7495 // expansion depending on what the user typed. Unfortunately we have to
7496 // manually handle it here to make sure we have the correct xp_context set.
7497#ifdef FEAT_SPELL
7498 if (options[opt_idx].var == (char_u *)&p_sps)
7499 {
7500 if (STRNCMP(xp->xp_pattern, "file:", 5) == 0)
7501 {
7502 xp->xp_pattern += 5;
7503 return;
7504 }
7505 else if (options[expand_option_idx].opt_expand_cb != NULL)
7506 {
7507 xp->xp_context = EXPAND_STRING_SETTING;
7508 }
7509 }
7510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511}
7512
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007513/*
7514 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7515 *
7516 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7517 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7518 *
7519 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7520 * regular expression 'regmatch', then stores the match in matches[idx] and
7521 * returns TRUE.
7522 *
7523 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7524 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7525 *
7526 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7527 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7528 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02007529 static int
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007530match_str(
7531 char_u *str,
7532 regmatch_T *regmatch,
7533 char_u **matches,
7534 int idx,
7535 int test_only,
7536 int fuzzy,
7537 char_u *fuzzystr,
7538 fuzmatch_str_T *fuzmatch)
7539{
7540 if (!fuzzy)
7541 {
7542 if (vim_regexec(regmatch, str, (colnr_T)0))
7543 {
7544 if (!test_only)
7545 matches[idx] = vim_strsave(str);
7546 return TRUE;
7547 }
7548 }
7549 else
7550 {
7551 int score;
7552
7553 score = fuzzy_match_str(str, fuzzystr);
7554 if (score != 0)
7555 {
7556 if (!test_only)
7557 {
7558 fuzmatch[idx].idx = idx;
7559 fuzmatch[idx].str = vim_strsave(str);
7560 fuzmatch[idx].score = score;
7561 }
7562
7563 return TRUE;
7564 }
7565 }
7566
7567 return FALSE;
7568}
7569
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007571ExpandSettings(
7572 expand_T *xp,
7573 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007574 char_u *fuzzystr,
7575 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007576 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007577 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007579 int num_normal = 0; // Nr of matching non-term-code settings
7580 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 int opt_idx;
7582 int match;
7583 int count = 0;
7584 char_u *str;
7585 int loop;
7586 int is_term_opt;
7587 char_u name_buf[MAX_KEY_NAME_LEN];
7588 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007589 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007590 int fuzzy;
7591 fuzmatch_str_T *fuzmatch = NULL;
7592
Christian Brabandtcb747892022-05-08 21:10:56 +01007593 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007595 // do this loop twice:
7596 // loop == 0: count the number of matching options
7597 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 for (loop = 0; loop <= 1; ++loop)
7599 {
7600 regmatch->rm_ic = ic;
7601 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7602 {
K.Takataeeec2542021-06-02 13:28:16 +02007603 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007604 {
7605 if (match_str((char_u *)names[match], regmatch, *matches,
7606 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 {
7608 if (loop == 0)
7609 num_normal++;
7610 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007611 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 }
7615 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7616 opt_idx++)
7617 {
7618 if (options[opt_idx].var == NULL)
7619 continue;
7620 if (xp->xp_context == EXPAND_BOOL_SETTINGS
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007621 && !(options[opt_idx].flags & P_BOOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007623 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 if (is_term_opt && num_normal > 0)
7625 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007626
7627 if (match_str(str, regmatch, *matches, count, (loop == 0),
7628 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 {
7630 if (loop == 0)
7631 {
7632 if (is_term_opt)
7633 num_term++;
7634 else
7635 num_normal++;
7636 }
7637 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007638 count++;
7639 }
7640 else if (!fuzzy && options[opt_idx].shortname != NULL
7641 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007642 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007643 {
7644 // Compare against the abbreviated option name (for regular
7645 // expression match). Fuzzy matching (previous if) already
7646 // matches against both the expanded and abbreviated names.
7647 if (loop == 0)
7648 {
7649 if (is_term_opt)
7650 num_term++;
7651 else
7652 num_normal++;
7653 }
7654 else
7655 (*matches)[count++] = vim_strsave(str);
7656 }
7657 else if (is_term_opt)
7658 {
7659 name_buf[0] = '<';
7660 name_buf[1] = 't';
7661 name_buf[2] = '_';
7662 name_buf[3] = str[2];
7663 name_buf[4] = str[3];
7664 name_buf[5] = '>';
7665 name_buf[6] = NUL;
7666
7667 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7668 fuzzy, fuzzystr, fuzmatch))
7669 {
7670 if (loop == 0)
7671 num_term++;
7672 else
7673 count++;
7674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 }
7676 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007677
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007678 // Check terminal key codes, these are not in the option table
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7680 {
7681 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7682 {
7683 if (!isprint(str[0]) || !isprint(str[1]))
7684 continue;
7685
7686 name_buf[0] = 't';
7687 name_buf[1] = '_';
7688 name_buf[2] = str[0];
7689 name_buf[3] = str[1];
7690 name_buf[4] = NUL;
7691
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007692 if (match_str(name_buf, regmatch, *matches, count,
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007693 (loop == 0), fuzzy, fuzzystr, fuzmatch))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007694 {
7695 if (loop == 0)
7696 num_term++;
7697 else
7698 count++;
7699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 else
7701 {
7702 name_buf[0] = '<';
7703 name_buf[1] = 't';
7704 name_buf[2] = '_';
7705 name_buf[3] = str[0];
7706 name_buf[4] = str[1];
7707 name_buf[5] = '>';
7708 name_buf[6] = NUL;
7709
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007710 if (match_str(name_buf, regmatch, *matches, count,
7711 (loop == 0), fuzzy, fuzzystr,
7712 fuzmatch))
7713 {
7714 if (loop == 0)
7715 num_term++;
7716 else
7717 count++;
7718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 }
7720 }
7721
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007722 // Check special key names.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007723 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7725 {
7726 name_buf[0] = '<';
7727 STRCPY(name_buf + 1, str);
7728 STRCAT(name_buf, ">");
7729
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007730 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7731 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 {
7733 if (loop == 0)
7734 num_term++;
7735 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007736 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 }
7738 }
7739 }
7740 if (loop == 0)
7741 {
7742 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007743 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007745 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 else
7747 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007748 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007750 *matches = ALLOC_MULT(char_u *, *numMatches);
7751 if (*matches == NULL)
7752 {
7753 *matches = (char_u **)"";
7754 return FAIL;
7755 }
7756 }
7757 else
7758 {
7759 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7760 if (fuzmatch == NULL)
7761 {
7762 *matches = (char_u **)"";
7763 return FAIL;
7764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 }
7766 }
7767 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007768
7769 if (fuzzy &&
7770 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7771 return FAIL;
7772
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 return OK;
7774}
7775
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007776// Escape an option value that can be used on the command-line with :set.
7777// Caller needs to free the returned string, unless NULL is returned.
7778 static char_u*
7779escape_option_str_cmdline(char_u *var)
7780{
7781 char_u *buf;
7782
7783 // A backslash is required before some characters. This is the reverse of
7784 // what happens in do_set().
7785 buf = vim_strsave_escaped(var, escape_chars);
7786 if (buf == NULL)
7787 return NULL;
7788
7789#ifdef BACKSLASH_IN_FILENAME
7790 // For MS-Windows et al. we don't double backslashes at the start and
7791 // before a file name character.
7792 // The reverse is found at stropt_copy_value().
7793 for (var = buf; *var != NUL; MB_PTR_ADV(var))
7794 if (var[0] == '\\' && var[1] == '\\'
7795 && expand_option_idx >= 0
7796 && (options[expand_option_idx].flags & P_EXPAND)
7797 && vim_isfilec(var[2])
7798 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
7799 STRMOVE(var, var + 1);
7800#endif
7801 return buf;
7802}
7803
7804/*
7805 * Expansion handler for :set= when we just want to fill in with the existing value.
7806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007808ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007810 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 char_u *buf;
7812
zeertzjqb0d45ec2023-01-25 15:04:22 +00007813 *numMatches = 0;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007814 *matches = ALLOC_MULT(char_u *, 1);
zeertzjqb0d45ec2023-01-25 15:04:22 +00007815 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 return FAIL;
7817
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007818 // For a terminal key code expand_option_idx is < 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 if (expand_option_idx < 0)
7820 {
7821 var = find_termcode(expand_option_name + 2);
7822 if (var == NULL)
7823 expand_option_idx = findoption(expand_option_name);
7824 }
7825
7826 if (expand_option_idx >= 0)
7827 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007828 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 option_value2string(&options[expand_option_idx], expand_option_flags);
7830 var = NameBuff;
7831 }
7832 else if (var == NULL)
7833 var = (char_u *)"";
7834
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007835 buf = escape_option_str_cmdline(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 if (buf == NULL)
7837 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007838 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 return FAIL;
7840 }
7841
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007842 (*matches)[0] = buf;
zeertzjqb0d45ec2023-01-25 15:04:22 +00007843 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 return OK;
7845}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846
7847/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007848 * Expansion handler for :set=/:set+= when the option has a custom expansion handler.
7849 */
7850 int
7851ExpandStringSetting(
7852 expand_T *xp,
7853 regmatch_T *regmatch,
7854 int *numMatches,
7855 char_u ***matches)
7856{
7857 char_u *var = NULL; // init for GCC
7858 char_u *buf;
7859
7860 if (expand_option_idx < 0 ||
7861 options[expand_option_idx].opt_expand_cb == NULL)
7862 {
7863 // Not supposed to reach this. This function is only for options with
7864 // custom expansion callbacks.
7865 return FAIL;
7866 }
7867
7868 optexpand_T args;
7869 args.oe_varp = get_varp_scope(&options[expand_option_idx], expand_option_flags);
7870 args.oe_append = expand_option_append;
7871 args.oe_regmatch = regmatch;
7872 args.oe_xp = xp;
7873 args.oe_set_arg = xp->xp_line + expand_option_start_col;
7874 args.oe_include_orig_val =
7875 !expand_option_append &&
7876 (*args.oe_set_arg == NUL);
7877
7878 // Retrieve the existing value, but escape it as a reverse of setting it.
7879 // We technically only need to do this when oe_append or
7880 // oe_include_orig_val is true.
7881 option_value2string(&options[expand_option_idx], expand_option_flags);
7882 var = NameBuff;
7883 buf = escape_option_str_cmdline(var);
7884 if (buf == NULL)
7885 return FAIL;
7886
7887 args.oe_opt_value = buf;
7888
7889 int num_ret = options[expand_option_idx].opt_expand_cb(&args, numMatches, matches);
7890
7891 vim_free(buf);
7892 return num_ret;
7893}
7894
7895/*
7896 * Expansion handler for :set-=
7897 */
7898 int
7899ExpandSettingSubtract(
7900 expand_T *xp,
7901 regmatch_T *regmatch,
7902 int *numMatches,
7903 char_u ***matches)
7904{
7905 if (expand_option_idx < 0)
7906 // term option
7907 return ExpandOldSetting(numMatches, matches);
7908
7909 char_u *option_val = *(char_u**)get_option_varp_scope(
7910 expand_option_idx, expand_option_flags);
7911
7912 long_u option_flags = options[expand_option_idx].flags;
7913
7914 if (option_flags & P_NUM)
7915 return ExpandOldSetting(numMatches, matches);
7916 else if (option_flags & P_COMMA)
7917 {
7918 // Split the option by comma, then present each option to the user if
7919 // it matches the pattern.
7920 // This condition needs to go first, because 'whichwrap' has both
7921 // P_COMMA and P_FLAGLIST.
7922 garray_T ga;
7923
7924 char_u *item;
7925 char_u *option_copy;
7926 char_u *next_val;
7927 char_u *comma;
7928
7929 if (*option_val == NUL)
7930 return FAIL;
7931
7932 // Make a copy as we need to inject null characters destructively.
7933 option_copy = vim_strsave(option_val);
7934 if (option_copy == NULL)
7935 return FAIL;
7936 next_val = option_copy;
7937
7938 ga_init2(&ga, sizeof(char_u *), 10);
7939
7940 do
7941 {
7942 item = next_val;
7943 comma = vim_strchr(next_val, ',');
7944 while (comma != NULL && comma != next_val && *(comma - 1) == '\\')
7945 {
7946 // "\," is interpreted as a literal comma rather than option
7947 // separator when reading options in copy_option_part(). Skip
7948 // it.
7949 comma = vim_strchr(comma + 1, ',');
7950 }
7951 if (comma != NULL)
7952 {
7953 *comma = NUL; // null-terminate this value, required by later functions
7954 next_val = comma + 1;
7955 }
7956 else
7957 next_val = NULL;
7958
7959 if (*item == NUL)
7960 // empty value, don't add to list
7961 continue;
7962
7963 if (!vim_regexec(regmatch, item, (colnr_T)0))
7964 continue;
7965
7966 char_u *buf = escape_option_str_cmdline(item);
7967 if (buf == NULL)
7968 {
7969 vim_free(option_copy);
7970 ga_clear_strings(&ga);
7971 return FAIL;
7972 }
7973 if (ga_add_string(&ga, buf) != OK)
7974 {
7975 vim_free(buf);
7976 break;
7977 }
7978 } while (next_val != NULL);
7979
7980 vim_free(option_copy);
7981
7982 *matches = ga.ga_data;
7983 *numMatches = ga.ga_len;
7984 return OK;
7985 }
7986 else if (option_flags & P_FLAGLIST)
7987 {
7988 // Only present the flags that are set on the option as the other flags
7989 // are not meaningful to do set-= on.
7990
7991 if (*xp->xp_pattern != NUL)
7992 {
7993 // Don't suggest anything if cmdline is non-empty. Vim's set-=
7994 // behavior requires consecutive strings and it's usually
7995 // unintuitive to users if ther try to subtract multiple flags at
7996 // once.
7997 return FAIL;
7998 }
7999
Yee Cheng Chin6d113472023-10-02 21:38:39 +02008000 size_t num_flags = STRLEN(option_val);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008001 if (num_flags == 0)
8002 return FAIL;
8003
8004 *matches = ALLOC_MULT(char_u *, num_flags + 1);
8005 if (*matches == NULL)
8006 return FAIL;
8007
8008 int count = 0;
8009 char_u *p;
8010
8011 p = vim_strsave(option_val);
8012 if (p == NULL)
8013 {
8014 VIM_CLEAR(*matches);
8015 return FAIL;
8016 }
8017 (*matches)[count++] = p;
8018
8019 if (num_flags > 1)
8020 {
8021 // If more than one flags, split the flags up and expose each
8022 // character as individual choice.
8023 for (char_u *flag = option_val; *flag != NUL; flag++)
8024 {
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02008025 p = vim_strnsave(flag, 1);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008026 if (p == NULL)
8027 break;
8028 (*matches)[count++] = p;
8029 }
8030 }
8031
8032 *numMatches = count;
8033 return OK;
8034 }
8035
8036 return ExpandOldSetting(numMatches, matches);
8037}
8038
8039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 * Get the value for the numeric or string option *opp in a nice format into
8041 * NameBuff[]. Must not be called with a hidden option!
8042 */
8043 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008044option_value2string(
8045 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008046 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047{
8048 char_u *varp;
8049
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008050 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051
8052 if (opp->flags & P_NUM)
8053 {
8054 long wc = 0;
8055
8056 if (wc_use_keyname(varp, &wc))
8057 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
8058 else if (wc != 0)
8059 STRCPY(NameBuff, transchar((int)wc));
8060 else
8061 sprintf((char *)NameBuff, "%ld", *(long *)varp);
8062 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008063 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 {
8065 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008066 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 NameBuff[0] = NUL;
8068#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008069 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008070 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 STRCPY(NameBuff, "*****");
8072#endif
8073 else if (opp->flags & P_EXPAND)
8074 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008075 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 else if ((char_u **)opp->var == &p_pt)
8077 str2specialbuf(p_pt, NameBuff, MAXPATHL);
8078 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008079 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 }
8081}
8082
8083/*
8084 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
8085 * printed as a keyname.
8086 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
8087 */
8088 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008089wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090{
8091 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
8092 {
8093 *wcp = *(long *)varp;
8094 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
8095 return TRUE;
8096 }
8097 return FALSE;
8098}
8099
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 * Return TRUE if "x" is present in 'shortmess' option, or
8102 * 'shortmess' contains 'a' and "x" is present in SHM_A.
8103 */
8104 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008105shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01008107 return p_shm != NULL &&
8108 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 || (vim_strchr(p_shm, 'a') != NULL
8110 && vim_strchr((char_u *)SHM_A, x) != NULL));
8111}
8112
8113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
8115 *
8116 * Reset 'compatible' and set the values for options that didn't get set yet
8117 * to the Vim defaults.
8118 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008119 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 */
8121 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008122vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008124 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00008125 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008126 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127
8128 if (!option_was_set((char_u *)"cp"))
8129 {
8130 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02008131 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
8133 set_option_default(opt_idx, OPT_FREE, FALSE);
8134 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008135 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008137
8138 if (fname != NULL)
8139 {
8140 p = vim_getenv(envname, &dofree);
8141 if (p == NULL)
8142 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008143 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008144 p = FullName_save(fname, FALSE);
8145 if (p != NULL)
8146 {
8147 vim_setenv(envname, p);
8148 vim_free(p);
8149 }
8150 }
8151 else if (dofree)
8152 vim_free(p);
8153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154}
8155
8156/*
8157 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
8158 */
8159 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008160change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008162 int opt_idx;
8163
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 if (p_cp != on)
8165 {
8166 p_cp = on;
8167 compatible_set();
8168 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008169 opt_idx = findoption((char_u *)"cp");
8170 if (opt_idx >= 0)
8171 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172}
8173
8174/*
8175 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02008176 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 */
8178 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008179option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180{
8181 int idx;
8182
8183 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008184 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 return FALSE;
8186 if (options[idx].flags & P_WAS_SET)
8187 return TRUE;
8188 return FALSE;
8189}
8190
8191/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008192 * Reset the flag indicating option "name" was set.
8193 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02008194 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008195reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008196{
8197 int idx = findoption(name);
8198
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008199 if (idx < 0)
8200 return FAIL;
8201
8202 options[idx].flags &= ~P_WAS_SET;
8203 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008204}
8205
8206/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 * compatible_set() - Called when 'compatible' has been set or unset.
8208 *
8209 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
8210 * flag) to a Vi compatible value.
8211 * When 'compatible' is unset: Set all options that have a different default
8212 * for Vim (without the P_VI_DEF flag) to that default.
8213 */
8214 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008215compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216{
8217 int opt_idx;
8218
Bram Moolenaardac13472019-09-16 21:06:21 +02008219 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
8221 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
8222 set_option_default(opt_idx, OPT_FREE, p_cp);
8223 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008224 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225}
8226
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 * Check if backspacing over something is allowed.
8229 */
8230 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008231can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008232 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02008234#ifdef FEAT_JOB_CHANNEL
8235 if (what == BS_START && bt_prompt(curbuf))
8236 return FALSE;
8237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 switch (*p_bs)
8239 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008240 case '3': return TRUE;
8241 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 case '1': return (what != BS_START);
8243 case '0': return FALSE;
8244 }
8245 return vim_strchr(p_bs, what) != NULL;
8246}
8247
8248/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01008249 * Return the effective 'scrolloff' value for the current window, using the
8250 * global value when appropriate.
8251 */
8252 long
8253get_scrolloff_value(void)
8254{
8255 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
8256}
8257
8258/*
8259 * Return the effective 'sidescrolloff' value for the current window, using the
8260 * global value when appropriate.
8261 */
8262 long
8263get_sidescrolloff_value(void)
8264{
8265 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
8266}
8267
8268/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008269 * Get the local or global value of 'backupcopy'.
8270 */
8271 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008272get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008273{
8274 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
8275}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02008276
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008277#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02008278/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00008279 * Get the local or global value of 'formatlistpat'.
8280 */
8281 char_u *
8282get_flp_value(buf_T *buf)
8283{
Christian Brabandtc53b4672022-01-15 10:01:05 +00008284 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
8285 return p_flp;
8286 return buf->b_p_flp;
8287}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008288#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00008289
8290/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02008291 * Get the local or global value of the 'virtualedit' flags.
8292 */
8293 unsigned int
8294get_ve_flags(void)
8295{
Gary Johnson51ad8502021-08-03 18:33:08 +02008296 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
8297 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02008298}
8299
Bram Moolenaaree857022019-11-09 23:26:40 +01008300#if defined(FEAT_LINEBREAK) || defined(PROTO)
8301/*
8302 * Get the local or global value of 'showbreak'.
8303 */
8304 char_u *
8305get_showbreak_value(win_T *win)
8306{
8307 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8308 return p_sbr;
8309 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8310 return empty_option;
8311 return win->w_p_sbr;
8312}
8313#endif
8314
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008315#if defined(FEAT_EVAL) || defined(PROTO)
8316/*
8317 * Get window or buffer local options.
8318 */
8319 dict_T *
8320get_winbuf_options(int bufopt)
8321{
8322 dict_T *d;
8323 int opt_idx;
8324
8325 d = dict_alloc();
8326 if (d == NULL)
8327 return NULL;
8328
Bram Moolenaardac13472019-09-16 21:06:21 +02008329 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008330 {
8331 struct vimoption *opt = &options[opt_idx];
8332
8333 if ((bufopt && (opt->indir & PV_BUF))
8334 || (!bufopt && (opt->indir & PV_WIN)))
8335 {
8336 char_u *varp = get_varp(opt);
8337
8338 if (varp != NULL)
8339 {
8340 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008341 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008342 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008343 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008344 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008345 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008346 }
8347 }
8348 }
8349
8350 return d;
8351}
8352#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008353
Bram Moolenaardac13472019-09-16 21:06:21 +02008354#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008355/*
8356 * This is called when 'culopt' is changed
8357 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008358 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008359fill_culopt_flags(char_u *val, win_T *wp)
8360{
8361 char_u *p;
8362 char_u culopt_flags_new = 0;
8363
8364 if (val == NULL)
8365 p = wp->w_p_culopt;
8366 else
8367 p = val;
8368 while (*p != NUL)
8369 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008370 // Note: Keep this in sync with p_culopt_values.
Bram Moolenaar017ba072019-09-14 21:01:23 +02008371 if (STRNCMP(p, "line", 4) == 0)
8372 {
8373 p += 4;
8374 culopt_flags_new |= CULOPT_LINE;
8375 }
8376 else if (STRNCMP(p, "both", 4) == 0)
8377 {
8378 p += 4;
8379 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8380 }
8381 else if (STRNCMP(p, "number", 6) == 0)
8382 {
8383 p += 6;
8384 culopt_flags_new |= CULOPT_NBR;
8385 }
8386 else if (STRNCMP(p, "screenline", 10) == 0)
8387 {
8388 p += 10;
8389 culopt_flags_new |= CULOPT_SCRLINE;
8390 }
8391
8392 if (*p != ',' && *p != NUL)
8393 return FAIL;
8394 if (*p == ',')
8395 ++p;
8396 }
8397
8398 // Can't have both "line" and "screenline".
8399 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8400 return FAIL;
8401 wp->w_p_culopt_flags = culopt_flags_new;
8402
8403 return OK;
8404}
8405#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008406
8407/*
8408 * Get the value of 'magic' adjusted for Vim9 script.
8409 */
8410 int
8411magic_isset(void)
8412{
8413 switch (magic_overruled)
8414 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008415 case OPTION_MAGIC_ON: return TRUE;
8416 case OPTION_MAGIC_OFF: return FALSE;
8417 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008418 }
8419#ifdef FEAT_EVAL
8420 if (in_vim9script())
8421 return TRUE;
8422#endif
8423 return p_magic;
8424}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008425
8426/*
8427 * Set the callback function value for an option that accepts a function name,
8428 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8429 * Returns OK if the option is successfully set to a function, otherwise
8430 * returns FAIL.
8431 */
8432 int
8433option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8434{
8435#ifdef FEAT_EVAL
8436 typval_T *tv;
8437 callback_T cb;
8438
8439 if (optval == NULL || *optval == NUL)
8440 {
8441 free_callback(optcb);
8442 return OK;
8443 }
8444
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008445 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008446 || (STRNCMP(optval, "function(", 9) == 0)
8447 || (STRNCMP(optval, "funcref(", 8) == 0))
8448 // Lambda expression or a funcref
8449 tv = eval_expr(optval, NULL);
8450 else
8451 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008452 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008453 if (tv == NULL)
8454 return FAIL;
8455
8456 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008457 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008458 {
8459 free_tv(tv);
8460 return FAIL;
8461 }
8462
8463 free_callback(optcb);
8464 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008465 if (cb.cb_free_name)
8466 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008467 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008468
8469 // when using Vim9 style "import.funcname" it needs to be expanded to
8470 // "import#funcname".
8471 expand_autload_callback(optcb);
8472
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008473 return OK;
8474#else
8475 return FAIL;
8476#endif
8477}
Christian Brabandt757593c2023-08-22 21:44:10 +02008478
8479#if defined(FEAT_EVAL) || defined(PROTO)
8480 static void
8481didset_options_sctx(int opt_flags, char **buf)
8482{
8483 for (int i = 0; ; ++i)
8484 {
8485 if (buf[i] == NULL)
8486 break;
8487
8488 int idx = findoption((char_u *)buf[i]);
8489 if (idx >= 0)
8490 set_option_sctx_idx(idx, opt_flags, current_sctx);
8491 }
8492}
8493#endif