blob: 5f04406265d4a54c7c2e1913eeb4ed7726b22865 [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
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 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
Bram Moolenaard787e402021-12-24 21:36:12 +000048 type_T *vv_type; // type or NULL
Bram Moolenaare5cdf152019-08-29 22:09:46 +020049 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
50} vimvars[VV_LEN] =
51{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020052 // The order here must match the VV_ defines in vim.h!
53 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaard787e402021-12-24 21:36:12 +000054 {VV_NAME("count", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
55 {VV_NAME("count1", VAR_NUMBER), NULL, VV_RO},
56 {VV_NAME("prevcount", VAR_NUMBER), NULL, VV_RO},
57 {VV_NAME("errmsg", VAR_STRING), NULL, VV_COMPAT},
58 {VV_NAME("warningmsg", VAR_STRING), NULL, 0},
59 {VV_NAME("statusmsg", VAR_STRING), NULL, 0},
60 {VV_NAME("shell_error", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
61 {VV_NAME("this_session", VAR_STRING), NULL, VV_COMPAT},
62 {VV_NAME("version", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
63 {VV_NAME("lnum", VAR_NUMBER), NULL, VV_RO_SBX},
64 {VV_NAME("termresponse", VAR_STRING), NULL, VV_RO},
65 {VV_NAME("fname", VAR_STRING), NULL, VV_RO},
66 {VV_NAME("lang", VAR_STRING), NULL, VV_RO},
67 {VV_NAME("lc_time", VAR_STRING), NULL, VV_RO},
68 {VV_NAME("ctype", VAR_STRING), NULL, VV_RO},
69 {VV_NAME("charconvert_from", VAR_STRING), NULL, VV_RO},
70 {VV_NAME("charconvert_to", VAR_STRING), NULL, VV_RO},
71 {VV_NAME("fname_in", VAR_STRING), NULL, VV_RO},
72 {VV_NAME("fname_out", VAR_STRING), NULL, VV_RO},
73 {VV_NAME("fname_new", VAR_STRING), NULL, VV_RO},
74 {VV_NAME("fname_diff", VAR_STRING), NULL, VV_RO},
75 {VV_NAME("cmdarg", VAR_STRING), NULL, VV_RO},
76 {VV_NAME("foldstart", VAR_NUMBER), NULL, VV_RO_SBX},
77 {VV_NAME("foldend", VAR_NUMBER), NULL, VV_RO_SBX},
78 {VV_NAME("folddashes", VAR_STRING), NULL, VV_RO_SBX},
79 {VV_NAME("foldlevel", VAR_NUMBER), NULL, VV_RO_SBX},
80 {VV_NAME("progname", VAR_STRING), NULL, VV_RO},
81 {VV_NAME("servername", VAR_STRING), NULL, VV_RO},
82 {VV_NAME("dying", VAR_NUMBER), NULL, VV_RO},
83 {VV_NAME("exception", VAR_STRING), NULL, VV_RO},
84 {VV_NAME("throwpoint", VAR_STRING), NULL, VV_RO},
85 {VV_NAME("register", VAR_STRING), NULL, VV_RO},
86 {VV_NAME("cmdbang", VAR_NUMBER), NULL, VV_RO},
87 {VV_NAME("insertmode", VAR_STRING), NULL, VV_RO},
88 {VV_NAME("val", VAR_UNKNOWN), NULL, VV_RO},
89 {VV_NAME("key", VAR_UNKNOWN), NULL, VV_RO},
90 {VV_NAME("profiling", VAR_NUMBER), NULL, VV_RO},
91 {VV_NAME("fcs_reason", VAR_STRING), NULL, VV_RO},
92 {VV_NAME("fcs_choice", VAR_STRING), NULL, 0},
93 {VV_NAME("beval_bufnr", VAR_NUMBER), NULL, VV_RO},
94 {VV_NAME("beval_winnr", VAR_NUMBER), NULL, VV_RO},
95 {VV_NAME("beval_winid", VAR_NUMBER), NULL, VV_RO},
96 {VV_NAME("beval_lnum", VAR_NUMBER), NULL, VV_RO},
97 {VV_NAME("beval_col", VAR_NUMBER), NULL, VV_RO},
98 {VV_NAME("beval_text", VAR_STRING), NULL, VV_RO},
99 {VV_NAME("scrollstart", VAR_STRING), NULL, 0},
100 {VV_NAME("swapname", VAR_STRING), NULL, VV_RO},
101 {VV_NAME("swapchoice", VAR_STRING), NULL, 0},
102 {VV_NAME("swapcommand", VAR_STRING), NULL, VV_RO},
103 {VV_NAME("char", VAR_STRING), NULL, 0},
104 {VV_NAME("mouse_win", VAR_NUMBER), NULL, 0},
105 {VV_NAME("mouse_winid", VAR_NUMBER), NULL, 0},
106 {VV_NAME("mouse_lnum", VAR_NUMBER), NULL, 0},
107 {VV_NAME("mouse_col", VAR_NUMBER), NULL, 0},
108 {VV_NAME("operator", VAR_STRING), NULL, VV_RO},
109 {VV_NAME("searchforward", VAR_NUMBER), NULL, 0},
110 {VV_NAME("hlsearch", VAR_NUMBER), NULL, 0},
111 {VV_NAME("oldfiles", VAR_LIST), &t_list_string, 0},
112 {VV_NAME("windowid", VAR_NUMBER), NULL, VV_RO},
113 {VV_NAME("progpath", VAR_STRING), NULL, VV_RO},
114 {VV_NAME("completed_item", VAR_DICT), &t_dict_string, VV_RO},
115 {VV_NAME("option_new", VAR_STRING), NULL, VV_RO},
116 {VV_NAME("option_old", VAR_STRING), NULL, VV_RO},
117 {VV_NAME("option_oldlocal", VAR_STRING), NULL, VV_RO},
118 {VV_NAME("option_oldglobal", VAR_STRING), NULL, VV_RO},
119 {VV_NAME("option_command", VAR_STRING), NULL, VV_RO},
120 {VV_NAME("option_type", VAR_STRING), NULL, VV_RO},
121 {VV_NAME("errors", VAR_LIST), &t_list_string, 0},
122 {VV_NAME("false", VAR_BOOL), NULL, VV_RO},
123 {VV_NAME("true", VAR_BOOL), NULL, VV_RO},
124 {VV_NAME("none", VAR_SPECIAL), NULL, VV_RO},
125 {VV_NAME("null", VAR_SPECIAL), NULL, VV_RO},
126 {VV_NAME("numbermax", VAR_NUMBER), NULL, VV_RO},
127 {VV_NAME("numbermin", VAR_NUMBER), NULL, VV_RO},
128 {VV_NAME("numbersize", VAR_NUMBER), NULL, VV_RO},
129 {VV_NAME("vim_did_enter", VAR_NUMBER), NULL, VV_RO},
130 {VV_NAME("testing", VAR_NUMBER), NULL, 0},
131 {VV_NAME("t_number", VAR_NUMBER), NULL, VV_RO},
132 {VV_NAME("t_string", VAR_NUMBER), NULL, VV_RO},
133 {VV_NAME("t_func", VAR_NUMBER), NULL, VV_RO},
134 {VV_NAME("t_list", VAR_NUMBER), NULL, VV_RO},
135 {VV_NAME("t_dict", VAR_NUMBER), NULL, VV_RO},
136 {VV_NAME("t_float", VAR_NUMBER), NULL, VV_RO},
137 {VV_NAME("t_bool", VAR_NUMBER), NULL, VV_RO},
138 {VV_NAME("t_none", VAR_NUMBER), NULL, VV_RO},
139 {VV_NAME("t_job", VAR_NUMBER), NULL, VV_RO},
140 {VV_NAME("t_channel", VAR_NUMBER), NULL, VV_RO},
141 {VV_NAME("t_blob", VAR_NUMBER), NULL, VV_RO},
142 {VV_NAME("termrfgresp", VAR_STRING), NULL, VV_RO},
143 {VV_NAME("termrbgresp", VAR_STRING), NULL, VV_RO},
144 {VV_NAME("termu7resp", VAR_STRING), NULL, VV_RO},
145 {VV_NAME("termstyleresp", VAR_STRING), NULL, VV_RO},
146 {VV_NAME("termblinkresp", VAR_STRING), NULL, VV_RO},
147 {VV_NAME("event", VAR_DICT), NULL, VV_RO},
148 {VV_NAME("versionlong", VAR_NUMBER), NULL, VV_RO},
149 {VV_NAME("echospace", VAR_NUMBER), NULL, VV_RO},
150 {VV_NAME("argv", VAR_LIST), &t_list_string, VV_RO},
151 {VV_NAME("collate", VAR_STRING), NULL, VV_RO},
152 {VV_NAME("exiting", VAR_SPECIAL), NULL, VV_RO},
153 {VV_NAME("colornames", VAR_DICT), &t_dict_string, VV_RO},
154 {VV_NAME("sizeofint", VAR_NUMBER), NULL, VV_RO},
155 {VV_NAME("sizeoflong", VAR_NUMBER), NULL, VV_RO},
156 {VV_NAME("sizeofpointer", VAR_NUMBER), NULL, VV_RO},
naohiro ono56200ee2022-01-01 14:59:44 +0000157 {VV_NAME("maxcol", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200158};
159
160// shorthand
Bram Moolenaard787e402021-12-24 21:36:12 +0000161#define vv_tv_type vv_di.di_tv.v_type
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200162#define vv_nr vv_di.di_tv.vval.v_number
163#define vv_float vv_di.di_tv.vval.v_float
164#define vv_str vv_di.di_tv.vval.v_string
165#define vv_list vv_di.di_tv.vval.v_list
166#define vv_dict vv_di.di_tv.vval.v_dict
167#define vv_blob vv_di.di_tv.vval.v_blob
168#define vv_tv vv_di.di_tv
169
170static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200171static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200172#define vimvarht vimvardict.dv_hashtab
173
174// for VIM_VERSION_ defines
175#include "version.h"
176
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200177static void list_glob_vars(int *first);
178static void list_buf_vars(int *first);
179static void list_win_vars(int *first);
180static void list_tab_vars(int *first);
181static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100182static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op, int var_idx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200183static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
184static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200185static void list_one_var(dictitem_T *v, char *prefix, int *first);
186static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
187
188/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200189 * Initialize global and vim special variables
190 */
191 void
192evalvars_init(void)
193{
194 int i;
195 struct vimvar *p;
196
197 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
198 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
199 vimvardict.dv_lock = VAR_FIXED;
200 hash_init(&compat_hashtab);
201
202 for (i = 0; i < VV_LEN; ++i)
203 {
204 p = &vimvars[i];
205 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
206 {
207 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
208 getout(1);
209 }
210 STRCPY(p->vv_di.di_key, p->vv_name);
211 if (p->vv_flags & VV_RO)
212 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
213 else if (p->vv_flags & VV_RO_SBX)
214 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
215 else
216 p->vv_di.di_flags = DI_FLAGS_FIX;
217
218 // add to v: scope dict, unless the value is not always available
Bram Moolenaard787e402021-12-24 21:36:12 +0000219 if (p->vv_tv_type != VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200220 hash_add(&vimvarht, p->vv_di.di_key);
221 if (p->vv_flags & VV_COMPAT)
222 // add to compat scope dict
223 hash_add(&compat_hashtab, p->vv_di.di_key);
224 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200225 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
226 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200227
228 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
229 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100230 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200231 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
232 set_vim_var_list(VV_ERRORS, list_alloc());
233 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
234
235 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
236 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
237 set_vim_var_nr(VV_NONE, VVAL_NONE);
238 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100239 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
240 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100241 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaar69b30722021-11-02 21:39:49 +0000242 set_vim_var_nr(VV_SIZEOFINT, sizeof(int));
243 set_vim_var_nr(VV_SIZEOFLONG, sizeof(long));
244 set_vim_var_nr(VV_SIZEOFPOINTER, sizeof(char *));
naohiro ono56200ee2022-01-01 14:59:44 +0000245 set_vim_var_nr(VV_MAXCOL, MAXCOL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200246
247 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
248 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
249 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
250 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
251 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
252 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
253 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
254 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
255 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
256 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
257 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
258
259 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
260
Drew Vogele30d1022021-10-24 20:35:07 +0100261 set_vim_var_dict(VV_COLORNAMES, dict_alloc());
262
Bram Moolenaar439c0362020-06-06 15:58:03 +0200263 // Default for v:register is not 0 but '"'. This is adjusted once the
264 // clipboard has been setup by calling reset_reg_var().
265 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200266}
267
268#if defined(EXITFREE) || defined(PROTO)
269/*
270 * Free all vim variables information on exit
271 */
272 void
273evalvars_clear(void)
274{
275 int i;
276 struct vimvar *p;
277
278 for (i = 0; i < VV_LEN; ++i)
279 {
280 p = &vimvars[i];
281 if (p->vv_di.di_tv.v_type == VAR_STRING)
282 VIM_CLEAR(p->vv_str);
283 else if (p->vv_di.di_tv.v_type == VAR_LIST)
284 {
285 list_unref(p->vv_list);
286 p->vv_list = NULL;
287 }
288 }
289 hash_clear(&vimvarht);
290 hash_init(&vimvarht); // garbage_collect() will access it
291 hash_clear(&compat_hashtab);
292
293 // global variables
294 vars_clear(&globvarht);
295
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100296 // Script-local variables. Clear all the variables here.
297 // The scriptvar_T is cleared later in free_scriptnames(), because a
298 // variable in one script might hold a reference to the whole scope of
299 // another script.
300 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200301 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200302}
303#endif
304
305 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200306garbage_collect_globvars(int copyID)
307{
308 return set_ref_in_ht(&globvarht, copyID, NULL);
309}
310
311 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200312garbage_collect_vimvars(int copyID)
313{
314 return set_ref_in_ht(&vimvarht, copyID, NULL);
315}
316
317 int
318garbage_collect_scriptvars(int copyID)
319{
Bram Moolenaared234f22020-10-15 20:42:20 +0200320 int i;
321 int idx;
322 int abort = FALSE;
323 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200324
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100325 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200326 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200327 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
328
Bram Moolenaared234f22020-10-15 20:42:20 +0200329 si = SCRIPT_ITEM(i);
330 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
331 {
332 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
333
Bram Moolenaard00a7fb2021-03-08 20:47:14 +0100334 if (sv->sv_name != NULL)
335 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
Bram Moolenaared234f22020-10-15 20:42:20 +0200336 }
337 }
338
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200339 return abort;
340}
341
342/*
343 * Set an internal variable to a string value. Creates the variable if it does
344 * not already exist.
345 */
346 void
347set_internal_string_var(char_u *name, char_u *value)
348{
349 char_u *val;
350 typval_T *tvp;
351
352 val = vim_strsave(value);
353 if (val != NULL)
354 {
355 tvp = alloc_string_tv(val);
356 if (tvp != NULL)
357 {
358 set_var(name, tvp, FALSE);
359 free_tv(tvp);
360 }
361 }
362}
363
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200364 int
365eval_charconvert(
366 char_u *enc_from,
367 char_u *enc_to,
368 char_u *fname_from,
369 char_u *fname_to)
370{
371 int err = FALSE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000372 sctx_T saved_sctx = current_sctx;
373 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200374
375 set_vim_var_string(VV_CC_FROM, enc_from, -1);
376 set_vim_var_string(VV_CC_TO, enc_to, -1);
377 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
378 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000379 ctx = get_option_sctx("charconvert");
380 if (ctx != NULL)
381 current_sctx = *ctx;
382
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200383 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
384 err = TRUE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000385
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200386 set_vim_var_string(VV_CC_FROM, NULL, -1);
387 set_vim_var_string(VV_CC_TO, NULL, -1);
388 set_vim_var_string(VV_FNAME_IN, NULL, -1);
389 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000390 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200391
392 if (err)
393 return FAIL;
394 return OK;
395}
396
397# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
398 int
399eval_printexpr(char_u *fname, char_u *args)
400{
401 int err = FALSE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000402 sctx_T saved_sctx = current_sctx;
403 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200404
405 set_vim_var_string(VV_FNAME_IN, fname, -1);
406 set_vim_var_string(VV_CMDARG, args, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000407 ctx = get_option_sctx("printexpr");
408 if (ctx != NULL)
409 current_sctx = *ctx;
410
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200411 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
412 err = TRUE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000413
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200414 set_vim_var_string(VV_FNAME_IN, NULL, -1);
415 set_vim_var_string(VV_CMDARG, NULL, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000416 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200417
418 if (err)
419 {
420 mch_remove(fname);
421 return FAIL;
422 }
423 return OK;
424}
425# endif
426
427# if defined(FEAT_DIFF) || defined(PROTO)
428 void
429eval_diff(
430 char_u *origfile,
431 char_u *newfile,
432 char_u *outfile)
433{
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000434 sctx_T saved_sctx = current_sctx;
435 sctx_T *ctx;
436 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200437
438 set_vim_var_string(VV_FNAME_IN, origfile, -1);
439 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
440 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000441
442 ctx = get_option_sctx("diffexpr");
443 if (ctx != NULL)
444 current_sctx = *ctx;
445
446 // errors are ignored
447 tv = eval_expr(p_dex, NULL);
Bram Moolenaar39b89442022-01-22 18:21:36 +0000448 free_tv(tv);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000449
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200450 set_vim_var_string(VV_FNAME_IN, NULL, -1);
451 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
452 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000453 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200454}
455
456 void
457eval_patch(
458 char_u *origfile,
459 char_u *difffile,
460 char_u *outfile)
461{
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000462 sctx_T saved_sctx = current_sctx;
463 sctx_T *ctx;
464 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200465
466 set_vim_var_string(VV_FNAME_IN, origfile, -1);
467 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
468 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000469
470 ctx = get_option_sctx("patchexpr");
471 if (ctx != NULL)
472 current_sctx = *ctx;
473
474 // errors are ignored
475 tv = eval_expr(p_pex, NULL);
476 free_tv(tv);
477
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200478 set_vim_var_string(VV_FNAME_IN, NULL, -1);
479 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
480 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000481 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200482}
483# endif
484
485#if defined(FEAT_SPELL) || defined(PROTO)
486/*
487 * Evaluate an expression to a list with suggestions.
488 * For the "expr:" part of 'spellsuggest'.
489 * Returns NULL when there is an error.
490 */
491 list_T *
492eval_spell_expr(char_u *badword, char_u *expr)
493{
494 typval_T save_val;
495 typval_T rettv;
496 list_T *list = NULL;
497 char_u *p = skipwhite(expr);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000498 sctx_T saved_sctx = current_sctx;
499 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200500
501 // Set "v:val" to the bad word.
502 prepare_vimvar(VV_VAL, &save_val);
503 set_vim_var_string(VV_VAL, badword, -1);
504 if (p_verbose == 0)
505 ++emsg_off;
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000506 ctx = get_option_sctx("spellsuggest");
507 if (ctx != NULL)
508 current_sctx = *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200509
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200510 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200511 {
512 if (rettv.v_type != VAR_LIST)
513 clear_tv(&rettv);
514 else
515 list = rettv.vval.v_list;
516 }
517
518 if (p_verbose == 0)
519 --emsg_off;
520 clear_tv(get_vim_var_tv(VV_VAL));
521 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000522 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200523
524 return list;
525}
526
527/*
528 * "list" is supposed to contain two items: a word and a number. Return the
529 * word in "pp" and the number as the return value.
530 * Return -1 if anything isn't right.
531 * Used to get the good word and score from the eval_spell_expr() result.
532 */
533 int
534get_spellword(list_T *list, char_u **pp)
535{
536 listitem_T *li;
537
538 li = list->lv_first;
539 if (li == NULL)
540 return -1;
541 *pp = tv_get_string(&li->li_tv);
542
543 li = li->li_next;
544 if (li == NULL)
545 return -1;
546 return (int)tv_get_number(&li->li_tv);
547}
548#endif
549
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200550/*
551 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200552 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200553 * When not used yet add the variable to the v: hashtable.
554 */
555 void
556prepare_vimvar(int idx, typval_T *save_tv)
557{
558 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200559 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaard787e402021-12-24 21:36:12 +0000560 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200561 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
562}
563
564/*
565 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200566 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200567 * When no longer defined, remove the variable from the v: hashtable.
568 */
569 void
570restore_vimvar(int idx, typval_T *save_tv)
571{
572 hashitem_T *hi;
573
574 vimvars[idx].vv_tv = *save_tv;
Bram Moolenaard787e402021-12-24 21:36:12 +0000575 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200576 {
577 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
578 if (HASHITEM_EMPTY(hi))
579 internal_error("restore_vimvar()");
580 else
581 hash_remove(&vimvarht, hi);
582 }
583}
584
585/*
586 * List Vim variables.
587 */
588 static void
589list_vim_vars(int *first)
590{
591 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
592}
593
594/*
595 * List script-local variables, if there is a script.
596 */
597 static void
598list_script_vars(int *first)
599{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200600 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200601 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
602 "s:", FALSE, first);
603}
604
605/*
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100606 * Evaluate all the Vim expressions (`=expr`) in string "str" and return the
607 * resulting string. The caller must free the returned string.
608 */
609 static char_u *
610eval_all_expr_in_str(char_u *str)
611{
612 garray_T ga;
613 char_u *s;
614 char_u *p;
615 char_u save_c;
616 char_u *exprval;
617 int status;
618
619 ga_init2(&ga, 1, 80);
620 p = str;
621
622 // Look for `=expr`, evaluate the expression and replace `=expr` with the
623 // result.
624 while (*p != NUL)
625 {
626 s = p;
627 while (*p != NUL && (*p != '`' || p[1] != '='))
628 p++;
629 ga_concat_len(&ga, s, p - s);
630 if (*p == NUL)
631 break; // no backtick expression found
632
633 s = p;
634 p += 2; // skip `=
635
636 status = *p == NUL ? OK : skip_expr(&p, NULL);
637 if (status == FAIL || *p != '`')
638 {
639 // invalid expression or missing ending backtick
640 if (status != FAIL)
641 emsg(_(e_missing_backtick));
642 vim_free(ga.ga_data);
643 return NULL;
644 }
645 s += 2; // skip `=
646 save_c = *p;
647 *p = NUL;
648 exprval = eval_to_string(s, TRUE);
649 *p = save_c;
650 p++;
651 if (exprval == NULL)
652 {
653 // expression evaluation failed
654 vim_free(ga.ga_data);
655 return NULL;
656 }
657 ga_concat(&ga, exprval);
658 vim_free(exprval);
659 }
660 ga_append(&ga, NUL);
661
662 return ga.ga_data;
663}
664
665/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200666 * Get a list of lines from a HERE document. The here document is a list of
667 * lines surrounded by a marker.
668 * cmd << {marker}
669 * {line1}
670 * {line2}
671 * ....
672 * {marker}
673 *
674 * The {marker} is a string. If the optional 'trim' word is supplied before the
675 * marker, then the leading indentation before the lines (matching the
676 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200677 *
678 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
679 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
680 * missing, then '.' is accepted as a marker.
681 *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100682 * Returns a List with {lines} or NULL on failure.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200683 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100684 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200685heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200686{
687 char_u *theline;
688 char_u *marker;
689 list_T *l;
690 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100691 char_u *str;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200692 int marker_indent_len = 0;
693 int text_indent_len = 0;
694 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200695 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200696 int comment_char = in_vim9script() ? '#' : '"';
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100697 int evalstr = FALSE;
698 int eval_failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200699
700 if (eap->getline == NULL)
701 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000702 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200703 return NULL;
704 }
705
706 // Check for the optional 'trim' word before the marker
707 cmd = skipwhite(cmd);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200708
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100709 while (TRUE)
710 {
711 if (STRNCMP(cmd, "trim", 4) == 0
712 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200713 {
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100714 cmd = skipwhite(cmd + 4);
715
716 // Trim the indentation from all the lines in the here document.
717 // The amount of indentation trimmed is the same as the indentation
718 // of the first line after the :let command line. To find the end
719 // marker the indent of the :let command line is trimmed.
720 p = *eap->cmdlinep;
721 while (VIM_ISWHITE(*p))
722 {
723 p++;
724 marker_indent_len++;
725 }
726 text_indent_len = -1;
727
728 continue;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200729 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100730 if (STRNCMP(cmd, "eval", 4) == 0
731 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
732 {
733 cmd = skipwhite(cmd + 4);
734 evalstr = TRUE;
735 continue;
736 }
737 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200738 }
739
740 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200741 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200742 {
743 marker = skipwhite(cmd);
744 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200745 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200746 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000747 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200748 return NULL;
749 }
750 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200751 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200752 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000753 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200754 return NULL;
755 }
756 }
757 else
758 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200759 // When getting lines for an embedded script, if the marker is missing,
760 // accept '.' as the marker.
761 if (script_get)
762 marker = dot;
763 else
764 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000765 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200766 return NULL;
767 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200768 }
769
770 l = list_alloc();
771 if (l == NULL)
772 return NULL;
773
774 for (;;)
775 {
776 int mi = 0;
777 int ti = 0;
778
779 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
780 if (theline == NULL)
781 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000782 semsg(_(e_missing_end_marker_str), marker);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200783 break;
784 }
785
786 // with "trim": skip the indent matching the :let line to find the
787 // marker
788 if (marker_indent_len > 0
789 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
790 mi = marker_indent_len;
791 if (STRCMP(marker, theline + mi) == 0)
792 {
793 vim_free(theline);
794 break;
795 }
796
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100797 // If expression evaluation failed in the heredoc, then skip till the
798 // end marker.
799 if (eval_failed)
800 {
801 vim_free(theline);
802 continue;
803 }
804
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200805 if (text_indent_len == -1 && *theline != NUL)
806 {
807 // set the text indent from the first line.
808 p = theline;
809 text_indent_len = 0;
810 while (VIM_ISWHITE(*p))
811 {
812 p++;
813 text_indent_len++;
814 }
815 text_indent = vim_strnsave(theline, text_indent_len);
816 }
817 // with "trim": skip the indent matching the first line
818 if (text_indent != NULL)
819 for (ti = 0; ti < text_indent_len; ++ti)
820 if (theline[ti] != text_indent[ti])
821 break;
822
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100823 str = theline + ti;
824 if (evalstr)
825 {
826 str = eval_all_expr_in_str(str);
827 if (str == NULL)
828 {
829 // expression evaluation failed
830 vim_free(theline);
831 eval_failed = TRUE;
832 continue;
833 }
834 vim_free(theline);
835 theline = str;
836 }
837
838 if (list_append_string(l, str, -1) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200839 break;
840 vim_free(theline);
841 }
842 vim_free(text_indent);
843
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100844 if (eval_failed)
845 {
846 // expression evaluation in the heredoc failed
847 list_free(l);
848 return NULL;
849 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200850 return l;
851}
852
853/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200854 * Vim9 variable declaration:
855 * ":var name"
856 * ":var name: type"
857 * ":var name = expr"
858 * ":var name: type = expr"
859 * etc.
860 */
861 void
862ex_var(exarg_T *eap)
863{
Bram Moolenaar330a3882022-03-05 11:05:57 +0000864 char_u *p = eap->cmd;
Bram Moolenaare1d12112022-03-05 11:37:48 +0000865 int has_var;
Bram Moolenaar330a3882022-03-05 11:05:57 +0000866
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200867 if (!in_vim9script())
868 {
869 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
870 return;
871 }
Bram Moolenaare1d12112022-03-05 11:37:48 +0000872 has_var = checkforcmd_noparen(&p, "var", 3);
873 if (current_sctx.sc_sid == 0 && has_var)
Bram Moolenaar0e1574c2022-03-03 17:05:35 +0000874 {
875 emsg(_(e_cannot_declare_variable_on_command_line));
876 return;
877 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200878 ex_let(eap);
879}
880
881/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200882 * ":let" list all variable values
883 * ":let var1 var2" list variable values
884 * ":let var = expr" assignment command.
885 * ":let var += expr" assignment command.
886 * ":let var -= expr" assignment command.
887 * ":let var *= expr" assignment command.
888 * ":let var /= expr" assignment command.
889 * ":let var %= expr" assignment command.
890 * ":let var .= expr" assignment command.
891 * ":let var ..= expr" assignment command.
892 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100893 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100894 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200895 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200896 * ":final var = expr" assignment command.
897 * ":final [var1, var2] = expr" unpack list.
898 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200899 * ":const" list all variable values
900 * ":const var1 var2" list variable values
901 * ":const var = expr" assignment command.
902 * ":const [var1, var2] = expr" unpack list.
903 */
904 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200905ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200906{
907 char_u *arg = eap->arg;
908 char_u *expr = NULL;
909 typval_T rettv;
910 int i;
911 int var_count = 0;
912 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200913 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200914 char_u *argend;
915 int first = TRUE;
916 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200917 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100918 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200919 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200920
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200921 if (eap->cmdidx == CMD_final && !vim9script)
922 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100923 // In legacy Vim script ":final" is short for ":finally".
924 ex_finally(eap);
925 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200926 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200927 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200928 {
929 emsg(_(e_cannot_use_let_in_vim9_script));
930 return;
931 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200932
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100933 if (eap->cmdidx == CMD_const)
934 flags |= ASSIGN_CONST;
935 else if (eap->cmdidx == CMD_final)
936 flags |= ASSIGN_FINAL;
937
938 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100939 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200940 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100941
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200942 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200943 if (argend == NULL)
944 return;
945 if (argend > arg && argend[-1] == '.') // for var.='str'
946 --argend;
947 expr = skipwhite(argend);
948 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +0200949 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200950 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200951 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
952 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200953 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200954 {
955 // ":let" without "=": list variables
956 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000957 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200958 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000959 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200960 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200961 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200962 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200963 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100964 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +0000965 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100966 else
967 // Vim9 declaration ":var name: type"
968 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200969 }
970 else
971 {
972 // ":let var1 var2" - list values
973 arg = list_arg_vars(eap, arg, &first);
974 }
975 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200976 else if (!eap->skip)
977 {
978 // ":let"
979 list_glob_vars(&first);
980 list_buf_vars(&first);
981 list_win_vars(&first);
982 list_tab_vars(&first);
983 list_script_vars(&first);
984 list_func_vars(&first);
985 list_vim_vars(&first);
986 }
Bram Moolenaar63b91732021-08-05 20:40:03 +0200987 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200988 }
989 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
990 {
991 list_T *l;
Bram Moolenaar81530e32021-07-28 21:25:49 +0200992 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200993
994 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200995 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200996 if (l != NULL)
997 {
998 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200999 if (!eap->skip)
1000 {
Bram Moolenaar81530e32021-07-28 21:25:49 +02001001 // errors are for the assignment, not the end marker
1002 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001003 op[0] = '=';
1004 op[1] = NUL;
1005 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001006 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001007 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001008 clear_tv(&rettv);
1009 }
1010 }
1011 else
1012 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001013 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001014 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001015
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02001016 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001017 i = FAIL;
1018 if (has_assign || concat)
1019 {
Bram Moolenaar9a562c12021-01-23 13:39:14 +01001020 int cur_lnum;
1021
Bram Moolenaar32e35112020-05-14 22:41:15 +02001022 op[0] = '=';
1023 op[1] = NUL;
1024 if (*expr != '=')
1025 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001026 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +02001027 {
1028 // +=, /=, etc. require an existing variable
1029 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
1030 i = FAIL;
1031 }
1032 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +02001033 {
1034 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001035 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001036 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001037 {
Bram Moolenaar32e35112020-05-14 22:41:15 +02001038 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001039 ++len;
1040 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02001041 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001042 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001043 }
1044 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001045 ++expr;
1046
Bram Moolenaar7f2c3412021-11-29 16:01:49 +00001047 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
Bram Moolenaarc7e44a72020-07-29 21:37:43 +02001048 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001049 {
1050 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01001051 semsg(_(e_white_space_required_before_and_after_str_at_str),
1052 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001053 i = FAIL;
1054 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02001055
1056 if (eap->skip)
1057 ++emsg_skip;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001058 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaarc7e44a72020-07-29 21:37:43 +02001059 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaar9a562c12021-01-23 13:39:14 +01001060 cur_lnum = SOURCING_LNUM;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001061 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001062 if (eap->skip)
1063 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001064 clear_evalarg(&evalarg, eap);
Bram Moolenaar9a562c12021-01-23 13:39:14 +01001065
1066 // Restore the line number so that any type error is given for the
1067 // declaration, not the expression.
1068 SOURCING_LNUM = cur_lnum;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001069 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001070 if (eap->skip)
1071 {
1072 if (i != FAIL)
1073 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001074 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001075 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001076 {
1077 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001078 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001079 clear_tv(&rettv);
1080 }
1081 }
1082}
1083
1084/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001085 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001086 * Handles both "var" with any type and "[var, var; var]" with a list type.
1087 * When "op" is not NULL it points to a string with characters that
1088 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1089 * or concatenate.
1090 * Returns OK or FAIL;
1091 */
1092 int
1093ex_let_vars(
1094 char_u *arg_start,
1095 typval_T *tv,
1096 int copy, // copy values from "tv", don't move
1097 int semicolon, // from skip_var_list()
1098 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001099 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001100 char_u *op)
1101{
1102 char_u *arg = arg_start;
1103 list_T *l;
1104 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001105 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001106 listitem_T *item;
1107 typval_T ltv;
1108
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00001109 if (tv->v_type == VAR_VOID)
1110 {
1111 emsg(_(e_cannot_use_void_value));
1112 return FAIL;
1113 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001114 if (*arg != '[')
1115 {
1116 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001117 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001118 return FAIL;
1119 return OK;
1120 }
1121
1122 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1123 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
1124 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001125 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001126 return FAIL;
1127 }
1128
1129 i = list_len(l);
1130 if (semicolon == 0 && var_count < i)
1131 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001132 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001133 return FAIL;
1134 }
1135 if (var_count - semicolon > i)
1136 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001137 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001138 return FAIL;
1139 }
1140
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001141 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001142 item = l->lv_first;
1143 while (*arg != ']')
1144 {
1145 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001146 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001147 arg = ex_let_one(arg, &item->li_tv, TRUE,
1148 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001149 item = item->li_next;
1150 if (arg == NULL)
1151 return FAIL;
1152
1153 arg = skipwhite(arg);
1154 if (*arg == ';')
1155 {
1156 // Put the rest of the list (may be empty) in the var after ';'.
1157 // Create a new list for this.
1158 l = list_alloc();
1159 if (l == NULL)
1160 return FAIL;
1161 while (item != NULL)
1162 {
1163 list_append_tv(l, &item->li_tv);
1164 item = item->li_next;
1165 }
1166
1167 ltv.v_type = VAR_LIST;
1168 ltv.v_lock = 0;
1169 ltv.vval.v_list = l;
1170 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001171 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001172
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001173 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1174 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001175 clear_tv(&ltv);
1176 if (arg == NULL)
1177 return FAIL;
1178 break;
1179 }
1180 else if (*arg != ',' && *arg != ']')
1181 {
1182 internal_error("ex_let_vars()");
1183 return FAIL;
1184 }
1185 }
1186
1187 return OK;
1188}
1189
1190/*
1191 * Skip over assignable variable "var" or list of variables "[var, var]".
1192 * Used for ":let varvar = expr" and ":for varvar in expr".
1193 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001194 * for "[var, var; var]" set "semicolon" to 1.
1195 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001196 * Return NULL for an error.
1197 */
1198 char_u *
1199skip_var_list(
1200 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001202 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001203 int *semicolon,
1204 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001205{
1206 char_u *p, *s;
1207
1208 if (*arg == '[')
1209 {
1210 // "[var, var]": find the matching ']'.
1211 p = arg;
1212 for (;;)
1213 {
1214 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001215 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001216 if (s == p)
1217 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001218 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001219 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001220 return NULL;
1221 }
1222 ++*var_count;
1223
1224 p = skipwhite(s);
1225 if (*p == ']')
1226 break;
1227 else if (*p == ';')
1228 {
1229 if (*semicolon == 1)
1230 {
Bram Moolenaar8b716f52022-02-15 21:17:56 +00001231 if (!silent)
1232 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001233 return NULL;
1234 }
1235 *semicolon = 1;
1236 }
1237 else if (*p != ',')
1238 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001239 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001240 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001241 return NULL;
1242 }
1243 }
1244 return p + 1;
1245 }
1246 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001247 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001248}
1249
1250/*
1251 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1252 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001253 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001254 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001255 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001256skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001257{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001258 char_u *end;
1259 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001261 if (*arg == '@' && arg[1] != NUL)
1262 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001264 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001265
1266 // "a: type" is declaring variable "a" with a type, not "a:".
1267 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001268 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001269 --end;
1270
Bram Moolenaar585587d2021-01-17 20:52:13 +01001271 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001272 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001273 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001274 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275 }
1276 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001277}
1278
1279/*
1280 * List variables for hashtab "ht" with prefix "prefix".
1281 * If "empty" is TRUE also list NULL strings as empty strings.
1282 */
1283 void
1284list_hashtable_vars(
1285 hashtab_T *ht,
1286 char *prefix,
1287 int empty,
1288 int *first)
1289{
1290 hashitem_T *hi;
1291 dictitem_T *di;
1292 int todo;
1293 char_u buf[IOSIZE];
1294
1295 todo = (int)ht->ht_used;
1296 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1297 {
1298 if (!HASHITEM_EMPTY(hi))
1299 {
1300 --todo;
1301 di = HI2DI(hi);
1302
1303 // apply :filter /pat/ to variable name
1304 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1305 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1306 if (message_filtered(buf))
1307 continue;
1308
1309 if (empty || di->di_tv.v_type != VAR_STRING
1310 || di->di_tv.vval.v_string != NULL)
1311 list_one_var(di, prefix, first);
1312 }
1313 }
1314}
1315
1316/*
1317 * List global variables.
1318 */
1319 static void
1320list_glob_vars(int *first)
1321{
1322 list_hashtable_vars(&globvarht, "", TRUE, first);
1323}
1324
1325/*
1326 * List buffer variables.
1327 */
1328 static void
1329list_buf_vars(int *first)
1330{
1331 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1332}
1333
1334/*
1335 * List window variables.
1336 */
1337 static void
1338list_win_vars(int *first)
1339{
1340 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1341}
1342
1343/*
1344 * List tab page variables.
1345 */
1346 static void
1347list_tab_vars(int *first)
1348{
1349 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1350}
1351
1352/*
1353 * List variables in "arg".
1354 */
1355 static char_u *
1356list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1357{
1358 int error = FALSE;
1359 int len;
1360 char_u *name;
1361 char_u *name_start;
1362 char_u *arg_subsc;
1363 char_u *tofree;
1364 typval_T tv;
1365
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001366 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001367 {
1368 if (error || eap->skip)
1369 {
1370 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1371 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1372 {
1373 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001374 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001375 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001376 break;
1377 }
1378 }
1379 else
1380 {
1381 // get_name_len() takes care of expanding curly braces
1382 name_start = name = arg;
1383 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1384 if (len <= 0)
1385 {
1386 // This is mainly to keep test 49 working: when expanding
1387 // curly braces fails overrule the exception error message.
1388 if (len < 0 && !aborting())
1389 {
1390 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001391 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001392 break;
1393 }
1394 error = TRUE;
1395 }
1396 else
1397 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001398 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001399 if (tofree != NULL)
1400 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001401 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001402 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001403 error = TRUE;
1404 else
1405 {
1406 // handle d.key, l[idx], f(expr)
1407 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001408 if (handle_subscript(&arg, name_start, &tv,
1409 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001410 error = TRUE;
1411 else
1412 {
1413 if (arg == arg_subsc && len == 2 && name[1] == ':')
1414 {
1415 switch (*name)
1416 {
1417 case 'g': list_glob_vars(first); break;
1418 case 'b': list_buf_vars(first); break;
1419 case 'w': list_win_vars(first); break;
1420 case 't': list_tab_vars(first); break;
1421 case 'v': list_vim_vars(first); break;
1422 case 's': list_script_vars(first); break;
1423 case 'l': list_func_vars(first); break;
1424 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001425 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001426 }
1427 }
1428 else
1429 {
1430 char_u numbuf[NUMBUFLEN];
1431 char_u *tf;
1432 int c;
1433 char_u *s;
1434
1435 s = echo_string(&tv, &tf, numbuf, 0);
1436 c = *arg;
1437 *arg = NUL;
1438 list_one_var_a("",
1439 arg == arg_subsc ? name : name_start,
1440 tv.v_type,
1441 s == NULL ? (char_u *)"" : s,
1442 first);
1443 *arg = c;
1444 vim_free(tf);
1445 }
1446 clear_tv(&tv);
1447 }
1448 }
1449 }
1450
1451 vim_free(tofree);
1452 }
1453
1454 arg = skipwhite(arg);
1455 }
1456
1457 return arg;
1458}
1459
1460/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001461 * Set an environment variable, part of ex_let_one().
1462 */
1463 static char_u *
1464ex_let_env(
1465 char_u *arg,
1466 typval_T *tv,
1467 int flags,
1468 char_u *endchars,
1469 char_u *op)
1470{
1471 char_u *arg_end = NULL;
1472 char_u *name;
1473 int len;
1474
1475 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1476 && (flags & ASSIGN_FOR_LOOP) == 0)
1477 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001478 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001479 return NULL;
1480 }
1481
1482 // Find the end of the name.
1483 ++arg;
1484 name = arg;
1485 len = get_env_len(&arg);
1486 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001487 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001488 else
1489 {
1490 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001491 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001492 else if (endchars != NULL
1493 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1494 emsg(_(e_unexpected_characters_in_let));
1495 else if (!check_secure())
1496 {
1497 char_u *tofree = NULL;
1498 int c1 = name[len];
1499 char_u *p;
1500
1501 name[len] = NUL;
1502 p = tv_get_string_chk(tv);
1503 if (p != NULL && op != NULL && *op == '.')
1504 {
1505 int mustfree = FALSE;
1506 char_u *s = vim_getenv(name, &mustfree);
1507
1508 if (s != NULL)
1509 {
1510 p = tofree = concat_str(s, p);
1511 if (mustfree)
1512 vim_free(s);
1513 }
1514 }
1515 if (p != NULL)
1516 {
1517 vim_setenv_ext(name, p);
1518 arg_end = arg;
1519 }
1520 name[len] = c1;
1521 vim_free(tofree);
1522 }
1523 }
1524 return arg_end;
1525}
1526
1527/*
1528 * Set an option, part of ex_let_one().
1529 */
1530 static char_u *
1531ex_let_option(
1532 char_u *arg,
1533 typval_T *tv,
1534 int flags,
1535 char_u *endchars,
1536 char_u *op)
1537{
1538 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001539 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001540 char_u *arg_end = NULL;
1541
1542 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1543 && (flags & ASSIGN_FOR_LOOP) == 0)
1544 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001545 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001546 return NULL;
1547 }
1548
1549 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001550 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001551 if (p == NULL || (endchars != NULL
1552 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1553 emsg(_(e_unexpected_characters_in_let));
1554 else
1555 {
1556 int c1;
1557 long n = 0;
1558 getoption_T opt_type;
1559 long numval;
1560 char_u *stringval = NULL;
1561 char_u *s = NULL;
1562 int failed = FALSE;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001563 int opt_p_flags;
1564 char_u *tofree = NULL;
Bram Moolenaar92c33eb2021-12-07 11:03:39 +00001565 char_u numbuf[NUMBUFLEN];
1566
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001567 c1 = *p;
1568 *p = NUL;
1569
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001570 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags,
1571 scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001572 if ((opt_type == gov_bool
1573 || opt_type == gov_number
1574 || opt_type == gov_hidden_bool
1575 || opt_type == gov_hidden_number)
1576 && (tv->v_type != VAR_STRING || !in_vim9script()))
1577 {
1578 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1579 // bool, possibly hidden
1580 n = (long)tv_get_bool(tv);
1581 else
1582 // number, possibly hidden
1583 n = (long)tv_get_number(tv);
1584 }
1585
Bram Moolenaaref082e12021-12-12 21:02:03 +00001586 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001587 || tv->v_type == VAR_FUNC))
1588 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001589 // If the option can be set to a function reference or a lambda
1590 // and the passed value is a function reference, then convert it to
1591 // the name (string) of the function reference.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001592 s = tv2string(tv, &tofree, numbuf, 0);
1593 }
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001594 // Avoid setting a string option to the text "v:false" or similar.
1595 // In Vim9 script also don't convert a number to string.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001596 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001597 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1598 s = tv_get_string_chk(tv);
1599
1600 if (op != NULL && *op != '=')
1601 {
1602 if (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1603 || (opt_type == gov_string && *op != '.'))
1604 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001605 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001606 failed = TRUE; // don't set the value
1607
1608 }
1609 else
1610 {
1611 // number, in legacy script also bool
1612 if (opt_type == gov_number
1613 || (opt_type == gov_bool && !in_vim9script()))
1614 {
1615 switch (*op)
1616 {
1617 case '+': n = numval + n; break;
1618 case '-': n = numval - n; break;
1619 case '*': n = numval * n; break;
1620 case '/': n = (long)num_divide(numval, n,
1621 &failed); break;
1622 case '%': n = (long)num_modulus(numval, n,
1623 &failed); break;
1624 }
1625 s = NULL;
1626 }
1627 else if (opt_type == gov_string
1628 && stringval != NULL && s != NULL)
1629 {
1630 // string
1631 s = concat_str(stringval, s);
1632 vim_free(stringval);
1633 stringval = s;
1634 }
1635 }
1636 }
1637
1638 if (!failed)
1639 {
1640 if (opt_type != gov_string || s != NULL)
1641 {
Bram Moolenaar8ccbbeb2022-03-02 19:49:38 +00001642 char *err = set_option_value(arg, n, s, scope);
1643
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001644 arg_end = p;
Bram Moolenaar8ccbbeb2022-03-02 19:49:38 +00001645 if (err != NULL)
1646 emsg(_(err));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001647 }
1648 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001649 emsg(_(e_string_required));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001650 }
1651 *p = c1;
1652 vim_free(stringval);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001653 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001654 }
1655 return arg_end;
1656}
1657
1658/*
1659 * Set a register, part of ex_let_one().
1660 */
1661 static char_u *
1662ex_let_register(
1663 char_u *arg,
1664 typval_T *tv,
1665 int flags,
1666 char_u *endchars,
1667 char_u *op)
1668{
1669 char_u *arg_end = NULL;
1670
1671 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1672 && (flags & ASSIGN_FOR_LOOP) == 0)
1673 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001674 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001675 return NULL;
1676 }
1677 ++arg;
1678 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001679 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001680 else if (endchars != NULL
1681 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1682 emsg(_(e_unexpected_characters_in_let));
1683 else
1684 {
1685 char_u *ptofree = NULL;
1686 char_u *p;
1687
1688 p = tv_get_string_chk(tv);
1689 if (p != NULL && op != NULL && *op == '.')
1690 {
1691 char_u *s = get_reg_contents(*arg == '@'
1692 ? '"' : *arg, GREG_EXPR_SRC);
1693
1694 if (s != NULL)
1695 {
1696 p = ptofree = concat_str(s, p);
1697 vim_free(s);
1698 }
1699 }
1700 if (p != NULL)
1701 {
1702 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1703 arg_end = arg + 1;
1704 }
1705 vim_free(ptofree);
1706 }
1707 return arg_end;
1708}
1709
1710/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001711 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1712 * Returns a pointer to the char just after the var name.
1713 * Returns NULL if there is an error.
1714 */
1715 static char_u *
1716ex_let_one(
1717 char_u *arg, // points to variable name
1718 typval_T *tv, // value to assign to variable
1719 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001720 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001721 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001722 char_u *op, // "+", "-", "." or NULL
1723 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001724{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001725 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001726
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001727 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001728 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001729 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1730 {
1731 vim9_declare_error(arg);
1732 return NULL;
1733 }
1734
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001735 if (*arg == '$')
1736 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001737 // ":let $VAR = expr": Set environment variable.
1738 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001739 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001740 else if (*arg == '&')
1741 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001742 // ":let &option = expr": Set option value.
1743 // ":let &l:option = expr": Set local option value.
1744 // ":let &g:option = expr": Set global option value.
1745 // ":for &ts in range(8)": Set option value for for loop
1746 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001747 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001748 else if (*arg == '@')
1749 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001750 // ":let @r = expr": Set register contents.
1751 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001752 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001753 else if (eval_isnamec1(*arg) || *arg == '{')
1754 {
1755 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001756 char_u *p;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001757 int lval_flags = (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1758 ? GLV_NO_DECL : 0;
1759 if (op != NULL && *op != '=')
1760 lval_flags |= GLV_ASSIGN_WITH_OP;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001761
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001762 // ":let var = expr": Set internal variable.
1763 // ":let var: type = expr": Set internal variable with type.
1764 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001765 p = get_lval(arg, tv, &lv, FALSE, FALSE, lval_flags, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001766 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001767 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 if (endchars != NULL && vim_strchr(endchars,
1769 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001770 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001771 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001772 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001773 else
1774 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001775 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001776 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001777 }
1778 }
1779 clear_lval(&lv);
1780 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001781 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001782 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001783
1784 return arg_end;
1785}
1786
1787/*
1788 * ":unlet[!] var1 ... " command.
1789 */
1790 void
1791ex_unlet(exarg_T *eap)
1792{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001793 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001794}
1795
1796/*
1797 * ":lockvar" and ":unlockvar" commands
1798 */
1799 void
1800ex_lockvar(exarg_T *eap)
1801{
1802 char_u *arg = eap->arg;
1803 int deep = 2;
1804
1805 if (eap->forceit)
1806 deep = -1;
1807 else if (vim_isdigit(*arg))
1808 {
1809 deep = getdigits(&arg);
1810 arg = skipwhite(arg);
1811 }
1812
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001813 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001814}
1815
1816/*
1817 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001818 * Also used for Vim9 script. "callback" is invoked as:
1819 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001820 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001821 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001822ex_unletlock(
1823 exarg_T *eap,
1824 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001825 int deep,
1826 int glv_flags,
1827 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1828 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001829{
1830 char_u *arg = argstart;
1831 char_u *name_end;
1832 int error = FALSE;
1833 lval_T lv;
1834
1835 do
1836 {
1837 if (*arg == '$')
1838 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001839 lv.ll_name = arg;
1840 lv.ll_tv = NULL;
1841 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001842 if (get_env_len(&arg) == 0)
1843 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001844 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001845 return;
1846 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001847 if (!error && !eap->skip
1848 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1849 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001850 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001851 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001852 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001853 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001854 // Parse the name and find the end.
1855 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001856 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001857 if (lv.ll_name == NULL)
1858 error = TRUE; // error but continue parsing
1859 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1860 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001861 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001862 if (name_end != NULL)
1863 {
1864 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00001865 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001866 }
1867 if (!(eap->skip || error))
1868 clear_lval(&lv);
1869 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001870 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001871
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001872 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001873 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001874 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001875
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001876 if (!eap->skip)
1877 clear_lval(&lv);
1878 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001879
1880 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001881 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001882
Bram Moolenaar63b91732021-08-05 20:40:03 +02001883 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001884}
1885
1886 static int
1887do_unlet_var(
1888 lval_T *lp,
1889 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001890 exarg_T *eap,
1891 int deep UNUSED,
1892 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001893{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001894 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001895 int ret = OK;
1896 int cc;
1897
1898 if (lp->ll_tv == NULL)
1899 {
1900 cc = *name_end;
1901 *name_end = NUL;
1902
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001903 // Environment variable, normal name or expanded name.
1904 if (*lp->ll_name == '$')
LemonBoy77142312022-04-15 20:50:46 +01001905 vim_unsetenv_ext(lp->ll_name + 1);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001906 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001907 ret = FAIL;
1908 *name_end = cc;
1909 }
1910 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001911 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001912 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001913 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001914 return FAIL;
1915 else if (lp->ll_range)
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00001916 list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_n1,
1917 !lp->ll_empty2, lp->ll_n2);
1918 else if (lp->ll_list != NULL)
1919 // unlet a List item.
1920 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001921 else
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00001922 // unlet a Dictionary item.
1923 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001924
1925 return ret;
1926}
1927
1928/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001929 * Unlet one item or a range of items from a list.
1930 * Return OK or FAIL.
1931 */
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00001932 void
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001933list_unlet_range(
1934 list_T *l,
1935 listitem_T *li_first,
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001936 long n1_arg,
1937 int has_n2,
1938 long n2)
1939{
1940 listitem_T *li = li_first;
1941 int n1 = n1_arg;
1942
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001943 // Delete a range of List items.
1944 li = li_first;
1945 n1 = n1_arg;
1946 while (li != NULL && (!has_n2 || n2 >= n1))
1947 {
1948 listitem_T *next = li->li_next;
1949
1950 listitem_remove(l, li);
1951 li = next;
1952 ++n1;
1953 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001954}
1955/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001956 * "unlet" a variable. Return OK if it existed, FAIL if not.
1957 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1958 */
1959 int
1960do_unlet(char_u *name, int forceit)
1961{
1962 hashtab_T *ht;
1963 hashitem_T *hi;
1964 char_u *varname;
1965 dict_T *d;
1966 dictitem_T *di;
1967
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001968 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001969 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001970 return FAIL;
1971
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001972 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001973
1974 // can't :unlet a script variable in Vim9 script from a function
1975 if (ht == get_script_local_ht()
1976 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1977 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1978 == SCRIPT_VERSION_VIM9
1979 && check_vim9_unlet(name) == FAIL)
1980 return FAIL;
1981
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001982 if (ht != NULL && *varname != NUL)
1983 {
1984 d = get_current_funccal_dict(ht);
1985 if (d == NULL)
1986 {
1987 if (ht == &globvarht)
1988 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001989 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001990 d = &vimvardict;
1991 else
1992 {
1993 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1994 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1995 }
1996 if (d == NULL)
1997 {
1998 internal_error("do_unlet()");
1999 return FAIL;
2000 }
2001 }
2002 hi = hash_find(ht, varname);
2003 if (HASHITEM_EMPTY(hi))
2004 hi = find_hi_in_scoped_ht(name, &ht);
2005 if (hi != NULL && !HASHITEM_EMPTY(hi))
2006 {
2007 di = HI2DI(hi);
2008 if (var_check_fixed(di->di_flags, name, FALSE)
2009 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02002010 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002011 return FAIL;
2012
2013 delete_var(ht, hi);
2014 return OK;
2015 }
2016 }
2017 if (forceit)
2018 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00002019 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002020 return FAIL;
2021}
2022
2023/*
2024 * Lock or unlock variable indicated by "lp".
2025 * "deep" is the levels to go (-1 for unlimited);
2026 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2027 */
2028 static int
2029do_lock_var(
2030 lval_T *lp,
2031 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002032 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002033 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002034 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002035{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002036 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002037 int ret = OK;
2038 int cc;
2039 dictitem_T *di;
2040
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002041 if (lp->ll_tv == NULL)
2042 {
2043 cc = *name_end;
2044 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002045 if (*lp->ll_name == '$')
2046 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002047 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002048 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002049 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002050 else
2051 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002052 // Normal name or expanded name.
2053 di = find_var(lp->ll_name, NULL, TRUE);
2054 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002055 {
2056 if (in_vim9script())
2057 semsg(_(e_cannot_find_variable_to_unlock_str),
2058 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002059 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002060 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002061 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002062 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002063 if ((di->di_flags & DI_FLAGS_FIX)
2064 && di->di_tv.v_type != VAR_DICT
2065 && di->di_tv.v_type != VAR_LIST)
2066 {
2067 // For historic reasons this error is not given for a list
2068 // or dict. E.g., the b: dict could be locked/unlocked.
2069 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
2070 ret = FAIL;
2071 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002072 else
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002073 {
2074 if (in_vim9script())
2075 {
2076 svar_T *sv = find_typval_in_script(&di->di_tv,
2077 0, FALSE);
2078
2079 if (sv != NULL && sv->sv_const != 0)
2080 {
2081 semsg(_(e_cannot_change_readonly_variable_str),
2082 lp->ll_name);
2083 ret = FAIL;
2084 }
2085 }
2086
2087 if (ret == OK)
2088 {
2089 if (lock)
2090 di->di_flags |= DI_FLAGS_LOCK;
2091 else
2092 di->di_flags &= ~DI_FLAGS_LOCK;
2093 if (deep != 0)
2094 item_lock(&di->di_tv, deep, lock, FALSE);
2095 }
2096 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002097 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002098 }
2099 *name_end = cc;
2100 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002101 else if (deep == 0)
2102 {
2103 // nothing to do
2104 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002105 else if (lp->ll_range)
2106 {
2107 listitem_T *li = lp->ll_li;
2108
2109 // (un)lock a range of List items.
2110 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2111 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02002112 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002113 li = li->li_next;
2114 ++lp->ll_n1;
2115 }
2116 }
2117 else if (lp->ll_list != NULL)
2118 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002119 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002120 else
2121 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002122 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002123
2124 return ret;
2125}
2126
2127/*
2128 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002129 * When "check_refcount" is TRUE do not lock a list or dict with a reference
2130 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002131 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002132 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02002133item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002134{
2135 static int recurse = 0;
2136 list_T *l;
2137 listitem_T *li;
2138 dict_T *d;
2139 blob_T *b;
2140 hashitem_T *hi;
2141 int todo;
2142
2143 if (recurse >= DICT_MAXNEST)
2144 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002145 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002146 return;
2147 }
2148 if (deep == 0)
2149 return;
2150 ++recurse;
2151
2152 // lock/unlock the item itself
2153 if (lock)
2154 tv->v_lock |= VAR_LOCKED;
2155 else
2156 tv->v_lock &= ~VAR_LOCKED;
2157
2158 switch (tv->v_type)
2159 {
2160 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002161 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002163 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002165 case VAR_STRING:
2166 case VAR_FUNC:
2167 case VAR_PARTIAL:
2168 case VAR_FLOAT:
2169 case VAR_SPECIAL:
2170 case VAR_JOB:
2171 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002172 case VAR_INSTR:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002173 break;
2174
2175 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002176 if ((b = tv->vval.v_blob) != NULL
2177 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002178 {
2179 if (lock)
2180 b->bv_lock |= VAR_LOCKED;
2181 else
2182 b->bv_lock &= ~VAR_LOCKED;
2183 }
2184 break;
2185 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002186 if ((l = tv->vval.v_list) != NULL
2187 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002188 {
2189 if (lock)
2190 l->lv_lock |= VAR_LOCKED;
2191 else
2192 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002193 if (deep < 0 || deep > 1)
2194 {
2195 if (l->lv_first == &range_list_item)
2196 l->lv_lock |= VAR_ITEMS_LOCKED;
2197 else
2198 {
2199 // recursive: lock/unlock the items the List contains
2200 CHECK_LIST_MATERIALIZE(l);
2201 FOR_ALL_LIST_ITEMS(l, li) item_lock(&li->li_tv,
2202 deep - 1, lock, check_refcount);
2203 }
2204 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002205 }
2206 break;
2207 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002208 if ((d = tv->vval.v_dict) != NULL
2209 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002210 {
2211 if (lock)
2212 d->dv_lock |= VAR_LOCKED;
2213 else
2214 d->dv_lock &= ~VAR_LOCKED;
2215 if (deep < 0 || deep > 1)
2216 {
2217 // recursive: lock/unlock the items the List contains
2218 todo = (int)d->dv_hashtab.ht_used;
2219 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2220 {
2221 if (!HASHITEM_EMPTY(hi))
2222 {
2223 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002224 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2225 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002226 }
2227 }
2228 }
2229 }
2230 }
2231 --recurse;
2232}
2233
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002234#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2235/*
2236 * Delete all "menutrans_" variables.
2237 */
2238 void
2239del_menutrans_vars(void)
2240{
2241 hashitem_T *hi;
2242 int todo;
2243
2244 hash_lock(&globvarht);
2245 todo = (int)globvarht.ht_used;
2246 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2247 {
2248 if (!HASHITEM_EMPTY(hi))
2249 {
2250 --todo;
2251 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2252 delete_var(&globvarht, hi);
2253 }
2254 }
2255 hash_unlock(&globvarht);
2256}
2257#endif
2258
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002259/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002260 * Local string buffer for the next two functions to store a variable name
2261 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2262 * get_user_var_name().
2263 */
2264
2265static char_u *varnamebuf = NULL;
2266static int varnamebuflen = 0;
2267
2268/*
2269 * Function to concatenate a prefix and a variable name.
2270 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002271 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002272cat_prefix_varname(int prefix, char_u *name)
2273{
2274 int len;
2275
2276 len = (int)STRLEN(name) + 3;
2277 if (len > varnamebuflen)
2278 {
2279 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002280 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002281 varnamebuf = alloc(len);
2282 if (varnamebuf == NULL)
2283 {
2284 varnamebuflen = 0;
2285 return NULL;
2286 }
2287 varnamebuflen = len;
2288 }
2289 *varnamebuf = prefix;
2290 varnamebuf[1] = ':';
2291 STRCPY(varnamebuf + 2, name);
2292 return varnamebuf;
2293}
2294
2295/*
2296 * Function given to ExpandGeneric() to obtain the list of user defined
2297 * (global/buffer/window/built-in) variable names.
2298 */
2299 char_u *
2300get_user_var_name(expand_T *xp, int idx)
2301{
2302 static long_u gdone;
2303 static long_u bdone;
2304 static long_u wdone;
2305 static long_u tdone;
2306 static int vidx;
2307 static hashitem_T *hi;
2308 hashtab_T *ht;
2309
2310 if (idx == 0)
2311 {
2312 gdone = bdone = wdone = vidx = 0;
2313 tdone = 0;
2314 }
2315
2316 // Global variables
2317 if (gdone < globvarht.ht_used)
2318 {
2319 if (gdone++ == 0)
2320 hi = globvarht.ht_array;
2321 else
2322 ++hi;
2323 while (HASHITEM_EMPTY(hi))
2324 ++hi;
2325 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2326 return cat_prefix_varname('g', hi->hi_key);
2327 return hi->hi_key;
2328 }
2329
2330 // b: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002331 ht = &prevwin_curwin()->w_buffer->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002332 if (bdone < ht->ht_used)
2333 {
2334 if (bdone++ == 0)
2335 hi = ht->ht_array;
2336 else
2337 ++hi;
2338 while (HASHITEM_EMPTY(hi))
2339 ++hi;
2340 return cat_prefix_varname('b', hi->hi_key);
2341 }
2342
2343 // w: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002344 ht = &prevwin_curwin()->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002345 if (wdone < ht->ht_used)
2346 {
2347 if (wdone++ == 0)
2348 hi = ht->ht_array;
2349 else
2350 ++hi;
2351 while (HASHITEM_EMPTY(hi))
2352 ++hi;
2353 return cat_prefix_varname('w', hi->hi_key);
2354 }
2355
2356 // t: variables
2357 ht = &curtab->tp_vars->dv_hashtab;
2358 if (tdone < ht->ht_used)
2359 {
2360 if (tdone++ == 0)
2361 hi = ht->ht_array;
2362 else
2363 ++hi;
2364 while (HASHITEM_EMPTY(hi))
2365 ++hi;
2366 return cat_prefix_varname('t', hi->hi_key);
2367 }
2368
2369 // v: variables
2370 if (vidx < VV_LEN)
2371 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2372
2373 VIM_CLEAR(varnamebuf);
2374 varnamebuflen = 0;
2375 return NULL;
2376}
2377
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002378 char *
2379get_var_special_name(int nr)
2380{
2381 switch (nr)
2382 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002383 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2384 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002385 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002386 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002387 }
2388 internal_error("get_var_special_name()");
2389 return "42";
2390}
2391
2392/*
2393 * Returns the global variable dictionary
2394 */
2395 dict_T *
2396get_globvar_dict(void)
2397{
2398 return &globvardict;
2399}
2400
2401/*
2402 * Returns the global variable hash table
2403 */
2404 hashtab_T *
2405get_globvar_ht(void)
2406{
2407 return &globvarht;
2408}
2409
2410/*
2411 * Returns the v: variable dictionary
2412 */
2413 dict_T *
2414get_vimvar_dict(void)
2415{
2416 return &vimvardict;
2417}
2418
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002419/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002421 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002422 */
2423 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002424find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002426 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2427 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428
2429 if (di == NULL)
2430 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002431 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2433 return (int)(vv - vimvars);
2434}
2435
2436
2437/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002438 * Set type of v: variable to "type".
2439 */
2440 void
2441set_vim_var_type(int idx, vartype_T type)
2442{
Bram Moolenaard787e402021-12-24 21:36:12 +00002443 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002444}
2445
2446/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002447 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002448 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002449 */
2450 void
2451set_vim_var_nr(int idx, varnumber_T val)
2452{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002453 vimvars[idx].vv_nr = val;
2454}
2455
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456 char *
2457get_vim_var_name(int idx)
2458{
2459 return vimvars[idx].vv_name;
2460}
2461
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002462/*
2463 * Get typval_T v: variable value.
2464 */
2465 typval_T *
2466get_vim_var_tv(int idx)
2467{
2468 return &vimvars[idx].vv_tv;
2469}
2470
Bram Moolenaard787e402021-12-24 21:36:12 +00002471 type_T *
2472get_vim_var_type(int idx, garray_T *type_list)
2473{
2474 if (vimvars[idx].vv_type != NULL)
2475 return vimvars[idx].vv_type;
2476 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2477}
2478
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002479/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002480 * Set v: variable to "tv". Only accepts the same type.
2481 * Takes over the value of "tv".
2482 */
2483 int
2484set_vim_var_tv(int idx, typval_T *tv)
2485{
Bram Moolenaard787e402021-12-24 21:36:12 +00002486 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002487 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002488 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002489 clear_tv(tv);
2490 return FAIL;
2491 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002492 // VV_RO is also checked when compiling, but let's check here as well.
2493 if (vimvars[idx].vv_flags & VV_RO)
2494 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002495 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002496 return FAIL;
2497 }
2498 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2499 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002500 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002501 return FAIL;
2502 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002503 clear_tv(&vimvars[idx].vv_di.di_tv);
2504 vimvars[idx].vv_di.di_tv = *tv;
2505 return OK;
2506}
2507
2508/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002509 * Get number v: variable value.
2510 */
2511 varnumber_T
2512get_vim_var_nr(int idx)
2513{
2514 return vimvars[idx].vv_nr;
2515}
2516
2517/*
2518 * Get string v: variable value. Uses a static buffer, can only be used once.
2519 * If the String variable has never been set, return an empty string.
2520 * Never returns NULL;
2521 */
2522 char_u *
2523get_vim_var_str(int idx)
2524{
2525 return tv_get_string(&vimvars[idx].vv_tv);
2526}
2527
2528/*
2529 * Get List v: variable value. Caller must take care of reference count when
2530 * needed.
2531 */
2532 list_T *
2533get_vim_var_list(int idx)
2534{
2535 return vimvars[idx].vv_list;
2536}
2537
2538/*
2539 * Get Dict v: variable value. Caller must take care of reference count when
2540 * needed.
2541 */
2542 dict_T *
2543get_vim_var_dict(int idx)
2544{
2545 return vimvars[idx].vv_dict;
2546}
2547
2548/*
2549 * Set v:char to character "c".
2550 */
2551 void
2552set_vim_var_char(int c)
2553{
2554 char_u buf[MB_MAXBYTES + 1];
2555
2556 if (has_mbyte)
2557 buf[(*mb_char2bytes)(c, buf)] = NUL;
2558 else
2559 {
2560 buf[0] = c;
2561 buf[1] = NUL;
2562 }
2563 set_vim_var_string(VV_CHAR, buf, -1);
2564}
2565
2566/*
2567 * Set v:count to "count" and v:count1 to "count1".
2568 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2569 */
2570 void
2571set_vcount(
2572 long count,
2573 long count1,
2574 int set_prevcount)
2575{
2576 if (set_prevcount)
2577 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2578 vimvars[VV_COUNT].vv_nr = count;
2579 vimvars[VV_COUNT1].vv_nr = count1;
2580}
2581
2582/*
2583 * Save variables that might be changed as a side effect. Used when executing
2584 * a timer callback.
2585 */
2586 void
2587save_vimvars(vimvars_save_T *vvsave)
2588{
2589 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2590 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2591 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2592}
2593
2594/*
2595 * Restore variables saved by save_vimvars().
2596 */
2597 void
2598restore_vimvars(vimvars_save_T *vvsave)
2599{
2600 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2601 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2602 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2603}
2604
2605/*
2606 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2607 * value.
2608 */
2609 void
2610set_vim_var_string(
2611 int idx,
2612 char_u *val,
2613 int len) // length of "val" to use or -1 (whole string)
2614{
2615 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002616 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002617 if (val == NULL)
2618 vimvars[idx].vv_str = NULL;
2619 else if (len == -1)
2620 vimvars[idx].vv_str = vim_strsave(val);
2621 else
2622 vimvars[idx].vv_str = vim_strnsave(val, len);
2623}
2624
2625/*
2626 * Set List v: variable to "val".
2627 */
2628 void
2629set_vim_var_list(int idx, list_T *val)
2630{
2631 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002632 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002633 vimvars[idx].vv_list = val;
2634 if (val != NULL)
2635 ++val->lv_refcount;
2636}
2637
2638/*
2639 * Set Dictionary v: variable to "val".
2640 */
2641 void
2642set_vim_var_dict(int idx, dict_T *val)
2643{
2644 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002645 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002646 vimvars[idx].vv_dict = val;
2647 if (val != NULL)
2648 {
2649 ++val->dv_refcount;
2650 dict_set_items_ro(val);
2651 }
2652}
2653
2654/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002655 * Set the v:argv list.
2656 */
2657 void
2658set_argv_var(char **argv, int argc)
2659{
2660 list_T *l = list_alloc();
2661 int i;
2662
2663 if (l == NULL)
2664 getout(1);
2665 l->lv_lock = VAR_FIXED;
2666 for (i = 0; i < argc; ++i)
2667 {
2668 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2669 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002670 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002671 }
2672 set_vim_var_list(VV_ARGV, l);
2673}
2674
2675/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002676 * Reset v:register, taking the 'clipboard' setting into account.
2677 */
2678 void
2679reset_reg_var(void)
2680{
2681 int regname = 0;
2682
2683 // Adjust the register according to 'clipboard', so that when
2684 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2685#ifdef FEAT_CLIPBOARD
2686 adjust_clip_reg(&regname);
2687#endif
2688 set_reg_var(regname);
2689}
2690
2691/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002692 * Set v:register if needed.
2693 */
2694 void
2695set_reg_var(int c)
2696{
2697 char_u regname;
2698
2699 if (c == 0 || c == ' ')
2700 regname = '"';
2701 else
2702 regname = c;
2703 // Avoid free/alloc when the value is already right.
2704 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2705 set_vim_var_string(VV_REG, &regname, 1);
2706}
2707
2708/*
2709 * Get or set v:exception. If "oldval" == NULL, return the current value.
2710 * Otherwise, restore the value to "oldval" and return NULL.
2711 * Must always be called in pairs to save and restore v:exception! Does not
2712 * take care of memory allocations.
2713 */
2714 char_u *
2715v_exception(char_u *oldval)
2716{
2717 if (oldval == NULL)
2718 return vimvars[VV_EXCEPTION].vv_str;
2719
2720 vimvars[VV_EXCEPTION].vv_str = oldval;
2721 return NULL;
2722}
2723
2724/*
2725 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2726 * Otherwise, restore the value to "oldval" and return NULL.
2727 * Must always be called in pairs to save and restore v:throwpoint! Does not
2728 * take care of memory allocations.
2729 */
2730 char_u *
2731v_throwpoint(char_u *oldval)
2732{
2733 if (oldval == NULL)
2734 return vimvars[VV_THROWPOINT].vv_str;
2735
2736 vimvars[VV_THROWPOINT].vv_str = oldval;
2737 return NULL;
2738}
2739
2740/*
2741 * Set v:cmdarg.
2742 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2743 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2744 * Must always be called in pairs!
2745 */
2746 char_u *
2747set_cmdarg(exarg_T *eap, char_u *oldarg)
2748{
2749 char_u *oldval;
2750 char_u *newval;
2751 unsigned len;
2752
2753 oldval = vimvars[VV_CMDARG].vv_str;
2754 if (eap == NULL)
2755 {
2756 vim_free(oldval);
2757 vimvars[VV_CMDARG].vv_str = oldarg;
2758 return NULL;
2759 }
2760
2761 if (eap->force_bin == FORCE_BIN)
2762 len = 6;
2763 else if (eap->force_bin == FORCE_NOBIN)
2764 len = 8;
2765 else
2766 len = 0;
2767
2768 if (eap->read_edit)
2769 len += 7;
2770
2771 if (eap->force_ff != 0)
2772 len += 10; // " ++ff=unix"
2773 if (eap->force_enc != 0)
2774 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2775 if (eap->bad_char != 0)
2776 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2777
2778 newval = alloc(len + 1);
2779 if (newval == NULL)
2780 return NULL;
2781
2782 if (eap->force_bin == FORCE_BIN)
2783 sprintf((char *)newval, " ++bin");
2784 else if (eap->force_bin == FORCE_NOBIN)
2785 sprintf((char *)newval, " ++nobin");
2786 else
2787 *newval = NUL;
2788
2789 if (eap->read_edit)
2790 STRCAT(newval, " ++edit");
2791
2792 if (eap->force_ff != 0)
2793 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2794 eap->force_ff == 'u' ? "unix"
2795 : eap->force_ff == 'd' ? "dos"
2796 : "mac");
2797 if (eap->force_enc != 0)
2798 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2799 eap->cmd + eap->force_enc);
2800 if (eap->bad_char == BAD_KEEP)
2801 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2802 else if (eap->bad_char == BAD_DROP)
2803 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2804 else if (eap->bad_char != 0)
2805 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2806 vimvars[VV_CMDARG].vv_str = newval;
2807 return oldval;
2808}
2809
2810/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002811 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002812 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2813 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002814 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2815 */
2816 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002817eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002818 char_u *name,
2819 int len, // length of "name"
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002820 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002821 typval_T *rettv, // NULL when only checking existence
2822 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002823 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002824{
2825 int ret = OK;
2826 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002827 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002828 hashtab_T *ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002829 int cc;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002830 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002831
2832 // truncate the name, so that we can use strcmp()
2833 cc = name[len];
2834 name[len] = NUL;
2835
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002836 // Check for local variable when debugging.
2837 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002838 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002839 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002840 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2841
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002842 if (v != NULL)
2843 {
2844 tv = &v->di_tv;
2845 if (dip != NULL)
2846 *dip = v;
2847 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002848 else
2849 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002850 }
2851
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002852 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002854 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002855 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2856
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002857 if (sid == 0)
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00002858 import = find_imported(p, 0, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859
2860 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002861 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002863 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002864 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00002865 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02002866 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002867 ht = &SCRIPT_VARS(sid);
2868 if (ht != NULL)
2869 {
2870 dictitem_T *v = find_var_in_ht(ht, 0, name,
2871 flags & EVAL_VAR_NOAUTOLOAD);
2872
2873 if (v != NULL)
2874 {
2875 tv = &v->di_tv;
2876 if (dip != NULL)
2877 *dip = v;
2878 }
2879 else
2880 ht = NULL;
2881 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002882 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002883 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002884 {
2885 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00002886 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002887 ret = FAIL;
2888 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002889 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002890 else
2891 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002892 if (rettv != NULL)
2893 {
2894 rettv->v_type = VAR_ANY;
2895 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
2896 }
2897 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002898 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00002900 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002901 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002902 int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002903 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002904
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002905 // In Vim9 script we can get a function reference by using the
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002906 // function name. For a global non-autoload function "g:" is
2907 // required.
2908 if (ufunc != NULL && (has_g_prefix
2909 || !func_requires_g_prefix(ufunc)))
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002910 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002911 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002912 if (rettv != NULL)
2913 {
2914 rettv->v_type = VAR_FUNC;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002915 if (has_g_prefix)
Bram Moolenaaref082e12021-12-12 21:02:03 +00002916 // Keep the "g:", otherwise script-local may be
2917 // assumed.
2918 rettv->vval.v_string = vim_strsave(name);
2919 else
2920 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002921 if (rettv->vval.v_string != NULL)
2922 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002923 }
2924 }
2925 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 }
2927
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002928 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002929 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002930 if (tv == NULL)
2931 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002932 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002933 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002934 ret = FAIL;
2935 }
2936 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002937 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01002938 svar_T *sv = NULL;
2939 int was_assigned = FALSE;
2940
Bram Moolenaar11005b02021-07-11 20:59:00 +02002941 if (ht != NULL && ht == get_script_local_ht()
2942 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002943 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01002944 sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaarf055d452021-07-08 20:57:24 +02002945 if (sv != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01002946 {
Bram Moolenaarf055d452021-07-08 20:57:24 +02002947 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01002948 was_assigned = sv->sv_flags & SVFLAG_ASSIGNED;
2949 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02002950 }
2951
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01002952 // If a list or dict variable wasn't initialized and has meaningful
2953 // type, do it now. Not for global variables, they are not
2954 // declared.
Bram Moolenaar7a222242022-03-01 19:23:24 +00002955 if (ht != &globvarht)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002956 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01002957 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01002958 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01002959 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02002960 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00002961 tv->vval.v_dict = dict_alloc();
2962 if (tv->vval.v_dict != NULL)
2963 {
2964 ++tv->vval.v_dict->dv_refcount;
2965 tv->vval.v_dict->dv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01002966 if (sv != NULL)
2967 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00002968 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02002969 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01002970 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01002971 && ((type != NULL && !was_assigned)
Bram Moolenaar501f9782022-03-27 16:51:04 +01002972 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02002973 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00002974 tv->vval.v_list = list_alloc();
2975 if (tv->vval.v_list != NULL)
2976 {
2977 ++tv->vval.v_list->lv_refcount;
2978 tv->vval.v_list->lv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01002979 if (sv != NULL)
2980 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00002981 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02002982 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01002983 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01002984 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01002985 || !in_vim9script()))
2986 {
2987 tv->vval.v_blob = blob_alloc();
2988 if (tv->vval.v_blob != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01002989 {
Bram Moolenaar859cc212022-03-28 15:22:35 +01002990 ++tv->vval.v_blob->bv_refcount;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01002991 if (sv != NULL)
2992 sv->sv_flags |= SVFLAG_ASSIGNED;
2993 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01002994 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002995 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002996 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002997 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002998 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002999
3000 name[len] = cc;
3001
3002 return ret;
3003}
3004
3005/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003006 * Check if variable "name[len]" is a local variable or an argument.
3007 * If so, "*eval_lavars_used" is set to TRUE.
3008 */
3009 void
3010check_vars(char_u *name, int len)
3011{
3012 int cc;
3013 char_u *varname;
3014 hashtab_T *ht;
3015
3016 if (eval_lavars_used == NULL)
3017 return;
3018
3019 // truncate the name, so that we can use strcmp()
3020 cc = name[len];
3021 name[len] = NUL;
3022
3023 ht = find_var_ht(name, &varname);
3024 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
3025 {
3026 if (find_var(name, NULL, TRUE) != NULL)
3027 *eval_lavars_used = TRUE;
3028 }
3029
3030 name[len] = cc;
3031}
3032
3033/*
3034 * Find variable "name" in the list of variables.
3035 * Return a pointer to it if found, NULL if not found.
3036 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003037 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003038 */
3039 dictitem_T *
3040find_var(char_u *name, hashtab_T **htp, int no_autoload)
3041{
3042 char_u *varname;
3043 hashtab_T *ht;
3044 dictitem_T *ret = NULL;
3045
3046 ht = find_var_ht(name, &varname);
3047 if (htp != NULL)
3048 *htp = ht;
3049 if (ht == NULL)
3050 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003051 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003052 if (ret != NULL)
3053 return ret;
3054
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003055 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003056 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003057 if (ret != NULL)
3058 return ret;
3059
3060 // in Vim9 script items without a scope can be script-local
3061 if (in_vim9script() && name[0] != NUL && name[1] != ':')
3062 {
3063 ht = get_script_local_ht();
3064 if (ht != NULL)
3065 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003066 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003067 if (ret != NULL)
3068 {
3069 if (htp != NULL)
3070 *htp = ht;
3071 return ret;
3072 }
3073 }
3074 }
3075
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003076 // When using "vim9script autoload" script-local items are prefixed but can
3077 // be used with s:name.
3078 if (SCRIPT_ID_VALID(current_sctx.sc_sid)
3079 && name[0] == 's' && name[1] == ':')
3080 {
3081 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
3082
3083 if (si->sn_autoload_prefix != NULL)
3084 {
3085 char_u *auto_name = concat_str(si->sn_autoload_prefix, name + 2);
3086
3087 if (auto_name != NULL)
3088 {
3089 ht = &globvarht;
3090 ret = find_var_in_ht(ht, *name, auto_name, TRUE);
Bram Moolenaar130f65d2022-01-13 20:39:41 +00003091 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003092 if (ret != NULL)
3093 {
3094 if (htp != NULL)
3095 *htp = ht;
3096 return ret;
3097 }
3098 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003099 }
3100 }
3101
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003102 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003103}
3104
3105/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00003106 * Like find_var() but if the name starts with <SNR>99_ then look in the
3107 * referenced script (used for a funcref).
3108 */
3109 dictitem_T *
3110find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
3111{
3112 if (STRNCMP(name, "<SNR>", 5) == 0 && isdigit(name[5]))
3113 {
3114 char_u *p = name + 5;
3115 int sid = getdigits(&p);
3116
3117 if (SCRIPT_ID_VALID(sid) && *p == '_')
3118 {
3119 hashtab_T *ht = &SCRIPT_VARS(sid);
3120
3121 if (ht != NULL)
3122 {
3123 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
3124
3125 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003126 {
3127 if (htp != NULL)
3128 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00003129 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003130 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00003131 }
3132 }
3133 }
3134
3135 return find_var(name, htp, no_autoload);
3136}
3137
3138/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003139 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02003140 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003141 * Returns NULL if not found.
3142 */
3143 dictitem_T *
3144find_var_in_ht(
3145 hashtab_T *ht,
3146 int htname,
3147 char_u *varname,
3148 int no_autoload)
3149{
3150 hashitem_T *hi;
3151
3152 if (*varname == NUL)
3153 {
3154 // Must be something like "s:", otherwise "ht" would be NULL.
3155 switch (htname)
3156 {
3157 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
3158 case 'g': return &globvars_var;
3159 case 'v': return &vimvars_var;
3160 case 'b': return &curbuf->b_bufvar;
3161 case 'w': return &curwin->w_winvar;
3162 case 't': return &curtab->tp_winvar;
3163 case 'l': return get_funccal_local_var();
3164 case 'a': return get_funccal_args_var();
3165 }
3166 return NULL;
3167 }
3168
3169 hi = hash_find(ht, varname);
3170 if (HASHITEM_EMPTY(hi))
3171 {
3172 // For global variables we may try auto-loading the script. If it
3173 // worked find the variable again. Don't auto-load a script if it was
3174 // loaded already, otherwise it would be loaded every time when
3175 // checking if a function name is a Funcref variable.
3176 if (ht == &globvarht && !no_autoload)
3177 {
3178 // Note: script_autoload() may make "hi" invalid. It must either
3179 // be obtained again or not used.
3180 if (!script_autoload(varname, FALSE) || aborting())
3181 return NULL;
3182 hi = hash_find(ht, varname);
3183 }
3184 if (HASHITEM_EMPTY(hi))
3185 return NULL;
3186 }
3187 return HI2DI(hi);
3188}
3189
3190/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 * Get the script-local hashtab. NULL if not in a script context.
3192 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003193 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194get_script_local_ht(void)
3195{
3196 scid_T sid = current_sctx.sc_sid;
3197
Bram Moolenaare3d46852020-08-29 13:39:17 +02003198 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 return &SCRIPT_VARS(sid);
3200 return NULL;
3201}
3202
3203/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003204 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003205 * When "cmd" is TRUE it must look like a command, a function must be followed
3206 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003207 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003209 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003210lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003211 char_u *name,
3212 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003213 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003214 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215{
3216 hashtab_T *ht = get_script_local_ht();
3217 char_u buffer[30];
3218 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003219 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003221 int is_global = FALSE;
3222 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223
3224 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003225 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 if (len < sizeof(buffer) - 1)
3227 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003228 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 vim_strncpy(buffer, name, len);
3230 p = buffer;
3231 }
3232 else
3233 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003234 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003236 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 }
3238
3239 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003240 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241
3242 // if not script-local, then perhaps imported
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003243 if (res == FAIL && find_imported(p, 0, FALSE) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003244 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 if (p != buffer)
3246 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003247
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003248 // Find a function, so that a following "->" works.
3249 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3250 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003251 if (res != OK)
3252 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003253 p = skipwhite(name + len);
3254
3255 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003256 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003257 // Do not check for an internal function, since it might also be a
3258 // valid command, such as ":split" versus "split()".
3259 // Skip "g:" before a function name.
3260 if (name[0] == 'g' && name[1] == ':')
3261 {
3262 is_global = TRUE;
3263 fname = name + 2;
3264 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003265 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003266 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003267 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003268 }
3269
Bram Moolenaar709664c2020-12-12 14:33:41 +01003270 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271}
3272
3273/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003274 * Find the hashtab used for a variable name.
3275 * Return NULL if the name is not valid.
3276 * Set "varname" to the start of name without ':'.
3277 */
3278 hashtab_T *
3279find_var_ht(char_u *name, char_u **varname)
3280{
3281 hashitem_T *hi;
3282 hashtab_T *ht;
3283
3284 if (name[0] == NUL)
3285 return NULL;
3286 if (name[1] != ':')
3287 {
3288 // The name must not start with a colon or #.
3289 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3290 return NULL;
3291 *varname = name;
3292
3293 // "version" is "v:version" in all scopes if scriptversion < 3.
3294 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003295 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003296 {
3297 hi = hash_find(&compat_hashtab, name);
3298 if (!HASHITEM_EMPTY(hi))
3299 return &compat_hashtab;
3300 }
3301
3302 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 if (ht != NULL)
3304 return ht; // local variable
3305
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003306 // In Vim9 script items at the script level are script-local, except
3307 // for autoload names.
3308 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 {
3310 ht = get_script_local_ht();
3311 if (ht != NULL)
3312 return ht;
3313 }
3314
3315 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003316 }
3317 *varname = name + 2;
3318 if (*name == 'g') // global variable
3319 return &globvarht;
3320 // There must be no ':' or '#' in the rest of the name, unless g: is used
3321 if (vim_strchr(name + 2, ':') != NULL
3322 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3323 return NULL;
3324 if (*name == 'b') // buffer variable
3325 return &curbuf->b_vars->dv_hashtab;
3326 if (*name == 'w') // window variable
3327 return &curwin->w_vars->dv_hashtab;
3328 if (*name == 't') // tab page variable
3329 return &curtab->tp_vars->dv_hashtab;
3330 if (*name == 'v') // v: variable
3331 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003332 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003333 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003335 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336 if (*name == 'a') // a: function argument
3337 return get_funccal_args_ht();
3338 if (*name == 'l') // l: local function variable
3339 return get_funccal_local_ht();
3340 }
3341 if (*name == 's') // script variable
3342 {
3343 ht = get_script_local_ht();
3344 if (ht != NULL)
3345 return ht;
3346 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003347 return NULL;
3348}
3349
3350/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003351 * Get the string value of a (global/local) variable.
3352 * Note: see tv_get_string() for how long the pointer remains valid.
3353 * Returns NULL when it doesn't exist.
3354 */
3355 char_u *
3356get_var_value(char_u *name)
3357{
3358 dictitem_T *v;
3359
3360 v = find_var(name, NULL, FALSE);
3361 if (v == NULL)
3362 return NULL;
3363 return tv_get_string(&v->di_tv);
3364}
3365
3366/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003367 * Allocate a new hashtab for a sourced script. It will be used while
3368 * sourcing this script and when executing functions defined in the script.
3369 */
3370 void
3371new_script_vars(scid_T id)
3372{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003373 scriptvar_T *sv;
3374
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003375 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3376 if (sv == NULL)
3377 return;
3378 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003379 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003380}
3381
3382/*
3383 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3384 * point to it.
3385 */
3386 void
3387init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3388{
3389 hash_init(&dict->dv_hashtab);
3390 dict->dv_lock = 0;
3391 dict->dv_scope = scope;
3392 dict->dv_refcount = DO_NOT_FREE_CNT;
3393 dict->dv_copyID = 0;
3394 dict_var->di_tv.vval.v_dict = dict;
3395 dict_var->di_tv.v_type = VAR_DICT;
3396 dict_var->di_tv.v_lock = VAR_FIXED;
3397 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3398 dict_var->di_key[0] = NUL;
3399}
3400
3401/*
3402 * Unreference a dictionary initialized by init_var_dict().
3403 */
3404 void
3405unref_var_dict(dict_T *dict)
3406{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003407 // Now the dict needs to be freed if no one else is using it, go back to
3408 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003409 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3410 dict_unref(dict);
3411}
3412
3413/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003414 * Clean up a list of internal variables.
3415 * Frees all allocated variables and the value they contain.
3416 * Clears hashtab "ht", does not free it.
3417 */
3418 void
3419vars_clear(hashtab_T *ht)
3420{
3421 vars_clear_ext(ht, TRUE);
3422}
3423
3424/*
3425 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3426 */
3427 void
3428vars_clear_ext(hashtab_T *ht, int free_val)
3429{
3430 int todo;
3431 hashitem_T *hi;
3432 dictitem_T *v;
3433
3434 hash_lock(ht);
3435 todo = (int)ht->ht_used;
3436 for (hi = ht->ht_array; todo > 0; ++hi)
3437 {
3438 if (!HASHITEM_EMPTY(hi))
3439 {
3440 --todo;
3441
3442 // Free the variable. Don't remove it from the hashtab,
3443 // ht_array might change then. hash_clear() takes care of it
3444 // later.
3445 v = HI2DI(hi);
3446 if (free_val)
3447 clear_tv(&v->di_tv);
3448 if (v->di_flags & DI_FLAGS_ALLOC)
3449 vim_free(v);
3450 }
3451 }
3452 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003453 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003454}
3455
3456/*
3457 * Delete a variable from hashtab "ht" at item "hi".
3458 * Clear the variable value and free the dictitem.
3459 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003460 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003461delete_var(hashtab_T *ht, hashitem_T *hi)
3462{
3463 dictitem_T *di = HI2DI(hi);
3464
3465 hash_remove(ht, hi);
3466 clear_tv(&di->di_tv);
3467 vim_free(di);
3468}
3469
3470/*
3471 * List the value of one internal variable.
3472 */
3473 static void
3474list_one_var(dictitem_T *v, char *prefix, int *first)
3475{
3476 char_u *tofree;
3477 char_u *s;
3478 char_u numbuf[NUMBUFLEN];
3479
3480 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3481 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3482 s == NULL ? (char_u *)"" : s, first);
3483 vim_free(tofree);
3484}
3485
3486 static void
3487list_one_var_a(
3488 char *prefix,
3489 char_u *name,
3490 int type,
3491 char_u *string,
3492 int *first) // when TRUE clear rest of screen and set to FALSE
3493{
3494 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3495 msg_start();
3496 msg_puts(prefix);
3497 if (name != NULL) // "a:" vars don't have a name stored
3498 msg_puts((char *)name);
3499 msg_putchar(' ');
3500 msg_advance(22);
3501 if (type == VAR_NUMBER)
3502 msg_putchar('#');
3503 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3504 msg_putchar('*');
3505 else if (type == VAR_LIST)
3506 {
3507 msg_putchar('[');
3508 if (*string == '[')
3509 ++string;
3510 }
3511 else if (type == VAR_DICT)
3512 {
3513 msg_putchar('{');
3514 if (*string == '{')
3515 ++string;
3516 }
3517 else
3518 msg_putchar(' ');
3519
3520 msg_outtrans(string);
3521
3522 if (type == VAR_FUNC || type == VAR_PARTIAL)
3523 msg_puts("()");
3524 if (*first)
3525 {
3526 msg_clr_eos();
3527 *first = FALSE;
3528 }
3529}
3530
3531/*
3532 * Set variable "name" to value in "tv".
3533 * If the variable already exists, the value is updated.
3534 * Otherwise the variable is created.
3535 */
3536 void
3537set_var(
3538 char_u *name,
3539 typval_T *tv,
3540 int copy) // make copy of value in "tv"
3541{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003542 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003543}
3544
3545/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003546 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003547 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003548 * If the variable already exists and "is_const" is FALSE the value is updated.
3549 * Otherwise the variable is created.
3550 */
3551 void
3552set_var_const(
3553 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003554 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003555 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003556 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003557 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003558 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003559 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003560{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003561 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003562 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003563 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 dictitem_T *di;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003565 typval_T *dest_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003566 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003567 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003568 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003570 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003571 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003572 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003573 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003574 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003575
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003576 if (sid != 0)
3577 {
3578 if (SCRIPT_ID_VALID(sid))
3579 ht = &SCRIPT_VARS(sid);
3580 varname = name;
3581 }
3582 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003583 {
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003584 scriptitem_T *si;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003585
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003586 if (in_vim9script() && is_export
3587 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3588 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
Bram Moolenaar0e1574c2022-03-03 17:05:35 +00003589 ->sn_autoload_prefix != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003590 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003591 // In a vim9 autoload script an exported variable is put in the
3592 // global namespace with the autoload prefix.
3593 var_in_autoload = TRUE;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003594 varname = concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003595 if (varname == NULL)
3596 goto failed;
3597 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003598 ht = &globvarht;
3599 }
3600 else
3601 ht = find_var_ht(name, &varname);
3602 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003603 if (ht == NULL || *varname == NUL)
3604 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003605 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003606 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003607 }
Bram Moolenaar54969f42022-02-07 13:56:44 +00003608 is_script_local = ht == get_script_local_ht() || sid != 0
3609 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003610
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003611 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003612 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003613 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003614 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003615 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003616 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003617 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003618 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003619 }
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003620 if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
3621 && vim_strchr((char_u *)"gwbt", name[0]) != NULL)
3622 // Do not make g:var, w:var, b:var or t:var final.
3623 flags &= ~ASSIGN_FINAL;
3624
Bram Moolenaare535db82021-03-31 21:07:24 +02003625 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003626 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3627 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003628 // For "[a, _] = list" the underscore is ignored.
3629 if ((flags & ASSIGN_UNPACK) == 0)
3630 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003631 goto failed;
3632 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003633
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003635
Bram Moolenaar24e93162021-07-18 20:40:33 +02003636 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003637 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003638 imported_T *import = find_imported(varname, 0, FALSE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003639
Bram Moolenaar24e93162021-07-18 20:40:33 +02003640 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003641 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003642 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02003643 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003645 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003646 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003648 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3649 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003650 }
Bram Moolenaar75e27d72022-02-13 13:56:29 +00003651 if (!in_vim9script())
3652 {
3653 semsg(_(e_cannot_create_vim9_script_variable_in_function_str),
3654 name);
3655 goto failed;
3656 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003657 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658
Bram Moolenaar993faa32022-02-21 15:59:11 +00003659 // Search in parent scope which is possible to reference from lambda
3660 if (di == NULL)
3661 di = find_var_in_scoped_ht(name, TRUE);
3662
3663 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
3664 && var_wrong_func_name(name, di == NULL))
3665 goto failed;
3666
3667 if (need_convert_to_bool(type, tv))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003668 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003669 // Destination is a bool and the value is not, but it can be
3670 // converted.
3671 CLEAR_FIELD(bool_tv);
3672 bool_tv.v_type = VAR_BOOL;
3673 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3674 tv = &bool_tv;
3675 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003676
Bram Moolenaar993faa32022-02-21 15:59:11 +00003677 if (di != NULL)
3678 {
3679 // Item already exists. Allowed to replace when reloading.
3680 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003681 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003682 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
3683 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003684 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003685 emsg(_(e_cannot_modify_existing_variable));
3686 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003687 }
3688
Bram Moolenaar993faa32022-02-21 15:59:11 +00003689 if (is_script_local && vim9script
3690 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003691 {
3692 semsg(_(e_redefining_script_item_str), name);
3693 goto failed;
3694 }
3695
Bram Moolenaar993faa32022-02-21 15:59:11 +00003696 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003697 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003698 where_T where = WHERE_INIT;
Bram Moolenaar7a411a32022-04-04 14:58:06 +01003699 svar_T *sv = find_typval_in_script(&di->di_tv, sid, TRUE);
Bram Moolenaar993faa32022-02-21 15:59:11 +00003700
3701 if (sv != NULL)
3702 {
3703 // check the type and adjust to bool if needed
3704 where.wt_index = var_idx;
3705 where.wt_variable = TRUE;
3706 if (check_script_var_type(sv, tv, name, where) == FAIL)
3707 goto failed;
3708 if (type == NULL)
3709 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003710 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003711 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003712 }
3713
Bram Moolenaar993faa32022-02-21 15:59:11 +00003714 if ((flags & ASSIGN_FOR_LOOP) == 0
3715 && var_check_permission(di, name) == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003716 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003717 }
3718 else
3719 {
3720 // can only redefine once
3721 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003722
Bram Moolenaar993faa32022-02-21 15:59:11 +00003723 // A Vim9 script-local variable is also present in sn_all_vars
3724 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003725 if (var_in_vim9script || var_in_autoload)
Bram Moolenaar993faa32022-02-21 15:59:11 +00003726 update_vim9_script_var(FALSE, di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003727 var_in_autoload ? name : di->di_key, flags,
Bram Moolenaar993faa32022-02-21 15:59:11 +00003728 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003729 }
3730
Bram Moolenaar993faa32022-02-21 15:59:11 +00003731 // existing variable, need to clear the value
3732
3733 // Handle setting internal di: variables separately where needed to
3734 // prevent changing the type.
3735 if (ht == &vimvarht)
3736 {
3737 if (di->di_tv.v_type == VAR_STRING)
3738 {
3739 VIM_CLEAR(di->di_tv.vval.v_string);
3740 if (copy || tv->v_type != VAR_STRING)
3741 {
3742 char_u *val = tv_get_string(tv);
3743
3744 // Careful: when assigning to v:errmsg and
3745 // tv_get_string() causes an error message the variable
3746 // will already be set.
3747 if (di->di_tv.vval.v_string == NULL)
3748 di->di_tv.vval.v_string = vim_strsave(val);
3749 }
3750 else
3751 {
3752 // Take over the string to avoid an extra alloc/free.
3753 di->di_tv.vval.v_string = tv->vval.v_string;
3754 tv->vval.v_string = NULL;
3755 }
3756 goto failed;
3757 }
3758 else if (di->di_tv.v_type == VAR_NUMBER)
3759 {
3760 di->di_tv.vval.v_number = tv_get_number(tv);
3761 if (STRCMP(varname, "searchforward") == 0)
3762 set_search_direction(di->di_tv.vval.v_number
3763 ? '/' : '?');
3764#ifdef FEAT_SEARCH_EXTRA
3765 else if (STRCMP(varname, "hlsearch") == 0)
3766 {
3767 no_hlsearch = !di->di_tv.vval.v_number;
3768 redraw_all_later(SOME_VALID);
3769 }
3770#endif
3771 goto failed;
3772 }
3773 else if (di->di_tv.v_type != tv->v_type)
3774 {
3775 semsg(_(e_setting_str_to_value_with_wrong_type), name);
3776 goto failed;
3777 }
3778 }
3779
3780 clear_tv(&di->di_tv);
3781 }
3782 else
3783 {
3784 // Item not found, check if a function already exists.
3785 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
3786 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
3787 {
3788 semsg(_(e_redefining_script_item_str), name);
3789 goto failed;
3790 }
3791
3792 // add a new variable
3793 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3794 {
3795 semsg(_(e_unknown_variable_str), name);
3796 goto failed;
3797 }
3798
3799 // Can't add "v:" or "a:" variable.
3800 if (ht == &vimvarht || ht == get_funccal_args_ht())
3801 {
3802 semsg(_(e_illegal_variable_name_str), name);
3803 goto failed;
3804 }
3805
3806 // Make sure the variable name is valid. In Vim9 script an
3807 // autoload variable must be prefixed with "g:" unless in an
3808 // autoload script.
3809 if (!valid_varname(varname, -1, !vim9script
3810 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
3811 goto failed;
3812
3813 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3814 if (di == NULL)
3815 goto failed;
3816 STRCPY(di->di_key, varname);
3817 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
3818 {
3819 vim_free(di);
3820 goto failed;
3821 }
3822 di->di_flags = DI_FLAGS_ALLOC;
3823 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3824 di->di_flags |= DI_FLAGS_LOCK;
3825
3826 // A Vim9 script-local variable is also added to sn_all_vars and
3827 // sn_var_vals. It may set "type" from "tv".
3828 if (var_in_vim9script || var_in_autoload)
3829 update_vim9_script_var(TRUE, di,
3830 var_in_autoload ? name : di->di_key, flags,
3831 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003832 }
3833
Bram Moolenaar993faa32022-02-21 15:59:11 +00003834 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003835 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003836 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003837 else
3838 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003839 *dest_tv = *tv;
3840 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003841 init_tv(tv);
3842 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003843 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003844
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003845 if (vim9script && type != NULL)
Bram Moolenaar381692b2022-02-02 20:01:27 +00003846 set_tv_type(dest_tv, type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003847
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003848 // ":const var = value" locks the value
3849 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003850 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003851 // Like :lockvar! name: lock the value and what it contains, but only
3852 // if the reference count is up to one. That locks only literal
3853 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003854 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003855
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003856failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003857 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003858 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003859 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003860}
3861
3862/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003863 * Check in this order for backwards compatibility:
3864 * - Whether the variable is read-only
3865 * - Whether the variable value is locked
3866 * - Whether the variable is locked
3867 */
3868 int
3869var_check_permission(dictitem_T *di, char_u *name)
3870{
3871 if (var_check_ro(di->di_flags, name, FALSE)
3872 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3873 || var_check_lock(di->di_flags, name, FALSE))
3874 return FAIL;
3875 return OK;
3876}
3877
3878/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003879 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3880 * Also give an error message.
3881 */
3882 int
3883var_check_ro(int flags, char_u *name, int use_gettext)
3884{
3885 if (flags & DI_FLAGS_RO)
3886 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003887 if (name == NULL)
3888 emsg(_(e_cannot_change_readonly_variable));
3889 else
3890 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02003891 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003892 return TRUE;
3893 }
3894 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3895 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003896 if (name == NULL)
3897 emsg(_(e_cannot_set_variable_in_sandbox));
3898 else
3899 semsg(_(e_cannot_set_variable_in_sandbox_str),
3900 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003901 return TRUE;
3902 }
3903 return FALSE;
3904}
3905
3906/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003907 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3908 * Also give an error message.
3909 */
3910 int
3911var_check_lock(int flags, char_u *name, int use_gettext)
3912{
3913 if (flags & DI_FLAGS_LOCK)
3914 {
3915 semsg(_(e_variable_is_locked_str),
3916 use_gettext ? (char_u *)_(name) : name);
3917 return TRUE;
3918 }
3919 return FALSE;
3920}
3921
3922/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003923 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3924 * Also give an error message.
3925 */
3926 int
3927var_check_fixed(int flags, char_u *name, int use_gettext)
3928{
3929 if (flags & DI_FLAGS_FIX)
3930 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003931 if (name == NULL)
3932 emsg(_(e_cannot_delete_variable));
3933 else
3934 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003935 use_gettext ? (char_u *)_(name) : name);
3936 return TRUE;
3937 }
3938 return FALSE;
3939}
3940
3941/*
3942 * Check if a funcref is assigned to a valid variable name.
3943 * Return TRUE and give an error if not.
3944 */
3945 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003946var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003947 char_u *name, // points to start of variable name
3948 int new_var) // TRUE when creating the variable
3949{
Bram Moolenaar32154662021-03-28 21:14:06 +02003950 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
3951 // the name can be used without the s: prefix.
3952 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
3953 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003954 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3955 ? name[2] : name[0]))
3956 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003957 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003958 return TRUE;
3959 }
3960 // Don't allow hiding a function. When "v" is not NULL we might be
3961 // assigning another function to the same var, the type is checked
3962 // below.
3963 if (new_var && function_exists(name, FALSE))
3964 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003965 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003966 name);
3967 return TRUE;
3968 }
3969 return FALSE;
3970}
3971
3972/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003973 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3974 * value. Also give an error message, using "name" or _("name") when
3975 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003976 */
3977 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003978value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003979{
3980 if (lock & VAR_LOCKED)
3981 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003982 if (name == NULL)
3983 emsg(_(e_value_is_locked));
3984 else
3985 semsg(_(e_value_is_locked_str),
3986 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003987 return TRUE;
3988 }
3989 if (lock & VAR_FIXED)
3990 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003991 if (name == NULL)
3992 emsg(_(e_cannot_change_value));
3993 else
3994 semsg(_(e_cannot_change_value_of_str),
3995 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003996 return TRUE;
3997 }
3998 return FALSE;
3999}
4000
4001/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01004002 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004003 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004004 * Return FALSE and give an error if not.
4005 */
4006 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004007valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004008{
4009 char_u *p;
4010
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004011 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004012 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01004013 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004014 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004015 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004016 return FALSE;
4017 }
4018 return TRUE;
4019}
4020
4021/*
4022 * getwinvar() and gettabwinvar()
4023 */
4024 static void
4025getwinvar(
4026 typval_T *argvars,
4027 typval_T *rettv,
4028 int off) // 1 for gettabwinvar()
4029{
4030 win_T *win;
4031 char_u *varname;
4032 dictitem_T *v;
4033 tabpage_T *tp = NULL;
4034 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004035 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004036 int need_switch_win;
4037
4038 if (off == 1)
4039 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4040 else
4041 tp = curtab;
4042 win = find_win_by_nr(&argvars[off], tp);
4043 varname = tv_get_string_chk(&argvars[off + 1]);
4044 ++emsg_off;
4045
4046 rettv->v_type = VAR_STRING;
4047 rettv->vval.v_string = NULL;
4048
4049 if (win != NULL && varname != NULL)
4050 {
4051 // Set curwin to be our win, temporarily. Also set the tabpage,
4052 // otherwise the window is not valid. Only do this when needed,
4053 // autocommands get blocked.
4054 need_switch_win = !(tp == curtab && win == curwin);
4055 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00004056 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004057 {
4058 if (*varname == '&')
4059 {
4060 if (varname[1] == NUL)
4061 {
4062 // get all window-local options in a dict
4063 dict_T *opts = get_winbuf_options(FALSE);
4064
4065 if (opts != NULL)
4066 {
4067 rettv_dict_set(rettv, opts);
4068 done = TRUE;
4069 }
4070 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004071 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004072 // window-local-option
4073 done = TRUE;
4074 }
4075 else
4076 {
4077 // Look up the variable.
4078 // Let getwinvar({nr}, "") return the "w:" dictionary.
4079 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
4080 varname, FALSE);
4081 if (v != NULL)
4082 {
4083 copy_tv(&v->di_tv, rettv);
4084 done = TRUE;
4085 }
4086 }
4087 }
4088
4089 if (need_switch_win)
4090 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004091 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004092 }
4093
4094 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
4095 // use the default return value
4096 copy_tv(&argvars[off + 2], rettv);
4097
4098 --emsg_off;
4099}
4100
4101/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02004102 * Set option "varname" to the value of "varp" for the current buffer/window.
4103 */
4104 static void
4105set_option_from_tv(char_u *varname, typval_T *varp)
4106{
4107 long numval = 0;
4108 char_u *strval;
4109 char_u nbuf[NUMBUFLEN];
4110 int error = FALSE;
4111
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004112 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004113 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004114 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004115 strval = (char_u *)"0"; // avoid using "false"
4116 }
4117 else
4118 {
4119 if (!in_vim9script() || varp->v_type != VAR_STRING)
4120 numval = (long)tv_get_number_chk(varp, &error);
4121 strval = tv_get_string_buf_chk(varp, nbuf);
4122 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02004123 if (!error && strval != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004124 set_option_value_give_err(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar191929b2020-08-19 21:20:49 +02004125}
4126
4127/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004128 * "setwinvar()" and "settabwinvar()" functions
4129 */
4130 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004131setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004132{
4133 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004134 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004135 int need_switch_win;
4136 char_u *varname, *winvarname;
4137 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004138 tabpage_T *tp = NULL;
4139
4140 if (check_secure())
4141 return;
4142
4143 if (off == 1)
4144 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4145 else
4146 tp = curtab;
4147 win = find_win_by_nr(&argvars[off], tp);
4148 varname = tv_get_string_chk(&argvars[off + 1]);
4149 varp = &argvars[off + 2];
4150
4151 if (win != NULL && varname != NULL && varp != NULL)
4152 {
4153 need_switch_win = !(tp == curtab && win == curwin);
4154 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00004155 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004156 {
4157 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02004158 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004159 else
4160 {
4161 winvarname = alloc(STRLEN(varname) + 3);
4162 if (winvarname != NULL)
4163 {
4164 STRCPY(winvarname, "w:");
4165 STRCPY(winvarname + 2, varname);
4166 set_var(winvarname, varp, TRUE);
4167 vim_free(winvarname);
4168 }
4169 }
4170 }
4171 if (need_switch_win)
Bram Moolenaar18f47402022-01-06 13:24:51 +00004172 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004173 }
4174}
4175
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004176/*
4177 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4178 * v:option_type, and v:option_command.
4179 */
4180 void
4181reset_v_option_vars(void)
4182{
4183 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4184 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4185 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4186 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4187 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4188 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4189}
4190
4191/*
4192 * Add an assert error to v:errors.
4193 */
4194 void
4195assert_error(garray_T *gap)
4196{
4197 struct vimvar *vp = &vimvars[VV_ERRORS];
4198
Bram Moolenaard787e402021-12-24 21:36:12 +00004199 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004200 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004201 set_vim_var_list(VV_ERRORS, list_alloc());
4202 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4203}
4204
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004205 int
4206var_exists(char_u *var)
4207{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004208 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004209 char_u *name;
4210 char_u *tofree;
4211 typval_T tv;
4212 int len = 0;
4213 int n = FALSE;
4214
4215 // get_name_len() takes care of expanding curly braces
4216 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004217 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004218 if (len > 0)
4219 {
4220 if (tofree != NULL)
4221 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004222 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004223 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004224 if (n)
4225 {
4226 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004227 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004228 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4229 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004230 if (n)
4231 clear_tv(&tv);
4232 }
4233 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004234 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004235 n = FALSE;
4236
4237 vim_free(tofree);
4238 return n;
4239}
4240
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004241static lval_T *redir_lval = NULL;
4242#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4243static garray_T redir_ga; // only valid when redir_lval is not NULL
4244static char_u *redir_endp = NULL;
4245static char_u *redir_varname = NULL;
4246
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004247 int
4248alloc_redir_lval(void)
4249{
4250 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4251 if (redir_lval == NULL)
4252 return FAIL;
4253 return OK;
4254}
4255
4256 void
4257clear_redir_lval(void)
4258{
4259 VIM_CLEAR(redir_lval);
4260}
4261
4262 void
4263init_redir_ga(void)
4264{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004265 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004266}
4267
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004268/*
4269 * Start recording command output to a variable
4270 * When "append" is TRUE append to an existing variable.
4271 * Returns OK if successfully completed the setup. FAIL otherwise.
4272 */
4273 int
4274var_redir_start(char_u *name, int append)
4275{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004276 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004277 typval_T tv;
4278
4279 // Catch a bad name early.
4280 if (!eval_isnamec1(*name))
4281 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004282 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004283 return FAIL;
4284 }
4285
4286 // Make a copy of the name, it is used in redir_lval until redir ends.
4287 redir_varname = vim_strsave(name);
4288 if (redir_varname == NULL)
4289 return FAIL;
4290
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004291 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004292 {
4293 var_redir_stop();
4294 return FAIL;
4295 }
4296
4297 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004298 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004299
4300 // Parse the variable name (can be a dict or list entry).
4301 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4302 FNE_CHECK_START);
4303 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4304 {
4305 clear_lval(redir_lval);
4306 if (redir_endp != NULL && *redir_endp != NUL)
4307 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004308 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004309 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004310 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004311 redir_endp = NULL; // don't store a value, only cleanup
4312 var_redir_stop();
4313 return FAIL;
4314 }
4315
4316 // check if we can write to the variable: set it to or append an empty
4317 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004318 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004319 tv.v_type = VAR_STRING;
4320 tv.vval.v_string = (char_u *)"";
4321 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004322 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004323 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004324 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004325 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004326 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004327 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004328 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004329 {
4330 redir_endp = NULL; // don't store a value, only cleanup
4331 var_redir_stop();
4332 return FAIL;
4333 }
4334
4335 return OK;
4336}
4337
4338/*
4339 * Append "value[value_len]" to the variable set by var_redir_start().
4340 * The actual appending is postponed until redirection ends, because the value
4341 * appended may in fact be the string we write to, changing it may cause freed
4342 * memory to be used:
4343 * :redir => foo
4344 * :let foo
4345 * :redir END
4346 */
4347 void
4348var_redir_str(char_u *value, int value_len)
4349{
4350 int len;
4351
4352 if (redir_lval == NULL)
4353 return;
4354
4355 if (value_len == -1)
4356 len = (int)STRLEN(value); // Append the entire string
4357 else
4358 len = value_len; // Append only "value_len" characters
4359
4360 if (ga_grow(&redir_ga, len) == OK)
4361 {
4362 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4363 redir_ga.ga_len += len;
4364 }
4365 else
4366 var_redir_stop();
4367}
4368
4369/*
4370 * Stop redirecting command output to a variable.
4371 * Frees the allocated memory.
4372 */
4373 void
4374var_redir_stop(void)
4375{
4376 typval_T tv;
4377
4378 if (EVALCMD_BUSY)
4379 {
4380 redir_lval = NULL;
4381 return;
4382 }
4383
4384 if (redir_lval != NULL)
4385 {
4386 // If there was no error: assign the text to the variable.
4387 if (redir_endp != NULL)
4388 {
4389 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4390 tv.v_type = VAR_STRING;
4391 tv.vval.v_string = redir_ga.ga_data;
4392 // Call get_lval() again, if it's inside a Dict or List it may
4393 // have changed.
4394 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4395 FALSE, FALSE, 0, FNE_CHECK_START);
4396 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004397 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004398 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004399 clear_lval(redir_lval);
4400 }
4401
4402 // free the collected output
4403 VIM_CLEAR(redir_ga.ga_data);
4404
4405 VIM_CLEAR(redir_lval);
4406 }
4407 VIM_CLEAR(redir_varname);
4408}
4409
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004410/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004411 * Get the collected redirected text and clear redir_ga.
4412 */
4413 char_u *
4414get_clear_redir_ga(void)
4415{
4416 char_u *res;
4417
4418 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4419 res = redir_ga.ga_data;
4420 redir_ga.ga_data = NULL;
4421 return res;
4422}
4423
4424/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004425 * "gettabvar()" function
4426 */
4427 void
4428f_gettabvar(typval_T *argvars, typval_T *rettv)
4429{
Bram Moolenaar18f47402022-01-06 13:24:51 +00004430 switchwin_T switchwin;
4431 tabpage_T *tp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004432 dictitem_T *v;
4433 char_u *varname;
4434 int done = FALSE;
4435
4436 rettv->v_type = VAR_STRING;
4437 rettv->vval.v_string = NULL;
4438
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004439 if (in_vim9script()
4440 && (check_for_number_arg(argvars, 0) == FAIL
4441 || check_for_string_arg(argvars, 1) == FAIL))
4442 return;
4443
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004444 varname = tv_get_string_chk(&argvars[1]);
4445 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4446 if (tp != NULL && varname != NULL)
4447 {
4448 // Set tp to be our tabpage, temporarily. Also set the window to the
4449 // first window in the tabpage, otherwise the window is not valid.
Bram Moolenaar18f47402022-01-06 13:24:51 +00004450 if (switch_win(&switchwin,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004451 tp == curtab || tp->tp_firstwin == NULL ? firstwin
4452 : tp->tp_firstwin, tp, TRUE) == OK)
4453 {
4454 // look up the variable
4455 // Let gettabvar({nr}, "") return the "t:" dictionary.
4456 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
4457 if (v != NULL)
4458 {
4459 copy_tv(&v->di_tv, rettv);
4460 done = TRUE;
4461 }
4462 }
4463
4464 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004465 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004466 }
4467
4468 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4469 copy_tv(&argvars[2], rettv);
4470}
4471
4472/*
4473 * "gettabwinvar()" function
4474 */
4475 void
4476f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4477{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004478 if (in_vim9script()
4479 && (check_for_number_arg(argvars, 0) == FAIL
4480 || check_for_number_arg(argvars, 1) == FAIL
4481 || check_for_string_arg(argvars, 2) == FAIL))
4482 return;
4483
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004484 getwinvar(argvars, rettv, 1);
4485}
4486
4487/*
4488 * "getwinvar()" function
4489 */
4490 void
4491f_getwinvar(typval_T *argvars, typval_T *rettv)
4492{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004493 if (in_vim9script()
4494 && (check_for_number_arg(argvars, 0) == FAIL
4495 || check_for_string_arg(argvars, 1) == FAIL))
4496 return;
4497
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004498 getwinvar(argvars, rettv, 0);
4499}
4500
4501/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004502 * "getbufvar()" function
4503 */
4504 void
4505f_getbufvar(typval_T *argvars, typval_T *rettv)
4506{
4507 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004508 char_u *varname;
4509 dictitem_T *v;
4510 int done = FALSE;
4511
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004512 if (in_vim9script()
4513 && (check_for_buffer_arg(argvars, 0) == FAIL
4514 || check_for_string_arg(argvars, 1) == FAIL))
4515 return;
4516
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004517 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004518 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004519
4520 rettv->v_type = VAR_STRING;
4521 rettv->vval.v_string = NULL;
4522
4523 if (buf != NULL && varname != NULL)
4524 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004525 if (*varname == '&')
4526 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004527 buf_T *save_curbuf = curbuf;
4528
4529 // set curbuf to be our buf, temporarily
4530 curbuf = buf;
4531
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004532 if (varname[1] == NUL)
4533 {
4534 // get all buffer-local options in a dict
4535 dict_T *opts = get_winbuf_options(TRUE);
4536
4537 if (opts != NULL)
4538 {
4539 rettv_dict_set(rettv, opts);
4540 done = TRUE;
4541 }
4542 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004543 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004544 // buffer-local-option
4545 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02004546
4547 // restore previous notion of curbuf
4548 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004549 }
4550 else
4551 {
4552 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02004553 if (*varname == NUL)
4554 // Let getbufvar({nr}, "") return the "b:" dictionary.
4555 v = &buf->b_bufvar;
4556 else
4557 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
4558 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004559 if (v != NULL)
4560 {
4561 copy_tv(&v->di_tv, rettv);
4562 done = TRUE;
4563 }
4564 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004565 }
4566
4567 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4568 // use the default value
4569 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004570}
4571
4572/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004573 * "settabvar()" function
4574 */
4575 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004576f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004577{
4578 tabpage_T *save_curtab;
4579 tabpage_T *tp;
4580 char_u *varname, *tabvarname;
4581 typval_T *varp;
4582
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004583 if (check_secure())
4584 return;
4585
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004586 if (in_vim9script()
4587 && (check_for_number_arg(argvars, 0) == FAIL
4588 || check_for_string_arg(argvars, 1) == FAIL))
4589 return;
4590
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004591 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4592 varname = tv_get_string_chk(&argvars[1]);
4593 varp = &argvars[2];
4594
4595 if (varname != NULL && varp != NULL && tp != NULL)
4596 {
4597 save_curtab = curtab;
4598 goto_tabpage_tp(tp, FALSE, FALSE);
4599
4600 tabvarname = alloc(STRLEN(varname) + 3);
4601 if (tabvarname != NULL)
4602 {
4603 STRCPY(tabvarname, "t:");
4604 STRCPY(tabvarname + 2, varname);
4605 set_var(tabvarname, varp, TRUE);
4606 vim_free(tabvarname);
4607 }
4608
4609 // Restore current tabpage
4610 if (valid_tabpage(save_curtab))
4611 goto_tabpage_tp(save_curtab, FALSE, FALSE);
4612 }
4613}
4614
4615/*
4616 * "settabwinvar()" function
4617 */
4618 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004619f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004620{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004621 if (in_vim9script()
4622 && (check_for_number_arg(argvars, 0) == FAIL
4623 || check_for_number_arg(argvars, 1) == FAIL
4624 || check_for_string_arg(argvars, 2) == FAIL))
4625 return;
4626
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004627 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004628}
4629
4630/*
4631 * "setwinvar()" function
4632 */
4633 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004634f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004635{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004636 if (in_vim9script()
4637 && (check_for_number_arg(argvars, 0) == FAIL
4638 || check_for_string_arg(argvars, 1) == FAIL))
4639 return;
4640
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004641 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004642}
4643
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004644/*
4645 * "setbufvar()" function
4646 */
4647 void
4648f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4649{
4650 buf_T *buf;
4651 char_u *varname, *bufvarname;
4652 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004653
4654 if (check_secure())
4655 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004656
4657 if (in_vim9script()
4658 && (check_for_buffer_arg(argvars, 0) == FAIL
4659 || check_for_string_arg(argvars, 1) == FAIL))
4660 return;
4661
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004662 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004663 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004664 varp = &argvars[2];
4665
4666 if (buf != NULL && varname != NULL && varp != NULL)
4667 {
4668 if (*varname == '&')
4669 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004670 aco_save_T aco;
4671
4672 // set curbuf to be our buf, temporarily
4673 aucmd_prepbuf(&aco, buf);
4674
Bram Moolenaar191929b2020-08-19 21:20:49 +02004675 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004676
4677 // reset notion of buffer
4678 aucmd_restbuf(&aco);
4679 }
4680 else
4681 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004682 bufvarname = alloc(STRLEN(varname) + 3);
4683 if (bufvarname != NULL)
4684 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004685 buf_T *save_curbuf = curbuf;
4686
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004687 curbuf = buf;
4688 STRCPY(bufvarname, "b:");
4689 STRCPY(bufvarname + 2, varname);
4690 set_var(bufvarname, varp, TRUE);
4691 vim_free(bufvarname);
4692 curbuf = save_curbuf;
4693 }
4694 }
4695 }
4696}
4697
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004698/*
4699 * Get a callback from "arg". It can be a Funcref or a function name.
4700 * When "arg" is zero return an empty string.
4701 * "cb_name" is not allocated.
4702 * "cb_name" is set to NULL for an invalid argument.
4703 */
4704 callback_T
4705get_callback(typval_T *arg)
4706{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004707 callback_T res;
4708 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004709
4710 res.cb_free_name = FALSE;
4711 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4712 {
4713 res.cb_partial = arg->vval.v_partial;
4714 ++res.cb_partial->pt_refcount;
4715 res.cb_name = partial_name(res.cb_partial);
4716 }
4717 else
4718 {
4719 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004720 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4721 && isdigit(*arg->vval.v_string))
4722 r = FAIL;
4723 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004724 {
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004725 if (arg->v_type == VAR_STRING)
4726 {
4727 char_u *name;
4728
4729 name = get_scriptlocal_funcname(arg->vval.v_string);
4730 if (name != NULL)
4731 {
4732 vim_free(arg->vval.v_string);
4733 arg->vval.v_string = name;
4734 }
4735 }
4736
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004737 res.cb_name = arg->vval.v_string;
4738 func_ref(res.cb_name);
4739 }
4740 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004741 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004742 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004743 r = FAIL;
4744
4745 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004746 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004747 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004748 res.cb_name = NULL;
4749 }
4750 }
4751 return res;
4752}
4753
4754/*
4755 * Copy a callback into a typval_T.
4756 */
4757 void
4758put_callback(callback_T *cb, typval_T *tv)
4759{
4760 if (cb->cb_partial != NULL)
4761 {
4762 tv->v_type = VAR_PARTIAL;
4763 tv->vval.v_partial = cb->cb_partial;
4764 ++tv->vval.v_partial->pt_refcount;
4765 }
4766 else
4767 {
4768 tv->v_type = VAR_FUNC;
4769 tv->vval.v_string = vim_strsave(cb->cb_name);
4770 func_ref(cb->cb_name);
4771 }
4772}
4773
4774/*
4775 * Make a copy of "src" into "dest", allocating the function name if needed,
4776 * without incrementing the refcount.
4777 */
4778 void
4779set_callback(callback_T *dest, callback_T *src)
4780{
4781 if (src->cb_partial == NULL)
4782 {
4783 // just a function name, make a copy
4784 dest->cb_name = vim_strsave(src->cb_name);
4785 dest->cb_free_name = TRUE;
4786 }
4787 else
4788 {
4789 // cb_name is a pointer into cb_partial
4790 dest->cb_name = src->cb_name;
4791 dest->cb_free_name = FALSE;
4792 }
4793 dest->cb_partial = src->cb_partial;
4794}
4795
4796/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004797 * Copy callback from "src" to "dest", incrementing the refcounts.
4798 */
4799 void
4800copy_callback(callback_T *dest, callback_T *src)
4801{
4802 dest->cb_partial = src->cb_partial;
4803 if (dest->cb_partial != NULL)
4804 {
4805 dest->cb_name = src->cb_name;
4806 dest->cb_free_name = FALSE;
4807 ++dest->cb_partial->pt_refcount;
4808 }
4809 else
4810 {
4811 dest->cb_name = vim_strsave(src->cb_name);
4812 dest->cb_free_name = TRUE;
4813 func_ref(src->cb_name);
4814 }
4815}
4816
4817/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004818 * When a callback refers to an autoload import, change the function name to
4819 * the "path#name" form. Uses the current script context.
4820 * Only works when the name is allocated.
4821 */
4822 void
4823expand_autload_callback(callback_T *cb)
4824{
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004825 char_u *name;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004826 char_u *p;
4827 imported_T *import;
4828
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004829 if (!in_vim9script() || cb->cb_name == NULL
4830 || (!cb->cb_free_name
4831 && (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004832 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004833 if (cb->cb_partial != NULL)
4834 name = cb->cb_partial->pt_name;
4835 else
4836 name = cb->cb_name;
4837 p = vim_strchr(name, '.');
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004838 if (p == NULL)
4839 return;
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004840 import = find_imported(name, p - name, FALSE);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004841 if (import != NULL && SCRIPT_ID_VALID(import->imp_sid))
4842 {
4843 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
4844
4845 if (si->sn_autoload_prefix != NULL)
4846 {
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004847 char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004848
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004849 if (newname != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004850 {
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004851 if (cb->cb_partial != NULL)
4852 {
4853 if (cb->cb_name == cb->cb_partial->pt_name)
4854 cb->cb_name = newname;
4855 vim_free(cb->cb_partial->pt_name);
4856 cb->cb_partial->pt_name = newname;
4857 }
4858 else
4859 {
4860 vim_free(cb->cb_name);
4861 cb->cb_name = newname;
4862 }
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004863 }
4864 }
4865 }
4866}
4867
4868/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004869 * Unref/free "callback" returned by get_callback() or set_callback().
4870 */
4871 void
4872free_callback(callback_T *callback)
4873{
4874 if (callback->cb_partial != NULL)
4875 {
4876 partial_unref(callback->cb_partial);
4877 callback->cb_partial = NULL;
4878 }
4879 else if (callback->cb_name != NULL)
4880 func_unref(callback->cb_name);
4881 if (callback->cb_free_name)
4882 {
4883 vim_free(callback->cb_name);
4884 callback->cb_free_name = FALSE;
4885 }
4886 callback->cb_name = NULL;
4887}
4888
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004889#endif // FEAT_EVAL