blob: 179953178695494b8ca6bb3d3c38043bd4570dfd [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!)
48 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
49} vimvars[VV_LEN] =
50{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020051 // The order here must match the VV_ defines in vim.h!
52 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaare5cdf152019-08-29 22:09:46 +020053 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
54 {VV_NAME("count1", VAR_NUMBER), VV_RO},
55 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
56 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
57 {VV_NAME("warningmsg", VAR_STRING), 0},
58 {VV_NAME("statusmsg", VAR_STRING), 0},
59 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
60 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
61 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
62 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
63 {VV_NAME("termresponse", VAR_STRING), VV_RO},
64 {VV_NAME("fname", VAR_STRING), VV_RO},
65 {VV_NAME("lang", VAR_STRING), VV_RO},
66 {VV_NAME("lc_time", VAR_STRING), VV_RO},
67 {VV_NAME("ctype", VAR_STRING), VV_RO},
68 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
69 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
70 {VV_NAME("fname_in", VAR_STRING), VV_RO},
71 {VV_NAME("fname_out", VAR_STRING), VV_RO},
72 {VV_NAME("fname_new", VAR_STRING), VV_RO},
73 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
74 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
75 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
76 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
77 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
78 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
79 {VV_NAME("progname", VAR_STRING), VV_RO},
80 {VV_NAME("servername", VAR_STRING), VV_RO},
81 {VV_NAME("dying", VAR_NUMBER), VV_RO},
82 {VV_NAME("exception", VAR_STRING), VV_RO},
83 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
84 {VV_NAME("register", VAR_STRING), VV_RO},
85 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
86 {VV_NAME("insertmode", VAR_STRING), VV_RO},
87 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
88 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
89 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
90 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
91 {VV_NAME("fcs_choice", VAR_STRING), 0},
92 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
93 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
94 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
95 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
96 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
97 {VV_NAME("beval_text", VAR_STRING), VV_RO},
98 {VV_NAME("scrollstart", VAR_STRING), 0},
99 {VV_NAME("swapname", VAR_STRING), VV_RO},
100 {VV_NAME("swapchoice", VAR_STRING), 0},
101 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
102 {VV_NAME("char", VAR_STRING), 0},
103 {VV_NAME("mouse_win", VAR_NUMBER), 0},
104 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
105 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
106 {VV_NAME("mouse_col", VAR_NUMBER), 0},
107 {VV_NAME("operator", VAR_STRING), VV_RO},
108 {VV_NAME("searchforward", VAR_NUMBER), 0},
109 {VV_NAME("hlsearch", VAR_NUMBER), 0},
110 {VV_NAME("oldfiles", VAR_LIST), 0},
111 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
112 {VV_NAME("progpath", VAR_STRING), VV_RO},
113 {VV_NAME("completed_item", VAR_DICT), VV_RO},
114 {VV_NAME("option_new", VAR_STRING), VV_RO},
115 {VV_NAME("option_old", VAR_STRING), VV_RO},
116 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
117 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
118 {VV_NAME("option_command", VAR_STRING), VV_RO},
119 {VV_NAME("option_type", VAR_STRING), VV_RO},
120 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100121 {VV_NAME("false", VAR_BOOL), VV_RO},
122 {VV_NAME("true", VAR_BOOL), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200123 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100124 {VV_NAME("null", VAR_SPECIAL), VV_RO},
125 {VV_NAME("numbersize", VAR_NUMBER), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200126 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
127 {VV_NAME("testing", VAR_NUMBER), 0},
128 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
129 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
130 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
131 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
132 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
133 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
134 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
135 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
136 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
137 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
138 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
139 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
140 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
141 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
142 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
143 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
144 {VV_NAME("event", VAR_DICT), VV_RO},
145 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
146 {VV_NAME("echospace", VAR_NUMBER), VV_RO},
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100147 {VV_NAME("argv", VAR_LIST), VV_RO},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200148 {VV_NAME("collate", VAR_STRING), VV_RO},
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100149 {VV_NAME("exiting", VAR_SPECIAL), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200150};
151
152// shorthand
153#define vv_type vv_di.di_tv.v_type
154#define vv_nr vv_di.di_tv.vval.v_number
155#define vv_float vv_di.di_tv.vval.v_float
156#define vv_str vv_di.di_tv.vval.v_string
157#define vv_list vv_di.di_tv.vval.v_list
158#define vv_dict vv_di.di_tv.vval.v_dict
159#define vv_blob vv_di.di_tv.vval.v_blob
160#define vv_tv vv_di.di_tv
161
162static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200163static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200164#define vimvarht vimvardict.dv_hashtab
165
166// for VIM_VERSION_ defines
167#include "version.h"
168
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200169static void list_glob_vars(int *first);
170static void list_buf_vars(int *first);
171static void list_win_vars(int *first);
172static void list_tab_vars(int *first);
173static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100174static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200175static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
176static 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 +0200177static void list_one_var(dictitem_T *v, char *prefix, int *first);
178static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
179
180/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200181 * Initialize global and vim special variables
182 */
183 void
184evalvars_init(void)
185{
186 int i;
187 struct vimvar *p;
188
189 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
190 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
191 vimvardict.dv_lock = VAR_FIXED;
192 hash_init(&compat_hashtab);
193
194 for (i = 0; i < VV_LEN; ++i)
195 {
196 p = &vimvars[i];
197 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
198 {
199 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
200 getout(1);
201 }
202 STRCPY(p->vv_di.di_key, p->vv_name);
203 if (p->vv_flags & VV_RO)
204 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
205 else if (p->vv_flags & VV_RO_SBX)
206 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
207 else
208 p->vv_di.di_flags = DI_FLAGS_FIX;
209
210 // add to v: scope dict, unless the value is not always available
211 if (p->vv_type != VAR_UNKNOWN)
212 hash_add(&vimvarht, p->vv_di.di_key);
213 if (p->vv_flags & VV_COMPAT)
214 // add to compat scope dict
215 hash_add(&compat_hashtab, p->vv_di.di_key);
216 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200217 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
218 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200219
220 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
221 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100222 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200223 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
224 set_vim_var_list(VV_ERRORS, list_alloc());
225 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
226
227 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
228 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
229 set_vim_var_nr(VV_NONE, VVAL_NONE);
230 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100231 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200232
233 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
234 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
235 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
236 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
237 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
238 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
239 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
240 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
241 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
242 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
243 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
244
245 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
246
Bram Moolenaar439c0362020-06-06 15:58:03 +0200247 // Default for v:register is not 0 but '"'. This is adjusted once the
248 // clipboard has been setup by calling reset_reg_var().
249 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200250}
251
252#if defined(EXITFREE) || defined(PROTO)
253/*
254 * Free all vim variables information on exit
255 */
256 void
257evalvars_clear(void)
258{
259 int i;
260 struct vimvar *p;
261
262 for (i = 0; i < VV_LEN; ++i)
263 {
264 p = &vimvars[i];
265 if (p->vv_di.di_tv.v_type == VAR_STRING)
266 VIM_CLEAR(p->vv_str);
267 else if (p->vv_di.di_tv.v_type == VAR_LIST)
268 {
269 list_unref(p->vv_list);
270 p->vv_list = NULL;
271 }
272 }
273 hash_clear(&vimvarht);
274 hash_init(&vimvarht); // garbage_collect() will access it
275 hash_clear(&compat_hashtab);
276
277 // global variables
278 vars_clear(&globvarht);
279
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100280 // Script-local variables. Clear all the variables here.
281 // The scriptvar_T is cleared later in free_scriptnames(), because a
282 // variable in one script might hold a reference to the whole scope of
283 // another script.
284 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200285 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200286}
287#endif
288
289 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200290garbage_collect_globvars(int copyID)
291{
292 return set_ref_in_ht(&globvarht, copyID, NULL);
293}
294
295 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200296garbage_collect_vimvars(int copyID)
297{
298 return set_ref_in_ht(&vimvarht, copyID, NULL);
299}
300
301 int
302garbage_collect_scriptvars(int copyID)
303{
Bram Moolenaared234f22020-10-15 20:42:20 +0200304 int i;
305 int idx;
306 int abort = FALSE;
307 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200308
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100309 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200310 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200311 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
312
Bram Moolenaared234f22020-10-15 20:42:20 +0200313 si = SCRIPT_ITEM(i);
314 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
315 {
316 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
317
318 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
319 }
320 }
321
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200322 return abort;
323}
324
325/*
326 * Set an internal variable to a string value. Creates the variable if it does
327 * not already exist.
328 */
329 void
330set_internal_string_var(char_u *name, char_u *value)
331{
332 char_u *val;
333 typval_T *tvp;
334
335 val = vim_strsave(value);
336 if (val != NULL)
337 {
338 tvp = alloc_string_tv(val);
339 if (tvp != NULL)
340 {
341 set_var(name, tvp, FALSE);
342 free_tv(tvp);
343 }
344 }
345}
346
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200347 int
348eval_charconvert(
349 char_u *enc_from,
350 char_u *enc_to,
351 char_u *fname_from,
352 char_u *fname_to)
353{
354 int err = FALSE;
355
356 set_vim_var_string(VV_CC_FROM, enc_from, -1);
357 set_vim_var_string(VV_CC_TO, enc_to, -1);
358 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
359 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
360 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
361 err = TRUE;
362 set_vim_var_string(VV_CC_FROM, NULL, -1);
363 set_vim_var_string(VV_CC_TO, NULL, -1);
364 set_vim_var_string(VV_FNAME_IN, NULL, -1);
365 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
366
367 if (err)
368 return FAIL;
369 return OK;
370}
371
372# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
373 int
374eval_printexpr(char_u *fname, char_u *args)
375{
376 int err = FALSE;
377
378 set_vim_var_string(VV_FNAME_IN, fname, -1);
379 set_vim_var_string(VV_CMDARG, args, -1);
380 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
381 err = TRUE;
382 set_vim_var_string(VV_FNAME_IN, NULL, -1);
383 set_vim_var_string(VV_CMDARG, NULL, -1);
384
385 if (err)
386 {
387 mch_remove(fname);
388 return FAIL;
389 }
390 return OK;
391}
392# endif
393
394# if defined(FEAT_DIFF) || defined(PROTO)
395 void
396eval_diff(
397 char_u *origfile,
398 char_u *newfile,
399 char_u *outfile)
400{
401 int err = FALSE;
402
403 set_vim_var_string(VV_FNAME_IN, origfile, -1);
404 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
405 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
406 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
407 set_vim_var_string(VV_FNAME_IN, NULL, -1);
408 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
409 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
410}
411
412 void
413eval_patch(
414 char_u *origfile,
415 char_u *difffile,
416 char_u *outfile)
417{
418 int err;
419
420 set_vim_var_string(VV_FNAME_IN, origfile, -1);
421 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
422 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
423 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
424 set_vim_var_string(VV_FNAME_IN, NULL, -1);
425 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
426 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
427}
428# endif
429
430#if defined(FEAT_SPELL) || defined(PROTO)
431/*
432 * Evaluate an expression to a list with suggestions.
433 * For the "expr:" part of 'spellsuggest'.
434 * Returns NULL when there is an error.
435 */
436 list_T *
437eval_spell_expr(char_u *badword, char_u *expr)
438{
439 typval_T save_val;
440 typval_T rettv;
441 list_T *list = NULL;
442 char_u *p = skipwhite(expr);
443
444 // Set "v:val" to the bad word.
445 prepare_vimvar(VV_VAL, &save_val);
446 set_vim_var_string(VV_VAL, badword, -1);
447 if (p_verbose == 0)
448 ++emsg_off;
449
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200450 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200451 {
452 if (rettv.v_type != VAR_LIST)
453 clear_tv(&rettv);
454 else
455 list = rettv.vval.v_list;
456 }
457
458 if (p_verbose == 0)
459 --emsg_off;
460 clear_tv(get_vim_var_tv(VV_VAL));
461 restore_vimvar(VV_VAL, &save_val);
462
463 return list;
464}
465
466/*
467 * "list" is supposed to contain two items: a word and a number. Return the
468 * word in "pp" and the number as the return value.
469 * Return -1 if anything isn't right.
470 * Used to get the good word and score from the eval_spell_expr() result.
471 */
472 int
473get_spellword(list_T *list, char_u **pp)
474{
475 listitem_T *li;
476
477 li = list->lv_first;
478 if (li == NULL)
479 return -1;
480 *pp = tv_get_string(&li->li_tv);
481
482 li = li->li_next;
483 if (li == NULL)
484 return -1;
485 return (int)tv_get_number(&li->li_tv);
486}
487#endif
488
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200489/*
490 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200491 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200492 * When not used yet add the variable to the v: hashtable.
493 */
494 void
495prepare_vimvar(int idx, typval_T *save_tv)
496{
497 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200498 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200499 if (vimvars[idx].vv_type == VAR_UNKNOWN)
500 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
501}
502
503/*
504 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200505 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200506 * When no longer defined, remove the variable from the v: hashtable.
507 */
508 void
509restore_vimvar(int idx, typval_T *save_tv)
510{
511 hashitem_T *hi;
512
513 vimvars[idx].vv_tv = *save_tv;
514 if (vimvars[idx].vv_type == VAR_UNKNOWN)
515 {
516 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
517 if (HASHITEM_EMPTY(hi))
518 internal_error("restore_vimvar()");
519 else
520 hash_remove(&vimvarht, hi);
521 }
522}
523
524/*
525 * List Vim variables.
526 */
527 static void
528list_vim_vars(int *first)
529{
530 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
531}
532
533/*
534 * List script-local variables, if there is a script.
535 */
536 static void
537list_script_vars(int *first)
538{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200539 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200540 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
541 "s:", FALSE, first);
542}
543
544/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200545 * Get a list of lines from a HERE document. The here document is a list of
546 * lines surrounded by a marker.
547 * cmd << {marker}
548 * {line1}
549 * {line2}
550 * ....
551 * {marker}
552 *
553 * The {marker} is a string. If the optional 'trim' word is supplied before the
554 * marker, then the leading indentation before the lines (matching the
555 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200556 *
557 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
558 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
559 * missing, then '.' is accepted as a marker.
560 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200561 * Returns a List with {lines} or NULL.
562 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200564heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200565{
566 char_u *theline;
567 char_u *marker;
568 list_T *l;
569 char_u *p;
570 int marker_indent_len = 0;
571 int text_indent_len = 0;
572 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200573 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200574 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200575
576 if (eap->getline == NULL)
577 {
578 emsg(_("E991: cannot use =<< here"));
579 return NULL;
580 }
581
582 // Check for the optional 'trim' word before the marker
583 cmd = skipwhite(cmd);
584 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
585 {
586 cmd = skipwhite(cmd + 4);
587
588 // Trim the indentation from all the lines in the here document.
589 // The amount of indentation trimmed is the same as the indentation of
590 // the first line after the :let command line. To find the end marker
591 // the indent of the :let command line is trimmed.
592 p = *eap->cmdlinep;
593 while (VIM_ISWHITE(*p))
594 {
595 p++;
596 marker_indent_len++;
597 }
598 text_indent_len = -1;
599 }
600
601 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200602 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200603 {
604 marker = skipwhite(cmd);
605 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200606 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200607 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200608 semsg(_(e_trailing_arg), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200609 return NULL;
610 }
611 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200612 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200613 {
614 emsg(_("E221: Marker cannot start with lower case letter"));
615 return NULL;
616 }
617 }
618 else
619 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200620 // When getting lines for an embedded script, if the marker is missing,
621 // accept '.' as the marker.
622 if (script_get)
623 marker = dot;
624 else
625 {
626 emsg(_("E172: Missing marker"));
627 return NULL;
628 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200629 }
630
631 l = list_alloc();
632 if (l == NULL)
633 return NULL;
634
635 for (;;)
636 {
637 int mi = 0;
638 int ti = 0;
639
640 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
641 if (theline == NULL)
642 {
643 semsg(_("E990: Missing end marker '%s'"), marker);
644 break;
645 }
646
647 // with "trim": skip the indent matching the :let line to find the
648 // marker
649 if (marker_indent_len > 0
650 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
651 mi = marker_indent_len;
652 if (STRCMP(marker, theline + mi) == 0)
653 {
654 vim_free(theline);
655 break;
656 }
657
658 if (text_indent_len == -1 && *theline != NUL)
659 {
660 // set the text indent from the first line.
661 p = theline;
662 text_indent_len = 0;
663 while (VIM_ISWHITE(*p))
664 {
665 p++;
666 text_indent_len++;
667 }
668 text_indent = vim_strnsave(theline, text_indent_len);
669 }
670 // with "trim": skip the indent matching the first line
671 if (text_indent != NULL)
672 for (ti = 0; ti < text_indent_len; ++ti)
673 if (theline[ti] != text_indent[ti])
674 break;
675
676 if (list_append_string(l, theline + ti, -1) == FAIL)
677 break;
678 vim_free(theline);
679 }
680 vim_free(text_indent);
681
682 return l;
683}
684
685/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200686 * Vim9 variable declaration:
687 * ":var name"
688 * ":var name: type"
689 * ":var name = expr"
690 * ":var name: type = expr"
691 * etc.
692 */
693 void
694ex_var(exarg_T *eap)
695{
696 if (!in_vim9script())
697 {
698 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
699 return;
700 }
701 ex_let(eap);
702}
703
704/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200705 * ":let" list all variable values
706 * ":let var1 var2" list variable values
707 * ":let var = expr" assignment command.
708 * ":let var += expr" assignment command.
709 * ":let var -= expr" assignment command.
710 * ":let var *= expr" assignment command.
711 * ":let var /= expr" assignment command.
712 * ":let var %= expr" assignment command.
713 * ":let var .= expr" assignment command.
714 * ":let var ..= expr" assignment command.
715 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100716 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100717 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200718 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200719 * ":final var = expr" assignment command.
720 * ":final [var1, var2] = expr" unpack list.
721 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200722 * ":const" list all variable values
723 * ":const var1 var2" list variable values
724 * ":const var = expr" assignment command.
725 * ":const [var1, var2] = expr" unpack list.
726 */
727 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200728ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200729{
730 char_u *arg = eap->arg;
731 char_u *expr = NULL;
732 typval_T rettv;
733 int i;
734 int var_count = 0;
735 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200736 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200737 char_u *argend;
738 int first = TRUE;
739 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200740 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100741 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200742 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200743
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200744 if (eap->cmdidx == CMD_final && !vim9script)
745 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100746 // In legacy Vim script ":final" is short for ":finally".
747 ex_finally(eap);
748 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200749 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200750 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200751 {
752 emsg(_(e_cannot_use_let_in_vim9_script));
753 return;
754 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200755
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100756 if (eap->cmdidx == CMD_const)
757 flags |= ASSIGN_CONST;
758 else if (eap->cmdidx == CMD_final)
759 flags |= ASSIGN_FINAL;
760
761 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200763 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200765 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200766 if (argend == NULL)
767 return;
768 if (argend > arg && argend[-1] == '.') // for var.='str'
769 --argend;
770 expr = skipwhite(argend);
771 concat = expr[0] == '.'
772 && ((expr[1] == '=' && current_sctx.sc_version < 2)
773 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200774 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
775 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200776 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200777 {
778 // ":let" without "=": list variables
779 if (*arg == '[')
780 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200781 else if (expr[0] == '.' && expr[1] == '=')
782 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200783 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200784 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200785 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200786 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100787 // Vim9 declaration ":var name: type"
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200788 arg = vim9_declare_scriptvar(eap, arg);
789 }
790 else
791 {
792 // ":let var1 var2" - list values
793 arg = list_arg_vars(eap, arg, &first);
794 }
795 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200796 else if (!eap->skip)
797 {
798 // ":let"
799 list_glob_vars(&first);
800 list_buf_vars(&first);
801 list_win_vars(&first);
802 list_tab_vars(&first);
803 list_script_vars(&first);
804 list_func_vars(&first);
805 list_vim_vars(&first);
806 }
807 eap->nextcmd = check_nextcmd(arg);
808 }
809 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
810 {
811 list_T *l;
812
813 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200814 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200815 if (l != NULL)
816 {
817 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200818 if (!eap->skip)
819 {
820 op[0] = '=';
821 op[1] = NUL;
822 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200824 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200825 clear_tv(&rettv);
826 }
827 }
828 else
829 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200830 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200831 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200832
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200833 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200834 i = FAIL;
835 if (has_assign || concat)
836 {
837 op[0] = '=';
838 op[1] = NUL;
839 if (*expr != '=')
840 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200841 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200842 {
843 // +=, /=, etc. require an existing variable
844 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
845 i = FAIL;
846 }
847 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200848 {
849 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200850 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200851 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200852 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200853 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200854 ++len;
855 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200856 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200857 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200858 }
859 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200860 ++expr;
861
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200862 if (vim9script && (!VIM_ISWHITE(*argend)
863 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200864 {
865 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100866 semsg(_(e_white_space_required_before_and_after_str_at_str),
867 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200868 i = FAIL;
869 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200870
871 if (eap->skip)
872 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200873 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200874 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200875 if (getline_equal(eap->getline, eap->cookie, getsourceline))
876 {
877 evalarg.eval_getline = eap->getline;
878 evalarg.eval_cookie = eap->cookie;
879 }
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200880 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200881 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200882 if (eap->skip)
883 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200884 clear_evalarg(&evalarg, eap);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200885 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200886 if (eap->skip)
887 {
888 if (i != FAIL)
889 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200890 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200891 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200892 {
893 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200894 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200895 clear_tv(&rettv);
896 }
897 }
898}
899
900/*
901 * Assign the typevalue "tv" to the variable or variables at "arg_start".
902 * Handles both "var" with any type and "[var, var; var]" with a list type.
903 * When "op" is not NULL it points to a string with characters that
904 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
905 * or concatenate.
906 * Returns OK or FAIL;
907 */
908 int
909ex_let_vars(
910 char_u *arg_start,
911 typval_T *tv,
912 int copy, // copy values from "tv", don't move
913 int semicolon, // from skip_var_list()
914 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100915 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200916 char_u *op)
917{
918 char_u *arg = arg_start;
919 list_T *l;
920 int i;
921 listitem_T *item;
922 typval_T ltv;
923
924 if (*arg != '[')
925 {
926 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200928 return FAIL;
929 return OK;
930 }
931
932 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
933 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
934 {
935 emsg(_(e_listreq));
936 return FAIL;
937 }
938
939 i = list_len(l);
940 if (semicolon == 0 && var_count < i)
941 {
942 emsg(_("E687: Less targets than List items"));
943 return FAIL;
944 }
945 if (var_count - semicolon > i)
946 {
947 emsg(_("E688: More targets than List items"));
948 return FAIL;
949 }
950
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200951 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200952 item = l->lv_first;
953 while (*arg != ']')
954 {
955 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200957 item = item->li_next;
958 if (arg == NULL)
959 return FAIL;
960
961 arg = skipwhite(arg);
962 if (*arg == ';')
963 {
964 // Put the rest of the list (may be empty) in the var after ';'.
965 // Create a new list for this.
966 l = list_alloc();
967 if (l == NULL)
968 return FAIL;
969 while (item != NULL)
970 {
971 list_append_tv(l, &item->li_tv);
972 item = item->li_next;
973 }
974
975 ltv.v_type = VAR_LIST;
976 ltv.v_lock = 0;
977 ltv.vval.v_list = l;
978 l->lv_refcount = 1;
979
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200981 (char_u *)"]", op);
982 clear_tv(&ltv);
983 if (arg == NULL)
984 return FAIL;
985 break;
986 }
987 else if (*arg != ',' && *arg != ']')
988 {
989 internal_error("ex_let_vars()");
990 return FAIL;
991 }
992 }
993
994 return OK;
995}
996
997/*
998 * Skip over assignable variable "var" or list of variables "[var, var]".
999 * Used for ":let varvar = expr" and ":for varvar in expr".
1000 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001001 * for "[var, var; var]" set "semicolon" to 1.
1002 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001003 * Return NULL for an error.
1004 */
1005 char_u *
1006skip_var_list(
1007 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001008 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001009 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001010 int *semicolon,
1011 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001012{
1013 char_u *p, *s;
1014
1015 if (*arg == '[')
1016 {
1017 // "[var, var]": find the matching ']'.
1018 p = arg;
1019 for (;;)
1020 {
1021 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001022 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001023 if (s == p)
1024 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001025 if (!silent)
1026 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001027 return NULL;
1028 }
1029 ++*var_count;
1030
1031 p = skipwhite(s);
1032 if (*p == ']')
1033 break;
1034 else if (*p == ';')
1035 {
1036 if (*semicolon == 1)
1037 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001038 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001039 return NULL;
1040 }
1041 *semicolon = 1;
1042 }
1043 else if (*p != ',')
1044 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001045 if (!silent)
1046 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001047 return NULL;
1048 }
1049 }
1050 return p + 1;
1051 }
1052 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001054}
1055
1056/*
1057 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1058 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001060 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001061 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001062skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001063{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001064 char_u *end;
1065 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001067 if (*arg == '@' && arg[1] != NUL)
1068 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001069 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001070 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001071
1072 // "a: type" is declaring variable "a" with a type, not "a:".
1073 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001074 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001075 --end;
1076
Bram Moolenaar585587d2021-01-17 20:52:13 +01001077 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001078 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001079 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001080 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 }
1082 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001083}
1084
1085/*
1086 * List variables for hashtab "ht" with prefix "prefix".
1087 * If "empty" is TRUE also list NULL strings as empty strings.
1088 */
1089 void
1090list_hashtable_vars(
1091 hashtab_T *ht,
1092 char *prefix,
1093 int empty,
1094 int *first)
1095{
1096 hashitem_T *hi;
1097 dictitem_T *di;
1098 int todo;
1099 char_u buf[IOSIZE];
1100
1101 todo = (int)ht->ht_used;
1102 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1103 {
1104 if (!HASHITEM_EMPTY(hi))
1105 {
1106 --todo;
1107 di = HI2DI(hi);
1108
1109 // apply :filter /pat/ to variable name
1110 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1111 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1112 if (message_filtered(buf))
1113 continue;
1114
1115 if (empty || di->di_tv.v_type != VAR_STRING
1116 || di->di_tv.vval.v_string != NULL)
1117 list_one_var(di, prefix, first);
1118 }
1119 }
1120}
1121
1122/*
1123 * List global variables.
1124 */
1125 static void
1126list_glob_vars(int *first)
1127{
1128 list_hashtable_vars(&globvarht, "", TRUE, first);
1129}
1130
1131/*
1132 * List buffer variables.
1133 */
1134 static void
1135list_buf_vars(int *first)
1136{
1137 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1138}
1139
1140/*
1141 * List window variables.
1142 */
1143 static void
1144list_win_vars(int *first)
1145{
1146 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1147}
1148
1149/*
1150 * List tab page variables.
1151 */
1152 static void
1153list_tab_vars(int *first)
1154{
1155 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1156}
1157
1158/*
1159 * List variables in "arg".
1160 */
1161 static char_u *
1162list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1163{
1164 int error = FALSE;
1165 int len;
1166 char_u *name;
1167 char_u *name_start;
1168 char_u *arg_subsc;
1169 char_u *tofree;
1170 typval_T tv;
1171
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001172 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001173 {
1174 if (error || eap->skip)
1175 {
1176 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1177 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1178 {
1179 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001180 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001181 break;
1182 }
1183 }
1184 else
1185 {
1186 // get_name_len() takes care of expanding curly braces
1187 name_start = name = arg;
1188 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1189 if (len <= 0)
1190 {
1191 // This is mainly to keep test 49 working: when expanding
1192 // curly braces fails overrule the exception error message.
1193 if (len < 0 && !aborting())
1194 {
1195 emsg_severe = TRUE;
1196 semsg(_(e_invarg2), arg);
1197 break;
1198 }
1199 error = TRUE;
1200 }
1201 else
1202 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001203 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001204 if (tofree != NULL)
1205 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001206 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001207 error = TRUE;
1208 else
1209 {
1210 // handle d.key, l[idx], f(expr)
1211 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001212 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001213 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001214 error = TRUE;
1215 else
1216 {
1217 if (arg == arg_subsc && len == 2 && name[1] == ':')
1218 {
1219 switch (*name)
1220 {
1221 case 'g': list_glob_vars(first); break;
1222 case 'b': list_buf_vars(first); break;
1223 case 'w': list_win_vars(first); break;
1224 case 't': list_tab_vars(first); break;
1225 case 'v': list_vim_vars(first); break;
1226 case 's': list_script_vars(first); break;
1227 case 'l': list_func_vars(first); break;
1228 default:
1229 semsg(_("E738: Can't list variables for %s"), name);
1230 }
1231 }
1232 else
1233 {
1234 char_u numbuf[NUMBUFLEN];
1235 char_u *tf;
1236 int c;
1237 char_u *s;
1238
1239 s = echo_string(&tv, &tf, numbuf, 0);
1240 c = *arg;
1241 *arg = NUL;
1242 list_one_var_a("",
1243 arg == arg_subsc ? name : name_start,
1244 tv.v_type,
1245 s == NULL ? (char_u *)"" : s,
1246 first);
1247 *arg = c;
1248 vim_free(tf);
1249 }
1250 clear_tv(&tv);
1251 }
1252 }
1253 }
1254
1255 vim_free(tofree);
1256 }
1257
1258 arg = skipwhite(arg);
1259 }
1260
1261 return arg;
1262}
1263
1264/*
1265 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1266 * Returns a pointer to the char just after the var name.
1267 * Returns NULL if there is an error.
1268 */
1269 static char_u *
1270ex_let_one(
1271 char_u *arg, // points to variable name
1272 typval_T *tv, // value to assign to variable
1273 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001274 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001275 char_u *endchars, // valid chars after variable name or NULL
1276 char_u *op) // "+", "-", "." or NULL
1277{
1278 int c1;
1279 char_u *name;
1280 char_u *p;
1281 char_u *arg_end = NULL;
1282 int len;
1283 int opt_flags;
1284 char_u *tofree = NULL;
1285
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001286 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001287 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001288 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1289 {
1290 vim9_declare_error(arg);
1291 return NULL;
1292 }
1293
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001294 // ":let $VAR = expr": Set environment variable.
1295 if (*arg == '$')
1296 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001297 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001298 {
1299 emsg(_("E996: Cannot lock an environment variable"));
1300 return NULL;
1301 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001302
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001303 // Find the end of the name.
1304 ++arg;
1305 name = arg;
1306 len = get_env_len(&arg);
1307 if (len == 0)
1308 semsg(_(e_invarg2), name - 1);
1309 else
1310 {
1311 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1312 semsg(_(e_letwrong), op);
1313 else if (endchars != NULL
1314 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1315 emsg(_(e_letunexp));
1316 else if (!check_secure())
1317 {
1318 c1 = name[len];
1319 name[len] = NUL;
1320 p = tv_get_string_chk(tv);
1321 if (p != NULL && op != NULL && *op == '.')
1322 {
1323 int mustfree = FALSE;
1324 char_u *s = vim_getenv(name, &mustfree);
1325
1326 if (s != NULL)
1327 {
1328 p = tofree = concat_str(s, p);
1329 if (mustfree)
1330 vim_free(s);
1331 }
1332 }
1333 if (p != NULL)
1334 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001335 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001336 arg_end = arg;
1337 }
1338 name[len] = c1;
1339 vim_free(tofree);
1340 }
1341 }
1342 }
1343
1344 // ":let &option = expr": Set option value.
1345 // ":let &l:option = expr": Set local option value.
1346 // ":let &g:option = expr": Set global option value.
1347 else if (*arg == '&')
1348 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001349 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001350 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001351 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001352 return NULL;
1353 }
1354 // Find the end of the name.
1355 p = find_option_end(&arg, &opt_flags);
1356 if (p == NULL || (endchars != NULL
1357 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1358 emsg(_(e_letunexp));
1359 else
1360 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001361 long n = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001362 getoption_T opt_type;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001363 long numval;
1364 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001365 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001366 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001367
1368 c1 = *p;
1369 *p = NUL;
1370
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001371 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001372 if ((opt_type == gov_bool
1373 || opt_type == gov_number
1374 || opt_type == gov_hidden_bool
1375 || opt_type == gov_hidden_number)
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001376 && (tv->v_type != VAR_STRING || !in_vim9script()))
Bram Moolenaar0ea04402021-01-04 13:37:54 +01001377 {
1378 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1379 // bool, possibly hidden
1380 n = (long)tv_get_bool(tv);
1381 else
1382 // number, possibly hidden
1383 n = (long)tv_get_number(tv);
1384 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001385
1386 // Avoid setting a string option to the text "v:false" or similar.
1387 // In Vim9 script also don't convert a number to string.
1388 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1389 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1390 s = tv_get_string_chk(tv);
1391
1392 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001393 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001394 if (((opt_type == gov_bool || opt_type == gov_number)
1395 && *op == '.')
1396 || (opt_type == gov_string && *op != '.'))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001397 {
1398 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001399 failed = TRUE; // don't set the value
1400
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001401 }
1402 else
1403 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001404 // number, in legacy script also bool
1405 if (opt_type == gov_number
1406 || (opt_type == gov_bool && !in_vim9script()))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001407 {
1408 switch (*op)
1409 {
1410 case '+': n = numval + n; break;
1411 case '-': n = numval - n; break;
1412 case '*': n = numval * n; break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001413 case '/': n = (long)num_divide(numval, n,
1414 &failed); break;
1415 case '%': n = (long)num_modulus(numval, n,
1416 &failed); break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001417 }
1418 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001419 else if (opt_type == gov_string
1420 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001421 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001422 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001423 s = concat_str(stringval, s);
1424 vim_free(stringval);
1425 stringval = s;
1426 }
1427 }
1428 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001429
1430 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001431 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001432 if (opt_type != gov_string || s != NULL)
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001433 {
1434 set_option_value(arg, n, s, opt_flags);
1435 arg_end = p;
1436 }
1437 else
1438 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001439 }
1440 *p = c1;
1441 vim_free(stringval);
1442 }
1443 }
1444
1445 // ":let @r = expr": Set register contents.
1446 else if (*arg == '@')
1447 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001448 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001449 {
1450 emsg(_("E996: Cannot lock a register"));
1451 return NULL;
1452 }
1453 ++arg;
1454 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1455 semsg(_(e_letwrong), op);
1456 else if (endchars != NULL
1457 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1458 emsg(_(e_letunexp));
1459 else
1460 {
1461 char_u *ptofree = NULL;
1462 char_u *s;
1463
1464 p = tv_get_string_chk(tv);
1465 if (p != NULL && op != NULL && *op == '.')
1466 {
1467 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1468 if (s != NULL)
1469 {
1470 p = ptofree = concat_str(s, p);
1471 vim_free(s);
1472 }
1473 }
1474 if (p != NULL)
1475 {
1476 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1477 arg_end = arg + 1;
1478 }
1479 vim_free(ptofree);
1480 }
1481 }
1482
1483 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001484 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001485 // ":let {expr} = expr": Idem, name made with curly braces
1486 else if (eval_isnamec1(*arg) || *arg == '{')
1487 {
1488 lval_T lv;
1489
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001490 p = get_lval(arg, tv, &lv, FALSE, FALSE,
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001491 (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1492 ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001493 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001494 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495 if (endchars != NULL && vim_strchr(endchars,
1496 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001497 emsg(_(e_letunexp));
1498 else
1499 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001501 arg_end = p;
1502 }
1503 }
1504 clear_lval(&lv);
1505 }
1506
1507 else
1508 semsg(_(e_invarg2), arg);
1509
1510 return arg_end;
1511}
1512
1513/*
1514 * ":unlet[!] var1 ... " command.
1515 */
1516 void
1517ex_unlet(exarg_T *eap)
1518{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001519 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001520}
1521
1522/*
1523 * ":lockvar" and ":unlockvar" commands
1524 */
1525 void
1526ex_lockvar(exarg_T *eap)
1527{
1528 char_u *arg = eap->arg;
1529 int deep = 2;
1530
1531 if (eap->forceit)
1532 deep = -1;
1533 else if (vim_isdigit(*arg))
1534 {
1535 deep = getdigits(&arg);
1536 arg = skipwhite(arg);
1537 }
1538
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001539 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001540}
1541
1542/*
1543 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001544 * Also used for Vim9 script. "callback" is invoked as:
1545 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001546 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001547 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001548ex_unletlock(
1549 exarg_T *eap,
1550 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001551 int deep,
1552 int glv_flags,
1553 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1554 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001555{
1556 char_u *arg = argstart;
1557 char_u *name_end;
1558 int error = FALSE;
1559 lval_T lv;
1560
1561 do
1562 {
1563 if (*arg == '$')
1564 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001565 lv.ll_name = arg;
1566 lv.ll_tv = NULL;
1567 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001568 if (get_env_len(&arg) == 0)
1569 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001570 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001571 return;
1572 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001573 if (!error && !eap->skip
1574 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1575 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001576 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001577 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001578 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001579 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001580 // Parse the name and find the end.
1581 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001582 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001583 if (lv.ll_name == NULL)
1584 error = TRUE; // error but continue parsing
1585 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1586 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001587 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001588 if (name_end != NULL)
1589 {
1590 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001591 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001592 }
1593 if (!(eap->skip || error))
1594 clear_lval(&lv);
1595 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001596 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001597
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001598 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001599 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001600 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001601
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001602 if (!eap->skip)
1603 clear_lval(&lv);
1604 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001605
1606 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001607 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001608
1609 eap->nextcmd = check_nextcmd(arg);
1610}
1611
1612 static int
1613do_unlet_var(
1614 lval_T *lp,
1615 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001616 exarg_T *eap,
1617 int deep UNUSED,
1618 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001619{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001620 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001621 int ret = OK;
1622 int cc;
1623
1624 if (lp->ll_tv == NULL)
1625 {
1626 cc = *name_end;
1627 *name_end = NUL;
1628
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001629 // Environment variable, normal name or expanded name.
1630 if (*lp->ll_name == '$')
1631 vim_unsetenv(lp->ll_name + 1);
1632 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001633 ret = FAIL;
1634 *name_end = cc;
1635 }
1636 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001637 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001638 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001639 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001640 return FAIL;
1641 else if (lp->ll_range)
1642 {
1643 listitem_T *li;
1644 listitem_T *ll_li = lp->ll_li;
1645 int ll_n1 = lp->ll_n1;
1646
1647 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1648 {
1649 li = ll_li->li_next;
Bram Moolenaara187c432020-09-16 21:08:28 +02001650 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001651 return FAIL;
1652 ll_li = li;
1653 ++ll_n1;
1654 }
1655
1656 // Delete a range of List items.
1657 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1658 {
1659 li = lp->ll_li->li_next;
1660 listitem_remove(lp->ll_list, lp->ll_li);
1661 lp->ll_li = li;
1662 ++lp->ll_n1;
1663 }
1664 }
1665 else
1666 {
1667 if (lp->ll_list != NULL)
1668 // unlet a List item.
1669 listitem_remove(lp->ll_list, lp->ll_li);
1670 else
1671 // unlet a Dictionary item.
1672 dictitem_remove(lp->ll_dict, lp->ll_di);
1673 }
1674
1675 return ret;
1676}
1677
1678/*
1679 * "unlet" a variable. Return OK if it existed, FAIL if not.
1680 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1681 */
1682 int
1683do_unlet(char_u *name, int forceit)
1684{
1685 hashtab_T *ht;
1686 hashitem_T *hi;
1687 char_u *varname;
1688 dict_T *d;
1689 dictitem_T *di;
1690
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001691 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001692 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001693 return FAIL;
1694
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001695 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001696
1697 // can't :unlet a script variable in Vim9 script from a function
1698 if (ht == get_script_local_ht()
1699 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1700 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1701 == SCRIPT_VERSION_VIM9
1702 && check_vim9_unlet(name) == FAIL)
1703 return FAIL;
1704
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001705 if (ht != NULL && *varname != NUL)
1706 {
1707 d = get_current_funccal_dict(ht);
1708 if (d == NULL)
1709 {
1710 if (ht == &globvarht)
1711 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001712 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001713 d = &vimvardict;
1714 else
1715 {
1716 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1717 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1718 }
1719 if (d == NULL)
1720 {
1721 internal_error("do_unlet()");
1722 return FAIL;
1723 }
1724 }
1725 hi = hash_find(ht, varname);
1726 if (HASHITEM_EMPTY(hi))
1727 hi = find_hi_in_scoped_ht(name, &ht);
1728 if (hi != NULL && !HASHITEM_EMPTY(hi))
1729 {
1730 di = HI2DI(hi);
1731 if (var_check_fixed(di->di_flags, name, FALSE)
1732 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001733 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001734 return FAIL;
1735
1736 delete_var(ht, hi);
1737 return OK;
1738 }
1739 }
1740 if (forceit)
1741 return OK;
1742 semsg(_("E108: No such variable: \"%s\""), name);
1743 return FAIL;
1744}
1745
1746/*
1747 * Lock or unlock variable indicated by "lp".
1748 * "deep" is the levels to go (-1 for unlimited);
1749 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1750 */
1751 static int
1752do_lock_var(
1753 lval_T *lp,
1754 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001755 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001756 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001757 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001758{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001759 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001760 int ret = OK;
1761 int cc;
1762 dictitem_T *di;
1763
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001764 if (lp->ll_tv == NULL)
1765 {
1766 cc = *name_end;
1767 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001768 if (*lp->ll_name == '$')
1769 {
1770 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001771 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001772 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001773 else
1774 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001775 // Normal name or expanded name.
1776 di = find_var(lp->ll_name, NULL, TRUE);
1777 if (di == NULL)
1778 ret = FAIL;
1779 else if ((di->di_flags & DI_FLAGS_FIX)
1780 && di->di_tv.v_type != VAR_DICT
1781 && di->di_tv.v_type != VAR_LIST)
1782 {
1783 // For historic reasons this error is not given for a list or
1784 // dict. E.g., the b: dict could be locked/unlocked.
1785 semsg(_(e_lock_unlock), lp->ll_name);
1786 ret = FAIL;
1787 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001788 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001789 {
1790 if (lock)
1791 di->di_flags |= DI_FLAGS_LOCK;
1792 else
1793 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001794 if (deep != 0)
1795 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001796 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001797 }
1798 *name_end = cc;
1799 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001800 else if (deep == 0)
1801 {
1802 // nothing to do
1803 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001804 else if (lp->ll_range)
1805 {
1806 listitem_T *li = lp->ll_li;
1807
1808 // (un)lock a range of List items.
1809 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1810 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001811 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001812 li = li->li_next;
1813 ++lp->ll_n1;
1814 }
1815 }
1816 else if (lp->ll_list != NULL)
1817 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001818 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001819 else
1820 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001821 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001822
1823 return ret;
1824}
1825
1826/*
1827 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001828 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1829 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001830 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001831 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001832item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001833{
1834 static int recurse = 0;
1835 list_T *l;
1836 listitem_T *li;
1837 dict_T *d;
1838 blob_T *b;
1839 hashitem_T *hi;
1840 int todo;
1841
1842 if (recurse >= DICT_MAXNEST)
1843 {
1844 emsg(_("E743: variable nested too deep for (un)lock"));
1845 return;
1846 }
1847 if (deep == 0)
1848 return;
1849 ++recurse;
1850
1851 // lock/unlock the item itself
1852 if (lock)
1853 tv->v_lock |= VAR_LOCKED;
1854 else
1855 tv->v_lock &= ~VAR_LOCKED;
1856
1857 switch (tv->v_type)
1858 {
1859 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001860 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001861 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001862 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001863 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001864 case VAR_STRING:
1865 case VAR_FUNC:
1866 case VAR_PARTIAL:
1867 case VAR_FLOAT:
1868 case VAR_SPECIAL:
1869 case VAR_JOB:
1870 case VAR_CHANNEL:
1871 break;
1872
1873 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001874 if ((b = tv->vval.v_blob) != NULL
1875 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001876 {
1877 if (lock)
1878 b->bv_lock |= VAR_LOCKED;
1879 else
1880 b->bv_lock &= ~VAR_LOCKED;
1881 }
1882 break;
1883 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001884 if ((l = tv->vval.v_list) != NULL
1885 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001886 {
1887 if (lock)
1888 l->lv_lock |= VAR_LOCKED;
1889 else
1890 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001891 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001892 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001893 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001894 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001895 }
1896 break;
1897 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001898 if ((d = tv->vval.v_dict) != NULL
1899 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001900 {
1901 if (lock)
1902 d->dv_lock |= VAR_LOCKED;
1903 else
1904 d->dv_lock &= ~VAR_LOCKED;
1905 if (deep < 0 || deep > 1)
1906 {
1907 // recursive: lock/unlock the items the List contains
1908 todo = (int)d->dv_hashtab.ht_used;
1909 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1910 {
1911 if (!HASHITEM_EMPTY(hi))
1912 {
1913 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001914 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1915 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001916 }
1917 }
1918 }
1919 }
1920 }
1921 --recurse;
1922}
1923
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001924#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1925/*
1926 * Delete all "menutrans_" variables.
1927 */
1928 void
1929del_menutrans_vars(void)
1930{
1931 hashitem_T *hi;
1932 int todo;
1933
1934 hash_lock(&globvarht);
1935 todo = (int)globvarht.ht_used;
1936 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1937 {
1938 if (!HASHITEM_EMPTY(hi))
1939 {
1940 --todo;
1941 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1942 delete_var(&globvarht, hi);
1943 }
1944 }
1945 hash_unlock(&globvarht);
1946}
1947#endif
1948
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001949/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001950 * Local string buffer for the next two functions to store a variable name
1951 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1952 * get_user_var_name().
1953 */
1954
1955static char_u *varnamebuf = NULL;
1956static int varnamebuflen = 0;
1957
1958/*
1959 * Function to concatenate a prefix and a variable name.
1960 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001961 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001962cat_prefix_varname(int prefix, char_u *name)
1963{
1964 int len;
1965
1966 len = (int)STRLEN(name) + 3;
1967 if (len > varnamebuflen)
1968 {
1969 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001970 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001971 varnamebuf = alloc(len);
1972 if (varnamebuf == NULL)
1973 {
1974 varnamebuflen = 0;
1975 return NULL;
1976 }
1977 varnamebuflen = len;
1978 }
1979 *varnamebuf = prefix;
1980 varnamebuf[1] = ':';
1981 STRCPY(varnamebuf + 2, name);
1982 return varnamebuf;
1983}
1984
1985/*
1986 * Function given to ExpandGeneric() to obtain the list of user defined
1987 * (global/buffer/window/built-in) variable names.
1988 */
1989 char_u *
1990get_user_var_name(expand_T *xp, int idx)
1991{
1992 static long_u gdone;
1993 static long_u bdone;
1994 static long_u wdone;
1995 static long_u tdone;
1996 static int vidx;
1997 static hashitem_T *hi;
1998 hashtab_T *ht;
1999
2000 if (idx == 0)
2001 {
2002 gdone = bdone = wdone = vidx = 0;
2003 tdone = 0;
2004 }
2005
2006 // Global variables
2007 if (gdone < globvarht.ht_used)
2008 {
2009 if (gdone++ == 0)
2010 hi = globvarht.ht_array;
2011 else
2012 ++hi;
2013 while (HASHITEM_EMPTY(hi))
2014 ++hi;
2015 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2016 return cat_prefix_varname('g', hi->hi_key);
2017 return hi->hi_key;
2018 }
2019
2020 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002021 ht =
2022#ifdef FEAT_CMDWIN
2023 // In cmdwin, the alternative buffer should be used.
2024 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2025 &prevwin->w_buffer->b_vars->dv_hashtab :
2026#endif
2027 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002028 if (bdone < ht->ht_used)
2029 {
2030 if (bdone++ == 0)
2031 hi = ht->ht_array;
2032 else
2033 ++hi;
2034 while (HASHITEM_EMPTY(hi))
2035 ++hi;
2036 return cat_prefix_varname('b', hi->hi_key);
2037 }
2038
2039 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002040 ht =
2041#ifdef FEAT_CMDWIN
2042 // In cmdwin, the alternative window should be used.
2043 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2044 &prevwin->w_vars->dv_hashtab :
2045#endif
2046 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002047 if (wdone < ht->ht_used)
2048 {
2049 if (wdone++ == 0)
2050 hi = ht->ht_array;
2051 else
2052 ++hi;
2053 while (HASHITEM_EMPTY(hi))
2054 ++hi;
2055 return cat_prefix_varname('w', hi->hi_key);
2056 }
2057
2058 // t: variables
2059 ht = &curtab->tp_vars->dv_hashtab;
2060 if (tdone < ht->ht_used)
2061 {
2062 if (tdone++ == 0)
2063 hi = ht->ht_array;
2064 else
2065 ++hi;
2066 while (HASHITEM_EMPTY(hi))
2067 ++hi;
2068 return cat_prefix_varname('t', hi->hi_key);
2069 }
2070
2071 // v: variables
2072 if (vidx < VV_LEN)
2073 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2074
2075 VIM_CLEAR(varnamebuf);
2076 varnamebuflen = 0;
2077 return NULL;
2078}
2079
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002080 char *
2081get_var_special_name(int nr)
2082{
2083 switch (nr)
2084 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002085 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2086 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002087 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002088 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002089 }
2090 internal_error("get_var_special_name()");
2091 return "42";
2092}
2093
2094/*
2095 * Returns the global variable dictionary
2096 */
2097 dict_T *
2098get_globvar_dict(void)
2099{
2100 return &globvardict;
2101}
2102
2103/*
2104 * Returns the global variable hash table
2105 */
2106 hashtab_T *
2107get_globvar_ht(void)
2108{
2109 return &globvarht;
2110}
2111
2112/*
2113 * Returns the v: variable dictionary
2114 */
2115 dict_T *
2116get_vimvar_dict(void)
2117{
2118 return &vimvardict;
2119}
2120
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002121/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002123 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124 */
2125 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002126find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002127{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002128 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2129 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002130
2131 if (di == NULL)
2132 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002133 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2135 return (int)(vv - vimvars);
2136}
2137
2138
2139/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002140 * Set type of v: variable to "type".
2141 */
2142 void
2143set_vim_var_type(int idx, vartype_T type)
2144{
2145 vimvars[idx].vv_type = type;
2146}
2147
2148/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002149 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002150 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002151 */
2152 void
2153set_vim_var_nr(int idx, varnumber_T val)
2154{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002155 vimvars[idx].vv_nr = val;
2156}
2157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002158 char *
2159get_vim_var_name(int idx)
2160{
2161 return vimvars[idx].vv_name;
2162}
2163
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002164/*
2165 * Get typval_T v: variable value.
2166 */
2167 typval_T *
2168get_vim_var_tv(int idx)
2169{
2170 return &vimvars[idx].vv_tv;
2171}
2172
2173/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002174 * Set v: variable to "tv". Only accepts the same type.
2175 * Takes over the value of "tv".
2176 */
2177 int
2178set_vim_var_tv(int idx, typval_T *tv)
2179{
2180 if (vimvars[idx].vv_type != tv->v_type)
2181 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002182 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002183 clear_tv(tv);
2184 return FAIL;
2185 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002186 // VV_RO is also checked when compiling, but let's check here as well.
2187 if (vimvars[idx].vv_flags & VV_RO)
2188 {
2189 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2190 return FAIL;
2191 }
2192 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2193 {
2194 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2195 return FAIL;
2196 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002197 clear_tv(&vimvars[idx].vv_di.di_tv);
2198 vimvars[idx].vv_di.di_tv = *tv;
2199 return OK;
2200}
2201
2202/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002203 * Get number v: variable value.
2204 */
2205 varnumber_T
2206get_vim_var_nr(int idx)
2207{
2208 return vimvars[idx].vv_nr;
2209}
2210
2211/*
2212 * Get string v: variable value. Uses a static buffer, can only be used once.
2213 * If the String variable has never been set, return an empty string.
2214 * Never returns NULL;
2215 */
2216 char_u *
2217get_vim_var_str(int idx)
2218{
2219 return tv_get_string(&vimvars[idx].vv_tv);
2220}
2221
2222/*
2223 * Get List v: variable value. Caller must take care of reference count when
2224 * needed.
2225 */
2226 list_T *
2227get_vim_var_list(int idx)
2228{
2229 return vimvars[idx].vv_list;
2230}
2231
2232/*
2233 * Get Dict v: variable value. Caller must take care of reference count when
2234 * needed.
2235 */
2236 dict_T *
2237get_vim_var_dict(int idx)
2238{
2239 return vimvars[idx].vv_dict;
2240}
2241
2242/*
2243 * Set v:char to character "c".
2244 */
2245 void
2246set_vim_var_char(int c)
2247{
2248 char_u buf[MB_MAXBYTES + 1];
2249
2250 if (has_mbyte)
2251 buf[(*mb_char2bytes)(c, buf)] = NUL;
2252 else
2253 {
2254 buf[0] = c;
2255 buf[1] = NUL;
2256 }
2257 set_vim_var_string(VV_CHAR, buf, -1);
2258}
2259
2260/*
2261 * Set v:count to "count" and v:count1 to "count1".
2262 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2263 */
2264 void
2265set_vcount(
2266 long count,
2267 long count1,
2268 int set_prevcount)
2269{
2270 if (set_prevcount)
2271 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2272 vimvars[VV_COUNT].vv_nr = count;
2273 vimvars[VV_COUNT1].vv_nr = count1;
2274}
2275
2276/*
2277 * Save variables that might be changed as a side effect. Used when executing
2278 * a timer callback.
2279 */
2280 void
2281save_vimvars(vimvars_save_T *vvsave)
2282{
2283 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2284 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2285 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2286}
2287
2288/*
2289 * Restore variables saved by save_vimvars().
2290 */
2291 void
2292restore_vimvars(vimvars_save_T *vvsave)
2293{
2294 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2295 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2296 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2297}
2298
2299/*
2300 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2301 * value.
2302 */
2303 void
2304set_vim_var_string(
2305 int idx,
2306 char_u *val,
2307 int len) // length of "val" to use or -1 (whole string)
2308{
2309 clear_tv(&vimvars[idx].vv_di.di_tv);
2310 vimvars[idx].vv_type = VAR_STRING;
2311 if (val == NULL)
2312 vimvars[idx].vv_str = NULL;
2313 else if (len == -1)
2314 vimvars[idx].vv_str = vim_strsave(val);
2315 else
2316 vimvars[idx].vv_str = vim_strnsave(val, len);
2317}
2318
2319/*
2320 * Set List v: variable to "val".
2321 */
2322 void
2323set_vim_var_list(int idx, list_T *val)
2324{
2325 clear_tv(&vimvars[idx].vv_di.di_tv);
2326 vimvars[idx].vv_type = VAR_LIST;
2327 vimvars[idx].vv_list = val;
2328 if (val != NULL)
2329 ++val->lv_refcount;
2330}
2331
2332/*
2333 * Set Dictionary v: variable to "val".
2334 */
2335 void
2336set_vim_var_dict(int idx, dict_T *val)
2337{
2338 clear_tv(&vimvars[idx].vv_di.di_tv);
2339 vimvars[idx].vv_type = VAR_DICT;
2340 vimvars[idx].vv_dict = val;
2341 if (val != NULL)
2342 {
2343 ++val->dv_refcount;
2344 dict_set_items_ro(val);
2345 }
2346}
2347
2348/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002349 * Set the v:argv list.
2350 */
2351 void
2352set_argv_var(char **argv, int argc)
2353{
2354 list_T *l = list_alloc();
2355 int i;
2356
2357 if (l == NULL)
2358 getout(1);
2359 l->lv_lock = VAR_FIXED;
2360 for (i = 0; i < argc; ++i)
2361 {
2362 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2363 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002364 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002365 }
2366 set_vim_var_list(VV_ARGV, l);
2367}
2368
2369/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002370 * Reset v:register, taking the 'clipboard' setting into account.
2371 */
2372 void
2373reset_reg_var(void)
2374{
2375 int regname = 0;
2376
2377 // Adjust the register according to 'clipboard', so that when
2378 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2379#ifdef FEAT_CLIPBOARD
2380 adjust_clip_reg(&regname);
2381#endif
2382 set_reg_var(regname);
2383}
2384
2385/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002386 * Set v:register if needed.
2387 */
2388 void
2389set_reg_var(int c)
2390{
2391 char_u regname;
2392
2393 if (c == 0 || c == ' ')
2394 regname = '"';
2395 else
2396 regname = c;
2397 // Avoid free/alloc when the value is already right.
2398 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2399 set_vim_var_string(VV_REG, &regname, 1);
2400}
2401
2402/*
2403 * Get or set v:exception. If "oldval" == NULL, return the current value.
2404 * Otherwise, restore the value to "oldval" and return NULL.
2405 * Must always be called in pairs to save and restore v:exception! Does not
2406 * take care of memory allocations.
2407 */
2408 char_u *
2409v_exception(char_u *oldval)
2410{
2411 if (oldval == NULL)
2412 return vimvars[VV_EXCEPTION].vv_str;
2413
2414 vimvars[VV_EXCEPTION].vv_str = oldval;
2415 return NULL;
2416}
2417
2418/*
2419 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2420 * Otherwise, restore the value to "oldval" and return NULL.
2421 * Must always be called in pairs to save and restore v:throwpoint! Does not
2422 * take care of memory allocations.
2423 */
2424 char_u *
2425v_throwpoint(char_u *oldval)
2426{
2427 if (oldval == NULL)
2428 return vimvars[VV_THROWPOINT].vv_str;
2429
2430 vimvars[VV_THROWPOINT].vv_str = oldval;
2431 return NULL;
2432}
2433
2434/*
2435 * Set v:cmdarg.
2436 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2437 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2438 * Must always be called in pairs!
2439 */
2440 char_u *
2441set_cmdarg(exarg_T *eap, char_u *oldarg)
2442{
2443 char_u *oldval;
2444 char_u *newval;
2445 unsigned len;
2446
2447 oldval = vimvars[VV_CMDARG].vv_str;
2448 if (eap == NULL)
2449 {
2450 vim_free(oldval);
2451 vimvars[VV_CMDARG].vv_str = oldarg;
2452 return NULL;
2453 }
2454
2455 if (eap->force_bin == FORCE_BIN)
2456 len = 6;
2457 else if (eap->force_bin == FORCE_NOBIN)
2458 len = 8;
2459 else
2460 len = 0;
2461
2462 if (eap->read_edit)
2463 len += 7;
2464
2465 if (eap->force_ff != 0)
2466 len += 10; // " ++ff=unix"
2467 if (eap->force_enc != 0)
2468 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2469 if (eap->bad_char != 0)
2470 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2471
2472 newval = alloc(len + 1);
2473 if (newval == NULL)
2474 return NULL;
2475
2476 if (eap->force_bin == FORCE_BIN)
2477 sprintf((char *)newval, " ++bin");
2478 else if (eap->force_bin == FORCE_NOBIN)
2479 sprintf((char *)newval, " ++nobin");
2480 else
2481 *newval = NUL;
2482
2483 if (eap->read_edit)
2484 STRCAT(newval, " ++edit");
2485
2486 if (eap->force_ff != 0)
2487 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2488 eap->force_ff == 'u' ? "unix"
2489 : eap->force_ff == 'd' ? "dos"
2490 : "mac");
2491 if (eap->force_enc != 0)
2492 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2493 eap->cmd + eap->force_enc);
2494 if (eap->bad_char == BAD_KEEP)
2495 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2496 else if (eap->bad_char == BAD_DROP)
2497 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2498 else if (eap->bad_char != 0)
2499 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2500 vimvars[VV_CMDARG].vv_str = newval;
2501 return oldval;
2502}
2503
2504/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002505 * Get the value of internal variable "name".
2506 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2507 */
2508 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002509eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002510 char_u *name,
2511 int len, // length of "name"
2512 typval_T *rettv, // NULL when only checking existence
2513 dictitem_T **dip, // non-NULL when typval's dict item is needed
2514 int verbose, // may give error message
2515 int no_autoload) // do not use script autoloading
2516{
2517 int ret = OK;
2518 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002519 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002520 dictitem_T *v;
2521 int cc;
2522
2523 // truncate the name, so that we can use strcmp()
2524 cc = name[len];
2525 name[len] = NUL;
2526
2527 // Check for user-defined variables.
2528 v = find_var(name, NULL, no_autoload);
2529 if (v != NULL)
2530 {
2531 tv = &v->di_tv;
2532 if (dip != NULL)
2533 *dip = v;
2534 }
2535
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002536 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002538 imported_T *import;
2539 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2540
2541 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542
2543 // imported variable from another script
2544 if (import != NULL)
2545 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002546 if (import->imp_funcname != NULL)
2547 {
2548 foundFunc = TRUE;
2549 if (rettv != NULL)
2550 {
2551 rettv->v_type = VAR_FUNC;
2552 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2553 }
2554 }
Bram Moolenaara6294952020-12-27 13:39:50 +01002555 else if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002556 {
2557 emsg("Sorry, 'import * as X' not implemented yet");
2558 ret = FAIL;
2559 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002560 else
2561 {
2562 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002563 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002565 tv = sv->sv_tv;
2566 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002568 else if (in_vim9script())
2569 {
2570 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2571
2572 if (ufunc != NULL)
2573 {
2574 foundFunc = TRUE;
2575 if (rettv != NULL)
2576 {
2577 rettv->v_type = VAR_FUNC;
2578 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2579 }
2580 }
2581 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 }
2583
Bram Moolenaarc620c052020-07-08 15:16:19 +02002584 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002585 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002586 if (tv == NULL)
2587 {
2588 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002589 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002590 ret = FAIL;
2591 }
2592 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002593 {
2594 // If a list or dict variable wasn't initialized, do it now.
2595 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2596 {
2597 tv->vval.v_dict = dict_alloc();
2598 if (tv->vval.v_dict != NULL)
2599 ++tv->vval.v_dict->dv_refcount;
2600 }
2601 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2602 {
2603 tv->vval.v_list = list_alloc();
2604 if (tv->vval.v_list != NULL)
2605 ++tv->vval.v_list->lv_refcount;
2606 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002607 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002608 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002609 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002610
2611 name[len] = cc;
2612
2613 return ret;
2614}
2615
2616/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002617 * Check if variable "name[len]" is a local variable or an argument.
2618 * If so, "*eval_lavars_used" is set to TRUE.
2619 */
2620 void
2621check_vars(char_u *name, int len)
2622{
2623 int cc;
2624 char_u *varname;
2625 hashtab_T *ht;
2626
2627 if (eval_lavars_used == NULL)
2628 return;
2629
2630 // truncate the name, so that we can use strcmp()
2631 cc = name[len];
2632 name[len] = NUL;
2633
2634 ht = find_var_ht(name, &varname);
2635 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2636 {
2637 if (find_var(name, NULL, TRUE) != NULL)
2638 *eval_lavars_used = TRUE;
2639 }
2640
2641 name[len] = cc;
2642}
2643
2644/*
2645 * Find variable "name" in the list of variables.
2646 * Return a pointer to it if found, NULL if not found.
2647 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002648 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002649 */
2650 dictitem_T *
2651find_var(char_u *name, hashtab_T **htp, int no_autoload)
2652{
2653 char_u *varname;
2654 hashtab_T *ht;
2655 dictitem_T *ret = NULL;
2656
2657 ht = find_var_ht(name, &varname);
2658 if (htp != NULL)
2659 *htp = ht;
2660 if (ht == NULL)
2661 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002662 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002663 if (ret != NULL)
2664 return ret;
2665
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002666 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002667 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002668 if (ret != NULL)
2669 return ret;
2670
2671 // in Vim9 script items without a scope can be script-local
2672 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2673 {
2674 ht = get_script_local_ht();
2675 if (ht != NULL)
2676 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002677 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002678 if (ret != NULL)
2679 {
2680 if (htp != NULL)
2681 *htp = ht;
2682 return ret;
2683 }
2684 }
2685 }
2686
2687 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002688}
2689
2690/*
2691 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002692 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002693 * Returns NULL if not found.
2694 */
2695 dictitem_T *
2696find_var_in_ht(
2697 hashtab_T *ht,
2698 int htname,
2699 char_u *varname,
2700 int no_autoload)
2701{
2702 hashitem_T *hi;
2703
2704 if (*varname == NUL)
2705 {
2706 // Must be something like "s:", otherwise "ht" would be NULL.
2707 switch (htname)
2708 {
2709 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2710 case 'g': return &globvars_var;
2711 case 'v': return &vimvars_var;
2712 case 'b': return &curbuf->b_bufvar;
2713 case 'w': return &curwin->w_winvar;
2714 case 't': return &curtab->tp_winvar;
2715 case 'l': return get_funccal_local_var();
2716 case 'a': return get_funccal_args_var();
2717 }
2718 return NULL;
2719 }
2720
2721 hi = hash_find(ht, varname);
2722 if (HASHITEM_EMPTY(hi))
2723 {
2724 // For global variables we may try auto-loading the script. If it
2725 // worked find the variable again. Don't auto-load a script if it was
2726 // loaded already, otherwise it would be loaded every time when
2727 // checking if a function name is a Funcref variable.
2728 if (ht == &globvarht && !no_autoload)
2729 {
2730 // Note: script_autoload() may make "hi" invalid. It must either
2731 // be obtained again or not used.
2732 if (!script_autoload(varname, FALSE) || aborting())
2733 return NULL;
2734 hi = hash_find(ht, varname);
2735 }
2736 if (HASHITEM_EMPTY(hi))
2737 return NULL;
2738 }
2739 return HI2DI(hi);
2740}
2741
2742/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743 * Get the script-local hashtab. NULL if not in a script context.
2744 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002745 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746get_script_local_ht(void)
2747{
2748 scid_T sid = current_sctx.sc_sid;
2749
Bram Moolenaare3d46852020-08-29 13:39:17 +02002750 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 return &SCRIPT_VARS(sid);
2752 return NULL;
2753}
2754
2755/*
2756 * Look for "name[len]" in script-local variables.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002757 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01002759 int
2760lookup_scriptvar(
2761 char_u *name,
2762 size_t len,
2763 void *lvar UNUSED,
2764 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765{
2766 hashtab_T *ht = get_script_local_ht();
2767 char_u buffer[30];
2768 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01002769 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770 hashitem_T *hi;
2771
2772 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002773 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 if (len < sizeof(buffer) - 1)
2775 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002776 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 vim_strncpy(buffer, name, len);
2778 p = buffer;
2779 }
2780 else
2781 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002782 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002784 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785 }
2786
2787 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002788 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789
2790 // if not script-local, then perhaps imported
Bram Moolenaar709664c2020-12-12 14:33:41 +01002791 if (res == FAIL && find_imported(p, 0, NULL) != NULL)
2792 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793
2794 if (p != buffer)
2795 vim_free(p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002796 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797}
2798
2799/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002800 * Find the hashtab used for a variable name.
2801 * Return NULL if the name is not valid.
2802 * Set "varname" to the start of name without ':'.
2803 */
2804 hashtab_T *
2805find_var_ht(char_u *name, char_u **varname)
2806{
2807 hashitem_T *hi;
2808 hashtab_T *ht;
2809
2810 if (name[0] == NUL)
2811 return NULL;
2812 if (name[1] != ':')
2813 {
2814 // The name must not start with a colon or #.
2815 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2816 return NULL;
2817 *varname = name;
2818
2819 // "version" is "v:version" in all scopes if scriptversion < 3.
2820 // Same for a few other variables marked with VV_COMPAT.
2821 if (current_sctx.sc_version < 3)
2822 {
2823 hi = hash_find(&compat_hashtab, name);
2824 if (!HASHITEM_EMPTY(hi))
2825 return &compat_hashtab;
2826 }
2827
2828 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 if (ht != NULL)
2830 return ht; // local variable
2831
2832 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002833 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834 {
2835 ht = get_script_local_ht();
2836 if (ht != NULL)
2837 return ht;
2838 }
2839
2840 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002841 }
2842 *varname = name + 2;
2843 if (*name == 'g') // global variable
2844 return &globvarht;
2845 // There must be no ':' or '#' in the rest of the name, unless g: is used
2846 if (vim_strchr(name + 2, ':') != NULL
2847 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2848 return NULL;
2849 if (*name == 'b') // buffer variable
2850 return &curbuf->b_vars->dv_hashtab;
2851 if (*name == 'w') // window variable
2852 return &curwin->w_vars->dv_hashtab;
2853 if (*name == 't') // tab page variable
2854 return &curtab->tp_vars->dv_hashtab;
2855 if (*name == 'v') // v: variable
2856 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002857 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002858 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002860 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002861 if (*name == 'a') // a: function argument
2862 return get_funccal_args_ht();
2863 if (*name == 'l') // l: local function variable
2864 return get_funccal_local_ht();
2865 }
2866 if (*name == 's') // script variable
2867 {
2868 ht = get_script_local_ht();
2869 if (ht != NULL)
2870 return ht;
2871 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002872 return NULL;
2873}
2874
2875/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002876 * Get the string value of a (global/local) variable.
2877 * Note: see tv_get_string() for how long the pointer remains valid.
2878 * Returns NULL when it doesn't exist.
2879 */
2880 char_u *
2881get_var_value(char_u *name)
2882{
2883 dictitem_T *v;
2884
2885 v = find_var(name, NULL, FALSE);
2886 if (v == NULL)
2887 return NULL;
2888 return tv_get_string(&v->di_tv);
2889}
2890
2891/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002892 * Allocate a new hashtab for a sourced script. It will be used while
2893 * sourcing this script and when executing functions defined in the script.
2894 */
2895 void
2896new_script_vars(scid_T id)
2897{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002898 scriptvar_T *sv;
2899
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002900 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2901 if (sv == NULL)
2902 return;
2903 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002904 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002905}
2906
2907/*
2908 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2909 * point to it.
2910 */
2911 void
2912init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2913{
2914 hash_init(&dict->dv_hashtab);
2915 dict->dv_lock = 0;
2916 dict->dv_scope = scope;
2917 dict->dv_refcount = DO_NOT_FREE_CNT;
2918 dict->dv_copyID = 0;
2919 dict_var->di_tv.vval.v_dict = dict;
2920 dict_var->di_tv.v_type = VAR_DICT;
2921 dict_var->di_tv.v_lock = VAR_FIXED;
2922 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2923 dict_var->di_key[0] = NUL;
2924}
2925
2926/*
2927 * Unreference a dictionary initialized by init_var_dict().
2928 */
2929 void
2930unref_var_dict(dict_T *dict)
2931{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002932 // Now the dict needs to be freed if no one else is using it, go back to
2933 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002934 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2935 dict_unref(dict);
2936}
2937
2938/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002939 * Clean up a list of internal variables.
2940 * Frees all allocated variables and the value they contain.
2941 * Clears hashtab "ht", does not free it.
2942 */
2943 void
2944vars_clear(hashtab_T *ht)
2945{
2946 vars_clear_ext(ht, TRUE);
2947}
2948
2949/*
2950 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2951 */
2952 void
2953vars_clear_ext(hashtab_T *ht, int free_val)
2954{
2955 int todo;
2956 hashitem_T *hi;
2957 dictitem_T *v;
2958
2959 hash_lock(ht);
2960 todo = (int)ht->ht_used;
2961 for (hi = ht->ht_array; todo > 0; ++hi)
2962 {
2963 if (!HASHITEM_EMPTY(hi))
2964 {
2965 --todo;
2966
2967 // Free the variable. Don't remove it from the hashtab,
2968 // ht_array might change then. hash_clear() takes care of it
2969 // later.
2970 v = HI2DI(hi);
2971 if (free_val)
2972 clear_tv(&v->di_tv);
2973 if (v->di_flags & DI_FLAGS_ALLOC)
2974 vim_free(v);
2975 }
2976 }
2977 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02002978 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002979}
2980
2981/*
2982 * Delete a variable from hashtab "ht" at item "hi".
2983 * Clear the variable value and free the dictitem.
2984 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02002985 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002986delete_var(hashtab_T *ht, hashitem_T *hi)
2987{
2988 dictitem_T *di = HI2DI(hi);
2989
2990 hash_remove(ht, hi);
2991 clear_tv(&di->di_tv);
2992 vim_free(di);
2993}
2994
2995/*
2996 * List the value of one internal variable.
2997 */
2998 static void
2999list_one_var(dictitem_T *v, char *prefix, int *first)
3000{
3001 char_u *tofree;
3002 char_u *s;
3003 char_u numbuf[NUMBUFLEN];
3004
3005 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3006 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3007 s == NULL ? (char_u *)"" : s, first);
3008 vim_free(tofree);
3009}
3010
3011 static void
3012list_one_var_a(
3013 char *prefix,
3014 char_u *name,
3015 int type,
3016 char_u *string,
3017 int *first) // when TRUE clear rest of screen and set to FALSE
3018{
3019 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3020 msg_start();
3021 msg_puts(prefix);
3022 if (name != NULL) // "a:" vars don't have a name stored
3023 msg_puts((char *)name);
3024 msg_putchar(' ');
3025 msg_advance(22);
3026 if (type == VAR_NUMBER)
3027 msg_putchar('#');
3028 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3029 msg_putchar('*');
3030 else if (type == VAR_LIST)
3031 {
3032 msg_putchar('[');
3033 if (*string == '[')
3034 ++string;
3035 }
3036 else if (type == VAR_DICT)
3037 {
3038 msg_putchar('{');
3039 if (*string == '{')
3040 ++string;
3041 }
3042 else
3043 msg_putchar(' ');
3044
3045 msg_outtrans(string);
3046
3047 if (type == VAR_FUNC || type == VAR_PARTIAL)
3048 msg_puts("()");
3049 if (*first)
3050 {
3051 msg_clr_eos();
3052 *first = FALSE;
3053 }
3054}
3055
3056/*
3057 * Set variable "name" to value in "tv".
3058 * If the variable already exists, the value is updated.
3059 * Otherwise the variable is created.
3060 */
3061 void
3062set_var(
3063 char_u *name,
3064 typval_T *tv,
3065 int copy) // make copy of value in "tv"
3066{
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003067 set_var_const(name, NULL, tv, copy, ASSIGN_DECL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003068}
3069
3070/*
3071 * Set variable "name" to value in "tv".
3072 * If the variable already exists and "is_const" is FALSE the value is updated.
3073 * Otherwise the variable is created.
3074 */
3075 void
3076set_var_const(
3077 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003079 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003080 int copy, // make copy of value in "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003081 int flags) // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003082{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003083 typval_T *tv = tv_arg;
3084 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003086 char_u *varname;
3087 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003089 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003090
3091 ht = find_var_ht(name, &varname);
3092 if (ht == NULL || *varname == NUL)
3093 {
3094 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003095 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003096 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003097 is_script_local = ht == get_script_local_ht();
3098
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003099 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003100 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003101 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003102 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003103 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003104 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003105 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003106 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003107 }
3108
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003110
3111 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 if (di == NULL)
3113 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003114
3115 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003116 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003117 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003118
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003119 if (need_convert_to_bool(type, tv))
3120 {
3121 // Destination is a bool and the value is not, but it can be converted.
3122 CLEAR_FIELD(bool_tv);
3123 bool_tv.v_type = VAR_BOOL;
3124 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3125 tv = &bool_tv;
3126 }
3127
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003129 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003131 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003132 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 {
3134 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003135 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 }
3137
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003138 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139 {
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003140 if ((flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003141 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003142 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003143 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003144 }
3145
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003146 // check the type and adjust to bool if needed
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003147 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003148 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003150
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003151 if (var_check_permission(di, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003152 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003153 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 else
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003155 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 // can only redefine once
3157 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003158
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003159 // A Vim9 script-local variable is also present in sn_all_vars and
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003160 // sn_var_vals. It may set "type" from "tv".
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003161 if (is_script_local && vim9script)
Bram Moolenaar08251752021-01-11 21:20:18 +01003162 update_vim9_script_var(FALSE, di, flags, tv, &type);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003163 }
3164
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003165 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003166
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003168 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003169 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003170 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003172 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003174 if (copy || tv->v_type != VAR_STRING)
3175 {
3176 char_u *val = tv_get_string(tv);
3177
3178 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003179 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 if (di->di_tv.vval.v_string == NULL)
3181 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003182 }
3183 else
3184 {
3185 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003187 tv->vval.v_string = NULL;
3188 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003189 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003190 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003192 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003194 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003196#ifdef FEAT_SEARCH_EXTRA
3197 else if (STRCMP(varname, "hlsearch") == 0)
3198 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003200 redraw_all_later(SOME_VALID);
3201 }
3202#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003203 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003204 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003206 {
3207 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003208 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003209 }
3210 }
3211
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003213 }
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003214 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003215 {
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003216 // add a new variable
3217 if (vim9script && is_script_local && (flags & ASSIGN_NO_DECL))
3218 {
3219 semsg(_(e_unknown_variable_str), name);
3220 goto failed;
3221 }
3222
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003223 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003224 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003225 {
3226 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003227 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003228 }
3229
Bram Moolenaar03290b82020-12-19 16:30:44 +01003230 // Make sure the variable name is valid. In Vim9 script an autoload
3231 // variable must be prefixed with "g:".
3232 if (!valid_varname(varname, !vim9script
3233 || STRNCMP(name, "g:", 2) == 0))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003234 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3237 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003238 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 STRCPY(di->di_key, varname);
3240 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003241 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003243 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003244 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003246 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 di->di_flags |= DI_FLAGS_LOCK;
3248
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003249 // A Vim9 script-local variable is also added to sn_all_vars and
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003250 // sn_var_vals. It may set "type" from "tv".
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003251 if (is_script_local && vim9script)
Bram Moolenaar08251752021-01-11 21:20:18 +01003252 update_vim9_script_var(TRUE, di, flags, tv, &type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003253 }
3254
3255 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003257 else
3258 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 di->di_tv = *tv;
3260 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003261 init_tv(tv);
3262 }
3263
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003264 if (vim9script && type != NULL)
3265 {
3266 if (type->tt_type == VAR_DICT && di->di_tv.vval.v_dict != NULL)
3267 di->di_tv.vval.v_dict->dv_type = alloc_type(type);
3268 else if (type->tt_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
3269 di->di_tv.vval.v_list->lv_type = alloc_type(type);
3270 }
3271
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003272 // ":const var = value" locks the value
3273 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003274 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003275 // Like :lockvar! name: lock the value and what it contains, but only
3276 // if the reference count is up to one. That locks only literal
3277 // values.
3278 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003279 return;
3280failed:
3281 if (!copy)
3282 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003283}
3284
3285/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003286 * Check in this order for backwards compatibility:
3287 * - Whether the variable is read-only
3288 * - Whether the variable value is locked
3289 * - Whether the variable is locked
3290 */
3291 int
3292var_check_permission(dictitem_T *di, char_u *name)
3293{
3294 if (var_check_ro(di->di_flags, name, FALSE)
3295 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3296 || var_check_lock(di->di_flags, name, FALSE))
3297 return FAIL;
3298 return OK;
3299}
3300
3301/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003302 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3303 * Also give an error message.
3304 */
3305 int
3306var_check_ro(int flags, char_u *name, int use_gettext)
3307{
3308 if (flags & DI_FLAGS_RO)
3309 {
3310 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3311 return TRUE;
3312 }
3313 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3314 {
3315 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3316 return TRUE;
3317 }
3318 return FALSE;
3319}
3320
3321/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003322 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3323 * Also give an error message.
3324 */
3325 int
3326var_check_lock(int flags, char_u *name, int use_gettext)
3327{
3328 if (flags & DI_FLAGS_LOCK)
3329 {
3330 semsg(_(e_variable_is_locked_str),
3331 use_gettext ? (char_u *)_(name) : name);
3332 return TRUE;
3333 }
3334 return FALSE;
3335}
3336
3337/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003338 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3339 * Also give an error message.
3340 */
3341 int
3342var_check_fixed(int flags, char_u *name, int use_gettext)
3343{
3344 if (flags & DI_FLAGS_FIX)
3345 {
3346 semsg(_("E795: Cannot delete variable %s"),
3347 use_gettext ? (char_u *)_(name) : name);
3348 return TRUE;
3349 }
3350 return FALSE;
3351}
3352
3353/*
3354 * Check if a funcref is assigned to a valid variable name.
3355 * Return TRUE and give an error if not.
3356 */
3357 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003358var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003359 char_u *name, // points to start of variable name
3360 int new_var) // TRUE when creating the variable
3361{
3362 // Allow for w: b: s: and t:.
3363 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3364 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3365 ? name[2] : name[0]))
3366 {
3367 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3368 name);
3369 return TRUE;
3370 }
3371 // Don't allow hiding a function. When "v" is not NULL we might be
3372 // assigning another function to the same var, the type is checked
3373 // below.
3374 if (new_var && function_exists(name, FALSE))
3375 {
3376 semsg(_("E705: Variable name conflicts with existing function: %s"),
3377 name);
3378 return TRUE;
3379 }
3380 return FALSE;
3381}
3382
3383/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003384 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3385 * value. Also give an error message, using "name" or _("name") when
3386 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003387 */
3388 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003389value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003390{
3391 if (lock & VAR_LOCKED)
3392 {
3393 semsg(_("E741: Value is locked: %s"),
3394 name == NULL ? (char_u *)_("Unknown")
3395 : use_gettext ? (char_u *)_(name)
3396 : name);
3397 return TRUE;
3398 }
3399 if (lock & VAR_FIXED)
3400 {
3401 semsg(_("E742: Cannot change value of %s"),
3402 name == NULL ? (char_u *)_("Unknown")
3403 : use_gettext ? (char_u *)_(name)
3404 : name);
3405 return TRUE;
3406 }
3407 return FALSE;
3408}
3409
3410/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003411 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003412 * Return FALSE and give an error if not.
3413 */
3414 int
Bram Moolenaar03290b82020-12-19 16:30:44 +01003415valid_varname(char_u *varname, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003416{
3417 char_u *p;
3418
3419 for (p = varname; *p != NUL; ++p)
3420 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003421 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003422 {
3423 semsg(_(e_illvar), varname);
3424 return FALSE;
3425 }
3426 return TRUE;
3427}
3428
3429/*
3430 * getwinvar() and gettabwinvar()
3431 */
3432 static void
3433getwinvar(
3434 typval_T *argvars,
3435 typval_T *rettv,
3436 int off) // 1 for gettabwinvar()
3437{
3438 win_T *win;
3439 char_u *varname;
3440 dictitem_T *v;
3441 tabpage_T *tp = NULL;
3442 int done = FALSE;
3443 win_T *oldcurwin;
3444 tabpage_T *oldtabpage;
3445 int need_switch_win;
3446
3447 if (off == 1)
3448 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3449 else
3450 tp = curtab;
3451 win = find_win_by_nr(&argvars[off], tp);
3452 varname = tv_get_string_chk(&argvars[off + 1]);
3453 ++emsg_off;
3454
3455 rettv->v_type = VAR_STRING;
3456 rettv->vval.v_string = NULL;
3457
3458 if (win != NULL && varname != NULL)
3459 {
3460 // Set curwin to be our win, temporarily. Also set the tabpage,
3461 // otherwise the window is not valid. Only do this when needed,
3462 // autocommands get blocked.
3463 need_switch_win = !(tp == curtab && win == curwin);
3464 if (!need_switch_win
3465 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3466 {
3467 if (*varname == '&')
3468 {
3469 if (varname[1] == NUL)
3470 {
3471 // get all window-local options in a dict
3472 dict_T *opts = get_winbuf_options(FALSE);
3473
3474 if (opts != NULL)
3475 {
3476 rettv_dict_set(rettv, opts);
3477 done = TRUE;
3478 }
3479 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003480 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003481 // window-local-option
3482 done = TRUE;
3483 }
3484 else
3485 {
3486 // Look up the variable.
3487 // Let getwinvar({nr}, "") return the "w:" dictionary.
3488 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3489 varname, FALSE);
3490 if (v != NULL)
3491 {
3492 copy_tv(&v->di_tv, rettv);
3493 done = TRUE;
3494 }
3495 }
3496 }
3497
3498 if (need_switch_win)
3499 // restore previous notion of curwin
3500 restore_win(oldcurwin, oldtabpage, TRUE);
3501 }
3502
3503 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3504 // use the default return value
3505 copy_tv(&argvars[off + 2], rettv);
3506
3507 --emsg_off;
3508}
3509
3510/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003511 * Set option "varname" to the value of "varp" for the current buffer/window.
3512 */
3513 static void
3514set_option_from_tv(char_u *varname, typval_T *varp)
3515{
3516 long numval = 0;
3517 char_u *strval;
3518 char_u nbuf[NUMBUFLEN];
3519 int error = FALSE;
3520
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003521 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003522 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003523 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003524 strval = (char_u *)"0"; // avoid using "false"
3525 }
3526 else
3527 {
3528 if (!in_vim9script() || varp->v_type != VAR_STRING)
3529 numval = (long)tv_get_number_chk(varp, &error);
3530 strval = tv_get_string_buf_chk(varp, nbuf);
3531 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02003532 if (!error && strval != NULL)
3533 set_option_value(varname, numval, strval, OPT_LOCAL);
3534}
3535
3536/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003537 * "setwinvar()" and "settabwinvar()" functions
3538 */
3539 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003540setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003541{
3542 win_T *win;
3543 win_T *save_curwin;
3544 tabpage_T *save_curtab;
3545 int need_switch_win;
3546 char_u *varname, *winvarname;
3547 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003548 tabpage_T *tp = NULL;
3549
3550 if (check_secure())
3551 return;
3552
3553 if (off == 1)
3554 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3555 else
3556 tp = curtab;
3557 win = find_win_by_nr(&argvars[off], tp);
3558 varname = tv_get_string_chk(&argvars[off + 1]);
3559 varp = &argvars[off + 2];
3560
3561 if (win != NULL && varname != NULL && varp != NULL)
3562 {
3563 need_switch_win = !(tp == curtab && win == curwin);
3564 if (!need_switch_win
3565 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3566 {
3567 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003568 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003569 else
3570 {
3571 winvarname = alloc(STRLEN(varname) + 3);
3572 if (winvarname != NULL)
3573 {
3574 STRCPY(winvarname, "w:");
3575 STRCPY(winvarname + 2, varname);
3576 set_var(winvarname, varp, TRUE);
3577 vim_free(winvarname);
3578 }
3579 }
3580 }
3581 if (need_switch_win)
3582 restore_win(save_curwin, save_curtab, TRUE);
3583 }
3584}
3585
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003586/*
3587 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3588 * v:option_type, and v:option_command.
3589 */
3590 void
3591reset_v_option_vars(void)
3592{
3593 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3594 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3595 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3596 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3597 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3598 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3599}
3600
3601/*
3602 * Add an assert error to v:errors.
3603 */
3604 void
3605assert_error(garray_T *gap)
3606{
3607 struct vimvar *vp = &vimvars[VV_ERRORS];
3608
3609 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003610 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003611 set_vim_var_list(VV_ERRORS, list_alloc());
3612 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3613}
3614
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003615 int
3616var_exists(char_u *var)
3617{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003618 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003619 char_u *name;
3620 char_u *tofree;
3621 typval_T tv;
3622 int len = 0;
3623 int n = FALSE;
3624
3625 // get_name_len() takes care of expanding curly braces
3626 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003627 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003628 if (len > 0)
3629 {
3630 if (tofree != NULL)
3631 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003632 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003633 if (n)
3634 {
3635 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003636 arg = skipwhite(arg);
3637 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003638 if (n)
3639 clear_tv(&tv);
3640 }
3641 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003642 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003643 n = FALSE;
3644
3645 vim_free(tofree);
3646 return n;
3647}
3648
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003649static lval_T *redir_lval = NULL;
3650#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3651static garray_T redir_ga; // only valid when redir_lval is not NULL
3652static char_u *redir_endp = NULL;
3653static char_u *redir_varname = NULL;
3654
3655/*
3656 * Start recording command output to a variable
3657 * When "append" is TRUE append to an existing variable.
3658 * Returns OK if successfully completed the setup. FAIL otherwise.
3659 */
3660 int
3661var_redir_start(char_u *name, int append)
3662{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003663 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003664 typval_T tv;
3665
3666 // Catch a bad name early.
3667 if (!eval_isnamec1(*name))
3668 {
3669 emsg(_(e_invarg));
3670 return FAIL;
3671 }
3672
3673 // Make a copy of the name, it is used in redir_lval until redir ends.
3674 redir_varname = vim_strsave(name);
3675 if (redir_varname == NULL)
3676 return FAIL;
3677
3678 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3679 if (redir_lval == NULL)
3680 {
3681 var_redir_stop();
3682 return FAIL;
3683 }
3684
3685 // The output is stored in growarray "redir_ga" until redirection ends.
3686 ga_init2(&redir_ga, (int)sizeof(char), 500);
3687
3688 // Parse the variable name (can be a dict or list entry).
3689 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3690 FNE_CHECK_START);
3691 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3692 {
3693 clear_lval(redir_lval);
3694 if (redir_endp != NULL && *redir_endp != NUL)
3695 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003696 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003697 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003698 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003699 redir_endp = NULL; // don't store a value, only cleanup
3700 var_redir_stop();
3701 return FAIL;
3702 }
3703
3704 // check if we can write to the variable: set it to or append an empty
3705 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003706 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003707 tv.v_type = VAR_STRING;
3708 tv.vval.v_string = (char_u *)"";
3709 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003710 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3711 ASSIGN_NO_DECL, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003712 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003713 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3714 ASSIGN_NO_DECL, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003715 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003716 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003717 {
3718 redir_endp = NULL; // don't store a value, only cleanup
3719 var_redir_stop();
3720 return FAIL;
3721 }
3722
3723 return OK;
3724}
3725
3726/*
3727 * Append "value[value_len]" to the variable set by var_redir_start().
3728 * The actual appending is postponed until redirection ends, because the value
3729 * appended may in fact be the string we write to, changing it may cause freed
3730 * memory to be used:
3731 * :redir => foo
3732 * :let foo
3733 * :redir END
3734 */
3735 void
3736var_redir_str(char_u *value, int value_len)
3737{
3738 int len;
3739
3740 if (redir_lval == NULL)
3741 return;
3742
3743 if (value_len == -1)
3744 len = (int)STRLEN(value); // Append the entire string
3745 else
3746 len = value_len; // Append only "value_len" characters
3747
3748 if (ga_grow(&redir_ga, len) == OK)
3749 {
3750 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3751 redir_ga.ga_len += len;
3752 }
3753 else
3754 var_redir_stop();
3755}
3756
3757/*
3758 * Stop redirecting command output to a variable.
3759 * Frees the allocated memory.
3760 */
3761 void
3762var_redir_stop(void)
3763{
3764 typval_T tv;
3765
3766 if (EVALCMD_BUSY)
3767 {
3768 redir_lval = NULL;
3769 return;
3770 }
3771
3772 if (redir_lval != NULL)
3773 {
3774 // If there was no error: assign the text to the variable.
3775 if (redir_endp != NULL)
3776 {
3777 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3778 tv.v_type = VAR_STRING;
3779 tv.vval.v_string = redir_ga.ga_data;
3780 // Call get_lval() again, if it's inside a Dict or List it may
3781 // have changed.
3782 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3783 FALSE, FALSE, 0, FNE_CHECK_START);
3784 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003786 (char_u *)".");
3787 clear_lval(redir_lval);
3788 }
3789
3790 // free the collected output
3791 VIM_CLEAR(redir_ga.ga_data);
3792
3793 VIM_CLEAR(redir_lval);
3794 }
3795 VIM_CLEAR(redir_varname);
3796}
3797
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003798/*
3799 * "gettabvar()" function
3800 */
3801 void
3802f_gettabvar(typval_T *argvars, typval_T *rettv)
3803{
3804 win_T *oldcurwin;
3805 tabpage_T *tp, *oldtabpage;
3806 dictitem_T *v;
3807 char_u *varname;
3808 int done = FALSE;
3809
3810 rettv->v_type = VAR_STRING;
3811 rettv->vval.v_string = NULL;
3812
3813 varname = tv_get_string_chk(&argvars[1]);
3814 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3815 if (tp != NULL && varname != NULL)
3816 {
3817 // Set tp to be our tabpage, temporarily. Also set the window to the
3818 // first window in the tabpage, otherwise the window is not valid.
3819 if (switch_win(&oldcurwin, &oldtabpage,
3820 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3821 : tp->tp_firstwin, tp, TRUE) == OK)
3822 {
3823 // look up the variable
3824 // Let gettabvar({nr}, "") return the "t:" dictionary.
3825 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3826 if (v != NULL)
3827 {
3828 copy_tv(&v->di_tv, rettv);
3829 done = TRUE;
3830 }
3831 }
3832
3833 // restore previous notion of curwin
3834 restore_win(oldcurwin, oldtabpage, TRUE);
3835 }
3836
3837 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3838 copy_tv(&argvars[2], rettv);
3839}
3840
3841/*
3842 * "gettabwinvar()" function
3843 */
3844 void
3845f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3846{
3847 getwinvar(argvars, rettv, 1);
3848}
3849
3850/*
3851 * "getwinvar()" function
3852 */
3853 void
3854f_getwinvar(typval_T *argvars, typval_T *rettv)
3855{
3856 getwinvar(argvars, rettv, 0);
3857}
3858
3859/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003860 * "getbufvar()" function
3861 */
3862 void
3863f_getbufvar(typval_T *argvars, typval_T *rettv)
3864{
3865 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003866 char_u *varname;
3867 dictitem_T *v;
3868 int done = FALSE;
3869
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003870 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003871 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003872
3873 rettv->v_type = VAR_STRING;
3874 rettv->vval.v_string = NULL;
3875
3876 if (buf != NULL && varname != NULL)
3877 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003878 if (*varname == '&')
3879 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003880 buf_T *save_curbuf = curbuf;
3881
3882 // set curbuf to be our buf, temporarily
3883 curbuf = buf;
3884
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003885 if (varname[1] == NUL)
3886 {
3887 // get all buffer-local options in a dict
3888 dict_T *opts = get_winbuf_options(TRUE);
3889
3890 if (opts != NULL)
3891 {
3892 rettv_dict_set(rettv, opts);
3893 done = TRUE;
3894 }
3895 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003896 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003897 // buffer-local-option
3898 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003899
3900 // restore previous notion of curbuf
3901 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003902 }
3903 else
3904 {
3905 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003906 if (*varname == NUL)
3907 // Let getbufvar({nr}, "") return the "b:" dictionary.
3908 v = &buf->b_bufvar;
3909 else
3910 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3911 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003912 if (v != NULL)
3913 {
3914 copy_tv(&v->di_tv, rettv);
3915 done = TRUE;
3916 }
3917 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003918 }
3919
3920 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3921 // use the default value
3922 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003923}
3924
3925/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003926 * "settabvar()" function
3927 */
3928 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003929f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003930{
3931 tabpage_T *save_curtab;
3932 tabpage_T *tp;
3933 char_u *varname, *tabvarname;
3934 typval_T *varp;
3935
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003936 if (check_secure())
3937 return;
3938
3939 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3940 varname = tv_get_string_chk(&argvars[1]);
3941 varp = &argvars[2];
3942
3943 if (varname != NULL && varp != NULL && tp != NULL)
3944 {
3945 save_curtab = curtab;
3946 goto_tabpage_tp(tp, FALSE, FALSE);
3947
3948 tabvarname = alloc(STRLEN(varname) + 3);
3949 if (tabvarname != NULL)
3950 {
3951 STRCPY(tabvarname, "t:");
3952 STRCPY(tabvarname + 2, varname);
3953 set_var(tabvarname, varp, TRUE);
3954 vim_free(tabvarname);
3955 }
3956
3957 // Restore current tabpage
3958 if (valid_tabpage(save_curtab))
3959 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3960 }
3961}
3962
3963/*
3964 * "settabwinvar()" function
3965 */
3966 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003967f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003968{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003969 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003970}
3971
3972/*
3973 * "setwinvar()" function
3974 */
3975 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003976f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003977{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003978 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003979}
3980
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003981/*
3982 * "setbufvar()" function
3983 */
3984 void
3985f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3986{
3987 buf_T *buf;
3988 char_u *varname, *bufvarname;
3989 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003990
3991 if (check_secure())
3992 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003993 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003994 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003995 varp = &argvars[2];
3996
3997 if (buf != NULL && varname != NULL && varp != NULL)
3998 {
3999 if (*varname == '&')
4000 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004001 aco_save_T aco;
4002
4003 // set curbuf to be our buf, temporarily
4004 aucmd_prepbuf(&aco, buf);
4005
Bram Moolenaar191929b2020-08-19 21:20:49 +02004006 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004007
4008 // reset notion of buffer
4009 aucmd_restbuf(&aco);
4010 }
4011 else
4012 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004013 bufvarname = alloc(STRLEN(varname) + 3);
4014 if (bufvarname != NULL)
4015 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004016 buf_T *save_curbuf = curbuf;
4017
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004018 curbuf = buf;
4019 STRCPY(bufvarname, "b:");
4020 STRCPY(bufvarname + 2, varname);
4021 set_var(bufvarname, varp, TRUE);
4022 vim_free(bufvarname);
4023 curbuf = save_curbuf;
4024 }
4025 }
4026 }
4027}
4028
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004029/*
4030 * Get a callback from "arg". It can be a Funcref or a function name.
4031 * When "arg" is zero return an empty string.
4032 * "cb_name" is not allocated.
4033 * "cb_name" is set to NULL for an invalid argument.
4034 */
4035 callback_T
4036get_callback(typval_T *arg)
4037{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004038 callback_T res;
4039 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004040
4041 res.cb_free_name = FALSE;
4042 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4043 {
4044 res.cb_partial = arg->vval.v_partial;
4045 ++res.cb_partial->pt_refcount;
4046 res.cb_name = partial_name(res.cb_partial);
4047 }
4048 else
4049 {
4050 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004051 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4052 && isdigit(*arg->vval.v_string))
4053 r = FAIL;
4054 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004055 {
4056 // Note that we don't make a copy of the string.
4057 res.cb_name = arg->vval.v_string;
4058 func_ref(res.cb_name);
4059 }
4060 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004061 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004062 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004063 r = FAIL;
4064
4065 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004066 {
4067 emsg(_("E921: Invalid callback argument"));
4068 res.cb_name = NULL;
4069 }
4070 }
4071 return res;
4072}
4073
4074/*
4075 * Copy a callback into a typval_T.
4076 */
4077 void
4078put_callback(callback_T *cb, typval_T *tv)
4079{
4080 if (cb->cb_partial != NULL)
4081 {
4082 tv->v_type = VAR_PARTIAL;
4083 tv->vval.v_partial = cb->cb_partial;
4084 ++tv->vval.v_partial->pt_refcount;
4085 }
4086 else
4087 {
4088 tv->v_type = VAR_FUNC;
4089 tv->vval.v_string = vim_strsave(cb->cb_name);
4090 func_ref(cb->cb_name);
4091 }
4092}
4093
4094/*
4095 * Make a copy of "src" into "dest", allocating the function name if needed,
4096 * without incrementing the refcount.
4097 */
4098 void
4099set_callback(callback_T *dest, callback_T *src)
4100{
4101 if (src->cb_partial == NULL)
4102 {
4103 // just a function name, make a copy
4104 dest->cb_name = vim_strsave(src->cb_name);
4105 dest->cb_free_name = TRUE;
4106 }
4107 else
4108 {
4109 // cb_name is a pointer into cb_partial
4110 dest->cb_name = src->cb_name;
4111 dest->cb_free_name = FALSE;
4112 }
4113 dest->cb_partial = src->cb_partial;
4114}
4115
4116/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004117 * Copy callback from "src" to "dest", incrementing the refcounts.
4118 */
4119 void
4120copy_callback(callback_T *dest, callback_T *src)
4121{
4122 dest->cb_partial = src->cb_partial;
4123 if (dest->cb_partial != NULL)
4124 {
4125 dest->cb_name = src->cb_name;
4126 dest->cb_free_name = FALSE;
4127 ++dest->cb_partial->pt_refcount;
4128 }
4129 else
4130 {
4131 dest->cb_name = vim_strsave(src->cb_name);
4132 dest->cb_free_name = TRUE;
4133 func_ref(src->cb_name);
4134 }
4135}
4136
4137/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004138 * Unref/free "callback" returned by get_callback() or set_callback().
4139 */
4140 void
4141free_callback(callback_T *callback)
4142{
4143 if (callback->cb_partial != NULL)
4144 {
4145 partial_unref(callback->cb_partial);
4146 callback->cb_partial = NULL;
4147 }
4148 else if (callback->cb_name != NULL)
4149 func_unref(callback->cb_name);
4150 if (callback->cb_free_name)
4151 {
4152 vim_free(callback->cb_name);
4153 callback->cb_free_name = FALSE;
4154 }
4155 callback->cb_name = NULL;
4156}
4157
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004158#endif // FEAT_EVAL