blob: 095653da75a8d0d7609ddcb232022c09ebcdbf84 [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 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002985void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002986{
2987 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002988 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002989}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002990
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002992 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2993 * characters or characters in "allowed".
2994 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002995 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002996valid_name(char_u *val, char *allowed)
2997{
2998 char_u *s;
2999
3000 for (s = val; *s != NUL; ++s)
3001 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
3002 return FALSE;
3003 return TRUE;
3004}
3005
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003006#if defined(FEAT_EVAL) || defined(PROTO)
3007/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003008 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003009 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003010 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003011 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003012set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003013{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003014 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
3015 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003016 sctx_T new_script_ctx = script_ctx;
3017
Bram Moolenaar51258742020-05-03 17:19:33 +02003018 // Modeline already has the line number set.
3019 if (!(opt_flags & OPT_MODELINE))
3020 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003021
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003022 // Remember where the option was set. For local options need to do that
3023 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003024 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003025 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003026 if (both || (opt_flags & OPT_LOCAL))
3027 {
3028 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003029 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003030 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00003031 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003032 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00003033 if (both)
3034 // also setting the "all buffers" value
3035 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
3036 new_script_ctx;
3037 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003038 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003039}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003040
3041/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00003042 * Get the script context of global option "name".
3043 *
3044 */
3045 sctx_T *
3046get_option_sctx(char *name)
3047{
3048 int idx = findoption((char_u *)name);
3049
3050 if (idx >= 0)
3051 return &options[idx].script_ctx;
3052 siemsg("no such option: %s", name);
3053 return NULL;
3054}
3055
3056/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003057 * Set the script_ctx for a termcap option.
3058 * "name" must be the two character code, e.g. "RV".
3059 * When "name" is NULL use "opt_idx".
3060 */
3061 void
3062set_term_option_sctx_idx(char *name, int opt_idx)
3063{
3064 char_u buf[5];
3065 int idx;
3066
3067 if (name == NULL)
3068 idx = opt_idx;
3069 else
3070 {
3071 buf[0] = 't';
3072 buf[1] = '_';
3073 buf[2] = name[0];
3074 buf[3] = name[1];
3075 buf[4] = 0;
3076 idx = findoption(buf);
3077 }
3078 if (idx >= 0)
3079 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
3080}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003081#endif
3082
Bram Moolenaarf4140482020-02-15 23:06:45 +01003083#if defined(FEAT_EVAL)
3084/*
3085 * Apply the OptionSet autocommand.
3086 */
3087 static void
3088apply_optionset_autocmd(
3089 int opt_idx,
3090 long opt_flags,
3091 long oldval,
3092 long oldval_g,
3093 long newval,
3094 char *errmsg)
3095{
3096 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
3097
3098 // Don't do this while starting up, failure or recursively.
3099 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
3100 return;
3101
3102 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
3103 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
3104 oldval_g);
3105 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
3106 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
3107 (opt_flags & OPT_LOCAL) ? "local" : "global");
3108 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3109 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3110 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
3111 if (opt_flags & OPT_LOCAL)
3112 {
3113 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3114 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3115 }
3116 if (opt_flags & OPT_GLOBAL)
3117 {
3118 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3119 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3120 }
3121 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3122 {
3123 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3124 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3125 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3126 }
3127 if (opt_flags & OPT_MODELINE)
3128 {
3129 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3130 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3131 }
3132 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3133 NULL, FALSE, NULL);
3134 reset_v_option_vars();
3135}
3136#endif
3137
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003138#if defined(FEAT_ARABIC) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003139/*
3140 * Process the updated 'arabic' option value.
3141 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003142 char *
3143did_set_arabic(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003144{
3145 char *errmsg = NULL;
3146
3147 if (curwin->w_p_arab)
3148 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003149 // 'arabic' is set, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003150 if (!p_tbidi)
3151 {
3152 // set rightleft mode
3153 if (!curwin->w_p_rl)
3154 {
3155 curwin->w_p_rl = TRUE;
3156 changed_window_setting();
3157 }
3158
3159 // Enable Arabic shaping (major part of what Arabic requires)
3160 if (!p_arshape)
3161 {
3162 p_arshape = TRUE;
3163 redraw_later_clear();
3164 }
3165 }
3166
3167 // Arabic requires a utf-8 encoding, inform the user if it's not
3168 // set.
3169 if (STRCMP(p_enc, "utf-8") != 0)
3170 {
3171 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3172
3173 msg_source(HL_ATTR(HLF_W));
3174 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
3175#ifdef FEAT_EVAL
3176 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3177#endif
3178 }
3179
3180 // set 'delcombine'
3181 p_deco = TRUE;
3182
3183# ifdef FEAT_KEYMAP
3184 // Force-set the necessary keymap for arabic
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003185 errmsg = set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3186 OPT_LOCAL);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003187# endif
3188 }
3189 else
3190 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003191 // 'arabic' is reset, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003192 if (!p_tbidi)
3193 {
3194 // reset rightleft mode
3195 if (curwin->w_p_rl)
3196 {
3197 curwin->w_p_rl = FALSE;
3198 changed_window_setting();
3199 }
3200
3201 // 'arabicshape' isn't reset, it is a global option and
3202 // another window may still need it "on".
3203 }
3204
3205 // 'delcombine' isn't reset, it is a global option and another
3206 // window may still want it "on".
3207
3208# ifdef FEAT_KEYMAP
3209 // Revert to the default keymap
3210 curbuf->b_p_iminsert = B_IMODE_NONE;
3211 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3212# endif
3213 }
3214
3215 return errmsg;
3216}
3217#endif
3218
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003219#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
3220/*
3221 * Process the updated 'autochdir' option value.
3222 */
3223 char *
3224did_set_autochdir(optset_T *args UNUSED)
3225{
3226 // Change directories when the 'acd' option is set now.
3227 DO_AUTOCHDIR;
3228 return NULL;
3229}
3230#endif
3231
3232#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
3233/*
3234 * Process the updated 'ballooneval' option value.
3235 */
3236 char *
3237did_set_ballooneval(optset_T *args)
3238{
3239 if (balloonEvalForTerm)
3240 return NULL;
3241
3242 if (p_beval && !args->os_oldval.boolean)
3243 gui_mch_enable_beval_area(balloonEval);
3244 else if (!p_beval && args->os_oldval.boolean)
3245 gui_mch_disable_beval_area(balloonEval);
3246
3247 return NULL;
3248}
3249#endif
3250
3251#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
3252/*
3253 * Process the updated 'balloonevalterm' option value.
3254 */
3255 char *
3256did_set_balloonevalterm(optset_T *args UNUSED)
3257{
3258 mch_bevalterm_changed();
3259 return NULL;
3260}
3261#endif
3262
3263/*
3264 * Process the updated 'binary' option value.
3265 */
3266 char *
3267did_set_binary(optset_T *args)
3268{
3269 // when 'bin' is set also set some other options
3270 set_options_bin(args->os_oldval.boolean, curbuf->b_p_bin, args->os_flags);
3271 redraw_titles();
3272
3273 return NULL;
3274}
3275
3276#if defined(FEAT_LINEBREAK) || defined(PROTO)
3277/*
3278 * Called when the 'breakat' option changes value.
3279 */
3280 char *
3281did_set_breakat(optset_T *args UNUSED)
3282{
3283 char_u *p;
3284 int i;
3285
3286 for (i = 0; i < 256; i++)
3287 breakat_flags[i] = FALSE;
3288
3289 if (p_breakat != NULL)
3290 for (p = p_breakat; *p; p++)
3291 breakat_flags[*p] = TRUE;
3292
3293 return NULL;
3294}
3295#endif
3296
3297/*
3298 * Process the updated 'buflisted' option value.
3299 */
3300 char *
3301did_set_buflisted(optset_T *args)
3302{
3303 // when 'buflisted' changes, trigger autocommands
3304 if (args->os_oldval.boolean != curbuf->b_p_bl)
3305 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3306 NULL, NULL, TRUE, curbuf);
3307 return NULL;
3308}
3309
3310/*
3311 * Process the new 'cmdheight' option value.
3312 */
3313 char *
3314did_set_cmdheight(optset_T *args)
3315{
3316 long old_value = args->os_oldval.number;
3317 char *errmsg = NULL;
3318
3319 // if p_ch changed value, change the command line height
3320 if (p_ch < 1)
3321 {
3322 errmsg = e_argument_must_be_positive;
3323 p_ch = 1;
3324 }
3325 if (p_ch > Rows - min_rows() + 1)
3326 p_ch = Rows - min_rows() + 1;
3327
3328 // Only compute the new window layout when startup has been
3329 // completed. Otherwise the frame sizes may be wrong.
3330 if ((p_ch != old_value
3331 || tabline_height() + topframe->fr_height != Rows - p_ch)
3332 && full_screen
3333#ifdef FEAT_GUI
3334 && !gui.starting
3335#endif
3336 )
3337 command_height();
3338
3339 return errmsg;
3340}
3341
3342/*
3343 * Process the updated 'compatible' option value.
3344 */
3345 char *
3346did_set_compatible(optset_T *args UNUSED)
3347{
3348 compatible_set();
3349 return NULL;
3350}
3351
3352#if defined(FEAT_CONCEAL) || defined(PROTO)
3353/*
3354 * Process the new 'conceallevel' option value.
3355 */
3356 char *
3357did_set_conceallevel(optset_T *args UNUSED)
3358{
3359 char *errmsg = NULL;
3360
3361 if (curwin->w_p_cole < 0)
3362 {
3363 errmsg = e_argument_must_be_positive;
3364 curwin->w_p_cole = 0;
3365 }
3366 else if (curwin->w_p_cole > 3)
3367 {
3368 errmsg = e_invalid_argument;
3369 curwin->w_p_cole = 3;
3370 }
3371
3372 return errmsg;
3373}
3374#endif
3375
3376#if defined(FEAT_DIFF) || defined(PROTO)
3377/*
3378 * Process the updated 'diff' option value.
3379 */
3380 char *
3381did_set_diff(optset_T *args UNUSED)
3382{
3383 // May add or remove the buffer from the list of diff buffers.
3384 diff_buf_adjust(curwin);
3385# ifdef FEAT_FOLDING
3386 if (foldmethodIsDiff(curwin))
3387 foldUpdateAll(curwin);
3388# endif
3389 return NULL;
3390}
3391#endif
3392
3393/*
3394 * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb'
3395 * option value.
3396 */
3397 char *
3398did_set_eof_eol_fixeol_bomb(optset_T *args UNUSED)
3399{
3400 // redraw the window title and tab page text
3401 redraw_titles();
3402 return NULL;
3403}
3404
3405/*
3406 * Process the updated 'equalalways' option value.
3407 */
3408 char *
3409did_set_equalalways(optset_T *args)
3410{
3411 if (p_ea && !args->os_oldval.boolean)
3412 win_equal(curwin, FALSE, 0);
3413
3414 return NULL;
3415}
3416
3417#if defined(FEAT_FOLDING) || defined(PROTO)
3418/*
3419 * Process the new 'foldcolumn' option value.
3420 */
3421 char *
3422did_set_foldcolumn(optset_T *args UNUSED)
3423{
3424 char *errmsg = NULL;
3425
3426 if (curwin->w_p_fdc < 0)
3427 {
3428 errmsg = e_argument_must_be_positive;
3429 curwin->w_p_fdc = 0;
3430 }
3431 else if (curwin->w_p_fdc > 12)
3432 {
3433 errmsg = e_invalid_argument;
3434 curwin->w_p_fdc = 12;
3435 }
3436
3437 return errmsg;
3438}
3439
3440/*
3441 * Process the new 'foldlevel' option value.
3442 */
3443 char *
3444did_set_foldlevel(optset_T *args UNUSED)
3445{
3446 if (curwin->w_p_fdl < 0)
3447 curwin->w_p_fdl = 0;
3448 newFoldLevel();
3449 return NULL;
3450}
3451
3452/*
3453 * Process the new 'foldminlines' option value.
3454 */
3455 char *
3456did_set_foldminlines(optset_T *args UNUSED)
3457{
3458 foldUpdateAll(curwin);
3459 return NULL;
3460}
3461
3462/*
3463 * Process the new 'foldnestmax' option value.
3464 */
3465 char *
3466did_set_foldnestmax(optset_T *args UNUSED)
3467{
3468 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3469 foldUpdateAll(curwin);
3470 return NULL;
3471}
3472#endif
3473
3474#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
3475/*
3476 * Process the updated 'hlsearch' option value.
3477 */
3478 char *
3479did_set_hlsearch(optset_T *args UNUSED)
3480{
3481 // when 'hlsearch' is set or reset: reset no_hlsearch
3482 set_no_hlsearch(FALSE);
3483 return NULL;
3484}
3485#endif
3486
3487/*
3488 * Process the updated 'ignorecase' option value.
3489 */
3490 char *
3491did_set_ignorecase(optset_T *args UNUSED)
3492{
3493 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
3494 if (p_hls)
3495 redraw_all_later(UPD_SOME_VALID);
3496 return NULL;
3497}
3498
3499#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
3500/*
3501 * Process the updated 'imdisable' option value.
3502 */
3503 char *
3504did_set_imdisable(optset_T *args UNUSED)
3505{
3506 // Only de-activate it here, it will be enabled when changing mode.
3507 if (p_imdisable)
3508 im_set_active(FALSE);
3509 else if (State & MODE_INSERT)
3510 // When the option is set from an autocommand, it may need to take
3511 // effect right away.
3512 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
3513 return NULL;
3514}
3515#endif
3516
3517/*
3518 * Process the new 'iminsert' option value.
3519 */
3520 char *
3521did_set_iminsert(optset_T *args UNUSED)
3522{
3523 char *errmsg = NULL;
3524
3525 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3526 {
3527 errmsg = e_invalid_argument;
3528 curbuf->b_p_iminsert = B_IMODE_NONE;
3529 }
3530 p_iminsert = curbuf->b_p_iminsert;
3531 if (termcap_active) // don't do this in the alternate screen
3532 showmode();
3533#if defined(FEAT_KEYMAP)
3534 // Show/unshow value of 'keymap' in status lines.
3535 status_redraw_curbuf();
3536#endif
3537
3538 return errmsg;
3539}
3540
3541/*
3542 * Process the new 'imsearch' option value.
3543 */
3544 char *
3545did_set_imsearch(optset_T *args UNUSED)
3546{
3547 char *errmsg = NULL;
3548
3549 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3550 {
3551 errmsg = e_invalid_argument;
3552 curbuf->b_p_imsearch = B_IMODE_NONE;
3553 }
3554 p_imsearch = curbuf->b_p_imsearch;
3555
3556 return errmsg;
3557}
3558
3559#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3560/*
3561 * Process the new 'imstyle' option value.
3562 */
3563 char *
3564did_set_imstyle(optset_T *args UNUSED)
3565{
3566 char *errmsg = NULL;
3567
3568 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3569 errmsg = e_invalid_argument;
3570
3571 return errmsg;
3572}
3573#endif
3574
3575/*
3576 * Process the updated 'insertmode' option value.
3577 */
3578 char *
3579did_set_insertmode(optset_T *args)
3580{
3581 // when 'insertmode' is set from an autocommand need to do work here
3582 if (p_im)
3583 {
3584 if ((State & MODE_INSERT) == 0)
3585 need_start_insertmode = TRUE;
3586 stop_insert_mode = FALSE;
3587 }
3588 // only reset if it was set previously
3589 else if (args->os_oldval.boolean)
3590 {
3591 need_start_insertmode = FALSE;
3592 stop_insert_mode = TRUE;
3593 if (restart_edit != 0 && mode_displayed)
3594 clear_cmdline = TRUE; // remove "(insert)"
3595 restart_edit = 0;
3596 }
3597
3598 return NULL;
3599}
3600
3601#if defined(FEAT_LANGMAP) || defined(PROTO)
3602/*
3603 * Process the updated 'langnoremap' option value.
3604 */
3605 char *
3606did_set_langnoremap(optset_T *args UNUSED)
3607{
3608 // 'langnoremap' -> !'langremap'
3609 p_lrm = !p_lnr;
3610 return NULL;
3611}
3612
3613/*
3614 * Process the updated 'langremap' option value.
3615 */
3616 char *
3617did_set_langremap(optset_T *args UNUSED)
3618{
3619 // 'langremap' -> !'langnoremap'
3620 p_lnr = !p_lrm;
3621 return NULL;
3622}
3623#endif
3624
3625/*
3626 * Process the new 'laststatus' option value.
3627 */
3628 char *
3629did_set_laststatus(optset_T *args UNUSED)
3630{
3631 last_status(FALSE); // (re)set last window status line
3632 return NULL;
3633}
3634
3635#if defined(FEAT_GUI) || defined(PROTO)
3636/*
3637 * Process the new 'linespace' option value.
3638 */
3639 char *
3640did_set_linespace(optset_T *args UNUSED)
3641{
3642 // Recompute gui.char_height and resize the Vim window to keep the
3643 // same number of lines.
3644 if (gui.in_use && gui_mch_adjust_charheight() == OK)
3645 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
3646 return NULL;
3647}
3648#endif
3649
3650/*
3651 * Process the updated 'lisp' option value.
3652 */
3653 char *
3654did_set_lisp(optset_T *args UNUSED)
3655{
3656 // When 'lisp' option changes include/exclude '-' in keyword characters.
3657 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
3658 return NULL;
3659}
3660
3661/*
3662 * Process the new 'maxcombine' option value.
3663 */
3664 char *
3665did_set_maxcombine(optset_T *args UNUSED)
3666{
3667 if (p_mco > MAX_MCO)
3668 p_mco = MAX_MCO;
3669 else if (p_mco < 0)
3670 p_mco = 0;
3671 screenclear(); // will re-allocate the screen
3672 return NULL;
3673}
3674
3675/*
3676 * Process the updated 'modifiable' option value.
3677 */
3678 char *
3679did_set_modifiable(optset_T *args UNUSED)
3680{
3681 // when 'modifiable' is changed, redraw the window title
3682
3683# ifdef FEAT_TERMINAL
3684 // Cannot set 'modifiable' when in Terminal mode.
3685 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3686 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
3687 {
3688 curbuf->b_p_ma = FALSE;
3689 args->os_doskip = TRUE;
3690 return e_cannot_make_terminal_with_running_job_modifiable;
3691 }
3692# endif
3693 redraw_titles();
3694
3695 return NULL;
3696}
3697
3698/*
3699 * Process the updated 'modified' option value.
3700 */
3701 char *
3702did_set_modified(optset_T *args)
3703{
3704 if (!args->os_newval.boolean)
3705 save_file_ff(curbuf); // Buffer is unchanged
3706 redraw_titles();
3707 modified_was_set = args->os_newval.boolean;
3708 return NULL;
3709}
3710
3711#if defined(FEAT_GUI) || defined(PROTO)
3712/*
3713 * Process the updated 'mousehide' option value.
3714 */
3715 char *
3716did_set_mousehide(optset_T *args UNUSED)
3717{
3718 if (!p_mh)
3719 gui_mch_mousehide(FALSE);
3720 return NULL;
3721}
3722#endif
3723
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003724/*
3725 * Process the updated 'number' or 'relativenumber' option value.
3726 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003727 char *
3728did_set_number_relativenumber(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003729{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003730#if (defined(FEAT_SIGNS) && defined(FEAT_GUI)) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003731 if (gui.in_use
3732 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3733 && curbuf->b_signlist != NULL)
3734 {
3735 // If the 'number' or 'relativenumber' options are modified and
3736 // 'signcolumn' is set to 'number', then clear the screen for a full
3737 // refresh. Otherwise the sign icons are not displayed properly in the
3738 // number column. If the 'number' option is set and only the
3739 // 'relativenumber' option is toggled, then don't refresh the screen
3740 // (optimization).
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003741 if (!(curwin->w_p_nu && ((int *)args->os_varp == &curwin->w_p_rnu)))
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003742 redraw_all_later(UPD_CLEAR);
3743 }
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003744#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003745 return NULL;
3746}
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003747
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003748#if defined(FEAT_LINEBREAK) || defined(PROTO)
3749/*
3750 * Process the new 'numberwidth' option value.
3751 */
3752 char *
3753did_set_numberwidth(optset_T *args UNUSED)
3754{
3755 char *errmsg = NULL;
3756
3757 // 'numberwidth' must be positive
3758 if (curwin->w_p_nuw < 1)
3759 {
3760 errmsg = e_argument_must_be_positive;
3761 curwin->w_p_nuw = 1;
3762 }
3763 if (curwin->w_p_nuw > 20)
3764 {
3765 errmsg = e_invalid_argument;
3766 curwin->w_p_nuw = 20;
3767 }
3768 curwin->w_nrwidth_line_count = 0; // trigger a redraw
3769
3770 return errmsg;
3771}
3772#endif
3773
3774/*
3775 * Process the updated 'paste' option value. Called after p_paste was set or
3776 * reset. When 'paste' is set or reset also change other options.
3777 */
3778 char *
3779did_set_paste(optset_T *args UNUSED)
3780{
3781 static int old_p_paste = FALSE;
3782 static int save_sm = 0;
3783 static int save_sta = 0;
3784 static int save_ru = 0;
3785#ifdef FEAT_RIGHTLEFT
3786 static int save_ri = 0;
3787 static int save_hkmap = 0;
3788#endif
3789 buf_T *buf;
3790
3791 if (p_paste)
3792 {
3793 // Paste switched from off to on.
3794 // Save the current values, so they can be restored later.
3795 if (!old_p_paste)
3796 {
3797 // save options for each buffer
3798 FOR_ALL_BUFFERS(buf)
3799 {
3800 buf->b_p_tw_nopaste = buf->b_p_tw;
3801 buf->b_p_wm_nopaste = buf->b_p_wm;
3802 buf->b_p_sts_nopaste = buf->b_p_sts;
3803 buf->b_p_ai_nopaste = buf->b_p_ai;
3804 buf->b_p_et_nopaste = buf->b_p_et;
3805#ifdef FEAT_VARTABS
3806 if (buf->b_p_vsts_nopaste)
3807 vim_free(buf->b_p_vsts_nopaste);
3808 buf->b_p_vsts_nopaste =
3809 buf->b_p_vsts && buf->b_p_vsts != empty_option
3810 ? vim_strsave(buf->b_p_vsts) : NULL;
3811#endif
3812 }
3813
3814 // save global options
3815 save_sm = p_sm;
3816 save_sta = p_sta;
3817 save_ru = p_ru;
3818#ifdef FEAT_RIGHTLEFT
3819 save_ri = p_ri;
3820 save_hkmap = p_hkmap;
3821#endif
3822 // save global values for local buffer options
3823 p_ai_nopaste = p_ai;
3824 p_et_nopaste = p_et;
3825 p_sts_nopaste = p_sts;
3826 p_tw_nopaste = p_tw;
3827 p_wm_nopaste = p_wm;
3828#ifdef FEAT_VARTABS
3829 if (p_vsts_nopaste)
3830 vim_free(p_vsts_nopaste);
3831 p_vsts_nopaste = p_vsts && p_vsts != empty_option
3832 ? vim_strsave(p_vsts) : NULL;
3833#endif
3834 }
3835
3836 // Always set the option values, also when 'paste' is set when it is
3837 // already on. Set options for each buffer.
3838 FOR_ALL_BUFFERS(buf)
3839 {
3840 buf->b_p_tw = 0; // textwidth is 0
3841 buf->b_p_wm = 0; // wrapmargin is 0
3842 buf->b_p_sts = 0; // softtabstop is 0
3843 buf->b_p_ai = 0; // no auto-indent
3844 buf->b_p_et = 0; // no expandtab
3845#ifdef FEAT_VARTABS
3846 if (buf->b_p_vsts)
3847 free_string_option(buf->b_p_vsts);
3848 buf->b_p_vsts = empty_option;
3849 VIM_CLEAR(buf->b_p_vsts_array);
3850#endif
3851 }
3852
3853 // set global options
3854 p_sm = 0; // no showmatch
3855 p_sta = 0; // no smarttab
3856 if (p_ru)
3857 status_redraw_all(); // redraw to remove the ruler
3858 p_ru = 0; // no ruler
3859#ifdef FEAT_RIGHTLEFT
3860 p_ri = 0; // no reverse insert
3861 p_hkmap = 0; // no Hebrew keyboard
3862#endif
3863 // set global values for local buffer options
3864 p_tw = 0;
3865 p_wm = 0;
3866 p_sts = 0;
3867 p_ai = 0;
Christian Brabandt757593c2023-08-22 21:44:10 +02003868 p_et = 0;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003869#ifdef FEAT_VARTABS
3870 if (p_vsts)
3871 free_string_option(p_vsts);
3872 p_vsts = empty_option;
3873#endif
3874 }
3875
3876 // Paste switched from on to off: Restore saved values.
3877 else if (old_p_paste)
3878 {
3879 // restore options for each buffer
3880 FOR_ALL_BUFFERS(buf)
3881 {
3882 buf->b_p_tw = buf->b_p_tw_nopaste;
3883 buf->b_p_wm = buf->b_p_wm_nopaste;
3884 buf->b_p_sts = buf->b_p_sts_nopaste;
3885 buf->b_p_ai = buf->b_p_ai_nopaste;
3886 buf->b_p_et = buf->b_p_et_nopaste;
3887#ifdef FEAT_VARTABS
3888 if (buf->b_p_vsts)
3889 free_string_option(buf->b_p_vsts);
3890 buf->b_p_vsts = buf->b_p_vsts_nopaste
3891 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
3892 vim_free(buf->b_p_vsts_array);
3893 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
3894 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
3895 else
3896 buf->b_p_vsts_array = NULL;
3897#endif
3898 }
3899
3900 // restore global options
3901 p_sm = save_sm;
3902 p_sta = save_sta;
3903 if (p_ru != save_ru)
3904 status_redraw_all(); // redraw to draw the ruler
3905 p_ru = save_ru;
3906#ifdef FEAT_RIGHTLEFT
3907 p_ri = save_ri;
3908 p_hkmap = save_hkmap;
3909#endif
3910 // set global values for local buffer options
3911 p_ai = p_ai_nopaste;
3912 p_et = p_et_nopaste;
3913 p_sts = p_sts_nopaste;
3914 p_tw = p_tw_nopaste;
3915 p_wm = p_wm_nopaste;
3916#ifdef FEAT_VARTABS
3917 if (p_vsts)
3918 free_string_option(p_vsts);
3919 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
3920#endif
3921 }
3922
3923 old_p_paste = p_paste;
3924
Christian Brabandt757593c2023-08-22 21:44:10 +02003925#if defined(FEAT_EVAL) || defined(PROTO)
3926 // Remember where the dependent options were reset
3927 didset_options_sctx((OPT_LOCAL | OPT_GLOBAL), p_paste_dep_opts);
3928#endif
3929
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003930 return NULL;
3931}
3932
3933
3934#ifdef FEAT_QUICKFIX
3935/*
3936 * Process the updated 'previewwindow' option value.
3937 */
3938 char *
3939did_set_previewwindow(optset_T *args)
3940{
3941 if (!curwin->w_p_pvw)
3942 return NULL;
3943
3944 // There can be only one window with 'previewwindow' set.
3945 win_T *win;
3946
3947 FOR_ALL_WINDOWS(win)
3948 if (win->w_p_pvw && win != curwin)
3949 {
3950 curwin->w_p_pvw = FALSE;
3951 args->os_doskip = TRUE;
3952 return e_preview_window_already_exists;
3953 }
3954
3955 return NULL;
3956}
3957#endif
3958
3959#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
3960/*
3961 * Process the new 'pyxversion' option value.
3962 */
3963 char *
3964did_set_pyxversion(optset_T *args UNUSED)
3965{
3966 char *errmsg = NULL;
3967
3968 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3969 errmsg = e_invalid_argument;
3970
3971 return errmsg;
3972}
3973#endif
3974
3975/*
3976 * Process the updated 'readonly' option value.
3977 */
3978 char *
3979did_set_readonly(optset_T *args)
3980{
3981 // when 'readonly' is reset globally, also reset readonlymode
3982 if (!curbuf->b_p_ro && (args->os_flags & OPT_LOCAL) == 0)
3983 readonlymode = FALSE;
3984
3985 // when 'readonly' is set may give W10 again
3986 if (curbuf->b_p_ro)
3987 curbuf->b_did_warn = FALSE;
3988
3989 redraw_titles();
3990
3991 return NULL;
3992}
3993
3994/*
3995 * Process the updated 'scrollbind' option value.
3996 */
3997 char *
3998did_set_scrollbind(optset_T *args UNUSED)
3999{
4000 // when 'scrollbind' is set: snapshot the current position to avoid a jump
4001 // at the end of normal_cmd()
4002 if (!curwin->w_p_scb)
4003 return NULL;
4004
4005 do_check_scrollbind(FALSE);
4006 curwin->w_scbind_pos = curwin->w_topline;
4007 return NULL;
4008}
4009
4010#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
4011/*
4012 * Process the updated 'shellslash' option value.
4013 */
4014 char *
4015did_set_shellslash(optset_T *args UNUSED)
4016{
4017 if (p_ssl)
4018 {
4019 psepc = '/';
4020 psepcN = '\\';
4021 pseps[0] = '/';
4022 }
4023 else
4024 {
4025 psepc = '\\';
4026 psepcN = '/';
4027 pseps[0] = '\\';
4028 }
4029
4030 // need to adjust the file name arguments and buffer names.
4031 buflist_slash_adjust();
4032 alist_slash_adjust();
4033# ifdef FEAT_EVAL
4034 scriptnames_slash_adjust();
4035# endif
4036 return NULL;
4037}
4038#endif
4039
4040/*
4041 * Process the new 'shiftwidth' or the 'tabstop' option value.
4042 */
4043 char *
4044did_set_shiftwidth_tabstop(optset_T *args)
4045{
4046 long *pp = (long *)args->os_varp;
4047 char *errmsg = NULL;
4048
4049 if (curbuf->b_p_sw < 0)
4050 {
4051 errmsg = e_argument_must_be_positive;
4052#ifdef FEAT_VARTABS
4053 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4054 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4055 ? tabstop_first(curbuf->b_p_vts_array)
4056 : curbuf->b_p_ts;
4057#else
4058 curbuf->b_p_sw = curbuf->b_p_ts;
4059#endif
4060 }
4061
4062#ifdef FEAT_FOLDING
4063 if (foldmethodIsIndent(curwin))
4064 foldUpdateAll(curwin);
4065#endif
4066 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
4067 // parse 'cinoptions'.
4068 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
4069 parse_cino(curbuf);
4070
4071 return errmsg;
4072}
4073
4074/*
4075 * Process the new 'showtabline' option value.
4076 */
4077 char *
4078did_set_showtabline(optset_T *args UNUSED)
4079{
4080 shell_new_rows(); // recompute window positions and heights
4081 return NULL;
4082}
4083
4084/*
4085 * Process the updated 'smoothscroll' option value.
4086 */
4087 char *
4088did_set_smoothscroll(optset_T *args UNUSED)
4089{
4090 if (curwin->w_p_sms)
4091 return NULL;
4092
4093 curwin->w_skipcol = 0;
4094 changed_line_abv_curs();
4095 return NULL;
4096}
4097
4098#if defined(FEAT_SPELL) || defined(PROTO)
4099/*
4100 * Process the updated 'spell' option value.
4101 */
4102 char *
4103did_set_spell(optset_T *args UNUSED)
4104{
4105 if (curwin->w_p_spell)
4106 return parse_spelllang(curwin);
4107
4108 return NULL;
4109}
4110#endif
4111
4112/*
4113 * Process the updated 'swapfile' option value.
4114 */
4115 char *
4116did_set_swapfile(optset_T *args UNUSED)
4117{
4118 // when 'swf' is set, create swapfile, when reset remove swapfile
4119 if (curbuf->b_p_swf && p_uc)
4120 ml_open_file(curbuf); // create the swap file
4121 else
4122 // no need to reset curbuf->b_may_swap, ml_open_file() will check
4123 // buf->b_p_swf
4124 mf_close_file(curbuf, TRUE); // remove the swap file
4125 return NULL;
4126}
4127
4128#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004129 char *
4130did_set_termguicolors(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004131{
4132# ifdef FEAT_VTP
4133 // Do not turn on 'tgc' when 24-bit colors are not supported.
4134 if (
4135# ifdef VIMDLL
4136 !gui.in_use && !gui.starting &&
4137# endif
4138 !has_vtp_working())
4139 {
4140 p_tgc = 0;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004141 args->os_doskip = TRUE;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004142 return e_24_bit_colors_are_not_supported_on_this_environment;
4143 }
4144 if (is_term_win32())
4145 swap_tcap();
4146# endif
4147# ifdef FEAT_GUI
4148 if (!gui.in_use && !gui.starting)
4149# endif
4150 highlight_gui_started();
4151# ifdef FEAT_VTP
4152 // reset t_Co
4153 if (is_term_win32())
4154 {
4155 control_console_color_rgb();
4156 set_termname(T_NAME);
4157 init_highlight(TRUE, FALSE);
4158 }
4159# endif
4160# ifdef FEAT_TERMINAL
4161 term_update_colors_all();
4162 term_update_palette_all();
4163 term_update_wincolor_all();
4164# endif
4165
4166 return NULL;
4167}
4168#endif
4169
4170/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004171 * Process the updated 'terse' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004173 char *
4174did_set_terse(optset_T *args UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004176 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004178 // when 'terse' is set change 'shortmess'
4179 p = vim_strchr(p_shm, SHM_SEARCH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004181 // insert 's' in p_shm
4182 if (p_terse && p == NULL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004183 {
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004184 STRCPY(IObuff, p_shm);
4185 STRCAT(IObuff, "s");
4186 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004187 }
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004188 // remove 's' from p_shm
4189 else if (!p_terse && p != NULL)
4190 STRMOVE(p, p + 1);
4191 return NULL;
4192}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004194/*
4195 * Process the updated 'textauto' option value.
4196 */
4197 char *
4198did_set_textauto(optset_T *args)
4199{
4200 // when 'textauto' is set or reset also change 'fileformats'
4201 set_string_option_direct((char_u *)"ffs", -1,
4202 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
4203 OPT_FREE | args->os_flags, 0);
Bram Moolenaar53744302015-07-17 17:38:22 +02004204
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004205 return NULL;
4206}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004208/*
4209 * Process the updated 'textmode' option value.
4210 */
4211 char *
4212did_set_textmode(optset_T *args)
4213{
4214 // when 'textmode' is set or reset also change 'fileformat'
4215 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, args->os_flags);
4216
4217 return NULL;
4218}
4219
4220/*
4221 * Process the new 'textwidth' option value.
4222 */
4223 char *
4224did_set_textwidth(optset_T *args UNUSED)
4225{
4226 char *errmsg = NULL;
4227
4228 if (curbuf->b_p_tw < 0)
4229 {
4230 errmsg = e_argument_must_be_positive;
4231 curbuf->b_p_tw = 0;
4232 }
4233#ifdef FEAT_SYN_HL
4234 {
4235 win_T *wp;
4236 tabpage_T *tp;
4237
4238 FOR_ALL_TAB_WINDOWS(tp, wp)
4239 check_colorcolumn(wp);
4240 }
Bram Moolenaar53744302015-07-17 17:38:22 +02004241#endif
4242
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004243 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244}
4245
4246/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004247 * Process the updated 'title' or the 'icon' option value.
4248 */
4249 char *
4250did_set_title_icon(optset_T *args UNUSED)
4251{
4252 // when 'title' changed, may need to change the title; same for 'icon'
4253 did_set_title();
4254 return NULL;
4255}
4256
4257/*
4258 * Process the new 'titlelen' option value.
4259 */
4260 char *
4261did_set_titlelen(optset_T *args)
4262{
4263 long old_value = args->os_oldval.number;
4264 char *errmsg = NULL;
4265
4266 // if 'titlelen' has changed, redraw the title
4267 if (p_titlelen < 0)
4268 {
4269 errmsg = e_argument_must_be_positive;
4270 p_titlelen = 85;
4271 }
4272 if (starting != NO_SCREEN && old_value != p_titlelen)
4273 need_maketitle = TRUE;
4274
4275 return errmsg;
4276}
4277
4278#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
4279/*
4280 * Process the updated 'undofile' option value.
4281 */
4282 char *
4283did_set_undofile(optset_T *args)
4284{
4285 // Only take action when the option was set.
4286 if (!curbuf->b_p_udf && !p_udf)
4287 return NULL;
4288
4289 // When reset we do not delete the undo file, the option may be set again
4290 // without making any changes in between.
4291 char_u hash[UNDO_HASH_SIZE];
4292 buf_T *save_curbuf = curbuf;
4293
4294 FOR_ALL_BUFFERS(curbuf)
4295 {
4296 // When 'undofile' is set globally: for every buffer, otherwise
4297 // only for the current buffer: Try to read in the undofile,
4298 // if one exists, the buffer wasn't changed and the buffer was
4299 // loaded
4300 if ((curbuf == save_curbuf
4301 || (args->os_flags & OPT_GLOBAL)
4302 || args->os_flags == 0)
4303 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
4304 {
4305#ifdef FEAT_CRYPT
Christian Brabandtaae58342023-04-23 17:50:22 +01004306 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004307 continue;
4308#endif
4309 u_compute_hash(hash);
4310 u_read_undo(NULL, hash, curbuf->b_fname);
4311 }
4312 }
4313 curbuf = save_curbuf;
4314
4315 return NULL;
4316}
4317#endif
4318
4319/*
4320 * Process the new global 'undolevels' option value.
4321 */
4322 static void
4323update_global_undolevels(long value, long old_value)
4324{
4325 // sync undo before 'undolevels' changes
4326
4327 // use the old value, otherwise u_sync() may not work properly
4328 p_ul = old_value;
4329 u_sync(TRUE);
4330 p_ul = value;
4331}
4332
4333/*
4334 * Process the new buffer local 'undolevels' option value.
4335 */
4336 static void
4337update_buflocal_undolevels(long value, long old_value)
4338{
4339 // use the old value, otherwise u_sync() may not work properly
4340 curbuf->b_p_ul = old_value;
4341 u_sync(TRUE);
4342 curbuf->b_p_ul = value;
4343}
4344
4345/*
4346 * Process the new 'undolevels' option value.
4347 */
4348 char *
4349did_set_undolevels(optset_T *args)
4350{
4351 long *pp = (long *)args->os_varp;
4352
4353 if (pp == &p_ul) // global 'undolevels'
4354 update_global_undolevels(args->os_newval.number,
4355 args->os_oldval.number);
4356 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4357 update_buflocal_undolevels(args->os_newval.number,
4358 args->os_oldval.number);
4359
4360 return NULL;
4361}
4362
4363/*
4364 * Process the new 'updatecount' option value.
4365 */
4366 char *
4367did_set_updatecount(optset_T *args)
4368{
4369 long old_value = args->os_oldval.number;
4370 char *errmsg = NULL;
4371
4372 // when 'updatecount' changes from zero to non-zero, open swap files
4373 if (p_uc < 0)
4374 {
4375 errmsg = e_argument_must_be_positive;
4376 p_uc = 100;
4377 }
4378 if (p_uc && !old_value)
4379 ml_open_files();
4380
4381 return errmsg;
4382}
4383
4384/*
4385 * Process the updated 'weirdinvert' option value.
4386 */
4387 char *
4388did_set_weirdinvert(optset_T *args)
4389{
4390 // When 'weirdinvert' changed, set/reset 't_xs'.
4391 // Then set 'weirdinvert' according to value of 't_xs'.
4392 if (p_wiv && !args->os_oldval.boolean)
4393 T_XS = (char_u *)"y";
4394 else if (!p_wiv && args->os_oldval.boolean)
4395 T_XS = empty_option;
4396 p_wiv = (*T_XS != NUL);
4397
4398 return NULL;
4399}
4400
4401/*
4402 * Process the new 'window' option value.
4403 */
4404 char *
4405did_set_window(optset_T *args UNUSED)
4406{
4407 if (p_window < 1)
4408 p_window = 1;
4409 else if (p_window >= Rows)
4410 p_window = Rows - 1;
4411 return NULL;
4412}
4413
4414/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004415 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004417 char *
4418did_set_winheight_helpheight(optset_T *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004420 long *pp = (long *)args->os_varp;
4421 char *errmsg = NULL;
4422
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004423 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004425 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004426 p_wh = 1;
4427 }
4428 if (p_wmh > p_wh)
4429 {
4430 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4431 p_wh = p_wmh;
4432 }
4433 if (p_hh < 0)
4434 {
4435 errmsg = e_argument_must_be_positive;
4436 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
4438
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004439 // Change window height NOW
4440 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004442 if (pp == &p_wh && curwin->w_height < p_wh)
4443 win_setheight((int)p_wh);
4444 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
4445 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004448 return errmsg;
4449}
4450
4451/*
4452 * Process the new 'winminheight' option value.
4453 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004454 char *
4455did_set_winminheight(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004456{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004457 char *errmsg = NULL;
4458
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004459 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004461 errmsg = e_argument_must_be_positive;
4462 p_wmh = 0;
4463 }
4464 if (p_wmh > p_wh)
4465 {
4466 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4467 p_wmh = p_wh;
4468 }
4469 win_setminheight();
4470
4471 return errmsg;
4472}
4473
4474/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004475 * Process the new 'winminwidth' option value.
4476 */
4477 char *
4478did_set_winminwidth(optset_T *args UNUSED)
4479{
4480 char *errmsg = NULL;
4481
4482 if (p_wmw < 0)
4483 {
4484 errmsg = e_argument_must_be_positive;
4485 p_wmw = 0;
4486 }
4487 if (p_wmw > p_wiw)
4488 {
4489 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4490 p_wmw = p_wiw;
4491 }
4492 win_setminwidth();
4493
4494 return errmsg;
4495}
4496
4497/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004498 * Process the new 'winwidth' option value.
4499 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004500 char *
4501did_set_winwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004502{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004503 char *errmsg = NULL;
4504
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004505 if (p_wiw < 1)
4506 {
4507 errmsg = e_argument_must_be_positive;
4508 p_wiw = 1;
4509 }
4510 if (p_wmw > p_wiw)
4511 {
4512 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4513 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004515
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004516 // Change window width NOW
4517 if (!ONE_WINDOW && curwin->w_width < p_wiw)
4518 win_setwidth((int)p_wiw);
4519
4520 return errmsg;
4521}
4522
4523/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004524 * Process the updated 'wrap' option value.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004525 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004526 char *
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004527did_set_wrap(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004528{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004529 // If 'wrap' is set, set w_leftcol to zero.
4530 if (curwin->w_p_wrap)
4531 curwin->w_leftcol = 0;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004532 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004533}
4534
4535/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004536 * Set the value of a boolean option, and take care of side effects.
4537 * Returns NULL for success, or an error message for an error.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004538 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004539 static char *
4540set_bool_option(
4541 int opt_idx, // index in options[] table
4542 char_u *varp, // pointer to the option variable
4543 int value, // new value
4544 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004545{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004546 int old_value = *(int *)varp;
4547#if defined(FEAT_EVAL)
4548 int old_global_value = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004550 char *errmsg = NULL;
4551
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004552 // Disallow changing some options from secure mode
4553 if ((secure
4554#ifdef HAVE_SANDBOX
4555 || sandbox != 0
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004556#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004557 ) && (options[opt_idx].flags & P_SECURE))
4558 return e_not_allowed_here;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004559
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004560#if defined(FEAT_EVAL)
4561 // Save the global value before changing anything. This is needed as for
4562 // a global-only option setting the "local value" in fact sets the global
4563 // value (since there is only one value).
4564 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4565 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
4566 OPT_GLOBAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004568
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004569 *(int *)varp = value; // set the new value
4570#ifdef FEAT_EVAL
4571 // Remember where the option was set.
4572 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004573#endif
4574
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004576 need_mouse_correct = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004579 // May set global value for local option.
4580 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4581 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004582
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004583 // Handle side effects of changing a bool option.
4584 if (options[opt_idx].opt_did_set_cb != NULL)
4585 {
4586 optset_T args;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004587
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004588 CLEAR_FIELD(args);
4589 args.os_varp = varp;
4590 args.os_flags = opt_flags;
4591 args.os_oldval.boolean = old_value;
4592 args.os_newval.boolean = value;
4593 args.os_errbuf = NULL;
4594 errmsg = options[opt_idx].opt_did_set_cb(&args);
4595 if (args.os_doskip)
4596 return errmsg;
4597 }
4598
4599 // after handling side effects, call autocommand
4600
4601 options[opt_idx].flags |= P_WAS_SET;
4602
4603#if defined(FEAT_EVAL)
4604 apply_optionset_autocmd(opt_idx, opt_flags,
4605 (long)(old_value ? TRUE : FALSE),
4606 (long)(old_global_value ? TRUE : FALSE),
4607 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004608#endif
4609
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004610 comp_col(); // in case 'ruler' or 'showcmd' changed
4611 if (curwin->w_curswant != MAXCOL
4612 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
4613 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004614
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004615 if ((opt_flags & OPT_NO_REDRAW) == 0)
4616 check_redraw(options[opt_idx].flags);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004617
4618 return errmsg;
4619}
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004620
4621/*
4622 * Check the bounds of numeric options.
4623 */
4624 static char *
4625check_num_option_bounds(
4626 long *pp,
4627 long old_value,
4628 long old_Rows,
4629 long old_Columns,
4630 char *errbuf,
4631 size_t errbuflen,
4632 char *errmsg)
4633{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 if (Rows < min_rows() && full_screen)
4635 {
4636 if (errbuf != NULL)
4637 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004638 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004639 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 errmsg = errbuf;
4641 }
4642 Rows = min_rows();
4643 }
4644 if (Columns < MIN_COLUMNS && full_screen)
4645 {
4646 if (errbuf != NULL)
4647 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004648 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004649 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 errmsg = errbuf;
4651 }
4652 Columns = MIN_COLUMNS;
4653 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004654 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004656 // If the screen (shell) height has been changed, assume it is the
4657 // physical screenheight.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 if (old_Rows != Rows || old_Columns != Columns)
4659 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004660 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 if (updating_screen)
4662 *pp = old_value;
4663 else if (full_screen
4664#ifdef FEAT_GUI
4665 && !gui.starting
4666#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004667 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 set_shellsize((int)Columns, (int)Rows, TRUE);
4669 else
4670 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004671 // Postpone the resizing; check the size and cmdline position for
4672 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 check_shellsize();
4674 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4675 cmdline_row = Rows - p_ch;
4676 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004677 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004678 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 }
4680
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 if (curbuf->b_p_ts <= 0)
4682 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004683 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 curbuf->b_p_ts = 8;
4685 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004686 else if (curbuf->b_p_ts > TABSTOP_MAX)
4687 {
4688 errmsg = e_invalid_argument;
4689 curbuf->b_p_ts = 8;
4690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 if (p_tm < 0)
4692 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004693 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 p_tm = 0;
4695 }
4696 if ((curwin->w_p_scr <= 0
4697 || (curwin->w_p_scr > curwin->w_height
4698 && curwin->w_height > 0))
4699 && full_screen)
4700 {
4701 if (pp == &(curwin->w_p_scr))
4702 {
4703 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004704 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 win_comp_scroll(curwin);
4706 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004707 // If 'scroll' became invalid because of a side effect silently adjust
4708 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 else if (curwin->w_p_scr <= 0)
4710 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004711 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 curwin->w_p_scr = curwin->w_height;
4713 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004714 if (p_hi < 0)
4715 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004716 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004717 p_hi = 0;
4718 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004719 else if (p_hi > 10000)
4720 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004721 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004722 p_hi = 10000;
4723 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004724 if (p_re < 0 || p_re > 2)
4725 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004726 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004727 p_re = 0;
4728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 if (p_report < 0)
4730 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004731 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 p_report = 1;
4733 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004734 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004736 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 p_sj = Rows / 2;
4738 else
4739 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004740 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 p_sj = 1;
4742 }
4743 }
4744 if (p_so < 0 && full_screen)
4745 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004746 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 p_so = 0;
4748 }
4749 if (p_siso < 0 && full_screen)
4750 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004751 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 p_siso = 0;
4753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 if (p_cwh < 1)
4755 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004756 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 p_cwh = 1;
4758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 if (p_ut < 0)
4760 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004761 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 p_ut = 2000;
4763 }
4764 if (p_ss < 0)
4765 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004766 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 p_ss = 0;
4768 }
4769
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004770 return errmsg;
4771}
4772
4773/*
4774 * Set the value of a number option, and take care of side effects.
4775 * Returns NULL for success, or an error message for an error.
4776 */
4777 static char *
4778set_num_option(
4779 int opt_idx, // index in options[] table
4780 char_u *varp, // pointer to the option variable
4781 long value, // new value
4782 char *errbuf, // buffer for error messages
4783 size_t errbuflen, // length of "errbuf"
4784 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4785 // OPT_MODELINE, etc.
4786{
4787 char *errmsg = NULL;
4788 long old_value = *(long *)varp;
4789#if defined(FEAT_EVAL)
4790 long old_global_value = 0; // only used when setting a local and
4791 // global option
4792#endif
4793 long old_Rows = Rows; // remember old Rows
4794 long old_Columns = Columns; // remember old Columns
4795 long *pp = (long *)varp;
4796
4797 // Disallow changing some options from secure mode.
4798 if ((secure
4799#ifdef HAVE_SANDBOX
4800 || sandbox != 0
4801#endif
4802 ) && (options[opt_idx].flags & P_SECURE))
4803 return e_not_allowed_here;
4804
4805#if defined(FEAT_EVAL)
4806 // Save the global value before changing anything. This is needed as for
4807 // a global-only option setting the "local value" in fact sets the global
4808 // value (since there is only one value).
4809 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4810 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4811 OPT_GLOBAL);
4812#endif
4813
4814 *pp = value;
4815#ifdef FEAT_EVAL
4816 // Remember where the option was set.
4817 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4818#endif
4819#ifdef FEAT_GUI
4820 need_mouse_correct = TRUE;
4821#endif
4822
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004823 // Invoke the option specific callback function to validate and apply the
4824 // new value.
4825 if (options[opt_idx].opt_did_set_cb != NULL)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004826 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004827 optset_T args;
4828
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004829 CLEAR_FIELD(args);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004830 args.os_varp = varp;
4831 args.os_flags = opt_flags;
4832 args.os_oldval.number = old_value;
4833 args.os_newval.number = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004834 args.os_errbuf = NULL;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004835 errmsg = options[opt_idx].opt_did_set_cb(&args);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004836 }
4837
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004838 // Check the bounds for numeric options here
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004839 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4840 errbuf, errbuflen, errmsg);
4841
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004842 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4844 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4845
4846 options[opt_idx].flags |= P_WAS_SET;
4847
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004848#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004849 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4850 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004851#endif
4852
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004853 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02004854 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01004855 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004856 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004857 if ((opt_flags & OPT_NO_REDRAW) == 0)
4858 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859
4860 return errmsg;
4861}
4862
4863/*
4864 * Called after an option changed: check if something needs to be redrawn.
4865 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004866 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004867check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004869 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004870 int doclear = (flags & P_RCLR) == P_RCLR;
4871 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004873 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875
4876 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
4877 changed_window_setting();
4878 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004879 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01004880 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004881 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004882 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004883 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004885 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886}
4887
4888/*
4889 * Find index for option 'arg'.
4890 * Return -1 if not found.
4891 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004892 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004893findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894{
4895 int opt_idx;
4896 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004897 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 int is_term_opt;
4899
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004900 // For first call: Initialize the quick-access table.
4901 // It contains the index for the first option that starts with a certain
4902 // letter. There are 26 letters, plus the first "t_" option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 if (quick_tab[1] == 0)
4904 {
4905 p = options[0].fullname;
4906 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4907 {
4908 if (s[0] != p[0])
4909 {
4910 if (s[0] == 't' && s[1] == '_')
4911 quick_tab[26] = opt_idx;
4912 else
4913 quick_tab[CharOrdLow(s[0])] = opt_idx;
4914 }
4915 p = s;
4916 }
4917 }
4918
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004919 // Check for name starting with an illegal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 return -1;
4922
4923 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4924 if (is_term_opt)
4925 opt_idx = quick_tab[26];
4926 else
4927 opt_idx = quick_tab[CharOrdLow(arg[0])];
4928 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4929 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004930 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 break;
4932 }
4933 if (s == NULL && !is_term_opt)
4934 {
4935 opt_idx = quick_tab[CharOrdLow(arg[0])];
4936 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
4937 {
4938 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004939 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 break;
4941 s = NULL;
4942 }
4943 }
4944 if (s == NULL)
4945 opt_idx = -1;
4946 return opt_idx;
4947}
4948
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004949#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
4950 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951/*
4952 * Get the value for an option.
4953 *
4954 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004955 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01004956 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004957 * String option: gov_string, *stringval gets allocated string.
4958 * Hidden Number option: gov_hidden_number.
4959 * Hidden Toggle option: gov_hidden_bool.
4960 * Hidden String option: gov_hidden_string.
4961 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004962 *
4963 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004965 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01004966get_option_value(
4967 char_u *name,
4968 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004969 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004970 int *flagsp,
4971 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972{
4973 int opt_idx;
4974 char_u *varp;
4975
4976 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004977 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01004978 {
4979 int key;
4980
4981 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004982 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004983 {
4984 char_u key_name[2];
4985 char_u *p;
4986
Bram Moolenaar10c75c42021-12-28 20:53:30 +00004987 if (flagsp != NULL)
4988 *flagsp = 0; // terminal option has no flags
4989
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004990 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01004991 if (key < 0)
4992 {
4993 key_name[0] = KEY2TERMCAP0(key);
4994 key_name[1] = KEY2TERMCAP1(key);
4995 }
4996 else
4997 {
4998 key_name[0] = KS_KEY;
4999 key_name[1] = (key & 0xff);
5000 }
5001 p = find_termcode(key_name);
5002 if (p != NULL)
5003 {
5004 if (stringval != NULL)
5005 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005006 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01005007 }
5008 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005009 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01005010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005012 varp = get_varp_scope(&(options[opt_idx]), scope);
5013
5014 if (flagsp != NULL)
5015 // Return the P_xxxx option flags.
5016 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017
5018 if (options[opt_idx].flags & P_STRING)
5019 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005020 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005021 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 if (stringval != NULL)
5023 {
zeertzjq51f0bc32022-05-09 13:33:39 +01005024 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01005025 *stringval = str2special_save(*(char_u **)(varp), FALSE,
5026 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005028 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01005029 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00005030 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01005033 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 *stringval = vim_strsave(*(char_u **)(varp));
5035 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005036 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 }
5038
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005039 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005040 return (options[opt_idx].flags & P_NUM)
5041 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 if (options[opt_idx].flags & P_NUM)
5043 *numval = *(long *)varp;
5044 else
5045 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005046 // Special case: 'modified' is b_changed, but we also want to consider
5047 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 if ((int *)varp == &curbuf->b_changed)
5049 *numval = curbufIsChanged();
5050 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02005051 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005053 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054}
5055#endif
5056
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005057#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005058/*
5059 * Returns the option attributes and its value. Unlike the above function it
5060 * will return either global value or local value of the option depending on
5061 * what was requested, but it will never return global value if it was
5062 * requested to return local one and vice versa. Neither it will return
5063 * buffer-local value if it was requested to return window-local one.
5064 *
5065 * Pretends that option is absent if it is not present in the requested scope
5066 * (i.e. has no global, window-local or buffer-local value depending on
5067 * opt_type). Uses
5068 *
5069 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02005070 * 0 hidden or unknown option, also option that does not have requested
5071 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005072 * see SOPT_* in vim.h for other flags
5073 *
5074 * Possible opt_type values: see SREQ_* in vim.h
5075 */
5076 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005077get_option_value_strict(
5078 char_u *name,
5079 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005080 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01005081 int opt_type,
5082 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005083{
5084 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02005085 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005086 struct vimoption *p;
5087 int r = 0;
5088
5089 opt_idx = findoption(name);
5090 if (opt_idx < 0)
5091 return 0;
5092
5093 p = &(options[opt_idx]);
5094
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005095 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005096 if (p->var == NULL)
5097 return 0;
5098
5099 if (p->flags & P_BOOL)
5100 r |= SOPT_BOOL;
5101 else if (p->flags & P_NUM)
5102 r |= SOPT_NUM;
5103 else if (p->flags & P_STRING)
5104 r |= SOPT_STRING;
5105
5106 if (p->indir == PV_NONE)
5107 {
5108 if (opt_type == SREQ_GLOBAL)
5109 r |= SOPT_GLOBAL;
5110 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005111 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005112 }
5113 else
5114 {
5115 if (p->indir & PV_BOTH)
5116 r |= SOPT_GLOBAL;
5117 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005118 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005119
5120 if (p->indir & PV_WIN)
5121 {
5122 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005123 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005124 else
5125 r |= SOPT_WIN;
5126 }
5127 else if (p->indir & PV_BUF)
5128 {
5129 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005130 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005131 else
5132 r |= SOPT_BUF;
5133 }
5134 }
5135
5136 if (stringval == NULL)
5137 return r;
5138
5139 if (opt_type == SREQ_GLOBAL)
5140 varp = p->var;
5141 else
5142 {
5143 if (opt_type == SREQ_BUF)
5144 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005145 // Special case: 'modified' is b_changed, but we also want to
5146 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005147 if (p->indir == PV_MOD)
5148 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005149 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005150 varp = NULL;
5151 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005152# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005153 else if (p->indir == PV_KEY)
5154 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005155 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005156 *stringval = NULL;
5157 varp = NULL;
5158 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005159# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005160 else
5161 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005162 buf_T *save_curbuf = curbuf;
5163
5164 // only getting a pointer, no need to use aucmd_prepbuf()
5165 curbuf = (buf_T *)from;
5166 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005167 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02005168 curbuf = save_curbuf;
5169 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005170 }
5171 }
5172 else if (opt_type == SREQ_WIN)
5173 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005174 win_T *save_curwin = curwin;
5175
5176 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005177 curbuf = curwin->w_buffer;
5178 varp = get_varp(p);
5179 curwin = save_curwin;
5180 curbuf = curwin->w_buffer;
5181 }
5182 if (varp == p->var)
5183 return (r | SOPT_UNSET);
5184 }
5185
5186 if (varp != NULL)
5187 {
5188 if (p->flags & P_STRING)
5189 *stringval = vim_strsave(*(char_u **)(varp));
5190 else if (p->flags & P_NUM)
5191 *numval = *(long *) varp;
5192 else
5193 *numval = *(int *)varp;
5194 }
5195
5196 return r;
5197}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005198
5199/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005200 * Iterate over options. First argument is a pointer to a pointer to a
5201 * structure inside options[] array, second is option type like in the above
5202 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005203 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005204 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005205 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005206 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005207 * first argument to NULL.
5208 *
5209 * Returns full option name for current option on each call.
5210 */
5211 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005212option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005213{
5214 struct vimoption *ret = NULL;
5215 do
5216 {
5217 if (*option == NULL)
5218 *option = (void *) options;
5219 else if (((struct vimoption *) (*option))->fullname == NULL)
5220 {
5221 *option = NULL;
5222 return NULL;
5223 }
5224 else
5225 *option = (void *) (((struct vimoption *) (*option)) + 1);
5226
5227 ret = ((struct vimoption *) (*option));
5228
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005229 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005230 if (ret->var == NULL)
5231 {
5232 ret = NULL;
5233 continue;
5234 }
5235
5236 switch (opt_type)
5237 {
5238 case SREQ_GLOBAL:
5239 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
5240 ret = NULL;
5241 break;
5242 case SREQ_BUF:
5243 if (!(ret->indir & PV_BUF))
5244 ret = NULL;
5245 break;
5246 case SREQ_WIN:
5247 if (!(ret->indir & PV_WIN))
5248 ret = NULL;
5249 break;
5250 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005251 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005252 return NULL;
5253 }
5254 }
5255 while (ret == NULL);
5256
5257 return (char_u *)ret->fullname;
5258}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005259#endif
5260
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005262 * Return the flags for the option at 'opt_idx'.
5263 */
5264 long_u
5265get_option_flags(int opt_idx)
5266{
5267 return options[opt_idx].flags;
5268}
5269
5270/*
5271 * Set a flag for the option at 'opt_idx'.
5272 */
5273 void
5274set_option_flag(int opt_idx, long_u flag)
5275{
5276 options[opt_idx].flags |= flag;
5277}
5278
5279/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005280 * Returns TRUE if the option at 'opt_idx' is a global option
5281 */
5282 int
5283is_global_option(int opt_idx)
5284{
5285 return options[opt_idx].indir == PV_NONE;
5286}
5287
5288/*
5289 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5290 * local value.
5291 */
5292 int
5293is_global_local_option(int opt_idx)
5294{
5295 return options[opt_idx].indir & PV_BOTH;
5296}
5297
5298/*
5299 * Returns TRUE if the option at 'opt_idx' is a window-local option
5300 */
5301 int
5302is_window_local_option(int opt_idx)
5303{
5304 return options[opt_idx].var == VAR_WIN;
5305}
5306
5307/*
5308 * Returns TRUE if the option at 'opt_idx' is a hidden option
5309 */
5310 int
5311is_hidden_option(int opt_idx)
5312{
5313 return options[opt_idx].var == NULL;
5314}
5315
5316#if defined(FEAT_CRYPT) || defined(PROTO)
5317/*
5318 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5319 */
5320 int
5321is_crypt_key_option(int opt_idx)
5322{
5323 return options[opt_idx].indir == PV_KEY;
5324}
5325#endif
5326
5327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 * Set the value of option "name".
5329 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005330 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005331 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005333 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005334set_option_value(
5335 char_u *name,
5336 long number,
5337 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005338 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339{
5340 int opt_idx;
5341 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005342 long_u flags;
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005343 static char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344
5345 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005346 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005347 {
5348 int key;
5349
5350 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005351 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005352 {
5353 char_u key_name[2];
5354
5355 if (key < 0)
5356 {
5357 key_name[0] = KEY2TERMCAP0(key);
5358 key_name[1] = KEY2TERMCAP1(key);
5359 }
5360 else
5361 {
5362 key_name[0] = KS_KEY;
5363 key_name[1] = (key & 0xff);
5364 }
5365 add_termcode(key_name, string, FALSE);
5366 if (full_screen)
5367 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005368 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005369 return NULL;
5370 }
5371
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005372 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 else
5375 {
5376 flags = options[opt_idx].flags;
5377#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005378 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005380 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005381 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005382 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005385 if (flags & P_STRING)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005386 return set_string_option(opt_idx, string, opt_flags, errbuf);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005387
5388 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5389 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005391 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005393 int idx;
5394
5395 // Either we are given a string or we are setting option
5396 // to zero.
5397 for (idx = 0; string[idx] == '0'; ++idx)
5398 ;
5399 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005400 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005401 // There's another character after zeros or the string
5402 // is empty. In both cases, we are trying to set a
5403 // num option using a string.
5404 semsg(_(e_number_required_after_str_equal_str),
5405 name, string);
5406 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005407
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005410 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005411 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005412 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005413 errbuf, sizeof(errbuf), opt_flags);
5414 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005415 else
5416 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005418
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005420 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421}
5422
5423/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005424 * Call set_option_value() and when an error is returned report it.
5425 */
5426 void
5427set_option_value_give_err(
5428 char_u *name,
5429 long number,
5430 char_u *string,
5431 int opt_flags) // OPT_LOCAL or 0 (both)
5432{
5433 char *errmsg = set_option_value(name, number, string, opt_flags);
5434
5435 if (errmsg != NULL)
5436 emsg(_(errmsg));
5437}
5438
5439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 * Get the terminal code for a terminal option.
5441 * Returns NULL when not found.
5442 */
5443 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005444get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445{
5446 int opt_idx;
5447 char_u *varp;
5448
5449 if (tname[0] != 't' || tname[1] != '_' ||
5450 tname[2] == NUL || tname[3] == NUL)
5451 return NULL;
5452 if ((opt_idx = findoption(tname)) >= 0)
5453 {
5454 varp = get_varp(&(options[opt_idx]));
5455 if (varp != NULL)
5456 varp = *(char_u **)(varp);
5457 return varp;
5458 }
5459 return find_termcode(tname + 2);
5460}
5461
5462 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005463get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464{
5465 int i;
5466
5467 i = findoption((char_u *)"hl");
5468 if (i >= 0)
5469 return options[i].def_val[VI_DEFAULT];
5470 return (char_u *)NULL;
5471}
5472
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005473 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005474get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005475{
5476 int i;
5477
5478 i = findoption((char_u *)"enc");
5479 if (i >= 0)
5480 return options[i].def_val[VI_DEFAULT];
5481 return (char_u *)NULL;
5482}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005483
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005484#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005485 int
5486is_option_allocated(char *name)
5487{
5488 int idx = findoption((char_u *)name);
5489
5490 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5491}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005492#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005493
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494/*
5495 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005496 * When "has_lt" is true there is a '<' before "*arg_arg".
5497 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 */
5499 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005500find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005502 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005504 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005506 // Don't use get_special_key_code() for t_xx, we don't want it to call
5507 // add_termcap_entry().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5509 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005510 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005512 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005514 key = find_special_key(&arg, &modifiers,
5515 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005516 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 key = 0;
5518 }
5519 return key;
5520}
5521
5522/*
5523 * if 'all' == 0: show changed options
5524 * if 'all' == 1: show all normal options
5525 * if 'all' == 2: show all terminal options
5526 */
5527 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005528showoptions(
5529 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005530 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531{
5532 struct vimoption *p;
5533 int col;
5534 int isterm;
5535 char_u *varp;
5536 struct vimoption **items;
5537 int item_count;
5538 int run;
5539 int row, rows;
5540 int cols;
5541 int i;
5542 int len;
5543
5544#define INC 20
5545#define GAP 3
5546
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005547 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 if (items == NULL)
5549 return;
5550
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005551 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005553 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005555 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005557 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005559 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005561 // Do the loop two times:
5562 // 1. display the short items
5563 // 2. display the long items (only strings and numbers)
5564 // When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 for (run = 1; run <= 2 && !got_int; ++run)
5566 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005567 // collect the items in items[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 item_count = 0;
5569 for (p = &options[0]; p->fullname != NULL; p++)
5570 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005571 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005572 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005573 continue;
5574
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 varp = NULL;
5576 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005577 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 {
5579 if (p->indir != PV_NONE && !isterm)
5580 varp = get_varp_scope(p, opt_flags);
5581 }
5582 else
5583 varp = get_varp(p);
5584 if (varp != NULL
5585 && ((all == 2 && isterm)
5586 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005587 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005589 if (opt_flags & OPT_ONECOLUMN)
5590 len = Columns;
5591 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005592 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 else
5594 {
5595 option_value2string(p, opt_flags);
5596 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5597 }
5598 if ((len <= INC - GAP && run == 1) ||
5599 (len > INC - GAP && run == 2))
5600 items[item_count++] = p;
5601 }
5602 }
5603
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005604 // display the items
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 if (run == 1)
5606 {
5607 cols = (Columns + GAP - 3) / INC;
5608 if (cols == 0)
5609 cols = 1;
5610 rows = (item_count + cols - 1) / cols;
5611 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005612 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 rows = item_count;
5614 for (row = 0; row < rows && !got_int; ++row)
5615 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005616 msg_putchar('\n'); // go to next line
5617 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 break;
5619 col = 0;
5620 for (i = row; i < item_count; i += rows)
5621 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005622 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 showoneopt(items[i], opt_flags);
5624 col += INC;
5625 }
5626 out_flush();
5627 ui_breakcheck();
5628 }
5629 }
5630 vim_free(items);
5631}
5632
5633/*
5634 * Return TRUE if option "p" has its default value.
5635 */
5636 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005637optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638{
5639 int dvi;
5640
5641 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005642 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005643 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005645 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005647 // the cast to long is required for Manx C, long_i is
5648 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005649 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005650 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5652}
5653
5654/*
5655 * showoneopt: show the value of one option
5656 * must not be called with a hidden option!
5657 */
5658 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005659showoneopt(
5660 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005661 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005663 char_u *varp;
5664 int save_silent = silent_mode;
5665
5666 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005667 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
5669 varp = get_varp_scope(p, opt_flags);
5670
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005671 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5673 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005674 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005676 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005678 msg_puts(" ");
5679 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 if (!(p->flags & P_BOOL))
5681 {
5682 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005683 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 option_value2string(p, opt_flags);
5685 msg_outtrans(NameBuff);
5686 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005687
5688 silent_mode = save_silent;
5689 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690}
5691
5692/*
5693 * Write modified options as ":set" commands to a file.
5694 *
5695 * There are three values for "opt_flags":
5696 * OPT_GLOBAL: Write global option values and fresh values of
5697 * buffer-local options (used for start of a session
5698 * file).
5699 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5700 * curwin (used for a vimrc file).
5701 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5702 * and local values for window-local options of
5703 * curwin. Local values are also written when at the
5704 * default value, because a modeline or autocommand
5705 * may have set them when doing ":edit file" and the
5706 * user has set them back at the default or fresh
5707 * value.
5708 * When "local_only" is TRUE, don't write fresh
5709 * values, only local values (for ":mkview").
5710 * (fresh value = value used for a new buffer or window for a local option).
5711 *
5712 * Return FAIL on error, OK otherwise.
5713 */
5714 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005715makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716{
5717 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005718 char_u *varp; // currently used value
5719 char_u *varp_fresh; // local value
5720 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 char *cmd;
5722 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005723 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
5725 /*
5726 * The options that don't have a default (terminal name, columns, lines)
5727 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005728 * Do the loop over "options[]" twice: once for options with the
5729 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005731 for (pri = 1; pri >= 0; --pri)
5732 {
5733 for (p = &options[0]; !istermoption(p); p++)
5734 if (!(p->flags & P_NO_MKRC)
5735 && !istermoption(p)
5736 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005738 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5740 continue;
5741
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005742 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5743 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5745 continue;
5746
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005747 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005749 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 continue;
5751
Bram Moolenaard23b7142021-04-17 21:04:34 +02005752 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5753 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005754 continue;
5755
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 round = 2;
5757 if (p->indir != PV_NONE)
5758 {
5759 if (p->var == VAR_WIN)
5760 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005761 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 if (!(opt_flags & OPT_LOCAL))
5763 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005764 // When fresh value of window-local option is not at the
5765 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5767 {
5768 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005769 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 {
5771 round = 1;
5772 varp_local = varp;
5773 varp = varp_fresh;
5774 }
5775 }
5776 }
5777 }
5778
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005779 // Round 1: fresh value for window-local options.
5780 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 for ( ; round <= 2; varp = varp_local, ++round)
5782 {
5783 if (round == 1 || (opt_flags & OPT_GLOBAL))
5784 cmd = "set";
5785 else
5786 cmd = "setlocal";
5787
5788 if (p->flags & P_BOOL)
5789 {
5790 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5791 return FAIL;
5792 }
5793 else if (p->flags & P_NUM)
5794 {
5795 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5796 return FAIL;
5797 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005798 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005800 int do_endif = FALSE;
5801
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005802 // Don't set 'syntax' and 'filetype' again if the value is
5803 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005804 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005805#if defined(FEAT_SYN_HL)
5806 p->indir == PV_SYN ||
5807#endif
5808 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 {
5810 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5811 *(char_u **)(varp)) < 0
5812 || put_eol(fd) < 0)
5813 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005814 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 }
5816 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005817 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005819 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 {
5821 if (put_line(fd, "endif") == FAIL)
5822 return FAIL;
5823 }
5824 }
5825 }
5826 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 return OK;
5829}
5830
5831#if defined(FEAT_FOLDING) || defined(PROTO)
5832/*
5833 * Generate set commands for the local fold options only. Used when
5834 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5835 */
5836 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005837makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005839 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005841 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 == FAIL
5843# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005844 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005846 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 == FAIL
5848 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5849 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5850 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5851 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5852 )
5853 return FAIL;
5854
5855 return OK;
5856}
5857#endif
5858
5859 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005860put_setstring(
5861 FILE *fd,
5862 char *cmd,
5863 char *name,
5864 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005865 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866{
5867 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005868 char_u *buf = NULL;
5869 char_u *part = NULL;
5870 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871
5872 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5873 return FAIL;
5874 if (*valuep != NULL)
5875 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005876 // Output 'pastetoggle' as key names. For other
5877 // options some characters have to be escaped with
5878 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 if (valuep == &p_pt)
5880 {
5881 s = *valuep;
5882 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005883 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 return FAIL;
5885 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005886 // expand the option value, replace $HOME by ~
5887 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005889 int size = (int)STRLEN(*valuep) + 1;
5890
5891 // replace home directory in the whole option value into "buf"
5892 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005893 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005894 goto fail;
5895 home_replace(NULL, *valuep, buf, size, FALSE);
5896
5897 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005898 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005899 // can be expanded when read back.
5900 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5901 && vim_strchr(*valuep, ',') != NULL)
5902 {
5903 part = alloc(size);
5904 if (part == NULL)
5905 goto fail;
5906
5907 // write line break to clear the option, e.g. ':set rtp='
5908 if (put_eol(fd) == FAIL)
5909 goto fail;
5910
5911 p = buf;
5912 while (*p != NUL)
5913 {
5914 // for each comma separated option part, append value to
5915 // the option, :set rtp+=value
5916 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5917 goto fail;
5918 (void)copy_option_part(&p, part, size, ",");
5919 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5920 goto fail;
5921 }
5922 vim_free(buf);
5923 vim_free(part);
5924 return OK;
5925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005927 {
5928 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005930 }
5931 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 }
5933 else if (put_escstr(fd, *valuep, 2) == FAIL)
5934 return FAIL;
5935 }
5936 if (put_eol(fd) < 0)
5937 return FAIL;
5938 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005939fail:
5940 vim_free(buf);
5941 vim_free(part);
5942 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943}
5944
5945 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005946put_setnum(
5947 FILE *fd,
5948 char *cmd,
5949 char *name,
5950 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951{
5952 long wc;
5953
5954 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5955 return FAIL;
5956 if (wc_use_keyname((char_u *)valuep, &wc))
5957 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005958 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5960 return FAIL;
5961 }
5962 else if (fprintf(fd, "%ld", *valuep) < 0)
5963 return FAIL;
5964 if (put_eol(fd) < 0)
5965 return FAIL;
5966 return OK;
5967}
5968
5969 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005970put_setbool(
5971 FILE *fd,
5972 char *cmd,
5973 char *name,
5974 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005976 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005977 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5979 || put_eol(fd) < 0)
5980 return FAIL;
5981 return OK;
5982}
5983
5984/*
5985 * Clear all the terminal options.
5986 * If the option has been allocated, free the memory.
5987 * Terminal options are never hidden or indirect.
5988 */
5989 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005990clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005992 // Reset a few things before clearing the old options. This may cause
5993 // outputting a few things that the terminal doesn't understand, but the
5994 // screen will be cleared later, so this is OK.
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005995 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005996 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005998 // When starting the GUI close the display opened for the clipboard.
5999 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 if (gui.starting)
6001 clear_xterm_clip();
6002#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006003 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006005 free_termoptions();
6006}
6007
6008 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006009free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006010{
6011 struct vimoption *p;
6012
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006013 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 if (istermoption(p))
6015 {
6016 if (p->flags & P_ALLOCED)
6017 free_string_option(*(char_u **)(p->var));
6018 if (p->flags & P_DEF_ALLOCED)
6019 free_string_option(p->def_val[VI_DEFAULT]);
6020 *(char_u **)(p->var) = empty_option;
6021 p->def_val[VI_DEFAULT] = empty_option;
6022 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006023#ifdef FEAT_EVAL
6024 // remember where the option was cleared
6025 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
6026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 }
6028 clear_termcodes();
6029}
6030
6031/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00006032 * Free the string for one term option, if it was allocated.
6033 * Set the string to empty_option and clear allocated flag.
6034 * "var" points to the option value.
6035 */
6036 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006037free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00006038{
6039 struct vimoption *p;
6040
6041 for (p = &options[0]; p->fullname != NULL; p++)
6042 if (p->var == var)
6043 {
6044 if (p->flags & P_ALLOCED)
6045 free_string_option(*(char_u **)(p->var));
6046 *(char_u **)(p->var) = empty_option;
6047 p->flags &= ~P_ALLOCED;
6048 break;
6049 }
6050}
6051
6052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 * Set the terminal option defaults to the current value.
6054 * Used after setting the terminal name.
6055 */
6056 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006057set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058{
6059 struct vimoption *p;
6060
6061 for (p = &options[0]; p->fullname != NULL; p++)
6062 {
6063 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
6064 {
6065 if (p->flags & P_DEF_ALLOCED)
6066 {
6067 free_string_option(p->def_val[VI_DEFAULT]);
6068 p->flags &= ~P_DEF_ALLOCED;
6069 }
6070 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
6071 if (p->flags & P_ALLOCED)
6072 {
6073 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006074 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 }
6076 }
6077 }
6078}
6079
6080/*
6081 * return TRUE if 'p' starts with 't_'
6082 */
6083 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006084istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085{
6086 return (p->fullname[0] == 't' && p->fullname[1] == '_');
6087}
6088
Bram Moolenaardac13472019-09-16 21:06:21 +02006089/*
6090 * Returns TRUE if the option at 'opt_idx' starts with 't_'
6091 */
6092 int
6093istermoption_idx(int opt_idx)
6094{
6095 return istermoption(&options[opt_idx]);
6096}
6097
Bram Moolenaar113e1072019-01-20 15:30:40 +01006098#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006100 * Unset local option value, similar to ":set opt<".
6101 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006102 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006103unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006104{
6105 struct vimoption *p;
6106 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006107 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006108
6109 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02006110 if (opt_idx < 0)
6111 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006112 p = &(options[opt_idx]);
6113
6114 switch ((int)p->indir)
6115 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006116 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006117 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006118 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006119 break;
6120 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006121 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006122 break;
6123 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006124 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006125 break;
6126 case PV_AR:
6127 buf->b_p_ar = -1;
6128 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006129 case PV_BKC:
6130 clear_string_option(&buf->b_p_bkc);
6131 buf->b_bkc_flags = 0;
6132 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006133 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006134 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006135 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006136 case PV_TC:
6137 clear_string_option(&buf->b_p_tc);
6138 buf->b_tc_flags = 0;
6139 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006140 case PV_SISO:
6141 curwin->w_p_siso = -1;
6142 break;
6143 case PV_SO:
6144 curwin->w_p_so = -1;
6145 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006146# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006147 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006148 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006149 break;
6150 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006151 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006152 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006153# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006154 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006155 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006156 break;
6157 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006158 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006159 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006160# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006161 case PV_TSRFU:
6162 clear_string_option(&buf->b_p_tsrfu);
6163 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006164# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006165 case PV_FP:
6166 clear_string_option(&buf->b_p_fp);
6167 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006168# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006169 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006170 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006171 break;
6172 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006173 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006174 break;
6175 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006176 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006177 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006178# endif
6179# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006180 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006181 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006182 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006183# endif
6184# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006185 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006186 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006187 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006188# endif
6189# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006190 case PV_SBR:
6191 clear_string_option(&((win_T *)from)->w_p_sbr);
6192 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006193# endif
6194# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006195 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006196 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006197 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006198# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006199 case PV_UL:
6200 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6201 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006202 case PV_LW:
6203 clear_string_option(&buf->b_p_lw);
6204 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006205 case PV_MENC:
6206 clear_string_option(&buf->b_p_menc);
6207 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006208 case PV_LCS:
6209 clear_string_option(&((win_T *)from)->w_p_lcs);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006210 set_listchars_option((win_T *)from, ((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006211 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006212 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006213 case PV_FCS:
6214 clear_string_option(&((win_T *)from)->w_p_fcs);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006215 set_fillchars_option((win_T *)from, ((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006216 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006217 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006218 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006219 clear_string_option(&((win_T *)from)->w_p_ve);
6220 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006221 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006222 }
6223}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006224#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006225
6226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006228 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 */
6230 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006231get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006233 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 {
6235 if (p->var == VAR_WIN)
6236 return (char_u *)GLOBAL_WO(get_varp(p));
6237 return p->var;
6238 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006239 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 {
6241 switch ((int)p->indir)
6242 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006243 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006245 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
6246 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6247 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006249 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6250 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6251 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006252 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006253 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006254 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006255 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6256 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006258 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6259 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006261 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6262 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006263#ifdef FEAT_COMPL_FUNC
6264 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6265#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006266#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6267 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6268#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006269#if defined(FEAT_CRYPT)
6270 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6271#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006272#ifdef FEAT_LINEBREAK
6273 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6274#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006275#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006276 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006277#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006278 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006279 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006280 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006281 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006282 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006283 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006284 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006285
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006287 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 }
6289 return get_varp(p);
6290}
6291
6292/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006293 * Get pointer to option variable at 'opt_idx', depending on local or global
6294 * scope.
6295 */
6296 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006297get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006298{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006299 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006300}
6301
6302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 * Get pointer to option variable.
6304 */
6305 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006306get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006308 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 if (p->var == NULL)
6310 return NULL;
6311
6312 switch ((int)p->indir)
6313 {
6314 case PV_NONE: return p->var;
6315
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006316 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006317 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006319 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006321 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006323 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006325 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006327 case PV_TC: return *curbuf->b_p_tc != NUL
6328 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006329 case PV_BKC: return *curbuf->b_p_bkc != NUL
6330 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006331 case PV_SISO: return curwin->w_p_siso >= 0
6332 ? (char_u *)&(curwin->w_p_siso) : p->var;
6333 case PV_SO: return curwin->w_p_so >= 0
6334 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006336 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006338 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6340#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006341 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006343 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006345#ifdef FEAT_COMPL_FUNC
6346 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6347 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6348#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006349 case PV_FP: return *curbuf->b_p_fp != NUL
6350 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006352 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006354 case PV_GP: return *curbuf->b_p_gp != NUL
6355 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6356 case PV_MP: return *curbuf->b_p_mp != NUL
6357 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006359#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6360 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6361 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6362#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006363#if defined(FEAT_CRYPT)
6364 case PV_CM: return *curbuf->b_p_cm != NUL
6365 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6366#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006367#ifdef FEAT_LINEBREAK
6368 case PV_SBR: return *curwin->w_p_sbr != NUL
6369 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6370#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006371#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006372 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006373 ? (char_u *)&(curwin->w_p_stl) : p->var;
6374#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006375 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6376 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006377 case PV_LW: return *curbuf->b_p_lw != NUL
6378 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006379 case PV_MENC: return *curbuf->b_p_menc != NUL
6380 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381#ifdef FEAT_ARABIC
6382 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6383#endif
6384 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006385 case PV_LCS: return *curwin->w_p_lcs != NUL
6386 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006387 case PV_FCS: return *curwin->w_p_fcs != NUL
6388 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006389 case PV_VE: return *curwin->w_p_ve != NUL
6390 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006391#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006392 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6393#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006394#ifdef FEAT_SYN_HL
6395 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6396 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006397 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006398 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400#ifdef FEAT_DIFF
6401 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6402#endif
6403#ifdef FEAT_FOLDING
6404 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6405 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6406 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6407 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6408 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6409 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6410 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6411# ifdef FEAT_EVAL
6412 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6413 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6414# endif
6415 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6416#endif
6417 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006418 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006419#ifdef FEAT_LINEBREAK
6420 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006423 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006424#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6426#endif
6427#ifdef FEAT_RIGHTLEFT
6428 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6429 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6430#endif
6431 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006432 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6434#ifdef FEAT_LINEBREAK
6435 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006436 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6437 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006439 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006441 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006442#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006443 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6444 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6445#endif
6446#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006447 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6448 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6449 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451
6452 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6453 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6456 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6458 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6460 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6461 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006462 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465#ifdef FEAT_FOLDING
6466 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006469#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006470 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006472#ifdef FEAT_COMPL_FUNC
6473 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006474 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006475#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006476#ifdef FEAT_EVAL
6477 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6478#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006479 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006481 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006487 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6489 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6490 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6491 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6492#ifdef FEAT_FIND_ID
6493# ifdef FEAT_EVAL
6494 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6495# endif
6496#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006497#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6499 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6500#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006501#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006502 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504#ifdef FEAT_CRYPT
6505 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006508 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6510 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6511 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6512 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6513 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006515 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6522#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006523 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006524 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6525#endif
6526#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006527 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6528 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6529 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006530 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531#endif
6532 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6533 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6534 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6535 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006536#ifdef FEAT_PERSISTENT_UNDO
6537 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6540#ifdef FEAT_KEYMAP
6541 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6542#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006543#ifdef FEAT_SIGNS
6544 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6545#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006546#ifdef FEAT_VARTABS
6547 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6548 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6549#endif
RestorerZ68ebcee2023-05-31 17:12:14 +01006550 default: iemsg(e_get_varp_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006552 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 return (char_u *)&(curbuf->b_p_wm);
6554}
6555
6556/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006557 * Return a pointer to the variable for option at 'opt_idx'
6558 */
6559 char_u *
6560get_option_var(int opt_idx)
6561{
6562 return options[opt_idx].var;
6563}
6564
Dominique Pelle748b3082022-01-08 12:41:16 +00006565#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006566/*
6567 * Return the full name of the option at 'opt_idx'
6568 */
6569 char_u *
6570get_option_fullname(int opt_idx)
6571{
6572 return (char_u *)options[opt_idx].fullname;
6573}
Dominique Pelle748b3082022-01-08 12:41:16 +00006574#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006575
6576/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00006577 * Return the did_set callback function for the option at 'opt_idx'
6578 */
6579 opt_did_set_cb_T
6580get_option_did_set_cb(int opt_idx)
6581{
6582 return options[opt_idx].opt_did_set_cb;
6583}
6584
6585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 * Get the value of 'equalprg', either the buffer-local one or the global one.
6587 */
6588 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006589get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590{
6591 if (*curbuf->b_p_ep == NUL)
6592 return p_ep;
6593 return curbuf->b_p_ep;
6594}
6595
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596/*
6597 * Copy options from one window to another.
6598 * Used when splitting a window.
6599 */
6600 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006601win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602{
6603 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6604 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006605 after_copy_winopt(wp_to);
6606}
6607
6608/*
6609 * After copying window options: update variables depending on options.
6610 */
6611 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006612after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006613{
6614#ifdef FEAT_LINEBREAK
6615 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006616#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006617#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006618 fill_culopt_flags(NULL, wp);
6619 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006620#endif
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006621 set_listchars_option(wp, wp->w_p_lcs, TRUE);
6622 set_fillchars_option(wp, wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006623}
6624
6625 static char_u *
6626copy_option_val(char_u *val)
6627{
6628 if (val == empty_option)
6629 return empty_option; // no need to allocate memory
6630 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632
6633/*
6634 * Copy the options from one winopt_T to another.
6635 * Doesn't free the old option values in "to", use clear_winopt() for that.
6636 * The 'scroll' option is not copied, because it depends on the window height.
6637 * The 'previewwindow' option is reset, there can be only one preview window.
6638 */
6639 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006640copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641{
6642#ifdef FEAT_ARABIC
6643 to->wo_arab = from->wo_arab;
6644#endif
6645 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006646 to->wo_lcs = copy_option_val(from->wo_lcs);
6647 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006649 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006650 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006651 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006652#ifdef FEAT_LINEBREAK
6653 to->wo_nuw = from->wo_nuw;
6654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655#ifdef FEAT_RIGHTLEFT
6656 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006657 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006659#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006660 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006661#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006662#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006663 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006666#ifdef FEAT_DIFF
6667 to->wo_wrap_save = from->wo_wrap_save;
6668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669#ifdef FEAT_LINEBREAK
6670 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006671 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006672 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006674 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006676 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006677 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006678 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006679 to->wo_crb_save = from->wo_crb_save;
Christian Brabandt4a8eb6e2023-08-13 19:43:42 +02006680 to->wo_siso = from->wo_siso;
6681 to->wo_so = from->wo_so;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006682#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006683 to->wo_spell = from->wo_spell;
6684#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006685#ifdef FEAT_SYN_HL
6686 to->wo_cuc = from->wo_cuc;
6687 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006688 to->wo_culopt = copy_option_val(from->wo_culopt);
6689 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691#ifdef FEAT_DIFF
6692 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006693 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006695#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006696 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006697 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006698#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006699#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006700 to->wo_twk = copy_option_val(from->wo_twk);
6701 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703#ifdef FEAT_FOLDING
6704 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006705 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006707 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006708 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 to->wo_fml = from->wo_fml;
6710 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006711 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006712 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006713 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006714 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 to->wo_fdn = from->wo_fdn;
6716# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006717 to->wo_fde = copy_option_val(from->wo_fde);
6718 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006720 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006722#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006723 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006724#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006725
6726#ifdef FEAT_EVAL
6727 // Copy the script context so that we know where the value was last set.
6728 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6729 sizeof(to->wo_script_ctx));
6730#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006731 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732}
6733
6734/*
6735 * Check string options in a window for a NULL value.
6736 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006737 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006738check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739{
6740 check_winopt(&win->w_onebuf_opt);
6741 check_winopt(&win->w_allbuf_opt);
6742}
6743
6744/*
6745 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6746 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006747 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006748check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749{
6750#ifdef FEAT_FOLDING
6751 check_string_option(&wop->wo_fdi);
6752 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006753 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754# ifdef FEAT_EVAL
6755 check_string_option(&wop->wo_fde);
6756 check_string_option(&wop->wo_fdt);
6757# endif
6758 check_string_option(&wop->wo_fmr);
6759#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006760#ifdef FEAT_SIGNS
6761 check_string_option(&wop->wo_scl);
6762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763#ifdef FEAT_RIGHTLEFT
6764 check_string_option(&wop->wo_rlc);
6765#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006766#ifdef FEAT_LINEBREAK
6767 check_string_option(&wop->wo_sbr);
6768#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006769#ifdef FEAT_STL_OPT
6770 check_string_option(&wop->wo_stl);
6771#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006772#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006773 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006774 check_string_option(&wop->wo_cc);
6775#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006776#ifdef FEAT_CONCEAL
6777 check_string_option(&wop->wo_cocu);
6778#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006779#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006780 check_string_option(&wop->wo_twk);
6781 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006782#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006783#ifdef FEAT_LINEBREAK
6784 check_string_option(&wop->wo_briopt);
6785#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006786 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006787 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006788 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006789 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790}
6791
6792/*
6793 * Free the allocated memory inside a winopt_T.
6794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006796clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797{
6798#ifdef FEAT_FOLDING
6799 clear_string_option(&wop->wo_fdi);
6800 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006801 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802# ifdef FEAT_EVAL
6803 clear_string_option(&wop->wo_fde);
6804 clear_string_option(&wop->wo_fdt);
6805# endif
6806 clear_string_option(&wop->wo_fmr);
6807#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006808#ifdef FEAT_SIGNS
6809 clear_string_option(&wop->wo_scl);
6810#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006811#ifdef FEAT_LINEBREAK
6812 clear_string_option(&wop->wo_briopt);
6813#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006814 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815#ifdef FEAT_RIGHTLEFT
6816 clear_string_option(&wop->wo_rlc);
6817#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006818#ifdef FEAT_LINEBREAK
6819 clear_string_option(&wop->wo_sbr);
6820#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006821#ifdef FEAT_STL_OPT
6822 clear_string_option(&wop->wo_stl);
6823#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006824#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006825 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006826 clear_string_option(&wop->wo_cc);
6827#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006828#ifdef FEAT_CONCEAL
6829 clear_string_option(&wop->wo_cocu);
6830#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006831#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006832 clear_string_option(&wop->wo_twk);
6833 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006834#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006835 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006836 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006837 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838}
6839
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006840#ifdef FEAT_EVAL
6841// Index into the options table for a buffer-local option enum.
6842static int buf_opt_idx[BV_COUNT];
6843# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6844
6845/*
6846 * Initialize buf_opt_idx[] if not done already.
6847 */
6848 static void
6849init_buf_opt_idx(void)
6850{
6851 static int did_init_buf_opt_idx = FALSE;
6852 int i;
6853
6854 if (did_init_buf_opt_idx)
6855 return;
6856 did_init_buf_opt_idx = TRUE;
6857 for (i = 0; !istermoption_idx(i); i++)
6858 if (options[i].indir & PV_BUF)
6859 buf_opt_idx[options[i].indir & PV_MASK] = i;
6860}
6861#else
6862# define COPY_OPT_SCTX(buf, bv)
6863#endif
6864
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865/*
6866 * Copy global option values to local options for one buffer.
6867 * Used when creating a new buffer and sometimes when entering a buffer.
6868 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006869 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6871 * appropriate.
6872 * BCO_NOHELP Don't copy the values to a help buffer.
6873 */
6874 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006875buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876{
6877 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006878 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 int dont_do_help;
6880 int did_isk = FALSE;
6881
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006882 // Skip this when the option defaults have not been set yet. Happens when
6883 // main() allocates the first buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 if (p_cpo != NULL)
6885 {
6886 /*
6887 * Always copy when entering and 'cpo' contains 'S'.
6888 * Don't copy when already initialized.
6889 * Don't copy when 'cpo' contains 's' and not entering.
6890 * 'S' BCO_ENTER initialized 's' should_copy
6891 * yes yes X X TRUE
6892 * yes no yes X FALSE
6893 * no X yes X FALSE
6894 * X no no yes FALSE
6895 * X no no no TRUE
6896 * no yes no X TRUE
6897 */
6898 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6899 && (buf->b_p_initialized
6900 || (!(flags & BCO_ENTER)
6901 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6902 should_copy = FALSE;
6903
6904 if (should_copy || (flags & BCO_ALWAYS))
6905 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006906#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006907 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006908 init_buf_opt_idx();
6909#endif
6910 // Don't copy the options specific to a help buffer when
6911 // BCO_NOHELP is given or the options were initialized already
6912 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6914 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006915 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 {
6917 save_p_isk = buf->b_p_isk;
6918 buf->b_p_isk = NULL;
6919 }
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006920 // Always free the allocated strings. If not already initialized,
6921 // reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 if (!buf->b_p_initialized)
6923 {
6924 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006925 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006928 switch (*p_ffs)
6929 {
6930 case 'm':
6931 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6932 case 'd':
6933 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6934 case 'u':
6935 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6936 default:
6937 buf->b_p_ff = vim_strsave(p_ff);
6938 }
6939 if (buf->b_p_ff != NULL)
6940 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 buf->b_p_bh = empty_option;
6942 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 }
6944 else
6945 free_buf_options(buf, FALSE);
6946
6947 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006948 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 buf->b_p_ai_nopaste = p_ai_nopaste;
6950 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006951 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006953 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 buf->b_p_tw_nopaste = p_tw_nopaste;
6955 buf->b_p_tw_nobin = p_tw_nobin;
6956 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006957 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 buf->b_p_wm_nopaste = p_wm_nopaste;
6959 buf->b_p_wm_nobin = p_wm_nobin;
6960 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006961 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006962 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006963 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006964 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006965 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006967 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006969 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006971 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 buf->b_p_ml_nobin = p_ml_nobin;
6973 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006974 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006975 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006976 buf->b_p_swf = FALSE;
6977 else
6978 {
6979 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006980 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006983 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006984#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006985 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006986 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006988#ifdef FEAT_COMPL_FUNC
6989 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006990 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006991 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006992 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006993 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006994 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006995#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006996#ifdef FEAT_EVAL
6997 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006998 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006999 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02007000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007002 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007004#ifdef FEAT_VARTABS
7005 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007006 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007007 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007008 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007009 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007010 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007011 buf->b_p_vsts_nopaste = p_vsts_nopaste
7012 ? vim_strsave(p_vsts_nopaste) : NULL;
7013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007015 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007017 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018#ifdef FEAT_FOLDING
7019 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007020 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021#endif
7022 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007023 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007024 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007025 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02007026 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
7027 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007029 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007031 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007033 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007035 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007036
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007038 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007040 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007042 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01007043 buf->b_p_cinsd = vim_strsave(p_cinsd);
7044 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01007045 buf->b_p_lop = vim_strsave(p_lop);
7046 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007047
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007048 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007051 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007053 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007055 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007057 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00007059 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007060 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01007061 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007062#endif
7063#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02007064 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007065 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007066 (void)compile_cap_prog(&buf->b_s);
7067 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007068 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007069 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007070 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02007071 buf->b_s.b_p_spo = vim_strsave(p_spo);
7072 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007074#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007076 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007078 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01007080 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007081#if defined(FEAT_EVAL)
7082 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007083 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085#ifdef FEAT_CRYPT
7086 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007087 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007090 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091#ifdef FEAT_KEYMAP
7092 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007093 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 buf->b_kmap_state |= KEYMAP_INIT;
7095#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007096#ifdef FEAT_TERMINAL
7097 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007098 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007099#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007100 // This isn't really an option, but copying the langmap and IME
7101 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007103 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007105 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007107 // options that are normally global but also have a local value
7108 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01007110 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007111 buf->b_p_bkc = empty_option;
7112 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113#ifdef FEAT_QUICKFIX
7114 buf->b_p_gp = empty_option;
7115 buf->b_p_mp = empty_option;
7116 buf->b_p_efm = empty_option;
7117#endif
7118 buf->b_p_ep = empty_option;
7119 buf->b_p_kp = empty_option;
7120 buf->b_p_path = empty_option;
7121 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007122 buf->b_p_tc = empty_option;
7123 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124#ifdef FEAT_FIND_ID
7125 buf->b_p_def = empty_option;
7126 buf->b_p_inc = empty_option;
7127# ifdef FEAT_EVAL
7128 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007129 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130# endif
7131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 buf->b_p_dict = empty_option;
7133 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01007134#ifdef FEAT_COMPL_FUNC
7135 buf->b_p_tsrfu = empty_option;
7136#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007137 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007138 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007139#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7140 buf->b_p_bexpr = empty_option;
7141#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007142#if defined(FEAT_CRYPT)
7143 buf->b_p_cm = empty_option;
7144#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007145#ifdef FEAT_PERSISTENT_UNDO
7146 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007147 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007148#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007149 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007150 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007152 // Don't copy the options set by ex_help(), use the saved values,
7153 // when going from a help buffer to a non-help buffer.
7154 // Don't touch these at all when BCO_NOHELP is used and going from
7155 // or to a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007157 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007159#ifdef FEAT_VARTABS
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007160 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007161 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007162 else
7163 buf->b_p_vts_array = NULL;
7164#endif
7165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 else
7167 {
7168 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007169 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 did_isk = TRUE;
7171 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00007172 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007173#ifdef FEAT_VARTABS
7174 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007175 COPY_OPT_SCTX(buf, BV_VTS);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007176 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007177 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007178 else
7179 buf->b_p_vts_array = NULL;
7180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 if (buf->b_p_bt[0] == 'h')
7183 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007185 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 }
7187 }
7188
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007189 // When the options should be copied (ignoring BCO_ALWAYS), set the
7190 // flag that indicates that the options have been initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 if (should_copy)
7192 buf->b_p_initialized = TRUE;
7193 }
7194
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007195 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 if (did_isk)
7197 (void)buf_init_chartab(buf, FALSE);
7198}
7199
7200/*
7201 * Reset the 'modifiable' option and its default value.
7202 */
7203 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007204reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205{
7206 int opt_idx;
7207
7208 curbuf->b_p_ma = FALSE;
7209 p_ma = FALSE;
7210 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007211 if (opt_idx >= 0)
7212 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213}
7214
7215/*
7216 * Set the global value for 'iminsert' to the local value.
7217 */
7218 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007219set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220{
7221 p_iminsert = curbuf->b_p_iminsert;
7222}
7223
7224/*
7225 * Set the global value for 'imsearch' to the local value.
7226 */
7227 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007228set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229{
7230 p_imsearch = curbuf->b_p_imsearch;
7231}
7232
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233static int expand_option_idx = -1;
7234static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7235static int expand_option_flags = 0;
7236
7237 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007238set_context_in_set_cmd(
7239 expand_T *xp,
7240 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007241 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242{
7243 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007244 long_u flags = 0; // init for GCC
7245 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 char_u *p;
7247 char_u *s;
7248 int is_term_option = FALSE;
7249 int key;
7250
7251 expand_option_flags = opt_flags;
7252
7253 xp->xp_context = EXPAND_SETTINGS;
7254 if (*arg == NUL)
7255 {
7256 xp->xp_pattern = arg;
7257 return;
7258 }
7259 p = arg + STRLEN(arg) - 1;
7260 if (*p == ' ' && *(p - 1) != '\\')
7261 {
7262 xp->xp_pattern = p + 1;
7263 return;
7264 }
7265 while (p > arg)
7266 {
7267 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007268 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 if (*p == ' ' || *p == ',')
7270 {
7271 while (s > arg && *(s - 1) == '\\')
7272 --s;
7273 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007274 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 if (*p == ' ' && ((p - s) & 1) == 0)
7276 {
7277 ++p;
7278 break;
7279 }
7280 --p;
7281 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007282 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 {
7284 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007285 xp->xp_prefix = XP_PREFIX_NO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 p += 2;
7287 }
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007288 else if (STRNCMP(p, "inv", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 {
7290 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007291 xp->xp_prefix = XP_PREFIX_INV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 p += 3;
7293 }
7294 xp->xp_pattern = arg = p;
7295 if (*arg == '<')
7296 {
7297 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007298 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 return;
7300 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007301 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 {
7303 xp->xp_context = EXPAND_NOTHING;
7304 return;
7305 }
7306 nextchar = *++p;
7307 is_term_option = TRUE;
7308 expand_option_name[2] = KEY2TERMCAP0(key);
7309 expand_option_name[3] = KEY2TERMCAP1(key);
7310 }
7311 else
7312 {
7313 if (p[0] == 't' && p[1] == '_')
7314 {
7315 p += 2;
7316 if (*p != NUL)
7317 ++p;
7318 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007319 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 nextchar = *++p;
7321 is_term_option = TRUE;
7322 expand_option_name[2] = p[-2];
7323 expand_option_name[3] = p[-1];
7324 }
7325 else
7326 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007327 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7329 p++;
7330 if (*p == NUL)
7331 return;
7332 nextchar = *p;
7333 *p = NUL;
7334 opt_idx = findoption(arg);
7335 *p = nextchar;
7336 if (opt_idx == -1 || options[opt_idx].var == NULL)
7337 {
7338 xp->xp_context = EXPAND_NOTHING;
7339 return;
7340 }
7341 flags = options[opt_idx].flags;
7342 if (flags & P_BOOL)
7343 {
7344 xp->xp_context = EXPAND_NOTHING;
7345 return;
7346 }
7347 }
7348 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007349 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7351 {
7352 ++p;
7353 nextchar = '=';
7354 }
7355 if ((nextchar != '=' && nextchar != ':')
7356 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7357 {
7358 xp->xp_context = EXPAND_UNSUCCESSFUL;
7359 return;
7360 }
7361 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
7362 {
7363 xp->xp_context = EXPAND_OLD_SETTING;
7364 if (is_term_option)
7365 expand_option_idx = -1;
7366 else
7367 expand_option_idx = opt_idx;
7368 xp->xp_pattern = p + 1;
7369 return;
7370 }
7371 xp->xp_context = EXPAND_NOTHING;
7372 if (is_term_option || (flags & P_NUM))
7373 return;
7374
7375 xp->xp_pattern = p + 1;
7376
Doug Kearns6dfdff32023-08-27 18:48:51 +02007377#ifdef FEAT_SYN_HL
7378 if (options[opt_idx].var == (char_u *)&p_syn)
7379 {
7380 xp->xp_context = EXPAND_OWNSYNTAX;
7381 return;
7382 }
7383#endif
7384
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 if (flags & P_EXPAND)
7386 {
7387 p = options[opt_idx].var;
7388 if (p == (char_u *)&p_bdir
7389 || p == (char_u *)&p_dir
7390 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007391 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394#ifdef FEAT_SESSION
7395 || p == (char_u *)&p_vdir
7396#endif
7397 )
7398 {
7399 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007400 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 xp->xp_backslash = XP_BS_THREE;
7402 else
7403 xp->xp_backslash = XP_BS_ONE;
7404 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01007405 else if (p == (char_u *)&p_ft)
7406 {
7407 xp->xp_context = EXPAND_FILETYPE;
7408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 else
7410 {
7411 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007412 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 if (p == (char_u *)&p_tags)
7414 xp->xp_backslash = XP_BS_THREE;
7415 else
7416 xp->xp_backslash = XP_BS_ONE;
7417 }
7418 }
7419
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007420 // For an option that is a list of file names, find the start of the
7421 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
7423 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007424 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 if (*p == ' ' || *p == ',')
7426 {
7427 s = p;
7428 while (s > xp->xp_pattern && *(s - 1) == '\\')
7429 --s;
7430 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
7431 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
7432 {
7433 xp->xp_pattern = p + 1;
7434 break;
7435 }
7436 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007437
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007438#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007439 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007440 if (options[opt_idx].var == (char_u *)&p_sps
7441 && STRNCMP(p, "file:", 5) == 0)
7442 {
7443 xp->xp_pattern = p + 5;
7444 break;
7445 }
7446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448}
7449
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007450/*
7451 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7452 *
7453 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7454 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7455 *
7456 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7457 * regular expression 'regmatch', then stores the match in matches[idx] and
7458 * returns TRUE.
7459 *
7460 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7461 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7462 *
7463 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7464 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7465 */
7466 static int
7467match_str(
7468 char_u *str,
7469 regmatch_T *regmatch,
7470 char_u **matches,
7471 int idx,
7472 int test_only,
7473 int fuzzy,
7474 char_u *fuzzystr,
7475 fuzmatch_str_T *fuzmatch)
7476{
7477 if (!fuzzy)
7478 {
7479 if (vim_regexec(regmatch, str, (colnr_T)0))
7480 {
7481 if (!test_only)
7482 matches[idx] = vim_strsave(str);
7483 return TRUE;
7484 }
7485 }
7486 else
7487 {
7488 int score;
7489
7490 score = fuzzy_match_str(str, fuzzystr);
7491 if (score != 0)
7492 {
7493 if (!test_only)
7494 {
7495 fuzmatch[idx].idx = idx;
7496 fuzmatch[idx].str = vim_strsave(str);
7497 fuzmatch[idx].score = score;
7498 }
7499
7500 return TRUE;
7501 }
7502 }
7503
7504 return FALSE;
7505}
7506
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007508ExpandSettings(
7509 expand_T *xp,
7510 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007511 char_u *fuzzystr,
7512 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007513 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007514 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007516 int num_normal = 0; // Nr of matching non-term-code settings
7517 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 int opt_idx;
7519 int match;
7520 int count = 0;
7521 char_u *str;
7522 int loop;
7523 int is_term_opt;
7524 char_u name_buf[MAX_KEY_NAME_LEN];
7525 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007526 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007527 int fuzzy;
7528 fuzmatch_str_T *fuzmatch = NULL;
7529
Christian Brabandtcb747892022-05-08 21:10:56 +01007530 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007532 // do this loop twice:
7533 // loop == 0: count the number of matching options
7534 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 for (loop = 0; loop <= 1; ++loop)
7536 {
7537 regmatch->rm_ic = ic;
7538 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7539 {
K.Takataeeec2542021-06-02 13:28:16 +02007540 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007541 {
7542 if (match_str((char_u *)names[match], regmatch, *matches,
7543 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 {
7545 if (loop == 0)
7546 num_normal++;
7547 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007548 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 }
7552 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7553 opt_idx++)
7554 {
7555 if (options[opt_idx].var == NULL)
7556 continue;
7557 if (xp->xp_context == EXPAND_BOOL_SETTINGS
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007558 && !(options[opt_idx].flags & P_BOOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007560 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 if (is_term_opt && num_normal > 0)
7562 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007563
7564 if (match_str(str, regmatch, *matches, count, (loop == 0),
7565 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 {
7567 if (loop == 0)
7568 {
7569 if (is_term_opt)
7570 num_term++;
7571 else
7572 num_normal++;
7573 }
7574 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007575 count++;
7576 }
7577 else if (!fuzzy && options[opt_idx].shortname != NULL
7578 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007579 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007580 {
7581 // Compare against the abbreviated option name (for regular
7582 // expression match). Fuzzy matching (previous if) already
7583 // matches against both the expanded and abbreviated names.
7584 if (loop == 0)
7585 {
7586 if (is_term_opt)
7587 num_term++;
7588 else
7589 num_normal++;
7590 }
7591 else
7592 (*matches)[count++] = vim_strsave(str);
7593 }
7594 else if (is_term_opt)
7595 {
7596 name_buf[0] = '<';
7597 name_buf[1] = 't';
7598 name_buf[2] = '_';
7599 name_buf[3] = str[2];
7600 name_buf[4] = str[3];
7601 name_buf[5] = '>';
7602 name_buf[6] = NUL;
7603
7604 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7605 fuzzy, fuzzystr, fuzmatch))
7606 {
7607 if (loop == 0)
7608 num_term++;
7609 else
7610 count++;
7611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 }
7613 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007614
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007615 // Check terminal key codes, these are not in the option table
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7617 {
7618 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7619 {
7620 if (!isprint(str[0]) || !isprint(str[1]))
7621 continue;
7622
7623 name_buf[0] = 't';
7624 name_buf[1] = '_';
7625 name_buf[2] = str[0];
7626 name_buf[3] = str[1];
7627 name_buf[4] = NUL;
7628
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007629 if (match_str(name_buf, regmatch, *matches, count,
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007630 (loop == 0), fuzzy, fuzzystr, fuzmatch))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007631 {
7632 if (loop == 0)
7633 num_term++;
7634 else
7635 count++;
7636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 else
7638 {
7639 name_buf[0] = '<';
7640 name_buf[1] = 't';
7641 name_buf[2] = '_';
7642 name_buf[3] = str[0];
7643 name_buf[4] = str[1];
7644 name_buf[5] = '>';
7645 name_buf[6] = NUL;
7646
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007647 if (match_str(name_buf, regmatch, *matches, count,
7648 (loop == 0), fuzzy, fuzzystr,
7649 fuzmatch))
7650 {
7651 if (loop == 0)
7652 num_term++;
7653 else
7654 count++;
7655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 }
7657 }
7658
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007659 // Check special key names.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007660 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7662 {
7663 name_buf[0] = '<';
7664 STRCPY(name_buf + 1, str);
7665 STRCAT(name_buf, ">");
7666
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007667 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7668 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 {
7670 if (loop == 0)
7671 num_term++;
7672 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007673 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 }
7675 }
7676 }
7677 if (loop == 0)
7678 {
7679 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007680 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007682 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 else
7684 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007685 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007687 *matches = ALLOC_MULT(char_u *, *numMatches);
7688 if (*matches == NULL)
7689 {
7690 *matches = (char_u **)"";
7691 return FAIL;
7692 }
7693 }
7694 else
7695 {
7696 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7697 if (fuzmatch == NULL)
7698 {
7699 *matches = (char_u **)"";
7700 return FAIL;
7701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 }
7703 }
7704 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007705
7706 if (fuzzy &&
7707 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7708 return FAIL;
7709
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 return OK;
7711}
7712
7713 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007714ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007716 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 char_u *buf;
7718
zeertzjqb0d45ec2023-01-25 15:04:22 +00007719 *numMatches = 0;
7720 *matches = ALLOC_ONE(char_u *);
7721 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 return FAIL;
7723
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007724 // For a terminal key code expand_option_idx is < 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 if (expand_option_idx < 0)
7726 {
7727 var = find_termcode(expand_option_name + 2);
7728 if (var == NULL)
7729 expand_option_idx = findoption(expand_option_name);
7730 }
7731
7732 if (expand_option_idx >= 0)
7733 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007734 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 option_value2string(&options[expand_option_idx], expand_option_flags);
7736 var = NameBuff;
7737 }
7738 else if (var == NULL)
7739 var = (char_u *)"";
7740
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007741 // A backslash is required before some characters. This is the reverse of
7742 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 buf = vim_strsave_escaped(var, escape_chars);
7744
7745 if (buf == NULL)
7746 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007747 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 return FAIL;
7749 }
7750
7751#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007752 // For MS-Windows et al. we don't double backslashes at the start and
7753 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007754 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 if (var[0] == '\\' && var[1] == '\\'
7756 && expand_option_idx >= 0
7757 && (options[expand_option_idx].flags & P_EXPAND)
7758 && vim_isfilec(var[2])
7759 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00007760 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761#endif
7762
zeertzjqb0d45ec2023-01-25 15:04:22 +00007763 *matches[0] = buf;
7764 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 return OK;
7766}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767
7768/*
7769 * Get the value for the numeric or string option *opp in a nice format into
7770 * NameBuff[]. Must not be called with a hidden option!
7771 */
7772 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007773option_value2string(
7774 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007775 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776{
7777 char_u *varp;
7778
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007779 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780
7781 if (opp->flags & P_NUM)
7782 {
7783 long wc = 0;
7784
7785 if (wc_use_keyname(varp, &wc))
7786 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
7787 else if (wc != 0)
7788 STRCPY(NameBuff, transchar((int)wc));
7789 else
7790 sprintf((char *)NameBuff, "%ld", *(long *)varp);
7791 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007792 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 {
7794 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007795 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 NameBuff[0] = NUL;
7797#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007798 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007799 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 STRCPY(NameBuff, "*****");
7801#endif
7802 else if (opp->flags & P_EXPAND)
7803 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007804 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 else if ((char_u **)opp->var == &p_pt)
7806 str2specialbuf(p_pt, NameBuff, MAXPATHL);
7807 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007808 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 }
7810}
7811
7812/*
7813 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
7814 * printed as a keyname.
7815 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
7816 */
7817 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007818wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819{
7820 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
7821 {
7822 *wcp = *(long *)varp;
7823 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
7824 return TRUE;
7825 }
7826 return FALSE;
7827}
7828
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 * Return TRUE if "x" is present in 'shortmess' option, or
7831 * 'shortmess' contains 'a' and "x" is present in SHM_A.
7832 */
7833 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007834shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01007836 return p_shm != NULL &&
7837 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 || (vim_strchr(p_shm, 'a') != NULL
7839 && vim_strchr((char_u *)SHM_A, x) != NULL));
7840}
7841
7842/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7844 *
7845 * Reset 'compatible' and set the values for options that didn't get set yet
7846 * to the Vim defaults.
7847 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007848 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 */
7850 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007851vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007853 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007854 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007855 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856
7857 if (!option_was_set((char_u *)"cp"))
7858 {
7859 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007860 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7862 set_option_default(opt_idx, OPT_FREE, FALSE);
7863 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007864 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007866
7867 if (fname != NULL)
7868 {
7869 p = vim_getenv(envname, &dofree);
7870 if (p == NULL)
7871 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007872 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007873 p = FullName_save(fname, FALSE);
7874 if (p != NULL)
7875 {
7876 vim_setenv(envname, p);
7877 vim_free(p);
7878 }
7879 }
7880 else if (dofree)
7881 vim_free(p);
7882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883}
7884
7885/*
7886 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7887 */
7888 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007889change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007891 int opt_idx;
7892
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 if (p_cp != on)
7894 {
7895 p_cp = on;
7896 compatible_set();
7897 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007898 opt_idx = findoption((char_u *)"cp");
7899 if (opt_idx >= 0)
7900 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901}
7902
7903/*
7904 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007905 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 */
7907 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007908option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909{
7910 int idx;
7911
7912 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007913 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 return FALSE;
7915 if (options[idx].flags & P_WAS_SET)
7916 return TRUE;
7917 return FALSE;
7918}
7919
7920/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007921 * Reset the flag indicating option "name" was set.
7922 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007923 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007924reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007925{
7926 int idx = findoption(name);
7927
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007928 if (idx < 0)
7929 return FAIL;
7930
7931 options[idx].flags &= ~P_WAS_SET;
7932 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007933}
7934
7935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 * compatible_set() - Called when 'compatible' has been set or unset.
7937 *
7938 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7939 * flag) to a Vi compatible value.
7940 * When 'compatible' is unset: Set all options that have a different default
7941 * for Vim (without the P_VI_DEF flag) to that default.
7942 */
7943 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007944compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945{
7946 int opt_idx;
7947
Bram Moolenaardac13472019-09-16 21:06:21 +02007948 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7950 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7951 set_option_default(opt_idx, OPT_FREE, p_cp);
7952 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007953 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954}
7955
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 * Check if backspacing over something is allowed.
7958 */
7959 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007960can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007961 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007963#ifdef FEAT_JOB_CHANNEL
7964 if (what == BS_START && bt_prompt(curbuf))
7965 return FALSE;
7966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 switch (*p_bs)
7968 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007969 case '3': return TRUE;
7970 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 case '1': return (what != BS_START);
7972 case '0': return FALSE;
7973 }
7974 return vim_strchr(p_bs, what) != NULL;
7975}
7976
7977/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007978 * Return the effective 'scrolloff' value for the current window, using the
7979 * global value when appropriate.
7980 */
7981 long
7982get_scrolloff_value(void)
7983{
7984 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7985}
7986
7987/*
7988 * Return the effective 'sidescrolloff' value for the current window, using the
7989 * global value when appropriate.
7990 */
7991 long
7992get_sidescrolloff_value(void)
7993{
7994 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7995}
7996
7997/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007998 * Get the local or global value of 'backupcopy'.
7999 */
8000 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008001get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008002{
8003 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
8004}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02008005
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008006#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02008007/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00008008 * Get the local or global value of 'formatlistpat'.
8009 */
8010 char_u *
8011get_flp_value(buf_T *buf)
8012{
Christian Brabandtc53b4672022-01-15 10:01:05 +00008013 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
8014 return p_flp;
8015 return buf->b_p_flp;
8016}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008017#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00008018
8019/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02008020 * Get the local or global value of the 'virtualedit' flags.
8021 */
8022 unsigned int
8023get_ve_flags(void)
8024{
Gary Johnson51ad8502021-08-03 18:33:08 +02008025 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
8026 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02008027}
8028
Bram Moolenaaree857022019-11-09 23:26:40 +01008029#if defined(FEAT_LINEBREAK) || defined(PROTO)
8030/*
8031 * Get the local or global value of 'showbreak'.
8032 */
8033 char_u *
8034get_showbreak_value(win_T *win)
8035{
8036 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8037 return p_sbr;
8038 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8039 return empty_option;
8040 return win->w_p_sbr;
8041}
8042#endif
8043
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008044#if defined(FEAT_EVAL) || defined(PROTO)
8045/*
8046 * Get window or buffer local options.
8047 */
8048 dict_T *
8049get_winbuf_options(int bufopt)
8050{
8051 dict_T *d;
8052 int opt_idx;
8053
8054 d = dict_alloc();
8055 if (d == NULL)
8056 return NULL;
8057
Bram Moolenaardac13472019-09-16 21:06:21 +02008058 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008059 {
8060 struct vimoption *opt = &options[opt_idx];
8061
8062 if ((bufopt && (opt->indir & PV_BUF))
8063 || (!bufopt && (opt->indir & PV_WIN)))
8064 {
8065 char_u *varp = get_varp(opt);
8066
8067 if (varp != NULL)
8068 {
8069 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008070 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008071 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008072 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008073 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008074 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008075 }
8076 }
8077 }
8078
8079 return d;
8080}
8081#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008082
Bram Moolenaardac13472019-09-16 21:06:21 +02008083#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008084/*
8085 * This is called when 'culopt' is changed
8086 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008087 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008088fill_culopt_flags(char_u *val, win_T *wp)
8089{
8090 char_u *p;
8091 char_u culopt_flags_new = 0;
8092
8093 if (val == NULL)
8094 p = wp->w_p_culopt;
8095 else
8096 p = val;
8097 while (*p != NUL)
8098 {
8099 if (STRNCMP(p, "line", 4) == 0)
8100 {
8101 p += 4;
8102 culopt_flags_new |= CULOPT_LINE;
8103 }
8104 else if (STRNCMP(p, "both", 4) == 0)
8105 {
8106 p += 4;
8107 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8108 }
8109 else if (STRNCMP(p, "number", 6) == 0)
8110 {
8111 p += 6;
8112 culopt_flags_new |= CULOPT_NBR;
8113 }
8114 else if (STRNCMP(p, "screenline", 10) == 0)
8115 {
8116 p += 10;
8117 culopt_flags_new |= CULOPT_SCRLINE;
8118 }
8119
8120 if (*p != ',' && *p != NUL)
8121 return FAIL;
8122 if (*p == ',')
8123 ++p;
8124 }
8125
8126 // Can't have both "line" and "screenline".
8127 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8128 return FAIL;
8129 wp->w_p_culopt_flags = culopt_flags_new;
8130
8131 return OK;
8132}
8133#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008134
8135/*
8136 * Get the value of 'magic' adjusted for Vim9 script.
8137 */
8138 int
8139magic_isset(void)
8140{
8141 switch (magic_overruled)
8142 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008143 case OPTION_MAGIC_ON: return TRUE;
8144 case OPTION_MAGIC_OFF: return FALSE;
8145 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008146 }
8147#ifdef FEAT_EVAL
8148 if (in_vim9script())
8149 return TRUE;
8150#endif
8151 return p_magic;
8152}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008153
8154/*
8155 * Set the callback function value for an option that accepts a function name,
8156 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8157 * Returns OK if the option is successfully set to a function, otherwise
8158 * returns FAIL.
8159 */
8160 int
8161option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8162{
8163#ifdef FEAT_EVAL
8164 typval_T *tv;
8165 callback_T cb;
8166
8167 if (optval == NULL || *optval == NUL)
8168 {
8169 free_callback(optcb);
8170 return OK;
8171 }
8172
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008173 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008174 || (STRNCMP(optval, "function(", 9) == 0)
8175 || (STRNCMP(optval, "funcref(", 8) == 0))
8176 // Lambda expression or a funcref
8177 tv = eval_expr(optval, NULL);
8178 else
8179 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008180 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008181 if (tv == NULL)
8182 return FAIL;
8183
8184 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008185 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008186 {
8187 free_tv(tv);
8188 return FAIL;
8189 }
8190
8191 free_callback(optcb);
8192 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008193 if (cb.cb_free_name)
8194 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008195 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008196
8197 // when using Vim9 style "import.funcname" it needs to be expanded to
8198 // "import#funcname".
8199 expand_autload_callback(optcb);
8200
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008201 return OK;
8202#else
8203 return FAIL;
8204#endif
8205}
Christian Brabandt757593c2023-08-22 21:44:10 +02008206
8207#if defined(FEAT_EVAL) || defined(PROTO)
8208 static void
8209didset_options_sctx(int opt_flags, char **buf)
8210{
8211 for (int i = 0; ; ++i)
8212 {
8213 if (buf[i] == NULL)
8214 break;
8215
8216 int idx = findoption((char_u *)buf[i]);
8217 if (idx >= 0)
8218 set_option_sctx_idx(idx, opt_flags, current_sctx);
8219 }
8220}
8221#endif