blob: 8123a2a2c631505cbd34fd9f5f79902df6e17596 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Code to handle user-settable options. This is all pretty much table-
12 * driven. Checklist for adding a new option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000013 * - Put it in the options array in optiondefs.h (copy an existing entry).
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 * - For a global option: Add a variable for it in option.h.
15 * - For a buffer or window local option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000016 * - Add a PV_XX macro definition to the optiondefs.h file.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017 * - Add a variable to the window or buffer struct in structs.h.
18 * - For a window option, add some code to copy_winopt().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000019 * - For a window string option, add code to check_win_options() and
20 * clear_winopt().
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 * - For a buffer option, add some code to buf_copy_options().
22 * - For a buffer string option, add code to check_buf_options().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000023 * - If it's a numeric option, add any necessary bounds checks to
24 * set_num_option().
25 * - If it's a list of flags, add some code in did_set_string_option(), search
26 * for WW_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027 * - When adding an option with expansion (P_EXPAND), but with a different
28 * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
Bram Moolenaarcea912a2016-10-12 14:20:24 +020029 * - Add documentation! One line in doc/quickref.txt, full description in
Bram Moolenaar071d4272004-06-13 20:20:40 +000030 * options.txt, and any other related places.
31 * - Add an entry in runtime/optwin.vim.
32 * When making changes:
33 * - Adjust the help for the option in doc/option.txt.
34 * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
35 * comment at the help for the 'compatible' option.
36 */
37
38#define IN_OPTION_C
39#include "vim.h"
Bram Moolenaar0eddca42019-09-12 22:26:43 +020040#include "optiondefs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010042static void set_options_default(int opt_flags);
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +010043static void set_string_default_esc(char *name, char_u *val, int escape);
Bram Moolenaarb00ef052020-09-12 14:53:53 +020044static char_u *find_dup_item(char_u *origval, char_u *newval, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010045static char_u *option_expand(int opt_idx, char_u *val);
46static void didset_options(void);
47static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000048#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010049static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000050#else
51# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
52#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010053static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
54static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, size_t errbuflen, int opt_flags);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020055static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020057static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010058static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010059static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010060static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
61static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020062static int istermoption(struct vimoption *p);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000063static char_u *get_varp_scope(struct vimoption *p, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020065static void check_win_options(win_T *win);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000066static void option_value2string(struct vimoption *, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010067static void check_winopt(winopt_T *wop);
68static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010069static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Christian Brabandt757593c2023-08-22 21:44:10 +020071#if defined(FEAT_EVAL) || defined(PROTO)
72static char *(p_bin_dep_opts[]) = {"textwidth", "wrapmargin", "modeline", "expandtab", NULL};
73static char *(p_paste_dep_opts[]) = {"autoindent", "expandtab", "ruler", "showmatch", "smarttab",
74 "softtabstop", "textwidth", "wrapmargin",
75#ifdef FEAT_RIGHTLEFT
76 "hkmap", "revins",
77#endif
78#ifdef FEAT_VARTABS
79 "varsofttabstop",
80#endif
81 NULL};
82static void didset_options_sctx(int opt_flags, char **buf);
83#endif
84
85
Bram Moolenaar071d4272004-06-13 20:20:40 +000086/*
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000087 * Initialize the 'shell' option to a default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 */
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000089 static void
90set_init_default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000091{
92 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000093
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000094 // Find default value for 'shell' option.
95 // Don't use it if it is empty.
Bram Moolenaar7c626922005-02-07 22:01:03 +000096 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010097#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +000098 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010099 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +0000101 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200102#if defined(MSWIN)
103 {
104 // For MS-Windows put the path in quotes instead of escaping spaces.
105 char_u *cmd;
106 size_t len;
107
108 if (vim_strchr(p, ' ') != NULL)
109 {
110 len = STRLEN(p) + 3; // two quotes and a trailing NUL
111 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +0200112 if (cmd != NULL)
113 {
114 vim_snprintf((char *)cmd, len, "\"%s\"", p);
115 set_string_default("sh", cmd);
116 vim_free(cmd);
117 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200118 }
119 else
120 set_string_default("sh", p);
121 }
122#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100123 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200124#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000125}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000127/*
128 * Set the default for 'backupskip' to include environment variables for
129 * temp files.
130 */
131 static void
132set_init_default_backupskip(void)
133{
134 int opt_idx;
135 long_u n;
136 char_u *p;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100137#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000138 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100139#else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000140 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100141#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000142 int len;
143 garray_T ga;
Bram Moolenaar14338022023-03-15 22:05:44 +0000144 char_u *item;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200145
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000146 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000148 ga_init2(&ga, 1, 100);
149 for (n = 0; n < (long)ARRAY_LENGTH(names); ++n)
150 {
Bram Moolenaar14338022023-03-15 22:05:44 +0000151 int mustfree = FALSE;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100152#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000153 if (*names[n] == NUL)
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100154# ifdef MACOS_X
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000155 p = (char_u *)"/private/tmp";
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100156# else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000157 p = (char_u *)"/tmp";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000159 else
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100160#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000161 p = vim_getenv((char_u *)names[n], &mustfree);
162 if (p != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000164 // First time count the NUL, otherwise count the ','.
165 len = (int)STRLEN(p) + 3;
166 item = alloc(len);
Bram Moolenaar14338022023-03-15 22:05:44 +0000167 if (item != NULL)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000168 {
Bram Moolenaar14338022023-03-15 22:05:44 +0000169 STRCPY(item, p);
170 add_pathsep(item);
171 STRCAT(item, "*");
172 if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
173 == NULL
174 && ga_grow(&ga, len) == OK)
175 {
176 if (ga.ga_len > 0)
177 STRCAT(ga.ga_data, ",");
178 STRCAT(ga.ga_data, item);
179 ga.ga_len += len;
180 }
181 vim_free(item);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000184 if (mustfree)
185 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000187 if (ga.ga_data != NULL)
188 {
189 set_string_default("bsk", ga.ga_data);
190 vim_free(ga.ga_data);
191 }
192}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000194/*
195 * Initialize the 'maxmemtot' and 'maxmem' options to a default value.
196 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory.
197 */
198 static void
199set_init_default_maxmemtot(void)
200{
201 int opt_idx;
202 long_u n;
203
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 opt_idx = findoption((char_u *)"maxmemtot");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000205 if (opt_idx < 0)
206 return;
207
208#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
209 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 {
ichizok02560422022-04-05 14:18:44 +0100212#if defined(HAVE_AVAIL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000213 // Use amount of memory available at this moment.
214 n = (mch_avail_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100215#elif defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000216 // Use amount of memory available to Vim.
217 n = (mch_total_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100218#else
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000219 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000221 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
222 opt_idx = findoption((char_u *)"maxmem");
223 if (opt_idx >= 0)
224 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000225#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000226 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
227 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000228#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000229 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000232}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000234/*
235 * Initialize the 'cdpath' option to a default value.
236 */
237 static void
238set_init_default_cdpath(void)
239{
240 int opt_idx;
241 char_u *cdpath;
242 char_u *buf;
243 int i;
244 int j;
245 int mustfree = FALSE;
246
247 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
248 if (cdpath == NULL)
249 return;
250
251 buf = alloc((STRLEN(cdpath) << 1) + 2);
252 if (buf != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000254 buf[0] = ','; // start with ",", current dir first
255 j = 1;
256 for (i = 0; cdpath[i] != NUL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000258 if (vim_ispathlistsep(cdpath[i]))
259 buf[j++] = ',';
260 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000262 if (cdpath[i] == ' ' || cdpath[i] == ',')
263 buf[j++] = '\\';
264 buf[j++] = cdpath[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 }
266 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000267 buf[j] = NUL;
268 opt_idx = findoption((char_u *)"cdpath");
269 if (opt_idx >= 0)
270 {
271 options[opt_idx].def_val[VI_DEFAULT] = buf;
272 options[opt_idx].flags |= P_DEF_ALLOCED;
273 }
274 else
275 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000277 if (mustfree)
278 vim_free(cdpath);
279}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000281/*
282 * Initialize the 'printencoding' option to a default value.
283 */
284 static void
285set_init_default_printencoding(void)
286{
ichizok02560422022-04-05 14:18:44 +0100287#if defined(FEAT_POSTSCRIPT) && \
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000288 (defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100289 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100291# if defined(MSWIN)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000292 (char_u *)"cp1252"
ichizok02560422022-04-05 14:18:44 +0100293# elif defined(VMS)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000294 (char_u *)"dec-mcs"
ichizok02560422022-04-05 14:18:44 +0100295# elif defined(MAC)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000296 (char_u *)"mac-roman"
ichizok02560422022-04-05 14:18:44 +0100297# else // HPUX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000298 (char_u *)"hp-roman8"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000300 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000302}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303
304#ifdef FEAT_POSTSCRIPT
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000305/*
306 * Initialize the 'printexpr' option to a default value.
307 */
308 static void
309set_init_default_printexpr(void)
310{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100311 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100313# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000314 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
ichizok02560422022-04-05 14:18:44 +0100315# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
317
ichizok02560422022-04-05 14:18:44 +0100318# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320# endif
321 );
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000322}
323#endif
324
325#ifdef UNIX
326/*
327 * Force restricted-mode on for "nologin" or "false" $SHELL
328 */
329 static void
330set_init_restricted_mode(void)
331{
332 char_u *p;
333
334 p = get_isolated_shell_name();
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000335 if (p == NULL)
336 return;
337 if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
338 restricted = TRUE;
339 vim_free(p);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000340}
341#endif
342
343#ifdef CLEAN_RUNTIMEPATH
344/*
345 * When Vim is started with the "--clean" argument, set the default value
346 * for the 'runtimepath' and 'packpath' options.
347 */
348 static void
349set_init_clean_rtp(void)
350{
351 int opt_idx;
352
353 opt_idx = findoption((char_u *)"runtimepath");
354 if (opt_idx >= 0)
355 {
356 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
357 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
358 }
359 opt_idx = findoption((char_u *)"packpath");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000360 if (opt_idx < 0)
361 return;
362 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
363 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000364}
365#endif
366
367/*
368 * Expand environment variables and things like "~" for the defaults.
369 * If option_expand() returns non-NULL the variable is expanded. This can
370 * only happen for non-indirect options.
371 * Also set the default to the expanded value, so ":set" does not list
372 * them.
373 * Don't set the P_ALLOCED flag, because we don't want to free the
374 * default.
375 */
376 static void
377set_init_expand_env(void)
378{
379 int opt_idx;
380 char_u *p;
381
382 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
383 {
384 if ((options[opt_idx].flags & P_GETTEXT)
385 && options[opt_idx].var != NULL)
386 p = (char_u *)_(*(char **)options[opt_idx].var);
387 else
388 p = option_expand(opt_idx, NULL);
389 if (p != NULL && (p = vim_strsave(p)) != NULL)
390 {
391 *(char_u **)options[opt_idx].var = p;
392 // VIMEXP
393 // Defaults for all expanded options are currently the same for Vi
394 // and Vim. When this changes, add some code here! Also need to
395 // split P_DEF_ALLOCED in two.
396 if (options[opt_idx].flags & P_DEF_ALLOCED)
397 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
398 options[opt_idx].def_val[VI_DEFAULT] = p;
399 options[opt_idx].flags |= P_DEF_ALLOCED;
400 }
401 }
402}
403
404/*
405 * Initialize the 'LANG' environment variable to a default value.
406 */
407 static void
408set_init_lang_env(void)
409{
410#if defined(MSWIN) && defined(FEAT_GETTEXT)
411 // If $LANG isn't set, try to get a good value for it. This makes the
412 // right language be used automatically. Don't do this for English.
413 if (mch_getenv((char_u *)"LANG") == NULL)
414 {
415 char buf[20];
416 long_u n;
417
418 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
419 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
420 // only the first two.
421 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
422 (LPTSTR)buf, 20);
423 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
424 {
425 // There are a few exceptions (probably more)
426 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
427 STRCPY(buf, "zh_TW");
428 else if (STRNICMP(buf, "chs", 3) == 0
429 || STRNICMP(buf, "zhc", 3) == 0)
430 STRCPY(buf, "zh_CN");
431 else if (STRNICMP(buf, "jp", 2) == 0)
432 STRCPY(buf, "ja");
433 else
434 buf[2] = NUL; // truncate to two-letter code
435 vim_setenv((char_u *)"LANG", (char_u *)buf);
436 }
437 }
438#elif defined(MACOS_CONVERT)
439 // Moved to os_mac_conv.c to avoid dependency problems.
440 mac_lang_init();
441#endif
442}
443
444/*
445 * Initialize the 'encoding' option to a default value.
446 */
447 static void
448set_init_default_encoding(void)
449{
450 char_u *p;
451 int opt_idx;
452
Igor Todorovski497e5282024-01-12 17:59:18 +0100453# if defined(MSWIN) || defined(__MVS__)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000454 // MS-Windows has builtin support for conversion to and from Unicode, using
455 // "utf-8" for 'encoding' should work best for most users.
Igor Todorovski497e5282024-01-12 17:59:18 +0100456 // z/OS built should default to UTF-8 mode as setlocale does not respect utf-8 environment variable locales
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000457 p = vim_strsave((char_u *)ENC_DFLT);
458# else
459 // enc_locale() will try to find the encoding of the current locale.
460 // This works best for properly configured systems, old and new.
461 p = enc_locale();
462# endif
463 if (p == NULL)
464 return;
465
466 // Try setting 'encoding' and check if the value is valid.
467 // If not, go back to the default encoding.
468 char_u *save_enc = p_enc;
469 p_enc = p;
470 if (STRCMP(p_enc, "gb18030") == 0)
471 {
472 // We don't support "gb18030", but "cp936" is a good substitute
473 // for practical purposes, thus use that. It's not an alias to
474 // still support conversion between gb18030 and utf-8.
475 p_enc = vim_strsave((char_u *)"cp936");
476 vim_free(p);
477 }
478 if (mb_init() == NULL)
479 {
480 opt_idx = findoption((char_u *)"encoding");
481 if (opt_idx >= 0)
482 {
483 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
484 options[opt_idx].flags |= P_DEF_ALLOCED;
485 }
486
487#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
488 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
489 {
490 // Adjust the default for 'isprint' and 'iskeyword' to match
491 // latin1. Also set the defaults for when 'nocompatible' is
492 // set.
493 set_string_option_direct((char_u *)"isp", -1,
494 ISP_LATIN1, OPT_FREE, SID_NONE);
495 set_string_option_direct((char_u *)"isk", -1,
496 ISK_LATIN1, OPT_FREE, SID_NONE);
497 opt_idx = findoption((char_u *)"isp");
498 if (opt_idx >= 0)
499 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
500 opt_idx = findoption((char_u *)"isk");
501 if (opt_idx >= 0)
502 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
503 (void)init_chartab();
504 }
505#endif
506
507#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
508 // Win32 console: When GetACP() returns a different value from
509 // GetConsoleCP() set 'termencoding'.
510 if (
511# ifdef VIMDLL
512 (!gui.in_use && !gui.starting) &&
513# endif
514 GetACP() != GetConsoleCP())
515 {
516 char buf[50];
517
518 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
519 // Use an alternative value.
520 if (GetConsoleCP() == 0)
521 sprintf(buf, "cp%ld", (long)GetACP());
522 else
523 sprintf(buf, "cp%ld", (long)GetConsoleCP());
524 p_tenc = vim_strsave((char_u *)buf);
525 if (p_tenc != NULL)
526 {
527 opt_idx = findoption((char_u *)"termencoding");
528 if (opt_idx >= 0)
529 {
530 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
531 options[opt_idx].flags |= P_DEF_ALLOCED;
532 }
533 convert_setup(&input_conv, p_tenc, p_enc);
534 convert_setup(&output_conv, p_enc, p_tenc);
535 }
536 else
537 p_tenc = empty_option;
538 }
539#endif
540#if defined(MSWIN)
541 // $HOME may have characters in active code page.
542 init_homedir();
543#endif
544 }
545 else
546 {
547 vim_free(p_enc);
548 p_enc = save_enc;
549 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000550}
551
552/*
553 * Initialize the options, first part.
554 *
555 * Called only once from main(), just after creating the first buffer.
556 * If "clean_arg" is TRUE Vim was started with --clean.
557 */
558 void
559set_init_1(int clean_arg)
560{
561#ifdef FEAT_LANGMAP
562 langmap_init();
563#endif
564
565 // Be Vi compatible by default
566 p_cp = TRUE;
567
568 // Use POSIX compatibility when $VIM_POSIX is set.
569 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
570 {
571 set_string_default("cpo", (char_u *)CPO_ALL);
572 set_string_default("shm", (char_u *)SHM_POSIX);
573 }
574
575 set_init_default_shell();
576 set_init_default_backupskip();
577 set_init_default_maxmemtot();
578 set_init_default_cdpath();
579 set_init_default_printencoding();
580#ifdef FEAT_POSTSCRIPT
581 set_init_default_printexpr();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582#endif
583
584 /*
585 * Set all the options (except the terminal options) to their default
586 * value. Also set the global value for local options.
587 */
588 set_options_default(0);
589
matveytadbb1bf2022-02-01 17:26:12 +0000590#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000591 set_init_restricted_mode();
matveytadbb1bf2022-02-01 17:26:12 +0000592#endif
593
Bram Moolenaar07268702018-03-01 21:57:32 +0100594#ifdef CLEAN_RUNTIMEPATH
595 if (clean_arg)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000596 set_init_clean_rtp();
Bram Moolenaar07268702018-03-01 21:57:32 +0100597#endif
598
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599#ifdef FEAT_GUI
600 if (found_reverse_arg)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100601 set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602#endif
603
604 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100605 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100606 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 check_buf_options(curbuf);
608 check_win_options(curwin);
609 check_options();
610
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100611 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 didset_options();
613
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000614#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100615 // Use the current chartab for the generic chartab. This is not in
616 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000617 init_spell_chartab();
618#endif
619
K.Takatace3189d2023-02-15 19:13:43 +0000620 set_init_default_encoding();
621
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000622 // Expand environment variables and things like "~" for the defaults.
623 set_init_expand_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100625 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100628 // Detect use of mlterm.
629 // Mlterm is a terminal emulator akin to xterm that has some special
630 // abilities (bidi namely).
631 // NOTE: mlterm's author is being asked to 'set' a variable
632 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 if (mch_getenv((char_u *)"MLTERM") != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100634 set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635#endif
636
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200637 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000639 set_init_lang_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
641#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100642 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 set_helplang_default(get_mess_lang());
644#endif
645}
646
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200647static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
648
649/*
650 * Set the "fileencodings" option to the default value for when 'encoding' is
651 * utf-8.
652 */
653 void
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +0000654set_fencs_unicode(void)
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200655{
656 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
657 OPT_FREE, 0);
658}
659
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660/*
661 * Set an option to its default value.
662 * This does not take care of side effects!
663 */
664 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100665set_option_default(
666 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100667 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
668 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100670 char_u *varp; // pointer to variable for current option
671 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000673 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
675
676 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
677 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100678 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 {
680 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
681 if (flags & P_STRING)
682 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200683 // 'fencs' default value depends on 'encoding'
684 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
685 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100686 // Use set_string_option_direct() for local options to handle
687 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200688 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200689 set_string_option_direct(NULL, opt_idx,
690 options[opt_idx].def_val[dvi], opt_flags, 0);
691 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200693 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
694 free_string_option(*(char_u **)(varp));
695 *(char_u **)varp = options[opt_idx].def_val[dvi];
696 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 }
698 }
699 else if (flags & P_NUM)
700 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000701 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 win_comp_scroll(curwin);
703 else
704 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100705 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
706
707 if ((long *)varp == &curwin->w_p_so
708 || (long *)varp == &curwin->w_p_siso)
709 // 'scrolloff' and 'sidescrolloff' local values have a
710 // different default value than the global default.
711 *(long *)varp = -1;
712 else
713 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100714 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 if (both)
716 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100717 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 }
719 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100720 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100722 // the cast to long is required for Manx C, long_i is needed for
723 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000724 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000725#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100726 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000727 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
728 *(int *)varp = FALSE;
729#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100730 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 if (both)
732 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
733 *(int *)varp;
734 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000735
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100736 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000737 flagsp = insecure_flag(opt_idx, opt_flags);
738 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 }
740
741#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200742 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743#endif
744}
745
746/*
747 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200748 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 */
750 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100751set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100752 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753{
754 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000756 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757
Bram Moolenaardac13472019-09-16 21:06:21 +0200758 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200759 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200760 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100761 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200762# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200763 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200764 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200765# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100766 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 set_option_default(i, opt_flags, p_cp);
768
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100769 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000770 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200772 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773}
774
775/*
776 * Set the Vi-default value of a string option.
777 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100778 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100780 static void
781set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782{
783 char_u *p;
784 int opt_idx;
785
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100786 if (escape && vim_strchr(val, ' ') != NULL)
787 p = vim_strsave_escaped(val, (char_u *)" ");
788 else
789 p = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000790 if (p == NULL) // we don't want a NULL
791 return;
792
793 opt_idx = findoption((char_u *)name);
794 if (opt_idx < 0)
795 return;
796
797 if (options[opt_idx].flags & P_DEF_ALLOCED)
798 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
799 options[opt_idx].def_val[VI_DEFAULT] = p;
800 options[opt_idx].flags |= P_DEF_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801}
802
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100803 void
804set_string_default(char *name, char_u *val)
805{
806 set_string_default_esc(name, val, FALSE);
807}
808
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200810 * For an option value that contains comma separated items, find "newval" in
811 * "origval". Return NULL if not found.
812 */
813 static char_u *
814find_dup_item(char_u *origval, char_u *newval, long_u flags)
815{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200816 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200817 size_t newlen;
818 char_u *s;
819
820 if (origval == NULL)
821 return NULL;
822
823 newlen = STRLEN(newval);
824 for (s = origval; *s != NUL; ++s)
825 {
826 if ((!(flags & P_COMMA)
827 || s == origval
828 || (s[-1] == ',' && !(bs & 1)))
829 && STRNCMP(s, newval, newlen) == 0
830 && (!(flags & P_COMMA)
831 || s[newlen] == ','
832 || s[newlen] == NUL))
833 return s;
834 // Count backslashes. Only a comma with an even number of backslashes
835 // or a single backslash preceded by a comma before it is recognized as
836 // a separator.
837 if ((s > origval + 1
838 && s[-1] == '\\'
839 && s[-2] != ',')
840 || (s == origval + 1
841 && s[-1] == '\\'))
842 ++bs;
843 else
844 bs = 0;
845 }
846 return NULL;
847}
848
849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 * Set the Vi-default value of a number option.
851 * Used for 'lines' and 'columns'.
852 */
853 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100854set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000856 int opt_idx;
857
858 opt_idx = findoption((char_u *)name);
859 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000860 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861}
862
Dominique Pelle748b3082022-01-08 12:41:16 +0000863#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200864/*
865 * Set all window-local and buffer-local options to the Vim default.
866 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200867 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200868 */
869 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200870set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200871{
872 win_T *save_curwin = curwin;
873 int i;
874
875 curwin = wp;
876 curbuf = curwin->w_buffer;
877 block_autocmds();
878
Bram Moolenaardac13472019-09-16 21:06:21 +0200879 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200880 {
881 struct vimoption *p = &(options[i]);
882 char_u *varp = get_varp_scope(p, OPT_LOCAL);
883
884 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200885 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200886 && !(options[i].flags & P_NODEFAULT)
887 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200888 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200889 }
890
891 unblock_autocmds();
892 curwin = save_curwin;
893 curbuf = curwin->w_buffer;
894}
Dominique Pelle748b3082022-01-08 12:41:16 +0000895#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200896
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000897#if defined(EXITFREE) || defined(PROTO)
898/*
899 * Free all options.
900 */
901 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100902free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000903{
904 int i;
905
Bram Moolenaardac13472019-09-16 21:06:21 +0200906 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000907 {
908 if (options[i].indir == PV_NONE)
909 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100910 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100911 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000912 free_string_option(*(char_u **)options[i].var);
913 if (options[i].flags & P_DEF_ALLOCED)
914 free_string_option(options[i].def_val[VI_DEFAULT]);
915 }
916 else if (options[i].var != VAR_WIN
917 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100918 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200919 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000920 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000921 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000922 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000923}
924#endif
925
926
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927/*
928 * Initialize the options, part two: After getting Rows and Columns and
929 * setting 'term'.
930 */
931 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100932set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000934 int idx;
935
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000936 // 'scroll' defaults to half the window height. The stored default is zero,
937 // which results in the actual value computed from the window height.
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000938 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000939 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000940 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 comp_col();
942
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000943 // 'window' is only for backwards compatibility with Vi.
944 // Default is Rows - 1.
Bram Moolenaard68071d2006-05-02 22:08:30 +0000945 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000946 p_window = Rows - 1;
947 set_number_default("window", Rows - 1);
948
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100949 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100950#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000951 // If 'background' wasn't set by the user, try guessing the value,
952 // depending on the terminal name. Only need to check for terminals
953 // with a dark background, that can handle color.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000954 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000955 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
956 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000958 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100959 // don't mark it as set, when starting the GUI it may be
960 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000961 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 }
963#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000964
965#ifdef CURSOR_SHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000966 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000967#endif
968#ifdef FEAT_MOUSESHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000969 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000970#endif
971#ifdef FEAT_PRINTER
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000972 (void)parse_printoptions(NULL); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974}
975
976/*
977 * Initialize the options, part three: After reading the .vimrc
978 */
979 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100980set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100982#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983/*
984 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
985 * This is done after other initializations, where 'shell' might have been
986 * set, but only if they have not been set before.
987 */
988 char_u *p;
989 int idx_srr;
990 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100991# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 int idx_sp;
993 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995
996 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000997 if (idx_srr < 0)
998 do_srr = FALSE;
999 else
1000 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001001# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001003 if (idx_sp < 0)
1004 do_sp = FALSE;
1005 else
1006 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001007# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001008 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 if (p != NULL)
1010 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001011 // Default for p_sp is "| tee", for p_srr is ">".
1012 // For known shells it is changed here to include stderr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 if ( fnamecmp(p, "csh") == 0
1014 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001015# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 || fnamecmp(p, "csh.exe") == 0
1017 || fnamecmp(p, "tcsh.exe") == 0
1018# endif
1019 )
1020 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001021# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (do_sp)
1023 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001024# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001026# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001028# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1030 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001031# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 if (do_srr)
1033 {
1034 p_srr = (char_u *)">&";
1035 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1036 }
1037 }
Mike Williams12795022021-06-28 20:53:58 +02001038# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +02001039 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
1040 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +02001041 else if ( fnamecmp(p, "powershell") == 0
1042 || fnamecmp(p, "powershell.exe") == 0
1043 )
1044 {
1045# if defined(FEAT_QUICKFIX)
1046 if (do_sp)
1047 {
1048 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
1049 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1050 }
1051# endif
1052 if (do_srr)
1053 {
1054 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
1055 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1056 }
1057 }
1058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 else
Natanael Copa56318362021-05-06 18:46:35 +02001060 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 if ( fnamecmp(p, "sh") == 0
1062 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001063 || fnamecmp(p, "mksh") == 0
1064 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001066 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001068 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001069 || fnamecmp(p, "ash") == 0
1070 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001071 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001072# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 || fnamecmp(p, "cmd") == 0
1074 || fnamecmp(p, "sh.exe") == 0
1075 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001076 || fnamecmp(p, "mksh.exe") == 0
1077 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001079 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 || fnamecmp(p, "bash.exe") == 0
1081 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001082 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001083 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001085 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001087# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 if (do_sp)
1089 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001090# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001092# else
Mike Williamsa3d1b292021-06-30 20:56:00 +02001093 if (fnamecmp(p, "pwsh") == 0)
1094 p_sp = (char_u *)">%s 2>&1";
1095 else
1096 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001097# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1099 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001100# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 if (do_srr)
1102 {
1103 p_srr = (char_u *)">%s 2>&1";
1104 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1105 }
1106 }
1107 vim_free(p);
1108 }
1109#endif
1110
Bram Moolenaar4f974752019-02-17 17:44:42 +01001111#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001113 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1114 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001116 * set, but only if they have not been set before.
1117 * Default values depend on shell (cmd.exe is default shell):
1118 *
1119 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001120 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001121 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001122 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001123 * "sh" like shells - "-c" "\""
1124 *
1125 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 */
Mike Williams12795022021-06-28 20:53:58 +02001127 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1128 {
1129 int idx_opt;
1130
1131 idx_opt = findoption((char_u *)"shcf");
1132 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1133 {
1134 p_shcf = (char_u*)"-Command";
1135 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1136 }
1137
1138 idx_opt = findoption((char_u *)"sxq");
1139 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1140 {
1141 p_sxq = (char_u*)"\"";
1142 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1143 }
1144 }
1145 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 {
1147 int idx3;
1148
1149 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001150 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 {
1152 p_shcf = (char_u *)"-c";
1153 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1154 }
1155
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001156 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001158 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {
1160 p_sxq = (char_u *)"\"";
1161 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001164 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1165 {
1166 int idx3;
1167
1168 /*
1169 * cmd.exe on Windows will strip the first and last double quote given
1170 * on the command line, e.g. most of the time things like:
1171 * cmd /c "my path/to/echo" "my args to echo"
1172 * become:
1173 * my path/to/echo" "my args to echo
1174 * when executed.
1175 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001176 * To avoid this, set shellxquote to surround the command in
1177 * parenthesis. This appears to make most commands work, without
1178 * breaking commands that worked previously, such as
1179 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001180 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001181 idx3 = findoption((char_u *)"sxq");
1182 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1183 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001184 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001185 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1186 }
1187
Bram Moolenaara64ba222012-02-12 23:23:31 +01001188 idx3 = findoption((char_u *)"shcf");
1189 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1190 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001191 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001192 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1193 }
1194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195#endif
1196
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001197 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001198 {
1199 int idx_ffs = findoption((char_u *)"ffs");
1200
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001201 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001202 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1203 set_fileformat(default_fileformat(), OPT_LOCAL);
1204 }
1205
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207}
1208
1209#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1210/*
1211 * When 'helplang' is still at its default value, set it to "lang".
1212 * Only the first two characters of "lang" are used.
1213 */
1214 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001215set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216{
1217 int idx;
1218
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001219 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 return;
1221 idx = findoption((char_u *)"hlg");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001222 if (idx < 0 || (options[idx].flags & P_WAS_SET))
1223 return;
1224
1225 if (options[idx].flags & P_ALLOCED)
1226 free_string_option(p_hlg);
1227 p_hlg = vim_strsave(lang);
1228 if (p_hlg == NULL)
1229 p_hlg = empty_option;
1230 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001232 // zh_CN becomes "cn", zh_TW becomes "tw"
1233 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001234 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001235 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1236 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001237 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001238 // any C like setting, such as C.UTF-8, becomes "en"
1239 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1240 {
1241 p_hlg[0] = 'e';
1242 p_hlg[1] = 'n';
1243 }
1244 p_hlg[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001246 options[idx].flags |= P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247}
1248#endif
1249
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250/*
1251 * 'title' and 'icon' only default to true if they have not been set or reset
1252 * in .vimrc and we can read the old value.
1253 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1254 * they can be reset. This reduces startup time when using X on a remote
1255 * machine.
1256 */
1257 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001258set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259{
1260 int idx1;
1261 long val;
1262
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001263 // If GUI is (going to be) used, we can always set the window title and
1264 // icon name. Saves a bit of time, because the X11 display server does
1265 // not need to be contacted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001267 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 {
1269#ifdef FEAT_GUI
1270 if (gui.starting || gui.in_use)
1271 val = TRUE;
1272 else
1273#endif
1274 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001275 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 p_title = val;
1277 }
1278 idx1 = findoption((char_u *)"icon");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001279 if (idx1 < 0 || (options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001281 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001283
1284#ifdef FEAT_GUI
1285 if (gui.starting || gui.in_use)
1286 val = TRUE;
1287 else
1288#endif
1289 val = mch_can_restore_icon();
1290 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
1291 p_icon = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001294 void
1295ex_set(exarg_T *eap)
1296{
1297 int flags = 0;
1298
1299 if (eap->cmdidx == CMD_setlocal)
1300 flags = OPT_LOCAL;
1301 else if (eap->cmdidx == CMD_setglobal)
1302 flags = OPT_GLOBAL;
1303#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001304 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001305 ex_options(eap);
1306 else
1307#endif
1308 {
1309 if (eap->forceit)
1310 flags |= OPT_ONECOLUMN;
1311 (void)do_set(eap->arg, flags);
1312 }
1313}
1314
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001315/*
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001316 * :set boolean option prefix
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001317 */
Bram Moolenaar47403942022-09-21 21:12:53 +01001318typedef enum {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001319 PREFIX_NO = 0, // "no" prefix
1320 PREFIX_NONE, // no prefix
1321 PREFIX_INV, // "inv" prefix
1322} set_prefix_T;
1323
1324/*
1325 * Return the prefix type for the option name in *argp.
1326 */
1327 static set_prefix_T
1328get_option_prefix(char_u **argp)
1329{
1330 int prefix = PREFIX_NONE;
1331 char_u *arg = *argp;
1332
1333 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
1334 {
1335 prefix = PREFIX_NO;
1336 arg += 2;
1337 }
1338 else if (STRNCMP(arg, "inv", 3) == 0)
1339 {
1340 prefix = PREFIX_INV;
1341 arg += 3;
1342 }
1343
1344 *argp = arg;
1345 return prefix;
1346}
1347
1348/*
1349 * Parse the option name in "arg" and return the option index in "*opt_idxp",
1350 * and the option name length in "*lenp". For a <t_xx> option, return the key
1351 * number in "*keyp".
1352 *
1353 * Returns FAIL if an option starting with "<" doesn't end with a ">",
1354 * otherwise returns OK.
1355 */
1356 static int
1357parse_option_name(char_u *arg, int *opt_idxp, int *lenp, int *keyp)
1358{
1359 int key = 0;
1360 int len;
1361 int opt_idx;
1362
1363 if (*arg == '<')
1364 {
1365 opt_idx = -1;
1366 // look out for <t_>;>
1367 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1368 len = 5;
1369 else
1370 {
1371 len = 1;
1372 while (arg[len] != NUL && arg[len] != '>')
1373 ++len;
1374 }
1375 if (arg[len] != '>')
1376 return FAIL;
1377
1378 arg[len] = NUL; // put NUL after name
1379 if (arg[1] == 't' && arg[2] == '_') // could be term code
1380 opt_idx = findoption(arg + 1);
1381 arg[len++] = '>'; // restore '>'
1382 if (opt_idx == -1)
1383 key = find_key_option(arg + 1, TRUE);
1384 }
1385 else
1386 {
1387 int nextchar; // next non-white char after option name
1388
1389 len = 0;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001390 // The two characters after "t_" may not be alphanumeric.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001391 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1392 len = 4;
1393 else
1394 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1395 ++len;
1396 nextchar = arg[len];
1397 arg[len] = NUL; // put NUL after name
1398 opt_idx = findoption(arg);
1399 arg[len] = nextchar; // restore nextchar
1400 if (opt_idx == -1)
1401 key = find_key_option(arg, FALSE);
1402 }
1403
1404 *keyp = key;
1405 *lenp = len;
1406 *opt_idxp = opt_idx;
1407
1408 return OK;
1409}
1410
1411/*
1412 * Get the option operator (+=, ^=, -=).
1413 */
1414 static set_op_T
1415get_opt_op(char_u *arg)
1416{
1417 set_op_T op = OP_NONE;
1418
1419 if (*arg != NUL && *(arg + 1) == '=')
1420 {
1421 if (*arg == '+')
1422 op = OP_ADDING; // "+="
1423 else if (*arg == '^')
1424 op = OP_PREPENDING; // "^="
1425 else if (*arg == '-')
1426 op = OP_REMOVING; // "-="
1427 }
1428
1429 return op;
1430}
1431
1432/*
1433 * Validate whether the value of the option in "opt_idx" can be changed.
1434 * Returns FAIL if the option can be skipped or cannot be changed. Returns OK
1435 * if it can be changed.
1436 */
1437 static int
1438validate_opt_idx(int opt_idx, int opt_flags, long_u flags, char **errmsg)
1439{
1440 // Skip all options that are not window-local (used when showing
1441 // an already loaded buffer in a window).
1442 if ((opt_flags & OPT_WINONLY)
1443 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1444 return FAIL;
1445
1446 // Skip all options that are window-local (used for :vimgrep).
1447 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1448 && options[opt_idx].var == VAR_WIN)
1449 return FAIL;
1450
1451 // Disallow changing some options from modelines.
1452 if (opt_flags & OPT_MODELINE)
1453 {
1454 if (flags & (P_SECURE | P_NO_ML))
1455 {
1456 *errmsg = e_not_allowed_in_modeline;
1457 return FAIL;
1458 }
1459 if ((flags & P_MLE) && !p_mle)
1460 {
1461 *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
1462 return FAIL;
1463 }
1464#ifdef FEAT_DIFF
1465 // In diff mode some options are overruled. This avoids that
1466 // 'foldmethod' becomes "marker" instead of "diff" and that
1467 // "wrap" gets set.
1468 if (curwin->w_p_diff
1469 && opt_idx >= 0 // shut up coverity warning
1470 && (
1471# ifdef FEAT_FOLDING
1472 options[opt_idx].indir == PV_FDM ||
1473# endif
1474 options[opt_idx].indir == PV_WRAP))
1475 return FAIL;
1476#endif
1477 }
1478
1479#ifdef HAVE_SANDBOX
1480 // Disallow changing some options in the sandbox
1481 if (sandbox != 0 && (flags & P_SECURE))
1482 {
1483 *errmsg = e_not_allowed_in_sandbox;
1484 return FAIL;
1485 }
1486#endif
1487
1488 return OK;
1489}
1490
Bram Moolenaar47403942022-09-21 21:12:53 +01001491/*
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001492 * Get the Vim/Vi default value for a string option.
1493 */
1494 static char_u *
1495stropt_get_default_val(
1496 int opt_idx,
1497 char_u *varp,
1498 int flags,
1499 int cp_val)
1500{
1501 char_u *newval;
1502
1503 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1504 ? VI_DEFAULT : VIM_DEFAULT];
1505 if ((char_u **)varp == &p_bg)
1506 {
1507 // guess the value of 'background'
1508#ifdef FEAT_GUI
1509 if (gui.in_use)
1510 newval = gui_bg_default();
1511 else
1512#endif
1513 newval = term_bg_default();
1514 }
1515 else if ((char_u **)varp == &p_fencs && enc_utf8)
1516 newval = fencs_utf8_default;
1517
1518 // expand environment variables and ~ since the default value was
1519 // already expanded, only required when an environment variable was set
1520 // later
1521 if (newval == NULL)
1522 newval = empty_option;
1523 else
1524 {
1525 char_u *s = option_expand(opt_idx, newval);
1526 if (s == NULL)
1527 s = newval;
1528 newval = vim_strsave(s);
1529 }
1530
1531 return newval;
1532}
1533
1534/*
1535 * Convert the 'backspace' option number value to a string: for adding,
1536 * prepending and removing string.
1537 */
1538 static void
1539opt_backspace_nr2str(
1540 char_u *varp,
1541 char_u **origval_p,
1542 char_u **origval_l_p,
1543 char_u **origval_g_p,
1544 char_u **oldval_p)
1545{
1546 int i = getdigits((char_u **)varp);
1547
1548 switch (i)
1549 {
1550 case 0:
1551 *(char_u **)varp = empty_option;
1552 break;
1553 case 1:
1554 *(char_u **)varp = vim_strsave((char_u *)"indent,eol");
1555 break;
1556 case 2:
1557 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,start");
1558 break;
1559 case 3:
1560 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,nostop");
1561 break;
1562 }
1563 vim_free(*oldval_p);
1564 if (*origval_p == *oldval_p)
1565 *origval_p = *(char_u **)varp;
1566 if (*origval_l_p == *oldval_p)
1567 *origval_l_p = *(char_u **)varp;
1568 if (*origval_g_p == *oldval_p)
1569 *origval_g_p = *(char_u **)varp;
1570 *oldval_p = *(char_u **)varp;
1571}
1572
1573/*
1574 * Convert the 'whichwrap' option number value to a string, for backwards
1575 * compatibility with Vim 3.0.
1576 * Note: 'argp' is a pointer to a char_u pointer and is updated.
1577 */
1578 static char_u *
1579opt_whichwrap_nr2str(char_u **argp, char_u *whichwrap)
1580{
1581 *whichwrap = NUL;
1582 int i = getdigits(argp);
1583 if (i & 1)
1584 STRCAT(whichwrap, "b,");
1585 if (i & 2)
1586 STRCAT(whichwrap, "s,");
1587 if (i & 4)
1588 STRCAT(whichwrap, "h,l,");
1589 if (i & 8)
1590 STRCAT(whichwrap, "<,>,");
1591 if (i & 16)
1592 STRCAT(whichwrap, "[,],");
1593 if (*whichwrap != NUL) // remove trailing ,
1594 whichwrap[STRLEN(whichwrap) - 1] = NUL;
1595
1596 return whichwrap;
1597}
1598
1599/*
1600 * Copy the new string value into allocated memory for the option.
1601 * Can't use set_string_option_direct(), because we need to remove the
1602 * backslashes.
1603 */
1604 static char_u *
1605stropt_copy_value(
1606 char_u *origval,
1607 char_u **argp,
1608 set_op_T op,
1609 int flags UNUSED)
1610{
1611 char_u *arg = *argp;
1612 unsigned newlen;
1613 char_u *newval;
1614 char_u *s = NULL;
1615
1616 // get a bit too much
1617 newlen = (unsigned)STRLEN(arg) + 1;
1618 if (op != OP_NONE)
1619 newlen += (unsigned)STRLEN(origval) + 1;
1620 newval = alloc(newlen);
1621 if (newval == NULL) // out of mem, don't change
1622 return NULL;
1623 s = newval;
1624
1625 // Copy the string, skip over escaped chars.
1626 // For MS-DOS and WIN32 backslashes before normal file name characters
1627 // are not removed, and keep backslash at start, for "\\machine\path",
1628 // but do remove it for "\\\\machine\\path".
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001629 // The reverse is found in escape_option_str_cmdline().
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001630 while (*arg != NUL && !VIM_ISWHITE(*arg))
1631 {
1632 int i;
1633
1634 if (*arg == '\\' && arg[1] != NUL
1635#ifdef BACKSLASH_IN_FILENAME
1636 && !((flags & P_EXPAND)
1637 && vim_isfilec(arg[1])
1638 && !VIM_ISWHITE(arg[1])
1639 && (arg[1] != '\\'
1640 || (s == newval && arg[2] != '\\')))
1641#endif
1642 )
1643 ++arg; // remove backslash
1644 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
1645 {
1646 // copy multibyte char
1647 mch_memmove(s, arg, (size_t)i);
1648 arg += i;
1649 s += i;
1650 }
1651 else
1652 *s++ = *arg++;
1653 }
1654 *s = NUL;
1655
1656 *argp = arg;
1657 return newval;
1658}
1659
1660/*
1661 * Expand environment variables and ~ in string option value 'newval'.
1662 */
1663 static char_u *
1664stropt_expand_envvar(
1665 int opt_idx,
1666 char_u *origval,
1667 char_u *newval,
1668 set_op_T op)
1669{
1670 char_u *s = option_expand(opt_idx, newval);
1671 if (s == NULL)
1672 return newval;
1673
1674 vim_free(newval);
1675 unsigned newlen = (unsigned)STRLEN(s) + 1;
1676 if (op != OP_NONE)
1677 newlen += (unsigned)STRLEN(origval) + 1;
1678
1679 newval = alloc(newlen);
1680 if (newval == NULL)
1681 return NULL;
1682
1683 STRCPY(newval, s);
1684
1685 return newval;
1686}
1687
1688/*
1689 * Concatenate the original and new values of a string option, adding a "," if
1690 * needed.
1691 */
1692 static void
1693stropt_concat_with_comma(
1694 char_u *origval,
1695 char_u *newval,
1696 set_op_T op,
1697 int flags)
1698{
1699 int len = 0;
1700
1701 int comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1702 if (op == OP_ADDING)
1703 {
1704 len = (int)STRLEN(origval);
1705 // strip a trailing comma, would get 2
1706 if (comma && len > 1
1707 && (flags & P_ONECOMMA) == P_ONECOMMA
1708 && origval[len - 1] == ','
1709 && origval[len - 2] != '\\')
1710 len--;
1711 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1712 mch_memmove(newval, origval, (size_t)len);
1713 }
1714 else
1715 {
1716 len = (int)STRLEN(newval);
1717 STRMOVE(newval + len + comma, origval);
1718 }
1719 if (comma)
1720 newval[len] = ',';
1721}
1722
1723/*
1724 * Remove a value from a string option. Copy string option value in "origval"
1725 * to "newval" and then remove the string "strval" of length "len".
1726 */
1727 static void
1728stropt_remove_val(
1729 char_u *origval,
1730 char_u *newval,
1731 int flags,
1732 char_u *strval,
1733 int len)
1734{
1735 // Remove newval[] from origval[]. (Note: "len" has been set above
1736 // and is used here).
1737 STRCPY(newval, origval);
1738 if (*strval)
1739 {
1740 // may need to remove a comma
1741 if (flags & P_COMMA)
1742 {
1743 if (strval == origval)
1744 {
1745 // include comma after string
1746 if (strval[len] == ',')
1747 ++len;
1748 }
1749 else
1750 {
1751 // include comma before string
1752 --strval;
1753 ++len;
1754 }
1755 }
1756 STRMOVE(newval + (strval - origval), strval + len);
1757 }
1758}
1759
1760/*
1761 * Remove flags that appear twice in the string option value 'newval'.
1762 */
1763 static void
1764stropt_remove_dupflags(char_u *newval, int flags)
1765{
1766 char_u *s = newval;
1767
1768 // Remove flags that appear twice.
1769 while (*s)
1770 {
1771 // if options have P_FLAGLIST and P_ONECOMMA such as 'whichwrap'
1772 if (flags & P_ONECOMMA)
1773 {
1774 if (*s != ',' && *(s + 1) == ',' && vim_strchr(s + 2, *s) != NULL)
1775 {
1776 // Remove the duplicated value and the next comma.
1777 STRMOVE(s, s + 2);
1778 continue;
1779 }
1780 }
1781 else
1782 {
1783 if ((!(flags & P_COMMA) || *s != ',')
1784 && vim_strchr(s + 1, *s) != NULL)
1785 {
1786 STRMOVE(s, s + 1);
1787 continue;
1788 }
1789 }
1790 ++s;
1791 }
1792}
1793
1794/*
1795 * Get the string value specified for a ":set" command. The following set
1796 * options are supported:
1797 * set {opt}&
1798 * set {opt}<
1799 * set {opt}={val}
1800 * set {opt}:{val}
1801 */
1802 static char_u *
1803stropt_get_newval(
1804 int nextchar,
1805 int opt_idx,
1806 char_u **argp,
1807 char_u *varp,
1808 char_u **origval_arg,
1809 char_u **origval_l_arg,
1810 char_u **origval_g_arg,
1811 char_u **oldval_arg,
1812 set_op_T *op_arg,
1813 int flags,
1814 int cp_val)
1815{
1816 char_u *arg = *argp;
1817 char_u *origval = *origval_arg;
1818 char_u *origval_l = *origval_l_arg;
1819 char_u *origval_g = *origval_g_arg;
1820 char_u *oldval = *oldval_arg;
1821 set_op_T op = *op_arg;
1822 char_u *save_arg = NULL;
1823 char_u *newval;
1824 char_u *s = NULL;
1825 char_u whichwrap[80];
1826
1827 if (nextchar == '&') // set to default val
1828 newval = stropt_get_default_val(opt_idx, varp, flags, cp_val);
1829 else if (nextchar == '<') // set to global val
1830 newval = vim_strsave(*(char_u **)get_varp_scope(
1831 &(options[opt_idx]), OPT_GLOBAL));
1832 else
1833 {
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001834 ++arg; // jump to after the '=' or ':'
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001835
1836 // Set 'keywordprg' to ":help" if an empty
1837 // value was passed to :set by the user.
1838 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
1839 {
1840 save_arg = arg;
1841 arg = (char_u *)":help";
1842 }
1843 // Convert 'backspace' number to string
1844 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1845 opt_backspace_nr2str(varp, &origval, &origval_l, &origval_g,
1846 &oldval);
1847 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
1848 {
1849 // Convert 'whichwrap' number to string, for backwards
1850 // compatibility with Vim 3.0.
1851 char_u *t = opt_whichwrap_nr2str(&arg, whichwrap);
1852 save_arg = arg;
1853 arg = t;
1854 }
1855 // Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1856 // version 3.0
1857 else if (*arg == '>' && (varp == (char_u *)&p_dir
1858 || varp == (char_u *)&p_bdir))
1859 ++arg;
1860
1861 // Copy the new string into allocated memory.
1862 newval = stropt_copy_value(origval, &arg, op, flags);
1863 if (newval == NULL)
1864 goto done;
1865
1866 // Expand environment variables and ~.
1867 // Don't do it when adding without inserting a comma.
1868 if (op == OP_NONE || (flags & P_COMMA))
1869 {
1870 newval = stropt_expand_envvar(opt_idx, origval, newval, op);
1871 if (newval == NULL)
1872 goto done;
1873 }
1874
1875 // locate newval[] in origval[] when removing it and when adding to
1876 // avoid duplicates
1877 int len = 0;
1878 if (op == OP_REMOVING || (flags & P_NODUP))
1879 {
1880 len = (int)STRLEN(newval);
1881 s = find_dup_item(origval, newval, flags);
1882
1883 // do not add if already there
1884 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
1885 {
1886 op = OP_NONE;
1887 STRCPY(newval, origval);
1888 }
1889
1890 // if no duplicate, move pointer to end of original value
1891 if (s == NULL)
1892 s = origval + (int)STRLEN(origval);
1893 }
1894
1895 // concatenate the two strings; add a ',' if needed
1896 if (op == OP_ADDING || op == OP_PREPENDING)
1897 stropt_concat_with_comma(origval, newval, op, flags);
1898 else if (op == OP_REMOVING)
1899 // Remove newval[] from origval[]. (Note: "len" has been set above
1900 // and is used here).
1901 stropt_remove_val(origval, newval, flags, s, len);
1902
1903 if (flags & P_FLAGLIST)
1904 // Remove flags that appear twice.
1905 stropt_remove_dupflags(newval, flags);
1906 }
1907
1908done:
1909 if (save_arg != NULL)
1910 arg = save_arg; // arg was temporarily changed, restore it
1911 *argp = arg;
1912 *origval_arg = origval;
1913 *origval_l_arg = origval_l;
1914 *origval_g_arg = origval_g;
1915 *oldval_arg = oldval;
1916 *op_arg = op;
1917
1918 return newval;
1919}
1920
1921/*
Bram Moolenaar47403942022-09-21 21:12:53 +01001922 * Part of do_set() for string options.
1923 * Returns FAIL on failure, do not process further options.
1924 */
1925 static int
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001926do_set_option_string(
Bram Moolenaar47403942022-09-21 21:12:53 +01001927 int opt_idx,
1928 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01001929 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01001930 int nextchar,
1931 set_op_T op_arg,
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001932 long_u flags,
Bram Moolenaar47403942022-09-21 21:12:53 +01001933 int cp_val,
1934 char_u *varp_arg,
1935 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +01001936 size_t errbuflen,
Bram Moolenaar47403942022-09-21 21:12:53 +01001937 int *value_checked,
1938 char **errmsg)
1939{
Bram Moolenaar6f981142022-09-22 12:48:58 +01001940 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01001941 set_op_T op = op_arg;
1942 char_u *varp = varp_arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01001943 char_u *oldval = NULL; // previous value if *varp
1944 char_u *newval;
1945 char_u *origval = NULL;
1946 char_u *origval_l = NULL;
1947 char_u *origval_g = NULL;
1948#if defined(FEAT_EVAL)
1949 char_u *saved_origval = NULL;
1950 char_u *saved_origval_l = NULL;
1951 char_u *saved_origval_g = NULL;
1952 char_u *saved_newval = NULL;
1953#endif
Bram Moolenaar47403942022-09-21 21:12:53 +01001954
1955 // When using ":set opt=val" for a global option
1956 // with a local value the local value will be
1957 // reset, use the global value here.
1958 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
1959 && ((int)options[opt_idx].indir & PV_BOTH))
1960 varp = options[opt_idx].var;
1961
1962 // The old value is kept until we are sure that the new value is valid.
1963 oldval = *(char_u **)varp;
1964
1965 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1966 {
1967 origval_l = *(char_u **)get_varp_scope(
1968 &(options[opt_idx]), OPT_LOCAL);
1969 origval_g = *(char_u **)get_varp_scope(
1970 &(options[opt_idx]), OPT_GLOBAL);
1971
1972 // A global-local string option might have an empty option as value to
1973 // indicate that the global value should be used.
1974 if (((int)options[opt_idx].indir & PV_BOTH)
1975 && origval_l == empty_option)
1976 origval_l = origval_g;
1977 }
1978
1979 // When setting the local value of a global option, the old value may be
1980 // the global value.
1981 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
1982 origval = *(char_u **)get_varp(&options[opt_idx]);
1983 else
1984 origval = oldval;
1985
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001986 // Get the new value for the option
1987 newval = stropt_get_newval(nextchar, opt_idx, &arg, varp, &origval,
1988 &origval_l, &origval_g, &oldval, &op, flags,
1989 cp_val);
Bram Moolenaar47403942022-09-21 21:12:53 +01001990
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001991 // Set the new value.
Bram Moolenaar47403942022-09-21 21:12:53 +01001992 *(char_u **)(varp) = newval;
Bram Moolenaar339e1142023-02-15 14:26:25 +00001993 if (newval == NULL)
1994 *(char_u **)(varp) = empty_option;
Bram Moolenaar47403942022-09-21 21:12:53 +01001995
1996#if defined(FEAT_EVAL)
1997 if (!starting
1998# ifdef FEAT_CRYPT
1999 && options[opt_idx].indir != PV_KEY
2000# endif
2001 && origval != NULL && newval != NULL)
2002 {
2003 // origval may be freed by did_set_string_option(), make a copy.
2004 saved_origval = vim_strsave(origval);
2005 // newval (and varp) may become invalid if the buffer is closed by
2006 // autocommands.
2007 saved_newval = vim_strsave(newval);
2008 if (origval_l != NULL)
2009 saved_origval_l = vim_strsave(origval_l);
2010 if (origval_g != NULL)
2011 saved_origval_g = vim_strsave(origval_g);
2012 }
2013#endif
2014
2015 {
2016 long_u *p = insecure_flag(opt_idx, opt_flags);
2017 int secure_saved = secure;
2018
2019 // When an option is set in the sandbox, from a modeline or in secure
2020 // mode, then deal with side effects in secure mode. Also when the
2021 // value was set with the P_INSECURE flag and is not completely
2022 // replaced.
2023 if ((opt_flags & OPT_MODELINE)
2024#ifdef HAVE_SANDBOX
2025 || sandbox != 0
2026#endif
2027 || (op != OP_NONE && (*p & P_INSECURE)))
2028 secure = 1;
2029
2030 // Handle side effects, and set the global value for ":set" on local
2031 // options. Note: when setting 'syntax' or 'filetype' autocommands may
2032 // be triggered that can cause havoc.
2033 *errmsg = did_set_string_option(
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002034 opt_idx, (char_u **)varp, oldval, newval, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002035 errbuflen, opt_flags, op, value_checked);
Bram Moolenaar47403942022-09-21 21:12:53 +01002036
2037 secure = secure_saved;
2038 }
2039
2040#if defined(FEAT_EVAL)
2041 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00002042 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01002043 saved_origval_l, saved_origval_g, saved_newval);
2044 vim_free(saved_origval);
2045 vim_free(saved_origval_l);
2046 vim_free(saved_origval_g);
2047 vim_free(saved_newval);
2048#endif
2049
Bram Moolenaar6f981142022-09-22 12:48:58 +01002050 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002051 return *errmsg == NULL ? OK : FAIL;
2052}
2053
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002055 * Set a boolean option.
2056 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002057 */
2058 static char *
2059do_set_option_bool(
2060 int opt_idx,
2061 int opt_flags,
2062 set_prefix_T prefix,
2063 long_u flags,
2064 char_u *varp,
2065 int nextchar,
2066 int afterchar,
2067 int cp_val)
2068
2069{
2070 varnumber_T value;
2071
2072 if (nextchar == '=' || nextchar == ':')
2073 return e_invalid_argument;
Bram Moolenaar546933f2023-02-06 16:40:49 +00002074 if (opt_idx < 0 || varp == NULL)
2075 return NULL; // "cannot happen"
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002076
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002077 // ":set opt!": invert
2078 // ":set opt&": reset to default value
2079 // ":set opt<": reset to global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002080 if (nextchar == '!')
2081 value = *(int *)(varp) ^ 1;
2082 else if (nextchar == '&')
2083 value = (int)(long)(long_i)options[opt_idx].def_val[
2084 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2085 else if (nextchar == '<')
2086 {
2087 // For 'autoread' -1 means to use global value.
2088 if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL)
2089 value = -1;
2090 else
2091 value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2092 }
2093 else
2094 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002095 // ":set invopt": invert
2096 // ":set opt" or ":set noopt": set or reset
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002097 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
2098 return e_trailing_characters;
2099 if (prefix == PREFIX_INV)
2100 value = *(int *)(varp) ^ 1;
2101 else
2102 value = prefix == PREFIX_NO ? 0 : 1;
2103 }
2104
2105 return set_bool_option(opt_idx, varp, (int)value, opt_flags);
2106}
2107
2108/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002109 * Set a numeric option.
2110 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002111 */
2112 static char *
2113do_set_option_numeric(
2114 int opt_idx,
2115 int opt_flags,
2116 char_u **argp,
2117 int nextchar,
2118 set_op_T op,
2119 long_u flags,
2120 int cp_val,
2121 char_u *varp,
2122 char *errbuf,
2123 size_t errbuflen)
2124{
2125 char_u *arg = *argp;
2126 varnumber_T value;
2127 int i;
2128 char *errmsg = NULL;
2129
Bram Moolenaar546933f2023-02-06 16:40:49 +00002130 if (opt_idx < 0 || varp == NULL)
2131 return NULL; // "cannot happen"
2132 //
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002133 /*
2134 * Different ways to set a number option:
2135 * & set to default value
2136 * < set to global value
2137 * <xx> accept special key codes for 'wildchar'
2138 * c accept any non-digit for 'wildchar'
2139 * [-]0-9 set number
2140 * other error
2141 */
2142 ++arg;
2143 if (nextchar == '&')
2144 value = (long)(long_i)options[opt_idx].def_val[
2145 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2146 else if (nextchar == '<')
2147 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002148 if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL)
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002149 // for 'undolevels' NO_LOCAL_UNDOLEVEL means using the global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002150 value = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002151 else if (opt_flags == OPT_LOCAL
2152 && ((long *)varp == &curwin->w_p_siso
2153 || (long *)varp == &curwin->w_p_so))
2154 // for 'scrolloff'/'sidescrolloff' -1 means using the global value
2155 value = -1;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002156 else
2157 value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2158 }
2159 else if (((long *)varp == &p_wc || (long *)varp == &p_wcm)
2160 && (*arg == '<'
2161 || *arg == '^'
2162 || (*arg != NUL
2163 && (!arg[1] || VIM_ISWHITE(arg[1]))
2164 && !VIM_ISDIGIT(*arg))))
2165 {
2166 value = string_to_key(arg, FALSE);
2167 if (value == 0 && (long *)varp != &p_wcm)
2168 {
2169 errmsg = e_invalid_argument;
2170 goto skip;
2171 }
2172 }
2173 else if (*arg == '-' || VIM_ISDIGIT(*arg))
2174 {
2175 // Allow negative (for 'undolevels'), octal and hex numbers.
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002176 vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE, NULL);
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002177 if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i])))
2178 {
2179 errmsg = e_number_required_after_equal;
2180 goto skip;
2181 }
2182 }
2183 else
2184 {
2185 errmsg = e_number_required_after_equal;
2186 goto skip;
2187 }
2188
2189 if (op == OP_ADDING)
2190 value = *(long *)varp + value;
2191 else if (op == OP_PREPENDING)
2192 value = *(long *)varp * value;
2193 else if (op == OP_REMOVING)
2194 value = *(long *)varp - value;
2195
2196 errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen,
2197 opt_flags);
2198
2199skip:
2200 *argp = arg;
2201 return errmsg;
2202}
2203
2204/*
2205 * Set a key code (t_xx) option
2206 */
2207 static char *
2208do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar)
2209{
2210 char_u *arg = *argp;
2211 char_u *p;
2212
2213 if (nextchar == '&')
2214 {
2215 if (add_termcap_entry(key_name, TRUE) == FAIL)
2216 return e_not_found_in_termcap;
2217 }
2218 else
2219 {
2220 ++arg; // jump to after the '=' or ':'
2221 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
2222 if (*p == '\\' && p[1] != NUL)
2223 ++p;
2224 nextchar = *p;
2225 *p = NUL;
2226 add_termcode(key_name, arg, FALSE);
2227 *p = nextchar;
2228 }
2229 if (full_screen)
2230 ttest(FALSE);
2231 redraw_all_later(UPD_CLEAR);
2232
2233 *argp = arg;
2234 return NULL;
2235}
2236
2237/*
2238 * Set an option to a new value.
2239 */
2240 static char *
2241do_set_option_value(
2242 int opt_idx,
2243 int opt_flags,
2244 char_u **argp,
2245 set_prefix_T prefix,
2246 set_op_T op,
2247 long_u flags,
2248 char_u *varp,
2249 char_u *key_name,
2250 int nextchar,
2251 int afterchar,
2252 int cp_val,
2253 int *stopopteval,
2254 char *errbuf,
2255 size_t errbuflen)
2256{
2257 int value_checked = FALSE;
2258 char *errmsg = NULL;
2259 char_u *arg = *argp;
2260
2261 if (flags & P_BOOL)
2262 {
2263 // boolean option
2264 errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp,
2265 nextchar, afterchar, cp_val);
2266 if (errmsg != NULL)
2267 goto skip;
2268 }
2269 else
2270 {
2271 // numeric or string option
2272 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2273 || prefix != PREFIX_NONE)
2274 {
2275 errmsg = e_invalid_argument;
2276 goto skip;
2277 }
2278
2279 if (flags & P_NUM)
2280 {
2281 // numeric option
2282 errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar,
2283 op, flags, cp_val, varp,
2284 errbuf, errbuflen);
2285 if (errmsg != NULL)
2286 goto skip;
2287 }
2288 else if (opt_idx >= 0)
2289 {
2290 // string option
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002291 if (do_set_option_string(opt_idx, opt_flags, &arg, nextchar, op,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002292 flags, cp_val, varp, errbuf, errbuflen,
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002293 &value_checked, &errmsg) == FAIL)
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002294 {
2295 if (errmsg != NULL)
2296 goto skip;
2297 *stopopteval = TRUE;
2298 goto skip;
2299 }
2300 }
2301 else
2302 {
2303 // key code option
2304 errmsg = do_set_option_keycode(&arg, key_name, nextchar);
2305 if (errmsg != NULL)
2306 goto skip;
2307 }
2308 }
2309
2310 if (opt_idx >= 0)
2311 did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked);
2312
2313skip:
2314 *argp = arg;
2315 return errmsg;
2316}
2317
2318/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002319 * Set an option to a new value.
2320 * Return NULL if OK, return an untranslated error message when something is
2321 * wrong. "errbuf[errbuflen]" can be used to create the error message.
2322 */
2323 static char *
2324do_set_option(
2325 int opt_flags,
2326 char_u **argp,
2327 char_u *arg_start,
2328 char_u **startarg,
2329 int *did_show,
2330 int *stopopteval,
2331 char *errbuf,
2332 size_t errbuflen)
2333{
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002334 int opt_idx;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002335 char_u *arg;
2336 set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix
2337 set_op_T op;
2338 long_u flags; // flags for current option
2339 char_u *varp; // pointer to variable for current option
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002340 char_u key_name[2];
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002341 int nextchar; // next non-white char after option name
2342 int afterchar; // character just after option name
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002343 char *errmsg = NULL;
2344 int key;
2345 int len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002346
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002347 prefix = get_option_prefix(argp);
2348 arg = *argp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002349
2350 // find end of name
2351 key = 0;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002352 if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL)
2353 return e_invalid_argument;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002354
2355 // remember character after option name
2356 afterchar = arg[len];
2357
2358 if (in_vim9script())
2359 {
2360 char_u *p = skipwhite(arg + len);
2361
2362 // disallow white space before =val, +=val, -=val, ^=val
2363 if (p > arg + len && (p[0] == '='
2364 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
2365 && p[1] == '=')))
2366 {
2367 errmsg = e_no_white_space_allowed_between_option_and;
2368 arg = p;
2369 *startarg = p;
2370 goto skip;
2371 }
2372 }
2373 else
2374 // skip white space, allow ":set ai ?", ":set hlsearch !"
2375 while (VIM_ISWHITE(arg[len]))
2376 ++len;
2377
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002378 op = get_opt_op(arg + len);
2379 if (op != OP_NONE)
2380 len++;
2381
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002382 nextchar = arg[len];
2383
2384 if (opt_idx == -1 && key == 0) // found a mismatch: skip
2385 {
2386 if (in_vim9script() && arg > arg_start
2387 && vim_strchr((char_u *)"!&<", *arg) != NULL)
2388 errmsg = e_no_white_space_allowed_between_option_and;
2389 else
2390 errmsg = e_unknown_option;
2391 goto skip;
2392 }
2393
2394 if (opt_idx >= 0)
2395 {
2396 if (options[opt_idx].var == NULL) // hidden option: skip
2397 {
2398 // Only give an error message when requesting the value of
2399 // a hidden option, ignore setting it.
2400 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
2401 && (!(options[opt_idx].flags & P_BOOL)
2402 || nextchar == '?'))
2403 errmsg = e_option_not_supported;
2404 goto skip;
2405 }
2406
2407 flags = options[opt_idx].flags;
2408 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
2409 }
2410 else
2411 {
2412 flags = P_STRING;
Bram Moolenaar40b48722023-02-05 17:04:50 +00002413 varp = NULL;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002414 if (key < 0)
2415 {
2416 key_name[0] = KEY2TERMCAP0(key);
2417 key_name[1] = KEY2TERMCAP1(key);
2418 }
2419 else
2420 {
2421 key_name[0] = KS_KEY;
2422 key_name[1] = (key & 0xff);
2423 }
2424 }
2425
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002426 // Make sure the option value can be changed.
2427 if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002428 goto skip;
2429
Bram Moolenaar40b48722023-02-05 17:04:50 +00002430 int cp_val = p_cp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002431 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
2432 {
2433 arg += len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002434 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
2435 {
2436 if (arg[3] == 'm') // "opt&vim": set to Vim default
2437 {
2438 cp_val = FALSE;
2439 arg += 3;
2440 }
2441 else // "opt&vi": set to Vi default
2442 {
2443 cp_val = TRUE;
2444 arg += 2;
2445 }
2446 }
2447 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
2448 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
2449 {
2450 errmsg = e_trailing_characters;
2451 goto skip;
2452 }
2453 }
2454
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002455 // Allow '=' and ':' for historical reasons (MSDOS command.com).
2456 // Allows only one '=' character per "set" command line. grrr. (jw)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002457 if (nextchar == '?'
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002458 || (prefix == PREFIX_NONE
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002459 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
2460 && !(flags & P_BOOL)))
2461 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002462 // print value
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002463 if (*did_show)
2464 msg_putchar('\n'); // cursor below last one
2465 else
2466 {
2467 gotocmdline(TRUE); // cursor at status line
2468 *did_show = TRUE; // remember that we did a line
2469 }
2470 if (opt_idx >= 0)
2471 {
2472 showoneopt(&options[opt_idx], opt_flags);
2473#ifdef FEAT_EVAL
2474 if (p_verbose > 0)
2475 {
2476 // Mention where the option was last set.
2477 if (varp == options[opt_idx].var)
2478 last_set_msg(options[opt_idx].script_ctx);
2479 else if ((int)options[opt_idx].indir & PV_WIN)
2480 last_set_msg(curwin->w_p_script_ctx[
2481 (int)options[opt_idx].indir & PV_MASK]);
2482 else if ((int)options[opt_idx].indir & PV_BUF)
2483 last_set_msg(curbuf->b_p_script_ctx[
2484 (int)options[opt_idx].indir & PV_MASK]);
2485 }
2486#endif
2487 }
2488 else
2489 {
2490 char_u *p;
2491
2492 p = find_termcode(key_name);
2493 if (p == NULL)
2494 {
2495 errmsg = e_key_code_not_set;
2496 goto skip;
2497 }
2498 else
2499 (void)show_one_termcode(key_name, p, TRUE);
2500 }
2501 if (nextchar != '?'
2502 && nextchar != NUL && !VIM_ISWHITE(afterchar))
2503 errmsg = e_trailing_characters;
2504 }
2505 else
2506 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002507 errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op,
2508 flags, varp, key_name, nextchar,
2509 afterchar, cp_val, stopopteval, errbuf,
2510 errbuflen);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002511 }
2512
2513skip:
2514 *argp = arg;
2515 return errmsg;
2516}
2517
2518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 * Parse 'arg' for option settings.
2520 *
2521 * 'arg' may be IObuff, but only when no errors can be present and option
2522 * does not need to be expanded with option_expand().
2523 * "opt_flags":
2524 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002525 * OPT_GLOBAL for ":setglobal"
2526 * OPT_LOCAL for ":setlocal" and a modeline
2527 * OPT_MODELINE for a modeline
2528 * OPT_WINONLY to only set window-local options
2529 * OPT_NOWIN to skip setting window-local options
2530 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002532 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 */
2534 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002535do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002536 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002537 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002539 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002541 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
2543 if (*arg == NUL)
2544 {
2545 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002546 did_show = TRUE;
2547 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 }
2549
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002550 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002552 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002553 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002555 // ":set all" show all options.
2556 // ":set all&" set all options to their default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 arg += 3;
2558 if (*arg == '&')
2559 {
2560 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002561 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002563 didset_options();
2564 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002565 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 }
2567 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002568 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002570 did_show = TRUE;
2571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002573 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 {
2575 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002576 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002577 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 arg += 7;
2579 }
2580 else
2581 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002582 int stopopteval = FALSE;
2583 char *errmsg = NULL;
Christian Brabandtb39b2402023-11-29 11:34:05 +01002584 char errbuf[ERR_BUFLEN];
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002585 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002587 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2588 &did_show, &stopopteval, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002589 ERR_BUFLEN);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002590 if (stopopteval)
2591 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002593 // Advance to next argument.
2594 // - skip until a blank found, taking care of backslashes
2595 // - skip blanks
2596 // - skip one "=val" argument (for hidden options ":set gfn =xx")
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 for (i = 0; i < 2 ; ++i)
2598 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002599 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 if (*arg++ == '\\' && *arg != NUL)
2601 ++arg;
2602 arg = skipwhite(arg);
2603 if (*arg != '=')
2604 break;
2605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002607 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002609 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
2610 i = (int)STRLEN(IObuff) + 2;
2611 if (i + (arg - startarg) < IOSIZE)
2612 {
2613 // append the argument with the error
2614 STRCAT(IObuff, ": ");
2615 mch_memmove(IObuff + i, startarg, (arg - startarg));
2616 IObuff[i + (arg - startarg)] = NUL;
2617 }
2618 // make sure all characters are printable
2619 trans_characters(IObuff, IOSIZE);
2620
2621 ++no_wait_return; // wait_return() done later
2622 emsg((char *)IObuff); // show error highlighted
2623 --no_wait_return;
2624
2625 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 }
2628
2629 arg = skipwhite(arg);
2630 }
2631
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002632theend:
2633 if (silent_mode && did_show)
2634 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002635 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002636 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002637 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002638 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002639 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002640 out_flush();
2641 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002642 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002643 }
2644
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 return OK;
2646}
2647
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002648/*
2649 * Call this when an option has been given a new value through a user command.
2650 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2651 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002652 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002653did_set_option(
2654 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002655 int opt_flags, // possibly with OPT_MODELINE
2656 int new_value, // value was replaced completely
2657 int value_checked) // value was checked to be safe, no need to set the
2658 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002659{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002660 long_u *p;
2661
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002662 options[opt_idx].flags |= P_WAS_SET;
2663
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002664 // When an option is set in the sandbox, from a modeline or in secure mode
2665 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2666 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002667 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002668 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002669#ifdef HAVE_SANDBOX
2670 || sandbox != 0
2671#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002672 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002673 *p = *p | P_INSECURE;
2674 else if (new_value)
2675 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002676}
2677
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678/*
2679 * Convert a key name or string into a key value.
2680 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002681 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002683 int
2684string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685{
2686 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002687 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 if (*arg == '^')
2689 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002690 if (multi_byte)
2691 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 return *arg;
2693}
2694
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695/*
2696 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2697 * maketitle() to create and display it.
2698 * When switching the title or icon off, call mch_restore_title() to get
2699 * the old value back.
2700 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002701 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002702did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703{
2704 if (starting != NO_SCREEN
2705#ifdef FEAT_GUI
2706 && !gui.starting
2707#endif
2708 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711
2712/*
2713 * set_options_bin - called when 'bin' changes value.
2714 */
2715 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002716set_options_bin(
2717 int oldval,
2718 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002719 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002721 // The option values that are changed when 'bin' changes are
2722 // copied when 'bin is set and restored when 'bin' is reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 if (newval)
2724 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002725 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 {
2727 if (!(opt_flags & OPT_GLOBAL))
2728 {
2729 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2730 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2731 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2732 curbuf->b_p_et_nobin = curbuf->b_p_et;
2733 }
2734 if (!(opt_flags & OPT_LOCAL))
2735 {
2736 p_tw_nobin = p_tw;
2737 p_wm_nobin = p_wm;
2738 p_ml_nobin = p_ml;
2739 p_et_nobin = p_et;
2740 }
2741 }
2742
2743 if (!(opt_flags & OPT_GLOBAL))
2744 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002745 curbuf->b_p_tw = 0; // no automatic line wrap
2746 curbuf->b_p_wm = 0; // no automatic line wrap
2747 curbuf->b_p_ml = 0; // no modelines
2748 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 }
2750 if (!(opt_flags & OPT_LOCAL))
2751 {
2752 p_tw = 0;
2753 p_wm = 0;
2754 p_ml = FALSE;
2755 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002756 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 }
2758 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002759 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 {
2761 if (!(opt_flags & OPT_GLOBAL))
2762 {
2763 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2764 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2765 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2766 curbuf->b_p_et = curbuf->b_p_et_nobin;
2767 }
2768 if (!(opt_flags & OPT_LOCAL))
2769 {
2770 p_tw = p_tw_nobin;
2771 p_wm = p_wm_nobin;
2772 p_ml = p_ml_nobin;
2773 p_et = p_et_nobin;
2774 }
2775 }
Christian Brabandt757593c2023-08-22 21:44:10 +02002776#if defined(FEAT_EVAL) || defined(PROTO)
2777 // Remember where the dependent option were reset
2778 didset_options_sctx(opt_flags, p_bin_dep_opts);
2779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780}
2781
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782/*
2783 * Expand environment variables for some string options.
2784 * These string options cannot be indirect!
2785 * If "val" is NULL expand the current value of the option.
2786 * Return pointer to NameBuff, or NULL when not expanded.
2787 */
2788 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002789option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002791 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2793 return NULL;
2794
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002795 // If val is longer than MAXPATHL no meaningful expansion can be done,
2796 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 if (val != NULL && STRLEN(val) > MAXPATHL)
2798 return NULL;
2799
2800 if (val == NULL)
2801 val = *(char_u **)options[opt_idx].var;
2802
2803 /*
2804 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2805 * Escape spaces when expanding 'tags', they are used to separate file
2806 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002807 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 */
2809 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002810 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002811#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002812 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2813#endif
2814 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002815 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 return NULL;
2817
2818 return NameBuff;
2819}
2820
2821/*
2822 * After setting various option values: recompute variables that depend on
2823 * option values.
2824 */
2825 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002826didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002828 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 (void)init_chartab();
2830
Bram Moolenaardac13472019-09-16 21:06:21 +02002831 didset_string_options();
2832
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002833#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002834 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002835 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002836 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002837 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002838#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002839 // set cedit_key
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002840 (void)did_set_cedit(NULL);
Bram Moolenaar597a4222014-06-25 14:39:50 +02002841#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002842 // initialize the table for 'breakat'.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002843 did_set_breakat(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002844#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002845 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002846}
2847
2848/*
2849 * More side effects of setting options.
2850 */
2851 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002852didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002853{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002854 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002855 (void)highlight_changed();
2856
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002857 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002858 check_opt_wim();
2859
Bram Moolenaareed9d462021-02-15 20:38:25 +01002860 // Parse default for 'listchars'.
zeertzjq6a8d2e12024-01-17 20:54:49 +01002861 (void)set_listchars_option(curwin, curwin->w_p_lcs, TRUE, NULL, 0);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002862
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002863 // Parse default for 'fillchars'.
zeertzjq6a8d2e12024-01-17 20:54:49 +01002864 (void)set_fillchars_option(curwin, curwin->w_p_fcs, TRUE, NULL, 0);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002865
2866#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002867 // Parse default for 'clipboard'
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002868 (void)did_set_clipboard(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002869#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002870#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002871 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002872 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002873 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002874 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876}
2877
2878/*
2879 * Check for string options that are NULL (normally only termcap options).
2880 */
2881 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002882check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883{
2884 int opt_idx;
2885
2886 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2887 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2888 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2889}
2890
2891/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002892 * Return the option index found by a pointer into term_strings[].
2893 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002895 int
2896get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002898 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899
2900 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2901 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002902 return opt_idx;
2903 return -1; // cannot happen: didn't find it!
2904}
2905
2906/*
2907 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2908 * Return the option index or -1 if not found.
2909 */
2910 int
2911set_term_option_alloced(char_u **p)
2912{
2913 int opt_idx = get_term_opt_idx(p);
2914
2915 if (opt_idx >= 0)
2916 options[opt_idx].flags |= P_ALLOCED;
2917 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918}
2919
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002920#if defined(FEAT_EVAL) || defined(PROTO)
2921/*
2922 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2923 * Return FALSE when it wasn't.
2924 * Return -1 for an unknown option.
2925 */
2926 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002927was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002928{
2929 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002930 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002931
2932 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002933 {
2934 flagp = insecure_flag(idx, opt_flags);
2935 return (*flagp & P_INSECURE) != 0;
2936 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002937 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002938 return -1;
2939}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002940
2941/*
2942 * Get a pointer to the flags used for the P_INSECURE flag of option
2943 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002944 * NOTE: Caller must make sure that "curwin" is set to the window from which
2945 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002946 */
2947 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002948insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002949{
2950 if (opt_flags & OPT_LOCAL)
2951 switch ((int)options[opt_idx].indir)
2952 {
2953#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002954 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002955#endif
2956#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002957# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002958 case PV_FDE: return &curwin->w_p_fde_flags;
2959 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002960# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002961# ifdef FEAT_BEVAL
2962 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2963# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002964 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002965 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002966# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002967 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002968# endif
2969#endif
2970 }
2971
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002972 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002973 return &options[opt_idx].flags;
2974}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002975#endif
2976
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002977/*
2978 * Redraw the window title and/or tab page text later.
2979 */
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02002980 void
2981redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002982{
2983 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002984 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002985}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002986
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002988 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2989 * characters or characters in "allowed".
2990 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002991 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002992valid_name(char_u *val, char *allowed)
2993{
2994 char_u *s;
2995
2996 for (s = val; *s != NUL; ++s)
2997 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2998 return FALSE;
2999 return TRUE;
3000}
3001
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003002#if defined(FEAT_EVAL) || defined(PROTO)
3003/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003004 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003005 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003006 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003007 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003008set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003009{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003010 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
3011 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003012 sctx_T new_script_ctx = script_ctx;
3013
Bram Moolenaar51258742020-05-03 17:19:33 +02003014 // Modeline already has the line number set.
3015 if (!(opt_flags & OPT_MODELINE))
3016 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003017
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003018 // Remember where the option was set. For local options need to do that
3019 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003020 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003021 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003022 if (both || (opt_flags & OPT_LOCAL))
3023 {
3024 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003025 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003026 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00003027 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003028 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00003029 if (both)
3030 // also setting the "all buffers" value
3031 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
3032 new_script_ctx;
3033 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003034 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003035}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003036
3037/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00003038 * Get the script context of global option "name".
3039 *
3040 */
3041 sctx_T *
3042get_option_sctx(char *name)
3043{
3044 int idx = findoption((char_u *)name);
3045
3046 if (idx >= 0)
3047 return &options[idx].script_ctx;
3048 siemsg("no such option: %s", name);
3049 return NULL;
3050}
3051
3052/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003053 * Set the script_ctx for a termcap option.
3054 * "name" must be the two character code, e.g. "RV".
3055 * When "name" is NULL use "opt_idx".
3056 */
3057 void
3058set_term_option_sctx_idx(char *name, int opt_idx)
3059{
3060 char_u buf[5];
3061 int idx;
3062
3063 if (name == NULL)
3064 idx = opt_idx;
3065 else
3066 {
3067 buf[0] = 't';
3068 buf[1] = '_';
3069 buf[2] = name[0];
3070 buf[3] = name[1];
3071 buf[4] = 0;
3072 idx = findoption(buf);
3073 }
3074 if (idx >= 0)
3075 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
3076}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003077#endif
3078
Bram Moolenaarf4140482020-02-15 23:06:45 +01003079#if defined(FEAT_EVAL)
3080/*
3081 * Apply the OptionSet autocommand.
3082 */
3083 static void
3084apply_optionset_autocmd(
3085 int opt_idx,
3086 long opt_flags,
3087 long oldval,
3088 long oldval_g,
3089 long newval,
3090 char *errmsg)
3091{
3092 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
3093
3094 // Don't do this while starting up, failure or recursively.
3095 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
3096 return;
3097
3098 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
3099 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
3100 oldval_g);
3101 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
3102 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
3103 (opt_flags & OPT_LOCAL) ? "local" : "global");
3104 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3105 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3106 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
3107 if (opt_flags & OPT_LOCAL)
3108 {
3109 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3110 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3111 }
3112 if (opt_flags & OPT_GLOBAL)
3113 {
3114 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3115 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3116 }
3117 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3118 {
3119 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3120 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3121 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3122 }
3123 if (opt_flags & OPT_MODELINE)
3124 {
3125 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3126 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3127 }
3128 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3129 NULL, FALSE, NULL);
3130 reset_v_option_vars();
3131}
3132#endif
3133
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003134#if defined(FEAT_ARABIC) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003135/*
3136 * Process the updated 'arabic' option value.
3137 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003138 char *
3139did_set_arabic(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003140{
3141 char *errmsg = NULL;
3142
3143 if (curwin->w_p_arab)
3144 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003145 // 'arabic' is set, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003146 if (!p_tbidi)
3147 {
3148 // set rightleft mode
3149 if (!curwin->w_p_rl)
3150 {
3151 curwin->w_p_rl = TRUE;
3152 changed_window_setting();
3153 }
3154
3155 // Enable Arabic shaping (major part of what Arabic requires)
3156 if (!p_arshape)
3157 {
3158 p_arshape = TRUE;
3159 redraw_later_clear();
3160 }
3161 }
3162
3163 // Arabic requires a utf-8 encoding, inform the user if it's not
3164 // set.
3165 if (STRCMP(p_enc, "utf-8") != 0)
3166 {
3167 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3168
3169 msg_source(HL_ATTR(HLF_W));
3170 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
3171#ifdef FEAT_EVAL
3172 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3173#endif
3174 }
3175
3176 // set 'delcombine'
3177 p_deco = TRUE;
3178
3179# ifdef FEAT_KEYMAP
3180 // Force-set the necessary keymap for arabic
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003181 errmsg = set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3182 OPT_LOCAL);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003183# endif
3184 }
3185 else
3186 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003187 // 'arabic' is reset, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003188 if (!p_tbidi)
3189 {
3190 // reset rightleft mode
3191 if (curwin->w_p_rl)
3192 {
3193 curwin->w_p_rl = FALSE;
3194 changed_window_setting();
3195 }
3196
3197 // 'arabicshape' isn't reset, it is a global option and
3198 // another window may still need it "on".
3199 }
3200
3201 // 'delcombine' isn't reset, it is a global option and another
3202 // window may still want it "on".
3203
3204# ifdef FEAT_KEYMAP
3205 // Revert to the default keymap
3206 curbuf->b_p_iminsert = B_IMODE_NONE;
3207 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3208# endif
3209 }
3210
3211 return errmsg;
3212}
3213#endif
3214
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003215#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
3216/*
3217 * Process the updated 'autochdir' option value.
3218 */
3219 char *
3220did_set_autochdir(optset_T *args UNUSED)
3221{
3222 // Change directories when the 'acd' option is set now.
3223 DO_AUTOCHDIR;
3224 return NULL;
3225}
3226#endif
3227
3228#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
3229/*
3230 * Process the updated 'ballooneval' option value.
3231 */
3232 char *
3233did_set_ballooneval(optset_T *args)
3234{
3235 if (balloonEvalForTerm)
3236 return NULL;
3237
3238 if (p_beval && !args->os_oldval.boolean)
3239 gui_mch_enable_beval_area(balloonEval);
3240 else if (!p_beval && args->os_oldval.boolean)
3241 gui_mch_disable_beval_area(balloonEval);
3242
3243 return NULL;
3244}
3245#endif
3246
3247#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
3248/*
3249 * Process the updated 'balloonevalterm' option value.
3250 */
3251 char *
3252did_set_balloonevalterm(optset_T *args UNUSED)
3253{
3254 mch_bevalterm_changed();
3255 return NULL;
3256}
3257#endif
3258
3259/*
3260 * Process the updated 'binary' option value.
3261 */
3262 char *
3263did_set_binary(optset_T *args)
3264{
3265 // when 'bin' is set also set some other options
3266 set_options_bin(args->os_oldval.boolean, curbuf->b_p_bin, args->os_flags);
3267 redraw_titles();
3268
3269 return NULL;
3270}
3271
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003272/*
3273 * Process the updated 'buflisted' option value.
3274 */
3275 char *
3276did_set_buflisted(optset_T *args)
3277{
3278 // when 'buflisted' changes, trigger autocommands
3279 if (args->os_oldval.boolean != curbuf->b_p_bl)
3280 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3281 NULL, NULL, TRUE, curbuf);
3282 return NULL;
3283}
3284
3285/*
3286 * Process the new 'cmdheight' option value.
3287 */
3288 char *
3289did_set_cmdheight(optset_T *args)
3290{
3291 long old_value = args->os_oldval.number;
3292 char *errmsg = NULL;
3293
3294 // if p_ch changed value, change the command line height
3295 if (p_ch < 1)
3296 {
3297 errmsg = e_argument_must_be_positive;
3298 p_ch = 1;
3299 }
3300 if (p_ch > Rows - min_rows() + 1)
3301 p_ch = Rows - min_rows() + 1;
3302
3303 // Only compute the new window layout when startup has been
3304 // completed. Otherwise the frame sizes may be wrong.
3305 if ((p_ch != old_value
3306 || tabline_height() + topframe->fr_height != Rows - p_ch)
3307 && full_screen
3308#ifdef FEAT_GUI
3309 && !gui.starting
3310#endif
3311 )
3312 command_height();
3313
3314 return errmsg;
3315}
3316
3317/*
3318 * Process the updated 'compatible' option value.
3319 */
3320 char *
3321did_set_compatible(optset_T *args UNUSED)
3322{
3323 compatible_set();
3324 return NULL;
3325}
3326
3327#if defined(FEAT_CONCEAL) || defined(PROTO)
3328/*
3329 * Process the new 'conceallevel' option value.
3330 */
3331 char *
3332did_set_conceallevel(optset_T *args UNUSED)
3333{
3334 char *errmsg = NULL;
3335
3336 if (curwin->w_p_cole < 0)
3337 {
3338 errmsg = e_argument_must_be_positive;
3339 curwin->w_p_cole = 0;
3340 }
3341 else if (curwin->w_p_cole > 3)
3342 {
3343 errmsg = e_invalid_argument;
3344 curwin->w_p_cole = 3;
3345 }
3346
3347 return errmsg;
3348}
3349#endif
3350
3351#if defined(FEAT_DIFF) || defined(PROTO)
3352/*
3353 * Process the updated 'diff' option value.
3354 */
3355 char *
3356did_set_diff(optset_T *args UNUSED)
3357{
3358 // May add or remove the buffer from the list of diff buffers.
3359 diff_buf_adjust(curwin);
3360# ifdef FEAT_FOLDING
3361 if (foldmethodIsDiff(curwin))
3362 foldUpdateAll(curwin);
3363# endif
3364 return NULL;
3365}
3366#endif
3367
3368/*
3369 * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb'
3370 * option value.
3371 */
3372 char *
3373did_set_eof_eol_fixeol_bomb(optset_T *args UNUSED)
3374{
3375 // redraw the window title and tab page text
3376 redraw_titles();
3377 return NULL;
3378}
3379
3380/*
3381 * Process the updated 'equalalways' option value.
3382 */
3383 char *
3384did_set_equalalways(optset_T *args)
3385{
3386 if (p_ea && !args->os_oldval.boolean)
3387 win_equal(curwin, FALSE, 0);
3388
3389 return NULL;
3390}
3391
3392#if defined(FEAT_FOLDING) || defined(PROTO)
3393/*
3394 * Process the new 'foldcolumn' option value.
3395 */
3396 char *
3397did_set_foldcolumn(optset_T *args UNUSED)
3398{
3399 char *errmsg = NULL;
3400
3401 if (curwin->w_p_fdc < 0)
3402 {
3403 errmsg = e_argument_must_be_positive;
3404 curwin->w_p_fdc = 0;
3405 }
3406 else if (curwin->w_p_fdc > 12)
3407 {
3408 errmsg = e_invalid_argument;
3409 curwin->w_p_fdc = 12;
3410 }
3411
3412 return errmsg;
3413}
3414
3415/*
3416 * Process the new 'foldlevel' option value.
3417 */
3418 char *
3419did_set_foldlevel(optset_T *args UNUSED)
3420{
3421 if (curwin->w_p_fdl < 0)
3422 curwin->w_p_fdl = 0;
3423 newFoldLevel();
3424 return NULL;
3425}
3426
3427/*
3428 * Process the new 'foldminlines' option value.
3429 */
3430 char *
3431did_set_foldminlines(optset_T *args UNUSED)
3432{
3433 foldUpdateAll(curwin);
3434 return NULL;
3435}
3436
3437/*
3438 * Process the new 'foldnestmax' option value.
3439 */
3440 char *
3441did_set_foldnestmax(optset_T *args UNUSED)
3442{
3443 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3444 foldUpdateAll(curwin);
3445 return NULL;
3446}
3447#endif
3448
3449#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
3450/*
3451 * Process the updated 'hlsearch' option value.
3452 */
3453 char *
3454did_set_hlsearch(optset_T *args UNUSED)
3455{
3456 // when 'hlsearch' is set or reset: reset no_hlsearch
3457 set_no_hlsearch(FALSE);
3458 return NULL;
3459}
3460#endif
3461
3462/*
3463 * Process the updated 'ignorecase' option value.
3464 */
3465 char *
3466did_set_ignorecase(optset_T *args UNUSED)
3467{
3468 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
3469 if (p_hls)
3470 redraw_all_later(UPD_SOME_VALID);
3471 return NULL;
3472}
3473
3474#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
3475/*
3476 * Process the updated 'imdisable' option value.
3477 */
3478 char *
3479did_set_imdisable(optset_T *args UNUSED)
3480{
3481 // Only de-activate it here, it will be enabled when changing mode.
3482 if (p_imdisable)
3483 im_set_active(FALSE);
3484 else if (State & MODE_INSERT)
3485 // When the option is set from an autocommand, it may need to take
3486 // effect right away.
3487 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
3488 return NULL;
3489}
3490#endif
3491
3492/*
3493 * Process the new 'iminsert' option value.
3494 */
3495 char *
3496did_set_iminsert(optset_T *args UNUSED)
3497{
3498 char *errmsg = NULL;
3499
3500 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3501 {
3502 errmsg = e_invalid_argument;
3503 curbuf->b_p_iminsert = B_IMODE_NONE;
3504 }
3505 p_iminsert = curbuf->b_p_iminsert;
3506 if (termcap_active) // don't do this in the alternate screen
3507 showmode();
3508#if defined(FEAT_KEYMAP)
3509 // Show/unshow value of 'keymap' in status lines.
3510 status_redraw_curbuf();
3511#endif
3512
3513 return errmsg;
3514}
3515
3516/*
3517 * Process the new 'imsearch' option value.
3518 */
3519 char *
3520did_set_imsearch(optset_T *args UNUSED)
3521{
3522 char *errmsg = NULL;
3523
3524 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3525 {
3526 errmsg = e_invalid_argument;
3527 curbuf->b_p_imsearch = B_IMODE_NONE;
3528 }
3529 p_imsearch = curbuf->b_p_imsearch;
3530
3531 return errmsg;
3532}
3533
3534#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3535/*
3536 * Process the new 'imstyle' option value.
3537 */
3538 char *
3539did_set_imstyle(optset_T *args UNUSED)
3540{
3541 char *errmsg = NULL;
3542
3543 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3544 errmsg = e_invalid_argument;
3545
3546 return errmsg;
3547}
3548#endif
3549
3550/*
3551 * Process the updated 'insertmode' option value.
3552 */
3553 char *
3554did_set_insertmode(optset_T *args)
3555{
3556 // when 'insertmode' is set from an autocommand need to do work here
3557 if (p_im)
3558 {
3559 if ((State & MODE_INSERT) == 0)
3560 need_start_insertmode = TRUE;
3561 stop_insert_mode = FALSE;
3562 }
3563 // only reset if it was set previously
3564 else if (args->os_oldval.boolean)
3565 {
3566 need_start_insertmode = FALSE;
3567 stop_insert_mode = TRUE;
3568 if (restart_edit != 0 && mode_displayed)
3569 clear_cmdline = TRUE; // remove "(insert)"
3570 restart_edit = 0;
3571 }
3572
3573 return NULL;
3574}
3575
3576#if defined(FEAT_LANGMAP) || defined(PROTO)
3577/*
3578 * Process the updated 'langnoremap' option value.
3579 */
3580 char *
3581did_set_langnoremap(optset_T *args UNUSED)
3582{
3583 // 'langnoremap' -> !'langremap'
3584 p_lrm = !p_lnr;
3585 return NULL;
3586}
3587
3588/*
3589 * Process the updated 'langremap' option value.
3590 */
3591 char *
3592did_set_langremap(optset_T *args UNUSED)
3593{
3594 // 'langremap' -> !'langnoremap'
3595 p_lnr = !p_lrm;
3596 return NULL;
3597}
3598#endif
3599
3600/*
3601 * Process the new 'laststatus' option value.
3602 */
3603 char *
3604did_set_laststatus(optset_T *args UNUSED)
3605{
3606 last_status(FALSE); // (re)set last window status line
3607 return NULL;
3608}
3609
3610#if defined(FEAT_GUI) || defined(PROTO)
3611/*
3612 * Process the new 'linespace' option value.
3613 */
3614 char *
3615did_set_linespace(optset_T *args UNUSED)
3616{
3617 // Recompute gui.char_height and resize the Vim window to keep the
3618 // same number of lines.
3619 if (gui.in_use && gui_mch_adjust_charheight() == OK)
3620 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
3621 return NULL;
3622}
3623#endif
3624
3625/*
3626 * Process the updated 'lisp' option value.
3627 */
3628 char *
3629did_set_lisp(optset_T *args UNUSED)
3630{
3631 // When 'lisp' option changes include/exclude '-' in keyword characters.
3632 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
3633 return NULL;
3634}
3635
3636/*
3637 * Process the new 'maxcombine' option value.
3638 */
3639 char *
3640did_set_maxcombine(optset_T *args UNUSED)
3641{
3642 if (p_mco > MAX_MCO)
3643 p_mco = MAX_MCO;
3644 else if (p_mco < 0)
3645 p_mco = 0;
3646 screenclear(); // will re-allocate the screen
3647 return NULL;
3648}
3649
3650/*
3651 * Process the updated 'modifiable' option value.
3652 */
3653 char *
3654did_set_modifiable(optset_T *args UNUSED)
3655{
3656 // when 'modifiable' is changed, redraw the window title
3657
3658# ifdef FEAT_TERMINAL
3659 // Cannot set 'modifiable' when in Terminal mode.
3660 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3661 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
3662 {
3663 curbuf->b_p_ma = FALSE;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003664 return e_cannot_make_terminal_with_running_job_modifiable;
3665 }
3666# endif
3667 redraw_titles();
3668
3669 return NULL;
3670}
3671
3672/*
3673 * Process the updated 'modified' option value.
3674 */
3675 char *
3676did_set_modified(optset_T *args)
3677{
3678 if (!args->os_newval.boolean)
3679 save_file_ff(curbuf); // Buffer is unchanged
3680 redraw_titles();
3681 modified_was_set = args->os_newval.boolean;
3682 return NULL;
3683}
3684
3685#if defined(FEAT_GUI) || defined(PROTO)
3686/*
3687 * Process the updated 'mousehide' option value.
3688 */
3689 char *
3690did_set_mousehide(optset_T *args UNUSED)
3691{
3692 if (!p_mh)
3693 gui_mch_mousehide(FALSE);
3694 return NULL;
3695}
3696#endif
3697
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003698/*
3699 * Process the updated 'number' or 'relativenumber' option value.
3700 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003701 char *
3702did_set_number_relativenumber(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003703{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003704#if (defined(FEAT_SIGNS) && defined(FEAT_GUI)) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003705 if (gui.in_use
3706 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3707 && curbuf->b_signlist != NULL)
3708 {
3709 // If the 'number' or 'relativenumber' options are modified and
3710 // 'signcolumn' is set to 'number', then clear the screen for a full
3711 // refresh. Otherwise the sign icons are not displayed properly in the
3712 // number column. If the 'number' option is set and only the
3713 // 'relativenumber' option is toggled, then don't refresh the screen
3714 // (optimization).
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003715 if (!(curwin->w_p_nu && ((int *)args->os_varp == &curwin->w_p_rnu)))
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003716 redraw_all_later(UPD_CLEAR);
3717 }
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003718#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003719 return NULL;
3720}
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003721
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003722#if defined(FEAT_LINEBREAK) || defined(PROTO)
3723/*
3724 * Process the new 'numberwidth' option value.
3725 */
3726 char *
3727did_set_numberwidth(optset_T *args UNUSED)
3728{
3729 char *errmsg = NULL;
3730
3731 // 'numberwidth' must be positive
3732 if (curwin->w_p_nuw < 1)
3733 {
3734 errmsg = e_argument_must_be_positive;
3735 curwin->w_p_nuw = 1;
3736 }
3737 if (curwin->w_p_nuw > 20)
3738 {
3739 errmsg = e_invalid_argument;
3740 curwin->w_p_nuw = 20;
3741 }
3742 curwin->w_nrwidth_line_count = 0; // trigger a redraw
3743
3744 return errmsg;
3745}
3746#endif
3747
3748/*
3749 * Process the updated 'paste' option value. Called after p_paste was set or
3750 * reset. When 'paste' is set or reset also change other options.
3751 */
3752 char *
3753did_set_paste(optset_T *args UNUSED)
3754{
3755 static int old_p_paste = FALSE;
3756 static int save_sm = 0;
3757 static int save_sta = 0;
3758 static int save_ru = 0;
3759#ifdef FEAT_RIGHTLEFT
3760 static int save_ri = 0;
3761 static int save_hkmap = 0;
3762#endif
3763 buf_T *buf;
3764
3765 if (p_paste)
3766 {
3767 // Paste switched from off to on.
3768 // Save the current values, so they can be restored later.
3769 if (!old_p_paste)
3770 {
3771 // save options for each buffer
3772 FOR_ALL_BUFFERS(buf)
3773 {
3774 buf->b_p_tw_nopaste = buf->b_p_tw;
3775 buf->b_p_wm_nopaste = buf->b_p_wm;
3776 buf->b_p_sts_nopaste = buf->b_p_sts;
3777 buf->b_p_ai_nopaste = buf->b_p_ai;
3778 buf->b_p_et_nopaste = buf->b_p_et;
3779#ifdef FEAT_VARTABS
3780 if (buf->b_p_vsts_nopaste)
3781 vim_free(buf->b_p_vsts_nopaste);
3782 buf->b_p_vsts_nopaste =
3783 buf->b_p_vsts && buf->b_p_vsts != empty_option
3784 ? vim_strsave(buf->b_p_vsts) : NULL;
3785#endif
3786 }
3787
3788 // save global options
3789 save_sm = p_sm;
3790 save_sta = p_sta;
3791 save_ru = p_ru;
3792#ifdef FEAT_RIGHTLEFT
3793 save_ri = p_ri;
3794 save_hkmap = p_hkmap;
3795#endif
3796 // save global values for local buffer options
3797 p_ai_nopaste = p_ai;
3798 p_et_nopaste = p_et;
3799 p_sts_nopaste = p_sts;
3800 p_tw_nopaste = p_tw;
3801 p_wm_nopaste = p_wm;
3802#ifdef FEAT_VARTABS
3803 if (p_vsts_nopaste)
3804 vim_free(p_vsts_nopaste);
3805 p_vsts_nopaste = p_vsts && p_vsts != empty_option
3806 ? vim_strsave(p_vsts) : NULL;
3807#endif
3808 }
3809
3810 // Always set the option values, also when 'paste' is set when it is
3811 // already on. Set options for each buffer.
3812 FOR_ALL_BUFFERS(buf)
3813 {
3814 buf->b_p_tw = 0; // textwidth is 0
3815 buf->b_p_wm = 0; // wrapmargin is 0
3816 buf->b_p_sts = 0; // softtabstop is 0
3817 buf->b_p_ai = 0; // no auto-indent
3818 buf->b_p_et = 0; // no expandtab
3819#ifdef FEAT_VARTABS
3820 if (buf->b_p_vsts)
3821 free_string_option(buf->b_p_vsts);
3822 buf->b_p_vsts = empty_option;
3823 VIM_CLEAR(buf->b_p_vsts_array);
3824#endif
3825 }
3826
3827 // set global options
3828 p_sm = 0; // no showmatch
3829 p_sta = 0; // no smarttab
3830 if (p_ru)
3831 status_redraw_all(); // redraw to remove the ruler
3832 p_ru = 0; // no ruler
3833#ifdef FEAT_RIGHTLEFT
3834 p_ri = 0; // no reverse insert
3835 p_hkmap = 0; // no Hebrew keyboard
3836#endif
3837 // set global values for local buffer options
3838 p_tw = 0;
3839 p_wm = 0;
3840 p_sts = 0;
3841 p_ai = 0;
Christian Brabandt757593c2023-08-22 21:44:10 +02003842 p_et = 0;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003843#ifdef FEAT_VARTABS
3844 if (p_vsts)
3845 free_string_option(p_vsts);
3846 p_vsts = empty_option;
3847#endif
3848 }
3849
3850 // Paste switched from on to off: Restore saved values.
3851 else if (old_p_paste)
3852 {
3853 // restore options for each buffer
3854 FOR_ALL_BUFFERS(buf)
3855 {
3856 buf->b_p_tw = buf->b_p_tw_nopaste;
3857 buf->b_p_wm = buf->b_p_wm_nopaste;
3858 buf->b_p_sts = buf->b_p_sts_nopaste;
3859 buf->b_p_ai = buf->b_p_ai_nopaste;
3860 buf->b_p_et = buf->b_p_et_nopaste;
3861#ifdef FEAT_VARTABS
3862 if (buf->b_p_vsts)
3863 free_string_option(buf->b_p_vsts);
3864 buf->b_p_vsts = buf->b_p_vsts_nopaste
3865 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
3866 vim_free(buf->b_p_vsts_array);
3867 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
3868 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
3869 else
3870 buf->b_p_vsts_array = NULL;
3871#endif
3872 }
3873
3874 // restore global options
3875 p_sm = save_sm;
3876 p_sta = save_sta;
3877 if (p_ru != save_ru)
3878 status_redraw_all(); // redraw to draw the ruler
3879 p_ru = save_ru;
3880#ifdef FEAT_RIGHTLEFT
3881 p_ri = save_ri;
3882 p_hkmap = save_hkmap;
3883#endif
3884 // set global values for local buffer options
3885 p_ai = p_ai_nopaste;
3886 p_et = p_et_nopaste;
3887 p_sts = p_sts_nopaste;
3888 p_tw = p_tw_nopaste;
3889 p_wm = p_wm_nopaste;
3890#ifdef FEAT_VARTABS
3891 if (p_vsts)
3892 free_string_option(p_vsts);
3893 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
3894#endif
3895 }
3896
3897 old_p_paste = p_paste;
3898
Christian Brabandt757593c2023-08-22 21:44:10 +02003899#if defined(FEAT_EVAL) || defined(PROTO)
3900 // Remember where the dependent options were reset
3901 didset_options_sctx((OPT_LOCAL | OPT_GLOBAL), p_paste_dep_opts);
3902#endif
3903
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003904 return NULL;
3905}
3906
3907
3908#ifdef FEAT_QUICKFIX
3909/*
3910 * Process the updated 'previewwindow' option value.
3911 */
3912 char *
Yegappan Lakshmanan3ee25962023-12-04 20:31:14 +01003913did_set_previewwindow(optset_T *args UNUSED)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003914{
3915 if (!curwin->w_p_pvw)
3916 return NULL;
3917
3918 // There can be only one window with 'previewwindow' set.
3919 win_T *win;
3920
3921 FOR_ALL_WINDOWS(win)
3922 if (win->w_p_pvw && win != curwin)
3923 {
3924 curwin->w_p_pvw = FALSE;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003925 return e_preview_window_already_exists;
3926 }
3927
3928 return NULL;
3929}
3930#endif
3931
3932#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
3933/*
3934 * Process the new 'pyxversion' option value.
3935 */
3936 char *
3937did_set_pyxversion(optset_T *args UNUSED)
3938{
3939 char *errmsg = NULL;
3940
3941 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3942 errmsg = e_invalid_argument;
3943
3944 return errmsg;
3945}
3946#endif
3947
3948/*
3949 * Process the updated 'readonly' option value.
3950 */
3951 char *
3952did_set_readonly(optset_T *args)
3953{
3954 // when 'readonly' is reset globally, also reset readonlymode
3955 if (!curbuf->b_p_ro && (args->os_flags & OPT_LOCAL) == 0)
3956 readonlymode = FALSE;
3957
3958 // when 'readonly' is set may give W10 again
3959 if (curbuf->b_p_ro)
3960 curbuf->b_did_warn = FALSE;
3961
3962 redraw_titles();
3963
3964 return NULL;
3965}
3966
3967/*
3968 * Process the updated 'scrollbind' option value.
3969 */
3970 char *
3971did_set_scrollbind(optset_T *args UNUSED)
3972{
3973 // when 'scrollbind' is set: snapshot the current position to avoid a jump
3974 // at the end of normal_cmd()
3975 if (!curwin->w_p_scb)
3976 return NULL;
3977
3978 do_check_scrollbind(FALSE);
3979 curwin->w_scbind_pos = curwin->w_topline;
3980 return NULL;
3981}
3982
3983#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3984/*
3985 * Process the updated 'shellslash' option value.
3986 */
3987 char *
3988did_set_shellslash(optset_T *args UNUSED)
3989{
3990 if (p_ssl)
3991 {
3992 psepc = '/';
3993 psepcN = '\\';
3994 pseps[0] = '/';
3995 }
3996 else
3997 {
3998 psepc = '\\';
3999 psepcN = '/';
4000 pseps[0] = '\\';
4001 }
4002
4003 // need to adjust the file name arguments and buffer names.
4004 buflist_slash_adjust();
4005 alist_slash_adjust();
4006# ifdef FEAT_EVAL
4007 scriptnames_slash_adjust();
4008# endif
4009 return NULL;
4010}
4011#endif
4012
4013/*
4014 * Process the new 'shiftwidth' or the 'tabstop' option value.
4015 */
4016 char *
4017did_set_shiftwidth_tabstop(optset_T *args)
4018{
4019 long *pp = (long *)args->os_varp;
4020 char *errmsg = NULL;
4021
4022 if (curbuf->b_p_sw < 0)
4023 {
4024 errmsg = e_argument_must_be_positive;
4025#ifdef FEAT_VARTABS
4026 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4027 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4028 ? tabstop_first(curbuf->b_p_vts_array)
4029 : curbuf->b_p_ts;
4030#else
4031 curbuf->b_p_sw = curbuf->b_p_ts;
4032#endif
4033 }
4034
4035#ifdef FEAT_FOLDING
4036 if (foldmethodIsIndent(curwin))
4037 foldUpdateAll(curwin);
4038#endif
4039 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
4040 // parse 'cinoptions'.
4041 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
4042 parse_cino(curbuf);
4043
4044 return errmsg;
4045}
4046
4047/*
4048 * Process the new 'showtabline' option value.
4049 */
4050 char *
4051did_set_showtabline(optset_T *args UNUSED)
4052{
4053 shell_new_rows(); // recompute window positions and heights
4054 return NULL;
4055}
4056
4057/*
4058 * Process the updated 'smoothscroll' option value.
4059 */
4060 char *
4061did_set_smoothscroll(optset_T *args UNUSED)
4062{
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004063 if (!curwin->w_p_sms)
4064 curwin->w_skipcol = 0;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004065
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004066 return NULL;
4067}
4068
4069#if defined(FEAT_SPELL) || defined(PROTO)
4070/*
4071 * Process the updated 'spell' option value.
4072 */
4073 char *
4074did_set_spell(optset_T *args UNUSED)
4075{
4076 if (curwin->w_p_spell)
4077 return parse_spelllang(curwin);
4078
4079 return NULL;
4080}
4081#endif
4082
4083/*
4084 * Process the updated 'swapfile' option value.
4085 */
4086 char *
4087did_set_swapfile(optset_T *args UNUSED)
4088{
4089 // when 'swf' is set, create swapfile, when reset remove swapfile
4090 if (curbuf->b_p_swf && p_uc)
4091 ml_open_file(curbuf); // create the swap file
4092 else
4093 // no need to reset curbuf->b_may_swap, ml_open_file() will check
4094 // buf->b_p_swf
4095 mf_close_file(curbuf, TRUE); // remove the swap file
4096 return NULL;
4097}
4098
4099#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004100 char *
4101did_set_termguicolors(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004102{
4103# ifdef FEAT_VTP
4104 // Do not turn on 'tgc' when 24-bit colors are not supported.
4105 if (
4106# ifdef VIMDLL
4107 !gui.in_use && !gui.starting &&
4108# endif
4109 !has_vtp_working())
4110 {
4111 p_tgc = 0;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004112 return e_24_bit_colors_are_not_supported_on_this_environment;
4113 }
4114 if (is_term_win32())
4115 swap_tcap();
4116# endif
4117# ifdef FEAT_GUI
4118 if (!gui.in_use && !gui.starting)
4119# endif
4120 highlight_gui_started();
4121# ifdef FEAT_VTP
4122 // reset t_Co
4123 if (is_term_win32())
4124 {
4125 control_console_color_rgb();
4126 set_termname(T_NAME);
4127 init_highlight(TRUE, FALSE);
4128 }
4129# endif
4130# ifdef FEAT_TERMINAL
4131 term_update_colors_all();
4132 term_update_palette_all();
4133 term_update_wincolor_all();
4134# endif
4135
4136 return NULL;
4137}
4138#endif
4139
4140/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004141 * Process the updated 'terse' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004143 char *
4144did_set_terse(optset_T *args UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004146 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004148 // when 'terse' is set change 'shortmess'
4149 p = vim_strchr(p_shm, SHM_SEARCH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004151 // insert 's' in p_shm
4152 if (p_terse && p == NULL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004153 {
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004154 STRCPY(IObuff, p_shm);
4155 STRCAT(IObuff, "s");
4156 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004157 }
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004158 // remove 's' from p_shm
4159 else if (!p_terse && p != NULL)
4160 STRMOVE(p, p + 1);
4161 return NULL;
4162}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004164/*
4165 * Process the updated 'textauto' option value.
4166 */
4167 char *
4168did_set_textauto(optset_T *args)
4169{
4170 // when 'textauto' is set or reset also change 'fileformats'
4171 set_string_option_direct((char_u *)"ffs", -1,
4172 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
4173 OPT_FREE | args->os_flags, 0);
Bram Moolenaar53744302015-07-17 17:38:22 +02004174
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004175 return NULL;
4176}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004178/*
4179 * Process the updated 'textmode' option value.
4180 */
4181 char *
4182did_set_textmode(optset_T *args)
4183{
4184 // when 'textmode' is set or reset also change 'fileformat'
4185 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, args->os_flags);
4186
4187 return NULL;
4188}
4189
4190/*
4191 * Process the new 'textwidth' option value.
4192 */
4193 char *
4194did_set_textwidth(optset_T *args UNUSED)
4195{
4196 char *errmsg = NULL;
4197
4198 if (curbuf->b_p_tw < 0)
4199 {
4200 errmsg = e_argument_must_be_positive;
4201 curbuf->b_p_tw = 0;
4202 }
4203#ifdef FEAT_SYN_HL
4204 {
4205 win_T *wp;
4206 tabpage_T *tp;
4207
4208 FOR_ALL_TAB_WINDOWS(tp, wp)
4209 check_colorcolumn(wp);
4210 }
Bram Moolenaar53744302015-07-17 17:38:22 +02004211#endif
4212
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004213 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214}
4215
4216/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004217 * Process the updated 'title' or the 'icon' option value.
4218 */
4219 char *
4220did_set_title_icon(optset_T *args UNUSED)
4221{
4222 // when 'title' changed, may need to change the title; same for 'icon'
4223 did_set_title();
4224 return NULL;
4225}
4226
4227/*
4228 * Process the new 'titlelen' option value.
4229 */
4230 char *
4231did_set_titlelen(optset_T *args)
4232{
4233 long old_value = args->os_oldval.number;
4234 char *errmsg = NULL;
4235
4236 // if 'titlelen' has changed, redraw the title
4237 if (p_titlelen < 0)
4238 {
4239 errmsg = e_argument_must_be_positive;
4240 p_titlelen = 85;
4241 }
4242 if (starting != NO_SCREEN && old_value != p_titlelen)
4243 need_maketitle = TRUE;
4244
4245 return errmsg;
4246}
4247
4248#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
4249/*
4250 * Process the updated 'undofile' option value.
4251 */
4252 char *
4253did_set_undofile(optset_T *args)
4254{
4255 // Only take action when the option was set.
4256 if (!curbuf->b_p_udf && !p_udf)
4257 return NULL;
4258
4259 // When reset we do not delete the undo file, the option may be set again
4260 // without making any changes in between.
4261 char_u hash[UNDO_HASH_SIZE];
4262 buf_T *save_curbuf = curbuf;
4263
4264 FOR_ALL_BUFFERS(curbuf)
4265 {
4266 // When 'undofile' is set globally: for every buffer, otherwise
4267 // only for the current buffer: Try to read in the undofile,
4268 // if one exists, the buffer wasn't changed and the buffer was
4269 // loaded
4270 if ((curbuf == save_curbuf
4271 || (args->os_flags & OPT_GLOBAL)
4272 || args->os_flags == 0)
4273 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
4274 {
4275#ifdef FEAT_CRYPT
Christian Brabandtaae58342023-04-23 17:50:22 +01004276 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004277 continue;
4278#endif
4279 u_compute_hash(hash);
4280 u_read_undo(NULL, hash, curbuf->b_fname);
4281 }
4282 }
4283 curbuf = save_curbuf;
4284
4285 return NULL;
4286}
4287#endif
4288
4289/*
4290 * Process the new global 'undolevels' option value.
4291 */
4292 static void
4293update_global_undolevels(long value, long old_value)
4294{
4295 // sync undo before 'undolevels' changes
4296
4297 // use the old value, otherwise u_sync() may not work properly
4298 p_ul = old_value;
4299 u_sync(TRUE);
4300 p_ul = value;
4301}
4302
4303/*
4304 * Process the new buffer local 'undolevels' option value.
4305 */
4306 static void
4307update_buflocal_undolevels(long value, long old_value)
4308{
4309 // use the old value, otherwise u_sync() may not work properly
4310 curbuf->b_p_ul = old_value;
4311 u_sync(TRUE);
4312 curbuf->b_p_ul = value;
4313}
4314
4315/*
4316 * Process the new 'undolevels' option value.
4317 */
4318 char *
4319did_set_undolevels(optset_T *args)
4320{
4321 long *pp = (long *)args->os_varp;
4322
4323 if (pp == &p_ul) // global 'undolevels'
4324 update_global_undolevels(args->os_newval.number,
4325 args->os_oldval.number);
4326 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4327 update_buflocal_undolevels(args->os_newval.number,
4328 args->os_oldval.number);
4329
4330 return NULL;
4331}
4332
4333/*
4334 * Process the new 'updatecount' option value.
4335 */
4336 char *
4337did_set_updatecount(optset_T *args)
4338{
4339 long old_value = args->os_oldval.number;
4340 char *errmsg = NULL;
4341
4342 // when 'updatecount' changes from zero to non-zero, open swap files
4343 if (p_uc < 0)
4344 {
4345 errmsg = e_argument_must_be_positive;
4346 p_uc = 100;
4347 }
4348 if (p_uc && !old_value)
4349 ml_open_files();
4350
4351 return errmsg;
4352}
4353
4354/*
4355 * Process the updated 'weirdinvert' option value.
4356 */
4357 char *
4358did_set_weirdinvert(optset_T *args)
4359{
4360 // When 'weirdinvert' changed, set/reset 't_xs'.
4361 // Then set 'weirdinvert' according to value of 't_xs'.
4362 if (p_wiv && !args->os_oldval.boolean)
4363 T_XS = (char_u *)"y";
4364 else if (!p_wiv && args->os_oldval.boolean)
4365 T_XS = empty_option;
4366 p_wiv = (*T_XS != NUL);
4367
4368 return NULL;
4369}
4370
4371/*
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +02004372 * Process the new 'wildchar' / 'wildcharm' option value.
4373 */
4374 char *
4375did_set_wildchar(optset_T *args)
4376{
4377 long c = *(long *)args->os_varp;
4378
4379 // Don't allow key values that wouldn't work as wildchar.
4380 if (c == Ctrl_C || c == '\n' || c == '\r' || c == K_KENTER)
4381 return e_invalid_argument;
4382
4383 return NULL;
4384}
4385
4386/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004387 * Process the new 'window' option value.
4388 */
4389 char *
4390did_set_window(optset_T *args UNUSED)
4391{
4392 if (p_window < 1)
4393 p_window = 1;
4394 else if (p_window >= Rows)
4395 p_window = Rows - 1;
4396 return NULL;
4397}
4398
4399/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004400 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004402 char *
4403did_set_winheight_helpheight(optset_T *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004405 long *pp = (long *)args->os_varp;
4406 char *errmsg = NULL;
4407
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004408 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004410 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004411 p_wh = 1;
4412 }
4413 if (p_wmh > p_wh)
4414 {
4415 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4416 p_wh = p_wmh;
4417 }
4418 if (p_hh < 0)
4419 {
4420 errmsg = e_argument_must_be_positive;
4421 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 }
4423
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004424 // Change window height NOW
4425 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004427 if (pp == &p_wh && curwin->w_height < p_wh)
4428 win_setheight((int)p_wh);
4429 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
4430 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004433 return errmsg;
4434}
4435
4436/*
4437 * Process the new 'winminheight' option value.
4438 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004439 char *
4440did_set_winminheight(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004441{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004442 char *errmsg = NULL;
4443
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004444 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004446 errmsg = e_argument_must_be_positive;
4447 p_wmh = 0;
4448 }
4449 if (p_wmh > p_wh)
4450 {
4451 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4452 p_wmh = p_wh;
4453 }
4454 win_setminheight();
4455
4456 return errmsg;
4457}
4458
4459/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004460 * Process the new 'winminwidth' option value.
4461 */
4462 char *
4463did_set_winminwidth(optset_T *args UNUSED)
4464{
4465 char *errmsg = NULL;
4466
4467 if (p_wmw < 0)
4468 {
4469 errmsg = e_argument_must_be_positive;
4470 p_wmw = 0;
4471 }
4472 if (p_wmw > p_wiw)
4473 {
4474 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4475 p_wmw = p_wiw;
4476 }
4477 win_setminwidth();
4478
4479 return errmsg;
4480}
4481
4482/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004483 * Process the new 'winwidth' option value.
4484 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004485 char *
4486did_set_winwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004487{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004488 char *errmsg = NULL;
4489
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004490 if (p_wiw < 1)
4491 {
4492 errmsg = e_argument_must_be_positive;
4493 p_wiw = 1;
4494 }
4495 if (p_wmw > p_wiw)
4496 {
4497 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4498 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004500
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004501 // Change window width NOW
4502 if (!ONE_WINDOW && curwin->w_width < p_wiw)
4503 win_setwidth((int)p_wiw);
4504
4505 return errmsg;
4506}
4507
4508/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004509 * Process the updated 'wrap' option value.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004510 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004511 char *
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004512did_set_wrap(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004513{
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004514 // Set w_leftcol or w_skipcol to zero.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004515 if (curwin->w_p_wrap)
4516 curwin->w_leftcol = 0;
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004517 else
4518 curwin->w_skipcol = 0;
4519
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004520 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004521}
4522
4523/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004524 * Set the value of a boolean option, and take care of side effects.
4525 * Returns NULL for success, or an error message for an error.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004526 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004527 static char *
4528set_bool_option(
4529 int opt_idx, // index in options[] table
4530 char_u *varp, // pointer to the option variable
4531 int value, // new value
4532 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004533{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004534 int old_value = *(int *)varp;
4535#if defined(FEAT_EVAL)
4536 int old_global_value = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004538 char *errmsg = NULL;
4539
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004540 // Disallow changing some options from secure mode
4541 if ((secure
4542#ifdef HAVE_SANDBOX
4543 || sandbox != 0
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004544#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004545 ) && (options[opt_idx].flags & P_SECURE))
4546 return e_not_allowed_here;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004547
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004548#if defined(FEAT_EVAL)
4549 // Save the global value before changing anything. This is needed as for
4550 // a global-only option setting the "local value" in fact sets the global
4551 // value (since there is only one value).
4552 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4553 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
4554 OPT_GLOBAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004556
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004557 *(int *)varp = value; // set the new value
4558#ifdef FEAT_EVAL
4559 // Remember where the option was set.
4560 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004561#endif
4562
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004564 need_mouse_correct = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004567 // May set global value for local option.
4568 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4569 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004570
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004571 // Handle side effects of changing a bool option.
4572 if (options[opt_idx].opt_did_set_cb != NULL)
4573 {
4574 optset_T args;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004575
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004576 CLEAR_FIELD(args);
4577 args.os_varp = varp;
4578 args.os_flags = opt_flags;
4579 args.os_oldval.boolean = old_value;
4580 args.os_newval.boolean = value;
4581 args.os_errbuf = NULL;
4582 errmsg = options[opt_idx].opt_did_set_cb(&args);
zeertzjqdeba02d2023-11-02 21:01:19 +01004583 if (errmsg != NULL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004584 return errmsg;
4585 }
4586
4587 // after handling side effects, call autocommand
4588
4589 options[opt_idx].flags |= P_WAS_SET;
4590
4591#if defined(FEAT_EVAL)
4592 apply_optionset_autocmd(opt_idx, opt_flags,
4593 (long)(old_value ? TRUE : FALSE),
4594 (long)(old_global_value ? TRUE : FALSE),
4595 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004596#endif
4597
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004598 comp_col(); // in case 'ruler' or 'showcmd' changed
zeertzjqfcaed6a2024-02-18 09:33:54 +01004599
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004600 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004601 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0
4602 && (options[opt_idx].flags & P_HLONLY) == 0)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004603 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004604
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004605 if ((opt_flags & OPT_NO_REDRAW) == 0)
4606 check_redraw(options[opt_idx].flags);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004607
4608 return errmsg;
4609}
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004610
4611/*
4612 * Check the bounds of numeric options.
4613 */
4614 static char *
4615check_num_option_bounds(
4616 long *pp,
4617 long old_value,
4618 long old_Rows,
4619 long old_Columns,
4620 char *errbuf,
4621 size_t errbuflen,
4622 char *errmsg)
4623{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 if (Rows < min_rows() && full_screen)
4625 {
4626 if (errbuf != NULL)
4627 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004628 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004629 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 errmsg = errbuf;
4631 }
4632 Rows = min_rows();
4633 }
4634 if (Columns < MIN_COLUMNS && full_screen)
4635 {
4636 if (errbuf != NULL)
4637 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004638 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004639 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 errmsg = errbuf;
4641 }
4642 Columns = MIN_COLUMNS;
4643 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004644 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004646 // If the screen (shell) height has been changed, assume it is the
4647 // physical screenheight.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 if (old_Rows != Rows || old_Columns != Columns)
4649 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004650 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 if (updating_screen)
4652 *pp = old_value;
4653 else if (full_screen
4654#ifdef FEAT_GUI
4655 && !gui.starting
4656#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004657 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 set_shellsize((int)Columns, (int)Rows, TRUE);
4659 else
4660 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004661 // Postpone the resizing; check the size and cmdline position for
4662 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 check_shellsize();
4664 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4665 cmdline_row = Rows - p_ch;
4666 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004667 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004668 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 }
4670
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 if (curbuf->b_p_ts <= 0)
4672 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004673 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 curbuf->b_p_ts = 8;
4675 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004676 else if (curbuf->b_p_ts > TABSTOP_MAX)
4677 {
4678 errmsg = e_invalid_argument;
4679 curbuf->b_p_ts = 8;
4680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 if (p_tm < 0)
4682 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004683 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 p_tm = 0;
4685 }
4686 if ((curwin->w_p_scr <= 0
4687 || (curwin->w_p_scr > curwin->w_height
4688 && curwin->w_height > 0))
4689 && full_screen)
4690 {
4691 if (pp == &(curwin->w_p_scr))
4692 {
4693 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004694 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 win_comp_scroll(curwin);
4696 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004697 // If 'scroll' became invalid because of a side effect silently adjust
4698 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 else if (curwin->w_p_scr <= 0)
4700 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004701 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 curwin->w_p_scr = curwin->w_height;
4703 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004704 if (p_hi < 0)
4705 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004706 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004707 p_hi = 0;
4708 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004709 else if (p_hi > 10000)
4710 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004711 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004712 p_hi = 10000;
4713 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004714 if (p_re < 0 || p_re > 2)
4715 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004716 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004717 p_re = 0;
4718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 if (p_report < 0)
4720 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004721 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 p_report = 1;
4723 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004724 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004726 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 p_sj = Rows / 2;
4728 else
4729 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004730 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 p_sj = 1;
4732 }
4733 }
4734 if (p_so < 0 && full_screen)
4735 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004736 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 p_so = 0;
4738 }
4739 if (p_siso < 0 && full_screen)
4740 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004741 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 p_siso = 0;
4743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 if (p_cwh < 1)
4745 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004746 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 p_cwh = 1;
4748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 if (p_ut < 0)
4750 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004751 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 p_ut = 2000;
4753 }
4754 if (p_ss < 0)
4755 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004756 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 p_ss = 0;
4758 }
4759
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004760 return errmsg;
4761}
4762
4763/*
4764 * Set the value of a number option, and take care of side effects.
4765 * Returns NULL for success, or an error message for an error.
4766 */
4767 static char *
4768set_num_option(
4769 int opt_idx, // index in options[] table
4770 char_u *varp, // pointer to the option variable
4771 long value, // new value
4772 char *errbuf, // buffer for error messages
4773 size_t errbuflen, // length of "errbuf"
4774 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4775 // OPT_MODELINE, etc.
4776{
4777 char *errmsg = NULL;
4778 long old_value = *(long *)varp;
4779#if defined(FEAT_EVAL)
4780 long old_global_value = 0; // only used when setting a local and
4781 // global option
4782#endif
4783 long old_Rows = Rows; // remember old Rows
4784 long old_Columns = Columns; // remember old Columns
4785 long *pp = (long *)varp;
4786
4787 // Disallow changing some options from secure mode.
4788 if ((secure
4789#ifdef HAVE_SANDBOX
4790 || sandbox != 0
4791#endif
4792 ) && (options[opt_idx].flags & P_SECURE))
4793 return e_not_allowed_here;
4794
4795#if defined(FEAT_EVAL)
4796 // Save the global value before changing anything. This is needed as for
4797 // a global-only option setting the "local value" in fact sets the global
4798 // value (since there is only one value).
4799 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4800 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4801 OPT_GLOBAL);
4802#endif
4803
4804 *pp = value;
4805#ifdef FEAT_EVAL
4806 // Remember where the option was set.
4807 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4808#endif
4809#ifdef FEAT_GUI
4810 need_mouse_correct = TRUE;
4811#endif
4812
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004813 // Invoke the option specific callback function to validate and apply the
4814 // new value.
4815 if (options[opt_idx].opt_did_set_cb != NULL)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004816 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004817 optset_T args;
4818
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004819 CLEAR_FIELD(args);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004820 args.os_varp = varp;
4821 args.os_flags = opt_flags;
4822 args.os_oldval.number = old_value;
4823 args.os_newval.number = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004824 args.os_errbuf = NULL;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004825 errmsg = options[opt_idx].opt_did_set_cb(&args);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004826 }
4827
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004828 // Check the bounds for numeric options here
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004829 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4830 errbuf, errbuflen, errmsg);
4831
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004832 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4834 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4835
4836 options[opt_idx].flags |= P_WAS_SET;
4837
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004838#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004839 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4840 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004841#endif
4842
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004843 comp_col(); // in case 'columns' or 'ls' changed
zeertzjqfcaed6a2024-02-18 09:33:54 +01004844
Bram Moolenaar913077c2012-03-28 19:59:04 +02004845 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004846 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0
4847 && (options[opt_idx].flags & P_HLONLY) == 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004848 curwin->w_set_curswant = TRUE;
zeertzjqfcaed6a2024-02-18 09:33:54 +01004849
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004850 if ((opt_flags & OPT_NO_REDRAW) == 0)
4851 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852
4853 return errmsg;
4854}
4855
4856/*
4857 * Called after an option changed: check if something needs to be redrawn.
4858 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004859 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004860check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004862 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004863 int doclear = (flags & P_RCLR) == P_RCLR;
4864 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004866 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868
4869 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
zeertzjqfcaed6a2024-02-18 09:33:54 +01004870 {
4871 if (flags & P_HLONLY)
4872 redraw_later(UPD_NOT_VALID);
4873 else
4874 changed_window_setting();
4875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004877 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004878 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004879 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004881 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882}
4883
4884/*
4885 * Find index for option 'arg'.
4886 * Return -1 if not found.
4887 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004888 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004889findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890{
4891 int opt_idx;
4892 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004893 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 int is_term_opt;
4895
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004896 // For first call: Initialize the quick-access table.
4897 // It contains the index for the first option that starts with a certain
4898 // letter. There are 26 letters, plus the first "t_" option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 if (quick_tab[1] == 0)
4900 {
4901 p = options[0].fullname;
4902 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4903 {
4904 if (s[0] != p[0])
4905 {
4906 if (s[0] == 't' && s[1] == '_')
4907 quick_tab[26] = opt_idx;
4908 else
4909 quick_tab[CharOrdLow(s[0])] = opt_idx;
4910 }
4911 p = s;
4912 }
4913 }
4914
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004915 // Check for name starting with an illegal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 return -1;
4918
4919 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4920 if (is_term_opt)
4921 opt_idx = quick_tab[26];
4922 else
4923 opt_idx = quick_tab[CharOrdLow(arg[0])];
zeertzjqf48558e2023-12-08 21:34:31 +01004924 for (; (s = options[opt_idx].fullname) != NULL && s[0] == arg[0]; opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004926 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 break;
4928 }
zeertzjqf48558e2023-12-08 21:34:31 +01004929 if (s != NULL && s[0] != arg[0])
4930 s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 if (s == NULL && !is_term_opt)
4932 {
4933 opt_idx = quick_tab[CharOrdLow(arg[0])];
4934 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
4935 {
4936 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004937 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 break;
4939 s = NULL;
4940 }
4941 }
4942 if (s == NULL)
4943 opt_idx = -1;
4944 return opt_idx;
4945}
4946
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004947#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
4948 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949/*
4950 * Get the value for an option.
4951 *
4952 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004953 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01004954 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004955 * String option: gov_string, *stringval gets allocated string.
4956 * Hidden Number option: gov_hidden_number.
4957 * Hidden Toggle option: gov_hidden_bool.
4958 * Hidden String option: gov_hidden_string.
4959 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004960 *
4961 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004963 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01004964get_option_value(
4965 char_u *name,
4966 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004967 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004968 int *flagsp,
4969 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970{
4971 int opt_idx;
4972 char_u *varp;
4973
4974 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004975 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01004976 {
4977 int key;
4978
4979 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004980 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004981 {
4982 char_u key_name[2];
4983 char_u *p;
4984
Bram Moolenaar10c75c42021-12-28 20:53:30 +00004985 if (flagsp != NULL)
4986 *flagsp = 0; // terminal option has no flags
4987
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004988 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01004989 if (key < 0)
4990 {
4991 key_name[0] = KEY2TERMCAP0(key);
4992 key_name[1] = KEY2TERMCAP1(key);
4993 }
4994 else
4995 {
4996 key_name[0] = KS_KEY;
4997 key_name[1] = (key & 0xff);
4998 }
4999 p = find_termcode(key_name);
5000 if (p != NULL)
5001 {
5002 if (stringval != NULL)
5003 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005004 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01005005 }
5006 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005007 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01005008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005010 varp = get_varp_scope(&(options[opt_idx]), scope);
5011
5012 if (flagsp != NULL)
5013 // Return the P_xxxx option flags.
5014 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015
5016 if (options[opt_idx].flags & P_STRING)
5017 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005018 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005019 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 if (stringval != NULL)
5021 {
zeertzjq51f0bc32022-05-09 13:33:39 +01005022 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01005023 *stringval = str2special_save(*(char_u **)(varp), FALSE,
5024 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005026 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01005027 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00005028 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01005031 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 *stringval = vim_strsave(*(char_u **)(varp));
5033 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005034 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 }
5036
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005037 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005038 return (options[opt_idx].flags & P_NUM)
5039 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 if (options[opt_idx].flags & P_NUM)
5041 *numval = *(long *)varp;
5042 else
5043 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005044 // Special case: 'modified' is b_changed, but we also want to consider
5045 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 if ((int *)varp == &curbuf->b_changed)
5047 *numval = curbufIsChanged();
5048 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02005049 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005051 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052}
5053#endif
5054
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005055#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005056/*
5057 * Returns the option attributes and its value. Unlike the above function it
5058 * will return either global value or local value of the option depending on
5059 * what was requested, but it will never return global value if it was
5060 * requested to return local one and vice versa. Neither it will return
5061 * buffer-local value if it was requested to return window-local one.
5062 *
5063 * Pretends that option is absent if it is not present in the requested scope
5064 * (i.e. has no global, window-local or buffer-local value depending on
5065 * opt_type). Uses
5066 *
5067 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02005068 * 0 hidden or unknown option, also option that does not have requested
5069 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005070 * see SOPT_* in vim.h for other flags
5071 *
5072 * Possible opt_type values: see SREQ_* in vim.h
5073 */
5074 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005075get_option_value_strict(
5076 char_u *name,
5077 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005078 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01005079 int opt_type,
5080 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005081{
5082 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02005083 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005084 struct vimoption *p;
5085 int r = 0;
5086
5087 opt_idx = findoption(name);
5088 if (opt_idx < 0)
5089 return 0;
5090
5091 p = &(options[opt_idx]);
5092
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005093 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005094 if (p->var == NULL)
5095 return 0;
5096
5097 if (p->flags & P_BOOL)
5098 r |= SOPT_BOOL;
5099 else if (p->flags & P_NUM)
5100 r |= SOPT_NUM;
5101 else if (p->flags & P_STRING)
5102 r |= SOPT_STRING;
5103
5104 if (p->indir == PV_NONE)
5105 {
5106 if (opt_type == SREQ_GLOBAL)
5107 r |= SOPT_GLOBAL;
5108 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005109 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005110 }
5111 else
5112 {
5113 if (p->indir & PV_BOTH)
5114 r |= SOPT_GLOBAL;
5115 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005116 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005117
5118 if (p->indir & PV_WIN)
5119 {
5120 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005121 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005122 else
5123 r |= SOPT_WIN;
5124 }
5125 else if (p->indir & PV_BUF)
5126 {
5127 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005128 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005129 else
5130 r |= SOPT_BUF;
5131 }
5132 }
5133
5134 if (stringval == NULL)
5135 return r;
5136
5137 if (opt_type == SREQ_GLOBAL)
5138 varp = p->var;
5139 else
5140 {
5141 if (opt_type == SREQ_BUF)
5142 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005143 // Special case: 'modified' is b_changed, but we also want to
5144 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005145 if (p->indir == PV_MOD)
5146 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005147 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005148 varp = NULL;
5149 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005150# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005151 else if (p->indir == PV_KEY)
5152 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005153 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005154 *stringval = NULL;
5155 varp = NULL;
5156 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005157# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005158 else
5159 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005160 buf_T *save_curbuf = curbuf;
5161
5162 // only getting a pointer, no need to use aucmd_prepbuf()
5163 curbuf = (buf_T *)from;
5164 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005165 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02005166 curbuf = save_curbuf;
5167 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005168 }
5169 }
5170 else if (opt_type == SREQ_WIN)
5171 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005172 win_T *save_curwin = curwin;
5173
5174 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005175 curbuf = curwin->w_buffer;
5176 varp = get_varp(p);
5177 curwin = save_curwin;
5178 curbuf = curwin->w_buffer;
5179 }
5180 if (varp == p->var)
5181 return (r | SOPT_UNSET);
5182 }
5183
5184 if (varp != NULL)
5185 {
5186 if (p->flags & P_STRING)
5187 *stringval = vim_strsave(*(char_u **)(varp));
5188 else if (p->flags & P_NUM)
5189 *numval = *(long *) varp;
5190 else
5191 *numval = *(int *)varp;
5192 }
5193
5194 return r;
5195}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005196
5197/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005198 * Iterate over options. First argument is a pointer to a pointer to a
5199 * structure inside options[] array, second is option type like in the above
5200 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005201 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005202 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005203 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005204 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005205 * first argument to NULL.
5206 *
5207 * Returns full option name for current option on each call.
5208 */
5209 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005210option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005211{
5212 struct vimoption *ret = NULL;
5213 do
5214 {
5215 if (*option == NULL)
5216 *option = (void *) options;
5217 else if (((struct vimoption *) (*option))->fullname == NULL)
5218 {
5219 *option = NULL;
5220 return NULL;
5221 }
5222 else
5223 *option = (void *) (((struct vimoption *) (*option)) + 1);
5224
5225 ret = ((struct vimoption *) (*option));
5226
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005227 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005228 if (ret->var == NULL)
5229 {
5230 ret = NULL;
5231 continue;
5232 }
5233
5234 switch (opt_type)
5235 {
5236 case SREQ_GLOBAL:
5237 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
5238 ret = NULL;
5239 break;
5240 case SREQ_BUF:
5241 if (!(ret->indir & PV_BUF))
5242 ret = NULL;
5243 break;
5244 case SREQ_WIN:
5245 if (!(ret->indir & PV_WIN))
5246 ret = NULL;
5247 break;
5248 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005249 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005250 return NULL;
5251 }
5252 }
5253 while (ret == NULL);
5254
5255 return (char_u *)ret->fullname;
5256}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005257#endif
5258
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005260 * Return the flags for the option at 'opt_idx'.
5261 */
5262 long_u
5263get_option_flags(int opt_idx)
5264{
5265 return options[opt_idx].flags;
5266}
5267
5268/*
5269 * Set a flag for the option at 'opt_idx'.
5270 */
5271 void
5272set_option_flag(int opt_idx, long_u flag)
5273{
5274 options[opt_idx].flags |= flag;
5275}
5276
5277/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005278 * Returns TRUE if the option at 'opt_idx' is a global option
5279 */
5280 int
5281is_global_option(int opt_idx)
5282{
5283 return options[opt_idx].indir == PV_NONE;
5284}
5285
5286/*
5287 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5288 * local value.
5289 */
5290 int
5291is_global_local_option(int opt_idx)
5292{
5293 return options[opt_idx].indir & PV_BOTH;
5294}
5295
5296/*
5297 * Returns TRUE if the option at 'opt_idx' is a window-local option
5298 */
5299 int
5300is_window_local_option(int opt_idx)
5301{
5302 return options[opt_idx].var == VAR_WIN;
5303}
5304
5305/*
5306 * Returns TRUE if the option at 'opt_idx' is a hidden option
5307 */
5308 int
5309is_hidden_option(int opt_idx)
5310{
5311 return options[opt_idx].var == NULL;
5312}
5313
5314#if defined(FEAT_CRYPT) || defined(PROTO)
5315/*
5316 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5317 */
5318 int
5319is_crypt_key_option(int opt_idx)
5320{
5321 return options[opt_idx].indir == PV_KEY;
5322}
5323#endif
5324
5325/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 * Set the value of option "name".
5327 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005328 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005329 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005331 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005332set_option_value(
5333 char_u *name,
5334 long number,
5335 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005336 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337{
5338 int opt_idx;
5339 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005340 long_u flags;
Christian Brabandtb39b2402023-11-29 11:34:05 +01005341 static char errbuf[ERR_BUFLEN];
5342 int errbuflen = ERR_BUFLEN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343
5344 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005345 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005346 {
5347 int key;
5348
5349 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005350 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005351 {
5352 char_u key_name[2];
5353
5354 if (key < 0)
5355 {
5356 key_name[0] = KEY2TERMCAP0(key);
5357 key_name[1] = KEY2TERMCAP1(key);
5358 }
5359 else
5360 {
5361 key_name[0] = KS_KEY;
5362 key_name[1] = (key & 0xff);
5363 }
5364 add_termcode(key_name, string, FALSE);
5365 if (full_screen)
5366 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005367 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005368 return NULL;
5369 }
5370
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005371 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 else
5374 {
5375 flags = options[opt_idx].flags;
5376#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005377 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005379 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005380 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005381 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005384 if (flags & P_STRING)
Christian Brabandtb39b2402023-11-29 11:34:05 +01005385 return set_string_option(opt_idx, string, opt_flags, errbuf, errbuflen);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005386
5387 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5388 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005390 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005392 int idx;
5393
5394 // Either we are given a string or we are setting option
5395 // to zero.
5396 for (idx = 0; string[idx] == '0'; ++idx)
5397 ;
5398 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005399 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005400 // There's another character after zeros or the string
5401 // is empty. In both cases, we are trying to set a
5402 // num option using a string.
5403 semsg(_(e_number_required_after_str_equal_str),
5404 name, string);
5405 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005406
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005409 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005410 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005411 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005412 errbuf, sizeof(errbuf), opt_flags);
5413 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005414 else
5415 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005417
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005419 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420}
5421
5422/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005423 * Call set_option_value() and when an error is returned report it.
5424 */
5425 void
5426set_option_value_give_err(
5427 char_u *name,
5428 long number,
5429 char_u *string,
5430 int opt_flags) // OPT_LOCAL or 0 (both)
5431{
5432 char *errmsg = set_option_value(name, number, string, opt_flags);
5433
5434 if (errmsg != NULL)
5435 emsg(_(errmsg));
5436}
5437
5438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 * Get the terminal code for a terminal option.
5440 * Returns NULL when not found.
5441 */
5442 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005443get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444{
5445 int opt_idx;
5446 char_u *varp;
5447
5448 if (tname[0] != 't' || tname[1] != '_' ||
5449 tname[2] == NUL || tname[3] == NUL)
5450 return NULL;
5451 if ((opt_idx = findoption(tname)) >= 0)
5452 {
5453 varp = get_varp(&(options[opt_idx]));
5454 if (varp != NULL)
5455 varp = *(char_u **)(varp);
5456 return varp;
5457 }
5458 return find_termcode(tname + 2);
5459}
5460
5461 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005462get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463{
5464 int i;
5465
5466 i = findoption((char_u *)"hl");
5467 if (i >= 0)
5468 return options[i].def_val[VI_DEFAULT];
5469 return (char_u *)NULL;
5470}
5471
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005472 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005473get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005474{
5475 int i;
5476
5477 i = findoption((char_u *)"enc");
5478 if (i >= 0)
5479 return options[i].def_val[VI_DEFAULT];
5480 return (char_u *)NULL;
5481}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005482
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005483#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005484 int
5485is_option_allocated(char *name)
5486{
5487 int idx = findoption((char_u *)name);
5488
5489 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5490}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005491#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005492
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493/*
5494 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005495 * When "has_lt" is true there is a '<' before "*arg_arg".
5496 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 */
5498 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005499find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005501 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005503 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005505 // Don't use get_special_key_code() for t_xx, we don't want it to call
5506 // add_termcap_entry().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5508 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005509 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005511 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005513 key = find_special_key(&arg, &modifiers,
5514 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005515 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 key = 0;
5517 }
5518 return key;
5519}
5520
5521/*
5522 * if 'all' == 0: show changed options
5523 * if 'all' == 1: show all normal options
5524 * if 'all' == 2: show all terminal options
5525 */
5526 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005527showoptions(
5528 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005529 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530{
5531 struct vimoption *p;
5532 int col;
5533 int isterm;
5534 char_u *varp;
5535 struct vimoption **items;
5536 int item_count;
5537 int run;
5538 int row, rows;
5539 int cols;
5540 int i;
5541 int len;
5542
5543#define INC 20
5544#define GAP 3
5545
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005546 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 if (items == NULL)
5548 return;
5549
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005550 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005552 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005554 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005556 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005558 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005560 // Do the loop two times:
5561 // 1. display the short items
5562 // 2. display the long items (only strings and numbers)
5563 // When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 for (run = 1; run <= 2 && !got_int; ++run)
5565 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005566 // collect the items in items[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 item_count = 0;
5568 for (p = &options[0]; p->fullname != NULL; p++)
5569 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005570 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005571 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005572 continue;
5573
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 varp = NULL;
5575 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005576 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 {
5578 if (p->indir != PV_NONE && !isterm)
5579 varp = get_varp_scope(p, opt_flags);
5580 }
5581 else
5582 varp = get_varp(p);
5583 if (varp != NULL
5584 && ((all == 2 && isterm)
5585 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005586 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005588 if (opt_flags & OPT_ONECOLUMN)
5589 len = Columns;
5590 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005591 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 else
5593 {
5594 option_value2string(p, opt_flags);
5595 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5596 }
5597 if ((len <= INC - GAP && run == 1) ||
5598 (len > INC - GAP && run == 2))
5599 items[item_count++] = p;
5600 }
5601 }
5602
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005603 // display the items
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 if (run == 1)
5605 {
5606 cols = (Columns + GAP - 3) / INC;
5607 if (cols == 0)
5608 cols = 1;
5609 rows = (item_count + cols - 1) / cols;
5610 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005611 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 rows = item_count;
5613 for (row = 0; row < rows && !got_int; ++row)
5614 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005615 msg_putchar('\n'); // go to next line
5616 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 break;
5618 col = 0;
5619 for (i = row; i < item_count; i += rows)
5620 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005621 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 showoneopt(items[i], opt_flags);
5623 col += INC;
5624 }
5625 out_flush();
5626 ui_breakcheck();
5627 }
5628 }
5629 vim_free(items);
5630}
5631
5632/*
5633 * Return TRUE if option "p" has its default value.
5634 */
5635 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005636optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
5638 int dvi;
5639
5640 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005641 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005642 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005644 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005646 // the cast to long is required for Manx C, long_i is
5647 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005648 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005649 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5651}
5652
5653/*
5654 * showoneopt: show the value of one option
5655 * must not be called with a hidden option!
5656 */
5657 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005658showoneopt(
5659 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005660 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005662 char_u *varp;
5663 int save_silent = silent_mode;
5664
5665 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005666 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667
5668 varp = get_varp_scope(p, opt_flags);
5669
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005670 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5672 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005673 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005675 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005677 msg_puts(" ");
5678 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 if (!(p->flags & P_BOOL))
5680 {
5681 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005682 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 option_value2string(p, opt_flags);
5684 msg_outtrans(NameBuff);
5685 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005686
5687 silent_mode = save_silent;
5688 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689}
5690
5691/*
5692 * Write modified options as ":set" commands to a file.
5693 *
5694 * There are three values for "opt_flags":
5695 * OPT_GLOBAL: Write global option values and fresh values of
5696 * buffer-local options (used for start of a session
5697 * file).
5698 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5699 * curwin (used for a vimrc file).
5700 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5701 * and local values for window-local options of
5702 * curwin. Local values are also written when at the
5703 * default value, because a modeline or autocommand
5704 * may have set them when doing ":edit file" and the
5705 * user has set them back at the default or fresh
5706 * value.
5707 * When "local_only" is TRUE, don't write fresh
5708 * values, only local values (for ":mkview").
5709 * (fresh value = value used for a new buffer or window for a local option).
5710 *
5711 * Return FAIL on error, OK otherwise.
5712 */
5713 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005714makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715{
5716 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005717 char_u *varp; // currently used value
5718 char_u *varp_fresh; // local value
5719 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 char *cmd;
5721 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005722 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723
5724 /*
5725 * The options that don't have a default (terminal name, columns, lines)
5726 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005727 * Do the loop over "options[]" twice: once for options with the
5728 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005730 for (pri = 1; pri >= 0; --pri)
5731 {
5732 for (p = &options[0]; !istermoption(p); p++)
5733 if (!(p->flags & P_NO_MKRC)
5734 && !istermoption(p)
5735 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005737 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5739 continue;
5740
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005741 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5742 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5744 continue;
5745
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005746 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005748 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 continue;
5750
Bram Moolenaard23b7142021-04-17 21:04:34 +02005751 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5752 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005753 continue;
5754
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 round = 2;
5756 if (p->indir != PV_NONE)
5757 {
5758 if (p->var == VAR_WIN)
5759 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005760 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 if (!(opt_flags & OPT_LOCAL))
5762 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005763 // When fresh value of window-local option is not at the
5764 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5766 {
5767 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005768 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 {
5770 round = 1;
5771 varp_local = varp;
5772 varp = varp_fresh;
5773 }
5774 }
5775 }
5776 }
5777
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005778 // Round 1: fresh value for window-local options.
5779 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 for ( ; round <= 2; varp = varp_local, ++round)
5781 {
5782 if (round == 1 || (opt_flags & OPT_GLOBAL))
5783 cmd = "set";
5784 else
5785 cmd = "setlocal";
5786
5787 if (p->flags & P_BOOL)
5788 {
5789 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5790 return FAIL;
5791 }
5792 else if (p->flags & P_NUM)
5793 {
5794 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5795 return FAIL;
5796 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005797 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005799 int do_endif = FALSE;
5800
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005801 // Don't set 'syntax' and 'filetype' again if the value is
5802 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005803 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005804#if defined(FEAT_SYN_HL)
5805 p->indir == PV_SYN ||
5806#endif
5807 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 {
5809 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5810 *(char_u **)(varp)) < 0
5811 || put_eol(fd) < 0)
5812 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005813 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 }
5815 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005816 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005818 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 {
5820 if (put_line(fd, "endif") == FAIL)
5821 return FAIL;
5822 }
5823 }
5824 }
5825 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 return OK;
5828}
5829
5830#if defined(FEAT_FOLDING) || defined(PROTO)
5831/*
5832 * Generate set commands for the local fold options only. Used when
5833 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5834 */
5835 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005836makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005838 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005840 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 == FAIL
5842# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005843 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005845 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 == FAIL
5847 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5848 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5849 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5850 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5851 )
5852 return FAIL;
5853
5854 return OK;
5855}
5856#endif
5857
5858 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005859put_setstring(
5860 FILE *fd,
5861 char *cmd,
5862 char *name,
5863 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005864 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865{
5866 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005867 char_u *buf = NULL;
5868 char_u *part = NULL;
5869 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870
5871 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5872 return FAIL;
5873 if (*valuep != NULL)
5874 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005875 // Output 'pastetoggle' as key names. For other
5876 // options some characters have to be escaped with
5877 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 if (valuep == &p_pt)
5879 {
5880 s = *valuep;
5881 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005882 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 return FAIL;
5884 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005885 // expand the option value, replace $HOME by ~
5886 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005888 int size = (int)STRLEN(*valuep) + 1;
5889
5890 // replace home directory in the whole option value into "buf"
5891 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005892 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005893 goto fail;
5894 home_replace(NULL, *valuep, buf, size, FALSE);
5895
5896 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005897 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005898 // can be expanded when read back.
5899 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5900 && vim_strchr(*valuep, ',') != NULL)
5901 {
5902 part = alloc(size);
5903 if (part == NULL)
5904 goto fail;
5905
5906 // write line break to clear the option, e.g. ':set rtp='
5907 if (put_eol(fd) == FAIL)
5908 goto fail;
5909
5910 p = buf;
5911 while (*p != NUL)
5912 {
5913 // for each comma separated option part, append value to
5914 // the option, :set rtp+=value
5915 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5916 goto fail;
5917 (void)copy_option_part(&p, part, size, ",");
5918 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5919 goto fail;
5920 }
5921 vim_free(buf);
5922 vim_free(part);
5923 return OK;
5924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005926 {
5927 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005929 }
5930 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 }
5932 else if (put_escstr(fd, *valuep, 2) == FAIL)
5933 return FAIL;
5934 }
5935 if (put_eol(fd) < 0)
5936 return FAIL;
5937 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005938fail:
5939 vim_free(buf);
5940 vim_free(part);
5941 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942}
5943
5944 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005945put_setnum(
5946 FILE *fd,
5947 char *cmd,
5948 char *name,
5949 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950{
5951 long wc;
5952
5953 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5954 return FAIL;
5955 if (wc_use_keyname((char_u *)valuep, &wc))
5956 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005957 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5959 return FAIL;
5960 }
5961 else if (fprintf(fd, "%ld", *valuep) < 0)
5962 return FAIL;
5963 if (put_eol(fd) < 0)
5964 return FAIL;
5965 return OK;
5966}
5967
5968 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005969put_setbool(
5970 FILE *fd,
5971 char *cmd,
5972 char *name,
5973 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005975 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005976 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5978 || put_eol(fd) < 0)
5979 return FAIL;
5980 return OK;
5981}
5982
5983/*
5984 * Clear all the terminal options.
5985 * If the option has been allocated, free the memory.
5986 * Terminal options are never hidden or indirect.
5987 */
5988 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005989clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005991 // Reset a few things before clearing the old options. This may cause
5992 // outputting a few things that the terminal doesn't understand, but the
5993 // screen will be cleared later, so this is OK.
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005994 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005995 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005997 // When starting the GUI close the display opened for the clipboard.
5998 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 if (gui.starting)
6000 clear_xterm_clip();
6001#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006002 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006004 free_termoptions();
6005}
6006
6007 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006008free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006009{
6010 struct vimoption *p;
6011
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006012 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 if (istermoption(p))
6014 {
6015 if (p->flags & P_ALLOCED)
6016 free_string_option(*(char_u **)(p->var));
6017 if (p->flags & P_DEF_ALLOCED)
6018 free_string_option(p->def_val[VI_DEFAULT]);
6019 *(char_u **)(p->var) = empty_option;
6020 p->def_val[VI_DEFAULT] = empty_option;
6021 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006022#ifdef FEAT_EVAL
6023 // remember where the option was cleared
6024 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
6025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 }
6027 clear_termcodes();
6028}
6029
6030/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00006031 * Free the string for one term option, if it was allocated.
6032 * Set the string to empty_option and clear allocated flag.
6033 * "var" points to the option value.
6034 */
6035 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006036free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00006037{
6038 struct vimoption *p;
6039
6040 for (p = &options[0]; p->fullname != NULL; p++)
6041 if (p->var == var)
6042 {
6043 if (p->flags & P_ALLOCED)
6044 free_string_option(*(char_u **)(p->var));
6045 *(char_u **)(p->var) = empty_option;
6046 p->flags &= ~P_ALLOCED;
6047 break;
6048 }
6049}
6050
6051/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 * Set the terminal option defaults to the current value.
6053 * Used after setting the terminal name.
6054 */
6055 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006056set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057{
6058 struct vimoption *p;
6059
6060 for (p = &options[0]; p->fullname != NULL; p++)
6061 {
6062 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
6063 {
6064 if (p->flags & P_DEF_ALLOCED)
6065 {
6066 free_string_option(p->def_val[VI_DEFAULT]);
6067 p->flags &= ~P_DEF_ALLOCED;
6068 }
6069 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
6070 if (p->flags & P_ALLOCED)
6071 {
6072 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006073 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 }
6075 }
6076 }
6077}
6078
6079/*
6080 * return TRUE if 'p' starts with 't_'
6081 */
6082 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006083istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084{
6085 return (p->fullname[0] == 't' && p->fullname[1] == '_');
6086}
6087
Bram Moolenaardac13472019-09-16 21:06:21 +02006088/*
6089 * Returns TRUE if the option at 'opt_idx' starts with 't_'
6090 */
6091 int
6092istermoption_idx(int opt_idx)
6093{
6094 return istermoption(&options[opt_idx]);
6095}
6096
Bram Moolenaar113e1072019-01-20 15:30:40 +01006097#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006099 * Unset local option value, similar to ":set opt<".
6100 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006101 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006102unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006103{
6104 struct vimoption *p;
6105 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006106 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006107
6108 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02006109 if (opt_idx < 0)
6110 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006111 p = &(options[opt_idx]);
6112
6113 switch ((int)p->indir)
6114 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006115 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006116 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006117 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006118 break;
6119 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006120 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006121 break;
6122 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006123 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006124 break;
6125 case PV_AR:
6126 buf->b_p_ar = -1;
6127 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006128 case PV_BKC:
6129 clear_string_option(&buf->b_p_bkc);
6130 buf->b_bkc_flags = 0;
6131 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006132 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006133 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006134 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006135 case PV_TC:
6136 clear_string_option(&buf->b_p_tc);
6137 buf->b_tc_flags = 0;
6138 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006139 case PV_SISO:
6140 curwin->w_p_siso = -1;
6141 break;
6142 case PV_SO:
6143 curwin->w_p_so = -1;
6144 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006145# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006146 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006147 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006148 break;
6149 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006150 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006151 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006152# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006153 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006154 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006155 break;
6156 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006157 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006158 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006159# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006160 case PV_TSRFU:
6161 clear_string_option(&buf->b_p_tsrfu);
6162 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006163# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006164 case PV_FP:
6165 clear_string_option(&buf->b_p_fp);
6166 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006167# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006168 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006169 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006170 break;
6171 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006172 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006173 break;
6174 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006175 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006176 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006177# endif
6178# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006179 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006180 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006181 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006182# endif
6183# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006184 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006185 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006186 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006187# endif
6188# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006189 case PV_SBR:
6190 clear_string_option(&((win_T *)from)->w_p_sbr);
6191 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006192# endif
6193# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006194 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006195 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006196 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006197# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006198 case PV_UL:
6199 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6200 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006201 case PV_LW:
6202 clear_string_option(&buf->b_p_lw);
6203 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006204 case PV_MENC:
6205 clear_string_option(&buf->b_p_menc);
6206 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006207 case PV_LCS:
6208 clear_string_option(&((win_T *)from)->w_p_lcs);
zeertzjq6a8d2e12024-01-17 20:54:49 +01006209 set_listchars_option((win_T *)from, ((win_T *)from)->w_p_lcs, TRUE,
6210 NULL, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006211 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006212 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006213 case PV_FCS:
6214 clear_string_option(&((win_T *)from)->w_p_fcs);
zeertzjq6a8d2e12024-01-17 20:54:49 +01006215 set_fillchars_option((win_T *)from, ((win_T *)from)->w_p_fcs, TRUE,
6216 NULL, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006217 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006218 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006219 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006220 clear_string_option(&((win_T *)from)->w_p_ve);
6221 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006222 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006223 }
6224}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006225#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006226
6227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006229 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 */
6231 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006232get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006234 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 {
6236 if (p->var == VAR_WIN)
6237 return (char_u *)GLOBAL_WO(get_varp(p));
6238 return p->var;
6239 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006240 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 {
6242 switch ((int)p->indir)
6243 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006244 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006246 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
6247 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6248 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006250 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6251 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6252 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006253 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006254 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006255 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006256 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6257 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006259 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6260 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006262 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6263 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006264#ifdef FEAT_COMPL_FUNC
6265 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6266#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006267#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6268 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6269#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006270#if defined(FEAT_CRYPT)
6271 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6272#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006273#ifdef FEAT_LINEBREAK
6274 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6275#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006276#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006277 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006278#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006279 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006280 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006281 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006282 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006283 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006284 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006285 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006286
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006288 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 }
6290 return get_varp(p);
6291}
6292
6293/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006294 * Get pointer to option variable at 'opt_idx', depending on local or global
6295 * scope.
6296 */
6297 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006298get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006299{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006300 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006301}
6302
6303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 * Get pointer to option variable.
6305 */
6306 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006307get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006309 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 if (p->var == NULL)
6311 return NULL;
6312
6313 switch ((int)p->indir)
6314 {
6315 case PV_NONE: return p->var;
6316
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006317 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006318 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006320 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006322 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006324 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006326 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006328 case PV_TC: return *curbuf->b_p_tc != NUL
6329 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006330 case PV_BKC: return *curbuf->b_p_bkc != NUL
6331 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006332 case PV_SISO: return curwin->w_p_siso >= 0
6333 ? (char_u *)&(curwin->w_p_siso) : p->var;
6334 case PV_SO: return curwin->w_p_so >= 0
6335 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006337 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006339 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6341#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006342 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006344 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006346#ifdef FEAT_COMPL_FUNC
6347 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6348 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6349#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006350 case PV_FP: return *curbuf->b_p_fp != NUL
6351 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006353 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006355 case PV_GP: return *curbuf->b_p_gp != NUL
6356 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6357 case PV_MP: return *curbuf->b_p_mp != NUL
6358 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006360#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6361 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6362 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6363#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006364#if defined(FEAT_CRYPT)
6365 case PV_CM: return *curbuf->b_p_cm != NUL
6366 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6367#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006368#ifdef FEAT_LINEBREAK
6369 case PV_SBR: return *curwin->w_p_sbr != NUL
6370 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6371#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006372#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006373 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006374 ? (char_u *)&(curwin->w_p_stl) : p->var;
6375#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006376 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6377 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006378 case PV_LW: return *curbuf->b_p_lw != NUL
6379 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006380 case PV_MENC: return *curbuf->b_p_menc != NUL
6381 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382#ifdef FEAT_ARABIC
6383 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6384#endif
6385 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006386 case PV_LCS: return *curwin->w_p_lcs != NUL
6387 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006388 case PV_FCS: return *curwin->w_p_fcs != NUL
6389 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006390 case PV_VE: return *curwin->w_p_ve != NUL
6391 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006392#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006393 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6394#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006395#ifdef FEAT_SYN_HL
6396 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6397 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006398 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006399 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401#ifdef FEAT_DIFF
6402 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6403#endif
6404#ifdef FEAT_FOLDING
6405 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6406 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6407 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6408 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6409 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6410 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6411 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6412# ifdef FEAT_EVAL
6413 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6414 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6415# endif
6416 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6417#endif
6418 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006419 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006420#ifdef FEAT_LINEBREAK
6421 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6422#endif
Colin Kennedy21570352024-03-03 16:16:47 +01006423 case PV_WFB: return (char_u *)&(curwin->w_p_wfb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006425 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006426#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6428#endif
6429#ifdef FEAT_RIGHTLEFT
6430 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6431 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6432#endif
6433 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006434 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6436#ifdef FEAT_LINEBREAK
6437 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006438 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6439 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006441 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006443 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006444#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006445 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6446 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6447#endif
6448#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006449 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6450 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6451 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453
6454 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6455 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6458 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6460 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6462 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6463 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006464 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467#ifdef FEAT_FOLDING
6468 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006471#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006472 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006474#ifdef FEAT_COMPL_FUNC
6475 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006476 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006477#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006478#ifdef FEAT_EVAL
6479 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6480#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006481 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006483 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006489 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6491 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6492 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6493 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6494#ifdef FEAT_FIND_ID
6495# ifdef FEAT_EVAL
6496 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6497# endif
6498#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006499#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6501 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6502#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006503#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006504 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506#ifdef FEAT_CRYPT
6507 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006510 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6512 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6513 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6514 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6515 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006517 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6524#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006525 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006526 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6527#endif
6528#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006529 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6530 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6531 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006532 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533#endif
6534 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6535 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6536 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6537 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006538#ifdef FEAT_PERSISTENT_UNDO
6539 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6542#ifdef FEAT_KEYMAP
6543 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6544#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006545#ifdef FEAT_SIGNS
6546 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6547#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006548#ifdef FEAT_VARTABS
6549 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6550 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6551#endif
RestorerZ68ebcee2023-05-31 17:12:14 +01006552 default: iemsg(e_get_varp_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006554 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 return (char_u *)&(curbuf->b_p_wm);
6556}
6557
6558/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006559 * Return a pointer to the variable for option at 'opt_idx'
6560 */
6561 char_u *
6562get_option_var(int opt_idx)
6563{
6564 return options[opt_idx].var;
6565}
6566
Dominique Pelle748b3082022-01-08 12:41:16 +00006567#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006568/*
6569 * Return the full name of the option at 'opt_idx'
6570 */
6571 char_u *
6572get_option_fullname(int opt_idx)
6573{
6574 return (char_u *)options[opt_idx].fullname;
6575}
Dominique Pelle748b3082022-01-08 12:41:16 +00006576#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006577
6578/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00006579 * Return the did_set callback function for the option at 'opt_idx'
6580 */
6581 opt_did_set_cb_T
6582get_option_did_set_cb(int opt_idx)
6583{
6584 return options[opt_idx].opt_did_set_cb;
6585}
6586
6587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 * Get the value of 'equalprg', either the buffer-local one or the global one.
6589 */
6590 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006591get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592{
6593 if (*curbuf->b_p_ep == NUL)
6594 return p_ep;
6595 return curbuf->b_p_ep;
6596}
6597
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598/*
6599 * Copy options from one window to another.
6600 * Used when splitting a window.
6601 */
6602 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006603win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604{
6605 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6606 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006607 after_copy_winopt(wp_to);
6608}
6609
6610/*
6611 * After copying window options: update variables depending on options.
6612 */
6613 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006614after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006615{
6616#ifdef FEAT_LINEBREAK
6617 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006618#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006619#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006620 fill_culopt_flags(NULL, wp);
6621 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006622#endif
zeertzjq6a8d2e12024-01-17 20:54:49 +01006623 set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
6624 set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006625}
6626
6627 static char_u *
6628copy_option_val(char_u *val)
6629{
6630 if (val == empty_option)
6631 return empty_option; // no need to allocate memory
6632 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634
6635/*
6636 * Copy the options from one winopt_T to another.
6637 * Doesn't free the old option values in "to", use clear_winopt() for that.
6638 * The 'scroll' option is not copied, because it depends on the window height.
6639 * The 'previewwindow' option is reset, there can be only one preview window.
6640 */
6641 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006642copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643{
6644#ifdef FEAT_ARABIC
6645 to->wo_arab = from->wo_arab;
6646#endif
6647 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006648 to->wo_lcs = copy_option_val(from->wo_lcs);
6649 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006651 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006652 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006653 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006654#ifdef FEAT_LINEBREAK
6655 to->wo_nuw = from->wo_nuw;
6656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657#ifdef FEAT_RIGHTLEFT
6658 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006659 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006661#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006662 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006663#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006664#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006665 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006668#ifdef FEAT_DIFF
6669 to->wo_wrap_save = from->wo_wrap_save;
6670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671#ifdef FEAT_LINEBREAK
6672 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006673 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006674 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006676 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006678 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006679 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006680 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006681 to->wo_crb_save = from->wo_crb_save;
Christian Brabandt4a8eb6e2023-08-13 19:43:42 +02006682 to->wo_siso = from->wo_siso;
6683 to->wo_so = from->wo_so;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006684#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006685 to->wo_spell = from->wo_spell;
6686#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006687#ifdef FEAT_SYN_HL
6688 to->wo_cuc = from->wo_cuc;
6689 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006690 to->wo_culopt = copy_option_val(from->wo_culopt);
6691 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693#ifdef FEAT_DIFF
6694 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006695 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006697#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006698 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006699 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006700#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006701#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006702 to->wo_twk = copy_option_val(from->wo_twk);
6703 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705#ifdef FEAT_FOLDING
6706 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006707 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006709 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006710 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 to->wo_fml = from->wo_fml;
6712 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006713 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006714 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006715 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006716 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 to->wo_fdn = from->wo_fdn;
6718# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006719 to->wo_fde = copy_option_val(from->wo_fde);
6720 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006722 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006724#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006725 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006726#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006727
6728#ifdef FEAT_EVAL
6729 // Copy the script context so that we know where the value was last set.
6730 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6731 sizeof(to->wo_script_ctx));
6732#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006733 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734}
6735
6736/*
6737 * Check string options in a window for a NULL value.
6738 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006739 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006740check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741{
6742 check_winopt(&win->w_onebuf_opt);
6743 check_winopt(&win->w_allbuf_opt);
6744}
6745
6746/*
6747 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6748 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006749 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006750check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751{
6752#ifdef FEAT_FOLDING
6753 check_string_option(&wop->wo_fdi);
6754 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006755 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756# ifdef FEAT_EVAL
6757 check_string_option(&wop->wo_fde);
6758 check_string_option(&wop->wo_fdt);
6759# endif
6760 check_string_option(&wop->wo_fmr);
6761#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006762#ifdef FEAT_SIGNS
6763 check_string_option(&wop->wo_scl);
6764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765#ifdef FEAT_RIGHTLEFT
6766 check_string_option(&wop->wo_rlc);
6767#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006768#ifdef FEAT_LINEBREAK
6769 check_string_option(&wop->wo_sbr);
6770#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006771#ifdef FEAT_STL_OPT
6772 check_string_option(&wop->wo_stl);
6773#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006774#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006775 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006776 check_string_option(&wop->wo_cc);
6777#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006778#ifdef FEAT_CONCEAL
6779 check_string_option(&wop->wo_cocu);
6780#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006781#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006782 check_string_option(&wop->wo_twk);
6783 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006784#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006785#ifdef FEAT_LINEBREAK
6786 check_string_option(&wop->wo_briopt);
6787#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006788 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006789 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006790 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006791 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792}
6793
6794/*
6795 * Free the allocated memory inside a winopt_T.
6796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006798clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799{
6800#ifdef FEAT_FOLDING
6801 clear_string_option(&wop->wo_fdi);
6802 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006803 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804# ifdef FEAT_EVAL
6805 clear_string_option(&wop->wo_fde);
6806 clear_string_option(&wop->wo_fdt);
6807# endif
6808 clear_string_option(&wop->wo_fmr);
6809#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006810#ifdef FEAT_SIGNS
6811 clear_string_option(&wop->wo_scl);
6812#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006813#ifdef FEAT_LINEBREAK
6814 clear_string_option(&wop->wo_briopt);
6815#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006816 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817#ifdef FEAT_RIGHTLEFT
6818 clear_string_option(&wop->wo_rlc);
6819#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006820#ifdef FEAT_LINEBREAK
6821 clear_string_option(&wop->wo_sbr);
6822#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006823#ifdef FEAT_STL_OPT
6824 clear_string_option(&wop->wo_stl);
6825#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006826#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006827 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006828 clear_string_option(&wop->wo_cc);
6829#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006830#ifdef FEAT_CONCEAL
6831 clear_string_option(&wop->wo_cocu);
6832#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006833#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006834 clear_string_option(&wop->wo_twk);
6835 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006836#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006837 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006838 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006839 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840}
6841
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006842#ifdef FEAT_EVAL
6843// Index into the options table for a buffer-local option enum.
6844static int buf_opt_idx[BV_COUNT];
6845# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6846
6847/*
6848 * Initialize buf_opt_idx[] if not done already.
6849 */
6850 static void
6851init_buf_opt_idx(void)
6852{
6853 static int did_init_buf_opt_idx = FALSE;
6854 int i;
6855
6856 if (did_init_buf_opt_idx)
6857 return;
6858 did_init_buf_opt_idx = TRUE;
6859 for (i = 0; !istermoption_idx(i); i++)
6860 if (options[i].indir & PV_BUF)
6861 buf_opt_idx[options[i].indir & PV_MASK] = i;
6862}
6863#else
6864# define COPY_OPT_SCTX(buf, bv)
6865#endif
6866
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867/*
6868 * Copy global option values to local options for one buffer.
6869 * Used when creating a new buffer and sometimes when entering a buffer.
6870 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006871 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6873 * appropriate.
6874 * BCO_NOHELP Don't copy the values to a help buffer.
6875 */
6876 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006877buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878{
6879 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006880 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 int dont_do_help;
6882 int did_isk = FALSE;
6883
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006884 // Skip this when the option defaults have not been set yet. Happens when
6885 // main() allocates the first buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 if (p_cpo != NULL)
6887 {
6888 /*
6889 * Always copy when entering and 'cpo' contains 'S'.
6890 * Don't copy when already initialized.
6891 * Don't copy when 'cpo' contains 's' and not entering.
6892 * 'S' BCO_ENTER initialized 's' should_copy
6893 * yes yes X X TRUE
6894 * yes no yes X FALSE
6895 * no X yes X FALSE
6896 * X no no yes FALSE
6897 * X no no no TRUE
6898 * no yes no X TRUE
6899 */
6900 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6901 && (buf->b_p_initialized
6902 || (!(flags & BCO_ENTER)
6903 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6904 should_copy = FALSE;
6905
6906 if (should_copy || (flags & BCO_ALWAYS))
6907 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006908#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006909 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006910 init_buf_opt_idx();
6911#endif
6912 // Don't copy the options specific to a help buffer when
6913 // BCO_NOHELP is given or the options were initialized already
6914 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6916 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006917 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 {
6919 save_p_isk = buf->b_p_isk;
6920 buf->b_p_isk = NULL;
6921 }
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006922 // Always free the allocated strings. If not already initialized,
6923 // reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 if (!buf->b_p_initialized)
6925 {
6926 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006927 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006930 switch (*p_ffs)
6931 {
6932 case 'm':
6933 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6934 case 'd':
6935 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6936 case 'u':
6937 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6938 default:
6939 buf->b_p_ff = vim_strsave(p_ff);
6940 }
6941 if (buf->b_p_ff != NULL)
6942 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 buf->b_p_bh = empty_option;
6944 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 }
6946 else
6947 free_buf_options(buf, FALSE);
6948
6949 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006950 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 buf->b_p_ai_nopaste = p_ai_nopaste;
6952 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006953 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006955 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 buf->b_p_tw_nopaste = p_tw_nopaste;
6957 buf->b_p_tw_nobin = p_tw_nobin;
6958 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006959 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 buf->b_p_wm_nopaste = p_wm_nopaste;
6961 buf->b_p_wm_nobin = p_wm_nobin;
6962 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006963 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006964 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006965 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006966 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006967 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006969 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006971 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006973 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 buf->b_p_ml_nobin = p_ml_nobin;
6975 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006976 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006977 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006978 buf->b_p_swf = FALSE;
6979 else
6980 {
6981 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006982 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006985 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006986#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006987 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006988 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006990#ifdef FEAT_COMPL_FUNC
6991 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006992 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006993 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006994 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006995 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006996 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006997#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006998#ifdef FEAT_EVAL
6999 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007000 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007001 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02007002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007004 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007006#ifdef FEAT_VARTABS
7007 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007008 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007009 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007010 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007011 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007012 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007013 buf->b_p_vsts_nopaste = p_vsts_nopaste
7014 ? vim_strsave(p_vsts_nopaste) : NULL;
7015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007017 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007019 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020#ifdef FEAT_FOLDING
7021 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007022 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023#endif
7024 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007025 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007026 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007027 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02007028 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
7029 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007031 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007033 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007035 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007037 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007038
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007040 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007042 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007044 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01007045 buf->b_p_cinsd = vim_strsave(p_cinsd);
7046 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01007047 buf->b_p_lop = vim_strsave(p_lop);
7048 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007049
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007050 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007053 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007055 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007057 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007059 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00007061 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007062 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01007063 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007064#endif
7065#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02007066 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007067 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007068 (void)compile_cap_prog(&buf->b_s);
7069 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007070 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007071 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007072 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02007073 buf->b_s.b_p_spo = vim_strsave(p_spo);
7074 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007076#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007078 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007080 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01007082 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007083#if defined(FEAT_EVAL)
7084 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007085 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087#ifdef FEAT_CRYPT
7088 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007089 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007092 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093#ifdef FEAT_KEYMAP
7094 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007095 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 buf->b_kmap_state |= KEYMAP_INIT;
7097#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007098#ifdef FEAT_TERMINAL
7099 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007100 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007101#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007102 // This isn't really an option, but copying the langmap and IME
7103 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007105 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007107 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007109 // options that are normally global but also have a local value
7110 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01007112 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007113 buf->b_p_bkc = empty_option;
7114 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115#ifdef FEAT_QUICKFIX
7116 buf->b_p_gp = empty_option;
7117 buf->b_p_mp = empty_option;
7118 buf->b_p_efm = empty_option;
7119#endif
7120 buf->b_p_ep = empty_option;
7121 buf->b_p_kp = empty_option;
7122 buf->b_p_path = empty_option;
7123 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007124 buf->b_p_tc = empty_option;
7125 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126#ifdef FEAT_FIND_ID
7127 buf->b_p_def = empty_option;
7128 buf->b_p_inc = empty_option;
7129# ifdef FEAT_EVAL
7130 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007131 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132# endif
7133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 buf->b_p_dict = empty_option;
7135 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01007136#ifdef FEAT_COMPL_FUNC
7137 buf->b_p_tsrfu = empty_option;
7138#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007139 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007140 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007141#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7142 buf->b_p_bexpr = empty_option;
7143#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007144#if defined(FEAT_CRYPT)
7145 buf->b_p_cm = empty_option;
7146#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007147#ifdef FEAT_PERSISTENT_UNDO
7148 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007149 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007150#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007151 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007152 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007154 // Don't copy the options set by ex_help(), use the saved values,
7155 // when going from a help buffer to a non-help buffer.
7156 // Don't touch these at all when BCO_NOHELP is used and going from
7157 // or to a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007159 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007161#ifdef FEAT_VARTABS
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007162 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007163 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007164 else
7165 buf->b_p_vts_array = NULL;
7166#endif
7167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 else
7169 {
7170 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007171 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 did_isk = TRUE;
7173 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00007174 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007175#ifdef FEAT_VARTABS
7176 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007177 COPY_OPT_SCTX(buf, BV_VTS);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007178 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007179 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007180 else
7181 buf->b_p_vts_array = NULL;
7182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 if (buf->b_p_bt[0] == 'h')
7185 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007187 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 }
7189 }
7190
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007191 // When the options should be copied (ignoring BCO_ALWAYS), set the
7192 // flag that indicates that the options have been initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 if (should_copy)
7194 buf->b_p_initialized = TRUE;
7195 }
7196
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007197 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 if (did_isk)
7199 (void)buf_init_chartab(buf, FALSE);
7200}
7201
7202/*
7203 * Reset the 'modifiable' option and its default value.
7204 */
7205 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007206reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207{
7208 int opt_idx;
7209
7210 curbuf->b_p_ma = FALSE;
7211 p_ma = FALSE;
7212 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007213 if (opt_idx >= 0)
7214 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215}
7216
7217/*
7218 * Set the global value for 'iminsert' to the local value.
7219 */
7220 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007221set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222{
7223 p_iminsert = curbuf->b_p_iminsert;
7224}
7225
7226/*
7227 * Set the global value for 'imsearch' to the local value.
7228 */
7229 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007230set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231{
7232 p_imsearch = curbuf->b_p_imsearch;
7233}
7234
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235static int expand_option_idx = -1;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007236static int expand_option_start_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7238static int expand_option_flags = 0;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007239static int expand_option_append = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240
7241 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007242set_context_in_set_cmd(
7243 expand_T *xp,
7244 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007245 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246{
7247 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007248 long_u flags = 0; // init for GCC
7249 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 char_u *p;
7251 char_u *s;
7252 int is_term_option = FALSE;
7253 int key;
7254
7255 expand_option_flags = opt_flags;
7256
7257 xp->xp_context = EXPAND_SETTINGS;
7258 if (*arg == NUL)
7259 {
7260 xp->xp_pattern = arg;
7261 return;
7262 }
7263 p = arg + STRLEN(arg) - 1;
7264 if (*p == ' ' && *(p - 1) != '\\')
7265 {
7266 xp->xp_pattern = p + 1;
7267 return;
7268 }
7269 while (p > arg)
7270 {
7271 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007272 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 if (*p == ' ' || *p == ',')
7274 {
7275 while (s > arg && *(s - 1) == '\\')
7276 --s;
7277 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007278 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 if (*p == ' ' && ((p - s) & 1) == 0)
7280 {
7281 ++p;
7282 break;
7283 }
7284 --p;
7285 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007286 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 {
7288 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007289 xp->xp_prefix = XP_PREFIX_NO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 p += 2;
7291 }
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007292 else if (STRNCMP(p, "inv", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 {
7294 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007295 xp->xp_prefix = XP_PREFIX_INV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 p += 3;
7297 }
7298 xp->xp_pattern = arg = p;
7299 if (*arg == '<')
7300 {
7301 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007302 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 return;
7304 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007305 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 {
7307 xp->xp_context = EXPAND_NOTHING;
7308 return;
7309 }
7310 nextchar = *++p;
7311 is_term_option = TRUE;
7312 expand_option_name[2] = KEY2TERMCAP0(key);
7313 expand_option_name[3] = KEY2TERMCAP1(key);
7314 }
7315 else
7316 {
7317 if (p[0] == 't' && p[1] == '_')
7318 {
7319 p += 2;
7320 if (*p != NUL)
7321 ++p;
7322 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007323 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 nextchar = *++p;
7325 is_term_option = TRUE;
7326 expand_option_name[2] = p[-2];
7327 expand_option_name[3] = p[-1];
7328 }
7329 else
7330 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007331 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7333 p++;
7334 if (*p == NUL)
7335 return;
7336 nextchar = *p;
7337 *p = NUL;
7338 opt_idx = findoption(arg);
7339 *p = nextchar;
7340 if (opt_idx == -1 || options[opt_idx].var == NULL)
7341 {
7342 xp->xp_context = EXPAND_NOTHING;
7343 return;
7344 }
7345 flags = options[opt_idx].flags;
7346 if (flags & P_BOOL)
7347 {
7348 xp->xp_context = EXPAND_NOTHING;
7349 return;
7350 }
7351 }
7352 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007353 // handle "-=" and "+="
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007354 expand_option_append = FALSE;
7355 int expand_option_subtract = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7357 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007358 if (nextchar == '-')
7359 expand_option_subtract = TRUE;
7360 if (nextchar == '+' || nextchar == '^')
7361 expand_option_append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 ++p;
7363 nextchar = '=';
7364 }
7365 if ((nextchar != '=' && nextchar != ':')
7366 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7367 {
7368 xp->xp_context = EXPAND_UNSUCCESSFUL;
7369 return;
7370 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007371
7372 // Below are for handling expanding a specific option's value after the '='
7373 // or ':'
7374
7375 if (is_term_option)
7376 expand_option_idx = -1;
7377 else
7378 expand_option_idx = opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02007380 if (!is_term_option)
7381 {
7382 if (options[opt_idx].flags & P_NO_CMD_EXPAND)
7383 {
7384 xp->xp_context=EXPAND_UNSUCCESSFUL;
7385 return;
7386 }
7387 }
7388
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 xp->xp_pattern = p + 1;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007390 expand_option_start_col = (int)(p + 1 - xp->xp_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007392 // Certain options currently have special case handling to reuse the
7393 // expansion logic with other commands.
Doug Kearns6dfdff32023-08-27 18:48:51 +02007394#ifdef FEAT_SYN_HL
7395 if (options[opt_idx].var == (char_u *)&p_syn)
7396 {
7397 xp->xp_context = EXPAND_OWNSYNTAX;
7398 return;
7399 }
7400#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007401 if (options[opt_idx].var == (char_u *)&p_ft)
7402 {
7403 xp->xp_context = EXPAND_FILETYPE;
7404 return;
7405 }
Doug Kearns81642d92024-01-04 22:37:44 +01007406#ifdef FEAT_KEYMAP
7407 if (options[opt_idx].var == (char_u *)&p_keymap)
7408 {
7409 xp->xp_context = EXPAND_KEYMAP;
7410 return;
7411 }
7412#endif
Doug Kearns6dfdff32023-08-27 18:48:51 +02007413
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007414 // Now pick. If the option has a custom expander, use that. Otherwise, just
7415 // fill with the existing option value.
7416 if (expand_option_subtract)
7417 {
7418 xp->xp_context = EXPAND_SETTING_SUBTRACT;
7419 return;
7420 }
7421 else if (expand_option_idx >= 0 &&
7422 options[expand_option_idx].opt_expand_cb != NULL)
7423 {
7424 xp->xp_context = EXPAND_STRING_SETTING;
7425 }
7426 else if (*xp->xp_pattern == NUL)
7427 {
7428 xp->xp_context = EXPAND_OLD_SETTING;
7429 return;
7430 }
7431 else
7432 xp->xp_context = EXPAND_NOTHING;
7433
7434 if (is_term_option || (flags & P_NUM))
7435 return;
7436
7437 // Only string options below
7438
7439 // Options that have P_EXPAND are considered to all use file/dir expansion.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 if (flags & P_EXPAND)
7441 {
7442 p = options[opt_idx].var;
7443 if (p == (char_u *)&p_bdir
7444 || p == (char_u *)&p_dir
7445 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007446 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449#ifdef FEAT_SESSION
7450 || p == (char_u *)&p_vdir
7451#endif
7452 )
7453 {
7454 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007455 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 xp->xp_backslash = XP_BS_THREE;
7457 else
7458 xp->xp_backslash = XP_BS_ONE;
7459 }
7460 else
7461 {
7462 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007463 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 if (p == (char_u *)&p_tags)
7465 xp->xp_backslash = XP_BS_THREE;
7466 else
7467 xp->xp_backslash = XP_BS_ONE;
7468 }
Yee Cheng Chin54844852023-10-09 18:12:31 +02007469 if (flags & P_COMMA)
7470 xp->xp_backslash |= XP_BS_COMMA;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 }
7472
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007473 // For an option that is a list of file names, or comma/colon-separated
7474 // values, split it by the delimiter and find the start of the current
7475 // pattern, while accounting for backslash-escaped space/commas/colons.
7476 // Triple-backslashed escaped file names (e.g. 'path') can also be
7477 // delimited by space.
7478 if ((flags & P_EXPAND) || (flags & P_COMMA) || (flags & P_COLON))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007480 for (p = arg + STRLEN(arg) - 1; p >= xp->xp_pattern; --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007482 // count number of backslashes before ' ' or ',' or ':'
7483 if (*p == ' ' || *p == ',' ||
7484 (*p == ':' && (flags & P_COLON)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007486 s = p;
7487 while (s > xp->xp_pattern && *(s - 1) == '\\')
7488 --s;
Yee Cheng Chin54844852023-10-09 18:12:31 +02007489 if ((*p == ' ' && ((xp->xp_backslash & XP_BS_THREE) && (p - s) < 3))
7490#if defined(BACKSLASH_IN_FILENAME)
7491 || (*p == ',' && (flags & P_COMMA) && (p - s) < 1)
7492#else
7493 || (*p == ',' && (flags & P_COMMA) && (p - s) < 2)
7494#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007495 || (*p == ':' && (flags & P_COLON)))
7496 {
7497 xp->xp_pattern = p + 1;
7498 break;
7499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 }
7501 }
7502 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007503
7504 // An option that is a list of single-character flags should always start
7505 // at the end as we don't complete words.
7506 if (flags & P_FLAGLIST)
7507 xp->xp_pattern = arg + STRLEN(arg);
7508
7509 // Some options can either be using file/dir expansions, or custom value
7510 // expansion depending on what the user typed. Unfortunately we have to
7511 // manually handle it here to make sure we have the correct xp_context set.
7512#ifdef FEAT_SPELL
7513 if (options[opt_idx].var == (char_u *)&p_sps)
7514 {
7515 if (STRNCMP(xp->xp_pattern, "file:", 5) == 0)
7516 {
7517 xp->xp_pattern += 5;
7518 return;
7519 }
7520 else if (options[expand_option_idx].opt_expand_cb != NULL)
7521 {
7522 xp->xp_context = EXPAND_STRING_SETTING;
7523 }
7524 }
7525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526}
7527
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007528/*
7529 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7530 *
7531 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7532 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7533 *
7534 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7535 * regular expression 'regmatch', then stores the match in matches[idx] and
7536 * returns TRUE.
7537 *
7538 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7539 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7540 *
7541 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7542 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7543 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02007544 static int
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007545match_str(
7546 char_u *str,
7547 regmatch_T *regmatch,
7548 char_u **matches,
7549 int idx,
7550 int test_only,
7551 int fuzzy,
7552 char_u *fuzzystr,
7553 fuzmatch_str_T *fuzmatch)
7554{
7555 if (!fuzzy)
7556 {
7557 if (vim_regexec(regmatch, str, (colnr_T)0))
7558 {
7559 if (!test_only)
7560 matches[idx] = vim_strsave(str);
7561 return TRUE;
7562 }
7563 }
7564 else
7565 {
7566 int score;
7567
7568 score = fuzzy_match_str(str, fuzzystr);
7569 if (score != 0)
7570 {
7571 if (!test_only)
7572 {
7573 fuzmatch[idx].idx = idx;
7574 fuzmatch[idx].str = vim_strsave(str);
7575 fuzmatch[idx].score = score;
7576 }
7577
7578 return TRUE;
7579 }
7580 }
7581
7582 return FALSE;
7583}
7584
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007586ExpandSettings(
7587 expand_T *xp,
7588 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007589 char_u *fuzzystr,
7590 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007591 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007592 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007594 int num_normal = 0; // Nr of matching non-term-code settings
7595 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 int opt_idx;
7597 int match;
7598 int count = 0;
7599 char_u *str;
7600 int loop;
7601 int is_term_opt;
7602 char_u name_buf[MAX_KEY_NAME_LEN];
7603 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007604 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007605 int fuzzy;
7606 fuzmatch_str_T *fuzmatch = NULL;
7607
Christian Brabandtcb747892022-05-08 21:10:56 +01007608 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007610 // do this loop twice:
7611 // loop == 0: count the number of matching options
7612 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 for (loop = 0; loop <= 1; ++loop)
7614 {
7615 regmatch->rm_ic = ic;
7616 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7617 {
K.Takataeeec2542021-06-02 13:28:16 +02007618 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007619 {
7620 if (match_str((char_u *)names[match], regmatch, *matches,
7621 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 {
7623 if (loop == 0)
7624 num_normal++;
7625 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007626 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 }
7630 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7631 opt_idx++)
7632 {
7633 if (options[opt_idx].var == NULL)
7634 continue;
7635 if (xp->xp_context == EXPAND_BOOL_SETTINGS
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007636 && !(options[opt_idx].flags & P_BOOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007638 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 if (is_term_opt && num_normal > 0)
7640 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007641
7642 if (match_str(str, regmatch, *matches, count, (loop == 0),
7643 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 {
7645 if (loop == 0)
7646 {
7647 if (is_term_opt)
7648 num_term++;
7649 else
7650 num_normal++;
7651 }
7652 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007653 count++;
7654 }
7655 else if (!fuzzy && options[opt_idx].shortname != NULL
7656 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007657 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007658 {
7659 // Compare against the abbreviated option name (for regular
7660 // expression match). Fuzzy matching (previous if) already
7661 // matches against both the expanded and abbreviated names.
7662 if (loop == 0)
7663 {
7664 if (is_term_opt)
7665 num_term++;
7666 else
7667 num_normal++;
7668 }
7669 else
7670 (*matches)[count++] = vim_strsave(str);
7671 }
7672 else if (is_term_opt)
7673 {
7674 name_buf[0] = '<';
7675 name_buf[1] = 't';
7676 name_buf[2] = '_';
7677 name_buf[3] = str[2];
7678 name_buf[4] = str[3];
7679 name_buf[5] = '>';
7680 name_buf[6] = NUL;
7681
7682 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7683 fuzzy, fuzzystr, fuzmatch))
7684 {
7685 if (loop == 0)
7686 num_term++;
7687 else
7688 count++;
7689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 }
7691 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007692
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007693 // Check terminal key codes, these are not in the option table
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7695 {
7696 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7697 {
Keith Thompson184f71c2024-01-04 21:19:04 +01007698 if (!SAFE_isprint(str[0]) || !SAFE_isprint(str[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 continue;
7700
7701 name_buf[0] = 't';
7702 name_buf[1] = '_';
7703 name_buf[2] = str[0];
7704 name_buf[3] = str[1];
7705 name_buf[4] = NUL;
7706
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007707 if (match_str(name_buf, regmatch, *matches, count,
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007708 (loop == 0), fuzzy, fuzzystr, fuzmatch))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007709 {
7710 if (loop == 0)
7711 num_term++;
7712 else
7713 count++;
7714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 else
7716 {
7717 name_buf[0] = '<';
7718 name_buf[1] = 't';
7719 name_buf[2] = '_';
7720 name_buf[3] = str[0];
7721 name_buf[4] = str[1];
7722 name_buf[5] = '>';
7723 name_buf[6] = NUL;
7724
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007725 if (match_str(name_buf, regmatch, *matches, count,
7726 (loop == 0), fuzzy, fuzzystr,
7727 fuzmatch))
7728 {
7729 if (loop == 0)
7730 num_term++;
7731 else
7732 count++;
7733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 }
7735 }
7736
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007737 // Check special key names.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007738 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7740 {
7741 name_buf[0] = '<';
7742 STRCPY(name_buf + 1, str);
7743 STRCAT(name_buf, ">");
7744
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007745 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7746 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 {
7748 if (loop == 0)
7749 num_term++;
7750 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007751 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 }
7753 }
7754 }
7755 if (loop == 0)
7756 {
7757 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007758 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007760 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 else
7762 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007763 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007765 *matches = ALLOC_MULT(char_u *, *numMatches);
7766 if (*matches == NULL)
7767 {
7768 *matches = (char_u **)"";
7769 return FAIL;
7770 }
7771 }
7772 else
7773 {
7774 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7775 if (fuzmatch == NULL)
7776 {
7777 *matches = (char_u **)"";
7778 return FAIL;
7779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 }
7781 }
7782 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007783
7784 if (fuzzy &&
7785 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7786 return FAIL;
7787
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 return OK;
7789}
7790
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007791// Escape an option value that can be used on the command-line with :set.
7792// Caller needs to free the returned string, unless NULL is returned.
7793 static char_u*
7794escape_option_str_cmdline(char_u *var)
7795{
7796 char_u *buf;
7797
7798 // A backslash is required before some characters. This is the reverse of
7799 // what happens in do_set().
7800 buf = vim_strsave_escaped(var, escape_chars);
7801 if (buf == NULL)
7802 return NULL;
7803
7804#ifdef BACKSLASH_IN_FILENAME
7805 // For MS-Windows et al. we don't double backslashes at the start and
7806 // before a file name character.
7807 // The reverse is found at stropt_copy_value().
7808 for (var = buf; *var != NUL; MB_PTR_ADV(var))
7809 if (var[0] == '\\' && var[1] == '\\'
7810 && expand_option_idx >= 0
7811 && (options[expand_option_idx].flags & P_EXPAND)
7812 && vim_isfilec(var[2])
7813 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
7814 STRMOVE(var, var + 1);
7815#endif
7816 return buf;
7817}
7818
7819/*
7820 * Expansion handler for :set= when we just want to fill in with the existing value.
7821 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007823ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007825 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 char_u *buf;
7827
zeertzjqb0d45ec2023-01-25 15:04:22 +00007828 *numMatches = 0;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007829 *matches = ALLOC_MULT(char_u *, 1);
zeertzjqb0d45ec2023-01-25 15:04:22 +00007830 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 return FAIL;
7832
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007833 // For a terminal key code expand_option_idx is < 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 if (expand_option_idx < 0)
7835 {
7836 var = find_termcode(expand_option_name + 2);
7837 if (var == NULL)
7838 expand_option_idx = findoption(expand_option_name);
7839 }
7840
7841 if (expand_option_idx >= 0)
7842 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007843 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 option_value2string(&options[expand_option_idx], expand_option_flags);
7845 var = NameBuff;
7846 }
7847 else if (var == NULL)
7848 var = (char_u *)"";
7849
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007850 buf = escape_option_str_cmdline(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 if (buf == NULL)
7852 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007853 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 return FAIL;
7855 }
7856
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007857 (*matches)[0] = buf;
zeertzjqb0d45ec2023-01-25 15:04:22 +00007858 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 return OK;
7860}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861
7862/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007863 * Expansion handler for :set=/:set+= when the option has a custom expansion handler.
7864 */
7865 int
7866ExpandStringSetting(
7867 expand_T *xp,
7868 regmatch_T *regmatch,
7869 int *numMatches,
7870 char_u ***matches)
7871{
7872 char_u *var = NULL; // init for GCC
7873 char_u *buf;
7874
7875 if (expand_option_idx < 0 ||
7876 options[expand_option_idx].opt_expand_cb == NULL)
7877 {
7878 // Not supposed to reach this. This function is only for options with
7879 // custom expansion callbacks.
7880 return FAIL;
7881 }
7882
7883 optexpand_T args;
7884 args.oe_varp = get_varp_scope(&options[expand_option_idx], expand_option_flags);
7885 args.oe_append = expand_option_append;
7886 args.oe_regmatch = regmatch;
7887 args.oe_xp = xp;
7888 args.oe_set_arg = xp->xp_line + expand_option_start_col;
7889 args.oe_include_orig_val =
7890 !expand_option_append &&
7891 (*args.oe_set_arg == NUL);
7892
7893 // Retrieve the existing value, but escape it as a reverse of setting it.
7894 // We technically only need to do this when oe_append or
7895 // oe_include_orig_val is true.
7896 option_value2string(&options[expand_option_idx], expand_option_flags);
7897 var = NameBuff;
7898 buf = escape_option_str_cmdline(var);
7899 if (buf == NULL)
7900 return FAIL;
7901
7902 args.oe_opt_value = buf;
7903
7904 int num_ret = options[expand_option_idx].opt_expand_cb(&args, numMatches, matches);
7905
7906 vim_free(buf);
7907 return num_ret;
7908}
7909
7910/*
7911 * Expansion handler for :set-=
7912 */
7913 int
7914ExpandSettingSubtract(
7915 expand_T *xp,
7916 regmatch_T *regmatch,
7917 int *numMatches,
7918 char_u ***matches)
7919{
7920 if (expand_option_idx < 0)
7921 // term option
7922 return ExpandOldSetting(numMatches, matches);
7923
7924 char_u *option_val = *(char_u**)get_option_varp_scope(
7925 expand_option_idx, expand_option_flags);
7926
7927 long_u option_flags = options[expand_option_idx].flags;
7928
7929 if (option_flags & P_NUM)
7930 return ExpandOldSetting(numMatches, matches);
7931 else if (option_flags & P_COMMA)
7932 {
7933 // Split the option by comma, then present each option to the user if
7934 // it matches the pattern.
7935 // This condition needs to go first, because 'whichwrap' has both
7936 // P_COMMA and P_FLAGLIST.
7937 garray_T ga;
7938
7939 char_u *item;
7940 char_u *option_copy;
7941 char_u *next_val;
7942 char_u *comma;
7943
7944 if (*option_val == NUL)
7945 return FAIL;
7946
7947 // Make a copy as we need to inject null characters destructively.
7948 option_copy = vim_strsave(option_val);
7949 if (option_copy == NULL)
7950 return FAIL;
7951 next_val = option_copy;
7952
7953 ga_init2(&ga, sizeof(char_u *), 10);
7954
7955 do
7956 {
7957 item = next_val;
7958 comma = vim_strchr(next_val, ',');
7959 while (comma != NULL && comma != next_val && *(comma - 1) == '\\')
7960 {
7961 // "\," is interpreted as a literal comma rather than option
7962 // separator when reading options in copy_option_part(). Skip
7963 // it.
7964 comma = vim_strchr(comma + 1, ',');
7965 }
7966 if (comma != NULL)
7967 {
7968 *comma = NUL; // null-terminate this value, required by later functions
7969 next_val = comma + 1;
7970 }
7971 else
7972 next_val = NULL;
7973
7974 if (*item == NUL)
7975 // empty value, don't add to list
7976 continue;
7977
7978 if (!vim_regexec(regmatch, item, (colnr_T)0))
7979 continue;
7980
7981 char_u *buf = escape_option_str_cmdline(item);
7982 if (buf == NULL)
7983 {
7984 vim_free(option_copy);
7985 ga_clear_strings(&ga);
7986 return FAIL;
7987 }
7988 if (ga_add_string(&ga, buf) != OK)
7989 {
7990 vim_free(buf);
7991 break;
7992 }
7993 } while (next_val != NULL);
7994
7995 vim_free(option_copy);
7996
7997 *matches = ga.ga_data;
7998 *numMatches = ga.ga_len;
7999 return OK;
8000 }
8001 else if (option_flags & P_FLAGLIST)
8002 {
8003 // Only present the flags that are set on the option as the other flags
8004 // are not meaningful to do set-= on.
8005
8006 if (*xp->xp_pattern != NUL)
8007 {
8008 // Don't suggest anything if cmdline is non-empty. Vim's set-=
8009 // behavior requires consecutive strings and it's usually
Viktor Szépedbf749b2023-10-16 09:53:37 +02008010 // unintuitive to users if they try to subtract multiple flags at
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008011 // once.
8012 return FAIL;
8013 }
8014
Yee Cheng Chin6d113472023-10-02 21:38:39 +02008015 size_t num_flags = STRLEN(option_val);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008016 if (num_flags == 0)
8017 return FAIL;
8018
8019 *matches = ALLOC_MULT(char_u *, num_flags + 1);
8020 if (*matches == NULL)
8021 return FAIL;
8022
8023 int count = 0;
8024 char_u *p;
8025
8026 p = vim_strsave(option_val);
8027 if (p == NULL)
8028 {
8029 VIM_CLEAR(*matches);
8030 return FAIL;
8031 }
8032 (*matches)[count++] = p;
8033
8034 if (num_flags > 1)
8035 {
8036 // If more than one flags, split the flags up and expose each
8037 // character as individual choice.
8038 for (char_u *flag = option_val; *flag != NUL; flag++)
8039 {
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02008040 p = vim_strnsave(flag, 1);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008041 if (p == NULL)
8042 break;
8043 (*matches)[count++] = p;
8044 }
8045 }
8046
8047 *numMatches = count;
8048 return OK;
8049 }
8050
8051 return ExpandOldSetting(numMatches, matches);
8052}
8053
8054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 * Get the value for the numeric or string option *opp in a nice format into
8056 * NameBuff[]. Must not be called with a hidden option!
8057 */
8058 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008059option_value2string(
8060 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008061 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062{
8063 char_u *varp;
8064
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008065 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066
8067 if (opp->flags & P_NUM)
8068 {
8069 long wc = 0;
8070
8071 if (wc_use_keyname(varp, &wc))
8072 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
8073 else if (wc != 0)
8074 STRCPY(NameBuff, transchar((int)wc));
8075 else
8076 sprintf((char *)NameBuff, "%ld", *(long *)varp);
8077 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008078 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 {
8080 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008081 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 NameBuff[0] = NUL;
8083#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008084 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008085 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 STRCPY(NameBuff, "*****");
8087#endif
8088 else if (opp->flags & P_EXPAND)
8089 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008090 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 else if ((char_u **)opp->var == &p_pt)
8092 str2specialbuf(p_pt, NameBuff, MAXPATHL);
8093 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008094 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 }
8096}
8097
8098/*
8099 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
8100 * printed as a keyname.
8101 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
8102 */
8103 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008104wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105{
8106 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
8107 {
8108 *wcp = *(long *)varp;
8109 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
8110 return TRUE;
8111 }
8112 return FALSE;
8113}
8114
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 * Return TRUE if "x" is present in 'shortmess' option, or
8117 * 'shortmess' contains 'a' and "x" is present in SHM_A.
8118 */
8119 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008120shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01008122 return p_shm != NULL &&
8123 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 || (vim_strchr(p_shm, 'a') != NULL
8125 && vim_strchr((char_u *)SHM_A, x) != NULL));
8126}
8127
8128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
8130 *
8131 * Reset 'compatible' and set the values for options that didn't get set yet
8132 * to the Vim defaults.
8133 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008134 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 */
8136 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008137vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008139 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00008140 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008141 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142
8143 if (!option_was_set((char_u *)"cp"))
8144 {
8145 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02008146 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
8148 set_option_default(opt_idx, OPT_FREE, FALSE);
8149 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008150 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008152
8153 if (fname != NULL)
8154 {
8155 p = vim_getenv(envname, &dofree);
8156 if (p == NULL)
8157 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008158 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008159 p = FullName_save(fname, FALSE);
8160 if (p != NULL)
8161 {
8162 vim_setenv(envname, p);
8163 vim_free(p);
8164 }
8165 }
8166 else if (dofree)
8167 vim_free(p);
8168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169}
8170
8171/*
8172 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
8173 */
8174 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008175change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008177 int opt_idx;
8178
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 if (p_cp != on)
8180 {
8181 p_cp = on;
8182 compatible_set();
8183 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008184 opt_idx = findoption((char_u *)"cp");
8185 if (opt_idx >= 0)
8186 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187}
8188
8189/*
8190 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02008191 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 */
8193 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008194option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195{
8196 int idx;
8197
8198 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008199 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 return FALSE;
8201 if (options[idx].flags & P_WAS_SET)
8202 return TRUE;
8203 return FALSE;
8204}
8205
8206/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008207 * Reset the flag indicating option "name" was set.
8208 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02008209 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008210reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008211{
8212 int idx = findoption(name);
8213
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008214 if (idx < 0)
8215 return FAIL;
8216
8217 options[idx].flags &= ~P_WAS_SET;
8218 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008219}
8220
8221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 * compatible_set() - Called when 'compatible' has been set or unset.
8223 *
8224 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
8225 * flag) to a Vi compatible value.
8226 * When 'compatible' is unset: Set all options that have a different default
8227 * for Vim (without the P_VI_DEF flag) to that default.
8228 */
8229 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008230compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231{
8232 int opt_idx;
8233
Bram Moolenaardac13472019-09-16 21:06:21 +02008234 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
8236 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
8237 set_option_default(opt_idx, OPT_FREE, p_cp);
8238 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008239 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240}
8241
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 * Check if backspacing over something is allowed.
8244 */
8245 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008246can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008247 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02008249#ifdef FEAT_JOB_CHANNEL
8250 if (what == BS_START && bt_prompt(curbuf))
8251 return FALSE;
8252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 switch (*p_bs)
8254 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008255 case '3': return TRUE;
8256 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 case '1': return (what != BS_START);
8258 case '0': return FALSE;
8259 }
8260 return vim_strchr(p_bs, what) != NULL;
8261}
8262
8263/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01008264 * Return the effective 'scrolloff' value for the current window, using the
8265 * global value when appropriate.
8266 */
8267 long
8268get_scrolloff_value(void)
8269{
8270 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
8271}
8272
8273/*
8274 * Return the effective 'sidescrolloff' value for the current window, using the
8275 * global value when appropriate.
8276 */
8277 long
8278get_sidescrolloff_value(void)
8279{
8280 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
8281}
8282
8283/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008284 * Get the local or global value of 'backupcopy'.
8285 */
8286 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008287get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008288{
8289 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
8290}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02008291
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008292#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02008293/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00008294 * Get the local or global value of 'formatlistpat'.
8295 */
8296 char_u *
8297get_flp_value(buf_T *buf)
8298{
Christian Brabandtc53b4672022-01-15 10:01:05 +00008299 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
8300 return p_flp;
8301 return buf->b_p_flp;
8302}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008303#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00008304
8305/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02008306 * Get the local or global value of the 'virtualedit' flags.
8307 */
8308 unsigned int
8309get_ve_flags(void)
8310{
Gary Johnson51ad8502021-08-03 18:33:08 +02008311 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
8312 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02008313}
8314
Bram Moolenaaree857022019-11-09 23:26:40 +01008315#if defined(FEAT_LINEBREAK) || defined(PROTO)
8316/*
8317 * Get the local or global value of 'showbreak'.
8318 */
8319 char_u *
8320get_showbreak_value(win_T *win)
8321{
8322 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8323 return p_sbr;
8324 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8325 return empty_option;
8326 return win->w_p_sbr;
8327}
8328#endif
8329
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008330#if defined(FEAT_EVAL) || defined(PROTO)
8331/*
8332 * Get window or buffer local options.
8333 */
8334 dict_T *
8335get_winbuf_options(int bufopt)
8336{
8337 dict_T *d;
8338 int opt_idx;
8339
8340 d = dict_alloc();
8341 if (d == NULL)
8342 return NULL;
8343
Bram Moolenaardac13472019-09-16 21:06:21 +02008344 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008345 {
8346 struct vimoption *opt = &options[opt_idx];
8347
8348 if ((bufopt && (opt->indir & PV_BUF))
8349 || (!bufopt && (opt->indir & PV_WIN)))
8350 {
8351 char_u *varp = get_varp(opt);
8352
8353 if (varp != NULL)
8354 {
8355 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008356 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008357 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008358 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008359 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008360 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008361 }
8362 }
8363 }
8364
8365 return d;
8366}
8367#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008368
Bram Moolenaardac13472019-09-16 21:06:21 +02008369#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008370/*
8371 * This is called when 'culopt' is changed
8372 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008373 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008374fill_culopt_flags(char_u *val, win_T *wp)
8375{
8376 char_u *p;
8377 char_u culopt_flags_new = 0;
8378
8379 if (val == NULL)
8380 p = wp->w_p_culopt;
8381 else
8382 p = val;
8383 while (*p != NUL)
8384 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008385 // Note: Keep this in sync with p_culopt_values.
Bram Moolenaar017ba072019-09-14 21:01:23 +02008386 if (STRNCMP(p, "line", 4) == 0)
8387 {
8388 p += 4;
8389 culopt_flags_new |= CULOPT_LINE;
8390 }
8391 else if (STRNCMP(p, "both", 4) == 0)
8392 {
8393 p += 4;
8394 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8395 }
8396 else if (STRNCMP(p, "number", 6) == 0)
8397 {
8398 p += 6;
8399 culopt_flags_new |= CULOPT_NBR;
8400 }
8401 else if (STRNCMP(p, "screenline", 10) == 0)
8402 {
8403 p += 10;
8404 culopt_flags_new |= CULOPT_SCRLINE;
8405 }
8406
8407 if (*p != ',' && *p != NUL)
8408 return FAIL;
8409 if (*p == ',')
8410 ++p;
8411 }
8412
8413 // Can't have both "line" and "screenline".
8414 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8415 return FAIL;
8416 wp->w_p_culopt_flags = culopt_flags_new;
8417
8418 return OK;
8419}
8420#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008421
8422/*
8423 * Get the value of 'magic' adjusted for Vim9 script.
8424 */
8425 int
8426magic_isset(void)
8427{
8428 switch (magic_overruled)
8429 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008430 case OPTION_MAGIC_ON: return TRUE;
8431 case OPTION_MAGIC_OFF: return FALSE;
8432 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008433 }
8434#ifdef FEAT_EVAL
8435 if (in_vim9script())
8436 return TRUE;
8437#endif
8438 return p_magic;
8439}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008440
8441/*
8442 * Set the callback function value for an option that accepts a function name,
8443 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8444 * Returns OK if the option is successfully set to a function, otherwise
8445 * returns FAIL.
8446 */
8447 int
8448option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8449{
8450#ifdef FEAT_EVAL
8451 typval_T *tv;
8452 callback_T cb;
8453
8454 if (optval == NULL || *optval == NUL)
8455 {
8456 free_callback(optcb);
8457 return OK;
8458 }
8459
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008460 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008461 || (STRNCMP(optval, "function(", 9) == 0)
8462 || (STRNCMP(optval, "funcref(", 8) == 0))
8463 // Lambda expression or a funcref
8464 tv = eval_expr(optval, NULL);
8465 else
8466 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008467 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008468 if (tv == NULL)
8469 return FAIL;
8470
8471 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008472 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008473 {
8474 free_tv(tv);
8475 return FAIL;
8476 }
8477
8478 free_callback(optcb);
8479 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008480 if (cb.cb_free_name)
8481 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008482 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008483
8484 // when using Vim9 style "import.funcname" it needs to be expanded to
8485 // "import#funcname".
8486 expand_autload_callback(optcb);
8487
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008488 return OK;
8489#else
8490 return FAIL;
8491#endif
8492}
Christian Brabandt757593c2023-08-22 21:44:10 +02008493
8494#if defined(FEAT_EVAL) || defined(PROTO)
8495 static void
8496didset_options_sctx(int opt_flags, char **buf)
8497{
8498 for (int i = 0; ; ++i)
8499 {
8500 if (buf[i] == NULL)
8501 break;
8502
8503 int idx = findoption((char_u *)buf[i]);
8504 if (idx >= 0)
8505 set_option_sctx_idx(idx, opt_flags, current_sctx);
8506 }
8507}
8508#endif