blob: ee9502a2ed19ab232eda801561e4ad4b3cec1044 [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
196 && ga_grow(&ga, 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();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001017}
1018#endif
1019
1020
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021/*
1022 * Initialize the options, part two: After getting Rows and Columns and
1023 * setting 'term'.
1024 */
1025 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001026set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001028 int idx;
1029
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001030 // 'scroll' defaults to half the window height. The stored default is zero,
1031 // which results in the actual value computed from the window height.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001032 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001033 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001034 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 comp_col();
1036
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001037 // 'window' is only for backwards compatibility with Vi.
1038 // Default is Rows - 1.
Bram Moolenaard68071d2006-05-02 22:08:30 +00001039 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001040 p_window = Rows - 1;
1041 set_number_default("window", Rows - 1);
1042
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001043 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +01001044#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001045 // If 'background' wasn't set by the user, try guessing the value,
1046 // depending on the terminal name. Only need to check for terminals
1047 // with a dark background, that can handle color.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001048 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001049 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
1050 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001052 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001053 // don't mark it as set, when starting the GUI it may be
1054 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +00001055 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 }
1057#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001058
1059#ifdef CURSOR_SHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001060 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +00001061#endif
1062#ifdef FEAT_MOUSESHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001063 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +00001064#endif
1065#ifdef FEAT_PRINTER
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001066 (void)parse_printoptions(NULL); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +00001067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068}
1069
1070/*
1071 * Initialize the options, part three: After reading the .vimrc
1072 */
1073 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001074set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075{
Bram Moolenaar4f974752019-02-17 17:44:42 +01001076#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077/*
1078 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
1079 * This is done after other initializations, where 'shell' might have been
1080 * set, but only if they have not been set before.
1081 */
1082 char_u *p;
1083 int idx_srr;
1084 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001085# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 int idx_sp;
1087 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001088# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089
1090 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001091 if (idx_srr < 0)
1092 do_srr = FALSE;
1093 else
1094 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001095# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001097 if (idx_sp < 0)
1098 do_sp = FALSE;
1099 else
1100 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001101# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001102 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 if (p != NULL)
1104 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001105 // Default for p_sp is "| tee", for p_srr is ">".
1106 // For known shells it is changed here to include stderr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 if ( fnamecmp(p, "csh") == 0
1108 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001109# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 || fnamecmp(p, "csh.exe") == 0
1111 || fnamecmp(p, "tcsh.exe") == 0
1112# endif
1113 )
1114 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001115# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 if (do_sp)
1117 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001118# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001120# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001122# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1124 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 if (do_srr)
1127 {
1128 p_srr = (char_u *)">&";
1129 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1130 }
1131 }
Mike Williams12795022021-06-28 20:53:58 +02001132# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +02001133 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
1134 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +02001135 else if ( fnamecmp(p, "powershell") == 0
1136 || fnamecmp(p, "powershell.exe") == 0
1137 )
1138 {
1139# if defined(FEAT_QUICKFIX)
1140 if (do_sp)
1141 {
1142 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
1143 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1144 }
1145# endif
1146 if (do_srr)
1147 {
1148 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
1149 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1150 }
1151 }
1152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 else
Natanael Copa56318362021-05-06 18:46:35 +02001154 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if ( fnamecmp(p, "sh") == 0
1156 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001157 || fnamecmp(p, "mksh") == 0
1158 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001160 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001162 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001163 || fnamecmp(p, "ash") == 0
1164 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001165 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001166# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 || fnamecmp(p, "cmd") == 0
1168 || fnamecmp(p, "sh.exe") == 0
1169 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001170 || fnamecmp(p, "mksh.exe") == 0
1171 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001173 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 || fnamecmp(p, "bash.exe") == 0
1175 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001176 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001177 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001179 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001181# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 if (do_sp)
1183 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001184# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001186# else
Mike Williamsa3d1b292021-06-30 20:56:00 +02001187 if (fnamecmp(p, "pwsh") == 0)
1188 p_sp = (char_u *)">%s 2>&1";
1189 else
1190 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001191# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1193 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001194# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 if (do_srr)
1196 {
1197 p_srr = (char_u *)">%s 2>&1";
1198 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1199 }
1200 }
1201 vim_free(p);
1202 }
1203#endif
1204
Bram Moolenaar4f974752019-02-17 17:44:42 +01001205#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001207 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1208 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001210 * set, but only if they have not been set before.
1211 * Default values depend on shell (cmd.exe is default shell):
1212 *
1213 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001214 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001215 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001216 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001217 * "sh" like shells - "-c" "\""
1218 *
1219 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 */
Mike Williams12795022021-06-28 20:53:58 +02001221 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1222 {
1223 int idx_opt;
1224
1225 idx_opt = findoption((char_u *)"shcf");
1226 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1227 {
1228 p_shcf = (char_u*)"-Command";
1229 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1230 }
1231
1232 idx_opt = findoption((char_u *)"sxq");
1233 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1234 {
1235 p_sxq = (char_u*)"\"";
1236 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1237 }
1238 }
1239 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 {
1241 int idx3;
1242
1243 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001244 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 {
1246 p_shcf = (char_u *)"-c";
1247 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1248 }
1249
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001250 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001252 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 {
1254 p_sxq = (char_u *)"\"";
1255 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001258 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1259 {
1260 int idx3;
1261
1262 /*
1263 * cmd.exe on Windows will strip the first and last double quote given
1264 * on the command line, e.g. most of the time things like:
1265 * cmd /c "my path/to/echo" "my args to echo"
1266 * become:
1267 * my path/to/echo" "my args to echo
1268 * when executed.
1269 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001270 * To avoid this, set shellxquote to surround the command in
1271 * parenthesis. This appears to make most commands work, without
1272 * breaking commands that worked previously, such as
1273 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001274 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001275 idx3 = findoption((char_u *)"sxq");
1276 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1277 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001278 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001279 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1280 }
1281
Bram Moolenaara64ba222012-02-12 23:23:31 +01001282 idx3 = findoption((char_u *)"shcf");
1283 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1284 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001285 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001286 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1287 }
1288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289#endif
1290
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001291 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001292 {
1293 int idx_ffs = findoption((char_u *)"ffs");
1294
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001295 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001296 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1297 set_fileformat(default_fileformat(), OPT_LOCAL);
1298 }
1299
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301}
1302
1303#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1304/*
1305 * When 'helplang' is still at its default value, set it to "lang".
1306 * Only the first two characters of "lang" are used.
1307 */
1308 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001309set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310{
1311 int idx;
John Marriott95dacbb2024-09-10 21:25:14 +02001312 size_t langlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313
John Marriott95dacbb2024-09-10 21:25:14 +02001314 if (lang == NULL) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 return;
John Marriott95dacbb2024-09-10 21:25:14 +02001316
1317 langlen = STRLEN(lang);
1318 if (langlen < 2) // safety check
1319 return;
1320
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 idx = findoption((char_u *)"hlg");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001322 if (idx < 0 || (options[idx].flags & P_WAS_SET))
1323 return;
1324
1325 if (options[idx].flags & P_ALLOCED)
1326 free_string_option(p_hlg);
John Marriott95dacbb2024-09-10 21:25:14 +02001327 p_hlg = vim_strnsave(lang, langlen);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001328 if (p_hlg == NULL)
1329 p_hlg = empty_option;
1330 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001332 // zh_CN becomes "cn", zh_TW becomes "tw"
John Marriott95dacbb2024-09-10 21:25:14 +02001333 if (STRNICMP(p_hlg, "zh_", 3) == 0 && langlen >= 5)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001334 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001335 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1336 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001337 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001338 // any C like setting, such as C.UTF-8, becomes "en"
John Marriott95dacbb2024-09-10 21:25:14 +02001339 else if (langlen >= 1 && *p_hlg == 'C')
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001340 {
1341 p_hlg[0] = 'e';
1342 p_hlg[1] = 'n';
1343 }
1344 p_hlg[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001346 options[idx].flags |= P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347}
1348#endif
1349
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350/*
1351 * 'title' and 'icon' only default to true if they have not been set or reset
1352 * in .vimrc and we can read the old value.
1353 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1354 * they can be reset. This reduces startup time when using X on a remote
1355 * machine.
1356 */
1357 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001358set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359{
1360 int idx1;
1361 long val;
1362
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001363 // If GUI is (going to be) used, we can always set the window title and
1364 // icon name. Saves a bit of time, because the X11 display server does
1365 // not need to be contacted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001367 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 {
1369#ifdef FEAT_GUI
1370 if (gui.starting || gui.in_use)
1371 val = TRUE;
1372 else
1373#endif
1374 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001375 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 p_title = val;
1377 }
1378 idx1 = findoption((char_u *)"icon");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001379 if (idx1 < 0 || (options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001381 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001383
1384#ifdef FEAT_GUI
1385 if (gui.starting || gui.in_use)
1386 val = TRUE;
1387 else
1388#endif
1389 val = mch_can_restore_icon();
1390 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
1391 p_icon = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001394 void
1395ex_set(exarg_T *eap)
1396{
1397 int flags = 0;
1398
1399 if (eap->cmdidx == CMD_setlocal)
1400 flags = OPT_LOCAL;
1401 else if (eap->cmdidx == CMD_setglobal)
1402 flags = OPT_GLOBAL;
1403#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001404 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001405 ex_options(eap);
1406 else
1407#endif
1408 {
1409 if (eap->forceit)
1410 flags |= OPT_ONECOLUMN;
1411 (void)do_set(eap->arg, flags);
1412 }
1413}
1414
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001415/*
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001416 * :set boolean option prefix
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001417 */
Bram Moolenaar47403942022-09-21 21:12:53 +01001418typedef enum {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001419 PREFIX_NO = 0, // "no" prefix
1420 PREFIX_NONE, // no prefix
1421 PREFIX_INV, // "inv" prefix
1422} set_prefix_T;
1423
1424/*
1425 * Return the prefix type for the option name in *argp.
1426 */
1427 static set_prefix_T
1428get_option_prefix(char_u **argp)
1429{
1430 int prefix = PREFIX_NONE;
1431 char_u *arg = *argp;
1432
1433 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
1434 {
1435 prefix = PREFIX_NO;
1436 arg += 2;
1437 }
1438 else if (STRNCMP(arg, "inv", 3) == 0)
1439 {
1440 prefix = PREFIX_INV;
1441 arg += 3;
1442 }
1443
1444 *argp = arg;
1445 return prefix;
1446}
1447
1448/*
1449 * Parse the option name in "arg" and return the option index in "*opt_idxp",
1450 * and the option name length in "*lenp". For a <t_xx> option, return the key
1451 * number in "*keyp".
1452 *
1453 * Returns FAIL if an option starting with "<" doesn't end with a ">",
1454 * otherwise returns OK.
1455 */
1456 static int
1457parse_option_name(char_u *arg, int *opt_idxp, int *lenp, int *keyp)
1458{
1459 int key = 0;
1460 int len;
1461 int opt_idx;
1462
1463 if (*arg == '<')
1464 {
1465 opt_idx = -1;
1466 // look out for <t_>;>
1467 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1468 len = 5;
1469 else
1470 {
1471 len = 1;
1472 while (arg[len] != NUL && arg[len] != '>')
1473 ++len;
1474 }
1475 if (arg[len] != '>')
1476 return FAIL;
1477
1478 arg[len] = NUL; // put NUL after name
1479 if (arg[1] == 't' && arg[2] == '_') // could be term code
1480 opt_idx = findoption(arg + 1);
1481 arg[len++] = '>'; // restore '>'
1482 if (opt_idx == -1)
1483 key = find_key_option(arg + 1, TRUE);
1484 }
1485 else
1486 {
1487 int nextchar; // next non-white char after option name
1488
1489 len = 0;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001490 // The two characters after "t_" may not be alphanumeric.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001491 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1492 len = 4;
1493 else
1494 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1495 ++len;
1496 nextchar = arg[len];
1497 arg[len] = NUL; // put NUL after name
1498 opt_idx = findoption(arg);
1499 arg[len] = nextchar; // restore nextchar
1500 if (opt_idx == -1)
1501 key = find_key_option(arg, FALSE);
1502 }
1503
1504 *keyp = key;
1505 *lenp = len;
1506 *opt_idxp = opt_idx;
1507
1508 return OK;
1509}
1510
1511/*
1512 * Get the option operator (+=, ^=, -=).
1513 */
1514 static set_op_T
1515get_opt_op(char_u *arg)
1516{
1517 set_op_T op = OP_NONE;
1518
1519 if (*arg != NUL && *(arg + 1) == '=')
1520 {
1521 if (*arg == '+')
1522 op = OP_ADDING; // "+="
1523 else if (*arg == '^')
1524 op = OP_PREPENDING; // "^="
1525 else if (*arg == '-')
1526 op = OP_REMOVING; // "-="
1527 }
1528
1529 return op;
1530}
1531
1532/*
1533 * Validate whether the value of the option in "opt_idx" can be changed.
1534 * Returns FAIL if the option can be skipped or cannot be changed. Returns OK
1535 * if it can be changed.
1536 */
1537 static int
1538validate_opt_idx(int opt_idx, int opt_flags, long_u flags, char **errmsg)
1539{
1540 // Skip all options that are not window-local (used when showing
1541 // an already loaded buffer in a window).
1542 if ((opt_flags & OPT_WINONLY)
1543 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1544 return FAIL;
1545
1546 // Skip all options that are window-local (used for :vimgrep).
1547 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1548 && options[opt_idx].var == VAR_WIN)
1549 return FAIL;
1550
1551 // Disallow changing some options from modelines.
1552 if (opt_flags & OPT_MODELINE)
1553 {
1554 if (flags & (P_SECURE | P_NO_ML))
1555 {
1556 *errmsg = e_not_allowed_in_modeline;
1557 return FAIL;
1558 }
1559 if ((flags & P_MLE) && !p_mle)
1560 {
1561 *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
1562 return FAIL;
1563 }
1564#ifdef FEAT_DIFF
1565 // In diff mode some options are overruled. This avoids that
1566 // 'foldmethod' becomes "marker" instead of "diff" and that
1567 // "wrap" gets set.
1568 if (curwin->w_p_diff
1569 && opt_idx >= 0 // shut up coverity warning
1570 && (
1571# ifdef FEAT_FOLDING
1572 options[opt_idx].indir == PV_FDM ||
1573# endif
1574 options[opt_idx].indir == PV_WRAP))
1575 return FAIL;
1576#endif
1577 }
1578
1579#ifdef HAVE_SANDBOX
1580 // Disallow changing some options in the sandbox
1581 if (sandbox != 0 && (flags & P_SECURE))
1582 {
1583 *errmsg = e_not_allowed_in_sandbox;
1584 return FAIL;
1585 }
1586#endif
1587
1588 return OK;
1589}
1590
Bram Moolenaar47403942022-09-21 21:12:53 +01001591/*
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001592 * Get the Vim/Vi default value for a string option.
1593 */
1594 static char_u *
1595stropt_get_default_val(
1596 int opt_idx,
1597 char_u *varp,
1598 int flags,
1599 int cp_val)
1600{
1601 char_u *newval;
1602
1603 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1604 ? VI_DEFAULT : VIM_DEFAULT];
1605 if ((char_u **)varp == &p_bg)
1606 {
1607 // guess the value of 'background'
1608#ifdef FEAT_GUI
1609 if (gui.in_use)
1610 newval = gui_bg_default();
1611 else
1612#endif
1613 newval = term_bg_default();
1614 }
1615 else if ((char_u **)varp == &p_fencs && enc_utf8)
1616 newval = fencs_utf8_default;
1617
1618 // expand environment variables and ~ since the default value was
1619 // already expanded, only required when an environment variable was set
1620 // later
1621 if (newval == NULL)
1622 newval = empty_option;
1623 else
1624 {
1625 char_u *s = option_expand(opt_idx, newval);
1626 if (s == NULL)
1627 s = newval;
1628 newval = vim_strsave(s);
1629 }
1630
1631 return newval;
1632}
1633
1634/*
1635 * Convert the 'backspace' option number value to a string: for adding,
1636 * prepending and removing string.
1637 */
1638 static void
1639opt_backspace_nr2str(
1640 char_u *varp,
1641 char_u **origval_p,
1642 char_u **origval_l_p,
1643 char_u **origval_g_p,
1644 char_u **oldval_p)
1645{
1646 int i = getdigits((char_u **)varp);
1647
1648 switch (i)
1649 {
1650 case 0:
1651 *(char_u **)varp = empty_option;
1652 break;
1653 case 1:
John Marriott95dacbb2024-09-10 21:25:14 +02001654 *(char_u **)varp = vim_strnsave((char_u *)"indent,eol", STRLEN_LITERAL("indent,eol"));
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001655 break;
1656 case 2:
John Marriott95dacbb2024-09-10 21:25:14 +02001657 *(char_u **)varp = vim_strnsave((char_u *)"indent,eol,start", STRLEN_LITERAL("indent,eol,start"));
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001658 break;
1659 case 3:
John Marriott95dacbb2024-09-10 21:25:14 +02001660 *(char_u **)varp = vim_strnsave((char_u *)"indent,eol,nostop", STRLEN_LITERAL("indent,eol,nostop"));
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001661 break;
1662 }
1663 vim_free(*oldval_p);
1664 if (*origval_p == *oldval_p)
1665 *origval_p = *(char_u **)varp;
1666 if (*origval_l_p == *oldval_p)
1667 *origval_l_p = *(char_u **)varp;
1668 if (*origval_g_p == *oldval_p)
1669 *origval_g_p = *(char_u **)varp;
1670 *oldval_p = *(char_u **)varp;
1671}
1672
1673/*
1674 * Convert the 'whichwrap' option number value to a string, for backwards
1675 * compatibility with Vim 3.0.
1676 * Note: 'argp' is a pointer to a char_u pointer and is updated.
1677 */
1678 static char_u *
1679opt_whichwrap_nr2str(char_u **argp, char_u *whichwrap)
1680{
John Marriott95dacbb2024-09-10 21:25:14 +02001681 size_t len = 0;
1682
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001683 *whichwrap = NUL;
1684 int i = getdigits(argp);
1685 if (i & 1)
John Marriott95dacbb2024-09-10 21:25:14 +02001686 {
1687 STRCPY(whichwrap, "b,");
1688 len += 2;
1689 }
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001690 if (i & 2)
John Marriott95dacbb2024-09-10 21:25:14 +02001691 {
1692 STRCPY(whichwrap + len, "s,");
1693 len += 2;
1694 }
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001695 if (i & 4)
John Marriott95dacbb2024-09-10 21:25:14 +02001696 {
1697 STRCPY(whichwrap + len, "h,l,");
1698 len += 4;
1699 }
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001700 if (i & 8)
John Marriott95dacbb2024-09-10 21:25:14 +02001701 {
1702 STRCPY(whichwrap + len, "<,>,");
1703 len += 4;
1704 }
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001705 if (i & 16)
John Marriott95dacbb2024-09-10 21:25:14 +02001706 {
1707 STRCPY(whichwrap + len, "[,],");
1708 len += 4;
1709 }
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001710 if (*whichwrap != NUL) // remove trailing ,
John Marriott95dacbb2024-09-10 21:25:14 +02001711 whichwrap[len - 1] = NUL;
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001712
1713 return whichwrap;
1714}
1715
1716/*
1717 * Copy the new string value into allocated memory for the option.
1718 * Can't use set_string_option_direct(), because we need to remove the
1719 * backslashes.
1720 */
1721 static char_u *
1722stropt_copy_value(
1723 char_u *origval,
1724 char_u **argp,
1725 set_op_T op,
1726 int flags UNUSED)
1727{
1728 char_u *arg = *argp;
1729 unsigned newlen;
1730 char_u *newval;
1731 char_u *s = NULL;
1732
1733 // get a bit too much
1734 newlen = (unsigned)STRLEN(arg) + 1;
1735 if (op != OP_NONE)
1736 newlen += (unsigned)STRLEN(origval) + 1;
1737 newval = alloc(newlen);
1738 if (newval == NULL) // out of mem, don't change
1739 return NULL;
1740 s = newval;
1741
1742 // Copy the string, skip over escaped chars.
1743 // For MS-DOS and WIN32 backslashes before normal file name characters
1744 // are not removed, and keep backslash at start, for "\\machine\path",
1745 // but do remove it for "\\\\machine\\path".
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001746 // The reverse is found in escape_option_str_cmdline().
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001747 while (*arg != NUL && !VIM_ISWHITE(*arg))
1748 {
1749 int i;
1750
1751 if (*arg == '\\' && arg[1] != NUL
1752#ifdef BACKSLASH_IN_FILENAME
1753 && !((flags & P_EXPAND)
1754 && vim_isfilec(arg[1])
1755 && !VIM_ISWHITE(arg[1])
1756 && (arg[1] != '\\'
1757 || (s == newval && arg[2] != '\\')))
1758#endif
1759 )
1760 ++arg; // remove backslash
1761 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
1762 {
1763 // copy multibyte char
1764 mch_memmove(s, arg, (size_t)i);
1765 arg += i;
1766 s += i;
1767 }
1768 else
1769 *s++ = *arg++;
1770 }
1771 *s = NUL;
1772
1773 *argp = arg;
1774 return newval;
1775}
1776
1777/*
1778 * Expand environment variables and ~ in string option value 'newval'.
1779 */
1780 static char_u *
1781stropt_expand_envvar(
1782 int opt_idx,
1783 char_u *origval,
1784 char_u *newval,
1785 set_op_T op)
1786{
1787 char_u *s = option_expand(opt_idx, newval);
1788 if (s == NULL)
1789 return newval;
1790
1791 vim_free(newval);
1792 unsigned newlen = (unsigned)STRLEN(s) + 1;
1793 if (op != OP_NONE)
1794 newlen += (unsigned)STRLEN(origval) + 1;
1795
1796 newval = alloc(newlen);
1797 if (newval == NULL)
1798 return NULL;
1799
1800 STRCPY(newval, s);
1801
1802 return newval;
1803}
1804
1805/*
1806 * Concatenate the original and new values of a string option, adding a "," if
1807 * needed.
1808 */
1809 static void
1810stropt_concat_with_comma(
1811 char_u *origval,
1812 char_u *newval,
1813 set_op_T op,
1814 int flags)
1815{
1816 int len = 0;
1817
1818 int comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1819 if (op == OP_ADDING)
1820 {
1821 len = (int)STRLEN(origval);
1822 // strip a trailing comma, would get 2
1823 if (comma && len > 1
1824 && (flags & P_ONECOMMA) == P_ONECOMMA
1825 && origval[len - 1] == ','
1826 && origval[len - 2] != '\\')
1827 len--;
1828 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1829 mch_memmove(newval, origval, (size_t)len);
1830 }
1831 else
1832 {
1833 len = (int)STRLEN(newval);
1834 STRMOVE(newval + len + comma, origval);
1835 }
1836 if (comma)
1837 newval[len] = ',';
1838}
1839
1840/*
1841 * Remove a value from a string option. Copy string option value in "origval"
1842 * to "newval" and then remove the string "strval" of length "len".
1843 */
1844 static void
1845stropt_remove_val(
1846 char_u *origval,
1847 char_u *newval,
1848 int flags,
1849 char_u *strval,
1850 int len)
1851{
1852 // Remove newval[] from origval[]. (Note: "len" has been set above
1853 // and is used here).
1854 STRCPY(newval, origval);
1855 if (*strval)
1856 {
1857 // may need to remove a comma
1858 if (flags & P_COMMA)
1859 {
1860 if (strval == origval)
1861 {
1862 // include comma after string
1863 if (strval[len] == ',')
1864 ++len;
1865 }
1866 else
1867 {
1868 // include comma before string
1869 --strval;
1870 ++len;
1871 }
1872 }
1873 STRMOVE(newval + (strval - origval), strval + len);
1874 }
1875}
1876
1877/*
1878 * Remove flags that appear twice in the string option value 'newval'.
1879 */
1880 static void
1881stropt_remove_dupflags(char_u *newval, int flags)
1882{
1883 char_u *s = newval;
1884
1885 // Remove flags that appear twice.
1886 while (*s)
1887 {
1888 // if options have P_FLAGLIST and P_ONECOMMA such as 'whichwrap'
1889 if (flags & P_ONECOMMA)
1890 {
1891 if (*s != ',' && *(s + 1) == ',' && vim_strchr(s + 2, *s) != NULL)
1892 {
1893 // Remove the duplicated value and the next comma.
1894 STRMOVE(s, s + 2);
1895 continue;
1896 }
1897 }
1898 else
1899 {
1900 if ((!(flags & P_COMMA) || *s != ',')
1901 && vim_strchr(s + 1, *s) != NULL)
1902 {
1903 STRMOVE(s, s + 1);
1904 continue;
1905 }
1906 }
1907 ++s;
1908 }
1909}
1910
1911/*
1912 * Get the string value specified for a ":set" command. The following set
1913 * options are supported:
1914 * set {opt}&
1915 * set {opt}<
1916 * set {opt}={val}
1917 * set {opt}:{val}
1918 */
1919 static char_u *
1920stropt_get_newval(
1921 int nextchar,
1922 int opt_idx,
1923 char_u **argp,
1924 char_u *varp,
1925 char_u **origval_arg,
1926 char_u **origval_l_arg,
1927 char_u **origval_g_arg,
1928 char_u **oldval_arg,
1929 set_op_T *op_arg,
1930 int flags,
1931 int cp_val)
1932{
1933 char_u *arg = *argp;
1934 char_u *origval = *origval_arg;
1935 char_u *origval_l = *origval_l_arg;
1936 char_u *origval_g = *origval_g_arg;
1937 char_u *oldval = *oldval_arg;
1938 set_op_T op = *op_arg;
1939 char_u *save_arg = NULL;
1940 char_u *newval;
1941 char_u *s = NULL;
1942 char_u whichwrap[80];
1943
1944 if (nextchar == '&') // set to default val
1945 newval = stropt_get_default_val(opt_idx, varp, flags, cp_val);
1946 else if (nextchar == '<') // set to global val
1947 newval = vim_strsave(*(char_u **)get_varp_scope(
1948 &(options[opt_idx]), OPT_GLOBAL));
1949 else
1950 {
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001951 ++arg; // jump to after the '=' or ':'
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001952
1953 // Set 'keywordprg' to ":help" if an empty
1954 // value was passed to :set by the user.
1955 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
1956 {
1957 save_arg = arg;
1958 arg = (char_u *)":help";
1959 }
1960 // Convert 'backspace' number to string
1961 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1962 opt_backspace_nr2str(varp, &origval, &origval_l, &origval_g,
1963 &oldval);
1964 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
1965 {
1966 // Convert 'whichwrap' number to string, for backwards
1967 // compatibility with Vim 3.0.
1968 char_u *t = opt_whichwrap_nr2str(&arg, whichwrap);
1969 save_arg = arg;
1970 arg = t;
1971 }
1972 // Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1973 // version 3.0
1974 else if (*arg == '>' && (varp == (char_u *)&p_dir
1975 || varp == (char_u *)&p_bdir))
1976 ++arg;
1977
1978 // Copy the new string into allocated memory.
1979 newval = stropt_copy_value(origval, &arg, op, flags);
1980 if (newval == NULL)
1981 goto done;
1982
1983 // Expand environment variables and ~.
1984 // Don't do it when adding without inserting a comma.
1985 if (op == OP_NONE || (flags & P_COMMA))
1986 {
1987 newval = stropt_expand_envvar(opt_idx, origval, newval, op);
1988 if (newval == NULL)
1989 goto done;
1990 }
1991
1992 // locate newval[] in origval[] when removing it and when adding to
1993 // avoid duplicates
1994 int len = 0;
1995 if (op == OP_REMOVING || (flags & P_NODUP))
1996 {
1997 len = (int)STRLEN(newval);
John Marriott95dacbb2024-09-10 21:25:14 +02001998 s = find_dup_item(origval, newval, len, flags);
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001999
2000 // do not add if already there
2001 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
2002 {
2003 op = OP_NONE;
2004 STRCPY(newval, origval);
2005 }
2006
2007 // if no duplicate, move pointer to end of original value
2008 if (s == NULL)
2009 s = origval + (int)STRLEN(origval);
2010 }
2011
2012 // concatenate the two strings; add a ',' if needed
2013 if (op == OP_ADDING || op == OP_PREPENDING)
2014 stropt_concat_with_comma(origval, newval, op, flags);
2015 else if (op == OP_REMOVING)
2016 // Remove newval[] from origval[]. (Note: "len" has been set above
2017 // and is used here).
2018 stropt_remove_val(origval, newval, flags, s, len);
2019
2020 if (flags & P_FLAGLIST)
2021 // Remove flags that appear twice.
2022 stropt_remove_dupflags(newval, flags);
2023 }
2024
2025done:
2026 if (save_arg != NULL)
2027 arg = save_arg; // arg was temporarily changed, restore it
2028 *argp = arg;
2029 *origval_arg = origval;
2030 *origval_l_arg = origval_l;
2031 *origval_g_arg = origval_g;
2032 *oldval_arg = oldval;
2033 *op_arg = op;
2034
2035 return newval;
2036}
2037
2038/*
Bram Moolenaar47403942022-09-21 21:12:53 +01002039 * Part of do_set() for string options.
2040 * Returns FAIL on failure, do not process further options.
2041 */
2042 static int
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002043do_set_option_string(
Bram Moolenaar47403942022-09-21 21:12:53 +01002044 int opt_idx,
2045 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01002046 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01002047 int nextchar,
2048 set_op_T op_arg,
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02002049 long_u flags,
Bram Moolenaar47403942022-09-21 21:12:53 +01002050 int cp_val,
2051 char_u *varp_arg,
2052 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +01002053 size_t errbuflen,
Bram Moolenaar47403942022-09-21 21:12:53 +01002054 int *value_checked,
2055 char **errmsg)
2056{
Bram Moolenaar6f981142022-09-22 12:48:58 +01002057 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01002058 set_op_T op = op_arg;
2059 char_u *varp = varp_arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002060 char_u *oldval = NULL; // previous value if *varp
2061 char_u *newval;
2062 char_u *origval = NULL;
2063 char_u *origval_l = NULL;
2064 char_u *origval_g = NULL;
2065#if defined(FEAT_EVAL)
2066 char_u *saved_origval = NULL;
2067 char_u *saved_origval_l = NULL;
2068 char_u *saved_origval_g = NULL;
2069 char_u *saved_newval = NULL;
2070#endif
Bram Moolenaar47403942022-09-21 21:12:53 +01002071
2072 // When using ":set opt=val" for a global option
2073 // with a local value the local value will be
2074 // reset, use the global value here.
2075 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
2076 && ((int)options[opt_idx].indir & PV_BOTH))
2077 varp = options[opt_idx].var;
2078
2079 // The old value is kept until we are sure that the new value is valid.
2080 oldval = *(char_u **)varp;
2081
2082 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2083 {
2084 origval_l = *(char_u **)get_varp_scope(
2085 &(options[opt_idx]), OPT_LOCAL);
2086 origval_g = *(char_u **)get_varp_scope(
2087 &(options[opt_idx]), OPT_GLOBAL);
2088
2089 // A global-local string option might have an empty option as value to
2090 // indicate that the global value should be used.
2091 if (((int)options[opt_idx].indir & PV_BOTH)
2092 && origval_l == empty_option)
2093 origval_l = origval_g;
2094 }
2095
2096 // When setting the local value of a global option, the old value may be
2097 // the global value.
2098 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
2099 origval = *(char_u **)get_varp(&options[opt_idx]);
2100 else
2101 origval = oldval;
2102
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002103 // Get the new value for the option
2104 newval = stropt_get_newval(nextchar, opt_idx, &arg, varp, &origval,
2105 &origval_l, &origval_g, &oldval, &op, flags,
2106 cp_val);
Bram Moolenaar47403942022-09-21 21:12:53 +01002107
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002108 // Set the new value.
Bram Moolenaar47403942022-09-21 21:12:53 +01002109 *(char_u **)(varp) = newval;
Bram Moolenaar339e1142023-02-15 14:26:25 +00002110 if (newval == NULL)
2111 *(char_u **)(varp) = empty_option;
Bram Moolenaar47403942022-09-21 21:12:53 +01002112
2113#if defined(FEAT_EVAL)
2114 if (!starting
2115# ifdef FEAT_CRYPT
2116 && options[opt_idx].indir != PV_KEY
2117# endif
2118 && origval != NULL && newval != NULL)
2119 {
2120 // origval may be freed by did_set_string_option(), make a copy.
2121 saved_origval = vim_strsave(origval);
2122 // newval (and varp) may become invalid if the buffer is closed by
2123 // autocommands.
2124 saved_newval = vim_strsave(newval);
2125 if (origval_l != NULL)
2126 saved_origval_l = vim_strsave(origval_l);
2127 if (origval_g != NULL)
2128 saved_origval_g = vim_strsave(origval_g);
2129 }
2130#endif
2131
2132 {
2133 long_u *p = insecure_flag(opt_idx, opt_flags);
2134 int secure_saved = secure;
2135
2136 // When an option is set in the sandbox, from a modeline or in secure
2137 // mode, then deal with side effects in secure mode. Also when the
2138 // value was set with the P_INSECURE flag and is not completely
2139 // replaced.
2140 if ((opt_flags & OPT_MODELINE)
2141#ifdef HAVE_SANDBOX
2142 || sandbox != 0
2143#endif
2144 || (op != OP_NONE && (*p & P_INSECURE)))
2145 secure = 1;
2146
2147 // Handle side effects, and set the global value for ":set" on local
2148 // options. Note: when setting 'syntax' or 'filetype' autocommands may
2149 // be triggered that can cause havoc.
2150 *errmsg = did_set_string_option(
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002151 opt_idx, (char_u **)varp, oldval, newval, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002152 errbuflen, opt_flags, op, value_checked);
Bram Moolenaar47403942022-09-21 21:12:53 +01002153
2154 secure = secure_saved;
2155 }
2156
2157#if defined(FEAT_EVAL)
2158 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00002159 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01002160 saved_origval_l, saved_origval_g, saved_newval);
2161 vim_free(saved_origval);
2162 vim_free(saved_origval_l);
2163 vim_free(saved_origval_g);
2164 vim_free(saved_newval);
2165#endif
2166
Bram Moolenaar6f981142022-09-22 12:48:58 +01002167 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002168 return *errmsg == NULL ? OK : FAIL;
2169}
2170
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002172 * Set a boolean option.
2173 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002174 */
2175 static char *
2176do_set_option_bool(
2177 int opt_idx,
2178 int opt_flags,
2179 set_prefix_T prefix,
2180 long_u flags,
2181 char_u *varp,
2182 int nextchar,
2183 int afterchar,
2184 int cp_val)
2185
2186{
2187 varnumber_T value;
2188
2189 if (nextchar == '=' || nextchar == ':')
2190 return e_invalid_argument;
Bram Moolenaar546933f2023-02-06 16:40:49 +00002191 if (opt_idx < 0 || varp == NULL)
2192 return NULL; // "cannot happen"
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002193
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002194 // ":set opt!": invert
2195 // ":set opt&": reset to default value
2196 // ":set opt<": reset to global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002197 if (nextchar == '!')
2198 value = *(int *)(varp) ^ 1;
2199 else if (nextchar == '&')
2200 value = (int)(long)(long_i)options[opt_idx].def_val[
2201 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2202 else if (nextchar == '<')
2203 {
2204 // For 'autoread' -1 means to use global value.
2205 if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL)
2206 value = -1;
2207 else
2208 value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2209 }
2210 else
2211 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002212 // ":set invopt": invert
2213 // ":set opt" or ":set noopt": set or reset
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002214 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
2215 return e_trailing_characters;
2216 if (prefix == PREFIX_INV)
2217 value = *(int *)(varp) ^ 1;
2218 else
2219 value = prefix == PREFIX_NO ? 0 : 1;
2220 }
2221
2222 return set_bool_option(opt_idx, varp, (int)value, opt_flags);
2223}
2224
2225/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002226 * Set a numeric option.
2227 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002228 */
2229 static char *
2230do_set_option_numeric(
2231 int opt_idx,
2232 int opt_flags,
2233 char_u **argp,
2234 int nextchar,
2235 set_op_T op,
2236 long_u flags,
2237 int cp_val,
2238 char_u *varp,
2239 char *errbuf,
2240 size_t errbuflen)
2241{
2242 char_u *arg = *argp;
2243 varnumber_T value;
2244 int i;
2245 char *errmsg = NULL;
2246
Bram Moolenaar546933f2023-02-06 16:40:49 +00002247 if (opt_idx < 0 || varp == NULL)
2248 return NULL; // "cannot happen"
2249 //
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002250 /*
2251 * Different ways to set a number option:
2252 * & set to default value
2253 * < set to global value
2254 * <xx> accept special key codes for 'wildchar'
2255 * c accept any non-digit for 'wildchar'
2256 * [-]0-9 set number
2257 * other error
2258 */
2259 ++arg;
2260 if (nextchar == '&')
2261 value = (long)(long_i)options[opt_idx].def_val[
2262 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2263 else if (nextchar == '<')
2264 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002265 if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL)
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002266 // for 'undolevels' NO_LOCAL_UNDOLEVEL means using the global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002267 value = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002268 else if (opt_flags == OPT_LOCAL
2269 && ((long *)varp == &curwin->w_p_siso
2270 || (long *)varp == &curwin->w_p_so))
2271 // for 'scrolloff'/'sidescrolloff' -1 means using the global value
2272 value = -1;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002273 else
2274 value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2275 }
2276 else if (((long *)varp == &p_wc || (long *)varp == &p_wcm)
2277 && (*arg == '<'
2278 || *arg == '^'
2279 || (*arg != NUL
2280 && (!arg[1] || VIM_ISWHITE(arg[1]))
2281 && !VIM_ISDIGIT(*arg))))
2282 {
2283 value = string_to_key(arg, FALSE);
2284 if (value == 0 && (long *)varp != &p_wcm)
2285 {
2286 errmsg = e_invalid_argument;
2287 goto skip;
2288 }
2289 }
2290 else if (*arg == '-' || VIM_ISDIGIT(*arg))
2291 {
2292 // Allow negative (for 'undolevels'), octal and hex numbers.
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002293 vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE, NULL);
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002294 if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i])))
2295 {
2296 errmsg = e_number_required_after_equal;
2297 goto skip;
2298 }
2299 }
2300 else
2301 {
2302 errmsg = e_number_required_after_equal;
2303 goto skip;
2304 }
2305
2306 if (op == OP_ADDING)
2307 value = *(long *)varp + value;
2308 else if (op == OP_PREPENDING)
2309 value = *(long *)varp * value;
2310 else if (op == OP_REMOVING)
2311 value = *(long *)varp - value;
2312
2313 errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen,
2314 opt_flags);
2315
2316skip:
2317 *argp = arg;
2318 return errmsg;
2319}
2320
2321/*
2322 * Set a key code (t_xx) option
2323 */
2324 static char *
2325do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar)
2326{
2327 char_u *arg = *argp;
2328 char_u *p;
2329
2330 if (nextchar == '&')
2331 {
2332 if (add_termcap_entry(key_name, TRUE) == FAIL)
2333 return e_not_found_in_termcap;
2334 }
2335 else
2336 {
2337 ++arg; // jump to after the '=' or ':'
2338 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
2339 if (*p == '\\' && p[1] != NUL)
2340 ++p;
2341 nextchar = *p;
2342 *p = NUL;
2343 add_termcode(key_name, arg, FALSE);
2344 *p = nextchar;
2345 }
2346 if (full_screen)
2347 ttest(FALSE);
2348 redraw_all_later(UPD_CLEAR);
2349
2350 *argp = arg;
2351 return NULL;
2352}
2353
2354/*
2355 * Set an option to a new value.
2356 */
2357 static char *
2358do_set_option_value(
2359 int opt_idx,
2360 int opt_flags,
2361 char_u **argp,
2362 set_prefix_T prefix,
2363 set_op_T op,
2364 long_u flags,
2365 char_u *varp,
2366 char_u *key_name,
2367 int nextchar,
2368 int afterchar,
2369 int cp_val,
2370 int *stopopteval,
2371 char *errbuf,
2372 size_t errbuflen)
2373{
2374 int value_checked = FALSE;
2375 char *errmsg = NULL;
2376 char_u *arg = *argp;
2377
2378 if (flags & P_BOOL)
2379 {
2380 // boolean option
2381 errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp,
2382 nextchar, afterchar, cp_val);
2383 if (errmsg != NULL)
2384 goto skip;
2385 }
2386 else
2387 {
2388 // numeric or string option
2389 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2390 || prefix != PREFIX_NONE)
2391 {
2392 errmsg = e_invalid_argument;
2393 goto skip;
2394 }
2395
2396 if (flags & P_NUM)
2397 {
2398 // numeric option
2399 errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar,
2400 op, flags, cp_val, varp,
2401 errbuf, errbuflen);
2402 if (errmsg != NULL)
2403 goto skip;
2404 }
2405 else if (opt_idx >= 0)
2406 {
2407 // string option
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002408 if (do_set_option_string(opt_idx, opt_flags, &arg, nextchar, op,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002409 flags, cp_val, varp, errbuf, errbuflen,
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002410 &value_checked, &errmsg) == FAIL)
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002411 {
2412 if (errmsg != NULL)
2413 goto skip;
2414 *stopopteval = TRUE;
2415 goto skip;
2416 }
2417 }
2418 else
2419 {
2420 // key code option
2421 errmsg = do_set_option_keycode(&arg, key_name, nextchar);
2422 if (errmsg != NULL)
2423 goto skip;
2424 }
2425 }
2426
2427 if (opt_idx >= 0)
2428 did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked);
2429
2430skip:
2431 *argp = arg;
2432 return errmsg;
2433}
2434
2435/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002436 * Set an option to a new value.
2437 * Return NULL if OK, return an untranslated error message when something is
2438 * wrong. "errbuf[errbuflen]" can be used to create the error message.
2439 */
2440 static char *
2441do_set_option(
2442 int opt_flags,
2443 char_u **argp,
2444 char_u *arg_start,
2445 char_u **startarg,
2446 int *did_show,
2447 int *stopopteval,
2448 char *errbuf,
2449 size_t errbuflen)
2450{
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002451 int opt_idx;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002452 char_u *arg;
2453 set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix
2454 set_op_T op;
2455 long_u flags; // flags for current option
2456 char_u *varp; // pointer to variable for current option
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002457 char_u key_name[2];
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002458 int nextchar; // next non-white char after option name
2459 int afterchar; // character just after option name
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002460 char *errmsg = NULL;
2461 int key;
2462 int len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002463
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002464 prefix = get_option_prefix(argp);
2465 arg = *argp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002466
2467 // find end of name
2468 key = 0;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002469 if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL)
2470 return e_invalid_argument;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002471
2472 // remember character after option name
2473 afterchar = arg[len];
2474
2475 if (in_vim9script())
2476 {
2477 char_u *p = skipwhite(arg + len);
2478
2479 // disallow white space before =val, +=val, -=val, ^=val
2480 if (p > arg + len && (p[0] == '='
2481 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
2482 && p[1] == '=')))
2483 {
2484 errmsg = e_no_white_space_allowed_between_option_and;
2485 arg = p;
2486 *startarg = p;
2487 goto skip;
2488 }
2489 }
2490 else
2491 // skip white space, allow ":set ai ?", ":set hlsearch !"
2492 while (VIM_ISWHITE(arg[len]))
2493 ++len;
2494
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002495 op = get_opt_op(arg + len);
2496 if (op != OP_NONE)
2497 len++;
2498
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002499 nextchar = arg[len];
2500
2501 if (opt_idx == -1 && key == 0) // found a mismatch: skip
2502 {
2503 if (in_vim9script() && arg > arg_start
2504 && vim_strchr((char_u *)"!&<", *arg) != NULL)
2505 errmsg = e_no_white_space_allowed_between_option_and;
2506 else
2507 errmsg = e_unknown_option;
2508 goto skip;
2509 }
2510
2511 if (opt_idx >= 0)
2512 {
2513 if (options[opt_idx].var == NULL) // hidden option: skip
2514 {
2515 // Only give an error message when requesting the value of
2516 // a hidden option, ignore setting it.
2517 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
2518 && (!(options[opt_idx].flags & P_BOOL)
2519 || nextchar == '?'))
2520 errmsg = e_option_not_supported;
2521 goto skip;
2522 }
2523
2524 flags = options[opt_idx].flags;
2525 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
2526 }
2527 else
2528 {
2529 flags = P_STRING;
Bram Moolenaar40b48722023-02-05 17:04:50 +00002530 varp = NULL;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002531 if (key < 0)
2532 {
2533 key_name[0] = KEY2TERMCAP0(key);
2534 key_name[1] = KEY2TERMCAP1(key);
2535 }
2536 else
2537 {
2538 key_name[0] = KS_KEY;
2539 key_name[1] = (key & 0xff);
2540 }
2541 }
2542
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002543 // Make sure the option value can be changed.
2544 if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002545 goto skip;
2546
Bram Moolenaar40b48722023-02-05 17:04:50 +00002547 int cp_val = p_cp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002548 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
2549 {
2550 arg += len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002551 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
2552 {
2553 if (arg[3] == 'm') // "opt&vim": set to Vim default
2554 {
2555 cp_val = FALSE;
2556 arg += 3;
2557 }
2558 else // "opt&vi": set to Vi default
2559 {
2560 cp_val = TRUE;
2561 arg += 2;
2562 }
2563 }
2564 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
2565 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
2566 {
2567 errmsg = e_trailing_characters;
2568 goto skip;
2569 }
2570 }
2571
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002572 // Allow '=' and ':' for historical reasons (MSDOS command.com).
2573 // Allows only one '=' character per "set" command line. grrr. (jw)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002574 if (nextchar == '?'
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002575 || (prefix == PREFIX_NONE
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002576 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
2577 && !(flags & P_BOOL)))
2578 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002579 // print value
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002580 if (*did_show)
2581 msg_putchar('\n'); // cursor below last one
2582 else
2583 {
2584 gotocmdline(TRUE); // cursor at status line
2585 *did_show = TRUE; // remember that we did a line
2586 }
2587 if (opt_idx >= 0)
2588 {
2589 showoneopt(&options[opt_idx], opt_flags);
2590#ifdef FEAT_EVAL
2591 if (p_verbose > 0)
2592 {
2593 // Mention where the option was last set.
2594 if (varp == options[opt_idx].var)
2595 last_set_msg(options[opt_idx].script_ctx);
2596 else if ((int)options[opt_idx].indir & PV_WIN)
2597 last_set_msg(curwin->w_p_script_ctx[
2598 (int)options[opt_idx].indir & PV_MASK]);
2599 else if ((int)options[opt_idx].indir & PV_BUF)
2600 last_set_msg(curbuf->b_p_script_ctx[
2601 (int)options[opt_idx].indir & PV_MASK]);
2602 }
2603#endif
2604 }
2605 else
2606 {
2607 char_u *p;
2608
2609 p = find_termcode(key_name);
2610 if (p == NULL)
2611 {
2612 errmsg = e_key_code_not_set;
2613 goto skip;
2614 }
2615 else
2616 (void)show_one_termcode(key_name, p, TRUE);
2617 }
2618 if (nextchar != '?'
2619 && nextchar != NUL && !VIM_ISWHITE(afterchar))
2620 errmsg = e_trailing_characters;
2621 }
2622 else
2623 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002624 errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op,
2625 flags, varp, key_name, nextchar,
2626 afterchar, cp_val, stopopteval, errbuf,
2627 errbuflen);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002628 }
2629
2630skip:
2631 *argp = arg;
2632 return errmsg;
2633}
2634
2635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 * Parse 'arg' for option settings.
2637 *
2638 * 'arg' may be IObuff, but only when no errors can be present and option
2639 * does not need to be expanded with option_expand().
2640 * "opt_flags":
2641 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002642 * OPT_GLOBAL for ":setglobal"
2643 * OPT_LOCAL for ":setlocal" and a modeline
2644 * OPT_MODELINE for a modeline
2645 * OPT_WINONLY to only set window-local options
2646 * OPT_NOWIN to skip setting window-local options
2647 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002649 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 */
2651 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002652do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002653 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002654 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002656 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002658 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659
2660 if (*arg == NUL)
2661 {
2662 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002663 did_show = TRUE;
2664 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 }
2666
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002667 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002669 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002670 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002672 // ":set all" show all options.
2673 // ":set all&" set all options to their default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 arg += 3;
2675 if (*arg == '&')
2676 {
2677 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002678 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002680 didset_options();
2681 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002682 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 }
2684 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002685 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002687 did_show = TRUE;
2688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002690 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 {
2692 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002693 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002694 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 arg += 7;
2696 }
2697 else
2698 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002699 int stopopteval = FALSE;
2700 char *errmsg = NULL;
Christian Brabandtb39b2402023-11-29 11:34:05 +01002701 char errbuf[ERR_BUFLEN];
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002702 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002704 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2705 &did_show, &stopopteval, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002706 ERR_BUFLEN);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002707 if (stopopteval)
2708 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002710 // Advance to next argument.
2711 // - skip until a blank found, taking care of backslashes
2712 // - skip blanks
2713 // - skip one "=val" argument (for hidden options ":set gfn =xx")
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 for (i = 0; i < 2 ; ++i)
2715 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002716 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 if (*arg++ == '\\' && *arg != NUL)
2718 ++arg;
2719 arg = skipwhite(arg);
2720 if (*arg != '=')
2721 break;
2722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002724 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 {
John Marriott95dacbb2024-09-10 21:25:14 +02002726 i = vim_snprintf((char *)IObuff, IOSIZE, "%s", (char_u *)_(errmsg)) + 2;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002727 if (i + (arg - startarg) < IOSIZE)
2728 {
2729 // append the argument with the error
John Marriott95dacbb2024-09-10 21:25:14 +02002730 STRCPY(IObuff + i - 2, ": ");
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002731 mch_memmove(IObuff + i, startarg, (arg - startarg));
2732 IObuff[i + (arg - startarg)] = NUL;
2733 }
2734 // make sure all characters are printable
2735 trans_characters(IObuff, IOSIZE);
2736
2737 ++no_wait_return; // wait_return() done later
2738 emsg((char *)IObuff); // show error highlighted
2739 --no_wait_return;
2740
2741 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 }
2744
2745 arg = skipwhite(arg);
2746 }
2747
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002748theend:
2749 if (silent_mode && did_show)
2750 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002751 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002752 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002753 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002754 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002755 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002756 out_flush();
2757 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002758 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002759 }
2760
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 return OK;
2762}
2763
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002764/*
2765 * Call this when an option has been given a new value through a user command.
2766 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2767 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002768 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002769did_set_option(
2770 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002771 int opt_flags, // possibly with OPT_MODELINE
2772 int new_value, // value was replaced completely
2773 int value_checked) // value was checked to be safe, no need to set the
2774 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002775{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002776 long_u *p;
2777
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002778 options[opt_idx].flags |= P_WAS_SET;
2779
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002780 // When an option is set in the sandbox, from a modeline or in secure mode
2781 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2782 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002783 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002784 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002785#ifdef HAVE_SANDBOX
2786 || sandbox != 0
2787#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002788 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002789 *p = *p | P_INSECURE;
2790 else if (new_value)
2791 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002792}
2793
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794/*
2795 * Convert a key name or string into a key value.
2796 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002797 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002799 int
2800string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801{
2802 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002803 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 if (*arg == '^')
2805 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002806 if (multi_byte)
2807 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 return *arg;
2809}
2810
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811/*
2812 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2813 * maketitle() to create and display it.
2814 * When switching the title or icon off, call mch_restore_title() to get
2815 * the old value back.
2816 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002817 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002818did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819{
2820 if (starting != NO_SCREEN
2821#ifdef FEAT_GUI
2822 && !gui.starting
2823#endif
2824 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827
2828/*
2829 * set_options_bin - called when 'bin' changes value.
2830 */
2831 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002832set_options_bin(
2833 int oldval,
2834 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002835 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002837 // The option values that are changed when 'bin' changes are
2838 // copied when 'bin is set and restored when 'bin' is reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 if (newval)
2840 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002841 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 {
2843 if (!(opt_flags & OPT_GLOBAL))
2844 {
2845 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2846 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2847 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2848 curbuf->b_p_et_nobin = curbuf->b_p_et;
2849 }
2850 if (!(opt_flags & OPT_LOCAL))
2851 {
2852 p_tw_nobin = p_tw;
2853 p_wm_nobin = p_wm;
2854 p_ml_nobin = p_ml;
2855 p_et_nobin = p_et;
2856 }
2857 }
2858
2859 if (!(opt_flags & OPT_GLOBAL))
2860 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002861 curbuf->b_p_tw = 0; // no automatic line wrap
2862 curbuf->b_p_wm = 0; // no automatic line wrap
2863 curbuf->b_p_ml = 0; // no modelines
2864 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 }
2866 if (!(opt_flags & OPT_LOCAL))
2867 {
2868 p_tw = 0;
2869 p_wm = 0;
2870 p_ml = FALSE;
2871 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002872 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 }
2874 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002875 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 {
2877 if (!(opt_flags & OPT_GLOBAL))
2878 {
2879 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2880 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2881 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2882 curbuf->b_p_et = curbuf->b_p_et_nobin;
2883 }
2884 if (!(opt_flags & OPT_LOCAL))
2885 {
2886 p_tw = p_tw_nobin;
2887 p_wm = p_wm_nobin;
2888 p_ml = p_ml_nobin;
2889 p_et = p_et_nobin;
2890 }
2891 }
Christian Brabandt757593c2023-08-22 21:44:10 +02002892#if defined(FEAT_EVAL) || defined(PROTO)
2893 // Remember where the dependent option were reset
2894 didset_options_sctx(opt_flags, p_bin_dep_opts);
2895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896}
2897
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898/*
2899 * Expand environment variables for some string options.
2900 * These string options cannot be indirect!
2901 * If "val" is NULL expand the current value of the option.
2902 * Return pointer to NameBuff, or NULL when not expanded.
2903 */
2904 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002905option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002907 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2909 return NULL;
2910
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002911 // If val is longer than MAXPATHL no meaningful expansion can be done,
2912 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 if (val != NULL && STRLEN(val) > MAXPATHL)
2914 return NULL;
2915
2916 if (val == NULL)
2917 val = *(char_u **)options[opt_idx].var;
2918
2919 /*
2920 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2921 * Escape spaces when expanding 'tags', they are used to separate file
2922 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002923 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 */
2925 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002926 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002927#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002928 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2929#endif
2930 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002931 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 return NULL;
2933
2934 return NameBuff;
2935}
2936
2937/*
2938 * After setting various option values: recompute variables that depend on
2939 * option values.
2940 */
2941 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002942didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002944 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 (void)init_chartab();
2946
Bram Moolenaardac13472019-09-16 21:06:21 +02002947 didset_string_options();
2948
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002949#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002950 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002951 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002952 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002953 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002954#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002955 // set cedit_key
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002956 (void)did_set_cedit(NULL);
Bram Moolenaar597a4222014-06-25 14:39:50 +02002957#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002958 // initialize the table for 'breakat'.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002959 did_set_breakat(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002960#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002961 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002962}
2963
2964/*
2965 * More side effects of setting options.
2966 */
2967 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002968didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002969{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002970 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002971 (void)highlight_changed();
2972
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002973 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002974 check_opt_wim();
2975
Bram Moolenaareed9d462021-02-15 20:38:25 +01002976 // Parse default for 'listchars'.
zeertzjq6a8d2e12024-01-17 20:54:49 +01002977 (void)set_listchars_option(curwin, curwin->w_p_lcs, TRUE, NULL, 0);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002979 // Parse default for 'fillchars'.
zeertzjq6a8d2e12024-01-17 20:54:49 +01002980 (void)set_fillchars_option(curwin, curwin->w_p_fcs, TRUE, NULL, 0);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002981
2982#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002983 // Parse default for 'clipboard'
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002984 (void)did_set_clipboard(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002985#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002986#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002987 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002988 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002989 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002990 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992}
2993
2994/*
2995 * Check for string options that are NULL (normally only termcap options).
2996 */
2997 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002998check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999{
3000 int opt_idx;
3001
3002 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
3003 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
3004 check_string_option((char_u **)get_varp(&(options[opt_idx])));
3005}
3006
3007/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003008 * Return the option index found by a pointer into term_strings[].
3009 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003011 int
3012get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003014 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015
3016 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
3017 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003018 return opt_idx;
3019 return -1; // cannot happen: didn't find it!
3020}
3021
3022/*
3023 * Mark a terminal option as allocated, found by a pointer into term_strings[].
3024 * Return the option index or -1 if not found.
3025 */
3026 int
3027set_term_option_alloced(char_u **p)
3028{
3029 int opt_idx = get_term_opt_idx(p);
3030
3031 if (opt_idx >= 0)
3032 options[opt_idx].flags |= P_ALLOCED;
3033 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034}
3035
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003036#if defined(FEAT_EVAL) || defined(PROTO)
3037/*
3038 * Return TRUE when option "opt" was set from a modeline or in secure mode.
3039 * Return FALSE when it wasn't.
3040 * Return -1 for an unknown option.
3041 */
3042 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003043was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003044{
3045 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003046 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003047
3048 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003049 {
3050 flagp = insecure_flag(idx, opt_flags);
3051 return (*flagp & P_INSECURE) != 0;
3052 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01003053 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003054 return -1;
3055}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003056
3057/*
3058 * Get a pointer to the flags used for the P_INSECURE flag of option
3059 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01003060 * NOTE: Caller must make sure that "curwin" is set to the window from which
3061 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003062 */
3063 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003064insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003065{
3066 if (opt_flags & OPT_LOCAL)
3067 switch ((int)options[opt_idx].indir)
3068 {
3069#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003070 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003071#endif
3072#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00003073# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003074 case PV_FDE: return &curwin->w_p_fde_flags;
3075 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00003076# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00003077# ifdef FEAT_BEVAL
3078 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
3079# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003080 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003081 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003082# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003083 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003084# endif
3085#endif
3086 }
3087
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003088 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003089 return &options[opt_idx].flags;
3090}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003091#endif
3092
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003093/*
3094 * Redraw the window title and/or tab page text later.
3095 */
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02003096 void
3097redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003098{
3099 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003100 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003101}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003102
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02003104 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
3105 * characters or characters in "allowed".
3106 */
Bram Moolenaare677df82019-09-02 22:31:11 +02003107 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02003108valid_name(char_u *val, char *allowed)
3109{
3110 char_u *s;
3111
3112 for (s = val; *s != NUL; ++s)
3113 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
3114 return FALSE;
3115 return TRUE;
3116}
3117
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003118#if defined(FEAT_EVAL) || defined(PROTO)
3119/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003120 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003121 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003122 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003123 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003124set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003125{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003126 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
3127 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003128 sctx_T new_script_ctx = script_ctx;
3129
Bram Moolenaar51258742020-05-03 17:19:33 +02003130 // Modeline already has the line number set.
3131 if (!(opt_flags & OPT_MODELINE))
3132 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003133
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003134 // Remember where the option was set. For local options need to do that
3135 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003136 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003137 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003138 if (both || (opt_flags & OPT_LOCAL))
3139 {
3140 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003141 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003142 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00003143 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003144 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00003145 if (both)
3146 // also setting the "all buffers" value
3147 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
3148 new_script_ctx;
3149 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003150 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003151}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003152
3153/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00003154 * Get the script context of global option "name".
3155 *
3156 */
3157 sctx_T *
3158get_option_sctx(char *name)
3159{
3160 int idx = findoption((char_u *)name);
3161
3162 if (idx >= 0)
3163 return &options[idx].script_ctx;
3164 siemsg("no such option: %s", name);
3165 return NULL;
3166}
3167
3168/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003169 * Set the script_ctx for a termcap option.
3170 * "name" must be the two character code, e.g. "RV".
3171 * When "name" is NULL use "opt_idx".
3172 */
3173 void
3174set_term_option_sctx_idx(char *name, int opt_idx)
3175{
3176 char_u buf[5];
3177 int idx;
3178
3179 if (name == NULL)
3180 idx = opt_idx;
3181 else
3182 {
3183 buf[0] = 't';
3184 buf[1] = '_';
3185 buf[2] = name[0];
3186 buf[3] = name[1];
3187 buf[4] = 0;
3188 idx = findoption(buf);
3189 }
3190 if (idx >= 0)
3191 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
3192}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003193#endif
3194
Bram Moolenaarf4140482020-02-15 23:06:45 +01003195#if defined(FEAT_EVAL)
3196/*
3197 * Apply the OptionSet autocommand.
3198 */
3199 static void
3200apply_optionset_autocmd(
3201 int opt_idx,
3202 long opt_flags,
3203 long oldval,
3204 long oldval_g,
3205 long newval,
3206 char *errmsg)
3207{
3208 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
3209
3210 // Don't do this while starting up, failure or recursively.
3211 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
3212 return;
3213
3214 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
3215 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
3216 oldval_g);
3217 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
3218 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
3219 (opt_flags & OPT_LOCAL) ? "local" : "global");
3220 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3221 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3222 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
3223 if (opt_flags & OPT_LOCAL)
3224 {
3225 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3226 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3227 }
3228 if (opt_flags & OPT_GLOBAL)
3229 {
3230 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3231 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3232 }
3233 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3234 {
3235 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3236 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3237 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3238 }
3239 if (opt_flags & OPT_MODELINE)
3240 {
3241 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3242 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3243 }
3244 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3245 NULL, FALSE, NULL);
3246 reset_v_option_vars();
3247}
3248#endif
3249
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003250#if defined(FEAT_ARABIC) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003251/*
3252 * Process the updated 'arabic' option value.
3253 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003254 char *
3255did_set_arabic(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003256{
3257 char *errmsg = NULL;
3258
3259 if (curwin->w_p_arab)
3260 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003261 // 'arabic' is set, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003262 if (!p_tbidi)
3263 {
3264 // set rightleft mode
3265 if (!curwin->w_p_rl)
3266 {
3267 curwin->w_p_rl = TRUE;
3268 changed_window_setting();
3269 }
3270
3271 // Enable Arabic shaping (major part of what Arabic requires)
3272 if (!p_arshape)
3273 {
3274 p_arshape = TRUE;
3275 redraw_later_clear();
3276 }
3277 }
3278
3279 // Arabic requires a utf-8 encoding, inform the user if it's not
3280 // set.
3281 if (STRCMP(p_enc, "utf-8") != 0)
3282 {
3283 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3284
3285 msg_source(HL_ATTR(HLF_W));
3286 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
3287#ifdef FEAT_EVAL
3288 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3289#endif
3290 }
3291
3292 // set 'delcombine'
3293 p_deco = TRUE;
3294
3295# ifdef FEAT_KEYMAP
3296 // Force-set the necessary keymap for arabic
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003297 errmsg = set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3298 OPT_LOCAL);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003299# endif
3300 }
3301 else
3302 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003303 // 'arabic' is reset, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003304 if (!p_tbidi)
3305 {
3306 // reset rightleft mode
3307 if (curwin->w_p_rl)
3308 {
3309 curwin->w_p_rl = FALSE;
3310 changed_window_setting();
3311 }
3312
3313 // 'arabicshape' isn't reset, it is a global option and
3314 // another window may still need it "on".
3315 }
3316
3317 // 'delcombine' isn't reset, it is a global option and another
3318 // window may still want it "on".
3319
3320# ifdef FEAT_KEYMAP
3321 // Revert to the default keymap
3322 curbuf->b_p_iminsert = B_IMODE_NONE;
3323 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3324# endif
3325 }
3326
3327 return errmsg;
3328}
3329#endif
3330
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003331#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
3332/*
3333 * Process the updated 'autochdir' option value.
3334 */
3335 char *
3336did_set_autochdir(optset_T *args UNUSED)
3337{
3338 // Change directories when the 'acd' option is set now.
3339 DO_AUTOCHDIR;
3340 return NULL;
3341}
3342#endif
3343
3344#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
3345/*
3346 * Process the updated 'ballooneval' option value.
3347 */
3348 char *
3349did_set_ballooneval(optset_T *args)
3350{
3351 if (balloonEvalForTerm)
3352 return NULL;
3353
3354 if (p_beval && !args->os_oldval.boolean)
3355 gui_mch_enable_beval_area(balloonEval);
3356 else if (!p_beval && args->os_oldval.boolean)
3357 gui_mch_disable_beval_area(balloonEval);
3358
3359 return NULL;
3360}
3361#endif
3362
3363#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
3364/*
3365 * Process the updated 'balloonevalterm' option value.
3366 */
3367 char *
3368did_set_balloonevalterm(optset_T *args UNUSED)
3369{
3370 mch_bevalterm_changed();
3371 return NULL;
3372}
3373#endif
3374
3375/*
3376 * Process the updated 'binary' option value.
3377 */
3378 char *
3379did_set_binary(optset_T *args)
3380{
3381 // when 'bin' is set also set some other options
3382 set_options_bin(args->os_oldval.boolean, curbuf->b_p_bin, args->os_flags);
3383 redraw_titles();
3384
3385 return NULL;
3386}
3387
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003388/*
3389 * Process the updated 'buflisted' option value.
3390 */
3391 char *
3392did_set_buflisted(optset_T *args)
3393{
3394 // when 'buflisted' changes, trigger autocommands
3395 if (args->os_oldval.boolean != curbuf->b_p_bl)
3396 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3397 NULL, NULL, TRUE, curbuf);
3398 return NULL;
3399}
3400
3401/*
3402 * Process the new 'cmdheight' option value.
3403 */
3404 char *
3405did_set_cmdheight(optset_T *args)
3406{
3407 long old_value = args->os_oldval.number;
3408 char *errmsg = NULL;
3409
3410 // if p_ch changed value, change the command line height
3411 if (p_ch < 1)
3412 {
3413 errmsg = e_argument_must_be_positive;
3414 p_ch = 1;
3415 }
3416 if (p_ch > Rows - min_rows() + 1)
3417 p_ch = Rows - min_rows() + 1;
3418
3419 // Only compute the new window layout when startup has been
3420 // completed. Otherwise the frame sizes may be wrong.
3421 if ((p_ch != old_value
3422 || tabline_height() + topframe->fr_height != Rows - p_ch)
3423 && full_screen
3424#ifdef FEAT_GUI
3425 && !gui.starting
3426#endif
3427 )
3428 command_height();
3429
3430 return errmsg;
3431}
3432
3433/*
3434 * Process the updated 'compatible' option value.
3435 */
3436 char *
3437did_set_compatible(optset_T *args UNUSED)
3438{
3439 compatible_set();
3440 return NULL;
3441}
3442
3443#if defined(FEAT_CONCEAL) || defined(PROTO)
3444/*
3445 * Process the new 'conceallevel' option value.
3446 */
3447 char *
3448did_set_conceallevel(optset_T *args UNUSED)
3449{
3450 char *errmsg = NULL;
3451
3452 if (curwin->w_p_cole < 0)
3453 {
3454 errmsg = e_argument_must_be_positive;
3455 curwin->w_p_cole = 0;
3456 }
3457 else if (curwin->w_p_cole > 3)
3458 {
3459 errmsg = e_invalid_argument;
3460 curwin->w_p_cole = 3;
3461 }
3462
3463 return errmsg;
3464}
3465#endif
3466
3467#if defined(FEAT_DIFF) || defined(PROTO)
3468/*
3469 * Process the updated 'diff' option value.
3470 */
3471 char *
3472did_set_diff(optset_T *args UNUSED)
3473{
3474 // May add or remove the buffer from the list of diff buffers.
3475 diff_buf_adjust(curwin);
3476# ifdef FEAT_FOLDING
3477 if (foldmethodIsDiff(curwin))
3478 foldUpdateAll(curwin);
3479# endif
3480 return NULL;
3481}
3482#endif
3483
3484/*
3485 * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb'
3486 * option value.
3487 */
3488 char *
3489did_set_eof_eol_fixeol_bomb(optset_T *args UNUSED)
3490{
3491 // redraw the window title and tab page text
3492 redraw_titles();
3493 return NULL;
3494}
3495
3496/*
3497 * Process the updated 'equalalways' option value.
3498 */
3499 char *
3500did_set_equalalways(optset_T *args)
3501{
3502 if (p_ea && !args->os_oldval.boolean)
3503 win_equal(curwin, FALSE, 0);
3504
3505 return NULL;
3506}
3507
3508#if defined(FEAT_FOLDING) || defined(PROTO)
3509/*
3510 * Process the new 'foldcolumn' option value.
3511 */
3512 char *
3513did_set_foldcolumn(optset_T *args UNUSED)
3514{
3515 char *errmsg = NULL;
3516
3517 if (curwin->w_p_fdc < 0)
3518 {
3519 errmsg = e_argument_must_be_positive;
3520 curwin->w_p_fdc = 0;
3521 }
3522 else if (curwin->w_p_fdc > 12)
3523 {
3524 errmsg = e_invalid_argument;
3525 curwin->w_p_fdc = 12;
3526 }
3527
3528 return errmsg;
3529}
3530
3531/*
3532 * Process the new 'foldlevel' option value.
3533 */
3534 char *
3535did_set_foldlevel(optset_T *args UNUSED)
3536{
3537 if (curwin->w_p_fdl < 0)
3538 curwin->w_p_fdl = 0;
3539 newFoldLevel();
3540 return NULL;
3541}
3542
3543/*
3544 * Process the new 'foldminlines' option value.
3545 */
3546 char *
3547did_set_foldminlines(optset_T *args UNUSED)
3548{
3549 foldUpdateAll(curwin);
3550 return NULL;
3551}
3552
3553/*
3554 * Process the new 'foldnestmax' option value.
3555 */
3556 char *
3557did_set_foldnestmax(optset_T *args UNUSED)
3558{
3559 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3560 foldUpdateAll(curwin);
3561 return NULL;
3562}
3563#endif
3564
3565#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
3566/*
3567 * Process the updated 'hlsearch' option value.
3568 */
3569 char *
3570did_set_hlsearch(optset_T *args UNUSED)
3571{
3572 // when 'hlsearch' is set or reset: reset no_hlsearch
3573 set_no_hlsearch(FALSE);
3574 return NULL;
3575}
3576#endif
3577
3578/*
3579 * Process the updated 'ignorecase' option value.
3580 */
3581 char *
3582did_set_ignorecase(optset_T *args UNUSED)
3583{
3584 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
3585 if (p_hls)
3586 redraw_all_later(UPD_SOME_VALID);
3587 return NULL;
3588}
3589
3590#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
3591/*
3592 * Process the updated 'imdisable' option value.
3593 */
3594 char *
3595did_set_imdisable(optset_T *args UNUSED)
3596{
3597 // Only de-activate it here, it will be enabled when changing mode.
3598 if (p_imdisable)
3599 im_set_active(FALSE);
3600 else if (State & MODE_INSERT)
3601 // When the option is set from an autocommand, it may need to take
3602 // effect right away.
3603 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
3604 return NULL;
3605}
3606#endif
3607
3608/*
3609 * Process the new 'iminsert' option value.
3610 */
3611 char *
3612did_set_iminsert(optset_T *args UNUSED)
3613{
3614 char *errmsg = NULL;
3615
3616 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3617 {
3618 errmsg = e_invalid_argument;
3619 curbuf->b_p_iminsert = B_IMODE_NONE;
3620 }
3621 p_iminsert = curbuf->b_p_iminsert;
3622 if (termcap_active) // don't do this in the alternate screen
3623 showmode();
3624#if defined(FEAT_KEYMAP)
3625 // Show/unshow value of 'keymap' in status lines.
3626 status_redraw_curbuf();
3627#endif
3628
3629 return errmsg;
3630}
3631
3632/*
3633 * Process the new 'imsearch' option value.
3634 */
3635 char *
3636did_set_imsearch(optset_T *args UNUSED)
3637{
3638 char *errmsg = NULL;
3639
3640 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3641 {
3642 errmsg = e_invalid_argument;
3643 curbuf->b_p_imsearch = B_IMODE_NONE;
3644 }
3645 p_imsearch = curbuf->b_p_imsearch;
3646
3647 return errmsg;
3648}
3649
3650#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3651/*
3652 * Process the new 'imstyle' option value.
3653 */
3654 char *
3655did_set_imstyle(optset_T *args UNUSED)
3656{
3657 char *errmsg = NULL;
3658
3659 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3660 errmsg = e_invalid_argument;
3661
3662 return errmsg;
3663}
3664#endif
3665
3666/*
3667 * Process the updated 'insertmode' option value.
3668 */
3669 char *
3670did_set_insertmode(optset_T *args)
3671{
3672 // when 'insertmode' is set from an autocommand need to do work here
3673 if (p_im)
3674 {
3675 if ((State & MODE_INSERT) == 0)
3676 need_start_insertmode = TRUE;
3677 stop_insert_mode = FALSE;
3678 }
3679 // only reset if it was set previously
3680 else if (args->os_oldval.boolean)
3681 {
3682 need_start_insertmode = FALSE;
3683 stop_insert_mode = TRUE;
3684 if (restart_edit != 0 && mode_displayed)
3685 clear_cmdline = TRUE; // remove "(insert)"
3686 restart_edit = 0;
3687 }
3688
3689 return NULL;
3690}
3691
3692#if defined(FEAT_LANGMAP) || defined(PROTO)
3693/*
3694 * Process the updated 'langnoremap' option value.
3695 */
3696 char *
3697did_set_langnoremap(optset_T *args UNUSED)
3698{
3699 // 'langnoremap' -> !'langremap'
3700 p_lrm = !p_lnr;
3701 return NULL;
3702}
3703
3704/*
3705 * Process the updated 'langremap' option value.
3706 */
3707 char *
3708did_set_langremap(optset_T *args UNUSED)
3709{
3710 // 'langremap' -> !'langnoremap'
3711 p_lnr = !p_lrm;
3712 return NULL;
3713}
3714#endif
3715
3716/*
3717 * Process the new 'laststatus' option value.
3718 */
3719 char *
3720did_set_laststatus(optset_T *args UNUSED)
3721{
3722 last_status(FALSE); // (re)set last window status line
3723 return NULL;
3724}
3725
3726#if defined(FEAT_GUI) || defined(PROTO)
3727/*
3728 * Process the new 'linespace' option value.
3729 */
3730 char *
3731did_set_linespace(optset_T *args UNUSED)
3732{
3733 // Recompute gui.char_height and resize the Vim window to keep the
3734 // same number of lines.
3735 if (gui.in_use && gui_mch_adjust_charheight() == OK)
3736 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
3737 return NULL;
3738}
3739#endif
3740
3741/*
3742 * Process the updated 'lisp' option value.
3743 */
3744 char *
3745did_set_lisp(optset_T *args UNUSED)
3746{
3747 // When 'lisp' option changes include/exclude '-' in keyword characters.
3748 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
3749 return NULL;
3750}
3751
3752/*
3753 * Process the new 'maxcombine' option value.
3754 */
3755 char *
3756did_set_maxcombine(optset_T *args UNUSED)
3757{
3758 if (p_mco > MAX_MCO)
3759 p_mco = MAX_MCO;
3760 else if (p_mco < 0)
3761 p_mco = 0;
3762 screenclear(); // will re-allocate the screen
3763 return NULL;
3764}
3765
3766/*
3767 * Process the updated 'modifiable' option value.
3768 */
3769 char *
3770did_set_modifiable(optset_T *args UNUSED)
3771{
3772 // when 'modifiable' is changed, redraw the window title
3773
3774# ifdef FEAT_TERMINAL
3775 // Cannot set 'modifiable' when in Terminal mode.
3776 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3777 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
3778 {
3779 curbuf->b_p_ma = FALSE;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003780 return e_cannot_make_terminal_with_running_job_modifiable;
3781 }
3782# endif
3783 redraw_titles();
3784
3785 return NULL;
3786}
3787
3788/*
3789 * Process the updated 'modified' option value.
3790 */
3791 char *
3792did_set_modified(optset_T *args)
3793{
3794 if (!args->os_newval.boolean)
3795 save_file_ff(curbuf); // Buffer is unchanged
3796 redraw_titles();
zeertzjq5bf6c212024-03-31 18:41:27 +02003797 curbuf->b_modified_was_set = args->os_newval.boolean;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003798 return NULL;
3799}
3800
3801#if defined(FEAT_GUI) || defined(PROTO)
3802/*
3803 * Process the updated 'mousehide' option value.
3804 */
3805 char *
3806did_set_mousehide(optset_T *args UNUSED)
3807{
3808 if (!p_mh)
3809 gui_mch_mousehide(FALSE);
3810 return NULL;
3811}
3812#endif
3813
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003814/*
3815 * Process the updated 'number' or 'relativenumber' option value.
3816 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003817 char *
3818did_set_number_relativenumber(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003819{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003820#if (defined(FEAT_SIGNS) && defined(FEAT_GUI)) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003821 if (gui.in_use
3822 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3823 && curbuf->b_signlist != NULL)
3824 {
3825 // If the 'number' or 'relativenumber' options are modified and
3826 // 'signcolumn' is set to 'number', then clear the screen for a full
3827 // refresh. Otherwise the sign icons are not displayed properly in the
3828 // number column. If the 'number' option is set and only the
3829 // 'relativenumber' option is toggled, then don't refresh the screen
3830 // (optimization).
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003831 if (!(curwin->w_p_nu && ((int *)args->os_varp == &curwin->w_p_rnu)))
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003832 redraw_all_later(UPD_CLEAR);
3833 }
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003834#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003835 return NULL;
3836}
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003837
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003838#if defined(FEAT_LINEBREAK) || defined(PROTO)
3839/*
3840 * Process the new 'numberwidth' option value.
3841 */
3842 char *
3843did_set_numberwidth(optset_T *args UNUSED)
3844{
3845 char *errmsg = NULL;
3846
3847 // 'numberwidth' must be positive
3848 if (curwin->w_p_nuw < 1)
3849 {
3850 errmsg = e_argument_must_be_positive;
3851 curwin->w_p_nuw = 1;
3852 }
3853 if (curwin->w_p_nuw > 20)
3854 {
3855 errmsg = e_invalid_argument;
3856 curwin->w_p_nuw = 20;
3857 }
3858 curwin->w_nrwidth_line_count = 0; // trigger a redraw
3859
3860 return errmsg;
3861}
3862#endif
3863
3864/*
3865 * Process the updated 'paste' option value. Called after p_paste was set or
3866 * reset. When 'paste' is set or reset also change other options.
3867 */
3868 char *
3869did_set_paste(optset_T *args UNUSED)
3870{
3871 static int old_p_paste = FALSE;
3872 static int save_sm = 0;
3873 static int save_sta = 0;
3874 static int save_ru = 0;
3875#ifdef FEAT_RIGHTLEFT
3876 static int save_ri = 0;
3877 static int save_hkmap = 0;
3878#endif
3879 buf_T *buf;
3880
3881 if (p_paste)
3882 {
3883 // Paste switched from off to on.
3884 // Save the current values, so they can be restored later.
3885 if (!old_p_paste)
3886 {
3887 // save options for each buffer
3888 FOR_ALL_BUFFERS(buf)
3889 {
3890 buf->b_p_tw_nopaste = buf->b_p_tw;
3891 buf->b_p_wm_nopaste = buf->b_p_wm;
3892 buf->b_p_sts_nopaste = buf->b_p_sts;
3893 buf->b_p_ai_nopaste = buf->b_p_ai;
3894 buf->b_p_et_nopaste = buf->b_p_et;
3895#ifdef FEAT_VARTABS
3896 if (buf->b_p_vsts_nopaste)
3897 vim_free(buf->b_p_vsts_nopaste);
3898 buf->b_p_vsts_nopaste =
3899 buf->b_p_vsts && buf->b_p_vsts != empty_option
3900 ? vim_strsave(buf->b_p_vsts) : NULL;
3901#endif
3902 }
3903
3904 // save global options
3905 save_sm = p_sm;
3906 save_sta = p_sta;
3907 save_ru = p_ru;
3908#ifdef FEAT_RIGHTLEFT
3909 save_ri = p_ri;
3910 save_hkmap = p_hkmap;
3911#endif
3912 // save global values for local buffer options
3913 p_ai_nopaste = p_ai;
3914 p_et_nopaste = p_et;
3915 p_sts_nopaste = p_sts;
3916 p_tw_nopaste = p_tw;
3917 p_wm_nopaste = p_wm;
3918#ifdef FEAT_VARTABS
3919 if (p_vsts_nopaste)
3920 vim_free(p_vsts_nopaste);
3921 p_vsts_nopaste = p_vsts && p_vsts != empty_option
3922 ? vim_strsave(p_vsts) : NULL;
3923#endif
3924 }
3925
3926 // Always set the option values, also when 'paste' is set when it is
3927 // already on. Set options for each buffer.
3928 FOR_ALL_BUFFERS(buf)
3929 {
3930 buf->b_p_tw = 0; // textwidth is 0
3931 buf->b_p_wm = 0; // wrapmargin is 0
3932 buf->b_p_sts = 0; // softtabstop is 0
3933 buf->b_p_ai = 0; // no auto-indent
3934 buf->b_p_et = 0; // no expandtab
3935#ifdef FEAT_VARTABS
3936 if (buf->b_p_vsts)
3937 free_string_option(buf->b_p_vsts);
3938 buf->b_p_vsts = empty_option;
3939 VIM_CLEAR(buf->b_p_vsts_array);
3940#endif
3941 }
3942
3943 // set global options
3944 p_sm = 0; // no showmatch
3945 p_sta = 0; // no smarttab
3946 if (p_ru)
3947 status_redraw_all(); // redraw to remove the ruler
3948 p_ru = 0; // no ruler
3949#ifdef FEAT_RIGHTLEFT
3950 p_ri = 0; // no reverse insert
3951 p_hkmap = 0; // no Hebrew keyboard
3952#endif
3953 // set global values for local buffer options
3954 p_tw = 0;
3955 p_wm = 0;
3956 p_sts = 0;
3957 p_ai = 0;
Christian Brabandt757593c2023-08-22 21:44:10 +02003958 p_et = 0;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003959#ifdef FEAT_VARTABS
3960 if (p_vsts)
3961 free_string_option(p_vsts);
3962 p_vsts = empty_option;
3963#endif
3964 }
3965
3966 // Paste switched from on to off: Restore saved values.
3967 else if (old_p_paste)
3968 {
3969 // restore options for each buffer
3970 FOR_ALL_BUFFERS(buf)
3971 {
3972 buf->b_p_tw = buf->b_p_tw_nopaste;
3973 buf->b_p_wm = buf->b_p_wm_nopaste;
3974 buf->b_p_sts = buf->b_p_sts_nopaste;
3975 buf->b_p_ai = buf->b_p_ai_nopaste;
3976 buf->b_p_et = buf->b_p_et_nopaste;
3977#ifdef FEAT_VARTABS
3978 if (buf->b_p_vsts)
3979 free_string_option(buf->b_p_vsts);
3980 buf->b_p_vsts = buf->b_p_vsts_nopaste
3981 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
3982 vim_free(buf->b_p_vsts_array);
3983 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
3984 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
3985 else
3986 buf->b_p_vsts_array = NULL;
3987#endif
3988 }
3989
3990 // restore global options
3991 p_sm = save_sm;
3992 p_sta = save_sta;
3993 if (p_ru != save_ru)
3994 status_redraw_all(); // redraw to draw the ruler
3995 p_ru = save_ru;
3996#ifdef FEAT_RIGHTLEFT
3997 p_ri = save_ri;
3998 p_hkmap = save_hkmap;
3999#endif
4000 // set global values for local buffer options
4001 p_ai = p_ai_nopaste;
4002 p_et = p_et_nopaste;
4003 p_sts = p_sts_nopaste;
4004 p_tw = p_tw_nopaste;
4005 p_wm = p_wm_nopaste;
4006#ifdef FEAT_VARTABS
4007 if (p_vsts)
4008 free_string_option(p_vsts);
4009 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
4010#endif
4011 }
4012
4013 old_p_paste = p_paste;
4014
Christian Brabandt757593c2023-08-22 21:44:10 +02004015#if defined(FEAT_EVAL) || defined(PROTO)
4016 // Remember where the dependent options were reset
4017 didset_options_sctx((OPT_LOCAL | OPT_GLOBAL), p_paste_dep_opts);
4018#endif
4019
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004020 return NULL;
4021}
4022
4023
4024#ifdef FEAT_QUICKFIX
4025/*
4026 * Process the updated 'previewwindow' option value.
4027 */
4028 char *
Yegappan Lakshmanan3ee25962023-12-04 20:31:14 +01004029did_set_previewwindow(optset_T *args UNUSED)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004030{
4031 if (!curwin->w_p_pvw)
4032 return NULL;
4033
4034 // There can be only one window with 'previewwindow' set.
4035 win_T *win;
4036
4037 FOR_ALL_WINDOWS(win)
4038 if (win->w_p_pvw && win != curwin)
4039 {
4040 curwin->w_p_pvw = FALSE;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004041 return e_preview_window_already_exists;
4042 }
4043
4044 return NULL;
4045}
4046#endif
4047
4048#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
4049/*
4050 * Process the new 'pyxversion' option value.
4051 */
4052 char *
4053did_set_pyxversion(optset_T *args UNUSED)
4054{
4055 char *errmsg = NULL;
4056
4057 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
4058 errmsg = e_invalid_argument;
4059
4060 return errmsg;
4061}
4062#endif
4063
4064/*
4065 * Process the updated 'readonly' option value.
4066 */
4067 char *
4068did_set_readonly(optset_T *args)
4069{
4070 // when 'readonly' is reset globally, also reset readonlymode
4071 if (!curbuf->b_p_ro && (args->os_flags & OPT_LOCAL) == 0)
4072 readonlymode = FALSE;
4073
4074 // when 'readonly' is set may give W10 again
4075 if (curbuf->b_p_ro)
4076 curbuf->b_did_warn = FALSE;
4077
4078 redraw_titles();
4079
4080 return NULL;
4081}
4082
4083/*
4084 * Process the updated 'scrollbind' option value.
4085 */
4086 char *
4087did_set_scrollbind(optset_T *args UNUSED)
4088{
4089 // when 'scrollbind' is set: snapshot the current position to avoid a jump
4090 // at the end of normal_cmd()
4091 if (!curwin->w_p_scb)
4092 return NULL;
4093
4094 do_check_scrollbind(FALSE);
4095 curwin->w_scbind_pos = curwin->w_topline;
4096 return NULL;
4097}
4098
4099#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
4100/*
4101 * Process the updated 'shellslash' option value.
4102 */
4103 char *
4104did_set_shellslash(optset_T *args UNUSED)
4105{
4106 if (p_ssl)
4107 {
4108 psepc = '/';
4109 psepcN = '\\';
4110 pseps[0] = '/';
4111 }
4112 else
4113 {
4114 psepc = '\\';
4115 psepcN = '/';
4116 pseps[0] = '\\';
4117 }
4118
4119 // need to adjust the file name arguments and buffer names.
4120 buflist_slash_adjust();
4121 alist_slash_adjust();
4122# ifdef FEAT_EVAL
4123 scriptnames_slash_adjust();
4124# endif
4125 return NULL;
4126}
4127#endif
4128
4129/*
4130 * Process the new 'shiftwidth' or the 'tabstop' option value.
4131 */
4132 char *
4133did_set_shiftwidth_tabstop(optset_T *args)
4134{
4135 long *pp = (long *)args->os_varp;
4136 char *errmsg = NULL;
4137
4138 if (curbuf->b_p_sw < 0)
4139 {
4140 errmsg = e_argument_must_be_positive;
4141#ifdef FEAT_VARTABS
4142 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4143 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4144 ? tabstop_first(curbuf->b_p_vts_array)
4145 : curbuf->b_p_ts;
4146#else
4147 curbuf->b_p_sw = curbuf->b_p_ts;
4148#endif
4149 }
4150
4151#ifdef FEAT_FOLDING
4152 if (foldmethodIsIndent(curwin))
4153 foldUpdateAll(curwin);
4154#endif
4155 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
4156 // parse 'cinoptions'.
4157 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
4158 parse_cino(curbuf);
4159
4160 return errmsg;
4161}
4162
4163/*
4164 * Process the new 'showtabline' option value.
4165 */
4166 char *
4167did_set_showtabline(optset_T *args UNUSED)
4168{
4169 shell_new_rows(); // recompute window positions and heights
4170 return NULL;
4171}
4172
4173/*
4174 * Process the updated 'smoothscroll' option value.
4175 */
4176 char *
4177did_set_smoothscroll(optset_T *args UNUSED)
4178{
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004179 if (!curwin->w_p_sms)
4180 curwin->w_skipcol = 0;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004181
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004182 return NULL;
4183}
4184
4185#if defined(FEAT_SPELL) || defined(PROTO)
4186/*
4187 * Process the updated 'spell' option value.
4188 */
4189 char *
4190did_set_spell(optset_T *args UNUSED)
4191{
4192 if (curwin->w_p_spell)
4193 return parse_spelllang(curwin);
4194
4195 return NULL;
4196}
4197#endif
4198
4199/*
4200 * Process the updated 'swapfile' option value.
4201 */
4202 char *
4203did_set_swapfile(optset_T *args UNUSED)
4204{
4205 // when 'swf' is set, create swapfile, when reset remove swapfile
4206 if (curbuf->b_p_swf && p_uc)
4207 ml_open_file(curbuf); // create the swap file
4208 else
4209 // no need to reset curbuf->b_may_swap, ml_open_file() will check
4210 // buf->b_p_swf
4211 mf_close_file(curbuf, TRUE); // remove the swap file
4212 return NULL;
4213}
4214
4215#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004216 char *
4217did_set_termguicolors(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004218{
4219# ifdef FEAT_VTP
4220 // Do not turn on 'tgc' when 24-bit colors are not supported.
4221 if (
4222# ifdef VIMDLL
4223 !gui.in_use && !gui.starting &&
4224# endif
4225 !has_vtp_working())
4226 {
4227 p_tgc = 0;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004228 return e_24_bit_colors_are_not_supported_on_this_environment;
4229 }
4230 if (is_term_win32())
4231 swap_tcap();
4232# endif
4233# ifdef FEAT_GUI
4234 if (!gui.in_use && !gui.starting)
4235# endif
4236 highlight_gui_started();
4237# ifdef FEAT_VTP
4238 // reset t_Co
4239 if (is_term_win32())
4240 {
4241 control_console_color_rgb();
4242 set_termname(T_NAME);
4243 init_highlight(TRUE, FALSE);
4244 }
4245# endif
4246# ifdef FEAT_TERMINAL
4247 term_update_colors_all();
4248 term_update_palette_all();
4249 term_update_wincolor_all();
4250# endif
4251
4252 return NULL;
4253}
4254#endif
4255
4256/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004257 * Process the updated 'terse' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004259 char *
4260did_set_terse(optset_T *args UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004262 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004264 // when 'terse' is set change 'shortmess'
4265 p = vim_strchr(p_shm, SHM_SEARCH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004267 // insert 's' in p_shm
4268 if (p_terse && p == NULL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004269 {
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004270 STRCPY(IObuff, p_shm);
4271 STRCAT(IObuff, "s");
4272 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004273 }
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004274 // remove 's' from p_shm
4275 else if (!p_terse && p != NULL)
4276 STRMOVE(p, p + 1);
4277 return NULL;
4278}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004280/*
4281 * Process the updated 'textauto' option value.
4282 */
4283 char *
4284did_set_textauto(optset_T *args)
4285{
4286 // when 'textauto' is set or reset also change 'fileformats'
4287 set_string_option_direct((char_u *)"ffs", -1,
4288 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
4289 OPT_FREE | args->os_flags, 0);
Bram Moolenaar53744302015-07-17 17:38:22 +02004290
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004291 return NULL;
4292}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004294/*
4295 * Process the updated 'textmode' option value.
4296 */
4297 char *
4298did_set_textmode(optset_T *args)
4299{
4300 // when 'textmode' is set or reset also change 'fileformat'
4301 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, args->os_flags);
4302
4303 return NULL;
4304}
4305
4306/*
4307 * Process the new 'textwidth' option value.
4308 */
4309 char *
4310did_set_textwidth(optset_T *args UNUSED)
4311{
4312 char *errmsg = NULL;
4313
4314 if (curbuf->b_p_tw < 0)
4315 {
4316 errmsg = e_argument_must_be_positive;
4317 curbuf->b_p_tw = 0;
4318 }
4319#ifdef FEAT_SYN_HL
4320 {
4321 win_T *wp;
4322 tabpage_T *tp;
4323
4324 FOR_ALL_TAB_WINDOWS(tp, wp)
4325 check_colorcolumn(wp);
4326 }
Bram Moolenaar53744302015-07-17 17:38:22 +02004327#endif
4328
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004329 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330}
4331
4332/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004333 * Process the updated 'title' or the 'icon' option value.
4334 */
4335 char *
4336did_set_title_icon(optset_T *args UNUSED)
4337{
4338 // when 'title' changed, may need to change the title; same for 'icon'
4339 did_set_title();
4340 return NULL;
4341}
4342
4343/*
4344 * Process the new 'titlelen' option value.
4345 */
4346 char *
4347did_set_titlelen(optset_T *args)
4348{
4349 long old_value = args->os_oldval.number;
4350 char *errmsg = NULL;
4351
4352 // if 'titlelen' has changed, redraw the title
4353 if (p_titlelen < 0)
4354 {
4355 errmsg = e_argument_must_be_positive;
4356 p_titlelen = 85;
4357 }
4358 if (starting != NO_SCREEN && old_value != p_titlelen)
4359 need_maketitle = TRUE;
4360
4361 return errmsg;
4362}
4363
4364#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
4365/*
4366 * Process the updated 'undofile' option value.
4367 */
4368 char *
4369did_set_undofile(optset_T *args)
4370{
4371 // Only take action when the option was set.
4372 if (!curbuf->b_p_udf && !p_udf)
4373 return NULL;
4374
4375 // When reset we do not delete the undo file, the option may be set again
4376 // without making any changes in between.
4377 char_u hash[UNDO_HASH_SIZE];
4378 buf_T *save_curbuf = curbuf;
4379
4380 FOR_ALL_BUFFERS(curbuf)
4381 {
4382 // When 'undofile' is set globally: for every buffer, otherwise
4383 // only for the current buffer: Try to read in the undofile,
4384 // if one exists, the buffer wasn't changed and the buffer was
4385 // loaded
4386 if ((curbuf == save_curbuf
4387 || (args->os_flags & OPT_GLOBAL)
4388 || args->os_flags == 0)
4389 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
4390 {
4391#ifdef FEAT_CRYPT
Christian Brabandtaae58342023-04-23 17:50:22 +01004392 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004393 continue;
4394#endif
4395 u_compute_hash(hash);
4396 u_read_undo(NULL, hash, curbuf->b_fname);
4397 }
4398 }
4399 curbuf = save_curbuf;
4400
4401 return NULL;
4402}
4403#endif
4404
4405/*
4406 * Process the new global 'undolevels' option value.
4407 */
4408 static void
4409update_global_undolevels(long value, long old_value)
4410{
4411 // sync undo before 'undolevels' changes
4412
4413 // use the old value, otherwise u_sync() may not work properly
4414 p_ul = old_value;
4415 u_sync(TRUE);
4416 p_ul = value;
4417}
4418
4419/*
4420 * Process the new buffer local 'undolevels' option value.
4421 */
4422 static void
4423update_buflocal_undolevels(long value, long old_value)
4424{
4425 // use the old value, otherwise u_sync() may not work properly
4426 curbuf->b_p_ul = old_value;
4427 u_sync(TRUE);
4428 curbuf->b_p_ul = value;
4429}
4430
4431/*
4432 * Process the new 'undolevels' option value.
4433 */
4434 char *
4435did_set_undolevels(optset_T *args)
4436{
4437 long *pp = (long *)args->os_varp;
4438
4439 if (pp == &p_ul) // global 'undolevels'
4440 update_global_undolevels(args->os_newval.number,
4441 args->os_oldval.number);
4442 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4443 update_buflocal_undolevels(args->os_newval.number,
4444 args->os_oldval.number);
4445
4446 return NULL;
4447}
4448
4449/*
4450 * Process the new 'updatecount' option value.
4451 */
4452 char *
4453did_set_updatecount(optset_T *args)
4454{
4455 long old_value = args->os_oldval.number;
4456 char *errmsg = NULL;
4457
4458 // when 'updatecount' changes from zero to non-zero, open swap files
4459 if (p_uc < 0)
4460 {
4461 errmsg = e_argument_must_be_positive;
4462 p_uc = 100;
4463 }
4464 if (p_uc && !old_value)
4465 ml_open_files();
4466
4467 return errmsg;
4468}
4469
4470/*
4471 * Process the updated 'weirdinvert' option value.
4472 */
4473 char *
4474did_set_weirdinvert(optset_T *args)
4475{
4476 // When 'weirdinvert' changed, set/reset 't_xs'.
4477 // Then set 'weirdinvert' according to value of 't_xs'.
4478 if (p_wiv && !args->os_oldval.boolean)
4479 T_XS = (char_u *)"y";
4480 else if (!p_wiv && args->os_oldval.boolean)
4481 T_XS = empty_option;
4482 p_wiv = (*T_XS != NUL);
4483
4484 return NULL;
4485}
4486
4487/*
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +02004488 * Process the new 'wildchar' / 'wildcharm' option value.
4489 */
4490 char *
4491did_set_wildchar(optset_T *args)
4492{
4493 long c = *(long *)args->os_varp;
4494
4495 // Don't allow key values that wouldn't work as wildchar.
4496 if (c == Ctrl_C || c == '\n' || c == '\r' || c == K_KENTER)
4497 return e_invalid_argument;
4498
4499 return NULL;
4500}
4501
4502/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004503 * Process the new 'window' option value.
4504 */
4505 char *
4506did_set_window(optset_T *args UNUSED)
4507{
4508 if (p_window < 1)
4509 p_window = 1;
4510 else if (p_window >= Rows)
4511 p_window = Rows - 1;
4512 return NULL;
4513}
4514
4515/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004516 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004518 char *
4519did_set_winheight_helpheight(optset_T *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004521 long *pp = (long *)args->os_varp;
4522 char *errmsg = NULL;
4523
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004524 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004526 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004527 p_wh = 1;
4528 }
4529 if (p_wmh > p_wh)
4530 {
4531 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4532 p_wh = p_wmh;
4533 }
4534 if (p_hh < 0)
4535 {
4536 errmsg = e_argument_must_be_positive;
4537 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 }
4539
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004540 // Change window height NOW
4541 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004543 if (pp == &p_wh && curwin->w_height < p_wh)
4544 win_setheight((int)p_wh);
4545 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
4546 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004549 return errmsg;
4550}
4551
4552/*
4553 * Process the new 'winminheight' option value.
4554 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004555 char *
4556did_set_winminheight(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004557{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004558 char *errmsg = NULL;
4559
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004560 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004562 errmsg = e_argument_must_be_positive;
4563 p_wmh = 0;
4564 }
4565 if (p_wmh > p_wh)
4566 {
4567 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4568 p_wmh = p_wh;
4569 }
4570 win_setminheight();
4571
4572 return errmsg;
4573}
4574
4575/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004576 * Process the new 'winminwidth' option value.
4577 */
4578 char *
4579did_set_winminwidth(optset_T *args UNUSED)
4580{
4581 char *errmsg = NULL;
4582
4583 if (p_wmw < 0)
4584 {
4585 errmsg = e_argument_must_be_positive;
4586 p_wmw = 0;
4587 }
4588 if (p_wmw > p_wiw)
4589 {
4590 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4591 p_wmw = p_wiw;
4592 }
4593 win_setminwidth();
4594
4595 return errmsg;
4596}
4597
4598/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004599 * Process the new 'winwidth' option value.
4600 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004601 char *
4602did_set_winwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004603{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004604 char *errmsg = NULL;
4605
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004606 if (p_wiw < 1)
4607 {
4608 errmsg = e_argument_must_be_positive;
4609 p_wiw = 1;
4610 }
4611 if (p_wmw > p_wiw)
4612 {
4613 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4614 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004616
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004617 // Change window width NOW
4618 if (!ONE_WINDOW && curwin->w_width < p_wiw)
4619 win_setwidth((int)p_wiw);
4620
4621 return errmsg;
4622}
4623
4624/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004625 * Process the updated 'wrap' option value.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004626 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004627 char *
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004628did_set_wrap(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004629{
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004630 // Set w_leftcol or w_skipcol to zero.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004631 if (curwin->w_p_wrap)
4632 curwin->w_leftcol = 0;
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004633 else
4634 curwin->w_skipcol = 0;
4635
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004636 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004637}
4638
4639/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004640 * Set the value of a boolean option, and take care of side effects.
4641 * Returns NULL for success, or an error message for an error.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004642 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004643 static char *
4644set_bool_option(
4645 int opt_idx, // index in options[] table
4646 char_u *varp, // pointer to the option variable
4647 int value, // new value
4648 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004649{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004650 int old_value = *(int *)varp;
4651#if defined(FEAT_EVAL)
4652 int old_global_value = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004654 char *errmsg = NULL;
4655
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004656 // Disallow changing some options from secure mode
4657 if ((secure
4658#ifdef HAVE_SANDBOX
4659 || sandbox != 0
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004660#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004661 ) && (options[opt_idx].flags & P_SECURE))
4662 return e_not_allowed_here;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004663
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004664#if defined(FEAT_EVAL)
4665 // Save the global value before changing anything. This is needed as for
4666 // a global-only option setting the "local value" in fact sets the global
4667 // value (since there is only one value).
4668 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4669 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
4670 OPT_GLOBAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004672
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004673 *(int *)varp = value; // set the new value
4674#ifdef FEAT_EVAL
4675 // Remember where the option was set.
4676 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004677#endif
4678
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004680 need_mouse_correct = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004683 // May set global value for local option.
4684 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4685 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004686
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004687 // Handle side effects of changing a bool option.
4688 if (options[opt_idx].opt_did_set_cb != NULL)
4689 {
4690 optset_T args;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004691
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004692 CLEAR_FIELD(args);
4693 args.os_varp = varp;
4694 args.os_flags = opt_flags;
4695 args.os_oldval.boolean = old_value;
4696 args.os_newval.boolean = value;
4697 args.os_errbuf = NULL;
4698 errmsg = options[opt_idx].opt_did_set_cb(&args);
zeertzjqdeba02d2023-11-02 21:01:19 +01004699 if (errmsg != NULL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004700 return errmsg;
4701 }
4702
4703 // after handling side effects, call autocommand
4704
4705 options[opt_idx].flags |= P_WAS_SET;
4706
4707#if defined(FEAT_EVAL)
4708 apply_optionset_autocmd(opt_idx, opt_flags,
4709 (long)(old_value ? TRUE : FALSE),
4710 (long)(old_global_value ? TRUE : FALSE),
4711 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004712#endif
4713
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004714 comp_col(); // in case 'ruler' or 'showcmd' changed
zeertzjqfcaed6a2024-02-18 09:33:54 +01004715
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004716 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004717 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0
4718 && (options[opt_idx].flags & P_HLONLY) == 0)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004719 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004720
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004721 if ((opt_flags & OPT_NO_REDRAW) == 0)
4722 check_redraw(options[opt_idx].flags);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004723
4724 return errmsg;
4725}
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004726
4727/*
4728 * Check the bounds of numeric options.
4729 */
4730 static char *
4731check_num_option_bounds(
4732 long *pp,
4733 long old_value,
4734 long old_Rows,
4735 long old_Columns,
4736 char *errbuf,
4737 size_t errbuflen,
4738 char *errmsg)
4739{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 if (Rows < min_rows() && full_screen)
4741 {
4742 if (errbuf != NULL)
4743 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004744 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004745 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 errmsg = errbuf;
4747 }
4748 Rows = min_rows();
4749 }
4750 if (Columns < MIN_COLUMNS && full_screen)
4751 {
4752 if (errbuf != NULL)
4753 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004754 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004755 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 errmsg = errbuf;
4757 }
4758 Columns = MIN_COLUMNS;
4759 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004760 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004762 // If the screen (shell) height has been changed, assume it is the
4763 // physical screenheight.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 if (old_Rows != Rows || old_Columns != Columns)
4765 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004766 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 if (updating_screen)
4768 *pp = old_value;
4769 else if (full_screen
4770#ifdef FEAT_GUI
4771 && !gui.starting
4772#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004773 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 set_shellsize((int)Columns, (int)Rows, TRUE);
4775 else
4776 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004777 // Postpone the resizing; check the size and cmdline position for
4778 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 check_shellsize();
4780 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4781 cmdline_row = Rows - p_ch;
4782 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004783 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004784 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 }
4786
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 if (curbuf->b_p_ts <= 0)
4788 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004789 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 curbuf->b_p_ts = 8;
4791 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004792 else if (curbuf->b_p_ts > TABSTOP_MAX)
4793 {
4794 errmsg = e_invalid_argument;
4795 curbuf->b_p_ts = 8;
4796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 if (p_tm < 0)
4798 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004799 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 p_tm = 0;
4801 }
4802 if ((curwin->w_p_scr <= 0
4803 || (curwin->w_p_scr > curwin->w_height
4804 && curwin->w_height > 0))
4805 && full_screen)
4806 {
4807 if (pp == &(curwin->w_p_scr))
4808 {
4809 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004810 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 win_comp_scroll(curwin);
4812 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004813 // If 'scroll' became invalid because of a side effect silently adjust
4814 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 else if (curwin->w_p_scr <= 0)
4816 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004817 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 curwin->w_p_scr = curwin->w_height;
4819 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004820 if (p_hi < 0)
4821 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004822 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004823 p_hi = 0;
4824 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004825 else if (p_hi > 10000)
4826 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004827 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004828 p_hi = 10000;
4829 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004830 if (p_re < 0 || p_re > 2)
4831 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004832 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004833 p_re = 0;
4834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 if (p_report < 0)
4836 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004837 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 p_report = 1;
4839 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004840 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004842 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 p_sj = Rows / 2;
4844 else
4845 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004846 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 p_sj = 1;
4848 }
4849 }
4850 if (p_so < 0 && full_screen)
4851 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004852 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 p_so = 0;
4854 }
4855 if (p_siso < 0 && full_screen)
4856 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004857 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 p_siso = 0;
4859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 if (p_cwh < 1)
4861 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004862 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 p_cwh = 1;
4864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 if (p_ut < 0)
4866 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004867 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 p_ut = 2000;
4869 }
4870 if (p_ss < 0)
4871 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004872 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 p_ss = 0;
4874 }
4875
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004876 return errmsg;
4877}
4878
4879/*
4880 * Set the value of a number option, and take care of side effects.
4881 * Returns NULL for success, or an error message for an error.
4882 */
4883 static char *
4884set_num_option(
4885 int opt_idx, // index in options[] table
4886 char_u *varp, // pointer to the option variable
4887 long value, // new value
4888 char *errbuf, // buffer for error messages
4889 size_t errbuflen, // length of "errbuf"
4890 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4891 // OPT_MODELINE, etc.
4892{
4893 char *errmsg = NULL;
4894 long old_value = *(long *)varp;
4895#if defined(FEAT_EVAL)
4896 long old_global_value = 0; // only used when setting a local and
4897 // global option
4898#endif
4899 long old_Rows = Rows; // remember old Rows
4900 long old_Columns = Columns; // remember old Columns
4901 long *pp = (long *)varp;
4902
4903 // Disallow changing some options from secure mode.
4904 if ((secure
4905#ifdef HAVE_SANDBOX
4906 || sandbox != 0
4907#endif
4908 ) && (options[opt_idx].flags & P_SECURE))
4909 return e_not_allowed_here;
4910
4911#if defined(FEAT_EVAL)
4912 // Save the global value before changing anything. This is needed as for
4913 // a global-only option setting the "local value" in fact sets the global
4914 // value (since there is only one value).
4915 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4916 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4917 OPT_GLOBAL);
4918#endif
4919
4920 *pp = value;
4921#ifdef FEAT_EVAL
4922 // Remember where the option was set.
4923 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4924#endif
4925#ifdef FEAT_GUI
4926 need_mouse_correct = TRUE;
4927#endif
4928
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004929 // Invoke the option specific callback function to validate and apply the
4930 // new value.
4931 if (options[opt_idx].opt_did_set_cb != NULL)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004932 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004933 optset_T args;
4934
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004935 CLEAR_FIELD(args);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004936 args.os_varp = varp;
4937 args.os_flags = opt_flags;
4938 args.os_oldval.number = old_value;
4939 args.os_newval.number = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004940 args.os_errbuf = NULL;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004941 errmsg = options[opt_idx].opt_did_set_cb(&args);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004942 }
4943
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004944 // Check the bounds for numeric options here
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004945 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4946 errbuf, errbuflen, errmsg);
4947
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004948 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4950 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4951
4952 options[opt_idx].flags |= P_WAS_SET;
4953
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004954#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004955 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4956 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004957#endif
4958
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004959 comp_col(); // in case 'columns' or 'ls' changed
zeertzjqfcaed6a2024-02-18 09:33:54 +01004960
Bram Moolenaar913077c2012-03-28 19:59:04 +02004961 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004962 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0
4963 && (options[opt_idx].flags & P_HLONLY) == 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004964 curwin->w_set_curswant = TRUE;
zeertzjqfcaed6a2024-02-18 09:33:54 +01004965
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004966 if ((opt_flags & OPT_NO_REDRAW) == 0)
4967 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968
4969 return errmsg;
4970}
4971
4972/*
4973 * Called after an option changed: check if something needs to be redrawn.
4974 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004975 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004976check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004978 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004979 int doclear = (flags & P_RCLR) == P_RCLR;
4980 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004982 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984
4985 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
zeertzjqfcaed6a2024-02-18 09:33:54 +01004986 {
4987 if (flags & P_HLONLY)
4988 redraw_later(UPD_NOT_VALID);
4989 else
4990 changed_window_setting();
4991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004993 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004994 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004995 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004997 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998}
4999
5000/*
5001 * Find index for option 'arg'.
5002 * Return -1 if not found.
5003 */
Bram Moolenaardac13472019-09-16 21:06:21 +02005004 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005005findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006{
5007 int opt_idx;
5008 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005009 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 int is_term_opt;
5011
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005012 // For first call: Initialize the quick-access table.
5013 // It contains the index for the first option that starts with a certain
5014 // letter. There are 26 letters, plus the first "t_" option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 if (quick_tab[1] == 0)
5016 {
5017 p = options[0].fullname;
5018 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
5019 {
5020 if (s[0] != p[0])
5021 {
5022 if (s[0] == 't' && s[1] == '_')
5023 quick_tab[26] = opt_idx;
5024 else
5025 quick_tab[CharOrdLow(s[0])] = opt_idx;
5026 }
5027 p = s;
5028 }
5029 }
5030
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005031 // Check for name starting with an illegal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 return -1;
5034
5035 is_term_opt = (arg[0] == 't' && arg[1] == '_');
5036 if (is_term_opt)
5037 opt_idx = quick_tab[26];
5038 else
5039 opt_idx = quick_tab[CharOrdLow(arg[0])];
zeertzjqf48558e2023-12-08 21:34:31 +01005040 for (; (s = options[opt_idx].fullname) != NULL && s[0] == arg[0]; opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005042 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 break;
5044 }
zeertzjqf48558e2023-12-08 21:34:31 +01005045 if (s != NULL && s[0] != arg[0])
5046 s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 if (s == NULL && !is_term_opt)
5048 {
5049 opt_idx = quick_tab[CharOrdLow(arg[0])];
5050 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
5051 {
5052 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005053 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 break;
5055 s = NULL;
5056 }
5057 }
5058 if (s == NULL)
5059 opt_idx = -1;
5060 return opt_idx;
5061}
5062
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005063#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
5064 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065/*
5066 * Get the value for an option.
5067 *
5068 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005069 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01005070 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005071 * String option: gov_string, *stringval gets allocated string.
5072 * Hidden Number option: gov_hidden_number.
5073 * Hidden Toggle option: gov_hidden_bool.
5074 * Hidden String option: gov_hidden_string.
5075 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005076 *
5077 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005079 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01005080get_option_value(
5081 char_u *name,
5082 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005083 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005084 int *flagsp,
5085 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086{
5087 int opt_idx;
5088 char_u *varp;
5089
5090 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005091 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01005092 {
5093 int key;
5094
5095 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005096 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005097 {
5098 char_u key_name[2];
5099 char_u *p;
5100
Bram Moolenaar10c75c42021-12-28 20:53:30 +00005101 if (flagsp != NULL)
5102 *flagsp = 0; // terminal option has no flags
5103
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005104 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01005105 if (key < 0)
5106 {
5107 key_name[0] = KEY2TERMCAP0(key);
5108 key_name[1] = KEY2TERMCAP1(key);
5109 }
5110 else
5111 {
5112 key_name[0] = KS_KEY;
5113 key_name[1] = (key & 0xff);
5114 }
5115 p = find_termcode(key_name);
5116 if (p != NULL)
5117 {
5118 if (stringval != NULL)
5119 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005120 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01005121 }
5122 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005123 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01005124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005126 varp = get_varp_scope(&(options[opt_idx]), scope);
5127
5128 if (flagsp != NULL)
5129 // Return the P_xxxx option flags.
5130 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131
5132 if (options[opt_idx].flags & P_STRING)
5133 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005134 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005135 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 if (stringval != NULL)
5137 {
zeertzjq51f0bc32022-05-09 13:33:39 +01005138 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01005139 *stringval = str2special_save(*(char_u **)(varp), FALSE,
5140 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005142 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01005143 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00005144 && **(char_u **)(varp) != NUL)
John Marriott95dacbb2024-09-10 21:25:14 +02005145 *stringval = vim_strnsave((char_u *)"*****", STRLEN_LITERAL("*****"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01005147 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 *stringval = vim_strsave(*(char_u **)(varp));
5149 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005150 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 }
5152
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005153 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005154 return (options[opt_idx].flags & P_NUM)
5155 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 if (options[opt_idx].flags & P_NUM)
5157 *numval = *(long *)varp;
5158 else
5159 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005160 // Special case: 'modified' is b_changed, but we also want to consider
5161 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 if ((int *)varp == &curbuf->b_changed)
5163 *numval = curbufIsChanged();
5164 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02005165 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005167 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168}
5169#endif
5170
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005171#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005172/*
5173 * Returns the option attributes and its value. Unlike the above function it
5174 * will return either global value or local value of the option depending on
5175 * what was requested, but it will never return global value if it was
5176 * requested to return local one and vice versa. Neither it will return
5177 * buffer-local value if it was requested to return window-local one.
5178 *
5179 * Pretends that option is absent if it is not present in the requested scope
5180 * (i.e. has no global, window-local or buffer-local value depending on
5181 * opt_type). Uses
5182 *
5183 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02005184 * 0 hidden or unknown option, also option that does not have requested
5185 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005186 * see SOPT_* in vim.h for other flags
5187 *
5188 * Possible opt_type values: see SREQ_* in vim.h
5189 */
5190 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005191get_option_value_strict(
5192 char_u *name,
5193 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005194 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01005195 int opt_type,
5196 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005197{
5198 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02005199 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005200 struct vimoption *p;
5201 int r = 0;
5202
5203 opt_idx = findoption(name);
5204 if (opt_idx < 0)
5205 return 0;
5206
5207 p = &(options[opt_idx]);
5208
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005209 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005210 if (p->var == NULL)
5211 return 0;
5212
5213 if (p->flags & P_BOOL)
5214 r |= SOPT_BOOL;
5215 else if (p->flags & P_NUM)
5216 r |= SOPT_NUM;
5217 else if (p->flags & P_STRING)
5218 r |= SOPT_STRING;
5219
5220 if (p->indir == PV_NONE)
5221 {
5222 if (opt_type == SREQ_GLOBAL)
5223 r |= SOPT_GLOBAL;
5224 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005225 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005226 }
5227 else
5228 {
5229 if (p->indir & PV_BOTH)
5230 r |= SOPT_GLOBAL;
5231 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005232 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005233
5234 if (p->indir & PV_WIN)
5235 {
5236 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005237 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005238 else
5239 r |= SOPT_WIN;
5240 }
5241 else if (p->indir & PV_BUF)
5242 {
5243 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005244 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005245 else
5246 r |= SOPT_BUF;
5247 }
5248 }
5249
5250 if (stringval == NULL)
5251 return r;
5252
5253 if (opt_type == SREQ_GLOBAL)
5254 varp = p->var;
5255 else
5256 {
5257 if (opt_type == SREQ_BUF)
5258 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005259 // Special case: 'modified' is b_changed, but we also want to
5260 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005261 if (p->indir == PV_MOD)
5262 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005263 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005264 varp = NULL;
5265 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005266# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005267 else if (p->indir == PV_KEY)
5268 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005269 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005270 *stringval = NULL;
5271 varp = NULL;
5272 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005273# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005274 else
5275 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005276 buf_T *save_curbuf = curbuf;
5277
5278 // only getting a pointer, no need to use aucmd_prepbuf()
5279 curbuf = (buf_T *)from;
5280 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005281 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02005282 curbuf = save_curbuf;
5283 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005284 }
5285 }
5286 else if (opt_type == SREQ_WIN)
5287 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005288 win_T *save_curwin = curwin;
5289
5290 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005291 curbuf = curwin->w_buffer;
5292 varp = get_varp(p);
5293 curwin = save_curwin;
5294 curbuf = curwin->w_buffer;
5295 }
5296 if (varp == p->var)
5297 return (r | SOPT_UNSET);
5298 }
5299
5300 if (varp != NULL)
5301 {
5302 if (p->flags & P_STRING)
5303 *stringval = vim_strsave(*(char_u **)(varp));
5304 else if (p->flags & P_NUM)
5305 *numval = *(long *) varp;
5306 else
5307 *numval = *(int *)varp;
5308 }
5309
5310 return r;
5311}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005312
5313/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005314 * Iterate over options. First argument is a pointer to a pointer to a
5315 * structure inside options[] array, second is option type like in the above
5316 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005317 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005318 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005319 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005320 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005321 * first argument to NULL.
5322 *
5323 * Returns full option name for current option on each call.
5324 */
5325 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005326option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005327{
5328 struct vimoption *ret = NULL;
5329 do
5330 {
5331 if (*option == NULL)
5332 *option = (void *) options;
5333 else if (((struct vimoption *) (*option))->fullname == NULL)
5334 {
5335 *option = NULL;
5336 return NULL;
5337 }
5338 else
5339 *option = (void *) (((struct vimoption *) (*option)) + 1);
5340
5341 ret = ((struct vimoption *) (*option));
5342
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005343 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005344 if (ret->var == NULL)
5345 {
5346 ret = NULL;
5347 continue;
5348 }
5349
5350 switch (opt_type)
5351 {
5352 case SREQ_GLOBAL:
5353 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
5354 ret = NULL;
5355 break;
5356 case SREQ_BUF:
5357 if (!(ret->indir & PV_BUF))
5358 ret = NULL;
5359 break;
5360 case SREQ_WIN:
5361 if (!(ret->indir & PV_WIN))
5362 ret = NULL;
5363 break;
5364 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005365 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005366 return NULL;
5367 }
5368 }
5369 while (ret == NULL);
5370
5371 return (char_u *)ret->fullname;
5372}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005373#endif
5374
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005376 * Return the flags for the option at 'opt_idx'.
5377 */
5378 long_u
5379get_option_flags(int opt_idx)
5380{
5381 return options[opt_idx].flags;
5382}
5383
5384/*
5385 * Set a flag for the option at 'opt_idx'.
5386 */
5387 void
5388set_option_flag(int opt_idx, long_u flag)
5389{
5390 options[opt_idx].flags |= flag;
5391}
5392
5393/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005394 * Returns TRUE if the option at 'opt_idx' is a global option
5395 */
5396 int
5397is_global_option(int opt_idx)
5398{
5399 return options[opt_idx].indir == PV_NONE;
5400}
5401
5402/*
5403 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5404 * local value.
5405 */
5406 int
5407is_global_local_option(int opt_idx)
5408{
5409 return options[opt_idx].indir & PV_BOTH;
5410}
5411
5412/*
5413 * Returns TRUE if the option at 'opt_idx' is a window-local option
5414 */
5415 int
5416is_window_local_option(int opt_idx)
5417{
5418 return options[opt_idx].var == VAR_WIN;
5419}
5420
5421/*
5422 * Returns TRUE if the option at 'opt_idx' is a hidden option
5423 */
5424 int
5425is_hidden_option(int opt_idx)
5426{
5427 return options[opt_idx].var == NULL;
5428}
5429
5430#if defined(FEAT_CRYPT) || defined(PROTO)
5431/*
5432 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5433 */
5434 int
5435is_crypt_key_option(int opt_idx)
5436{
5437 return options[opt_idx].indir == PV_KEY;
5438}
5439#endif
5440
5441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 * Set the value of option "name".
5443 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005444 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005445 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005447 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005448set_option_value(
5449 char_u *name,
5450 long number,
5451 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005452 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453{
5454 int opt_idx;
5455 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005456 long_u flags;
Christian Brabandtb39b2402023-11-29 11:34:05 +01005457 static char errbuf[ERR_BUFLEN];
5458 int errbuflen = ERR_BUFLEN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459
5460 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005461 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005462 {
5463 int key;
5464
5465 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005466 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005467 {
5468 char_u key_name[2];
5469
5470 if (key < 0)
5471 {
5472 key_name[0] = KEY2TERMCAP0(key);
5473 key_name[1] = KEY2TERMCAP1(key);
5474 }
5475 else
5476 {
5477 key_name[0] = KS_KEY;
5478 key_name[1] = (key & 0xff);
5479 }
5480 add_termcode(key_name, string, FALSE);
5481 if (full_screen)
5482 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005483 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005484 return NULL;
5485 }
5486
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005487 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 else
5490 {
5491 flags = options[opt_idx].flags;
5492#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005493 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005495 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005496 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005497 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005500 if (flags & P_STRING)
Christian Brabandtb39b2402023-11-29 11:34:05 +01005501 return set_string_option(opt_idx, string, opt_flags, errbuf, errbuflen);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005502
5503 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5504 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005506 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005508 int idx;
5509
5510 // Either we are given a string or we are setting option
5511 // to zero.
5512 for (idx = 0; string[idx] == '0'; ++idx)
5513 ;
5514 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005515 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005516 // There's another character after zeros or the string
5517 // is empty. In both cases, we are trying to set a
5518 // num option using a string.
5519 semsg(_(e_number_required_after_str_equal_str),
5520 name, string);
5521 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005522
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005525 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005526 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005527 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005528 errbuf, sizeof(errbuf), opt_flags);
5529 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005530 else
5531 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005533
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005535 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536}
5537
5538/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005539 * Call set_option_value() and when an error is returned report it.
5540 */
5541 void
5542set_option_value_give_err(
5543 char_u *name,
5544 long number,
5545 char_u *string,
5546 int opt_flags) // OPT_LOCAL or 0 (both)
5547{
5548 char *errmsg = set_option_value(name, number, string, opt_flags);
5549
5550 if (errmsg != NULL)
5551 emsg(_(errmsg));
5552}
5553
5554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 * Get the terminal code for a terminal option.
5556 * Returns NULL when not found.
5557 */
5558 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005559get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560{
5561 int opt_idx;
5562 char_u *varp;
5563
5564 if (tname[0] != 't' || tname[1] != '_' ||
5565 tname[2] == NUL || tname[3] == NUL)
5566 return NULL;
5567 if ((opt_idx = findoption(tname)) >= 0)
5568 {
5569 varp = get_varp(&(options[opt_idx]));
5570 if (varp != NULL)
5571 varp = *(char_u **)(varp);
5572 return varp;
5573 }
5574 return find_termcode(tname + 2);
5575}
5576
5577 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005578get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579{
5580 int i;
5581
5582 i = findoption((char_u *)"hl");
5583 if (i >= 0)
5584 return options[i].def_val[VI_DEFAULT];
5585 return (char_u *)NULL;
5586}
5587
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005588 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005589get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005590{
5591 int i;
5592
5593 i = findoption((char_u *)"enc");
5594 if (i >= 0)
5595 return options[i].def_val[VI_DEFAULT];
5596 return (char_u *)NULL;
5597}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005598
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005599#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005600 int
5601is_option_allocated(char *name)
5602{
5603 int idx = findoption((char_u *)name);
5604
5605 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5606}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005607#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005608
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609/*
5610 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005611 * When "has_lt" is true there is a '<' before "*arg_arg".
5612 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 */
5614 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005615find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005617 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005619 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005621 // Don't use get_special_key_code() for t_xx, we don't want it to call
5622 // add_termcap_entry().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5624 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005625 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005627 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005629 key = find_special_key(&arg, &modifiers,
5630 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005631 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 key = 0;
5633 }
5634 return key;
5635}
5636
5637/*
5638 * if 'all' == 0: show changed options
5639 * if 'all' == 1: show all normal options
5640 * if 'all' == 2: show all terminal options
5641 */
5642 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005643showoptions(
5644 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005645 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646{
5647 struct vimoption *p;
5648 int col;
5649 int isterm;
5650 char_u *varp;
5651 struct vimoption **items;
5652 int item_count;
5653 int run;
5654 int row, rows;
5655 int cols;
5656 int i;
5657 int len;
5658
5659#define INC 20
5660#define GAP 3
5661
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005662 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 if (items == NULL)
5664 return;
5665
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005666 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005668 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005670 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005672 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005674 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005676 // Do the loop two times:
5677 // 1. display the short items
5678 // 2. display the long items (only strings and numbers)
5679 // When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 for (run = 1; run <= 2 && !got_int; ++run)
5681 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005682 // collect the items in items[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 item_count = 0;
5684 for (p = &options[0]; p->fullname != NULL; p++)
5685 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005686 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005687 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005688 continue;
5689
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 varp = NULL;
5691 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005692 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 {
5694 if (p->indir != PV_NONE && !isterm)
5695 varp = get_varp_scope(p, opt_flags);
5696 }
5697 else
5698 varp = get_varp(p);
5699 if (varp != NULL
5700 && ((all == 2 && isterm)
5701 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005702 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005704 if (opt_flags & OPT_ONECOLUMN)
5705 len = Columns;
5706 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005707 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 else
5709 {
5710 option_value2string(p, opt_flags);
5711 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5712 }
5713 if ((len <= INC - GAP && run == 1) ||
5714 (len > INC - GAP && run == 2))
5715 items[item_count++] = p;
5716 }
5717 }
5718
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005719 // display the items
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 if (run == 1)
5721 {
5722 cols = (Columns + GAP - 3) / INC;
5723 if (cols == 0)
5724 cols = 1;
5725 rows = (item_count + cols - 1) / cols;
5726 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005727 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 rows = item_count;
5729 for (row = 0; row < rows && !got_int; ++row)
5730 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005731 msg_putchar('\n'); // go to next line
5732 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 break;
5734 col = 0;
5735 for (i = row; i < item_count; i += rows)
5736 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005737 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 showoneopt(items[i], opt_flags);
5739 col += INC;
5740 }
5741 out_flush();
5742 ui_breakcheck();
5743 }
5744 }
5745 vim_free(items);
5746}
5747
5748/*
5749 * Return TRUE if option "p" has its default value.
5750 */
5751 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005752optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753{
5754 int dvi;
5755
5756 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005757 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005758 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005760 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005762 // the cast to long is required for Manx C, long_i is
5763 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005764 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005765 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5767}
5768
5769/*
5770 * showoneopt: show the value of one option
5771 * must not be called with a hidden option!
5772 */
5773 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005774showoneopt(
5775 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005776 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005778 char_u *varp;
5779 int save_silent = silent_mode;
5780
5781 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005782 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783
5784 varp = get_varp_scope(p, opt_flags);
5785
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005786 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5788 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005789 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005791 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005793 msg_puts(" ");
5794 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 if (!(p->flags & P_BOOL))
5796 {
5797 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005798 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 option_value2string(p, opt_flags);
5800 msg_outtrans(NameBuff);
5801 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005802
5803 silent_mode = save_silent;
5804 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805}
5806
5807/*
5808 * Write modified options as ":set" commands to a file.
5809 *
5810 * There are three values for "opt_flags":
5811 * OPT_GLOBAL: Write global option values and fresh values of
5812 * buffer-local options (used for start of a session
5813 * file).
5814 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5815 * curwin (used for a vimrc file).
5816 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5817 * and local values for window-local options of
5818 * curwin. Local values are also written when at the
5819 * default value, because a modeline or autocommand
5820 * may have set them when doing ":edit file" and the
5821 * user has set them back at the default or fresh
5822 * value.
5823 * When "local_only" is TRUE, don't write fresh
5824 * values, only local values (for ":mkview").
5825 * (fresh value = value used for a new buffer or window for a local option).
5826 *
5827 * Return FAIL on error, OK otherwise.
5828 */
5829 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005830makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831{
5832 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005833 char_u *varp; // currently used value
5834 char_u *varp_fresh; // local value
5835 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 char *cmd;
5837 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005838 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839
5840 /*
5841 * The options that don't have a default (terminal name, columns, lines)
5842 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005843 * Do the loop over "options[]" twice: once for options with the
5844 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005846 for (pri = 1; pri >= 0; --pri)
5847 {
5848 for (p = &options[0]; !istermoption(p); p++)
5849 if (!(p->flags & P_NO_MKRC)
5850 && !istermoption(p)
5851 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005853 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5855 continue;
5856
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005857 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5858 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5860 continue;
5861
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005862 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005864 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 continue;
5866
Bram Moolenaard23b7142021-04-17 21:04:34 +02005867 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5868 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005869 continue;
5870
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 round = 2;
5872 if (p->indir != PV_NONE)
5873 {
5874 if (p->var == VAR_WIN)
5875 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005876 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 if (!(opt_flags & OPT_LOCAL))
5878 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005879 // When fresh value of window-local option is not at the
5880 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5882 {
5883 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005884 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 {
5886 round = 1;
5887 varp_local = varp;
5888 varp = varp_fresh;
5889 }
5890 }
5891 }
5892 }
5893
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005894 // Round 1: fresh value for window-local options.
5895 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 for ( ; round <= 2; varp = varp_local, ++round)
5897 {
5898 if (round == 1 || (opt_flags & OPT_GLOBAL))
5899 cmd = "set";
5900 else
5901 cmd = "setlocal";
5902
5903 if (p->flags & P_BOOL)
5904 {
5905 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5906 return FAIL;
5907 }
5908 else if (p->flags & P_NUM)
5909 {
5910 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5911 return FAIL;
5912 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005913 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005915 int do_endif = FALSE;
5916
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005917 // Don't set 'syntax' and 'filetype' again if the value is
5918 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005919 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005920#if defined(FEAT_SYN_HL)
5921 p->indir == PV_SYN ||
5922#endif
5923 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 {
5925 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5926 *(char_u **)(varp)) < 0
5927 || put_eol(fd) < 0)
5928 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005929 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 }
5931 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005932 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005934 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 {
5936 if (put_line(fd, "endif") == FAIL)
5937 return FAIL;
5938 }
5939 }
5940 }
5941 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 return OK;
5944}
5945
5946#if defined(FEAT_FOLDING) || defined(PROTO)
5947/*
5948 * Generate set commands for the local fold options only. Used when
5949 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5950 */
5951 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005952makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005954 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005956 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 == FAIL
5958# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005959 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005961 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 == FAIL
5963 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5964 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5965 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5966 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5967 )
5968 return FAIL;
5969
5970 return OK;
5971}
5972#endif
5973
5974 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005975put_setstring(
5976 FILE *fd,
5977 char *cmd,
5978 char *name,
5979 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005980 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981{
5982 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005983 char_u *buf = NULL;
5984 char_u *part = NULL;
5985 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986
5987 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5988 return FAIL;
5989 if (*valuep != NULL)
5990 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005991 // Output 'pastetoggle' as key names. For other
5992 // options some characters have to be escaped with
5993 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 if (valuep == &p_pt)
5995 {
5996 s = *valuep;
5997 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005998 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 return FAIL;
6000 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006001 // expand the option value, replace $HOME by ~
6002 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006004 int size = (int)STRLEN(*valuep) + 1;
6005
6006 // replace home directory in the whole option value into "buf"
6007 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02006008 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006009 goto fail;
6010 home_replace(NULL, *valuep, buf, size, FALSE);
6011
6012 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01006013 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006014 // can be expanded when read back.
6015 if (size >= MAXPATHL && (flags & P_COMMA) != 0
6016 && vim_strchr(*valuep, ',') != NULL)
6017 {
6018 part = alloc(size);
6019 if (part == NULL)
6020 goto fail;
6021
6022 // write line break to clear the option, e.g. ':set rtp='
6023 if (put_eol(fd) == FAIL)
6024 goto fail;
6025
6026 p = buf;
6027 while (*p != NUL)
6028 {
6029 // for each comma separated option part, append value to
6030 // the option, :set rtp+=value
6031 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
6032 goto fail;
6033 (void)copy_option_part(&p, part, size, ",");
6034 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
6035 goto fail;
6036 }
6037 vim_free(buf);
6038 vim_free(part);
6039 return OK;
6040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02006042 {
6043 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02006045 }
6046 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 }
6048 else if (put_escstr(fd, *valuep, 2) == FAIL)
6049 return FAIL;
6050 }
6051 if (put_eol(fd) < 0)
6052 return FAIL;
6053 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006054fail:
6055 vim_free(buf);
6056 vim_free(part);
6057 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058}
6059
6060 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006061put_setnum(
6062 FILE *fd,
6063 char *cmd,
6064 char *name,
6065 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066{
6067 long wc;
6068
6069 if (fprintf(fd, "%s %s=", cmd, name) < 0)
6070 return FAIL;
6071 if (wc_use_keyname((char_u *)valuep, &wc))
6072 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006073 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
6075 return FAIL;
6076 }
6077 else if (fprintf(fd, "%ld", *valuep) < 0)
6078 return FAIL;
6079 if (put_eol(fd) < 0)
6080 return FAIL;
6081 return OK;
6082}
6083
6084 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006085put_setbool(
6086 FILE *fd,
6087 char *cmd,
6088 char *name,
6089 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006091 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00006092 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
6094 || put_eol(fd) < 0)
6095 return FAIL;
6096 return OK;
6097}
6098
6099/*
6100 * Clear all the terminal options.
6101 * If the option has been allocated, free the memory.
6102 * Terminal options are never hidden or indirect.
6103 */
6104 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006105clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006107 // Reset a few things before clearing the old options. This may cause
6108 // outputting a few things that the terminal doesn't understand, but the
6109 // screen will be cleared later, so this is OK.
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006110 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006111 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006113 // When starting the GUI close the display opened for the clipboard.
6114 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 if (gui.starting)
6116 clear_xterm_clip();
6117#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006118 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006120 free_termoptions();
6121}
6122
6123 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006124free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006125{
6126 struct vimoption *p;
6127
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006128 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 if (istermoption(p))
6130 {
6131 if (p->flags & P_ALLOCED)
6132 free_string_option(*(char_u **)(p->var));
6133 if (p->flags & P_DEF_ALLOCED)
6134 free_string_option(p->def_val[VI_DEFAULT]);
6135 *(char_u **)(p->var) = empty_option;
6136 p->def_val[VI_DEFAULT] = empty_option;
6137 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006138#ifdef FEAT_EVAL
6139 // remember where the option was cleared
6140 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
6141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 }
6143 clear_termcodes();
6144}
6145
6146/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00006147 * Free the string for one term option, if it was allocated.
6148 * Set the string to empty_option and clear allocated flag.
6149 * "var" points to the option value.
6150 */
6151 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006152free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00006153{
6154 struct vimoption *p;
6155
6156 for (p = &options[0]; p->fullname != NULL; p++)
6157 if (p->var == var)
6158 {
6159 if (p->flags & P_ALLOCED)
6160 free_string_option(*(char_u **)(p->var));
6161 *(char_u **)(p->var) = empty_option;
6162 p->flags &= ~P_ALLOCED;
6163 break;
6164 }
6165}
6166
6167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 * Set the terminal option defaults to the current value.
6169 * Used after setting the terminal name.
6170 */
6171 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006172set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173{
6174 struct vimoption *p;
6175
6176 for (p = &options[0]; p->fullname != NULL; p++)
6177 {
6178 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
6179 {
6180 if (p->flags & P_DEF_ALLOCED)
6181 {
6182 free_string_option(p->def_val[VI_DEFAULT]);
6183 p->flags &= ~P_DEF_ALLOCED;
6184 }
6185 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
6186 if (p->flags & P_ALLOCED)
6187 {
6188 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006189 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 }
6191 }
6192 }
6193}
6194
6195/*
6196 * return TRUE if 'p' starts with 't_'
6197 */
6198 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006199istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200{
6201 return (p->fullname[0] == 't' && p->fullname[1] == '_');
6202}
6203
Bram Moolenaardac13472019-09-16 21:06:21 +02006204/*
6205 * Returns TRUE if the option at 'opt_idx' starts with 't_'
6206 */
6207 int
6208istermoption_idx(int opt_idx)
6209{
6210 return istermoption(&options[opt_idx]);
6211}
6212
Bram Moolenaar113e1072019-01-20 15:30:40 +01006213#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006215 * Unset local option value, similar to ":set opt<".
6216 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006217 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006218unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006219{
6220 struct vimoption *p;
6221 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006222 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006223
6224 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02006225 if (opt_idx < 0)
6226 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006227 p = &(options[opt_idx]);
6228
6229 switch ((int)p->indir)
6230 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006231 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006232 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006233 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006234 break;
6235 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006236 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006237 break;
6238 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006239 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006240 break;
6241 case PV_AR:
6242 buf->b_p_ar = -1;
6243 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006244 case PV_BKC:
6245 clear_string_option(&buf->b_p_bkc);
6246 buf->b_bkc_flags = 0;
6247 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006248 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006249 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006250 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006251 case PV_TC:
6252 clear_string_option(&buf->b_p_tc);
6253 buf->b_tc_flags = 0;
6254 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006255 case PV_SISO:
6256 curwin->w_p_siso = -1;
6257 break;
6258 case PV_SO:
6259 curwin->w_p_so = -1;
6260 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006261# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006262 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006263 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006264 break;
6265 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006266 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006267 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006268# endif
zeertzjq529b9ad2024-06-05 20:27:06 +02006269 case PV_COT:
6270 clear_string_option(&buf->b_p_cot);
6271 buf->b_cot_flags = 0;
6272 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006273 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006274 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006275 break;
6276 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006277 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006278 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006279# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006280 case PV_TSRFU:
6281 clear_string_option(&buf->b_p_tsrfu);
6282 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006283# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006284 case PV_FP:
6285 clear_string_option(&buf->b_p_fp);
6286 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006287# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006288 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006289 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006290 break;
6291 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006292 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006293 break;
6294 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006295 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006296 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006297# endif
6298# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006299 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006300 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006301 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006302# endif
6303# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006304 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006305 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006306 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006307# endif
6308# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006309 case PV_SBR:
6310 clear_string_option(&((win_T *)from)->w_p_sbr);
6311 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006312# endif
6313# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006314 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006315 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006316 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006317# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006318 case PV_UL:
6319 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6320 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006321 case PV_LW:
6322 clear_string_option(&buf->b_p_lw);
6323 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006324 case PV_MENC:
6325 clear_string_option(&buf->b_p_menc);
6326 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006327 case PV_LCS:
6328 clear_string_option(&((win_T *)from)->w_p_lcs);
zeertzjq6a8d2e12024-01-17 20:54:49 +01006329 set_listchars_option((win_T *)from, ((win_T *)from)->w_p_lcs, TRUE,
6330 NULL, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006331 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006332 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006333 case PV_FCS:
6334 clear_string_option(&((win_T *)from)->w_p_fcs);
zeertzjq6a8d2e12024-01-17 20:54:49 +01006335 set_fillchars_option((win_T *)from, ((win_T *)from)->w_p_fcs, TRUE,
6336 NULL, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006337 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006338 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006339 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006340 clear_string_option(&((win_T *)from)->w_p_ve);
6341 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006342 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006343 }
6344}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006345#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006346
6347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006349 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 */
6351 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006352get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006354 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 {
6356 if (p->var == VAR_WIN)
6357 return (char_u *)GLOBAL_WO(get_varp(p));
6358 return p->var;
6359 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006360 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 {
6362 switch ((int)p->indir)
6363 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006364 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006366 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
6367 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6368 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006370 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6371 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6372 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006373 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006374 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006375 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006376 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6377 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006379 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6380 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381#endif
zeertzjq529b9ad2024-06-05 20:27:06 +02006382 case PV_COT: return (char_u *)&(curbuf->b_p_cot);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006383 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6384 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006385#ifdef FEAT_COMPL_FUNC
6386 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6387#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006388#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6389 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6390#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006391#if defined(FEAT_CRYPT)
6392 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6393#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006394#ifdef FEAT_LINEBREAK
6395 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6396#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006397#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006398 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006399#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006400 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006401 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006402 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006403 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006404 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006405 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006406 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006407
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006409 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 }
6411 return get_varp(p);
6412}
6413
6414/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006415 * Get pointer to option variable at 'opt_idx', depending on local or global
6416 * scope.
6417 */
6418 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006419get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006420{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006421 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006422}
6423
6424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 * Get pointer to option variable.
6426 */
6427 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006428get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006430 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 if (p->var == NULL)
6432 return NULL;
6433
6434 switch ((int)p->indir)
6435 {
6436 case PV_NONE: return p->var;
6437
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006438 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006439 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006441 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006443 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006445 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006447 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006449 case PV_TC: return *curbuf->b_p_tc != NUL
6450 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006451 case PV_BKC: return *curbuf->b_p_bkc != NUL
6452 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006453 case PV_SISO: return curwin->w_p_siso >= 0
6454 ? (char_u *)&(curwin->w_p_siso) : p->var;
6455 case PV_SO: return curwin->w_p_so >= 0
6456 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006458 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006460 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6462#endif
zeertzjq529b9ad2024-06-05 20:27:06 +02006463 case PV_COT: return *curbuf->b_p_cot != NUL
6464 ? (char_u *)&(curbuf->b_p_cot) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006465 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006467 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006469#ifdef FEAT_COMPL_FUNC
6470 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6471 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6472#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006473 case PV_FP: return *curbuf->b_p_fp != NUL
6474 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006476 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006478 case PV_GP: return *curbuf->b_p_gp != NUL
6479 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6480 case PV_MP: return *curbuf->b_p_mp != NUL
6481 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006483#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6484 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6485 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6486#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006487#if defined(FEAT_CRYPT)
6488 case PV_CM: return *curbuf->b_p_cm != NUL
6489 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6490#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006491#ifdef FEAT_LINEBREAK
6492 case PV_SBR: return *curwin->w_p_sbr != NUL
6493 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6494#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006495#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006496 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006497 ? (char_u *)&(curwin->w_p_stl) : p->var;
6498#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006499 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6500 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006501 case PV_LW: return *curbuf->b_p_lw != NUL
6502 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006503 case PV_MENC: return *curbuf->b_p_menc != NUL
6504 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505#ifdef FEAT_ARABIC
6506 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6507#endif
6508 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006509 case PV_LCS: return *curwin->w_p_lcs != NUL
6510 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006511 case PV_FCS: return *curwin->w_p_fcs != NUL
6512 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006513 case PV_VE: return *curwin->w_p_ve != NUL
6514 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006515#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006516 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6517#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006518#ifdef FEAT_SYN_HL
6519 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6520 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006521 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006522 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524#ifdef FEAT_DIFF
6525 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6526#endif
6527#ifdef FEAT_FOLDING
6528 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6529 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6530 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6531 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6532 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6533 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6534 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6535# ifdef FEAT_EVAL
6536 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6537 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6538# endif
6539 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6540#endif
6541 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006542 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006543#ifdef FEAT_LINEBREAK
6544 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6545#endif
Colin Kennedy21570352024-03-03 16:16:47 +01006546 case PV_WFB: return (char_u *)&(curwin->w_p_wfb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006548 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006549#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6551#endif
6552#ifdef FEAT_RIGHTLEFT
6553 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6554 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6555#endif
6556 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006557 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6559#ifdef FEAT_LINEBREAK
6560 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006561 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6562 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006564 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006566 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006567#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006568 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6569 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6570#endif
6571#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006572 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6573 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6574 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576
6577 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6578 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6581 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6583 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6585 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6586 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006587 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590#ifdef FEAT_FOLDING
6591 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006594#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006595 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006597#ifdef FEAT_COMPL_FUNC
6598 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006599 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006600#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006601#ifdef FEAT_EVAL
6602 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6603#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006604 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006606 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006612 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6614 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6615 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6616 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6617#ifdef FEAT_FIND_ID
6618# ifdef FEAT_EVAL
6619 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6620# endif
6621#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006622#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6624 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6625#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006626#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006627 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629#ifdef FEAT_CRYPT
6630 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006633 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6635 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6636 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6637 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6638 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006640 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6647#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006648 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006649 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6650#endif
6651#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006652 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6653 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6654 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006655 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656#endif
6657 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6658 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6659 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6660 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006661#ifdef FEAT_PERSISTENT_UNDO
6662 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6665#ifdef FEAT_KEYMAP
6666 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6667#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006668#ifdef FEAT_SIGNS
6669 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6670#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006671#ifdef FEAT_VARTABS
6672 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6673 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6674#endif
RestorerZ68ebcee2023-05-31 17:12:14 +01006675 default: iemsg(e_get_varp_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006677 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 return (char_u *)&(curbuf->b_p_wm);
6679}
6680
6681/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006682 * Return a pointer to the variable for option at 'opt_idx'
6683 */
6684 char_u *
6685get_option_var(int opt_idx)
6686{
6687 return options[opt_idx].var;
6688}
6689
Dominique Pelle748b3082022-01-08 12:41:16 +00006690#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006691/*
6692 * Return the full name of the option at 'opt_idx'
6693 */
6694 char_u *
6695get_option_fullname(int opt_idx)
6696{
6697 return (char_u *)options[opt_idx].fullname;
6698}
Dominique Pelle748b3082022-01-08 12:41:16 +00006699#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006700
6701/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00006702 * Return the did_set callback function for the option at 'opt_idx'
6703 */
6704 opt_did_set_cb_T
6705get_option_did_set_cb(int opt_idx)
6706{
6707 return options[opt_idx].opt_did_set_cb;
6708}
6709
6710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 * Get the value of 'equalprg', either the buffer-local one or the global one.
6712 */
6713 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006714get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715{
6716 if (*curbuf->b_p_ep == NUL)
6717 return p_ep;
6718 return curbuf->b_p_ep;
6719}
6720
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721/*
6722 * Copy options from one window to another.
6723 * Used when splitting a window.
6724 */
6725 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006726win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727{
6728 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6729 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006730 after_copy_winopt(wp_to);
6731}
6732
6733/*
6734 * After copying window options: update variables depending on options.
6735 */
6736 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006737after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006738{
6739#ifdef FEAT_LINEBREAK
6740 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006741#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006742#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006743 fill_culopt_flags(NULL, wp);
6744 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006745#endif
zeertzjq6a8d2e12024-01-17 20:54:49 +01006746 set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
6747 set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006748}
6749
6750 static char_u *
6751copy_option_val(char_u *val)
6752{
6753 if (val == empty_option)
6754 return empty_option; // no need to allocate memory
6755 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757
6758/*
6759 * Copy the options from one winopt_T to another.
6760 * Doesn't free the old option values in "to", use clear_winopt() for that.
6761 * The 'scroll' option is not copied, because it depends on the window height.
6762 * The 'previewwindow' option is reset, there can be only one preview window.
6763 */
6764 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006765copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766{
6767#ifdef FEAT_ARABIC
6768 to->wo_arab = from->wo_arab;
6769#endif
6770 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006771 to->wo_lcs = copy_option_val(from->wo_lcs);
6772 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006774 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006775 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006776 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006777#ifdef FEAT_LINEBREAK
6778 to->wo_nuw = from->wo_nuw;
6779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780#ifdef FEAT_RIGHTLEFT
6781 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006782 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006784#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006785 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006786#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006787#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006788 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006791#ifdef FEAT_DIFF
6792 to->wo_wrap_save = from->wo_wrap_save;
6793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794#ifdef FEAT_LINEBREAK
6795 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006796 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006797 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006799 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006801 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006802 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006803 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006804 to->wo_crb_save = from->wo_crb_save;
Christian Brabandt4a8eb6e2023-08-13 19:43:42 +02006805 to->wo_siso = from->wo_siso;
6806 to->wo_so = from->wo_so;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006807#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006808 to->wo_spell = from->wo_spell;
6809#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006810#ifdef FEAT_SYN_HL
6811 to->wo_cuc = from->wo_cuc;
6812 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006813 to->wo_culopt = copy_option_val(from->wo_culopt);
6814 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816#ifdef FEAT_DIFF
6817 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006818 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006820#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006821 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006822 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006823#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006824#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006825 to->wo_twk = copy_option_val(from->wo_twk);
6826 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828#ifdef FEAT_FOLDING
6829 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006830 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006832 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006833 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 to->wo_fml = from->wo_fml;
6835 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006836 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006837 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006838 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006839 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 to->wo_fdn = from->wo_fdn;
6841# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006842 to->wo_fde = copy_option_val(from->wo_fde);
6843 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006845 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006847#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006848 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006849#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006850
6851#ifdef FEAT_EVAL
6852 // Copy the script context so that we know where the value was last set.
6853 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6854 sizeof(to->wo_script_ctx));
6855#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006856 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857}
6858
6859/*
6860 * Check string options in a window for a NULL value.
6861 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006862 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006863check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864{
6865 check_winopt(&win->w_onebuf_opt);
6866 check_winopt(&win->w_allbuf_opt);
6867}
6868
6869/*
6870 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6871 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006872 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006873check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874{
6875#ifdef FEAT_FOLDING
6876 check_string_option(&wop->wo_fdi);
6877 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006878 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879# ifdef FEAT_EVAL
6880 check_string_option(&wop->wo_fde);
6881 check_string_option(&wop->wo_fdt);
6882# endif
6883 check_string_option(&wop->wo_fmr);
6884#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006885#ifdef FEAT_SIGNS
6886 check_string_option(&wop->wo_scl);
6887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888#ifdef FEAT_RIGHTLEFT
6889 check_string_option(&wop->wo_rlc);
6890#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006891#ifdef FEAT_LINEBREAK
6892 check_string_option(&wop->wo_sbr);
6893#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006894#ifdef FEAT_STL_OPT
6895 check_string_option(&wop->wo_stl);
6896#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006897#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006898 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006899 check_string_option(&wop->wo_cc);
6900#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006901#ifdef FEAT_CONCEAL
6902 check_string_option(&wop->wo_cocu);
6903#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006904#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006905 check_string_option(&wop->wo_twk);
6906 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006907#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006908#ifdef FEAT_LINEBREAK
6909 check_string_option(&wop->wo_briopt);
6910#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006911 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006912 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006913 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006914 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915}
6916
6917/*
6918 * Free the allocated memory inside a winopt_T.
6919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006921clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922{
6923#ifdef FEAT_FOLDING
6924 clear_string_option(&wop->wo_fdi);
6925 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006926 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927# ifdef FEAT_EVAL
6928 clear_string_option(&wop->wo_fde);
6929 clear_string_option(&wop->wo_fdt);
6930# endif
6931 clear_string_option(&wop->wo_fmr);
6932#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006933#ifdef FEAT_SIGNS
6934 clear_string_option(&wop->wo_scl);
6935#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006936#ifdef FEAT_LINEBREAK
6937 clear_string_option(&wop->wo_briopt);
6938#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006939 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940#ifdef FEAT_RIGHTLEFT
6941 clear_string_option(&wop->wo_rlc);
6942#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006943#ifdef FEAT_LINEBREAK
6944 clear_string_option(&wop->wo_sbr);
6945#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006946#ifdef FEAT_STL_OPT
6947 clear_string_option(&wop->wo_stl);
6948#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006949#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006950 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006951 clear_string_option(&wop->wo_cc);
6952#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006953#ifdef FEAT_CONCEAL
6954 clear_string_option(&wop->wo_cocu);
6955#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006956#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006957 clear_string_option(&wop->wo_twk);
6958 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006959#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006960 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006961 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006962 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963}
6964
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006965#ifdef FEAT_EVAL
6966// Index into the options table for a buffer-local option enum.
6967static int buf_opt_idx[BV_COUNT];
6968# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6969
6970/*
6971 * Initialize buf_opt_idx[] if not done already.
6972 */
6973 static void
6974init_buf_opt_idx(void)
6975{
6976 static int did_init_buf_opt_idx = FALSE;
6977 int i;
6978
6979 if (did_init_buf_opt_idx)
6980 return;
6981 did_init_buf_opt_idx = TRUE;
6982 for (i = 0; !istermoption_idx(i); i++)
6983 if (options[i].indir & PV_BUF)
6984 buf_opt_idx[options[i].indir & PV_MASK] = i;
6985}
6986#else
6987# define COPY_OPT_SCTX(buf, bv)
6988#endif
6989
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990/*
6991 * Copy global option values to local options for one buffer.
6992 * Used when creating a new buffer and sometimes when entering a buffer.
6993 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006994 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6996 * appropriate.
6997 * BCO_NOHELP Don't copy the values to a help buffer.
6998 */
6999 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007000buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001{
7002 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007003 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 int dont_do_help;
7005 int did_isk = FALSE;
7006
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007007 // Skip this when the option defaults have not been set yet. Happens when
7008 // main() allocates the first buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 if (p_cpo != NULL)
7010 {
7011 /*
7012 * Always copy when entering and 'cpo' contains 'S'.
7013 * Don't copy when already initialized.
7014 * Don't copy when 'cpo' contains 's' and not entering.
7015 * 'S' BCO_ENTER initialized 's' should_copy
7016 * yes yes X X TRUE
7017 * yes no yes X FALSE
7018 * no X yes X FALSE
7019 * X no no yes FALSE
7020 * X no no no TRUE
7021 * no yes no X TRUE
7022 */
7023 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
7024 && (buf->b_p_initialized
7025 || (!(flags & BCO_ENTER)
7026 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
7027 should_copy = FALSE;
7028
7029 if (should_copy || (flags & BCO_ALWAYS))
7030 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007031#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02007032 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007033 init_buf_opt_idx();
7034#endif
7035 // Don't copy the options specific to a help buffer when
7036 // BCO_NOHELP is given or the options were initialized already
7037 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
7039 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007040 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 {
7042 save_p_isk = buf->b_p_isk;
7043 buf->b_p_isk = NULL;
7044 }
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007045 // Always free the allocated strings. If not already initialized,
7046 // reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 if (!buf->b_p_initialized)
7048 {
7049 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007050 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02007053 switch (*p_ffs)
7054 {
7055 case 'm':
7056 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
7057 case 'd':
7058 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
7059 case 'u':
7060 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
7061 default:
7062 buf->b_p_ff = vim_strsave(p_ff);
7063 }
7064 if (buf->b_p_ff != NULL)
7065 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 buf->b_p_bh = empty_option;
7067 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 }
7069 else
7070 free_buf_options(buf, FALSE);
7071
7072 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007073 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 buf->b_p_ai_nopaste = p_ai_nopaste;
7075 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007076 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007078 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 buf->b_p_tw_nopaste = p_tw_nopaste;
7080 buf->b_p_tw_nobin = p_tw_nobin;
7081 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007082 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 buf->b_p_wm_nopaste = p_wm_nopaste;
7084 buf->b_p_wm_nobin = p_wm_nobin;
7085 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007086 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00007087 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007088 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02007089 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007090 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007092 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007094 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007096 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 buf->b_p_ml_nobin = p_ml_nobin;
7098 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007099 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02007100 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007101 buf->b_p_swf = FALSE;
7102 else
7103 {
7104 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00007105 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007108 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02007109#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02007110 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007111 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007113#ifdef FEAT_COMPL_FUNC
7114 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007115 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007116 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00007117 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007118 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007119 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007120#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02007121#ifdef FEAT_EVAL
7122 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007123 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007124 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02007125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007127 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007129#ifdef FEAT_VARTABS
7130 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007131 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007132 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007133 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007134 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007135 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007136 buf->b_p_vsts_nopaste = p_vsts_nopaste
7137 ? vim_strsave(p_vsts_nopaste) : NULL;
7138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007140 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007142 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143#ifdef FEAT_FOLDING
7144 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007145 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146#endif
7147 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007148 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007149 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007150 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02007151 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
7152 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007154 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007156 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007158 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007160 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007161
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007163 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007165 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007167 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01007168 buf->b_p_cinsd = vim_strsave(p_cinsd);
7169 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01007170 buf->b_p_lop = vim_strsave(p_lop);
7171 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007172
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007173 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007176 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007178 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007180 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007182 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00007184 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007185 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01007186 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007187#endif
7188#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02007189 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007190 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007191 (void)compile_cap_prog(&buf->b_s);
7192 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007193 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007194 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007195 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02007196 buf->b_s.b_p_spo = vim_strsave(p_spo);
7197 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007199#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007201 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007203 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01007205 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007206#if defined(FEAT_EVAL)
7207 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007208 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210#ifdef FEAT_CRYPT
7211 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007212 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007215 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216#ifdef FEAT_KEYMAP
7217 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007218 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 buf->b_kmap_state |= KEYMAP_INIT;
7220#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007221#ifdef FEAT_TERMINAL
7222 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007223 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007224#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007225 // This isn't really an option, but copying the langmap and IME
7226 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007228 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007230 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007232 // options that are normally global but also have a local value
7233 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01007235 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007236 buf->b_p_bkc = empty_option;
7237 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238#ifdef FEAT_QUICKFIX
7239 buf->b_p_gp = empty_option;
7240 buf->b_p_mp = empty_option;
7241 buf->b_p_efm = empty_option;
7242#endif
7243 buf->b_p_ep = empty_option;
7244 buf->b_p_kp = empty_option;
7245 buf->b_p_path = empty_option;
7246 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007247 buf->b_p_tc = empty_option;
7248 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249#ifdef FEAT_FIND_ID
7250 buf->b_p_def = empty_option;
7251 buf->b_p_inc = empty_option;
7252# ifdef FEAT_EVAL
7253 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007254 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255# endif
7256#endif
zeertzjq529b9ad2024-06-05 20:27:06 +02007257 buf->b_p_cot = empty_option;
7258 buf->b_cot_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 buf->b_p_dict = empty_option;
7260 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01007261#ifdef FEAT_COMPL_FUNC
7262 buf->b_p_tsrfu = empty_option;
7263#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007264 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007265 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007266#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7267 buf->b_p_bexpr = empty_option;
7268#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007269#if defined(FEAT_CRYPT)
7270 buf->b_p_cm = empty_option;
7271#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007272#ifdef FEAT_PERSISTENT_UNDO
7273 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007274 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007275#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007276 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007277 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007279 // Don't copy the options set by ex_help(), use the saved values,
7280 // when going from a help buffer to a non-help buffer.
7281 // Don't touch these at all when BCO_NOHELP is used and going from
7282 // or to a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007284 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007286#ifdef FEAT_VARTABS
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007287 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007288 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007289 else
7290 buf->b_p_vts_array = NULL;
7291#endif
7292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 else
7294 {
7295 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007296 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 did_isk = TRUE;
7298 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00007299 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007300#ifdef FEAT_VARTABS
7301 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007302 COPY_OPT_SCTX(buf, BV_VTS);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007303 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007304 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007305 else
7306 buf->b_p_vts_array = NULL;
7307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 if (buf->b_p_bt[0] == 'h')
7310 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007312 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 }
7314 }
7315
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007316 // When the options should be copied (ignoring BCO_ALWAYS), set the
7317 // flag that indicates that the options have been initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 if (should_copy)
7319 buf->b_p_initialized = TRUE;
7320 }
7321
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007322 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 if (did_isk)
7324 (void)buf_init_chartab(buf, FALSE);
7325}
7326
7327/*
7328 * Reset the 'modifiable' option and its default value.
7329 */
7330 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007331reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332{
7333 int opt_idx;
7334
7335 curbuf->b_p_ma = FALSE;
7336 p_ma = FALSE;
7337 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007338 if (opt_idx >= 0)
7339 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340}
7341
7342/*
7343 * Set the global value for 'iminsert' to the local value.
7344 */
7345 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007346set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347{
7348 p_iminsert = curbuf->b_p_iminsert;
7349}
7350
7351/*
7352 * Set the global value for 'imsearch' to the local value.
7353 */
7354 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007355set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356{
7357 p_imsearch = curbuf->b_p_imsearch;
7358}
7359
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360static int expand_option_idx = -1;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007361static int expand_option_start_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7363static int expand_option_flags = 0;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007364static int expand_option_append = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365
7366 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007367set_context_in_set_cmd(
7368 expand_T *xp,
7369 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007370 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371{
7372 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007373 long_u flags = 0; // init for GCC
7374 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 char_u *p;
7376 char_u *s;
7377 int is_term_option = FALSE;
7378 int key;
John Marriott95dacbb2024-09-10 21:25:14 +02007379 char_u *argend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380
7381 expand_option_flags = opt_flags;
7382
7383 xp->xp_context = EXPAND_SETTINGS;
7384 if (*arg == NUL)
7385 {
7386 xp->xp_pattern = arg;
7387 return;
7388 }
John Marriott95dacbb2024-09-10 21:25:14 +02007389 argend = arg + STRLEN(arg);
7390 p = argend - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 if (*p == ' ' && *(p - 1) != '\\')
7392 {
7393 xp->xp_pattern = p + 1;
7394 return;
7395 }
7396 while (p > arg)
7397 {
7398 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007399 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 if (*p == ' ' || *p == ',')
7401 {
7402 while (s > arg && *(s - 1) == '\\')
7403 --s;
7404 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007405 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 if (*p == ' ' && ((p - s) & 1) == 0)
7407 {
7408 ++p;
7409 break;
7410 }
7411 --p;
7412 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007413 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 {
7415 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007416 xp->xp_prefix = XP_PREFIX_NO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 p += 2;
7418 }
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007419 else if (STRNCMP(p, "inv", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 {
7421 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007422 xp->xp_prefix = XP_PREFIX_INV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 p += 3;
7424 }
7425 xp->xp_pattern = arg = p;
7426 if (*arg == '<')
7427 {
7428 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007429 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 return;
7431 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007432 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 {
7434 xp->xp_context = EXPAND_NOTHING;
7435 return;
7436 }
7437 nextchar = *++p;
7438 is_term_option = TRUE;
7439 expand_option_name[2] = KEY2TERMCAP0(key);
7440 expand_option_name[3] = KEY2TERMCAP1(key);
7441 }
7442 else
7443 {
7444 if (p[0] == 't' && p[1] == '_')
7445 {
7446 p += 2;
7447 if (*p != NUL)
7448 ++p;
7449 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007450 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 nextchar = *++p;
7452 is_term_option = TRUE;
7453 expand_option_name[2] = p[-2];
7454 expand_option_name[3] = p[-1];
7455 }
7456 else
7457 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007458 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7460 p++;
7461 if (*p == NUL)
7462 return;
7463 nextchar = *p;
7464 *p = NUL;
7465 opt_idx = findoption(arg);
7466 *p = nextchar;
7467 if (opt_idx == -1 || options[opt_idx].var == NULL)
7468 {
7469 xp->xp_context = EXPAND_NOTHING;
7470 return;
7471 }
7472 flags = options[opt_idx].flags;
7473 if (flags & P_BOOL)
7474 {
7475 xp->xp_context = EXPAND_NOTHING;
7476 return;
7477 }
7478 }
7479 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007480 // handle "-=" and "+="
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007481 expand_option_append = FALSE;
7482 int expand_option_subtract = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7484 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007485 if (nextchar == '-')
7486 expand_option_subtract = TRUE;
7487 if (nextchar == '+' || nextchar == '^')
7488 expand_option_append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 ++p;
7490 nextchar = '=';
7491 }
7492 if ((nextchar != '=' && nextchar != ':')
7493 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7494 {
7495 xp->xp_context = EXPAND_UNSUCCESSFUL;
7496 return;
7497 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007498
7499 // Below are for handling expanding a specific option's value after the '='
7500 // or ':'
7501
7502 if (is_term_option)
7503 expand_option_idx = -1;
7504 else
7505 expand_option_idx = opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02007507 if (!is_term_option)
7508 {
7509 if (options[opt_idx].flags & P_NO_CMD_EXPAND)
7510 {
7511 xp->xp_context=EXPAND_UNSUCCESSFUL;
7512 return;
7513 }
7514 }
7515
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 xp->xp_pattern = p + 1;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007517 expand_option_start_col = (int)(p + 1 - xp->xp_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007519 // Certain options currently have special case handling to reuse the
7520 // expansion logic with other commands.
Doug Kearns6dfdff32023-08-27 18:48:51 +02007521#ifdef FEAT_SYN_HL
7522 if (options[opt_idx].var == (char_u *)&p_syn)
7523 {
7524 xp->xp_context = EXPAND_OWNSYNTAX;
7525 return;
7526 }
7527#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007528 if (options[opt_idx].var == (char_u *)&p_ft)
7529 {
7530 xp->xp_context = EXPAND_FILETYPE;
7531 return;
7532 }
Doug Kearns81642d92024-01-04 22:37:44 +01007533#ifdef FEAT_KEYMAP
7534 if (options[opt_idx].var == (char_u *)&p_keymap)
7535 {
7536 xp->xp_context = EXPAND_KEYMAP;
7537 return;
7538 }
7539#endif
Doug Kearns6dfdff32023-08-27 18:48:51 +02007540
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007541 // Now pick. If the option has a custom expander, use that. Otherwise, just
7542 // fill with the existing option value.
7543 if (expand_option_subtract)
7544 {
7545 xp->xp_context = EXPAND_SETTING_SUBTRACT;
7546 return;
7547 }
7548 else if (expand_option_idx >= 0 &&
7549 options[expand_option_idx].opt_expand_cb != NULL)
7550 {
7551 xp->xp_context = EXPAND_STRING_SETTING;
7552 }
7553 else if (*xp->xp_pattern == NUL)
7554 {
7555 xp->xp_context = EXPAND_OLD_SETTING;
7556 return;
7557 }
7558 else
7559 xp->xp_context = EXPAND_NOTHING;
7560
7561 if (is_term_option || (flags & P_NUM))
7562 return;
7563
7564 // Only string options below
7565
7566 // Options that have P_EXPAND are considered to all use file/dir expansion.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 if (flags & P_EXPAND)
7568 {
7569 p = options[opt_idx].var;
7570 if (p == (char_u *)&p_bdir
7571 || p == (char_u *)&p_dir
7572 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007573 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576#ifdef FEAT_SESSION
7577 || p == (char_u *)&p_vdir
7578#endif
7579 )
7580 {
7581 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007582 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 xp->xp_backslash = XP_BS_THREE;
7584 else
7585 xp->xp_backslash = XP_BS_ONE;
7586 }
7587 else
7588 {
7589 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007590 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 if (p == (char_u *)&p_tags)
7592 xp->xp_backslash = XP_BS_THREE;
7593 else
7594 xp->xp_backslash = XP_BS_ONE;
7595 }
Yee Cheng Chin54844852023-10-09 18:12:31 +02007596 if (flags & P_COMMA)
7597 xp->xp_backslash |= XP_BS_COMMA;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 }
7599
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007600 // For an option that is a list of file names, or comma/colon-separated
7601 // values, split it by the delimiter and find the start of the current
7602 // pattern, while accounting for backslash-escaped space/commas/colons.
7603 // Triple-backslashed escaped file names (e.g. 'path') can also be
7604 // delimited by space.
7605 if ((flags & P_EXPAND) || (flags & P_COMMA) || (flags & P_COLON))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 {
John Marriott95dacbb2024-09-10 21:25:14 +02007607 for (p = argend - 1; p >= xp->xp_pattern; --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007609 // count number of backslashes before ' ' or ',' or ':'
7610 if (*p == ' ' || *p == ',' ||
7611 (*p == ':' && (flags & P_COLON)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007613 s = p;
7614 while (s > xp->xp_pattern && *(s - 1) == '\\')
7615 --s;
Yee Cheng Chin54844852023-10-09 18:12:31 +02007616 if ((*p == ' ' && ((xp->xp_backslash & XP_BS_THREE) && (p - s) < 3))
7617#if defined(BACKSLASH_IN_FILENAME)
7618 || (*p == ',' && (flags & P_COMMA) && (p - s) < 1)
7619#else
7620 || (*p == ',' && (flags & P_COMMA) && (p - s) < 2)
7621#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007622 || (*p == ':' && (flags & P_COLON)))
7623 {
7624 xp->xp_pattern = p + 1;
7625 break;
7626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 }
7628 }
7629 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007630
7631 // An option that is a list of single-character flags should always start
7632 // at the end as we don't complete words.
7633 if (flags & P_FLAGLIST)
John Marriott95dacbb2024-09-10 21:25:14 +02007634 xp->xp_pattern = argend;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007635
7636 // Some options can either be using file/dir expansions, or custom value
7637 // expansion depending on what the user typed. Unfortunately we have to
7638 // manually handle it here to make sure we have the correct xp_context set.
7639#ifdef FEAT_SPELL
7640 if (options[opt_idx].var == (char_u *)&p_sps)
7641 {
7642 if (STRNCMP(xp->xp_pattern, "file:", 5) == 0)
7643 {
7644 xp->xp_pattern += 5;
7645 return;
7646 }
7647 else if (options[expand_option_idx].opt_expand_cb != NULL)
7648 {
7649 xp->xp_context = EXPAND_STRING_SETTING;
7650 }
7651 }
7652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653}
7654
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007655/*
7656 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7657 *
7658 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7659 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7660 *
7661 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7662 * regular expression 'regmatch', then stores the match in matches[idx] and
7663 * returns TRUE.
7664 *
7665 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7666 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7667 *
7668 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7669 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7670 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02007671 static int
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007672match_str(
7673 char_u *str,
7674 regmatch_T *regmatch,
7675 char_u **matches,
7676 int idx,
7677 int test_only,
7678 int fuzzy,
7679 char_u *fuzzystr,
7680 fuzmatch_str_T *fuzmatch)
7681{
7682 if (!fuzzy)
7683 {
7684 if (vim_regexec(regmatch, str, (colnr_T)0))
7685 {
7686 if (!test_only)
7687 matches[idx] = vim_strsave(str);
7688 return TRUE;
7689 }
7690 }
7691 else
7692 {
7693 int score;
7694
7695 score = fuzzy_match_str(str, fuzzystr);
7696 if (score != 0)
7697 {
7698 if (!test_only)
7699 {
7700 fuzmatch[idx].idx = idx;
7701 fuzmatch[idx].str = vim_strsave(str);
7702 fuzmatch[idx].score = score;
7703 }
7704
7705 return TRUE;
7706 }
7707 }
7708
7709 return FALSE;
7710}
7711
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007713ExpandSettings(
7714 expand_T *xp,
7715 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007716 char_u *fuzzystr,
7717 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007718 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007719 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007721 int num_normal = 0; // Nr of matching non-term-code settings
7722 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 int opt_idx;
7724 int match;
7725 int count = 0;
7726 char_u *str;
7727 int loop;
7728 int is_term_opt;
7729 char_u name_buf[MAX_KEY_NAME_LEN];
7730 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007731 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007732 int fuzzy;
7733 fuzmatch_str_T *fuzmatch = NULL;
7734
Christian Brabandtcb747892022-05-08 21:10:56 +01007735 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007737 // do this loop twice:
7738 // loop == 0: count the number of matching options
7739 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 for (loop = 0; loop <= 1; ++loop)
7741 {
7742 regmatch->rm_ic = ic;
7743 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7744 {
K.Takataeeec2542021-06-02 13:28:16 +02007745 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007746 {
7747 if (match_str((char_u *)names[match], regmatch, *matches,
7748 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 {
7750 if (loop == 0)
7751 num_normal++;
7752 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007753 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 }
7757 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7758 opt_idx++)
7759 {
7760 if (options[opt_idx].var == NULL)
7761 continue;
7762 if (xp->xp_context == EXPAND_BOOL_SETTINGS
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007763 && !(options[opt_idx].flags & P_BOOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007765 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 if (is_term_opt && num_normal > 0)
7767 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007768
7769 if (match_str(str, regmatch, *matches, count, (loop == 0),
7770 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 {
7772 if (loop == 0)
7773 {
7774 if (is_term_opt)
7775 num_term++;
7776 else
7777 num_normal++;
7778 }
7779 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007780 count++;
7781 }
7782 else if (!fuzzy && options[opt_idx].shortname != NULL
7783 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007784 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007785 {
7786 // Compare against the abbreviated option name (for regular
7787 // expression match). Fuzzy matching (previous if) already
7788 // matches against both the expanded and abbreviated names.
7789 if (loop == 0)
7790 {
7791 if (is_term_opt)
7792 num_term++;
7793 else
7794 num_normal++;
7795 }
7796 else
7797 (*matches)[count++] = vim_strsave(str);
7798 }
7799 else if (is_term_opt)
7800 {
7801 name_buf[0] = '<';
7802 name_buf[1] = 't';
7803 name_buf[2] = '_';
7804 name_buf[3] = str[2];
7805 name_buf[4] = str[3];
7806 name_buf[5] = '>';
7807 name_buf[6] = NUL;
7808
7809 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7810 fuzzy, fuzzystr, fuzmatch))
7811 {
7812 if (loop == 0)
7813 num_term++;
7814 else
7815 count++;
7816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 }
7818 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007819
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007820 // Check terminal key codes, these are not in the option table
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7822 {
7823 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7824 {
Keith Thompson184f71c2024-01-04 21:19:04 +01007825 if (!SAFE_isprint(str[0]) || !SAFE_isprint(str[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 continue;
7827
7828 name_buf[0] = 't';
7829 name_buf[1] = '_';
7830 name_buf[2] = str[0];
7831 name_buf[3] = str[1];
7832 name_buf[4] = NUL;
7833
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007834 if (match_str(name_buf, regmatch, *matches, count,
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007835 (loop == 0), fuzzy, fuzzystr, fuzmatch))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007836 {
7837 if (loop == 0)
7838 num_term++;
7839 else
7840 count++;
7841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 else
7843 {
7844 name_buf[0] = '<';
7845 name_buf[1] = 't';
7846 name_buf[2] = '_';
7847 name_buf[3] = str[0];
7848 name_buf[4] = str[1];
7849 name_buf[5] = '>';
7850 name_buf[6] = NUL;
7851
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007852 if (match_str(name_buf, regmatch, *matches, count,
7853 (loop == 0), fuzzy, fuzzystr,
7854 fuzmatch))
7855 {
7856 if (loop == 0)
7857 num_term++;
7858 else
7859 count++;
7860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 }
7862 }
7863
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007864 // Check special key names.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007865 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7867 {
7868 name_buf[0] = '<';
7869 STRCPY(name_buf + 1, str);
7870 STRCAT(name_buf, ">");
7871
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007872 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7873 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 {
7875 if (loop == 0)
7876 num_term++;
7877 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007878 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 }
7880 }
7881 }
7882 if (loop == 0)
7883 {
7884 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007885 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007887 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 else
7889 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007890 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007892 *matches = ALLOC_MULT(char_u *, *numMatches);
7893 if (*matches == NULL)
7894 {
7895 *matches = (char_u **)"";
7896 return FAIL;
7897 }
7898 }
7899 else
7900 {
7901 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7902 if (fuzmatch == NULL)
7903 {
7904 *matches = (char_u **)"";
7905 return FAIL;
7906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 }
7908 }
7909 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007910
7911 if (fuzzy &&
7912 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7913 return FAIL;
7914
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 return OK;
7916}
7917
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007918// Escape an option value that can be used on the command-line with :set.
7919// Caller needs to free the returned string, unless NULL is returned.
7920 static char_u*
7921escape_option_str_cmdline(char_u *var)
7922{
7923 char_u *buf;
7924
7925 // A backslash is required before some characters. This is the reverse of
7926 // what happens in do_set().
7927 buf = vim_strsave_escaped(var, escape_chars);
7928 if (buf == NULL)
7929 return NULL;
7930
7931#ifdef BACKSLASH_IN_FILENAME
7932 // For MS-Windows et al. we don't double backslashes at the start and
7933 // before a file name character.
7934 // The reverse is found at stropt_copy_value().
7935 for (var = buf; *var != NUL; MB_PTR_ADV(var))
7936 if (var[0] == '\\' && var[1] == '\\'
7937 && expand_option_idx >= 0
7938 && (options[expand_option_idx].flags & P_EXPAND)
7939 && vim_isfilec(var[2])
7940 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
7941 STRMOVE(var, var + 1);
7942#endif
7943 return buf;
7944}
7945
7946/*
7947 * Expansion handler for :set= when we just want to fill in with the existing value.
7948 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007950ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007952 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 char_u *buf;
7954
zeertzjqb0d45ec2023-01-25 15:04:22 +00007955 *numMatches = 0;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007956 *matches = ALLOC_MULT(char_u *, 1);
zeertzjqb0d45ec2023-01-25 15:04:22 +00007957 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 return FAIL;
7959
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007960 // For a terminal key code expand_option_idx is < 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 if (expand_option_idx < 0)
7962 {
7963 var = find_termcode(expand_option_name + 2);
7964 if (var == NULL)
7965 expand_option_idx = findoption(expand_option_name);
7966 }
7967
7968 if (expand_option_idx >= 0)
7969 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007970 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 option_value2string(&options[expand_option_idx], expand_option_flags);
7972 var = NameBuff;
7973 }
7974 else if (var == NULL)
7975 var = (char_u *)"";
7976
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007977 buf = escape_option_str_cmdline(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 if (buf == NULL)
7979 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007980 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 return FAIL;
7982 }
7983
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007984 (*matches)[0] = buf;
zeertzjqb0d45ec2023-01-25 15:04:22 +00007985 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 return OK;
7987}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988
7989/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007990 * Expansion handler for :set=/:set+= when the option has a custom expansion handler.
7991 */
7992 int
7993ExpandStringSetting(
7994 expand_T *xp,
7995 regmatch_T *regmatch,
7996 int *numMatches,
7997 char_u ***matches)
7998{
7999 char_u *var = NULL; // init for GCC
8000 char_u *buf;
8001
8002 if (expand_option_idx < 0 ||
8003 options[expand_option_idx].opt_expand_cb == NULL)
8004 {
8005 // Not supposed to reach this. This function is only for options with
8006 // custom expansion callbacks.
8007 return FAIL;
8008 }
8009
8010 optexpand_T args;
8011 args.oe_varp = get_varp_scope(&options[expand_option_idx], expand_option_flags);
8012 args.oe_append = expand_option_append;
8013 args.oe_regmatch = regmatch;
8014 args.oe_xp = xp;
8015 args.oe_set_arg = xp->xp_line + expand_option_start_col;
8016 args.oe_include_orig_val =
8017 !expand_option_append &&
8018 (*args.oe_set_arg == NUL);
8019
8020 // Retrieve the existing value, but escape it as a reverse of setting it.
8021 // We technically only need to do this when oe_append or
8022 // oe_include_orig_val is true.
8023 option_value2string(&options[expand_option_idx], expand_option_flags);
8024 var = NameBuff;
8025 buf = escape_option_str_cmdline(var);
8026 if (buf == NULL)
8027 return FAIL;
8028
8029 args.oe_opt_value = buf;
8030
8031 int num_ret = options[expand_option_idx].opt_expand_cb(&args, numMatches, matches);
8032
8033 vim_free(buf);
8034 return num_ret;
8035}
8036
8037/*
8038 * Expansion handler for :set-=
8039 */
8040 int
8041ExpandSettingSubtract(
8042 expand_T *xp,
8043 regmatch_T *regmatch,
8044 int *numMatches,
8045 char_u ***matches)
8046{
8047 if (expand_option_idx < 0)
8048 // term option
8049 return ExpandOldSetting(numMatches, matches);
8050
8051 char_u *option_val = *(char_u**)get_option_varp_scope(
8052 expand_option_idx, expand_option_flags);
8053
8054 long_u option_flags = options[expand_option_idx].flags;
8055
8056 if (option_flags & P_NUM)
8057 return ExpandOldSetting(numMatches, matches);
8058 else if (option_flags & P_COMMA)
8059 {
8060 // Split the option by comma, then present each option to the user if
8061 // it matches the pattern.
8062 // This condition needs to go first, because 'whichwrap' has both
8063 // P_COMMA and P_FLAGLIST.
8064 garray_T ga;
8065
8066 char_u *item;
8067 char_u *option_copy;
8068 char_u *next_val;
8069 char_u *comma;
8070
8071 if (*option_val == NUL)
8072 return FAIL;
8073
8074 // Make a copy as we need to inject null characters destructively.
8075 option_copy = vim_strsave(option_val);
8076 if (option_copy == NULL)
8077 return FAIL;
8078 next_val = option_copy;
8079
8080 ga_init2(&ga, sizeof(char_u *), 10);
8081
8082 do
8083 {
8084 item = next_val;
8085 comma = vim_strchr(next_val, ',');
8086 while (comma != NULL && comma != next_val && *(comma - 1) == '\\')
8087 {
8088 // "\," is interpreted as a literal comma rather than option
8089 // separator when reading options in copy_option_part(). Skip
8090 // it.
8091 comma = vim_strchr(comma + 1, ',');
8092 }
8093 if (comma != NULL)
8094 {
8095 *comma = NUL; // null-terminate this value, required by later functions
8096 next_val = comma + 1;
8097 }
8098 else
8099 next_val = NULL;
8100
8101 if (*item == NUL)
8102 // empty value, don't add to list
8103 continue;
8104
8105 if (!vim_regexec(regmatch, item, (colnr_T)0))
8106 continue;
8107
8108 char_u *buf = escape_option_str_cmdline(item);
8109 if (buf == NULL)
8110 {
8111 vim_free(option_copy);
8112 ga_clear_strings(&ga);
8113 return FAIL;
8114 }
8115 if (ga_add_string(&ga, buf) != OK)
8116 {
8117 vim_free(buf);
8118 break;
8119 }
8120 } while (next_val != NULL);
8121
8122 vim_free(option_copy);
8123
8124 *matches = ga.ga_data;
8125 *numMatches = ga.ga_len;
8126 return OK;
8127 }
8128 else if (option_flags & P_FLAGLIST)
8129 {
8130 // Only present the flags that are set on the option as the other flags
8131 // are not meaningful to do set-= on.
8132
8133 if (*xp->xp_pattern != NUL)
8134 {
8135 // Don't suggest anything if cmdline is non-empty. Vim's set-=
8136 // behavior requires consecutive strings and it's usually
Viktor Szépedbf749b2023-10-16 09:53:37 +02008137 // unintuitive to users if they try to subtract multiple flags at
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008138 // once.
8139 return FAIL;
8140 }
8141
Yee Cheng Chin6d113472023-10-02 21:38:39 +02008142 size_t num_flags = STRLEN(option_val);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008143 if (num_flags == 0)
8144 return FAIL;
8145
8146 *matches = ALLOC_MULT(char_u *, num_flags + 1);
8147 if (*matches == NULL)
8148 return FAIL;
8149
8150 int count = 0;
8151 char_u *p;
8152
John Marriott95dacbb2024-09-10 21:25:14 +02008153 p = vim_strnsave(option_val, num_flags);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008154 if (p == NULL)
8155 {
8156 VIM_CLEAR(*matches);
8157 return FAIL;
8158 }
8159 (*matches)[count++] = p;
8160
8161 if (num_flags > 1)
8162 {
8163 // If more than one flags, split the flags up and expose each
8164 // character as individual choice.
8165 for (char_u *flag = option_val; *flag != NUL; flag++)
8166 {
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02008167 p = vim_strnsave(flag, 1);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008168 if (p == NULL)
8169 break;
8170 (*matches)[count++] = p;
8171 }
8172 }
8173
8174 *numMatches = count;
8175 return OK;
8176 }
8177
8178 return ExpandOldSetting(numMatches, matches);
8179}
8180
8181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 * Get the value for the numeric or string option *opp in a nice format into
8183 * NameBuff[]. Must not be called with a hidden option!
8184 */
8185 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008186option_value2string(
8187 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008188 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189{
8190 char_u *varp;
8191
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008192 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193
8194 if (opp->flags & P_NUM)
8195 {
8196 long wc = 0;
8197
8198 if (wc_use_keyname(varp, &wc))
8199 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
8200 else if (wc != 0)
8201 STRCPY(NameBuff, transchar((int)wc));
8202 else
8203 sprintf((char *)NameBuff, "%ld", *(long *)varp);
8204 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008205 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 {
8207 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008208 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 NameBuff[0] = NUL;
8210#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008211 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008212 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 STRCPY(NameBuff, "*****");
8214#endif
8215 else if (opp->flags & P_EXPAND)
8216 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008217 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 else if ((char_u **)opp->var == &p_pt)
8219 str2specialbuf(p_pt, NameBuff, MAXPATHL);
8220 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008221 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 }
8223}
8224
8225/*
8226 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
8227 * printed as a keyname.
8228 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
8229 */
8230 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008231wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232{
8233 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
8234 {
8235 *wcp = *(long *)varp;
8236 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
8237 return TRUE;
8238 }
8239 return FALSE;
8240}
8241
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 * Return TRUE if "x" is present in 'shortmess' option, or
8244 * 'shortmess' contains 'a' and "x" is present in SHM_A.
8245 */
8246 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008247shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01008249 return p_shm != NULL &&
8250 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 || (vim_strchr(p_shm, 'a') != NULL
8252 && vim_strchr((char_u *)SHM_A, x) != NULL));
8253}
8254
8255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
8257 *
8258 * Reset 'compatible' and set the values for options that didn't get set yet
8259 * to the Vim defaults.
8260 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008261 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 */
8263 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008264vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008266 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00008267 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008268 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269
8270 if (!option_was_set((char_u *)"cp"))
8271 {
8272 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02008273 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
8275 set_option_default(opt_idx, OPT_FREE, FALSE);
8276 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008277 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008279
8280 if (fname != NULL)
8281 {
8282 p = vim_getenv(envname, &dofree);
8283 if (p == NULL)
8284 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008285 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008286 p = FullName_save(fname, FALSE);
8287 if (p != NULL)
8288 {
8289 vim_setenv(envname, p);
Christian Brabandt4e7249a2024-09-05 17:46:19 +02008290 if (vim_getenv((char_u *)"MYVIMDIR", &dofree) == NULL)
8291 {
8292 size_t usedlen = 0;
8293 int len = 0;
8294 char_u *fbuf = NULL;
8295
8296 if (STRNCMP(gettail(fname), ".vimrc", 6) == 0)
8297 {
8298 len = STRLEN(p) - 2;
8299 p[len] = '/';
8300 }
8301 else if (STRNCMP(gettail(fname), ".gvimrc", 7) == 0)
8302 {
8303 len = STRLEN(p);
8304 char_u *buf = alloc(len);
8305 if (buf != NULL)
8306 {
8307 mch_memmove(buf, fname, len - 7);
8308 mch_memmove(buf + len - 7, (char_u *)".vim/", 5);
8309 len -= 2; // decrement len, so that we can set len+1 = NUL below
8310 vim_free(p);
8311 p = buf;
8312 }
8313 }
8314#ifdef MSWIN
8315 else if (STRNCMP(gettail(fname), "_vimrc", 6) == 0)
8316 {
8317 len = STRLEN(p) + 4; // remove _vimrc, add vimfiles/
8318 char_u *buf = alloc(len);
8319 if (buf != NULL)
8320 {
8321 mch_memmove(buf, fname, len - 10);
8322 mch_memmove(buf + len - 10, (char_u *)"vimfiles\\", 9);
8323 len -= 2; // decrement len, so that we can set len+1 = NUL below
8324 vim_free(p);
8325 p = buf;
8326 }
8327 }
8328#endif
8329 else
8330 (void)modify_fname((char_u *)":h", FALSE, &usedlen, &p, &fbuf, &len);
8331
8332 if (p != NULL)
8333 {
8334 // keep the directory separator
8335 p[len + 1] = NUL;
8336 vim_setenv((char_u *)"MYVIMDIR", p);
8337 }
8338 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008339 vim_free(p);
8340 }
8341 }
8342 else if (dofree)
8343 vim_free(p);
8344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345}
8346
8347/*
8348 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
8349 */
8350 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008351change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008353 int opt_idx;
8354
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 if (p_cp != on)
8356 {
8357 p_cp = on;
8358 compatible_set();
8359 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008360 opt_idx = findoption((char_u *)"cp");
8361 if (opt_idx >= 0)
8362 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363}
8364
8365/*
8366 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02008367 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 */
8369 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008370option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371{
8372 int idx;
8373
8374 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008375 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 return FALSE;
8377 if (options[idx].flags & P_WAS_SET)
8378 return TRUE;
8379 return FALSE;
8380}
8381
8382/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008383 * Reset the flag indicating option "name" was set.
8384 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02008385 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008386reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008387{
8388 int idx = findoption(name);
8389
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008390 if (idx < 0)
8391 return FAIL;
8392
8393 options[idx].flags &= ~P_WAS_SET;
8394 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008395}
8396
8397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 * compatible_set() - Called when 'compatible' has been set or unset.
8399 *
8400 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
8401 * flag) to a Vi compatible value.
8402 * When 'compatible' is unset: Set all options that have a different default
8403 * for Vim (without the P_VI_DEF flag) to that default.
8404 */
8405 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008406compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407{
8408 int opt_idx;
8409
Bram Moolenaardac13472019-09-16 21:06:21 +02008410 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
8412 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
8413 set_option_default(opt_idx, OPT_FREE, p_cp);
8414 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008415 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416}
8417
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 * Check if backspacing over something is allowed.
8420 */
8421 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008422can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008423 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02008425#ifdef FEAT_JOB_CHANNEL
8426 if (what == BS_START && bt_prompt(curbuf))
8427 return FALSE;
8428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 switch (*p_bs)
8430 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008431 case '3': return TRUE;
8432 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 case '1': return (what != BS_START);
8434 case '0': return FALSE;
8435 }
8436 return vim_strchr(p_bs, what) != NULL;
8437}
8438
8439/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01008440 * Return the effective 'scrolloff' value for the current window, using the
8441 * global value when appropriate.
8442 */
8443 long
8444get_scrolloff_value(void)
8445{
8446 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
8447}
8448
8449/*
8450 * Return the effective 'sidescrolloff' value for the current window, using the
8451 * global value when appropriate.
8452 */
8453 long
8454get_sidescrolloff_value(void)
8455{
8456 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
8457}
8458
8459/*
zeertzjqaa925ee2024-06-09 18:24:05 +02008460 * Get the local or global value of 'backupcopy' flags.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008461 */
8462 unsigned int
zeertzjqaa925ee2024-06-09 18:24:05 +02008463get_bkc_flags(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008464{
8465 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
8466}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02008467
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008468#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02008469/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00008470 * Get the local or global value of 'formatlistpat'.
8471 */
8472 char_u *
8473get_flp_value(buf_T *buf)
8474{
Christian Brabandtc53b4672022-01-15 10:01:05 +00008475 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
8476 return p_flp;
8477 return buf->b_p_flp;
8478}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008479#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00008480
8481/*
zeertzjqaa925ee2024-06-09 18:24:05 +02008482 * Get the local or global value of 'virtualedit' flags.
Gary Johnson53ba05b2021-07-26 22:19:10 +02008483 */
8484 unsigned int
8485get_ve_flags(void)
8486{
Gary Johnson51ad8502021-08-03 18:33:08 +02008487 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
8488 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02008489}
8490
Bram Moolenaaree857022019-11-09 23:26:40 +01008491#if defined(FEAT_LINEBREAK) || defined(PROTO)
8492/*
8493 * Get the local or global value of 'showbreak'.
8494 */
8495 char_u *
8496get_showbreak_value(win_T *win)
8497{
8498 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8499 return p_sbr;
8500 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8501 return empty_option;
8502 return win->w_p_sbr;
8503}
8504#endif
8505
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008506#if defined(FEAT_EVAL) || defined(PROTO)
8507/*
8508 * Get window or buffer local options.
8509 */
8510 dict_T *
8511get_winbuf_options(int bufopt)
8512{
8513 dict_T *d;
8514 int opt_idx;
8515
8516 d = dict_alloc();
8517 if (d == NULL)
8518 return NULL;
8519
Bram Moolenaardac13472019-09-16 21:06:21 +02008520 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008521 {
8522 struct vimoption *opt = &options[opt_idx];
8523
8524 if ((bufopt && (opt->indir & PV_BUF))
8525 || (!bufopt && (opt->indir & PV_WIN)))
8526 {
8527 char_u *varp = get_varp(opt);
8528
8529 if (varp != NULL)
8530 {
8531 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008532 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008533 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008534 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008535 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008536 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008537 }
8538 }
8539 }
8540
8541 return d;
8542}
8543#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008544
Bram Moolenaardac13472019-09-16 21:06:21 +02008545#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008546/*
8547 * This is called when 'culopt' is changed
8548 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008549 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008550fill_culopt_flags(char_u *val, win_T *wp)
8551{
8552 char_u *p;
8553 char_u culopt_flags_new = 0;
8554
8555 if (val == NULL)
8556 p = wp->w_p_culopt;
8557 else
8558 p = val;
8559 while (*p != NUL)
8560 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008561 // Note: Keep this in sync with p_culopt_values.
Bram Moolenaar017ba072019-09-14 21:01:23 +02008562 if (STRNCMP(p, "line", 4) == 0)
8563 {
8564 p += 4;
8565 culopt_flags_new |= CULOPT_LINE;
8566 }
8567 else if (STRNCMP(p, "both", 4) == 0)
8568 {
8569 p += 4;
8570 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8571 }
8572 else if (STRNCMP(p, "number", 6) == 0)
8573 {
8574 p += 6;
8575 culopt_flags_new |= CULOPT_NBR;
8576 }
8577 else if (STRNCMP(p, "screenline", 10) == 0)
8578 {
8579 p += 10;
8580 culopt_flags_new |= CULOPT_SCRLINE;
8581 }
8582
8583 if (*p != ',' && *p != NUL)
8584 return FAIL;
8585 if (*p == ',')
8586 ++p;
8587 }
8588
8589 // Can't have both "line" and "screenline".
8590 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8591 return FAIL;
8592 wp->w_p_culopt_flags = culopt_flags_new;
8593
8594 return OK;
8595}
8596#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008597
8598/*
8599 * Get the value of 'magic' adjusted for Vim9 script.
8600 */
8601 int
8602magic_isset(void)
8603{
8604 switch (magic_overruled)
8605 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008606 case OPTION_MAGIC_ON: return TRUE;
8607 case OPTION_MAGIC_OFF: return FALSE;
8608 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008609 }
8610#ifdef FEAT_EVAL
8611 if (in_vim9script())
8612 return TRUE;
8613#endif
8614 return p_magic;
8615}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008616
8617/*
8618 * Set the callback function value for an option that accepts a function name,
8619 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8620 * Returns OK if the option is successfully set to a function, otherwise
8621 * returns FAIL.
8622 */
8623 int
8624option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8625{
8626#ifdef FEAT_EVAL
8627 typval_T *tv;
8628 callback_T cb;
8629
8630 if (optval == NULL || *optval == NUL)
8631 {
8632 free_callback(optcb);
8633 return OK;
8634 }
8635
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008636 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008637 || (STRNCMP(optval, "function(", 9) == 0)
8638 || (STRNCMP(optval, "funcref(", 8) == 0))
8639 // Lambda expression or a funcref
8640 tv = eval_expr(optval, NULL);
8641 else
8642 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008643 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008644 if (tv == NULL)
8645 return FAIL;
8646
8647 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008648 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008649 {
8650 free_tv(tv);
8651 return FAIL;
8652 }
8653
8654 free_callback(optcb);
8655 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008656 if (cb.cb_free_name)
8657 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008658 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008659
8660 // when using Vim9 style "import.funcname" it needs to be expanded to
8661 // "import#funcname".
8662 expand_autload_callback(optcb);
8663
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008664 return OK;
8665#else
8666 return FAIL;
8667#endif
8668}
Christian Brabandt757593c2023-08-22 21:44:10 +02008669
8670#if defined(FEAT_EVAL) || defined(PROTO)
8671 static void
8672didset_options_sctx(int opt_flags, char **buf)
8673{
8674 for (int i = 0; ; ++i)
8675 {
8676 if (buf[i] == NULL)
8677 break;
8678
8679 int idx = findoption((char_u *)buf[i]);
8680 if (idx >= 0)
8681 set_option_sctx_idx(idx, opt_flags, current_sctx);
8682 }
8683}
8684#endif