blob: 1bb9e29baeaf4f2ae033c231b809763bc73d6414 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Code to handle user-settable options. This is all pretty much table-
12 * driven. Checklist for adding a new option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000013 * - Put it in the options array in optiondefs.h (copy an existing entry).
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 * - For a global option: Add a variable for it in option.h.
15 * - For a buffer or window local option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000016 * - Add a PV_XX macro definition to the optiondefs.h file.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017 * - Add a variable to the window or buffer struct in structs.h.
18 * - For a window option, add some code to copy_winopt().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000019 * - For a window string option, add code to check_win_options() and
20 * clear_winopt().
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 * - For a buffer option, add some code to buf_copy_options().
22 * - For a buffer string option, add code to check_buf_options().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000023 * - If it's a numeric option, add any necessary bounds checks to
24 * set_num_option().
25 * - If it's a list of flags, add some code in did_set_string_option(), search
26 * for WW_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027 * - When adding an option with expansion (P_EXPAND), but with a different
28 * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
Bram Moolenaarcea912a2016-10-12 14:20:24 +020029 * - Add documentation! One line in doc/quickref.txt, full description in
Bram Moolenaar071d4272004-06-13 20:20:40 +000030 * options.txt, and any other related places.
31 * - Add an entry in runtime/optwin.vim.
32 * When making changes:
33 * - Adjust the help for the option in doc/option.txt.
34 * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
35 * comment at the help for the 'compatible' option.
36 */
37
38#define IN_OPTION_C
39#include "vim.h"
Bram Moolenaar0eddca42019-09-12 22:26:43 +020040#include "optiondefs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010042static void set_options_default(int opt_flags);
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +010043static void set_string_default_esc(char *name, char_u *val, int escape);
Bram Moolenaarb00ef052020-09-12 14:53:53 +020044static char_u *find_dup_item(char_u *origval, char_u *newval, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010045static char_u *option_expand(int opt_idx, char_u *val);
46static void didset_options(void);
47static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000048#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010049static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000050#else
51# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
52#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010053static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
54static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, size_t errbuflen, int opt_flags);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020055static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020057static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010058static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010059static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010060static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
61static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020062static int istermoption(struct vimoption *p);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000063static char_u *get_varp_scope(struct vimoption *p, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020065static void check_win_options(win_T *win);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000066static void option_value2string(struct vimoption *, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010067static void check_winopt(winopt_T *wop);
68static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010069static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Christian Brabandt757593c2023-08-22 21:44:10 +020071#if defined(FEAT_EVAL) || defined(PROTO)
72static char *(p_bin_dep_opts[]) = {"textwidth", "wrapmargin", "modeline", "expandtab", NULL};
73static char *(p_paste_dep_opts[]) = {"autoindent", "expandtab", "ruler", "showmatch", "smarttab",
74 "softtabstop", "textwidth", "wrapmargin",
75#ifdef FEAT_RIGHTLEFT
76 "hkmap", "revins",
77#endif
78#ifdef FEAT_VARTABS
79 "varsofttabstop",
80#endif
81 NULL};
82static void didset_options_sctx(int opt_flags, char **buf);
83#endif
84
85
Bram Moolenaar071d4272004-06-13 20:20:40 +000086/*
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000087 * Initialize the 'shell' option to a default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 */
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000089 static void
90set_init_default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000091{
92 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000093
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000094 // Find default value for 'shell' option.
95 // Don't use it if it is empty.
Bram Moolenaar7c626922005-02-07 22:01:03 +000096 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010097#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +000098 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010099 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +0000101 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200102#if defined(MSWIN)
103 {
104 // For MS-Windows put the path in quotes instead of escaping spaces.
105 char_u *cmd;
106 size_t len;
107
108 if (vim_strchr(p, ' ') != NULL)
109 {
110 len = STRLEN(p) + 3; // two quotes and a trailing NUL
111 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +0200112 if (cmd != NULL)
113 {
114 vim_snprintf((char *)cmd, len, "\"%s\"", p);
115 set_string_default("sh", cmd);
116 vim_free(cmd);
117 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200118 }
119 else
120 set_string_default("sh", p);
121 }
122#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100123 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200124#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000125}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000127/*
128 * Set the default for 'backupskip' to include environment variables for
129 * temp files.
130 */
131 static void
132set_init_default_backupskip(void)
133{
134 int opt_idx;
135 long_u n;
136 char_u *p;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100137#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000138 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100139#else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000140 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100141#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000142 int len;
143 garray_T ga;
Bram Moolenaar14338022023-03-15 22:05:44 +0000144 char_u *item;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200145
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000146 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000148 ga_init2(&ga, 1, 100);
149 for (n = 0; n < (long)ARRAY_LENGTH(names); ++n)
150 {
Bram Moolenaar14338022023-03-15 22:05:44 +0000151 int mustfree = FALSE;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100152#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000153 if (*names[n] == NUL)
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100154# ifdef MACOS_X
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000155 p = (char_u *)"/private/tmp";
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100156# else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000157 p = (char_u *)"/tmp";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000159 else
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100160#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000161 p = vim_getenv((char_u *)names[n], &mustfree);
162 if (p != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000164 // First time count the NUL, otherwise count the ','.
165 len = (int)STRLEN(p) + 3;
166 item = alloc(len);
Bram Moolenaar14338022023-03-15 22:05:44 +0000167 if (item != NULL)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000168 {
Bram Moolenaar14338022023-03-15 22:05:44 +0000169 STRCPY(item, p);
170 add_pathsep(item);
171 STRCAT(item, "*");
172 if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
173 == NULL
174 && ga_grow(&ga, len) == OK)
175 {
176 if (ga.ga_len > 0)
177 STRCAT(ga.ga_data, ",");
178 STRCAT(ga.ga_data, item);
179 ga.ga_len += len;
180 }
181 vim_free(item);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000184 if (mustfree)
185 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000187 if (ga.ga_data != NULL)
188 {
189 set_string_default("bsk", ga.ga_data);
190 vim_free(ga.ga_data);
191 }
192}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000194/*
195 * Initialize the 'maxmemtot' and 'maxmem' options to a default value.
196 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory.
197 */
198 static void
199set_init_default_maxmemtot(void)
200{
201 int opt_idx;
202 long_u n;
203
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 opt_idx = findoption((char_u *)"maxmemtot");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000205 if (opt_idx < 0)
206 return;
207
208#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
209 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 {
ichizok02560422022-04-05 14:18:44 +0100212#if defined(HAVE_AVAIL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000213 // Use amount of memory available at this moment.
214 n = (mch_avail_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100215#elif defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000216 // Use amount of memory available to Vim.
217 n = (mch_total_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100218#else
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000219 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000221 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
222 opt_idx = findoption((char_u *)"maxmem");
223 if (opt_idx >= 0)
224 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000225#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000226 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
227 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000228#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000229 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000232}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000234/*
235 * Initialize the 'cdpath' option to a default value.
236 */
237 static void
238set_init_default_cdpath(void)
239{
240 int opt_idx;
241 char_u *cdpath;
242 char_u *buf;
243 int i;
244 int j;
245 int mustfree = FALSE;
246
247 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
248 if (cdpath == NULL)
249 return;
250
251 buf = alloc((STRLEN(cdpath) << 1) + 2);
252 if (buf != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000254 buf[0] = ','; // start with ",", current dir first
255 j = 1;
256 for (i = 0; cdpath[i] != NUL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000258 if (vim_ispathlistsep(cdpath[i]))
259 buf[j++] = ',';
260 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000262 if (cdpath[i] == ' ' || cdpath[i] == ',')
263 buf[j++] = '\\';
264 buf[j++] = cdpath[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 }
266 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000267 buf[j] = NUL;
268 opt_idx = findoption((char_u *)"cdpath");
269 if (opt_idx >= 0)
270 {
271 options[opt_idx].def_val[VI_DEFAULT] = buf;
272 options[opt_idx].flags |= P_DEF_ALLOCED;
273 }
274 else
275 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000277 if (mustfree)
278 vim_free(cdpath);
279}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000281/*
282 * Initialize the 'printencoding' option to a default value.
283 */
284 static void
285set_init_default_printencoding(void)
286{
ichizok02560422022-04-05 14:18:44 +0100287#if defined(FEAT_POSTSCRIPT) && \
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000288 (defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100289 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100291# if defined(MSWIN)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000292 (char_u *)"cp1252"
ichizok02560422022-04-05 14:18:44 +0100293# elif defined(VMS)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000294 (char_u *)"dec-mcs"
ichizok02560422022-04-05 14:18:44 +0100295# elif defined(MAC)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000296 (char_u *)"mac-roman"
ichizok02560422022-04-05 14:18:44 +0100297# else // HPUX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000298 (char_u *)"hp-roman8"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000300 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000302}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303
304#ifdef FEAT_POSTSCRIPT
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000305/*
306 * Initialize the 'printexpr' option to a default value.
307 */
308 static void
309set_init_default_printexpr(void)
310{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100311 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100313# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000314 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
ichizok02560422022-04-05 14:18:44 +0100315# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
317
ichizok02560422022-04-05 14:18:44 +0100318# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320# endif
321 );
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000322}
323#endif
324
325#ifdef UNIX
326/*
327 * Force restricted-mode on for "nologin" or "false" $SHELL
328 */
329 static void
330set_init_restricted_mode(void)
331{
332 char_u *p;
333
334 p = get_isolated_shell_name();
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000335 if (p == NULL)
336 return;
337 if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
338 restricted = TRUE;
339 vim_free(p);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000340}
341#endif
342
343#ifdef CLEAN_RUNTIMEPATH
344/*
345 * When Vim is started with the "--clean" argument, set the default value
346 * for the 'runtimepath' and 'packpath' options.
347 */
348 static void
349set_init_clean_rtp(void)
350{
351 int opt_idx;
352
353 opt_idx = findoption((char_u *)"runtimepath");
354 if (opt_idx >= 0)
355 {
356 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
357 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
358 }
359 opt_idx = findoption((char_u *)"packpath");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000360 if (opt_idx < 0)
361 return;
362 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
363 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000364}
365#endif
366
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200367#ifdef UNIX
368/*
369 * Change 'runtimepath' and 'packdir' to '$XDG_CONFIG_HOME/vim' if the only
370 * vimrc found is located in '$XDG_CONFIG_HOME/vim/vimrc'.
371 * In case the '$XDG_CONFIG_HOME' variable is not set, '$HOME/.config' is used
372 * as a fallback as is defined in the XDG base dir specification:
373 * <https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html>
374 */
375 static void
376set_init_xdg_rtp(void)
377{
378 int opt_idx;
379 int has_xdg_env = TRUE;
380 int should_free_xdg_dir = FALSE;
381 char_u *vimrc1 = NULL;
382 char_u *vimrc2 = NULL;
383 char_u *xdg_dir = NULL;
384 char_u *xdg_rtp = NULL;
385 char_u *vimrc_xdg = NULL;
386
387 vimrc1 = expand_env_save((char_u *)USR_VIMRC_FILE);
388 vimrc2 = expand_env_save((char_u *)USR_VIMRC_FILE2);
389
390 xdg_dir = mch_getenv("XDG_CONFIG_HOME");
391 if (!xdg_dir)
392 {
393 xdg_dir = expand_env_save((char_u *)"~/.config");
394 should_free_xdg_dir = TRUE;
395 has_xdg_env = FALSE;
396 }
397 vimrc_xdg = concat_fnames(xdg_dir, (char_u *)"vim/vimrc", TRUE);
398
399 if (file_is_readable(vimrc1) || file_is_readable(vimrc2) ||
400 !file_is_readable(vimrc_xdg))
401 goto theend;
402
403 xdg_rtp = has_xdg_env ? (char_u *)XDG_RUNTIMEPATH
404 : (char_u *)XDG_RUNTIMEPATH_FB;
405
406 if ((opt_idx = findoption((char_u *)"runtimepath")) < 0)
407 goto theend;
408
409 options[opt_idx].def_val[VI_DEFAULT] = xdg_rtp;
410 p_rtp = xdg_rtp;
411
412 if ((opt_idx = findoption((char_u *)"packpath")) < 0)
413 goto theend;
414
415 options[opt_idx].def_val[VI_DEFAULT] = xdg_rtp;
416 p_pp = xdg_rtp;
417
418theend:
419 vim_free(vimrc1);
420 vim_free(vimrc2);
421 vim_free(vimrc_xdg);
422 if (should_free_xdg_dir)
423 vim_free(xdg_dir);
424}
425#endif
426
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000427/*
428 * Expand environment variables and things like "~" for the defaults.
429 * If option_expand() returns non-NULL the variable is expanded. This can
430 * only happen for non-indirect options.
431 * Also set the default to the expanded value, so ":set" does not list
432 * them.
433 * Don't set the P_ALLOCED flag, because we don't want to free the
434 * default.
435 */
436 static void
437set_init_expand_env(void)
438{
439 int opt_idx;
440 char_u *p;
441
442 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
443 {
444 if ((options[opt_idx].flags & P_GETTEXT)
445 && options[opt_idx].var != NULL)
446 p = (char_u *)_(*(char **)options[opt_idx].var);
447 else
448 p = option_expand(opt_idx, NULL);
449 if (p != NULL && (p = vim_strsave(p)) != NULL)
450 {
451 *(char_u **)options[opt_idx].var = p;
452 // VIMEXP
453 // Defaults for all expanded options are currently the same for Vi
454 // and Vim. When this changes, add some code here! Also need to
455 // split P_DEF_ALLOCED in two.
456 if (options[opt_idx].flags & P_DEF_ALLOCED)
457 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
458 options[opt_idx].def_val[VI_DEFAULT] = p;
459 options[opt_idx].flags |= P_DEF_ALLOCED;
460 }
461 }
462}
463
464/*
465 * Initialize the 'LANG' environment variable to a default value.
466 */
467 static void
468set_init_lang_env(void)
469{
470#if defined(MSWIN) && defined(FEAT_GETTEXT)
471 // If $LANG isn't set, try to get a good value for it. This makes the
472 // right language be used automatically. Don't do this for English.
473 if (mch_getenv((char_u *)"LANG") == NULL)
474 {
475 char buf[20];
476 long_u n;
477
478 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
479 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
480 // only the first two.
481 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
482 (LPTSTR)buf, 20);
483 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
484 {
485 // There are a few exceptions (probably more)
486 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
487 STRCPY(buf, "zh_TW");
488 else if (STRNICMP(buf, "chs", 3) == 0
489 || STRNICMP(buf, "zhc", 3) == 0)
490 STRCPY(buf, "zh_CN");
491 else if (STRNICMP(buf, "jp", 2) == 0)
492 STRCPY(buf, "ja");
493 else
494 buf[2] = NUL; // truncate to two-letter code
495 vim_setenv((char_u *)"LANG", (char_u *)buf);
496 }
497 }
498#elif defined(MACOS_CONVERT)
499 // Moved to os_mac_conv.c to avoid dependency problems.
500 mac_lang_init();
501#endif
502}
503
504/*
505 * Initialize the 'encoding' option to a default value.
506 */
507 static void
508set_init_default_encoding(void)
509{
510 char_u *p;
511 int opt_idx;
512
Igor Todorovski497e5282024-01-12 17:59:18 +0100513# if defined(MSWIN) || defined(__MVS__)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000514 // MS-Windows has builtin support for conversion to and from Unicode, using
515 // "utf-8" for 'encoding' should work best for most users.
Igor Todorovski497e5282024-01-12 17:59:18 +0100516 // z/OS built should default to UTF-8 mode as setlocale does not respect utf-8 environment variable locales
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000517 p = vim_strsave((char_u *)ENC_DFLT);
518# else
519 // enc_locale() will try to find the encoding of the current locale.
520 // This works best for properly configured systems, old and new.
521 p = enc_locale();
522# endif
523 if (p == NULL)
524 return;
525
526 // Try setting 'encoding' and check if the value is valid.
527 // If not, go back to the default encoding.
528 char_u *save_enc = p_enc;
529 p_enc = p;
530 if (STRCMP(p_enc, "gb18030") == 0)
531 {
532 // We don't support "gb18030", but "cp936" is a good substitute
533 // for practical purposes, thus use that. It's not an alias to
534 // still support conversion between gb18030 and utf-8.
535 p_enc = vim_strsave((char_u *)"cp936");
536 vim_free(p);
537 }
538 if (mb_init() == NULL)
539 {
540 opt_idx = findoption((char_u *)"encoding");
541 if (opt_idx >= 0)
542 {
543 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
544 options[opt_idx].flags |= P_DEF_ALLOCED;
545 }
546
547#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
548 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
549 {
550 // Adjust the default for 'isprint' and 'iskeyword' to match
551 // latin1. Also set the defaults for when 'nocompatible' is
552 // set.
553 set_string_option_direct((char_u *)"isp", -1,
554 ISP_LATIN1, OPT_FREE, SID_NONE);
555 set_string_option_direct((char_u *)"isk", -1,
556 ISK_LATIN1, OPT_FREE, SID_NONE);
557 opt_idx = findoption((char_u *)"isp");
558 if (opt_idx >= 0)
559 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
560 opt_idx = findoption((char_u *)"isk");
561 if (opt_idx >= 0)
562 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
563 (void)init_chartab();
564 }
565#endif
566
567#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
568 // Win32 console: When GetACP() returns a different value from
569 // GetConsoleCP() set 'termencoding'.
570 if (
571# ifdef VIMDLL
572 (!gui.in_use && !gui.starting) &&
573# endif
574 GetACP() != GetConsoleCP())
575 {
576 char buf[50];
577
578 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
579 // Use an alternative value.
580 if (GetConsoleCP() == 0)
581 sprintf(buf, "cp%ld", (long)GetACP());
582 else
583 sprintf(buf, "cp%ld", (long)GetConsoleCP());
584 p_tenc = vim_strsave((char_u *)buf);
585 if (p_tenc != NULL)
586 {
587 opt_idx = findoption((char_u *)"termencoding");
588 if (opt_idx >= 0)
589 {
590 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
591 options[opt_idx].flags |= P_DEF_ALLOCED;
592 }
593 convert_setup(&input_conv, p_tenc, p_enc);
594 convert_setup(&output_conv, p_enc, p_tenc);
595 }
596 else
597 p_tenc = empty_option;
598 }
599#endif
600#if defined(MSWIN)
601 // $HOME may have characters in active code page.
602 init_homedir();
603#endif
604 }
605 else
606 {
607 vim_free(p_enc);
608 p_enc = save_enc;
609 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000610}
611
612/*
613 * Initialize the options, first part.
614 *
615 * Called only once from main(), just after creating the first buffer.
616 * If "clean_arg" is TRUE Vim was started with --clean.
617 */
618 void
619set_init_1(int clean_arg)
620{
621#ifdef FEAT_LANGMAP
622 langmap_init();
623#endif
624
625 // Be Vi compatible by default
626 p_cp = TRUE;
627
628 // Use POSIX compatibility when $VIM_POSIX is set.
629 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
630 {
631 set_string_default("cpo", (char_u *)CPO_ALL);
632 set_string_default("shm", (char_u *)SHM_POSIX);
633 }
634
635 set_init_default_shell();
636 set_init_default_backupskip();
637 set_init_default_maxmemtot();
638 set_init_default_cdpath();
639 set_init_default_printencoding();
640#ifdef FEAT_POSTSCRIPT
641 set_init_default_printexpr();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642#endif
643
644 /*
645 * Set all the options (except the terminal options) to their default
646 * value. Also set the global value for local options.
647 */
648 set_options_default(0);
649
matveytadbb1bf2022-02-01 17:26:12 +0000650#ifdef UNIX
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200651 set_init_xdg_rtp();
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000652 set_init_restricted_mode();
matveytadbb1bf2022-02-01 17:26:12 +0000653#endif
654
Bram Moolenaar07268702018-03-01 21:57:32 +0100655#ifdef CLEAN_RUNTIMEPATH
656 if (clean_arg)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000657 set_init_clean_rtp();
Bram Moolenaar07268702018-03-01 21:57:32 +0100658#endif
659
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660#ifdef FEAT_GUI
661 if (found_reverse_arg)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100662 set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663#endif
664
665 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100666 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100667 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 check_buf_options(curbuf);
669 check_win_options(curwin);
670 check_options();
671
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100672 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 didset_options();
674
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000675#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100676 // Use the current chartab for the generic chartab. This is not in
677 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000678 init_spell_chartab();
679#endif
680
K.Takatace3189d2023-02-15 19:13:43 +0000681 set_init_default_encoding();
682
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000683 // Expand environment variables and things like "~" for the defaults.
684 set_init_expand_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100686 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100689 // Detect use of mlterm.
690 // Mlterm is a terminal emulator akin to xterm that has some special
691 // abilities (bidi namely).
692 // NOTE: mlterm's author is being asked to 'set' a variable
693 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 if (mch_getenv((char_u *)"MLTERM") != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100695 set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696#endif
697
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200698 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000700 set_init_lang_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701
702#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100703 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 set_helplang_default(get_mess_lang());
705#endif
706}
707
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200708static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
709
710/*
711 * Set the "fileencodings" option to the default value for when 'encoding' is
712 * utf-8.
713 */
714 void
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +0000715set_fencs_unicode(void)
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200716{
717 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
718 OPT_FREE, 0);
719}
720
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721/*
722 * Set an option to its default value.
723 * This does not take care of side effects!
724 */
725 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100726set_option_default(
727 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100728 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
729 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100731 char_u *varp; // pointer to variable for current option
732 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000734 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
736
737 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
738 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100739 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 {
741 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
742 if (flags & P_STRING)
743 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200744 // 'fencs' default value depends on 'encoding'
745 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
746 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100747 // Use set_string_option_direct() for local options to handle
748 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200749 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200750 set_string_option_direct(NULL, opt_idx,
751 options[opt_idx].def_val[dvi], opt_flags, 0);
752 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200754 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
755 free_string_option(*(char_u **)(varp));
756 *(char_u **)varp = options[opt_idx].def_val[dvi];
757 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 }
759 }
760 else if (flags & P_NUM)
761 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000762 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 win_comp_scroll(curwin);
764 else
765 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100766 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
767
768 if ((long *)varp == &curwin->w_p_so
769 || (long *)varp == &curwin->w_p_siso)
770 // 'scrolloff' and 'sidescrolloff' local values have a
771 // different default value than the global default.
772 *(long *)varp = -1;
773 else
774 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100775 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 if (both)
777 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100778 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 }
780 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100781 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100783 // the cast to long is required for Manx C, long_i is needed for
784 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000785 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000786#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100787 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000788 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
789 *(int *)varp = FALSE;
790#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100791 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 if (both)
793 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
794 *(int *)varp;
795 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000796
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100797 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000798 flagsp = insecure_flag(opt_idx, opt_flags);
799 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 }
801
802#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200803 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804#endif
805}
806
807/*
808 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200809 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 */
811 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100812set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100813 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814{
815 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000817 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818
Bram Moolenaardac13472019-09-16 21:06:21 +0200819 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200820 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200821 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100822 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200823# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200824 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200825 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200826# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100827 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 set_option_default(i, opt_flags, p_cp);
829
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100830 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000831 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200833 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834}
835
836/*
837 * Set the Vi-default value of a string option.
838 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100839 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100841 static void
842set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843{
844 char_u *p;
845 int opt_idx;
846
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100847 if (escape && vim_strchr(val, ' ') != NULL)
848 p = vim_strsave_escaped(val, (char_u *)" ");
849 else
850 p = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000851 if (p == NULL) // we don't want a NULL
852 return;
853
854 opt_idx = findoption((char_u *)name);
855 if (opt_idx < 0)
Christian Brabandt29269a72024-04-16 22:44:31 +0200856 {
857 vim_free(p);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000858 return;
Christian Brabandt29269a72024-04-16 22:44:31 +0200859 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000860
861 if (options[opt_idx].flags & P_DEF_ALLOCED)
862 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
863 options[opt_idx].def_val[VI_DEFAULT] = p;
864 options[opt_idx].flags |= P_DEF_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865}
866
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100867 void
868set_string_default(char *name, char_u *val)
869{
870 set_string_default_esc(name, val, FALSE);
871}
872
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200874 * For an option value that contains comma separated items, find "newval" in
875 * "origval". Return NULL if not found.
876 */
877 static char_u *
878find_dup_item(char_u *origval, char_u *newval, long_u flags)
879{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200880 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200881 size_t newlen;
882 char_u *s;
883
884 if (origval == NULL)
885 return NULL;
886
887 newlen = STRLEN(newval);
888 for (s = origval; *s != NUL; ++s)
889 {
890 if ((!(flags & P_COMMA)
891 || s == origval
892 || (s[-1] == ',' && !(bs & 1)))
893 && STRNCMP(s, newval, newlen) == 0
894 && (!(flags & P_COMMA)
895 || s[newlen] == ','
896 || s[newlen] == NUL))
897 return s;
898 // Count backslashes. Only a comma with an even number of backslashes
899 // or a single backslash preceded by a comma before it is recognized as
900 // a separator.
901 if ((s > origval + 1
902 && s[-1] == '\\'
903 && s[-2] != ',')
904 || (s == origval + 1
905 && s[-1] == '\\'))
906 ++bs;
907 else
908 bs = 0;
909 }
910 return NULL;
911}
912
913/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 * Set the Vi-default value of a number option.
915 * Used for 'lines' and 'columns'.
916 */
917 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100918set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000920 int opt_idx;
921
922 opt_idx = findoption((char_u *)name);
923 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000924 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925}
926
Dominique Pelle748b3082022-01-08 12:41:16 +0000927#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200928/*
929 * Set all window-local and buffer-local options to the Vim default.
930 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200931 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200932 */
933 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200934set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200935{
936 win_T *save_curwin = curwin;
937 int i;
938
939 curwin = wp;
940 curbuf = curwin->w_buffer;
941 block_autocmds();
942
Bram Moolenaardac13472019-09-16 21:06:21 +0200943 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200944 {
945 struct vimoption *p = &(options[i]);
946 char_u *varp = get_varp_scope(p, OPT_LOCAL);
947
948 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200949 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200950 && !(options[i].flags & P_NODEFAULT)
951 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200952 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200953 }
954
955 unblock_autocmds();
956 curwin = save_curwin;
957 curbuf = curwin->w_buffer;
958}
Dominique Pelle748b3082022-01-08 12:41:16 +0000959#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200960
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000961#if defined(EXITFREE) || defined(PROTO)
962/*
963 * Free all options.
964 */
965 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100966free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000967{
968 int i;
969
Bram Moolenaardac13472019-09-16 21:06:21 +0200970 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000971 {
972 if (options[i].indir == PV_NONE)
973 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100974 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100975 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000976 free_string_option(*(char_u **)options[i].var);
977 if (options[i].flags & P_DEF_ALLOCED)
978 free_string_option(options[i].def_val[VI_DEFAULT]);
979 }
980 else if (options[i].var != VAR_WIN
981 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100982 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200983 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000984 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000985 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000986 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000987}
988#endif
989
990
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991/*
992 * Initialize the options, part two: After getting Rows and Columns and
993 * setting 'term'.
994 */
995 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100996set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000998 int idx;
999
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001000 // 'scroll' defaults to half the window height. The stored default is zero,
1001 // which results in the actual value computed from the window height.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001002 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001003 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001004 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 comp_col();
1006
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001007 // 'window' is only for backwards compatibility with Vi.
1008 // Default is Rows - 1.
Bram Moolenaard68071d2006-05-02 22:08:30 +00001009 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001010 p_window = Rows - 1;
1011 set_number_default("window", Rows - 1);
1012
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001013 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +01001014#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001015 // If 'background' wasn't set by the user, try guessing the value,
1016 // depending on the terminal name. Only need to check for terminals
1017 // with a dark background, that can handle color.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001018 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001019 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
1020 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001022 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001023 // don't mark it as set, when starting the GUI it may be
1024 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +00001025 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 }
1027#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001028
1029#ifdef CURSOR_SHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001030 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +00001031#endif
1032#ifdef FEAT_MOUSESHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001033 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +00001034#endif
1035#ifdef FEAT_PRINTER
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001036 (void)parse_printoptions(NULL); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +00001037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038}
1039
1040/*
1041 * Initialize the options, part three: After reading the .vimrc
1042 */
1043 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001044set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045{
Bram Moolenaar4f974752019-02-17 17:44:42 +01001046#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047/*
1048 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
1049 * This is done after other initializations, where 'shell' might have been
1050 * set, but only if they have not been set before.
1051 */
1052 char_u *p;
1053 int idx_srr;
1054 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001055# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 int idx_sp;
1057 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001058# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
1060 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001061 if (idx_srr < 0)
1062 do_srr = FALSE;
1063 else
1064 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001065# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001067 if (idx_sp < 0)
1068 do_sp = FALSE;
1069 else
1070 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001071# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001072 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 if (p != NULL)
1074 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001075 // Default for p_sp is "| tee", for p_srr is ">".
1076 // For known shells it is changed here to include stderr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 if ( fnamecmp(p, "csh") == 0
1078 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001079# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 || fnamecmp(p, "csh.exe") == 0
1081 || fnamecmp(p, "tcsh.exe") == 0
1082# endif
1083 )
1084 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001085# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 if (do_sp)
1087 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001088# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001090# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001092# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1094 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001095# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 if (do_srr)
1097 {
1098 p_srr = (char_u *)">&";
1099 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1100 }
1101 }
Mike Williams12795022021-06-28 20:53:58 +02001102# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +02001103 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
1104 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +02001105 else if ( fnamecmp(p, "powershell") == 0
1106 || fnamecmp(p, "powershell.exe") == 0
1107 )
1108 {
1109# if defined(FEAT_QUICKFIX)
1110 if (do_sp)
1111 {
1112 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
1113 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1114 }
1115# endif
1116 if (do_srr)
1117 {
1118 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
1119 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1120 }
1121 }
1122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 else
Natanael Copa56318362021-05-06 18:46:35 +02001124 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 if ( fnamecmp(p, "sh") == 0
1126 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001127 || fnamecmp(p, "mksh") == 0
1128 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001130 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001132 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001133 || fnamecmp(p, "ash") == 0
1134 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001135 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001136# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 || fnamecmp(p, "cmd") == 0
1138 || fnamecmp(p, "sh.exe") == 0
1139 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001140 || fnamecmp(p, "mksh.exe") == 0
1141 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001143 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 || fnamecmp(p, "bash.exe") == 0
1145 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001146 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001147 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001149 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001151# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 if (do_sp)
1153 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001154# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001156# else
Mike Williamsa3d1b292021-06-30 20:56:00 +02001157 if (fnamecmp(p, "pwsh") == 0)
1158 p_sp = (char_u *)">%s 2>&1";
1159 else
1160 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001161# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1163 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001164# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 if (do_srr)
1166 {
1167 p_srr = (char_u *)">%s 2>&1";
1168 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1169 }
1170 }
1171 vim_free(p);
1172 }
1173#endif
1174
Bram Moolenaar4f974752019-02-17 17:44:42 +01001175#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001177 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1178 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001180 * set, but only if they have not been set before.
1181 * Default values depend on shell (cmd.exe is default shell):
1182 *
1183 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001184 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001185 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001186 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001187 * "sh" like shells - "-c" "\""
1188 *
1189 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 */
Mike Williams12795022021-06-28 20:53:58 +02001191 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1192 {
1193 int idx_opt;
1194
1195 idx_opt = findoption((char_u *)"shcf");
1196 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1197 {
1198 p_shcf = (char_u*)"-Command";
1199 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1200 }
1201
1202 idx_opt = findoption((char_u *)"sxq");
1203 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1204 {
1205 p_sxq = (char_u*)"\"";
1206 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1207 }
1208 }
1209 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 {
1211 int idx3;
1212
1213 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001214 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 {
1216 p_shcf = (char_u *)"-c";
1217 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1218 }
1219
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001220 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001222 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 {
1224 p_sxq = (char_u *)"\"";
1225 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001228 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1229 {
1230 int idx3;
1231
1232 /*
1233 * cmd.exe on Windows will strip the first and last double quote given
1234 * on the command line, e.g. most of the time things like:
1235 * cmd /c "my path/to/echo" "my args to echo"
1236 * become:
1237 * my path/to/echo" "my args to echo
1238 * when executed.
1239 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001240 * To avoid this, set shellxquote to surround the command in
1241 * parenthesis. This appears to make most commands work, without
1242 * breaking commands that worked previously, such as
1243 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001244 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001245 idx3 = findoption((char_u *)"sxq");
1246 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1247 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001248 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001249 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1250 }
1251
Bram Moolenaara64ba222012-02-12 23:23:31 +01001252 idx3 = findoption((char_u *)"shcf");
1253 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1254 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001255 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001256 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1257 }
1258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259#endif
1260
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001261 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001262 {
1263 int idx_ffs = findoption((char_u *)"ffs");
1264
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001265 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001266 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1267 set_fileformat(default_fileformat(), OPT_LOCAL);
1268 }
1269
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271}
1272
1273#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1274/*
1275 * When 'helplang' is still at its default value, set it to "lang".
1276 * Only the first two characters of "lang" are used.
1277 */
1278 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001279set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280{
1281 int idx;
1282
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001283 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 return;
1285 idx = findoption((char_u *)"hlg");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001286 if (idx < 0 || (options[idx].flags & P_WAS_SET))
1287 return;
1288
1289 if (options[idx].flags & P_ALLOCED)
1290 free_string_option(p_hlg);
1291 p_hlg = vim_strsave(lang);
1292 if (p_hlg == NULL)
1293 p_hlg = empty_option;
1294 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001296 // zh_CN becomes "cn", zh_TW becomes "tw"
1297 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001298 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001299 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1300 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001301 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001302 // any C like setting, such as C.UTF-8, becomes "en"
1303 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1304 {
1305 p_hlg[0] = 'e';
1306 p_hlg[1] = 'n';
1307 }
1308 p_hlg[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001310 options[idx].flags |= P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311}
1312#endif
1313
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314/*
1315 * 'title' and 'icon' only default to true if they have not been set or reset
1316 * in .vimrc and we can read the old value.
1317 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1318 * they can be reset. This reduces startup time when using X on a remote
1319 * machine.
1320 */
1321 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001322set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323{
1324 int idx1;
1325 long val;
1326
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001327 // If GUI is (going to be) used, we can always set the window title and
1328 // icon name. Saves a bit of time, because the X11 display server does
1329 // not need to be contacted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001331 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 {
1333#ifdef FEAT_GUI
1334 if (gui.starting || gui.in_use)
1335 val = TRUE;
1336 else
1337#endif
1338 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001339 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 p_title = val;
1341 }
1342 idx1 = findoption((char_u *)"icon");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001343 if (idx1 < 0 || (options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001345 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001347
1348#ifdef FEAT_GUI
1349 if (gui.starting || gui.in_use)
1350 val = TRUE;
1351 else
1352#endif
1353 val = mch_can_restore_icon();
1354 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
1355 p_icon = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001358 void
1359ex_set(exarg_T *eap)
1360{
1361 int flags = 0;
1362
1363 if (eap->cmdidx == CMD_setlocal)
1364 flags = OPT_LOCAL;
1365 else if (eap->cmdidx == CMD_setglobal)
1366 flags = OPT_GLOBAL;
1367#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001368 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001369 ex_options(eap);
1370 else
1371#endif
1372 {
1373 if (eap->forceit)
1374 flags |= OPT_ONECOLUMN;
1375 (void)do_set(eap->arg, flags);
1376 }
1377}
1378
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001379/*
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001380 * :set boolean option prefix
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001381 */
Bram Moolenaar47403942022-09-21 21:12:53 +01001382typedef enum {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001383 PREFIX_NO = 0, // "no" prefix
1384 PREFIX_NONE, // no prefix
1385 PREFIX_INV, // "inv" prefix
1386} set_prefix_T;
1387
1388/*
1389 * Return the prefix type for the option name in *argp.
1390 */
1391 static set_prefix_T
1392get_option_prefix(char_u **argp)
1393{
1394 int prefix = PREFIX_NONE;
1395 char_u *arg = *argp;
1396
1397 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
1398 {
1399 prefix = PREFIX_NO;
1400 arg += 2;
1401 }
1402 else if (STRNCMP(arg, "inv", 3) == 0)
1403 {
1404 prefix = PREFIX_INV;
1405 arg += 3;
1406 }
1407
1408 *argp = arg;
1409 return prefix;
1410}
1411
1412/*
1413 * Parse the option name in "arg" and return the option index in "*opt_idxp",
1414 * and the option name length in "*lenp". For a <t_xx> option, return the key
1415 * number in "*keyp".
1416 *
1417 * Returns FAIL if an option starting with "<" doesn't end with a ">",
1418 * otherwise returns OK.
1419 */
1420 static int
1421parse_option_name(char_u *arg, int *opt_idxp, int *lenp, int *keyp)
1422{
1423 int key = 0;
1424 int len;
1425 int opt_idx;
1426
1427 if (*arg == '<')
1428 {
1429 opt_idx = -1;
1430 // look out for <t_>;>
1431 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1432 len = 5;
1433 else
1434 {
1435 len = 1;
1436 while (arg[len] != NUL && arg[len] != '>')
1437 ++len;
1438 }
1439 if (arg[len] != '>')
1440 return FAIL;
1441
1442 arg[len] = NUL; // put NUL after name
1443 if (arg[1] == 't' && arg[2] == '_') // could be term code
1444 opt_idx = findoption(arg + 1);
1445 arg[len++] = '>'; // restore '>'
1446 if (opt_idx == -1)
1447 key = find_key_option(arg + 1, TRUE);
1448 }
1449 else
1450 {
1451 int nextchar; // next non-white char after option name
1452
1453 len = 0;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001454 // The two characters after "t_" may not be alphanumeric.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001455 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1456 len = 4;
1457 else
1458 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1459 ++len;
1460 nextchar = arg[len];
1461 arg[len] = NUL; // put NUL after name
1462 opt_idx = findoption(arg);
1463 arg[len] = nextchar; // restore nextchar
1464 if (opt_idx == -1)
1465 key = find_key_option(arg, FALSE);
1466 }
1467
1468 *keyp = key;
1469 *lenp = len;
1470 *opt_idxp = opt_idx;
1471
1472 return OK;
1473}
1474
1475/*
1476 * Get the option operator (+=, ^=, -=).
1477 */
1478 static set_op_T
1479get_opt_op(char_u *arg)
1480{
1481 set_op_T op = OP_NONE;
1482
1483 if (*arg != NUL && *(arg + 1) == '=')
1484 {
1485 if (*arg == '+')
1486 op = OP_ADDING; // "+="
1487 else if (*arg == '^')
1488 op = OP_PREPENDING; // "^="
1489 else if (*arg == '-')
1490 op = OP_REMOVING; // "-="
1491 }
1492
1493 return op;
1494}
1495
1496/*
1497 * Validate whether the value of the option in "opt_idx" can be changed.
1498 * Returns FAIL if the option can be skipped or cannot be changed. Returns OK
1499 * if it can be changed.
1500 */
1501 static int
1502validate_opt_idx(int opt_idx, int opt_flags, long_u flags, char **errmsg)
1503{
1504 // Skip all options that are not window-local (used when showing
1505 // an already loaded buffer in a window).
1506 if ((opt_flags & OPT_WINONLY)
1507 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1508 return FAIL;
1509
1510 // Skip all options that are window-local (used for :vimgrep).
1511 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1512 && options[opt_idx].var == VAR_WIN)
1513 return FAIL;
1514
1515 // Disallow changing some options from modelines.
1516 if (opt_flags & OPT_MODELINE)
1517 {
1518 if (flags & (P_SECURE | P_NO_ML))
1519 {
1520 *errmsg = e_not_allowed_in_modeline;
1521 return FAIL;
1522 }
1523 if ((flags & P_MLE) && !p_mle)
1524 {
1525 *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
1526 return FAIL;
1527 }
1528#ifdef FEAT_DIFF
1529 // In diff mode some options are overruled. This avoids that
1530 // 'foldmethod' becomes "marker" instead of "diff" and that
1531 // "wrap" gets set.
1532 if (curwin->w_p_diff
1533 && opt_idx >= 0 // shut up coverity warning
1534 && (
1535# ifdef FEAT_FOLDING
1536 options[opt_idx].indir == PV_FDM ||
1537# endif
1538 options[opt_idx].indir == PV_WRAP))
1539 return FAIL;
1540#endif
1541 }
1542
1543#ifdef HAVE_SANDBOX
1544 // Disallow changing some options in the sandbox
1545 if (sandbox != 0 && (flags & P_SECURE))
1546 {
1547 *errmsg = e_not_allowed_in_sandbox;
1548 return FAIL;
1549 }
1550#endif
1551
1552 return OK;
1553}
1554
Bram Moolenaar47403942022-09-21 21:12:53 +01001555/*
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001556 * Get the Vim/Vi default value for a string option.
1557 */
1558 static char_u *
1559stropt_get_default_val(
1560 int opt_idx,
1561 char_u *varp,
1562 int flags,
1563 int cp_val)
1564{
1565 char_u *newval;
1566
1567 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1568 ? VI_DEFAULT : VIM_DEFAULT];
1569 if ((char_u **)varp == &p_bg)
1570 {
1571 // guess the value of 'background'
1572#ifdef FEAT_GUI
1573 if (gui.in_use)
1574 newval = gui_bg_default();
1575 else
1576#endif
1577 newval = term_bg_default();
1578 }
1579 else if ((char_u **)varp == &p_fencs && enc_utf8)
1580 newval = fencs_utf8_default;
1581
1582 // expand environment variables and ~ since the default value was
1583 // already expanded, only required when an environment variable was set
1584 // later
1585 if (newval == NULL)
1586 newval = empty_option;
1587 else
1588 {
1589 char_u *s = option_expand(opt_idx, newval);
1590 if (s == NULL)
1591 s = newval;
1592 newval = vim_strsave(s);
1593 }
1594
1595 return newval;
1596}
1597
1598/*
1599 * Convert the 'backspace' option number value to a string: for adding,
1600 * prepending and removing string.
1601 */
1602 static void
1603opt_backspace_nr2str(
1604 char_u *varp,
1605 char_u **origval_p,
1606 char_u **origval_l_p,
1607 char_u **origval_g_p,
1608 char_u **oldval_p)
1609{
1610 int i = getdigits((char_u **)varp);
1611
1612 switch (i)
1613 {
1614 case 0:
1615 *(char_u **)varp = empty_option;
1616 break;
1617 case 1:
1618 *(char_u **)varp = vim_strsave((char_u *)"indent,eol");
1619 break;
1620 case 2:
1621 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,start");
1622 break;
1623 case 3:
1624 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,nostop");
1625 break;
1626 }
1627 vim_free(*oldval_p);
1628 if (*origval_p == *oldval_p)
1629 *origval_p = *(char_u **)varp;
1630 if (*origval_l_p == *oldval_p)
1631 *origval_l_p = *(char_u **)varp;
1632 if (*origval_g_p == *oldval_p)
1633 *origval_g_p = *(char_u **)varp;
1634 *oldval_p = *(char_u **)varp;
1635}
1636
1637/*
1638 * Convert the 'whichwrap' option number value to a string, for backwards
1639 * compatibility with Vim 3.0.
1640 * Note: 'argp' is a pointer to a char_u pointer and is updated.
1641 */
1642 static char_u *
1643opt_whichwrap_nr2str(char_u **argp, char_u *whichwrap)
1644{
1645 *whichwrap = NUL;
1646 int i = getdigits(argp);
1647 if (i & 1)
1648 STRCAT(whichwrap, "b,");
1649 if (i & 2)
1650 STRCAT(whichwrap, "s,");
1651 if (i & 4)
1652 STRCAT(whichwrap, "h,l,");
1653 if (i & 8)
1654 STRCAT(whichwrap, "<,>,");
1655 if (i & 16)
1656 STRCAT(whichwrap, "[,],");
1657 if (*whichwrap != NUL) // remove trailing ,
1658 whichwrap[STRLEN(whichwrap) - 1] = NUL;
1659
1660 return whichwrap;
1661}
1662
1663/*
1664 * Copy the new string value into allocated memory for the option.
1665 * Can't use set_string_option_direct(), because we need to remove the
1666 * backslashes.
1667 */
1668 static char_u *
1669stropt_copy_value(
1670 char_u *origval,
1671 char_u **argp,
1672 set_op_T op,
1673 int flags UNUSED)
1674{
1675 char_u *arg = *argp;
1676 unsigned newlen;
1677 char_u *newval;
1678 char_u *s = NULL;
1679
1680 // get a bit too much
1681 newlen = (unsigned)STRLEN(arg) + 1;
1682 if (op != OP_NONE)
1683 newlen += (unsigned)STRLEN(origval) + 1;
1684 newval = alloc(newlen);
1685 if (newval == NULL) // out of mem, don't change
1686 return NULL;
1687 s = newval;
1688
1689 // Copy the string, skip over escaped chars.
1690 // For MS-DOS and WIN32 backslashes before normal file name characters
1691 // are not removed, and keep backslash at start, for "\\machine\path",
1692 // but do remove it for "\\\\machine\\path".
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001693 // The reverse is found in escape_option_str_cmdline().
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001694 while (*arg != NUL && !VIM_ISWHITE(*arg))
1695 {
1696 int i;
1697
1698 if (*arg == '\\' && arg[1] != NUL
1699#ifdef BACKSLASH_IN_FILENAME
1700 && !((flags & P_EXPAND)
1701 && vim_isfilec(arg[1])
1702 && !VIM_ISWHITE(arg[1])
1703 && (arg[1] != '\\'
1704 || (s == newval && arg[2] != '\\')))
1705#endif
1706 )
1707 ++arg; // remove backslash
1708 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
1709 {
1710 // copy multibyte char
1711 mch_memmove(s, arg, (size_t)i);
1712 arg += i;
1713 s += i;
1714 }
1715 else
1716 *s++ = *arg++;
1717 }
1718 *s = NUL;
1719
1720 *argp = arg;
1721 return newval;
1722}
1723
1724/*
1725 * Expand environment variables and ~ in string option value 'newval'.
1726 */
1727 static char_u *
1728stropt_expand_envvar(
1729 int opt_idx,
1730 char_u *origval,
1731 char_u *newval,
1732 set_op_T op)
1733{
1734 char_u *s = option_expand(opt_idx, newval);
1735 if (s == NULL)
1736 return newval;
1737
1738 vim_free(newval);
1739 unsigned newlen = (unsigned)STRLEN(s) + 1;
1740 if (op != OP_NONE)
1741 newlen += (unsigned)STRLEN(origval) + 1;
1742
1743 newval = alloc(newlen);
1744 if (newval == NULL)
1745 return NULL;
1746
1747 STRCPY(newval, s);
1748
1749 return newval;
1750}
1751
1752/*
1753 * Concatenate the original and new values of a string option, adding a "," if
1754 * needed.
1755 */
1756 static void
1757stropt_concat_with_comma(
1758 char_u *origval,
1759 char_u *newval,
1760 set_op_T op,
1761 int flags)
1762{
1763 int len = 0;
1764
1765 int comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1766 if (op == OP_ADDING)
1767 {
1768 len = (int)STRLEN(origval);
1769 // strip a trailing comma, would get 2
1770 if (comma && len > 1
1771 && (flags & P_ONECOMMA) == P_ONECOMMA
1772 && origval[len - 1] == ','
1773 && origval[len - 2] != '\\')
1774 len--;
1775 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1776 mch_memmove(newval, origval, (size_t)len);
1777 }
1778 else
1779 {
1780 len = (int)STRLEN(newval);
1781 STRMOVE(newval + len + comma, origval);
1782 }
1783 if (comma)
1784 newval[len] = ',';
1785}
1786
1787/*
1788 * Remove a value from a string option. Copy string option value in "origval"
1789 * to "newval" and then remove the string "strval" of length "len".
1790 */
1791 static void
1792stropt_remove_val(
1793 char_u *origval,
1794 char_u *newval,
1795 int flags,
1796 char_u *strval,
1797 int len)
1798{
1799 // Remove newval[] from origval[]. (Note: "len" has been set above
1800 // and is used here).
1801 STRCPY(newval, origval);
1802 if (*strval)
1803 {
1804 // may need to remove a comma
1805 if (flags & P_COMMA)
1806 {
1807 if (strval == origval)
1808 {
1809 // include comma after string
1810 if (strval[len] == ',')
1811 ++len;
1812 }
1813 else
1814 {
1815 // include comma before string
1816 --strval;
1817 ++len;
1818 }
1819 }
1820 STRMOVE(newval + (strval - origval), strval + len);
1821 }
1822}
1823
1824/*
1825 * Remove flags that appear twice in the string option value 'newval'.
1826 */
1827 static void
1828stropt_remove_dupflags(char_u *newval, int flags)
1829{
1830 char_u *s = newval;
1831
1832 // Remove flags that appear twice.
1833 while (*s)
1834 {
1835 // if options have P_FLAGLIST and P_ONECOMMA such as 'whichwrap'
1836 if (flags & P_ONECOMMA)
1837 {
1838 if (*s != ',' && *(s + 1) == ',' && vim_strchr(s + 2, *s) != NULL)
1839 {
1840 // Remove the duplicated value and the next comma.
1841 STRMOVE(s, s + 2);
1842 continue;
1843 }
1844 }
1845 else
1846 {
1847 if ((!(flags & P_COMMA) || *s != ',')
1848 && vim_strchr(s + 1, *s) != NULL)
1849 {
1850 STRMOVE(s, s + 1);
1851 continue;
1852 }
1853 }
1854 ++s;
1855 }
1856}
1857
1858/*
1859 * Get the string value specified for a ":set" command. The following set
1860 * options are supported:
1861 * set {opt}&
1862 * set {opt}<
1863 * set {opt}={val}
1864 * set {opt}:{val}
1865 */
1866 static char_u *
1867stropt_get_newval(
1868 int nextchar,
1869 int opt_idx,
1870 char_u **argp,
1871 char_u *varp,
1872 char_u **origval_arg,
1873 char_u **origval_l_arg,
1874 char_u **origval_g_arg,
1875 char_u **oldval_arg,
1876 set_op_T *op_arg,
1877 int flags,
1878 int cp_val)
1879{
1880 char_u *arg = *argp;
1881 char_u *origval = *origval_arg;
1882 char_u *origval_l = *origval_l_arg;
1883 char_u *origval_g = *origval_g_arg;
1884 char_u *oldval = *oldval_arg;
1885 set_op_T op = *op_arg;
1886 char_u *save_arg = NULL;
1887 char_u *newval;
1888 char_u *s = NULL;
1889 char_u whichwrap[80];
1890
1891 if (nextchar == '&') // set to default val
1892 newval = stropt_get_default_val(opt_idx, varp, flags, cp_val);
1893 else if (nextchar == '<') // set to global val
1894 newval = vim_strsave(*(char_u **)get_varp_scope(
1895 &(options[opt_idx]), OPT_GLOBAL));
1896 else
1897 {
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001898 ++arg; // jump to after the '=' or ':'
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001899
1900 // Set 'keywordprg' to ":help" if an empty
1901 // value was passed to :set by the user.
1902 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
1903 {
1904 save_arg = arg;
1905 arg = (char_u *)":help";
1906 }
1907 // Convert 'backspace' number to string
1908 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1909 opt_backspace_nr2str(varp, &origval, &origval_l, &origval_g,
1910 &oldval);
1911 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
1912 {
1913 // Convert 'whichwrap' number to string, for backwards
1914 // compatibility with Vim 3.0.
1915 char_u *t = opt_whichwrap_nr2str(&arg, whichwrap);
1916 save_arg = arg;
1917 arg = t;
1918 }
1919 // Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1920 // version 3.0
1921 else if (*arg == '>' && (varp == (char_u *)&p_dir
1922 || varp == (char_u *)&p_bdir))
1923 ++arg;
1924
1925 // Copy the new string into allocated memory.
1926 newval = stropt_copy_value(origval, &arg, op, flags);
1927 if (newval == NULL)
1928 goto done;
1929
1930 // Expand environment variables and ~.
1931 // Don't do it when adding without inserting a comma.
1932 if (op == OP_NONE || (flags & P_COMMA))
1933 {
1934 newval = stropt_expand_envvar(opt_idx, origval, newval, op);
1935 if (newval == NULL)
1936 goto done;
1937 }
1938
1939 // locate newval[] in origval[] when removing it and when adding to
1940 // avoid duplicates
1941 int len = 0;
1942 if (op == OP_REMOVING || (flags & P_NODUP))
1943 {
1944 len = (int)STRLEN(newval);
1945 s = find_dup_item(origval, newval, flags);
1946
1947 // do not add if already there
1948 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
1949 {
1950 op = OP_NONE;
1951 STRCPY(newval, origval);
1952 }
1953
1954 // if no duplicate, move pointer to end of original value
1955 if (s == NULL)
1956 s = origval + (int)STRLEN(origval);
1957 }
1958
1959 // concatenate the two strings; add a ',' if needed
1960 if (op == OP_ADDING || op == OP_PREPENDING)
1961 stropt_concat_with_comma(origval, newval, op, flags);
1962 else if (op == OP_REMOVING)
1963 // Remove newval[] from origval[]. (Note: "len" has been set above
1964 // and is used here).
1965 stropt_remove_val(origval, newval, flags, s, len);
1966
1967 if (flags & P_FLAGLIST)
1968 // Remove flags that appear twice.
1969 stropt_remove_dupflags(newval, flags);
1970 }
1971
1972done:
1973 if (save_arg != NULL)
1974 arg = save_arg; // arg was temporarily changed, restore it
1975 *argp = arg;
1976 *origval_arg = origval;
1977 *origval_l_arg = origval_l;
1978 *origval_g_arg = origval_g;
1979 *oldval_arg = oldval;
1980 *op_arg = op;
1981
1982 return newval;
1983}
1984
1985/*
Bram Moolenaar47403942022-09-21 21:12:53 +01001986 * Part of do_set() for string options.
1987 * Returns FAIL on failure, do not process further options.
1988 */
1989 static int
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001990do_set_option_string(
Bram Moolenaar47403942022-09-21 21:12:53 +01001991 int opt_idx,
1992 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01001993 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01001994 int nextchar,
1995 set_op_T op_arg,
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001996 long_u flags,
Bram Moolenaar47403942022-09-21 21:12:53 +01001997 int cp_val,
1998 char_u *varp_arg,
1999 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +01002000 size_t errbuflen,
Bram Moolenaar47403942022-09-21 21:12:53 +01002001 int *value_checked,
2002 char **errmsg)
2003{
Bram Moolenaar6f981142022-09-22 12:48:58 +01002004 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01002005 set_op_T op = op_arg;
2006 char_u *varp = varp_arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002007 char_u *oldval = NULL; // previous value if *varp
2008 char_u *newval;
2009 char_u *origval = NULL;
2010 char_u *origval_l = NULL;
2011 char_u *origval_g = NULL;
2012#if defined(FEAT_EVAL)
2013 char_u *saved_origval = NULL;
2014 char_u *saved_origval_l = NULL;
2015 char_u *saved_origval_g = NULL;
2016 char_u *saved_newval = NULL;
2017#endif
Bram Moolenaar47403942022-09-21 21:12:53 +01002018
2019 // When using ":set opt=val" for a global option
2020 // with a local value the local value will be
2021 // reset, use the global value here.
2022 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
2023 && ((int)options[opt_idx].indir & PV_BOTH))
2024 varp = options[opt_idx].var;
2025
2026 // The old value is kept until we are sure that the new value is valid.
2027 oldval = *(char_u **)varp;
2028
2029 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2030 {
2031 origval_l = *(char_u **)get_varp_scope(
2032 &(options[opt_idx]), OPT_LOCAL);
2033 origval_g = *(char_u **)get_varp_scope(
2034 &(options[opt_idx]), OPT_GLOBAL);
2035
2036 // A global-local string option might have an empty option as value to
2037 // indicate that the global value should be used.
2038 if (((int)options[opt_idx].indir & PV_BOTH)
2039 && origval_l == empty_option)
2040 origval_l = origval_g;
2041 }
2042
2043 // When setting the local value of a global option, the old value may be
2044 // the global value.
2045 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
2046 origval = *(char_u **)get_varp(&options[opt_idx]);
2047 else
2048 origval = oldval;
2049
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002050 // Get the new value for the option
2051 newval = stropt_get_newval(nextchar, opt_idx, &arg, varp, &origval,
2052 &origval_l, &origval_g, &oldval, &op, flags,
2053 cp_val);
Bram Moolenaar47403942022-09-21 21:12:53 +01002054
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002055 // Set the new value.
Bram Moolenaar47403942022-09-21 21:12:53 +01002056 *(char_u **)(varp) = newval;
Bram Moolenaar339e1142023-02-15 14:26:25 +00002057 if (newval == NULL)
2058 *(char_u **)(varp) = empty_option;
Bram Moolenaar47403942022-09-21 21:12:53 +01002059
2060#if defined(FEAT_EVAL)
2061 if (!starting
2062# ifdef FEAT_CRYPT
2063 && options[opt_idx].indir != PV_KEY
2064# endif
2065 && origval != NULL && newval != NULL)
2066 {
2067 // origval may be freed by did_set_string_option(), make a copy.
2068 saved_origval = vim_strsave(origval);
2069 // newval (and varp) may become invalid if the buffer is closed by
2070 // autocommands.
2071 saved_newval = vim_strsave(newval);
2072 if (origval_l != NULL)
2073 saved_origval_l = vim_strsave(origval_l);
2074 if (origval_g != NULL)
2075 saved_origval_g = vim_strsave(origval_g);
2076 }
2077#endif
2078
2079 {
2080 long_u *p = insecure_flag(opt_idx, opt_flags);
2081 int secure_saved = secure;
2082
2083 // When an option is set in the sandbox, from a modeline or in secure
2084 // mode, then deal with side effects in secure mode. Also when the
2085 // value was set with the P_INSECURE flag and is not completely
2086 // replaced.
2087 if ((opt_flags & OPT_MODELINE)
2088#ifdef HAVE_SANDBOX
2089 || sandbox != 0
2090#endif
2091 || (op != OP_NONE && (*p & P_INSECURE)))
2092 secure = 1;
2093
2094 // Handle side effects, and set the global value for ":set" on local
2095 // options. Note: when setting 'syntax' or 'filetype' autocommands may
2096 // be triggered that can cause havoc.
2097 *errmsg = did_set_string_option(
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002098 opt_idx, (char_u **)varp, oldval, newval, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002099 errbuflen, opt_flags, op, value_checked);
Bram Moolenaar47403942022-09-21 21:12:53 +01002100
2101 secure = secure_saved;
2102 }
2103
2104#if defined(FEAT_EVAL)
2105 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00002106 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01002107 saved_origval_l, saved_origval_g, saved_newval);
2108 vim_free(saved_origval);
2109 vim_free(saved_origval_l);
2110 vim_free(saved_origval_g);
2111 vim_free(saved_newval);
2112#endif
2113
Bram Moolenaar6f981142022-09-22 12:48:58 +01002114 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002115 return *errmsg == NULL ? OK : FAIL;
2116}
2117
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002119 * Set a boolean option.
2120 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002121 */
2122 static char *
2123do_set_option_bool(
2124 int opt_idx,
2125 int opt_flags,
2126 set_prefix_T prefix,
2127 long_u flags,
2128 char_u *varp,
2129 int nextchar,
2130 int afterchar,
2131 int cp_val)
2132
2133{
2134 varnumber_T value;
2135
2136 if (nextchar == '=' || nextchar == ':')
2137 return e_invalid_argument;
Bram Moolenaar546933f2023-02-06 16:40:49 +00002138 if (opt_idx < 0 || varp == NULL)
2139 return NULL; // "cannot happen"
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002140
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002141 // ":set opt!": invert
2142 // ":set opt&": reset to default value
2143 // ":set opt<": reset to global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002144 if (nextchar == '!')
2145 value = *(int *)(varp) ^ 1;
2146 else if (nextchar == '&')
2147 value = (int)(long)(long_i)options[opt_idx].def_val[
2148 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2149 else if (nextchar == '<')
2150 {
2151 // For 'autoread' -1 means to use global value.
2152 if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL)
2153 value = -1;
2154 else
2155 value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2156 }
2157 else
2158 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002159 // ":set invopt": invert
2160 // ":set opt" or ":set noopt": set or reset
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002161 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
2162 return e_trailing_characters;
2163 if (prefix == PREFIX_INV)
2164 value = *(int *)(varp) ^ 1;
2165 else
2166 value = prefix == PREFIX_NO ? 0 : 1;
2167 }
2168
2169 return set_bool_option(opt_idx, varp, (int)value, opt_flags);
2170}
2171
2172/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002173 * Set a numeric option.
2174 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002175 */
2176 static char *
2177do_set_option_numeric(
2178 int opt_idx,
2179 int opt_flags,
2180 char_u **argp,
2181 int nextchar,
2182 set_op_T op,
2183 long_u flags,
2184 int cp_val,
2185 char_u *varp,
2186 char *errbuf,
2187 size_t errbuflen)
2188{
2189 char_u *arg = *argp;
2190 varnumber_T value;
2191 int i;
2192 char *errmsg = NULL;
2193
Bram Moolenaar546933f2023-02-06 16:40:49 +00002194 if (opt_idx < 0 || varp == NULL)
2195 return NULL; // "cannot happen"
2196 //
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002197 /*
2198 * Different ways to set a number option:
2199 * & set to default value
2200 * < set to global value
2201 * <xx> accept special key codes for 'wildchar'
2202 * c accept any non-digit for 'wildchar'
2203 * [-]0-9 set number
2204 * other error
2205 */
2206 ++arg;
2207 if (nextchar == '&')
2208 value = (long)(long_i)options[opt_idx].def_val[
2209 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2210 else if (nextchar == '<')
2211 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002212 if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL)
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002213 // for 'undolevels' NO_LOCAL_UNDOLEVEL means using the global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002214 value = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002215 else if (opt_flags == OPT_LOCAL
2216 && ((long *)varp == &curwin->w_p_siso
2217 || (long *)varp == &curwin->w_p_so))
2218 // for 'scrolloff'/'sidescrolloff' -1 means using the global value
2219 value = -1;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002220 else
2221 value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2222 }
2223 else if (((long *)varp == &p_wc || (long *)varp == &p_wcm)
2224 && (*arg == '<'
2225 || *arg == '^'
2226 || (*arg != NUL
2227 && (!arg[1] || VIM_ISWHITE(arg[1]))
2228 && !VIM_ISDIGIT(*arg))))
2229 {
2230 value = string_to_key(arg, FALSE);
2231 if (value == 0 && (long *)varp != &p_wcm)
2232 {
2233 errmsg = e_invalid_argument;
2234 goto skip;
2235 }
2236 }
2237 else if (*arg == '-' || VIM_ISDIGIT(*arg))
2238 {
2239 // Allow negative (for 'undolevels'), octal and hex numbers.
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002240 vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE, NULL);
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002241 if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i])))
2242 {
2243 errmsg = e_number_required_after_equal;
2244 goto skip;
2245 }
2246 }
2247 else
2248 {
2249 errmsg = e_number_required_after_equal;
2250 goto skip;
2251 }
2252
2253 if (op == OP_ADDING)
2254 value = *(long *)varp + value;
2255 else if (op == OP_PREPENDING)
2256 value = *(long *)varp * value;
2257 else if (op == OP_REMOVING)
2258 value = *(long *)varp - value;
2259
2260 errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen,
2261 opt_flags);
2262
2263skip:
2264 *argp = arg;
2265 return errmsg;
2266}
2267
2268/*
2269 * Set a key code (t_xx) option
2270 */
2271 static char *
2272do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar)
2273{
2274 char_u *arg = *argp;
2275 char_u *p;
2276
2277 if (nextchar == '&')
2278 {
2279 if (add_termcap_entry(key_name, TRUE) == FAIL)
2280 return e_not_found_in_termcap;
2281 }
2282 else
2283 {
2284 ++arg; // jump to after the '=' or ':'
2285 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
2286 if (*p == '\\' && p[1] != NUL)
2287 ++p;
2288 nextchar = *p;
2289 *p = NUL;
2290 add_termcode(key_name, arg, FALSE);
2291 *p = nextchar;
2292 }
2293 if (full_screen)
2294 ttest(FALSE);
2295 redraw_all_later(UPD_CLEAR);
2296
2297 *argp = arg;
2298 return NULL;
2299}
2300
2301/*
2302 * Set an option to a new value.
2303 */
2304 static char *
2305do_set_option_value(
2306 int opt_idx,
2307 int opt_flags,
2308 char_u **argp,
2309 set_prefix_T prefix,
2310 set_op_T op,
2311 long_u flags,
2312 char_u *varp,
2313 char_u *key_name,
2314 int nextchar,
2315 int afterchar,
2316 int cp_val,
2317 int *stopopteval,
2318 char *errbuf,
2319 size_t errbuflen)
2320{
2321 int value_checked = FALSE;
2322 char *errmsg = NULL;
2323 char_u *arg = *argp;
2324
2325 if (flags & P_BOOL)
2326 {
2327 // boolean option
2328 errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp,
2329 nextchar, afterchar, cp_val);
2330 if (errmsg != NULL)
2331 goto skip;
2332 }
2333 else
2334 {
2335 // numeric or string option
2336 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2337 || prefix != PREFIX_NONE)
2338 {
2339 errmsg = e_invalid_argument;
2340 goto skip;
2341 }
2342
2343 if (flags & P_NUM)
2344 {
2345 // numeric option
2346 errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar,
2347 op, flags, cp_val, varp,
2348 errbuf, errbuflen);
2349 if (errmsg != NULL)
2350 goto skip;
2351 }
2352 else if (opt_idx >= 0)
2353 {
2354 // string option
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002355 if (do_set_option_string(opt_idx, opt_flags, &arg, nextchar, op,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002356 flags, cp_val, varp, errbuf, errbuflen,
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002357 &value_checked, &errmsg) == FAIL)
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002358 {
2359 if (errmsg != NULL)
2360 goto skip;
2361 *stopopteval = TRUE;
2362 goto skip;
2363 }
2364 }
2365 else
2366 {
2367 // key code option
2368 errmsg = do_set_option_keycode(&arg, key_name, nextchar);
2369 if (errmsg != NULL)
2370 goto skip;
2371 }
2372 }
2373
2374 if (opt_idx >= 0)
2375 did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked);
2376
2377skip:
2378 *argp = arg;
2379 return errmsg;
2380}
2381
2382/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002383 * Set an option to a new value.
2384 * Return NULL if OK, return an untranslated error message when something is
2385 * wrong. "errbuf[errbuflen]" can be used to create the error message.
2386 */
2387 static char *
2388do_set_option(
2389 int opt_flags,
2390 char_u **argp,
2391 char_u *arg_start,
2392 char_u **startarg,
2393 int *did_show,
2394 int *stopopteval,
2395 char *errbuf,
2396 size_t errbuflen)
2397{
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002398 int opt_idx;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002399 char_u *arg;
2400 set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix
2401 set_op_T op;
2402 long_u flags; // flags for current option
2403 char_u *varp; // pointer to variable for current option
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002404 char_u key_name[2];
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002405 int nextchar; // next non-white char after option name
2406 int afterchar; // character just after option name
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002407 char *errmsg = NULL;
2408 int key;
2409 int len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002410
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002411 prefix = get_option_prefix(argp);
2412 arg = *argp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002413
2414 // find end of name
2415 key = 0;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002416 if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL)
2417 return e_invalid_argument;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002418
2419 // remember character after option name
2420 afterchar = arg[len];
2421
2422 if (in_vim9script())
2423 {
2424 char_u *p = skipwhite(arg + len);
2425
2426 // disallow white space before =val, +=val, -=val, ^=val
2427 if (p > arg + len && (p[0] == '='
2428 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
2429 && p[1] == '=')))
2430 {
2431 errmsg = e_no_white_space_allowed_between_option_and;
2432 arg = p;
2433 *startarg = p;
2434 goto skip;
2435 }
2436 }
2437 else
2438 // skip white space, allow ":set ai ?", ":set hlsearch !"
2439 while (VIM_ISWHITE(arg[len]))
2440 ++len;
2441
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002442 op = get_opt_op(arg + len);
2443 if (op != OP_NONE)
2444 len++;
2445
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002446 nextchar = arg[len];
2447
2448 if (opt_idx == -1 && key == 0) // found a mismatch: skip
2449 {
2450 if (in_vim9script() && arg > arg_start
2451 && vim_strchr((char_u *)"!&<", *arg) != NULL)
2452 errmsg = e_no_white_space_allowed_between_option_and;
2453 else
2454 errmsg = e_unknown_option;
2455 goto skip;
2456 }
2457
2458 if (opt_idx >= 0)
2459 {
2460 if (options[opt_idx].var == NULL) // hidden option: skip
2461 {
2462 // Only give an error message when requesting the value of
2463 // a hidden option, ignore setting it.
2464 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
2465 && (!(options[opt_idx].flags & P_BOOL)
2466 || nextchar == '?'))
2467 errmsg = e_option_not_supported;
2468 goto skip;
2469 }
2470
2471 flags = options[opt_idx].flags;
2472 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
2473 }
2474 else
2475 {
2476 flags = P_STRING;
Bram Moolenaar40b48722023-02-05 17:04:50 +00002477 varp = NULL;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002478 if (key < 0)
2479 {
2480 key_name[0] = KEY2TERMCAP0(key);
2481 key_name[1] = KEY2TERMCAP1(key);
2482 }
2483 else
2484 {
2485 key_name[0] = KS_KEY;
2486 key_name[1] = (key & 0xff);
2487 }
2488 }
2489
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002490 // Make sure the option value can be changed.
2491 if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002492 goto skip;
2493
Bram Moolenaar40b48722023-02-05 17:04:50 +00002494 int cp_val = p_cp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002495 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
2496 {
2497 arg += len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002498 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
2499 {
2500 if (arg[3] == 'm') // "opt&vim": set to Vim default
2501 {
2502 cp_val = FALSE;
2503 arg += 3;
2504 }
2505 else // "opt&vi": set to Vi default
2506 {
2507 cp_val = TRUE;
2508 arg += 2;
2509 }
2510 }
2511 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
2512 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
2513 {
2514 errmsg = e_trailing_characters;
2515 goto skip;
2516 }
2517 }
2518
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002519 // Allow '=' and ':' for historical reasons (MSDOS command.com).
2520 // Allows only one '=' character per "set" command line. grrr. (jw)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002521 if (nextchar == '?'
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002522 || (prefix == PREFIX_NONE
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002523 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
2524 && !(flags & P_BOOL)))
2525 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002526 // print value
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002527 if (*did_show)
2528 msg_putchar('\n'); // cursor below last one
2529 else
2530 {
2531 gotocmdline(TRUE); // cursor at status line
2532 *did_show = TRUE; // remember that we did a line
2533 }
2534 if (opt_idx >= 0)
2535 {
2536 showoneopt(&options[opt_idx], opt_flags);
2537#ifdef FEAT_EVAL
2538 if (p_verbose > 0)
2539 {
2540 // Mention where the option was last set.
2541 if (varp == options[opt_idx].var)
2542 last_set_msg(options[opt_idx].script_ctx);
2543 else if ((int)options[opt_idx].indir & PV_WIN)
2544 last_set_msg(curwin->w_p_script_ctx[
2545 (int)options[opt_idx].indir & PV_MASK]);
2546 else if ((int)options[opt_idx].indir & PV_BUF)
2547 last_set_msg(curbuf->b_p_script_ctx[
2548 (int)options[opt_idx].indir & PV_MASK]);
2549 }
2550#endif
2551 }
2552 else
2553 {
2554 char_u *p;
2555
2556 p = find_termcode(key_name);
2557 if (p == NULL)
2558 {
2559 errmsg = e_key_code_not_set;
2560 goto skip;
2561 }
2562 else
2563 (void)show_one_termcode(key_name, p, TRUE);
2564 }
2565 if (nextchar != '?'
2566 && nextchar != NUL && !VIM_ISWHITE(afterchar))
2567 errmsg = e_trailing_characters;
2568 }
2569 else
2570 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002571 errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op,
2572 flags, varp, key_name, nextchar,
2573 afterchar, cp_val, stopopteval, errbuf,
2574 errbuflen);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002575 }
2576
2577skip:
2578 *argp = arg;
2579 return errmsg;
2580}
2581
2582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 * Parse 'arg' for option settings.
2584 *
2585 * 'arg' may be IObuff, but only when no errors can be present and option
2586 * does not need to be expanded with option_expand().
2587 * "opt_flags":
2588 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002589 * OPT_GLOBAL for ":setglobal"
2590 * OPT_LOCAL for ":setlocal" and a modeline
2591 * OPT_MODELINE for a modeline
2592 * OPT_WINONLY to only set window-local options
2593 * OPT_NOWIN to skip setting window-local options
2594 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002596 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 */
2598 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002599do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002600 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002601 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002603 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002605 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606
2607 if (*arg == NUL)
2608 {
2609 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002610 did_show = TRUE;
2611 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 }
2613
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002614 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002616 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002617 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002619 // ":set all" show all options.
2620 // ":set all&" set all options to their default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 arg += 3;
2622 if (*arg == '&')
2623 {
2624 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002625 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002627 didset_options();
2628 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002629 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 }
2631 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002632 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002634 did_show = TRUE;
2635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002637 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 {
2639 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002640 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002641 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 arg += 7;
2643 }
2644 else
2645 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002646 int stopopteval = FALSE;
2647 char *errmsg = NULL;
Christian Brabandtb39b2402023-11-29 11:34:05 +01002648 char errbuf[ERR_BUFLEN];
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002649 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002651 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2652 &did_show, &stopopteval, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002653 ERR_BUFLEN);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002654 if (stopopteval)
2655 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002657 // Advance to next argument.
2658 // - skip until a blank found, taking care of backslashes
2659 // - skip blanks
2660 // - skip one "=val" argument (for hidden options ":set gfn =xx")
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 for (i = 0; i < 2 ; ++i)
2662 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002663 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 if (*arg++ == '\\' && *arg != NUL)
2665 ++arg;
2666 arg = skipwhite(arg);
2667 if (*arg != '=')
2668 break;
2669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002671 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002673 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
2674 i = (int)STRLEN(IObuff) + 2;
2675 if (i + (arg - startarg) < IOSIZE)
2676 {
2677 // append the argument with the error
2678 STRCAT(IObuff, ": ");
2679 mch_memmove(IObuff + i, startarg, (arg - startarg));
2680 IObuff[i + (arg - startarg)] = NUL;
2681 }
2682 // make sure all characters are printable
2683 trans_characters(IObuff, IOSIZE);
2684
2685 ++no_wait_return; // wait_return() done later
2686 emsg((char *)IObuff); // show error highlighted
2687 --no_wait_return;
2688
2689 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 }
2692
2693 arg = skipwhite(arg);
2694 }
2695
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002696theend:
2697 if (silent_mode && did_show)
2698 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002699 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002700 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002701 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002702 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002703 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002704 out_flush();
2705 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002706 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002707 }
2708
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 return OK;
2710}
2711
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002712/*
2713 * Call this when an option has been given a new value through a user command.
2714 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2715 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002716 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002717did_set_option(
2718 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002719 int opt_flags, // possibly with OPT_MODELINE
2720 int new_value, // value was replaced completely
2721 int value_checked) // value was checked to be safe, no need to set the
2722 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002723{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002724 long_u *p;
2725
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002726 options[opt_idx].flags |= P_WAS_SET;
2727
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002728 // When an option is set in the sandbox, from a modeline or in secure mode
2729 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2730 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002731 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002732 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002733#ifdef HAVE_SANDBOX
2734 || sandbox != 0
2735#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002736 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002737 *p = *p | P_INSECURE;
2738 else if (new_value)
2739 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002740}
2741
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742/*
2743 * Convert a key name or string into a key value.
2744 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002745 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002747 int
2748string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749{
2750 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002751 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 if (*arg == '^')
2753 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002754 if (multi_byte)
2755 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 return *arg;
2757}
2758
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759/*
2760 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2761 * maketitle() to create and display it.
2762 * When switching the title or icon off, call mch_restore_title() to get
2763 * the old value back.
2764 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002765 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002766did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767{
2768 if (starting != NO_SCREEN
2769#ifdef FEAT_GUI
2770 && !gui.starting
2771#endif
2772 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775
2776/*
2777 * set_options_bin - called when 'bin' changes value.
2778 */
2779 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002780set_options_bin(
2781 int oldval,
2782 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002783 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002785 // The option values that are changed when 'bin' changes are
2786 // copied when 'bin is set and restored when 'bin' is reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 if (newval)
2788 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002789 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 {
2791 if (!(opt_flags & OPT_GLOBAL))
2792 {
2793 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2794 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2795 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2796 curbuf->b_p_et_nobin = curbuf->b_p_et;
2797 }
2798 if (!(opt_flags & OPT_LOCAL))
2799 {
2800 p_tw_nobin = p_tw;
2801 p_wm_nobin = p_wm;
2802 p_ml_nobin = p_ml;
2803 p_et_nobin = p_et;
2804 }
2805 }
2806
2807 if (!(opt_flags & OPT_GLOBAL))
2808 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002809 curbuf->b_p_tw = 0; // no automatic line wrap
2810 curbuf->b_p_wm = 0; // no automatic line wrap
2811 curbuf->b_p_ml = 0; // no modelines
2812 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 }
2814 if (!(opt_flags & OPT_LOCAL))
2815 {
2816 p_tw = 0;
2817 p_wm = 0;
2818 p_ml = FALSE;
2819 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002820 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 }
2822 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002823 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 {
2825 if (!(opt_flags & OPT_GLOBAL))
2826 {
2827 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2828 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2829 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2830 curbuf->b_p_et = curbuf->b_p_et_nobin;
2831 }
2832 if (!(opt_flags & OPT_LOCAL))
2833 {
2834 p_tw = p_tw_nobin;
2835 p_wm = p_wm_nobin;
2836 p_ml = p_ml_nobin;
2837 p_et = p_et_nobin;
2838 }
2839 }
Christian Brabandt757593c2023-08-22 21:44:10 +02002840#if defined(FEAT_EVAL) || defined(PROTO)
2841 // Remember where the dependent option were reset
2842 didset_options_sctx(opt_flags, p_bin_dep_opts);
2843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844}
2845
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846/*
2847 * Expand environment variables for some string options.
2848 * These string options cannot be indirect!
2849 * If "val" is NULL expand the current value of the option.
2850 * Return pointer to NameBuff, or NULL when not expanded.
2851 */
2852 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002853option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002855 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2857 return NULL;
2858
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002859 // If val is longer than MAXPATHL no meaningful expansion can be done,
2860 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 if (val != NULL && STRLEN(val) > MAXPATHL)
2862 return NULL;
2863
2864 if (val == NULL)
2865 val = *(char_u **)options[opt_idx].var;
2866
2867 /*
2868 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2869 * Escape spaces when expanding 'tags', they are used to separate file
2870 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002871 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 */
2873 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002874 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002875#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002876 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2877#endif
2878 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002879 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 return NULL;
2881
2882 return NameBuff;
2883}
2884
2885/*
2886 * After setting various option values: recompute variables that depend on
2887 * option values.
2888 */
2889 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002890didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002892 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 (void)init_chartab();
2894
Bram Moolenaardac13472019-09-16 21:06:21 +02002895 didset_string_options();
2896
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002897#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002898 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002899 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002900 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002901 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002902#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002903 // set cedit_key
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002904 (void)did_set_cedit(NULL);
Bram Moolenaar597a4222014-06-25 14:39:50 +02002905#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002906 // initialize the table for 'breakat'.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002907 did_set_breakat(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002908#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002909 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002910}
2911
2912/*
2913 * More side effects of setting options.
2914 */
2915 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002916didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002917{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002918 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002919 (void)highlight_changed();
2920
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002921 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002922 check_opt_wim();
2923
Bram Moolenaareed9d462021-02-15 20:38:25 +01002924 // Parse default for 'listchars'.
zeertzjq6a8d2e12024-01-17 20:54:49 +01002925 (void)set_listchars_option(curwin, curwin->w_p_lcs, TRUE, NULL, 0);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002926
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002927 // Parse default for 'fillchars'.
zeertzjq6a8d2e12024-01-17 20:54:49 +01002928 (void)set_fillchars_option(curwin, curwin->w_p_fcs, TRUE, NULL, 0);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002929
2930#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002931 // Parse default for 'clipboard'
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002932 (void)did_set_clipboard(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002933#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002934#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002935 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002936 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002937 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002938 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940}
2941
2942/*
2943 * Check for string options that are NULL (normally only termcap options).
2944 */
2945 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002946check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947{
2948 int opt_idx;
2949
2950 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2951 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2952 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2953}
2954
2955/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002956 * Return the option index found by a pointer into term_strings[].
2957 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002959 int
2960get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002962 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963
2964 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2965 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002966 return opt_idx;
2967 return -1; // cannot happen: didn't find it!
2968}
2969
2970/*
2971 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2972 * Return the option index or -1 if not found.
2973 */
2974 int
2975set_term_option_alloced(char_u **p)
2976{
2977 int opt_idx = get_term_opt_idx(p);
2978
2979 if (opt_idx >= 0)
2980 options[opt_idx].flags |= P_ALLOCED;
2981 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982}
2983
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002984#if defined(FEAT_EVAL) || defined(PROTO)
2985/*
2986 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2987 * Return FALSE when it wasn't.
2988 * Return -1 for an unknown option.
2989 */
2990 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002991was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002992{
2993 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002994 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002995
2996 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002997 {
2998 flagp = insecure_flag(idx, opt_flags);
2999 return (*flagp & P_INSECURE) != 0;
3000 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01003001 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003002 return -1;
3003}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003004
3005/*
3006 * Get a pointer to the flags used for the P_INSECURE flag of option
3007 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01003008 * NOTE: Caller must make sure that "curwin" is set to the window from which
3009 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003010 */
3011 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003012insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003013{
3014 if (opt_flags & OPT_LOCAL)
3015 switch ((int)options[opt_idx].indir)
3016 {
3017#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003018 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003019#endif
3020#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00003021# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003022 case PV_FDE: return &curwin->w_p_fde_flags;
3023 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00003024# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00003025# ifdef FEAT_BEVAL
3026 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
3027# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003028 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003029 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003030# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003031 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003032# endif
3033#endif
3034 }
3035
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003036 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003037 return &options[opt_idx].flags;
3038}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003039#endif
3040
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003041/*
3042 * Redraw the window title and/or tab page text later.
3043 */
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02003044 void
3045redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003046{
3047 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003048 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003049}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003050
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02003052 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
3053 * characters or characters in "allowed".
3054 */
Bram Moolenaare677df82019-09-02 22:31:11 +02003055 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02003056valid_name(char_u *val, char *allowed)
3057{
3058 char_u *s;
3059
3060 for (s = val; *s != NUL; ++s)
3061 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
3062 return FALSE;
3063 return TRUE;
3064}
3065
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003066#if defined(FEAT_EVAL) || defined(PROTO)
3067/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003068 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003069 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003070 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003071 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003072set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003073{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003074 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
3075 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003076 sctx_T new_script_ctx = script_ctx;
3077
Bram Moolenaar51258742020-05-03 17:19:33 +02003078 // Modeline already has the line number set.
3079 if (!(opt_flags & OPT_MODELINE))
3080 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003081
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003082 // Remember where the option was set. For local options need to do that
3083 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003084 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003085 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003086 if (both || (opt_flags & OPT_LOCAL))
3087 {
3088 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003089 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003090 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00003091 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003092 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00003093 if (both)
3094 // also setting the "all buffers" value
3095 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
3096 new_script_ctx;
3097 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003098 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003099}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003100
3101/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00003102 * Get the script context of global option "name".
3103 *
3104 */
3105 sctx_T *
3106get_option_sctx(char *name)
3107{
3108 int idx = findoption((char_u *)name);
3109
3110 if (idx >= 0)
3111 return &options[idx].script_ctx;
3112 siemsg("no such option: %s", name);
3113 return NULL;
3114}
3115
3116/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003117 * Set the script_ctx for a termcap option.
3118 * "name" must be the two character code, e.g. "RV".
3119 * When "name" is NULL use "opt_idx".
3120 */
3121 void
3122set_term_option_sctx_idx(char *name, int opt_idx)
3123{
3124 char_u buf[5];
3125 int idx;
3126
3127 if (name == NULL)
3128 idx = opt_idx;
3129 else
3130 {
3131 buf[0] = 't';
3132 buf[1] = '_';
3133 buf[2] = name[0];
3134 buf[3] = name[1];
3135 buf[4] = 0;
3136 idx = findoption(buf);
3137 }
3138 if (idx >= 0)
3139 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
3140}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003141#endif
3142
Bram Moolenaarf4140482020-02-15 23:06:45 +01003143#if defined(FEAT_EVAL)
3144/*
3145 * Apply the OptionSet autocommand.
3146 */
3147 static void
3148apply_optionset_autocmd(
3149 int opt_idx,
3150 long opt_flags,
3151 long oldval,
3152 long oldval_g,
3153 long newval,
3154 char *errmsg)
3155{
3156 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
3157
3158 // Don't do this while starting up, failure or recursively.
3159 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
3160 return;
3161
3162 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
3163 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
3164 oldval_g);
3165 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
3166 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
3167 (opt_flags & OPT_LOCAL) ? "local" : "global");
3168 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3169 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3170 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
3171 if (opt_flags & OPT_LOCAL)
3172 {
3173 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3174 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3175 }
3176 if (opt_flags & OPT_GLOBAL)
3177 {
3178 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3179 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3180 }
3181 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3182 {
3183 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3184 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3185 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3186 }
3187 if (opt_flags & OPT_MODELINE)
3188 {
3189 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3190 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3191 }
3192 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3193 NULL, FALSE, NULL);
3194 reset_v_option_vars();
3195}
3196#endif
3197
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003198#if defined(FEAT_ARABIC) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003199/*
3200 * Process the updated 'arabic' option value.
3201 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003202 char *
3203did_set_arabic(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003204{
3205 char *errmsg = NULL;
3206
3207 if (curwin->w_p_arab)
3208 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003209 // 'arabic' is set, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003210 if (!p_tbidi)
3211 {
3212 // set rightleft mode
3213 if (!curwin->w_p_rl)
3214 {
3215 curwin->w_p_rl = TRUE;
3216 changed_window_setting();
3217 }
3218
3219 // Enable Arabic shaping (major part of what Arabic requires)
3220 if (!p_arshape)
3221 {
3222 p_arshape = TRUE;
3223 redraw_later_clear();
3224 }
3225 }
3226
3227 // Arabic requires a utf-8 encoding, inform the user if it's not
3228 // set.
3229 if (STRCMP(p_enc, "utf-8") != 0)
3230 {
3231 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3232
3233 msg_source(HL_ATTR(HLF_W));
3234 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
3235#ifdef FEAT_EVAL
3236 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3237#endif
3238 }
3239
3240 // set 'delcombine'
3241 p_deco = TRUE;
3242
3243# ifdef FEAT_KEYMAP
3244 // Force-set the necessary keymap for arabic
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003245 errmsg = set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3246 OPT_LOCAL);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003247# endif
3248 }
3249 else
3250 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003251 // 'arabic' is reset, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003252 if (!p_tbidi)
3253 {
3254 // reset rightleft mode
3255 if (curwin->w_p_rl)
3256 {
3257 curwin->w_p_rl = FALSE;
3258 changed_window_setting();
3259 }
3260
3261 // 'arabicshape' isn't reset, it is a global option and
3262 // another window may still need it "on".
3263 }
3264
3265 // 'delcombine' isn't reset, it is a global option and another
3266 // window may still want it "on".
3267
3268# ifdef FEAT_KEYMAP
3269 // Revert to the default keymap
3270 curbuf->b_p_iminsert = B_IMODE_NONE;
3271 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3272# endif
3273 }
3274
3275 return errmsg;
3276}
3277#endif
3278
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003279#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
3280/*
3281 * Process the updated 'autochdir' option value.
3282 */
3283 char *
3284did_set_autochdir(optset_T *args UNUSED)
3285{
3286 // Change directories when the 'acd' option is set now.
3287 DO_AUTOCHDIR;
3288 return NULL;
3289}
3290#endif
3291
3292#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
3293/*
3294 * Process the updated 'ballooneval' option value.
3295 */
3296 char *
3297did_set_ballooneval(optset_T *args)
3298{
3299 if (balloonEvalForTerm)
3300 return NULL;
3301
3302 if (p_beval && !args->os_oldval.boolean)
3303 gui_mch_enable_beval_area(balloonEval);
3304 else if (!p_beval && args->os_oldval.boolean)
3305 gui_mch_disable_beval_area(balloonEval);
3306
3307 return NULL;
3308}
3309#endif
3310
3311#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
3312/*
3313 * Process the updated 'balloonevalterm' option value.
3314 */
3315 char *
3316did_set_balloonevalterm(optset_T *args UNUSED)
3317{
3318 mch_bevalterm_changed();
3319 return NULL;
3320}
3321#endif
3322
3323/*
3324 * Process the updated 'binary' option value.
3325 */
3326 char *
3327did_set_binary(optset_T *args)
3328{
3329 // when 'bin' is set also set some other options
3330 set_options_bin(args->os_oldval.boolean, curbuf->b_p_bin, args->os_flags);
3331 redraw_titles();
3332
3333 return NULL;
3334}
3335
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003336/*
3337 * Process the updated 'buflisted' option value.
3338 */
3339 char *
3340did_set_buflisted(optset_T *args)
3341{
3342 // when 'buflisted' changes, trigger autocommands
3343 if (args->os_oldval.boolean != curbuf->b_p_bl)
3344 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3345 NULL, NULL, TRUE, curbuf);
3346 return NULL;
3347}
3348
3349/*
3350 * Process the new 'cmdheight' option value.
3351 */
3352 char *
3353did_set_cmdheight(optset_T *args)
3354{
3355 long old_value = args->os_oldval.number;
3356 char *errmsg = NULL;
3357
3358 // if p_ch changed value, change the command line height
3359 if (p_ch < 1)
3360 {
3361 errmsg = e_argument_must_be_positive;
3362 p_ch = 1;
3363 }
3364 if (p_ch > Rows - min_rows() + 1)
3365 p_ch = Rows - min_rows() + 1;
3366
3367 // Only compute the new window layout when startup has been
3368 // completed. Otherwise the frame sizes may be wrong.
3369 if ((p_ch != old_value
3370 || tabline_height() + topframe->fr_height != Rows - p_ch)
3371 && full_screen
3372#ifdef FEAT_GUI
3373 && !gui.starting
3374#endif
3375 )
3376 command_height();
3377
3378 return errmsg;
3379}
3380
3381/*
3382 * Process the updated 'compatible' option value.
3383 */
3384 char *
3385did_set_compatible(optset_T *args UNUSED)
3386{
3387 compatible_set();
3388 return NULL;
3389}
3390
3391#if defined(FEAT_CONCEAL) || defined(PROTO)
3392/*
3393 * Process the new 'conceallevel' option value.
3394 */
3395 char *
3396did_set_conceallevel(optset_T *args UNUSED)
3397{
3398 char *errmsg = NULL;
3399
3400 if (curwin->w_p_cole < 0)
3401 {
3402 errmsg = e_argument_must_be_positive;
3403 curwin->w_p_cole = 0;
3404 }
3405 else if (curwin->w_p_cole > 3)
3406 {
3407 errmsg = e_invalid_argument;
3408 curwin->w_p_cole = 3;
3409 }
3410
3411 return errmsg;
3412}
3413#endif
3414
3415#if defined(FEAT_DIFF) || defined(PROTO)
3416/*
3417 * Process the updated 'diff' option value.
3418 */
3419 char *
3420did_set_diff(optset_T *args UNUSED)
3421{
3422 // May add or remove the buffer from the list of diff buffers.
3423 diff_buf_adjust(curwin);
3424# ifdef FEAT_FOLDING
3425 if (foldmethodIsDiff(curwin))
3426 foldUpdateAll(curwin);
3427# endif
3428 return NULL;
3429}
3430#endif
3431
3432/*
3433 * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb'
3434 * option value.
3435 */
3436 char *
3437did_set_eof_eol_fixeol_bomb(optset_T *args UNUSED)
3438{
3439 // redraw the window title and tab page text
3440 redraw_titles();
3441 return NULL;
3442}
3443
3444/*
3445 * Process the updated 'equalalways' option value.
3446 */
3447 char *
3448did_set_equalalways(optset_T *args)
3449{
3450 if (p_ea && !args->os_oldval.boolean)
3451 win_equal(curwin, FALSE, 0);
3452
3453 return NULL;
3454}
3455
3456#if defined(FEAT_FOLDING) || defined(PROTO)
3457/*
3458 * Process the new 'foldcolumn' option value.
3459 */
3460 char *
3461did_set_foldcolumn(optset_T *args UNUSED)
3462{
3463 char *errmsg = NULL;
3464
3465 if (curwin->w_p_fdc < 0)
3466 {
3467 errmsg = e_argument_must_be_positive;
3468 curwin->w_p_fdc = 0;
3469 }
3470 else if (curwin->w_p_fdc > 12)
3471 {
3472 errmsg = e_invalid_argument;
3473 curwin->w_p_fdc = 12;
3474 }
3475
3476 return errmsg;
3477}
3478
3479/*
3480 * Process the new 'foldlevel' option value.
3481 */
3482 char *
3483did_set_foldlevel(optset_T *args UNUSED)
3484{
3485 if (curwin->w_p_fdl < 0)
3486 curwin->w_p_fdl = 0;
3487 newFoldLevel();
3488 return NULL;
3489}
3490
3491/*
3492 * Process the new 'foldminlines' option value.
3493 */
3494 char *
3495did_set_foldminlines(optset_T *args UNUSED)
3496{
3497 foldUpdateAll(curwin);
3498 return NULL;
3499}
3500
3501/*
3502 * Process the new 'foldnestmax' option value.
3503 */
3504 char *
3505did_set_foldnestmax(optset_T *args UNUSED)
3506{
3507 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3508 foldUpdateAll(curwin);
3509 return NULL;
3510}
3511#endif
3512
3513#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
3514/*
3515 * Process the updated 'hlsearch' option value.
3516 */
3517 char *
3518did_set_hlsearch(optset_T *args UNUSED)
3519{
3520 // when 'hlsearch' is set or reset: reset no_hlsearch
3521 set_no_hlsearch(FALSE);
3522 return NULL;
3523}
3524#endif
3525
3526/*
3527 * Process the updated 'ignorecase' option value.
3528 */
3529 char *
3530did_set_ignorecase(optset_T *args UNUSED)
3531{
3532 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
3533 if (p_hls)
3534 redraw_all_later(UPD_SOME_VALID);
3535 return NULL;
3536}
3537
3538#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
3539/*
3540 * Process the updated 'imdisable' option value.
3541 */
3542 char *
3543did_set_imdisable(optset_T *args UNUSED)
3544{
3545 // Only de-activate it here, it will be enabled when changing mode.
3546 if (p_imdisable)
3547 im_set_active(FALSE);
3548 else if (State & MODE_INSERT)
3549 // When the option is set from an autocommand, it may need to take
3550 // effect right away.
3551 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
3552 return NULL;
3553}
3554#endif
3555
3556/*
3557 * Process the new 'iminsert' option value.
3558 */
3559 char *
3560did_set_iminsert(optset_T *args UNUSED)
3561{
3562 char *errmsg = NULL;
3563
3564 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3565 {
3566 errmsg = e_invalid_argument;
3567 curbuf->b_p_iminsert = B_IMODE_NONE;
3568 }
3569 p_iminsert = curbuf->b_p_iminsert;
3570 if (termcap_active) // don't do this in the alternate screen
3571 showmode();
3572#if defined(FEAT_KEYMAP)
3573 // Show/unshow value of 'keymap' in status lines.
3574 status_redraw_curbuf();
3575#endif
3576
3577 return errmsg;
3578}
3579
3580/*
3581 * Process the new 'imsearch' option value.
3582 */
3583 char *
3584did_set_imsearch(optset_T *args UNUSED)
3585{
3586 char *errmsg = NULL;
3587
3588 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3589 {
3590 errmsg = e_invalid_argument;
3591 curbuf->b_p_imsearch = B_IMODE_NONE;
3592 }
3593 p_imsearch = curbuf->b_p_imsearch;
3594
3595 return errmsg;
3596}
3597
3598#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3599/*
3600 * Process the new 'imstyle' option value.
3601 */
3602 char *
3603did_set_imstyle(optset_T *args UNUSED)
3604{
3605 char *errmsg = NULL;
3606
3607 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3608 errmsg = e_invalid_argument;
3609
3610 return errmsg;
3611}
3612#endif
3613
3614/*
3615 * Process the updated 'insertmode' option value.
3616 */
3617 char *
3618did_set_insertmode(optset_T *args)
3619{
3620 // when 'insertmode' is set from an autocommand need to do work here
3621 if (p_im)
3622 {
3623 if ((State & MODE_INSERT) == 0)
3624 need_start_insertmode = TRUE;
3625 stop_insert_mode = FALSE;
3626 }
3627 // only reset if it was set previously
3628 else if (args->os_oldval.boolean)
3629 {
3630 need_start_insertmode = FALSE;
3631 stop_insert_mode = TRUE;
3632 if (restart_edit != 0 && mode_displayed)
3633 clear_cmdline = TRUE; // remove "(insert)"
3634 restart_edit = 0;
3635 }
3636
3637 return NULL;
3638}
3639
3640#if defined(FEAT_LANGMAP) || defined(PROTO)
3641/*
3642 * Process the updated 'langnoremap' option value.
3643 */
3644 char *
3645did_set_langnoremap(optset_T *args UNUSED)
3646{
3647 // 'langnoremap' -> !'langremap'
3648 p_lrm = !p_lnr;
3649 return NULL;
3650}
3651
3652/*
3653 * Process the updated 'langremap' option value.
3654 */
3655 char *
3656did_set_langremap(optset_T *args UNUSED)
3657{
3658 // 'langremap' -> !'langnoremap'
3659 p_lnr = !p_lrm;
3660 return NULL;
3661}
3662#endif
3663
3664/*
3665 * Process the new 'laststatus' option value.
3666 */
3667 char *
3668did_set_laststatus(optset_T *args UNUSED)
3669{
3670 last_status(FALSE); // (re)set last window status line
3671 return NULL;
3672}
3673
3674#if defined(FEAT_GUI) || defined(PROTO)
3675/*
3676 * Process the new 'linespace' option value.
3677 */
3678 char *
3679did_set_linespace(optset_T *args UNUSED)
3680{
3681 // Recompute gui.char_height and resize the Vim window to keep the
3682 // same number of lines.
3683 if (gui.in_use && gui_mch_adjust_charheight() == OK)
3684 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
3685 return NULL;
3686}
3687#endif
3688
3689/*
3690 * Process the updated 'lisp' option value.
3691 */
3692 char *
3693did_set_lisp(optset_T *args UNUSED)
3694{
3695 // When 'lisp' option changes include/exclude '-' in keyword characters.
3696 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
3697 return NULL;
3698}
3699
3700/*
3701 * Process the new 'maxcombine' option value.
3702 */
3703 char *
3704did_set_maxcombine(optset_T *args UNUSED)
3705{
3706 if (p_mco > MAX_MCO)
3707 p_mco = MAX_MCO;
3708 else if (p_mco < 0)
3709 p_mco = 0;
3710 screenclear(); // will re-allocate the screen
3711 return NULL;
3712}
3713
3714/*
3715 * Process the updated 'modifiable' option value.
3716 */
3717 char *
3718did_set_modifiable(optset_T *args UNUSED)
3719{
3720 // when 'modifiable' is changed, redraw the window title
3721
3722# ifdef FEAT_TERMINAL
3723 // Cannot set 'modifiable' when in Terminal mode.
3724 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3725 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
3726 {
3727 curbuf->b_p_ma = FALSE;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003728 return e_cannot_make_terminal_with_running_job_modifiable;
3729 }
3730# endif
3731 redraw_titles();
3732
3733 return NULL;
3734}
3735
3736/*
3737 * Process the updated 'modified' option value.
3738 */
3739 char *
3740did_set_modified(optset_T *args)
3741{
3742 if (!args->os_newval.boolean)
3743 save_file_ff(curbuf); // Buffer is unchanged
3744 redraw_titles();
zeertzjq5bf6c212024-03-31 18:41:27 +02003745 curbuf->b_modified_was_set = args->os_newval.boolean;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003746 return NULL;
3747}
3748
3749#if defined(FEAT_GUI) || defined(PROTO)
3750/*
3751 * Process the updated 'mousehide' option value.
3752 */
3753 char *
3754did_set_mousehide(optset_T *args UNUSED)
3755{
3756 if (!p_mh)
3757 gui_mch_mousehide(FALSE);
3758 return NULL;
3759}
3760#endif
3761
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003762/*
3763 * Process the updated 'number' or 'relativenumber' option value.
3764 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003765 char *
3766did_set_number_relativenumber(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003767{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003768#if (defined(FEAT_SIGNS) && defined(FEAT_GUI)) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003769 if (gui.in_use
3770 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3771 && curbuf->b_signlist != NULL)
3772 {
3773 // If the 'number' or 'relativenumber' options are modified and
3774 // 'signcolumn' is set to 'number', then clear the screen for a full
3775 // refresh. Otherwise the sign icons are not displayed properly in the
3776 // number column. If the 'number' option is set and only the
3777 // 'relativenumber' option is toggled, then don't refresh the screen
3778 // (optimization).
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003779 if (!(curwin->w_p_nu && ((int *)args->os_varp == &curwin->w_p_rnu)))
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003780 redraw_all_later(UPD_CLEAR);
3781 }
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003782#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003783 return NULL;
3784}
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003785
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003786#if defined(FEAT_LINEBREAK) || defined(PROTO)
3787/*
3788 * Process the new 'numberwidth' option value.
3789 */
3790 char *
3791did_set_numberwidth(optset_T *args UNUSED)
3792{
3793 char *errmsg = NULL;
3794
3795 // 'numberwidth' must be positive
3796 if (curwin->w_p_nuw < 1)
3797 {
3798 errmsg = e_argument_must_be_positive;
3799 curwin->w_p_nuw = 1;
3800 }
3801 if (curwin->w_p_nuw > 20)
3802 {
3803 errmsg = e_invalid_argument;
3804 curwin->w_p_nuw = 20;
3805 }
3806 curwin->w_nrwidth_line_count = 0; // trigger a redraw
3807
3808 return errmsg;
3809}
3810#endif
3811
3812/*
3813 * Process the updated 'paste' option value. Called after p_paste was set or
3814 * reset. When 'paste' is set or reset also change other options.
3815 */
3816 char *
3817did_set_paste(optset_T *args UNUSED)
3818{
3819 static int old_p_paste = FALSE;
3820 static int save_sm = 0;
3821 static int save_sta = 0;
3822 static int save_ru = 0;
3823#ifdef FEAT_RIGHTLEFT
3824 static int save_ri = 0;
3825 static int save_hkmap = 0;
3826#endif
3827 buf_T *buf;
3828
3829 if (p_paste)
3830 {
3831 // Paste switched from off to on.
3832 // Save the current values, so they can be restored later.
3833 if (!old_p_paste)
3834 {
3835 // save options for each buffer
3836 FOR_ALL_BUFFERS(buf)
3837 {
3838 buf->b_p_tw_nopaste = buf->b_p_tw;
3839 buf->b_p_wm_nopaste = buf->b_p_wm;
3840 buf->b_p_sts_nopaste = buf->b_p_sts;
3841 buf->b_p_ai_nopaste = buf->b_p_ai;
3842 buf->b_p_et_nopaste = buf->b_p_et;
3843#ifdef FEAT_VARTABS
3844 if (buf->b_p_vsts_nopaste)
3845 vim_free(buf->b_p_vsts_nopaste);
3846 buf->b_p_vsts_nopaste =
3847 buf->b_p_vsts && buf->b_p_vsts != empty_option
3848 ? vim_strsave(buf->b_p_vsts) : NULL;
3849#endif
3850 }
3851
3852 // save global options
3853 save_sm = p_sm;
3854 save_sta = p_sta;
3855 save_ru = p_ru;
3856#ifdef FEAT_RIGHTLEFT
3857 save_ri = p_ri;
3858 save_hkmap = p_hkmap;
3859#endif
3860 // save global values for local buffer options
3861 p_ai_nopaste = p_ai;
3862 p_et_nopaste = p_et;
3863 p_sts_nopaste = p_sts;
3864 p_tw_nopaste = p_tw;
3865 p_wm_nopaste = p_wm;
3866#ifdef FEAT_VARTABS
3867 if (p_vsts_nopaste)
3868 vim_free(p_vsts_nopaste);
3869 p_vsts_nopaste = p_vsts && p_vsts != empty_option
3870 ? vim_strsave(p_vsts) : NULL;
3871#endif
3872 }
3873
3874 // Always set the option values, also when 'paste' is set when it is
3875 // already on. Set options for each buffer.
3876 FOR_ALL_BUFFERS(buf)
3877 {
3878 buf->b_p_tw = 0; // textwidth is 0
3879 buf->b_p_wm = 0; // wrapmargin is 0
3880 buf->b_p_sts = 0; // softtabstop is 0
3881 buf->b_p_ai = 0; // no auto-indent
3882 buf->b_p_et = 0; // no expandtab
3883#ifdef FEAT_VARTABS
3884 if (buf->b_p_vsts)
3885 free_string_option(buf->b_p_vsts);
3886 buf->b_p_vsts = empty_option;
3887 VIM_CLEAR(buf->b_p_vsts_array);
3888#endif
3889 }
3890
3891 // set global options
3892 p_sm = 0; // no showmatch
3893 p_sta = 0; // no smarttab
3894 if (p_ru)
3895 status_redraw_all(); // redraw to remove the ruler
3896 p_ru = 0; // no ruler
3897#ifdef FEAT_RIGHTLEFT
3898 p_ri = 0; // no reverse insert
3899 p_hkmap = 0; // no Hebrew keyboard
3900#endif
3901 // set global values for local buffer options
3902 p_tw = 0;
3903 p_wm = 0;
3904 p_sts = 0;
3905 p_ai = 0;
Christian Brabandt757593c2023-08-22 21:44:10 +02003906 p_et = 0;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003907#ifdef FEAT_VARTABS
3908 if (p_vsts)
3909 free_string_option(p_vsts);
3910 p_vsts = empty_option;
3911#endif
3912 }
3913
3914 // Paste switched from on to off: Restore saved values.
3915 else if (old_p_paste)
3916 {
3917 // restore options for each buffer
3918 FOR_ALL_BUFFERS(buf)
3919 {
3920 buf->b_p_tw = buf->b_p_tw_nopaste;
3921 buf->b_p_wm = buf->b_p_wm_nopaste;
3922 buf->b_p_sts = buf->b_p_sts_nopaste;
3923 buf->b_p_ai = buf->b_p_ai_nopaste;
3924 buf->b_p_et = buf->b_p_et_nopaste;
3925#ifdef FEAT_VARTABS
3926 if (buf->b_p_vsts)
3927 free_string_option(buf->b_p_vsts);
3928 buf->b_p_vsts = buf->b_p_vsts_nopaste
3929 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
3930 vim_free(buf->b_p_vsts_array);
3931 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
3932 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
3933 else
3934 buf->b_p_vsts_array = NULL;
3935#endif
3936 }
3937
3938 // restore global options
3939 p_sm = save_sm;
3940 p_sta = save_sta;
3941 if (p_ru != save_ru)
3942 status_redraw_all(); // redraw to draw the ruler
3943 p_ru = save_ru;
3944#ifdef FEAT_RIGHTLEFT
3945 p_ri = save_ri;
3946 p_hkmap = save_hkmap;
3947#endif
3948 // set global values for local buffer options
3949 p_ai = p_ai_nopaste;
3950 p_et = p_et_nopaste;
3951 p_sts = p_sts_nopaste;
3952 p_tw = p_tw_nopaste;
3953 p_wm = p_wm_nopaste;
3954#ifdef FEAT_VARTABS
3955 if (p_vsts)
3956 free_string_option(p_vsts);
3957 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
3958#endif
3959 }
3960
3961 old_p_paste = p_paste;
3962
Christian Brabandt757593c2023-08-22 21:44:10 +02003963#if defined(FEAT_EVAL) || defined(PROTO)
3964 // Remember where the dependent options were reset
3965 didset_options_sctx((OPT_LOCAL | OPT_GLOBAL), p_paste_dep_opts);
3966#endif
3967
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003968 return NULL;
3969}
3970
3971
3972#ifdef FEAT_QUICKFIX
3973/*
3974 * Process the updated 'previewwindow' option value.
3975 */
3976 char *
Yegappan Lakshmanan3ee25962023-12-04 20:31:14 +01003977did_set_previewwindow(optset_T *args UNUSED)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003978{
3979 if (!curwin->w_p_pvw)
3980 return NULL;
3981
3982 // There can be only one window with 'previewwindow' set.
3983 win_T *win;
3984
3985 FOR_ALL_WINDOWS(win)
3986 if (win->w_p_pvw && win != curwin)
3987 {
3988 curwin->w_p_pvw = FALSE;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003989 return e_preview_window_already_exists;
3990 }
3991
3992 return NULL;
3993}
3994#endif
3995
3996#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
3997/*
3998 * Process the new 'pyxversion' option value.
3999 */
4000 char *
4001did_set_pyxversion(optset_T *args UNUSED)
4002{
4003 char *errmsg = NULL;
4004
4005 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
4006 errmsg = e_invalid_argument;
4007
4008 return errmsg;
4009}
4010#endif
4011
4012/*
4013 * Process the updated 'readonly' option value.
4014 */
4015 char *
4016did_set_readonly(optset_T *args)
4017{
4018 // when 'readonly' is reset globally, also reset readonlymode
4019 if (!curbuf->b_p_ro && (args->os_flags & OPT_LOCAL) == 0)
4020 readonlymode = FALSE;
4021
4022 // when 'readonly' is set may give W10 again
4023 if (curbuf->b_p_ro)
4024 curbuf->b_did_warn = FALSE;
4025
4026 redraw_titles();
4027
4028 return NULL;
4029}
4030
4031/*
4032 * Process the updated 'scrollbind' option value.
4033 */
4034 char *
4035did_set_scrollbind(optset_T *args UNUSED)
4036{
4037 // when 'scrollbind' is set: snapshot the current position to avoid a jump
4038 // at the end of normal_cmd()
4039 if (!curwin->w_p_scb)
4040 return NULL;
4041
4042 do_check_scrollbind(FALSE);
4043 curwin->w_scbind_pos = curwin->w_topline;
4044 return NULL;
4045}
4046
4047#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
4048/*
4049 * Process the updated 'shellslash' option value.
4050 */
4051 char *
4052did_set_shellslash(optset_T *args UNUSED)
4053{
4054 if (p_ssl)
4055 {
4056 psepc = '/';
4057 psepcN = '\\';
4058 pseps[0] = '/';
4059 }
4060 else
4061 {
4062 psepc = '\\';
4063 psepcN = '/';
4064 pseps[0] = '\\';
4065 }
4066
4067 // need to adjust the file name arguments and buffer names.
4068 buflist_slash_adjust();
4069 alist_slash_adjust();
4070# ifdef FEAT_EVAL
4071 scriptnames_slash_adjust();
4072# endif
4073 return NULL;
4074}
4075#endif
4076
4077/*
4078 * Process the new 'shiftwidth' or the 'tabstop' option value.
4079 */
4080 char *
4081did_set_shiftwidth_tabstop(optset_T *args)
4082{
4083 long *pp = (long *)args->os_varp;
4084 char *errmsg = NULL;
4085
4086 if (curbuf->b_p_sw < 0)
4087 {
4088 errmsg = e_argument_must_be_positive;
4089#ifdef FEAT_VARTABS
4090 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4091 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4092 ? tabstop_first(curbuf->b_p_vts_array)
4093 : curbuf->b_p_ts;
4094#else
4095 curbuf->b_p_sw = curbuf->b_p_ts;
4096#endif
4097 }
4098
4099#ifdef FEAT_FOLDING
4100 if (foldmethodIsIndent(curwin))
4101 foldUpdateAll(curwin);
4102#endif
4103 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
4104 // parse 'cinoptions'.
4105 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
4106 parse_cino(curbuf);
4107
4108 return errmsg;
4109}
4110
4111/*
4112 * Process the new 'showtabline' option value.
4113 */
4114 char *
4115did_set_showtabline(optset_T *args UNUSED)
4116{
4117 shell_new_rows(); // recompute window positions and heights
4118 return NULL;
4119}
4120
4121/*
4122 * Process the updated 'smoothscroll' option value.
4123 */
4124 char *
4125did_set_smoothscroll(optset_T *args UNUSED)
4126{
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004127 if (!curwin->w_p_sms)
4128 curwin->w_skipcol = 0;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004129
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004130 return NULL;
4131}
4132
4133#if defined(FEAT_SPELL) || defined(PROTO)
4134/*
4135 * Process the updated 'spell' option value.
4136 */
4137 char *
4138did_set_spell(optset_T *args UNUSED)
4139{
4140 if (curwin->w_p_spell)
4141 return parse_spelllang(curwin);
4142
4143 return NULL;
4144}
4145#endif
4146
4147/*
4148 * Process the updated 'swapfile' option value.
4149 */
4150 char *
4151did_set_swapfile(optset_T *args UNUSED)
4152{
4153 // when 'swf' is set, create swapfile, when reset remove swapfile
4154 if (curbuf->b_p_swf && p_uc)
4155 ml_open_file(curbuf); // create the swap file
4156 else
4157 // no need to reset curbuf->b_may_swap, ml_open_file() will check
4158 // buf->b_p_swf
4159 mf_close_file(curbuf, TRUE); // remove the swap file
4160 return NULL;
4161}
4162
4163#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004164 char *
4165did_set_termguicolors(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004166{
4167# ifdef FEAT_VTP
4168 // Do not turn on 'tgc' when 24-bit colors are not supported.
4169 if (
4170# ifdef VIMDLL
4171 !gui.in_use && !gui.starting &&
4172# endif
4173 !has_vtp_working())
4174 {
4175 p_tgc = 0;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004176 return e_24_bit_colors_are_not_supported_on_this_environment;
4177 }
4178 if (is_term_win32())
4179 swap_tcap();
4180# endif
4181# ifdef FEAT_GUI
4182 if (!gui.in_use && !gui.starting)
4183# endif
4184 highlight_gui_started();
4185# ifdef FEAT_VTP
4186 // reset t_Co
4187 if (is_term_win32())
4188 {
4189 control_console_color_rgb();
4190 set_termname(T_NAME);
4191 init_highlight(TRUE, FALSE);
4192 }
4193# endif
4194# ifdef FEAT_TERMINAL
4195 term_update_colors_all();
4196 term_update_palette_all();
4197 term_update_wincolor_all();
4198# endif
4199
4200 return NULL;
4201}
4202#endif
4203
4204/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004205 * Process the updated 'terse' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004207 char *
4208did_set_terse(optset_T *args UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004210 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004212 // when 'terse' is set change 'shortmess'
4213 p = vim_strchr(p_shm, SHM_SEARCH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004215 // insert 's' in p_shm
4216 if (p_terse && p == NULL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004217 {
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004218 STRCPY(IObuff, p_shm);
4219 STRCAT(IObuff, "s");
4220 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004221 }
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004222 // remove 's' from p_shm
4223 else if (!p_terse && p != NULL)
4224 STRMOVE(p, p + 1);
4225 return NULL;
4226}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004228/*
4229 * Process the updated 'textauto' option value.
4230 */
4231 char *
4232did_set_textauto(optset_T *args)
4233{
4234 // when 'textauto' is set or reset also change 'fileformats'
4235 set_string_option_direct((char_u *)"ffs", -1,
4236 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
4237 OPT_FREE | args->os_flags, 0);
Bram Moolenaar53744302015-07-17 17:38:22 +02004238
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004239 return NULL;
4240}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004242/*
4243 * Process the updated 'textmode' option value.
4244 */
4245 char *
4246did_set_textmode(optset_T *args)
4247{
4248 // when 'textmode' is set or reset also change 'fileformat'
4249 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, args->os_flags);
4250
4251 return NULL;
4252}
4253
4254/*
4255 * Process the new 'textwidth' option value.
4256 */
4257 char *
4258did_set_textwidth(optset_T *args UNUSED)
4259{
4260 char *errmsg = NULL;
4261
4262 if (curbuf->b_p_tw < 0)
4263 {
4264 errmsg = e_argument_must_be_positive;
4265 curbuf->b_p_tw = 0;
4266 }
4267#ifdef FEAT_SYN_HL
4268 {
4269 win_T *wp;
4270 tabpage_T *tp;
4271
4272 FOR_ALL_TAB_WINDOWS(tp, wp)
4273 check_colorcolumn(wp);
4274 }
Bram Moolenaar53744302015-07-17 17:38:22 +02004275#endif
4276
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004277 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278}
4279
4280/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004281 * Process the updated 'title' or the 'icon' option value.
4282 */
4283 char *
4284did_set_title_icon(optset_T *args UNUSED)
4285{
4286 // when 'title' changed, may need to change the title; same for 'icon'
4287 did_set_title();
4288 return NULL;
4289}
4290
4291/*
4292 * Process the new 'titlelen' option value.
4293 */
4294 char *
4295did_set_titlelen(optset_T *args)
4296{
4297 long old_value = args->os_oldval.number;
4298 char *errmsg = NULL;
4299
4300 // if 'titlelen' has changed, redraw the title
4301 if (p_titlelen < 0)
4302 {
4303 errmsg = e_argument_must_be_positive;
4304 p_titlelen = 85;
4305 }
4306 if (starting != NO_SCREEN && old_value != p_titlelen)
4307 need_maketitle = TRUE;
4308
4309 return errmsg;
4310}
4311
4312#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
4313/*
4314 * Process the updated 'undofile' option value.
4315 */
4316 char *
4317did_set_undofile(optset_T *args)
4318{
4319 // Only take action when the option was set.
4320 if (!curbuf->b_p_udf && !p_udf)
4321 return NULL;
4322
4323 // When reset we do not delete the undo file, the option may be set again
4324 // without making any changes in between.
4325 char_u hash[UNDO_HASH_SIZE];
4326 buf_T *save_curbuf = curbuf;
4327
4328 FOR_ALL_BUFFERS(curbuf)
4329 {
4330 // When 'undofile' is set globally: for every buffer, otherwise
4331 // only for the current buffer: Try to read in the undofile,
4332 // if one exists, the buffer wasn't changed and the buffer was
4333 // loaded
4334 if ((curbuf == save_curbuf
4335 || (args->os_flags & OPT_GLOBAL)
4336 || args->os_flags == 0)
4337 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
4338 {
4339#ifdef FEAT_CRYPT
Christian Brabandtaae58342023-04-23 17:50:22 +01004340 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004341 continue;
4342#endif
4343 u_compute_hash(hash);
4344 u_read_undo(NULL, hash, curbuf->b_fname);
4345 }
4346 }
4347 curbuf = save_curbuf;
4348
4349 return NULL;
4350}
4351#endif
4352
4353/*
4354 * Process the new global 'undolevels' option value.
4355 */
4356 static void
4357update_global_undolevels(long value, long old_value)
4358{
4359 // sync undo before 'undolevels' changes
4360
4361 // use the old value, otherwise u_sync() may not work properly
4362 p_ul = old_value;
4363 u_sync(TRUE);
4364 p_ul = value;
4365}
4366
4367/*
4368 * Process the new buffer local 'undolevels' option value.
4369 */
4370 static void
4371update_buflocal_undolevels(long value, long old_value)
4372{
4373 // use the old value, otherwise u_sync() may not work properly
4374 curbuf->b_p_ul = old_value;
4375 u_sync(TRUE);
4376 curbuf->b_p_ul = value;
4377}
4378
4379/*
4380 * Process the new 'undolevels' option value.
4381 */
4382 char *
4383did_set_undolevels(optset_T *args)
4384{
4385 long *pp = (long *)args->os_varp;
4386
4387 if (pp == &p_ul) // global 'undolevels'
4388 update_global_undolevels(args->os_newval.number,
4389 args->os_oldval.number);
4390 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4391 update_buflocal_undolevels(args->os_newval.number,
4392 args->os_oldval.number);
4393
4394 return NULL;
4395}
4396
4397/*
4398 * Process the new 'updatecount' option value.
4399 */
4400 char *
4401did_set_updatecount(optset_T *args)
4402{
4403 long old_value = args->os_oldval.number;
4404 char *errmsg = NULL;
4405
4406 // when 'updatecount' changes from zero to non-zero, open swap files
4407 if (p_uc < 0)
4408 {
4409 errmsg = e_argument_must_be_positive;
4410 p_uc = 100;
4411 }
4412 if (p_uc && !old_value)
4413 ml_open_files();
4414
4415 return errmsg;
4416}
4417
4418/*
4419 * Process the updated 'weirdinvert' option value.
4420 */
4421 char *
4422did_set_weirdinvert(optset_T *args)
4423{
4424 // When 'weirdinvert' changed, set/reset 't_xs'.
4425 // Then set 'weirdinvert' according to value of 't_xs'.
4426 if (p_wiv && !args->os_oldval.boolean)
4427 T_XS = (char_u *)"y";
4428 else if (!p_wiv && args->os_oldval.boolean)
4429 T_XS = empty_option;
4430 p_wiv = (*T_XS != NUL);
4431
4432 return NULL;
4433}
4434
4435/*
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +02004436 * Process the new 'wildchar' / 'wildcharm' option value.
4437 */
4438 char *
4439did_set_wildchar(optset_T *args)
4440{
4441 long c = *(long *)args->os_varp;
4442
4443 // Don't allow key values that wouldn't work as wildchar.
4444 if (c == Ctrl_C || c == '\n' || c == '\r' || c == K_KENTER)
4445 return e_invalid_argument;
4446
4447 return NULL;
4448}
4449
4450/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004451 * Process the new 'window' option value.
4452 */
4453 char *
4454did_set_window(optset_T *args UNUSED)
4455{
4456 if (p_window < 1)
4457 p_window = 1;
4458 else if (p_window >= Rows)
4459 p_window = Rows - 1;
4460 return NULL;
4461}
4462
4463/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004464 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004466 char *
4467did_set_winheight_helpheight(optset_T *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004469 long *pp = (long *)args->os_varp;
4470 char *errmsg = NULL;
4471
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004472 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004474 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004475 p_wh = 1;
4476 }
4477 if (p_wmh > p_wh)
4478 {
4479 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4480 p_wh = p_wmh;
4481 }
4482 if (p_hh < 0)
4483 {
4484 errmsg = e_argument_must_be_positive;
4485 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 }
4487
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004488 // Change window height NOW
4489 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004491 if (pp == &p_wh && curwin->w_height < p_wh)
4492 win_setheight((int)p_wh);
4493 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
4494 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004497 return errmsg;
4498}
4499
4500/*
4501 * Process the new 'winminheight' option value.
4502 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004503 char *
4504did_set_winminheight(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004505{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004506 char *errmsg = NULL;
4507
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004508 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004510 errmsg = e_argument_must_be_positive;
4511 p_wmh = 0;
4512 }
4513 if (p_wmh > p_wh)
4514 {
4515 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4516 p_wmh = p_wh;
4517 }
4518 win_setminheight();
4519
4520 return errmsg;
4521}
4522
4523/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004524 * Process the new 'winminwidth' option value.
4525 */
4526 char *
4527did_set_winminwidth(optset_T *args UNUSED)
4528{
4529 char *errmsg = NULL;
4530
4531 if (p_wmw < 0)
4532 {
4533 errmsg = e_argument_must_be_positive;
4534 p_wmw = 0;
4535 }
4536 if (p_wmw > p_wiw)
4537 {
4538 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4539 p_wmw = p_wiw;
4540 }
4541 win_setminwidth();
4542
4543 return errmsg;
4544}
4545
4546/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004547 * Process the new 'winwidth' option value.
4548 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004549 char *
4550did_set_winwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004551{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004552 char *errmsg = NULL;
4553
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004554 if (p_wiw < 1)
4555 {
4556 errmsg = e_argument_must_be_positive;
4557 p_wiw = 1;
4558 }
4559 if (p_wmw > p_wiw)
4560 {
4561 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4562 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004564
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004565 // Change window width NOW
4566 if (!ONE_WINDOW && curwin->w_width < p_wiw)
4567 win_setwidth((int)p_wiw);
4568
4569 return errmsg;
4570}
4571
4572/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004573 * Process the updated 'wrap' option value.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004574 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004575 char *
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004576did_set_wrap(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004577{
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004578 // Set w_leftcol or w_skipcol to zero.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004579 if (curwin->w_p_wrap)
4580 curwin->w_leftcol = 0;
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004581 else
4582 curwin->w_skipcol = 0;
4583
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004584 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004585}
4586
4587/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004588 * Set the value of a boolean option, and take care of side effects.
4589 * Returns NULL for success, or an error message for an error.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004590 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004591 static char *
4592set_bool_option(
4593 int opt_idx, // index in options[] table
4594 char_u *varp, // pointer to the option variable
4595 int value, // new value
4596 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004597{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004598 int old_value = *(int *)varp;
4599#if defined(FEAT_EVAL)
4600 int old_global_value = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004602 char *errmsg = NULL;
4603
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004604 // Disallow changing some options from secure mode
4605 if ((secure
4606#ifdef HAVE_SANDBOX
4607 || sandbox != 0
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004608#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004609 ) && (options[opt_idx].flags & P_SECURE))
4610 return e_not_allowed_here;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004611
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004612#if defined(FEAT_EVAL)
4613 // Save the global value before changing anything. This is needed as for
4614 // a global-only option setting the "local value" in fact sets the global
4615 // value (since there is only one value).
4616 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4617 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
4618 OPT_GLOBAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004620
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004621 *(int *)varp = value; // set the new value
4622#ifdef FEAT_EVAL
4623 // Remember where the option was set.
4624 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004625#endif
4626
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004628 need_mouse_correct = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004631 // May set global value for local option.
4632 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4633 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004634
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004635 // Handle side effects of changing a bool option.
4636 if (options[opt_idx].opt_did_set_cb != NULL)
4637 {
4638 optset_T args;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004639
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004640 CLEAR_FIELD(args);
4641 args.os_varp = varp;
4642 args.os_flags = opt_flags;
4643 args.os_oldval.boolean = old_value;
4644 args.os_newval.boolean = value;
4645 args.os_errbuf = NULL;
4646 errmsg = options[opt_idx].opt_did_set_cb(&args);
zeertzjqdeba02d2023-11-02 21:01:19 +01004647 if (errmsg != NULL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004648 return errmsg;
4649 }
4650
4651 // after handling side effects, call autocommand
4652
4653 options[opt_idx].flags |= P_WAS_SET;
4654
4655#if defined(FEAT_EVAL)
4656 apply_optionset_autocmd(opt_idx, opt_flags,
4657 (long)(old_value ? TRUE : FALSE),
4658 (long)(old_global_value ? TRUE : FALSE),
4659 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004660#endif
4661
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004662 comp_col(); // in case 'ruler' or 'showcmd' changed
zeertzjqfcaed6a2024-02-18 09:33:54 +01004663
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004664 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004665 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0
4666 && (options[opt_idx].flags & P_HLONLY) == 0)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004667 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004668
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004669 if ((opt_flags & OPT_NO_REDRAW) == 0)
4670 check_redraw(options[opt_idx].flags);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004671
4672 return errmsg;
4673}
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004674
4675/*
4676 * Check the bounds of numeric options.
4677 */
4678 static char *
4679check_num_option_bounds(
4680 long *pp,
4681 long old_value,
4682 long old_Rows,
4683 long old_Columns,
4684 char *errbuf,
4685 size_t errbuflen,
4686 char *errmsg)
4687{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 if (Rows < min_rows() && full_screen)
4689 {
4690 if (errbuf != NULL)
4691 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004692 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004693 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 errmsg = errbuf;
4695 }
4696 Rows = min_rows();
4697 }
4698 if (Columns < MIN_COLUMNS && full_screen)
4699 {
4700 if (errbuf != NULL)
4701 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004702 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004703 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 errmsg = errbuf;
4705 }
4706 Columns = MIN_COLUMNS;
4707 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004708 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004710 // If the screen (shell) height has been changed, assume it is the
4711 // physical screenheight.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 if (old_Rows != Rows || old_Columns != Columns)
4713 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004714 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 if (updating_screen)
4716 *pp = old_value;
4717 else if (full_screen
4718#ifdef FEAT_GUI
4719 && !gui.starting
4720#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004721 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 set_shellsize((int)Columns, (int)Rows, TRUE);
4723 else
4724 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004725 // Postpone the resizing; check the size and cmdline position for
4726 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 check_shellsize();
4728 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4729 cmdline_row = Rows - p_ch;
4730 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004731 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004732 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 }
4734
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 if (curbuf->b_p_ts <= 0)
4736 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004737 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 curbuf->b_p_ts = 8;
4739 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004740 else if (curbuf->b_p_ts > TABSTOP_MAX)
4741 {
4742 errmsg = e_invalid_argument;
4743 curbuf->b_p_ts = 8;
4744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 if (p_tm < 0)
4746 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004747 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 p_tm = 0;
4749 }
4750 if ((curwin->w_p_scr <= 0
4751 || (curwin->w_p_scr > curwin->w_height
4752 && curwin->w_height > 0))
4753 && full_screen)
4754 {
4755 if (pp == &(curwin->w_p_scr))
4756 {
4757 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004758 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 win_comp_scroll(curwin);
4760 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004761 // If 'scroll' became invalid because of a side effect silently adjust
4762 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 else if (curwin->w_p_scr <= 0)
4764 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004765 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 curwin->w_p_scr = curwin->w_height;
4767 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004768 if (p_hi < 0)
4769 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004770 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004771 p_hi = 0;
4772 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004773 else if (p_hi > 10000)
4774 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004775 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004776 p_hi = 10000;
4777 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004778 if (p_re < 0 || p_re > 2)
4779 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004780 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004781 p_re = 0;
4782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 if (p_report < 0)
4784 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004785 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 p_report = 1;
4787 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004788 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004790 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 p_sj = Rows / 2;
4792 else
4793 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004794 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 p_sj = 1;
4796 }
4797 }
4798 if (p_so < 0 && full_screen)
4799 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004800 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 p_so = 0;
4802 }
4803 if (p_siso < 0 && full_screen)
4804 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004805 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 p_siso = 0;
4807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 if (p_cwh < 1)
4809 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004810 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 p_cwh = 1;
4812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 if (p_ut < 0)
4814 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004815 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 p_ut = 2000;
4817 }
4818 if (p_ss < 0)
4819 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004820 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 p_ss = 0;
4822 }
4823
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004824 return errmsg;
4825}
4826
4827/*
4828 * Set the value of a number option, and take care of side effects.
4829 * Returns NULL for success, or an error message for an error.
4830 */
4831 static char *
4832set_num_option(
4833 int opt_idx, // index in options[] table
4834 char_u *varp, // pointer to the option variable
4835 long value, // new value
4836 char *errbuf, // buffer for error messages
4837 size_t errbuflen, // length of "errbuf"
4838 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4839 // OPT_MODELINE, etc.
4840{
4841 char *errmsg = NULL;
4842 long old_value = *(long *)varp;
4843#if defined(FEAT_EVAL)
4844 long old_global_value = 0; // only used when setting a local and
4845 // global option
4846#endif
4847 long old_Rows = Rows; // remember old Rows
4848 long old_Columns = Columns; // remember old Columns
4849 long *pp = (long *)varp;
4850
4851 // Disallow changing some options from secure mode.
4852 if ((secure
4853#ifdef HAVE_SANDBOX
4854 || sandbox != 0
4855#endif
4856 ) && (options[opt_idx].flags & P_SECURE))
4857 return e_not_allowed_here;
4858
4859#if defined(FEAT_EVAL)
4860 // Save the global value before changing anything. This is needed as for
4861 // a global-only option setting the "local value" in fact sets the global
4862 // value (since there is only one value).
4863 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4864 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4865 OPT_GLOBAL);
4866#endif
4867
4868 *pp = value;
4869#ifdef FEAT_EVAL
4870 // Remember where the option was set.
4871 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4872#endif
4873#ifdef FEAT_GUI
4874 need_mouse_correct = TRUE;
4875#endif
4876
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004877 // Invoke the option specific callback function to validate and apply the
4878 // new value.
4879 if (options[opt_idx].opt_did_set_cb != NULL)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004880 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004881 optset_T args;
4882
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004883 CLEAR_FIELD(args);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004884 args.os_varp = varp;
4885 args.os_flags = opt_flags;
4886 args.os_oldval.number = old_value;
4887 args.os_newval.number = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004888 args.os_errbuf = NULL;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004889 errmsg = options[opt_idx].opt_did_set_cb(&args);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004890 }
4891
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004892 // Check the bounds for numeric options here
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004893 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4894 errbuf, errbuflen, errmsg);
4895
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004896 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4898 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4899
4900 options[opt_idx].flags |= P_WAS_SET;
4901
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004902#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004903 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4904 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004905#endif
4906
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004907 comp_col(); // in case 'columns' or 'ls' changed
zeertzjqfcaed6a2024-02-18 09:33:54 +01004908
Bram Moolenaar913077c2012-03-28 19:59:04 +02004909 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004910 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0
4911 && (options[opt_idx].flags & P_HLONLY) == 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004912 curwin->w_set_curswant = TRUE;
zeertzjqfcaed6a2024-02-18 09:33:54 +01004913
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004914 if ((opt_flags & OPT_NO_REDRAW) == 0)
4915 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916
4917 return errmsg;
4918}
4919
4920/*
4921 * Called after an option changed: check if something needs to be redrawn.
4922 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004923 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004924check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004926 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004927 int doclear = (flags & P_RCLR) == P_RCLR;
4928 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004930 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932
4933 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
zeertzjqfcaed6a2024-02-18 09:33:54 +01004934 {
4935 if (flags & P_HLONLY)
4936 redraw_later(UPD_NOT_VALID);
4937 else
4938 changed_window_setting();
4939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004941 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004942 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004943 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004945 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946}
4947
4948/*
4949 * Find index for option 'arg'.
4950 * Return -1 if not found.
4951 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004952 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004953findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954{
4955 int opt_idx;
4956 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004957 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 int is_term_opt;
4959
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004960 // For first call: Initialize the quick-access table.
4961 // It contains the index for the first option that starts with a certain
4962 // letter. There are 26 letters, plus the first "t_" option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 if (quick_tab[1] == 0)
4964 {
4965 p = options[0].fullname;
4966 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4967 {
4968 if (s[0] != p[0])
4969 {
4970 if (s[0] == 't' && s[1] == '_')
4971 quick_tab[26] = opt_idx;
4972 else
4973 quick_tab[CharOrdLow(s[0])] = opt_idx;
4974 }
4975 p = s;
4976 }
4977 }
4978
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004979 // Check for name starting with an illegal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 return -1;
4982
4983 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4984 if (is_term_opt)
4985 opt_idx = quick_tab[26];
4986 else
4987 opt_idx = quick_tab[CharOrdLow(arg[0])];
zeertzjqf48558e2023-12-08 21:34:31 +01004988 for (; (s = options[opt_idx].fullname) != NULL && s[0] == arg[0]; opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004990 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 break;
4992 }
zeertzjqf48558e2023-12-08 21:34:31 +01004993 if (s != NULL && s[0] != arg[0])
4994 s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 if (s == NULL && !is_term_opt)
4996 {
4997 opt_idx = quick_tab[CharOrdLow(arg[0])];
4998 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
4999 {
5000 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005001 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 break;
5003 s = NULL;
5004 }
5005 }
5006 if (s == NULL)
5007 opt_idx = -1;
5008 return opt_idx;
5009}
5010
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005011#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
5012 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013/*
5014 * Get the value for an option.
5015 *
5016 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005017 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01005018 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005019 * String option: gov_string, *stringval gets allocated string.
5020 * Hidden Number option: gov_hidden_number.
5021 * Hidden Toggle option: gov_hidden_bool.
5022 * Hidden String option: gov_hidden_string.
5023 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005024 *
5025 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005027 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01005028get_option_value(
5029 char_u *name,
5030 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005031 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005032 int *flagsp,
5033 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034{
5035 int opt_idx;
5036 char_u *varp;
5037
5038 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005039 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01005040 {
5041 int key;
5042
5043 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005044 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005045 {
5046 char_u key_name[2];
5047 char_u *p;
5048
Bram Moolenaar10c75c42021-12-28 20:53:30 +00005049 if (flagsp != NULL)
5050 *flagsp = 0; // terminal option has no flags
5051
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005052 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01005053 if (key < 0)
5054 {
5055 key_name[0] = KEY2TERMCAP0(key);
5056 key_name[1] = KEY2TERMCAP1(key);
5057 }
5058 else
5059 {
5060 key_name[0] = KS_KEY;
5061 key_name[1] = (key & 0xff);
5062 }
5063 p = find_termcode(key_name);
5064 if (p != NULL)
5065 {
5066 if (stringval != NULL)
5067 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005068 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01005069 }
5070 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005071 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01005072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005074 varp = get_varp_scope(&(options[opt_idx]), scope);
5075
5076 if (flagsp != NULL)
5077 // Return the P_xxxx option flags.
5078 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079
5080 if (options[opt_idx].flags & P_STRING)
5081 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005082 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005083 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 if (stringval != NULL)
5085 {
zeertzjq51f0bc32022-05-09 13:33:39 +01005086 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01005087 *stringval = str2special_save(*(char_u **)(varp), FALSE,
5088 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005090 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01005091 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00005092 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01005095 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 *stringval = vim_strsave(*(char_u **)(varp));
5097 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005098 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 }
5100
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005101 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005102 return (options[opt_idx].flags & P_NUM)
5103 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 if (options[opt_idx].flags & P_NUM)
5105 *numval = *(long *)varp;
5106 else
5107 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005108 // Special case: 'modified' is b_changed, but we also want to consider
5109 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 if ((int *)varp == &curbuf->b_changed)
5111 *numval = curbufIsChanged();
5112 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02005113 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005115 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116}
5117#endif
5118
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005119#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005120/*
5121 * Returns the option attributes and its value. Unlike the above function it
5122 * will return either global value or local value of the option depending on
5123 * what was requested, but it will never return global value if it was
5124 * requested to return local one and vice versa. Neither it will return
5125 * buffer-local value if it was requested to return window-local one.
5126 *
5127 * Pretends that option is absent if it is not present in the requested scope
5128 * (i.e. has no global, window-local or buffer-local value depending on
5129 * opt_type). Uses
5130 *
5131 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02005132 * 0 hidden or unknown option, also option that does not have requested
5133 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005134 * see SOPT_* in vim.h for other flags
5135 *
5136 * Possible opt_type values: see SREQ_* in vim.h
5137 */
5138 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005139get_option_value_strict(
5140 char_u *name,
5141 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005142 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01005143 int opt_type,
5144 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005145{
5146 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02005147 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005148 struct vimoption *p;
5149 int r = 0;
5150
5151 opt_idx = findoption(name);
5152 if (opt_idx < 0)
5153 return 0;
5154
5155 p = &(options[opt_idx]);
5156
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005157 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005158 if (p->var == NULL)
5159 return 0;
5160
5161 if (p->flags & P_BOOL)
5162 r |= SOPT_BOOL;
5163 else if (p->flags & P_NUM)
5164 r |= SOPT_NUM;
5165 else if (p->flags & P_STRING)
5166 r |= SOPT_STRING;
5167
5168 if (p->indir == PV_NONE)
5169 {
5170 if (opt_type == SREQ_GLOBAL)
5171 r |= SOPT_GLOBAL;
5172 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005173 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005174 }
5175 else
5176 {
5177 if (p->indir & PV_BOTH)
5178 r |= SOPT_GLOBAL;
5179 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005180 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005181
5182 if (p->indir & PV_WIN)
5183 {
5184 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005185 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005186 else
5187 r |= SOPT_WIN;
5188 }
5189 else if (p->indir & PV_BUF)
5190 {
5191 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005192 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005193 else
5194 r |= SOPT_BUF;
5195 }
5196 }
5197
5198 if (stringval == NULL)
5199 return r;
5200
5201 if (opt_type == SREQ_GLOBAL)
5202 varp = p->var;
5203 else
5204 {
5205 if (opt_type == SREQ_BUF)
5206 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005207 // Special case: 'modified' is b_changed, but we also want to
5208 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005209 if (p->indir == PV_MOD)
5210 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005211 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005212 varp = NULL;
5213 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005214# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005215 else if (p->indir == PV_KEY)
5216 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005217 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005218 *stringval = NULL;
5219 varp = NULL;
5220 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005221# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005222 else
5223 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005224 buf_T *save_curbuf = curbuf;
5225
5226 // only getting a pointer, no need to use aucmd_prepbuf()
5227 curbuf = (buf_T *)from;
5228 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005229 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02005230 curbuf = save_curbuf;
5231 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005232 }
5233 }
5234 else if (opt_type == SREQ_WIN)
5235 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005236 win_T *save_curwin = curwin;
5237
5238 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005239 curbuf = curwin->w_buffer;
5240 varp = get_varp(p);
5241 curwin = save_curwin;
5242 curbuf = curwin->w_buffer;
5243 }
5244 if (varp == p->var)
5245 return (r | SOPT_UNSET);
5246 }
5247
5248 if (varp != NULL)
5249 {
5250 if (p->flags & P_STRING)
5251 *stringval = vim_strsave(*(char_u **)(varp));
5252 else if (p->flags & P_NUM)
5253 *numval = *(long *) varp;
5254 else
5255 *numval = *(int *)varp;
5256 }
5257
5258 return r;
5259}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005260
5261/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005262 * Iterate over options. First argument is a pointer to a pointer to a
5263 * structure inside options[] array, second is option type like in the above
5264 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005265 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005266 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005267 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005268 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005269 * first argument to NULL.
5270 *
5271 * Returns full option name for current option on each call.
5272 */
5273 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005274option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005275{
5276 struct vimoption *ret = NULL;
5277 do
5278 {
5279 if (*option == NULL)
5280 *option = (void *) options;
5281 else if (((struct vimoption *) (*option))->fullname == NULL)
5282 {
5283 *option = NULL;
5284 return NULL;
5285 }
5286 else
5287 *option = (void *) (((struct vimoption *) (*option)) + 1);
5288
5289 ret = ((struct vimoption *) (*option));
5290
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005291 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005292 if (ret->var == NULL)
5293 {
5294 ret = NULL;
5295 continue;
5296 }
5297
5298 switch (opt_type)
5299 {
5300 case SREQ_GLOBAL:
5301 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
5302 ret = NULL;
5303 break;
5304 case SREQ_BUF:
5305 if (!(ret->indir & PV_BUF))
5306 ret = NULL;
5307 break;
5308 case SREQ_WIN:
5309 if (!(ret->indir & PV_WIN))
5310 ret = NULL;
5311 break;
5312 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005313 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005314 return NULL;
5315 }
5316 }
5317 while (ret == NULL);
5318
5319 return (char_u *)ret->fullname;
5320}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005321#endif
5322
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005324 * Return the flags for the option at 'opt_idx'.
5325 */
5326 long_u
5327get_option_flags(int opt_idx)
5328{
5329 return options[opt_idx].flags;
5330}
5331
5332/*
5333 * Set a flag for the option at 'opt_idx'.
5334 */
5335 void
5336set_option_flag(int opt_idx, long_u flag)
5337{
5338 options[opt_idx].flags |= flag;
5339}
5340
5341/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005342 * Returns TRUE if the option at 'opt_idx' is a global option
5343 */
5344 int
5345is_global_option(int opt_idx)
5346{
5347 return options[opt_idx].indir == PV_NONE;
5348}
5349
5350/*
5351 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5352 * local value.
5353 */
5354 int
5355is_global_local_option(int opt_idx)
5356{
5357 return options[opt_idx].indir & PV_BOTH;
5358}
5359
5360/*
5361 * Returns TRUE if the option at 'opt_idx' is a window-local option
5362 */
5363 int
5364is_window_local_option(int opt_idx)
5365{
5366 return options[opt_idx].var == VAR_WIN;
5367}
5368
5369/*
5370 * Returns TRUE if the option at 'opt_idx' is a hidden option
5371 */
5372 int
5373is_hidden_option(int opt_idx)
5374{
5375 return options[opt_idx].var == NULL;
5376}
5377
5378#if defined(FEAT_CRYPT) || defined(PROTO)
5379/*
5380 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5381 */
5382 int
5383is_crypt_key_option(int opt_idx)
5384{
5385 return options[opt_idx].indir == PV_KEY;
5386}
5387#endif
5388
5389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 * Set the value of option "name".
5391 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005392 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005393 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005395 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005396set_option_value(
5397 char_u *name,
5398 long number,
5399 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005400 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401{
5402 int opt_idx;
5403 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005404 long_u flags;
Christian Brabandtb39b2402023-11-29 11:34:05 +01005405 static char errbuf[ERR_BUFLEN];
5406 int errbuflen = ERR_BUFLEN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407
5408 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005409 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005410 {
5411 int key;
5412
5413 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005414 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005415 {
5416 char_u key_name[2];
5417
5418 if (key < 0)
5419 {
5420 key_name[0] = KEY2TERMCAP0(key);
5421 key_name[1] = KEY2TERMCAP1(key);
5422 }
5423 else
5424 {
5425 key_name[0] = KS_KEY;
5426 key_name[1] = (key & 0xff);
5427 }
5428 add_termcode(key_name, string, FALSE);
5429 if (full_screen)
5430 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005431 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005432 return NULL;
5433 }
5434
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005435 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 else
5438 {
5439 flags = options[opt_idx].flags;
5440#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005441 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005443 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005444 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005445 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005448 if (flags & P_STRING)
Christian Brabandtb39b2402023-11-29 11:34:05 +01005449 return set_string_option(opt_idx, string, opt_flags, errbuf, errbuflen);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005450
5451 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5452 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005454 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005456 int idx;
5457
5458 // Either we are given a string or we are setting option
5459 // to zero.
5460 for (idx = 0; string[idx] == '0'; ++idx)
5461 ;
5462 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005463 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005464 // There's another character after zeros or the string
5465 // is empty. In both cases, we are trying to set a
5466 // num option using a string.
5467 semsg(_(e_number_required_after_str_equal_str),
5468 name, string);
5469 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005470
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005473 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005474 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005475 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005476 errbuf, sizeof(errbuf), opt_flags);
5477 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005478 else
5479 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005481
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005483 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484}
5485
5486/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005487 * Call set_option_value() and when an error is returned report it.
5488 */
5489 void
5490set_option_value_give_err(
5491 char_u *name,
5492 long number,
5493 char_u *string,
5494 int opt_flags) // OPT_LOCAL or 0 (both)
5495{
5496 char *errmsg = set_option_value(name, number, string, opt_flags);
5497
5498 if (errmsg != NULL)
5499 emsg(_(errmsg));
5500}
5501
5502/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 * Get the terminal code for a terminal option.
5504 * Returns NULL when not found.
5505 */
5506 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005507get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508{
5509 int opt_idx;
5510 char_u *varp;
5511
5512 if (tname[0] != 't' || tname[1] != '_' ||
5513 tname[2] == NUL || tname[3] == NUL)
5514 return NULL;
5515 if ((opt_idx = findoption(tname)) >= 0)
5516 {
5517 varp = get_varp(&(options[opt_idx]));
5518 if (varp != NULL)
5519 varp = *(char_u **)(varp);
5520 return varp;
5521 }
5522 return find_termcode(tname + 2);
5523}
5524
5525 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005526get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527{
5528 int i;
5529
5530 i = findoption((char_u *)"hl");
5531 if (i >= 0)
5532 return options[i].def_val[VI_DEFAULT];
5533 return (char_u *)NULL;
5534}
5535
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005536 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005537get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005538{
5539 int i;
5540
5541 i = findoption((char_u *)"enc");
5542 if (i >= 0)
5543 return options[i].def_val[VI_DEFAULT];
5544 return (char_u *)NULL;
5545}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005546
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005547#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005548 int
5549is_option_allocated(char *name)
5550{
5551 int idx = findoption((char_u *)name);
5552
5553 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5554}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005555#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005556
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557/*
5558 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005559 * When "has_lt" is true there is a '<' before "*arg_arg".
5560 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 */
5562 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005563find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005565 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005567 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005569 // Don't use get_special_key_code() for t_xx, we don't want it to call
5570 // add_termcap_entry().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5572 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005573 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005575 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005577 key = find_special_key(&arg, &modifiers,
5578 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005579 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 key = 0;
5581 }
5582 return key;
5583}
5584
5585/*
5586 * if 'all' == 0: show changed options
5587 * if 'all' == 1: show all normal options
5588 * if 'all' == 2: show all terminal options
5589 */
5590 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005591showoptions(
5592 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005593 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594{
5595 struct vimoption *p;
5596 int col;
5597 int isterm;
5598 char_u *varp;
5599 struct vimoption **items;
5600 int item_count;
5601 int run;
5602 int row, rows;
5603 int cols;
5604 int i;
5605 int len;
5606
5607#define INC 20
5608#define GAP 3
5609
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005610 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 if (items == NULL)
5612 return;
5613
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005614 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005616 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005618 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005620 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005622 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005624 // Do the loop two times:
5625 // 1. display the short items
5626 // 2. display the long items (only strings and numbers)
5627 // When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 for (run = 1; run <= 2 && !got_int; ++run)
5629 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005630 // collect the items in items[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 item_count = 0;
5632 for (p = &options[0]; p->fullname != NULL; p++)
5633 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005634 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005635 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005636 continue;
5637
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 varp = NULL;
5639 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005640 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 {
5642 if (p->indir != PV_NONE && !isterm)
5643 varp = get_varp_scope(p, opt_flags);
5644 }
5645 else
5646 varp = get_varp(p);
5647 if (varp != NULL
5648 && ((all == 2 && isterm)
5649 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005650 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005652 if (opt_flags & OPT_ONECOLUMN)
5653 len = Columns;
5654 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005655 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 else
5657 {
5658 option_value2string(p, opt_flags);
5659 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5660 }
5661 if ((len <= INC - GAP && run == 1) ||
5662 (len > INC - GAP && run == 2))
5663 items[item_count++] = p;
5664 }
5665 }
5666
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005667 // display the items
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 if (run == 1)
5669 {
5670 cols = (Columns + GAP - 3) / INC;
5671 if (cols == 0)
5672 cols = 1;
5673 rows = (item_count + cols - 1) / cols;
5674 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005675 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 rows = item_count;
5677 for (row = 0; row < rows && !got_int; ++row)
5678 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005679 msg_putchar('\n'); // go to next line
5680 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 break;
5682 col = 0;
5683 for (i = row; i < item_count; i += rows)
5684 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005685 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 showoneopt(items[i], opt_flags);
5687 col += INC;
5688 }
5689 out_flush();
5690 ui_breakcheck();
5691 }
5692 }
5693 vim_free(items);
5694}
5695
5696/*
5697 * Return TRUE if option "p" has its default value.
5698 */
5699 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005700optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701{
5702 int dvi;
5703
5704 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005705 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005706 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005708 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005710 // the cast to long is required for Manx C, long_i is
5711 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005712 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005713 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5715}
5716
5717/*
5718 * showoneopt: show the value of one option
5719 * must not be called with a hidden option!
5720 */
5721 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005722showoneopt(
5723 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005724 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005726 char_u *varp;
5727 int save_silent = silent_mode;
5728
5729 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005730 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
5732 varp = get_varp_scope(p, opt_flags);
5733
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005734 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5736 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005737 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005739 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005741 msg_puts(" ");
5742 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 if (!(p->flags & P_BOOL))
5744 {
5745 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005746 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 option_value2string(p, opt_flags);
5748 msg_outtrans(NameBuff);
5749 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005750
5751 silent_mode = save_silent;
5752 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753}
5754
5755/*
5756 * Write modified options as ":set" commands to a file.
5757 *
5758 * There are three values for "opt_flags":
5759 * OPT_GLOBAL: Write global option values and fresh values of
5760 * buffer-local options (used for start of a session
5761 * file).
5762 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5763 * curwin (used for a vimrc file).
5764 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5765 * and local values for window-local options of
5766 * curwin. Local values are also written when at the
5767 * default value, because a modeline or autocommand
5768 * may have set them when doing ":edit file" and the
5769 * user has set them back at the default or fresh
5770 * value.
5771 * When "local_only" is TRUE, don't write fresh
5772 * values, only local values (for ":mkview").
5773 * (fresh value = value used for a new buffer or window for a local option).
5774 *
5775 * Return FAIL on error, OK otherwise.
5776 */
5777 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005778makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779{
5780 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005781 char_u *varp; // currently used value
5782 char_u *varp_fresh; // local value
5783 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 char *cmd;
5785 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005786 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787
5788 /*
5789 * The options that don't have a default (terminal name, columns, lines)
5790 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005791 * Do the loop over "options[]" twice: once for options with the
5792 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005794 for (pri = 1; pri >= 0; --pri)
5795 {
5796 for (p = &options[0]; !istermoption(p); p++)
5797 if (!(p->flags & P_NO_MKRC)
5798 && !istermoption(p)
5799 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005801 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5803 continue;
5804
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005805 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5806 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5808 continue;
5809
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005810 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005812 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 continue;
5814
Bram Moolenaard23b7142021-04-17 21:04:34 +02005815 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5816 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005817 continue;
5818
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 round = 2;
5820 if (p->indir != PV_NONE)
5821 {
5822 if (p->var == VAR_WIN)
5823 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005824 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 if (!(opt_flags & OPT_LOCAL))
5826 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005827 // When fresh value of window-local option is not at the
5828 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5830 {
5831 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005832 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 {
5834 round = 1;
5835 varp_local = varp;
5836 varp = varp_fresh;
5837 }
5838 }
5839 }
5840 }
5841
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005842 // Round 1: fresh value for window-local options.
5843 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 for ( ; round <= 2; varp = varp_local, ++round)
5845 {
5846 if (round == 1 || (opt_flags & OPT_GLOBAL))
5847 cmd = "set";
5848 else
5849 cmd = "setlocal";
5850
5851 if (p->flags & P_BOOL)
5852 {
5853 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5854 return FAIL;
5855 }
5856 else if (p->flags & P_NUM)
5857 {
5858 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5859 return FAIL;
5860 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005861 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005863 int do_endif = FALSE;
5864
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005865 // Don't set 'syntax' and 'filetype' again if the value is
5866 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005867 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005868#if defined(FEAT_SYN_HL)
5869 p->indir == PV_SYN ||
5870#endif
5871 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 {
5873 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5874 *(char_u **)(varp)) < 0
5875 || put_eol(fd) < 0)
5876 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005877 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 }
5879 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005880 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005882 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 {
5884 if (put_line(fd, "endif") == FAIL)
5885 return FAIL;
5886 }
5887 }
5888 }
5889 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 return OK;
5892}
5893
5894#if defined(FEAT_FOLDING) || defined(PROTO)
5895/*
5896 * Generate set commands for the local fold options only. Used when
5897 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5898 */
5899 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005900makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005902 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005904 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 == FAIL
5906# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005907 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005909 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 == FAIL
5911 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5912 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5913 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5914 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5915 )
5916 return FAIL;
5917
5918 return OK;
5919}
5920#endif
5921
5922 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005923put_setstring(
5924 FILE *fd,
5925 char *cmd,
5926 char *name,
5927 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005928 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929{
5930 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005931 char_u *buf = NULL;
5932 char_u *part = NULL;
5933 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934
5935 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5936 return FAIL;
5937 if (*valuep != NULL)
5938 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005939 // Output 'pastetoggle' as key names. For other
5940 // options some characters have to be escaped with
5941 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 if (valuep == &p_pt)
5943 {
5944 s = *valuep;
5945 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005946 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 return FAIL;
5948 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005949 // expand the option value, replace $HOME by ~
5950 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005952 int size = (int)STRLEN(*valuep) + 1;
5953
5954 // replace home directory in the whole option value into "buf"
5955 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005956 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005957 goto fail;
5958 home_replace(NULL, *valuep, buf, size, FALSE);
5959
5960 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005961 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005962 // can be expanded when read back.
5963 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5964 && vim_strchr(*valuep, ',') != NULL)
5965 {
5966 part = alloc(size);
5967 if (part == NULL)
5968 goto fail;
5969
5970 // write line break to clear the option, e.g. ':set rtp='
5971 if (put_eol(fd) == FAIL)
5972 goto fail;
5973
5974 p = buf;
5975 while (*p != NUL)
5976 {
5977 // for each comma separated option part, append value to
5978 // the option, :set rtp+=value
5979 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5980 goto fail;
5981 (void)copy_option_part(&p, part, size, ",");
5982 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5983 goto fail;
5984 }
5985 vim_free(buf);
5986 vim_free(part);
5987 return OK;
5988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005990 {
5991 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005993 }
5994 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 }
5996 else if (put_escstr(fd, *valuep, 2) == FAIL)
5997 return FAIL;
5998 }
5999 if (put_eol(fd) < 0)
6000 return FAIL;
6001 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006002fail:
6003 vim_free(buf);
6004 vim_free(part);
6005 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006}
6007
6008 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006009put_setnum(
6010 FILE *fd,
6011 char *cmd,
6012 char *name,
6013 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014{
6015 long wc;
6016
6017 if (fprintf(fd, "%s %s=", cmd, name) < 0)
6018 return FAIL;
6019 if (wc_use_keyname((char_u *)valuep, &wc))
6020 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006021 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
6023 return FAIL;
6024 }
6025 else if (fprintf(fd, "%ld", *valuep) < 0)
6026 return FAIL;
6027 if (put_eol(fd) < 0)
6028 return FAIL;
6029 return OK;
6030}
6031
6032 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006033put_setbool(
6034 FILE *fd,
6035 char *cmd,
6036 char *name,
6037 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006039 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00006040 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
6042 || put_eol(fd) < 0)
6043 return FAIL;
6044 return OK;
6045}
6046
6047/*
6048 * Clear all the terminal options.
6049 * If the option has been allocated, free the memory.
6050 * Terminal options are never hidden or indirect.
6051 */
6052 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006053clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006055 // Reset a few things before clearing the old options. This may cause
6056 // outputting a few things that the terminal doesn't understand, but the
6057 // screen will be cleared later, so this is OK.
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006058 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006059 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006061 // When starting the GUI close the display opened for the clipboard.
6062 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 if (gui.starting)
6064 clear_xterm_clip();
6065#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006066 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006068 free_termoptions();
6069}
6070
6071 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006072free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006073{
6074 struct vimoption *p;
6075
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006076 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 if (istermoption(p))
6078 {
6079 if (p->flags & P_ALLOCED)
6080 free_string_option(*(char_u **)(p->var));
6081 if (p->flags & P_DEF_ALLOCED)
6082 free_string_option(p->def_val[VI_DEFAULT]);
6083 *(char_u **)(p->var) = empty_option;
6084 p->def_val[VI_DEFAULT] = empty_option;
6085 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006086#ifdef FEAT_EVAL
6087 // remember where the option was cleared
6088 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
6089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 }
6091 clear_termcodes();
6092}
6093
6094/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00006095 * Free the string for one term option, if it was allocated.
6096 * Set the string to empty_option and clear allocated flag.
6097 * "var" points to the option value.
6098 */
6099 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006100free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00006101{
6102 struct vimoption *p;
6103
6104 for (p = &options[0]; p->fullname != NULL; p++)
6105 if (p->var == var)
6106 {
6107 if (p->flags & P_ALLOCED)
6108 free_string_option(*(char_u **)(p->var));
6109 *(char_u **)(p->var) = empty_option;
6110 p->flags &= ~P_ALLOCED;
6111 break;
6112 }
6113}
6114
6115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 * Set the terminal option defaults to the current value.
6117 * Used after setting the terminal name.
6118 */
6119 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006120set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121{
6122 struct vimoption *p;
6123
6124 for (p = &options[0]; p->fullname != NULL; p++)
6125 {
6126 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
6127 {
6128 if (p->flags & P_DEF_ALLOCED)
6129 {
6130 free_string_option(p->def_val[VI_DEFAULT]);
6131 p->flags &= ~P_DEF_ALLOCED;
6132 }
6133 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
6134 if (p->flags & P_ALLOCED)
6135 {
6136 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006137 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 }
6139 }
6140 }
6141}
6142
6143/*
6144 * return TRUE if 'p' starts with 't_'
6145 */
6146 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006147istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148{
6149 return (p->fullname[0] == 't' && p->fullname[1] == '_');
6150}
6151
Bram Moolenaardac13472019-09-16 21:06:21 +02006152/*
6153 * Returns TRUE if the option at 'opt_idx' starts with 't_'
6154 */
6155 int
6156istermoption_idx(int opt_idx)
6157{
6158 return istermoption(&options[opt_idx]);
6159}
6160
Bram Moolenaar113e1072019-01-20 15:30:40 +01006161#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006163 * Unset local option value, similar to ":set opt<".
6164 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006165 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006166unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006167{
6168 struct vimoption *p;
6169 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006170 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006171
6172 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02006173 if (opt_idx < 0)
6174 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006175 p = &(options[opt_idx]);
6176
6177 switch ((int)p->indir)
6178 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006179 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006180 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006181 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006182 break;
6183 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006184 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006185 break;
6186 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006187 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006188 break;
6189 case PV_AR:
6190 buf->b_p_ar = -1;
6191 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006192 case PV_BKC:
6193 clear_string_option(&buf->b_p_bkc);
6194 buf->b_bkc_flags = 0;
6195 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006196 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006197 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006198 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006199 case PV_TC:
6200 clear_string_option(&buf->b_p_tc);
6201 buf->b_tc_flags = 0;
6202 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006203 case PV_SISO:
6204 curwin->w_p_siso = -1;
6205 break;
6206 case PV_SO:
6207 curwin->w_p_so = -1;
6208 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006209# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006210 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006211 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006212 break;
6213 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006214 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006215 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006216# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006217 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006218 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006219 break;
6220 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006221 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006222 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006223# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006224 case PV_TSRFU:
6225 clear_string_option(&buf->b_p_tsrfu);
6226 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006227# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006228 case PV_FP:
6229 clear_string_option(&buf->b_p_fp);
6230 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006231# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006232 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006233 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006234 break;
6235 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006236 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006237 break;
6238 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006239 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006240 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006241# endif
6242# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006243 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006244 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006245 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006246# endif
6247# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006248 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006249 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006250 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006251# endif
6252# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006253 case PV_SBR:
6254 clear_string_option(&((win_T *)from)->w_p_sbr);
6255 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006256# endif
6257# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006258 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006259 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006260 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006261# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006262 case PV_UL:
6263 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6264 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006265 case PV_LW:
6266 clear_string_option(&buf->b_p_lw);
6267 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006268 case PV_MENC:
6269 clear_string_option(&buf->b_p_menc);
6270 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006271 case PV_LCS:
6272 clear_string_option(&((win_T *)from)->w_p_lcs);
zeertzjq6a8d2e12024-01-17 20:54:49 +01006273 set_listchars_option((win_T *)from, ((win_T *)from)->w_p_lcs, TRUE,
6274 NULL, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006275 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006276 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006277 case PV_FCS:
6278 clear_string_option(&((win_T *)from)->w_p_fcs);
zeertzjq6a8d2e12024-01-17 20:54:49 +01006279 set_fillchars_option((win_T *)from, ((win_T *)from)->w_p_fcs, TRUE,
6280 NULL, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006281 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006282 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006283 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006284 clear_string_option(&((win_T *)from)->w_p_ve);
6285 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006286 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006287 }
6288}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006289#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006290
6291/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006293 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 */
6295 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006296get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006298 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 {
6300 if (p->var == VAR_WIN)
6301 return (char_u *)GLOBAL_WO(get_varp(p));
6302 return p->var;
6303 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006304 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 {
6306 switch ((int)p->indir)
6307 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006308 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006310 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
6311 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6312 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006314 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6315 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6316 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006317 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006318 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006319 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006320 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6321 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006323 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6324 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006326 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6327 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006328#ifdef FEAT_COMPL_FUNC
6329 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6330#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006331#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6332 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6333#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006334#if defined(FEAT_CRYPT)
6335 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6336#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006337#ifdef FEAT_LINEBREAK
6338 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6339#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006340#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006341 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006342#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006343 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006344 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006345 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006346 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006347 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006348 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006349 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006350
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006352 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 }
6354 return get_varp(p);
6355}
6356
6357/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006358 * Get pointer to option variable at 'opt_idx', depending on local or global
6359 * scope.
6360 */
6361 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006362get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006363{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006364 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006365}
6366
6367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 * Get pointer to option variable.
6369 */
6370 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006371get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006373 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 if (p->var == NULL)
6375 return NULL;
6376
6377 switch ((int)p->indir)
6378 {
6379 case PV_NONE: return p->var;
6380
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006381 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006382 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006384 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006386 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006388 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006390 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006392 case PV_TC: return *curbuf->b_p_tc != NUL
6393 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006394 case PV_BKC: return *curbuf->b_p_bkc != NUL
6395 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006396 case PV_SISO: return curwin->w_p_siso >= 0
6397 ? (char_u *)&(curwin->w_p_siso) : p->var;
6398 case PV_SO: return curwin->w_p_so >= 0
6399 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006401 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006403 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6405#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006406 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006408 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006410#ifdef FEAT_COMPL_FUNC
6411 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6412 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6413#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006414 case PV_FP: return *curbuf->b_p_fp != NUL
6415 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006417 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006419 case PV_GP: return *curbuf->b_p_gp != NUL
6420 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6421 case PV_MP: return *curbuf->b_p_mp != NUL
6422 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006424#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6425 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6426 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6427#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006428#if defined(FEAT_CRYPT)
6429 case PV_CM: return *curbuf->b_p_cm != NUL
6430 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6431#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006432#ifdef FEAT_LINEBREAK
6433 case PV_SBR: return *curwin->w_p_sbr != NUL
6434 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6435#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006436#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006437 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006438 ? (char_u *)&(curwin->w_p_stl) : p->var;
6439#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006440 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6441 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006442 case PV_LW: return *curbuf->b_p_lw != NUL
6443 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006444 case PV_MENC: return *curbuf->b_p_menc != NUL
6445 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446#ifdef FEAT_ARABIC
6447 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6448#endif
6449 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006450 case PV_LCS: return *curwin->w_p_lcs != NUL
6451 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006452 case PV_FCS: return *curwin->w_p_fcs != NUL
6453 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006454 case PV_VE: return *curwin->w_p_ve != NUL
6455 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006456#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006457 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6458#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006459#ifdef FEAT_SYN_HL
6460 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6461 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006462 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006463 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465#ifdef FEAT_DIFF
6466 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6467#endif
6468#ifdef FEAT_FOLDING
6469 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6470 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6471 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6472 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6473 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6474 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6475 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6476# ifdef FEAT_EVAL
6477 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6478 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6479# endif
6480 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6481#endif
6482 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006483 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006484#ifdef FEAT_LINEBREAK
6485 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6486#endif
Colin Kennedy21570352024-03-03 16:16:47 +01006487 case PV_WFB: return (char_u *)&(curwin->w_p_wfb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006489 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006490#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6492#endif
6493#ifdef FEAT_RIGHTLEFT
6494 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6495 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6496#endif
6497 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006498 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6500#ifdef FEAT_LINEBREAK
6501 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006502 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6503 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006505 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006507 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006508#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006509 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6510 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6511#endif
6512#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006513 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6514 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6515 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517
6518 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6519 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6522 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6524 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6526 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6527 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006528 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531#ifdef FEAT_FOLDING
6532 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006535#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006536 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006538#ifdef FEAT_COMPL_FUNC
6539 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006540 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006541#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006542#ifdef FEAT_EVAL
6543 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6544#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006545 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006547 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006553 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6555 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6556 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6557 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6558#ifdef FEAT_FIND_ID
6559# ifdef FEAT_EVAL
6560 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6561# endif
6562#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006563#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6565 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6566#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006567#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006568 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570#ifdef FEAT_CRYPT
6571 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006574 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6576 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6577 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6578 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6579 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006581 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6588#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006589 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006590 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6591#endif
6592#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006593 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6594 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6595 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006596 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597#endif
6598 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6599 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6600 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6601 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006602#ifdef FEAT_PERSISTENT_UNDO
6603 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6606#ifdef FEAT_KEYMAP
6607 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6608#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006609#ifdef FEAT_SIGNS
6610 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6611#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006612#ifdef FEAT_VARTABS
6613 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6614 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6615#endif
RestorerZ68ebcee2023-05-31 17:12:14 +01006616 default: iemsg(e_get_varp_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006618 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 return (char_u *)&(curbuf->b_p_wm);
6620}
6621
6622/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006623 * Return a pointer to the variable for option at 'opt_idx'
6624 */
6625 char_u *
6626get_option_var(int opt_idx)
6627{
6628 return options[opt_idx].var;
6629}
6630
Dominique Pelle748b3082022-01-08 12:41:16 +00006631#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006632/*
6633 * Return the full name of the option at 'opt_idx'
6634 */
6635 char_u *
6636get_option_fullname(int opt_idx)
6637{
6638 return (char_u *)options[opt_idx].fullname;
6639}
Dominique Pelle748b3082022-01-08 12:41:16 +00006640#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006641
6642/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00006643 * Return the did_set callback function for the option at 'opt_idx'
6644 */
6645 opt_did_set_cb_T
6646get_option_did_set_cb(int opt_idx)
6647{
6648 return options[opt_idx].opt_did_set_cb;
6649}
6650
6651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 * Get the value of 'equalprg', either the buffer-local one or the global one.
6653 */
6654 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006655get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656{
6657 if (*curbuf->b_p_ep == NUL)
6658 return p_ep;
6659 return curbuf->b_p_ep;
6660}
6661
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662/*
6663 * Copy options from one window to another.
6664 * Used when splitting a window.
6665 */
6666 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006667win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668{
6669 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6670 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006671 after_copy_winopt(wp_to);
6672}
6673
6674/*
6675 * After copying window options: update variables depending on options.
6676 */
6677 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006678after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006679{
6680#ifdef FEAT_LINEBREAK
6681 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006682#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006683#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006684 fill_culopt_flags(NULL, wp);
6685 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006686#endif
zeertzjq6a8d2e12024-01-17 20:54:49 +01006687 set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
6688 set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006689}
6690
6691 static char_u *
6692copy_option_val(char_u *val)
6693{
6694 if (val == empty_option)
6695 return empty_option; // no need to allocate memory
6696 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698
6699/*
6700 * Copy the options from one winopt_T to another.
6701 * Doesn't free the old option values in "to", use clear_winopt() for that.
6702 * The 'scroll' option is not copied, because it depends on the window height.
6703 * The 'previewwindow' option is reset, there can be only one preview window.
6704 */
6705 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006706copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707{
6708#ifdef FEAT_ARABIC
6709 to->wo_arab = from->wo_arab;
6710#endif
6711 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006712 to->wo_lcs = copy_option_val(from->wo_lcs);
6713 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006715 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006716 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006717 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006718#ifdef FEAT_LINEBREAK
6719 to->wo_nuw = from->wo_nuw;
6720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721#ifdef FEAT_RIGHTLEFT
6722 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006723 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006725#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006726 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006727#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006728#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006729 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006732#ifdef FEAT_DIFF
6733 to->wo_wrap_save = from->wo_wrap_save;
6734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735#ifdef FEAT_LINEBREAK
6736 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006737 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006738 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006740 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006742 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006743 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006744 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006745 to->wo_crb_save = from->wo_crb_save;
Christian Brabandt4a8eb6e2023-08-13 19:43:42 +02006746 to->wo_siso = from->wo_siso;
6747 to->wo_so = from->wo_so;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006748#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006749 to->wo_spell = from->wo_spell;
6750#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006751#ifdef FEAT_SYN_HL
6752 to->wo_cuc = from->wo_cuc;
6753 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006754 to->wo_culopt = copy_option_val(from->wo_culopt);
6755 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757#ifdef FEAT_DIFF
6758 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006759 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006761#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006762 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006763 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006764#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006765#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006766 to->wo_twk = copy_option_val(from->wo_twk);
6767 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769#ifdef FEAT_FOLDING
6770 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006771 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006773 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006774 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 to->wo_fml = from->wo_fml;
6776 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006777 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006778 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006779 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006780 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 to->wo_fdn = from->wo_fdn;
6782# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006783 to->wo_fde = copy_option_val(from->wo_fde);
6784 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006786 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006788#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006789 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006790#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006791
6792#ifdef FEAT_EVAL
6793 // Copy the script context so that we know where the value was last set.
6794 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6795 sizeof(to->wo_script_ctx));
6796#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006797 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798}
6799
6800/*
6801 * Check string options in a window for a NULL value.
6802 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006803 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006804check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805{
6806 check_winopt(&win->w_onebuf_opt);
6807 check_winopt(&win->w_allbuf_opt);
6808}
6809
6810/*
6811 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6812 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006813 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006814check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815{
6816#ifdef FEAT_FOLDING
6817 check_string_option(&wop->wo_fdi);
6818 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006819 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820# ifdef FEAT_EVAL
6821 check_string_option(&wop->wo_fde);
6822 check_string_option(&wop->wo_fdt);
6823# endif
6824 check_string_option(&wop->wo_fmr);
6825#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006826#ifdef FEAT_SIGNS
6827 check_string_option(&wop->wo_scl);
6828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829#ifdef FEAT_RIGHTLEFT
6830 check_string_option(&wop->wo_rlc);
6831#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006832#ifdef FEAT_LINEBREAK
6833 check_string_option(&wop->wo_sbr);
6834#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006835#ifdef FEAT_STL_OPT
6836 check_string_option(&wop->wo_stl);
6837#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006838#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006839 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006840 check_string_option(&wop->wo_cc);
6841#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006842#ifdef FEAT_CONCEAL
6843 check_string_option(&wop->wo_cocu);
6844#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006845#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006846 check_string_option(&wop->wo_twk);
6847 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006848#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006849#ifdef FEAT_LINEBREAK
6850 check_string_option(&wop->wo_briopt);
6851#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006852 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006853 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006854 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006855 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856}
6857
6858/*
6859 * Free the allocated memory inside a winopt_T.
6860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006862clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863{
6864#ifdef FEAT_FOLDING
6865 clear_string_option(&wop->wo_fdi);
6866 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006867 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868# ifdef FEAT_EVAL
6869 clear_string_option(&wop->wo_fde);
6870 clear_string_option(&wop->wo_fdt);
6871# endif
6872 clear_string_option(&wop->wo_fmr);
6873#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006874#ifdef FEAT_SIGNS
6875 clear_string_option(&wop->wo_scl);
6876#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006877#ifdef FEAT_LINEBREAK
6878 clear_string_option(&wop->wo_briopt);
6879#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006880 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881#ifdef FEAT_RIGHTLEFT
6882 clear_string_option(&wop->wo_rlc);
6883#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006884#ifdef FEAT_LINEBREAK
6885 clear_string_option(&wop->wo_sbr);
6886#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006887#ifdef FEAT_STL_OPT
6888 clear_string_option(&wop->wo_stl);
6889#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006890#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006891 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006892 clear_string_option(&wop->wo_cc);
6893#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006894#ifdef FEAT_CONCEAL
6895 clear_string_option(&wop->wo_cocu);
6896#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006897#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006898 clear_string_option(&wop->wo_twk);
6899 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006900#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006901 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006902 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006903 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904}
6905
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006906#ifdef FEAT_EVAL
6907// Index into the options table for a buffer-local option enum.
6908static int buf_opt_idx[BV_COUNT];
6909# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6910
6911/*
6912 * Initialize buf_opt_idx[] if not done already.
6913 */
6914 static void
6915init_buf_opt_idx(void)
6916{
6917 static int did_init_buf_opt_idx = FALSE;
6918 int i;
6919
6920 if (did_init_buf_opt_idx)
6921 return;
6922 did_init_buf_opt_idx = TRUE;
6923 for (i = 0; !istermoption_idx(i); i++)
6924 if (options[i].indir & PV_BUF)
6925 buf_opt_idx[options[i].indir & PV_MASK] = i;
6926}
6927#else
6928# define COPY_OPT_SCTX(buf, bv)
6929#endif
6930
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931/*
6932 * Copy global option values to local options for one buffer.
6933 * Used when creating a new buffer and sometimes when entering a buffer.
6934 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006935 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6937 * appropriate.
6938 * BCO_NOHELP Don't copy the values to a help buffer.
6939 */
6940 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006941buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942{
6943 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006944 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 int dont_do_help;
6946 int did_isk = FALSE;
6947
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006948 // Skip this when the option defaults have not been set yet. Happens when
6949 // main() allocates the first buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 if (p_cpo != NULL)
6951 {
6952 /*
6953 * Always copy when entering and 'cpo' contains 'S'.
6954 * Don't copy when already initialized.
6955 * Don't copy when 'cpo' contains 's' and not entering.
6956 * 'S' BCO_ENTER initialized 's' should_copy
6957 * yes yes X X TRUE
6958 * yes no yes X FALSE
6959 * no X yes X FALSE
6960 * X no no yes FALSE
6961 * X no no no TRUE
6962 * no yes no X TRUE
6963 */
6964 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6965 && (buf->b_p_initialized
6966 || (!(flags & BCO_ENTER)
6967 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6968 should_copy = FALSE;
6969
6970 if (should_copy || (flags & BCO_ALWAYS))
6971 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006972#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006973 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006974 init_buf_opt_idx();
6975#endif
6976 // Don't copy the options specific to a help buffer when
6977 // BCO_NOHELP is given or the options were initialized already
6978 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6980 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006981 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 {
6983 save_p_isk = buf->b_p_isk;
6984 buf->b_p_isk = NULL;
6985 }
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006986 // Always free the allocated strings. If not already initialized,
6987 // reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 if (!buf->b_p_initialized)
6989 {
6990 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006991 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006994 switch (*p_ffs)
6995 {
6996 case 'm':
6997 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6998 case 'd':
6999 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
7000 case 'u':
7001 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
7002 default:
7003 buf->b_p_ff = vim_strsave(p_ff);
7004 }
7005 if (buf->b_p_ff != NULL)
7006 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 buf->b_p_bh = empty_option;
7008 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 }
7010 else
7011 free_buf_options(buf, FALSE);
7012
7013 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007014 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 buf->b_p_ai_nopaste = p_ai_nopaste;
7016 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007017 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007019 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 buf->b_p_tw_nopaste = p_tw_nopaste;
7021 buf->b_p_tw_nobin = p_tw_nobin;
7022 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007023 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 buf->b_p_wm_nopaste = p_wm_nopaste;
7025 buf->b_p_wm_nobin = p_wm_nobin;
7026 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007027 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00007028 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007029 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02007030 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007031 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007033 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007035 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007037 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 buf->b_p_ml_nobin = p_ml_nobin;
7039 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007040 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02007041 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007042 buf->b_p_swf = FALSE;
7043 else
7044 {
7045 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00007046 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007049 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02007050#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02007051 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007052 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007054#ifdef FEAT_COMPL_FUNC
7055 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007056 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007057 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00007058 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007059 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007060 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007061#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02007062#ifdef FEAT_EVAL
7063 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007064 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007065 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02007066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007068 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007070#ifdef FEAT_VARTABS
7071 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007072 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007073 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007074 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007075 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007076 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007077 buf->b_p_vsts_nopaste = p_vsts_nopaste
7078 ? vim_strsave(p_vsts_nopaste) : NULL;
7079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007081 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007083 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084#ifdef FEAT_FOLDING
7085 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007086 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087#endif
7088 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007089 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007090 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007091 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02007092 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
7093 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007095 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007097 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007099 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007101 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007102
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007104 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007106 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007108 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01007109 buf->b_p_cinsd = vim_strsave(p_cinsd);
7110 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01007111 buf->b_p_lop = vim_strsave(p_lop);
7112 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007113
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007114 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007117 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007119 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007121 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007123 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00007125 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007126 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01007127 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007128#endif
7129#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02007130 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007131 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007132 (void)compile_cap_prog(&buf->b_s);
7133 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007134 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007135 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007136 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02007137 buf->b_s.b_p_spo = vim_strsave(p_spo);
7138 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007140#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007142 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007144 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01007146 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007147#if defined(FEAT_EVAL)
7148 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007149 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151#ifdef FEAT_CRYPT
7152 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007153 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007156 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157#ifdef FEAT_KEYMAP
7158 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007159 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 buf->b_kmap_state |= KEYMAP_INIT;
7161#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007162#ifdef FEAT_TERMINAL
7163 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007164 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007165#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007166 // This isn't really an option, but copying the langmap and IME
7167 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007169 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007171 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007173 // options that are normally global but also have a local value
7174 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01007176 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007177 buf->b_p_bkc = empty_option;
7178 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179#ifdef FEAT_QUICKFIX
7180 buf->b_p_gp = empty_option;
7181 buf->b_p_mp = empty_option;
7182 buf->b_p_efm = empty_option;
7183#endif
7184 buf->b_p_ep = empty_option;
7185 buf->b_p_kp = empty_option;
7186 buf->b_p_path = empty_option;
7187 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007188 buf->b_p_tc = empty_option;
7189 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190#ifdef FEAT_FIND_ID
7191 buf->b_p_def = empty_option;
7192 buf->b_p_inc = empty_option;
7193# ifdef FEAT_EVAL
7194 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007195 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196# endif
7197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 buf->b_p_dict = empty_option;
7199 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01007200#ifdef FEAT_COMPL_FUNC
7201 buf->b_p_tsrfu = empty_option;
7202#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007203 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007204 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007205#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7206 buf->b_p_bexpr = empty_option;
7207#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007208#if defined(FEAT_CRYPT)
7209 buf->b_p_cm = empty_option;
7210#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007211#ifdef FEAT_PERSISTENT_UNDO
7212 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007213 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007214#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007215 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007216 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007218 // Don't copy the options set by ex_help(), use the saved values,
7219 // when going from a help buffer to a non-help buffer.
7220 // Don't touch these at all when BCO_NOHELP is used and going from
7221 // or to a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007223 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007225#ifdef FEAT_VARTABS
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007226 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007227 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007228 else
7229 buf->b_p_vts_array = NULL;
7230#endif
7231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 else
7233 {
7234 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007235 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 did_isk = TRUE;
7237 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00007238 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007239#ifdef FEAT_VARTABS
7240 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007241 COPY_OPT_SCTX(buf, BV_VTS);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007242 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007243 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007244 else
7245 buf->b_p_vts_array = NULL;
7246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 if (buf->b_p_bt[0] == 'h')
7249 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007251 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 }
7253 }
7254
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007255 // When the options should be copied (ignoring BCO_ALWAYS), set the
7256 // flag that indicates that the options have been initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 if (should_copy)
7258 buf->b_p_initialized = TRUE;
7259 }
7260
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007261 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 if (did_isk)
7263 (void)buf_init_chartab(buf, FALSE);
7264}
7265
7266/*
7267 * Reset the 'modifiable' option and its default value.
7268 */
7269 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007270reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271{
7272 int opt_idx;
7273
7274 curbuf->b_p_ma = FALSE;
7275 p_ma = FALSE;
7276 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007277 if (opt_idx >= 0)
7278 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279}
7280
7281/*
7282 * Set the global value for 'iminsert' to the local value.
7283 */
7284 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007285set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286{
7287 p_iminsert = curbuf->b_p_iminsert;
7288}
7289
7290/*
7291 * Set the global value for 'imsearch' to the local value.
7292 */
7293 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007294set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295{
7296 p_imsearch = curbuf->b_p_imsearch;
7297}
7298
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299static int expand_option_idx = -1;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007300static int expand_option_start_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7302static int expand_option_flags = 0;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007303static int expand_option_append = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304
7305 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007306set_context_in_set_cmd(
7307 expand_T *xp,
7308 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007309 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310{
7311 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007312 long_u flags = 0; // init for GCC
7313 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 char_u *p;
7315 char_u *s;
7316 int is_term_option = FALSE;
7317 int key;
7318
7319 expand_option_flags = opt_flags;
7320
7321 xp->xp_context = EXPAND_SETTINGS;
7322 if (*arg == NUL)
7323 {
7324 xp->xp_pattern = arg;
7325 return;
7326 }
7327 p = arg + STRLEN(arg) - 1;
7328 if (*p == ' ' && *(p - 1) != '\\')
7329 {
7330 xp->xp_pattern = p + 1;
7331 return;
7332 }
7333 while (p > arg)
7334 {
7335 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007336 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 if (*p == ' ' || *p == ',')
7338 {
7339 while (s > arg && *(s - 1) == '\\')
7340 --s;
7341 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007342 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 if (*p == ' ' && ((p - s) & 1) == 0)
7344 {
7345 ++p;
7346 break;
7347 }
7348 --p;
7349 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007350 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 {
7352 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007353 xp->xp_prefix = XP_PREFIX_NO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 p += 2;
7355 }
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007356 else if (STRNCMP(p, "inv", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 {
7358 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007359 xp->xp_prefix = XP_PREFIX_INV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 p += 3;
7361 }
7362 xp->xp_pattern = arg = p;
7363 if (*arg == '<')
7364 {
7365 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007366 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 return;
7368 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007369 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 {
7371 xp->xp_context = EXPAND_NOTHING;
7372 return;
7373 }
7374 nextchar = *++p;
7375 is_term_option = TRUE;
7376 expand_option_name[2] = KEY2TERMCAP0(key);
7377 expand_option_name[3] = KEY2TERMCAP1(key);
7378 }
7379 else
7380 {
7381 if (p[0] == 't' && p[1] == '_')
7382 {
7383 p += 2;
7384 if (*p != NUL)
7385 ++p;
7386 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007387 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 nextchar = *++p;
7389 is_term_option = TRUE;
7390 expand_option_name[2] = p[-2];
7391 expand_option_name[3] = p[-1];
7392 }
7393 else
7394 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007395 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7397 p++;
7398 if (*p == NUL)
7399 return;
7400 nextchar = *p;
7401 *p = NUL;
7402 opt_idx = findoption(arg);
7403 *p = nextchar;
7404 if (opt_idx == -1 || options[opt_idx].var == NULL)
7405 {
7406 xp->xp_context = EXPAND_NOTHING;
7407 return;
7408 }
7409 flags = options[opt_idx].flags;
7410 if (flags & P_BOOL)
7411 {
7412 xp->xp_context = EXPAND_NOTHING;
7413 return;
7414 }
7415 }
7416 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007417 // handle "-=" and "+="
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007418 expand_option_append = FALSE;
7419 int expand_option_subtract = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7421 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007422 if (nextchar == '-')
7423 expand_option_subtract = TRUE;
7424 if (nextchar == '+' || nextchar == '^')
7425 expand_option_append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 ++p;
7427 nextchar = '=';
7428 }
7429 if ((nextchar != '=' && nextchar != ':')
7430 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7431 {
7432 xp->xp_context = EXPAND_UNSUCCESSFUL;
7433 return;
7434 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007435
7436 // Below are for handling expanding a specific option's value after the '='
7437 // or ':'
7438
7439 if (is_term_option)
7440 expand_option_idx = -1;
7441 else
7442 expand_option_idx = opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02007444 if (!is_term_option)
7445 {
7446 if (options[opt_idx].flags & P_NO_CMD_EXPAND)
7447 {
7448 xp->xp_context=EXPAND_UNSUCCESSFUL;
7449 return;
7450 }
7451 }
7452
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 xp->xp_pattern = p + 1;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007454 expand_option_start_col = (int)(p + 1 - xp->xp_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007456 // Certain options currently have special case handling to reuse the
7457 // expansion logic with other commands.
Doug Kearns6dfdff32023-08-27 18:48:51 +02007458#ifdef FEAT_SYN_HL
7459 if (options[opt_idx].var == (char_u *)&p_syn)
7460 {
7461 xp->xp_context = EXPAND_OWNSYNTAX;
7462 return;
7463 }
7464#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007465 if (options[opt_idx].var == (char_u *)&p_ft)
7466 {
7467 xp->xp_context = EXPAND_FILETYPE;
7468 return;
7469 }
Doug Kearns81642d92024-01-04 22:37:44 +01007470#ifdef FEAT_KEYMAP
7471 if (options[opt_idx].var == (char_u *)&p_keymap)
7472 {
7473 xp->xp_context = EXPAND_KEYMAP;
7474 return;
7475 }
7476#endif
Doug Kearns6dfdff32023-08-27 18:48:51 +02007477
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007478 // Now pick. If the option has a custom expander, use that. Otherwise, just
7479 // fill with the existing option value.
7480 if (expand_option_subtract)
7481 {
7482 xp->xp_context = EXPAND_SETTING_SUBTRACT;
7483 return;
7484 }
7485 else if (expand_option_idx >= 0 &&
7486 options[expand_option_idx].opt_expand_cb != NULL)
7487 {
7488 xp->xp_context = EXPAND_STRING_SETTING;
7489 }
7490 else if (*xp->xp_pattern == NUL)
7491 {
7492 xp->xp_context = EXPAND_OLD_SETTING;
7493 return;
7494 }
7495 else
7496 xp->xp_context = EXPAND_NOTHING;
7497
7498 if (is_term_option || (flags & P_NUM))
7499 return;
7500
7501 // Only string options below
7502
7503 // Options that have P_EXPAND are considered to all use file/dir expansion.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 if (flags & P_EXPAND)
7505 {
7506 p = options[opt_idx].var;
7507 if (p == (char_u *)&p_bdir
7508 || p == (char_u *)&p_dir
7509 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007510 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513#ifdef FEAT_SESSION
7514 || p == (char_u *)&p_vdir
7515#endif
7516 )
7517 {
7518 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007519 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 xp->xp_backslash = XP_BS_THREE;
7521 else
7522 xp->xp_backslash = XP_BS_ONE;
7523 }
7524 else
7525 {
7526 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007527 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 if (p == (char_u *)&p_tags)
7529 xp->xp_backslash = XP_BS_THREE;
7530 else
7531 xp->xp_backslash = XP_BS_ONE;
7532 }
Yee Cheng Chin54844852023-10-09 18:12:31 +02007533 if (flags & P_COMMA)
7534 xp->xp_backslash |= XP_BS_COMMA;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 }
7536
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007537 // For an option that is a list of file names, or comma/colon-separated
7538 // values, split it by the delimiter and find the start of the current
7539 // pattern, while accounting for backslash-escaped space/commas/colons.
7540 // Triple-backslashed escaped file names (e.g. 'path') can also be
7541 // delimited by space.
7542 if ((flags & P_EXPAND) || (flags & P_COMMA) || (flags & P_COLON))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007544 for (p = arg + STRLEN(arg) - 1; p >= xp->xp_pattern; --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007546 // count number of backslashes before ' ' or ',' or ':'
7547 if (*p == ' ' || *p == ',' ||
7548 (*p == ':' && (flags & P_COLON)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007550 s = p;
7551 while (s > xp->xp_pattern && *(s - 1) == '\\')
7552 --s;
Yee Cheng Chin54844852023-10-09 18:12:31 +02007553 if ((*p == ' ' && ((xp->xp_backslash & XP_BS_THREE) && (p - s) < 3))
7554#if defined(BACKSLASH_IN_FILENAME)
7555 || (*p == ',' && (flags & P_COMMA) && (p - s) < 1)
7556#else
7557 || (*p == ',' && (flags & P_COMMA) && (p - s) < 2)
7558#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007559 || (*p == ':' && (flags & P_COLON)))
7560 {
7561 xp->xp_pattern = p + 1;
7562 break;
7563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 }
7565 }
7566 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007567
7568 // An option that is a list of single-character flags should always start
7569 // at the end as we don't complete words.
7570 if (flags & P_FLAGLIST)
7571 xp->xp_pattern = arg + STRLEN(arg);
7572
7573 // Some options can either be using file/dir expansions, or custom value
7574 // expansion depending on what the user typed. Unfortunately we have to
7575 // manually handle it here to make sure we have the correct xp_context set.
7576#ifdef FEAT_SPELL
7577 if (options[opt_idx].var == (char_u *)&p_sps)
7578 {
7579 if (STRNCMP(xp->xp_pattern, "file:", 5) == 0)
7580 {
7581 xp->xp_pattern += 5;
7582 return;
7583 }
7584 else if (options[expand_option_idx].opt_expand_cb != NULL)
7585 {
7586 xp->xp_context = EXPAND_STRING_SETTING;
7587 }
7588 }
7589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590}
7591
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007592/*
7593 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7594 *
7595 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7596 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7597 *
7598 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7599 * regular expression 'regmatch', then stores the match in matches[idx] and
7600 * returns TRUE.
7601 *
7602 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7603 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7604 *
7605 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7606 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7607 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02007608 static int
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007609match_str(
7610 char_u *str,
7611 regmatch_T *regmatch,
7612 char_u **matches,
7613 int idx,
7614 int test_only,
7615 int fuzzy,
7616 char_u *fuzzystr,
7617 fuzmatch_str_T *fuzmatch)
7618{
7619 if (!fuzzy)
7620 {
7621 if (vim_regexec(regmatch, str, (colnr_T)0))
7622 {
7623 if (!test_only)
7624 matches[idx] = vim_strsave(str);
7625 return TRUE;
7626 }
7627 }
7628 else
7629 {
7630 int score;
7631
7632 score = fuzzy_match_str(str, fuzzystr);
7633 if (score != 0)
7634 {
7635 if (!test_only)
7636 {
7637 fuzmatch[idx].idx = idx;
7638 fuzmatch[idx].str = vim_strsave(str);
7639 fuzmatch[idx].score = score;
7640 }
7641
7642 return TRUE;
7643 }
7644 }
7645
7646 return FALSE;
7647}
7648
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007650ExpandSettings(
7651 expand_T *xp,
7652 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007653 char_u *fuzzystr,
7654 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007655 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007656 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007658 int num_normal = 0; // Nr of matching non-term-code settings
7659 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 int opt_idx;
7661 int match;
7662 int count = 0;
7663 char_u *str;
7664 int loop;
7665 int is_term_opt;
7666 char_u name_buf[MAX_KEY_NAME_LEN];
7667 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007668 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007669 int fuzzy;
7670 fuzmatch_str_T *fuzmatch = NULL;
7671
Christian Brabandtcb747892022-05-08 21:10:56 +01007672 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007674 // do this loop twice:
7675 // loop == 0: count the number of matching options
7676 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 for (loop = 0; loop <= 1; ++loop)
7678 {
7679 regmatch->rm_ic = ic;
7680 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7681 {
K.Takataeeec2542021-06-02 13:28:16 +02007682 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007683 {
7684 if (match_str((char_u *)names[match], regmatch, *matches,
7685 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 {
7687 if (loop == 0)
7688 num_normal++;
7689 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007690 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 }
7694 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7695 opt_idx++)
7696 {
7697 if (options[opt_idx].var == NULL)
7698 continue;
7699 if (xp->xp_context == EXPAND_BOOL_SETTINGS
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007700 && !(options[opt_idx].flags & P_BOOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007702 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 if (is_term_opt && num_normal > 0)
7704 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007705
7706 if (match_str(str, regmatch, *matches, count, (loop == 0),
7707 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 {
7709 if (loop == 0)
7710 {
7711 if (is_term_opt)
7712 num_term++;
7713 else
7714 num_normal++;
7715 }
7716 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007717 count++;
7718 }
7719 else if (!fuzzy && options[opt_idx].shortname != NULL
7720 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007721 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007722 {
7723 // Compare against the abbreviated option name (for regular
7724 // expression match). Fuzzy matching (previous if) already
7725 // matches against both the expanded and abbreviated names.
7726 if (loop == 0)
7727 {
7728 if (is_term_opt)
7729 num_term++;
7730 else
7731 num_normal++;
7732 }
7733 else
7734 (*matches)[count++] = vim_strsave(str);
7735 }
7736 else if (is_term_opt)
7737 {
7738 name_buf[0] = '<';
7739 name_buf[1] = 't';
7740 name_buf[2] = '_';
7741 name_buf[3] = str[2];
7742 name_buf[4] = str[3];
7743 name_buf[5] = '>';
7744 name_buf[6] = NUL;
7745
7746 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7747 fuzzy, fuzzystr, fuzmatch))
7748 {
7749 if (loop == 0)
7750 num_term++;
7751 else
7752 count++;
7753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 }
7755 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007756
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007757 // Check terminal key codes, these are not in the option table
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7759 {
7760 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7761 {
Keith Thompson184f71c2024-01-04 21:19:04 +01007762 if (!SAFE_isprint(str[0]) || !SAFE_isprint(str[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 continue;
7764
7765 name_buf[0] = 't';
7766 name_buf[1] = '_';
7767 name_buf[2] = str[0];
7768 name_buf[3] = str[1];
7769 name_buf[4] = NUL;
7770
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007771 if (match_str(name_buf, regmatch, *matches, count,
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007772 (loop == 0), fuzzy, fuzzystr, fuzmatch))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007773 {
7774 if (loop == 0)
7775 num_term++;
7776 else
7777 count++;
7778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 else
7780 {
7781 name_buf[0] = '<';
7782 name_buf[1] = 't';
7783 name_buf[2] = '_';
7784 name_buf[3] = str[0];
7785 name_buf[4] = str[1];
7786 name_buf[5] = '>';
7787 name_buf[6] = NUL;
7788
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007789 if (match_str(name_buf, regmatch, *matches, count,
7790 (loop == 0), fuzzy, fuzzystr,
7791 fuzmatch))
7792 {
7793 if (loop == 0)
7794 num_term++;
7795 else
7796 count++;
7797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 }
7799 }
7800
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007801 // Check special key names.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007802 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7804 {
7805 name_buf[0] = '<';
7806 STRCPY(name_buf + 1, str);
7807 STRCAT(name_buf, ">");
7808
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007809 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7810 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 {
7812 if (loop == 0)
7813 num_term++;
7814 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007815 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 }
7817 }
7818 }
7819 if (loop == 0)
7820 {
7821 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007822 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007824 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 else
7826 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007827 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007829 *matches = ALLOC_MULT(char_u *, *numMatches);
7830 if (*matches == NULL)
7831 {
7832 *matches = (char_u **)"";
7833 return FAIL;
7834 }
7835 }
7836 else
7837 {
7838 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7839 if (fuzmatch == NULL)
7840 {
7841 *matches = (char_u **)"";
7842 return FAIL;
7843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 }
7845 }
7846 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007847
7848 if (fuzzy &&
7849 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7850 return FAIL;
7851
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 return OK;
7853}
7854
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007855// Escape an option value that can be used on the command-line with :set.
7856// Caller needs to free the returned string, unless NULL is returned.
7857 static char_u*
7858escape_option_str_cmdline(char_u *var)
7859{
7860 char_u *buf;
7861
7862 // A backslash is required before some characters. This is the reverse of
7863 // what happens in do_set().
7864 buf = vim_strsave_escaped(var, escape_chars);
7865 if (buf == NULL)
7866 return NULL;
7867
7868#ifdef BACKSLASH_IN_FILENAME
7869 // For MS-Windows et al. we don't double backslashes at the start and
7870 // before a file name character.
7871 // The reverse is found at stropt_copy_value().
7872 for (var = buf; *var != NUL; MB_PTR_ADV(var))
7873 if (var[0] == '\\' && var[1] == '\\'
7874 && expand_option_idx >= 0
7875 && (options[expand_option_idx].flags & P_EXPAND)
7876 && vim_isfilec(var[2])
7877 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
7878 STRMOVE(var, var + 1);
7879#endif
7880 return buf;
7881}
7882
7883/*
7884 * Expansion handler for :set= when we just want to fill in with the existing value.
7885 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007887ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007889 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 char_u *buf;
7891
zeertzjqb0d45ec2023-01-25 15:04:22 +00007892 *numMatches = 0;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007893 *matches = ALLOC_MULT(char_u *, 1);
zeertzjqb0d45ec2023-01-25 15:04:22 +00007894 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 return FAIL;
7896
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007897 // For a terminal key code expand_option_idx is < 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 if (expand_option_idx < 0)
7899 {
7900 var = find_termcode(expand_option_name + 2);
7901 if (var == NULL)
7902 expand_option_idx = findoption(expand_option_name);
7903 }
7904
7905 if (expand_option_idx >= 0)
7906 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007907 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 option_value2string(&options[expand_option_idx], expand_option_flags);
7909 var = NameBuff;
7910 }
7911 else if (var == NULL)
7912 var = (char_u *)"";
7913
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007914 buf = escape_option_str_cmdline(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 if (buf == NULL)
7916 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007917 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 return FAIL;
7919 }
7920
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007921 (*matches)[0] = buf;
zeertzjqb0d45ec2023-01-25 15:04:22 +00007922 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 return OK;
7924}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925
7926/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007927 * Expansion handler for :set=/:set+= when the option has a custom expansion handler.
7928 */
7929 int
7930ExpandStringSetting(
7931 expand_T *xp,
7932 regmatch_T *regmatch,
7933 int *numMatches,
7934 char_u ***matches)
7935{
7936 char_u *var = NULL; // init for GCC
7937 char_u *buf;
7938
7939 if (expand_option_idx < 0 ||
7940 options[expand_option_idx].opt_expand_cb == NULL)
7941 {
7942 // Not supposed to reach this. This function is only for options with
7943 // custom expansion callbacks.
7944 return FAIL;
7945 }
7946
7947 optexpand_T args;
7948 args.oe_varp = get_varp_scope(&options[expand_option_idx], expand_option_flags);
7949 args.oe_append = expand_option_append;
7950 args.oe_regmatch = regmatch;
7951 args.oe_xp = xp;
7952 args.oe_set_arg = xp->xp_line + expand_option_start_col;
7953 args.oe_include_orig_val =
7954 !expand_option_append &&
7955 (*args.oe_set_arg == NUL);
7956
7957 // Retrieve the existing value, but escape it as a reverse of setting it.
7958 // We technically only need to do this when oe_append or
7959 // oe_include_orig_val is true.
7960 option_value2string(&options[expand_option_idx], expand_option_flags);
7961 var = NameBuff;
7962 buf = escape_option_str_cmdline(var);
7963 if (buf == NULL)
7964 return FAIL;
7965
7966 args.oe_opt_value = buf;
7967
7968 int num_ret = options[expand_option_idx].opt_expand_cb(&args, numMatches, matches);
7969
7970 vim_free(buf);
7971 return num_ret;
7972}
7973
7974/*
7975 * Expansion handler for :set-=
7976 */
7977 int
7978ExpandSettingSubtract(
7979 expand_T *xp,
7980 regmatch_T *regmatch,
7981 int *numMatches,
7982 char_u ***matches)
7983{
7984 if (expand_option_idx < 0)
7985 // term option
7986 return ExpandOldSetting(numMatches, matches);
7987
7988 char_u *option_val = *(char_u**)get_option_varp_scope(
7989 expand_option_idx, expand_option_flags);
7990
7991 long_u option_flags = options[expand_option_idx].flags;
7992
7993 if (option_flags & P_NUM)
7994 return ExpandOldSetting(numMatches, matches);
7995 else if (option_flags & P_COMMA)
7996 {
7997 // Split the option by comma, then present each option to the user if
7998 // it matches the pattern.
7999 // This condition needs to go first, because 'whichwrap' has both
8000 // P_COMMA and P_FLAGLIST.
8001 garray_T ga;
8002
8003 char_u *item;
8004 char_u *option_copy;
8005 char_u *next_val;
8006 char_u *comma;
8007
8008 if (*option_val == NUL)
8009 return FAIL;
8010
8011 // Make a copy as we need to inject null characters destructively.
8012 option_copy = vim_strsave(option_val);
8013 if (option_copy == NULL)
8014 return FAIL;
8015 next_val = option_copy;
8016
8017 ga_init2(&ga, sizeof(char_u *), 10);
8018
8019 do
8020 {
8021 item = next_val;
8022 comma = vim_strchr(next_val, ',');
8023 while (comma != NULL && comma != next_val && *(comma - 1) == '\\')
8024 {
8025 // "\," is interpreted as a literal comma rather than option
8026 // separator when reading options in copy_option_part(). Skip
8027 // it.
8028 comma = vim_strchr(comma + 1, ',');
8029 }
8030 if (comma != NULL)
8031 {
8032 *comma = NUL; // null-terminate this value, required by later functions
8033 next_val = comma + 1;
8034 }
8035 else
8036 next_val = NULL;
8037
8038 if (*item == NUL)
8039 // empty value, don't add to list
8040 continue;
8041
8042 if (!vim_regexec(regmatch, item, (colnr_T)0))
8043 continue;
8044
8045 char_u *buf = escape_option_str_cmdline(item);
8046 if (buf == NULL)
8047 {
8048 vim_free(option_copy);
8049 ga_clear_strings(&ga);
8050 return FAIL;
8051 }
8052 if (ga_add_string(&ga, buf) != OK)
8053 {
8054 vim_free(buf);
8055 break;
8056 }
8057 } while (next_val != NULL);
8058
8059 vim_free(option_copy);
8060
8061 *matches = ga.ga_data;
8062 *numMatches = ga.ga_len;
8063 return OK;
8064 }
8065 else if (option_flags & P_FLAGLIST)
8066 {
8067 // Only present the flags that are set on the option as the other flags
8068 // are not meaningful to do set-= on.
8069
8070 if (*xp->xp_pattern != NUL)
8071 {
8072 // Don't suggest anything if cmdline is non-empty. Vim's set-=
8073 // behavior requires consecutive strings and it's usually
Viktor Szépedbf749b2023-10-16 09:53:37 +02008074 // unintuitive to users if they try to subtract multiple flags at
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008075 // once.
8076 return FAIL;
8077 }
8078
Yee Cheng Chin6d113472023-10-02 21:38:39 +02008079 size_t num_flags = STRLEN(option_val);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008080 if (num_flags == 0)
8081 return FAIL;
8082
8083 *matches = ALLOC_MULT(char_u *, num_flags + 1);
8084 if (*matches == NULL)
8085 return FAIL;
8086
8087 int count = 0;
8088 char_u *p;
8089
8090 p = vim_strsave(option_val);
8091 if (p == NULL)
8092 {
8093 VIM_CLEAR(*matches);
8094 return FAIL;
8095 }
8096 (*matches)[count++] = p;
8097
8098 if (num_flags > 1)
8099 {
8100 // If more than one flags, split the flags up and expose each
8101 // character as individual choice.
8102 for (char_u *flag = option_val; *flag != NUL; flag++)
8103 {
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02008104 p = vim_strnsave(flag, 1);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008105 if (p == NULL)
8106 break;
8107 (*matches)[count++] = p;
8108 }
8109 }
8110
8111 *numMatches = count;
8112 return OK;
8113 }
8114
8115 return ExpandOldSetting(numMatches, matches);
8116}
8117
8118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 * Get the value for the numeric or string option *opp in a nice format into
8120 * NameBuff[]. Must not be called with a hidden option!
8121 */
8122 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008123option_value2string(
8124 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008125 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126{
8127 char_u *varp;
8128
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008129 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130
8131 if (opp->flags & P_NUM)
8132 {
8133 long wc = 0;
8134
8135 if (wc_use_keyname(varp, &wc))
8136 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
8137 else if (wc != 0)
8138 STRCPY(NameBuff, transchar((int)wc));
8139 else
8140 sprintf((char *)NameBuff, "%ld", *(long *)varp);
8141 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008142 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 {
8144 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008145 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 NameBuff[0] = NUL;
8147#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008148 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008149 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 STRCPY(NameBuff, "*****");
8151#endif
8152 else if (opp->flags & P_EXPAND)
8153 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008154 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 else if ((char_u **)opp->var == &p_pt)
8156 str2specialbuf(p_pt, NameBuff, MAXPATHL);
8157 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008158 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 }
8160}
8161
8162/*
8163 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
8164 * printed as a keyname.
8165 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
8166 */
8167 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008168wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169{
8170 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
8171 {
8172 *wcp = *(long *)varp;
8173 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
8174 return TRUE;
8175 }
8176 return FALSE;
8177}
8178
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 * Return TRUE if "x" is present in 'shortmess' option, or
8181 * 'shortmess' contains 'a' and "x" is present in SHM_A.
8182 */
8183 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008184shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01008186 return p_shm != NULL &&
8187 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 || (vim_strchr(p_shm, 'a') != NULL
8189 && vim_strchr((char_u *)SHM_A, x) != NULL));
8190}
8191
8192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
8194 *
8195 * Reset 'compatible' and set the values for options that didn't get set yet
8196 * to the Vim defaults.
8197 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008198 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 */
8200 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008201vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008203 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00008204 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008205 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206
8207 if (!option_was_set((char_u *)"cp"))
8208 {
8209 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02008210 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
8212 set_option_default(opt_idx, OPT_FREE, FALSE);
8213 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008214 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008216
8217 if (fname != NULL)
8218 {
8219 p = vim_getenv(envname, &dofree);
8220 if (p == NULL)
8221 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008222 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008223 p = FullName_save(fname, FALSE);
8224 if (p != NULL)
8225 {
8226 vim_setenv(envname, p);
8227 vim_free(p);
8228 }
8229 }
8230 else if (dofree)
8231 vim_free(p);
8232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233}
8234
8235/*
8236 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
8237 */
8238 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008239change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008241 int opt_idx;
8242
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 if (p_cp != on)
8244 {
8245 p_cp = on;
8246 compatible_set();
8247 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008248 opt_idx = findoption((char_u *)"cp");
8249 if (opt_idx >= 0)
8250 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251}
8252
8253/*
8254 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02008255 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 */
8257 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008258option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259{
8260 int idx;
8261
8262 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008263 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 return FALSE;
8265 if (options[idx].flags & P_WAS_SET)
8266 return TRUE;
8267 return FALSE;
8268}
8269
8270/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008271 * Reset the flag indicating option "name" was set.
8272 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02008273 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008274reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008275{
8276 int idx = findoption(name);
8277
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008278 if (idx < 0)
8279 return FAIL;
8280
8281 options[idx].flags &= ~P_WAS_SET;
8282 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008283}
8284
8285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 * compatible_set() - Called when 'compatible' has been set or unset.
8287 *
8288 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
8289 * flag) to a Vi compatible value.
8290 * When 'compatible' is unset: Set all options that have a different default
8291 * for Vim (without the P_VI_DEF flag) to that default.
8292 */
8293 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008294compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295{
8296 int opt_idx;
8297
Bram Moolenaardac13472019-09-16 21:06:21 +02008298 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
8300 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
8301 set_option_default(opt_idx, OPT_FREE, p_cp);
8302 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008303 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304}
8305
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 * Check if backspacing over something is allowed.
8308 */
8309 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008310can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008311 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02008313#ifdef FEAT_JOB_CHANNEL
8314 if (what == BS_START && bt_prompt(curbuf))
8315 return FALSE;
8316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 switch (*p_bs)
8318 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008319 case '3': return TRUE;
8320 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 case '1': return (what != BS_START);
8322 case '0': return FALSE;
8323 }
8324 return vim_strchr(p_bs, what) != NULL;
8325}
8326
8327/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01008328 * Return the effective 'scrolloff' value for the current window, using the
8329 * global value when appropriate.
8330 */
8331 long
8332get_scrolloff_value(void)
8333{
8334 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
8335}
8336
8337/*
8338 * Return the effective 'sidescrolloff' value for the current window, using the
8339 * global value when appropriate.
8340 */
8341 long
8342get_sidescrolloff_value(void)
8343{
8344 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
8345}
8346
8347/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008348 * Get the local or global value of 'backupcopy'.
8349 */
8350 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008351get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008352{
8353 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
8354}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02008355
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008356#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02008357/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00008358 * Get the local or global value of 'formatlistpat'.
8359 */
8360 char_u *
8361get_flp_value(buf_T *buf)
8362{
Christian Brabandtc53b4672022-01-15 10:01:05 +00008363 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
8364 return p_flp;
8365 return buf->b_p_flp;
8366}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008367#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00008368
8369/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02008370 * Get the local or global value of the 'virtualedit' flags.
8371 */
8372 unsigned int
8373get_ve_flags(void)
8374{
Gary Johnson51ad8502021-08-03 18:33:08 +02008375 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
8376 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02008377}
8378
Bram Moolenaaree857022019-11-09 23:26:40 +01008379#if defined(FEAT_LINEBREAK) || defined(PROTO)
8380/*
8381 * Get the local or global value of 'showbreak'.
8382 */
8383 char_u *
8384get_showbreak_value(win_T *win)
8385{
8386 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8387 return p_sbr;
8388 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8389 return empty_option;
8390 return win->w_p_sbr;
8391}
8392#endif
8393
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008394#if defined(FEAT_EVAL) || defined(PROTO)
8395/*
8396 * Get window or buffer local options.
8397 */
8398 dict_T *
8399get_winbuf_options(int bufopt)
8400{
8401 dict_T *d;
8402 int opt_idx;
8403
8404 d = dict_alloc();
8405 if (d == NULL)
8406 return NULL;
8407
Bram Moolenaardac13472019-09-16 21:06:21 +02008408 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008409 {
8410 struct vimoption *opt = &options[opt_idx];
8411
8412 if ((bufopt && (opt->indir & PV_BUF))
8413 || (!bufopt && (opt->indir & PV_WIN)))
8414 {
8415 char_u *varp = get_varp(opt);
8416
8417 if (varp != NULL)
8418 {
8419 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008420 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008421 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008422 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008423 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008424 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008425 }
8426 }
8427 }
8428
8429 return d;
8430}
8431#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008432
Bram Moolenaardac13472019-09-16 21:06:21 +02008433#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008434/*
8435 * This is called when 'culopt' is changed
8436 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008437 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008438fill_culopt_flags(char_u *val, win_T *wp)
8439{
8440 char_u *p;
8441 char_u culopt_flags_new = 0;
8442
8443 if (val == NULL)
8444 p = wp->w_p_culopt;
8445 else
8446 p = val;
8447 while (*p != NUL)
8448 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008449 // Note: Keep this in sync with p_culopt_values.
Bram Moolenaar017ba072019-09-14 21:01:23 +02008450 if (STRNCMP(p, "line", 4) == 0)
8451 {
8452 p += 4;
8453 culopt_flags_new |= CULOPT_LINE;
8454 }
8455 else if (STRNCMP(p, "both", 4) == 0)
8456 {
8457 p += 4;
8458 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8459 }
8460 else if (STRNCMP(p, "number", 6) == 0)
8461 {
8462 p += 6;
8463 culopt_flags_new |= CULOPT_NBR;
8464 }
8465 else if (STRNCMP(p, "screenline", 10) == 0)
8466 {
8467 p += 10;
8468 culopt_flags_new |= CULOPT_SCRLINE;
8469 }
8470
8471 if (*p != ',' && *p != NUL)
8472 return FAIL;
8473 if (*p == ',')
8474 ++p;
8475 }
8476
8477 // Can't have both "line" and "screenline".
8478 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8479 return FAIL;
8480 wp->w_p_culopt_flags = culopt_flags_new;
8481
8482 return OK;
8483}
8484#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008485
8486/*
8487 * Get the value of 'magic' adjusted for Vim9 script.
8488 */
8489 int
8490magic_isset(void)
8491{
8492 switch (magic_overruled)
8493 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008494 case OPTION_MAGIC_ON: return TRUE;
8495 case OPTION_MAGIC_OFF: return FALSE;
8496 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008497 }
8498#ifdef FEAT_EVAL
8499 if (in_vim9script())
8500 return TRUE;
8501#endif
8502 return p_magic;
8503}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008504
8505/*
8506 * Set the callback function value for an option that accepts a function name,
8507 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8508 * Returns OK if the option is successfully set to a function, otherwise
8509 * returns FAIL.
8510 */
8511 int
8512option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8513{
8514#ifdef FEAT_EVAL
8515 typval_T *tv;
8516 callback_T cb;
8517
8518 if (optval == NULL || *optval == NUL)
8519 {
8520 free_callback(optcb);
8521 return OK;
8522 }
8523
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008524 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008525 || (STRNCMP(optval, "function(", 9) == 0)
8526 || (STRNCMP(optval, "funcref(", 8) == 0))
8527 // Lambda expression or a funcref
8528 tv = eval_expr(optval, NULL);
8529 else
8530 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008531 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008532 if (tv == NULL)
8533 return FAIL;
8534
8535 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008536 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008537 {
8538 free_tv(tv);
8539 return FAIL;
8540 }
8541
8542 free_callback(optcb);
8543 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008544 if (cb.cb_free_name)
8545 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008546 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008547
8548 // when using Vim9 style "import.funcname" it needs to be expanded to
8549 // "import#funcname".
8550 expand_autload_callback(optcb);
8551
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008552 return OK;
8553#else
8554 return FAIL;
8555#endif
8556}
Christian Brabandt757593c2023-08-22 21:44:10 +02008557
8558#if defined(FEAT_EVAL) || defined(PROTO)
8559 static void
8560didset_options_sctx(int opt_flags, char **buf)
8561{
8562 for (int i = 0; ; ++i)
8563 {
8564 if (buf[i] == NULL)
8565 break;
8566
8567 int idx = findoption((char_u *)buf[i]);
8568 if (idx >= 0)
8569 set_option_sctx_idx(idx, opt_flags, current_sctx);
8570 }
8571}
8572#endif