blob: 9f20bb244bef674c3f629c679d2671f595b9a32d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Code to handle user-settable options. This is all pretty much table-
12 * driven. Checklist for adding a new option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000013 * - Put it in the options array in optiondefs.h (copy an existing entry).
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 * - For a global option: Add a variable for it in option.h.
15 * - For a buffer or window local option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000016 * - Add a PV_XX macro definition to the optiondefs.h file.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017 * - Add a variable to the window or buffer struct in structs.h.
18 * - For a window option, add some code to copy_winopt().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000019 * - For a window string option, add code to check_win_options() and
20 * clear_winopt().
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 * - For a buffer option, add some code to buf_copy_options().
22 * - For a buffer string option, add code to check_buf_options().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000023 * - If it's a numeric option, add any necessary bounds checks to
24 * set_num_option().
25 * - If it's a list of flags, add some code in did_set_string_option(), search
26 * for WW_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027 * - When adding an option with expansion (P_EXPAND), but with a different
28 * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
Bram Moolenaarcea912a2016-10-12 14:20:24 +020029 * - Add documentation! One line in doc/quickref.txt, full description in
Bram Moolenaar071d4272004-06-13 20:20:40 +000030 * options.txt, and any other related places.
31 * - Add an entry in runtime/optwin.vim.
32 * When making changes:
33 * - Adjust the help for the option in doc/option.txt.
34 * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
35 * comment at the help for the 'compatible' option.
36 */
37
38#define IN_OPTION_C
39#include "vim.h"
Bram Moolenaar0eddca42019-09-12 22:26:43 +020040#include "optiondefs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010042static void set_options_default(int opt_flags);
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +010043static void set_string_default_esc(char *name, char_u *val, int escape);
Bram Moolenaarb00ef052020-09-12 14:53:53 +020044static char_u *find_dup_item(char_u *origval, char_u *newval, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010045static char_u *option_expand(int opt_idx, char_u *val);
46static void didset_options(void);
47static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000048#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010049static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000050#else
51# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
52#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010053static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
54static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, size_t errbuflen, int opt_flags);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020055static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020057static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010058static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010059static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010060static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
61static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020062static int istermoption(struct vimoption *p);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000063static char_u *get_varp_scope(struct vimoption *p, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020065static void check_win_options(win_T *win);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000066static void option_value2string(struct vimoption *, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010067static void check_winopt(winopt_T *wop);
68static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010069static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Christian Brabandt757593c2023-08-22 21:44:10 +020071#if defined(FEAT_EVAL) || defined(PROTO)
72static char *(p_bin_dep_opts[]) = {"textwidth", "wrapmargin", "modeline", "expandtab", NULL};
73static char *(p_paste_dep_opts[]) = {"autoindent", "expandtab", "ruler", "showmatch", "smarttab",
74 "softtabstop", "textwidth", "wrapmargin",
75#ifdef FEAT_RIGHTLEFT
76 "hkmap", "revins",
77#endif
78#ifdef FEAT_VARTABS
79 "varsofttabstop",
80#endif
81 NULL};
82static void didset_options_sctx(int opt_flags, char **buf);
83#endif
84
85
Bram Moolenaar071d4272004-06-13 20:20:40 +000086/*
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000087 * Initialize the 'shell' option to a default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 */
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000089 static void
90set_init_default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000091{
92 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000093
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000094 // Find default value for 'shell' option.
95 // Don't use it if it is empty.
Bram Moolenaar7c626922005-02-07 22:01:03 +000096 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010097#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +000098 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010099 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +0000101 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200102#if defined(MSWIN)
103 {
104 // For MS-Windows put the path in quotes instead of escaping spaces.
105 char_u *cmd;
106 size_t len;
107
108 if (vim_strchr(p, ' ') != NULL)
109 {
110 len = STRLEN(p) + 3; // two quotes and a trailing NUL
111 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +0200112 if (cmd != NULL)
113 {
114 vim_snprintf((char *)cmd, len, "\"%s\"", p);
115 set_string_default("sh", cmd);
116 vim_free(cmd);
117 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200118 }
119 else
120 set_string_default("sh", p);
121 }
122#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100123 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200124#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000125}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000127/*
128 * Set the default for 'backupskip' to include environment variables for
129 * temp files.
130 */
131 static void
132set_init_default_backupskip(void)
133{
134 int opt_idx;
135 long_u n;
136 char_u *p;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100137#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000138 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100139#else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000140 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100141#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000142 int len;
143 garray_T ga;
Bram Moolenaar14338022023-03-15 22:05:44 +0000144 char_u *item;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200145
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000146 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000148 ga_init2(&ga, 1, 100);
149 for (n = 0; n < (long)ARRAY_LENGTH(names); ++n)
150 {
Bram Moolenaar14338022023-03-15 22:05:44 +0000151 int mustfree = FALSE;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100152#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000153 if (*names[n] == NUL)
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100154# ifdef MACOS_X
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000155 p = (char_u *)"/private/tmp";
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100156# else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000157 p = (char_u *)"/tmp";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000159 else
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100160#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000161 p = vim_getenv((char_u *)names[n], &mustfree);
162 if (p != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000164 // First time count the NUL, otherwise count the ','.
165 len = (int)STRLEN(p) + 3;
166 item = alloc(len);
Bram Moolenaar14338022023-03-15 22:05:44 +0000167 if (item != NULL)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000168 {
Bram Moolenaar14338022023-03-15 22:05:44 +0000169 STRCPY(item, p);
170 add_pathsep(item);
171 STRCAT(item, "*");
172 if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
173 == NULL
174 && ga_grow(&ga, len) == OK)
175 {
176 if (ga.ga_len > 0)
177 STRCAT(ga.ga_data, ",");
178 STRCAT(ga.ga_data, item);
179 ga.ga_len += len;
180 }
181 vim_free(item);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000184 if (mustfree)
185 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000187 if (ga.ga_data != NULL)
188 {
189 set_string_default("bsk", ga.ga_data);
190 vim_free(ga.ga_data);
191 }
192}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000194/*
195 * Initialize the 'maxmemtot' and 'maxmem' options to a default value.
196 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory.
197 */
198 static void
199set_init_default_maxmemtot(void)
200{
201 int opt_idx;
202 long_u n;
203
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 opt_idx = findoption((char_u *)"maxmemtot");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000205 if (opt_idx < 0)
206 return;
207
208#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
209 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 {
ichizok02560422022-04-05 14:18:44 +0100212#if defined(HAVE_AVAIL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000213 // Use amount of memory available at this moment.
214 n = (mch_avail_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100215#elif defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000216 // Use amount of memory available to Vim.
217 n = (mch_total_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100218#else
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000219 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000221 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
222 opt_idx = findoption((char_u *)"maxmem");
223 if (opt_idx >= 0)
224 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000225#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000226 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
227 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000228#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000229 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000232}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000234/*
235 * Initialize the 'cdpath' option to a default value.
236 */
237 static void
238set_init_default_cdpath(void)
239{
240 int opt_idx;
241 char_u *cdpath;
242 char_u *buf;
243 int i;
244 int j;
245 int mustfree = FALSE;
246
247 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
248 if (cdpath == NULL)
249 return;
250
251 buf = alloc((STRLEN(cdpath) << 1) + 2);
252 if (buf != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000254 buf[0] = ','; // start with ",", current dir first
255 j = 1;
256 for (i = 0; cdpath[i] != NUL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000258 if (vim_ispathlistsep(cdpath[i]))
259 buf[j++] = ',';
260 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000262 if (cdpath[i] == ' ' || cdpath[i] == ',')
263 buf[j++] = '\\';
264 buf[j++] = cdpath[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 }
266 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000267 buf[j] = NUL;
268 opt_idx = findoption((char_u *)"cdpath");
269 if (opt_idx >= 0)
270 {
271 options[opt_idx].def_val[VI_DEFAULT] = buf;
272 options[opt_idx].flags |= P_DEF_ALLOCED;
273 }
274 else
275 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000277 if (mustfree)
278 vim_free(cdpath);
279}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000281/*
282 * Initialize the 'printencoding' option to a default value.
283 */
284 static void
285set_init_default_printencoding(void)
286{
ichizok02560422022-04-05 14:18:44 +0100287#if defined(FEAT_POSTSCRIPT) && \
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000288 (defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100289 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100291# if defined(MSWIN)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000292 (char_u *)"cp1252"
ichizok02560422022-04-05 14:18:44 +0100293# elif defined(VMS)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000294 (char_u *)"dec-mcs"
ichizok02560422022-04-05 14:18:44 +0100295# elif defined(MAC)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000296 (char_u *)"mac-roman"
ichizok02560422022-04-05 14:18:44 +0100297# else // HPUX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000298 (char_u *)"hp-roman8"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000300 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000302}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303
304#ifdef FEAT_POSTSCRIPT
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000305/*
306 * Initialize the 'printexpr' option to a default value.
307 */
308 static void
309set_init_default_printexpr(void)
310{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100311 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100313# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000314 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
ichizok02560422022-04-05 14:18:44 +0100315# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
317
ichizok02560422022-04-05 14:18:44 +0100318# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320# endif
321 );
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000322}
323#endif
324
325#ifdef UNIX
326/*
327 * Force restricted-mode on for "nologin" or "false" $SHELL
328 */
329 static void
330set_init_restricted_mode(void)
331{
332 char_u *p;
333
334 p = get_isolated_shell_name();
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000335 if (p == NULL)
336 return;
337 if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
338 restricted = TRUE;
339 vim_free(p);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000340}
341#endif
342
343#ifdef CLEAN_RUNTIMEPATH
344/*
345 * When Vim is started with the "--clean" argument, set the default value
346 * for the 'runtimepath' and 'packpath' options.
347 */
348 static void
349set_init_clean_rtp(void)
350{
351 int opt_idx;
352
353 opt_idx = findoption((char_u *)"runtimepath");
354 if (opt_idx >= 0)
355 {
356 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
357 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
358 }
359 opt_idx = findoption((char_u *)"packpath");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000360 if (opt_idx < 0)
361 return;
362 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
363 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000364}
365#endif
366
367/*
368 * Expand environment variables and things like "~" for the defaults.
369 * If option_expand() returns non-NULL the variable is expanded. This can
370 * only happen for non-indirect options.
371 * Also set the default to the expanded value, so ":set" does not list
372 * them.
373 * Don't set the P_ALLOCED flag, because we don't want to free the
374 * default.
375 */
376 static void
377set_init_expand_env(void)
378{
379 int opt_idx;
380 char_u *p;
381
382 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
383 {
384 if ((options[opt_idx].flags & P_GETTEXT)
385 && options[opt_idx].var != NULL)
386 p = (char_u *)_(*(char **)options[opt_idx].var);
387 else
388 p = option_expand(opt_idx, NULL);
389 if (p != NULL && (p = vim_strsave(p)) != NULL)
390 {
391 *(char_u **)options[opt_idx].var = p;
392 // VIMEXP
393 // Defaults for all expanded options are currently the same for Vi
394 // and Vim. When this changes, add some code here! Also need to
395 // split P_DEF_ALLOCED in two.
396 if (options[opt_idx].flags & P_DEF_ALLOCED)
397 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
398 options[opt_idx].def_val[VI_DEFAULT] = p;
399 options[opt_idx].flags |= P_DEF_ALLOCED;
400 }
401 }
402}
403
404/*
405 * Initialize the 'LANG' environment variable to a default value.
406 */
407 static void
408set_init_lang_env(void)
409{
410#if defined(MSWIN) && defined(FEAT_GETTEXT)
411 // If $LANG isn't set, try to get a good value for it. This makes the
412 // right language be used automatically. Don't do this for English.
413 if (mch_getenv((char_u *)"LANG") == NULL)
414 {
415 char buf[20];
416 long_u n;
417
418 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
419 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
420 // only the first two.
421 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
422 (LPTSTR)buf, 20);
423 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
424 {
425 // There are a few exceptions (probably more)
426 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
427 STRCPY(buf, "zh_TW");
428 else if (STRNICMP(buf, "chs", 3) == 0
429 || STRNICMP(buf, "zhc", 3) == 0)
430 STRCPY(buf, "zh_CN");
431 else if (STRNICMP(buf, "jp", 2) == 0)
432 STRCPY(buf, "ja");
433 else
434 buf[2] = NUL; // truncate to two-letter code
435 vim_setenv((char_u *)"LANG", (char_u *)buf);
436 }
437 }
438#elif defined(MACOS_CONVERT)
439 // Moved to os_mac_conv.c to avoid dependency problems.
440 mac_lang_init();
441#endif
442}
443
444/*
445 * Initialize the 'encoding' option to a default value.
446 */
447 static void
448set_init_default_encoding(void)
449{
450 char_u *p;
451 int opt_idx;
452
453# ifdef MSWIN
454 // MS-Windows has builtin support for conversion to and from Unicode, using
455 // "utf-8" for 'encoding' should work best for most users.
456 p = vim_strsave((char_u *)ENC_DFLT);
457# else
458 // enc_locale() will try to find the encoding of the current locale.
459 // This works best for properly configured systems, old and new.
460 p = enc_locale();
461# endif
462 if (p == NULL)
463 return;
464
465 // Try setting 'encoding' and check if the value is valid.
466 // If not, go back to the default encoding.
467 char_u *save_enc = p_enc;
468 p_enc = p;
469 if (STRCMP(p_enc, "gb18030") == 0)
470 {
471 // We don't support "gb18030", but "cp936" is a good substitute
472 // for practical purposes, thus use that. It's not an alias to
473 // still support conversion between gb18030 and utf-8.
474 p_enc = vim_strsave((char_u *)"cp936");
475 vim_free(p);
476 }
477 if (mb_init() == NULL)
478 {
479 opt_idx = findoption((char_u *)"encoding");
480 if (opt_idx >= 0)
481 {
482 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
483 options[opt_idx].flags |= P_DEF_ALLOCED;
484 }
485
486#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
487 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
488 {
489 // Adjust the default for 'isprint' and 'iskeyword' to match
490 // latin1. Also set the defaults for when 'nocompatible' is
491 // set.
492 set_string_option_direct((char_u *)"isp", -1,
493 ISP_LATIN1, OPT_FREE, SID_NONE);
494 set_string_option_direct((char_u *)"isk", -1,
495 ISK_LATIN1, OPT_FREE, SID_NONE);
496 opt_idx = findoption((char_u *)"isp");
497 if (opt_idx >= 0)
498 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
499 opt_idx = findoption((char_u *)"isk");
500 if (opt_idx >= 0)
501 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
502 (void)init_chartab();
503 }
504#endif
505
506#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
507 // Win32 console: When GetACP() returns a different value from
508 // GetConsoleCP() set 'termencoding'.
509 if (
510# ifdef VIMDLL
511 (!gui.in_use && !gui.starting) &&
512# endif
513 GetACP() != GetConsoleCP())
514 {
515 char buf[50];
516
517 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
518 // Use an alternative value.
519 if (GetConsoleCP() == 0)
520 sprintf(buf, "cp%ld", (long)GetACP());
521 else
522 sprintf(buf, "cp%ld", (long)GetConsoleCP());
523 p_tenc = vim_strsave((char_u *)buf);
524 if (p_tenc != NULL)
525 {
526 opt_idx = findoption((char_u *)"termencoding");
527 if (opt_idx >= 0)
528 {
529 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
530 options[opt_idx].flags |= P_DEF_ALLOCED;
531 }
532 convert_setup(&input_conv, p_tenc, p_enc);
533 convert_setup(&output_conv, p_enc, p_tenc);
534 }
535 else
536 p_tenc = empty_option;
537 }
538#endif
539#if defined(MSWIN)
540 // $HOME may have characters in active code page.
541 init_homedir();
542#endif
543 }
544 else
545 {
546 vim_free(p_enc);
547 p_enc = save_enc;
548 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000549}
550
551/*
552 * Initialize the options, first part.
553 *
554 * Called only once from main(), just after creating the first buffer.
555 * If "clean_arg" is TRUE Vim was started with --clean.
556 */
557 void
558set_init_1(int clean_arg)
559{
560#ifdef FEAT_LANGMAP
561 langmap_init();
562#endif
563
564 // Be Vi compatible by default
565 p_cp = TRUE;
566
567 // Use POSIX compatibility when $VIM_POSIX is set.
568 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
569 {
570 set_string_default("cpo", (char_u *)CPO_ALL);
571 set_string_default("shm", (char_u *)SHM_POSIX);
572 }
573
574 set_init_default_shell();
575 set_init_default_backupskip();
576 set_init_default_maxmemtot();
577 set_init_default_cdpath();
578 set_init_default_printencoding();
579#ifdef FEAT_POSTSCRIPT
580 set_init_default_printexpr();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581#endif
582
583 /*
584 * Set all the options (except the terminal options) to their default
585 * value. Also set the global value for local options.
586 */
587 set_options_default(0);
588
matveytadbb1bf2022-02-01 17:26:12 +0000589#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000590 set_init_restricted_mode();
matveytadbb1bf2022-02-01 17:26:12 +0000591#endif
592
Bram Moolenaar07268702018-03-01 21:57:32 +0100593#ifdef CLEAN_RUNTIMEPATH
594 if (clean_arg)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000595 set_init_clean_rtp();
Bram Moolenaar07268702018-03-01 21:57:32 +0100596#endif
597
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598#ifdef FEAT_GUI
599 if (found_reverse_arg)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100600 set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601#endif
602
603 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100604 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100605 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 check_buf_options(curbuf);
607 check_win_options(curwin);
608 check_options();
609
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100610 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 didset_options();
612
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000613#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100614 // Use the current chartab for the generic chartab. This is not in
615 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000616 init_spell_chartab();
617#endif
618
K.Takatace3189d2023-02-15 19:13:43 +0000619 set_init_default_encoding();
620
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000621 // Expand environment variables and things like "~" for the defaults.
622 set_init_expand_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100624 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100627 // Detect use of mlterm.
628 // Mlterm is a terminal emulator akin to xterm that has some special
629 // abilities (bidi namely).
630 // NOTE: mlterm's author is being asked to 'set' a variable
631 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 if (mch_getenv((char_u *)"MLTERM") != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100633 set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634#endif
635
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200636 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000638 set_init_lang_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
640#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100641 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 set_helplang_default(get_mess_lang());
643#endif
644}
645
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200646static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
647
648/*
649 * Set the "fileencodings" option to the default value for when 'encoding' is
650 * utf-8.
651 */
652 void
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +0000653set_fencs_unicode(void)
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200654{
655 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
656 OPT_FREE, 0);
657}
658
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659/*
660 * Set an option to its default value.
661 * This does not take care of side effects!
662 */
663 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100664set_option_default(
665 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100666 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
667 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100669 char_u *varp; // pointer to variable for current option
670 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000672 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
674
675 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
676 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100677 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 {
679 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
680 if (flags & P_STRING)
681 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200682 // 'fencs' default value depends on 'encoding'
683 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
684 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100685 // Use set_string_option_direct() for local options to handle
686 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200687 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200688 set_string_option_direct(NULL, opt_idx,
689 options[opt_idx].def_val[dvi], opt_flags, 0);
690 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200692 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
693 free_string_option(*(char_u **)(varp));
694 *(char_u **)varp = options[opt_idx].def_val[dvi];
695 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 }
697 }
698 else if (flags & P_NUM)
699 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000700 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 win_comp_scroll(curwin);
702 else
703 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100704 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
705
706 if ((long *)varp == &curwin->w_p_so
707 || (long *)varp == &curwin->w_p_siso)
708 // 'scrolloff' and 'sidescrolloff' local values have a
709 // different default value than the global default.
710 *(long *)varp = -1;
711 else
712 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100713 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 if (both)
715 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100716 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 }
718 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100719 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100721 // the cast to long is required for Manx C, long_i is needed for
722 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000723 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000724#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100725 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000726 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
727 *(int *)varp = FALSE;
728#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100729 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 if (both)
731 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
732 *(int *)varp;
733 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000734
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100735 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000736 flagsp = insecure_flag(opt_idx, opt_flags);
737 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 }
739
740#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200741 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742#endif
743}
744
745/*
746 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200747 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 */
749 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100750set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100751 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752{
753 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000755 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756
Bram Moolenaardac13472019-09-16 21:06:21 +0200757 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200758 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200759 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100760 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200761# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200762 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200763 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200764# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100765 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 set_option_default(i, opt_flags, p_cp);
767
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100768 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000769 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200771 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772}
773
774/*
775 * Set the Vi-default value of a string option.
776 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100777 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100779 static void
780set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781{
782 char_u *p;
783 int opt_idx;
784
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100785 if (escape && vim_strchr(val, ' ') != NULL)
786 p = vim_strsave_escaped(val, (char_u *)" ");
787 else
788 p = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000789 if (p == NULL) // we don't want a NULL
790 return;
791
792 opt_idx = findoption((char_u *)name);
793 if (opt_idx < 0)
794 return;
795
796 if (options[opt_idx].flags & P_DEF_ALLOCED)
797 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
798 options[opt_idx].def_val[VI_DEFAULT] = p;
799 options[opt_idx].flags |= P_DEF_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800}
801
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100802 void
803set_string_default(char *name, char_u *val)
804{
805 set_string_default_esc(name, val, FALSE);
806}
807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200809 * For an option value that contains comma separated items, find "newval" in
810 * "origval". Return NULL if not found.
811 */
812 static char_u *
813find_dup_item(char_u *origval, char_u *newval, long_u flags)
814{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200815 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200816 size_t newlen;
817 char_u *s;
818
819 if (origval == NULL)
820 return NULL;
821
822 newlen = STRLEN(newval);
823 for (s = origval; *s != NUL; ++s)
824 {
825 if ((!(flags & P_COMMA)
826 || s == origval
827 || (s[-1] == ',' && !(bs & 1)))
828 && STRNCMP(s, newval, newlen) == 0
829 && (!(flags & P_COMMA)
830 || s[newlen] == ','
831 || s[newlen] == NUL))
832 return s;
833 // Count backslashes. Only a comma with an even number of backslashes
834 // or a single backslash preceded by a comma before it is recognized as
835 // a separator.
836 if ((s > origval + 1
837 && s[-1] == '\\'
838 && s[-2] != ',')
839 || (s == origval + 1
840 && s[-1] == '\\'))
841 ++bs;
842 else
843 bs = 0;
844 }
845 return NULL;
846}
847
848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 * Set the Vi-default value of a number option.
850 * Used for 'lines' and 'columns'.
851 */
852 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100853set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000855 int opt_idx;
856
857 opt_idx = findoption((char_u *)name);
858 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000859 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860}
861
Dominique Pelle748b3082022-01-08 12:41:16 +0000862#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200863/*
864 * Set all window-local and buffer-local options to the Vim default.
865 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200866 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200867 */
868 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200869set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200870{
871 win_T *save_curwin = curwin;
872 int i;
873
874 curwin = wp;
875 curbuf = curwin->w_buffer;
876 block_autocmds();
877
Bram Moolenaardac13472019-09-16 21:06:21 +0200878 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200879 {
880 struct vimoption *p = &(options[i]);
881 char_u *varp = get_varp_scope(p, OPT_LOCAL);
882
883 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200884 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200885 && !(options[i].flags & P_NODEFAULT)
886 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200887 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200888 }
889
890 unblock_autocmds();
891 curwin = save_curwin;
892 curbuf = curwin->w_buffer;
893}
Dominique Pelle748b3082022-01-08 12:41:16 +0000894#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200895
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000896#if defined(EXITFREE) || defined(PROTO)
897/*
898 * Free all options.
899 */
900 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100901free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000902{
903 int i;
904
Bram Moolenaardac13472019-09-16 21:06:21 +0200905 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000906 {
907 if (options[i].indir == PV_NONE)
908 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100909 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100910 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000911 free_string_option(*(char_u **)options[i].var);
912 if (options[i].flags & P_DEF_ALLOCED)
913 free_string_option(options[i].def_val[VI_DEFAULT]);
914 }
915 else if (options[i].var != VAR_WIN
916 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100917 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200918 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000919 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000920 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000921 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000922}
923#endif
924
925
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926/*
927 * Initialize the options, part two: After getting Rows and Columns and
928 * setting 'term'.
929 */
930 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100931set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000933 int idx;
934
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000935 // 'scroll' defaults to half the window height. The stored default is zero,
936 // which results in the actual value computed from the window height.
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000937 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000938 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000939 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 comp_col();
941
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000942 // 'window' is only for backwards compatibility with Vi.
943 // Default is Rows - 1.
Bram Moolenaard68071d2006-05-02 22:08:30 +0000944 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000945 p_window = Rows - 1;
946 set_number_default("window", Rows - 1);
947
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100948 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100949#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000950 // If 'background' wasn't set by the user, try guessing the value,
951 // depending on the terminal name. Only need to check for terminals
952 // with a dark background, that can handle color.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000953 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000954 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
955 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000957 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100958 // don't mark it as set, when starting the GUI it may be
959 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000960 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 }
962#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000963
964#ifdef CURSOR_SHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000965 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000966#endif
967#ifdef FEAT_MOUSESHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000968 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000969#endif
970#ifdef FEAT_PRINTER
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000971 (void)parse_printoptions(NULL); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/*
976 * Initialize the options, part three: After reading the .vimrc
977 */
978 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100979set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100981#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982/*
983 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
984 * This is done after other initializations, where 'shell' might have been
985 * set, but only if they have not been set before.
986 */
987 char_u *p;
988 int idx_srr;
989 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100990# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 int idx_sp;
992 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100993# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994
995 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000996 if (idx_srr < 0)
997 do_srr = FALSE;
998 else
999 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001000# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001002 if (idx_sp < 0)
1003 do_sp = FALSE;
1004 else
1005 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001006# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001007 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 if (p != NULL)
1009 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001010 // Default for p_sp is "| tee", for p_srr is ">".
1011 // For known shells it is changed here to include stderr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 if ( fnamecmp(p, "csh") == 0
1013 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001014# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 || fnamecmp(p, "csh.exe") == 0
1016 || fnamecmp(p, "tcsh.exe") == 0
1017# endif
1018 )
1019 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001020# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 if (do_sp)
1022 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001023# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001025# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001027# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1029 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001030# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 if (do_srr)
1032 {
1033 p_srr = (char_u *)">&";
1034 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1035 }
1036 }
Mike Williams12795022021-06-28 20:53:58 +02001037# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +02001038 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
1039 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +02001040 else if ( fnamecmp(p, "powershell") == 0
1041 || fnamecmp(p, "powershell.exe") == 0
1042 )
1043 {
1044# if defined(FEAT_QUICKFIX)
1045 if (do_sp)
1046 {
1047 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
1048 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1049 }
1050# endif
1051 if (do_srr)
1052 {
1053 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
1054 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1055 }
1056 }
1057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 else
Natanael Copa56318362021-05-06 18:46:35 +02001059 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 if ( fnamecmp(p, "sh") == 0
1061 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001062 || fnamecmp(p, "mksh") == 0
1063 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001065 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001067 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001068 || fnamecmp(p, "ash") == 0
1069 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001070 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001071# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 || fnamecmp(p, "cmd") == 0
1073 || fnamecmp(p, "sh.exe") == 0
1074 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001075 || fnamecmp(p, "mksh.exe") == 0
1076 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001078 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 || fnamecmp(p, "bash.exe") == 0
1080 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001081 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001082 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001084 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001086# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 if (do_sp)
1088 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001089# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001091# else
Mike Williamsa3d1b292021-06-30 20:56:00 +02001092 if (fnamecmp(p, "pwsh") == 0)
1093 p_sp = (char_u *)">%s 2>&1";
1094 else
1095 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001096# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1098 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001099# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 if (do_srr)
1101 {
1102 p_srr = (char_u *)">%s 2>&1";
1103 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1104 }
1105 }
1106 vim_free(p);
1107 }
1108#endif
1109
Bram Moolenaar4f974752019-02-17 17:44:42 +01001110#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001112 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1113 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001115 * set, but only if they have not been set before.
1116 * Default values depend on shell (cmd.exe is default shell):
1117 *
1118 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001119 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001120 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001121 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001122 * "sh" like shells - "-c" "\""
1123 *
1124 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 */
Mike Williams12795022021-06-28 20:53:58 +02001126 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1127 {
1128 int idx_opt;
1129
1130 idx_opt = findoption((char_u *)"shcf");
1131 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1132 {
1133 p_shcf = (char_u*)"-Command";
1134 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1135 }
1136
1137 idx_opt = findoption((char_u *)"sxq");
1138 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1139 {
1140 p_sxq = (char_u*)"\"";
1141 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1142 }
1143 }
1144 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 {
1146 int idx3;
1147
1148 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001149 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 {
1151 p_shcf = (char_u *)"-c";
1152 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1153 }
1154
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001155 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001157 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 {
1159 p_sxq = (char_u *)"\"";
1160 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001163 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1164 {
1165 int idx3;
1166
1167 /*
1168 * cmd.exe on Windows will strip the first and last double quote given
1169 * on the command line, e.g. most of the time things like:
1170 * cmd /c "my path/to/echo" "my args to echo"
1171 * become:
1172 * my path/to/echo" "my args to echo
1173 * when executed.
1174 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001175 * To avoid this, set shellxquote to surround the command in
1176 * parenthesis. This appears to make most commands work, without
1177 * breaking commands that worked previously, such as
1178 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001179 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001180 idx3 = findoption((char_u *)"sxq");
1181 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1182 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001183 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001184 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1185 }
1186
Bram Moolenaara64ba222012-02-12 23:23:31 +01001187 idx3 = findoption((char_u *)"shcf");
1188 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1189 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001190 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001191 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1192 }
1193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194#endif
1195
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001196 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001197 {
1198 int idx_ffs = findoption((char_u *)"ffs");
1199
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001200 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001201 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1202 set_fileformat(default_fileformat(), OPT_LOCAL);
1203 }
1204
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206}
1207
1208#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1209/*
1210 * When 'helplang' is still at its default value, set it to "lang".
1211 * Only the first two characters of "lang" are used.
1212 */
1213 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001214set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215{
1216 int idx;
1217
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001218 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 return;
1220 idx = findoption((char_u *)"hlg");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001221 if (idx < 0 || (options[idx].flags & P_WAS_SET))
1222 return;
1223
1224 if (options[idx].flags & P_ALLOCED)
1225 free_string_option(p_hlg);
1226 p_hlg = vim_strsave(lang);
1227 if (p_hlg == NULL)
1228 p_hlg = empty_option;
1229 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001231 // zh_CN becomes "cn", zh_TW becomes "tw"
1232 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001233 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001234 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1235 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001236 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001237 // any C like setting, such as C.UTF-8, becomes "en"
1238 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1239 {
1240 p_hlg[0] = 'e';
1241 p_hlg[1] = 'n';
1242 }
1243 p_hlg[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001245 options[idx].flags |= P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246}
1247#endif
1248
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249/*
1250 * 'title' and 'icon' only default to true if they have not been set or reset
1251 * in .vimrc and we can read the old value.
1252 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1253 * they can be reset. This reduces startup time when using X on a remote
1254 * machine.
1255 */
1256 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001257set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258{
1259 int idx1;
1260 long val;
1261
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001262 // If GUI is (going to be) used, we can always set the window title and
1263 // icon name. Saves a bit of time, because the X11 display server does
1264 // not need to be contacted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001266 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 {
1268#ifdef FEAT_GUI
1269 if (gui.starting || gui.in_use)
1270 val = TRUE;
1271 else
1272#endif
1273 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001274 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 p_title = val;
1276 }
1277 idx1 = findoption((char_u *)"icon");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001278 if (idx1 < 0 || (options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001280 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001282
1283#ifdef FEAT_GUI
1284 if (gui.starting || gui.in_use)
1285 val = TRUE;
1286 else
1287#endif
1288 val = mch_can_restore_icon();
1289 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
1290 p_icon = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001293 void
1294ex_set(exarg_T *eap)
1295{
1296 int flags = 0;
1297
1298 if (eap->cmdidx == CMD_setlocal)
1299 flags = OPT_LOCAL;
1300 else if (eap->cmdidx == CMD_setglobal)
1301 flags = OPT_GLOBAL;
1302#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001303 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001304 ex_options(eap);
1305 else
1306#endif
1307 {
1308 if (eap->forceit)
1309 flags |= OPT_ONECOLUMN;
1310 (void)do_set(eap->arg, flags);
1311 }
1312}
1313
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001314/*
1315 * :set operator types
1316 */
Bram Moolenaar47403942022-09-21 21:12:53 +01001317typedef enum {
1318 OP_NONE = 0,
1319 OP_ADDING, // "opt+=arg"
1320 OP_PREPENDING, // "opt^=arg"
1321 OP_REMOVING, // "opt-=arg"
1322} set_op_T;
1323
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001324typedef enum {
1325 PREFIX_NO = 0, // "no" prefix
1326 PREFIX_NONE, // no prefix
1327 PREFIX_INV, // "inv" prefix
1328} set_prefix_T;
1329
1330/*
1331 * Return the prefix type for the option name in *argp.
1332 */
1333 static set_prefix_T
1334get_option_prefix(char_u **argp)
1335{
1336 int prefix = PREFIX_NONE;
1337 char_u *arg = *argp;
1338
1339 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
1340 {
1341 prefix = PREFIX_NO;
1342 arg += 2;
1343 }
1344 else if (STRNCMP(arg, "inv", 3) == 0)
1345 {
1346 prefix = PREFIX_INV;
1347 arg += 3;
1348 }
1349
1350 *argp = arg;
1351 return prefix;
1352}
1353
1354/*
1355 * Parse the option name in "arg" and return the option index in "*opt_idxp",
1356 * and the option name length in "*lenp". For a <t_xx> option, return the key
1357 * number in "*keyp".
1358 *
1359 * Returns FAIL if an option starting with "<" doesn't end with a ">",
1360 * otherwise returns OK.
1361 */
1362 static int
1363parse_option_name(char_u *arg, int *opt_idxp, int *lenp, int *keyp)
1364{
1365 int key = 0;
1366 int len;
1367 int opt_idx;
1368
1369 if (*arg == '<')
1370 {
1371 opt_idx = -1;
1372 // look out for <t_>;>
1373 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1374 len = 5;
1375 else
1376 {
1377 len = 1;
1378 while (arg[len] != NUL && arg[len] != '>')
1379 ++len;
1380 }
1381 if (arg[len] != '>')
1382 return FAIL;
1383
1384 arg[len] = NUL; // put NUL after name
1385 if (arg[1] == 't' && arg[2] == '_') // could be term code
1386 opt_idx = findoption(arg + 1);
1387 arg[len++] = '>'; // restore '>'
1388 if (opt_idx == -1)
1389 key = find_key_option(arg + 1, TRUE);
1390 }
1391 else
1392 {
1393 int nextchar; // next non-white char after option name
1394
1395 len = 0;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001396 // The two characters after "t_" may not be alphanumeric.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001397 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1398 len = 4;
1399 else
1400 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1401 ++len;
1402 nextchar = arg[len];
1403 arg[len] = NUL; // put NUL after name
1404 opt_idx = findoption(arg);
1405 arg[len] = nextchar; // restore nextchar
1406 if (opt_idx == -1)
1407 key = find_key_option(arg, FALSE);
1408 }
1409
1410 *keyp = key;
1411 *lenp = len;
1412 *opt_idxp = opt_idx;
1413
1414 return OK;
1415}
1416
1417/*
1418 * Get the option operator (+=, ^=, -=).
1419 */
1420 static set_op_T
1421get_opt_op(char_u *arg)
1422{
1423 set_op_T op = OP_NONE;
1424
1425 if (*arg != NUL && *(arg + 1) == '=')
1426 {
1427 if (*arg == '+')
1428 op = OP_ADDING; // "+="
1429 else if (*arg == '^')
1430 op = OP_PREPENDING; // "^="
1431 else if (*arg == '-')
1432 op = OP_REMOVING; // "-="
1433 }
1434
1435 return op;
1436}
1437
1438/*
1439 * Validate whether the value of the option in "opt_idx" can be changed.
1440 * Returns FAIL if the option can be skipped or cannot be changed. Returns OK
1441 * if it can be changed.
1442 */
1443 static int
1444validate_opt_idx(int opt_idx, int opt_flags, long_u flags, char **errmsg)
1445{
1446 // Skip all options that are not window-local (used when showing
1447 // an already loaded buffer in a window).
1448 if ((opt_flags & OPT_WINONLY)
1449 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1450 return FAIL;
1451
1452 // Skip all options that are window-local (used for :vimgrep).
1453 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1454 && options[opt_idx].var == VAR_WIN)
1455 return FAIL;
1456
1457 // Disallow changing some options from modelines.
1458 if (opt_flags & OPT_MODELINE)
1459 {
1460 if (flags & (P_SECURE | P_NO_ML))
1461 {
1462 *errmsg = e_not_allowed_in_modeline;
1463 return FAIL;
1464 }
1465 if ((flags & P_MLE) && !p_mle)
1466 {
1467 *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
1468 return FAIL;
1469 }
1470#ifdef FEAT_DIFF
1471 // In diff mode some options are overruled. This avoids that
1472 // 'foldmethod' becomes "marker" instead of "diff" and that
1473 // "wrap" gets set.
1474 if (curwin->w_p_diff
1475 && opt_idx >= 0 // shut up coverity warning
1476 && (
1477# ifdef FEAT_FOLDING
1478 options[opt_idx].indir == PV_FDM ||
1479# endif
1480 options[opt_idx].indir == PV_WRAP))
1481 return FAIL;
1482#endif
1483 }
1484
1485#ifdef HAVE_SANDBOX
1486 // Disallow changing some options in the sandbox
1487 if (sandbox != 0 && (flags & P_SECURE))
1488 {
1489 *errmsg = e_not_allowed_in_sandbox;
1490 return FAIL;
1491 }
1492#endif
1493
1494 return OK;
1495}
1496
Bram Moolenaar47403942022-09-21 21:12:53 +01001497/*
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001498 * Get the Vim/Vi default value for a string option.
1499 */
1500 static char_u *
1501stropt_get_default_val(
1502 int opt_idx,
1503 char_u *varp,
1504 int flags,
1505 int cp_val)
1506{
1507 char_u *newval;
1508
1509 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1510 ? VI_DEFAULT : VIM_DEFAULT];
1511 if ((char_u **)varp == &p_bg)
1512 {
1513 // guess the value of 'background'
1514#ifdef FEAT_GUI
1515 if (gui.in_use)
1516 newval = gui_bg_default();
1517 else
1518#endif
1519 newval = term_bg_default();
1520 }
1521 else if ((char_u **)varp == &p_fencs && enc_utf8)
1522 newval = fencs_utf8_default;
1523
1524 // expand environment variables and ~ since the default value was
1525 // already expanded, only required when an environment variable was set
1526 // later
1527 if (newval == NULL)
1528 newval = empty_option;
1529 else
1530 {
1531 char_u *s = option_expand(opt_idx, newval);
1532 if (s == NULL)
1533 s = newval;
1534 newval = vim_strsave(s);
1535 }
1536
1537 return newval;
1538}
1539
1540/*
1541 * Convert the 'backspace' option number value to a string: for adding,
1542 * prepending and removing string.
1543 */
1544 static void
1545opt_backspace_nr2str(
1546 char_u *varp,
1547 char_u **origval_p,
1548 char_u **origval_l_p,
1549 char_u **origval_g_p,
1550 char_u **oldval_p)
1551{
1552 int i = getdigits((char_u **)varp);
1553
1554 switch (i)
1555 {
1556 case 0:
1557 *(char_u **)varp = empty_option;
1558 break;
1559 case 1:
1560 *(char_u **)varp = vim_strsave((char_u *)"indent,eol");
1561 break;
1562 case 2:
1563 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,start");
1564 break;
1565 case 3:
1566 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,nostop");
1567 break;
1568 }
1569 vim_free(*oldval_p);
1570 if (*origval_p == *oldval_p)
1571 *origval_p = *(char_u **)varp;
1572 if (*origval_l_p == *oldval_p)
1573 *origval_l_p = *(char_u **)varp;
1574 if (*origval_g_p == *oldval_p)
1575 *origval_g_p = *(char_u **)varp;
1576 *oldval_p = *(char_u **)varp;
1577}
1578
1579/*
1580 * Convert the 'whichwrap' option number value to a string, for backwards
1581 * compatibility with Vim 3.0.
1582 * Note: 'argp' is a pointer to a char_u pointer and is updated.
1583 */
1584 static char_u *
1585opt_whichwrap_nr2str(char_u **argp, char_u *whichwrap)
1586{
1587 *whichwrap = NUL;
1588 int i = getdigits(argp);
1589 if (i & 1)
1590 STRCAT(whichwrap, "b,");
1591 if (i & 2)
1592 STRCAT(whichwrap, "s,");
1593 if (i & 4)
1594 STRCAT(whichwrap, "h,l,");
1595 if (i & 8)
1596 STRCAT(whichwrap, "<,>,");
1597 if (i & 16)
1598 STRCAT(whichwrap, "[,],");
1599 if (*whichwrap != NUL) // remove trailing ,
1600 whichwrap[STRLEN(whichwrap) - 1] = NUL;
1601
1602 return whichwrap;
1603}
1604
1605/*
1606 * Copy the new string value into allocated memory for the option.
1607 * Can't use set_string_option_direct(), because we need to remove the
1608 * backslashes.
1609 */
1610 static char_u *
1611stropt_copy_value(
1612 char_u *origval,
1613 char_u **argp,
1614 set_op_T op,
1615 int flags UNUSED)
1616{
1617 char_u *arg = *argp;
1618 unsigned newlen;
1619 char_u *newval;
1620 char_u *s = NULL;
1621
1622 // get a bit too much
1623 newlen = (unsigned)STRLEN(arg) + 1;
1624 if (op != OP_NONE)
1625 newlen += (unsigned)STRLEN(origval) + 1;
1626 newval = alloc(newlen);
1627 if (newval == NULL) // out of mem, don't change
1628 return NULL;
1629 s = newval;
1630
1631 // Copy the string, skip over escaped chars.
1632 // For MS-DOS and WIN32 backslashes before normal file name characters
1633 // are not removed, and keep backslash at start, for "\\machine\path",
1634 // but do remove it for "\\\\machine\\path".
1635 // The reverse is found in ExpandOldSetting().
1636 while (*arg != NUL && !VIM_ISWHITE(*arg))
1637 {
1638 int i;
1639
1640 if (*arg == '\\' && arg[1] != NUL
1641#ifdef BACKSLASH_IN_FILENAME
1642 && !((flags & P_EXPAND)
1643 && vim_isfilec(arg[1])
1644 && !VIM_ISWHITE(arg[1])
1645 && (arg[1] != '\\'
1646 || (s == newval && arg[2] != '\\')))
1647#endif
1648 )
1649 ++arg; // remove backslash
1650 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
1651 {
1652 // copy multibyte char
1653 mch_memmove(s, arg, (size_t)i);
1654 arg += i;
1655 s += i;
1656 }
1657 else
1658 *s++ = *arg++;
1659 }
1660 *s = NUL;
1661
1662 *argp = arg;
1663 return newval;
1664}
1665
1666/*
1667 * Expand environment variables and ~ in string option value 'newval'.
1668 */
1669 static char_u *
1670stropt_expand_envvar(
1671 int opt_idx,
1672 char_u *origval,
1673 char_u *newval,
1674 set_op_T op)
1675{
1676 char_u *s = option_expand(opt_idx, newval);
1677 if (s == NULL)
1678 return newval;
1679
1680 vim_free(newval);
1681 unsigned newlen = (unsigned)STRLEN(s) + 1;
1682 if (op != OP_NONE)
1683 newlen += (unsigned)STRLEN(origval) + 1;
1684
1685 newval = alloc(newlen);
1686 if (newval == NULL)
1687 return NULL;
1688
1689 STRCPY(newval, s);
1690
1691 return newval;
1692}
1693
1694/*
1695 * Concatenate the original and new values of a string option, adding a "," if
1696 * needed.
1697 */
1698 static void
1699stropt_concat_with_comma(
1700 char_u *origval,
1701 char_u *newval,
1702 set_op_T op,
1703 int flags)
1704{
1705 int len = 0;
1706
1707 int comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1708 if (op == OP_ADDING)
1709 {
1710 len = (int)STRLEN(origval);
1711 // strip a trailing comma, would get 2
1712 if (comma && len > 1
1713 && (flags & P_ONECOMMA) == P_ONECOMMA
1714 && origval[len - 1] == ','
1715 && origval[len - 2] != '\\')
1716 len--;
1717 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1718 mch_memmove(newval, origval, (size_t)len);
1719 }
1720 else
1721 {
1722 len = (int)STRLEN(newval);
1723 STRMOVE(newval + len + comma, origval);
1724 }
1725 if (comma)
1726 newval[len] = ',';
1727}
1728
1729/*
1730 * Remove a value from a string option. Copy string option value in "origval"
1731 * to "newval" and then remove the string "strval" of length "len".
1732 */
1733 static void
1734stropt_remove_val(
1735 char_u *origval,
1736 char_u *newval,
1737 int flags,
1738 char_u *strval,
1739 int len)
1740{
1741 // Remove newval[] from origval[]. (Note: "len" has been set above
1742 // and is used here).
1743 STRCPY(newval, origval);
1744 if (*strval)
1745 {
1746 // may need to remove a comma
1747 if (flags & P_COMMA)
1748 {
1749 if (strval == origval)
1750 {
1751 // include comma after string
1752 if (strval[len] == ',')
1753 ++len;
1754 }
1755 else
1756 {
1757 // include comma before string
1758 --strval;
1759 ++len;
1760 }
1761 }
1762 STRMOVE(newval + (strval - origval), strval + len);
1763 }
1764}
1765
1766/*
1767 * Remove flags that appear twice in the string option value 'newval'.
1768 */
1769 static void
1770stropt_remove_dupflags(char_u *newval, int flags)
1771{
1772 char_u *s = newval;
1773
1774 // Remove flags that appear twice.
1775 while (*s)
1776 {
1777 // if options have P_FLAGLIST and P_ONECOMMA such as 'whichwrap'
1778 if (flags & P_ONECOMMA)
1779 {
1780 if (*s != ',' && *(s + 1) == ',' && vim_strchr(s + 2, *s) != NULL)
1781 {
1782 // Remove the duplicated value and the next comma.
1783 STRMOVE(s, s + 2);
1784 continue;
1785 }
1786 }
1787 else
1788 {
1789 if ((!(flags & P_COMMA) || *s != ',')
1790 && vim_strchr(s + 1, *s) != NULL)
1791 {
1792 STRMOVE(s, s + 1);
1793 continue;
1794 }
1795 }
1796 ++s;
1797 }
1798}
1799
1800/*
1801 * Get the string value specified for a ":set" command. The following set
1802 * options are supported:
1803 * set {opt}&
1804 * set {opt}<
1805 * set {opt}={val}
1806 * set {opt}:{val}
1807 */
1808 static char_u *
1809stropt_get_newval(
1810 int nextchar,
1811 int opt_idx,
1812 char_u **argp,
1813 char_u *varp,
1814 char_u **origval_arg,
1815 char_u **origval_l_arg,
1816 char_u **origval_g_arg,
1817 char_u **oldval_arg,
1818 set_op_T *op_arg,
1819 int flags,
1820 int cp_val)
1821{
1822 char_u *arg = *argp;
1823 char_u *origval = *origval_arg;
1824 char_u *origval_l = *origval_l_arg;
1825 char_u *origval_g = *origval_g_arg;
1826 char_u *oldval = *oldval_arg;
1827 set_op_T op = *op_arg;
1828 char_u *save_arg = NULL;
1829 char_u *newval;
1830 char_u *s = NULL;
1831 char_u whichwrap[80];
1832
1833 if (nextchar == '&') // set to default val
1834 newval = stropt_get_default_val(opt_idx, varp, flags, cp_val);
1835 else if (nextchar == '<') // set to global val
1836 newval = vim_strsave(*(char_u **)get_varp_scope(
1837 &(options[opt_idx]), OPT_GLOBAL));
1838 else
1839 {
1840 ++arg; // jump to after the '=' or ':'
1841
1842 // Set 'keywordprg' to ":help" if an empty
1843 // value was passed to :set by the user.
1844 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
1845 {
1846 save_arg = arg;
1847 arg = (char_u *)":help";
1848 }
1849 // Convert 'backspace' number to string
1850 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1851 opt_backspace_nr2str(varp, &origval, &origval_l, &origval_g,
1852 &oldval);
1853 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
1854 {
1855 // Convert 'whichwrap' number to string, for backwards
1856 // compatibility with Vim 3.0.
1857 char_u *t = opt_whichwrap_nr2str(&arg, whichwrap);
1858 save_arg = arg;
1859 arg = t;
1860 }
1861 // Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1862 // version 3.0
1863 else if (*arg == '>' && (varp == (char_u *)&p_dir
1864 || varp == (char_u *)&p_bdir))
1865 ++arg;
1866
1867 // Copy the new string into allocated memory.
1868 newval = stropt_copy_value(origval, &arg, op, flags);
1869 if (newval == NULL)
1870 goto done;
1871
1872 // Expand environment variables and ~.
1873 // Don't do it when adding without inserting a comma.
1874 if (op == OP_NONE || (flags & P_COMMA))
1875 {
1876 newval = stropt_expand_envvar(opt_idx, origval, newval, op);
1877 if (newval == NULL)
1878 goto done;
1879 }
1880
1881 // locate newval[] in origval[] when removing it and when adding to
1882 // avoid duplicates
1883 int len = 0;
1884 if (op == OP_REMOVING || (flags & P_NODUP))
1885 {
1886 len = (int)STRLEN(newval);
1887 s = find_dup_item(origval, newval, flags);
1888
1889 // do not add if already there
1890 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
1891 {
1892 op = OP_NONE;
1893 STRCPY(newval, origval);
1894 }
1895
1896 // if no duplicate, move pointer to end of original value
1897 if (s == NULL)
1898 s = origval + (int)STRLEN(origval);
1899 }
1900
1901 // concatenate the two strings; add a ',' if needed
1902 if (op == OP_ADDING || op == OP_PREPENDING)
1903 stropt_concat_with_comma(origval, newval, op, flags);
1904 else if (op == OP_REMOVING)
1905 // Remove newval[] from origval[]. (Note: "len" has been set above
1906 // and is used here).
1907 stropt_remove_val(origval, newval, flags, s, len);
1908
1909 if (flags & P_FLAGLIST)
1910 // Remove flags that appear twice.
1911 stropt_remove_dupflags(newval, flags);
1912 }
1913
1914done:
1915 if (save_arg != NULL)
1916 arg = save_arg; // arg was temporarily changed, restore it
1917 *argp = arg;
1918 *origval_arg = origval;
1919 *origval_l_arg = origval_l;
1920 *origval_g_arg = origval_g;
1921 *oldval_arg = oldval;
1922 *op_arg = op;
1923
1924 return newval;
1925}
1926
1927/*
Bram Moolenaar47403942022-09-21 21:12:53 +01001928 * Part of do_set() for string options.
1929 * Returns FAIL on failure, do not process further options.
1930 */
1931 static int
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001932do_set_option_string(
Bram Moolenaar47403942022-09-21 21:12:53 +01001933 int opt_idx,
1934 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01001935 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01001936 int nextchar,
1937 set_op_T op_arg,
1938 int flags,
1939 int cp_val,
1940 char_u *varp_arg,
1941 char *errbuf,
1942 int *value_checked,
1943 char **errmsg)
1944{
Bram Moolenaar6f981142022-09-22 12:48:58 +01001945 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01001946 set_op_T op = op_arg;
1947 char_u *varp = varp_arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01001948 char_u *oldval = NULL; // previous value if *varp
1949 char_u *newval;
1950 char_u *origval = NULL;
1951 char_u *origval_l = NULL;
1952 char_u *origval_g = NULL;
1953#if defined(FEAT_EVAL)
1954 char_u *saved_origval = NULL;
1955 char_u *saved_origval_l = NULL;
1956 char_u *saved_origval_g = NULL;
1957 char_u *saved_newval = NULL;
1958#endif
Bram Moolenaar47403942022-09-21 21:12:53 +01001959
1960 // When using ":set opt=val" for a global option
1961 // with a local value the local value will be
1962 // reset, use the global value here.
1963 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
1964 && ((int)options[opt_idx].indir & PV_BOTH))
1965 varp = options[opt_idx].var;
1966
1967 // The old value is kept until we are sure that the new value is valid.
1968 oldval = *(char_u **)varp;
1969
1970 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1971 {
1972 origval_l = *(char_u **)get_varp_scope(
1973 &(options[opt_idx]), OPT_LOCAL);
1974 origval_g = *(char_u **)get_varp_scope(
1975 &(options[opt_idx]), OPT_GLOBAL);
1976
1977 // A global-local string option might have an empty option as value to
1978 // indicate that the global value should be used.
1979 if (((int)options[opt_idx].indir & PV_BOTH)
1980 && origval_l == empty_option)
1981 origval_l = origval_g;
1982 }
1983
1984 // When setting the local value of a global option, the old value may be
1985 // the global value.
1986 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
1987 origval = *(char_u **)get_varp(&options[opt_idx]);
1988 else
1989 origval = oldval;
1990
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001991 // Get the new value for the option
1992 newval = stropt_get_newval(nextchar, opt_idx, &arg, varp, &origval,
1993 &origval_l, &origval_g, &oldval, &op, flags,
1994 cp_val);
Bram Moolenaar47403942022-09-21 21:12:53 +01001995
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001996 // Set the new value.
Bram Moolenaar47403942022-09-21 21:12:53 +01001997 *(char_u **)(varp) = newval;
Bram Moolenaar339e1142023-02-15 14:26:25 +00001998 if (newval == NULL)
1999 *(char_u **)(varp) = empty_option;
Bram Moolenaar47403942022-09-21 21:12:53 +01002000
2001#if defined(FEAT_EVAL)
2002 if (!starting
2003# ifdef FEAT_CRYPT
2004 && options[opt_idx].indir != PV_KEY
2005# endif
2006 && origval != NULL && newval != NULL)
2007 {
2008 // origval may be freed by did_set_string_option(), make a copy.
2009 saved_origval = vim_strsave(origval);
2010 // newval (and varp) may become invalid if the buffer is closed by
2011 // autocommands.
2012 saved_newval = vim_strsave(newval);
2013 if (origval_l != NULL)
2014 saved_origval_l = vim_strsave(origval_l);
2015 if (origval_g != NULL)
2016 saved_origval_g = vim_strsave(origval_g);
2017 }
2018#endif
2019
2020 {
2021 long_u *p = insecure_flag(opt_idx, opt_flags);
2022 int secure_saved = secure;
2023
2024 // When an option is set in the sandbox, from a modeline or in secure
2025 // mode, then deal with side effects in secure mode. Also when the
2026 // value was set with the P_INSECURE flag and is not completely
2027 // replaced.
2028 if ((opt_flags & OPT_MODELINE)
2029#ifdef HAVE_SANDBOX
2030 || sandbox != 0
2031#endif
2032 || (op != OP_NONE && (*p & P_INSECURE)))
2033 secure = 1;
2034
2035 // Handle side effects, and set the global value for ":set" on local
2036 // options. Note: when setting 'syntax' or 'filetype' autocommands may
2037 // be triggered that can cause havoc.
2038 *errmsg = did_set_string_option(
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002039 opt_idx, (char_u **)varp, oldval, newval, errbuf,
Bram Moolenaar47403942022-09-21 21:12:53 +01002040 opt_flags, value_checked);
2041
2042 secure = secure_saved;
2043 }
2044
2045#if defined(FEAT_EVAL)
2046 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00002047 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01002048 saved_origval_l, saved_origval_g, saved_newval);
2049 vim_free(saved_origval);
2050 vim_free(saved_origval_l);
2051 vim_free(saved_origval_g);
2052 vim_free(saved_newval);
2053#endif
2054
Bram Moolenaar6f981142022-09-22 12:48:58 +01002055 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002056 return *errmsg == NULL ? OK : FAIL;
2057}
2058
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002060 * Set a boolean option.
2061 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002062 */
2063 static char *
2064do_set_option_bool(
2065 int opt_idx,
2066 int opt_flags,
2067 set_prefix_T prefix,
2068 long_u flags,
2069 char_u *varp,
2070 int nextchar,
2071 int afterchar,
2072 int cp_val)
2073
2074{
2075 varnumber_T value;
2076
2077 if (nextchar == '=' || nextchar == ':')
2078 return e_invalid_argument;
Bram Moolenaar546933f2023-02-06 16:40:49 +00002079 if (opt_idx < 0 || varp == NULL)
2080 return NULL; // "cannot happen"
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002081
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002082 // ":set opt!": invert
2083 // ":set opt&": reset to default value
2084 // ":set opt<": reset to global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002085 if (nextchar == '!')
2086 value = *(int *)(varp) ^ 1;
2087 else if (nextchar == '&')
2088 value = (int)(long)(long_i)options[opt_idx].def_val[
2089 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2090 else if (nextchar == '<')
2091 {
2092 // For 'autoread' -1 means to use global value.
2093 if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL)
2094 value = -1;
2095 else
2096 value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2097 }
2098 else
2099 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002100 // ":set invopt": invert
2101 // ":set opt" or ":set noopt": set or reset
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002102 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
2103 return e_trailing_characters;
2104 if (prefix == PREFIX_INV)
2105 value = *(int *)(varp) ^ 1;
2106 else
2107 value = prefix == PREFIX_NO ? 0 : 1;
2108 }
2109
2110 return set_bool_option(opt_idx, varp, (int)value, opt_flags);
2111}
2112
2113/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002114 * Set a numeric option.
2115 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002116 */
2117 static char *
2118do_set_option_numeric(
2119 int opt_idx,
2120 int opt_flags,
2121 char_u **argp,
2122 int nextchar,
2123 set_op_T op,
2124 long_u flags,
2125 int cp_val,
2126 char_u *varp,
2127 char *errbuf,
2128 size_t errbuflen)
2129{
2130 char_u *arg = *argp;
2131 varnumber_T value;
2132 int i;
2133 char *errmsg = NULL;
2134
Bram Moolenaar546933f2023-02-06 16:40:49 +00002135 if (opt_idx < 0 || varp == NULL)
2136 return NULL; // "cannot happen"
2137 //
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002138 /*
2139 * Different ways to set a number option:
2140 * & set to default value
2141 * < set to global value
2142 * <xx> accept special key codes for 'wildchar'
2143 * c accept any non-digit for 'wildchar'
2144 * [-]0-9 set number
2145 * other error
2146 */
2147 ++arg;
2148 if (nextchar == '&')
2149 value = (long)(long_i)options[opt_idx].def_val[
2150 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2151 else if (nextchar == '<')
2152 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002153 if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL)
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002154 // for 'undolevels' NO_LOCAL_UNDOLEVEL means using the global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002155 value = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002156 else if (opt_flags == OPT_LOCAL
2157 && ((long *)varp == &curwin->w_p_siso
2158 || (long *)varp == &curwin->w_p_so))
2159 // for 'scrolloff'/'sidescrolloff' -1 means using the global value
2160 value = -1;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002161 else
2162 value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2163 }
2164 else if (((long *)varp == &p_wc || (long *)varp == &p_wcm)
2165 && (*arg == '<'
2166 || *arg == '^'
2167 || (*arg != NUL
2168 && (!arg[1] || VIM_ISWHITE(arg[1]))
2169 && !VIM_ISDIGIT(*arg))))
2170 {
2171 value = string_to_key(arg, FALSE);
2172 if (value == 0 && (long *)varp != &p_wcm)
2173 {
2174 errmsg = e_invalid_argument;
2175 goto skip;
2176 }
2177 }
2178 else if (*arg == '-' || VIM_ISDIGIT(*arg))
2179 {
2180 // Allow negative (for 'undolevels'), octal and hex numbers.
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002181 vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE, NULL);
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002182 if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i])))
2183 {
2184 errmsg = e_number_required_after_equal;
2185 goto skip;
2186 }
2187 }
2188 else
2189 {
2190 errmsg = e_number_required_after_equal;
2191 goto skip;
2192 }
2193
2194 if (op == OP_ADDING)
2195 value = *(long *)varp + value;
2196 else if (op == OP_PREPENDING)
2197 value = *(long *)varp * value;
2198 else if (op == OP_REMOVING)
2199 value = *(long *)varp - value;
2200
2201 errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen,
2202 opt_flags);
2203
2204skip:
2205 *argp = arg;
2206 return errmsg;
2207}
2208
2209/*
2210 * Set a key code (t_xx) option
2211 */
2212 static char *
2213do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar)
2214{
2215 char_u *arg = *argp;
2216 char_u *p;
2217
2218 if (nextchar == '&')
2219 {
2220 if (add_termcap_entry(key_name, TRUE) == FAIL)
2221 return e_not_found_in_termcap;
2222 }
2223 else
2224 {
2225 ++arg; // jump to after the '=' or ':'
2226 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
2227 if (*p == '\\' && p[1] != NUL)
2228 ++p;
2229 nextchar = *p;
2230 *p = NUL;
2231 add_termcode(key_name, arg, FALSE);
2232 *p = nextchar;
2233 }
2234 if (full_screen)
2235 ttest(FALSE);
2236 redraw_all_later(UPD_CLEAR);
2237
2238 *argp = arg;
2239 return NULL;
2240}
2241
2242/*
2243 * Set an option to a new value.
2244 */
2245 static char *
2246do_set_option_value(
2247 int opt_idx,
2248 int opt_flags,
2249 char_u **argp,
2250 set_prefix_T prefix,
2251 set_op_T op,
2252 long_u flags,
2253 char_u *varp,
2254 char_u *key_name,
2255 int nextchar,
2256 int afterchar,
2257 int cp_val,
2258 int *stopopteval,
2259 char *errbuf,
2260 size_t errbuflen)
2261{
2262 int value_checked = FALSE;
2263 char *errmsg = NULL;
2264 char_u *arg = *argp;
2265
2266 if (flags & P_BOOL)
2267 {
2268 // boolean option
2269 errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp,
2270 nextchar, afterchar, cp_val);
2271 if (errmsg != NULL)
2272 goto skip;
2273 }
2274 else
2275 {
2276 // numeric or string option
2277 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2278 || prefix != PREFIX_NONE)
2279 {
2280 errmsg = e_invalid_argument;
2281 goto skip;
2282 }
2283
2284 if (flags & P_NUM)
2285 {
2286 // numeric option
2287 errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar,
2288 op, flags, cp_val, varp,
2289 errbuf, errbuflen);
2290 if (errmsg != NULL)
2291 goto skip;
2292 }
2293 else if (opt_idx >= 0)
2294 {
2295 // string option
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002296 if (do_set_option_string(opt_idx, opt_flags, &arg, nextchar, op,
2297 flags, cp_val, varp, errbuf,
2298 &value_checked, &errmsg) == FAIL)
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002299 {
2300 if (errmsg != NULL)
2301 goto skip;
2302 *stopopteval = TRUE;
2303 goto skip;
2304 }
2305 }
2306 else
2307 {
2308 // key code option
2309 errmsg = do_set_option_keycode(&arg, key_name, nextchar);
2310 if (errmsg != NULL)
2311 goto skip;
2312 }
2313 }
2314
2315 if (opt_idx >= 0)
2316 did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked);
2317
2318skip:
2319 *argp = arg;
2320 return errmsg;
2321}
2322
2323/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002324 * Set an option to a new value.
2325 * Return NULL if OK, return an untranslated error message when something is
2326 * wrong. "errbuf[errbuflen]" can be used to create the error message.
2327 */
2328 static char *
2329do_set_option(
2330 int opt_flags,
2331 char_u **argp,
2332 char_u *arg_start,
2333 char_u **startarg,
2334 int *did_show,
2335 int *stopopteval,
2336 char *errbuf,
2337 size_t errbuflen)
2338{
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002339 int opt_idx;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002340 char_u *arg;
2341 set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix
2342 set_op_T op;
2343 long_u flags; // flags for current option
2344 char_u *varp; // pointer to variable for current option
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002345 char_u key_name[2];
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002346 int nextchar; // next non-white char after option name
2347 int afterchar; // character just after option name
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002348 char *errmsg = NULL;
2349 int key;
2350 int len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002351
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002352 prefix = get_option_prefix(argp);
2353 arg = *argp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002354
2355 // find end of name
2356 key = 0;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002357 if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL)
2358 return e_invalid_argument;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002359
2360 // remember character after option name
2361 afterchar = arg[len];
2362
2363 if (in_vim9script())
2364 {
2365 char_u *p = skipwhite(arg + len);
2366
2367 // disallow white space before =val, +=val, -=val, ^=val
2368 if (p > arg + len && (p[0] == '='
2369 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
2370 && p[1] == '=')))
2371 {
2372 errmsg = e_no_white_space_allowed_between_option_and;
2373 arg = p;
2374 *startarg = p;
2375 goto skip;
2376 }
2377 }
2378 else
2379 // skip white space, allow ":set ai ?", ":set hlsearch !"
2380 while (VIM_ISWHITE(arg[len]))
2381 ++len;
2382
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002383 op = get_opt_op(arg + len);
2384 if (op != OP_NONE)
2385 len++;
2386
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002387 nextchar = arg[len];
2388
2389 if (opt_idx == -1 && key == 0) // found a mismatch: skip
2390 {
2391 if (in_vim9script() && arg > arg_start
2392 && vim_strchr((char_u *)"!&<", *arg) != NULL)
2393 errmsg = e_no_white_space_allowed_between_option_and;
2394 else
2395 errmsg = e_unknown_option;
2396 goto skip;
2397 }
2398
2399 if (opt_idx >= 0)
2400 {
2401 if (options[opt_idx].var == NULL) // hidden option: skip
2402 {
2403 // Only give an error message when requesting the value of
2404 // a hidden option, ignore setting it.
2405 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
2406 && (!(options[opt_idx].flags & P_BOOL)
2407 || nextchar == '?'))
2408 errmsg = e_option_not_supported;
2409 goto skip;
2410 }
2411
2412 flags = options[opt_idx].flags;
2413 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
2414 }
2415 else
2416 {
2417 flags = P_STRING;
Bram Moolenaar40b48722023-02-05 17:04:50 +00002418 varp = NULL;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002419 if (key < 0)
2420 {
2421 key_name[0] = KEY2TERMCAP0(key);
2422 key_name[1] = KEY2TERMCAP1(key);
2423 }
2424 else
2425 {
2426 key_name[0] = KS_KEY;
2427 key_name[1] = (key & 0xff);
2428 }
2429 }
2430
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002431 // Make sure the option value can be changed.
2432 if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002433 goto skip;
2434
Bram Moolenaar40b48722023-02-05 17:04:50 +00002435 int cp_val = p_cp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002436 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
2437 {
2438 arg += len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002439 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
2440 {
2441 if (arg[3] == 'm') // "opt&vim": set to Vim default
2442 {
2443 cp_val = FALSE;
2444 arg += 3;
2445 }
2446 else // "opt&vi": set to Vi default
2447 {
2448 cp_val = TRUE;
2449 arg += 2;
2450 }
2451 }
2452 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
2453 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
2454 {
2455 errmsg = e_trailing_characters;
2456 goto skip;
2457 }
2458 }
2459
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002460 // Allow '=' and ':' for historical reasons (MSDOS command.com).
2461 // Allows only one '=' character per "set" command line. grrr. (jw)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002462 if (nextchar == '?'
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002463 || (prefix == PREFIX_NONE
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002464 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
2465 && !(flags & P_BOOL)))
2466 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002467 // print value
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002468 if (*did_show)
2469 msg_putchar('\n'); // cursor below last one
2470 else
2471 {
2472 gotocmdline(TRUE); // cursor at status line
2473 *did_show = TRUE; // remember that we did a line
2474 }
2475 if (opt_idx >= 0)
2476 {
2477 showoneopt(&options[opt_idx], opt_flags);
2478#ifdef FEAT_EVAL
2479 if (p_verbose > 0)
2480 {
2481 // Mention where the option was last set.
2482 if (varp == options[opt_idx].var)
2483 last_set_msg(options[opt_idx].script_ctx);
2484 else if ((int)options[opt_idx].indir & PV_WIN)
2485 last_set_msg(curwin->w_p_script_ctx[
2486 (int)options[opt_idx].indir & PV_MASK]);
2487 else if ((int)options[opt_idx].indir & PV_BUF)
2488 last_set_msg(curbuf->b_p_script_ctx[
2489 (int)options[opt_idx].indir & PV_MASK]);
2490 }
2491#endif
2492 }
2493 else
2494 {
2495 char_u *p;
2496
2497 p = find_termcode(key_name);
2498 if (p == NULL)
2499 {
2500 errmsg = e_key_code_not_set;
2501 goto skip;
2502 }
2503 else
2504 (void)show_one_termcode(key_name, p, TRUE);
2505 }
2506 if (nextchar != '?'
2507 && nextchar != NUL && !VIM_ISWHITE(afterchar))
2508 errmsg = e_trailing_characters;
2509 }
2510 else
2511 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002512 errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op,
2513 flags, varp, key_name, nextchar,
2514 afterchar, cp_val, stopopteval, errbuf,
2515 errbuflen);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002516 }
2517
2518skip:
2519 *argp = arg;
2520 return errmsg;
2521}
2522
2523/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 * Parse 'arg' for option settings.
2525 *
2526 * 'arg' may be IObuff, but only when no errors can be present and option
2527 * does not need to be expanded with option_expand().
2528 * "opt_flags":
2529 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002530 * OPT_GLOBAL for ":setglobal"
2531 * OPT_LOCAL for ":setlocal" and a modeline
2532 * OPT_MODELINE for a modeline
2533 * OPT_WINONLY to only set window-local options
2534 * OPT_NOWIN to skip setting window-local options
2535 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002537 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 */
2539 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002540do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002541 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002542 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002544 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002546 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547
2548 if (*arg == NUL)
2549 {
2550 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002551 did_show = TRUE;
2552 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 }
2554
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002555 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002557 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002558 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002560 // ":set all" show all options.
2561 // ":set all&" set all options to their default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 arg += 3;
2563 if (*arg == '&')
2564 {
2565 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002566 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002568 didset_options();
2569 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002570 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 }
2572 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002573 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002575 did_show = TRUE;
2576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002578 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 {
2580 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002581 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002582 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 arg += 7;
2584 }
2585 else
2586 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002587 int stopopteval = FALSE;
2588 char *errmsg = NULL;
2589 char errbuf[80];
2590 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002592 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2593 &did_show, &stopopteval, errbuf,
2594 sizeof(errbuf));
2595 if (stopopteval)
2596 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002598 // Advance to next argument.
2599 // - skip until a blank found, taking care of backslashes
2600 // - skip blanks
2601 // - skip one "=val" argument (for hidden options ":set gfn =xx")
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 for (i = 0; i < 2 ; ++i)
2603 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002604 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 if (*arg++ == '\\' && *arg != NUL)
2606 ++arg;
2607 arg = skipwhite(arg);
2608 if (*arg != '=')
2609 break;
2610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002612 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002614 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
2615 i = (int)STRLEN(IObuff) + 2;
2616 if (i + (arg - startarg) < IOSIZE)
2617 {
2618 // append the argument with the error
2619 STRCAT(IObuff, ": ");
2620 mch_memmove(IObuff + i, startarg, (arg - startarg));
2621 IObuff[i + (arg - startarg)] = NUL;
2622 }
2623 // make sure all characters are printable
2624 trans_characters(IObuff, IOSIZE);
2625
2626 ++no_wait_return; // wait_return() done later
2627 emsg((char *)IObuff); // show error highlighted
2628 --no_wait_return;
2629
2630 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 }
2633
2634 arg = skipwhite(arg);
2635 }
2636
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002637theend:
2638 if (silent_mode && did_show)
2639 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002640 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002641 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002642 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002643 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002644 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002645 out_flush();
2646 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002647 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002648 }
2649
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 return OK;
2651}
2652
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002653/*
2654 * Call this when an option has been given a new value through a user command.
2655 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2656 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002657 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002658did_set_option(
2659 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002660 int opt_flags, // possibly with OPT_MODELINE
2661 int new_value, // value was replaced completely
2662 int value_checked) // value was checked to be safe, no need to set the
2663 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002664{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002665 long_u *p;
2666
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002667 options[opt_idx].flags |= P_WAS_SET;
2668
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002669 // When an option is set in the sandbox, from a modeline or in secure mode
2670 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2671 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002672 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002673 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002674#ifdef HAVE_SANDBOX
2675 || sandbox != 0
2676#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002677 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002678 *p = *p | P_INSECURE;
2679 else if (new_value)
2680 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002681}
2682
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683/*
2684 * Convert a key name or string into a key value.
2685 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002686 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002688 int
2689string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690{
2691 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002692 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 if (*arg == '^')
2694 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002695 if (multi_byte)
2696 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 return *arg;
2698}
2699
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700/*
2701 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2702 * maketitle() to create and display it.
2703 * When switching the title or icon off, call mch_restore_title() to get
2704 * the old value back.
2705 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002706 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002707did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
2709 if (starting != NO_SCREEN
2710#ifdef FEAT_GUI
2711 && !gui.starting
2712#endif
2713 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716
2717/*
2718 * set_options_bin - called when 'bin' changes value.
2719 */
2720 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002721set_options_bin(
2722 int oldval,
2723 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002724 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002726 // The option values that are changed when 'bin' changes are
2727 // copied when 'bin is set and restored when 'bin' is reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 if (newval)
2729 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002730 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 {
2732 if (!(opt_flags & OPT_GLOBAL))
2733 {
2734 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2735 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2736 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2737 curbuf->b_p_et_nobin = curbuf->b_p_et;
2738 }
2739 if (!(opt_flags & OPT_LOCAL))
2740 {
2741 p_tw_nobin = p_tw;
2742 p_wm_nobin = p_wm;
2743 p_ml_nobin = p_ml;
2744 p_et_nobin = p_et;
2745 }
2746 }
2747
2748 if (!(opt_flags & OPT_GLOBAL))
2749 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002750 curbuf->b_p_tw = 0; // no automatic line wrap
2751 curbuf->b_p_wm = 0; // no automatic line wrap
2752 curbuf->b_p_ml = 0; // no modelines
2753 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 }
2755 if (!(opt_flags & OPT_LOCAL))
2756 {
2757 p_tw = 0;
2758 p_wm = 0;
2759 p_ml = FALSE;
2760 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002761 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 }
2763 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002764 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 {
2766 if (!(opt_flags & OPT_GLOBAL))
2767 {
2768 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2769 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2770 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2771 curbuf->b_p_et = curbuf->b_p_et_nobin;
2772 }
2773 if (!(opt_flags & OPT_LOCAL))
2774 {
2775 p_tw = p_tw_nobin;
2776 p_wm = p_wm_nobin;
2777 p_ml = p_ml_nobin;
2778 p_et = p_et_nobin;
2779 }
2780 }
Christian Brabandt757593c2023-08-22 21:44:10 +02002781#if defined(FEAT_EVAL) || defined(PROTO)
2782 // Remember where the dependent option were reset
2783 didset_options_sctx(opt_flags, p_bin_dep_opts);
2784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785}
2786
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787/*
2788 * Expand environment variables for some string options.
2789 * These string options cannot be indirect!
2790 * If "val" is NULL expand the current value of the option.
2791 * Return pointer to NameBuff, or NULL when not expanded.
2792 */
2793 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002794option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002796 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2798 return NULL;
2799
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002800 // If val is longer than MAXPATHL no meaningful expansion can be done,
2801 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 if (val != NULL && STRLEN(val) > MAXPATHL)
2803 return NULL;
2804
2805 if (val == NULL)
2806 val = *(char_u **)options[opt_idx].var;
2807
2808 /*
2809 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2810 * Escape spaces when expanding 'tags', they are used to separate file
2811 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002812 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 */
2814 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002815 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002816#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002817 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2818#endif
2819 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002820 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 return NULL;
2822
2823 return NameBuff;
2824}
2825
2826/*
2827 * After setting various option values: recompute variables that depend on
2828 * option values.
2829 */
2830 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002831didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002833 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 (void)init_chartab();
2835
Bram Moolenaardac13472019-09-16 21:06:21 +02002836 didset_string_options();
2837
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002838#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002839 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002840 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002841 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002842 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002843#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002844 // set cedit_key
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002845 (void)did_set_cedit(NULL);
Bram Moolenaar597a4222014-06-25 14:39:50 +02002846#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002847 // initialize the table for 'breakat'.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002848 did_set_breakat(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002849#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002850 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002851}
2852
2853/*
2854 * More side effects of setting options.
2855 */
2856 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002857didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002858{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002859 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002860 (void)highlight_changed();
2861
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002862 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002863 check_opt_wim();
2864
Bram Moolenaareed9d462021-02-15 20:38:25 +01002865 // Parse default for 'listchars'.
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002866 (void)set_listchars_option(curwin, curwin->w_p_lcs, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002867
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002868 // Parse default for 'fillchars'.
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002869 (void)set_fillchars_option(curwin, curwin->w_p_fcs, TRUE);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002870
2871#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002872 // Parse default for 'clipboard'
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002873 (void)did_set_clipboard(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002874#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002875#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002876 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002877 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002878 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002879 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881}
2882
2883/*
2884 * Check for string options that are NULL (normally only termcap options).
2885 */
2886 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002887check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888{
2889 int opt_idx;
2890
2891 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2892 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2893 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2894}
2895
2896/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002897 * Return the option index found by a pointer into term_strings[].
2898 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002900 int
2901get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002903 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904
2905 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2906 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002907 return opt_idx;
2908 return -1; // cannot happen: didn't find it!
2909}
2910
2911/*
2912 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2913 * Return the option index or -1 if not found.
2914 */
2915 int
2916set_term_option_alloced(char_u **p)
2917{
2918 int opt_idx = get_term_opt_idx(p);
2919
2920 if (opt_idx >= 0)
2921 options[opt_idx].flags |= P_ALLOCED;
2922 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923}
2924
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002925#if defined(FEAT_EVAL) || defined(PROTO)
2926/*
2927 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2928 * Return FALSE when it wasn't.
2929 * Return -1 for an unknown option.
2930 */
2931 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002932was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002933{
2934 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002935 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002936
2937 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002938 {
2939 flagp = insecure_flag(idx, opt_flags);
2940 return (*flagp & P_INSECURE) != 0;
2941 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002942 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002943 return -1;
2944}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002945
2946/*
2947 * Get a pointer to the flags used for the P_INSECURE flag of option
2948 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002949 * NOTE: Caller must make sure that "curwin" is set to the window from which
2950 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002951 */
2952 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002953insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002954{
2955 if (opt_flags & OPT_LOCAL)
2956 switch ((int)options[opt_idx].indir)
2957 {
2958#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002959 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002960#endif
2961#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002962# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002963 case PV_FDE: return &curwin->w_p_fde_flags;
2964 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002965# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002966# ifdef FEAT_BEVAL
2967 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2968# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002969 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002970 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002971# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002972 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002973# endif
2974#endif
2975 }
2976
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002977 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002978 return &options[opt_idx].flags;
2979}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002980#endif
2981
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002982/*
2983 * Redraw the window title and/or tab page text later.
2984 */
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02002985 void
2986redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002987{
2988 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002989 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002990}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002991
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002993 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2994 * characters or characters in "allowed".
2995 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002996 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002997valid_name(char_u *val, char *allowed)
2998{
2999 char_u *s;
3000
3001 for (s = val; *s != NUL; ++s)
3002 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
3003 return FALSE;
3004 return TRUE;
3005}
3006
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003007#if defined(FEAT_EVAL) || defined(PROTO)
3008/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003009 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003010 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003011 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003012 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003013set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003014{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003015 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
3016 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003017 sctx_T new_script_ctx = script_ctx;
3018
Bram Moolenaar51258742020-05-03 17:19:33 +02003019 // Modeline already has the line number set.
3020 if (!(opt_flags & OPT_MODELINE))
3021 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003022
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003023 // Remember where the option was set. For local options need to do that
3024 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003025 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003026 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003027 if (both || (opt_flags & OPT_LOCAL))
3028 {
3029 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003030 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003031 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00003032 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003033 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00003034 if (both)
3035 // also setting the "all buffers" value
3036 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
3037 new_script_ctx;
3038 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003039 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003040}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003041
3042/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00003043 * Get the script context of global option "name".
3044 *
3045 */
3046 sctx_T *
3047get_option_sctx(char *name)
3048{
3049 int idx = findoption((char_u *)name);
3050
3051 if (idx >= 0)
3052 return &options[idx].script_ctx;
3053 siemsg("no such option: %s", name);
3054 return NULL;
3055}
3056
3057/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003058 * Set the script_ctx for a termcap option.
3059 * "name" must be the two character code, e.g. "RV".
3060 * When "name" is NULL use "opt_idx".
3061 */
3062 void
3063set_term_option_sctx_idx(char *name, int opt_idx)
3064{
3065 char_u buf[5];
3066 int idx;
3067
3068 if (name == NULL)
3069 idx = opt_idx;
3070 else
3071 {
3072 buf[0] = 't';
3073 buf[1] = '_';
3074 buf[2] = name[0];
3075 buf[3] = name[1];
3076 buf[4] = 0;
3077 idx = findoption(buf);
3078 }
3079 if (idx >= 0)
3080 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
3081}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003082#endif
3083
Bram Moolenaarf4140482020-02-15 23:06:45 +01003084#if defined(FEAT_EVAL)
3085/*
3086 * Apply the OptionSet autocommand.
3087 */
3088 static void
3089apply_optionset_autocmd(
3090 int opt_idx,
3091 long opt_flags,
3092 long oldval,
3093 long oldval_g,
3094 long newval,
3095 char *errmsg)
3096{
3097 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
3098
3099 // Don't do this while starting up, failure or recursively.
3100 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
3101 return;
3102
3103 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
3104 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
3105 oldval_g);
3106 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
3107 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
3108 (opt_flags & OPT_LOCAL) ? "local" : "global");
3109 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3110 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3111 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
3112 if (opt_flags & OPT_LOCAL)
3113 {
3114 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3115 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3116 }
3117 if (opt_flags & OPT_GLOBAL)
3118 {
3119 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3120 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3121 }
3122 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3123 {
3124 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3125 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3126 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3127 }
3128 if (opt_flags & OPT_MODELINE)
3129 {
3130 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3131 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3132 }
3133 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3134 NULL, FALSE, NULL);
3135 reset_v_option_vars();
3136}
3137#endif
3138
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003139#if defined(FEAT_ARABIC) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003140/*
3141 * Process the updated 'arabic' option value.
3142 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003143 char *
3144did_set_arabic(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003145{
3146 char *errmsg = NULL;
3147
3148 if (curwin->w_p_arab)
3149 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003150 // 'arabic' is set, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003151 if (!p_tbidi)
3152 {
3153 // set rightleft mode
3154 if (!curwin->w_p_rl)
3155 {
3156 curwin->w_p_rl = TRUE;
3157 changed_window_setting();
3158 }
3159
3160 // Enable Arabic shaping (major part of what Arabic requires)
3161 if (!p_arshape)
3162 {
3163 p_arshape = TRUE;
3164 redraw_later_clear();
3165 }
3166 }
3167
3168 // Arabic requires a utf-8 encoding, inform the user if it's not
3169 // set.
3170 if (STRCMP(p_enc, "utf-8") != 0)
3171 {
3172 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3173
3174 msg_source(HL_ATTR(HLF_W));
3175 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
3176#ifdef FEAT_EVAL
3177 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3178#endif
3179 }
3180
3181 // set 'delcombine'
3182 p_deco = TRUE;
3183
3184# ifdef FEAT_KEYMAP
3185 // Force-set the necessary keymap for arabic
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003186 errmsg = set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3187 OPT_LOCAL);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003188# endif
3189 }
3190 else
3191 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003192 // 'arabic' is reset, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003193 if (!p_tbidi)
3194 {
3195 // reset rightleft mode
3196 if (curwin->w_p_rl)
3197 {
3198 curwin->w_p_rl = FALSE;
3199 changed_window_setting();
3200 }
3201
3202 // 'arabicshape' isn't reset, it is a global option and
3203 // another window may still need it "on".
3204 }
3205
3206 // 'delcombine' isn't reset, it is a global option and another
3207 // window may still want it "on".
3208
3209# ifdef FEAT_KEYMAP
3210 // Revert to the default keymap
3211 curbuf->b_p_iminsert = B_IMODE_NONE;
3212 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3213# endif
3214 }
3215
3216 return errmsg;
3217}
3218#endif
3219
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003220#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
3221/*
3222 * Process the updated 'autochdir' option value.
3223 */
3224 char *
3225did_set_autochdir(optset_T *args UNUSED)
3226{
3227 // Change directories when the 'acd' option is set now.
3228 DO_AUTOCHDIR;
3229 return NULL;
3230}
3231#endif
3232
3233#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
3234/*
3235 * Process the updated 'ballooneval' option value.
3236 */
3237 char *
3238did_set_ballooneval(optset_T *args)
3239{
3240 if (balloonEvalForTerm)
3241 return NULL;
3242
3243 if (p_beval && !args->os_oldval.boolean)
3244 gui_mch_enable_beval_area(balloonEval);
3245 else if (!p_beval && args->os_oldval.boolean)
3246 gui_mch_disable_beval_area(balloonEval);
3247
3248 return NULL;
3249}
3250#endif
3251
3252#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
3253/*
3254 * Process the updated 'balloonevalterm' option value.
3255 */
3256 char *
3257did_set_balloonevalterm(optset_T *args UNUSED)
3258{
3259 mch_bevalterm_changed();
3260 return NULL;
3261}
3262#endif
3263
3264/*
3265 * Process the updated 'binary' option value.
3266 */
3267 char *
3268did_set_binary(optset_T *args)
3269{
3270 // when 'bin' is set also set some other options
3271 set_options_bin(args->os_oldval.boolean, curbuf->b_p_bin, args->os_flags);
3272 redraw_titles();
3273
3274 return NULL;
3275}
3276
3277#if defined(FEAT_LINEBREAK) || defined(PROTO)
3278/*
3279 * Called when the 'breakat' option changes value.
3280 */
3281 char *
3282did_set_breakat(optset_T *args UNUSED)
3283{
3284 char_u *p;
3285 int i;
3286
3287 for (i = 0; i < 256; i++)
3288 breakat_flags[i] = FALSE;
3289
3290 if (p_breakat != NULL)
3291 for (p = p_breakat; *p; p++)
3292 breakat_flags[*p] = TRUE;
3293
3294 return NULL;
3295}
3296#endif
3297
3298/*
3299 * Process the updated 'buflisted' option value.
3300 */
3301 char *
3302did_set_buflisted(optset_T *args)
3303{
3304 // when 'buflisted' changes, trigger autocommands
3305 if (args->os_oldval.boolean != curbuf->b_p_bl)
3306 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3307 NULL, NULL, TRUE, curbuf);
3308 return NULL;
3309}
3310
3311/*
3312 * Process the new 'cmdheight' option value.
3313 */
3314 char *
3315did_set_cmdheight(optset_T *args)
3316{
3317 long old_value = args->os_oldval.number;
3318 char *errmsg = NULL;
3319
3320 // if p_ch changed value, change the command line height
3321 if (p_ch < 1)
3322 {
3323 errmsg = e_argument_must_be_positive;
3324 p_ch = 1;
3325 }
3326 if (p_ch > Rows - min_rows() + 1)
3327 p_ch = Rows - min_rows() + 1;
3328
3329 // Only compute the new window layout when startup has been
3330 // completed. Otherwise the frame sizes may be wrong.
3331 if ((p_ch != old_value
3332 || tabline_height() + topframe->fr_height != Rows - p_ch)
3333 && full_screen
3334#ifdef FEAT_GUI
3335 && !gui.starting
3336#endif
3337 )
3338 command_height();
3339
3340 return errmsg;
3341}
3342
3343/*
3344 * Process the updated 'compatible' option value.
3345 */
3346 char *
3347did_set_compatible(optset_T *args UNUSED)
3348{
3349 compatible_set();
3350 return NULL;
3351}
3352
3353#if defined(FEAT_CONCEAL) || defined(PROTO)
3354/*
3355 * Process the new 'conceallevel' option value.
3356 */
3357 char *
3358did_set_conceallevel(optset_T *args UNUSED)
3359{
3360 char *errmsg = NULL;
3361
3362 if (curwin->w_p_cole < 0)
3363 {
3364 errmsg = e_argument_must_be_positive;
3365 curwin->w_p_cole = 0;
3366 }
3367 else if (curwin->w_p_cole > 3)
3368 {
3369 errmsg = e_invalid_argument;
3370 curwin->w_p_cole = 3;
3371 }
3372
3373 return errmsg;
3374}
3375#endif
3376
3377#if defined(FEAT_DIFF) || defined(PROTO)
3378/*
3379 * Process the updated 'diff' option value.
3380 */
3381 char *
3382did_set_diff(optset_T *args UNUSED)
3383{
3384 // May add or remove the buffer from the list of diff buffers.
3385 diff_buf_adjust(curwin);
3386# ifdef FEAT_FOLDING
3387 if (foldmethodIsDiff(curwin))
3388 foldUpdateAll(curwin);
3389# endif
3390 return NULL;
3391}
3392#endif
3393
3394/*
3395 * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb'
3396 * option value.
3397 */
3398 char *
3399did_set_eof_eol_fixeol_bomb(optset_T *args UNUSED)
3400{
3401 // redraw the window title and tab page text
3402 redraw_titles();
3403 return NULL;
3404}
3405
3406/*
3407 * Process the updated 'equalalways' option value.
3408 */
3409 char *
3410did_set_equalalways(optset_T *args)
3411{
3412 if (p_ea && !args->os_oldval.boolean)
3413 win_equal(curwin, FALSE, 0);
3414
3415 return NULL;
3416}
3417
3418#if defined(FEAT_FOLDING) || defined(PROTO)
3419/*
3420 * Process the new 'foldcolumn' option value.
3421 */
3422 char *
3423did_set_foldcolumn(optset_T *args UNUSED)
3424{
3425 char *errmsg = NULL;
3426
3427 if (curwin->w_p_fdc < 0)
3428 {
3429 errmsg = e_argument_must_be_positive;
3430 curwin->w_p_fdc = 0;
3431 }
3432 else if (curwin->w_p_fdc > 12)
3433 {
3434 errmsg = e_invalid_argument;
3435 curwin->w_p_fdc = 12;
3436 }
3437
3438 return errmsg;
3439}
3440
3441/*
3442 * Process the new 'foldlevel' option value.
3443 */
3444 char *
3445did_set_foldlevel(optset_T *args UNUSED)
3446{
3447 if (curwin->w_p_fdl < 0)
3448 curwin->w_p_fdl = 0;
3449 newFoldLevel();
3450 return NULL;
3451}
3452
3453/*
3454 * Process the new 'foldminlines' option value.
3455 */
3456 char *
3457did_set_foldminlines(optset_T *args UNUSED)
3458{
3459 foldUpdateAll(curwin);
3460 return NULL;
3461}
3462
3463/*
3464 * Process the new 'foldnestmax' option value.
3465 */
3466 char *
3467did_set_foldnestmax(optset_T *args UNUSED)
3468{
3469 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3470 foldUpdateAll(curwin);
3471 return NULL;
3472}
3473#endif
3474
3475#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
3476/*
3477 * Process the updated 'hlsearch' option value.
3478 */
3479 char *
3480did_set_hlsearch(optset_T *args UNUSED)
3481{
3482 // when 'hlsearch' is set or reset: reset no_hlsearch
3483 set_no_hlsearch(FALSE);
3484 return NULL;
3485}
3486#endif
3487
3488/*
3489 * Process the updated 'ignorecase' option value.
3490 */
3491 char *
3492did_set_ignorecase(optset_T *args UNUSED)
3493{
3494 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
3495 if (p_hls)
3496 redraw_all_later(UPD_SOME_VALID);
3497 return NULL;
3498}
3499
3500#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
3501/*
3502 * Process the updated 'imdisable' option value.
3503 */
3504 char *
3505did_set_imdisable(optset_T *args UNUSED)
3506{
3507 // Only de-activate it here, it will be enabled when changing mode.
3508 if (p_imdisable)
3509 im_set_active(FALSE);
3510 else if (State & MODE_INSERT)
3511 // When the option is set from an autocommand, it may need to take
3512 // effect right away.
3513 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
3514 return NULL;
3515}
3516#endif
3517
3518/*
3519 * Process the new 'iminsert' option value.
3520 */
3521 char *
3522did_set_iminsert(optset_T *args UNUSED)
3523{
3524 char *errmsg = NULL;
3525
3526 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3527 {
3528 errmsg = e_invalid_argument;
3529 curbuf->b_p_iminsert = B_IMODE_NONE;
3530 }
3531 p_iminsert = curbuf->b_p_iminsert;
3532 if (termcap_active) // don't do this in the alternate screen
3533 showmode();
3534#if defined(FEAT_KEYMAP)
3535 // Show/unshow value of 'keymap' in status lines.
3536 status_redraw_curbuf();
3537#endif
3538
3539 return errmsg;
3540}
3541
3542/*
3543 * Process the new 'imsearch' option value.
3544 */
3545 char *
3546did_set_imsearch(optset_T *args UNUSED)
3547{
3548 char *errmsg = NULL;
3549
3550 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3551 {
3552 errmsg = e_invalid_argument;
3553 curbuf->b_p_imsearch = B_IMODE_NONE;
3554 }
3555 p_imsearch = curbuf->b_p_imsearch;
3556
3557 return errmsg;
3558}
3559
3560#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3561/*
3562 * Process the new 'imstyle' option value.
3563 */
3564 char *
3565did_set_imstyle(optset_T *args UNUSED)
3566{
3567 char *errmsg = NULL;
3568
3569 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3570 errmsg = e_invalid_argument;
3571
3572 return errmsg;
3573}
3574#endif
3575
3576/*
3577 * Process the updated 'insertmode' option value.
3578 */
3579 char *
3580did_set_insertmode(optset_T *args)
3581{
3582 // when 'insertmode' is set from an autocommand need to do work here
3583 if (p_im)
3584 {
3585 if ((State & MODE_INSERT) == 0)
3586 need_start_insertmode = TRUE;
3587 stop_insert_mode = FALSE;
3588 }
3589 // only reset if it was set previously
3590 else if (args->os_oldval.boolean)
3591 {
3592 need_start_insertmode = FALSE;
3593 stop_insert_mode = TRUE;
3594 if (restart_edit != 0 && mode_displayed)
3595 clear_cmdline = TRUE; // remove "(insert)"
3596 restart_edit = 0;
3597 }
3598
3599 return NULL;
3600}
3601
3602#if defined(FEAT_LANGMAP) || defined(PROTO)
3603/*
3604 * Process the updated 'langnoremap' option value.
3605 */
3606 char *
3607did_set_langnoremap(optset_T *args UNUSED)
3608{
3609 // 'langnoremap' -> !'langremap'
3610 p_lrm = !p_lnr;
3611 return NULL;
3612}
3613
3614/*
3615 * Process the updated 'langremap' option value.
3616 */
3617 char *
3618did_set_langremap(optset_T *args UNUSED)
3619{
3620 // 'langremap' -> !'langnoremap'
3621 p_lnr = !p_lrm;
3622 return NULL;
3623}
3624#endif
3625
3626/*
3627 * Process the new 'laststatus' option value.
3628 */
3629 char *
3630did_set_laststatus(optset_T *args UNUSED)
3631{
3632 last_status(FALSE); // (re)set last window status line
3633 return NULL;
3634}
3635
3636#if defined(FEAT_GUI) || defined(PROTO)
3637/*
3638 * Process the new 'linespace' option value.
3639 */
3640 char *
3641did_set_linespace(optset_T *args UNUSED)
3642{
3643 // Recompute gui.char_height and resize the Vim window to keep the
3644 // same number of lines.
3645 if (gui.in_use && gui_mch_adjust_charheight() == OK)
3646 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
3647 return NULL;
3648}
3649#endif
3650
3651/*
3652 * Process the updated 'lisp' option value.
3653 */
3654 char *
3655did_set_lisp(optset_T *args UNUSED)
3656{
3657 // When 'lisp' option changes include/exclude '-' in keyword characters.
3658 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
3659 return NULL;
3660}
3661
3662/*
3663 * Process the new 'maxcombine' option value.
3664 */
3665 char *
3666did_set_maxcombine(optset_T *args UNUSED)
3667{
3668 if (p_mco > MAX_MCO)
3669 p_mco = MAX_MCO;
3670 else if (p_mco < 0)
3671 p_mco = 0;
3672 screenclear(); // will re-allocate the screen
3673 return NULL;
3674}
3675
3676/*
3677 * Process the updated 'modifiable' option value.
3678 */
3679 char *
3680did_set_modifiable(optset_T *args UNUSED)
3681{
3682 // when 'modifiable' is changed, redraw the window title
3683
3684# ifdef FEAT_TERMINAL
3685 // Cannot set 'modifiable' when in Terminal mode.
3686 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3687 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
3688 {
3689 curbuf->b_p_ma = FALSE;
3690 args->os_doskip = TRUE;
3691 return e_cannot_make_terminal_with_running_job_modifiable;
3692 }
3693# endif
3694 redraw_titles();
3695
3696 return NULL;
3697}
3698
3699/*
3700 * Process the updated 'modified' option value.
3701 */
3702 char *
3703did_set_modified(optset_T *args)
3704{
3705 if (!args->os_newval.boolean)
3706 save_file_ff(curbuf); // Buffer is unchanged
3707 redraw_titles();
3708 modified_was_set = args->os_newval.boolean;
3709 return NULL;
3710}
3711
3712#if defined(FEAT_GUI) || defined(PROTO)
3713/*
3714 * Process the updated 'mousehide' option value.
3715 */
3716 char *
3717did_set_mousehide(optset_T *args UNUSED)
3718{
3719 if (!p_mh)
3720 gui_mch_mousehide(FALSE);
3721 return NULL;
3722}
3723#endif
3724
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003725/*
3726 * Process the updated 'number' or 'relativenumber' option value.
3727 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003728 char *
3729did_set_number_relativenumber(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003730{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003731#if (defined(FEAT_SIGNS) && defined(FEAT_GUI)) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003732 if (gui.in_use
3733 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3734 && curbuf->b_signlist != NULL)
3735 {
3736 // If the 'number' or 'relativenumber' options are modified and
3737 // 'signcolumn' is set to 'number', then clear the screen for a full
3738 // refresh. Otherwise the sign icons are not displayed properly in the
3739 // number column. If the 'number' option is set and only the
3740 // 'relativenumber' option is toggled, then don't refresh the screen
3741 // (optimization).
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003742 if (!(curwin->w_p_nu && ((int *)args->os_varp == &curwin->w_p_rnu)))
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003743 redraw_all_later(UPD_CLEAR);
3744 }
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003745#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003746 return NULL;
3747}
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003748
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003749#if defined(FEAT_LINEBREAK) || defined(PROTO)
3750/*
3751 * Process the new 'numberwidth' option value.
3752 */
3753 char *
3754did_set_numberwidth(optset_T *args UNUSED)
3755{
3756 char *errmsg = NULL;
3757
3758 // 'numberwidth' must be positive
3759 if (curwin->w_p_nuw < 1)
3760 {
3761 errmsg = e_argument_must_be_positive;
3762 curwin->w_p_nuw = 1;
3763 }
3764 if (curwin->w_p_nuw > 20)
3765 {
3766 errmsg = e_invalid_argument;
3767 curwin->w_p_nuw = 20;
3768 }
3769 curwin->w_nrwidth_line_count = 0; // trigger a redraw
3770
3771 return errmsg;
3772}
3773#endif
3774
3775/*
3776 * Process the updated 'paste' option value. Called after p_paste was set or
3777 * reset. When 'paste' is set or reset also change other options.
3778 */
3779 char *
3780did_set_paste(optset_T *args UNUSED)
3781{
3782 static int old_p_paste = FALSE;
3783 static int save_sm = 0;
3784 static int save_sta = 0;
3785 static int save_ru = 0;
3786#ifdef FEAT_RIGHTLEFT
3787 static int save_ri = 0;
3788 static int save_hkmap = 0;
3789#endif
3790 buf_T *buf;
3791
3792 if (p_paste)
3793 {
3794 // Paste switched from off to on.
3795 // Save the current values, so they can be restored later.
3796 if (!old_p_paste)
3797 {
3798 // save options for each buffer
3799 FOR_ALL_BUFFERS(buf)
3800 {
3801 buf->b_p_tw_nopaste = buf->b_p_tw;
3802 buf->b_p_wm_nopaste = buf->b_p_wm;
3803 buf->b_p_sts_nopaste = buf->b_p_sts;
3804 buf->b_p_ai_nopaste = buf->b_p_ai;
3805 buf->b_p_et_nopaste = buf->b_p_et;
3806#ifdef FEAT_VARTABS
3807 if (buf->b_p_vsts_nopaste)
3808 vim_free(buf->b_p_vsts_nopaste);
3809 buf->b_p_vsts_nopaste =
3810 buf->b_p_vsts && buf->b_p_vsts != empty_option
3811 ? vim_strsave(buf->b_p_vsts) : NULL;
3812#endif
3813 }
3814
3815 // save global options
3816 save_sm = p_sm;
3817 save_sta = p_sta;
3818 save_ru = p_ru;
3819#ifdef FEAT_RIGHTLEFT
3820 save_ri = p_ri;
3821 save_hkmap = p_hkmap;
3822#endif
3823 // save global values for local buffer options
3824 p_ai_nopaste = p_ai;
3825 p_et_nopaste = p_et;
3826 p_sts_nopaste = p_sts;
3827 p_tw_nopaste = p_tw;
3828 p_wm_nopaste = p_wm;
3829#ifdef FEAT_VARTABS
3830 if (p_vsts_nopaste)
3831 vim_free(p_vsts_nopaste);
3832 p_vsts_nopaste = p_vsts && p_vsts != empty_option
3833 ? vim_strsave(p_vsts) : NULL;
3834#endif
3835 }
3836
3837 // Always set the option values, also when 'paste' is set when it is
3838 // already on. Set options for each buffer.
3839 FOR_ALL_BUFFERS(buf)
3840 {
3841 buf->b_p_tw = 0; // textwidth is 0
3842 buf->b_p_wm = 0; // wrapmargin is 0
3843 buf->b_p_sts = 0; // softtabstop is 0
3844 buf->b_p_ai = 0; // no auto-indent
3845 buf->b_p_et = 0; // no expandtab
3846#ifdef FEAT_VARTABS
3847 if (buf->b_p_vsts)
3848 free_string_option(buf->b_p_vsts);
3849 buf->b_p_vsts = empty_option;
3850 VIM_CLEAR(buf->b_p_vsts_array);
3851#endif
3852 }
3853
3854 // set global options
3855 p_sm = 0; // no showmatch
3856 p_sta = 0; // no smarttab
3857 if (p_ru)
3858 status_redraw_all(); // redraw to remove the ruler
3859 p_ru = 0; // no ruler
3860#ifdef FEAT_RIGHTLEFT
3861 p_ri = 0; // no reverse insert
3862 p_hkmap = 0; // no Hebrew keyboard
3863#endif
3864 // set global values for local buffer options
3865 p_tw = 0;
3866 p_wm = 0;
3867 p_sts = 0;
3868 p_ai = 0;
Christian Brabandt757593c2023-08-22 21:44:10 +02003869 p_et = 0;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003870#ifdef FEAT_VARTABS
3871 if (p_vsts)
3872 free_string_option(p_vsts);
3873 p_vsts = empty_option;
3874#endif
3875 }
3876
3877 // Paste switched from on to off: Restore saved values.
3878 else if (old_p_paste)
3879 {
3880 // restore options for each buffer
3881 FOR_ALL_BUFFERS(buf)
3882 {
3883 buf->b_p_tw = buf->b_p_tw_nopaste;
3884 buf->b_p_wm = buf->b_p_wm_nopaste;
3885 buf->b_p_sts = buf->b_p_sts_nopaste;
3886 buf->b_p_ai = buf->b_p_ai_nopaste;
3887 buf->b_p_et = buf->b_p_et_nopaste;
3888#ifdef FEAT_VARTABS
3889 if (buf->b_p_vsts)
3890 free_string_option(buf->b_p_vsts);
3891 buf->b_p_vsts = buf->b_p_vsts_nopaste
3892 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
3893 vim_free(buf->b_p_vsts_array);
3894 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
3895 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
3896 else
3897 buf->b_p_vsts_array = NULL;
3898#endif
3899 }
3900
3901 // restore global options
3902 p_sm = save_sm;
3903 p_sta = save_sta;
3904 if (p_ru != save_ru)
3905 status_redraw_all(); // redraw to draw the ruler
3906 p_ru = save_ru;
3907#ifdef FEAT_RIGHTLEFT
3908 p_ri = save_ri;
3909 p_hkmap = save_hkmap;
3910#endif
3911 // set global values for local buffer options
3912 p_ai = p_ai_nopaste;
3913 p_et = p_et_nopaste;
3914 p_sts = p_sts_nopaste;
3915 p_tw = p_tw_nopaste;
3916 p_wm = p_wm_nopaste;
3917#ifdef FEAT_VARTABS
3918 if (p_vsts)
3919 free_string_option(p_vsts);
3920 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
3921#endif
3922 }
3923
3924 old_p_paste = p_paste;
3925
Christian Brabandt757593c2023-08-22 21:44:10 +02003926#if defined(FEAT_EVAL) || defined(PROTO)
3927 // Remember where the dependent options were reset
3928 didset_options_sctx((OPT_LOCAL | OPT_GLOBAL), p_paste_dep_opts);
3929#endif
3930
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003931 return NULL;
3932}
3933
3934
3935#ifdef FEAT_QUICKFIX
3936/*
3937 * Process the updated 'previewwindow' option value.
3938 */
3939 char *
3940did_set_previewwindow(optset_T *args)
3941{
3942 if (!curwin->w_p_pvw)
3943 return NULL;
3944
3945 // There can be only one window with 'previewwindow' set.
3946 win_T *win;
3947
3948 FOR_ALL_WINDOWS(win)
3949 if (win->w_p_pvw && win != curwin)
3950 {
3951 curwin->w_p_pvw = FALSE;
3952 args->os_doskip = TRUE;
3953 return e_preview_window_already_exists;
3954 }
3955
3956 return NULL;
3957}
3958#endif
3959
3960#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
3961/*
3962 * Process the new 'pyxversion' option value.
3963 */
3964 char *
3965did_set_pyxversion(optset_T *args UNUSED)
3966{
3967 char *errmsg = NULL;
3968
3969 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3970 errmsg = e_invalid_argument;
3971
3972 return errmsg;
3973}
3974#endif
3975
3976/*
3977 * Process the updated 'readonly' option value.
3978 */
3979 char *
3980did_set_readonly(optset_T *args)
3981{
3982 // when 'readonly' is reset globally, also reset readonlymode
3983 if (!curbuf->b_p_ro && (args->os_flags & OPT_LOCAL) == 0)
3984 readonlymode = FALSE;
3985
3986 // when 'readonly' is set may give W10 again
3987 if (curbuf->b_p_ro)
3988 curbuf->b_did_warn = FALSE;
3989
3990 redraw_titles();
3991
3992 return NULL;
3993}
3994
3995/*
3996 * Process the updated 'scrollbind' option value.
3997 */
3998 char *
3999did_set_scrollbind(optset_T *args UNUSED)
4000{
4001 // when 'scrollbind' is set: snapshot the current position to avoid a jump
4002 // at the end of normal_cmd()
4003 if (!curwin->w_p_scb)
4004 return NULL;
4005
4006 do_check_scrollbind(FALSE);
4007 curwin->w_scbind_pos = curwin->w_topline;
4008 return NULL;
4009}
4010
4011#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
4012/*
4013 * Process the updated 'shellslash' option value.
4014 */
4015 char *
4016did_set_shellslash(optset_T *args UNUSED)
4017{
4018 if (p_ssl)
4019 {
4020 psepc = '/';
4021 psepcN = '\\';
4022 pseps[0] = '/';
4023 }
4024 else
4025 {
4026 psepc = '\\';
4027 psepcN = '/';
4028 pseps[0] = '\\';
4029 }
4030
4031 // need to adjust the file name arguments and buffer names.
4032 buflist_slash_adjust();
4033 alist_slash_adjust();
4034# ifdef FEAT_EVAL
4035 scriptnames_slash_adjust();
4036# endif
4037 return NULL;
4038}
4039#endif
4040
4041/*
4042 * Process the new 'shiftwidth' or the 'tabstop' option value.
4043 */
4044 char *
4045did_set_shiftwidth_tabstop(optset_T *args)
4046{
4047 long *pp = (long *)args->os_varp;
4048 char *errmsg = NULL;
4049
4050 if (curbuf->b_p_sw < 0)
4051 {
4052 errmsg = e_argument_must_be_positive;
4053#ifdef FEAT_VARTABS
4054 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4055 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4056 ? tabstop_first(curbuf->b_p_vts_array)
4057 : curbuf->b_p_ts;
4058#else
4059 curbuf->b_p_sw = curbuf->b_p_ts;
4060#endif
4061 }
4062
4063#ifdef FEAT_FOLDING
4064 if (foldmethodIsIndent(curwin))
4065 foldUpdateAll(curwin);
4066#endif
4067 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
4068 // parse 'cinoptions'.
4069 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
4070 parse_cino(curbuf);
4071
4072 return errmsg;
4073}
4074
4075/*
4076 * Process the new 'showtabline' option value.
4077 */
4078 char *
4079did_set_showtabline(optset_T *args UNUSED)
4080{
4081 shell_new_rows(); // recompute window positions and heights
4082 return NULL;
4083}
4084
4085/*
4086 * Process the updated 'smoothscroll' option value.
4087 */
4088 char *
4089did_set_smoothscroll(optset_T *args UNUSED)
4090{
4091 if (curwin->w_p_sms)
4092 return NULL;
4093
4094 curwin->w_skipcol = 0;
4095 changed_line_abv_curs();
4096 return NULL;
4097}
4098
4099#if defined(FEAT_SPELL) || defined(PROTO)
4100/*
4101 * Process the updated 'spell' option value.
4102 */
4103 char *
4104did_set_spell(optset_T *args UNUSED)
4105{
4106 if (curwin->w_p_spell)
4107 return parse_spelllang(curwin);
4108
4109 return NULL;
4110}
4111#endif
4112
4113/*
4114 * Process the updated 'swapfile' option value.
4115 */
4116 char *
4117did_set_swapfile(optset_T *args UNUSED)
4118{
4119 // when 'swf' is set, create swapfile, when reset remove swapfile
4120 if (curbuf->b_p_swf && p_uc)
4121 ml_open_file(curbuf); // create the swap file
4122 else
4123 // no need to reset curbuf->b_may_swap, ml_open_file() will check
4124 // buf->b_p_swf
4125 mf_close_file(curbuf, TRUE); // remove the swap file
4126 return NULL;
4127}
4128
4129#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004130 char *
4131did_set_termguicolors(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004132{
4133# ifdef FEAT_VTP
4134 // Do not turn on 'tgc' when 24-bit colors are not supported.
4135 if (
4136# ifdef VIMDLL
4137 !gui.in_use && !gui.starting &&
4138# endif
4139 !has_vtp_working())
4140 {
4141 p_tgc = 0;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004142 args->os_doskip = TRUE;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004143 return e_24_bit_colors_are_not_supported_on_this_environment;
4144 }
4145 if (is_term_win32())
4146 swap_tcap();
4147# endif
4148# ifdef FEAT_GUI
4149 if (!gui.in_use && !gui.starting)
4150# endif
4151 highlight_gui_started();
4152# ifdef FEAT_VTP
4153 // reset t_Co
4154 if (is_term_win32())
4155 {
4156 control_console_color_rgb();
4157 set_termname(T_NAME);
4158 init_highlight(TRUE, FALSE);
4159 }
4160# endif
4161# ifdef FEAT_TERMINAL
4162 term_update_colors_all();
4163 term_update_palette_all();
4164 term_update_wincolor_all();
4165# endif
4166
4167 return NULL;
4168}
4169#endif
4170
4171/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004172 * Process the updated 'terse' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004174 char *
4175did_set_terse(optset_T *args UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004177 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004179 // when 'terse' is set change 'shortmess'
4180 p = vim_strchr(p_shm, SHM_SEARCH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004182 // insert 's' in p_shm
4183 if (p_terse && p == NULL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004184 {
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004185 STRCPY(IObuff, p_shm);
4186 STRCAT(IObuff, "s");
4187 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004188 }
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004189 // remove 's' from p_shm
4190 else if (!p_terse && p != NULL)
4191 STRMOVE(p, p + 1);
4192 return NULL;
4193}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004195/*
4196 * Process the updated 'textauto' option value.
4197 */
4198 char *
4199did_set_textauto(optset_T *args)
4200{
4201 // when 'textauto' is set or reset also change 'fileformats'
4202 set_string_option_direct((char_u *)"ffs", -1,
4203 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
4204 OPT_FREE | args->os_flags, 0);
Bram Moolenaar53744302015-07-17 17:38:22 +02004205
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004206 return NULL;
4207}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004209/*
4210 * Process the updated 'textmode' option value.
4211 */
4212 char *
4213did_set_textmode(optset_T *args)
4214{
4215 // when 'textmode' is set or reset also change 'fileformat'
4216 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, args->os_flags);
4217
4218 return NULL;
4219}
4220
4221/*
4222 * Process the new 'textwidth' option value.
4223 */
4224 char *
4225did_set_textwidth(optset_T *args UNUSED)
4226{
4227 char *errmsg = NULL;
4228
4229 if (curbuf->b_p_tw < 0)
4230 {
4231 errmsg = e_argument_must_be_positive;
4232 curbuf->b_p_tw = 0;
4233 }
4234#ifdef FEAT_SYN_HL
4235 {
4236 win_T *wp;
4237 tabpage_T *tp;
4238
4239 FOR_ALL_TAB_WINDOWS(tp, wp)
4240 check_colorcolumn(wp);
4241 }
Bram Moolenaar53744302015-07-17 17:38:22 +02004242#endif
4243
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004244 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245}
4246
4247/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004248 * Process the updated 'title' or the 'icon' option value.
4249 */
4250 char *
4251did_set_title_icon(optset_T *args UNUSED)
4252{
4253 // when 'title' changed, may need to change the title; same for 'icon'
4254 did_set_title();
4255 return NULL;
4256}
4257
4258/*
4259 * Process the new 'titlelen' option value.
4260 */
4261 char *
4262did_set_titlelen(optset_T *args)
4263{
4264 long old_value = args->os_oldval.number;
4265 char *errmsg = NULL;
4266
4267 // if 'titlelen' has changed, redraw the title
4268 if (p_titlelen < 0)
4269 {
4270 errmsg = e_argument_must_be_positive;
4271 p_titlelen = 85;
4272 }
4273 if (starting != NO_SCREEN && old_value != p_titlelen)
4274 need_maketitle = TRUE;
4275
4276 return errmsg;
4277}
4278
4279#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
4280/*
4281 * Process the updated 'undofile' option value.
4282 */
4283 char *
4284did_set_undofile(optset_T *args)
4285{
4286 // Only take action when the option was set.
4287 if (!curbuf->b_p_udf && !p_udf)
4288 return NULL;
4289
4290 // When reset we do not delete the undo file, the option may be set again
4291 // without making any changes in between.
4292 char_u hash[UNDO_HASH_SIZE];
4293 buf_T *save_curbuf = curbuf;
4294
4295 FOR_ALL_BUFFERS(curbuf)
4296 {
4297 // When 'undofile' is set globally: for every buffer, otherwise
4298 // only for the current buffer: Try to read in the undofile,
4299 // if one exists, the buffer wasn't changed and the buffer was
4300 // loaded
4301 if ((curbuf == save_curbuf
4302 || (args->os_flags & OPT_GLOBAL)
4303 || args->os_flags == 0)
4304 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
4305 {
4306#ifdef FEAT_CRYPT
Christian Brabandtaae58342023-04-23 17:50:22 +01004307 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004308 continue;
4309#endif
4310 u_compute_hash(hash);
4311 u_read_undo(NULL, hash, curbuf->b_fname);
4312 }
4313 }
4314 curbuf = save_curbuf;
4315
4316 return NULL;
4317}
4318#endif
4319
4320/*
4321 * Process the new global 'undolevels' option value.
4322 */
4323 static void
4324update_global_undolevels(long value, long old_value)
4325{
4326 // sync undo before 'undolevels' changes
4327
4328 // use the old value, otherwise u_sync() may not work properly
4329 p_ul = old_value;
4330 u_sync(TRUE);
4331 p_ul = value;
4332}
4333
4334/*
4335 * Process the new buffer local 'undolevels' option value.
4336 */
4337 static void
4338update_buflocal_undolevels(long value, long old_value)
4339{
4340 // use the old value, otherwise u_sync() may not work properly
4341 curbuf->b_p_ul = old_value;
4342 u_sync(TRUE);
4343 curbuf->b_p_ul = value;
4344}
4345
4346/*
4347 * Process the new 'undolevels' option value.
4348 */
4349 char *
4350did_set_undolevels(optset_T *args)
4351{
4352 long *pp = (long *)args->os_varp;
4353
4354 if (pp == &p_ul) // global 'undolevels'
4355 update_global_undolevels(args->os_newval.number,
4356 args->os_oldval.number);
4357 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4358 update_buflocal_undolevels(args->os_newval.number,
4359 args->os_oldval.number);
4360
4361 return NULL;
4362}
4363
4364/*
4365 * Process the new 'updatecount' option value.
4366 */
4367 char *
4368did_set_updatecount(optset_T *args)
4369{
4370 long old_value = args->os_oldval.number;
4371 char *errmsg = NULL;
4372
4373 // when 'updatecount' changes from zero to non-zero, open swap files
4374 if (p_uc < 0)
4375 {
4376 errmsg = e_argument_must_be_positive;
4377 p_uc = 100;
4378 }
4379 if (p_uc && !old_value)
4380 ml_open_files();
4381
4382 return errmsg;
4383}
4384
4385/*
4386 * Process the updated 'weirdinvert' option value.
4387 */
4388 char *
4389did_set_weirdinvert(optset_T *args)
4390{
4391 // When 'weirdinvert' changed, set/reset 't_xs'.
4392 // Then set 'weirdinvert' according to value of 't_xs'.
4393 if (p_wiv && !args->os_oldval.boolean)
4394 T_XS = (char_u *)"y";
4395 else if (!p_wiv && args->os_oldval.boolean)
4396 T_XS = empty_option;
4397 p_wiv = (*T_XS != NUL);
4398
4399 return NULL;
4400}
4401
4402/*
4403 * Process the new 'window' option value.
4404 */
4405 char *
4406did_set_window(optset_T *args UNUSED)
4407{
4408 if (p_window < 1)
4409 p_window = 1;
4410 else if (p_window >= Rows)
4411 p_window = Rows - 1;
4412 return NULL;
4413}
4414
4415/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004416 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004418 char *
4419did_set_winheight_helpheight(optset_T *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004421 long *pp = (long *)args->os_varp;
4422 char *errmsg = NULL;
4423
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004424 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004426 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004427 p_wh = 1;
4428 }
4429 if (p_wmh > p_wh)
4430 {
4431 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4432 p_wh = p_wmh;
4433 }
4434 if (p_hh < 0)
4435 {
4436 errmsg = e_argument_must_be_positive;
4437 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 }
4439
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004440 // Change window height NOW
4441 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004443 if (pp == &p_wh && curwin->w_height < p_wh)
4444 win_setheight((int)p_wh);
4445 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
4446 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004449 return errmsg;
4450}
4451
4452/*
4453 * Process the new 'winminheight' option value.
4454 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004455 char *
4456did_set_winminheight(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004457{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004458 char *errmsg = NULL;
4459
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004460 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004462 errmsg = e_argument_must_be_positive;
4463 p_wmh = 0;
4464 }
4465 if (p_wmh > p_wh)
4466 {
4467 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4468 p_wmh = p_wh;
4469 }
4470 win_setminheight();
4471
4472 return errmsg;
4473}
4474
4475/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004476 * Process the new 'winminwidth' option value.
4477 */
4478 char *
4479did_set_winminwidth(optset_T *args UNUSED)
4480{
4481 char *errmsg = NULL;
4482
4483 if (p_wmw < 0)
4484 {
4485 errmsg = e_argument_must_be_positive;
4486 p_wmw = 0;
4487 }
4488 if (p_wmw > p_wiw)
4489 {
4490 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4491 p_wmw = p_wiw;
4492 }
4493 win_setminwidth();
4494
4495 return errmsg;
4496}
4497
4498/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004499 * Process the new 'winwidth' option value.
4500 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004501 char *
4502did_set_winwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004503{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004504 char *errmsg = NULL;
4505
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004506 if (p_wiw < 1)
4507 {
4508 errmsg = e_argument_must_be_positive;
4509 p_wiw = 1;
4510 }
4511 if (p_wmw > p_wiw)
4512 {
4513 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4514 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004516
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004517 // Change window width NOW
4518 if (!ONE_WINDOW && curwin->w_width < p_wiw)
4519 win_setwidth((int)p_wiw);
4520
4521 return errmsg;
4522}
4523
4524/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004525 * Process the updated 'wrap' option value.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004526 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004527 char *
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004528did_set_wrap(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004529{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004530 // If 'wrap' is set, set w_leftcol to zero.
4531 if (curwin->w_p_wrap)
4532 curwin->w_leftcol = 0;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004533 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004534}
4535
4536/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004537 * Set the value of a boolean option, and take care of side effects.
4538 * Returns NULL for success, or an error message for an error.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004539 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004540 static char *
4541set_bool_option(
4542 int opt_idx, // index in options[] table
4543 char_u *varp, // pointer to the option variable
4544 int value, // new value
4545 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004546{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004547 int old_value = *(int *)varp;
4548#if defined(FEAT_EVAL)
4549 int old_global_value = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004551 char *errmsg = NULL;
4552
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004553 // Disallow changing some options from secure mode
4554 if ((secure
4555#ifdef HAVE_SANDBOX
4556 || sandbox != 0
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004557#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004558 ) && (options[opt_idx].flags & P_SECURE))
4559 return e_not_allowed_here;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004560
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004561#if defined(FEAT_EVAL)
4562 // Save the global value before changing anything. This is needed as for
4563 // a global-only option setting the "local value" in fact sets the global
4564 // value (since there is only one value).
4565 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4566 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
4567 OPT_GLOBAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004569
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004570 *(int *)varp = value; // set the new value
4571#ifdef FEAT_EVAL
4572 // Remember where the option was set.
4573 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004574#endif
4575
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004577 need_mouse_correct = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004580 // May set global value for local option.
4581 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4582 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004583
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004584 // Handle side effects of changing a bool option.
4585 if (options[opt_idx].opt_did_set_cb != NULL)
4586 {
4587 optset_T args;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004588
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004589 CLEAR_FIELD(args);
4590 args.os_varp = varp;
4591 args.os_flags = opt_flags;
4592 args.os_oldval.boolean = old_value;
4593 args.os_newval.boolean = value;
4594 args.os_errbuf = NULL;
4595 errmsg = options[opt_idx].opt_did_set_cb(&args);
4596 if (args.os_doskip)
4597 return errmsg;
4598 }
4599
4600 // after handling side effects, call autocommand
4601
4602 options[opt_idx].flags |= P_WAS_SET;
4603
4604#if defined(FEAT_EVAL)
4605 apply_optionset_autocmd(opt_idx, opt_flags,
4606 (long)(old_value ? TRUE : FALSE),
4607 (long)(old_global_value ? TRUE : FALSE),
4608 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004609#endif
4610
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004611 comp_col(); // in case 'ruler' or 'showcmd' changed
4612 if (curwin->w_curswant != MAXCOL
4613 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
4614 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004615
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004616 if ((opt_flags & OPT_NO_REDRAW) == 0)
4617 check_redraw(options[opt_idx].flags);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004618
4619 return errmsg;
4620}
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004621
4622/*
4623 * Check the bounds of numeric options.
4624 */
4625 static char *
4626check_num_option_bounds(
4627 long *pp,
4628 long old_value,
4629 long old_Rows,
4630 long old_Columns,
4631 char *errbuf,
4632 size_t errbuflen,
4633 char *errmsg)
4634{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 if (Rows < min_rows() && full_screen)
4636 {
4637 if (errbuf != NULL)
4638 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004639 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004640 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 errmsg = errbuf;
4642 }
4643 Rows = min_rows();
4644 }
4645 if (Columns < MIN_COLUMNS && full_screen)
4646 {
4647 if (errbuf != NULL)
4648 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004649 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004650 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 errmsg = errbuf;
4652 }
4653 Columns = MIN_COLUMNS;
4654 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004655 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004657 // If the screen (shell) height has been changed, assume it is the
4658 // physical screenheight.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 if (old_Rows != Rows || old_Columns != Columns)
4660 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004661 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 if (updating_screen)
4663 *pp = old_value;
4664 else if (full_screen
4665#ifdef FEAT_GUI
4666 && !gui.starting
4667#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004668 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 set_shellsize((int)Columns, (int)Rows, TRUE);
4670 else
4671 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004672 // Postpone the resizing; check the size and cmdline position for
4673 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 check_shellsize();
4675 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4676 cmdline_row = Rows - p_ch;
4677 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004678 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004679 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 }
4681
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 if (curbuf->b_p_ts <= 0)
4683 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004684 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 curbuf->b_p_ts = 8;
4686 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004687 else if (curbuf->b_p_ts > TABSTOP_MAX)
4688 {
4689 errmsg = e_invalid_argument;
4690 curbuf->b_p_ts = 8;
4691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 if (p_tm < 0)
4693 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004694 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 p_tm = 0;
4696 }
4697 if ((curwin->w_p_scr <= 0
4698 || (curwin->w_p_scr > curwin->w_height
4699 && curwin->w_height > 0))
4700 && full_screen)
4701 {
4702 if (pp == &(curwin->w_p_scr))
4703 {
4704 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004705 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 win_comp_scroll(curwin);
4707 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004708 // If 'scroll' became invalid because of a side effect silently adjust
4709 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 else if (curwin->w_p_scr <= 0)
4711 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004712 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 curwin->w_p_scr = curwin->w_height;
4714 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004715 if (p_hi < 0)
4716 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004717 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004718 p_hi = 0;
4719 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004720 else if (p_hi > 10000)
4721 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004722 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004723 p_hi = 10000;
4724 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004725 if (p_re < 0 || p_re > 2)
4726 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004727 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004728 p_re = 0;
4729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 if (p_report < 0)
4731 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004732 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 p_report = 1;
4734 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004735 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004737 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 p_sj = Rows / 2;
4739 else
4740 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004741 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 p_sj = 1;
4743 }
4744 }
4745 if (p_so < 0 && full_screen)
4746 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004747 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 p_so = 0;
4749 }
4750 if (p_siso < 0 && full_screen)
4751 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004752 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 p_siso = 0;
4754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 if (p_cwh < 1)
4756 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004757 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 p_cwh = 1;
4759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 if (p_ut < 0)
4761 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004762 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 p_ut = 2000;
4764 }
4765 if (p_ss < 0)
4766 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004767 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 p_ss = 0;
4769 }
4770
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004771 return errmsg;
4772}
4773
4774/*
4775 * Set the value of a number option, and take care of side effects.
4776 * Returns NULL for success, or an error message for an error.
4777 */
4778 static char *
4779set_num_option(
4780 int opt_idx, // index in options[] table
4781 char_u *varp, // pointer to the option variable
4782 long value, // new value
4783 char *errbuf, // buffer for error messages
4784 size_t errbuflen, // length of "errbuf"
4785 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4786 // OPT_MODELINE, etc.
4787{
4788 char *errmsg = NULL;
4789 long old_value = *(long *)varp;
4790#if defined(FEAT_EVAL)
4791 long old_global_value = 0; // only used when setting a local and
4792 // global option
4793#endif
4794 long old_Rows = Rows; // remember old Rows
4795 long old_Columns = Columns; // remember old Columns
4796 long *pp = (long *)varp;
4797
4798 // Disallow changing some options from secure mode.
4799 if ((secure
4800#ifdef HAVE_SANDBOX
4801 || sandbox != 0
4802#endif
4803 ) && (options[opt_idx].flags & P_SECURE))
4804 return e_not_allowed_here;
4805
4806#if defined(FEAT_EVAL)
4807 // Save the global value before changing anything. This is needed as for
4808 // a global-only option setting the "local value" in fact sets the global
4809 // value (since there is only one value).
4810 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4811 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4812 OPT_GLOBAL);
4813#endif
4814
4815 *pp = value;
4816#ifdef FEAT_EVAL
4817 // Remember where the option was set.
4818 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4819#endif
4820#ifdef FEAT_GUI
4821 need_mouse_correct = TRUE;
4822#endif
4823
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004824 // Invoke the option specific callback function to validate and apply the
4825 // new value.
4826 if (options[opt_idx].opt_did_set_cb != NULL)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004827 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004828 optset_T args;
4829
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004830 CLEAR_FIELD(args);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004831 args.os_varp = varp;
4832 args.os_flags = opt_flags;
4833 args.os_oldval.number = old_value;
4834 args.os_newval.number = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004835 args.os_errbuf = NULL;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004836 errmsg = options[opt_idx].opt_did_set_cb(&args);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004837 }
4838
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004839 // Check the bounds for numeric options here
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004840 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4841 errbuf, errbuflen, errmsg);
4842
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004843 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4845 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4846
4847 options[opt_idx].flags |= P_WAS_SET;
4848
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004849#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004850 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4851 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004852#endif
4853
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004854 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02004855 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01004856 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004857 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004858 if ((opt_flags & OPT_NO_REDRAW) == 0)
4859 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860
4861 return errmsg;
4862}
4863
4864/*
4865 * Called after an option changed: check if something needs to be redrawn.
4866 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004867 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004868check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004870 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004871 int doclear = (flags & P_RCLR) == P_RCLR;
4872 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004874 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876
4877 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
4878 changed_window_setting();
4879 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004880 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01004881 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004882 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004883 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004884 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004886 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887}
4888
4889/*
4890 * Find index for option 'arg'.
4891 * Return -1 if not found.
4892 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004893 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004894findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895{
4896 int opt_idx;
4897 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004898 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 int is_term_opt;
4900
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004901 // For first call: Initialize the quick-access table.
4902 // It contains the index for the first option that starts with a certain
4903 // letter. There are 26 letters, plus the first "t_" option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 if (quick_tab[1] == 0)
4905 {
4906 p = options[0].fullname;
4907 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4908 {
4909 if (s[0] != p[0])
4910 {
4911 if (s[0] == 't' && s[1] == '_')
4912 quick_tab[26] = opt_idx;
4913 else
4914 quick_tab[CharOrdLow(s[0])] = opt_idx;
4915 }
4916 p = s;
4917 }
4918 }
4919
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004920 // Check for name starting with an illegal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 return -1;
4923
4924 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4925 if (is_term_opt)
4926 opt_idx = quick_tab[26];
4927 else
4928 opt_idx = quick_tab[CharOrdLow(arg[0])];
4929 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4930 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004931 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 break;
4933 }
4934 if (s == NULL && !is_term_opt)
4935 {
4936 opt_idx = quick_tab[CharOrdLow(arg[0])];
4937 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
4938 {
4939 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004940 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 break;
4942 s = NULL;
4943 }
4944 }
4945 if (s == NULL)
4946 opt_idx = -1;
4947 return opt_idx;
4948}
4949
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004950#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
4951 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952/*
4953 * Get the value for an option.
4954 *
4955 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004956 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01004957 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004958 * String option: gov_string, *stringval gets allocated string.
4959 * Hidden Number option: gov_hidden_number.
4960 * Hidden Toggle option: gov_hidden_bool.
4961 * Hidden String option: gov_hidden_string.
4962 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004963 *
4964 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004966 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01004967get_option_value(
4968 char_u *name,
4969 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004970 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004971 int *flagsp,
4972 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973{
4974 int opt_idx;
4975 char_u *varp;
4976
4977 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004978 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01004979 {
4980 int key;
4981
4982 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004983 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004984 {
4985 char_u key_name[2];
4986 char_u *p;
4987
Bram Moolenaar10c75c42021-12-28 20:53:30 +00004988 if (flagsp != NULL)
4989 *flagsp = 0; // terminal option has no flags
4990
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004991 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01004992 if (key < 0)
4993 {
4994 key_name[0] = KEY2TERMCAP0(key);
4995 key_name[1] = KEY2TERMCAP1(key);
4996 }
4997 else
4998 {
4999 key_name[0] = KS_KEY;
5000 key_name[1] = (key & 0xff);
5001 }
5002 p = find_termcode(key_name);
5003 if (p != NULL)
5004 {
5005 if (stringval != NULL)
5006 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005007 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01005008 }
5009 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005010 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01005011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005013 varp = get_varp_scope(&(options[opt_idx]), scope);
5014
5015 if (flagsp != NULL)
5016 // Return the P_xxxx option flags.
5017 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018
5019 if (options[opt_idx].flags & P_STRING)
5020 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005021 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005022 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 if (stringval != NULL)
5024 {
zeertzjq51f0bc32022-05-09 13:33:39 +01005025 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01005026 *stringval = str2special_save(*(char_u **)(varp), FALSE,
5027 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005029 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01005030 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00005031 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01005034 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 *stringval = vim_strsave(*(char_u **)(varp));
5036 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005037 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 }
5039
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005040 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005041 return (options[opt_idx].flags & P_NUM)
5042 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 if (options[opt_idx].flags & P_NUM)
5044 *numval = *(long *)varp;
5045 else
5046 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005047 // Special case: 'modified' is b_changed, but we also want to consider
5048 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 if ((int *)varp == &curbuf->b_changed)
5050 *numval = curbufIsChanged();
5051 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02005052 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005054 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055}
5056#endif
5057
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005058#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005059/*
5060 * Returns the option attributes and its value. Unlike the above function it
5061 * will return either global value or local value of the option depending on
5062 * what was requested, but it will never return global value if it was
5063 * requested to return local one and vice versa. Neither it will return
5064 * buffer-local value if it was requested to return window-local one.
5065 *
5066 * Pretends that option is absent if it is not present in the requested scope
5067 * (i.e. has no global, window-local or buffer-local value depending on
5068 * opt_type). Uses
5069 *
5070 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02005071 * 0 hidden or unknown option, also option that does not have requested
5072 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005073 * see SOPT_* in vim.h for other flags
5074 *
5075 * Possible opt_type values: see SREQ_* in vim.h
5076 */
5077 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005078get_option_value_strict(
5079 char_u *name,
5080 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005081 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01005082 int opt_type,
5083 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005084{
5085 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02005086 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005087 struct vimoption *p;
5088 int r = 0;
5089
5090 opt_idx = findoption(name);
5091 if (opt_idx < 0)
5092 return 0;
5093
5094 p = &(options[opt_idx]);
5095
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005096 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005097 if (p->var == NULL)
5098 return 0;
5099
5100 if (p->flags & P_BOOL)
5101 r |= SOPT_BOOL;
5102 else if (p->flags & P_NUM)
5103 r |= SOPT_NUM;
5104 else if (p->flags & P_STRING)
5105 r |= SOPT_STRING;
5106
5107 if (p->indir == PV_NONE)
5108 {
5109 if (opt_type == SREQ_GLOBAL)
5110 r |= SOPT_GLOBAL;
5111 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005112 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005113 }
5114 else
5115 {
5116 if (p->indir & PV_BOTH)
5117 r |= SOPT_GLOBAL;
5118 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005119 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005120
5121 if (p->indir & PV_WIN)
5122 {
5123 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005124 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005125 else
5126 r |= SOPT_WIN;
5127 }
5128 else if (p->indir & PV_BUF)
5129 {
5130 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005131 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005132 else
5133 r |= SOPT_BUF;
5134 }
5135 }
5136
5137 if (stringval == NULL)
5138 return r;
5139
5140 if (opt_type == SREQ_GLOBAL)
5141 varp = p->var;
5142 else
5143 {
5144 if (opt_type == SREQ_BUF)
5145 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005146 // Special case: 'modified' is b_changed, but we also want to
5147 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005148 if (p->indir == PV_MOD)
5149 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005150 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005151 varp = NULL;
5152 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005153# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005154 else if (p->indir == PV_KEY)
5155 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005156 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005157 *stringval = NULL;
5158 varp = NULL;
5159 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005160# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005161 else
5162 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005163 buf_T *save_curbuf = curbuf;
5164
5165 // only getting a pointer, no need to use aucmd_prepbuf()
5166 curbuf = (buf_T *)from;
5167 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005168 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02005169 curbuf = save_curbuf;
5170 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005171 }
5172 }
5173 else if (opt_type == SREQ_WIN)
5174 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005175 win_T *save_curwin = curwin;
5176
5177 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005178 curbuf = curwin->w_buffer;
5179 varp = get_varp(p);
5180 curwin = save_curwin;
5181 curbuf = curwin->w_buffer;
5182 }
5183 if (varp == p->var)
5184 return (r | SOPT_UNSET);
5185 }
5186
5187 if (varp != NULL)
5188 {
5189 if (p->flags & P_STRING)
5190 *stringval = vim_strsave(*(char_u **)(varp));
5191 else if (p->flags & P_NUM)
5192 *numval = *(long *) varp;
5193 else
5194 *numval = *(int *)varp;
5195 }
5196
5197 return r;
5198}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005199
5200/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005201 * Iterate over options. First argument is a pointer to a pointer to a
5202 * structure inside options[] array, second is option type like in the above
5203 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005204 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005205 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005206 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005207 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005208 * first argument to NULL.
5209 *
5210 * Returns full option name for current option on each call.
5211 */
5212 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005213option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005214{
5215 struct vimoption *ret = NULL;
5216 do
5217 {
5218 if (*option == NULL)
5219 *option = (void *) options;
5220 else if (((struct vimoption *) (*option))->fullname == NULL)
5221 {
5222 *option = NULL;
5223 return NULL;
5224 }
5225 else
5226 *option = (void *) (((struct vimoption *) (*option)) + 1);
5227
5228 ret = ((struct vimoption *) (*option));
5229
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005230 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005231 if (ret->var == NULL)
5232 {
5233 ret = NULL;
5234 continue;
5235 }
5236
5237 switch (opt_type)
5238 {
5239 case SREQ_GLOBAL:
5240 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
5241 ret = NULL;
5242 break;
5243 case SREQ_BUF:
5244 if (!(ret->indir & PV_BUF))
5245 ret = NULL;
5246 break;
5247 case SREQ_WIN:
5248 if (!(ret->indir & PV_WIN))
5249 ret = NULL;
5250 break;
5251 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005252 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005253 return NULL;
5254 }
5255 }
5256 while (ret == NULL);
5257
5258 return (char_u *)ret->fullname;
5259}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005260#endif
5261
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005263 * Return the flags for the option at 'opt_idx'.
5264 */
5265 long_u
5266get_option_flags(int opt_idx)
5267{
5268 return options[opt_idx].flags;
5269}
5270
5271/*
5272 * Set a flag for the option at 'opt_idx'.
5273 */
5274 void
5275set_option_flag(int opt_idx, long_u flag)
5276{
5277 options[opt_idx].flags |= flag;
5278}
5279
5280/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005281 * Returns TRUE if the option at 'opt_idx' is a global option
5282 */
5283 int
5284is_global_option(int opt_idx)
5285{
5286 return options[opt_idx].indir == PV_NONE;
5287}
5288
5289/*
5290 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5291 * local value.
5292 */
5293 int
5294is_global_local_option(int opt_idx)
5295{
5296 return options[opt_idx].indir & PV_BOTH;
5297}
5298
5299/*
5300 * Returns TRUE if the option at 'opt_idx' is a window-local option
5301 */
5302 int
5303is_window_local_option(int opt_idx)
5304{
5305 return options[opt_idx].var == VAR_WIN;
5306}
5307
5308/*
5309 * Returns TRUE if the option at 'opt_idx' is a hidden option
5310 */
5311 int
5312is_hidden_option(int opt_idx)
5313{
5314 return options[opt_idx].var == NULL;
5315}
5316
5317#if defined(FEAT_CRYPT) || defined(PROTO)
5318/*
5319 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5320 */
5321 int
5322is_crypt_key_option(int opt_idx)
5323{
5324 return options[opt_idx].indir == PV_KEY;
5325}
5326#endif
5327
5328/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 * Set the value of option "name".
5330 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005331 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005332 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005334 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005335set_option_value(
5336 char_u *name,
5337 long number,
5338 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005339 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340{
5341 int opt_idx;
5342 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005343 long_u flags;
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005344 static char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345
5346 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005347 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005348 {
5349 int key;
5350
5351 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005352 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005353 {
5354 char_u key_name[2];
5355
5356 if (key < 0)
5357 {
5358 key_name[0] = KEY2TERMCAP0(key);
5359 key_name[1] = KEY2TERMCAP1(key);
5360 }
5361 else
5362 {
5363 key_name[0] = KS_KEY;
5364 key_name[1] = (key & 0xff);
5365 }
5366 add_termcode(key_name, string, FALSE);
5367 if (full_screen)
5368 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005369 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005370 return NULL;
5371 }
5372
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005373 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 else
5376 {
5377 flags = options[opt_idx].flags;
5378#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005379 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005381 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005382 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005383 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005386 if (flags & P_STRING)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005387 return set_string_option(opt_idx, string, opt_flags, errbuf);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005388
5389 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5390 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005392 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005394 int idx;
5395
5396 // Either we are given a string or we are setting option
5397 // to zero.
5398 for (idx = 0; string[idx] == '0'; ++idx)
5399 ;
5400 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005401 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005402 // There's another character after zeros or the string
5403 // is empty. In both cases, we are trying to set a
5404 // num option using a string.
5405 semsg(_(e_number_required_after_str_equal_str),
5406 name, string);
5407 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005408
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005411 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005412 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005413 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005414 errbuf, sizeof(errbuf), opt_flags);
5415 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005416 else
5417 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005419
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005421 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422}
5423
5424/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005425 * Call set_option_value() and when an error is returned report it.
5426 */
5427 void
5428set_option_value_give_err(
5429 char_u *name,
5430 long number,
5431 char_u *string,
5432 int opt_flags) // OPT_LOCAL or 0 (both)
5433{
5434 char *errmsg = set_option_value(name, number, string, opt_flags);
5435
5436 if (errmsg != NULL)
5437 emsg(_(errmsg));
5438}
5439
5440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 * Get the terminal code for a terminal option.
5442 * Returns NULL when not found.
5443 */
5444 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005445get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446{
5447 int opt_idx;
5448 char_u *varp;
5449
5450 if (tname[0] != 't' || tname[1] != '_' ||
5451 tname[2] == NUL || tname[3] == NUL)
5452 return NULL;
5453 if ((opt_idx = findoption(tname)) >= 0)
5454 {
5455 varp = get_varp(&(options[opt_idx]));
5456 if (varp != NULL)
5457 varp = *(char_u **)(varp);
5458 return varp;
5459 }
5460 return find_termcode(tname + 2);
5461}
5462
5463 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005464get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465{
5466 int i;
5467
5468 i = findoption((char_u *)"hl");
5469 if (i >= 0)
5470 return options[i].def_val[VI_DEFAULT];
5471 return (char_u *)NULL;
5472}
5473
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005474 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005475get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005476{
5477 int i;
5478
5479 i = findoption((char_u *)"enc");
5480 if (i >= 0)
5481 return options[i].def_val[VI_DEFAULT];
5482 return (char_u *)NULL;
5483}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005484
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005485#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005486 int
5487is_option_allocated(char *name)
5488{
5489 int idx = findoption((char_u *)name);
5490
5491 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5492}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005493#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005494
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495/*
5496 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005497 * When "has_lt" is true there is a '<' before "*arg_arg".
5498 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 */
5500 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005501find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005503 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005505 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005507 // Don't use get_special_key_code() for t_xx, we don't want it to call
5508 // add_termcap_entry().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5510 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005511 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005513 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005515 key = find_special_key(&arg, &modifiers,
5516 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005517 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 key = 0;
5519 }
5520 return key;
5521}
5522
5523/*
5524 * if 'all' == 0: show changed options
5525 * if 'all' == 1: show all normal options
5526 * if 'all' == 2: show all terminal options
5527 */
5528 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005529showoptions(
5530 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005531 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532{
5533 struct vimoption *p;
5534 int col;
5535 int isterm;
5536 char_u *varp;
5537 struct vimoption **items;
5538 int item_count;
5539 int run;
5540 int row, rows;
5541 int cols;
5542 int i;
5543 int len;
5544
5545#define INC 20
5546#define GAP 3
5547
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005548 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 if (items == NULL)
5550 return;
5551
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005552 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005554 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005556 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005558 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005560 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005562 // Do the loop two times:
5563 // 1. display the short items
5564 // 2. display the long items (only strings and numbers)
5565 // When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 for (run = 1; run <= 2 && !got_int; ++run)
5567 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005568 // collect the items in items[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 item_count = 0;
5570 for (p = &options[0]; p->fullname != NULL; p++)
5571 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005572 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005573 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005574 continue;
5575
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 varp = NULL;
5577 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005578 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 {
5580 if (p->indir != PV_NONE && !isterm)
5581 varp = get_varp_scope(p, opt_flags);
5582 }
5583 else
5584 varp = get_varp(p);
5585 if (varp != NULL
5586 && ((all == 2 && isterm)
5587 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005588 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005590 if (opt_flags & OPT_ONECOLUMN)
5591 len = Columns;
5592 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005593 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 else
5595 {
5596 option_value2string(p, opt_flags);
5597 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5598 }
5599 if ((len <= INC - GAP && run == 1) ||
5600 (len > INC - GAP && run == 2))
5601 items[item_count++] = p;
5602 }
5603 }
5604
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005605 // display the items
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 if (run == 1)
5607 {
5608 cols = (Columns + GAP - 3) / INC;
5609 if (cols == 0)
5610 cols = 1;
5611 rows = (item_count + cols - 1) / cols;
5612 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005613 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 rows = item_count;
5615 for (row = 0; row < rows && !got_int; ++row)
5616 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005617 msg_putchar('\n'); // go to next line
5618 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 break;
5620 col = 0;
5621 for (i = row; i < item_count; i += rows)
5622 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005623 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 showoneopt(items[i], opt_flags);
5625 col += INC;
5626 }
5627 out_flush();
5628 ui_breakcheck();
5629 }
5630 }
5631 vim_free(items);
5632}
5633
5634/*
5635 * Return TRUE if option "p" has its default value.
5636 */
5637 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005638optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639{
5640 int dvi;
5641
5642 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005643 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005644 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005646 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005648 // the cast to long is required for Manx C, long_i is
5649 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005650 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005651 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5653}
5654
5655/*
5656 * showoneopt: show the value of one option
5657 * must not be called with a hidden option!
5658 */
5659 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005660showoneopt(
5661 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005662 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005664 char_u *varp;
5665 int save_silent = silent_mode;
5666
5667 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005668 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669
5670 varp = get_varp_scope(p, opt_flags);
5671
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005672 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5674 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005675 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005677 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005679 msg_puts(" ");
5680 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 if (!(p->flags & P_BOOL))
5682 {
5683 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005684 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 option_value2string(p, opt_flags);
5686 msg_outtrans(NameBuff);
5687 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005688
5689 silent_mode = save_silent;
5690 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691}
5692
5693/*
5694 * Write modified options as ":set" commands to a file.
5695 *
5696 * There are three values for "opt_flags":
5697 * OPT_GLOBAL: Write global option values and fresh values of
5698 * buffer-local options (used for start of a session
5699 * file).
5700 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5701 * curwin (used for a vimrc file).
5702 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5703 * and local values for window-local options of
5704 * curwin. Local values are also written when at the
5705 * default value, because a modeline or autocommand
5706 * may have set them when doing ":edit file" and the
5707 * user has set them back at the default or fresh
5708 * value.
5709 * When "local_only" is TRUE, don't write fresh
5710 * values, only local values (for ":mkview").
5711 * (fresh value = value used for a new buffer or window for a local option).
5712 *
5713 * Return FAIL on error, OK otherwise.
5714 */
5715 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005716makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717{
5718 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005719 char_u *varp; // currently used value
5720 char_u *varp_fresh; // local value
5721 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 char *cmd;
5723 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005724 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725
5726 /*
5727 * The options that don't have a default (terminal name, columns, lines)
5728 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005729 * Do the loop over "options[]" twice: once for options with the
5730 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005732 for (pri = 1; pri >= 0; --pri)
5733 {
5734 for (p = &options[0]; !istermoption(p); p++)
5735 if (!(p->flags & P_NO_MKRC)
5736 && !istermoption(p)
5737 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005739 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5741 continue;
5742
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005743 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5744 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5746 continue;
5747
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005748 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005750 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 continue;
5752
Bram Moolenaard23b7142021-04-17 21:04:34 +02005753 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5754 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005755 continue;
5756
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 round = 2;
5758 if (p->indir != PV_NONE)
5759 {
5760 if (p->var == VAR_WIN)
5761 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005762 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 if (!(opt_flags & OPT_LOCAL))
5764 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005765 // When fresh value of window-local option is not at the
5766 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5768 {
5769 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005770 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 {
5772 round = 1;
5773 varp_local = varp;
5774 varp = varp_fresh;
5775 }
5776 }
5777 }
5778 }
5779
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005780 // Round 1: fresh value for window-local options.
5781 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 for ( ; round <= 2; varp = varp_local, ++round)
5783 {
5784 if (round == 1 || (opt_flags & OPT_GLOBAL))
5785 cmd = "set";
5786 else
5787 cmd = "setlocal";
5788
5789 if (p->flags & P_BOOL)
5790 {
5791 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5792 return FAIL;
5793 }
5794 else if (p->flags & P_NUM)
5795 {
5796 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5797 return FAIL;
5798 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005799 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005801 int do_endif = FALSE;
5802
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005803 // Don't set 'syntax' and 'filetype' again if the value is
5804 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005805 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005806#if defined(FEAT_SYN_HL)
5807 p->indir == PV_SYN ||
5808#endif
5809 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 {
5811 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5812 *(char_u **)(varp)) < 0
5813 || put_eol(fd) < 0)
5814 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005815 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 }
5817 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005818 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005820 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 {
5822 if (put_line(fd, "endif") == FAIL)
5823 return FAIL;
5824 }
5825 }
5826 }
5827 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 return OK;
5830}
5831
5832#if defined(FEAT_FOLDING) || defined(PROTO)
5833/*
5834 * Generate set commands for the local fold options only. Used when
5835 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5836 */
5837 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005838makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005840 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005842 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 == FAIL
5844# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005845 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005847 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 == FAIL
5849 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5850 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5851 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5852 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5853 )
5854 return FAIL;
5855
5856 return OK;
5857}
5858#endif
5859
5860 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005861put_setstring(
5862 FILE *fd,
5863 char *cmd,
5864 char *name,
5865 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005866 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867{
5868 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005869 char_u *buf = NULL;
5870 char_u *part = NULL;
5871 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872
5873 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5874 return FAIL;
5875 if (*valuep != NULL)
5876 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005877 // Output 'pastetoggle' as key names. For other
5878 // options some characters have to be escaped with
5879 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 if (valuep == &p_pt)
5881 {
5882 s = *valuep;
5883 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005884 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 return FAIL;
5886 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005887 // expand the option value, replace $HOME by ~
5888 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005890 int size = (int)STRLEN(*valuep) + 1;
5891
5892 // replace home directory in the whole option value into "buf"
5893 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005894 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005895 goto fail;
5896 home_replace(NULL, *valuep, buf, size, FALSE);
5897
5898 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005899 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005900 // can be expanded when read back.
5901 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5902 && vim_strchr(*valuep, ',') != NULL)
5903 {
5904 part = alloc(size);
5905 if (part == NULL)
5906 goto fail;
5907
5908 // write line break to clear the option, e.g. ':set rtp='
5909 if (put_eol(fd) == FAIL)
5910 goto fail;
5911
5912 p = buf;
5913 while (*p != NUL)
5914 {
5915 // for each comma separated option part, append value to
5916 // the option, :set rtp+=value
5917 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5918 goto fail;
5919 (void)copy_option_part(&p, part, size, ",");
5920 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5921 goto fail;
5922 }
5923 vim_free(buf);
5924 vim_free(part);
5925 return OK;
5926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005928 {
5929 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005931 }
5932 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 }
5934 else if (put_escstr(fd, *valuep, 2) == FAIL)
5935 return FAIL;
5936 }
5937 if (put_eol(fd) < 0)
5938 return FAIL;
5939 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005940fail:
5941 vim_free(buf);
5942 vim_free(part);
5943 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944}
5945
5946 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005947put_setnum(
5948 FILE *fd,
5949 char *cmd,
5950 char *name,
5951 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952{
5953 long wc;
5954
5955 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5956 return FAIL;
5957 if (wc_use_keyname((char_u *)valuep, &wc))
5958 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005959 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5961 return FAIL;
5962 }
5963 else if (fprintf(fd, "%ld", *valuep) < 0)
5964 return FAIL;
5965 if (put_eol(fd) < 0)
5966 return FAIL;
5967 return OK;
5968}
5969
5970 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005971put_setbool(
5972 FILE *fd,
5973 char *cmd,
5974 char *name,
5975 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005977 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005978 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5980 || put_eol(fd) < 0)
5981 return FAIL;
5982 return OK;
5983}
5984
5985/*
5986 * Clear all the terminal options.
5987 * If the option has been allocated, free the memory.
5988 * Terminal options are never hidden or indirect.
5989 */
5990 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005991clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005993 // Reset a few things before clearing the old options. This may cause
5994 // outputting a few things that the terminal doesn't understand, but the
5995 // screen will be cleared later, so this is OK.
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005996 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005997 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005999 // When starting the GUI close the display opened for the clipboard.
6000 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 if (gui.starting)
6002 clear_xterm_clip();
6003#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006004 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006006 free_termoptions();
6007}
6008
6009 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006010free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006011{
6012 struct vimoption *p;
6013
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006014 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 if (istermoption(p))
6016 {
6017 if (p->flags & P_ALLOCED)
6018 free_string_option(*(char_u **)(p->var));
6019 if (p->flags & P_DEF_ALLOCED)
6020 free_string_option(p->def_val[VI_DEFAULT]);
6021 *(char_u **)(p->var) = empty_option;
6022 p->def_val[VI_DEFAULT] = empty_option;
6023 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006024#ifdef FEAT_EVAL
6025 // remember where the option was cleared
6026 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
6027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 }
6029 clear_termcodes();
6030}
6031
6032/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00006033 * Free the string for one term option, if it was allocated.
6034 * Set the string to empty_option and clear allocated flag.
6035 * "var" points to the option value.
6036 */
6037 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006038free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00006039{
6040 struct vimoption *p;
6041
6042 for (p = &options[0]; p->fullname != NULL; p++)
6043 if (p->var == var)
6044 {
6045 if (p->flags & P_ALLOCED)
6046 free_string_option(*(char_u **)(p->var));
6047 *(char_u **)(p->var) = empty_option;
6048 p->flags &= ~P_ALLOCED;
6049 break;
6050 }
6051}
6052
6053/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 * Set the terminal option defaults to the current value.
6055 * Used after setting the terminal name.
6056 */
6057 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006058set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059{
6060 struct vimoption *p;
6061
6062 for (p = &options[0]; p->fullname != NULL; p++)
6063 {
6064 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
6065 {
6066 if (p->flags & P_DEF_ALLOCED)
6067 {
6068 free_string_option(p->def_val[VI_DEFAULT]);
6069 p->flags &= ~P_DEF_ALLOCED;
6070 }
6071 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
6072 if (p->flags & P_ALLOCED)
6073 {
6074 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006075 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 }
6077 }
6078 }
6079}
6080
6081/*
6082 * return TRUE if 'p' starts with 't_'
6083 */
6084 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006085istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086{
6087 return (p->fullname[0] == 't' && p->fullname[1] == '_');
6088}
6089
Bram Moolenaardac13472019-09-16 21:06:21 +02006090/*
6091 * Returns TRUE if the option at 'opt_idx' starts with 't_'
6092 */
6093 int
6094istermoption_idx(int opt_idx)
6095{
6096 return istermoption(&options[opt_idx]);
6097}
6098
Bram Moolenaar113e1072019-01-20 15:30:40 +01006099#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006101 * Unset local option value, similar to ":set opt<".
6102 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006103 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006104unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006105{
6106 struct vimoption *p;
6107 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006108 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006109
6110 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02006111 if (opt_idx < 0)
6112 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006113 p = &(options[opt_idx]);
6114
6115 switch ((int)p->indir)
6116 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006117 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006118 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006119 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006120 break;
6121 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006122 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006123 break;
6124 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006125 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006126 break;
6127 case PV_AR:
6128 buf->b_p_ar = -1;
6129 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006130 case PV_BKC:
6131 clear_string_option(&buf->b_p_bkc);
6132 buf->b_bkc_flags = 0;
6133 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006134 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006135 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006136 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006137 case PV_TC:
6138 clear_string_option(&buf->b_p_tc);
6139 buf->b_tc_flags = 0;
6140 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006141 case PV_SISO:
6142 curwin->w_p_siso = -1;
6143 break;
6144 case PV_SO:
6145 curwin->w_p_so = -1;
6146 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006147# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006148 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006149 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006150 break;
6151 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006152 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006153 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006154# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006155 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006156 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006157 break;
6158 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006159 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006160 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006161# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006162 case PV_TSRFU:
6163 clear_string_option(&buf->b_p_tsrfu);
6164 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006165# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006166 case PV_FP:
6167 clear_string_option(&buf->b_p_fp);
6168 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006169# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006170 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006171 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006172 break;
6173 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006174 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006175 break;
6176 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006177 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006178 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006179# endif
6180# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006181 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006182 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006183 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006184# endif
6185# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006186 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006187 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006188 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006189# endif
6190# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006191 case PV_SBR:
6192 clear_string_option(&((win_T *)from)->w_p_sbr);
6193 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006194# endif
6195# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006196 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006197 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006198 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006199# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006200 case PV_UL:
6201 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6202 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006203 case PV_LW:
6204 clear_string_option(&buf->b_p_lw);
6205 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006206 case PV_MENC:
6207 clear_string_option(&buf->b_p_menc);
6208 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006209 case PV_LCS:
6210 clear_string_option(&((win_T *)from)->w_p_lcs);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006211 set_listchars_option((win_T *)from, ((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006212 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006213 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006214 case PV_FCS:
6215 clear_string_option(&((win_T *)from)->w_p_fcs);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006216 set_fillchars_option((win_T *)from, ((win_T *)from)->w_p_fcs, TRUE);
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
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006424 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006425#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6427#endif
6428#ifdef FEAT_RIGHTLEFT
6429 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6430 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6431#endif
6432 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006433 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6435#ifdef FEAT_LINEBREAK
6436 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006437 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6438 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006440 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006442 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006443#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006444 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6445 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6446#endif
6447#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006448 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6449 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6450 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452
6453 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6454 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6457 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6459 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6461 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6462 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006463 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466#ifdef FEAT_FOLDING
6467 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006470#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006471 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006473#ifdef FEAT_COMPL_FUNC
6474 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006475 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006476#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006477#ifdef FEAT_EVAL
6478 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6479#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006480 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006482 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006488 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6490 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6491 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6492 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6493#ifdef FEAT_FIND_ID
6494# ifdef FEAT_EVAL
6495 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6496# endif
6497#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006498#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6500 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6501#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006502#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006503 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505#ifdef FEAT_CRYPT
6506 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006509 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6511 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6512 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6513 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6514 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006516 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6523#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006524 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006525 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6526#endif
6527#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006528 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6529 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6530 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006531 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532#endif
6533 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6534 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6535 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6536 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006537#ifdef FEAT_PERSISTENT_UNDO
6538 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6541#ifdef FEAT_KEYMAP
6542 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6543#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006544#ifdef FEAT_SIGNS
6545 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6546#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006547#ifdef FEAT_VARTABS
6548 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6549 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6550#endif
RestorerZ68ebcee2023-05-31 17:12:14 +01006551 default: iemsg(e_get_varp_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006553 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 return (char_u *)&(curbuf->b_p_wm);
6555}
6556
6557/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006558 * Return a pointer to the variable for option at 'opt_idx'
6559 */
6560 char_u *
6561get_option_var(int opt_idx)
6562{
6563 return options[opt_idx].var;
6564}
6565
Dominique Pelle748b3082022-01-08 12:41:16 +00006566#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006567/*
6568 * Return the full name of the option at 'opt_idx'
6569 */
6570 char_u *
6571get_option_fullname(int opt_idx)
6572{
6573 return (char_u *)options[opt_idx].fullname;
6574}
Dominique Pelle748b3082022-01-08 12:41:16 +00006575#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006576
6577/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00006578 * Return the did_set callback function for the option at 'opt_idx'
6579 */
6580 opt_did_set_cb_T
6581get_option_did_set_cb(int opt_idx)
6582{
6583 return options[opt_idx].opt_did_set_cb;
6584}
6585
6586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 * Get the value of 'equalprg', either the buffer-local one or the global one.
6588 */
6589 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006590get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591{
6592 if (*curbuf->b_p_ep == NUL)
6593 return p_ep;
6594 return curbuf->b_p_ep;
6595}
6596
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597/*
6598 * Copy options from one window to another.
6599 * Used when splitting a window.
6600 */
6601 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006602win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603{
6604 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6605 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006606 after_copy_winopt(wp_to);
6607}
6608
6609/*
6610 * After copying window options: update variables depending on options.
6611 */
6612 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006613after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006614{
6615#ifdef FEAT_LINEBREAK
6616 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006617#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006618#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006619 fill_culopt_flags(NULL, wp);
6620 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006621#endif
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006622 set_listchars_option(wp, wp->w_p_lcs, TRUE);
6623 set_fillchars_option(wp, wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006624}
6625
6626 static char_u *
6627copy_option_val(char_u *val)
6628{
6629 if (val == empty_option)
6630 return empty_option; // no need to allocate memory
6631 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633
6634/*
6635 * Copy the options from one winopt_T to another.
6636 * Doesn't free the old option values in "to", use clear_winopt() for that.
6637 * The 'scroll' option is not copied, because it depends on the window height.
6638 * The 'previewwindow' option is reset, there can be only one preview window.
6639 */
6640 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006641copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642{
6643#ifdef FEAT_ARABIC
6644 to->wo_arab = from->wo_arab;
6645#endif
6646 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006647 to->wo_lcs = copy_option_val(from->wo_lcs);
6648 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006650 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006651 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006652 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006653#ifdef FEAT_LINEBREAK
6654 to->wo_nuw = from->wo_nuw;
6655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656#ifdef FEAT_RIGHTLEFT
6657 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006658 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006660#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006661 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006662#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006663#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006664 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006667#ifdef FEAT_DIFF
6668 to->wo_wrap_save = from->wo_wrap_save;
6669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670#ifdef FEAT_LINEBREAK
6671 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006672 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006673 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006675 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006677 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006678 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006679 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006680 to->wo_crb_save = from->wo_crb_save;
Christian Brabandt4a8eb6e2023-08-13 19:43:42 +02006681 to->wo_siso = from->wo_siso;
6682 to->wo_so = from->wo_so;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006683#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006684 to->wo_spell = from->wo_spell;
6685#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006686#ifdef FEAT_SYN_HL
6687 to->wo_cuc = from->wo_cuc;
6688 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006689 to->wo_culopt = copy_option_val(from->wo_culopt);
6690 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692#ifdef FEAT_DIFF
6693 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006694 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006696#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006697 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006698 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006699#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006700#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006701 to->wo_twk = copy_option_val(from->wo_twk);
6702 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704#ifdef FEAT_FOLDING
6705 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006706 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006708 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006709 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 to->wo_fml = from->wo_fml;
6711 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006712 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006713 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006714 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006715 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 to->wo_fdn = from->wo_fdn;
6717# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006718 to->wo_fde = copy_option_val(from->wo_fde);
6719 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006721 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006723#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006724 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006725#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006726
6727#ifdef FEAT_EVAL
6728 // Copy the script context so that we know where the value was last set.
6729 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6730 sizeof(to->wo_script_ctx));
6731#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006732 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733}
6734
6735/*
6736 * Check string options in a window for a NULL value.
6737 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006738 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006739check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740{
6741 check_winopt(&win->w_onebuf_opt);
6742 check_winopt(&win->w_allbuf_opt);
6743}
6744
6745/*
6746 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6747 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006748 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006749check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750{
6751#ifdef FEAT_FOLDING
6752 check_string_option(&wop->wo_fdi);
6753 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006754 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755# ifdef FEAT_EVAL
6756 check_string_option(&wop->wo_fde);
6757 check_string_option(&wop->wo_fdt);
6758# endif
6759 check_string_option(&wop->wo_fmr);
6760#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006761#ifdef FEAT_SIGNS
6762 check_string_option(&wop->wo_scl);
6763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764#ifdef FEAT_RIGHTLEFT
6765 check_string_option(&wop->wo_rlc);
6766#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006767#ifdef FEAT_LINEBREAK
6768 check_string_option(&wop->wo_sbr);
6769#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006770#ifdef FEAT_STL_OPT
6771 check_string_option(&wop->wo_stl);
6772#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006773#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006774 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006775 check_string_option(&wop->wo_cc);
6776#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006777#ifdef FEAT_CONCEAL
6778 check_string_option(&wop->wo_cocu);
6779#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006780#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006781 check_string_option(&wop->wo_twk);
6782 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006783#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006784#ifdef FEAT_LINEBREAK
6785 check_string_option(&wop->wo_briopt);
6786#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006787 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006788 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006789 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006790 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791}
6792
6793/*
6794 * Free the allocated memory inside a winopt_T.
6795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006797clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798{
6799#ifdef FEAT_FOLDING
6800 clear_string_option(&wop->wo_fdi);
6801 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006802 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803# ifdef FEAT_EVAL
6804 clear_string_option(&wop->wo_fde);
6805 clear_string_option(&wop->wo_fdt);
6806# endif
6807 clear_string_option(&wop->wo_fmr);
6808#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006809#ifdef FEAT_SIGNS
6810 clear_string_option(&wop->wo_scl);
6811#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006812#ifdef FEAT_LINEBREAK
6813 clear_string_option(&wop->wo_briopt);
6814#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006815 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816#ifdef FEAT_RIGHTLEFT
6817 clear_string_option(&wop->wo_rlc);
6818#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006819#ifdef FEAT_LINEBREAK
6820 clear_string_option(&wop->wo_sbr);
6821#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006822#ifdef FEAT_STL_OPT
6823 clear_string_option(&wop->wo_stl);
6824#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006825#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006826 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006827 clear_string_option(&wop->wo_cc);
6828#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006829#ifdef FEAT_CONCEAL
6830 clear_string_option(&wop->wo_cocu);
6831#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006832#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006833 clear_string_option(&wop->wo_twk);
6834 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006835#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006836 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006837 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006838 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839}
6840
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006841#ifdef FEAT_EVAL
6842// Index into the options table for a buffer-local option enum.
6843static int buf_opt_idx[BV_COUNT];
6844# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6845
6846/*
6847 * Initialize buf_opt_idx[] if not done already.
6848 */
6849 static void
6850init_buf_opt_idx(void)
6851{
6852 static int did_init_buf_opt_idx = FALSE;
6853 int i;
6854
6855 if (did_init_buf_opt_idx)
6856 return;
6857 did_init_buf_opt_idx = TRUE;
6858 for (i = 0; !istermoption_idx(i); i++)
6859 if (options[i].indir & PV_BUF)
6860 buf_opt_idx[options[i].indir & PV_MASK] = i;
6861}
6862#else
6863# define COPY_OPT_SCTX(buf, bv)
6864#endif
6865
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866/*
6867 * Copy global option values to local options for one buffer.
6868 * Used when creating a new buffer and sometimes when entering a buffer.
6869 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006870 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6872 * appropriate.
6873 * BCO_NOHELP Don't copy the values to a help buffer.
6874 */
6875 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006876buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877{
6878 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006879 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 int dont_do_help;
6881 int did_isk = FALSE;
6882
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006883 // Skip this when the option defaults have not been set yet. Happens when
6884 // main() allocates the first buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 if (p_cpo != NULL)
6886 {
6887 /*
6888 * Always copy when entering and 'cpo' contains 'S'.
6889 * Don't copy when already initialized.
6890 * Don't copy when 'cpo' contains 's' and not entering.
6891 * 'S' BCO_ENTER initialized 's' should_copy
6892 * yes yes X X TRUE
6893 * yes no yes X FALSE
6894 * no X yes X FALSE
6895 * X no no yes FALSE
6896 * X no no no TRUE
6897 * no yes no X TRUE
6898 */
6899 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6900 && (buf->b_p_initialized
6901 || (!(flags & BCO_ENTER)
6902 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6903 should_copy = FALSE;
6904
6905 if (should_copy || (flags & BCO_ALWAYS))
6906 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006907#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006908 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006909 init_buf_opt_idx();
6910#endif
6911 // Don't copy the options specific to a help buffer when
6912 // BCO_NOHELP is given or the options were initialized already
6913 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6915 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006916 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 {
6918 save_p_isk = buf->b_p_isk;
6919 buf->b_p_isk = NULL;
6920 }
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006921 // Always free the allocated strings. If not already initialized,
6922 // reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 if (!buf->b_p_initialized)
6924 {
6925 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006926 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006929 switch (*p_ffs)
6930 {
6931 case 'm':
6932 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6933 case 'd':
6934 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6935 case 'u':
6936 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6937 default:
6938 buf->b_p_ff = vim_strsave(p_ff);
6939 }
6940 if (buf->b_p_ff != NULL)
6941 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 buf->b_p_bh = empty_option;
6943 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 }
6945 else
6946 free_buf_options(buf, FALSE);
6947
6948 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006949 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 buf->b_p_ai_nopaste = p_ai_nopaste;
6951 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006952 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006954 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 buf->b_p_tw_nopaste = p_tw_nopaste;
6956 buf->b_p_tw_nobin = p_tw_nobin;
6957 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006958 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 buf->b_p_wm_nopaste = p_wm_nopaste;
6960 buf->b_p_wm_nobin = p_wm_nobin;
6961 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006962 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006963 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006964 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006965 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006966 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006968 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006970 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006972 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 buf->b_p_ml_nobin = p_ml_nobin;
6974 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006975 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006976 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006977 buf->b_p_swf = FALSE;
6978 else
6979 {
6980 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006981 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006984 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006985#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006986 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006987 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006989#ifdef FEAT_COMPL_FUNC
6990 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006991 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006992 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006993 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006994 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006995 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006996#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006997#ifdef FEAT_EVAL
6998 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006999 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007000 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02007001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007003 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007005#ifdef FEAT_VARTABS
7006 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007007 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007008 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007009 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007010 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007011 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007012 buf->b_p_vsts_nopaste = p_vsts_nopaste
7013 ? vim_strsave(p_vsts_nopaste) : NULL;
7014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007016 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007018 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019#ifdef FEAT_FOLDING
7020 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007021 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022#endif
7023 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007024 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007025 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007026 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02007027 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
7028 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007030 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007032 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007034 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007036 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007037
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007039 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007041 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007043 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01007044 buf->b_p_cinsd = vim_strsave(p_cinsd);
7045 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01007046 buf->b_p_lop = vim_strsave(p_lop);
7047 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007048
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007049 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007052 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007054 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007056 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007058 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00007060 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007061 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01007062 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007063#endif
7064#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02007065 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007066 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007067 (void)compile_cap_prog(&buf->b_s);
7068 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007069 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007070 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007071 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02007072 buf->b_s.b_p_spo = vim_strsave(p_spo);
7073 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007075#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007077 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007079 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01007081 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007082#if defined(FEAT_EVAL)
7083 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007084 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086#ifdef FEAT_CRYPT
7087 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007088 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007091 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092#ifdef FEAT_KEYMAP
7093 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007094 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 buf->b_kmap_state |= KEYMAP_INIT;
7096#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007097#ifdef FEAT_TERMINAL
7098 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007099 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007100#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007101 // This isn't really an option, but copying the langmap and IME
7102 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007104 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007106 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007108 // options that are normally global but also have a local value
7109 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01007111 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007112 buf->b_p_bkc = empty_option;
7113 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114#ifdef FEAT_QUICKFIX
7115 buf->b_p_gp = empty_option;
7116 buf->b_p_mp = empty_option;
7117 buf->b_p_efm = empty_option;
7118#endif
7119 buf->b_p_ep = empty_option;
7120 buf->b_p_kp = empty_option;
7121 buf->b_p_path = empty_option;
7122 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007123 buf->b_p_tc = empty_option;
7124 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125#ifdef FEAT_FIND_ID
7126 buf->b_p_def = empty_option;
7127 buf->b_p_inc = empty_option;
7128# ifdef FEAT_EVAL
7129 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007130 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131# endif
7132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 buf->b_p_dict = empty_option;
7134 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01007135#ifdef FEAT_COMPL_FUNC
7136 buf->b_p_tsrfu = empty_option;
7137#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007138 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007139 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007140#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7141 buf->b_p_bexpr = empty_option;
7142#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007143#if defined(FEAT_CRYPT)
7144 buf->b_p_cm = empty_option;
7145#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007146#ifdef FEAT_PERSISTENT_UNDO
7147 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007148 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007149#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007150 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007151 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007153 // Don't copy the options set by ex_help(), use the saved values,
7154 // when going from a help buffer to a non-help buffer.
7155 // Don't touch these at all when BCO_NOHELP is used and going from
7156 // or to a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007158 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007160#ifdef FEAT_VARTABS
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007161 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007162 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007163 else
7164 buf->b_p_vts_array = NULL;
7165#endif
7166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 else
7168 {
7169 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007170 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 did_isk = TRUE;
7172 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00007173 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007174#ifdef FEAT_VARTABS
7175 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007176 COPY_OPT_SCTX(buf, BV_VTS);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007177 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007178 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007179 else
7180 buf->b_p_vts_array = NULL;
7181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 if (buf->b_p_bt[0] == 'h')
7184 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007186 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 }
7188 }
7189
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007190 // When the options should be copied (ignoring BCO_ALWAYS), set the
7191 // flag that indicates that the options have been initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 if (should_copy)
7193 buf->b_p_initialized = TRUE;
7194 }
7195
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007196 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 if (did_isk)
7198 (void)buf_init_chartab(buf, FALSE);
7199}
7200
7201/*
7202 * Reset the 'modifiable' option and its default value.
7203 */
7204 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007205reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206{
7207 int opt_idx;
7208
7209 curbuf->b_p_ma = FALSE;
7210 p_ma = FALSE;
7211 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007212 if (opt_idx >= 0)
7213 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214}
7215
7216/*
7217 * Set the global value for 'iminsert' to the local value.
7218 */
7219 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007220set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221{
7222 p_iminsert = curbuf->b_p_iminsert;
7223}
7224
7225/*
7226 * Set the global value for 'imsearch' to the local value.
7227 */
7228 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007229set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230{
7231 p_imsearch = curbuf->b_p_imsearch;
7232}
7233
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234static int expand_option_idx = -1;
7235static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7236static int expand_option_flags = 0;
7237
7238 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007239set_context_in_set_cmd(
7240 expand_T *xp,
7241 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007242 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243{
7244 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007245 long_u flags = 0; // init for GCC
7246 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 char_u *p;
7248 char_u *s;
7249 int is_term_option = FALSE;
7250 int key;
7251
7252 expand_option_flags = opt_flags;
7253
7254 xp->xp_context = EXPAND_SETTINGS;
7255 if (*arg == NUL)
7256 {
7257 xp->xp_pattern = arg;
7258 return;
7259 }
7260 p = arg + STRLEN(arg) - 1;
7261 if (*p == ' ' && *(p - 1) != '\\')
7262 {
7263 xp->xp_pattern = p + 1;
7264 return;
7265 }
7266 while (p > arg)
7267 {
7268 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007269 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 if (*p == ' ' || *p == ',')
7271 {
7272 while (s > arg && *(s - 1) == '\\')
7273 --s;
7274 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007275 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 if (*p == ' ' && ((p - s) & 1) == 0)
7277 {
7278 ++p;
7279 break;
7280 }
7281 --p;
7282 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007283 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 {
7285 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007286 xp->xp_prefix = XP_PREFIX_NO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 p += 2;
7288 }
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007289 else if (STRNCMP(p, "inv", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 {
7291 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007292 xp->xp_prefix = XP_PREFIX_INV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 p += 3;
7294 }
7295 xp->xp_pattern = arg = p;
7296 if (*arg == '<')
7297 {
7298 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007299 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 return;
7301 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007302 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 {
7304 xp->xp_context = EXPAND_NOTHING;
7305 return;
7306 }
7307 nextchar = *++p;
7308 is_term_option = TRUE;
7309 expand_option_name[2] = KEY2TERMCAP0(key);
7310 expand_option_name[3] = KEY2TERMCAP1(key);
7311 }
7312 else
7313 {
7314 if (p[0] == 't' && p[1] == '_')
7315 {
7316 p += 2;
7317 if (*p != NUL)
7318 ++p;
7319 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007320 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 nextchar = *++p;
7322 is_term_option = TRUE;
7323 expand_option_name[2] = p[-2];
7324 expand_option_name[3] = p[-1];
7325 }
7326 else
7327 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007328 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7330 p++;
7331 if (*p == NUL)
7332 return;
7333 nextchar = *p;
7334 *p = NUL;
7335 opt_idx = findoption(arg);
7336 *p = nextchar;
7337 if (opt_idx == -1 || options[opt_idx].var == NULL)
7338 {
7339 xp->xp_context = EXPAND_NOTHING;
7340 return;
7341 }
7342 flags = options[opt_idx].flags;
7343 if (flags & P_BOOL)
7344 {
7345 xp->xp_context = EXPAND_NOTHING;
7346 return;
7347 }
7348 }
7349 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007350 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7352 {
7353 ++p;
7354 nextchar = '=';
7355 }
7356 if ((nextchar != '=' && nextchar != ':')
7357 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7358 {
7359 xp->xp_context = EXPAND_UNSUCCESSFUL;
7360 return;
7361 }
7362 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
7363 {
7364 xp->xp_context = EXPAND_OLD_SETTING;
7365 if (is_term_option)
7366 expand_option_idx = -1;
7367 else
7368 expand_option_idx = opt_idx;
7369 xp->xp_pattern = p + 1;
7370 return;
7371 }
7372 xp->xp_context = EXPAND_NOTHING;
7373 if (is_term_option || (flags & P_NUM))
7374 return;
7375
7376 xp->xp_pattern = p + 1;
7377
Doug Kearns6dfdff32023-08-27 18:48:51 +02007378#ifdef FEAT_SYN_HL
7379 if (options[opt_idx].var == (char_u *)&p_syn)
7380 {
7381 xp->xp_context = EXPAND_OWNSYNTAX;
7382 return;
7383 }
7384#endif
7385
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 if (flags & P_EXPAND)
7387 {
7388 p = options[opt_idx].var;
7389 if (p == (char_u *)&p_bdir
7390 || p == (char_u *)&p_dir
7391 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007392 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395#ifdef FEAT_SESSION
7396 || p == (char_u *)&p_vdir
7397#endif
7398 )
7399 {
7400 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007401 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 xp->xp_backslash = XP_BS_THREE;
7403 else
7404 xp->xp_backslash = XP_BS_ONE;
7405 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01007406 else if (p == (char_u *)&p_ft)
7407 {
7408 xp->xp_context = EXPAND_FILETYPE;
7409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 else
7411 {
7412 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007413 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 if (p == (char_u *)&p_tags)
7415 xp->xp_backslash = XP_BS_THREE;
7416 else
7417 xp->xp_backslash = XP_BS_ONE;
7418 }
7419 }
7420
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007421 // For an option that is a list of file names, find the start of the
7422 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
7424 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007425 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 if (*p == ' ' || *p == ',')
7427 {
7428 s = p;
7429 while (s > xp->xp_pattern && *(s - 1) == '\\')
7430 --s;
7431 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
7432 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
7433 {
7434 xp->xp_pattern = p + 1;
7435 break;
7436 }
7437 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007438
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007439#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007440 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007441 if (options[opt_idx].var == (char_u *)&p_sps
7442 && STRNCMP(p, "file:", 5) == 0)
7443 {
7444 xp->xp_pattern = p + 5;
7445 break;
7446 }
7447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449}
7450
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007451/*
7452 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7453 *
7454 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7455 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7456 *
7457 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7458 * regular expression 'regmatch', then stores the match in matches[idx] and
7459 * returns TRUE.
7460 *
7461 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7462 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7463 *
7464 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7465 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7466 */
7467 static int
7468match_str(
7469 char_u *str,
7470 regmatch_T *regmatch,
7471 char_u **matches,
7472 int idx,
7473 int test_only,
7474 int fuzzy,
7475 char_u *fuzzystr,
7476 fuzmatch_str_T *fuzmatch)
7477{
7478 if (!fuzzy)
7479 {
7480 if (vim_regexec(regmatch, str, (colnr_T)0))
7481 {
7482 if (!test_only)
7483 matches[idx] = vim_strsave(str);
7484 return TRUE;
7485 }
7486 }
7487 else
7488 {
7489 int score;
7490
7491 score = fuzzy_match_str(str, fuzzystr);
7492 if (score != 0)
7493 {
7494 if (!test_only)
7495 {
7496 fuzmatch[idx].idx = idx;
7497 fuzmatch[idx].str = vim_strsave(str);
7498 fuzmatch[idx].score = score;
7499 }
7500
7501 return TRUE;
7502 }
7503 }
7504
7505 return FALSE;
7506}
7507
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007509ExpandSettings(
7510 expand_T *xp,
7511 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007512 char_u *fuzzystr,
7513 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007514 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007515 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007517 int num_normal = 0; // Nr of matching non-term-code settings
7518 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 int opt_idx;
7520 int match;
7521 int count = 0;
7522 char_u *str;
7523 int loop;
7524 int is_term_opt;
7525 char_u name_buf[MAX_KEY_NAME_LEN];
7526 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007527 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007528 int fuzzy;
7529 fuzmatch_str_T *fuzmatch = NULL;
7530
Christian Brabandtcb747892022-05-08 21:10:56 +01007531 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007533 // do this loop twice:
7534 // loop == 0: count the number of matching options
7535 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 for (loop = 0; loop <= 1; ++loop)
7537 {
7538 regmatch->rm_ic = ic;
7539 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7540 {
K.Takataeeec2542021-06-02 13:28:16 +02007541 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007542 {
7543 if (match_str((char_u *)names[match], regmatch, *matches,
7544 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 {
7546 if (loop == 0)
7547 num_normal++;
7548 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007549 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 }
7553 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7554 opt_idx++)
7555 {
7556 if (options[opt_idx].var == NULL)
7557 continue;
7558 if (xp->xp_context == EXPAND_BOOL_SETTINGS
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007559 && !(options[opt_idx].flags & P_BOOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007561 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 if (is_term_opt && num_normal > 0)
7563 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007564
7565 if (match_str(str, regmatch, *matches, count, (loop == 0),
7566 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 {
7568 if (loop == 0)
7569 {
7570 if (is_term_opt)
7571 num_term++;
7572 else
7573 num_normal++;
7574 }
7575 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007576 count++;
7577 }
7578 else if (!fuzzy && options[opt_idx].shortname != NULL
7579 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007580 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007581 {
7582 // Compare against the abbreviated option name (for regular
7583 // expression match). Fuzzy matching (previous if) already
7584 // matches against both the expanded and abbreviated names.
7585 if (loop == 0)
7586 {
7587 if (is_term_opt)
7588 num_term++;
7589 else
7590 num_normal++;
7591 }
7592 else
7593 (*matches)[count++] = vim_strsave(str);
7594 }
7595 else if (is_term_opt)
7596 {
7597 name_buf[0] = '<';
7598 name_buf[1] = 't';
7599 name_buf[2] = '_';
7600 name_buf[3] = str[2];
7601 name_buf[4] = str[3];
7602 name_buf[5] = '>';
7603 name_buf[6] = NUL;
7604
7605 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7606 fuzzy, fuzzystr, fuzmatch))
7607 {
7608 if (loop == 0)
7609 num_term++;
7610 else
7611 count++;
7612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 }
7614 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007615
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007616 // Check terminal key codes, these are not in the option table
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7618 {
7619 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7620 {
7621 if (!isprint(str[0]) || !isprint(str[1]))
7622 continue;
7623
7624 name_buf[0] = 't';
7625 name_buf[1] = '_';
7626 name_buf[2] = str[0];
7627 name_buf[3] = str[1];
7628 name_buf[4] = NUL;
7629
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007630 if (match_str(name_buf, regmatch, *matches, count,
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007631 (loop == 0), fuzzy, fuzzystr, fuzmatch))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007632 {
7633 if (loop == 0)
7634 num_term++;
7635 else
7636 count++;
7637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 else
7639 {
7640 name_buf[0] = '<';
7641 name_buf[1] = 't';
7642 name_buf[2] = '_';
7643 name_buf[3] = str[0];
7644 name_buf[4] = str[1];
7645 name_buf[5] = '>';
7646 name_buf[6] = NUL;
7647
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007648 if (match_str(name_buf, regmatch, *matches, count,
7649 (loop == 0), fuzzy, fuzzystr,
7650 fuzmatch))
7651 {
7652 if (loop == 0)
7653 num_term++;
7654 else
7655 count++;
7656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 }
7658 }
7659
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007660 // Check special key names.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007661 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7663 {
7664 name_buf[0] = '<';
7665 STRCPY(name_buf + 1, str);
7666 STRCAT(name_buf, ">");
7667
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007668 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7669 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 {
7671 if (loop == 0)
7672 num_term++;
7673 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007674 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 }
7676 }
7677 }
7678 if (loop == 0)
7679 {
7680 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007681 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007683 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 else
7685 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007686 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007688 *matches = ALLOC_MULT(char_u *, *numMatches);
7689 if (*matches == NULL)
7690 {
7691 *matches = (char_u **)"";
7692 return FAIL;
7693 }
7694 }
7695 else
7696 {
7697 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7698 if (fuzmatch == NULL)
7699 {
7700 *matches = (char_u **)"";
7701 return FAIL;
7702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 }
7704 }
7705 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007706
7707 if (fuzzy &&
7708 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7709 return FAIL;
7710
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 return OK;
7712}
7713
7714 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007715ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007717 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 char_u *buf;
7719
zeertzjqb0d45ec2023-01-25 15:04:22 +00007720 *numMatches = 0;
7721 *matches = ALLOC_ONE(char_u *);
7722 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 return FAIL;
7724
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007725 // For a terminal key code expand_option_idx is < 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 if (expand_option_idx < 0)
7727 {
7728 var = find_termcode(expand_option_name + 2);
7729 if (var == NULL)
7730 expand_option_idx = findoption(expand_option_name);
7731 }
7732
7733 if (expand_option_idx >= 0)
7734 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007735 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 option_value2string(&options[expand_option_idx], expand_option_flags);
7737 var = NameBuff;
7738 }
7739 else if (var == NULL)
7740 var = (char_u *)"";
7741
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007742 // A backslash is required before some characters. This is the reverse of
7743 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 buf = vim_strsave_escaped(var, escape_chars);
7745
7746 if (buf == NULL)
7747 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007748 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 return FAIL;
7750 }
7751
7752#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007753 // For MS-Windows et al. we don't double backslashes at the start and
7754 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007755 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 if (var[0] == '\\' && var[1] == '\\'
7757 && expand_option_idx >= 0
7758 && (options[expand_option_idx].flags & P_EXPAND)
7759 && vim_isfilec(var[2])
7760 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00007761 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762#endif
7763
zeertzjqb0d45ec2023-01-25 15:04:22 +00007764 *matches[0] = buf;
7765 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 return OK;
7767}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768
7769/*
7770 * Get the value for the numeric or string option *opp in a nice format into
7771 * NameBuff[]. Must not be called with a hidden option!
7772 */
7773 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007774option_value2string(
7775 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007776 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777{
7778 char_u *varp;
7779
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007780 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781
7782 if (opp->flags & P_NUM)
7783 {
7784 long wc = 0;
7785
7786 if (wc_use_keyname(varp, &wc))
7787 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
7788 else if (wc != 0)
7789 STRCPY(NameBuff, transchar((int)wc));
7790 else
7791 sprintf((char *)NameBuff, "%ld", *(long *)varp);
7792 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007793 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 {
7795 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007796 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 NameBuff[0] = NUL;
7798#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007799 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007800 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 STRCPY(NameBuff, "*****");
7802#endif
7803 else if (opp->flags & P_EXPAND)
7804 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007805 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 else if ((char_u **)opp->var == &p_pt)
7807 str2specialbuf(p_pt, NameBuff, MAXPATHL);
7808 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007809 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 }
7811}
7812
7813/*
7814 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
7815 * printed as a keyname.
7816 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
7817 */
7818 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007819wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820{
7821 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
7822 {
7823 *wcp = *(long *)varp;
7824 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
7825 return TRUE;
7826 }
7827 return FALSE;
7828}
7829
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 * Return TRUE if "x" is present in 'shortmess' option, or
7832 * 'shortmess' contains 'a' and "x" is present in SHM_A.
7833 */
7834 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007835shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01007837 return p_shm != NULL &&
7838 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 || (vim_strchr(p_shm, 'a') != NULL
7840 && vim_strchr((char_u *)SHM_A, x) != NULL));
7841}
7842
7843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7845 *
7846 * Reset 'compatible' and set the values for options that didn't get set yet
7847 * to the Vim defaults.
7848 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007849 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 */
7851 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007852vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007854 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007855 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007856 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857
7858 if (!option_was_set((char_u *)"cp"))
7859 {
7860 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007861 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7863 set_option_default(opt_idx, OPT_FREE, FALSE);
7864 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007865 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007867
7868 if (fname != NULL)
7869 {
7870 p = vim_getenv(envname, &dofree);
7871 if (p == NULL)
7872 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007873 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007874 p = FullName_save(fname, FALSE);
7875 if (p != NULL)
7876 {
7877 vim_setenv(envname, p);
7878 vim_free(p);
7879 }
7880 }
7881 else if (dofree)
7882 vim_free(p);
7883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884}
7885
7886/*
7887 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7888 */
7889 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007890change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007892 int opt_idx;
7893
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 if (p_cp != on)
7895 {
7896 p_cp = on;
7897 compatible_set();
7898 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007899 opt_idx = findoption((char_u *)"cp");
7900 if (opt_idx >= 0)
7901 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902}
7903
7904/*
7905 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007906 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 */
7908 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007909option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910{
7911 int idx;
7912
7913 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007914 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 return FALSE;
7916 if (options[idx].flags & P_WAS_SET)
7917 return TRUE;
7918 return FALSE;
7919}
7920
7921/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007922 * Reset the flag indicating option "name" was set.
7923 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007924 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007925reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007926{
7927 int idx = findoption(name);
7928
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007929 if (idx < 0)
7930 return FAIL;
7931
7932 options[idx].flags &= ~P_WAS_SET;
7933 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007934}
7935
7936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 * compatible_set() - Called when 'compatible' has been set or unset.
7938 *
7939 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7940 * flag) to a Vi compatible value.
7941 * When 'compatible' is unset: Set all options that have a different default
7942 * for Vim (without the P_VI_DEF flag) to that default.
7943 */
7944 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007945compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946{
7947 int opt_idx;
7948
Bram Moolenaardac13472019-09-16 21:06:21 +02007949 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7951 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7952 set_option_default(opt_idx, OPT_FREE, p_cp);
7953 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007954 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955}
7956
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 * Check if backspacing over something is allowed.
7959 */
7960 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007961can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007962 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007964#ifdef FEAT_JOB_CHANNEL
7965 if (what == BS_START && bt_prompt(curbuf))
7966 return FALSE;
7967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 switch (*p_bs)
7969 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007970 case '3': return TRUE;
7971 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 case '1': return (what != BS_START);
7973 case '0': return FALSE;
7974 }
7975 return vim_strchr(p_bs, what) != NULL;
7976}
7977
7978/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007979 * Return the effective 'scrolloff' value for the current window, using the
7980 * global value when appropriate.
7981 */
7982 long
7983get_scrolloff_value(void)
7984{
7985 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7986}
7987
7988/*
7989 * Return the effective 'sidescrolloff' value for the current window, using the
7990 * global value when appropriate.
7991 */
7992 long
7993get_sidescrolloff_value(void)
7994{
7995 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7996}
7997
7998/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007999 * Get the local or global value of 'backupcopy'.
8000 */
8001 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008002get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008003{
8004 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
8005}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02008006
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008007#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02008008/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00008009 * Get the local or global value of 'formatlistpat'.
8010 */
8011 char_u *
8012get_flp_value(buf_T *buf)
8013{
Christian Brabandtc53b4672022-01-15 10:01:05 +00008014 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
8015 return p_flp;
8016 return buf->b_p_flp;
8017}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008018#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00008019
8020/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02008021 * Get the local or global value of the 'virtualedit' flags.
8022 */
8023 unsigned int
8024get_ve_flags(void)
8025{
Gary Johnson51ad8502021-08-03 18:33:08 +02008026 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
8027 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02008028}
8029
Bram Moolenaaree857022019-11-09 23:26:40 +01008030#if defined(FEAT_LINEBREAK) || defined(PROTO)
8031/*
8032 * Get the local or global value of 'showbreak'.
8033 */
8034 char_u *
8035get_showbreak_value(win_T *win)
8036{
8037 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8038 return p_sbr;
8039 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8040 return empty_option;
8041 return win->w_p_sbr;
8042}
8043#endif
8044
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008045#if defined(FEAT_EVAL) || defined(PROTO)
8046/*
8047 * Get window or buffer local options.
8048 */
8049 dict_T *
8050get_winbuf_options(int bufopt)
8051{
8052 dict_T *d;
8053 int opt_idx;
8054
8055 d = dict_alloc();
8056 if (d == NULL)
8057 return NULL;
8058
Bram Moolenaardac13472019-09-16 21:06:21 +02008059 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008060 {
8061 struct vimoption *opt = &options[opt_idx];
8062
8063 if ((bufopt && (opt->indir & PV_BUF))
8064 || (!bufopt && (opt->indir & PV_WIN)))
8065 {
8066 char_u *varp = get_varp(opt);
8067
8068 if (varp != NULL)
8069 {
8070 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008071 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008072 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008073 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008074 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008075 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008076 }
8077 }
8078 }
8079
8080 return d;
8081}
8082#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008083
Bram Moolenaardac13472019-09-16 21:06:21 +02008084#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008085/*
8086 * This is called when 'culopt' is changed
8087 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008088 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008089fill_culopt_flags(char_u *val, win_T *wp)
8090{
8091 char_u *p;
8092 char_u culopt_flags_new = 0;
8093
8094 if (val == NULL)
8095 p = wp->w_p_culopt;
8096 else
8097 p = val;
8098 while (*p != NUL)
8099 {
8100 if (STRNCMP(p, "line", 4) == 0)
8101 {
8102 p += 4;
8103 culopt_flags_new |= CULOPT_LINE;
8104 }
8105 else if (STRNCMP(p, "both", 4) == 0)
8106 {
8107 p += 4;
8108 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8109 }
8110 else if (STRNCMP(p, "number", 6) == 0)
8111 {
8112 p += 6;
8113 culopt_flags_new |= CULOPT_NBR;
8114 }
8115 else if (STRNCMP(p, "screenline", 10) == 0)
8116 {
8117 p += 10;
8118 culopt_flags_new |= CULOPT_SCRLINE;
8119 }
8120
8121 if (*p != ',' && *p != NUL)
8122 return FAIL;
8123 if (*p == ',')
8124 ++p;
8125 }
8126
8127 // Can't have both "line" and "screenline".
8128 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8129 return FAIL;
8130 wp->w_p_culopt_flags = culopt_flags_new;
8131
8132 return OK;
8133}
8134#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008135
8136/*
8137 * Get the value of 'magic' adjusted for Vim9 script.
8138 */
8139 int
8140magic_isset(void)
8141{
8142 switch (magic_overruled)
8143 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008144 case OPTION_MAGIC_ON: return TRUE;
8145 case OPTION_MAGIC_OFF: return FALSE;
8146 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008147 }
8148#ifdef FEAT_EVAL
8149 if (in_vim9script())
8150 return TRUE;
8151#endif
8152 return p_magic;
8153}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008154
8155/*
8156 * Set the callback function value for an option that accepts a function name,
8157 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8158 * Returns OK if the option is successfully set to a function, otherwise
8159 * returns FAIL.
8160 */
8161 int
8162option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8163{
8164#ifdef FEAT_EVAL
8165 typval_T *tv;
8166 callback_T cb;
8167
8168 if (optval == NULL || *optval == NUL)
8169 {
8170 free_callback(optcb);
8171 return OK;
8172 }
8173
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008174 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008175 || (STRNCMP(optval, "function(", 9) == 0)
8176 || (STRNCMP(optval, "funcref(", 8) == 0))
8177 // Lambda expression or a funcref
8178 tv = eval_expr(optval, NULL);
8179 else
8180 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008181 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008182 if (tv == NULL)
8183 return FAIL;
8184
8185 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008186 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008187 {
8188 free_tv(tv);
8189 return FAIL;
8190 }
8191
8192 free_callback(optcb);
8193 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008194 if (cb.cb_free_name)
8195 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008196 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008197
8198 // when using Vim9 style "import.funcname" it needs to be expanded to
8199 // "import#funcname".
8200 expand_autload_callback(optcb);
8201
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008202 return OK;
8203#else
8204 return FAIL;
8205#endif
8206}
Christian Brabandt757593c2023-08-22 21:44:10 +02008207
8208#if defined(FEAT_EVAL) || defined(PROTO)
8209 static void
8210didset_options_sctx(int opt_flags, char **buf)
8211{
8212 for (int i = 0; ; ++i)
8213 {
8214 if (buf[i] == NULL)
8215 break;
8216
8217 int idx = findoption((char_u *)buf[i]);
8218 if (idx >= 0)
8219 set_option_sctx_idx(idx, opt_flags, current_sctx);
8220 }
8221}
8222#endif