blob: af07664df819101704879362b1140ae7cc5cbc7f [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);
John Marriott95dacbb2024-09-10 21:25:14 +020044static char_u *find_dup_item(char_u *origval, char_u *newval, size_t newvallen, 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;
John Marriott95dacbb2024-09-10 21:25:14 +0200135 int i;
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000136 char_u *p;
John Marriott95dacbb2024-09-10 21:25:14 +0200137 int plen;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100138#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000139 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100140#else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000141 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100142#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000143 garray_T ga;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200144
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000145 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000147 ga_init2(&ga, 1, 100);
John Marriott95dacbb2024-09-10 21:25:14 +0200148 for (i = 0; i < (int)ARRAY_LENGTH(names); ++i)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000149 {
Bram Moolenaar14338022023-03-15 22:05:44 +0000150 int mustfree = FALSE;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100151#ifdef UNIX
John Marriott95dacbb2024-09-10 21:25:14 +0200152 if (*names[i] == NUL)
153 {
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100154# ifdef MACOS_X
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000155 p = (char_u *)"/private/tmp";
John Marriott95dacbb2024-09-10 21:25:14 +0200156 plen = (int)STRLEN_LITERAL("/private/tmp");
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100157# else
John Marriott95dacbb2024-09-10 21:25:14 +0200158 p = (char_u *)"/tmp";
159 plen = (int)STRLEN_LITERAL("/tmp");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160# endif
John Marriott95dacbb2024-09-10 21:25:14 +0200161 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000162 else
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100163#endif
John Marriott95dacbb2024-09-10 21:25:14 +0200164 {
165 p = vim_getenv((char_u *)names[i], &mustfree);
166 plen = 0; // will be calculated below
167 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000168 if (p != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 {
John Marriott95dacbb2024-09-10 21:25:14 +0200170 char_u *item;
171 size_t itemsize;
172 int has_trailing_path_sep = FALSE;
173
174 if (plen == 0)
175 {
176 // the value was retrieved from the environment
177 plen = (int)STRLEN(p);
178 // does the value include a trailing path separator?
179 if (after_pathsep(p, p + plen))
180 has_trailing_path_sep = TRUE;
181 }
182
183 // item size needs to be large enough to include "/*" and a trailing NUL
184 // note: the value (and therefore plen) may already include a path separator
185 itemsize = plen + (has_trailing_path_sep ? 0 : 1) + 2;
186 item = alloc(itemsize);
Bram Moolenaar14338022023-03-15 22:05:44 +0000187 if (item != NULL)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000188 {
zeertzjq8feed3a2024-09-29 10:37:47 +0200189 // add a preceding comma as a separator after the first item
John Marriott95dacbb2024-09-10 21:25:14 +0200190 size_t itemseplen = (ga.ga_len == 0) ? 0 : 1;
191 size_t itemlen;
192
193 itemlen = vim_snprintf((char *)item, itemsize, "%s%s*", p, (has_trailing_path_sep) ? "" : PATHSEPSTR);
194
195 if (find_dup_item(ga.ga_data, item, itemlen, options[opt_idx].flags) == NULL
Yegappan Lakshmanan084529c2024-12-24 09:50:01 +0100196 && ga_grow(&ga, (int)(itemseplen + itemlen + 1)) == OK)
Bram Moolenaar14338022023-03-15 22:05:44 +0000197 {
John Marriott95dacbb2024-09-10 21:25:14 +0200198 ga.ga_len += vim_snprintf((char *)ga.ga_data + ga.ga_len,
199 itemseplen + itemlen + 1,
200 "%s%s", (itemseplen > 0) ? "," : "", item);
Bram Moolenaar14338022023-03-15 22:05:44 +0000201 }
202 vim_free(item);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000205 if (mustfree)
206 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000208 if (ga.ga_data != NULL)
209 {
210 set_string_default("bsk", ga.ga_data);
211 vim_free(ga.ga_data);
212 }
213}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000215/*
216 * Initialize the 'maxmemtot' and 'maxmem' options to a default value.
217 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory.
218 */
219 static void
220set_init_default_maxmemtot(void)
221{
222 int opt_idx;
223 long_u n;
224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 opt_idx = findoption((char_u *)"maxmemtot");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000226 if (opt_idx < 0)
227 return;
228
229#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
230 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 {
ichizok02560422022-04-05 14:18:44 +0100233#if defined(HAVE_AVAIL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000234 // Use amount of memory available at this moment.
235 n = (mch_avail_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100236#elif defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000237 // Use amount of memory available to Vim.
238 n = (mch_total_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100239#else
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000240 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000242 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
243 opt_idx = findoption((char_u *)"maxmem");
244 if (opt_idx >= 0)
245 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000246#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000247 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
248 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000249#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000250 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000253}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000255/*
256 * Initialize the 'cdpath' option to a default value.
257 */
258 static void
259set_init_default_cdpath(void)
260{
261 int opt_idx;
262 char_u *cdpath;
263 char_u *buf;
264 int i;
265 int j;
266 int mustfree = FALSE;
267
268 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
269 if (cdpath == NULL)
270 return;
271
272 buf = alloc((STRLEN(cdpath) << 1) + 2);
273 if (buf != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000275 buf[0] = ','; // start with ",", current dir first
276 j = 1;
277 for (i = 0; cdpath[i] != NUL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000279 if (vim_ispathlistsep(cdpath[i]))
280 buf[j++] = ',';
281 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000283 if (cdpath[i] == ' ' || cdpath[i] == ',')
284 buf[j++] = '\\';
285 buf[j++] = cdpath[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 }
287 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000288 buf[j] = NUL;
289 opt_idx = findoption((char_u *)"cdpath");
290 if (opt_idx >= 0)
291 {
292 options[opt_idx].def_val[VI_DEFAULT] = buf;
293 options[opt_idx].flags |= P_DEF_ALLOCED;
294 }
295 else
296 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000298 if (mustfree)
299 vim_free(cdpath);
300}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000302/*
303 * Initialize the 'printencoding' option to a default value.
304 */
305 static void
306set_init_default_printencoding(void)
307{
ichizok02560422022-04-05 14:18:44 +0100308#if defined(FEAT_POSTSCRIPT) && \
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000309 (defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100310 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100312# if defined(MSWIN)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000313 (char_u *)"cp1252"
ichizok02560422022-04-05 14:18:44 +0100314# elif defined(VMS)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000315 (char_u *)"dec-mcs"
ichizok02560422022-04-05 14:18:44 +0100316# elif defined(MAC)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000317 (char_u *)"mac-roman"
ichizok02560422022-04-05 14:18:44 +0100318# else // HPUX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000319 (char_u *)"hp-roman8"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000321 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000323}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324
325#ifdef FEAT_POSTSCRIPT
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000326/*
327 * Initialize the 'printexpr' option to a default value.
328 */
329 static void
330set_init_default_printexpr(void)
331{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100332 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100334# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000335 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
ichizok02560422022-04-05 14:18:44 +0100336# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
338
ichizok02560422022-04-05 14:18:44 +0100339# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341# endif
342 );
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000343}
344#endif
345
346#ifdef UNIX
347/*
348 * Force restricted-mode on for "nologin" or "false" $SHELL
349 */
350 static void
351set_init_restricted_mode(void)
352{
353 char_u *p;
354
355 p = get_isolated_shell_name();
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000356 if (p == NULL)
357 return;
358 if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
359 restricted = TRUE;
360 vim_free(p);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000361}
362#endif
363
364#ifdef CLEAN_RUNTIMEPATH
365/*
366 * When Vim is started with the "--clean" argument, set the default value
367 * for the 'runtimepath' and 'packpath' options.
368 */
369 static void
370set_init_clean_rtp(void)
371{
372 int opt_idx;
373
374 opt_idx = findoption((char_u *)"runtimepath");
375 if (opt_idx >= 0)
376 {
377 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
378 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
379 }
380 opt_idx = findoption((char_u *)"packpath");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000381 if (opt_idx < 0)
382 return;
383 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
384 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000385}
386#endif
387
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200388#ifdef UNIX
389/*
390 * Change 'runtimepath' and 'packdir' to '$XDG_CONFIG_HOME/vim' if the only
391 * vimrc found is located in '$XDG_CONFIG_HOME/vim/vimrc'.
392 * In case the '$XDG_CONFIG_HOME' variable is not set, '$HOME/.config' is used
393 * as a fallback as is defined in the XDG base dir specification:
394 * <https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html>
395 */
396 static void
397set_init_xdg_rtp(void)
398{
399 int opt_idx;
400 int has_xdg_env = TRUE;
401 int should_free_xdg_dir = FALSE;
402 char_u *vimrc1 = NULL;
403 char_u *vimrc2 = NULL;
404 char_u *xdg_dir = NULL;
405 char_u *xdg_rtp = NULL;
406 char_u *vimrc_xdg = NULL;
407
Christian Brabandtb09fa352024-04-21 14:52:20 +0200408 // initialize chartab, so we can expand $HOME
409 (void)init_chartab();
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200410 vimrc1 = expand_env_save((char_u *)USR_VIMRC_FILE);
411 vimrc2 = expand_env_save((char_u *)USR_VIMRC_FILE2);
412
413 xdg_dir = mch_getenv("XDG_CONFIG_HOME");
414 if (!xdg_dir)
415 {
416 xdg_dir = expand_env_save((char_u *)"~/.config");
417 should_free_xdg_dir = TRUE;
418 has_xdg_env = FALSE;
419 }
420 vimrc_xdg = concat_fnames(xdg_dir, (char_u *)"vim/vimrc", TRUE);
421
422 if (file_is_readable(vimrc1) || file_is_readable(vimrc2) ||
423 !file_is_readable(vimrc_xdg))
424 goto theend;
425
426 xdg_rtp = has_xdg_env ? (char_u *)XDG_RUNTIMEPATH
427 : (char_u *)XDG_RUNTIMEPATH_FB;
428
429 if ((opt_idx = findoption((char_u *)"runtimepath")) < 0)
430 goto theend;
431
432 options[opt_idx].def_val[VI_DEFAULT] = xdg_rtp;
433 p_rtp = xdg_rtp;
434
435 if ((opt_idx = findoption((char_u *)"packpath")) < 0)
436 goto theend;
437
438 options[opt_idx].def_val[VI_DEFAULT] = xdg_rtp;
439 p_pp = xdg_rtp;
440
Christian Brabandtc3e6e392024-05-04 09:48:15 +0200441#if defined(XDG_VDIR) && defined(FEAT_SESSION)
442 if ((opt_idx = findoption((char_u *)"viewdir")) < 0)
443 goto theend;
444
445 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)XDG_VDIR;
446 p_vdir = (char_u *)XDG_VDIR;
447#endif
448
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200449theend:
450 vim_free(vimrc1);
451 vim_free(vimrc2);
452 vim_free(vimrc_xdg);
453 if (should_free_xdg_dir)
454 vim_free(xdg_dir);
455}
456#endif
457
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000458/*
459 * Expand environment variables and things like "~" for the defaults.
460 * If option_expand() returns non-NULL the variable is expanded. This can
461 * only happen for non-indirect options.
462 * Also set the default to the expanded value, so ":set" does not list
463 * them.
464 * Don't set the P_ALLOCED flag, because we don't want to free the
465 * default.
466 */
467 static void
468set_init_expand_env(void)
469{
470 int opt_idx;
471 char_u *p;
472
473 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
474 {
475 if ((options[opt_idx].flags & P_GETTEXT)
476 && options[opt_idx].var != NULL)
477 p = (char_u *)_(*(char **)options[opt_idx].var);
478 else
479 p = option_expand(opt_idx, NULL);
480 if (p != NULL && (p = vim_strsave(p)) != NULL)
481 {
482 *(char_u **)options[opt_idx].var = p;
483 // VIMEXP
484 // Defaults for all expanded options are currently the same for Vi
485 // and Vim. When this changes, add some code here! Also need to
486 // split P_DEF_ALLOCED in two.
487 if (options[opt_idx].flags & P_DEF_ALLOCED)
488 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
489 options[opt_idx].def_val[VI_DEFAULT] = p;
490 options[opt_idx].flags |= P_DEF_ALLOCED;
491 }
492 }
493}
494
495/*
496 * Initialize the 'LANG' environment variable to a default value.
497 */
498 static void
499set_init_lang_env(void)
500{
501#if defined(MSWIN) && defined(FEAT_GETTEXT)
502 // If $LANG isn't set, try to get a good value for it. This makes the
503 // right language be used automatically. Don't do this for English.
504 if (mch_getenv((char_u *)"LANG") == NULL)
505 {
506 char buf[20];
507 long_u n;
508
509 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
510 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
511 // only the first two.
512 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
513 (LPTSTR)buf, 20);
514 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
515 {
516 // There are a few exceptions (probably more)
517 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
518 STRCPY(buf, "zh_TW");
519 else if (STRNICMP(buf, "chs", 3) == 0
520 || STRNICMP(buf, "zhc", 3) == 0)
521 STRCPY(buf, "zh_CN");
522 else if (STRNICMP(buf, "jp", 2) == 0)
523 STRCPY(buf, "ja");
524 else
525 buf[2] = NUL; // truncate to two-letter code
526 vim_setenv((char_u *)"LANG", (char_u *)buf);
527 }
528 }
529#elif defined(MACOS_CONVERT)
530 // Moved to os_mac_conv.c to avoid dependency problems.
531 mac_lang_init();
532#endif
533}
534
535/*
536 * Initialize the 'encoding' option to a default value.
537 */
538 static void
539set_init_default_encoding(void)
540{
541 char_u *p;
542 int opt_idx;
543
Igor Todorovski497e5282024-01-12 17:59:18 +0100544# if defined(MSWIN) || defined(__MVS__)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000545 // MS-Windows has builtin support for conversion to and from Unicode, using
546 // "utf-8" for 'encoding' should work best for most users.
Igor Todorovski497e5282024-01-12 17:59:18 +0100547 // z/OS built should default to UTF-8 mode as setlocale does not respect utf-8 environment variable locales
John Marriott95dacbb2024-09-10 21:25:14 +0200548 p = vim_strnsave((char_u *)ENC_DFLT, STRLEN_LITERAL(ENC_DFLT));
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000549# else
550 // enc_locale() will try to find the encoding of the current locale.
551 // This works best for properly configured systems, old and new.
552 p = enc_locale();
553# endif
554 if (p == NULL)
555 return;
556
557 // Try setting 'encoding' and check if the value is valid.
558 // If not, go back to the default encoding.
559 char_u *save_enc = p_enc;
560 p_enc = p;
561 if (STRCMP(p_enc, "gb18030") == 0)
562 {
563 // We don't support "gb18030", but "cp936" is a good substitute
564 // for practical purposes, thus use that. It's not an alias to
565 // still support conversion between gb18030 and utf-8.
John Marriott95dacbb2024-09-10 21:25:14 +0200566 p_enc = vim_strnsave((char_u *)"cp936", STRLEN_LITERAL("cp936"));
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000567 vim_free(p);
568 }
569 if (mb_init() == NULL)
570 {
571 opt_idx = findoption((char_u *)"encoding");
572 if (opt_idx >= 0)
573 {
574 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
575 options[opt_idx].flags |= P_DEF_ALLOCED;
576 }
577
578#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
579 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
580 {
581 // Adjust the default for 'isprint' and 'iskeyword' to match
582 // latin1. Also set the defaults for when 'nocompatible' is
583 // set.
584 set_string_option_direct((char_u *)"isp", -1,
585 ISP_LATIN1, OPT_FREE, SID_NONE);
586 set_string_option_direct((char_u *)"isk", -1,
587 ISK_LATIN1, OPT_FREE, SID_NONE);
588 opt_idx = findoption((char_u *)"isp");
589 if (opt_idx >= 0)
590 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
591 opt_idx = findoption((char_u *)"isk");
592 if (opt_idx >= 0)
593 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
594 (void)init_chartab();
595 }
596#endif
597
598#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
599 // Win32 console: When GetACP() returns a different value from
600 // GetConsoleCP() set 'termencoding'.
601 if (
602# ifdef VIMDLL
603 (!gui.in_use && !gui.starting) &&
604# endif
605 GetACP() != GetConsoleCP())
606 {
607 char buf[50];
John Marriott95dacbb2024-09-10 21:25:14 +0200608 size_t buflen;
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000609
610 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
611 // Use an alternative value.
612 if (GetConsoleCP() == 0)
John Marriott95dacbb2024-09-10 21:25:14 +0200613 buflen = vim_snprintf(buf, sizeof(buf), "cp%ld", (long)GetACP());
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000614 else
John Marriott95dacbb2024-09-10 21:25:14 +0200615 buflen = vim_snprintf(buf, sizeof(buf), "cp%ld", (long)GetConsoleCP());
616 p_tenc = vim_strnsave((char_u *)buf, buflen);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000617 if (p_tenc != NULL)
618 {
619 opt_idx = findoption((char_u *)"termencoding");
620 if (opt_idx >= 0)
621 {
622 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
623 options[opt_idx].flags |= P_DEF_ALLOCED;
624 }
625 convert_setup(&input_conv, p_tenc, p_enc);
626 convert_setup(&output_conv, p_enc, p_tenc);
627 }
628 else
629 p_tenc = empty_option;
630 }
631#endif
632#if defined(MSWIN)
633 // $HOME may have characters in active code page.
634 init_homedir();
635#endif
636 }
637 else
638 {
639 vim_free(p_enc);
640 p_enc = save_enc;
641 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000642}
643
644/*
645 * Initialize the options, first part.
646 *
647 * Called only once from main(), just after creating the first buffer.
648 * If "clean_arg" is TRUE Vim was started with --clean.
649 */
650 void
651set_init_1(int clean_arg)
652{
653#ifdef FEAT_LANGMAP
654 langmap_init();
655#endif
656
657 // Be Vi compatible by default
658 p_cp = TRUE;
659
660 // Use POSIX compatibility when $VIM_POSIX is set.
661 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
662 {
663 set_string_default("cpo", (char_u *)CPO_ALL);
664 set_string_default("shm", (char_u *)SHM_POSIX);
665 }
666
667 set_init_default_shell();
668 set_init_default_backupskip();
669 set_init_default_maxmemtot();
670 set_init_default_cdpath();
671 set_init_default_printencoding();
672#ifdef FEAT_POSTSCRIPT
673 set_init_default_printexpr();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674#endif
675
676 /*
677 * Set all the options (except the terminal options) to their default
678 * value. Also set the global value for local options.
679 */
680 set_options_default(0);
681
matveytadbb1bf2022-02-01 17:26:12 +0000682#ifdef UNIX
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200683 set_init_xdg_rtp();
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000684 set_init_restricted_mode();
matveytadbb1bf2022-02-01 17:26:12 +0000685#endif
686
Bram Moolenaar07268702018-03-01 21:57:32 +0100687#ifdef CLEAN_RUNTIMEPATH
688 if (clean_arg)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000689 set_init_clean_rtp();
Bram Moolenaar07268702018-03-01 21:57:32 +0100690#endif
691
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692#ifdef FEAT_GUI
693 if (found_reverse_arg)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100694 set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695#endif
696
697 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100698 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100699 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 check_buf_options(curbuf);
701 check_win_options(curwin);
702 check_options();
703
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100704 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 didset_options();
706
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000707#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100708 // Use the current chartab for the generic chartab. This is not in
709 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000710 init_spell_chartab();
711#endif
712
K.Takatace3189d2023-02-15 19:13:43 +0000713 set_init_default_encoding();
714
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000715 // Expand environment variables and things like "~" for the defaults.
716 set_init_expand_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100718 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100721 // Detect use of mlterm.
722 // Mlterm is a terminal emulator akin to xterm that has some special
723 // abilities (bidi namely).
724 // NOTE: mlterm's author is being asked to 'set' a variable
725 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 if (mch_getenv((char_u *)"MLTERM") != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100727 set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728#endif
729
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200730 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000732 set_init_lang_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733
734#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100735 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 set_helplang_default(get_mess_lang());
737#endif
738}
739
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200740static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
741
742/*
743 * Set the "fileencodings" option to the default value for when 'encoding' is
744 * utf-8.
745 */
746 void
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +0000747set_fencs_unicode(void)
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200748{
749 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
750 OPT_FREE, 0);
751}
752
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753/*
754 * Set an option to its default value.
755 * This does not take care of side effects!
756 */
757 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100758set_option_default(
759 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100760 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
761 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100763 char_u *varp; // pointer to variable for current option
764 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000766 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
768
769 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
770 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100771 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 {
773 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
774 if (flags & P_STRING)
775 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200776 // 'fencs' default value depends on 'encoding'
777 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
778 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100779 // Use set_string_option_direct() for local options to handle
780 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200781 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200782 set_string_option_direct(NULL, opt_idx,
783 options[opt_idx].def_val[dvi], opt_flags, 0);
784 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200786 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
787 free_string_option(*(char_u **)(varp));
788 *(char_u **)varp = options[opt_idx].def_val[dvi];
789 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 }
791 }
792 else if (flags & P_NUM)
793 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000794 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 win_comp_scroll(curwin);
796 else
797 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100798 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
799
800 if ((long *)varp == &curwin->w_p_so
801 || (long *)varp == &curwin->w_p_siso)
802 // 'scrolloff' and 'sidescrolloff' local values have a
803 // different default value than the global default.
804 *(long *)varp = -1;
805 else
806 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100807 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 if (both)
809 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100810 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 }
812 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100813 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100815 // the cast to long is required for Manx C, long_i is needed for
816 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000817 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000818#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100819 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000820 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
821 *(int *)varp = FALSE;
822#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100823 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 if (both)
825 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
826 *(int *)varp;
827 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000828
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100829 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000830 flagsp = insecure_flag(opt_idx, opt_flags);
831 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 }
833
834#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200835 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836#endif
837}
838
839/*
840 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200841 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 */
843 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100844set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100845 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846{
847 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000849 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850
Bram Moolenaardac13472019-09-16 21:06:21 +0200851 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200852 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200853 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100854 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200855# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200856 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200857 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200858# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100859 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 set_option_default(i, opt_flags, p_cp);
861
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100862 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000863 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200865 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866}
867
868/*
869 * Set the Vi-default value of a string option.
870 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100871 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100873 static void
874set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875{
876 char_u *p;
877 int opt_idx;
878
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100879 if (escape && vim_strchr(val, ' ') != NULL)
880 p = vim_strsave_escaped(val, (char_u *)" ");
881 else
882 p = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000883 if (p == NULL) // we don't want a NULL
884 return;
885
886 opt_idx = findoption((char_u *)name);
887 if (opt_idx < 0)
Christian Brabandt29269a72024-04-16 22:44:31 +0200888 {
889 vim_free(p);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000890 return;
Christian Brabandt29269a72024-04-16 22:44:31 +0200891 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000892
893 if (options[opt_idx].flags & P_DEF_ALLOCED)
894 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
895 options[opt_idx].def_val[VI_DEFAULT] = p;
896 options[opt_idx].flags |= P_DEF_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897}
898
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100899 void
900set_string_default(char *name, char_u *val)
901{
902 set_string_default_esc(name, val, FALSE);
903}
904
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200906 * For an option value that contains comma separated items, find "newval" in
907 * "origval". Return NULL if not found.
908 */
909 static char_u *
John Marriott95dacbb2024-09-10 21:25:14 +0200910find_dup_item(char_u *origval, char_u *newval, size_t newvallen, long_u flags)
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200911{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200912 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200913 char_u *s;
914
915 if (origval == NULL)
916 return NULL;
917
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200918 for (s = origval; *s != NUL; ++s)
919 {
920 if ((!(flags & P_COMMA)
921 || s == origval
922 || (s[-1] == ',' && !(bs & 1)))
John Marriott95dacbb2024-09-10 21:25:14 +0200923 && STRNCMP(s, newval, newvallen) == 0
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200924 && (!(flags & P_COMMA)
John Marriott95dacbb2024-09-10 21:25:14 +0200925 || s[newvallen] == ','
926 || s[newvallen] == NUL))
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200927 return s;
928 // Count backslashes. Only a comma with an even number of backslashes
929 // or a single backslash preceded by a comma before it is recognized as
930 // a separator.
931 if ((s > origval + 1
932 && s[-1] == '\\'
933 && s[-2] != ',')
934 || (s == origval + 1
935 && s[-1] == '\\'))
936 ++bs;
937 else
938 bs = 0;
939 }
940 return NULL;
941}
942
943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 * Set the Vi-default value of a number option.
945 * Used for 'lines' and 'columns'.
946 */
947 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100948set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000950 int opt_idx;
951
952 opt_idx = findoption((char_u *)name);
953 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000954 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955}
956
Dominique Pelle748b3082022-01-08 12:41:16 +0000957#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200958/*
959 * Set all window-local and buffer-local options to the Vim default.
960 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200961 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200962 */
963 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200964set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200965{
966 win_T *save_curwin = curwin;
967 int i;
968
969 curwin = wp;
970 curbuf = curwin->w_buffer;
971 block_autocmds();
972
Bram Moolenaardac13472019-09-16 21:06:21 +0200973 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200974 {
975 struct vimoption *p = &(options[i]);
976 char_u *varp = get_varp_scope(p, OPT_LOCAL);
977
978 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200979 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200980 && !(options[i].flags & P_NODEFAULT)
981 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200982 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200983 }
984
985 unblock_autocmds();
986 curwin = save_curwin;
987 curbuf = curwin->w_buffer;
988}
Dominique Pelle748b3082022-01-08 12:41:16 +0000989#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200990
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000991#if defined(EXITFREE) || defined(PROTO)
992/*
993 * Free all options.
994 */
995 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100996free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000997{
998 int i;
999
Bram Moolenaardac13472019-09-16 21:06:21 +02001000 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001001 {
1002 if (options[i].indir == PV_NONE)
1003 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001004 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +01001005 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001006 free_string_option(*(char_u **)options[i].var);
1007 if (options[i].flags & P_DEF_ALLOCED)
1008 free_string_option(options[i].def_val[VI_DEFAULT]);
1009 }
1010 else if (options[i].var != VAR_WIN
1011 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001012 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +02001013 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001014 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00001015 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00001016 free_tagfunc_option();
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01001017# if defined(FEAT_EVAL)
1018 free_findfunc_option();
1019# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001020}
1021#endif
1022
1023
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024/*
1025 * Initialize the options, part two: After getting Rows and Columns and
1026 * setting 'term'.
1027 */
1028 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001029set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001031 int idx;
1032
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001033 // 'scroll' defaults to half the window height. The stored default is zero,
1034 // which results in the actual value computed from the window height.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001035 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001036 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001037 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 comp_col();
1039
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001040 // 'window' is only for backwards compatibility with Vi.
1041 // Default is Rows - 1.
Bram Moolenaard68071d2006-05-02 22:08:30 +00001042 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001043 p_window = Rows - 1;
1044 set_number_default("window", Rows - 1);
1045
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001046 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +01001047#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001048 // If 'background' wasn't set by the user, try guessing the value,
1049 // depending on the terminal name. Only need to check for terminals
1050 // with a dark background, that can handle color.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001051 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001052 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
1053 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001055 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001056 // don't mark it as set, when starting the GUI it may be
1057 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +00001058 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 }
1060#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001061
1062#ifdef CURSOR_SHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001063 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +00001064#endif
1065#ifdef FEAT_MOUSESHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001066 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +00001067#endif
1068#ifdef FEAT_PRINTER
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001069 (void)parse_printoptions(NULL); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +00001070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071}
1072
1073/*
1074 * Initialize the options, part three: After reading the .vimrc
1075 */
1076 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001077set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078{
Bram Moolenaar4f974752019-02-17 17:44:42 +01001079#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080/*
1081 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
1082 * This is done after other initializations, where 'shell' might have been
1083 * set, but only if they have not been set before.
1084 */
1085 char_u *p;
1086 int idx_srr;
1087 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001088# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 int idx_sp;
1090 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001091# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092
1093 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001094 if (idx_srr < 0)
1095 do_srr = FALSE;
1096 else
1097 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001098# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001100 if (idx_sp < 0)
1101 do_sp = FALSE;
1102 else
1103 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001104# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001105 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 if (p != NULL)
1107 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001108 // Default for p_sp is "| tee", for p_srr is ">".
1109 // For known shells it is changed here to include stderr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 if ( fnamecmp(p, "csh") == 0
1111 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001112# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 || fnamecmp(p, "csh.exe") == 0
1114 || fnamecmp(p, "tcsh.exe") == 0
1115# endif
1116 )
1117 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001118# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 if (do_sp)
1120 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001121# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001123# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1127 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 if (do_srr)
1130 {
1131 p_srr = (char_u *)">&";
1132 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1133 }
1134 }
Mike Williams12795022021-06-28 20:53:58 +02001135# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +02001136 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
1137 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +02001138 else if ( fnamecmp(p, "powershell") == 0
1139 || fnamecmp(p, "powershell.exe") == 0
1140 )
1141 {
1142# if defined(FEAT_QUICKFIX)
1143 if (do_sp)
1144 {
1145 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
1146 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1147 }
1148# endif
1149 if (do_srr)
1150 {
1151 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
1152 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1153 }
1154 }
1155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 else
Natanael Copa56318362021-05-06 18:46:35 +02001157 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 if ( fnamecmp(p, "sh") == 0
1159 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001160 || fnamecmp(p, "mksh") == 0
1161 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001163 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001165 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001166 || fnamecmp(p, "ash") == 0
1167 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001168 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001169# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 || fnamecmp(p, "cmd") == 0
1171 || fnamecmp(p, "sh.exe") == 0
1172 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001173 || fnamecmp(p, "mksh.exe") == 0
1174 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001176 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 || fnamecmp(p, "bash.exe") == 0
1178 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001179 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001180 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001182 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001184# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 if (do_sp)
1186 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001187# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001189# else
Mike Williamsa3d1b292021-06-30 20:56:00 +02001190 if (fnamecmp(p, "pwsh") == 0)
1191 p_sp = (char_u *)">%s 2>&1";
1192 else
1193 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001194# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1196 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001197# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 if (do_srr)
1199 {
1200 p_srr = (char_u *)">%s 2>&1";
1201 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1202 }
1203 }
1204 vim_free(p);
1205 }
1206#endif
1207
Bram Moolenaar4f974752019-02-17 17:44:42 +01001208#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001210 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1211 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001213 * set, but only if they have not been set before.
1214 * Default values depend on shell (cmd.exe is default shell):
1215 *
1216 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001217 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001218 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001219 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001220 * "sh" like shells - "-c" "\""
1221 *
1222 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 */
Mike Williams12795022021-06-28 20:53:58 +02001224 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1225 {
1226 int idx_opt;
1227
1228 idx_opt = findoption((char_u *)"shcf");
1229 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1230 {
1231 p_shcf = (char_u*)"-Command";
1232 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1233 }
1234
1235 idx_opt = findoption((char_u *)"sxq");
1236 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1237 {
1238 p_sxq = (char_u*)"\"";
1239 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1240 }
1241 }
1242 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 {
1244 int idx3;
1245
1246 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001247 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 {
1249 p_shcf = (char_u *)"-c";
1250 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1251 }
1252
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001253 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001255 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 {
1257 p_sxq = (char_u *)"\"";
1258 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001261 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1262 {
1263 int idx3;
1264
1265 /*
1266 * cmd.exe on Windows will strip the first and last double quote given
1267 * on the command line, e.g. most of the time things like:
1268 * cmd /c "my path/to/echo" "my args to echo"
1269 * become:
1270 * my path/to/echo" "my args to echo
1271 * when executed.
1272 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001273 * To avoid this, set shellxquote to surround the command in
1274 * parenthesis. This appears to make most commands work, without
1275 * breaking commands that worked previously, such as
1276 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001277 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001278 idx3 = findoption((char_u *)"sxq");
1279 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1280 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001281 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001282 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1283 }
1284
Bram Moolenaara64ba222012-02-12 23:23:31 +01001285 idx3 = findoption((char_u *)"shcf");
1286 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1287 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001288 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001289 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1290 }
1291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292#endif
1293
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001294 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001295 {
1296 int idx_ffs = findoption((char_u *)"ffs");
1297
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001298 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001299 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1300 set_fileformat(default_fileformat(), OPT_LOCAL);
1301 }
1302
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304}
1305
1306#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1307/*
1308 * When 'helplang' is still at its default value, set it to "lang".
1309 * Only the first two characters of "lang" are used.
1310 */
1311 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001312set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313{
1314 int idx;
John Marriott95dacbb2024-09-10 21:25:14 +02001315 size_t langlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316
John Marriott95dacbb2024-09-10 21:25:14 +02001317 if (lang == NULL) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 return;
John Marriott95dacbb2024-09-10 21:25:14 +02001319
1320 langlen = STRLEN(lang);
1321 if (langlen < 2) // safety check
1322 return;
1323
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 idx = findoption((char_u *)"hlg");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001325 if (idx < 0 || (options[idx].flags & P_WAS_SET))
1326 return;
1327
1328 if (options[idx].flags & P_ALLOCED)
1329 free_string_option(p_hlg);
John Marriott95dacbb2024-09-10 21:25:14 +02001330 p_hlg = vim_strnsave(lang, langlen);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001331 if (p_hlg == NULL)
1332 p_hlg = empty_option;
1333 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001335 // zh_CN becomes "cn", zh_TW becomes "tw"
John Marriott95dacbb2024-09-10 21:25:14 +02001336 if (STRNICMP(p_hlg, "zh_", 3) == 0 && langlen >= 5)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001337 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001338 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1339 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001340 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001341 // any C like setting, such as C.UTF-8, becomes "en"
John Marriott95dacbb2024-09-10 21:25:14 +02001342 else if (langlen >= 1 && *p_hlg == 'C')
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001343 {
1344 p_hlg[0] = 'e';
1345 p_hlg[1] = 'n';
1346 }
1347 p_hlg[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001349 options[idx].flags |= P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350}
1351#endif
1352
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353/*
1354 * 'title' and 'icon' only default to true if they have not been set or reset
1355 * in .vimrc and we can read the old value.
1356 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1357 * they can be reset. This reduces startup time when using X on a remote
1358 * machine.
1359 */
1360 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001361set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362{
1363 int idx1;
1364 long val;
1365
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001366 // If GUI is (going to be) used, we can always set the window title and
1367 // icon name. Saves a bit of time, because the X11 display server does
1368 // not need to be contacted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001370 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 {
1372#ifdef FEAT_GUI
1373 if (gui.starting || gui.in_use)
1374 val = TRUE;
1375 else
1376#endif
1377 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001378 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 p_title = val;
1380 }
1381 idx1 = findoption((char_u *)"icon");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001382 if (idx1 < 0 || (options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001384 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001386
1387#ifdef FEAT_GUI
1388 if (gui.starting || gui.in_use)
1389 val = TRUE;
1390 else
1391#endif
1392 val = mch_can_restore_icon();
1393 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
1394 p_icon = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001397 void
1398ex_set(exarg_T *eap)
1399{
1400 int flags = 0;
1401
1402 if (eap->cmdidx == CMD_setlocal)
1403 flags = OPT_LOCAL;
1404 else if (eap->cmdidx == CMD_setglobal)
1405 flags = OPT_GLOBAL;
1406#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001407 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001408 ex_options(eap);
1409 else
1410#endif
1411 {
1412 if (eap->forceit)
1413 flags |= OPT_ONECOLUMN;
1414 (void)do_set(eap->arg, flags);
1415 }
1416}
1417
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001418/*
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001419 * :set boolean option prefix
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001420 */
Bram Moolenaar47403942022-09-21 21:12:53 +01001421typedef enum {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001422 PREFIX_NO = 0, // "no" prefix
1423 PREFIX_NONE, // no prefix
1424 PREFIX_INV, // "inv" prefix
1425} set_prefix_T;
1426
1427/*
1428 * Return the prefix type for the option name in *argp.
1429 */
1430 static set_prefix_T
1431get_option_prefix(char_u **argp)
1432{
1433 int prefix = PREFIX_NONE;
1434 char_u *arg = *argp;
1435
1436 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
1437 {
1438 prefix = PREFIX_NO;
1439 arg += 2;
1440 }
1441 else if (STRNCMP(arg, "inv", 3) == 0)
1442 {
1443 prefix = PREFIX_INV;
1444 arg += 3;
1445 }
1446
1447 *argp = arg;
1448 return prefix;
1449}
1450
1451/*
1452 * Parse the option name in "arg" and return the option index in "*opt_idxp",
1453 * and the option name length in "*lenp". For a <t_xx> option, return the key
1454 * number in "*keyp".
1455 *
1456 * Returns FAIL if an option starting with "<" doesn't end with a ">",
1457 * otherwise returns OK.
1458 */
1459 static int
1460parse_option_name(char_u *arg, int *opt_idxp, int *lenp, int *keyp)
1461{
1462 int key = 0;
1463 int len;
1464 int opt_idx;
1465
1466 if (*arg == '<')
1467 {
1468 opt_idx = -1;
1469 // look out for <t_>;>
1470 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1471 len = 5;
1472 else
1473 {
1474 len = 1;
1475 while (arg[len] != NUL && arg[len] != '>')
1476 ++len;
1477 }
1478 if (arg[len] != '>')
1479 return FAIL;
1480
1481 arg[len] = NUL; // put NUL after name
1482 if (arg[1] == 't' && arg[2] == '_') // could be term code
1483 opt_idx = findoption(arg + 1);
1484 arg[len++] = '>'; // restore '>'
1485 if (opt_idx == -1)
1486 key = find_key_option(arg + 1, TRUE);
1487 }
1488 else
1489 {
1490 int nextchar; // next non-white char after option name
1491
1492 len = 0;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001493 // The two characters after "t_" may not be alphanumeric.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001494 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1495 len = 4;
1496 else
1497 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1498 ++len;
1499 nextchar = arg[len];
1500 arg[len] = NUL; // put NUL after name
1501 opt_idx = findoption(arg);
1502 arg[len] = nextchar; // restore nextchar
1503 if (opt_idx == -1)
1504 key = find_key_option(arg, FALSE);
1505 }
1506
1507 *keyp = key;
1508 *lenp = len;
1509 *opt_idxp = opt_idx;
1510
1511 return OK;
1512}
1513
1514/*
1515 * Get the option operator (+=, ^=, -=).
1516 */
1517 static set_op_T
1518get_opt_op(char_u *arg)
1519{
1520 set_op_T op = OP_NONE;
1521
1522 if (*arg != NUL && *(arg + 1) == '=')
1523 {
1524 if (*arg == '+')
1525 op = OP_ADDING; // "+="
1526 else if (*arg == '^')
1527 op = OP_PREPENDING; // "^="
1528 else if (*arg == '-')
1529 op = OP_REMOVING; // "-="
1530 }
1531
1532 return op;
1533}
1534
1535/*
1536 * Validate whether the value of the option in "opt_idx" can be changed.
1537 * Returns FAIL if the option can be skipped or cannot be changed. Returns OK
1538 * if it can be changed.
1539 */
1540 static int
1541validate_opt_idx(int opt_idx, int opt_flags, long_u flags, char **errmsg)
1542{
1543 // Skip all options that are not window-local (used when showing
1544 // an already loaded buffer in a window).
1545 if ((opt_flags & OPT_WINONLY)
1546 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1547 return FAIL;
1548
1549 // Skip all options that are window-local (used for :vimgrep).
1550 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1551 && options[opt_idx].var == VAR_WIN)
1552 return FAIL;
1553
1554 // Disallow changing some options from modelines.
1555 if (opt_flags & OPT_MODELINE)
1556 {
1557 if (flags & (P_SECURE | P_NO_ML))
1558 {
1559 *errmsg = e_not_allowed_in_modeline;
1560 return FAIL;
1561 }
1562 if ((flags & P_MLE) && !p_mle)
1563 {
1564 *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
1565 return FAIL;
1566 }
1567#ifdef FEAT_DIFF
1568 // In diff mode some options are overruled. This avoids that
1569 // 'foldmethod' becomes "marker" instead of "diff" and that
1570 // "wrap" gets set.
1571 if (curwin->w_p_diff
1572 && opt_idx >= 0 // shut up coverity warning
1573 && (
1574# ifdef FEAT_FOLDING
1575 options[opt_idx].indir == PV_FDM ||
1576# endif
1577 options[opt_idx].indir == PV_WRAP))
1578 return FAIL;
1579#endif
1580 }
1581
1582#ifdef HAVE_SANDBOX
1583 // Disallow changing some options in the sandbox
1584 if (sandbox != 0 && (flags & P_SECURE))
1585 {
1586 *errmsg = e_not_allowed_in_sandbox;
1587 return FAIL;
1588 }
1589#endif
1590
1591 return OK;
1592}
1593
Bram Moolenaar47403942022-09-21 21:12:53 +01001594/*
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001595 * Get the Vim/Vi default value for a string option.
1596 */
1597 static char_u *
1598stropt_get_default_val(
1599 int opt_idx,
1600 char_u *varp,
1601 int flags,
1602 int cp_val)
1603{
1604 char_u *newval;
1605
1606 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1607 ? VI_DEFAULT : VIM_DEFAULT];
1608 if ((char_u **)varp == &p_bg)
1609 {
1610 // guess the value of 'background'
1611#ifdef FEAT_GUI
1612 if (gui.in_use)
1613 newval = gui_bg_default();
1614 else
1615#endif
1616 newval = term_bg_default();
1617 }
1618 else if ((char_u **)varp == &p_fencs && enc_utf8)
1619 newval = fencs_utf8_default;
1620
1621 // expand environment variables and ~ since the default value was
1622 // already expanded, only required when an environment variable was set
1623 // later
1624 if (newval == NULL)
1625 newval = empty_option;
1626 else
1627 {
1628 char_u *s = option_expand(opt_idx, newval);
1629 if (s == NULL)
1630 s = newval;
1631 newval = vim_strsave(s);
1632 }
1633
1634 return newval;
1635}
1636
1637/*
1638 * Convert the 'backspace' option number value to a string: for adding,
1639 * prepending and removing string.
1640 */
1641 static void
1642opt_backspace_nr2str(
1643 char_u *varp,
1644 char_u **origval_p,
1645 char_u **origval_l_p,
1646 char_u **origval_g_p,
1647 char_u **oldval_p)
1648{
1649 int i = getdigits((char_u **)varp);
1650
1651 switch (i)
1652 {
1653 case 0:
1654 *(char_u **)varp = empty_option;
1655 break;
1656 case 1:
John Marriott95dacbb2024-09-10 21:25:14 +02001657 *(char_u **)varp = vim_strnsave((char_u *)"indent,eol", STRLEN_LITERAL("indent,eol"));
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001658 break;
1659 case 2:
John Marriott95dacbb2024-09-10 21:25:14 +02001660 *(char_u **)varp = vim_strnsave((char_u *)"indent,eol,start", STRLEN_LITERAL("indent,eol,start"));
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001661 break;
1662 case 3:
John Marriott95dacbb2024-09-10 21:25:14 +02001663 *(char_u **)varp = vim_strnsave((char_u *)"indent,eol,nostop", STRLEN_LITERAL("indent,eol,nostop"));
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001664 break;
1665 }
1666 vim_free(*oldval_p);
1667 if (*origval_p == *oldval_p)
1668 *origval_p = *(char_u **)varp;
1669 if (*origval_l_p == *oldval_p)
1670 *origval_l_p = *(char_u **)varp;
1671 if (*origval_g_p == *oldval_p)
1672 *origval_g_p = *(char_u **)varp;
1673 *oldval_p = *(char_u **)varp;
1674}
1675
1676/*
1677 * Convert the 'whichwrap' option number value to a string, for backwards
1678 * compatibility with Vim 3.0.
1679 * Note: 'argp' is a pointer to a char_u pointer and is updated.
1680 */
1681 static char_u *
1682opt_whichwrap_nr2str(char_u **argp, char_u *whichwrap)
1683{
John Marriott95dacbb2024-09-10 21:25:14 +02001684 size_t len = 0;
1685
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001686 *whichwrap = NUL;
1687 int i = getdigits(argp);
1688 if (i & 1)
John Marriott95dacbb2024-09-10 21:25:14 +02001689 {
1690 STRCPY(whichwrap, "b,");
1691 len += 2;
1692 }
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001693 if (i & 2)
John Marriott95dacbb2024-09-10 21:25:14 +02001694 {
1695 STRCPY(whichwrap + len, "s,");
1696 len += 2;
1697 }
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001698 if (i & 4)
John Marriott95dacbb2024-09-10 21:25:14 +02001699 {
1700 STRCPY(whichwrap + len, "h,l,");
1701 len += 4;
1702 }
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001703 if (i & 8)
John Marriott95dacbb2024-09-10 21:25:14 +02001704 {
1705 STRCPY(whichwrap + len, "<,>,");
1706 len += 4;
1707 }
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001708 if (i & 16)
John Marriott95dacbb2024-09-10 21:25:14 +02001709 {
1710 STRCPY(whichwrap + len, "[,],");
1711 len += 4;
1712 }
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001713 if (*whichwrap != NUL) // remove trailing ,
John Marriott95dacbb2024-09-10 21:25:14 +02001714 whichwrap[len - 1] = NUL;
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001715
1716 return whichwrap;
1717}
1718
1719/*
1720 * Copy the new string value into allocated memory for the option.
1721 * Can't use set_string_option_direct(), because we need to remove the
1722 * backslashes.
1723 */
1724 static char_u *
1725stropt_copy_value(
1726 char_u *origval,
1727 char_u **argp,
1728 set_op_T op,
1729 int flags UNUSED)
1730{
1731 char_u *arg = *argp;
1732 unsigned newlen;
1733 char_u *newval;
1734 char_u *s = NULL;
1735
1736 // get a bit too much
1737 newlen = (unsigned)STRLEN(arg) + 1;
1738 if (op != OP_NONE)
1739 newlen += (unsigned)STRLEN(origval) + 1;
1740 newval = alloc(newlen);
1741 if (newval == NULL) // out of mem, don't change
1742 return NULL;
1743 s = newval;
1744
1745 // Copy the string, skip over escaped chars.
1746 // For MS-DOS and WIN32 backslashes before normal file name characters
1747 // are not removed, and keep backslash at start, for "\\machine\path",
1748 // but do remove it for "\\\\machine\\path".
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001749 // The reverse is found in escape_option_str_cmdline().
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001750 while (*arg != NUL && !VIM_ISWHITE(*arg))
1751 {
1752 int i;
1753
1754 if (*arg == '\\' && arg[1] != NUL
1755#ifdef BACKSLASH_IN_FILENAME
1756 && !((flags & P_EXPAND)
1757 && vim_isfilec(arg[1])
1758 && !VIM_ISWHITE(arg[1])
1759 && (arg[1] != '\\'
1760 || (s == newval && arg[2] != '\\')))
1761#endif
1762 )
1763 ++arg; // remove backslash
1764 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
1765 {
1766 // copy multibyte char
1767 mch_memmove(s, arg, (size_t)i);
1768 arg += i;
1769 s += i;
1770 }
1771 else
1772 *s++ = *arg++;
1773 }
1774 *s = NUL;
1775
1776 *argp = arg;
1777 return newval;
1778}
1779
1780/*
1781 * Expand environment variables and ~ in string option value 'newval'.
1782 */
1783 static char_u *
1784stropt_expand_envvar(
1785 int opt_idx,
1786 char_u *origval,
1787 char_u *newval,
1788 set_op_T op)
1789{
1790 char_u *s = option_expand(opt_idx, newval);
1791 if (s == NULL)
1792 return newval;
1793
1794 vim_free(newval);
1795 unsigned newlen = (unsigned)STRLEN(s) + 1;
1796 if (op != OP_NONE)
1797 newlen += (unsigned)STRLEN(origval) + 1;
1798
1799 newval = alloc(newlen);
1800 if (newval == NULL)
1801 return NULL;
1802
1803 STRCPY(newval, s);
1804
1805 return newval;
1806}
1807
1808/*
1809 * Concatenate the original and new values of a string option, adding a "," if
1810 * needed.
1811 */
1812 static void
1813stropt_concat_with_comma(
1814 char_u *origval,
1815 char_u *newval,
1816 set_op_T op,
1817 int flags)
1818{
1819 int len = 0;
1820
1821 int comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1822 if (op == OP_ADDING)
1823 {
1824 len = (int)STRLEN(origval);
1825 // strip a trailing comma, would get 2
1826 if (comma && len > 1
1827 && (flags & P_ONECOMMA) == P_ONECOMMA
1828 && origval[len - 1] == ','
1829 && origval[len - 2] != '\\')
1830 len--;
1831 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1832 mch_memmove(newval, origval, (size_t)len);
1833 }
1834 else
1835 {
1836 len = (int)STRLEN(newval);
1837 STRMOVE(newval + len + comma, origval);
1838 }
1839 if (comma)
1840 newval[len] = ',';
1841}
1842
1843/*
1844 * Remove a value from a string option. Copy string option value in "origval"
1845 * to "newval" and then remove the string "strval" of length "len".
1846 */
1847 static void
1848stropt_remove_val(
1849 char_u *origval,
1850 char_u *newval,
1851 int flags,
1852 char_u *strval,
1853 int len)
1854{
1855 // Remove newval[] from origval[]. (Note: "len" has been set above
1856 // and is used here).
1857 STRCPY(newval, origval);
1858 if (*strval)
1859 {
1860 // may need to remove a comma
1861 if (flags & P_COMMA)
1862 {
1863 if (strval == origval)
1864 {
1865 // include comma after string
1866 if (strval[len] == ',')
1867 ++len;
1868 }
1869 else
1870 {
1871 // include comma before string
1872 --strval;
1873 ++len;
1874 }
1875 }
1876 STRMOVE(newval + (strval - origval), strval + len);
1877 }
1878}
1879
1880/*
1881 * Remove flags that appear twice in the string option value 'newval'.
1882 */
1883 static void
1884stropt_remove_dupflags(char_u *newval, int flags)
1885{
1886 char_u *s = newval;
1887
1888 // Remove flags that appear twice.
1889 while (*s)
1890 {
1891 // if options have P_FLAGLIST and P_ONECOMMA such as 'whichwrap'
1892 if (flags & P_ONECOMMA)
1893 {
1894 if (*s != ',' && *(s + 1) == ',' && vim_strchr(s + 2, *s) != NULL)
1895 {
1896 // Remove the duplicated value and the next comma.
1897 STRMOVE(s, s + 2);
1898 continue;
1899 }
1900 }
1901 else
1902 {
1903 if ((!(flags & P_COMMA) || *s != ',')
1904 && vim_strchr(s + 1, *s) != NULL)
1905 {
1906 STRMOVE(s, s + 1);
1907 continue;
1908 }
1909 }
1910 ++s;
1911 }
1912}
1913
1914/*
1915 * Get the string value specified for a ":set" command. The following set
1916 * options are supported:
1917 * set {opt}&
1918 * set {opt}<
1919 * set {opt}={val}
1920 * set {opt}:{val}
1921 */
1922 static char_u *
1923stropt_get_newval(
1924 int nextchar,
1925 int opt_idx,
1926 char_u **argp,
1927 char_u *varp,
1928 char_u **origval_arg,
1929 char_u **origval_l_arg,
1930 char_u **origval_g_arg,
1931 char_u **oldval_arg,
1932 set_op_T *op_arg,
1933 int flags,
1934 int cp_val)
1935{
1936 char_u *arg = *argp;
1937 char_u *origval = *origval_arg;
1938 char_u *origval_l = *origval_l_arg;
1939 char_u *origval_g = *origval_g_arg;
1940 char_u *oldval = *oldval_arg;
1941 set_op_T op = *op_arg;
1942 char_u *save_arg = NULL;
1943 char_u *newval;
1944 char_u *s = NULL;
1945 char_u whichwrap[80];
1946
1947 if (nextchar == '&') // set to default val
1948 newval = stropt_get_default_val(opt_idx, varp, flags, cp_val);
1949 else if (nextchar == '<') // set to global val
1950 newval = vim_strsave(*(char_u **)get_varp_scope(
1951 &(options[opt_idx]), OPT_GLOBAL));
1952 else
1953 {
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001954 ++arg; // jump to after the '=' or ':'
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001955
1956 // Set 'keywordprg' to ":help" if an empty
1957 // value was passed to :set by the user.
1958 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
1959 {
1960 save_arg = arg;
1961 arg = (char_u *)":help";
1962 }
1963 // Convert 'backspace' number to string
1964 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1965 opt_backspace_nr2str(varp, &origval, &origval_l, &origval_g,
1966 &oldval);
1967 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
1968 {
1969 // Convert 'whichwrap' number to string, for backwards
1970 // compatibility with Vim 3.0.
1971 char_u *t = opt_whichwrap_nr2str(&arg, whichwrap);
1972 save_arg = arg;
1973 arg = t;
1974 }
1975 // Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1976 // version 3.0
1977 else if (*arg == '>' && (varp == (char_u *)&p_dir
1978 || varp == (char_u *)&p_bdir))
1979 ++arg;
1980
1981 // Copy the new string into allocated memory.
1982 newval = stropt_copy_value(origval, &arg, op, flags);
1983 if (newval == NULL)
1984 goto done;
1985
1986 // Expand environment variables and ~.
1987 // Don't do it when adding without inserting a comma.
1988 if (op == OP_NONE || (flags & P_COMMA))
1989 {
1990 newval = stropt_expand_envvar(opt_idx, origval, newval, op);
1991 if (newval == NULL)
1992 goto done;
1993 }
1994
1995 // locate newval[] in origval[] when removing it and when adding to
1996 // avoid duplicates
1997 int len = 0;
1998 if (op == OP_REMOVING || (flags & P_NODUP))
1999 {
2000 len = (int)STRLEN(newval);
John Marriott95dacbb2024-09-10 21:25:14 +02002001 s = find_dup_item(origval, newval, len, flags);
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002002
2003 // do not add if already there
2004 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
2005 {
2006 op = OP_NONE;
2007 STRCPY(newval, origval);
2008 }
2009
2010 // if no duplicate, move pointer to end of original value
2011 if (s == NULL)
2012 s = origval + (int)STRLEN(origval);
2013 }
2014
2015 // concatenate the two strings; add a ',' if needed
2016 if (op == OP_ADDING || op == OP_PREPENDING)
2017 stropt_concat_with_comma(origval, newval, op, flags);
2018 else if (op == OP_REMOVING)
2019 // Remove newval[] from origval[]. (Note: "len" has been set above
2020 // and is used here).
2021 stropt_remove_val(origval, newval, flags, s, len);
2022
2023 if (flags & P_FLAGLIST)
2024 // Remove flags that appear twice.
2025 stropt_remove_dupflags(newval, flags);
2026 }
2027
2028done:
2029 if (save_arg != NULL)
2030 arg = save_arg; // arg was temporarily changed, restore it
2031 *argp = arg;
2032 *origval_arg = origval;
2033 *origval_l_arg = origval_l;
2034 *origval_g_arg = origval_g;
2035 *oldval_arg = oldval;
2036 *op_arg = op;
2037
2038 return newval;
2039}
2040
2041/*
Bram Moolenaar47403942022-09-21 21:12:53 +01002042 * Part of do_set() for string options.
2043 * Returns FAIL on failure, do not process further options.
2044 */
2045 static int
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002046do_set_option_string(
Bram Moolenaar47403942022-09-21 21:12:53 +01002047 int opt_idx,
2048 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01002049 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01002050 int nextchar,
2051 set_op_T op_arg,
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02002052 long_u flags,
Bram Moolenaar47403942022-09-21 21:12:53 +01002053 int cp_val,
2054 char_u *varp_arg,
2055 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +01002056 size_t errbuflen,
Bram Moolenaar47403942022-09-21 21:12:53 +01002057 int *value_checked,
2058 char **errmsg)
2059{
Bram Moolenaar6f981142022-09-22 12:48:58 +01002060 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01002061 set_op_T op = op_arg;
2062 char_u *varp = varp_arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002063 char_u *oldval = NULL; // previous value if *varp
2064 char_u *newval;
2065 char_u *origval = NULL;
2066 char_u *origval_l = NULL;
2067 char_u *origval_g = NULL;
2068#if defined(FEAT_EVAL)
2069 char_u *saved_origval = NULL;
2070 char_u *saved_origval_l = NULL;
2071 char_u *saved_origval_g = NULL;
2072 char_u *saved_newval = NULL;
2073#endif
Bram Moolenaar47403942022-09-21 21:12:53 +01002074
2075 // When using ":set opt=val" for a global option
2076 // with a local value the local value will be
2077 // reset, use the global value here.
2078 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
2079 && ((int)options[opt_idx].indir & PV_BOTH))
2080 varp = options[opt_idx].var;
2081
2082 // The old value is kept until we are sure that the new value is valid.
2083 oldval = *(char_u **)varp;
2084
2085 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2086 {
2087 origval_l = *(char_u **)get_varp_scope(
2088 &(options[opt_idx]), OPT_LOCAL);
2089 origval_g = *(char_u **)get_varp_scope(
2090 &(options[opt_idx]), OPT_GLOBAL);
2091
2092 // A global-local string option might have an empty option as value to
2093 // indicate that the global value should be used.
2094 if (((int)options[opt_idx].indir & PV_BOTH)
2095 && origval_l == empty_option)
2096 origval_l = origval_g;
2097 }
2098
2099 // When setting the local value of a global option, the old value may be
2100 // the global value.
2101 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
2102 origval = *(char_u **)get_varp(&options[opt_idx]);
2103 else
2104 origval = oldval;
2105
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002106 // Get the new value for the option
2107 newval = stropt_get_newval(nextchar, opt_idx, &arg, varp, &origval,
2108 &origval_l, &origval_g, &oldval, &op, flags,
2109 cp_val);
Bram Moolenaar47403942022-09-21 21:12:53 +01002110
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002111 // Set the new value.
Bram Moolenaar47403942022-09-21 21:12:53 +01002112 *(char_u **)(varp) = newval;
Bram Moolenaar339e1142023-02-15 14:26:25 +00002113 if (newval == NULL)
2114 *(char_u **)(varp) = empty_option;
Bram Moolenaar47403942022-09-21 21:12:53 +01002115
2116#if defined(FEAT_EVAL)
2117 if (!starting
2118# ifdef FEAT_CRYPT
2119 && options[opt_idx].indir != PV_KEY
2120# endif
2121 && origval != NULL && newval != NULL)
2122 {
2123 // origval may be freed by did_set_string_option(), make a copy.
2124 saved_origval = vim_strsave(origval);
2125 // newval (and varp) may become invalid if the buffer is closed by
2126 // autocommands.
2127 saved_newval = vim_strsave(newval);
2128 if (origval_l != NULL)
2129 saved_origval_l = vim_strsave(origval_l);
2130 if (origval_g != NULL)
2131 saved_origval_g = vim_strsave(origval_g);
2132 }
2133#endif
2134
2135 {
2136 long_u *p = insecure_flag(opt_idx, opt_flags);
2137 int secure_saved = secure;
2138
2139 // When an option is set in the sandbox, from a modeline or in secure
2140 // mode, then deal with side effects in secure mode. Also when the
2141 // value was set with the P_INSECURE flag and is not completely
2142 // replaced.
2143 if ((opt_flags & OPT_MODELINE)
2144#ifdef HAVE_SANDBOX
2145 || sandbox != 0
2146#endif
2147 || (op != OP_NONE && (*p & P_INSECURE)))
2148 secure = 1;
2149
2150 // Handle side effects, and set the global value for ":set" on local
2151 // options. Note: when setting 'syntax' or 'filetype' autocommands may
2152 // be triggered that can cause havoc.
2153 *errmsg = did_set_string_option(
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002154 opt_idx, (char_u **)varp, oldval, newval, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002155 errbuflen, opt_flags, op, value_checked);
Bram Moolenaar47403942022-09-21 21:12:53 +01002156
2157 secure = secure_saved;
2158 }
2159
2160#if defined(FEAT_EVAL)
2161 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00002162 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01002163 saved_origval_l, saved_origval_g, saved_newval);
2164 vim_free(saved_origval);
2165 vim_free(saved_origval_l);
2166 vim_free(saved_origval_g);
2167 vim_free(saved_newval);
2168#endif
2169
Bram Moolenaar6f981142022-09-22 12:48:58 +01002170 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002171 return *errmsg == NULL ? OK : FAIL;
2172}
2173
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002175 * Set a boolean option.
2176 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002177 */
2178 static char *
2179do_set_option_bool(
2180 int opt_idx,
2181 int opt_flags,
2182 set_prefix_T prefix,
2183 long_u flags,
2184 char_u *varp,
2185 int nextchar,
2186 int afterchar,
2187 int cp_val)
2188
2189{
2190 varnumber_T value;
2191
2192 if (nextchar == '=' || nextchar == ':')
2193 return e_invalid_argument;
Bram Moolenaar546933f2023-02-06 16:40:49 +00002194 if (opt_idx < 0 || varp == NULL)
2195 return NULL; // "cannot happen"
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002196
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002197 // ":set opt!": invert
2198 // ":set opt&": reset to default value
2199 // ":set opt<": reset to global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002200 if (nextchar == '!')
2201 value = *(int *)(varp) ^ 1;
2202 else if (nextchar == '&')
2203 value = (int)(long)(long_i)options[opt_idx].def_val[
2204 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2205 else if (nextchar == '<')
2206 {
2207 // For 'autoread' -1 means to use global value.
2208 if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL)
2209 value = -1;
2210 else
2211 value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2212 }
2213 else
2214 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002215 // ":set invopt": invert
2216 // ":set opt" or ":set noopt": set or reset
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002217 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
2218 return e_trailing_characters;
2219 if (prefix == PREFIX_INV)
2220 value = *(int *)(varp) ^ 1;
2221 else
2222 value = prefix == PREFIX_NO ? 0 : 1;
2223 }
2224
2225 return set_bool_option(opt_idx, varp, (int)value, opt_flags);
2226}
2227
2228/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002229 * Set a numeric option.
2230 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002231 */
2232 static char *
2233do_set_option_numeric(
2234 int opt_idx,
2235 int opt_flags,
2236 char_u **argp,
2237 int nextchar,
2238 set_op_T op,
2239 long_u flags,
2240 int cp_val,
2241 char_u *varp,
2242 char *errbuf,
2243 size_t errbuflen)
2244{
2245 char_u *arg = *argp;
2246 varnumber_T value;
2247 int i;
2248 char *errmsg = NULL;
2249
Bram Moolenaar546933f2023-02-06 16:40:49 +00002250 if (opt_idx < 0 || varp == NULL)
2251 return NULL; // "cannot happen"
2252 //
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002253 /*
2254 * Different ways to set a number option:
2255 * & set to default value
2256 * < set to global value
Milly40c6bab2024-10-04 20:41:14 +02002257 * <xx> accept special key codes for 'wildchar' or 'wildcharm'
2258 * ^x accept ctrl key codes for 'wildchar' or 'wildcharm'
2259 * c accept any non-digit for 'wildchar' or 'wildcharm'
2260 * [-]0-9 set number
2261 * other error
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002262 */
2263 ++arg;
2264 if (nextchar == '&')
2265 value = (long)(long_i)options[opt_idx].def_val[
2266 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2267 else if (nextchar == '<')
2268 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002269 if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL)
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002270 // for 'undolevels' NO_LOCAL_UNDOLEVEL means using the global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002271 value = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002272 else if (opt_flags == OPT_LOCAL
2273 && ((long *)varp == &curwin->w_p_siso
2274 || (long *)varp == &curwin->w_p_so))
2275 // for 'scrolloff'/'sidescrolloff' -1 means using the global value
2276 value = -1;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002277 else
2278 value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2279 }
2280 else if (((long *)varp == &p_wc || (long *)varp == &p_wcm)
2281 && (*arg == '<'
2282 || *arg == '^'
2283 || (*arg != NUL
2284 && (!arg[1] || VIM_ISWHITE(arg[1]))
2285 && !VIM_ISDIGIT(*arg))))
2286 {
2287 value = string_to_key(arg, FALSE);
Milly40c6bab2024-10-04 20:41:14 +02002288 if (value == 0)
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002289 {
2290 errmsg = e_invalid_argument;
2291 goto skip;
2292 }
2293 }
2294 else if (*arg == '-' || VIM_ISDIGIT(*arg))
2295 {
2296 // Allow negative (for 'undolevels'), octal and hex numbers.
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002297 vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE, NULL);
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002298 if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i])))
2299 {
2300 errmsg = e_number_required_after_equal;
2301 goto skip;
2302 }
2303 }
2304 else
2305 {
2306 errmsg = e_number_required_after_equal;
2307 goto skip;
2308 }
2309
2310 if (op == OP_ADDING)
2311 value = *(long *)varp + value;
2312 else if (op == OP_PREPENDING)
2313 value = *(long *)varp * value;
2314 else if (op == OP_REMOVING)
2315 value = *(long *)varp - value;
2316
2317 errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen,
2318 opt_flags);
2319
2320skip:
2321 *argp = arg;
2322 return errmsg;
2323}
2324
2325/*
2326 * Set a key code (t_xx) option
2327 */
2328 static char *
2329do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar)
2330{
2331 char_u *arg = *argp;
2332 char_u *p;
2333
2334 if (nextchar == '&')
2335 {
2336 if (add_termcap_entry(key_name, TRUE) == FAIL)
2337 return e_not_found_in_termcap;
2338 }
2339 else
2340 {
2341 ++arg; // jump to after the '=' or ':'
2342 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
2343 if (*p == '\\' && p[1] != NUL)
2344 ++p;
2345 nextchar = *p;
2346 *p = NUL;
2347 add_termcode(key_name, arg, FALSE);
2348 *p = nextchar;
2349 }
2350 if (full_screen)
2351 ttest(FALSE);
2352 redraw_all_later(UPD_CLEAR);
2353
2354 *argp = arg;
2355 return NULL;
2356}
2357
2358/*
2359 * Set an option to a new value.
2360 */
2361 static char *
2362do_set_option_value(
2363 int opt_idx,
2364 int opt_flags,
2365 char_u **argp,
2366 set_prefix_T prefix,
2367 set_op_T op,
2368 long_u flags,
2369 char_u *varp,
2370 char_u *key_name,
2371 int nextchar,
2372 int afterchar,
2373 int cp_val,
2374 int *stopopteval,
2375 char *errbuf,
2376 size_t errbuflen)
2377{
2378 int value_checked = FALSE;
2379 char *errmsg = NULL;
2380 char_u *arg = *argp;
2381
2382 if (flags & P_BOOL)
2383 {
2384 // boolean option
2385 errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp,
2386 nextchar, afterchar, cp_val);
2387 if (errmsg != NULL)
2388 goto skip;
2389 }
2390 else
2391 {
2392 // numeric or string option
2393 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2394 || prefix != PREFIX_NONE)
2395 {
2396 errmsg = e_invalid_argument;
2397 goto skip;
2398 }
2399
2400 if (flags & P_NUM)
2401 {
2402 // numeric option
2403 errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar,
2404 op, flags, cp_val, varp,
2405 errbuf, errbuflen);
2406 if (errmsg != NULL)
2407 goto skip;
2408 }
2409 else if (opt_idx >= 0)
2410 {
2411 // string option
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002412 if (do_set_option_string(opt_idx, opt_flags, &arg, nextchar, op,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002413 flags, cp_val, varp, errbuf, errbuflen,
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002414 &value_checked, &errmsg) == FAIL)
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002415 {
2416 if (errmsg != NULL)
2417 goto skip;
2418 *stopopteval = TRUE;
2419 goto skip;
2420 }
2421 }
2422 else
2423 {
2424 // key code option
2425 errmsg = do_set_option_keycode(&arg, key_name, nextchar);
2426 if (errmsg != NULL)
2427 goto skip;
2428 }
2429 }
2430
2431 if (opt_idx >= 0)
2432 did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked);
2433
2434skip:
2435 *argp = arg;
2436 return errmsg;
2437}
2438
2439/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002440 * Set an option to a new value.
2441 * Return NULL if OK, return an untranslated error message when something is
2442 * wrong. "errbuf[errbuflen]" can be used to create the error message.
2443 */
2444 static char *
2445do_set_option(
2446 int opt_flags,
2447 char_u **argp,
2448 char_u *arg_start,
2449 char_u **startarg,
2450 int *did_show,
2451 int *stopopteval,
2452 char *errbuf,
2453 size_t errbuflen)
2454{
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002455 int opt_idx;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002456 char_u *arg;
2457 set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix
2458 set_op_T op;
2459 long_u flags; // flags for current option
2460 char_u *varp; // pointer to variable for current option
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002461 char_u key_name[2];
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002462 int nextchar; // next non-white char after option name
2463 int afterchar; // character just after option name
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002464 char *errmsg = NULL;
2465 int key;
2466 int len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002467
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002468 prefix = get_option_prefix(argp);
2469 arg = *argp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002470
2471 // find end of name
2472 key = 0;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002473 if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL)
2474 return e_invalid_argument;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002475
2476 // remember character after option name
2477 afterchar = arg[len];
2478
2479 if (in_vim9script())
2480 {
2481 char_u *p = skipwhite(arg + len);
2482
2483 // disallow white space before =val, +=val, -=val, ^=val
2484 if (p > arg + len && (p[0] == '='
2485 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
2486 && p[1] == '=')))
2487 {
2488 errmsg = e_no_white_space_allowed_between_option_and;
2489 arg = p;
2490 *startarg = p;
2491 goto skip;
2492 }
2493 }
2494 else
2495 // skip white space, allow ":set ai ?", ":set hlsearch !"
2496 while (VIM_ISWHITE(arg[len]))
2497 ++len;
2498
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002499 op = get_opt_op(arg + len);
2500 if (op != OP_NONE)
2501 len++;
2502
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002503 nextchar = arg[len];
2504
2505 if (opt_idx == -1 && key == 0) // found a mismatch: skip
2506 {
2507 if (in_vim9script() && arg > arg_start
2508 && vim_strchr((char_u *)"!&<", *arg) != NULL)
2509 errmsg = e_no_white_space_allowed_between_option_and;
2510 else
2511 errmsg = e_unknown_option;
2512 goto skip;
2513 }
2514
2515 if (opt_idx >= 0)
2516 {
2517 if (options[opt_idx].var == NULL) // hidden option: skip
2518 {
2519 // Only give an error message when requesting the value of
2520 // a hidden option, ignore setting it.
2521 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
2522 && (!(options[opt_idx].flags & P_BOOL)
2523 || nextchar == '?'))
2524 errmsg = e_option_not_supported;
2525 goto skip;
2526 }
2527
2528 flags = options[opt_idx].flags;
2529 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
2530 }
2531 else
2532 {
2533 flags = P_STRING;
Bram Moolenaar40b48722023-02-05 17:04:50 +00002534 varp = NULL;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002535 if (key < 0)
2536 {
2537 key_name[0] = KEY2TERMCAP0(key);
2538 key_name[1] = KEY2TERMCAP1(key);
2539 }
2540 else
2541 {
2542 key_name[0] = KS_KEY;
2543 key_name[1] = (key & 0xff);
2544 }
2545 }
2546
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002547 // Make sure the option value can be changed.
2548 if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002549 goto skip;
2550
Bram Moolenaar40b48722023-02-05 17:04:50 +00002551 int cp_val = p_cp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002552 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
2553 {
2554 arg += len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002555 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
2556 {
2557 if (arg[3] == 'm') // "opt&vim": set to Vim default
2558 {
2559 cp_val = FALSE;
2560 arg += 3;
2561 }
2562 else // "opt&vi": set to Vi default
2563 {
2564 cp_val = TRUE;
2565 arg += 2;
2566 }
2567 }
2568 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
2569 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
2570 {
2571 errmsg = e_trailing_characters;
2572 goto skip;
2573 }
2574 }
2575
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002576 // Allow '=' and ':' for historical reasons (MSDOS command.com).
2577 // Allows only one '=' character per "set" command line. grrr. (jw)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002578 if (nextchar == '?'
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002579 || (prefix == PREFIX_NONE
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002580 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
2581 && !(flags & P_BOOL)))
2582 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002583 // print value
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002584 if (*did_show)
2585 msg_putchar('\n'); // cursor below last one
2586 else
2587 {
2588 gotocmdline(TRUE); // cursor at status line
2589 *did_show = TRUE; // remember that we did a line
2590 }
2591 if (opt_idx >= 0)
2592 {
2593 showoneopt(&options[opt_idx], opt_flags);
2594#ifdef FEAT_EVAL
2595 if (p_verbose > 0)
2596 {
2597 // Mention where the option was last set.
2598 if (varp == options[opt_idx].var)
2599 last_set_msg(options[opt_idx].script_ctx);
2600 else if ((int)options[opt_idx].indir & PV_WIN)
2601 last_set_msg(curwin->w_p_script_ctx[
2602 (int)options[opt_idx].indir & PV_MASK]);
2603 else if ((int)options[opt_idx].indir & PV_BUF)
2604 last_set_msg(curbuf->b_p_script_ctx[
2605 (int)options[opt_idx].indir & PV_MASK]);
2606 }
2607#endif
2608 }
2609 else
2610 {
2611 char_u *p;
2612
2613 p = find_termcode(key_name);
2614 if (p == NULL)
2615 {
2616 errmsg = e_key_code_not_set;
2617 goto skip;
2618 }
2619 else
2620 (void)show_one_termcode(key_name, p, TRUE);
2621 }
2622 if (nextchar != '?'
2623 && nextchar != NUL && !VIM_ISWHITE(afterchar))
2624 errmsg = e_trailing_characters;
2625 }
2626 else
2627 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002628 errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op,
2629 flags, varp, key_name, nextchar,
2630 afterchar, cp_val, stopopteval, errbuf,
2631 errbuflen);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002632 }
2633
2634skip:
2635 *argp = arg;
2636 return errmsg;
2637}
2638
2639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 * Parse 'arg' for option settings.
2641 *
2642 * 'arg' may be IObuff, but only when no errors can be present and option
2643 * does not need to be expanded with option_expand().
2644 * "opt_flags":
2645 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002646 * OPT_GLOBAL for ":setglobal"
2647 * OPT_LOCAL for ":setlocal" and a modeline
2648 * OPT_MODELINE for a modeline
2649 * OPT_WINONLY to only set window-local options
2650 * OPT_NOWIN to skip setting window-local options
2651 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002653 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 */
2655 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002656do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002657 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002658 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002660 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002662 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663
2664 if (*arg == NUL)
2665 {
2666 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002667 did_show = TRUE;
2668 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 }
2670
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002671 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002673 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002674 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002676 // ":set all" show all options.
2677 // ":set all&" set all options to their default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 arg += 3;
2679 if (*arg == '&')
2680 {
2681 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002682 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002684 didset_options();
2685 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002686 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 }
2688 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002689 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002691 did_show = TRUE;
2692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002694 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 {
2696 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002697 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002698 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 arg += 7;
2700 }
2701 else
2702 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002703 int stopopteval = FALSE;
2704 char *errmsg = NULL;
Christian Brabandtb39b2402023-11-29 11:34:05 +01002705 char errbuf[ERR_BUFLEN];
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002706 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002708 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2709 &did_show, &stopopteval, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002710 ERR_BUFLEN);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002711 if (stopopteval)
2712 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002714 // Advance to next argument.
2715 // - skip until a blank found, taking care of backslashes
2716 // - skip blanks
2717 // - skip one "=val" argument (for hidden options ":set gfn =xx")
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 for (i = 0; i < 2 ; ++i)
2719 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002720 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 if (*arg++ == '\\' && *arg != NUL)
2722 ++arg;
2723 arg = skipwhite(arg);
2724 if (*arg != '=')
2725 break;
2726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002728 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 {
John Marriott95dacbb2024-09-10 21:25:14 +02002730 i = vim_snprintf((char *)IObuff, IOSIZE, "%s", (char_u *)_(errmsg)) + 2;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002731 if (i + (arg - startarg) < IOSIZE)
2732 {
2733 // append the argument with the error
John Marriott95dacbb2024-09-10 21:25:14 +02002734 STRCPY(IObuff + i - 2, ": ");
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002735 mch_memmove(IObuff + i, startarg, (arg - startarg));
2736 IObuff[i + (arg - startarg)] = NUL;
2737 }
2738 // make sure all characters are printable
2739 trans_characters(IObuff, IOSIZE);
2740
2741 ++no_wait_return; // wait_return() done later
2742 emsg((char *)IObuff); // show error highlighted
2743 --no_wait_return;
2744
2745 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 }
2748
2749 arg = skipwhite(arg);
2750 }
2751
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002752theend:
2753 if (silent_mode && did_show)
2754 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002755 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002756 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002757 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002758 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002759 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002760 out_flush();
2761 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002762 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002763 }
2764
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 return OK;
2766}
2767
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002768/*
2769 * Call this when an option has been given a new value through a user command.
2770 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2771 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002772 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002773did_set_option(
2774 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002775 int opt_flags, // possibly with OPT_MODELINE
2776 int new_value, // value was replaced completely
2777 int value_checked) // value was checked to be safe, no need to set the
2778 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002779{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002780 long_u *p;
2781
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002782 options[opt_idx].flags |= P_WAS_SET;
2783
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002784 // When an option is set in the sandbox, from a modeline or in secure mode
2785 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2786 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002787 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002788 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002789#ifdef HAVE_SANDBOX
2790 || sandbox != 0
2791#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002792 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002793 *p = *p | P_INSECURE;
2794 else if (new_value)
2795 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002796}
2797
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798/*
2799 * Convert a key name or string into a key value.
Millya9c6f902024-10-06 16:47:02 +02002800 * Used for 'cedit', 'termwinkey', 'wildchar' and 'wildcharm' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002801 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002803 int
2804string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805{
Millya9c6f902024-10-06 16:47:02 +02002806 if (*arg == '<' && arg[1])
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002807 return find_key_option(arg + 1, TRUE);
Millya9c6f902024-10-06 16:47:02 +02002808 if (*arg == '^' && arg[1])
2809 {
2810 int key = Ctrl_chr(arg[1]);
2811 if (key == 0) // ^@ is <Nul>
2812 key = K_ZERO;
2813 return key;
2814 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002815 if (multi_byte)
2816 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 return *arg;
2818}
2819
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820/*
2821 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2822 * maketitle() to create and display it.
2823 * When switching the title or icon off, call mch_restore_title() to get
2824 * the old value back.
2825 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002826 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002827did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828{
2829 if (starting != NO_SCREEN
2830#ifdef FEAT_GUI
2831 && !gui.starting
2832#endif
2833 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836
2837/*
2838 * set_options_bin - called when 'bin' changes value.
2839 */
2840 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002841set_options_bin(
2842 int oldval,
2843 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002844 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002846 // The option values that are changed when 'bin' changes are
2847 // copied when 'bin is set and restored when 'bin' is reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 if (newval)
2849 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002850 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {
2852 if (!(opt_flags & OPT_GLOBAL))
2853 {
2854 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2855 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2856 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2857 curbuf->b_p_et_nobin = curbuf->b_p_et;
2858 }
2859 if (!(opt_flags & OPT_LOCAL))
2860 {
2861 p_tw_nobin = p_tw;
2862 p_wm_nobin = p_wm;
2863 p_ml_nobin = p_ml;
2864 p_et_nobin = p_et;
2865 }
2866 }
2867
2868 if (!(opt_flags & OPT_GLOBAL))
2869 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002870 curbuf->b_p_tw = 0; // no automatic line wrap
2871 curbuf->b_p_wm = 0; // no automatic line wrap
2872 curbuf->b_p_ml = 0; // no modelines
2873 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 }
2875 if (!(opt_flags & OPT_LOCAL))
2876 {
2877 p_tw = 0;
2878 p_wm = 0;
2879 p_ml = FALSE;
2880 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002881 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 }
2883 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002884 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 {
2886 if (!(opt_flags & OPT_GLOBAL))
2887 {
2888 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2889 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2890 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2891 curbuf->b_p_et = curbuf->b_p_et_nobin;
2892 }
2893 if (!(opt_flags & OPT_LOCAL))
2894 {
2895 p_tw = p_tw_nobin;
2896 p_wm = p_wm_nobin;
2897 p_ml = p_ml_nobin;
2898 p_et = p_et_nobin;
2899 }
2900 }
Christian Brabandt757593c2023-08-22 21:44:10 +02002901#if defined(FEAT_EVAL) || defined(PROTO)
2902 // Remember where the dependent option were reset
2903 didset_options_sctx(opt_flags, p_bin_dep_opts);
2904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905}
2906
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907/*
2908 * Expand environment variables for some string options.
2909 * These string options cannot be indirect!
2910 * If "val" is NULL expand the current value of the option.
2911 * Return pointer to NameBuff, or NULL when not expanded.
2912 */
2913 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002914option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002916 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2918 return NULL;
2919
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002920 // If val is longer than MAXPATHL no meaningful expansion can be done,
2921 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 if (val != NULL && STRLEN(val) > MAXPATHL)
2923 return NULL;
2924
2925 if (val == NULL)
2926 val = *(char_u **)options[opt_idx].var;
2927
2928 /*
2929 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2930 * Escape spaces when expanding 'tags', they are used to separate file
2931 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002932 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 */
2934 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002935 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002936#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002937 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2938#endif
2939 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002940 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 return NULL;
2942
2943 return NameBuff;
2944}
2945
2946/*
2947 * After setting various option values: recompute variables that depend on
2948 * option values.
2949 */
2950 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002951didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002953 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 (void)init_chartab();
2955
Bram Moolenaardac13472019-09-16 21:06:21 +02002956 didset_string_options();
2957
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002958#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002959 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002960 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002961 (void)compile_cap_prog(curwin->w_s);
Milly322ad0c2024-10-14 20:21:48 +02002962 (void)did_set_spell_option();
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002963#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002964 // set cedit_key
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002965 (void)did_set_cedit(NULL);
Bram Moolenaar597a4222014-06-25 14:39:50 +02002966#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002967 // initialize the table for 'breakat'.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002968 did_set_breakat(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002969#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002970 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002971}
2972
2973/*
2974 * More side effects of setting options.
2975 */
2976 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002977didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002978{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002979 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002980 (void)highlight_changed();
2981
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002982 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002983 check_opt_wim();
2984
Bram Moolenaareed9d462021-02-15 20:38:25 +01002985 // Parse default for 'listchars'.
zeertzjq6a8d2e12024-01-17 20:54:49 +01002986 (void)set_listchars_option(curwin, curwin->w_p_lcs, TRUE, NULL, 0);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002987
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002988 // Parse default for 'fillchars'.
zeertzjq6a8d2e12024-01-17 20:54:49 +01002989 (void)set_fillchars_option(curwin, curwin->w_p_fcs, TRUE, NULL, 0);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002990
2991#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002992 // Parse default for 'clipboard'
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002993 (void)did_set_clipboard(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002994#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002995#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002996 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002997 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002998 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002999 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001}
3002
3003/*
3004 * Check for string options that are NULL (normally only termcap options).
3005 */
3006 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003007check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008{
3009 int opt_idx;
3010
3011 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
3012 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
3013 check_string_option((char_u **)get_varp(&(options[opt_idx])));
3014}
3015
3016/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003017 * Return the option index found by a pointer into term_strings[].
3018 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003020 int
3021get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003023 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024
3025 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
3026 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003027 return opt_idx;
3028 return -1; // cannot happen: didn't find it!
3029}
3030
3031/*
3032 * Mark a terminal option as allocated, found by a pointer into term_strings[].
3033 * Return the option index or -1 if not found.
3034 */
3035 int
3036set_term_option_alloced(char_u **p)
3037{
3038 int opt_idx = get_term_opt_idx(p);
3039
3040 if (opt_idx >= 0)
3041 options[opt_idx].flags |= P_ALLOCED;
3042 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043}
3044
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003045#if defined(FEAT_EVAL) || defined(PROTO)
3046/*
3047 * Return TRUE when option "opt" was set from a modeline or in secure mode.
3048 * Return FALSE when it wasn't.
3049 * Return -1 for an unknown option.
3050 */
3051 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003052was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003053{
3054 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003055 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003056
3057 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003058 {
3059 flagp = insecure_flag(idx, opt_flags);
3060 return (*flagp & P_INSECURE) != 0;
3061 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01003062 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003063 return -1;
3064}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003065
3066/*
3067 * Get a pointer to the flags used for the P_INSECURE flag of option
3068 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01003069 * NOTE: Caller must make sure that "curwin" is set to the window from which
3070 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003071 */
3072 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003073insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003074{
3075 if (opt_flags & OPT_LOCAL)
3076 switch ((int)options[opt_idx].indir)
3077 {
3078#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003079 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003080#endif
3081#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00003082# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003083 case PV_FDE: return &curwin->w_p_fde_flags;
3084 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00003085# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00003086# ifdef FEAT_BEVAL
3087 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
3088# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003089 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003090 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003091# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003092 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003093# endif
3094#endif
3095 }
3096
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003097 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003098 return &options[opt_idx].flags;
3099}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003100#endif
3101
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003102/*
3103 * Redraw the window title and/or tab page text later.
3104 */
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02003105 void
3106redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003107{
3108 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003109 redraw_tabline = TRUE;
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003110#if defined(FEAT_TABPANEL)
3111 redraw_tabpanel = TRUE;
3112#endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003113}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003114
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02003116 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
3117 * characters or characters in "allowed".
3118 */
Bram Moolenaare677df82019-09-02 22:31:11 +02003119 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02003120valid_name(char_u *val, char *allowed)
3121{
3122 char_u *s;
3123
3124 for (s = val; *s != NUL; ++s)
3125 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
3126 return FALSE;
3127 return TRUE;
3128}
3129
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003130#if defined(FEAT_EVAL) || defined(PROTO)
3131/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003132 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003133 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003134 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003135 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003136set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003137{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003138 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
3139 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003140 sctx_T new_script_ctx = script_ctx;
3141
Bram Moolenaar51258742020-05-03 17:19:33 +02003142 // Modeline already has the line number set.
3143 if (!(opt_flags & OPT_MODELINE))
3144 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003145
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003146 // Remember where the option was set. For local options need to do that
3147 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003148 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003149 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003150 if (both || (opt_flags & OPT_LOCAL))
3151 {
3152 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003153 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003154 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00003155 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003156 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00003157 if (both)
3158 // also setting the "all buffers" value
3159 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
3160 new_script_ctx;
3161 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003162 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003163}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003164
3165/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00003166 * Get the script context of global option "name".
3167 *
3168 */
3169 sctx_T *
3170get_option_sctx(char *name)
3171{
3172 int idx = findoption((char_u *)name);
3173
3174 if (idx >= 0)
3175 return &options[idx].script_ctx;
3176 siemsg("no such option: %s", name);
3177 return NULL;
3178}
3179
3180/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003181 * Set the script_ctx for a termcap option.
3182 * "name" must be the two character code, e.g. "RV".
3183 * When "name" is NULL use "opt_idx".
3184 */
3185 void
3186set_term_option_sctx_idx(char *name, int opt_idx)
3187{
3188 char_u buf[5];
3189 int idx;
3190
3191 if (name == NULL)
3192 idx = opt_idx;
3193 else
3194 {
3195 buf[0] = 't';
3196 buf[1] = '_';
3197 buf[2] = name[0];
3198 buf[3] = name[1];
3199 buf[4] = 0;
3200 idx = findoption(buf);
3201 }
3202 if (idx >= 0)
3203 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
3204}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003205#endif
3206
Bram Moolenaarf4140482020-02-15 23:06:45 +01003207#if defined(FEAT_EVAL)
3208/*
3209 * Apply the OptionSet autocommand.
3210 */
3211 static void
3212apply_optionset_autocmd(
3213 int opt_idx,
3214 long opt_flags,
3215 long oldval,
3216 long oldval_g,
3217 long newval,
3218 char *errmsg)
3219{
3220 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
3221
3222 // Don't do this while starting up, failure or recursively.
3223 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
3224 return;
3225
3226 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
3227 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
3228 oldval_g);
3229 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
3230 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
3231 (opt_flags & OPT_LOCAL) ? "local" : "global");
3232 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3233 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3234 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
3235 if (opt_flags & OPT_LOCAL)
3236 {
3237 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3238 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3239 }
3240 if (opt_flags & OPT_GLOBAL)
3241 {
3242 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3243 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3244 }
3245 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3246 {
3247 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3248 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3249 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3250 }
3251 if (opt_flags & OPT_MODELINE)
3252 {
3253 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3254 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3255 }
3256 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3257 NULL, FALSE, NULL);
3258 reset_v_option_vars();
3259}
3260#endif
3261
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003262#if defined(FEAT_ARABIC) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003263/*
3264 * Process the updated 'arabic' option value.
3265 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003266 char *
3267did_set_arabic(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003268{
3269 char *errmsg = NULL;
3270
3271 if (curwin->w_p_arab)
3272 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003273 // 'arabic' is set, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003274 if (!p_tbidi)
3275 {
3276 // set rightleft mode
3277 if (!curwin->w_p_rl)
3278 {
3279 curwin->w_p_rl = TRUE;
3280 changed_window_setting();
3281 }
3282
3283 // Enable Arabic shaping (major part of what Arabic requires)
3284 if (!p_arshape)
3285 {
3286 p_arshape = TRUE;
3287 redraw_later_clear();
3288 }
3289 }
3290
3291 // Arabic requires a utf-8 encoding, inform the user if it's not
3292 // set.
3293 if (STRCMP(p_enc, "utf-8") != 0)
3294 {
3295 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3296
3297 msg_source(HL_ATTR(HLF_W));
3298 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
3299#ifdef FEAT_EVAL
3300 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3301#endif
3302 }
3303
3304 // set 'delcombine'
3305 p_deco = TRUE;
3306
3307# ifdef FEAT_KEYMAP
3308 // Force-set the necessary keymap for arabic
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003309 errmsg = set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3310 OPT_LOCAL);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003311# endif
3312 }
3313 else
3314 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003315 // 'arabic' is reset, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003316 if (!p_tbidi)
3317 {
3318 // reset rightleft mode
3319 if (curwin->w_p_rl)
3320 {
3321 curwin->w_p_rl = FALSE;
3322 changed_window_setting();
3323 }
3324
3325 // 'arabicshape' isn't reset, it is a global option and
3326 // another window may still need it "on".
3327 }
3328
3329 // 'delcombine' isn't reset, it is a global option and another
3330 // window may still want it "on".
3331
3332# ifdef FEAT_KEYMAP
3333 // Revert to the default keymap
3334 curbuf->b_p_iminsert = B_IMODE_NONE;
3335 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3336# endif
3337 }
3338
3339 return errmsg;
3340}
3341#endif
3342
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003343#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
3344/*
3345 * Process the updated 'autochdir' option value.
3346 */
3347 char *
3348did_set_autochdir(optset_T *args UNUSED)
3349{
3350 // Change directories when the 'acd' option is set now.
3351 DO_AUTOCHDIR;
3352 return NULL;
3353}
3354#endif
3355
3356#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
3357/*
3358 * Process the updated 'ballooneval' option value.
3359 */
3360 char *
3361did_set_ballooneval(optset_T *args)
3362{
3363 if (balloonEvalForTerm)
3364 return NULL;
3365
3366 if (p_beval && !args->os_oldval.boolean)
3367 gui_mch_enable_beval_area(balloonEval);
3368 else if (!p_beval && args->os_oldval.boolean)
3369 gui_mch_disable_beval_area(balloonEval);
3370
3371 return NULL;
3372}
3373#endif
3374
3375#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
3376/*
3377 * Process the updated 'balloonevalterm' option value.
3378 */
3379 char *
3380did_set_balloonevalterm(optset_T *args UNUSED)
3381{
3382 mch_bevalterm_changed();
3383 return NULL;
3384}
3385#endif
3386
3387/*
3388 * Process the updated 'binary' option value.
3389 */
3390 char *
3391did_set_binary(optset_T *args)
3392{
3393 // when 'bin' is set also set some other options
3394 set_options_bin(args->os_oldval.boolean, curbuf->b_p_bin, args->os_flags);
3395 redraw_titles();
3396
3397 return NULL;
3398}
3399
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003400/*
3401 * Process the updated 'buflisted' option value.
3402 */
3403 char *
3404did_set_buflisted(optset_T *args)
3405{
3406 // when 'buflisted' changes, trigger autocommands
3407 if (args->os_oldval.boolean != curbuf->b_p_bl)
3408 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3409 NULL, NULL, TRUE, curbuf);
3410 return NULL;
3411}
3412
3413/*
3414 * Process the new 'cmdheight' option value.
3415 */
3416 char *
3417did_set_cmdheight(optset_T *args)
3418{
3419 long old_value = args->os_oldval.number;
3420 char *errmsg = NULL;
3421
3422 // if p_ch changed value, change the command line height
Milly2cddf0e2024-11-28 18:16:55 +01003423 if (p_ch < MIN_CMDHEIGHT)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003424 {
3425 errmsg = e_argument_must_be_positive;
Milly2cddf0e2024-11-28 18:16:55 +01003426 p_ch = MIN_CMDHEIGHT;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003427 }
Milly2cddf0e2024-11-28 18:16:55 +01003428 if (p_ch > Rows - min_rows() + MIN_CMDHEIGHT)
3429 p_ch = Rows - min_rows() + MIN_CMDHEIGHT;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003430
3431 // Only compute the new window layout when startup has been
3432 // completed. Otherwise the frame sizes may be wrong.
3433 if ((p_ch != old_value
3434 || tabline_height() + topframe->fr_height != Rows - p_ch)
3435 && full_screen
3436#ifdef FEAT_GUI
3437 && !gui.starting
3438#endif
3439 )
3440 command_height();
3441
3442 return errmsg;
3443}
3444
3445/*
3446 * Process the updated 'compatible' option value.
3447 */
3448 char *
3449did_set_compatible(optset_T *args UNUSED)
3450{
3451 compatible_set();
3452 return NULL;
3453}
3454
3455#if defined(FEAT_CONCEAL) || defined(PROTO)
3456/*
3457 * Process the new 'conceallevel' option value.
3458 */
3459 char *
3460did_set_conceallevel(optset_T *args UNUSED)
3461{
3462 char *errmsg = NULL;
3463
3464 if (curwin->w_p_cole < 0)
3465 {
3466 errmsg = e_argument_must_be_positive;
3467 curwin->w_p_cole = 0;
3468 }
3469 else if (curwin->w_p_cole > 3)
3470 {
3471 errmsg = e_invalid_argument;
3472 curwin->w_p_cole = 3;
3473 }
Milly11807282024-10-23 21:42:41 +02003474 if (curwin->w_allbuf_opt.wo_cole < 0)
3475 {
3476 errmsg = e_argument_must_be_positive;
3477 curwin->w_allbuf_opt.wo_cole = 0;
3478 }
3479 else if (curwin->w_allbuf_opt.wo_cole > 3)
3480 {
3481 errmsg = e_invalid_argument;
3482 curwin->w_allbuf_opt.wo_cole = 3;
3483 }
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003484
3485 return errmsg;
3486}
3487#endif
3488
3489#if defined(FEAT_DIFF) || defined(PROTO)
3490/*
3491 * Process the updated 'diff' option value.
3492 */
3493 char *
3494did_set_diff(optset_T *args UNUSED)
3495{
3496 // May add or remove the buffer from the list of diff buffers.
3497 diff_buf_adjust(curwin);
3498# ifdef FEAT_FOLDING
3499 if (foldmethodIsDiff(curwin))
3500 foldUpdateAll(curwin);
3501# endif
3502 return NULL;
3503}
3504#endif
3505
3506/*
3507 * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb'
3508 * option value.
3509 */
3510 char *
3511did_set_eof_eol_fixeol_bomb(optset_T *args UNUSED)
3512{
3513 // redraw the window title and tab page text
3514 redraw_titles();
3515 return NULL;
3516}
3517
3518/*
3519 * Process the updated 'equalalways' option value.
3520 */
3521 char *
3522did_set_equalalways(optset_T *args)
3523{
3524 if (p_ea && !args->os_oldval.boolean)
3525 win_equal(curwin, FALSE, 0);
3526
3527 return NULL;
3528}
3529
3530#if defined(FEAT_FOLDING) || defined(PROTO)
3531/*
3532 * Process the new 'foldcolumn' option value.
3533 */
3534 char *
3535did_set_foldcolumn(optset_T *args UNUSED)
3536{
3537 char *errmsg = NULL;
3538
3539 if (curwin->w_p_fdc < 0)
3540 {
3541 errmsg = e_argument_must_be_positive;
3542 curwin->w_p_fdc = 0;
3543 }
3544 else if (curwin->w_p_fdc > 12)
3545 {
3546 errmsg = e_invalid_argument;
3547 curwin->w_p_fdc = 12;
3548 }
Milly11807282024-10-23 21:42:41 +02003549 if (curwin->w_allbuf_opt.wo_fdc < 0)
3550 {
3551 errmsg = e_argument_must_be_positive;
3552 curwin->w_allbuf_opt.wo_fdc = 0;
3553 }
3554 else if (curwin->w_allbuf_opt.wo_fdc > 12)
3555 {
3556 errmsg = e_invalid_argument;
3557 curwin->w_allbuf_opt.wo_fdc = 12;
3558 }
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003559
3560 return errmsg;
3561}
3562
3563/*
3564 * Process the new 'foldlevel' option value.
3565 */
3566 char *
3567did_set_foldlevel(optset_T *args UNUSED)
3568{
3569 if (curwin->w_p_fdl < 0)
3570 curwin->w_p_fdl = 0;
3571 newFoldLevel();
3572 return NULL;
3573}
3574
3575/*
3576 * Process the new 'foldminlines' option value.
3577 */
3578 char *
3579did_set_foldminlines(optset_T *args UNUSED)
3580{
3581 foldUpdateAll(curwin);
3582 return NULL;
3583}
3584
3585/*
3586 * Process the new 'foldnestmax' option value.
3587 */
3588 char *
3589did_set_foldnestmax(optset_T *args UNUSED)
3590{
3591 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3592 foldUpdateAll(curwin);
3593 return NULL;
3594}
3595#endif
3596
3597#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
3598/*
3599 * Process the updated 'hlsearch' option value.
3600 */
3601 char *
3602did_set_hlsearch(optset_T *args UNUSED)
3603{
3604 // when 'hlsearch' is set or reset: reset no_hlsearch
3605 set_no_hlsearch(FALSE);
3606 return NULL;
3607}
3608#endif
3609
3610/*
3611 * Process the updated 'ignorecase' option value.
3612 */
3613 char *
3614did_set_ignorecase(optset_T *args UNUSED)
3615{
3616 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
3617 if (p_hls)
3618 redraw_all_later(UPD_SOME_VALID);
3619 return NULL;
3620}
3621
3622#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
3623/*
3624 * Process the updated 'imdisable' option value.
3625 */
3626 char *
3627did_set_imdisable(optset_T *args UNUSED)
3628{
3629 // Only de-activate it here, it will be enabled when changing mode.
3630 if (p_imdisable)
3631 im_set_active(FALSE);
3632 else if (State & MODE_INSERT)
3633 // When the option is set from an autocommand, it may need to take
3634 // effect right away.
3635 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
3636 return NULL;
3637}
3638#endif
3639
3640/*
3641 * Process the new 'iminsert' option value.
3642 */
3643 char *
3644did_set_iminsert(optset_T *args UNUSED)
3645{
3646 char *errmsg = NULL;
3647
3648 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3649 {
3650 errmsg = e_invalid_argument;
3651 curbuf->b_p_iminsert = B_IMODE_NONE;
3652 }
3653 p_iminsert = curbuf->b_p_iminsert;
3654 if (termcap_active) // don't do this in the alternate screen
3655 showmode();
3656#if defined(FEAT_KEYMAP)
3657 // Show/unshow value of 'keymap' in status lines.
3658 status_redraw_curbuf();
3659#endif
3660
3661 return errmsg;
3662}
3663
3664/*
3665 * Process the new 'imsearch' option value.
3666 */
3667 char *
3668did_set_imsearch(optset_T *args UNUSED)
3669{
3670 char *errmsg = NULL;
3671
3672 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3673 {
3674 errmsg = e_invalid_argument;
3675 curbuf->b_p_imsearch = B_IMODE_NONE;
3676 }
3677 p_imsearch = curbuf->b_p_imsearch;
3678
3679 return errmsg;
3680}
3681
3682#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3683/*
3684 * Process the new 'imstyle' option value.
3685 */
3686 char *
3687did_set_imstyle(optset_T *args UNUSED)
3688{
3689 char *errmsg = NULL;
3690
3691 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3692 errmsg = e_invalid_argument;
3693
3694 return errmsg;
3695}
3696#endif
3697
3698/*
3699 * Process the updated 'insertmode' option value.
3700 */
3701 char *
3702did_set_insertmode(optset_T *args)
3703{
3704 // when 'insertmode' is set from an autocommand need to do work here
3705 if (p_im)
3706 {
3707 if ((State & MODE_INSERT) == 0)
3708 need_start_insertmode = TRUE;
3709 stop_insert_mode = FALSE;
3710 }
3711 // only reset if it was set previously
3712 else if (args->os_oldval.boolean)
3713 {
3714 need_start_insertmode = FALSE;
3715 stop_insert_mode = TRUE;
3716 if (restart_edit != 0 && mode_displayed)
3717 clear_cmdline = TRUE; // remove "(insert)"
3718 restart_edit = 0;
3719 }
3720
3721 return NULL;
3722}
3723
3724#if defined(FEAT_LANGMAP) || defined(PROTO)
3725/*
3726 * Process the updated 'langnoremap' option value.
3727 */
3728 char *
3729did_set_langnoremap(optset_T *args UNUSED)
3730{
3731 // 'langnoremap' -> !'langremap'
3732 p_lrm = !p_lnr;
3733 return NULL;
3734}
3735
3736/*
3737 * Process the updated 'langremap' option value.
3738 */
3739 char *
3740did_set_langremap(optset_T *args UNUSED)
3741{
3742 // 'langremap' -> !'langnoremap'
3743 p_lnr = !p_lrm;
3744 return NULL;
3745}
3746#endif
3747
3748/*
3749 * Process the new 'laststatus' option value.
3750 */
3751 char *
3752did_set_laststatus(optset_T *args UNUSED)
3753{
3754 last_status(FALSE); // (re)set last window status line
3755 return NULL;
3756}
3757
3758#if defined(FEAT_GUI) || defined(PROTO)
3759/*
3760 * Process the new 'linespace' option value.
3761 */
3762 char *
3763did_set_linespace(optset_T *args UNUSED)
3764{
3765 // Recompute gui.char_height and resize the Vim window to keep the
3766 // same number of lines.
3767 if (gui.in_use && gui_mch_adjust_charheight() == OK)
3768 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
3769 return NULL;
3770}
3771#endif
3772
3773/*
3774 * Process the updated 'lisp' option value.
3775 */
3776 char *
3777did_set_lisp(optset_T *args UNUSED)
3778{
3779 // When 'lisp' option changes include/exclude '-' in keyword characters.
3780 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
3781 return NULL;
3782}
3783
3784/*
3785 * Process the new 'maxcombine' option value.
3786 */
3787 char *
3788did_set_maxcombine(optset_T *args UNUSED)
3789{
3790 if (p_mco > MAX_MCO)
3791 p_mco = MAX_MCO;
3792 else if (p_mco < 0)
3793 p_mco = 0;
3794 screenclear(); // will re-allocate the screen
3795 return NULL;
3796}
3797
3798/*
3799 * Process the updated 'modifiable' option value.
3800 */
3801 char *
3802did_set_modifiable(optset_T *args UNUSED)
3803{
3804 // when 'modifiable' is changed, redraw the window title
3805
3806# ifdef FEAT_TERMINAL
3807 // Cannot set 'modifiable' when in Terminal mode.
3808 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3809 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
3810 {
3811 curbuf->b_p_ma = FALSE;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003812 return e_cannot_make_terminal_with_running_job_modifiable;
3813 }
3814# endif
3815 redraw_titles();
3816
3817 return NULL;
3818}
3819
3820/*
3821 * Process the updated 'modified' option value.
3822 */
3823 char *
3824did_set_modified(optset_T *args)
3825{
3826 if (!args->os_newval.boolean)
3827 save_file_ff(curbuf); // Buffer is unchanged
3828 redraw_titles();
zeertzjq5bf6c212024-03-31 18:41:27 +02003829 curbuf->b_modified_was_set = args->os_newval.boolean;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003830 return NULL;
3831}
3832
3833#if defined(FEAT_GUI) || defined(PROTO)
3834/*
3835 * Process the updated 'mousehide' option value.
3836 */
3837 char *
3838did_set_mousehide(optset_T *args UNUSED)
3839{
3840 if (!p_mh)
3841 gui_mch_mousehide(FALSE);
3842 return NULL;
3843}
3844#endif
3845
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003846/*
3847 * Process the updated 'number' or 'relativenumber' option value.
3848 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003849 char *
3850did_set_number_relativenumber(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003851{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003852#if (defined(FEAT_SIGNS) && defined(FEAT_GUI)) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003853 if (gui.in_use
3854 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3855 && curbuf->b_signlist != NULL)
3856 {
3857 // If the 'number' or 'relativenumber' options are modified and
3858 // 'signcolumn' is set to 'number', then clear the screen for a full
3859 // refresh. Otherwise the sign icons are not displayed properly in the
3860 // number column. If the 'number' option is set and only the
3861 // 'relativenumber' option is toggled, then don't refresh the screen
3862 // (optimization).
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003863 if (!(curwin->w_p_nu && ((int *)args->os_varp == &curwin->w_p_rnu)))
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003864 redraw_all_later(UPD_CLEAR);
3865 }
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003866#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003867 return NULL;
3868}
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003869
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003870#if defined(FEAT_LINEBREAK) || defined(PROTO)
3871/*
3872 * Process the new 'numberwidth' option value.
3873 */
3874 char *
3875did_set_numberwidth(optset_T *args UNUSED)
3876{
3877 char *errmsg = NULL;
3878
3879 // 'numberwidth' must be positive
3880 if (curwin->w_p_nuw < 1)
3881 {
3882 errmsg = e_argument_must_be_positive;
3883 curwin->w_p_nuw = 1;
3884 }
Milly11807282024-10-23 21:42:41 +02003885 else if (curwin->w_p_nuw > 20)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003886 {
3887 errmsg = e_invalid_argument;
3888 curwin->w_p_nuw = 20;
3889 }
Milly11807282024-10-23 21:42:41 +02003890 if (curwin->w_allbuf_opt.wo_nuw < 1)
3891 {
3892 errmsg = e_argument_must_be_positive;
3893 curwin->w_allbuf_opt.wo_nuw = 1;
3894 }
3895 else if (curwin->w_allbuf_opt.wo_nuw > 20)
3896 {
3897 errmsg = e_invalid_argument;
3898 curwin->w_allbuf_opt.wo_nuw = 20;
3899 }
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003900 curwin->w_nrwidth_line_count = 0; // trigger a redraw
3901
3902 return errmsg;
3903}
3904#endif
3905
3906/*
3907 * Process the updated 'paste' option value. Called after p_paste was set or
3908 * reset. When 'paste' is set or reset also change other options.
3909 */
3910 char *
3911did_set_paste(optset_T *args UNUSED)
3912{
3913 static int old_p_paste = FALSE;
3914 static int save_sm = 0;
3915 static int save_sta = 0;
3916 static int save_ru = 0;
3917#ifdef FEAT_RIGHTLEFT
3918 static int save_ri = 0;
3919 static int save_hkmap = 0;
3920#endif
3921 buf_T *buf;
3922
3923 if (p_paste)
3924 {
3925 // Paste switched from off to on.
3926 // Save the current values, so they can be restored later.
3927 if (!old_p_paste)
3928 {
3929 // save options for each buffer
3930 FOR_ALL_BUFFERS(buf)
3931 {
3932 buf->b_p_tw_nopaste = buf->b_p_tw;
3933 buf->b_p_wm_nopaste = buf->b_p_wm;
3934 buf->b_p_sts_nopaste = buf->b_p_sts;
3935 buf->b_p_ai_nopaste = buf->b_p_ai;
3936 buf->b_p_et_nopaste = buf->b_p_et;
3937#ifdef FEAT_VARTABS
3938 if (buf->b_p_vsts_nopaste)
3939 vim_free(buf->b_p_vsts_nopaste);
3940 buf->b_p_vsts_nopaste =
3941 buf->b_p_vsts && buf->b_p_vsts != empty_option
3942 ? vim_strsave(buf->b_p_vsts) : NULL;
3943#endif
3944 }
3945
3946 // save global options
3947 save_sm = p_sm;
3948 save_sta = p_sta;
3949 save_ru = p_ru;
3950#ifdef FEAT_RIGHTLEFT
3951 save_ri = p_ri;
3952 save_hkmap = p_hkmap;
3953#endif
3954 // save global values for local buffer options
3955 p_ai_nopaste = p_ai;
3956 p_et_nopaste = p_et;
3957 p_sts_nopaste = p_sts;
3958 p_tw_nopaste = p_tw;
3959 p_wm_nopaste = p_wm;
3960#ifdef FEAT_VARTABS
3961 if (p_vsts_nopaste)
3962 vim_free(p_vsts_nopaste);
3963 p_vsts_nopaste = p_vsts && p_vsts != empty_option
3964 ? vim_strsave(p_vsts) : NULL;
3965#endif
3966 }
3967
3968 // Always set the option values, also when 'paste' is set when it is
3969 // already on. Set options for each buffer.
3970 FOR_ALL_BUFFERS(buf)
3971 {
3972 buf->b_p_tw = 0; // textwidth is 0
3973 buf->b_p_wm = 0; // wrapmargin is 0
3974 buf->b_p_sts = 0; // softtabstop is 0
3975 buf->b_p_ai = 0; // no auto-indent
3976 buf->b_p_et = 0; // no expandtab
3977#ifdef FEAT_VARTABS
3978 if (buf->b_p_vsts)
3979 free_string_option(buf->b_p_vsts);
3980 buf->b_p_vsts = empty_option;
3981 VIM_CLEAR(buf->b_p_vsts_array);
3982#endif
3983 }
3984
3985 // set global options
3986 p_sm = 0; // no showmatch
3987 p_sta = 0; // no smarttab
3988 if (p_ru)
3989 status_redraw_all(); // redraw to remove the ruler
3990 p_ru = 0; // no ruler
3991#ifdef FEAT_RIGHTLEFT
3992 p_ri = 0; // no reverse insert
3993 p_hkmap = 0; // no Hebrew keyboard
3994#endif
3995 // set global values for local buffer options
3996 p_tw = 0;
3997 p_wm = 0;
3998 p_sts = 0;
3999 p_ai = 0;
Christian Brabandt757593c2023-08-22 21:44:10 +02004000 p_et = 0;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004001#ifdef FEAT_VARTABS
4002 if (p_vsts)
4003 free_string_option(p_vsts);
4004 p_vsts = empty_option;
4005#endif
4006 }
4007
4008 // Paste switched from on to off: Restore saved values.
4009 else if (old_p_paste)
4010 {
4011 // restore options for each buffer
4012 FOR_ALL_BUFFERS(buf)
4013 {
4014 buf->b_p_tw = buf->b_p_tw_nopaste;
4015 buf->b_p_wm = buf->b_p_wm_nopaste;
4016 buf->b_p_sts = buf->b_p_sts_nopaste;
4017 buf->b_p_ai = buf->b_p_ai_nopaste;
4018 buf->b_p_et = buf->b_p_et_nopaste;
4019#ifdef FEAT_VARTABS
4020 if (buf->b_p_vsts)
4021 free_string_option(buf->b_p_vsts);
4022 buf->b_p_vsts = buf->b_p_vsts_nopaste
4023 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
4024 vim_free(buf->b_p_vsts_array);
4025 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
4026 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
4027 else
4028 buf->b_p_vsts_array = NULL;
4029#endif
4030 }
4031
4032 // restore global options
4033 p_sm = save_sm;
4034 p_sta = save_sta;
4035 if (p_ru != save_ru)
4036 status_redraw_all(); // redraw to draw the ruler
4037 p_ru = save_ru;
4038#ifdef FEAT_RIGHTLEFT
4039 p_ri = save_ri;
4040 p_hkmap = save_hkmap;
4041#endif
4042 // set global values for local buffer options
4043 p_ai = p_ai_nopaste;
4044 p_et = p_et_nopaste;
4045 p_sts = p_sts_nopaste;
4046 p_tw = p_tw_nopaste;
4047 p_wm = p_wm_nopaste;
4048#ifdef FEAT_VARTABS
4049 if (p_vsts)
4050 free_string_option(p_vsts);
4051 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
4052#endif
4053 }
4054
4055 old_p_paste = p_paste;
4056
Christian Brabandt757593c2023-08-22 21:44:10 +02004057#if defined(FEAT_EVAL) || defined(PROTO)
4058 // Remember where the dependent options were reset
4059 didset_options_sctx((OPT_LOCAL | OPT_GLOBAL), p_paste_dep_opts);
4060#endif
4061
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004062 return NULL;
4063}
4064
4065
4066#ifdef FEAT_QUICKFIX
4067/*
4068 * Process the updated 'previewwindow' option value.
4069 */
4070 char *
Yegappan Lakshmanan3ee25962023-12-04 20:31:14 +01004071did_set_previewwindow(optset_T *args UNUSED)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004072{
4073 if (!curwin->w_p_pvw)
4074 return NULL;
4075
4076 // There can be only one window with 'previewwindow' set.
4077 win_T *win;
4078
4079 FOR_ALL_WINDOWS(win)
4080 if (win->w_p_pvw && win != curwin)
4081 {
4082 curwin->w_p_pvw = FALSE;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004083 return e_preview_window_already_exists;
4084 }
4085
4086 return NULL;
4087}
4088#endif
4089
4090#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
4091/*
4092 * Process the new 'pyxversion' option value.
4093 */
4094 char *
4095did_set_pyxversion(optset_T *args UNUSED)
4096{
4097 char *errmsg = NULL;
4098
4099 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
4100 errmsg = e_invalid_argument;
4101
4102 return errmsg;
4103}
4104#endif
4105
4106/*
4107 * Process the updated 'readonly' option value.
4108 */
4109 char *
4110did_set_readonly(optset_T *args)
4111{
4112 // when 'readonly' is reset globally, also reset readonlymode
4113 if (!curbuf->b_p_ro && (args->os_flags & OPT_LOCAL) == 0)
4114 readonlymode = FALSE;
4115
4116 // when 'readonly' is set may give W10 again
4117 if (curbuf->b_p_ro)
4118 curbuf->b_did_warn = FALSE;
4119
4120 redraw_titles();
4121
4122 return NULL;
4123}
4124
4125/*
4126 * Process the updated 'scrollbind' option value.
4127 */
4128 char *
4129did_set_scrollbind(optset_T *args UNUSED)
4130{
4131 // when 'scrollbind' is set: snapshot the current position to avoid a jump
4132 // at the end of normal_cmd()
4133 if (!curwin->w_p_scb)
4134 return NULL;
4135
4136 do_check_scrollbind(FALSE);
4137 curwin->w_scbind_pos = curwin->w_topline;
4138 return NULL;
4139}
4140
4141#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
4142/*
4143 * Process the updated 'shellslash' option value.
4144 */
4145 char *
4146did_set_shellslash(optset_T *args UNUSED)
4147{
4148 if (p_ssl)
4149 {
4150 psepc = '/';
4151 psepcN = '\\';
4152 pseps[0] = '/';
4153 }
4154 else
4155 {
4156 psepc = '\\';
4157 psepcN = '/';
4158 pseps[0] = '\\';
4159 }
4160
4161 // need to adjust the file name arguments and buffer names.
4162 buflist_slash_adjust();
4163 alist_slash_adjust();
4164# ifdef FEAT_EVAL
4165 scriptnames_slash_adjust();
4166# endif
4167 return NULL;
4168}
4169#endif
4170
4171/*
4172 * Process the new 'shiftwidth' or the 'tabstop' option value.
4173 */
4174 char *
4175did_set_shiftwidth_tabstop(optset_T *args)
4176{
4177 long *pp = (long *)args->os_varp;
4178 char *errmsg = NULL;
4179
Milly11807282024-10-23 21:42:41 +02004180 if (curbuf->b_p_ts <= 0)
4181 {
4182 errmsg = e_argument_must_be_positive;
4183 curbuf->b_p_ts = 8;
4184 }
4185 else if (curbuf->b_p_ts > TABSTOP_MAX)
4186 {
4187 errmsg = e_invalid_argument;
4188 curbuf->b_p_ts = 8;
4189 }
4190 if (p_ts <= 0)
4191 {
4192 errmsg = e_argument_must_be_positive;
4193 p_ts = 8;
4194 }
4195 else if (p_ts > TABSTOP_MAX)
4196 {
4197 errmsg = e_invalid_argument;
4198 p_ts = 8;
4199 }
4200
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004201 if (curbuf->b_p_sw < 0)
4202 {
4203 errmsg = e_argument_must_be_positive;
4204#ifdef FEAT_VARTABS
4205 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4206 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4207 ? tabstop_first(curbuf->b_p_vts_array)
4208 : curbuf->b_p_ts;
4209#else
4210 curbuf->b_p_sw = curbuf->b_p_ts;
4211#endif
4212 }
Milly11807282024-10-23 21:42:41 +02004213 if (p_sw < 0)
4214 {
4215 errmsg = e_argument_must_be_positive;
4216#ifdef FEAT_VARTABS
4217 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4218 p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4219 ? tabstop_first(curbuf->b_p_vts_array)
4220 : curbuf->b_p_ts;
4221#else
4222 p_sw = curbuf->b_p_ts;
4223#endif
4224 }
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004225
4226#ifdef FEAT_FOLDING
4227 if (foldmethodIsIndent(curwin))
4228 foldUpdateAll(curwin);
4229#endif
4230 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
4231 // parse 'cinoptions'.
4232 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
4233 parse_cino(curbuf);
4234
4235 return errmsg;
4236}
4237
4238/*
4239 * Process the new 'showtabline' option value.
4240 */
4241 char *
4242did_set_showtabline(optset_T *args UNUSED)
4243{
4244 shell_new_rows(); // recompute window positions and heights
4245 return NULL;
4246}
4247
4248/*
4249 * Process the updated 'smoothscroll' option value.
4250 */
4251 char *
4252did_set_smoothscroll(optset_T *args UNUSED)
4253{
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004254 if (!curwin->w_p_sms)
4255 curwin->w_skipcol = 0;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004256
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004257 return NULL;
4258}
4259
4260#if defined(FEAT_SPELL) || defined(PROTO)
4261/*
4262 * Process the updated 'spell' option value.
4263 */
4264 char *
4265did_set_spell(optset_T *args UNUSED)
4266{
4267 if (curwin->w_p_spell)
4268 return parse_spelllang(curwin);
4269
4270 return NULL;
4271}
4272#endif
4273
4274/*
4275 * Process the updated 'swapfile' option value.
4276 */
4277 char *
4278did_set_swapfile(optset_T *args UNUSED)
4279{
4280 // when 'swf' is set, create swapfile, when reset remove swapfile
4281 if (curbuf->b_p_swf && p_uc)
4282 ml_open_file(curbuf); // create the swap file
4283 else
4284 // no need to reset curbuf->b_may_swap, ml_open_file() will check
4285 // buf->b_p_swf
4286 mf_close_file(curbuf, TRUE); // remove the swap file
4287 return NULL;
4288}
4289
4290#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004291 char *
4292did_set_termguicolors(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004293{
4294# ifdef FEAT_VTP
4295 // Do not turn on 'tgc' when 24-bit colors are not supported.
4296 if (
4297# ifdef VIMDLL
4298 !gui.in_use && !gui.starting &&
4299# endif
4300 !has_vtp_working())
4301 {
Christian Brabandtd7f58542025-01-31 16:13:14 +01004302 p_tgc_set = TRUE;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004303 p_tgc = 0;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004304 return e_24_bit_colors_are_not_supported_on_this_environment;
4305 }
4306 if (is_term_win32())
4307 swap_tcap();
4308# endif
4309# ifdef FEAT_GUI
4310 if (!gui.in_use && !gui.starting)
4311# endif
4312 highlight_gui_started();
4313# ifdef FEAT_VTP
4314 // reset t_Co
4315 if (is_term_win32())
4316 {
4317 control_console_color_rgb();
4318 set_termname(T_NAME);
4319 init_highlight(TRUE, FALSE);
4320 }
4321# endif
4322# ifdef FEAT_TERMINAL
4323 term_update_colors_all();
4324 term_update_palette_all();
4325 term_update_wincolor_all();
4326# endif
Christian Brabandtd7f58542025-01-31 16:13:14 +01004327 p_tgc_set = TRUE;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004328
4329 return NULL;
4330}
4331#endif
4332
Milly6d5f4a02024-10-22 23:17:45 +02004333#if defined(FEAT_TERMINAL) || defined(PROTO)
4334/*
4335 * Process the updated 'termwinscroll' option value.
4336 */
4337 char *
4338did_set_termwinscroll(optset_T *args)
4339{
4340 long *pp = (long *)args->os_varp;
4341 char *errmsg = NULL;
4342
4343 if (*pp < 1)
4344 {
4345 errmsg = e_argument_must_be_positive;
4346 *pp = 1;
4347 }
4348
4349 return errmsg;
4350}
4351#endif
4352
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004353/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004354 * Process the updated 'terse' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004356 char *
4357did_set_terse(optset_T *args UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004359 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004361 // when 'terse' is set change 'shortmess'
4362 p = vim_strchr(p_shm, SHM_SEARCH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004364 // insert 's' in p_shm
4365 if (p_terse && p == NULL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004366 {
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004367 STRCPY(IObuff, p_shm);
4368 STRCAT(IObuff, "s");
4369 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004370 }
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004371 // remove 's' from p_shm
4372 else if (!p_terse && p != NULL)
4373 STRMOVE(p, p + 1);
4374 return NULL;
4375}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004377/*
4378 * Process the updated 'textauto' option value.
4379 */
4380 char *
4381did_set_textauto(optset_T *args)
4382{
4383 // when 'textauto' is set or reset also change 'fileformats'
4384 set_string_option_direct((char_u *)"ffs", -1,
4385 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
4386 OPT_FREE | args->os_flags, 0);
Bram Moolenaar53744302015-07-17 17:38:22 +02004387
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004388 return NULL;
4389}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004391/*
4392 * Process the updated 'textmode' option value.
4393 */
4394 char *
4395did_set_textmode(optset_T *args)
4396{
4397 // when 'textmode' is set or reset also change 'fileformat'
4398 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, args->os_flags);
4399
4400 return NULL;
4401}
4402
4403/*
4404 * Process the new 'textwidth' option value.
4405 */
4406 char *
4407did_set_textwidth(optset_T *args UNUSED)
4408{
4409 char *errmsg = NULL;
4410
4411 if (curbuf->b_p_tw < 0)
4412 {
4413 errmsg = e_argument_must_be_positive;
4414 curbuf->b_p_tw = 0;
4415 }
Milly11807282024-10-23 21:42:41 +02004416 if (p_tw < 0)
4417 {
4418 errmsg = e_argument_must_be_positive;
4419 p_tw = 0;
4420 }
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004421#ifdef FEAT_SYN_HL
4422 {
4423 win_T *wp;
4424 tabpage_T *tp;
4425
4426 FOR_ALL_TAB_WINDOWS(tp, wp)
Millya441a3e2024-10-22 22:43:01 +02004427 check_colorcolumn(NULL, wp);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004428 }
Bram Moolenaar53744302015-07-17 17:38:22 +02004429#endif
4430
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004431 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432}
4433
4434/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004435 * Process the updated 'title' or the 'icon' option value.
4436 */
4437 char *
4438did_set_title_icon(optset_T *args UNUSED)
4439{
4440 // when 'title' changed, may need to change the title; same for 'icon'
4441 did_set_title();
4442 return NULL;
4443}
4444
4445/*
4446 * Process the new 'titlelen' option value.
4447 */
4448 char *
4449did_set_titlelen(optset_T *args)
4450{
4451 long old_value = args->os_oldval.number;
4452 char *errmsg = NULL;
4453
4454 // if 'titlelen' has changed, redraw the title
4455 if (p_titlelen < 0)
4456 {
4457 errmsg = e_argument_must_be_positive;
4458 p_titlelen = 85;
4459 }
4460 if (starting != NO_SCREEN && old_value != p_titlelen)
4461 need_maketitle = TRUE;
4462
4463 return errmsg;
4464}
4465
4466#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
4467/*
4468 * Process the updated 'undofile' option value.
4469 */
4470 char *
4471did_set_undofile(optset_T *args)
4472{
4473 // Only take action when the option was set.
4474 if (!curbuf->b_p_udf && !p_udf)
4475 return NULL;
4476
4477 // When reset we do not delete the undo file, the option may be set again
4478 // without making any changes in between.
4479 char_u hash[UNDO_HASH_SIZE];
4480 buf_T *save_curbuf = curbuf;
4481
4482 FOR_ALL_BUFFERS(curbuf)
4483 {
4484 // When 'undofile' is set globally: for every buffer, otherwise
4485 // only for the current buffer: Try to read in the undofile,
4486 // if one exists, the buffer wasn't changed and the buffer was
4487 // loaded
4488 if ((curbuf == save_curbuf
4489 || (args->os_flags & OPT_GLOBAL)
4490 || args->os_flags == 0)
4491 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
4492 {
4493#ifdef FEAT_CRYPT
Christian Brabandtaae58342023-04-23 17:50:22 +01004494 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004495 continue;
4496#endif
4497 u_compute_hash(hash);
4498 u_read_undo(NULL, hash, curbuf->b_fname);
4499 }
4500 }
4501 curbuf = save_curbuf;
4502
4503 return NULL;
4504}
4505#endif
4506
4507/*
4508 * Process the new global 'undolevels' option value.
4509 */
4510 static void
4511update_global_undolevels(long value, long old_value)
4512{
4513 // sync undo before 'undolevels' changes
4514
4515 // use the old value, otherwise u_sync() may not work properly
4516 p_ul = old_value;
4517 u_sync(TRUE);
4518 p_ul = value;
4519}
4520
4521/*
4522 * Process the new buffer local 'undolevels' option value.
4523 */
4524 static void
4525update_buflocal_undolevels(long value, long old_value)
4526{
4527 // use the old value, otherwise u_sync() may not work properly
4528 curbuf->b_p_ul = old_value;
4529 u_sync(TRUE);
4530 curbuf->b_p_ul = value;
4531}
4532
4533/*
4534 * Process the new 'undolevels' option value.
4535 */
4536 char *
4537did_set_undolevels(optset_T *args)
4538{
4539 long *pp = (long *)args->os_varp;
4540
4541 if (pp == &p_ul) // global 'undolevels'
4542 update_global_undolevels(args->os_newval.number,
4543 args->os_oldval.number);
4544 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4545 update_buflocal_undolevels(args->os_newval.number,
4546 args->os_oldval.number);
4547
4548 return NULL;
4549}
4550
4551/*
4552 * Process the new 'updatecount' option value.
4553 */
4554 char *
4555did_set_updatecount(optset_T *args)
4556{
4557 long old_value = args->os_oldval.number;
4558 char *errmsg = NULL;
4559
4560 // when 'updatecount' changes from zero to non-zero, open swap files
4561 if (p_uc < 0)
4562 {
4563 errmsg = e_argument_must_be_positive;
4564 p_uc = 100;
4565 }
4566 if (p_uc && !old_value)
4567 ml_open_files();
4568
4569 return errmsg;
4570}
4571
4572/*
4573 * Process the updated 'weirdinvert' option value.
4574 */
4575 char *
4576did_set_weirdinvert(optset_T *args)
4577{
4578 // When 'weirdinvert' changed, set/reset 't_xs'.
4579 // Then set 'weirdinvert' according to value of 't_xs'.
4580 if (p_wiv && !args->os_oldval.boolean)
4581 T_XS = (char_u *)"y";
4582 else if (!p_wiv && args->os_oldval.boolean)
4583 T_XS = empty_option;
4584 p_wiv = (*T_XS != NUL);
4585
4586 return NULL;
4587}
4588
4589/*
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +02004590 * Process the new 'wildchar' / 'wildcharm' option value.
4591 */
4592 char *
4593did_set_wildchar(optset_T *args)
4594{
4595 long c = *(long *)args->os_varp;
4596
4597 // Don't allow key values that wouldn't work as wildchar.
4598 if (c == Ctrl_C || c == '\n' || c == '\r' || c == K_KENTER)
4599 return e_invalid_argument;
4600
4601 return NULL;
4602}
4603
4604/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004605 * Process the new 'window' option value.
4606 */
4607 char *
4608did_set_window(optset_T *args UNUSED)
4609{
4610 if (p_window < 1)
4611 p_window = 1;
4612 else if (p_window >= Rows)
4613 p_window = Rows - 1;
4614 return NULL;
4615}
4616
4617/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004618 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004620 char *
4621did_set_winheight_helpheight(optset_T *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004623 long *pp = (long *)args->os_varp;
4624 char *errmsg = NULL;
4625
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004626 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004628 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004629 p_wh = 1;
4630 }
4631 if (p_wmh > p_wh)
4632 {
4633 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4634 p_wh = p_wmh;
4635 }
4636 if (p_hh < 0)
4637 {
4638 errmsg = e_argument_must_be_positive;
4639 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 }
4641
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004642 // Change window height NOW
4643 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004645 if (pp == &p_wh && curwin->w_height < p_wh)
4646 win_setheight((int)p_wh);
4647 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
4648 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004651 return errmsg;
4652}
4653
4654/*
4655 * Process the new 'winminheight' option value.
4656 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004657 char *
4658did_set_winminheight(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004659{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004660 char *errmsg = NULL;
4661
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004662 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004664 errmsg = e_argument_must_be_positive;
4665 p_wmh = 0;
4666 }
4667 if (p_wmh > p_wh)
4668 {
4669 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4670 p_wmh = p_wh;
4671 }
4672 win_setminheight();
4673
4674 return errmsg;
4675}
4676
4677/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004678 * Process the new 'winminwidth' option value.
4679 */
4680 char *
4681did_set_winminwidth(optset_T *args UNUSED)
4682{
4683 char *errmsg = NULL;
4684
4685 if (p_wmw < 0)
4686 {
4687 errmsg = e_argument_must_be_positive;
4688 p_wmw = 0;
4689 }
4690 if (p_wmw > p_wiw)
4691 {
4692 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4693 p_wmw = p_wiw;
4694 }
4695 win_setminwidth();
4696
4697 return errmsg;
4698}
4699
4700/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004701 * Process the new 'winwidth' option value.
4702 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004703 char *
4704did_set_winwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004705{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004706 char *errmsg = NULL;
4707
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004708 if (p_wiw < 1)
4709 {
4710 errmsg = e_argument_must_be_positive;
4711 p_wiw = 1;
4712 }
4713 if (p_wmw > p_wiw)
4714 {
4715 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4716 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004718
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004719 // Change window width NOW
4720 if (!ONE_WINDOW && curwin->w_width < p_wiw)
4721 win_setwidth((int)p_wiw);
4722
4723 return errmsg;
4724}
4725
Foxe Chenb90c2392025-06-27 21:10:35 +02004726#ifdef FEAT_WAYLAND_CLIPBOARD
4727/*
4728 * Process the new 'wlsteal' option value.
4729 */
4730 char *
4731did_set_wlsteal(optset_T *args UNUSED)
4732{
4733 wayland_cb_reload();
4734
4735 return NULL;
4736}
4737#endif
4738
4739#ifdef FEAT_WAYLAND
4740/*
4741 * Process the new 'wltimeoutlen' option value.
4742 */
4743 char *
4744did_set_wltimeoutlen(optset_T *args)
4745{
4746 if (p_wtm < 0)
4747 {
4748 p_wtm = args->os_oldval.number;
4749 return e_argument_must_be_positive;
4750 }
4751
4752 return NULL;
4753}
4754#endif
4755
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004756/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004757 * Process the updated 'wrap' option value.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004758 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004759 char *
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004760did_set_wrap(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004761{
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004762 // Set w_leftcol or w_skipcol to zero.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004763 if (curwin->w_p_wrap)
4764 curwin->w_leftcol = 0;
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004765 else
4766 curwin->w_skipcol = 0;
4767
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004768 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004769}
4770
64-bitman88d41ab2025-04-06 17:20:39 +02004771#ifdef FEAT_QUICKFIX
4772/*
4773 * Process the new 'chistory' or 'lhistory' option value. 'chistory' will
4774 * be used if args->os_varp is the same as p_chi, else 'lhistory'.
4775 */
4776 char *
4777did_set_xhistory(optset_T *args)
4778{
4779 int is_p_chi = (long*)args->os_varp == &p_chi;
4780 int err;
4781 long *arg = (is_p_chi) ? &p_chi :(long*)args->os_varp;
4782
4783 // cannot have zero or negative number of quickfix lists in a stack
4784 if (*arg < 1)
4785 {
4786 *arg = args->os_oldval.number;
4787 return e_cannot_have_negative_or_zero_number_of_quickfix;
4788 }
4789
4790 // cannot have more than 100 quickfix lists in a stack
4791 if (*arg > 100)
4792 {
4793 *arg = args->os_oldval.number;
4794 return e_cannot_have_more_than_hundred_quickfix;
4795 }
4796
4797 if (is_p_chi)
Hirohito Higashiadcfb6c2025-04-07 21:19:07 +02004798 err = qf_resize_stack(*arg);
64-bitman88d41ab2025-04-06 17:20:39 +02004799 else
4800 err = ll_resize_stack(curwin, *arg);
4801
4802 if (err == FAIL)
4803 return e_failed_resizing_quickfix_stack;
4804
4805 return NULL;
4806}
4807#endif
4808
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004809/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004810 * Set the value of a boolean option, and take care of side effects.
4811 * Returns NULL for success, or an error message for an error.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004812 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004813 static char *
4814set_bool_option(
4815 int opt_idx, // index in options[] table
4816 char_u *varp, // pointer to the option variable
4817 int value, // new value
4818 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004819{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004820 int old_value = *(int *)varp;
4821#if defined(FEAT_EVAL)
4822 int old_global_value = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004824 char *errmsg = NULL;
4825
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004826 // Disallow changing some options from secure mode
4827 if ((secure
4828#ifdef HAVE_SANDBOX
4829 || sandbox != 0
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004830#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004831 ) && (options[opt_idx].flags & P_SECURE))
4832 return e_not_allowed_here;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004833
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004834#if defined(FEAT_EVAL)
4835 // Save the global value before changing anything. This is needed as for
4836 // a global-only option setting the "local value" in fact sets the global
4837 // value (since there is only one value).
4838 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4839 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
4840 OPT_GLOBAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004842
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004843 *(int *)varp = value; // set the new value
4844#ifdef FEAT_EVAL
4845 // Remember where the option was set.
4846 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004847#endif
4848
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004850 need_mouse_correct = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004853 // May set global value for local option.
4854 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4855 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004856
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004857 // Handle side effects of changing a bool option.
4858 if (options[opt_idx].opt_did_set_cb != NULL)
4859 {
4860 optset_T args;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004861
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004862 CLEAR_FIELD(args);
4863 args.os_varp = varp;
4864 args.os_flags = opt_flags;
4865 args.os_oldval.boolean = old_value;
4866 args.os_newval.boolean = value;
4867 args.os_errbuf = NULL;
4868 errmsg = options[opt_idx].opt_did_set_cb(&args);
zeertzjqdeba02d2023-11-02 21:01:19 +01004869 if (errmsg != NULL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004870 return errmsg;
4871 }
4872
4873 // after handling side effects, call autocommand
4874
4875 options[opt_idx].flags |= P_WAS_SET;
4876
4877#if defined(FEAT_EVAL)
4878 apply_optionset_autocmd(opt_idx, opt_flags,
4879 (long)(old_value ? TRUE : FALSE),
4880 (long)(old_global_value ? TRUE : FALSE),
4881 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004882#endif
4883
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004884 comp_col(); // in case 'ruler' or 'showcmd' changed
zeertzjqfcaed6a2024-02-18 09:33:54 +01004885
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004886 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004887 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0
4888 && (options[opt_idx].flags & P_HLONLY) == 0)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004889 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004890
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004891 if ((opt_flags & OPT_NO_REDRAW) == 0)
4892 check_redraw(options[opt_idx].flags);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004893
4894 return errmsg;
4895}
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004896
4897/*
4898 * Check the bounds of numeric options.
4899 */
4900 static char *
4901check_num_option_bounds(
4902 long *pp,
4903 long old_value,
4904 long old_Rows,
4905 long old_Columns,
4906 char *errbuf,
4907 size_t errbuflen,
4908 char *errmsg)
4909{
Milly2cddf0e2024-11-28 18:16:55 +01004910 if (Rows < min_rows_for_all_tabpages() && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 {
4912 if (errbuf != NULL)
4913 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004914 vim_snprintf(errbuf, errbuflen,
Milly2cddf0e2024-11-28 18:16:55 +01004915 _(e_need_at_least_nr_lines), min_rows_for_all_tabpages());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 errmsg = errbuf;
4917 }
Milly2cddf0e2024-11-28 18:16:55 +01004918 Rows = min_rows_for_all_tabpages();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 }
4920 if (Columns < MIN_COLUMNS && full_screen)
4921 {
4922 if (errbuf != NULL)
4923 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004924 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004925 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 errmsg = errbuf;
4927 }
4928 Columns = MIN_COLUMNS;
4929 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004930 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004932 // If the screen (shell) height has been changed, assume it is the
4933 // physical screenheight.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 if (old_Rows != Rows || old_Columns != Columns)
4935 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004936 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 if (updating_screen)
4938 *pp = old_value;
4939 else if (full_screen
4940#ifdef FEAT_GUI
4941 && !gui.starting
4942#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004943 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 set_shellsize((int)Columns, (int)Rows, TRUE);
4945 else
4946 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004947 // Postpone the resizing; check the size and cmdline position for
4948 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 check_shellsize();
4950 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4951 cmdline_row = Rows - p_ch;
4952 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004953 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004954 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 }
4956
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 if (p_tm < 0)
4958 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004959 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 p_tm = 0;
4961 }
4962 if ((curwin->w_p_scr <= 0
4963 || (curwin->w_p_scr > curwin->w_height
4964 && curwin->w_height > 0))
4965 && full_screen)
4966 {
4967 if (pp == &(curwin->w_p_scr))
4968 {
4969 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004970 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 win_comp_scroll(curwin);
4972 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004973 // If 'scroll' became invalid because of a side effect silently adjust
4974 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 else if (curwin->w_p_scr <= 0)
4976 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004977 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 curwin->w_p_scr = curwin->w_height;
4979 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004980 if (p_hi < 0)
4981 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004982 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004983 p_hi = 0;
4984 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004985 else if (p_hi > 10000)
4986 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004987 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004988 p_hi = 10000;
4989 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004990 if (p_re < 0 || p_re > 2)
4991 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004992 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004993 p_re = 0;
4994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 if (p_report < 0)
4996 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004997 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 p_report = 1;
4999 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00005000 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005002 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 p_sj = Rows / 2;
5004 else
5005 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005006 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 p_sj = 1;
5008 }
5009 }
5010 if (p_so < 0 && full_screen)
5011 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005012 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 p_so = 0;
5014 }
5015 if (p_siso < 0 && full_screen)
5016 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005017 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 p_siso = 0;
5019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 if (p_cwh < 1)
5021 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005022 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 p_cwh = 1;
5024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 if (p_ut < 0)
5026 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005027 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 p_ut = 2000;
5029 }
5030 if (p_ss < 0)
5031 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005032 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 p_ss = 0;
5034 }
5035
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00005036 return errmsg;
5037}
5038
5039/*
5040 * Set the value of a number option, and take care of side effects.
5041 * Returns NULL for success, or an error message for an error.
5042 */
5043 static char *
5044set_num_option(
5045 int opt_idx, // index in options[] table
5046 char_u *varp, // pointer to the option variable
5047 long value, // new value
5048 char *errbuf, // buffer for error messages
5049 size_t errbuflen, // length of "errbuf"
5050 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
5051 // OPT_MODELINE, etc.
5052{
5053 char *errmsg = NULL;
5054 long old_value = *(long *)varp;
5055#if defined(FEAT_EVAL)
5056 long old_global_value = 0; // only used when setting a local and
5057 // global option
5058#endif
5059 long old_Rows = Rows; // remember old Rows
5060 long old_Columns = Columns; // remember old Columns
5061 long *pp = (long *)varp;
5062
5063 // Disallow changing some options from secure mode.
5064 if ((secure
5065#ifdef HAVE_SANDBOX
5066 || sandbox != 0
5067#endif
5068 ) && (options[opt_idx].flags & P_SECURE))
5069 return e_not_allowed_here;
5070
5071#if defined(FEAT_EVAL)
5072 // Save the global value before changing anything. This is needed as for
5073 // a global-only option setting the "local value" in fact sets the global
5074 // value (since there is only one value).
5075 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
5076 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
5077 OPT_GLOBAL);
5078#endif
5079
5080 *pp = value;
5081#ifdef FEAT_EVAL
5082 // Remember where the option was set.
5083 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
5084#endif
5085#ifdef FEAT_GUI
5086 need_mouse_correct = TRUE;
5087#endif
5088
Milly11807282024-10-23 21:42:41 +02005089 // May set global value for local option.
5090 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
5091 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
5092
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00005093 // Invoke the option specific callback function to validate and apply the
5094 // new value.
5095 if (options[opt_idx].opt_did_set_cb != NULL)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00005096 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00005097 optset_T args;
5098
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00005099 CLEAR_FIELD(args);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00005100 args.os_varp = varp;
5101 args.os_flags = opt_flags;
5102 args.os_oldval.number = old_value;
5103 args.os_newval.number = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00005104 args.os_errbuf = NULL;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00005105 errmsg = options[opt_idx].opt_did_set_cb(&args);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00005106 }
5107
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00005108 // Check the bounds for numeric options here
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00005109 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
5110 errbuf, errbuflen, errmsg);
5111
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 options[opt_idx].flags |= P_WAS_SET;
5113
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005114#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01005115 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
5116 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02005117#endif
5118
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005119 comp_col(); // in case 'columns' or 'ls' changed
zeertzjqfcaed6a2024-02-18 09:33:54 +01005120
Bram Moolenaar913077c2012-03-28 19:59:04 +02005121 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01005122 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0
5123 && (options[opt_idx].flags & P_HLONLY) == 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02005124 curwin->w_set_curswant = TRUE;
zeertzjqfcaed6a2024-02-18 09:33:54 +01005125
Bram Moolenaar37294bd2021-03-10 13:40:08 +01005126 if ((opt_flags & OPT_NO_REDRAW) == 0)
5127 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128
5129 return errmsg;
5130}
5131
5132/*
5133 * Called after an option changed: check if something needs to be redrawn.
5134 */
Bram Moolenaardac13472019-09-16 21:06:21 +02005135 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005136check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005138 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01005139 int doclear = (flags & P_RCLR) == P_RCLR;
5140 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005142 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144
5145 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
zeertzjqfcaed6a2024-02-18 09:33:54 +01005146 {
5147 if (flags & P_HLONLY)
5148 redraw_later(UPD_NOT_VALID);
5149 else
5150 changed_window_setting();
5151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005153 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01005154 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005155 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005157 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158}
5159
5160/*
5161 * Find index for option 'arg'.
5162 * Return -1 if not found.
5163 */
Bram Moolenaardac13472019-09-16 21:06:21 +02005164 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005165findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166{
5167 int opt_idx;
5168 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005169 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 int is_term_opt;
5171
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005172 // For first call: Initialize the quick-access table.
5173 // It contains the index for the first option that starts with a certain
5174 // letter. There are 26 letters, plus the first "t_" option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 if (quick_tab[1] == 0)
5176 {
5177 p = options[0].fullname;
5178 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
5179 {
5180 if (s[0] != p[0])
5181 {
5182 if (s[0] == 't' && s[1] == '_')
5183 quick_tab[26] = opt_idx;
5184 else
5185 quick_tab[CharOrdLow(s[0])] = opt_idx;
5186 }
5187 p = s;
5188 }
5189 }
5190
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005191 // Check for name starting with an illegal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 return -1;
5194
5195 is_term_opt = (arg[0] == 't' && arg[1] == '_');
5196 if (is_term_opt)
5197 opt_idx = quick_tab[26];
5198 else
5199 opt_idx = quick_tab[CharOrdLow(arg[0])];
zeertzjqf48558e2023-12-08 21:34:31 +01005200 for (; (s = options[opt_idx].fullname) != NULL && s[0] == arg[0]; opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005202 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 break;
5204 }
zeertzjqf48558e2023-12-08 21:34:31 +01005205 if (s != NULL && s[0] != arg[0])
5206 s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 if (s == NULL && !is_term_opt)
5208 {
5209 opt_idx = quick_tab[CharOrdLow(arg[0])];
5210 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
5211 {
5212 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005213 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 break;
5215 s = NULL;
5216 }
5217 }
5218 if (s == NULL)
5219 opt_idx = -1;
5220 return opt_idx;
5221}
5222
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005223#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
5224 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225/*
5226 * Get the value for an option.
5227 *
5228 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005229 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01005230 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005231 * String option: gov_string, *stringval gets allocated string.
5232 * Hidden Number option: gov_hidden_number.
5233 * Hidden Toggle option: gov_hidden_bool.
5234 * Hidden String option: gov_hidden_string.
5235 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005236 *
5237 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005239 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01005240get_option_value(
5241 char_u *name,
5242 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005243 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005244 int *flagsp,
5245 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246{
5247 int opt_idx;
5248 char_u *varp;
5249
5250 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005251 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01005252 {
5253 int key;
5254
5255 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005256 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005257 {
5258 char_u key_name[2];
5259 char_u *p;
5260
Bram Moolenaar10c75c42021-12-28 20:53:30 +00005261 if (flagsp != NULL)
5262 *flagsp = 0; // terminal option has no flags
5263
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005264 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01005265 if (key < 0)
5266 {
5267 key_name[0] = KEY2TERMCAP0(key);
5268 key_name[1] = KEY2TERMCAP1(key);
5269 }
5270 else
5271 {
5272 key_name[0] = KS_KEY;
5273 key_name[1] = (key & 0xff);
5274 }
5275 p = find_termcode(key_name);
5276 if (p != NULL)
5277 {
5278 if (stringval != NULL)
5279 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005280 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01005281 }
5282 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005283 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01005284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005286 varp = get_varp_scope(&(options[opt_idx]), scope);
5287
5288 if (flagsp != NULL)
5289 // Return the P_xxxx option flags.
5290 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291
5292 if (options[opt_idx].flags & P_STRING)
5293 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005294 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005295 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 if (stringval != NULL)
5297 {
zeertzjq51f0bc32022-05-09 13:33:39 +01005298 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01005299 *stringval = str2special_save(*(char_u **)(varp), FALSE,
5300 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005302 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01005303 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00005304 && **(char_u **)(varp) != NUL)
John Marriott95dacbb2024-09-10 21:25:14 +02005305 *stringval = vim_strnsave((char_u *)"*****", STRLEN_LITERAL("*****"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01005307 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 *stringval = vim_strsave(*(char_u **)(varp));
5309 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005310 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 }
5312
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005313 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005314 return (options[opt_idx].flags & P_NUM)
5315 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 if (options[opt_idx].flags & P_NUM)
5317 *numval = *(long *)varp;
5318 else
5319 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005320 // Special case: 'modified' is b_changed, but we also want to consider
5321 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 if ((int *)varp == &curbuf->b_changed)
5323 *numval = curbufIsChanged();
5324 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02005325 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005327 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328}
5329#endif
5330
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005331#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005332/*
5333 * Returns the option attributes and its value. Unlike the above function it
5334 * will return either global value or local value of the option depending on
5335 * what was requested, but it will never return global value if it was
5336 * requested to return local one and vice versa. Neither it will return
5337 * buffer-local value if it was requested to return window-local one.
5338 *
5339 * Pretends that option is absent if it is not present in the requested scope
5340 * (i.e. has no global, window-local or buffer-local value depending on
5341 * opt_type). Uses
5342 *
5343 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02005344 * 0 hidden or unknown option, also option that does not have requested
5345 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005346 * see SOPT_* in vim.h for other flags
5347 *
5348 * Possible opt_type values: see SREQ_* in vim.h
5349 */
5350 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005351get_option_value_strict(
5352 char_u *name,
5353 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005354 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01005355 int opt_type,
5356 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005357{
5358 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02005359 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005360 struct vimoption *p;
5361 int r = 0;
5362
5363 opt_idx = findoption(name);
5364 if (opt_idx < 0)
5365 return 0;
5366
5367 p = &(options[opt_idx]);
5368
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005369 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005370 if (p->var == NULL)
5371 return 0;
5372
5373 if (p->flags & P_BOOL)
5374 r |= SOPT_BOOL;
5375 else if (p->flags & P_NUM)
5376 r |= SOPT_NUM;
5377 else if (p->flags & P_STRING)
5378 r |= SOPT_STRING;
5379
5380 if (p->indir == PV_NONE)
5381 {
5382 if (opt_type == SREQ_GLOBAL)
5383 r |= SOPT_GLOBAL;
5384 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005385 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005386 }
5387 else
5388 {
5389 if (p->indir & PV_BOTH)
5390 r |= SOPT_GLOBAL;
5391 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005392 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005393
5394 if (p->indir & PV_WIN)
5395 {
5396 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005397 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005398 else
5399 r |= SOPT_WIN;
5400 }
5401 else if (p->indir & PV_BUF)
5402 {
5403 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005404 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005405 else
5406 r |= SOPT_BUF;
5407 }
5408 }
5409
5410 if (stringval == NULL)
5411 return r;
5412
5413 if (opt_type == SREQ_GLOBAL)
5414 varp = p->var;
5415 else
5416 {
5417 if (opt_type == SREQ_BUF)
5418 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005419 // Special case: 'modified' is b_changed, but we also want to
5420 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005421 if (p->indir == PV_MOD)
5422 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005423 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005424 varp = NULL;
5425 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005426# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005427 else if (p->indir == PV_KEY)
5428 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005429 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005430 *stringval = NULL;
5431 varp = NULL;
5432 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005433# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005434 else
5435 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005436 buf_T *save_curbuf = curbuf;
5437
5438 // only getting a pointer, no need to use aucmd_prepbuf()
5439 curbuf = (buf_T *)from;
5440 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005441 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02005442 curbuf = save_curbuf;
5443 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005444 }
5445 }
5446 else if (opt_type == SREQ_WIN)
5447 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005448 win_T *save_curwin = curwin;
5449
5450 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005451 curbuf = curwin->w_buffer;
5452 varp = get_varp(p);
5453 curwin = save_curwin;
5454 curbuf = curwin->w_buffer;
5455 }
5456 if (varp == p->var)
5457 return (r | SOPT_UNSET);
5458 }
5459
5460 if (varp != NULL)
5461 {
5462 if (p->flags & P_STRING)
5463 *stringval = vim_strsave(*(char_u **)(varp));
5464 else if (p->flags & P_NUM)
5465 *numval = *(long *) varp;
5466 else
5467 *numval = *(int *)varp;
5468 }
5469
5470 return r;
5471}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005472
5473/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005474 * Iterate over options. First argument is a pointer to a pointer to a
5475 * structure inside options[] array, second is option type like in the above
5476 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005477 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005478 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005479 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005480 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005481 * first argument to NULL.
5482 *
5483 * Returns full option name for current option on each call.
5484 */
5485 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005486option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005487{
5488 struct vimoption *ret = NULL;
5489 do
5490 {
5491 if (*option == NULL)
5492 *option = (void *) options;
5493 else if (((struct vimoption *) (*option))->fullname == NULL)
5494 {
5495 *option = NULL;
5496 return NULL;
5497 }
5498 else
5499 *option = (void *) (((struct vimoption *) (*option)) + 1);
5500
5501 ret = ((struct vimoption *) (*option));
5502
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005503 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005504 if (ret->var == NULL)
5505 {
5506 ret = NULL;
5507 continue;
5508 }
5509
5510 switch (opt_type)
5511 {
5512 case SREQ_GLOBAL:
5513 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
5514 ret = NULL;
5515 break;
5516 case SREQ_BUF:
5517 if (!(ret->indir & PV_BUF))
5518 ret = NULL;
5519 break;
5520 case SREQ_WIN:
5521 if (!(ret->indir & PV_WIN))
5522 ret = NULL;
5523 break;
5524 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005525 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005526 return NULL;
5527 }
5528 }
5529 while (ret == NULL);
5530
5531 return (char_u *)ret->fullname;
5532}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005533#endif
5534
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005536 * Return the flags for the option at 'opt_idx'.
5537 */
5538 long_u
5539get_option_flags(int opt_idx)
5540{
5541 return options[opt_idx].flags;
5542}
5543
5544/*
5545 * Set a flag for the option at 'opt_idx'.
5546 */
5547 void
5548set_option_flag(int opt_idx, long_u flag)
5549{
5550 options[opt_idx].flags |= flag;
5551}
5552
5553/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005554 * Returns TRUE if the option at 'opt_idx' is a global option
5555 */
5556 int
5557is_global_option(int opt_idx)
5558{
5559 return options[opt_idx].indir == PV_NONE;
5560}
5561
5562/*
5563 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5564 * local value.
5565 */
5566 int
5567is_global_local_option(int opt_idx)
5568{
5569 return options[opt_idx].indir & PV_BOTH;
5570}
5571
5572/*
5573 * Returns TRUE if the option at 'opt_idx' is a window-local option
5574 */
5575 int
5576is_window_local_option(int opt_idx)
5577{
5578 return options[opt_idx].var == VAR_WIN;
5579}
5580
5581/*
5582 * Returns TRUE if the option at 'opt_idx' is a hidden option
5583 */
5584 int
5585is_hidden_option(int opt_idx)
5586{
5587 return options[opt_idx].var == NULL;
5588}
5589
5590#if defined(FEAT_CRYPT) || defined(PROTO)
5591/*
5592 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5593 */
5594 int
5595is_crypt_key_option(int opt_idx)
5596{
5597 return options[opt_idx].indir == PV_KEY;
5598}
5599#endif
5600
5601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 * Set the value of option "name".
5603 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005604 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005605 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005607 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005608set_option_value(
5609 char_u *name,
5610 long number,
5611 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005612 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613{
5614 int opt_idx;
5615 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005616 long_u flags;
Christian Brabandtb39b2402023-11-29 11:34:05 +01005617 static char errbuf[ERR_BUFLEN];
5618 int errbuflen = ERR_BUFLEN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619
5620 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005621 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005622 {
5623 int key;
5624
5625 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005626 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005627 {
5628 char_u key_name[2];
5629
5630 if (key < 0)
5631 {
5632 key_name[0] = KEY2TERMCAP0(key);
5633 key_name[1] = KEY2TERMCAP1(key);
5634 }
5635 else
5636 {
5637 key_name[0] = KS_KEY;
5638 key_name[1] = (key & 0xff);
5639 }
5640 add_termcode(key_name, string, FALSE);
5641 if (full_screen)
5642 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005643 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005644 return NULL;
5645 }
5646
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005647 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 else
5650 {
5651 flags = options[opt_idx].flags;
5652#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005653 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005655 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005656 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005657 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005660 if (flags & P_STRING)
Christian Brabandtb39b2402023-11-29 11:34:05 +01005661 return set_string_option(opt_idx, string, opt_flags, errbuf, errbuflen);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005662
5663 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5664 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005666 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005668 int idx;
5669
5670 // Either we are given a string or we are setting option
5671 // to zero.
5672 for (idx = 0; string[idx] == '0'; ++idx)
5673 ;
5674 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005675 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005676 // There's another character after zeros or the string
5677 // is empty. In both cases, we are trying to set a
5678 // num option using a string.
5679 semsg(_(e_number_required_after_str_equal_str),
5680 name, string);
5681 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005682
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005685 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005686 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005687 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005688 errbuf, sizeof(errbuf), opt_flags);
5689 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005690 else
5691 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005693
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005695 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696}
5697
5698/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005699 * Call set_option_value() and when an error is returned report it.
5700 */
5701 void
5702set_option_value_give_err(
5703 char_u *name,
5704 long number,
5705 char_u *string,
5706 int opt_flags) // OPT_LOCAL or 0 (both)
5707{
5708 char *errmsg = set_option_value(name, number, string, opt_flags);
5709
5710 if (errmsg != NULL)
5711 emsg(_(errmsg));
5712}
5713
5714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 * Get the terminal code for a terminal option.
5716 * Returns NULL when not found.
5717 */
5718 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005719get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720{
5721 int opt_idx;
5722 char_u *varp;
5723
5724 if (tname[0] != 't' || tname[1] != '_' ||
5725 tname[2] == NUL || tname[3] == NUL)
5726 return NULL;
5727 if ((opt_idx = findoption(tname)) >= 0)
5728 {
5729 varp = get_varp(&(options[opt_idx]));
5730 if (varp != NULL)
5731 varp = *(char_u **)(varp);
5732 return varp;
5733 }
5734 return find_termcode(tname + 2);
5735}
5736
5737 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005738get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739{
5740 int i;
5741
5742 i = findoption((char_u *)"hl");
5743 if (i >= 0)
5744 return options[i].def_val[VI_DEFAULT];
5745 return (char_u *)NULL;
5746}
5747
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005748 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005749get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005750{
5751 int i;
5752
5753 i = findoption((char_u *)"enc");
5754 if (i >= 0)
5755 return options[i].def_val[VI_DEFAULT];
5756 return (char_u *)NULL;
5757}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005758
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005759#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005760 int
5761is_option_allocated(char *name)
5762{
5763 int idx = findoption((char_u *)name);
5764
5765 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5766}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005767#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005768
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769/*
5770 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005771 * When "has_lt" is true there is a '<' before "*arg_arg".
5772 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 */
5774 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005775find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005777 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005779 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005781 // Don't use get_special_key_code() for t_xx, we don't want it to call
5782 // add_termcap_entry().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
Millya9c6f902024-10-06 16:47:02 +02005784 {
5785 if (!has_lt || arg[4] == '>')
5786 key = TERMCAP2KEY(arg[2], arg[3]);
5787 }
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005788 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005790 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005792 key = find_special_key(&arg, &modifiers,
5793 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005794 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 key = 0;
5796 }
5797 return key;
5798}
5799
5800/*
5801 * if 'all' == 0: show changed options
5802 * if 'all' == 1: show all normal options
5803 * if 'all' == 2: show all terminal options
5804 */
5805 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005806showoptions(
5807 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005808 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809{
5810 struct vimoption *p;
5811 int col;
5812 int isterm;
5813 char_u *varp;
5814 struct vimoption **items;
5815 int item_count;
5816 int run;
5817 int row, rows;
5818 int cols;
5819 int i;
5820 int len;
5821
5822#define INC 20
5823#define GAP 3
5824
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005825 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 if (items == NULL)
5827 return;
5828
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005829 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005831 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005833 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005835 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005837 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005839 // Do the loop two times:
5840 // 1. display the short items
5841 // 2. display the long items (only strings and numbers)
5842 // When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 for (run = 1; run <= 2 && !got_int; ++run)
5844 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005845 // collect the items in items[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 item_count = 0;
5847 for (p = &options[0]; p->fullname != NULL; p++)
5848 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005849 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005850 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005851 continue;
5852
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 varp = NULL;
5854 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005855 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 {
5857 if (p->indir != PV_NONE && !isterm)
5858 varp = get_varp_scope(p, opt_flags);
5859 }
5860 else
5861 varp = get_varp(p);
5862 if (varp != NULL
5863 && ((all == 2 && isterm)
5864 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005865 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005867 if (opt_flags & OPT_ONECOLUMN)
5868 len = Columns;
5869 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005870 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 else
5872 {
5873 option_value2string(p, opt_flags);
5874 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5875 }
5876 if ((len <= INC - GAP && run == 1) ||
5877 (len > INC - GAP && run == 2))
5878 items[item_count++] = p;
5879 }
5880 }
5881
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005882 // display the items
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 if (run == 1)
5884 {
5885 cols = (Columns + GAP - 3) / INC;
5886 if (cols == 0)
5887 cols = 1;
5888 rows = (item_count + cols - 1) / cols;
5889 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005890 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 rows = item_count;
5892 for (row = 0; row < rows && !got_int; ++row)
5893 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005894 msg_putchar('\n'); // go to next line
5895 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 break;
5897 col = 0;
5898 for (i = row; i < item_count; i += rows)
5899 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005900 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 showoneopt(items[i], opt_flags);
5902 col += INC;
5903 }
5904 out_flush();
5905 ui_breakcheck();
5906 }
5907 }
5908 vim_free(items);
5909}
5910
5911/*
5912 * Return TRUE if option "p" has its default value.
5913 */
5914 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005915optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916{
5917 int dvi;
5918
5919 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005920 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005921 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005923 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005925 // the cast to long is required for Manx C, long_i is
5926 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005927 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005928 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5930}
5931
5932/*
5933 * showoneopt: show the value of one option
5934 * must not be called with a hidden option!
5935 */
5936 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005937showoneopt(
5938 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005939 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005941 char_u *varp;
5942 int save_silent = silent_mode;
5943
5944 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005945 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946
5947 varp = get_varp_scope(p, opt_flags);
5948
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005949 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5951 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005952 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005954 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005956 msg_puts(" ");
5957 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 if (!(p->flags & P_BOOL))
5959 {
5960 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005961 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 option_value2string(p, opt_flags);
5963 msg_outtrans(NameBuff);
5964 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005965
5966 silent_mode = save_silent;
5967 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968}
5969
5970/*
5971 * Write modified options as ":set" commands to a file.
5972 *
5973 * There are three values for "opt_flags":
5974 * OPT_GLOBAL: Write global option values and fresh values of
5975 * buffer-local options (used for start of a session
5976 * file).
5977 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5978 * curwin (used for a vimrc file).
5979 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5980 * and local values for window-local options of
5981 * curwin. Local values are also written when at the
5982 * default value, because a modeline or autocommand
5983 * may have set them when doing ":edit file" and the
5984 * user has set them back at the default or fresh
5985 * value.
5986 * When "local_only" is TRUE, don't write fresh
5987 * values, only local values (for ":mkview").
5988 * (fresh value = value used for a new buffer or window for a local option).
5989 *
5990 * Return FAIL on error, OK otherwise.
5991 */
5992 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005993makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994{
5995 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005996 char_u *varp; // currently used value
5997 char_u *varp_fresh; // local value
5998 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 char *cmd;
6000 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00006001 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002
6003 /*
6004 * The options that don't have a default (terminal name, columns, lines)
6005 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00006006 * Do the loop over "options[]" twice: once for options with the
6007 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00006009 for (pri = 1; pri >= 0; --pri)
6010 {
6011 for (p = &options[0]; !istermoption(p); p++)
6012 if (!(p->flags & P_NO_MKRC)
6013 && !istermoption(p)
6014 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006016 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
6018 continue;
6019
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006020 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
6021 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
6023 continue;
6024
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006025 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02006027 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 continue;
6029
Bram Moolenaard23b7142021-04-17 21:04:34 +02006030 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
6031 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02006032 continue;
6033
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 round = 2;
6035 if (p->indir != PV_NONE)
6036 {
6037 if (p->var == VAR_WIN)
6038 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006039 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 if (!(opt_flags & OPT_LOCAL))
6041 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006042 // When fresh value of window-local option is not at the
6043 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 if (!(opt_flags & OPT_GLOBAL) && !local_only)
6045 {
6046 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02006047 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 {
6049 round = 1;
6050 varp_local = varp;
6051 varp = varp_fresh;
6052 }
6053 }
6054 }
6055 }
6056
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006057 // Round 1: fresh value for window-local options.
6058 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 for ( ; round <= 2; varp = varp_local, ++round)
6060 {
6061 if (round == 1 || (opt_flags & OPT_GLOBAL))
6062 cmd = "set";
6063 else
6064 cmd = "setlocal";
6065
6066 if (p->flags & P_BOOL)
6067 {
6068 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
6069 return FAIL;
6070 }
6071 else if (p->flags & P_NUM)
6072 {
6073 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
6074 return FAIL;
6075 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006076 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006078 int do_endif = FALSE;
6079
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006080 // Don't set 'syntax' and 'filetype' again if the value is
6081 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006082 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006083#if defined(FEAT_SYN_HL)
6084 p->indir == PV_SYN ||
6085#endif
6086 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 {
6088 if (fprintf(fd, "if &%s != '%s'", p->fullname,
6089 *(char_u **)(varp)) < 0
6090 || put_eol(fd) < 0)
6091 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006092 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 }
6094 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006095 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006097 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 {
6099 if (put_line(fd, "endif") == FAIL)
6100 return FAIL;
6101 }
6102 }
6103 }
6104 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00006105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 return OK;
6107}
6108
6109#if defined(FEAT_FOLDING) || defined(PROTO)
6110/*
6111 * Generate set commands for the local fold options only. Used when
6112 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
6113 */
6114 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006115makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006117 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006119 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 == FAIL
6121# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006122 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006124 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 == FAIL
6126 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
6127 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
6128 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
6129 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
6130 )
6131 return FAIL;
6132
6133 return OK;
6134}
6135#endif
6136
6137 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006138put_setstring(
6139 FILE *fd,
6140 char *cmd,
6141 char *name,
6142 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006143 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144{
6145 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006146 char_u *buf = NULL;
6147 char_u *part = NULL;
6148 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149
6150 if (fprintf(fd, "%s %s=", cmd, name) < 0)
6151 return FAIL;
6152 if (*valuep != NULL)
6153 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006154 // Output 'pastetoggle' as key names. For other
6155 // options some characters have to be escaped with
6156 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 if (valuep == &p_pt)
6158 {
6159 s = *valuep;
6160 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01006161 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 return FAIL;
6163 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006164 // expand the option value, replace $HOME by ~
6165 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006167 int size = (int)STRLEN(*valuep) + 1;
6168
6169 // replace home directory in the whole option value into "buf"
6170 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02006171 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006172 goto fail;
6173 home_replace(NULL, *valuep, buf, size, FALSE);
6174
6175 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01006176 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006177 // can be expanded when read back.
6178 if (size >= MAXPATHL && (flags & P_COMMA) != 0
6179 && vim_strchr(*valuep, ',') != NULL)
6180 {
6181 part = alloc(size);
6182 if (part == NULL)
6183 goto fail;
6184
6185 // write line break to clear the option, e.g. ':set rtp='
6186 if (put_eol(fd) == FAIL)
6187 goto fail;
6188
6189 p = buf;
6190 while (*p != NUL)
6191 {
6192 // for each comma separated option part, append value to
6193 // the option, :set rtp+=value
6194 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
6195 goto fail;
6196 (void)copy_option_part(&p, part, size, ",");
6197 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
6198 goto fail;
6199 }
6200 vim_free(buf);
6201 vim_free(part);
6202 return OK;
6203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02006205 {
6206 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02006208 }
6209 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 }
6211 else if (put_escstr(fd, *valuep, 2) == FAIL)
6212 return FAIL;
6213 }
6214 if (put_eol(fd) < 0)
6215 return FAIL;
6216 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006217fail:
6218 vim_free(buf);
6219 vim_free(part);
6220 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221}
6222
6223 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006224put_setnum(
6225 FILE *fd,
6226 char *cmd,
6227 char *name,
6228 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229{
6230 long wc;
6231
6232 if (fprintf(fd, "%s %s=", cmd, name) < 0)
6233 return FAIL;
6234 if (wc_use_keyname((char_u *)valuep, &wc))
6235 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006236 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
6238 return FAIL;
6239 }
6240 else if (fprintf(fd, "%ld", *valuep) < 0)
6241 return FAIL;
6242 if (put_eol(fd) < 0)
6243 return FAIL;
6244 return OK;
6245}
6246
6247 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006248put_setbool(
6249 FILE *fd,
6250 char *cmd,
6251 char *name,
6252 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006254 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00006255 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
6257 || put_eol(fd) < 0)
6258 return FAIL;
6259 return OK;
6260}
6261
6262/*
6263 * Clear all the terminal options.
6264 * If the option has been allocated, free the memory.
6265 * Terminal options are never hidden or indirect.
6266 */
6267 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006268clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006270 // Reset a few things before clearing the old options. This may cause
6271 // outputting a few things that the terminal doesn't understand, but the
6272 // screen will be cleared later, so this is OK.
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006273 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006274 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006276 // When starting the GUI close the display opened for the clipboard.
6277 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 if (gui.starting)
6279 clear_xterm_clip();
6280#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006281 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006283 free_termoptions();
6284}
6285
6286 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006287free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006288{
6289 struct vimoption *p;
6290
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006291 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 if (istermoption(p))
6293 {
6294 if (p->flags & P_ALLOCED)
6295 free_string_option(*(char_u **)(p->var));
6296 if (p->flags & P_DEF_ALLOCED)
6297 free_string_option(p->def_val[VI_DEFAULT]);
6298 *(char_u **)(p->var) = empty_option;
6299 p->def_val[VI_DEFAULT] = empty_option;
6300 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006301#ifdef FEAT_EVAL
6302 // remember where the option was cleared
6303 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
6304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 }
6306 clear_termcodes();
6307}
6308
6309/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00006310 * Free the string for one term option, if it was allocated.
6311 * Set the string to empty_option and clear allocated flag.
6312 * "var" points to the option value.
6313 */
6314 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006315free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00006316{
6317 struct vimoption *p;
6318
6319 for (p = &options[0]; p->fullname != NULL; p++)
6320 if (p->var == var)
6321 {
6322 if (p->flags & P_ALLOCED)
6323 free_string_option(*(char_u **)(p->var));
6324 *(char_u **)(p->var) = empty_option;
6325 p->flags &= ~P_ALLOCED;
6326 break;
6327 }
6328}
6329
6330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 * Set the terminal option defaults to the current value.
6332 * Used after setting the terminal name.
6333 */
6334 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006335set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336{
6337 struct vimoption *p;
6338
6339 for (p = &options[0]; p->fullname != NULL; p++)
6340 {
6341 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
6342 {
6343 if (p->flags & P_DEF_ALLOCED)
6344 {
6345 free_string_option(p->def_val[VI_DEFAULT]);
6346 p->flags &= ~P_DEF_ALLOCED;
6347 }
6348 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
6349 if (p->flags & P_ALLOCED)
6350 {
6351 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006352 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 }
6354 }
6355 }
6356}
6357
6358/*
6359 * return TRUE if 'p' starts with 't_'
6360 */
6361 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006362istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363{
6364 return (p->fullname[0] == 't' && p->fullname[1] == '_');
6365}
6366
Bram Moolenaardac13472019-09-16 21:06:21 +02006367/*
6368 * Returns TRUE if the option at 'opt_idx' starts with 't_'
6369 */
6370 int
6371istermoption_idx(int opt_idx)
6372{
6373 return istermoption(&options[opt_idx]);
6374}
6375
Bram Moolenaar113e1072019-01-20 15:30:40 +01006376#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006378 * Unset local option value, similar to ":set opt<".
6379 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006380 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006381unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006382{
6383 struct vimoption *p;
6384 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006385 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006386
6387 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02006388 if (opt_idx < 0)
6389 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006390 p = &(options[opt_idx]);
6391
6392 switch ((int)p->indir)
6393 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006394 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006395 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006396 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006397 break;
6398 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006399 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006400 break;
6401 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006402 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006403 break;
6404 case PV_AR:
6405 buf->b_p_ar = -1;
6406 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006407 case PV_BKC:
6408 clear_string_option(&buf->b_p_bkc);
6409 buf->b_bkc_flags = 0;
6410 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006411 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006412 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006413 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006414 case PV_TC:
6415 clear_string_option(&buf->b_p_tc);
6416 buf->b_tc_flags = 0;
6417 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006418 case PV_SISO:
6419 curwin->w_p_siso = -1;
6420 break;
6421 case PV_SO:
6422 curwin->w_p_so = -1;
6423 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006424# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006425 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006426 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006427 break;
6428 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006429 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006430 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006431# endif
zeertzjq529b9ad2024-06-05 20:27:06 +02006432 case PV_COT:
6433 clear_string_option(&buf->b_p_cot);
6434 buf->b_cot_flags = 0;
6435 break;
glepnirbcd59952025-04-24 21:48:35 +02006436 case PV_ISE:
6437 clear_string_option(&buf->b_p_ise);
6438 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006439 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006440 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006441 break;
6442 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006443 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006444 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006445# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006446 case PV_TSRFU:
6447 clear_string_option(&buf->b_p_tsrfu);
6448 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006449# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006450 case PV_FP:
6451 clear_string_option(&buf->b_p_fp);
6452 break;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006453# ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006454 case PV_FFU:
6455 clear_string_option(&buf->b_p_ffu);
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006456 break;
6457# endif
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006458# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006459 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006460 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006461 break;
glepnir7b9eb632025-05-16 19:49:23 +02006462 case PV_GEFM:
6463 clear_string_option(&buf->b_p_gefm);
6464 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006465 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006466 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006467 break;
6468 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006469 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006470 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006471# endif
6472# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006473 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006474 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006475 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006476# endif
6477# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006478 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006479 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006480 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006481# endif
6482# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006483 case PV_SBR:
6484 clear_string_option(&((win_T *)from)->w_p_sbr);
6485 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006486# endif
6487# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006488 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006489 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006490 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006491# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006492 case PV_UL:
6493 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6494 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006495 case PV_LW:
6496 clear_string_option(&buf->b_p_lw);
6497 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006498 case PV_MENC:
6499 clear_string_option(&buf->b_p_menc);
6500 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006501 case PV_LCS:
6502 clear_string_option(&((win_T *)from)->w_p_lcs);
zeertzjq6a8d2e12024-01-17 20:54:49 +01006503 set_listchars_option((win_T *)from, ((win_T *)from)->w_p_lcs, TRUE,
6504 NULL, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006505 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006506 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006507 case PV_FCS:
6508 clear_string_option(&((win_T *)from)->w_p_fcs);
zeertzjq6a8d2e12024-01-17 20:54:49 +01006509 set_fillchars_option((win_T *)from, ((win_T *)from)->w_p_fcs, TRUE,
6510 NULL, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006511 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006512 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006513 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006514 clear_string_option(&((win_T *)from)->w_p_ve);
6515 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006516 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006517 }
6518}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006519#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006520
6521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006523 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 */
6525 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006526get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006528 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 {
6530 if (p->var == VAR_WIN)
6531 return (char_u *)GLOBAL_WO(get_varp(p));
6532 return p->var;
6533 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006534 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 {
6536 switch ((int)p->indir)
6537 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006538 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006539#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006540 case PV_FFU: return (char_u *)&(curbuf->b_p_ffu);
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006543 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
glepnir7b9eb632025-05-16 19:49:23 +02006544 case PV_GEFM: return (char_u *)&(curbuf->b_p_gefm);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006545 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6546 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006548 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6549 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6550 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006551 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006552 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006553 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006554 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6555 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006557 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6558 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559#endif
zeertzjq529b9ad2024-06-05 20:27:06 +02006560 case PV_COT: return (char_u *)&(curbuf->b_p_cot);
glepnirbcd59952025-04-24 21:48:35 +02006561 case PV_ISE: return (char_u *)&(curbuf->b_p_ise);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006562 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6563 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006564#ifdef FEAT_COMPL_FUNC
6565 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6566#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006567#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6568 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6569#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006570#if defined(FEAT_CRYPT)
6571 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6572#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006573#ifdef FEAT_LINEBREAK
6574 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6575#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006576#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006577 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006578#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006579 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006580 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006581 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006582 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006583 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006584 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006585 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006586
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006588 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 }
6590 return get_varp(p);
6591}
6592
6593/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006594 * Get pointer to option variable at 'opt_idx', depending on local or global
6595 * scope.
6596 */
6597 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006598get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006599{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006600 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006601}
6602
6603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 * Get pointer to option variable.
6605 */
6606 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006607get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006609 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 if (p->var == NULL)
6611 return NULL;
6612
6613 switch ((int)p->indir)
6614 {
6615 case PV_NONE: return p->var;
6616
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006617 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006618 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006620 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006622 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006624 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006626 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006628 case PV_TC: return *curbuf->b_p_tc != NUL
6629 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006630 case PV_BKC: return *curbuf->b_p_bkc != NUL
6631 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006632 case PV_SISO: return curwin->w_p_siso >= 0
6633 ? (char_u *)&(curwin->w_p_siso) : p->var;
6634 case PV_SO: return curwin->w_p_so >= 0
6635 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006637 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006639 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6641#endif
zeertzjq529b9ad2024-06-05 20:27:06 +02006642 case PV_COT: return *curbuf->b_p_cot != NUL
6643 ? (char_u *)&(curbuf->b_p_cot) : p->var;
glepnirbcd59952025-04-24 21:48:35 +02006644 case PV_ISE: return *curbuf->b_p_ise != NUL
6645 ? (char_u *)&(curbuf->b_p_ise) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006646 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006648 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006650#ifdef FEAT_COMPL_FUNC
6651 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6652 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6653#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006654 case PV_FP: return *curbuf->b_p_fp != NUL
6655 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006656#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006657 case PV_FFU: return *curbuf->b_p_ffu != NUL
6658 ? (char_u *)&(curbuf->b_p_ffu) : p->var;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006661 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 ? (char_u *)&(curbuf->b_p_efm) : p->var;
glepnir7b9eb632025-05-16 19:49:23 +02006663 case PV_GEFM: return *curbuf->b_p_gefm != NUL
6664 ? (char_u *)&(curbuf->b_p_gefm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006665 case PV_GP: return *curbuf->b_p_gp != NUL
6666 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6667 case PV_MP: return *curbuf->b_p_mp != NUL
6668 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006670#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6671 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6672 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6673#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006674#if defined(FEAT_CRYPT)
6675 case PV_CM: return *curbuf->b_p_cm != NUL
6676 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6677#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006678#ifdef FEAT_LINEBREAK
6679 case PV_SBR: return *curwin->w_p_sbr != NUL
6680 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6681#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006682#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006683 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006684 ? (char_u *)&(curwin->w_p_stl) : p->var;
6685#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006686 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6687 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006688 case PV_LW: return *curbuf->b_p_lw != NUL
6689 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006690 case PV_MENC: return *curbuf->b_p_menc != NUL
6691 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692#ifdef FEAT_ARABIC
6693 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6694#endif
6695 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006696 case PV_LCS: return *curwin->w_p_lcs != NUL
6697 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006698 case PV_FCS: return *curwin->w_p_fcs != NUL
6699 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006700 case PV_VE: return *curwin->w_p_ve != NUL
6701 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006702#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006703 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6704#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006705#ifdef FEAT_SYN_HL
6706 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6707 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006708 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006709 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711#ifdef FEAT_DIFF
6712 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6713#endif
Luuk van Baalb7147f82025-02-08 18:52:39 +01006714 case PV_EIW: return (char_u *)&(curwin->w_p_eiw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715#ifdef FEAT_FOLDING
6716 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6717 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6718 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6719 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6720 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6721 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6722 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6723# ifdef FEAT_EVAL
6724 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6725 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6726# endif
6727 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6728#endif
6729 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006730 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006731#ifdef FEAT_LINEBREAK
6732 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6733#endif
Colin Kennedy21570352024-03-03 16:16:47 +01006734 case PV_WFB: return (char_u *)&(curwin->w_p_wfb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006736 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006737#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
64-bitman88d41ab2025-04-06 17:20:39 +02006739 case PV_LHI: return (char_u *)&(curwin->w_p_lhi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740#endif
6741#ifdef FEAT_RIGHTLEFT
6742 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6743 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6744#endif
6745 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006746 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6748#ifdef FEAT_LINEBREAK
6749 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006750 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6751 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006753 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006755 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006756#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006757 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6758 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6759#endif
6760#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006761 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6762 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6763 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765
6766 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6767 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6770 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6772 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6774 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6775 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006776 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779#ifdef FEAT_FOLDING
6780 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006783#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006784 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006786#ifdef FEAT_COMPL_FUNC
6787 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006788 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006789#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006790#ifdef FEAT_EVAL
6791 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6792#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006793 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006795 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006801 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6803 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6804 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6805 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6806#ifdef FEAT_FIND_ID
6807# ifdef FEAT_EVAL
6808 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6809# endif
6810#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006811#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6813 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6814#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006815#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006816 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818#ifdef FEAT_CRYPT
6819 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006822 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6824 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6825 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6826 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6827 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006829 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6836#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006837 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006838 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6839#endif
6840#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006841 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6842 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6843 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006844 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845#endif
6846 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6847 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6848 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6849 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006850#ifdef FEAT_PERSISTENT_UNDO
6851 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6854#ifdef FEAT_KEYMAP
6855 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6856#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006857#ifdef FEAT_SIGNS
6858 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6859#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006860#ifdef FEAT_VARTABS
6861 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6862 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6863#endif
RestorerZ68ebcee2023-05-31 17:12:14 +01006864 default: iemsg(e_get_varp_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006866 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 return (char_u *)&(curbuf->b_p_wm);
6868}
6869
6870/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006871 * Return a pointer to the variable for option at 'opt_idx'
6872 */
6873 char_u *
6874get_option_var(int opt_idx)
6875{
6876 return options[opt_idx].var;
6877}
6878
Dominique Pelle748b3082022-01-08 12:41:16 +00006879#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006880/*
6881 * Return the full name of the option at 'opt_idx'
6882 */
6883 char_u *
6884get_option_fullname(int opt_idx)
6885{
6886 return (char_u *)options[opt_idx].fullname;
6887}
Dominique Pelle748b3082022-01-08 12:41:16 +00006888#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006889
6890/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00006891 * Return the did_set callback function for the option at 'opt_idx'
6892 */
6893 opt_did_set_cb_T
6894get_option_did_set_cb(int opt_idx)
6895{
6896 return options[opt_idx].opt_did_set_cb;
6897}
6898
6899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 * Get the value of 'equalprg', either the buffer-local one or the global one.
6901 */
6902 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006903get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904{
6905 if (*curbuf->b_p_ep == NUL)
6906 return p_ep;
6907 return curbuf->b_p_ep;
6908}
6909
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910/*
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006911 * Get the value of 'findfunc', either the buffer-local one or the global one.
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006912 */
6913 char_u *
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006914get_findfunc(void)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006915{
6916#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006917 if (*curbuf->b_p_ffu == NUL)
6918 return p_ffu;
6919 return curbuf->b_p_ffu;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006920#else
6921 return (char_u *)"";
6922#endif
6923}
6924
6925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 * Copy options from one window to another.
6927 * Used when splitting a window.
6928 */
6929 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006930win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931{
6932 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6933 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006934 after_copy_winopt(wp_to);
6935}
6936
6937/*
6938 * After copying window options: update variables depending on options.
6939 */
6940 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006941after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006942{
Christian Brabandtb065a102024-10-05 17:30:22 +02006943 // Set w_leftcol or w_skipcol to zero.
6944 if (wp->w_p_wrap)
6945 wp->w_leftcol = 0;
6946 else
6947 wp->w_skipcol = 0;
Bram Moolenaar010ee962019-09-25 20:37:36 +02006948#ifdef FEAT_LINEBREAK
Millyb38700a2024-10-22 22:59:39 +02006949 briopt_check(NULL, wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006950#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006951#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006952 fill_culopt_flags(NULL, wp);
Millya441a3e2024-10-22 22:43:01 +02006953 check_colorcolumn(NULL, wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006954#endif
zeertzjq6a8d2e12024-01-17 20:54:49 +01006955 set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
6956 set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006957}
6958
6959 static char_u *
6960copy_option_val(char_u *val)
6961{
6962 if (val == empty_option)
6963 return empty_option; // no need to allocate memory
6964 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966
6967/*
6968 * Copy the options from one winopt_T to another.
6969 * Doesn't free the old option values in "to", use clear_winopt() for that.
6970 * The 'scroll' option is not copied, because it depends on the window height.
6971 * The 'previewwindow' option is reset, there can be only one preview window.
6972 */
6973 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006974copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975{
6976#ifdef FEAT_ARABIC
6977 to->wo_arab = from->wo_arab;
6978#endif
6979 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006980 to->wo_lcs = copy_option_val(from->wo_lcs);
6981 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006983 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006984 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006985 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006986#ifdef FEAT_LINEBREAK
6987 to->wo_nuw = from->wo_nuw;
6988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989#ifdef FEAT_RIGHTLEFT
6990 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006991 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006993#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006994 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006995#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006996#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006997 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02007000#ifdef FEAT_DIFF
7001 to->wo_wrap_save = from->wo_wrap_save;
7002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003#ifdef FEAT_LINEBREAK
7004 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02007005 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01007006 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01007008 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02007010 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01007011 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01007012 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02007013 to->wo_crb_save = from->wo_crb_save;
Christian Brabandt4a8eb6e2023-08-13 19:43:42 +02007014 to->wo_siso = from->wo_siso;
7015 to->wo_so = from->wo_so;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007016#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007017 to->wo_spell = from->wo_spell;
7018#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007019#ifdef FEAT_SYN_HL
7020 to->wo_cuc = from->wo_cuc;
7021 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01007022 to->wo_culopt = copy_option_val(from->wo_culopt);
7023 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025#ifdef FEAT_DIFF
7026 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02007027 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028#endif
Luuk van Baalb7147f82025-02-08 18:52:39 +01007029 to->wo_eiw = copy_option_val(from->wo_eiw);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007030#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01007031 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02007032 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007033#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02007034#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01007035 to->wo_twk = copy_option_val(from->wo_twk);
7036 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02007037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038#ifdef FEAT_FOLDING
7039 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02007040 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02007042 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01007043 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 to->wo_fml = from->wo_fml;
7045 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02007046 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01007047 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02007048 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01007049 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 to->wo_fdn = from->wo_fdn;
7051# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01007052 to->wo_fde = copy_option_val(from->wo_fde);
7053 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01007055 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007057#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01007058 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007059#endif
64-bitman88d41ab2025-04-06 17:20:39 +02007060#ifdef FEAT_QUICKFIX
7061 to->wo_lhi = from->wo_lhi;
7062#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007063
7064#ifdef FEAT_EVAL
7065 // Copy the script context so that we know where the value was last set.
7066 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
7067 sizeof(to->wo_script_ctx));
7068#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007069 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070}
7071
7072/*
7073 * Check string options in a window for a NULL value.
7074 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02007075 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007076check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077{
7078 check_winopt(&win->w_onebuf_opt);
7079 check_winopt(&win->w_allbuf_opt);
7080}
7081
7082/*
7083 * Check for NULL pointers in a winopt_T and replace them with empty_option.
7084 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02007085 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007086check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087{
7088#ifdef FEAT_FOLDING
7089 check_string_option(&wop->wo_fdi);
7090 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02007091 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092# ifdef FEAT_EVAL
7093 check_string_option(&wop->wo_fde);
7094 check_string_option(&wop->wo_fdt);
7095# endif
7096 check_string_option(&wop->wo_fmr);
7097#endif
Luuk van Baalb7147f82025-02-08 18:52:39 +01007098 check_string_option(&wop->wo_eiw);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007099#ifdef FEAT_SIGNS
7100 check_string_option(&wop->wo_scl);
7101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102#ifdef FEAT_RIGHTLEFT
7103 check_string_option(&wop->wo_rlc);
7104#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01007105#ifdef FEAT_LINEBREAK
7106 check_string_option(&wop->wo_sbr);
7107#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007108#ifdef FEAT_STL_OPT
7109 check_string_option(&wop->wo_stl);
7110#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02007111#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02007112 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02007113 check_string_option(&wop->wo_cc);
7114#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007115#ifdef FEAT_CONCEAL
7116 check_string_option(&wop->wo_cocu);
7117#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02007118#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007119 check_string_option(&wop->wo_twk);
7120 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02007121#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02007122#ifdef FEAT_LINEBREAK
7123 check_string_option(&wop->wo_briopt);
7124#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02007125 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01007126 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01007127 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02007128 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129}
7130
7131/*
7132 * Free the allocated memory inside a winopt_T.
7133 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007135clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136{
7137#ifdef FEAT_FOLDING
7138 clear_string_option(&wop->wo_fdi);
7139 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02007140 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141# ifdef FEAT_EVAL
7142 clear_string_option(&wop->wo_fde);
7143 clear_string_option(&wop->wo_fdt);
7144# endif
7145 clear_string_option(&wop->wo_fmr);
7146#endif
Luuk van Baalb7147f82025-02-08 18:52:39 +01007147 clear_string_option(&wop->wo_eiw);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007148#ifdef FEAT_SIGNS
7149 clear_string_option(&wop->wo_scl);
7150#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02007151#ifdef FEAT_LINEBREAK
7152 clear_string_option(&wop->wo_briopt);
7153#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02007154 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155#ifdef FEAT_RIGHTLEFT
7156 clear_string_option(&wop->wo_rlc);
7157#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01007158#ifdef FEAT_LINEBREAK
7159 clear_string_option(&wop->wo_sbr);
7160#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007161#ifdef FEAT_STL_OPT
7162 clear_string_option(&wop->wo_stl);
7163#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02007164#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02007165 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02007166 clear_string_option(&wop->wo_cc);
7167#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007168#ifdef FEAT_CONCEAL
7169 clear_string_option(&wop->wo_cocu);
7170#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02007171#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007172 clear_string_option(&wop->wo_twk);
7173 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02007174#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01007175 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01007176 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02007177 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178}
7179
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007180#ifdef FEAT_EVAL
7181// Index into the options table for a buffer-local option enum.
7182static int buf_opt_idx[BV_COUNT];
7183# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
7184
7185/*
7186 * Initialize buf_opt_idx[] if not done already.
7187 */
7188 static void
7189init_buf_opt_idx(void)
7190{
7191 static int did_init_buf_opt_idx = FALSE;
7192 int i;
7193
7194 if (did_init_buf_opt_idx)
7195 return;
7196 did_init_buf_opt_idx = TRUE;
7197 for (i = 0; !istermoption_idx(i); i++)
7198 if (options[i].indir & PV_BUF)
7199 buf_opt_idx[options[i].indir & PV_MASK] = i;
7200}
7201#else
7202# define COPY_OPT_SCTX(buf, bv)
7203#endif
7204
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205/*
7206 * Copy global option values to local options for one buffer.
7207 * Used when creating a new buffer and sometimes when entering a buffer.
7208 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007209 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
7211 * appropriate.
7212 * BCO_NOHELP Don't copy the values to a help buffer.
7213 */
7214 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007215buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216{
7217 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007218 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 int dont_do_help;
7220 int did_isk = FALSE;
7221
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007222 // Skip this when the option defaults have not been set yet. Happens when
7223 // main() allocates the first buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 if (p_cpo != NULL)
7225 {
7226 /*
7227 * Always copy when entering and 'cpo' contains 'S'.
7228 * Don't copy when already initialized.
7229 * Don't copy when 'cpo' contains 's' and not entering.
7230 * 'S' BCO_ENTER initialized 's' should_copy
7231 * yes yes X X TRUE
7232 * yes no yes X FALSE
7233 * no X yes X FALSE
7234 * X no no yes FALSE
7235 * X no no no TRUE
7236 * no yes no X TRUE
7237 */
7238 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
7239 && (buf->b_p_initialized
7240 || (!(flags & BCO_ENTER)
7241 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
7242 should_copy = FALSE;
7243
7244 if (should_copy || (flags & BCO_ALWAYS))
7245 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007246#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02007247 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007248 init_buf_opt_idx();
7249#endif
7250 // Don't copy the options specific to a help buffer when
7251 // BCO_NOHELP is given or the options were initialized already
7252 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
7254 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007255 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 {
7257 save_p_isk = buf->b_p_isk;
7258 buf->b_p_isk = NULL;
7259 }
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007260 // Always free the allocated strings. If not already initialized,
7261 // reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 if (!buf->b_p_initialized)
7263 {
7264 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007265 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02007268 switch (*p_ffs)
7269 {
7270 case 'm':
7271 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
7272 case 'd':
7273 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
7274 case 'u':
7275 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
7276 default:
7277 buf->b_p_ff = vim_strsave(p_ff);
7278 }
7279 if (buf->b_p_ff != NULL)
7280 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 buf->b_p_bh = empty_option;
7282 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 }
7284 else
7285 free_buf_options(buf, FALSE);
7286
7287 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007288 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 buf->b_p_ai_nopaste = p_ai_nopaste;
7290 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007291 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007293 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 buf->b_p_tw_nopaste = p_tw_nopaste;
7295 buf->b_p_tw_nobin = p_tw_nobin;
7296 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007297 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 buf->b_p_wm_nopaste = p_wm_nopaste;
7299 buf->b_p_wm_nobin = p_wm_nobin;
7300 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007301 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00007302 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007303 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02007304 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007305 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007307 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007309 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007311 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 buf->b_p_ml_nobin = p_ml_nobin;
7313 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007314 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02007315 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007316 buf->b_p_swf = FALSE;
7317 else
7318 {
7319 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00007320 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007323 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02007324#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02007325 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007326 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007328#ifdef FEAT_COMPL_FUNC
7329 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007330 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007331 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00007332 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007333 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007334 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007335#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02007336#ifdef FEAT_EVAL
7337 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007338 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007339 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02007340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007342 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007344#ifdef FEAT_VARTABS
7345 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007346 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007347 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007348 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007349 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007350 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007351 buf->b_p_vsts_nopaste = p_vsts_nopaste
7352 ? vim_strsave(p_vsts_nopaste) : NULL;
7353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007355 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007357 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358#ifdef FEAT_FOLDING
7359 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007360 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361#endif
7362 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007363 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007364 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007365 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02007366 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
7367 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007369 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007371 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007373 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007375 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007376
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007378 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007380 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007382 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01007383 buf->b_p_cinsd = vim_strsave(p_cinsd);
7384 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01007385 buf->b_p_lop = vim_strsave(p_lop);
7386 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007387
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007388 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007391 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007393 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007395 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007397 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00007399 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007400 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01007401 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007402#endif
7403#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02007404 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007405 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007406 (void)compile_cap_prog(&buf->b_s);
7407 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007408 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007409 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007410 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02007411 buf->b_s.b_p_spo = vim_strsave(p_spo);
7412 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007414#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007416 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007418 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01007420 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007421#if defined(FEAT_EVAL)
7422 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007423 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425#ifdef FEAT_CRYPT
7426 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007427 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007430 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431#ifdef FEAT_KEYMAP
7432 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007433 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 buf->b_kmap_state |= KEYMAP_INIT;
7435#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007436#ifdef FEAT_TERMINAL
7437 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007438 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007439#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007440 // This isn't really an option, but copying the langmap and IME
7441 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007443 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007445 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007447 // options that are normally global but also have a local value
7448 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01007450 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007451 buf->b_p_bkc = empty_option;
7452 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453#ifdef FEAT_QUICKFIX
glepnir7b9eb632025-05-16 19:49:23 +02007454 buf->b_p_gefm = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 buf->b_p_gp = empty_option;
7456 buf->b_p_mp = empty_option;
7457 buf->b_p_efm = empty_option;
7458#endif
7459 buf->b_p_ep = empty_option;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007460#if defined(FEAT_EVAL)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007461 buf->b_p_ffu = empty_option;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 buf->b_p_kp = empty_option;
7464 buf->b_p_path = empty_option;
7465 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007466 buf->b_p_tc = empty_option;
7467 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468#ifdef FEAT_FIND_ID
7469 buf->b_p_def = empty_option;
7470 buf->b_p_inc = empty_option;
7471# ifdef FEAT_EVAL
7472 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007473 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474# endif
7475#endif
zeertzjq529b9ad2024-06-05 20:27:06 +02007476 buf->b_p_cot = empty_option;
7477 buf->b_cot_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 buf->b_p_dict = empty_option;
7479 buf->b_p_tsr = empty_option;
glepnirbcd59952025-04-24 21:48:35 +02007480 buf->b_p_ise = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01007481#ifdef FEAT_COMPL_FUNC
7482 buf->b_p_tsrfu = empty_option;
7483#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007484 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007485 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007486#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7487 buf->b_p_bexpr = empty_option;
7488#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007489#if defined(FEAT_CRYPT)
7490 buf->b_p_cm = empty_option;
7491#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007492#ifdef FEAT_PERSISTENT_UNDO
7493 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007494 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007495#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007496 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007497 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007499 // Don't copy the options set by ex_help(), use the saved values,
7500 // when going from a help buffer to a non-help buffer.
7501 // Don't touch these at all when BCO_NOHELP is used and going from
7502 // or to a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007504 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007506#ifdef FEAT_VARTABS
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007507 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007508 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007509 else
7510 buf->b_p_vts_array = NULL;
7511#endif
7512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 else
7514 {
7515 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007516 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 did_isk = TRUE;
7518 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00007519 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007520#ifdef FEAT_VARTABS
7521 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007522 COPY_OPT_SCTX(buf, BV_VTS);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007523 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007524 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007525 else
7526 buf->b_p_vts_array = NULL;
7527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 if (buf->b_p_bt[0] == 'h')
7530 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007532 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 }
7534 }
7535
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007536 // When the options should be copied (ignoring BCO_ALWAYS), set the
7537 // flag that indicates that the options have been initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 if (should_copy)
7539 buf->b_p_initialized = TRUE;
7540 }
7541
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007542 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 if (did_isk)
7544 (void)buf_init_chartab(buf, FALSE);
7545}
7546
7547/*
7548 * Reset the 'modifiable' option and its default value.
7549 */
7550 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007551reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552{
7553 int opt_idx;
7554
7555 curbuf->b_p_ma = FALSE;
7556 p_ma = FALSE;
7557 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007558 if (opt_idx >= 0)
7559 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560}
7561
7562/*
7563 * Set the global value for 'iminsert' to the local value.
7564 */
7565 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007566set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567{
7568 p_iminsert = curbuf->b_p_iminsert;
7569}
7570
7571/*
7572 * Set the global value for 'imsearch' to the local value.
7573 */
7574 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007575set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576{
7577 p_imsearch = curbuf->b_p_imsearch;
7578}
7579
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580static int expand_option_idx = -1;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007581static int expand_option_start_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7583static int expand_option_flags = 0;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007584static int expand_option_append = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585
7586 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007587set_context_in_set_cmd(
7588 expand_T *xp,
7589 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007590 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591{
7592 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007593 long_u flags = 0; // init for GCC
7594 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 char_u *p;
7596 char_u *s;
7597 int is_term_option = FALSE;
7598 int key;
John Marriott95dacbb2024-09-10 21:25:14 +02007599 char_u *argend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600
7601 expand_option_flags = opt_flags;
7602
7603 xp->xp_context = EXPAND_SETTINGS;
7604 if (*arg == NUL)
7605 {
7606 xp->xp_pattern = arg;
7607 return;
7608 }
John Marriott95dacbb2024-09-10 21:25:14 +02007609 argend = arg + STRLEN(arg);
7610 p = argend - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 if (*p == ' ' && *(p - 1) != '\\')
7612 {
7613 xp->xp_pattern = p + 1;
7614 return;
7615 }
7616 while (p > arg)
7617 {
7618 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007619 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 if (*p == ' ' || *p == ',')
7621 {
7622 while (s > arg && *(s - 1) == '\\')
7623 --s;
7624 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007625 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 if (*p == ' ' && ((p - s) & 1) == 0)
7627 {
7628 ++p;
7629 break;
7630 }
7631 --p;
7632 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007633 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 {
7635 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007636 xp->xp_prefix = XP_PREFIX_NO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 p += 2;
7638 }
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007639 else if (STRNCMP(p, "inv", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 {
7641 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007642 xp->xp_prefix = XP_PREFIX_INV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 p += 3;
7644 }
7645 xp->xp_pattern = arg = p;
7646 if (*arg == '<')
7647 {
7648 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007649 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 return;
7651 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007652 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 {
7654 xp->xp_context = EXPAND_NOTHING;
7655 return;
7656 }
7657 nextchar = *++p;
7658 is_term_option = TRUE;
7659 expand_option_name[2] = KEY2TERMCAP0(key);
7660 expand_option_name[3] = KEY2TERMCAP1(key);
7661 }
7662 else
7663 {
7664 if (p[0] == 't' && p[1] == '_')
7665 {
7666 p += 2;
7667 if (*p != NUL)
7668 ++p;
7669 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007670 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 nextchar = *++p;
7672 is_term_option = TRUE;
7673 expand_option_name[2] = p[-2];
7674 expand_option_name[3] = p[-1];
7675 }
7676 else
7677 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007678 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7680 p++;
7681 if (*p == NUL)
7682 return;
7683 nextchar = *p;
7684 *p = NUL;
7685 opt_idx = findoption(arg);
7686 *p = nextchar;
7687 if (opt_idx == -1 || options[opt_idx].var == NULL)
7688 {
7689 xp->xp_context = EXPAND_NOTHING;
7690 return;
7691 }
7692 flags = options[opt_idx].flags;
7693 if (flags & P_BOOL)
7694 {
7695 xp->xp_context = EXPAND_NOTHING;
7696 return;
7697 }
7698 }
7699 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007700 // handle "-=" and "+="
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007701 expand_option_append = FALSE;
7702 int expand_option_subtract = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7704 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007705 if (nextchar == '-')
7706 expand_option_subtract = TRUE;
7707 if (nextchar == '+' || nextchar == '^')
7708 expand_option_append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 ++p;
7710 nextchar = '=';
7711 }
7712 if ((nextchar != '=' && nextchar != ':')
7713 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7714 {
7715 xp->xp_context = EXPAND_UNSUCCESSFUL;
7716 return;
7717 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007718
7719 // Below are for handling expanding a specific option's value after the '='
7720 // or ':'
7721
7722 if (is_term_option)
7723 expand_option_idx = -1;
7724 else
7725 expand_option_idx = opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02007727 if (!is_term_option)
7728 {
7729 if (options[opt_idx].flags & P_NO_CMD_EXPAND)
7730 {
7731 xp->xp_context=EXPAND_UNSUCCESSFUL;
7732 return;
7733 }
7734 }
7735
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 xp->xp_pattern = p + 1;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007737 expand_option_start_col = (int)(p + 1 - xp->xp_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007739 // Certain options currently have special case handling to reuse the
7740 // expansion logic with other commands.
Doug Kearns6dfdff32023-08-27 18:48:51 +02007741#ifdef FEAT_SYN_HL
7742 if (options[opt_idx].var == (char_u *)&p_syn)
7743 {
7744 xp->xp_context = EXPAND_OWNSYNTAX;
7745 return;
7746 }
7747#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007748 if (options[opt_idx].var == (char_u *)&p_ft)
7749 {
7750 xp->xp_context = EXPAND_FILETYPE;
7751 return;
7752 }
Doug Kearns81642d92024-01-04 22:37:44 +01007753#ifdef FEAT_KEYMAP
7754 if (options[opt_idx].var == (char_u *)&p_keymap)
7755 {
7756 xp->xp_context = EXPAND_KEYMAP;
7757 return;
7758 }
7759#endif
Doug Kearns6dfdff32023-08-27 18:48:51 +02007760
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007761 // Now pick. If the option has a custom expander, use that. Otherwise, just
7762 // fill with the existing option value.
7763 if (expand_option_subtract)
7764 {
7765 xp->xp_context = EXPAND_SETTING_SUBTRACT;
7766 return;
7767 }
7768 else if (expand_option_idx >= 0 &&
7769 options[expand_option_idx].opt_expand_cb != NULL)
7770 {
7771 xp->xp_context = EXPAND_STRING_SETTING;
7772 }
7773 else if (*xp->xp_pattern == NUL)
7774 {
7775 xp->xp_context = EXPAND_OLD_SETTING;
7776 return;
7777 }
7778 else
7779 xp->xp_context = EXPAND_NOTHING;
7780
7781 if (is_term_option || (flags & P_NUM))
7782 return;
7783
7784 // Only string options below
7785
7786 // Options that have P_EXPAND are considered to all use file/dir expansion.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 if (flags & P_EXPAND)
7788 {
7789 p = options[opt_idx].var;
7790 if (p == (char_u *)&p_bdir
7791 || p == (char_u *)&p_dir
7792 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007793 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796#ifdef FEAT_SESSION
7797 || p == (char_u *)&p_vdir
7798#endif
7799 )
7800 {
7801 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007802 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 xp->xp_backslash = XP_BS_THREE;
7804 else
7805 xp->xp_backslash = XP_BS_ONE;
7806 }
7807 else
7808 {
7809 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007810 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 if (p == (char_u *)&p_tags)
7812 xp->xp_backslash = XP_BS_THREE;
7813 else
7814 xp->xp_backslash = XP_BS_ONE;
7815 }
Yee Cheng Chin54844852023-10-09 18:12:31 +02007816 if (flags & P_COMMA)
7817 xp->xp_backslash |= XP_BS_COMMA;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 }
7819
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007820 // For an option that is a list of file names, or comma/colon-separated
7821 // values, split it by the delimiter and find the start of the current
7822 // pattern, while accounting for backslash-escaped space/commas/colons.
7823 // Triple-backslashed escaped file names (e.g. 'path') can also be
7824 // delimited by space.
7825 if ((flags & P_EXPAND) || (flags & P_COMMA) || (flags & P_COLON))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 {
John Marriott95dacbb2024-09-10 21:25:14 +02007827 for (p = argend - 1; p >= xp->xp_pattern; --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007829 // count number of backslashes before ' ' or ',' or ':'
7830 if (*p == ' ' || *p == ',' ||
7831 (*p == ':' && (flags & P_COLON)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007833 s = p;
7834 while (s > xp->xp_pattern && *(s - 1) == '\\')
7835 --s;
Yee Cheng Chin54844852023-10-09 18:12:31 +02007836 if ((*p == ' ' && ((xp->xp_backslash & XP_BS_THREE) && (p - s) < 3))
7837#if defined(BACKSLASH_IN_FILENAME)
7838 || (*p == ',' && (flags & P_COMMA) && (p - s) < 1)
7839#else
7840 || (*p == ',' && (flags & P_COMMA) && (p - s) < 2)
7841#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007842 || (*p == ':' && (flags & P_COLON)))
7843 {
7844 xp->xp_pattern = p + 1;
7845 break;
7846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 }
7848 }
7849 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007850
7851 // An option that is a list of single-character flags should always start
7852 // at the end as we don't complete words.
7853 if (flags & P_FLAGLIST)
John Marriott95dacbb2024-09-10 21:25:14 +02007854 xp->xp_pattern = argend;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007855
7856 // Some options can either be using file/dir expansions, or custom value
7857 // expansion depending on what the user typed. Unfortunately we have to
7858 // manually handle it here to make sure we have the correct xp_context set.
7859#ifdef FEAT_SPELL
7860 if (options[opt_idx].var == (char_u *)&p_sps)
7861 {
7862 if (STRNCMP(xp->xp_pattern, "file:", 5) == 0)
7863 {
7864 xp->xp_pattern += 5;
7865 return;
7866 }
7867 else if (options[expand_option_idx].opt_expand_cb != NULL)
7868 {
7869 xp->xp_context = EXPAND_STRING_SETTING;
7870 }
7871 }
7872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873}
7874
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007875/*
7876 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7877 *
7878 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7879 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7880 *
7881 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7882 * regular expression 'regmatch', then stores the match in matches[idx] and
7883 * returns TRUE.
7884 *
7885 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7886 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7887 *
7888 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7889 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7890 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02007891 static int
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007892match_str(
7893 char_u *str,
7894 regmatch_T *regmatch,
7895 char_u **matches,
7896 int idx,
7897 int test_only,
7898 int fuzzy,
7899 char_u *fuzzystr,
7900 fuzmatch_str_T *fuzmatch)
7901{
7902 if (!fuzzy)
7903 {
7904 if (vim_regexec(regmatch, str, (colnr_T)0))
7905 {
7906 if (!test_only)
7907 matches[idx] = vim_strsave(str);
7908 return TRUE;
7909 }
7910 }
7911 else
7912 {
7913 int score;
7914
7915 score = fuzzy_match_str(str, fuzzystr);
7916 if (score != 0)
7917 {
7918 if (!test_only)
7919 {
7920 fuzmatch[idx].idx = idx;
7921 fuzmatch[idx].str = vim_strsave(str);
7922 fuzmatch[idx].score = score;
7923 }
7924
7925 return TRUE;
7926 }
7927 }
7928
7929 return FALSE;
7930}
7931
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007933ExpandSettings(
7934 expand_T *xp,
7935 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007936 char_u *fuzzystr,
7937 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007938 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007939 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007941 int num_normal = 0; // Nr of matching non-term-code settings
7942 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 int opt_idx;
7944 int match;
7945 int count = 0;
7946 char_u *str;
7947 int loop;
7948 int is_term_opt;
7949 char_u name_buf[MAX_KEY_NAME_LEN];
7950 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007951 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007952 int fuzzy;
7953 fuzmatch_str_T *fuzmatch = NULL;
7954
Christian Brabandtcb747892022-05-08 21:10:56 +01007955 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007957 // do this loop twice:
7958 // loop == 0: count the number of matching options
7959 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 for (loop = 0; loop <= 1; ++loop)
7961 {
7962 regmatch->rm_ic = ic;
7963 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7964 {
K.Takataeeec2542021-06-02 13:28:16 +02007965 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007966 {
7967 if (match_str((char_u *)names[match], regmatch, *matches,
7968 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 {
7970 if (loop == 0)
7971 num_normal++;
7972 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007973 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 }
7977 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7978 opt_idx++)
7979 {
7980 if (options[opt_idx].var == NULL)
7981 continue;
7982 if (xp->xp_context == EXPAND_BOOL_SETTINGS
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007983 && !(options[opt_idx].flags & P_BOOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007985 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 if (is_term_opt && num_normal > 0)
7987 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007988
7989 if (match_str(str, regmatch, *matches, count, (loop == 0),
7990 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 {
7992 if (loop == 0)
7993 {
7994 if (is_term_opt)
7995 num_term++;
7996 else
7997 num_normal++;
7998 }
7999 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00008000 count++;
8001 }
8002 else if (!fuzzy && options[opt_idx].shortname != NULL
8003 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01008004 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00008005 {
8006 // Compare against the abbreviated option name (for regular
8007 // expression match). Fuzzy matching (previous if) already
8008 // matches against both the expanded and abbreviated names.
8009 if (loop == 0)
8010 {
8011 if (is_term_opt)
8012 num_term++;
8013 else
8014 num_normal++;
8015 }
8016 else
8017 (*matches)[count++] = vim_strsave(str);
8018 }
8019 else if (is_term_opt)
8020 {
8021 name_buf[0] = '<';
8022 name_buf[1] = 't';
8023 name_buf[2] = '_';
8024 name_buf[3] = str[2];
8025 name_buf[4] = str[3];
8026 name_buf[5] = '>';
8027 name_buf[6] = NUL;
8028
8029 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
8030 fuzzy, fuzzystr, fuzmatch))
8031 {
8032 if (loop == 0)
8033 num_term++;
8034 else
8035 count++;
8036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 }
8038 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00008039
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00008040 // Check terminal key codes, these are not in the option table
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
8042 {
8043 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
8044 {
Keith Thompson184f71c2024-01-04 21:19:04 +01008045 if (!SAFE_isprint(str[0]) || !SAFE_isprint(str[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 continue;
8047
8048 name_buf[0] = 't';
8049 name_buf[1] = '_';
8050 name_buf[2] = str[0];
8051 name_buf[3] = str[1];
8052 name_buf[4] = NUL;
8053
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00008054 if (match_str(name_buf, regmatch, *matches, count,
Bram Moolenaar048d9d22023-05-06 22:21:11 +01008055 (loop == 0), fuzzy, fuzzystr, fuzmatch))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00008056 {
8057 if (loop == 0)
8058 num_term++;
8059 else
8060 count++;
8061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 else
8063 {
8064 name_buf[0] = '<';
8065 name_buf[1] = 't';
8066 name_buf[2] = '_';
8067 name_buf[3] = str[0];
8068 name_buf[4] = str[1];
8069 name_buf[5] = '>';
8070 name_buf[6] = NUL;
8071
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00008072 if (match_str(name_buf, regmatch, *matches, count,
8073 (loop == 0), fuzzy, fuzzystr,
8074 fuzmatch))
8075 {
8076 if (loop == 0)
8077 num_term++;
8078 else
8079 count++;
8080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 }
8082 }
8083
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00008084 // Check special key names.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008085 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
8087 {
8088 name_buf[0] = '<';
8089 STRCPY(name_buf + 1, str);
8090 STRCAT(name_buf, ">");
8091
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00008092 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
8093 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {
8095 if (loop == 0)
8096 num_term++;
8097 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00008098 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 }
8100 }
8101 }
8102 if (loop == 0)
8103 {
8104 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00008105 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00008107 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 else
8109 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00008110 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00008112 *matches = ALLOC_MULT(char_u *, *numMatches);
8113 if (*matches == NULL)
8114 {
8115 *matches = (char_u **)"";
8116 return FAIL;
8117 }
8118 }
8119 else
8120 {
8121 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
8122 if (fuzmatch == NULL)
8123 {
8124 *matches = (char_u **)"";
8125 return FAIL;
8126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 }
8128 }
8129 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00008130
8131 if (fuzzy &&
8132 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
8133 return FAIL;
8134
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 return OK;
8136}
8137
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008138// Escape an option value that can be used on the command-line with :set.
8139// Caller needs to free the returned string, unless NULL is returned.
8140 static char_u*
8141escape_option_str_cmdline(char_u *var)
8142{
8143 char_u *buf;
8144
8145 // A backslash is required before some characters. This is the reverse of
8146 // what happens in do_set().
8147 buf = vim_strsave_escaped(var, escape_chars);
8148 if (buf == NULL)
8149 return NULL;
8150
8151#ifdef BACKSLASH_IN_FILENAME
8152 // For MS-Windows et al. we don't double backslashes at the start and
8153 // before a file name character.
8154 // The reverse is found at stropt_copy_value().
8155 for (var = buf; *var != NUL; MB_PTR_ADV(var))
8156 if (var[0] == '\\' && var[1] == '\\'
8157 && expand_option_idx >= 0
8158 && (options[expand_option_idx].flags & P_EXPAND)
8159 && vim_isfilec(var[2])
8160 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
8161 STRMOVE(var, var + 1);
8162#endif
8163 return buf;
8164}
8165
8166/*
8167 * Expansion handler for :set= when we just want to fill in with the existing value.
8168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00008170ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008172 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 char_u *buf;
8174
zeertzjqb0d45ec2023-01-25 15:04:22 +00008175 *numMatches = 0;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008176 *matches = ALLOC_MULT(char_u *, 1);
zeertzjqb0d45ec2023-01-25 15:04:22 +00008177 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 return FAIL;
8179
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00008180 // For a terminal key code expand_option_idx is < 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 if (expand_option_idx < 0)
8182 {
8183 var = find_termcode(expand_option_name + 2);
8184 if (var == NULL)
8185 expand_option_idx = findoption(expand_option_name);
8186 }
8187
8188 if (expand_option_idx >= 0)
8189 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008190 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 option_value2string(&options[expand_option_idx], expand_option_flags);
8192 var = NameBuff;
8193 }
8194 else if (var == NULL)
8195 var = (char_u *)"";
8196
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008197 buf = escape_option_str_cmdline(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 if (buf == NULL)
8199 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00008200 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 return FAIL;
8202 }
8203
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008204 (*matches)[0] = buf;
zeertzjqb0d45ec2023-01-25 15:04:22 +00008205 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 return OK;
8207}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208
8209/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008210 * Expansion handler for :set=/:set+= when the option has a custom expansion handler.
8211 */
8212 int
8213ExpandStringSetting(
8214 expand_T *xp,
8215 regmatch_T *regmatch,
8216 int *numMatches,
8217 char_u ***matches)
8218{
8219 char_u *var = NULL; // init for GCC
8220 char_u *buf;
8221
8222 if (expand_option_idx < 0 ||
8223 options[expand_option_idx].opt_expand_cb == NULL)
8224 {
8225 // Not supposed to reach this. This function is only for options with
8226 // custom expansion callbacks.
8227 return FAIL;
8228 }
8229
8230 optexpand_T args;
8231 args.oe_varp = get_varp_scope(&options[expand_option_idx], expand_option_flags);
8232 args.oe_append = expand_option_append;
8233 args.oe_regmatch = regmatch;
8234 args.oe_xp = xp;
8235 args.oe_set_arg = xp->xp_line + expand_option_start_col;
8236 args.oe_include_orig_val =
8237 !expand_option_append &&
8238 (*args.oe_set_arg == NUL);
8239
8240 // Retrieve the existing value, but escape it as a reverse of setting it.
8241 // We technically only need to do this when oe_append or
8242 // oe_include_orig_val is true.
8243 option_value2string(&options[expand_option_idx], expand_option_flags);
8244 var = NameBuff;
8245 buf = escape_option_str_cmdline(var);
8246 if (buf == NULL)
8247 return FAIL;
8248
8249 args.oe_opt_value = buf;
8250
8251 int num_ret = options[expand_option_idx].opt_expand_cb(&args, numMatches, matches);
8252
8253 vim_free(buf);
8254 return num_ret;
8255}
8256
8257/*
8258 * Expansion handler for :set-=
8259 */
8260 int
8261ExpandSettingSubtract(
8262 expand_T *xp,
8263 regmatch_T *regmatch,
8264 int *numMatches,
8265 char_u ***matches)
8266{
8267 if (expand_option_idx < 0)
8268 // term option
8269 return ExpandOldSetting(numMatches, matches);
8270
8271 char_u *option_val = *(char_u**)get_option_varp_scope(
8272 expand_option_idx, expand_option_flags);
8273
8274 long_u option_flags = options[expand_option_idx].flags;
8275
8276 if (option_flags & P_NUM)
8277 return ExpandOldSetting(numMatches, matches);
8278 else if (option_flags & P_COMMA)
8279 {
8280 // Split the option by comma, then present each option to the user if
8281 // it matches the pattern.
8282 // This condition needs to go first, because 'whichwrap' has both
8283 // P_COMMA and P_FLAGLIST.
8284 garray_T ga;
8285
8286 char_u *item;
8287 char_u *option_copy;
8288 char_u *next_val;
8289 char_u *comma;
8290
8291 if (*option_val == NUL)
8292 return FAIL;
8293
8294 // Make a copy as we need to inject null characters destructively.
8295 option_copy = vim_strsave(option_val);
8296 if (option_copy == NULL)
8297 return FAIL;
8298 next_val = option_copy;
8299
8300 ga_init2(&ga, sizeof(char_u *), 10);
8301
8302 do
8303 {
8304 item = next_val;
8305 comma = vim_strchr(next_val, ',');
8306 while (comma != NULL && comma != next_val && *(comma - 1) == '\\')
8307 {
8308 // "\," is interpreted as a literal comma rather than option
8309 // separator when reading options in copy_option_part(). Skip
8310 // it.
8311 comma = vim_strchr(comma + 1, ',');
8312 }
8313 if (comma != NULL)
8314 {
8315 *comma = NUL; // null-terminate this value, required by later functions
8316 next_val = comma + 1;
8317 }
8318 else
8319 next_val = NULL;
8320
8321 if (*item == NUL)
8322 // empty value, don't add to list
8323 continue;
8324
8325 if (!vim_regexec(regmatch, item, (colnr_T)0))
8326 continue;
8327
8328 char_u *buf = escape_option_str_cmdline(item);
8329 if (buf == NULL)
8330 {
8331 vim_free(option_copy);
8332 ga_clear_strings(&ga);
8333 return FAIL;
8334 }
8335 if (ga_add_string(&ga, buf) != OK)
8336 {
8337 vim_free(buf);
8338 break;
8339 }
8340 } while (next_val != NULL);
8341
8342 vim_free(option_copy);
8343
8344 *matches = ga.ga_data;
8345 *numMatches = ga.ga_len;
8346 return OK;
8347 }
8348 else if (option_flags & P_FLAGLIST)
8349 {
8350 // Only present the flags that are set on the option as the other flags
8351 // are not meaningful to do set-= on.
8352
8353 if (*xp->xp_pattern != NUL)
8354 {
8355 // Don't suggest anything if cmdline is non-empty. Vim's set-=
8356 // behavior requires consecutive strings and it's usually
Viktor Szépedbf749b2023-10-16 09:53:37 +02008357 // unintuitive to users if they try to subtract multiple flags at
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008358 // once.
8359 return FAIL;
8360 }
8361
Yee Cheng Chin6d113472023-10-02 21:38:39 +02008362 size_t num_flags = STRLEN(option_val);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008363 if (num_flags == 0)
8364 return FAIL;
8365
8366 *matches = ALLOC_MULT(char_u *, num_flags + 1);
8367 if (*matches == NULL)
8368 return FAIL;
8369
8370 int count = 0;
8371 char_u *p;
8372
John Marriott95dacbb2024-09-10 21:25:14 +02008373 p = vim_strnsave(option_val, num_flags);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008374 if (p == NULL)
8375 {
8376 VIM_CLEAR(*matches);
8377 return FAIL;
8378 }
8379 (*matches)[count++] = p;
8380
8381 if (num_flags > 1)
8382 {
8383 // If more than one flags, split the flags up and expose each
8384 // character as individual choice.
8385 for (char_u *flag = option_val; *flag != NUL; flag++)
8386 {
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02008387 p = vim_strnsave(flag, 1);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008388 if (p == NULL)
8389 break;
8390 (*matches)[count++] = p;
8391 }
8392 }
8393
8394 *numMatches = count;
8395 return OK;
8396 }
8397
8398 return ExpandOldSetting(numMatches, matches);
8399}
8400
8401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 * Get the value for the numeric or string option *opp in a nice format into
8403 * NameBuff[]. Must not be called with a hidden option!
8404 */
8405 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008406option_value2string(
8407 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008408 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409{
8410 char_u *varp;
8411
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008412 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413
8414 if (opp->flags & P_NUM)
8415 {
8416 long wc = 0;
8417
8418 if (wc_use_keyname(varp, &wc))
8419 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
8420 else if (wc != 0)
8421 STRCPY(NameBuff, transchar((int)wc));
8422 else
8423 sprintf((char *)NameBuff, "%ld", *(long *)varp);
8424 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008425 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 {
8427 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008428 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 NameBuff[0] = NUL;
8430#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008431 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008432 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 STRCPY(NameBuff, "*****");
8434#endif
8435 else if (opp->flags & P_EXPAND)
8436 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008437 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 else if ((char_u **)opp->var == &p_pt)
8439 str2specialbuf(p_pt, NameBuff, MAXPATHL);
8440 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008441 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 }
8443}
8444
8445/*
8446 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
8447 * printed as a keyname.
8448 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
8449 */
8450 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008451wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452{
8453 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
8454 {
8455 *wcp = *(long *)varp;
8456 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
8457 return TRUE;
8458 }
8459 return FALSE;
8460}
8461
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 * Return TRUE if "x" is present in 'shortmess' option, or
8464 * 'shortmess' contains 'a' and "x" is present in SHM_A.
8465 */
8466 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008467shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01008469 return p_shm != NULL &&
8470 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 || (vim_strchr(p_shm, 'a') != NULL
8472 && vim_strchr((char_u *)SHM_A, x) != NULL));
8473}
8474
8475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
8477 *
8478 * Reset 'compatible' and set the values for options that didn't get set yet
8479 * to the Vim defaults.
8480 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008481 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 */
8483 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008484vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008486 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00008487 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008488 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489
8490 if (!option_was_set((char_u *)"cp"))
8491 {
8492 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02008493 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
8495 set_option_default(opt_idx, OPT_FREE, FALSE);
8496 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008497 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008499
8500 if (fname != NULL)
8501 {
8502 p = vim_getenv(envname, &dofree);
8503 if (p == NULL)
8504 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008505 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008506 p = FullName_save(fname, FALSE);
8507 if (p != NULL)
8508 {
8509 vim_setenv(envname, p);
8510 vim_free(p);
8511 }
Christian Brabandt1a741d32025-03-01 16:30:33 +01008512 // Set $MYVIMDIR
8513 export_myvimdir();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008514 }
8515 else if (dofree)
8516 vim_free(p);
8517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518}
8519
8520/*
8521 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
8522 */
8523 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008524change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008526 int opt_idx;
8527
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 if (p_cp != on)
8529 {
8530 p_cp = on;
8531 compatible_set();
8532 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008533 opt_idx = findoption((char_u *)"cp");
8534 if (opt_idx >= 0)
8535 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536}
8537
8538/*
8539 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02008540 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 */
8542 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008543option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544{
8545 int idx;
8546
8547 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008548 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 return FALSE;
8550 if (options[idx].flags & P_WAS_SET)
8551 return TRUE;
8552 return FALSE;
8553}
8554
8555/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008556 * Reset the flag indicating option "name" was set.
8557 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02008558 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008559reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008560{
8561 int idx = findoption(name);
8562
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008563 if (idx < 0)
8564 return FAIL;
8565
8566 options[idx].flags &= ~P_WAS_SET;
8567 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008568}
8569
8570/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 * compatible_set() - Called when 'compatible' has been set or unset.
8572 *
8573 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
8574 * flag) to a Vi compatible value.
8575 * When 'compatible' is unset: Set all options that have a different default
8576 * for Vim (without the P_VI_DEF flag) to that default.
8577 */
8578 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008579compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580{
8581 int opt_idx;
8582
Bram Moolenaardac13472019-09-16 21:06:21 +02008583 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
8585 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
8586 set_option_default(opt_idx, OPT_FREE, p_cp);
8587 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008588 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589}
8590
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 * Check if backspacing over something is allowed.
8593 */
8594 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008595can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008596 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02008598#ifdef FEAT_JOB_CHANNEL
8599 if (what == BS_START && bt_prompt(curbuf))
8600 return FALSE;
8601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 switch (*p_bs)
8603 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008604 case '3': return TRUE;
8605 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 case '1': return (what != BS_START);
8607 case '0': return FALSE;
8608 }
8609 return vim_strchr(p_bs, what) != NULL;
8610}
8611
8612/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01008613 * Return the effective 'scrolloff' value for the current window, using the
8614 * global value when appropriate.
8615 */
8616 long
8617get_scrolloff_value(void)
8618{
8619 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
8620}
8621
8622/*
8623 * Return the effective 'sidescrolloff' value for the current window, using the
8624 * global value when appropriate.
8625 */
8626 long
8627get_sidescrolloff_value(void)
8628{
8629 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
8630}
8631
8632/*
zeertzjqaa925ee2024-06-09 18:24:05 +02008633 * Get the local or global value of 'backupcopy' flags.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008634 */
8635 unsigned int
zeertzjqaa925ee2024-06-09 18:24:05 +02008636get_bkc_flags(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008637{
8638 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
8639}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02008640
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008641#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02008642/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00008643 * Get the local or global value of 'formatlistpat'.
8644 */
8645 char_u *
8646get_flp_value(buf_T *buf)
8647{
Christian Brabandtc53b4672022-01-15 10:01:05 +00008648 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
8649 return p_flp;
8650 return buf->b_p_flp;
8651}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008652#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00008653
8654/*
zeertzjqaa925ee2024-06-09 18:24:05 +02008655 * Get the local or global value of 'virtualedit' flags.
Gary Johnson53ba05b2021-07-26 22:19:10 +02008656 */
8657 unsigned int
8658get_ve_flags(void)
8659{
Gary Johnson51ad8502021-08-03 18:33:08 +02008660 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
8661 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02008662}
8663
Bram Moolenaaree857022019-11-09 23:26:40 +01008664#if defined(FEAT_LINEBREAK) || defined(PROTO)
8665/*
8666 * Get the local or global value of 'showbreak'.
8667 */
8668 char_u *
8669get_showbreak_value(win_T *win)
8670{
8671 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8672 return p_sbr;
8673 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8674 return empty_option;
8675 return win->w_p_sbr;
8676}
8677#endif
8678
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008679#if defined(FEAT_EVAL) || defined(PROTO)
8680/*
8681 * Get window or buffer local options.
8682 */
8683 dict_T *
8684get_winbuf_options(int bufopt)
8685{
8686 dict_T *d;
8687 int opt_idx;
8688
8689 d = dict_alloc();
8690 if (d == NULL)
8691 return NULL;
8692
Bram Moolenaardac13472019-09-16 21:06:21 +02008693 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008694 {
8695 struct vimoption *opt = &options[opt_idx];
8696
8697 if ((bufopt && (opt->indir & PV_BUF))
8698 || (!bufopt && (opt->indir & PV_WIN)))
8699 {
8700 char_u *varp = get_varp(opt);
8701
8702 if (varp != NULL)
8703 {
8704 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008705 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008706 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008707 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008708 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008709 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008710 }
8711 }
8712 }
8713
8714 return d;
8715}
8716#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008717
Bram Moolenaardac13472019-09-16 21:06:21 +02008718#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008719/*
8720 * This is called when 'culopt' is changed
8721 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008722 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008723fill_culopt_flags(char_u *val, win_T *wp)
8724{
8725 char_u *p;
8726 char_u culopt_flags_new = 0;
8727
8728 if (val == NULL)
8729 p = wp->w_p_culopt;
8730 else
8731 p = val;
8732 while (*p != NUL)
8733 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008734 // Note: Keep this in sync with p_culopt_values.
Bram Moolenaar017ba072019-09-14 21:01:23 +02008735 if (STRNCMP(p, "line", 4) == 0)
8736 {
8737 p += 4;
8738 culopt_flags_new |= CULOPT_LINE;
8739 }
8740 else if (STRNCMP(p, "both", 4) == 0)
8741 {
8742 p += 4;
8743 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8744 }
8745 else if (STRNCMP(p, "number", 6) == 0)
8746 {
8747 p += 6;
8748 culopt_flags_new |= CULOPT_NBR;
8749 }
8750 else if (STRNCMP(p, "screenline", 10) == 0)
8751 {
8752 p += 10;
8753 culopt_flags_new |= CULOPT_SCRLINE;
8754 }
8755
8756 if (*p != ',' && *p != NUL)
8757 return FAIL;
8758 if (*p == ',')
8759 ++p;
8760 }
8761
8762 // Can't have both "line" and "screenline".
8763 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8764 return FAIL;
8765 wp->w_p_culopt_flags = culopt_flags_new;
8766
8767 return OK;
8768}
8769#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008770
8771/*
8772 * Get the value of 'magic' adjusted for Vim9 script.
8773 */
8774 int
8775magic_isset(void)
8776{
8777 switch (magic_overruled)
8778 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008779 case OPTION_MAGIC_ON: return TRUE;
8780 case OPTION_MAGIC_OFF: return FALSE;
8781 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008782 }
8783#ifdef FEAT_EVAL
8784 if (in_vim9script())
8785 return TRUE;
8786#endif
8787 return p_magic;
8788}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008789
8790/*
8791 * Set the callback function value for an option that accepts a function name,
8792 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8793 * Returns OK if the option is successfully set to a function, otherwise
8794 * returns FAIL.
8795 */
8796 int
8797option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8798{
8799#ifdef FEAT_EVAL
8800 typval_T *tv;
8801 callback_T cb;
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01008802 int funcname = FALSE;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008803
8804 if (optval == NULL || *optval == NUL)
8805 {
8806 free_callback(optcb);
8807 return OK;
8808 }
8809
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008810 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008811 || (STRNCMP(optval, "function(", 9) == 0)
8812 || (STRNCMP(optval, "funcref(", 8) == 0))
8813 // Lambda expression or a funcref
8814 tv = eval_expr(optval, NULL);
8815 else
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01008816 {
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008817 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008818 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01008819 funcname = TRUE;
8820 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008821 if (tv == NULL)
8822 return FAIL;
8823
8824 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008825 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008826 {
8827 free_tv(tv);
8828 return FAIL;
8829 }
8830
8831 free_callback(optcb);
8832 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008833 if (cb.cb_free_name)
8834 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008835 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008836
Yegappan Lakshmanan6289f912025-01-14 17:13:36 +01008837 char_u *dot = NULL;
8838 if (in_vim9script() && funcname
8839 && ((dot = vim_strchr(optval, '.')) != NULL))
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01008840 {
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01008841 if (optcb->cb_free_name)
8842 vim_free(optcb->cb_name);
Yegappan Lakshmanan6289f912025-01-14 17:13:36 +01008843
8844 imported_T *import = find_imported(optval, dot - optval, FALSE);
8845 if (import != NULL && SCRIPT_ID_VALID(import->imp_sid)
8846 && !(import->imp_flags & IMP_FLAGS_AUTOLOAD))
8847 {
8848 char_u fnamebuf[MAX_FUNC_NAME_LEN];
8849
8850 // Imported non-autoloaded function. Replace the import script
8851 // name (import.funcname) with the script ID (<SNR>123_funcname)
8852 vim_snprintf((char *)fnamebuf, sizeof(fnamebuf), "<SNR>%d_%s",
8853 import->imp_sid, dot + 1);
8854 optcb->cb_name = vim_strsave(fnamebuf);
8855 optcb->cb_free_name = TRUE;
8856 }
8857 else
8858 {
8859 // Imported autoloaded function. Store the function name as
8860 // "import.funcname".
8861 optcb->cb_name = vim_strsave(optval);
8862 optcb->cb_free_name = TRUE;
8863 }
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01008864 }
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008865 // when using Vim9 style "import.funcname" it needs to be expanded to
8866 // "import#funcname".
8867 expand_autload_callback(optcb);
8868
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008869 return OK;
8870#else
8871 return FAIL;
8872#endif
8873}
Christian Brabandt757593c2023-08-22 21:44:10 +02008874
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02008875#if defined(FEAT_TABPANEL)
8876/*
8877 * Process the new 'showtabpanel' option value.
8878 */
8879 char *
John Marriotta9657c82025-06-12 21:37:18 +02008880did_set_showtabpanel(optset_T *args UNUSED)
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02008881{
8882 shell_new_columns();
8883 return NULL;
8884}
8885#endif
8886
Christian Brabandt757593c2023-08-22 21:44:10 +02008887#if defined(FEAT_EVAL) || defined(PROTO)
8888 static void
8889didset_options_sctx(int opt_flags, char **buf)
8890{
8891 for (int i = 0; ; ++i)
8892 {
8893 if (buf[i] == NULL)
8894 break;
8895
8896 int idx = findoption((char_u *)buf[i]);
8897 if (idx >= 0)
8898 set_option_sctx_idx(idx, opt_flags, current_sctx);
8899 }
8900}
8901#endif