blob: 381bda4954e354cfe723fd76ea607a434bc34508 [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},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200157};
158
159// shorthand
Bram Moolenaard787e402021-12-24 21:36:12 +0000160#define vv_tv_type vv_di.di_tv.v_type
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200161#define vv_nr vv_di.di_tv.vval.v_number
162#define vv_float vv_di.di_tv.vval.v_float
163#define vv_str vv_di.di_tv.vval.v_string
164#define vv_list vv_di.di_tv.vval.v_list
165#define vv_dict vv_di.di_tv.vval.v_dict
166#define vv_blob vv_di.di_tv.vval.v_blob
167#define vv_tv vv_di.di_tv
168
169static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200170static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200171#define vimvarht vimvardict.dv_hashtab
172
173// for VIM_VERSION_ defines
174#include "version.h"
175
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200176static void list_glob_vars(int *first);
177static void list_buf_vars(int *first);
178static void list_win_vars(int *first);
179static void list_tab_vars(int *first);
180static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100181static 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 +0200182static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
183static 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 +0200184static void list_one_var(dictitem_T *v, char *prefix, int *first);
185static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
186
187/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200188 * Initialize global and vim special variables
189 */
190 void
191evalvars_init(void)
192{
193 int i;
194 struct vimvar *p;
195
196 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
197 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
198 vimvardict.dv_lock = VAR_FIXED;
199 hash_init(&compat_hashtab);
200
201 for (i = 0; i < VV_LEN; ++i)
202 {
203 p = &vimvars[i];
204 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
205 {
206 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
207 getout(1);
208 }
209 STRCPY(p->vv_di.di_key, p->vv_name);
210 if (p->vv_flags & VV_RO)
211 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
212 else if (p->vv_flags & VV_RO_SBX)
213 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
214 else
215 p->vv_di.di_flags = DI_FLAGS_FIX;
216
217 // add to v: scope dict, unless the value is not always available
Bram Moolenaard787e402021-12-24 21:36:12 +0000218 if (p->vv_tv_type != VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200219 hash_add(&vimvarht, p->vv_di.di_key);
220 if (p->vv_flags & VV_COMPAT)
221 // add to compat scope dict
222 hash_add(&compat_hashtab, p->vv_di.di_key);
223 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200224 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
225 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200226
227 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
228 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100229 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200230 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
231 set_vim_var_list(VV_ERRORS, list_alloc());
232 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
233
234 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
235 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
236 set_vim_var_nr(VV_NONE, VVAL_NONE);
237 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100238 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
239 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100240 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaar69b30722021-11-02 21:39:49 +0000241 set_vim_var_nr(VV_SIZEOFINT, sizeof(int));
242 set_vim_var_nr(VV_SIZEOFLONG, sizeof(long));
243 set_vim_var_nr(VV_SIZEOFPOINTER, sizeof(char *));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200244
245 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
246 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
247 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
248 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
249 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
250 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
251 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
252 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
253 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
254 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
255 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
256
257 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
258
Drew Vogele30d1022021-10-24 20:35:07 +0100259 set_vim_var_dict(VV_COLORNAMES, dict_alloc());
260
Bram Moolenaar439c0362020-06-06 15:58:03 +0200261 // Default for v:register is not 0 but '"'. This is adjusted once the
262 // clipboard has been setup by calling reset_reg_var().
263 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200264}
265
266#if defined(EXITFREE) || defined(PROTO)
267/*
268 * Free all vim variables information on exit
269 */
270 void
271evalvars_clear(void)
272{
273 int i;
274 struct vimvar *p;
275
276 for (i = 0; i < VV_LEN; ++i)
277 {
278 p = &vimvars[i];
279 if (p->vv_di.di_tv.v_type == VAR_STRING)
280 VIM_CLEAR(p->vv_str);
281 else if (p->vv_di.di_tv.v_type == VAR_LIST)
282 {
283 list_unref(p->vv_list);
284 p->vv_list = NULL;
285 }
286 }
287 hash_clear(&vimvarht);
288 hash_init(&vimvarht); // garbage_collect() will access it
289 hash_clear(&compat_hashtab);
290
291 // global variables
292 vars_clear(&globvarht);
293
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100294 // Script-local variables. Clear all the variables here.
295 // The scriptvar_T is cleared later in free_scriptnames(), because a
296 // variable in one script might hold a reference to the whole scope of
297 // another script.
298 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200299 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200300}
301#endif
302
303 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200304garbage_collect_globvars(int copyID)
305{
306 return set_ref_in_ht(&globvarht, copyID, NULL);
307}
308
309 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200310garbage_collect_vimvars(int copyID)
311{
312 return set_ref_in_ht(&vimvarht, copyID, NULL);
313}
314
315 int
316garbage_collect_scriptvars(int copyID)
317{
Bram Moolenaared234f22020-10-15 20:42:20 +0200318 int i;
319 int idx;
320 int abort = FALSE;
321 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200322
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100323 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200324 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200325 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
326
Bram Moolenaared234f22020-10-15 20:42:20 +0200327 si = SCRIPT_ITEM(i);
328 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
329 {
330 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
331
Bram Moolenaard00a7fb2021-03-08 20:47:14 +0100332 if (sv->sv_name != NULL)
333 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
Bram Moolenaared234f22020-10-15 20:42:20 +0200334 }
335 }
336
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200337 return abort;
338}
339
340/*
341 * Set an internal variable to a string value. Creates the variable if it does
342 * not already exist.
343 */
344 void
345set_internal_string_var(char_u *name, char_u *value)
346{
347 char_u *val;
348 typval_T *tvp;
349
350 val = vim_strsave(value);
351 if (val != NULL)
352 {
353 tvp = alloc_string_tv(val);
354 if (tvp != NULL)
355 {
356 set_var(name, tvp, FALSE);
357 free_tv(tvp);
358 }
359 }
360}
361
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200362 int
363eval_charconvert(
364 char_u *enc_from,
365 char_u *enc_to,
366 char_u *fname_from,
367 char_u *fname_to)
368{
369 int err = FALSE;
370
371 set_vim_var_string(VV_CC_FROM, enc_from, -1);
372 set_vim_var_string(VV_CC_TO, enc_to, -1);
373 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
374 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
375 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
376 err = TRUE;
377 set_vim_var_string(VV_CC_FROM, NULL, -1);
378 set_vim_var_string(VV_CC_TO, NULL, -1);
379 set_vim_var_string(VV_FNAME_IN, NULL, -1);
380 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
381
382 if (err)
383 return FAIL;
384 return OK;
385}
386
387# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
388 int
389eval_printexpr(char_u *fname, char_u *args)
390{
391 int err = FALSE;
392
393 set_vim_var_string(VV_FNAME_IN, fname, -1);
394 set_vim_var_string(VV_CMDARG, args, -1);
395 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
396 err = TRUE;
397 set_vim_var_string(VV_FNAME_IN, NULL, -1);
398 set_vim_var_string(VV_CMDARG, NULL, -1);
399
400 if (err)
401 {
402 mch_remove(fname);
403 return FAIL;
404 }
405 return OK;
406}
407# endif
408
409# if defined(FEAT_DIFF) || defined(PROTO)
410 void
411eval_diff(
412 char_u *origfile,
413 char_u *newfile,
414 char_u *outfile)
415{
416 int err = FALSE;
417
418 set_vim_var_string(VV_FNAME_IN, origfile, -1);
419 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
420 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
421 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
422 set_vim_var_string(VV_FNAME_IN, NULL, -1);
423 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
424 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
425}
426
427 void
428eval_patch(
429 char_u *origfile,
430 char_u *difffile,
431 char_u *outfile)
432{
433 int err;
434
435 set_vim_var_string(VV_FNAME_IN, origfile, -1);
436 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
437 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
438 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
439 set_vim_var_string(VV_FNAME_IN, NULL, -1);
440 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
441 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
442}
443# endif
444
445#if defined(FEAT_SPELL) || defined(PROTO)
446/*
447 * Evaluate an expression to a list with suggestions.
448 * For the "expr:" part of 'spellsuggest'.
449 * Returns NULL when there is an error.
450 */
451 list_T *
452eval_spell_expr(char_u *badword, char_u *expr)
453{
454 typval_T save_val;
455 typval_T rettv;
456 list_T *list = NULL;
457 char_u *p = skipwhite(expr);
458
459 // Set "v:val" to the bad word.
460 prepare_vimvar(VV_VAL, &save_val);
461 set_vim_var_string(VV_VAL, badword, -1);
462 if (p_verbose == 0)
463 ++emsg_off;
464
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200465 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200466 {
467 if (rettv.v_type != VAR_LIST)
468 clear_tv(&rettv);
469 else
470 list = rettv.vval.v_list;
471 }
472
473 if (p_verbose == 0)
474 --emsg_off;
475 clear_tv(get_vim_var_tv(VV_VAL));
476 restore_vimvar(VV_VAL, &save_val);
477
478 return list;
479}
480
481/*
482 * "list" is supposed to contain two items: a word and a number. Return the
483 * word in "pp" and the number as the return value.
484 * Return -1 if anything isn't right.
485 * Used to get the good word and score from the eval_spell_expr() result.
486 */
487 int
488get_spellword(list_T *list, char_u **pp)
489{
490 listitem_T *li;
491
492 li = list->lv_first;
493 if (li == NULL)
494 return -1;
495 *pp = tv_get_string(&li->li_tv);
496
497 li = li->li_next;
498 if (li == NULL)
499 return -1;
500 return (int)tv_get_number(&li->li_tv);
501}
502#endif
503
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200504/*
505 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200506 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200507 * When not used yet add the variable to the v: hashtable.
508 */
509 void
510prepare_vimvar(int idx, typval_T *save_tv)
511{
512 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200513 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaard787e402021-12-24 21:36:12 +0000514 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200515 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
516}
517
518/*
519 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200520 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200521 * When no longer defined, remove the variable from the v: hashtable.
522 */
523 void
524restore_vimvar(int idx, typval_T *save_tv)
525{
526 hashitem_T *hi;
527
528 vimvars[idx].vv_tv = *save_tv;
Bram Moolenaard787e402021-12-24 21:36:12 +0000529 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200530 {
531 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
532 if (HASHITEM_EMPTY(hi))
533 internal_error("restore_vimvar()");
534 else
535 hash_remove(&vimvarht, hi);
536 }
537}
538
539/*
540 * List Vim variables.
541 */
542 static void
543list_vim_vars(int *first)
544{
545 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
546}
547
548/*
549 * List script-local variables, if there is a script.
550 */
551 static void
552list_script_vars(int *first)
553{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200554 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200555 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
556 "s:", FALSE, first);
557}
558
559/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200560 * Get a list of lines from a HERE document. The here document is a list of
561 * lines surrounded by a marker.
562 * cmd << {marker}
563 * {line1}
564 * {line2}
565 * ....
566 * {marker}
567 *
568 * The {marker} is a string. If the optional 'trim' word is supplied before the
569 * marker, then the leading indentation before the lines (matching the
570 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200571 *
572 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
573 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
574 * missing, then '.' is accepted as a marker.
575 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200576 * Returns a List with {lines} or NULL.
577 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100578 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200579heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200580{
581 char_u *theline;
582 char_u *marker;
583 list_T *l;
584 char_u *p;
585 int marker_indent_len = 0;
586 int text_indent_len = 0;
587 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200588 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200589 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200590
591 if (eap->getline == NULL)
592 {
593 emsg(_("E991: cannot use =<< here"));
594 return NULL;
595 }
596
597 // Check for the optional 'trim' word before the marker
598 cmd = skipwhite(cmd);
599 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
600 {
601 cmd = skipwhite(cmd + 4);
602
603 // Trim the indentation from all the lines in the here document.
604 // The amount of indentation trimmed is the same as the indentation of
605 // the first line after the :let command line. To find the end marker
606 // the indent of the :let command line is trimmed.
607 p = *eap->cmdlinep;
608 while (VIM_ISWHITE(*p))
609 {
610 p++;
611 marker_indent_len++;
612 }
613 text_indent_len = -1;
614 }
615
616 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200617 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200618 {
619 marker = skipwhite(cmd);
620 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200621 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200622 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200623 semsg(_(e_trailing_arg), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200624 return NULL;
625 }
626 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200627 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200628 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000629 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200630 return NULL;
631 }
632 }
633 else
634 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200635 // When getting lines for an embedded script, if the marker is missing,
636 // accept '.' as the marker.
637 if (script_get)
638 marker = dot;
639 else
640 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000641 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200642 return NULL;
643 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200644 }
645
646 l = list_alloc();
647 if (l == NULL)
648 return NULL;
649
650 for (;;)
651 {
652 int mi = 0;
653 int ti = 0;
654
655 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
656 if (theline == NULL)
657 {
658 semsg(_("E990: Missing end marker '%s'"), marker);
659 break;
660 }
661
662 // with "trim": skip the indent matching the :let line to find the
663 // marker
664 if (marker_indent_len > 0
665 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
666 mi = marker_indent_len;
667 if (STRCMP(marker, theline + mi) == 0)
668 {
669 vim_free(theline);
670 break;
671 }
672
673 if (text_indent_len == -1 && *theline != NUL)
674 {
675 // set the text indent from the first line.
676 p = theline;
677 text_indent_len = 0;
678 while (VIM_ISWHITE(*p))
679 {
680 p++;
681 text_indent_len++;
682 }
683 text_indent = vim_strnsave(theline, text_indent_len);
684 }
685 // with "trim": skip the indent matching the first line
686 if (text_indent != NULL)
687 for (ti = 0; ti < text_indent_len; ++ti)
688 if (theline[ti] != text_indent[ti])
689 break;
690
691 if (list_append_string(l, theline + ti, -1) == FAIL)
692 break;
693 vim_free(theline);
694 }
695 vim_free(text_indent);
696
697 return l;
698}
699
700/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200701 * Vim9 variable declaration:
702 * ":var name"
703 * ":var name: type"
704 * ":var name = expr"
705 * ":var name: type = expr"
706 * etc.
707 */
708 void
709ex_var(exarg_T *eap)
710{
711 if (!in_vim9script())
712 {
713 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
714 return;
715 }
716 ex_let(eap);
717}
718
719/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200720 * ":let" list all variable values
721 * ":let var1 var2" list variable values
722 * ":let var = expr" assignment command.
723 * ":let var += expr" assignment command.
724 * ":let var -= expr" assignment command.
725 * ":let var *= expr" assignment command.
726 * ":let var /= expr" assignment command.
727 * ":let var %= expr" assignment command.
728 * ":let var .= expr" assignment command.
729 * ":let var ..= expr" assignment command.
730 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100731 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100732 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200733 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200734 * ":final var = expr" assignment command.
735 * ":final [var1, var2] = expr" unpack list.
736 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200737 * ":const" list all variable values
738 * ":const var1 var2" list variable values
739 * ":const var = expr" assignment command.
740 * ":const [var1, var2] = expr" unpack list.
741 */
742 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200743ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200744{
745 char_u *arg = eap->arg;
746 char_u *expr = NULL;
747 typval_T rettv;
748 int i;
749 int var_count = 0;
750 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200751 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200752 char_u *argend;
753 int first = TRUE;
754 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200755 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100756 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200757 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200758
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200759 if (eap->cmdidx == CMD_final && !vim9script)
760 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100761 // In legacy Vim script ":final" is short for ":finally".
762 ex_finally(eap);
763 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200764 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200765 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200766 {
767 emsg(_(e_cannot_use_let_in_vim9_script));
768 return;
769 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200770
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100771 if (eap->cmdidx == CMD_const)
772 flags |= ASSIGN_CONST;
773 else if (eap->cmdidx == CMD_final)
774 flags |= ASSIGN_FINAL;
775
776 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200778 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200780 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200781 if (argend == NULL)
782 return;
783 if (argend > arg && argend[-1] == '.') // for var.='str'
784 --argend;
785 expr = skipwhite(argend);
786 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +0200787 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200788 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200789 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
790 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200791 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200792 {
793 // ":let" without "=": list variables
794 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000795 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200796 else if (expr[0] == '.' && expr[1] == '=')
797 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200798 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200799 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200800 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200801 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100802 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
803 semsg(_(e_trailing_arg), argend);
804 else
805 // Vim9 declaration ":var name: type"
806 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200807 }
808 else
809 {
810 // ":let var1 var2" - list values
811 arg = list_arg_vars(eap, arg, &first);
812 }
813 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200814 else if (!eap->skip)
815 {
816 // ":let"
817 list_glob_vars(&first);
818 list_buf_vars(&first);
819 list_win_vars(&first);
820 list_tab_vars(&first);
821 list_script_vars(&first);
822 list_func_vars(&first);
823 list_vim_vars(&first);
824 }
Bram Moolenaar63b91732021-08-05 20:40:03 +0200825 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200826 }
827 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
828 {
829 list_T *l;
Bram Moolenaar81530e32021-07-28 21:25:49 +0200830 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200831
832 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200833 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200834 if (l != NULL)
835 {
836 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200837 if (!eap->skip)
838 {
Bram Moolenaar81530e32021-07-28 21:25:49 +0200839 // errors are for the assignment, not the end marker
840 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200841 op[0] = '=';
842 op[1] = NUL;
843 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100844 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200845 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200846 clear_tv(&rettv);
847 }
848 }
849 else
850 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200851 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200852 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200853
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200854 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200855 i = FAIL;
856 if (has_assign || concat)
857 {
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100858 int cur_lnum;
859
Bram Moolenaar32e35112020-05-14 22:41:15 +0200860 op[0] = '=';
861 op[1] = NUL;
862 if (*expr != '=')
863 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200864 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200865 {
866 // +=, /=, etc. require an existing variable
867 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
868 i = FAIL;
869 }
870 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200871 {
872 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200873 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200874 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200875 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200876 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200877 ++len;
878 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200879 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200880 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200881 }
882 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200883 ++expr;
884
Bram Moolenaar7f2c3412021-11-29 16:01:49 +0000885 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200886 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200887 {
888 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100889 semsg(_(e_white_space_required_before_and_after_str_at_str),
890 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200891 i = FAIL;
892 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200893
894 if (eap->skip)
895 ++emsg_skip;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200896 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200897 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100898 cur_lnum = SOURCING_LNUM;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200899 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200900 if (eap->skip)
901 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200902 clear_evalarg(&evalarg, eap);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100903
904 // Restore the line number so that any type error is given for the
905 // declaration, not the expression.
906 SOURCING_LNUM = cur_lnum;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200907 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200908 if (eap->skip)
909 {
910 if (i != FAIL)
911 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200912 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200913 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200914 {
915 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200916 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200917 clear_tv(&rettv);
918 }
919 }
920}
921
922/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +0100923 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200924 * Handles both "var" with any type and "[var, var; var]" with a list type.
925 * When "op" is not NULL it points to a string with characters that
926 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
927 * or concatenate.
928 * Returns OK or FAIL;
929 */
930 int
931ex_let_vars(
932 char_u *arg_start,
933 typval_T *tv,
934 int copy, // copy values from "tv", don't move
935 int semicolon, // from skip_var_list()
936 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100937 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200938 char_u *op)
939{
940 char_u *arg = arg_start;
941 list_T *l;
942 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100943 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200944 listitem_T *item;
945 typval_T ltv;
946
947 if (*arg != '[')
948 {
949 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100950 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200951 return FAIL;
952 return OK;
953 }
954
955 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
956 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
957 {
958 emsg(_(e_listreq));
959 return FAIL;
960 }
961
962 i = list_len(l);
963 if (semicolon == 0 && var_count < i)
964 {
965 emsg(_("E687: Less targets than List items"));
966 return FAIL;
967 }
968 if (var_count - semicolon > i)
969 {
970 emsg(_("E688: More targets than List items"));
971 return FAIL;
972 }
973
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200974 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200975 item = l->lv_first;
976 while (*arg != ']')
977 {
978 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100979 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +0200980 arg = ex_let_one(arg, &item->li_tv, TRUE,
981 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200982 item = item->li_next;
983 if (arg == NULL)
984 return FAIL;
985
986 arg = skipwhite(arg);
987 if (*arg == ';')
988 {
989 // Put the rest of the list (may be empty) in the var after ';'.
990 // Create a new list for this.
991 l = list_alloc();
992 if (l == NULL)
993 return FAIL;
994 while (item != NULL)
995 {
996 list_append_tv(l, &item->li_tv);
997 item = item->li_next;
998 }
999
1000 ltv.v_type = VAR_LIST;
1001 ltv.v_lock = 0;
1002 ltv.vval.v_list = l;
1003 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001004 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001005
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001006 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1007 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001008 clear_tv(&ltv);
1009 if (arg == NULL)
1010 return FAIL;
1011 break;
1012 }
1013 else if (*arg != ',' && *arg != ']')
1014 {
1015 internal_error("ex_let_vars()");
1016 return FAIL;
1017 }
1018 }
1019
1020 return OK;
1021}
1022
1023/*
1024 * Skip over assignable variable "var" or list of variables "[var, var]".
1025 * Used for ":let varvar = expr" and ":for varvar in expr".
1026 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001027 * for "[var, var; var]" set "semicolon" to 1.
1028 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001029 * Return NULL for an error.
1030 */
1031 char_u *
1032skip_var_list(
1033 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001034 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001035 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001036 int *semicolon,
1037 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001038{
1039 char_u *p, *s;
1040
1041 if (*arg == '[')
1042 {
1043 // "[var, var]": find the matching ']'.
1044 p = arg;
1045 for (;;)
1046 {
1047 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001048 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001049 if (s == p)
1050 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001051 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001052 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001053 return NULL;
1054 }
1055 ++*var_count;
1056
1057 p = skipwhite(s);
1058 if (*p == ']')
1059 break;
1060 else if (*p == ';')
1061 {
1062 if (*semicolon == 1)
1063 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001064 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001065 return NULL;
1066 }
1067 *semicolon = 1;
1068 }
1069 else if (*p != ',')
1070 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001071 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001072 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001073 return NULL;
1074 }
1075 }
1076 return p + 1;
1077 }
1078 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001079 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001080}
1081
1082/*
1083 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1084 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001085 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001086 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001087 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001089{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001090 char_u *end;
1091 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001092
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001093 if (*arg == '@' && arg[1] != NUL)
1094 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001095 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001096 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001097
1098 // "a: type" is declaring variable "a" with a type, not "a:".
1099 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001100 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001101 --end;
1102
Bram Moolenaar585587d2021-01-17 20:52:13 +01001103 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001104 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001105 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001106 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001107 }
1108 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001109}
1110
1111/*
1112 * List variables for hashtab "ht" with prefix "prefix".
1113 * If "empty" is TRUE also list NULL strings as empty strings.
1114 */
1115 void
1116list_hashtable_vars(
1117 hashtab_T *ht,
1118 char *prefix,
1119 int empty,
1120 int *first)
1121{
1122 hashitem_T *hi;
1123 dictitem_T *di;
1124 int todo;
1125 char_u buf[IOSIZE];
1126
1127 todo = (int)ht->ht_used;
1128 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1129 {
1130 if (!HASHITEM_EMPTY(hi))
1131 {
1132 --todo;
1133 di = HI2DI(hi);
1134
1135 // apply :filter /pat/ to variable name
1136 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1137 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1138 if (message_filtered(buf))
1139 continue;
1140
1141 if (empty || di->di_tv.v_type != VAR_STRING
1142 || di->di_tv.vval.v_string != NULL)
1143 list_one_var(di, prefix, first);
1144 }
1145 }
1146}
1147
1148/*
1149 * List global variables.
1150 */
1151 static void
1152list_glob_vars(int *first)
1153{
1154 list_hashtable_vars(&globvarht, "", TRUE, first);
1155}
1156
1157/*
1158 * List buffer variables.
1159 */
1160 static void
1161list_buf_vars(int *first)
1162{
1163 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1164}
1165
1166/*
1167 * List window variables.
1168 */
1169 static void
1170list_win_vars(int *first)
1171{
1172 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1173}
1174
1175/*
1176 * List tab page variables.
1177 */
1178 static void
1179list_tab_vars(int *first)
1180{
1181 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1182}
1183
1184/*
1185 * List variables in "arg".
1186 */
1187 static char_u *
1188list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1189{
1190 int error = FALSE;
1191 int len;
1192 char_u *name;
1193 char_u *name_start;
1194 char_u *arg_subsc;
1195 char_u *tofree;
1196 typval_T tv;
1197
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001198 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001199 {
1200 if (error || eap->skip)
1201 {
1202 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1203 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1204 {
1205 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001206 if (!did_emsg)
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001207 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001208 break;
1209 }
1210 }
1211 else
1212 {
1213 // get_name_len() takes care of expanding curly braces
1214 name_start = name = arg;
1215 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1216 if (len <= 0)
1217 {
1218 // This is mainly to keep test 49 working: when expanding
1219 // curly braces fails overrule the exception error message.
1220 if (len < 0 && !aborting())
1221 {
1222 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001223 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001224 break;
1225 }
1226 error = TRUE;
1227 }
1228 else
1229 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001230 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001231 if (tofree != NULL)
1232 name = tofree;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001233 if (eval_variable(name, len, &tv, NULL,
1234 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001235 error = TRUE;
1236 else
1237 {
1238 // handle d.key, l[idx], f(expr)
1239 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001240 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001241 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001242 error = TRUE;
1243 else
1244 {
1245 if (arg == arg_subsc && len == 2 && name[1] == ':')
1246 {
1247 switch (*name)
1248 {
1249 case 'g': list_glob_vars(first); break;
1250 case 'b': list_buf_vars(first); break;
1251 case 'w': list_win_vars(first); break;
1252 case 't': list_tab_vars(first); break;
1253 case 'v': list_vim_vars(first); break;
1254 case 's': list_script_vars(first); break;
1255 case 'l': list_func_vars(first); break;
1256 default:
1257 semsg(_("E738: Can't list variables for %s"), name);
1258 }
1259 }
1260 else
1261 {
1262 char_u numbuf[NUMBUFLEN];
1263 char_u *tf;
1264 int c;
1265 char_u *s;
1266
1267 s = echo_string(&tv, &tf, numbuf, 0);
1268 c = *arg;
1269 *arg = NUL;
1270 list_one_var_a("",
1271 arg == arg_subsc ? name : name_start,
1272 tv.v_type,
1273 s == NULL ? (char_u *)"" : s,
1274 first);
1275 *arg = c;
1276 vim_free(tf);
1277 }
1278 clear_tv(&tv);
1279 }
1280 }
1281 }
1282
1283 vim_free(tofree);
1284 }
1285
1286 arg = skipwhite(arg);
1287 }
1288
1289 return arg;
1290}
1291
1292/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001293 * Set an environment variable, part of ex_let_one().
1294 */
1295 static char_u *
1296ex_let_env(
1297 char_u *arg,
1298 typval_T *tv,
1299 int flags,
1300 char_u *endchars,
1301 char_u *op)
1302{
1303 char_u *arg_end = NULL;
1304 char_u *name;
1305 int len;
1306
1307 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1308 && (flags & ASSIGN_FOR_LOOP) == 0)
1309 {
1310 emsg(_("E996: Cannot lock an environment variable"));
1311 return NULL;
1312 }
1313
1314 // Find the end of the name.
1315 ++arg;
1316 name = arg;
1317 len = get_env_len(&arg);
1318 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001319 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001320 else
1321 {
1322 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1323 semsg(_(e_letwrong), op);
1324 else if (endchars != NULL
1325 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1326 emsg(_(e_unexpected_characters_in_let));
1327 else if (!check_secure())
1328 {
1329 char_u *tofree = NULL;
1330 int c1 = name[len];
1331 char_u *p;
1332
1333 name[len] = NUL;
1334 p = tv_get_string_chk(tv);
1335 if (p != NULL && op != NULL && *op == '.')
1336 {
1337 int mustfree = FALSE;
1338 char_u *s = vim_getenv(name, &mustfree);
1339
1340 if (s != NULL)
1341 {
1342 p = tofree = concat_str(s, p);
1343 if (mustfree)
1344 vim_free(s);
1345 }
1346 }
1347 if (p != NULL)
1348 {
1349 vim_setenv_ext(name, p);
1350 arg_end = arg;
1351 }
1352 name[len] = c1;
1353 vim_free(tofree);
1354 }
1355 }
1356 return arg_end;
1357}
1358
1359/*
1360 * Set an option, part of ex_let_one().
1361 */
1362 static char_u *
1363ex_let_option(
1364 char_u *arg,
1365 typval_T *tv,
1366 int flags,
1367 char_u *endchars,
1368 char_u *op)
1369{
1370 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001371 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001372 char_u *arg_end = NULL;
1373
1374 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1375 && (flags & ASSIGN_FOR_LOOP) == 0)
1376 {
1377 emsg(_(e_const_option));
1378 return NULL;
1379 }
1380
1381 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001382 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001383 if (p == NULL || (endchars != NULL
1384 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1385 emsg(_(e_unexpected_characters_in_let));
1386 else
1387 {
1388 int c1;
1389 long n = 0;
1390 getoption_T opt_type;
1391 long numval;
1392 char_u *stringval = NULL;
1393 char_u *s = NULL;
1394 int failed = FALSE;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001395 int opt_p_flags;
1396 char_u *tofree = NULL;
Bram Moolenaar92c33eb2021-12-07 11:03:39 +00001397 char_u numbuf[NUMBUFLEN];
1398
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001399 c1 = *p;
1400 *p = NUL;
1401
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001402 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags,
1403 scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001404 if ((opt_type == gov_bool
1405 || opt_type == gov_number
1406 || opt_type == gov_hidden_bool
1407 || opt_type == gov_hidden_number)
1408 && (tv->v_type != VAR_STRING || !in_vim9script()))
1409 {
1410 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1411 // bool, possibly hidden
1412 n = (long)tv_get_bool(tv);
1413 else
1414 // number, possibly hidden
1415 n = (long)tv_get_number(tv);
1416 }
1417
Bram Moolenaaref082e12021-12-12 21:02:03 +00001418 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001419 || tv->v_type == VAR_FUNC))
1420 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001421 // If the option can be set to a function reference or a lambda
1422 // and the passed value is a function reference, then convert it to
1423 // the name (string) of the function reference.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001424 s = tv2string(tv, &tofree, numbuf, 0);
1425 }
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001426 // Avoid setting a string option to the text "v:false" or similar.
1427 // In Vim9 script also don't convert a number to string.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001428 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001429 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1430 s = tv_get_string_chk(tv);
1431
1432 if (op != NULL && *op != '=')
1433 {
1434 if (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1435 || (opt_type == gov_string && *op != '.'))
1436 {
1437 semsg(_(e_letwrong), op);
1438 failed = TRUE; // don't set the value
1439
1440 }
1441 else
1442 {
1443 // number, in legacy script also bool
1444 if (opt_type == gov_number
1445 || (opt_type == gov_bool && !in_vim9script()))
1446 {
1447 switch (*op)
1448 {
1449 case '+': n = numval + n; break;
1450 case '-': n = numval - n; break;
1451 case '*': n = numval * n; break;
1452 case '/': n = (long)num_divide(numval, n,
1453 &failed); break;
1454 case '%': n = (long)num_modulus(numval, n,
1455 &failed); break;
1456 }
1457 s = NULL;
1458 }
1459 else if (opt_type == gov_string
1460 && stringval != NULL && s != NULL)
1461 {
1462 // string
1463 s = concat_str(stringval, s);
1464 vim_free(stringval);
1465 stringval = s;
1466 }
1467 }
1468 }
1469
1470 if (!failed)
1471 {
1472 if (opt_type != gov_string || s != NULL)
1473 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001474 set_option_value(arg, n, s, scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001475 arg_end = p;
1476 }
1477 else
1478 emsg(_(e_stringreq));
1479 }
1480 *p = c1;
1481 vim_free(stringval);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001482 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001483 }
1484 return arg_end;
1485}
1486
1487/*
1488 * Set a register, part of ex_let_one().
1489 */
1490 static char_u *
1491ex_let_register(
1492 char_u *arg,
1493 typval_T *tv,
1494 int flags,
1495 char_u *endchars,
1496 char_u *op)
1497{
1498 char_u *arg_end = NULL;
1499
1500 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1501 && (flags & ASSIGN_FOR_LOOP) == 0)
1502 {
1503 emsg(_("E996: Cannot lock a register"));
1504 return NULL;
1505 }
1506 ++arg;
1507 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1508 semsg(_(e_letwrong), op);
1509 else if (endchars != NULL
1510 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1511 emsg(_(e_unexpected_characters_in_let));
1512 else
1513 {
1514 char_u *ptofree = NULL;
1515 char_u *p;
1516
1517 p = tv_get_string_chk(tv);
1518 if (p != NULL && op != NULL && *op == '.')
1519 {
1520 char_u *s = get_reg_contents(*arg == '@'
1521 ? '"' : *arg, GREG_EXPR_SRC);
1522
1523 if (s != NULL)
1524 {
1525 p = ptofree = concat_str(s, p);
1526 vim_free(s);
1527 }
1528 }
1529 if (p != NULL)
1530 {
1531 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1532 arg_end = arg + 1;
1533 }
1534 vim_free(ptofree);
1535 }
1536 return arg_end;
1537}
1538
1539/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001540 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1541 * Returns a pointer to the char just after the var name.
1542 * Returns NULL if there is an error.
1543 */
1544 static char_u *
1545ex_let_one(
1546 char_u *arg, // points to variable name
1547 typval_T *tv, // value to assign to variable
1548 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001549 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001550 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001551 char_u *op, // "+", "-", "." or NULL
1552 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001553{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001554 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001555
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001556 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001557 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001558 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1559 {
1560 vim9_declare_error(arg);
1561 return NULL;
1562 }
1563
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001564 if (*arg == '$')
1565 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001566 // ":let $VAR = expr": Set environment variable.
1567 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001568 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001569 else if (*arg == '&')
1570 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001571 // ":let &option = expr": Set option value.
1572 // ":let &l:option = expr": Set local option value.
1573 // ":let &g:option = expr": Set global option value.
1574 // ":for &ts in range(8)": Set option value for for loop
1575 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001576 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001577 else if (*arg == '@')
1578 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001579 // ":let @r = expr": Set register contents.
1580 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001581 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001582 else if (eval_isnamec1(*arg) || *arg == '{')
1583 {
1584 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001585 char_u *p;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001586
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001587 // ":let var = expr": Set internal variable.
1588 // ":let var: type = expr": Set internal variable with type.
1589 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001590 p = get_lval(arg, tv, &lv, FALSE, FALSE,
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001591 (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1592 ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001593 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001594 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595 if (endchars != NULL && vim_strchr(endchars,
1596 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001597 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001598 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001599 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001600 else
1601 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001602 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001603 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001604 }
1605 }
1606 clear_lval(&lv);
1607 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001608 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001609 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001610
1611 return arg_end;
1612}
1613
1614/*
1615 * ":unlet[!] var1 ... " command.
1616 */
1617 void
1618ex_unlet(exarg_T *eap)
1619{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001620 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001621}
1622
1623/*
1624 * ":lockvar" and ":unlockvar" commands
1625 */
1626 void
1627ex_lockvar(exarg_T *eap)
1628{
1629 char_u *arg = eap->arg;
1630 int deep = 2;
1631
1632 if (eap->forceit)
1633 deep = -1;
1634 else if (vim_isdigit(*arg))
1635 {
1636 deep = getdigits(&arg);
1637 arg = skipwhite(arg);
1638 }
1639
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001640 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001641}
1642
1643/*
1644 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001645 * Also used for Vim9 script. "callback" is invoked as:
1646 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001647 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001648 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001649ex_unletlock(
1650 exarg_T *eap,
1651 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001652 int deep,
1653 int glv_flags,
1654 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1655 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001656{
1657 char_u *arg = argstart;
1658 char_u *name_end;
1659 int error = FALSE;
1660 lval_T lv;
1661
1662 do
1663 {
1664 if (*arg == '$')
1665 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001666 lv.ll_name = arg;
1667 lv.ll_tv = NULL;
1668 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001669 if (get_env_len(&arg) == 0)
1670 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001671 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001672 return;
1673 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001674 if (!error && !eap->skip
1675 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1676 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001677 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001678 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001679 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001680 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001681 // Parse the name and find the end.
1682 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001683 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001684 if (lv.ll_name == NULL)
1685 error = TRUE; // error but continue parsing
1686 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1687 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001688 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001689 if (name_end != NULL)
1690 {
1691 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001692 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001693 }
1694 if (!(eap->skip || error))
1695 clear_lval(&lv);
1696 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001697 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001698
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001699 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001700 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001701 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001702
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001703 if (!eap->skip)
1704 clear_lval(&lv);
1705 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001706
1707 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001708 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001709
Bram Moolenaar63b91732021-08-05 20:40:03 +02001710 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001711}
1712
1713 static int
1714do_unlet_var(
1715 lval_T *lp,
1716 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001717 exarg_T *eap,
1718 int deep UNUSED,
1719 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001720{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001721 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001722 int ret = OK;
1723 int cc;
1724
1725 if (lp->ll_tv == NULL)
1726 {
1727 cc = *name_end;
1728 *name_end = NUL;
1729
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001730 // Environment variable, normal name or expanded name.
1731 if (*lp->ll_name == '$')
1732 vim_unsetenv(lp->ll_name + 1);
1733 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001734 ret = FAIL;
1735 *name_end = cc;
1736 }
1737 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001738 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001739 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001740 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001741 return FAIL;
1742 else if (lp->ll_range)
1743 {
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001744 if (list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_name, lp->ll_n1,
1745 !lp->ll_empty2, lp->ll_n2) == FAIL)
1746 return FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001747 }
1748 else
1749 {
1750 if (lp->ll_list != NULL)
1751 // unlet a List item.
1752 listitem_remove(lp->ll_list, lp->ll_li);
1753 else
1754 // unlet a Dictionary item.
1755 dictitem_remove(lp->ll_dict, lp->ll_di);
1756 }
1757
1758 return ret;
1759}
1760
1761/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001762 * Unlet one item or a range of items from a list.
1763 * Return OK or FAIL.
1764 */
1765 int
1766list_unlet_range(
1767 list_T *l,
1768 listitem_T *li_first,
1769 char_u *name,
1770 long n1_arg,
1771 int has_n2,
1772 long n2)
1773{
1774 listitem_T *li = li_first;
1775 int n1 = n1_arg;
1776
1777 while (li != NULL && (!has_n2 || n2 >= n1))
1778 {
1779 if (value_check_lock(li->li_tv.v_lock, name, FALSE))
1780 return FAIL;
1781 li = li->li_next;
1782 ++n1;
1783 }
1784
1785 // Delete a range of List items.
1786 li = li_first;
1787 n1 = n1_arg;
1788 while (li != NULL && (!has_n2 || n2 >= n1))
1789 {
1790 listitem_T *next = li->li_next;
1791
1792 listitem_remove(l, li);
1793 li = next;
1794 ++n1;
1795 }
1796 return OK;
1797}
1798/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001799 * "unlet" a variable. Return OK if it existed, FAIL if not.
1800 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1801 */
1802 int
1803do_unlet(char_u *name, int forceit)
1804{
1805 hashtab_T *ht;
1806 hashitem_T *hi;
1807 char_u *varname;
1808 dict_T *d;
1809 dictitem_T *di;
1810
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001811 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001812 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001813 return FAIL;
1814
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001815 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001816
1817 // can't :unlet a script variable in Vim9 script from a function
1818 if (ht == get_script_local_ht()
1819 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1820 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1821 == SCRIPT_VERSION_VIM9
1822 && check_vim9_unlet(name) == FAIL)
1823 return FAIL;
1824
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001825 if (ht != NULL && *varname != NUL)
1826 {
1827 d = get_current_funccal_dict(ht);
1828 if (d == NULL)
1829 {
1830 if (ht == &globvarht)
1831 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001832 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001833 d = &vimvardict;
1834 else
1835 {
1836 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1837 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1838 }
1839 if (d == NULL)
1840 {
1841 internal_error("do_unlet()");
1842 return FAIL;
1843 }
1844 }
1845 hi = hash_find(ht, varname);
1846 if (HASHITEM_EMPTY(hi))
1847 hi = find_hi_in_scoped_ht(name, &ht);
1848 if (hi != NULL && !HASHITEM_EMPTY(hi))
1849 {
1850 di = HI2DI(hi);
1851 if (var_check_fixed(di->di_flags, name, FALSE)
1852 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001853 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001854 return FAIL;
1855
1856 delete_var(ht, hi);
1857 return OK;
1858 }
1859 }
1860 if (forceit)
1861 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00001862 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001863 return FAIL;
1864}
1865
1866/*
1867 * Lock or unlock variable indicated by "lp".
1868 * "deep" is the levels to go (-1 for unlimited);
1869 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1870 */
1871 static int
1872do_lock_var(
1873 lval_T *lp,
1874 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001875 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001876 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001877 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001878{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001879 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001880 int ret = OK;
1881 int cc;
1882 dictitem_T *di;
1883
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001884 if (lp->ll_tv == NULL)
1885 {
1886 cc = *name_end;
1887 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001888 if (*lp->ll_name == '$')
1889 {
1890 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001891 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001892 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001893 else
1894 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001895 // Normal name or expanded name.
1896 di = find_var(lp->ll_name, NULL, TRUE);
1897 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001898 {
1899 if (in_vim9script())
1900 semsg(_(e_cannot_find_variable_to_unlock_str),
1901 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001902 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001903 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001904 else if ((di->di_flags & DI_FLAGS_FIX)
1905 && di->di_tv.v_type != VAR_DICT
1906 && di->di_tv.v_type != VAR_LIST)
1907 {
1908 // For historic reasons this error is not given for a list or
1909 // dict. E.g., the b: dict could be locked/unlocked.
1910 semsg(_(e_lock_unlock), lp->ll_name);
1911 ret = FAIL;
1912 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001913 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001914 {
1915 if (lock)
1916 di->di_flags |= DI_FLAGS_LOCK;
1917 else
1918 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001919 if (deep != 0)
1920 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001921 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001922 }
1923 *name_end = cc;
1924 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001925 else if (deep == 0)
1926 {
1927 // nothing to do
1928 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001929 else if (lp->ll_range)
1930 {
1931 listitem_T *li = lp->ll_li;
1932
1933 // (un)lock a range of List items.
1934 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1935 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001936 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001937 li = li->li_next;
1938 ++lp->ll_n1;
1939 }
1940 }
1941 else if (lp->ll_list != NULL)
1942 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001943 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001944 else
1945 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001946 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001947
1948 return ret;
1949}
1950
1951/*
1952 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001953 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1954 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001955 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001956 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001957item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001958{
1959 static int recurse = 0;
1960 list_T *l;
1961 listitem_T *li;
1962 dict_T *d;
1963 blob_T *b;
1964 hashitem_T *hi;
1965 int todo;
1966
1967 if (recurse >= DICT_MAXNEST)
1968 {
1969 emsg(_("E743: variable nested too deep for (un)lock"));
1970 return;
1971 }
1972 if (deep == 0)
1973 return;
1974 ++recurse;
1975
1976 // lock/unlock the item itself
1977 if (lock)
1978 tv->v_lock |= VAR_LOCKED;
1979 else
1980 tv->v_lock &= ~VAR_LOCKED;
1981
1982 switch (tv->v_type)
1983 {
1984 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001985 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001986 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001987 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001989 case VAR_STRING:
1990 case VAR_FUNC:
1991 case VAR_PARTIAL:
1992 case VAR_FLOAT:
1993 case VAR_SPECIAL:
1994 case VAR_JOB:
1995 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001996 case VAR_INSTR:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001997 break;
1998
1999 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002000 if ((b = tv->vval.v_blob) != NULL
2001 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002002 {
2003 if (lock)
2004 b->bv_lock |= VAR_LOCKED;
2005 else
2006 b->bv_lock &= ~VAR_LOCKED;
2007 }
2008 break;
2009 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002010 if ((l = tv->vval.v_list) != NULL
2011 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002012 {
2013 if (lock)
2014 l->lv_lock |= VAR_LOCKED;
2015 else
2016 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002017 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002018 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002019 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02002020 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002021 }
2022 break;
2023 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002024 if ((d = tv->vval.v_dict) != NULL
2025 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002026 {
2027 if (lock)
2028 d->dv_lock |= VAR_LOCKED;
2029 else
2030 d->dv_lock &= ~VAR_LOCKED;
2031 if (deep < 0 || deep > 1)
2032 {
2033 // recursive: lock/unlock the items the List contains
2034 todo = (int)d->dv_hashtab.ht_used;
2035 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2036 {
2037 if (!HASHITEM_EMPTY(hi))
2038 {
2039 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002040 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2041 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002042 }
2043 }
2044 }
2045 }
2046 }
2047 --recurse;
2048}
2049
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002050#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2051/*
2052 * Delete all "menutrans_" variables.
2053 */
2054 void
2055del_menutrans_vars(void)
2056{
2057 hashitem_T *hi;
2058 int todo;
2059
2060 hash_lock(&globvarht);
2061 todo = (int)globvarht.ht_used;
2062 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2063 {
2064 if (!HASHITEM_EMPTY(hi))
2065 {
2066 --todo;
2067 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2068 delete_var(&globvarht, hi);
2069 }
2070 }
2071 hash_unlock(&globvarht);
2072}
2073#endif
2074
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002075/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002076 * Local string buffer for the next two functions to store a variable name
2077 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2078 * get_user_var_name().
2079 */
2080
2081static char_u *varnamebuf = NULL;
2082static int varnamebuflen = 0;
2083
2084/*
2085 * Function to concatenate a prefix and a variable name.
2086 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002087 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002088cat_prefix_varname(int prefix, char_u *name)
2089{
2090 int len;
2091
2092 len = (int)STRLEN(name) + 3;
2093 if (len > varnamebuflen)
2094 {
2095 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002096 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002097 varnamebuf = alloc(len);
2098 if (varnamebuf == NULL)
2099 {
2100 varnamebuflen = 0;
2101 return NULL;
2102 }
2103 varnamebuflen = len;
2104 }
2105 *varnamebuf = prefix;
2106 varnamebuf[1] = ':';
2107 STRCPY(varnamebuf + 2, name);
2108 return varnamebuf;
2109}
2110
2111/*
2112 * Function given to ExpandGeneric() to obtain the list of user defined
2113 * (global/buffer/window/built-in) variable names.
2114 */
2115 char_u *
2116get_user_var_name(expand_T *xp, int idx)
2117{
2118 static long_u gdone;
2119 static long_u bdone;
2120 static long_u wdone;
2121 static long_u tdone;
2122 static int vidx;
2123 static hashitem_T *hi;
2124 hashtab_T *ht;
2125
2126 if (idx == 0)
2127 {
2128 gdone = bdone = wdone = vidx = 0;
2129 tdone = 0;
2130 }
2131
2132 // Global variables
2133 if (gdone < globvarht.ht_used)
2134 {
2135 if (gdone++ == 0)
2136 hi = globvarht.ht_array;
2137 else
2138 ++hi;
2139 while (HASHITEM_EMPTY(hi))
2140 ++hi;
2141 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2142 return cat_prefix_varname('g', hi->hi_key);
2143 return hi->hi_key;
2144 }
2145
2146 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002147 ht =
2148#ifdef FEAT_CMDWIN
2149 // In cmdwin, the alternative buffer should be used.
mityua1198122021-11-20 19:13:39 +00002150 is_in_cmdwin() ? &prevwin->w_buffer->b_vars->dv_hashtab :
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002151#endif
2152 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002153 if (bdone < ht->ht_used)
2154 {
2155 if (bdone++ == 0)
2156 hi = ht->ht_array;
2157 else
2158 ++hi;
2159 while (HASHITEM_EMPTY(hi))
2160 ++hi;
2161 return cat_prefix_varname('b', hi->hi_key);
2162 }
2163
2164 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002165 ht =
2166#ifdef FEAT_CMDWIN
2167 // In cmdwin, the alternative window should be used.
mityua1198122021-11-20 19:13:39 +00002168 is_in_cmdwin() ? &prevwin->w_vars->dv_hashtab :
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002169#endif
2170 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002171 if (wdone < ht->ht_used)
2172 {
2173 if (wdone++ == 0)
2174 hi = ht->ht_array;
2175 else
2176 ++hi;
2177 while (HASHITEM_EMPTY(hi))
2178 ++hi;
2179 return cat_prefix_varname('w', hi->hi_key);
2180 }
2181
2182 // t: variables
2183 ht = &curtab->tp_vars->dv_hashtab;
2184 if (tdone < ht->ht_used)
2185 {
2186 if (tdone++ == 0)
2187 hi = ht->ht_array;
2188 else
2189 ++hi;
2190 while (HASHITEM_EMPTY(hi))
2191 ++hi;
2192 return cat_prefix_varname('t', hi->hi_key);
2193 }
2194
2195 // v: variables
2196 if (vidx < VV_LEN)
2197 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2198
2199 VIM_CLEAR(varnamebuf);
2200 varnamebuflen = 0;
2201 return NULL;
2202}
2203
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002204 char *
2205get_var_special_name(int nr)
2206{
2207 switch (nr)
2208 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002209 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2210 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002211 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002212 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002213 }
2214 internal_error("get_var_special_name()");
2215 return "42";
2216}
2217
2218/*
2219 * Returns the global variable dictionary
2220 */
2221 dict_T *
2222get_globvar_dict(void)
2223{
2224 return &globvardict;
2225}
2226
2227/*
2228 * Returns the global variable hash table
2229 */
2230 hashtab_T *
2231get_globvar_ht(void)
2232{
2233 return &globvarht;
2234}
2235
2236/*
2237 * Returns the v: variable dictionary
2238 */
2239 dict_T *
2240get_vimvar_dict(void)
2241{
2242 return &vimvardict;
2243}
2244
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002245/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002247 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002248 */
2249 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002250find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002252 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2253 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002254
2255 if (di == NULL)
2256 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002257 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2259 return (int)(vv - vimvars);
2260}
2261
2262
2263/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002264 * Set type of v: variable to "type".
2265 */
2266 void
2267set_vim_var_type(int idx, vartype_T type)
2268{
Bram Moolenaard787e402021-12-24 21:36:12 +00002269 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002270}
2271
2272/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002273 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002274 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002275 */
2276 void
2277set_vim_var_nr(int idx, varnumber_T val)
2278{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002279 vimvars[idx].vv_nr = val;
2280}
2281
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002282 char *
2283get_vim_var_name(int idx)
2284{
2285 return vimvars[idx].vv_name;
2286}
2287
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002288/*
2289 * Get typval_T v: variable value.
2290 */
2291 typval_T *
2292get_vim_var_tv(int idx)
2293{
2294 return &vimvars[idx].vv_tv;
2295}
2296
Bram Moolenaard787e402021-12-24 21:36:12 +00002297 type_T *
2298get_vim_var_type(int idx, garray_T *type_list)
2299{
2300 if (vimvars[idx].vv_type != NULL)
2301 return vimvars[idx].vv_type;
2302 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2303}
2304
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002305/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002306 * Set v: variable to "tv". Only accepts the same type.
2307 * Takes over the value of "tv".
2308 */
2309 int
2310set_vim_var_tv(int idx, typval_T *tv)
2311{
Bram Moolenaard787e402021-12-24 21:36:12 +00002312 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002313 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002314 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002315 clear_tv(tv);
2316 return FAIL;
2317 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002318 // VV_RO is also checked when compiling, but let's check here as well.
2319 if (vimvars[idx].vv_flags & VV_RO)
2320 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002321 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002322 return FAIL;
2323 }
2324 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2325 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002326 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002327 return FAIL;
2328 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002329 clear_tv(&vimvars[idx].vv_di.di_tv);
2330 vimvars[idx].vv_di.di_tv = *tv;
2331 return OK;
2332}
2333
2334/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002335 * Get number v: variable value.
2336 */
2337 varnumber_T
2338get_vim_var_nr(int idx)
2339{
2340 return vimvars[idx].vv_nr;
2341}
2342
2343/*
2344 * Get string v: variable value. Uses a static buffer, can only be used once.
2345 * If the String variable has never been set, return an empty string.
2346 * Never returns NULL;
2347 */
2348 char_u *
2349get_vim_var_str(int idx)
2350{
2351 return tv_get_string(&vimvars[idx].vv_tv);
2352}
2353
2354/*
2355 * Get List v: variable value. Caller must take care of reference count when
2356 * needed.
2357 */
2358 list_T *
2359get_vim_var_list(int idx)
2360{
2361 return vimvars[idx].vv_list;
2362}
2363
2364/*
2365 * Get Dict v: variable value. Caller must take care of reference count when
2366 * needed.
2367 */
2368 dict_T *
2369get_vim_var_dict(int idx)
2370{
2371 return vimvars[idx].vv_dict;
2372}
2373
2374/*
2375 * Set v:char to character "c".
2376 */
2377 void
2378set_vim_var_char(int c)
2379{
2380 char_u buf[MB_MAXBYTES + 1];
2381
2382 if (has_mbyte)
2383 buf[(*mb_char2bytes)(c, buf)] = NUL;
2384 else
2385 {
2386 buf[0] = c;
2387 buf[1] = NUL;
2388 }
2389 set_vim_var_string(VV_CHAR, buf, -1);
2390}
2391
2392/*
2393 * Set v:count to "count" and v:count1 to "count1".
2394 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2395 */
2396 void
2397set_vcount(
2398 long count,
2399 long count1,
2400 int set_prevcount)
2401{
2402 if (set_prevcount)
2403 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2404 vimvars[VV_COUNT].vv_nr = count;
2405 vimvars[VV_COUNT1].vv_nr = count1;
2406}
2407
2408/*
2409 * Save variables that might be changed as a side effect. Used when executing
2410 * a timer callback.
2411 */
2412 void
2413save_vimvars(vimvars_save_T *vvsave)
2414{
2415 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2416 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2417 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2418}
2419
2420/*
2421 * Restore variables saved by save_vimvars().
2422 */
2423 void
2424restore_vimvars(vimvars_save_T *vvsave)
2425{
2426 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2427 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2428 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2429}
2430
2431/*
2432 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2433 * value.
2434 */
2435 void
2436set_vim_var_string(
2437 int idx,
2438 char_u *val,
2439 int len) // length of "val" to use or -1 (whole string)
2440{
2441 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002442 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002443 if (val == NULL)
2444 vimvars[idx].vv_str = NULL;
2445 else if (len == -1)
2446 vimvars[idx].vv_str = vim_strsave(val);
2447 else
2448 vimvars[idx].vv_str = vim_strnsave(val, len);
2449}
2450
2451/*
2452 * Set List v: variable to "val".
2453 */
2454 void
2455set_vim_var_list(int idx, list_T *val)
2456{
2457 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002458 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002459 vimvars[idx].vv_list = val;
2460 if (val != NULL)
2461 ++val->lv_refcount;
2462}
2463
2464/*
2465 * Set Dictionary v: variable to "val".
2466 */
2467 void
2468set_vim_var_dict(int idx, dict_T *val)
2469{
2470 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002471 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002472 vimvars[idx].vv_dict = val;
2473 if (val != NULL)
2474 {
2475 ++val->dv_refcount;
2476 dict_set_items_ro(val);
2477 }
2478}
2479
2480/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002481 * Set the v:argv list.
2482 */
2483 void
2484set_argv_var(char **argv, int argc)
2485{
2486 list_T *l = list_alloc();
2487 int i;
2488
2489 if (l == NULL)
2490 getout(1);
2491 l->lv_lock = VAR_FIXED;
2492 for (i = 0; i < argc; ++i)
2493 {
2494 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2495 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002496 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002497 }
2498 set_vim_var_list(VV_ARGV, l);
2499}
2500
2501/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002502 * Reset v:register, taking the 'clipboard' setting into account.
2503 */
2504 void
2505reset_reg_var(void)
2506{
2507 int regname = 0;
2508
2509 // Adjust the register according to 'clipboard', so that when
2510 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2511#ifdef FEAT_CLIPBOARD
2512 adjust_clip_reg(&regname);
2513#endif
2514 set_reg_var(regname);
2515}
2516
2517/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002518 * Set v:register if needed.
2519 */
2520 void
2521set_reg_var(int c)
2522{
2523 char_u regname;
2524
2525 if (c == 0 || c == ' ')
2526 regname = '"';
2527 else
2528 regname = c;
2529 // Avoid free/alloc when the value is already right.
2530 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2531 set_vim_var_string(VV_REG, &regname, 1);
2532}
2533
2534/*
2535 * Get or set v:exception. If "oldval" == NULL, return the current value.
2536 * Otherwise, restore the value to "oldval" and return NULL.
2537 * Must always be called in pairs to save and restore v:exception! Does not
2538 * take care of memory allocations.
2539 */
2540 char_u *
2541v_exception(char_u *oldval)
2542{
2543 if (oldval == NULL)
2544 return vimvars[VV_EXCEPTION].vv_str;
2545
2546 vimvars[VV_EXCEPTION].vv_str = oldval;
2547 return NULL;
2548}
2549
2550/*
2551 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2552 * Otherwise, restore the value to "oldval" and return NULL.
2553 * Must always be called in pairs to save and restore v:throwpoint! Does not
2554 * take care of memory allocations.
2555 */
2556 char_u *
2557v_throwpoint(char_u *oldval)
2558{
2559 if (oldval == NULL)
2560 return vimvars[VV_THROWPOINT].vv_str;
2561
2562 vimvars[VV_THROWPOINT].vv_str = oldval;
2563 return NULL;
2564}
2565
2566/*
2567 * Set v:cmdarg.
2568 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2569 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2570 * Must always be called in pairs!
2571 */
2572 char_u *
2573set_cmdarg(exarg_T *eap, char_u *oldarg)
2574{
2575 char_u *oldval;
2576 char_u *newval;
2577 unsigned len;
2578
2579 oldval = vimvars[VV_CMDARG].vv_str;
2580 if (eap == NULL)
2581 {
2582 vim_free(oldval);
2583 vimvars[VV_CMDARG].vv_str = oldarg;
2584 return NULL;
2585 }
2586
2587 if (eap->force_bin == FORCE_BIN)
2588 len = 6;
2589 else if (eap->force_bin == FORCE_NOBIN)
2590 len = 8;
2591 else
2592 len = 0;
2593
2594 if (eap->read_edit)
2595 len += 7;
2596
2597 if (eap->force_ff != 0)
2598 len += 10; // " ++ff=unix"
2599 if (eap->force_enc != 0)
2600 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2601 if (eap->bad_char != 0)
2602 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2603
2604 newval = alloc(len + 1);
2605 if (newval == NULL)
2606 return NULL;
2607
2608 if (eap->force_bin == FORCE_BIN)
2609 sprintf((char *)newval, " ++bin");
2610 else if (eap->force_bin == FORCE_NOBIN)
2611 sprintf((char *)newval, " ++nobin");
2612 else
2613 *newval = NUL;
2614
2615 if (eap->read_edit)
2616 STRCAT(newval, " ++edit");
2617
2618 if (eap->force_ff != 0)
2619 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2620 eap->force_ff == 'u' ? "unix"
2621 : eap->force_ff == 'd' ? "dos"
2622 : "mac");
2623 if (eap->force_enc != 0)
2624 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2625 eap->cmd + eap->force_enc);
2626 if (eap->bad_char == BAD_KEEP)
2627 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2628 else if (eap->bad_char == BAD_DROP)
2629 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2630 else if (eap->bad_char != 0)
2631 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2632 vimvars[VV_CMDARG].vv_str = newval;
2633 return oldval;
2634}
2635
2636/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002637 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002638 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2639 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002640 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2641 */
2642 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002643eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002644 char_u *name,
2645 int len, // length of "name"
2646 typval_T *rettv, // NULL when only checking existence
2647 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002648 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002649{
2650 int ret = OK;
2651 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002652 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002653 hashtab_T *ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002654 int cc;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002655 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002656
2657 // truncate the name, so that we can use strcmp()
2658 cc = name[len];
2659 name[len] = NUL;
2660
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002661 // Check for local variable when debugging.
2662 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002663 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002664 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002665 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2666
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002667 if (v != NULL)
2668 {
2669 tv = &v->di_tv;
2670 if (dip != NULL)
2671 *dip = v;
2672 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002673 else
2674 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002675 }
2676
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002677 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002678 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002679 imported_T *import;
2680 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2681
2682 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683
2684 // imported variable from another script
2685 if (import != NULL)
2686 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002687 if (import->imp_funcname != NULL)
2688 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002689 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002690 if (rettv != NULL)
2691 {
2692 rettv->v_type = VAR_FUNC;
2693 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2694 }
2695 }
Bram Moolenaara6294952020-12-27 13:39:50 +01002696 else if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002697 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002698 if ((flags & EVAL_VAR_IMPORT) == 0)
2699 {
2700 if (flags & EVAL_VAR_VERBOSE)
2701 emsg(_(e_import_as_name_not_supported_here));
2702 ret = FAIL;
2703 }
2704 else
2705 {
2706 if (rettv != NULL)
2707 {
2708 rettv->v_type = VAR_ANY;
2709 rettv->vval.v_number = import->imp_sid;
2710 }
2711 found = TRUE;
2712 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002713 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002714 else
2715 {
2716 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002717 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002719 tv = sv->sv_tv;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002720 type = sv->sv_type;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002721 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002722 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00002723 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002724 {
2725 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2726
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002727 // In Vim9 script we can get a function reference by using the
2728 // function name.
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002729 if (ufunc != NULL)
2730 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002731 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002732 if (rettv != NULL)
2733 {
2734 rettv->v_type = VAR_FUNC;
Bram Moolenaaref082e12021-12-12 21:02:03 +00002735 if (STRNCMP(name, "g:", 2) == 0)
2736 // Keep the "g:", otherwise script-local may be
2737 // assumed.
2738 rettv->vval.v_string = vim_strsave(name);
2739 else
2740 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002741 if (rettv->vval.v_string != NULL)
2742 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002743 }
2744 }
2745 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746 }
2747
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002748 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002749 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002750 if (tv == NULL)
2751 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002752 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002753 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002754 ret = FAIL;
2755 }
2756 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002757 {
Bram Moolenaar11005b02021-07-11 20:59:00 +02002758 if (ht != NULL && ht == get_script_local_ht()
2759 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002760 {
Bram Moolenaarc967d572021-07-08 21:38:50 +02002761 svar_T *sv = find_typval_in_script(tv);
Bram Moolenaarf055d452021-07-08 20:57:24 +02002762
Bram Moolenaarf055d452021-07-08 20:57:24 +02002763 if (sv != NULL)
2764 type = sv->sv_type;
2765 }
2766
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002767 // If a list or dict variable wasn't initialized, do it now.
2768 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2769 {
2770 tv->vval.v_dict = dict_alloc();
2771 if (tv->vval.v_dict != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002772 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002773 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002774 tv->vval.v_dict->dv_type = alloc_type(type);
2775 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002776 }
2777 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2778 {
2779 tv->vval.v_list = list_alloc();
2780 if (tv->vval.v_list != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002781 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002782 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002783 tv->vval.v_list->lv_type = alloc_type(type);
2784 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002785 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002786 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL)
2787 {
2788 tv->vval.v_blob = blob_alloc();
2789 if (tv->vval.v_blob != NULL)
2790 ++tv->vval.v_blob->bv_refcount;
2791 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002792 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002793 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002794 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002795
2796 name[len] = cc;
2797
2798 return ret;
2799}
2800
2801/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002802 * Check if variable "name[len]" is a local variable or an argument.
2803 * If so, "*eval_lavars_used" is set to TRUE.
2804 */
2805 void
2806check_vars(char_u *name, int len)
2807{
2808 int cc;
2809 char_u *varname;
2810 hashtab_T *ht;
2811
2812 if (eval_lavars_used == NULL)
2813 return;
2814
2815 // truncate the name, so that we can use strcmp()
2816 cc = name[len];
2817 name[len] = NUL;
2818
2819 ht = find_var_ht(name, &varname);
2820 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2821 {
2822 if (find_var(name, NULL, TRUE) != NULL)
2823 *eval_lavars_used = TRUE;
2824 }
2825
2826 name[len] = cc;
2827}
2828
2829/*
2830 * Find variable "name" in the list of variables.
2831 * Return a pointer to it if found, NULL if not found.
2832 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002833 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002834 */
2835 dictitem_T *
2836find_var(char_u *name, hashtab_T **htp, int no_autoload)
2837{
2838 char_u *varname;
2839 hashtab_T *ht;
2840 dictitem_T *ret = NULL;
2841
2842 ht = find_var_ht(name, &varname);
2843 if (htp != NULL)
2844 *htp = ht;
2845 if (ht == NULL)
2846 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002847 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002848 if (ret != NULL)
2849 return ret;
2850
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002851 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002852 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002853 if (ret != NULL)
2854 return ret;
2855
2856 // in Vim9 script items without a scope can be script-local
2857 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2858 {
2859 ht = get_script_local_ht();
2860 if (ht != NULL)
2861 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002862 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002863 if (ret != NULL)
2864 {
2865 if (htp != NULL)
2866 *htp = ht;
2867 return ret;
2868 }
2869 }
2870 }
2871
2872 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002873}
2874
2875/*
2876 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002877 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002878 * Returns NULL if not found.
2879 */
2880 dictitem_T *
2881find_var_in_ht(
2882 hashtab_T *ht,
2883 int htname,
2884 char_u *varname,
2885 int no_autoload)
2886{
2887 hashitem_T *hi;
2888
2889 if (*varname == NUL)
2890 {
2891 // Must be something like "s:", otherwise "ht" would be NULL.
2892 switch (htname)
2893 {
2894 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2895 case 'g': return &globvars_var;
2896 case 'v': return &vimvars_var;
2897 case 'b': return &curbuf->b_bufvar;
2898 case 'w': return &curwin->w_winvar;
2899 case 't': return &curtab->tp_winvar;
2900 case 'l': return get_funccal_local_var();
2901 case 'a': return get_funccal_args_var();
2902 }
2903 return NULL;
2904 }
2905
2906 hi = hash_find(ht, varname);
2907 if (HASHITEM_EMPTY(hi))
2908 {
2909 // For global variables we may try auto-loading the script. If it
2910 // worked find the variable again. Don't auto-load a script if it was
2911 // loaded already, otherwise it would be loaded every time when
2912 // checking if a function name is a Funcref variable.
2913 if (ht == &globvarht && !no_autoload)
2914 {
2915 // Note: script_autoload() may make "hi" invalid. It must either
2916 // be obtained again or not used.
2917 if (!script_autoload(varname, FALSE) || aborting())
2918 return NULL;
2919 hi = hash_find(ht, varname);
2920 }
2921 if (HASHITEM_EMPTY(hi))
2922 return NULL;
2923 }
2924 return HI2DI(hi);
2925}
2926
2927/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 * Get the script-local hashtab. NULL if not in a script context.
2929 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002930 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931get_script_local_ht(void)
2932{
2933 scid_T sid = current_sctx.sc_sid;
2934
Bram Moolenaare3d46852020-08-29 13:39:17 +02002935 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 return &SCRIPT_VARS(sid);
2937 return NULL;
2938}
2939
2940/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002941 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002942 * When "cmd" is TRUE it must look like a command, a function must be followed
2943 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01002944 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01002946 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002947lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01002948 char_u *name,
2949 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002950 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01002951 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952{
2953 hashtab_T *ht = get_script_local_ht();
2954 char_u buffer[30];
2955 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01002956 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002958 int is_global = FALSE;
2959 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960
2961 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002962 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 if (len < sizeof(buffer) - 1)
2964 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002965 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 vim_strncpy(buffer, name, len);
2967 p = buffer;
2968 }
2969 else
2970 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002971 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002973 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 }
2975
2976 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002977 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978
2979 // if not script-local, then perhaps imported
Bram Moolenaar709664c2020-12-12 14:33:41 +01002980 if (res == FAIL && find_imported(p, 0, NULL) != NULL)
2981 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 if (p != buffer)
2983 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002984
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002985 // Find a function, so that a following "->" works.
2986 // When used as a command require "(" or "->" to follow, "Cmd" is a user
2987 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002988 if (res != OK)
2989 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002990 p = skipwhite(name + len);
2991
2992 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002993 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002994 // Do not check for an internal function, since it might also be a
2995 // valid command, such as ":split" versus "split()".
2996 // Skip "g:" before a function name.
2997 if (name[0] == 'g' && name[1] == ':')
2998 {
2999 is_global = TRUE;
3000 fname = name + 2;
3001 }
3002 if (find_func(fname, is_global, NULL) != NULL)
3003 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003004 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003005 }
3006
Bram Moolenaar709664c2020-12-12 14:33:41 +01003007 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008}
3009
3010/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003011 * Find the hashtab used for a variable name.
3012 * Return NULL if the name is not valid.
3013 * Set "varname" to the start of name without ':'.
3014 */
3015 hashtab_T *
3016find_var_ht(char_u *name, char_u **varname)
3017{
3018 hashitem_T *hi;
3019 hashtab_T *ht;
3020
3021 if (name[0] == NUL)
3022 return NULL;
3023 if (name[1] != ':')
3024 {
3025 // The name must not start with a colon or #.
3026 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3027 return NULL;
3028 *varname = name;
3029
3030 // "version" is "v:version" in all scopes if scriptversion < 3.
3031 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003032 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003033 {
3034 hi = hash_find(&compat_hashtab, name);
3035 if (!HASHITEM_EMPTY(hi))
3036 return &compat_hashtab;
3037 }
3038
3039 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 if (ht != NULL)
3041 return ht; // local variable
3042
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003043 // In Vim9 script items at the script level are script-local, except
3044 // for autoload names.
3045 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 {
3047 ht = get_script_local_ht();
3048 if (ht != NULL)
3049 return ht;
3050 }
3051
3052 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003053 }
3054 *varname = name + 2;
3055 if (*name == 'g') // global variable
3056 return &globvarht;
3057 // There must be no ':' or '#' in the rest of the name, unless g: is used
3058 if (vim_strchr(name + 2, ':') != NULL
3059 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3060 return NULL;
3061 if (*name == 'b') // buffer variable
3062 return &curbuf->b_vars->dv_hashtab;
3063 if (*name == 'w') // window variable
3064 return &curwin->w_vars->dv_hashtab;
3065 if (*name == 't') // tab page variable
3066 return &curtab->tp_vars->dv_hashtab;
3067 if (*name == 'v') // v: variable
3068 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003069 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003070 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003072 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073 if (*name == 'a') // a: function argument
3074 return get_funccal_args_ht();
3075 if (*name == 'l') // l: local function variable
3076 return get_funccal_local_ht();
3077 }
3078 if (*name == 's') // script variable
3079 {
3080 ht = get_script_local_ht();
3081 if (ht != NULL)
3082 return ht;
3083 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003084 return NULL;
3085}
3086
3087/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003088 * Get the string value of a (global/local) variable.
3089 * Note: see tv_get_string() for how long the pointer remains valid.
3090 * Returns NULL when it doesn't exist.
3091 */
3092 char_u *
3093get_var_value(char_u *name)
3094{
3095 dictitem_T *v;
3096
3097 v = find_var(name, NULL, FALSE);
3098 if (v == NULL)
3099 return NULL;
3100 return tv_get_string(&v->di_tv);
3101}
3102
3103/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003104 * Allocate a new hashtab for a sourced script. It will be used while
3105 * sourcing this script and when executing functions defined in the script.
3106 */
3107 void
3108new_script_vars(scid_T id)
3109{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003110 scriptvar_T *sv;
3111
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003112 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3113 if (sv == NULL)
3114 return;
3115 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003116 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003117}
3118
3119/*
3120 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3121 * point to it.
3122 */
3123 void
3124init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3125{
3126 hash_init(&dict->dv_hashtab);
3127 dict->dv_lock = 0;
3128 dict->dv_scope = scope;
3129 dict->dv_refcount = DO_NOT_FREE_CNT;
3130 dict->dv_copyID = 0;
3131 dict_var->di_tv.vval.v_dict = dict;
3132 dict_var->di_tv.v_type = VAR_DICT;
3133 dict_var->di_tv.v_lock = VAR_FIXED;
3134 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3135 dict_var->di_key[0] = NUL;
3136}
3137
3138/*
3139 * Unreference a dictionary initialized by init_var_dict().
3140 */
3141 void
3142unref_var_dict(dict_T *dict)
3143{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003144 // Now the dict needs to be freed if no one else is using it, go back to
3145 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003146 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3147 dict_unref(dict);
3148}
3149
3150/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003151 * Clean up a list of internal variables.
3152 * Frees all allocated variables and the value they contain.
3153 * Clears hashtab "ht", does not free it.
3154 */
3155 void
3156vars_clear(hashtab_T *ht)
3157{
3158 vars_clear_ext(ht, TRUE);
3159}
3160
3161/*
3162 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3163 */
3164 void
3165vars_clear_ext(hashtab_T *ht, int free_val)
3166{
3167 int todo;
3168 hashitem_T *hi;
3169 dictitem_T *v;
3170
3171 hash_lock(ht);
3172 todo = (int)ht->ht_used;
3173 for (hi = ht->ht_array; todo > 0; ++hi)
3174 {
3175 if (!HASHITEM_EMPTY(hi))
3176 {
3177 --todo;
3178
3179 // Free the variable. Don't remove it from the hashtab,
3180 // ht_array might change then. hash_clear() takes care of it
3181 // later.
3182 v = HI2DI(hi);
3183 if (free_val)
3184 clear_tv(&v->di_tv);
3185 if (v->di_flags & DI_FLAGS_ALLOC)
3186 vim_free(v);
3187 }
3188 }
3189 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003190 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003191}
3192
3193/*
3194 * Delete a variable from hashtab "ht" at item "hi".
3195 * Clear the variable value and free the dictitem.
3196 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003197 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003198delete_var(hashtab_T *ht, hashitem_T *hi)
3199{
3200 dictitem_T *di = HI2DI(hi);
3201
3202 hash_remove(ht, hi);
3203 clear_tv(&di->di_tv);
3204 vim_free(di);
3205}
3206
3207/*
3208 * List the value of one internal variable.
3209 */
3210 static void
3211list_one_var(dictitem_T *v, char *prefix, int *first)
3212{
3213 char_u *tofree;
3214 char_u *s;
3215 char_u numbuf[NUMBUFLEN];
3216
3217 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3218 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3219 s == NULL ? (char_u *)"" : s, first);
3220 vim_free(tofree);
3221}
3222
3223 static void
3224list_one_var_a(
3225 char *prefix,
3226 char_u *name,
3227 int type,
3228 char_u *string,
3229 int *first) // when TRUE clear rest of screen and set to FALSE
3230{
3231 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3232 msg_start();
3233 msg_puts(prefix);
3234 if (name != NULL) // "a:" vars don't have a name stored
3235 msg_puts((char *)name);
3236 msg_putchar(' ');
3237 msg_advance(22);
3238 if (type == VAR_NUMBER)
3239 msg_putchar('#');
3240 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3241 msg_putchar('*');
3242 else if (type == VAR_LIST)
3243 {
3244 msg_putchar('[');
3245 if (*string == '[')
3246 ++string;
3247 }
3248 else if (type == VAR_DICT)
3249 {
3250 msg_putchar('{');
3251 if (*string == '{')
3252 ++string;
3253 }
3254 else
3255 msg_putchar(' ');
3256
3257 msg_outtrans(string);
3258
3259 if (type == VAR_FUNC || type == VAR_PARTIAL)
3260 msg_puts("()");
3261 if (*first)
3262 {
3263 msg_clr_eos();
3264 *first = FALSE;
3265 }
3266}
3267
3268/*
3269 * Set variable "name" to value in "tv".
3270 * If the variable already exists, the value is updated.
3271 * Otherwise the variable is created.
3272 */
3273 void
3274set_var(
3275 char_u *name,
3276 typval_T *tv,
3277 int copy) // make copy of value in "tv"
3278{
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003279 set_var_const(name, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003280}
3281
3282/*
3283 * Set variable "name" to value in "tv".
3284 * If the variable already exists and "is_const" is FALSE the value is updated.
3285 * Otherwise the variable is created.
3286 */
3287 void
3288set_var_const(
3289 char_u *name,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003290 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003291 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003292 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003293 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003294 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003295{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003296 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003297 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003298 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 dictitem_T *di;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003300 typval_T *dest_tv = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003301 char_u *varname;
3302 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003304 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003305 int var_in_vim9script;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003306 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003307 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003308
3309 ht = find_var_ht(name, &varname);
3310 if (ht == NULL || *varname == NUL)
3311 {
3312 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003313 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003314 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003315 is_script_local = ht == get_script_local_ht();
3316
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003317 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003318 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003319 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003320 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003321 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003322 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003323 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003324 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003325 }
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003326 if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
3327 && vim_strchr((char_u *)"gwbt", name[0]) != NULL)
3328 // Do not make g:var, w:var, b:var or t:var final.
3329 flags &= ~ASSIGN_FINAL;
3330
Bram Moolenaare535db82021-03-31 21:07:24 +02003331 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003332 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3333 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003334 // For "[a, _] = list" the underscore is ignored.
3335 if ((flags & ASSIGN_UNPACK) == 0)
3336 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003337 goto failed;
3338 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003339
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003341
Bram Moolenaar24e93162021-07-18 20:40:33 +02003342 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003343 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003344 imported_T *import = find_imported(varname, 0, NULL);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003345
Bram Moolenaar24e93162021-07-18 20:40:33 +02003346 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003347 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003348 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
3349 svar_T *sv;
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003350 where_T where = WHERE_INIT;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003351
3352 // imported variable from another script
3353 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003355 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003356 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 }
Bram Moolenaar6853c382021-09-07 22:12:19 +02003358 if (import->imp_flags & IMP_FLAGS_STAR)
3359 {
3360 semsg(_(e_cannot_use_str_itself_it_is_imported_with_star),
3361 name);
3362 goto failed;
3363 }
Bram Moolenaar60579352021-07-19 21:45:07 +02003364 sv = ((svar_T *)si->sn_var_vals.ga_data) + import->imp_var_vals_idx;
3365
Bram Moolenaar60579352021-07-19 21:45:07 +02003366 where.wt_variable = TRUE;
3367 if (check_typval_type(sv->sv_type, tv, where) == FAIL
3368 || value_check_lock(sv->sv_tv->v_lock, name, FALSE))
3369 {
3370 goto failed;
3371 }
3372
Bram Moolenaar24e93162021-07-18 20:40:33 +02003373 dest_tv = sv->sv_tv;
Bram Moolenaarf6488542021-07-18 21:24:50 +02003374 clear_tv(dest_tv);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003375 }
3376 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377
Bram Moolenaar24e93162021-07-18 20:40:33 +02003378 if (dest_tv == NULL)
3379 {
3380 // Search in parent scope which is possible to reference from lambda
3381 if (di == NULL)
3382 di = find_var_in_scoped_ht(name, TRUE);
3383
3384 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003385 && var_wrong_func_name(name, di == NULL))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003386 goto failed;
3387
3388 if (need_convert_to_bool(type, tv))
3389 {
Bram Moolenaarf6488542021-07-18 21:24:50 +02003390 // Destination is a bool and the value is not, but it can be
3391 // converted.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003392 CLEAR_FIELD(bool_tv);
3393 bool_tv.v_type = VAR_BOOL;
3394 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3395 tv = &bool_tv;
3396 }
3397
3398 if (di != NULL)
3399 {
3400 // Item already exists. Allowed to replace when reloading.
3401 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
3402 {
3403 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaarf6488542021-07-18 21:24:50 +02003404 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003405 {
3406 emsg(_(e_cannot_mod));
3407 goto failed;
3408 }
3409
3410 if (is_script_local && vim9script
Bram Moolenaarf6488542021-07-18 21:24:50 +02003411 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003412 {
3413 semsg(_(e_redefining_script_item_str), name);
3414 goto failed;
3415 }
3416
Bram Moolenaara06758d2021-10-15 00:18:37 +01003417 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003418 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003419 where_T where = WHERE_INIT;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003420 svar_T *sv = find_typval_in_script(&di->di_tv);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003421
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003422 if (sv != NULL)
3423 {
3424 // check the type and adjust to bool if needed
3425 where.wt_index = var_idx;
3426 where.wt_variable = TRUE;
3427 if (check_script_var_type(sv, tv, name, where) == FAIL)
3428 goto failed;
3429 if (type == NULL)
3430 type = sv->sv_type;
3431 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003432 }
3433
Bram Moolenaara06758d2021-10-15 00:18:37 +01003434 if ((flags & ASSIGN_FOR_LOOP) == 0
3435 && var_check_permission(di, name) == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003436 goto failed;
3437 }
3438 else
3439 {
3440 // can only redefine once
3441 di->di_flags &= ~DI_FLAGS_RELOAD;
3442
Bram Moolenaarf6488542021-07-18 21:24:50 +02003443 // A Vim9 script-local variable is also present in sn_all_vars
3444 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaar24e93162021-07-18 20:40:33 +02003445 if (var_in_vim9script)
3446 update_vim9_script_var(FALSE, di, flags, tv, &type,
Bram Moolenaarf6488542021-07-18 21:24:50 +02003447 (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003448 }
3449
3450 // existing variable, need to clear the value
3451
3452 // Handle setting internal di: variables separately where needed to
3453 // prevent changing the type.
3454 if (ht == &vimvarht)
3455 {
3456 if (di->di_tv.v_type == VAR_STRING)
3457 {
3458 VIM_CLEAR(di->di_tv.vval.v_string);
3459 if (copy || tv->v_type != VAR_STRING)
3460 {
3461 char_u *val = tv_get_string(tv);
3462
Bram Moolenaarf6488542021-07-18 21:24:50 +02003463 // Careful: when assigning to v:errmsg and
3464 // tv_get_string() causes an error message the variable
3465 // will already be set.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003466 if (di->di_tv.vval.v_string == NULL)
3467 di->di_tv.vval.v_string = vim_strsave(val);
3468 }
3469 else
3470 {
3471 // Take over the string to avoid an extra alloc/free.
3472 di->di_tv.vval.v_string = tv->vval.v_string;
3473 tv->vval.v_string = NULL;
3474 }
3475 goto failed;
3476 }
3477 else if (di->di_tv.v_type == VAR_NUMBER)
3478 {
3479 di->di_tv.vval.v_number = tv_get_number(tv);
3480 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003481 set_search_direction(di->di_tv.vval.v_number
3482 ? '/' : '?');
Bram Moolenaar24e93162021-07-18 20:40:33 +02003483#ifdef FEAT_SEARCH_EXTRA
3484 else if (STRCMP(varname, "hlsearch") == 0)
3485 {
3486 no_hlsearch = !di->di_tv.vval.v_number;
3487 redraw_all_later(SOME_VALID);
3488 }
3489#endif
3490 goto failed;
3491 }
3492 else if (di->di_tv.v_type != tv->v_type)
3493 {
3494 semsg(_("E963: setting %s to value with wrong type"), name);
3495 goto failed;
3496 }
3497 }
3498
3499 clear_tv(&di->di_tv);
3500 }
3501 else
3502 {
3503 // Item not found, check if a function already exists.
3504 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaarf6488542021-07-18 21:24:50 +02003505 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003506 {
3507 semsg(_(e_redefining_script_item_str), name);
3508 goto failed;
3509 }
3510
Bram Moolenaar24e93162021-07-18 20:40:33 +02003511 // add a new variable
3512 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3513 {
3514 semsg(_(e_unknown_variable_str), name);
3515 goto failed;
3516 }
3517
3518 // Can't add "v:" or "a:" variable.
3519 if (ht == &vimvarht || ht == get_funccal_args_ht())
3520 {
3521 semsg(_(e_illvar), name);
3522 goto failed;
3523 }
3524
3525 // Make sure the variable name is valid. In Vim9 script an autoload
3526 // variable must be prefixed with "g:".
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003527 if (!valid_varname(varname, -1, !vim9script
Bram Moolenaarf6488542021-07-18 21:24:50 +02003528 || STRNCMP(name, "g:", 2) == 0))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003529 goto failed;
3530
3531 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3532 if (di == NULL)
3533 goto failed;
3534 STRCPY(di->di_key, varname);
3535 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
3536 {
3537 vim_free(di);
3538 goto failed;
3539 }
3540 di->di_flags = DI_FLAGS_ALLOC;
3541 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3542 di->di_flags |= DI_FLAGS_LOCK;
3543
3544 // A Vim9 script-local variable is also added to sn_all_vars and
3545 // sn_var_vals. It may set "type" from "tv".
Bram Moolenaare535db82021-03-31 21:07:24 +02003546 if (var_in_vim9script)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003547 update_vim9_script_var(TRUE, di, flags, tv, &type,
Bram Moolenaarf6488542021-07-18 21:24:50 +02003548 (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003549 }
3550
Bram Moolenaar24e93162021-07-18 20:40:33 +02003551 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003552 }
3553
3554 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003555 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003556 else
3557 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003558 *dest_tv = *tv;
3559 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003560 init_tv(tv);
3561 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003562 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003563
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003564 if (vim9script && type != NULL)
3565 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003566 if (type->tt_type == VAR_DICT && dest_tv->vval.v_dict != NULL)
Bram Moolenaar464393a2021-09-11 23:07:44 +02003567 {
3568 if (dest_tv->vval.v_dict->dv_type != type)
3569 {
3570 free_type(dest_tv->vval.v_dict->dv_type);
3571 dest_tv->vval.v_dict->dv_type = alloc_type(type);
3572 }
3573 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003574 else if (type->tt_type == VAR_LIST && dest_tv->vval.v_list != NULL)
Bram Moolenaar464393a2021-09-11 23:07:44 +02003575 {
3576 if (dest_tv->vval.v_list->lv_type != type)
3577 {
3578 free_type(dest_tv->vval.v_list->lv_type);
3579 dest_tv->vval.v_list->lv_type = alloc_type(type);
3580 }
3581 }
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003582 }
3583
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003584 // ":const var = value" locks the value
3585 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003586 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003587 // Like :lockvar! name: lock the value and what it contains, but only
3588 // if the reference count is up to one. That locks only literal
3589 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003590 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003591
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003592failed:
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003593 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003594 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003595}
3596
3597/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003598 * Check in this order for backwards compatibility:
3599 * - Whether the variable is read-only
3600 * - Whether the variable value is locked
3601 * - Whether the variable is locked
3602 */
3603 int
3604var_check_permission(dictitem_T *di, char_u *name)
3605{
3606 if (var_check_ro(di->di_flags, name, FALSE)
3607 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3608 || var_check_lock(di->di_flags, name, FALSE))
3609 return FAIL;
3610 return OK;
3611}
3612
3613/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003614 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3615 * Also give an error message.
3616 */
3617 int
3618var_check_ro(int flags, char_u *name, int use_gettext)
3619{
3620 if (flags & DI_FLAGS_RO)
3621 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003622 if (name == NULL)
3623 emsg(_(e_cannot_change_readonly_variable));
3624 else
3625 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02003626 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003627 return TRUE;
3628 }
3629 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3630 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003631 if (name == NULL)
3632 emsg(_(e_cannot_set_variable_in_sandbox));
3633 else
3634 semsg(_(e_cannot_set_variable_in_sandbox_str),
3635 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003636 return TRUE;
3637 }
3638 return FALSE;
3639}
3640
3641/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003642 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3643 * Also give an error message.
3644 */
3645 int
3646var_check_lock(int flags, char_u *name, int use_gettext)
3647{
3648 if (flags & DI_FLAGS_LOCK)
3649 {
3650 semsg(_(e_variable_is_locked_str),
3651 use_gettext ? (char_u *)_(name) : name);
3652 return TRUE;
3653 }
3654 return FALSE;
3655}
3656
3657/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003658 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3659 * Also give an error message.
3660 */
3661 int
3662var_check_fixed(int flags, char_u *name, int use_gettext)
3663{
3664 if (flags & DI_FLAGS_FIX)
3665 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003666 if (name == NULL)
3667 emsg(_(e_cannot_delete_variable));
3668 else
3669 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003670 use_gettext ? (char_u *)_(name) : name);
3671 return TRUE;
3672 }
3673 return FALSE;
3674}
3675
3676/*
3677 * Check if a funcref is assigned to a valid variable name.
3678 * Return TRUE and give an error if not.
3679 */
3680 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003681var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003682 char_u *name, // points to start of variable name
3683 int new_var) // TRUE when creating the variable
3684{
Bram Moolenaar32154662021-03-28 21:14:06 +02003685 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
3686 // the name can be used without the s: prefix.
3687 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
3688 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003689 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3690 ? name[2] : name[0]))
3691 {
3692 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3693 name);
3694 return TRUE;
3695 }
3696 // Don't allow hiding a function. When "v" is not NULL we might be
3697 // assigning another function to the same var, the type is checked
3698 // below.
3699 if (new_var && function_exists(name, FALSE))
3700 {
3701 semsg(_("E705: Variable name conflicts with existing function: %s"),
3702 name);
3703 return TRUE;
3704 }
3705 return FALSE;
3706}
3707
3708/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003709 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3710 * value. Also give an error message, using "name" or _("name") when
3711 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003712 */
3713 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003714value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003715{
3716 if (lock & VAR_LOCKED)
3717 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003718 if (name == NULL)
3719 emsg(_(e_value_is_locked));
3720 else
3721 semsg(_(e_value_is_locked_str),
3722 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003723 return TRUE;
3724 }
3725 if (lock & VAR_FIXED)
3726 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003727 if (name == NULL)
3728 emsg(_(e_cannot_change_value));
3729 else
3730 semsg(_(e_cannot_change_value_of_str),
3731 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003732 return TRUE;
3733 }
3734 return FALSE;
3735}
3736
3737/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003738 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003739 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003740 * Return FALSE and give an error if not.
3741 */
3742 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003743valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003744{
3745 char_u *p;
3746
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003747 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003748 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003749 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003750 {
3751 semsg(_(e_illvar), varname);
3752 return FALSE;
3753 }
3754 return TRUE;
3755}
3756
3757/*
3758 * getwinvar() and gettabwinvar()
3759 */
3760 static void
3761getwinvar(
3762 typval_T *argvars,
3763 typval_T *rettv,
3764 int off) // 1 for gettabwinvar()
3765{
3766 win_T *win;
3767 char_u *varname;
3768 dictitem_T *v;
3769 tabpage_T *tp = NULL;
3770 int done = FALSE;
3771 win_T *oldcurwin;
3772 tabpage_T *oldtabpage;
3773 int need_switch_win;
3774
3775 if (off == 1)
3776 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3777 else
3778 tp = curtab;
3779 win = find_win_by_nr(&argvars[off], tp);
3780 varname = tv_get_string_chk(&argvars[off + 1]);
3781 ++emsg_off;
3782
3783 rettv->v_type = VAR_STRING;
3784 rettv->vval.v_string = NULL;
3785
3786 if (win != NULL && varname != NULL)
3787 {
3788 // Set curwin to be our win, temporarily. Also set the tabpage,
3789 // otherwise the window is not valid. Only do this when needed,
3790 // autocommands get blocked.
3791 need_switch_win = !(tp == curtab && win == curwin);
3792 if (!need_switch_win
3793 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3794 {
3795 if (*varname == '&')
3796 {
3797 if (varname[1] == NUL)
3798 {
3799 // get all window-local options in a dict
3800 dict_T *opts = get_winbuf_options(FALSE);
3801
3802 if (opts != NULL)
3803 {
3804 rettv_dict_set(rettv, opts);
3805 done = TRUE;
3806 }
3807 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003808 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003809 // window-local-option
3810 done = TRUE;
3811 }
3812 else
3813 {
3814 // Look up the variable.
3815 // Let getwinvar({nr}, "") return the "w:" dictionary.
3816 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3817 varname, FALSE);
3818 if (v != NULL)
3819 {
3820 copy_tv(&v->di_tv, rettv);
3821 done = TRUE;
3822 }
3823 }
3824 }
3825
3826 if (need_switch_win)
3827 // restore previous notion of curwin
3828 restore_win(oldcurwin, oldtabpage, TRUE);
3829 }
3830
3831 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3832 // use the default return value
3833 copy_tv(&argvars[off + 2], rettv);
3834
3835 --emsg_off;
3836}
3837
3838/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003839 * Set option "varname" to the value of "varp" for the current buffer/window.
3840 */
3841 static void
3842set_option_from_tv(char_u *varname, typval_T *varp)
3843{
3844 long numval = 0;
3845 char_u *strval;
3846 char_u nbuf[NUMBUFLEN];
3847 int error = FALSE;
3848
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003849 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003850 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003851 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003852 strval = (char_u *)"0"; // avoid using "false"
3853 }
3854 else
3855 {
3856 if (!in_vim9script() || varp->v_type != VAR_STRING)
3857 numval = (long)tv_get_number_chk(varp, &error);
3858 strval = tv_get_string_buf_chk(varp, nbuf);
3859 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02003860 if (!error && strval != NULL)
3861 set_option_value(varname, numval, strval, OPT_LOCAL);
3862}
3863
3864/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003865 * "setwinvar()" and "settabwinvar()" functions
3866 */
3867 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003868setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003869{
3870 win_T *win;
3871 win_T *save_curwin;
3872 tabpage_T *save_curtab;
3873 int need_switch_win;
3874 char_u *varname, *winvarname;
3875 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003876 tabpage_T *tp = NULL;
3877
3878 if (check_secure())
3879 return;
3880
3881 if (off == 1)
3882 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3883 else
3884 tp = curtab;
3885 win = find_win_by_nr(&argvars[off], tp);
3886 varname = tv_get_string_chk(&argvars[off + 1]);
3887 varp = &argvars[off + 2];
3888
3889 if (win != NULL && varname != NULL && varp != NULL)
3890 {
3891 need_switch_win = !(tp == curtab && win == curwin);
3892 if (!need_switch_win
3893 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3894 {
3895 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003896 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003897 else
3898 {
3899 winvarname = alloc(STRLEN(varname) + 3);
3900 if (winvarname != NULL)
3901 {
3902 STRCPY(winvarname, "w:");
3903 STRCPY(winvarname + 2, varname);
3904 set_var(winvarname, varp, TRUE);
3905 vim_free(winvarname);
3906 }
3907 }
3908 }
3909 if (need_switch_win)
3910 restore_win(save_curwin, save_curtab, TRUE);
3911 }
3912}
3913
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003914/*
3915 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3916 * v:option_type, and v:option_command.
3917 */
3918 void
3919reset_v_option_vars(void)
3920{
3921 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3922 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3923 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3924 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3925 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3926 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3927}
3928
3929/*
3930 * Add an assert error to v:errors.
3931 */
3932 void
3933assert_error(garray_T *gap)
3934{
3935 struct vimvar *vp = &vimvars[VV_ERRORS];
3936
Bram Moolenaard787e402021-12-24 21:36:12 +00003937 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003938 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003939 set_vim_var_list(VV_ERRORS, list_alloc());
3940 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3941}
3942
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003943 int
3944var_exists(char_u *var)
3945{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003946 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003947 char_u *name;
3948 char_u *tofree;
3949 typval_T tv;
3950 int len = 0;
3951 int n = FALSE;
3952
3953 // get_name_len() takes care of expanding curly braces
3954 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003955 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003956 if (len > 0)
3957 {
3958 if (tofree != NULL)
3959 name = tofree;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003960 n = (eval_variable(name, len, &tv, NULL,
3961 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003962 if (n)
3963 {
3964 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003965 arg = skipwhite(arg);
3966 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003967 if (n)
3968 clear_tv(&tv);
3969 }
3970 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003971 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003972 n = FALSE;
3973
3974 vim_free(tofree);
3975 return n;
3976}
3977
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003978static lval_T *redir_lval = NULL;
3979#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3980static garray_T redir_ga; // only valid when redir_lval is not NULL
3981static char_u *redir_endp = NULL;
3982static char_u *redir_varname = NULL;
3983
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003984 int
3985alloc_redir_lval(void)
3986{
3987 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3988 if (redir_lval == NULL)
3989 return FAIL;
3990 return OK;
3991}
3992
3993 void
3994clear_redir_lval(void)
3995{
3996 VIM_CLEAR(redir_lval);
3997}
3998
3999 void
4000init_redir_ga(void)
4001{
4002 ga_init2(&redir_ga, (int)sizeof(char), 500);
4003}
4004
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004005/*
4006 * Start recording command output to a variable
4007 * When "append" is TRUE append to an existing variable.
4008 * Returns OK if successfully completed the setup. FAIL otherwise.
4009 */
4010 int
4011var_redir_start(char_u *name, int append)
4012{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004013 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004014 typval_T tv;
4015
4016 // Catch a bad name early.
4017 if (!eval_isnamec1(*name))
4018 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004019 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004020 return FAIL;
4021 }
4022
4023 // Make a copy of the name, it is used in redir_lval until redir ends.
4024 redir_varname = vim_strsave(name);
4025 if (redir_varname == NULL)
4026 return FAIL;
4027
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004028 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004029 {
4030 var_redir_stop();
4031 return FAIL;
4032 }
4033
4034 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004035 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004036
4037 // Parse the variable name (can be a dict or list entry).
4038 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4039 FNE_CHECK_START);
4040 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4041 {
4042 clear_lval(redir_lval);
4043 if (redir_endp != NULL && *redir_endp != NUL)
4044 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004045 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004046 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004047 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004048 redir_endp = NULL; // don't store a value, only cleanup
4049 var_redir_stop();
4050 return FAIL;
4051 }
4052
4053 // check if we can write to the variable: set it to or append an empty
4054 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004055 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004056 tv.v_type = VAR_STRING;
4057 tv.vval.v_string = (char_u *)"";
4058 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004059 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004060 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004061 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004062 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004063 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004064 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004065 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004066 {
4067 redir_endp = NULL; // don't store a value, only cleanup
4068 var_redir_stop();
4069 return FAIL;
4070 }
4071
4072 return OK;
4073}
4074
4075/*
4076 * Append "value[value_len]" to the variable set by var_redir_start().
4077 * The actual appending is postponed until redirection ends, because the value
4078 * appended may in fact be the string we write to, changing it may cause freed
4079 * memory to be used:
4080 * :redir => foo
4081 * :let foo
4082 * :redir END
4083 */
4084 void
4085var_redir_str(char_u *value, int value_len)
4086{
4087 int len;
4088
4089 if (redir_lval == NULL)
4090 return;
4091
4092 if (value_len == -1)
4093 len = (int)STRLEN(value); // Append the entire string
4094 else
4095 len = value_len; // Append only "value_len" characters
4096
4097 if (ga_grow(&redir_ga, len) == OK)
4098 {
4099 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4100 redir_ga.ga_len += len;
4101 }
4102 else
4103 var_redir_stop();
4104}
4105
4106/*
4107 * Stop redirecting command output to a variable.
4108 * Frees the allocated memory.
4109 */
4110 void
4111var_redir_stop(void)
4112{
4113 typval_T tv;
4114
4115 if (EVALCMD_BUSY)
4116 {
4117 redir_lval = NULL;
4118 return;
4119 }
4120
4121 if (redir_lval != NULL)
4122 {
4123 // If there was no error: assign the text to the variable.
4124 if (redir_endp != NULL)
4125 {
4126 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4127 tv.v_type = VAR_STRING;
4128 tv.vval.v_string = redir_ga.ga_data;
4129 // Call get_lval() again, if it's inside a Dict or List it may
4130 // have changed.
4131 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4132 FALSE, FALSE, 0, FNE_CHECK_START);
4133 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004134 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004135 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004136 clear_lval(redir_lval);
4137 }
4138
4139 // free the collected output
4140 VIM_CLEAR(redir_ga.ga_data);
4141
4142 VIM_CLEAR(redir_lval);
4143 }
4144 VIM_CLEAR(redir_varname);
4145}
4146
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004147/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004148 * Get the collected redirected text and clear redir_ga.
4149 */
4150 char_u *
4151get_clear_redir_ga(void)
4152{
4153 char_u *res;
4154
4155 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4156 res = redir_ga.ga_data;
4157 redir_ga.ga_data = NULL;
4158 return res;
4159}
4160
4161/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004162 * "gettabvar()" function
4163 */
4164 void
4165f_gettabvar(typval_T *argvars, typval_T *rettv)
4166{
4167 win_T *oldcurwin;
4168 tabpage_T *tp, *oldtabpage;
4169 dictitem_T *v;
4170 char_u *varname;
4171 int done = FALSE;
4172
4173 rettv->v_type = VAR_STRING;
4174 rettv->vval.v_string = NULL;
4175
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004176 if (in_vim9script()
4177 && (check_for_number_arg(argvars, 0) == FAIL
4178 || check_for_string_arg(argvars, 1) == FAIL))
4179 return;
4180
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004181 varname = tv_get_string_chk(&argvars[1]);
4182 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4183 if (tp != NULL && varname != NULL)
4184 {
4185 // Set tp to be our tabpage, temporarily. Also set the window to the
4186 // first window in the tabpage, otherwise the window is not valid.
4187 if (switch_win(&oldcurwin, &oldtabpage,
4188 tp == curtab || tp->tp_firstwin == NULL ? firstwin
4189 : tp->tp_firstwin, tp, TRUE) == OK)
4190 {
4191 // look up the variable
4192 // Let gettabvar({nr}, "") return the "t:" dictionary.
4193 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
4194 if (v != NULL)
4195 {
4196 copy_tv(&v->di_tv, rettv);
4197 done = TRUE;
4198 }
4199 }
4200
4201 // restore previous notion of curwin
4202 restore_win(oldcurwin, oldtabpage, TRUE);
4203 }
4204
4205 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4206 copy_tv(&argvars[2], rettv);
4207}
4208
4209/*
4210 * "gettabwinvar()" function
4211 */
4212 void
4213f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4214{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004215 if (in_vim9script()
4216 && (check_for_number_arg(argvars, 0) == FAIL
4217 || check_for_number_arg(argvars, 1) == FAIL
4218 || check_for_string_arg(argvars, 2) == FAIL))
4219 return;
4220
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004221 getwinvar(argvars, rettv, 1);
4222}
4223
4224/*
4225 * "getwinvar()" function
4226 */
4227 void
4228f_getwinvar(typval_T *argvars, typval_T *rettv)
4229{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004230 if (in_vim9script()
4231 && (check_for_number_arg(argvars, 0) == FAIL
4232 || check_for_string_arg(argvars, 1) == FAIL))
4233 return;
4234
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004235 getwinvar(argvars, rettv, 0);
4236}
4237
4238/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004239 * "getbufvar()" function
4240 */
4241 void
4242f_getbufvar(typval_T *argvars, typval_T *rettv)
4243{
4244 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004245 char_u *varname;
4246 dictitem_T *v;
4247 int done = FALSE;
4248
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004249 if (in_vim9script()
4250 && (check_for_buffer_arg(argvars, 0) == FAIL
4251 || check_for_string_arg(argvars, 1) == FAIL))
4252 return;
4253
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004254 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004255 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004256
4257 rettv->v_type = VAR_STRING;
4258 rettv->vval.v_string = NULL;
4259
4260 if (buf != NULL && varname != NULL)
4261 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004262 if (*varname == '&')
4263 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004264 buf_T *save_curbuf = curbuf;
4265
4266 // set curbuf to be our buf, temporarily
4267 curbuf = buf;
4268
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004269 if (varname[1] == NUL)
4270 {
4271 // get all buffer-local options in a dict
4272 dict_T *opts = get_winbuf_options(TRUE);
4273
4274 if (opts != NULL)
4275 {
4276 rettv_dict_set(rettv, opts);
4277 done = TRUE;
4278 }
4279 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004280 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004281 // buffer-local-option
4282 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02004283
4284 // restore previous notion of curbuf
4285 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004286 }
4287 else
4288 {
4289 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02004290 if (*varname == NUL)
4291 // Let getbufvar({nr}, "") return the "b:" dictionary.
4292 v = &buf->b_bufvar;
4293 else
4294 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
4295 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004296 if (v != NULL)
4297 {
4298 copy_tv(&v->di_tv, rettv);
4299 done = TRUE;
4300 }
4301 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004302 }
4303
4304 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4305 // use the default value
4306 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004307}
4308
4309/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004310 * "settabvar()" function
4311 */
4312 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004313f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004314{
4315 tabpage_T *save_curtab;
4316 tabpage_T *tp;
4317 char_u *varname, *tabvarname;
4318 typval_T *varp;
4319
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004320 if (check_secure())
4321 return;
4322
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004323 if (in_vim9script()
4324 && (check_for_number_arg(argvars, 0) == FAIL
4325 || check_for_string_arg(argvars, 1) == FAIL))
4326 return;
4327
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004328 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4329 varname = tv_get_string_chk(&argvars[1]);
4330 varp = &argvars[2];
4331
4332 if (varname != NULL && varp != NULL && tp != NULL)
4333 {
4334 save_curtab = curtab;
4335 goto_tabpage_tp(tp, FALSE, FALSE);
4336
4337 tabvarname = alloc(STRLEN(varname) + 3);
4338 if (tabvarname != NULL)
4339 {
4340 STRCPY(tabvarname, "t:");
4341 STRCPY(tabvarname + 2, varname);
4342 set_var(tabvarname, varp, TRUE);
4343 vim_free(tabvarname);
4344 }
4345
4346 // Restore current tabpage
4347 if (valid_tabpage(save_curtab))
4348 goto_tabpage_tp(save_curtab, FALSE, FALSE);
4349 }
4350}
4351
4352/*
4353 * "settabwinvar()" function
4354 */
4355 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004356f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004357{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004358 if (in_vim9script()
4359 && (check_for_number_arg(argvars, 0) == FAIL
4360 || check_for_number_arg(argvars, 1) == FAIL
4361 || check_for_string_arg(argvars, 2) == FAIL))
4362 return;
4363
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004364 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004365}
4366
4367/*
4368 * "setwinvar()" function
4369 */
4370 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004371f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004372{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004373 if (in_vim9script()
4374 && (check_for_number_arg(argvars, 0) == FAIL
4375 || check_for_string_arg(argvars, 1) == FAIL))
4376 return;
4377
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004378 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004379}
4380
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004381/*
4382 * "setbufvar()" function
4383 */
4384 void
4385f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4386{
4387 buf_T *buf;
4388 char_u *varname, *bufvarname;
4389 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004390
4391 if (check_secure())
4392 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004393
4394 if (in_vim9script()
4395 && (check_for_buffer_arg(argvars, 0) == FAIL
4396 || check_for_string_arg(argvars, 1) == FAIL))
4397 return;
4398
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004399 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004400 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004401 varp = &argvars[2];
4402
4403 if (buf != NULL && varname != NULL && varp != NULL)
4404 {
4405 if (*varname == '&')
4406 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004407 aco_save_T aco;
4408
4409 // set curbuf to be our buf, temporarily
4410 aucmd_prepbuf(&aco, buf);
4411
Bram Moolenaar191929b2020-08-19 21:20:49 +02004412 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004413
4414 // reset notion of buffer
4415 aucmd_restbuf(&aco);
4416 }
4417 else
4418 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004419 bufvarname = alloc(STRLEN(varname) + 3);
4420 if (bufvarname != NULL)
4421 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004422 buf_T *save_curbuf = curbuf;
4423
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004424 curbuf = buf;
4425 STRCPY(bufvarname, "b:");
4426 STRCPY(bufvarname + 2, varname);
4427 set_var(bufvarname, varp, TRUE);
4428 vim_free(bufvarname);
4429 curbuf = save_curbuf;
4430 }
4431 }
4432 }
4433}
4434
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004435/*
4436 * Get a callback from "arg". It can be a Funcref or a function name.
4437 * When "arg" is zero return an empty string.
4438 * "cb_name" is not allocated.
4439 * "cb_name" is set to NULL for an invalid argument.
4440 */
4441 callback_T
4442get_callback(typval_T *arg)
4443{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004444 callback_T res;
4445 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004446
4447 res.cb_free_name = FALSE;
4448 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4449 {
4450 res.cb_partial = arg->vval.v_partial;
4451 ++res.cb_partial->pt_refcount;
4452 res.cb_name = partial_name(res.cb_partial);
4453 }
4454 else
4455 {
4456 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004457 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4458 && isdigit(*arg->vval.v_string))
4459 r = FAIL;
4460 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004461 {
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004462 if (arg->v_type == VAR_STRING)
4463 {
4464 char_u *name;
4465
4466 name = get_scriptlocal_funcname(arg->vval.v_string);
4467 if (name != NULL)
4468 {
4469 vim_free(arg->vval.v_string);
4470 arg->vval.v_string = name;
4471 }
4472 }
4473
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004474 res.cb_name = arg->vval.v_string;
4475 func_ref(res.cb_name);
4476 }
4477 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004478 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004479 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004480 r = FAIL;
4481
4482 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004483 {
4484 emsg(_("E921: Invalid callback argument"));
4485 res.cb_name = NULL;
4486 }
4487 }
4488 return res;
4489}
4490
4491/*
4492 * Copy a callback into a typval_T.
4493 */
4494 void
4495put_callback(callback_T *cb, typval_T *tv)
4496{
4497 if (cb->cb_partial != NULL)
4498 {
4499 tv->v_type = VAR_PARTIAL;
4500 tv->vval.v_partial = cb->cb_partial;
4501 ++tv->vval.v_partial->pt_refcount;
4502 }
4503 else
4504 {
4505 tv->v_type = VAR_FUNC;
4506 tv->vval.v_string = vim_strsave(cb->cb_name);
4507 func_ref(cb->cb_name);
4508 }
4509}
4510
4511/*
4512 * Make a copy of "src" into "dest", allocating the function name if needed,
4513 * without incrementing the refcount.
4514 */
4515 void
4516set_callback(callback_T *dest, callback_T *src)
4517{
4518 if (src->cb_partial == NULL)
4519 {
4520 // just a function name, make a copy
4521 dest->cb_name = vim_strsave(src->cb_name);
4522 dest->cb_free_name = TRUE;
4523 }
4524 else
4525 {
4526 // cb_name is a pointer into cb_partial
4527 dest->cb_name = src->cb_name;
4528 dest->cb_free_name = FALSE;
4529 }
4530 dest->cb_partial = src->cb_partial;
4531}
4532
4533/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004534 * Copy callback from "src" to "dest", incrementing the refcounts.
4535 */
4536 void
4537copy_callback(callback_T *dest, callback_T *src)
4538{
4539 dest->cb_partial = src->cb_partial;
4540 if (dest->cb_partial != NULL)
4541 {
4542 dest->cb_name = src->cb_name;
4543 dest->cb_free_name = FALSE;
4544 ++dest->cb_partial->pt_refcount;
4545 }
4546 else
4547 {
4548 dest->cb_name = vim_strsave(src->cb_name);
4549 dest->cb_free_name = TRUE;
4550 func_ref(src->cb_name);
4551 }
4552}
4553
4554/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004555 * Unref/free "callback" returned by get_callback() or set_callback().
4556 */
4557 void
4558free_callback(callback_T *callback)
4559{
4560 if (callback->cb_partial != NULL)
4561 {
4562 partial_unref(callback->cb_partial);
4563 callback->cb_partial = NULL;
4564 }
4565 else if (callback->cb_name != NULL)
4566 func_unref(callback->cb_name);
4567 if (callback->cb_free_name)
4568 {
4569 vim_free(callback->cb_name);
4570 callback->cb_free_name = FALSE;
4571 }
4572 callback->cb_name = NULL;
4573}
4574
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004575#endif // FEAT_EVAL