blob: bff6086e53781a2e0a5fbf2229dad7e5b4cb1620 [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 Moolenaare5cdf152019-08-29 22:09:46 +0200149};
150
151// shorthand
152#define vv_type vv_di.di_tv.v_type
153#define vv_nr vv_di.di_tv.vval.v_number
154#define vv_float vv_di.di_tv.vval.v_float
155#define vv_str vv_di.di_tv.vval.v_string
156#define vv_list vv_di.di_tv.vval.v_list
157#define vv_dict vv_di.di_tv.vval.v_dict
158#define vv_blob vv_di.di_tv.vval.v_blob
159#define vv_tv vv_di.di_tv
160
161static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200162static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200163#define vimvarht vimvardict.dv_hashtab
164
165// for VIM_VERSION_ defines
166#include "version.h"
167
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200168static void list_glob_vars(int *first);
169static void list_buf_vars(int *first);
170static void list_win_vars(int *first);
171static void list_tab_vars(int *first);
172static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173static 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 +0200174static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
175static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaar021bda52020-08-17 21:07:22 +0200176static void item_lock(typval_T *tv, int deep, int lock, int check_refcount);
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200177static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200178static void list_one_var(dictitem_T *v, char *prefix, int *first);
179static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
180
181/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200182 * Initialize global and vim special variables
183 */
184 void
185evalvars_init(void)
186{
187 int i;
188 struct vimvar *p;
189
190 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
191 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
192 vimvardict.dv_lock = VAR_FIXED;
193 hash_init(&compat_hashtab);
194
195 for (i = 0; i < VV_LEN; ++i)
196 {
197 p = &vimvars[i];
198 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
199 {
200 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
201 getout(1);
202 }
203 STRCPY(p->vv_di.di_key, p->vv_name);
204 if (p->vv_flags & VV_RO)
205 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
206 else if (p->vv_flags & VV_RO_SBX)
207 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
208 else
209 p->vv_di.di_flags = DI_FLAGS_FIX;
210
211 // add to v: scope dict, unless the value is not always available
212 if (p->vv_type != VAR_UNKNOWN)
213 hash_add(&vimvarht, p->vv_di.di_key);
214 if (p->vv_flags & VV_COMPAT)
215 // add to compat scope dict
216 hash_add(&compat_hashtab, p->vv_di.di_key);
217 }
218 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
219 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
220
221 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
222 set_vim_var_nr(VV_HLSEARCH, 1L);
223 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{
304 int i;
305 int abort = FALSE;
306
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100307 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200308 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
309
310 return abort;
311}
312
313/*
314 * Set an internal variable to a string value. Creates the variable if it does
315 * not already exist.
316 */
317 void
318set_internal_string_var(char_u *name, char_u *value)
319{
320 char_u *val;
321 typval_T *tvp;
322
323 val = vim_strsave(value);
324 if (val != NULL)
325 {
326 tvp = alloc_string_tv(val);
327 if (tvp != NULL)
328 {
329 set_var(name, tvp, FALSE);
330 free_tv(tvp);
331 }
332 }
333}
334
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200335 int
336eval_charconvert(
337 char_u *enc_from,
338 char_u *enc_to,
339 char_u *fname_from,
340 char_u *fname_to)
341{
342 int err = FALSE;
343
344 set_vim_var_string(VV_CC_FROM, enc_from, -1);
345 set_vim_var_string(VV_CC_TO, enc_to, -1);
346 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
347 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
348 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
349 err = TRUE;
350 set_vim_var_string(VV_CC_FROM, NULL, -1);
351 set_vim_var_string(VV_CC_TO, NULL, -1);
352 set_vim_var_string(VV_FNAME_IN, NULL, -1);
353 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
354
355 if (err)
356 return FAIL;
357 return OK;
358}
359
360# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
361 int
362eval_printexpr(char_u *fname, char_u *args)
363{
364 int err = FALSE;
365
366 set_vim_var_string(VV_FNAME_IN, fname, -1);
367 set_vim_var_string(VV_CMDARG, args, -1);
368 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
369 err = TRUE;
370 set_vim_var_string(VV_FNAME_IN, NULL, -1);
371 set_vim_var_string(VV_CMDARG, NULL, -1);
372
373 if (err)
374 {
375 mch_remove(fname);
376 return FAIL;
377 }
378 return OK;
379}
380# endif
381
382# if defined(FEAT_DIFF) || defined(PROTO)
383 void
384eval_diff(
385 char_u *origfile,
386 char_u *newfile,
387 char_u *outfile)
388{
389 int err = FALSE;
390
391 set_vim_var_string(VV_FNAME_IN, origfile, -1);
392 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
393 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
394 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
395 set_vim_var_string(VV_FNAME_IN, NULL, -1);
396 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
397 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
398}
399
400 void
401eval_patch(
402 char_u *origfile,
403 char_u *difffile,
404 char_u *outfile)
405{
406 int err;
407
408 set_vim_var_string(VV_FNAME_IN, origfile, -1);
409 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
410 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
411 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
412 set_vim_var_string(VV_FNAME_IN, NULL, -1);
413 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
414 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
415}
416# endif
417
418#if defined(FEAT_SPELL) || defined(PROTO)
419/*
420 * Evaluate an expression to a list with suggestions.
421 * For the "expr:" part of 'spellsuggest'.
422 * Returns NULL when there is an error.
423 */
424 list_T *
425eval_spell_expr(char_u *badword, char_u *expr)
426{
427 typval_T save_val;
428 typval_T rettv;
429 list_T *list = NULL;
430 char_u *p = skipwhite(expr);
431
432 // Set "v:val" to the bad word.
433 prepare_vimvar(VV_VAL, &save_val);
434 set_vim_var_string(VV_VAL, badword, -1);
435 if (p_verbose == 0)
436 ++emsg_off;
437
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200438 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200439 {
440 if (rettv.v_type != VAR_LIST)
441 clear_tv(&rettv);
442 else
443 list = rettv.vval.v_list;
444 }
445
446 if (p_verbose == 0)
447 --emsg_off;
448 clear_tv(get_vim_var_tv(VV_VAL));
449 restore_vimvar(VV_VAL, &save_val);
450
451 return list;
452}
453
454/*
455 * "list" is supposed to contain two items: a word and a number. Return the
456 * word in "pp" and the number as the return value.
457 * Return -1 if anything isn't right.
458 * Used to get the good word and score from the eval_spell_expr() result.
459 */
460 int
461get_spellword(list_T *list, char_u **pp)
462{
463 listitem_T *li;
464
465 li = list->lv_first;
466 if (li == NULL)
467 return -1;
468 *pp = tv_get_string(&li->li_tv);
469
470 li = li->li_next;
471 if (li == NULL)
472 return -1;
473 return (int)tv_get_number(&li->li_tv);
474}
475#endif
476
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200477/*
478 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200479 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200480 * When not used yet add the variable to the v: hashtable.
481 */
482 void
483prepare_vimvar(int idx, typval_T *save_tv)
484{
485 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200486 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200487 if (vimvars[idx].vv_type == VAR_UNKNOWN)
488 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
489}
490
491/*
492 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200493 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200494 * When no longer defined, remove the variable from the v: hashtable.
495 */
496 void
497restore_vimvar(int idx, typval_T *save_tv)
498{
499 hashitem_T *hi;
500
501 vimvars[idx].vv_tv = *save_tv;
502 if (vimvars[idx].vv_type == VAR_UNKNOWN)
503 {
504 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
505 if (HASHITEM_EMPTY(hi))
506 internal_error("restore_vimvar()");
507 else
508 hash_remove(&vimvarht, hi);
509 }
510}
511
512/*
513 * List Vim variables.
514 */
515 static void
516list_vim_vars(int *first)
517{
518 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
519}
520
521/*
522 * List script-local variables, if there is a script.
523 */
524 static void
525list_script_vars(int *first)
526{
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100527 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= script_items.ga_len)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200528 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
529 "s:", FALSE, first);
530}
531
532/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200533 * Get a list of lines from a HERE document. The here document is a list of
534 * lines surrounded by a marker.
535 * cmd << {marker}
536 * {line1}
537 * {line2}
538 * ....
539 * {marker}
540 *
541 * The {marker} is a string. If the optional 'trim' word is supplied before the
542 * marker, then the leading indentation before the lines (matching the
543 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200544 *
545 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
546 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
547 * missing, then '.' is accepted as a marker.
548 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200549 * Returns a List with {lines} or NULL.
550 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200552heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200553{
554 char_u *theline;
555 char_u *marker;
556 list_T *l;
557 char_u *p;
558 int marker_indent_len = 0;
559 int text_indent_len = 0;
560 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200561 char_u dot[] = ".";
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200562
563 if (eap->getline == NULL)
564 {
565 emsg(_("E991: cannot use =<< here"));
566 return NULL;
567 }
568
569 // Check for the optional 'trim' word before the marker
570 cmd = skipwhite(cmd);
571 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
572 {
573 cmd = skipwhite(cmd + 4);
574
575 // Trim the indentation from all the lines in the here document.
576 // The amount of indentation trimmed is the same as the indentation of
577 // the first line after the :let command line. To find the end marker
578 // the indent of the :let command line is trimmed.
579 p = *eap->cmdlinep;
580 while (VIM_ISWHITE(*p))
581 {
582 p++;
583 marker_indent_len++;
584 }
585 text_indent_len = -1;
586 }
587
588 // The marker is the next word.
589 if (*cmd != NUL && *cmd != '"')
590 {
591 marker = skipwhite(cmd);
592 p = skiptowhite(marker);
593 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
594 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200595 semsg(_(e_trailing_arg), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200596 return NULL;
597 }
598 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200599 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200600 {
601 emsg(_("E221: Marker cannot start with lower case letter"));
602 return NULL;
603 }
604 }
605 else
606 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200607 // When getting lines for an embedded script, if the marker is missing,
608 // accept '.' as the marker.
609 if (script_get)
610 marker = dot;
611 else
612 {
613 emsg(_("E172: Missing marker"));
614 return NULL;
615 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200616 }
617
618 l = list_alloc();
619 if (l == NULL)
620 return NULL;
621
622 for (;;)
623 {
624 int mi = 0;
625 int ti = 0;
626
627 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
628 if (theline == NULL)
629 {
630 semsg(_("E990: Missing end marker '%s'"), marker);
631 break;
632 }
633
634 // with "trim": skip the indent matching the :let line to find the
635 // marker
636 if (marker_indent_len > 0
637 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
638 mi = marker_indent_len;
639 if (STRCMP(marker, theline + mi) == 0)
640 {
641 vim_free(theline);
642 break;
643 }
644
645 if (text_indent_len == -1 && *theline != NUL)
646 {
647 // set the text indent from the first line.
648 p = theline;
649 text_indent_len = 0;
650 while (VIM_ISWHITE(*p))
651 {
652 p++;
653 text_indent_len++;
654 }
655 text_indent = vim_strnsave(theline, text_indent_len);
656 }
657 // with "trim": skip the indent matching the first line
658 if (text_indent != NULL)
659 for (ti = 0; ti < text_indent_len; ++ti)
660 if (theline[ti] != text_indent[ti])
661 break;
662
663 if (list_append_string(l, theline + ti, -1) == FAIL)
664 break;
665 vim_free(theline);
666 }
667 vim_free(text_indent);
668
669 return l;
670}
671
672/*
673 * ":let" list all variable values
674 * ":let var1 var2" list variable values
675 * ":let var = expr" assignment command.
676 * ":let var += expr" assignment command.
677 * ":let var -= expr" assignment command.
678 * ":let var *= expr" assignment command.
679 * ":let var /= expr" assignment command.
680 * ":let var %= expr" assignment command.
681 * ":let var .= expr" assignment command.
682 * ":let var ..= expr" assignment command.
683 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100684 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100685 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200686 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200687 * ":const" list all variable values
688 * ":const var1 var2" list variable values
689 * ":const var = expr" assignment command.
690 * ":const [var1, var2] = expr" unpack list.
691 */
692 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200693ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200694{
695 char_u *arg = eap->arg;
696 char_u *expr = NULL;
697 typval_T rettv;
698 int i;
699 int var_count = 0;
700 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200701 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200702 char_u *argend;
703 int first = TRUE;
704 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200705 int has_assign;
Bram Moolenaar09689a02020-05-09 22:50:08 +0200706 int flags = eap->cmdidx == CMD_const ? LET_IS_CONST : 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200707 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200708
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709 // detect Vim9 assignment without ":let" or ":const"
710 if (eap->arg == eap->cmd)
711 flags |= LET_NO_COMMAND;
712
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200713 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200714 if (argend == NULL)
715 return;
716 if (argend > arg && argend[-1] == '.') // for var.='str'
717 --argend;
718 expr = skipwhite(argend);
719 concat = expr[0] == '.'
720 && ((expr[1] == '=' && current_sctx.sc_version < 2)
721 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200722 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
723 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200724 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200725 {
726 // ":let" without "=": list variables
727 if (*arg == '[')
728 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200729 else if (expr[0] == '.' && expr[1] == '=')
730 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200731 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200732 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200733 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200734 {
735 // Vim9 declaration ":let var: type"
736 arg = vim9_declare_scriptvar(eap, arg);
737 }
738 else
739 {
740 // ":let var1 var2" - list values
741 arg = list_arg_vars(eap, arg, &first);
742 }
743 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200744 else if (!eap->skip)
745 {
746 // ":let"
747 list_glob_vars(&first);
748 list_buf_vars(&first);
749 list_win_vars(&first);
750 list_tab_vars(&first);
751 list_script_vars(&first);
752 list_func_vars(&first);
753 list_vim_vars(&first);
754 }
755 eap->nextcmd = check_nextcmd(arg);
756 }
757 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
758 {
759 list_T *l;
760
761 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200762 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200763 if (l != NULL)
764 {
765 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200766 if (!eap->skip)
767 {
768 op[0] = '=';
769 op[1] = NUL;
770 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100771 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200772 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200773 clear_tv(&rettv);
774 }
775 }
776 else
777 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200778 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200779 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200780
Bram Moolenaar32e35112020-05-14 22:41:15 +0200781 rettv.v_type = VAR_UNKNOWN;
782 i = FAIL;
783 if (has_assign || concat)
784 {
785 op[0] = '=';
786 op[1] = NUL;
787 if (*expr != '=')
788 {
789 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
790 {
791 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200792 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200793 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200794 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200795 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200796 ++len;
797 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200798 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200799 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200800 }
801 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200802 ++expr;
803
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200804 if (vim9script && (!VIM_ISWHITE(*argend)
805 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200806 {
807 vim_strncpy(op, expr - len, len);
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200808 semsg(_(e_white_space_required_before_and_after), op);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200809 i = FAIL;
810 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200811
812 if (eap->skip)
813 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200814 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200815 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200816 if (getline_equal(eap->getline, eap->cookie, getsourceline))
817 {
818 evalarg.eval_getline = eap->getline;
819 evalarg.eval_cookie = eap->cookie;
820 }
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200821 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200822 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200823 if (eap->skip)
824 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200825 clear_evalarg(&evalarg, eap);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200826 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200827 if (eap->skip)
828 {
829 if (i != FAIL)
830 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200831 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200832 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200833 {
834 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200835 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200836 clear_tv(&rettv);
837 }
838 }
839}
840
841/*
842 * Assign the typevalue "tv" to the variable or variables at "arg_start".
843 * Handles both "var" with any type and "[var, var; var]" with a list type.
844 * When "op" is not NULL it points to a string with characters that
845 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
846 * or concatenate.
847 * Returns OK or FAIL;
848 */
849 int
850ex_let_vars(
851 char_u *arg_start,
852 typval_T *tv,
853 int copy, // copy values from "tv", don't move
854 int semicolon, // from skip_var_list()
855 int var_count, // from skip_var_list()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100856 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200857 char_u *op)
858{
859 char_u *arg = arg_start;
860 list_T *l;
861 int i;
862 listitem_T *item;
863 typval_T ltv;
864
865 if (*arg != '[')
866 {
867 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200869 return FAIL;
870 return OK;
871 }
872
873 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
874 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
875 {
876 emsg(_(e_listreq));
877 return FAIL;
878 }
879
880 i = list_len(l);
881 if (semicolon == 0 && var_count < i)
882 {
883 emsg(_("E687: Less targets than List items"));
884 return FAIL;
885 }
886 if (var_count - semicolon > i)
887 {
888 emsg(_("E688: More targets than List items"));
889 return FAIL;
890 }
891
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200892 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200893 item = l->lv_first;
894 while (*arg != ']')
895 {
896 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100897 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200898 item = item->li_next;
899 if (arg == NULL)
900 return FAIL;
901
902 arg = skipwhite(arg);
903 if (*arg == ';')
904 {
905 // Put the rest of the list (may be empty) in the var after ';'.
906 // Create a new list for this.
907 l = list_alloc();
908 if (l == NULL)
909 return FAIL;
910 while (item != NULL)
911 {
912 list_append_tv(l, &item->li_tv);
913 item = item->li_next;
914 }
915
916 ltv.v_type = VAR_LIST;
917 ltv.v_lock = 0;
918 ltv.vval.v_list = l;
919 l->lv_refcount = 1;
920
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100921 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200922 (char_u *)"]", op);
923 clear_tv(&ltv);
924 if (arg == NULL)
925 return FAIL;
926 break;
927 }
928 else if (*arg != ',' && *arg != ']')
929 {
930 internal_error("ex_let_vars()");
931 return FAIL;
932 }
933 }
934
935 return OK;
936}
937
938/*
939 * Skip over assignable variable "var" or list of variables "[var, var]".
940 * Used for ":let varvar = expr" and ":for varvar in expr".
941 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200942 * for "[var, var; var]" set "semicolon" to 1.
943 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200944 * Return NULL for an error.
945 */
946 char_u *
947skip_var_list(
948 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200950 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200951 int *semicolon,
952 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200953{
954 char_u *p, *s;
955
956 if (*arg == '[')
957 {
958 // "[var, var]": find the matching ']'.
959 p = arg;
960 for (;;)
961 {
962 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200963 s = skip_var_one(p, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200964 if (s == p)
965 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200966 if (!silent)
967 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200968 return NULL;
969 }
970 ++*var_count;
971
972 p = skipwhite(s);
973 if (*p == ']')
974 break;
975 else if (*p == ';')
976 {
977 if (*semicolon == 1)
978 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200979 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200980 return NULL;
981 }
982 *semicolon = 1;
983 }
984 else if (*p != ',')
985 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200986 if (!silent)
987 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200988 return NULL;
989 }
990 }
991 return p + 1;
992 }
993 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100994 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200995}
996
997/*
998 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
999 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001000 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001001 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001002 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001003skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001004{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005 char_u *end;
1006
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001007 if (*arg == '@' && arg[1] != NUL)
1008 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001009 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001010 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001011 if (include_type && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001012 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001013 // "a: type" is declaring variable "a" with a type, not "a:".
1014 if (end == arg + 2 && end[-1] == ':')
1015 --end;
1016 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001017 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001018 }
1019 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001020}
1021
1022/*
1023 * List variables for hashtab "ht" with prefix "prefix".
1024 * If "empty" is TRUE also list NULL strings as empty strings.
1025 */
1026 void
1027list_hashtable_vars(
1028 hashtab_T *ht,
1029 char *prefix,
1030 int empty,
1031 int *first)
1032{
1033 hashitem_T *hi;
1034 dictitem_T *di;
1035 int todo;
1036 char_u buf[IOSIZE];
1037
1038 todo = (int)ht->ht_used;
1039 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1040 {
1041 if (!HASHITEM_EMPTY(hi))
1042 {
1043 --todo;
1044 di = HI2DI(hi);
1045
1046 // apply :filter /pat/ to variable name
1047 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1048 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1049 if (message_filtered(buf))
1050 continue;
1051
1052 if (empty || di->di_tv.v_type != VAR_STRING
1053 || di->di_tv.vval.v_string != NULL)
1054 list_one_var(di, prefix, first);
1055 }
1056 }
1057}
1058
1059/*
1060 * List global variables.
1061 */
1062 static void
1063list_glob_vars(int *first)
1064{
1065 list_hashtable_vars(&globvarht, "", TRUE, first);
1066}
1067
1068/*
1069 * List buffer variables.
1070 */
1071 static void
1072list_buf_vars(int *first)
1073{
1074 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1075}
1076
1077/*
1078 * List window variables.
1079 */
1080 static void
1081list_win_vars(int *first)
1082{
1083 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1084}
1085
1086/*
1087 * List tab page variables.
1088 */
1089 static void
1090list_tab_vars(int *first)
1091{
1092 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1093}
1094
1095/*
1096 * List variables in "arg".
1097 */
1098 static char_u *
1099list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1100{
1101 int error = FALSE;
1102 int len;
1103 char_u *name;
1104 char_u *name_start;
1105 char_u *arg_subsc;
1106 char_u *tofree;
1107 typval_T tv;
1108
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001109 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001110 {
1111 if (error || eap->skip)
1112 {
1113 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1114 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1115 {
1116 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001117 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001118 break;
1119 }
1120 }
1121 else
1122 {
1123 // get_name_len() takes care of expanding curly braces
1124 name_start = name = arg;
1125 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1126 if (len <= 0)
1127 {
1128 // This is mainly to keep test 49 working: when expanding
1129 // curly braces fails overrule the exception error message.
1130 if (len < 0 && !aborting())
1131 {
1132 emsg_severe = TRUE;
1133 semsg(_(e_invarg2), arg);
1134 break;
1135 }
1136 error = TRUE;
1137 }
1138 else
1139 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001140 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001141 if (tofree != NULL)
1142 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001143 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001144 error = TRUE;
1145 else
1146 {
1147 // handle d.key, l[idx], f(expr)
1148 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001149 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001150 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001151 error = TRUE;
1152 else
1153 {
1154 if (arg == arg_subsc && len == 2 && name[1] == ':')
1155 {
1156 switch (*name)
1157 {
1158 case 'g': list_glob_vars(first); break;
1159 case 'b': list_buf_vars(first); break;
1160 case 'w': list_win_vars(first); break;
1161 case 't': list_tab_vars(first); break;
1162 case 'v': list_vim_vars(first); break;
1163 case 's': list_script_vars(first); break;
1164 case 'l': list_func_vars(first); break;
1165 default:
1166 semsg(_("E738: Can't list variables for %s"), name);
1167 }
1168 }
1169 else
1170 {
1171 char_u numbuf[NUMBUFLEN];
1172 char_u *tf;
1173 int c;
1174 char_u *s;
1175
1176 s = echo_string(&tv, &tf, numbuf, 0);
1177 c = *arg;
1178 *arg = NUL;
1179 list_one_var_a("",
1180 arg == arg_subsc ? name : name_start,
1181 tv.v_type,
1182 s == NULL ? (char_u *)"" : s,
1183 first);
1184 *arg = c;
1185 vim_free(tf);
1186 }
1187 clear_tv(&tv);
1188 }
1189 }
1190 }
1191
1192 vim_free(tofree);
1193 }
1194
1195 arg = skipwhite(arg);
1196 }
1197
1198 return arg;
1199}
1200
1201/*
1202 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1203 * Returns a pointer to the char just after the var name.
1204 * Returns NULL if there is an error.
1205 */
1206 static char_u *
1207ex_let_one(
1208 char_u *arg, // points to variable name
1209 typval_T *tv, // value to assign to variable
1210 int copy, // copy value from "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001212 char_u *endchars, // valid chars after variable name or NULL
1213 char_u *op) // "+", "-", "." or NULL
1214{
1215 int c1;
1216 char_u *name;
1217 char_u *p;
1218 char_u *arg_end = NULL;
1219 int len;
1220 int opt_flags;
1221 char_u *tofree = NULL;
1222
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001223 if (in_vim9script() && (flags & LET_NO_COMMAND) == 0
1224 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1225 {
1226 vim9_declare_error(arg);
1227 return NULL;
1228 }
1229
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001230 // ":let $VAR = expr": Set environment variable.
1231 if (*arg == '$')
1232 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001233 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001234 {
1235 emsg(_("E996: Cannot lock an environment variable"));
1236 return NULL;
1237 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001238
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001239 // Find the end of the name.
1240 ++arg;
1241 name = arg;
1242 len = get_env_len(&arg);
1243 if (len == 0)
1244 semsg(_(e_invarg2), name - 1);
1245 else
1246 {
1247 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1248 semsg(_(e_letwrong), op);
1249 else if (endchars != NULL
1250 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1251 emsg(_(e_letunexp));
1252 else if (!check_secure())
1253 {
1254 c1 = name[len];
1255 name[len] = NUL;
1256 p = tv_get_string_chk(tv);
1257 if (p != NULL && op != NULL && *op == '.')
1258 {
1259 int mustfree = FALSE;
1260 char_u *s = vim_getenv(name, &mustfree);
1261
1262 if (s != NULL)
1263 {
1264 p = tofree = concat_str(s, p);
1265 if (mustfree)
1266 vim_free(s);
1267 }
1268 }
1269 if (p != NULL)
1270 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001271 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001272 arg_end = arg;
1273 }
1274 name[len] = c1;
1275 vim_free(tofree);
1276 }
1277 }
1278 }
1279
1280 // ":let &option = expr": Set option value.
1281 // ":let &l:option = expr": Set local option value.
1282 // ":let &g:option = expr": Set global option value.
1283 else if (*arg == '&')
1284 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001286 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001288 return NULL;
1289 }
1290 // Find the end of the name.
1291 p = find_option_end(&arg, &opt_flags);
1292 if (p == NULL || (endchars != NULL
1293 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1294 emsg(_(e_letunexp));
1295 else
1296 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001297 long n = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001298 int opt_type;
1299 long numval;
1300 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001301 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001302 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001303
1304 c1 = *p;
1305 *p = NUL;
1306
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001307 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
1308 if ((opt_type == 1 || opt_type == -1)
1309 && (tv->v_type != VAR_STRING || !in_vim9script()))
1310 // number, possibly hidden
1311 n = (long)tv_get_number(tv);
1312
1313 // Avoid setting a string option to the text "v:false" or similar.
1314 // In Vim9 script also don't convert a number to string.
1315 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1316 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1317 s = tv_get_string_chk(tv);
1318
1319 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001320 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001321 if ((opt_type == 1 && *op == '.')
1322 || (opt_type == 0 && *op != '.'))
1323 {
1324 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001325 failed = TRUE; // don't set the value
1326
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001327 }
1328 else
1329 {
1330 if (opt_type == 1) // number
1331 {
1332 switch (*op)
1333 {
1334 case '+': n = numval + n; break;
1335 case '-': n = numval - n; break;
1336 case '*': n = numval * n; break;
1337 case '/': n = (long)num_divide(numval, n); break;
1338 case '%': n = (long)num_modulus(numval, n); break;
1339 }
1340 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001341 else if (opt_type == 0 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001342 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001343 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001344 s = concat_str(stringval, s);
1345 vim_free(stringval);
1346 stringval = s;
1347 }
1348 }
1349 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001350
1351 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001352 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001353 if (opt_type != 0 || s != NULL)
1354 {
1355 set_option_value(arg, n, s, opt_flags);
1356 arg_end = p;
1357 }
1358 else
1359 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001360 }
1361 *p = c1;
1362 vim_free(stringval);
1363 }
1364 }
1365
1366 // ":let @r = expr": Set register contents.
1367 else if (*arg == '@')
1368 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001369 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001370 {
1371 emsg(_("E996: Cannot lock a register"));
1372 return NULL;
1373 }
1374 ++arg;
1375 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1376 semsg(_(e_letwrong), op);
1377 else if (endchars != NULL
1378 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1379 emsg(_(e_letunexp));
1380 else
1381 {
1382 char_u *ptofree = NULL;
1383 char_u *s;
1384
1385 p = tv_get_string_chk(tv);
1386 if (p != NULL && op != NULL && *op == '.')
1387 {
1388 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1389 if (s != NULL)
1390 {
1391 p = ptofree = concat_str(s, p);
1392 vim_free(s);
1393 }
1394 }
1395 if (p != NULL)
1396 {
1397 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1398 arg_end = arg + 1;
1399 }
1400 vim_free(ptofree);
1401 }
1402 }
1403
1404 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001405 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001406 // ":let {expr} = expr": Idem, name made with curly braces
1407 else if (eval_isnamec1(*arg) || *arg == '{')
1408 {
1409 lval_T lv;
1410
1411 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001412 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001413 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001414 if (endchars != NULL && vim_strchr(endchars,
1415 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001416 emsg(_(e_letunexp));
1417 else
1418 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001419 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001420 arg_end = p;
1421 }
1422 }
1423 clear_lval(&lv);
1424 }
1425
1426 else
1427 semsg(_(e_invarg2), arg);
1428
1429 return arg_end;
1430}
1431
1432/*
1433 * ":unlet[!] var1 ... " command.
1434 */
1435 void
1436ex_unlet(exarg_T *eap)
1437{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001438 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001439}
1440
1441/*
1442 * ":lockvar" and ":unlockvar" commands
1443 */
1444 void
1445ex_lockvar(exarg_T *eap)
1446{
1447 char_u *arg = eap->arg;
1448 int deep = 2;
1449
1450 if (eap->forceit)
1451 deep = -1;
1452 else if (vim_isdigit(*arg))
1453 {
1454 deep = getdigits(&arg);
1455 arg = skipwhite(arg);
1456 }
1457
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001458 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001459}
1460
1461/*
1462 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001463 * Also used for Vim9 script. "callback" is invoked as:
1464 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001465 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001466 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001467ex_unletlock(
1468 exarg_T *eap,
1469 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001470 int deep,
1471 int glv_flags,
1472 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1473 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001474{
1475 char_u *arg = argstart;
1476 char_u *name_end;
1477 int error = FALSE;
1478 lval_T lv;
1479
1480 do
1481 {
1482 if (*arg == '$')
1483 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001484 lv.ll_name = arg;
1485 lv.ll_tv = NULL;
1486 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001487 if (get_env_len(&arg) == 0)
1488 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001489 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001490 return;
1491 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001492 if (!error && !eap->skip
1493 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1494 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001495 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001496 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001497 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001498 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001499 // Parse the name and find the end.
1500 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1501 glv_flags, FNE_CHECK_START);
1502 if (lv.ll_name == NULL)
1503 error = TRUE; // error but continue parsing
1504 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1505 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001506 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001507 if (name_end != NULL)
1508 {
1509 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001510 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001511 }
1512 if (!(eap->skip || error))
1513 clear_lval(&lv);
1514 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001515 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001516
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001517 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001518 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001519 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001520
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001521 if (!eap->skip)
1522 clear_lval(&lv);
1523 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001524
1525 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001526 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001527
1528 eap->nextcmd = check_nextcmd(arg);
1529}
1530
1531 static int
1532do_unlet_var(
1533 lval_T *lp,
1534 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001535 exarg_T *eap,
1536 int deep UNUSED,
1537 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001538{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001539 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001540 int ret = OK;
1541 int cc;
1542
1543 if (lp->ll_tv == NULL)
1544 {
1545 cc = *name_end;
1546 *name_end = NUL;
1547
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001548 // Environment variable, normal name or expanded name.
1549 if (*lp->ll_name == '$')
1550 vim_unsetenv(lp->ll_name + 1);
1551 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001552 ret = FAIL;
1553 *name_end = cc;
1554 }
1555 else if ((lp->ll_list != NULL
1556 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
1557 || (lp->ll_dict != NULL
1558 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
1559 return FAIL;
1560 else if (lp->ll_range)
1561 {
1562 listitem_T *li;
1563 listitem_T *ll_li = lp->ll_li;
1564 int ll_n1 = lp->ll_n1;
1565
1566 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1567 {
1568 li = ll_li->li_next;
1569 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
1570 return FAIL;
1571 ll_li = li;
1572 ++ll_n1;
1573 }
1574
1575 // Delete a range of List items.
1576 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1577 {
1578 li = lp->ll_li->li_next;
1579 listitem_remove(lp->ll_list, lp->ll_li);
1580 lp->ll_li = li;
1581 ++lp->ll_n1;
1582 }
1583 }
1584 else
1585 {
1586 if (lp->ll_list != NULL)
1587 // unlet a List item.
1588 listitem_remove(lp->ll_list, lp->ll_li);
1589 else
1590 // unlet a Dictionary item.
1591 dictitem_remove(lp->ll_dict, lp->ll_di);
1592 }
1593
1594 return ret;
1595}
1596
1597/*
1598 * "unlet" a variable. Return OK if it existed, FAIL if not.
1599 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1600 */
1601 int
1602do_unlet(char_u *name, int forceit)
1603{
1604 hashtab_T *ht;
1605 hashitem_T *hi;
1606 char_u *varname;
1607 dict_T *d;
1608 dictitem_T *di;
1609
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001610 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001611 return FAIL;
1612
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001613 ht = find_var_ht(name, &varname);
1614 if (ht != NULL && *varname != NUL)
1615 {
1616 d = get_current_funccal_dict(ht);
1617 if (d == NULL)
1618 {
1619 if (ht == &globvarht)
1620 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001621 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001622 d = &vimvardict;
1623 else
1624 {
1625 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1626 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1627 }
1628 if (d == NULL)
1629 {
1630 internal_error("do_unlet()");
1631 return FAIL;
1632 }
1633 }
1634 hi = hash_find(ht, varname);
1635 if (HASHITEM_EMPTY(hi))
1636 hi = find_hi_in_scoped_ht(name, &ht);
1637 if (hi != NULL && !HASHITEM_EMPTY(hi))
1638 {
1639 di = HI2DI(hi);
1640 if (var_check_fixed(di->di_flags, name, FALSE)
1641 || var_check_ro(di->di_flags, name, FALSE)
1642 || var_check_lock(d->dv_lock, name, FALSE))
1643 return FAIL;
1644
1645 delete_var(ht, hi);
1646 return OK;
1647 }
1648 }
1649 if (forceit)
1650 return OK;
1651 semsg(_("E108: No such variable: \"%s\""), name);
1652 return FAIL;
1653}
1654
1655/*
1656 * Lock or unlock variable indicated by "lp".
1657 * "deep" is the levels to go (-1 for unlimited);
1658 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1659 */
1660 static int
1661do_lock_var(
1662 lval_T *lp,
1663 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001664 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001665 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001666 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001667{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001668 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001669 int ret = OK;
1670 int cc;
1671 dictitem_T *di;
1672
1673 if (deep == 0) // nothing to do
1674 return OK;
1675
1676 if (lp->ll_tv == NULL)
1677 {
1678 cc = *name_end;
1679 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001680 if (*lp->ll_name == '$')
1681 {
1682 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001683 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001684 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001685 else
1686 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001687 // Normal name or expanded name.
1688 di = find_var(lp->ll_name, NULL, TRUE);
1689 if (di == NULL)
1690 ret = FAIL;
1691 else if ((di->di_flags & DI_FLAGS_FIX)
1692 && di->di_tv.v_type != VAR_DICT
1693 && di->di_tv.v_type != VAR_LIST)
1694 {
1695 // For historic reasons this error is not given for a list or
1696 // dict. E.g., the b: dict could be locked/unlocked.
1697 semsg(_(e_lock_unlock), lp->ll_name);
1698 ret = FAIL;
1699 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001700 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001701 {
1702 if (lock)
1703 di->di_flags |= DI_FLAGS_LOCK;
1704 else
1705 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001706 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001707 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001708 }
1709 *name_end = cc;
1710 }
1711 else if (lp->ll_range)
1712 {
1713 listitem_T *li = lp->ll_li;
1714
1715 // (un)lock a range of List items.
1716 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1717 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001718 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001719 li = li->li_next;
1720 ++lp->ll_n1;
1721 }
1722 }
1723 else if (lp->ll_list != NULL)
1724 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001725 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001726 else
1727 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001728 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001729
1730 return ret;
1731}
1732
1733/*
1734 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001735 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1736 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001737 */
1738 static void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001739item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001740{
1741 static int recurse = 0;
1742 list_T *l;
1743 listitem_T *li;
1744 dict_T *d;
1745 blob_T *b;
1746 hashitem_T *hi;
1747 int todo;
1748
1749 if (recurse >= DICT_MAXNEST)
1750 {
1751 emsg(_("E743: variable nested too deep for (un)lock"));
1752 return;
1753 }
1754 if (deep == 0)
1755 return;
1756 ++recurse;
1757
1758 // lock/unlock the item itself
1759 if (lock)
1760 tv->v_lock |= VAR_LOCKED;
1761 else
1762 tv->v_lock &= ~VAR_LOCKED;
1763
1764 switch (tv->v_type)
1765 {
1766 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001767 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001769 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001771 case VAR_STRING:
1772 case VAR_FUNC:
1773 case VAR_PARTIAL:
1774 case VAR_FLOAT:
1775 case VAR_SPECIAL:
1776 case VAR_JOB:
1777 case VAR_CHANNEL:
1778 break;
1779
1780 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001781 if ((b = tv->vval.v_blob) != NULL
1782 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001783 {
1784 if (lock)
1785 b->bv_lock |= VAR_LOCKED;
1786 else
1787 b->bv_lock &= ~VAR_LOCKED;
1788 }
1789 break;
1790 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001791 if ((l = tv->vval.v_list) != NULL
1792 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001793 {
1794 if (lock)
1795 l->lv_lock |= VAR_LOCKED;
1796 else
1797 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001798 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001799 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001800 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001801 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001802 }
1803 break;
1804 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001805 if ((d = tv->vval.v_dict) != NULL
1806 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001807 {
1808 if (lock)
1809 d->dv_lock |= VAR_LOCKED;
1810 else
1811 d->dv_lock &= ~VAR_LOCKED;
1812 if (deep < 0 || deep > 1)
1813 {
1814 // recursive: lock/unlock the items the List contains
1815 todo = (int)d->dv_hashtab.ht_used;
1816 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1817 {
1818 if (!HASHITEM_EMPTY(hi))
1819 {
1820 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001821 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1822 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001823 }
1824 }
1825 }
1826 }
1827 }
1828 --recurse;
1829}
1830
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001831#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1832/*
1833 * Delete all "menutrans_" variables.
1834 */
1835 void
1836del_menutrans_vars(void)
1837{
1838 hashitem_T *hi;
1839 int todo;
1840
1841 hash_lock(&globvarht);
1842 todo = (int)globvarht.ht_used;
1843 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1844 {
1845 if (!HASHITEM_EMPTY(hi))
1846 {
1847 --todo;
1848 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1849 delete_var(&globvarht, hi);
1850 }
1851 }
1852 hash_unlock(&globvarht);
1853}
1854#endif
1855
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001856/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001857 * Local string buffer for the next two functions to store a variable name
1858 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1859 * get_user_var_name().
1860 */
1861
1862static char_u *varnamebuf = NULL;
1863static int varnamebuflen = 0;
1864
1865/*
1866 * Function to concatenate a prefix and a variable name.
1867 */
1868 static char_u *
1869cat_prefix_varname(int prefix, char_u *name)
1870{
1871 int len;
1872
1873 len = (int)STRLEN(name) + 3;
1874 if (len > varnamebuflen)
1875 {
1876 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001877 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001878 varnamebuf = alloc(len);
1879 if (varnamebuf == NULL)
1880 {
1881 varnamebuflen = 0;
1882 return NULL;
1883 }
1884 varnamebuflen = len;
1885 }
1886 *varnamebuf = prefix;
1887 varnamebuf[1] = ':';
1888 STRCPY(varnamebuf + 2, name);
1889 return varnamebuf;
1890}
1891
1892/*
1893 * Function given to ExpandGeneric() to obtain the list of user defined
1894 * (global/buffer/window/built-in) variable names.
1895 */
1896 char_u *
1897get_user_var_name(expand_T *xp, int idx)
1898{
1899 static long_u gdone;
1900 static long_u bdone;
1901 static long_u wdone;
1902 static long_u tdone;
1903 static int vidx;
1904 static hashitem_T *hi;
1905 hashtab_T *ht;
1906
1907 if (idx == 0)
1908 {
1909 gdone = bdone = wdone = vidx = 0;
1910 tdone = 0;
1911 }
1912
1913 // Global variables
1914 if (gdone < globvarht.ht_used)
1915 {
1916 if (gdone++ == 0)
1917 hi = globvarht.ht_array;
1918 else
1919 ++hi;
1920 while (HASHITEM_EMPTY(hi))
1921 ++hi;
1922 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1923 return cat_prefix_varname('g', hi->hi_key);
1924 return hi->hi_key;
1925 }
1926
1927 // b: variables
1928 ht = &curbuf->b_vars->dv_hashtab;
1929 if (bdone < ht->ht_used)
1930 {
1931 if (bdone++ == 0)
1932 hi = ht->ht_array;
1933 else
1934 ++hi;
1935 while (HASHITEM_EMPTY(hi))
1936 ++hi;
1937 return cat_prefix_varname('b', hi->hi_key);
1938 }
1939
1940 // w: variables
1941 ht = &curwin->w_vars->dv_hashtab;
1942 if (wdone < ht->ht_used)
1943 {
1944 if (wdone++ == 0)
1945 hi = ht->ht_array;
1946 else
1947 ++hi;
1948 while (HASHITEM_EMPTY(hi))
1949 ++hi;
1950 return cat_prefix_varname('w', hi->hi_key);
1951 }
1952
1953 // t: variables
1954 ht = &curtab->tp_vars->dv_hashtab;
1955 if (tdone < ht->ht_used)
1956 {
1957 if (tdone++ == 0)
1958 hi = ht->ht_array;
1959 else
1960 ++hi;
1961 while (HASHITEM_EMPTY(hi))
1962 ++hi;
1963 return cat_prefix_varname('t', hi->hi_key);
1964 }
1965
1966 // v: variables
1967 if (vidx < VV_LEN)
1968 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
1969
1970 VIM_CLEAR(varnamebuf);
1971 varnamebuflen = 0;
1972 return NULL;
1973}
1974
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001975 char *
1976get_var_special_name(int nr)
1977{
1978 switch (nr)
1979 {
1980 case VVAL_FALSE: return "v:false";
1981 case VVAL_TRUE: return "v:true";
1982 case VVAL_NONE: return "v:none";
1983 case VVAL_NULL: return "v:null";
1984 }
1985 internal_error("get_var_special_name()");
1986 return "42";
1987}
1988
1989/*
1990 * Returns the global variable dictionary
1991 */
1992 dict_T *
1993get_globvar_dict(void)
1994{
1995 return &globvardict;
1996}
1997
1998/*
1999 * Returns the global variable hash table
2000 */
2001 hashtab_T *
2002get_globvar_ht(void)
2003{
2004 return &globvarht;
2005}
2006
2007/*
2008 * Returns the v: variable dictionary
2009 */
2010 dict_T *
2011get_vimvar_dict(void)
2012{
2013 return &vimvardict;
2014}
2015
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002016/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002018 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019 */
2020 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002021find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002023 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2024 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025
2026 if (di == NULL)
2027 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002028 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2030 return (int)(vv - vimvars);
2031}
2032
2033
2034/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002035 * Set type of v: variable to "type".
2036 */
2037 void
2038set_vim_var_type(int idx, vartype_T type)
2039{
2040 vimvars[idx].vv_type = type;
2041}
2042
2043/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002044 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002045 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002046 */
2047 void
2048set_vim_var_nr(int idx, varnumber_T val)
2049{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002050 vimvars[idx].vv_nr = val;
2051}
2052
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053 char *
2054get_vim_var_name(int idx)
2055{
2056 return vimvars[idx].vv_name;
2057}
2058
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002059/*
2060 * Get typval_T v: variable value.
2061 */
2062 typval_T *
2063get_vim_var_tv(int idx)
2064{
2065 return &vimvars[idx].vv_tv;
2066}
2067
2068/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002069 * Set v: variable to "tv". Only accepts the same type.
2070 * Takes over the value of "tv".
2071 */
2072 int
2073set_vim_var_tv(int idx, typval_T *tv)
2074{
2075 if (vimvars[idx].vv_type != tv->v_type)
2076 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002077 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002078 clear_tv(tv);
2079 return FAIL;
2080 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002081 // VV_RO is also checked when compiling, but let's check here as well.
2082 if (vimvars[idx].vv_flags & VV_RO)
2083 {
2084 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2085 return FAIL;
2086 }
2087 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2088 {
2089 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2090 return FAIL;
2091 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002092 clear_tv(&vimvars[idx].vv_di.di_tv);
2093 vimvars[idx].vv_di.di_tv = *tv;
2094 return OK;
2095}
2096
2097/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002098 * Get number v: variable value.
2099 */
2100 varnumber_T
2101get_vim_var_nr(int idx)
2102{
2103 return vimvars[idx].vv_nr;
2104}
2105
2106/*
2107 * Get string v: variable value. Uses a static buffer, can only be used once.
2108 * If the String variable has never been set, return an empty string.
2109 * Never returns NULL;
2110 */
2111 char_u *
2112get_vim_var_str(int idx)
2113{
2114 return tv_get_string(&vimvars[idx].vv_tv);
2115}
2116
2117/*
2118 * Get List v: variable value. Caller must take care of reference count when
2119 * needed.
2120 */
2121 list_T *
2122get_vim_var_list(int idx)
2123{
2124 return vimvars[idx].vv_list;
2125}
2126
2127/*
2128 * Get Dict v: variable value. Caller must take care of reference count when
2129 * needed.
2130 */
2131 dict_T *
2132get_vim_var_dict(int idx)
2133{
2134 return vimvars[idx].vv_dict;
2135}
2136
2137/*
2138 * Set v:char to character "c".
2139 */
2140 void
2141set_vim_var_char(int c)
2142{
2143 char_u buf[MB_MAXBYTES + 1];
2144
2145 if (has_mbyte)
2146 buf[(*mb_char2bytes)(c, buf)] = NUL;
2147 else
2148 {
2149 buf[0] = c;
2150 buf[1] = NUL;
2151 }
2152 set_vim_var_string(VV_CHAR, buf, -1);
2153}
2154
2155/*
2156 * Set v:count to "count" and v:count1 to "count1".
2157 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2158 */
2159 void
2160set_vcount(
2161 long count,
2162 long count1,
2163 int set_prevcount)
2164{
2165 if (set_prevcount)
2166 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2167 vimvars[VV_COUNT].vv_nr = count;
2168 vimvars[VV_COUNT1].vv_nr = count1;
2169}
2170
2171/*
2172 * Save variables that might be changed as a side effect. Used when executing
2173 * a timer callback.
2174 */
2175 void
2176save_vimvars(vimvars_save_T *vvsave)
2177{
2178 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2179 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2180 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2181}
2182
2183/*
2184 * Restore variables saved by save_vimvars().
2185 */
2186 void
2187restore_vimvars(vimvars_save_T *vvsave)
2188{
2189 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2190 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2191 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2192}
2193
2194/*
2195 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2196 * value.
2197 */
2198 void
2199set_vim_var_string(
2200 int idx,
2201 char_u *val,
2202 int len) // length of "val" to use or -1 (whole string)
2203{
2204 clear_tv(&vimvars[idx].vv_di.di_tv);
2205 vimvars[idx].vv_type = VAR_STRING;
2206 if (val == NULL)
2207 vimvars[idx].vv_str = NULL;
2208 else if (len == -1)
2209 vimvars[idx].vv_str = vim_strsave(val);
2210 else
2211 vimvars[idx].vv_str = vim_strnsave(val, len);
2212}
2213
2214/*
2215 * Set List v: variable to "val".
2216 */
2217 void
2218set_vim_var_list(int idx, list_T *val)
2219{
2220 clear_tv(&vimvars[idx].vv_di.di_tv);
2221 vimvars[idx].vv_type = VAR_LIST;
2222 vimvars[idx].vv_list = val;
2223 if (val != NULL)
2224 ++val->lv_refcount;
2225}
2226
2227/*
2228 * Set Dictionary v: variable to "val".
2229 */
2230 void
2231set_vim_var_dict(int idx, dict_T *val)
2232{
2233 clear_tv(&vimvars[idx].vv_di.di_tv);
2234 vimvars[idx].vv_type = VAR_DICT;
2235 vimvars[idx].vv_dict = val;
2236 if (val != NULL)
2237 {
2238 ++val->dv_refcount;
2239 dict_set_items_ro(val);
2240 }
2241}
2242
2243/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002244 * Set the v:argv list.
2245 */
2246 void
2247set_argv_var(char **argv, int argc)
2248{
2249 list_T *l = list_alloc();
2250 int i;
2251
2252 if (l == NULL)
2253 getout(1);
2254 l->lv_lock = VAR_FIXED;
2255 for (i = 0; i < argc; ++i)
2256 {
2257 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2258 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002259 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002260 }
2261 set_vim_var_list(VV_ARGV, l);
2262}
2263
2264/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002265 * Reset v:register, taking the 'clipboard' setting into account.
2266 */
2267 void
2268reset_reg_var(void)
2269{
2270 int regname = 0;
2271
2272 // Adjust the register according to 'clipboard', so that when
2273 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2274#ifdef FEAT_CLIPBOARD
2275 adjust_clip_reg(&regname);
2276#endif
2277 set_reg_var(regname);
2278}
2279
2280/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002281 * Set v:register if needed.
2282 */
2283 void
2284set_reg_var(int c)
2285{
2286 char_u regname;
2287
2288 if (c == 0 || c == ' ')
2289 regname = '"';
2290 else
2291 regname = c;
2292 // Avoid free/alloc when the value is already right.
2293 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2294 set_vim_var_string(VV_REG, &regname, 1);
2295}
2296
2297/*
2298 * Get or set v:exception. If "oldval" == NULL, return the current value.
2299 * Otherwise, restore the value to "oldval" and return NULL.
2300 * Must always be called in pairs to save and restore v:exception! Does not
2301 * take care of memory allocations.
2302 */
2303 char_u *
2304v_exception(char_u *oldval)
2305{
2306 if (oldval == NULL)
2307 return vimvars[VV_EXCEPTION].vv_str;
2308
2309 vimvars[VV_EXCEPTION].vv_str = oldval;
2310 return NULL;
2311}
2312
2313/*
2314 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2315 * Otherwise, restore the value to "oldval" and return NULL.
2316 * Must always be called in pairs to save and restore v:throwpoint! Does not
2317 * take care of memory allocations.
2318 */
2319 char_u *
2320v_throwpoint(char_u *oldval)
2321{
2322 if (oldval == NULL)
2323 return vimvars[VV_THROWPOINT].vv_str;
2324
2325 vimvars[VV_THROWPOINT].vv_str = oldval;
2326 return NULL;
2327}
2328
2329/*
2330 * Set v:cmdarg.
2331 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2332 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2333 * Must always be called in pairs!
2334 */
2335 char_u *
2336set_cmdarg(exarg_T *eap, char_u *oldarg)
2337{
2338 char_u *oldval;
2339 char_u *newval;
2340 unsigned len;
2341
2342 oldval = vimvars[VV_CMDARG].vv_str;
2343 if (eap == NULL)
2344 {
2345 vim_free(oldval);
2346 vimvars[VV_CMDARG].vv_str = oldarg;
2347 return NULL;
2348 }
2349
2350 if (eap->force_bin == FORCE_BIN)
2351 len = 6;
2352 else if (eap->force_bin == FORCE_NOBIN)
2353 len = 8;
2354 else
2355 len = 0;
2356
2357 if (eap->read_edit)
2358 len += 7;
2359
2360 if (eap->force_ff != 0)
2361 len += 10; // " ++ff=unix"
2362 if (eap->force_enc != 0)
2363 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2364 if (eap->bad_char != 0)
2365 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2366
2367 newval = alloc(len + 1);
2368 if (newval == NULL)
2369 return NULL;
2370
2371 if (eap->force_bin == FORCE_BIN)
2372 sprintf((char *)newval, " ++bin");
2373 else if (eap->force_bin == FORCE_NOBIN)
2374 sprintf((char *)newval, " ++nobin");
2375 else
2376 *newval = NUL;
2377
2378 if (eap->read_edit)
2379 STRCAT(newval, " ++edit");
2380
2381 if (eap->force_ff != 0)
2382 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2383 eap->force_ff == 'u' ? "unix"
2384 : eap->force_ff == 'd' ? "dos"
2385 : "mac");
2386 if (eap->force_enc != 0)
2387 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2388 eap->cmd + eap->force_enc);
2389 if (eap->bad_char == BAD_KEEP)
2390 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2391 else if (eap->bad_char == BAD_DROP)
2392 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2393 else if (eap->bad_char != 0)
2394 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2395 vimvars[VV_CMDARG].vv_str = newval;
2396 return oldval;
2397}
2398
2399/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002400 * Get the value of internal variable "name".
2401 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2402 */
2403 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002404eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002405 char_u *name,
2406 int len, // length of "name"
2407 typval_T *rettv, // NULL when only checking existence
2408 dictitem_T **dip, // non-NULL when typval's dict item is needed
2409 int verbose, // may give error message
2410 int no_autoload) // do not use script autoloading
2411{
2412 int ret = OK;
2413 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002414 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002415 dictitem_T *v;
2416 int cc;
2417
2418 // truncate the name, so that we can use strcmp()
2419 cc = name[len];
2420 name[len] = NUL;
2421
2422 // Check for user-defined variables.
2423 v = find_var(name, NULL, no_autoload);
2424 if (v != NULL)
2425 {
2426 tv = &v->di_tv;
2427 if (dip != NULL)
2428 *dip = v;
2429 }
2430
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002431 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002433 imported_T *import;
2434 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2435
2436 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002437
2438 // imported variable from another script
2439 if (import != NULL)
2440 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002441 if (import->imp_funcname != NULL)
2442 {
2443 foundFunc = TRUE;
2444 if (rettv != NULL)
2445 {
2446 rettv->v_type = VAR_FUNC;
2447 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2448 }
2449 }
2450 else
2451 {
2452 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002453 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002455 tv = sv->sv_tv;
2456 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002457 }
2458 }
2459
Bram Moolenaarc620c052020-07-08 15:16:19 +02002460 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002461 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002462 if (tv == NULL)
2463 {
2464 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002465 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002466 ret = FAIL;
2467 }
2468 else if (rettv != NULL)
2469 copy_tv(tv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002470 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002471
2472 name[len] = cc;
2473
2474 return ret;
2475}
2476
2477/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002478 * Check if variable "name[len]" is a local variable or an argument.
2479 * If so, "*eval_lavars_used" is set to TRUE.
2480 */
2481 void
2482check_vars(char_u *name, int len)
2483{
2484 int cc;
2485 char_u *varname;
2486 hashtab_T *ht;
2487
2488 if (eval_lavars_used == NULL)
2489 return;
2490
2491 // truncate the name, so that we can use strcmp()
2492 cc = name[len];
2493 name[len] = NUL;
2494
2495 ht = find_var_ht(name, &varname);
2496 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2497 {
2498 if (find_var(name, NULL, TRUE) != NULL)
2499 *eval_lavars_used = TRUE;
2500 }
2501
2502 name[len] = cc;
2503}
2504
2505/*
2506 * Find variable "name" in the list of variables.
2507 * Return a pointer to it if found, NULL if not found.
2508 * Careful: "a:0" variables don't have a name.
2509 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2510 * hashtab_T used.
2511 */
2512 dictitem_T *
2513find_var(char_u *name, hashtab_T **htp, int no_autoload)
2514{
2515 char_u *varname;
2516 hashtab_T *ht;
2517 dictitem_T *ret = NULL;
2518
2519 ht = find_var_ht(name, &varname);
2520 if (htp != NULL)
2521 *htp = ht;
2522 if (ht == NULL)
2523 return NULL;
2524 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2525 if (ret != NULL)
2526 return ret;
2527
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002528 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002529 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2530}
2531
2532/*
2533 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002534 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002535 * Returns NULL if not found.
2536 */
2537 dictitem_T *
2538find_var_in_ht(
2539 hashtab_T *ht,
2540 int htname,
2541 char_u *varname,
2542 int no_autoload)
2543{
2544 hashitem_T *hi;
2545
2546 if (*varname == NUL)
2547 {
2548 // Must be something like "s:", otherwise "ht" would be NULL.
2549 switch (htname)
2550 {
2551 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2552 case 'g': return &globvars_var;
2553 case 'v': return &vimvars_var;
2554 case 'b': return &curbuf->b_bufvar;
2555 case 'w': return &curwin->w_winvar;
2556 case 't': return &curtab->tp_winvar;
2557 case 'l': return get_funccal_local_var();
2558 case 'a': return get_funccal_args_var();
2559 }
2560 return NULL;
2561 }
2562
2563 hi = hash_find(ht, varname);
2564 if (HASHITEM_EMPTY(hi))
2565 {
2566 // For global variables we may try auto-loading the script. If it
2567 // worked find the variable again. Don't auto-load a script if it was
2568 // loaded already, otherwise it would be loaded every time when
2569 // checking if a function name is a Funcref variable.
2570 if (ht == &globvarht && !no_autoload)
2571 {
2572 // Note: script_autoload() may make "hi" invalid. It must either
2573 // be obtained again or not used.
2574 if (!script_autoload(varname, FALSE) || aborting())
2575 return NULL;
2576 hi = hash_find(ht, varname);
2577 }
2578 if (HASHITEM_EMPTY(hi))
2579 return NULL;
2580 }
2581 return HI2DI(hi);
2582}
2583
2584/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 * Get the script-local hashtab. NULL if not in a script context.
2586 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02002587 static hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588get_script_local_ht(void)
2589{
2590 scid_T sid = current_sctx.sc_sid;
2591
2592 if (sid > 0 && sid <= script_items.ga_len)
2593 return &SCRIPT_VARS(sid);
2594 return NULL;
2595}
2596
2597/*
2598 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002599 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002601 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2603{
2604 hashtab_T *ht = get_script_local_ht();
2605 char_u buffer[30];
2606 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002607 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608 hashitem_T *hi;
2609
2610 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002611 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612 if (len < sizeof(buffer) - 1)
2613 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002614 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615 vim_strncpy(buffer, name, len);
2616 p = buffer;
2617 }
2618 else
2619 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002620 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002622 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002623 }
2624
2625 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002626 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002627
2628 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002629 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2630 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002631
2632 if (p != buffer)
2633 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002634 // Don't return "buffer", gcc complains.
2635 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636}
2637
2638/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002639 * Find the hashtab used for a variable name.
2640 * Return NULL if the name is not valid.
2641 * Set "varname" to the start of name without ':'.
2642 */
2643 hashtab_T *
2644find_var_ht(char_u *name, char_u **varname)
2645{
2646 hashitem_T *hi;
2647 hashtab_T *ht;
2648
2649 if (name[0] == NUL)
2650 return NULL;
2651 if (name[1] != ':')
2652 {
2653 // The name must not start with a colon or #.
2654 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2655 return NULL;
2656 *varname = name;
2657
2658 // "version" is "v:version" in all scopes if scriptversion < 3.
2659 // Same for a few other variables marked with VV_COMPAT.
2660 if (current_sctx.sc_version < 3)
2661 {
2662 hi = hash_find(&compat_hashtab, name);
2663 if (!HASHITEM_EMPTY(hi))
2664 return &compat_hashtab;
2665 }
2666
2667 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 if (ht != NULL)
2669 return ht; // local variable
2670
2671 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002672 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002673 {
2674 ht = get_script_local_ht();
2675 if (ht != NULL)
2676 return ht;
2677 }
2678
2679 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002680 }
2681 *varname = name + 2;
2682 if (*name == 'g') // global variable
2683 return &globvarht;
2684 // There must be no ':' or '#' in the rest of the name, unless g: is used
2685 if (vim_strchr(name + 2, ':') != NULL
2686 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2687 return NULL;
2688 if (*name == 'b') // buffer variable
2689 return &curbuf->b_vars->dv_hashtab;
2690 if (*name == 'w') // window variable
2691 return &curwin->w_vars->dv_hashtab;
2692 if (*name == 't') // tab page variable
2693 return &curtab->tp_vars->dv_hashtab;
2694 if (*name == 'v') // v: variable
2695 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002696 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002697 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002698 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002699 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002700 if (*name == 'a') // a: function argument
2701 return get_funccal_args_ht();
2702 if (*name == 'l') // l: local function variable
2703 return get_funccal_local_ht();
2704 }
2705 if (*name == 's') // script variable
2706 {
2707 ht = get_script_local_ht();
2708 if (ht != NULL)
2709 return ht;
2710 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002711 return NULL;
2712}
2713
2714/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002715 * Get the string value of a (global/local) variable.
2716 * Note: see tv_get_string() for how long the pointer remains valid.
2717 * Returns NULL when it doesn't exist.
2718 */
2719 char_u *
2720get_var_value(char_u *name)
2721{
2722 dictitem_T *v;
2723
2724 v = find_var(name, NULL, FALSE);
2725 if (v == NULL)
2726 return NULL;
2727 return tv_get_string(&v->di_tv);
2728}
2729
2730/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002731 * Allocate a new hashtab for a sourced script. It will be used while
2732 * sourcing this script and when executing functions defined in the script.
2733 */
2734 void
2735new_script_vars(scid_T id)
2736{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002737 scriptvar_T *sv;
2738
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002739 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2740 if (sv == NULL)
2741 return;
2742 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002743 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002744}
2745
2746/*
2747 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2748 * point to it.
2749 */
2750 void
2751init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2752{
2753 hash_init(&dict->dv_hashtab);
2754 dict->dv_lock = 0;
2755 dict->dv_scope = scope;
2756 dict->dv_refcount = DO_NOT_FREE_CNT;
2757 dict->dv_copyID = 0;
2758 dict_var->di_tv.vval.v_dict = dict;
2759 dict_var->di_tv.v_type = VAR_DICT;
2760 dict_var->di_tv.v_lock = VAR_FIXED;
2761 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2762 dict_var->di_key[0] = NUL;
2763}
2764
2765/*
2766 * Unreference a dictionary initialized by init_var_dict().
2767 */
2768 void
2769unref_var_dict(dict_T *dict)
2770{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002771 // Now the dict needs to be freed if no one else is using it, go back to
2772 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002773 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2774 dict_unref(dict);
2775}
2776
2777/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002778 * Clean up a list of internal variables.
2779 * Frees all allocated variables and the value they contain.
2780 * Clears hashtab "ht", does not free it.
2781 */
2782 void
2783vars_clear(hashtab_T *ht)
2784{
2785 vars_clear_ext(ht, TRUE);
2786}
2787
2788/*
2789 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2790 */
2791 void
2792vars_clear_ext(hashtab_T *ht, int free_val)
2793{
2794 int todo;
2795 hashitem_T *hi;
2796 dictitem_T *v;
2797
2798 hash_lock(ht);
2799 todo = (int)ht->ht_used;
2800 for (hi = ht->ht_array; todo > 0; ++hi)
2801 {
2802 if (!HASHITEM_EMPTY(hi))
2803 {
2804 --todo;
2805
2806 // Free the variable. Don't remove it from the hashtab,
2807 // ht_array might change then. hash_clear() takes care of it
2808 // later.
2809 v = HI2DI(hi);
2810 if (free_val)
2811 clear_tv(&v->di_tv);
2812 if (v->di_flags & DI_FLAGS_ALLOC)
2813 vim_free(v);
2814 }
2815 }
2816 hash_clear(ht);
2817 ht->ht_used = 0;
2818}
2819
2820/*
2821 * Delete a variable from hashtab "ht" at item "hi".
2822 * Clear the variable value and free the dictitem.
2823 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002824 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002825delete_var(hashtab_T *ht, hashitem_T *hi)
2826{
2827 dictitem_T *di = HI2DI(hi);
2828
2829 hash_remove(ht, hi);
2830 clear_tv(&di->di_tv);
2831 vim_free(di);
2832}
2833
2834/*
2835 * List the value of one internal variable.
2836 */
2837 static void
2838list_one_var(dictitem_T *v, char *prefix, int *first)
2839{
2840 char_u *tofree;
2841 char_u *s;
2842 char_u numbuf[NUMBUFLEN];
2843
2844 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2845 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2846 s == NULL ? (char_u *)"" : s, first);
2847 vim_free(tofree);
2848}
2849
2850 static void
2851list_one_var_a(
2852 char *prefix,
2853 char_u *name,
2854 int type,
2855 char_u *string,
2856 int *first) // when TRUE clear rest of screen and set to FALSE
2857{
2858 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2859 msg_start();
2860 msg_puts(prefix);
2861 if (name != NULL) // "a:" vars don't have a name stored
2862 msg_puts((char *)name);
2863 msg_putchar(' ');
2864 msg_advance(22);
2865 if (type == VAR_NUMBER)
2866 msg_putchar('#');
2867 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2868 msg_putchar('*');
2869 else if (type == VAR_LIST)
2870 {
2871 msg_putchar('[');
2872 if (*string == '[')
2873 ++string;
2874 }
2875 else if (type == VAR_DICT)
2876 {
2877 msg_putchar('{');
2878 if (*string == '{')
2879 ++string;
2880 }
2881 else
2882 msg_putchar(' ');
2883
2884 msg_outtrans(string);
2885
2886 if (type == VAR_FUNC || type == VAR_PARTIAL)
2887 msg_puts("()");
2888 if (*first)
2889 {
2890 msg_clr_eos();
2891 *first = FALSE;
2892 }
2893}
2894
2895/*
2896 * Set variable "name" to value in "tv".
2897 * If the variable already exists, the value is updated.
2898 * Otherwise the variable is created.
2899 */
2900 void
2901set_var(
2902 char_u *name,
2903 typval_T *tv,
2904 int copy) // make copy of value in "tv"
2905{
Bram Moolenaareeb27bf2020-07-04 17:39:10 +02002906 set_var_const(name, NULL, tv, copy, LET_NO_COMMAND);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002907}
2908
2909/*
2910 * Set variable "name" to value in "tv".
2911 * If the variable already exists and "is_const" is FALSE the value is updated.
2912 * Otherwise the variable is created.
2913 */
2914 void
2915set_var_const(
2916 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 type_T *type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002918 typval_T *tv,
2919 int copy, // make copy of value in "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 int flags) // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002921{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002923 char_u *varname;
2924 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 int is_script_local;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002926
2927 ht = find_var_ht(name, &varname);
2928 if (ht == NULL || *varname == NUL)
2929 {
2930 semsg(_(e_illvar), name);
2931 return;
2932 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002933 is_script_local = ht == get_script_local_ht();
2934
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002935 if (in_vim9script()
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002936 && !is_script_local
2937 && (flags & LET_NO_COMMAND) == 0
2938 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02002939 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002940 vim9_declare_error(name);
Bram Moolenaar67979662020-06-20 22:50:47 +02002941 return;
2942 }
2943
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002945
2946 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 if (di == NULL)
2948 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002949
2950 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02002951 && var_wrong_func_name(name, di == NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002952 return;
2953
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002955 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002957 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 if (flags & LET_IS_CONST)
2959 {
2960 emsg(_(e_cannot_mod));
2961 return;
2962 }
2963
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002964 if (is_script_local && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 {
Bram Moolenaar34db91f2020-06-13 19:00:10 +02002966 if ((flags & LET_NO_COMMAND) == 0)
2967 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002968 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaar34db91f2020-06-13 19:00:10 +02002969 return;
2970 }
2971
2972 // check the type
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002973 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
2974 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002976
2977 if (var_check_ro(di->di_flags, name, FALSE)
2978 || var_check_lock(di->di_tv.v_lock, name, FALSE))
2979 return;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002980 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 else
2982 // can only redefine once
2983 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002984
2985 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002986
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002988 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002989 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002990 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002992 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002994 if (copy || tv->v_type != VAR_STRING)
2995 {
2996 char_u *val = tv_get_string(tv);
2997
2998 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002999 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 if (di->di_tv.vval.v_string == NULL)
3001 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003002 }
3003 else
3004 {
3005 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003007 tv->vval.v_string = NULL;
3008 }
3009 return;
3010 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003012 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003014 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003016#ifdef FEAT_SEARCH_EXTRA
3017 else if (STRCMP(varname, "hlsearch") == 0)
3018 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003020 redraw_all_later(SOME_VALID);
3021 }
3022#endif
3023 return;
3024 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003026 {
3027 semsg(_("E963: setting %s to value with wrong type"), name);
3028 return;
3029 }
3030 }
3031
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003033 }
3034 else // add a new variable
3035 {
3036 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003037 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003038 {
3039 semsg(_(e_illvar), name);
3040 return;
3041 }
3042
3043 // Make sure the variable name is valid.
3044 if (!valid_varname(varname))
3045 return;
3046
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3048 if (di == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003049 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 STRCPY(di->di_key, varname);
3051 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003052 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003054 return;
3055 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 di->di_flags = DI_FLAGS_ALLOC;
3057 if (flags & LET_IS_CONST)
3058 di->di_flags |= DI_FLAGS_LOCK;
3059
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003060 if (is_script_local && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003062 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063
3064 // Store a pointer to the typval_T, so that it can be found by
3065 // index instead of using a hastab lookup.
3066 if (ga_grow(&si->sn_var_vals, 1) == OK)
3067 {
3068 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3069 + si->sn_var_vals.ga_len;
3070 sv->sv_name = di->di_key;
3071 sv->sv_tv = &di->di_tv;
Bram Moolenaar53b29e42020-08-15 14:31:20 +02003072 if (type == NULL)
3073 sv->sv_type = typval2type(tv, &si->sn_type_list);
3074 else
3075 sv->sv_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 sv->sv_const = (flags & LET_IS_CONST);
3077 sv->sv_export = is_export;
3078 ++si->sn_var_vals.ga_len;
3079
3080 // let ex_export() know the export worked.
3081 is_export = FALSE;
3082 }
3083 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003084 }
3085
3086 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003088 else
3089 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 di->di_tv = *tv;
3091 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003092 init_tv(tv);
3093 }
3094
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003095 if (flags & LET_IS_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003096 // Like :lockvar! name: lock the value and what it contains, but only
3097 // if the reference count is up to one. That locks only literal
3098 // values.
3099 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003100}
3101
3102/*
3103 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3104 * Also give an error message.
3105 */
3106 int
3107var_check_ro(int flags, char_u *name, int use_gettext)
3108{
3109 if (flags & DI_FLAGS_RO)
3110 {
3111 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3112 return TRUE;
3113 }
3114 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3115 {
3116 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3117 return TRUE;
3118 }
3119 return FALSE;
3120}
3121
3122/*
3123 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3124 * Also give an error message.
3125 */
3126 int
3127var_check_fixed(int flags, char_u *name, int use_gettext)
3128{
3129 if (flags & DI_FLAGS_FIX)
3130 {
3131 semsg(_("E795: Cannot delete variable %s"),
3132 use_gettext ? (char_u *)_(name) : name);
3133 return TRUE;
3134 }
3135 return FALSE;
3136}
3137
3138/*
3139 * Check if a funcref is assigned to a valid variable name.
3140 * Return TRUE and give an error if not.
3141 */
3142 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003143var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003144 char_u *name, // points to start of variable name
3145 int new_var) // TRUE when creating the variable
3146{
3147 // Allow for w: b: s: and t:.
3148 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3149 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3150 ? name[2] : name[0]))
3151 {
3152 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3153 name);
3154 return TRUE;
3155 }
3156 // Don't allow hiding a function. When "v" is not NULL we might be
3157 // assigning another function to the same var, the type is checked
3158 // below.
3159 if (new_var && function_exists(name, FALSE))
3160 {
3161 semsg(_("E705: Variable name conflicts with existing function: %s"),
3162 name);
3163 return TRUE;
3164 }
3165 return FALSE;
3166}
3167
3168/*
3169 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
3170 * Also give an error message, using "name" or _("name") when use_gettext is
3171 * TRUE.
3172 */
3173 int
3174var_check_lock(int lock, char_u *name, int use_gettext)
3175{
3176 if (lock & VAR_LOCKED)
3177 {
3178 semsg(_("E741: Value is locked: %s"),
3179 name == NULL ? (char_u *)_("Unknown")
3180 : use_gettext ? (char_u *)_(name)
3181 : name);
3182 return TRUE;
3183 }
3184 if (lock & VAR_FIXED)
3185 {
3186 semsg(_("E742: Cannot change value of %s"),
3187 name == NULL ? (char_u *)_("Unknown")
3188 : use_gettext ? (char_u *)_(name)
3189 : name);
3190 return TRUE;
3191 }
3192 return FALSE;
3193}
3194
3195/*
3196 * Check if a variable name is valid.
3197 * Return FALSE and give an error if not.
3198 */
3199 int
3200valid_varname(char_u *varname)
3201{
3202 char_u *p;
3203
3204 for (p = varname; *p != NUL; ++p)
3205 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3206 && *p != AUTOLOAD_CHAR)
3207 {
3208 semsg(_(e_illvar), varname);
3209 return FALSE;
3210 }
3211 return TRUE;
3212}
3213
3214/*
3215 * getwinvar() and gettabwinvar()
3216 */
3217 static void
3218getwinvar(
3219 typval_T *argvars,
3220 typval_T *rettv,
3221 int off) // 1 for gettabwinvar()
3222{
3223 win_T *win;
3224 char_u *varname;
3225 dictitem_T *v;
3226 tabpage_T *tp = NULL;
3227 int done = FALSE;
3228 win_T *oldcurwin;
3229 tabpage_T *oldtabpage;
3230 int need_switch_win;
3231
3232 if (off == 1)
3233 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3234 else
3235 tp = curtab;
3236 win = find_win_by_nr(&argvars[off], tp);
3237 varname = tv_get_string_chk(&argvars[off + 1]);
3238 ++emsg_off;
3239
3240 rettv->v_type = VAR_STRING;
3241 rettv->vval.v_string = NULL;
3242
3243 if (win != NULL && varname != NULL)
3244 {
3245 // Set curwin to be our win, temporarily. Also set the tabpage,
3246 // otherwise the window is not valid. Only do this when needed,
3247 // autocommands get blocked.
3248 need_switch_win = !(tp == curtab && win == curwin);
3249 if (!need_switch_win
3250 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3251 {
3252 if (*varname == '&')
3253 {
3254 if (varname[1] == NUL)
3255 {
3256 // get all window-local options in a dict
3257 dict_T *opts = get_winbuf_options(FALSE);
3258
3259 if (opts != NULL)
3260 {
3261 rettv_dict_set(rettv, opts);
3262 done = TRUE;
3263 }
3264 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003265 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003266 // window-local-option
3267 done = TRUE;
3268 }
3269 else
3270 {
3271 // Look up the variable.
3272 // Let getwinvar({nr}, "") return the "w:" dictionary.
3273 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3274 varname, FALSE);
3275 if (v != NULL)
3276 {
3277 copy_tv(&v->di_tv, rettv);
3278 done = TRUE;
3279 }
3280 }
3281 }
3282
3283 if (need_switch_win)
3284 // restore previous notion of curwin
3285 restore_win(oldcurwin, oldtabpage, TRUE);
3286 }
3287
3288 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3289 // use the default return value
3290 copy_tv(&argvars[off + 2], rettv);
3291
3292 --emsg_off;
3293}
3294
3295/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003296 * Set option "varname" to the value of "varp" for the current buffer/window.
3297 */
3298 static void
3299set_option_from_tv(char_u *varname, typval_T *varp)
3300{
3301 long numval = 0;
3302 char_u *strval;
3303 char_u nbuf[NUMBUFLEN];
3304 int error = FALSE;
3305
3306 if (!in_vim9script() || varp->v_type != VAR_STRING)
3307 numval = (long)tv_get_number_chk(varp, &error);
3308 strval = tv_get_string_buf_chk(varp, nbuf);
3309 if (!error && strval != NULL)
3310 set_option_value(varname, numval, strval, OPT_LOCAL);
3311}
3312
3313/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003314 * "setwinvar()" and "settabwinvar()" functions
3315 */
3316 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003317setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003318{
3319 win_T *win;
3320 win_T *save_curwin;
3321 tabpage_T *save_curtab;
3322 int need_switch_win;
3323 char_u *varname, *winvarname;
3324 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003325 tabpage_T *tp = NULL;
3326
3327 if (check_secure())
3328 return;
3329
3330 if (off == 1)
3331 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3332 else
3333 tp = curtab;
3334 win = find_win_by_nr(&argvars[off], tp);
3335 varname = tv_get_string_chk(&argvars[off + 1]);
3336 varp = &argvars[off + 2];
3337
3338 if (win != NULL && varname != NULL && varp != NULL)
3339 {
3340 need_switch_win = !(tp == curtab && win == curwin);
3341 if (!need_switch_win
3342 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3343 {
3344 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003345 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003346 else
3347 {
3348 winvarname = alloc(STRLEN(varname) + 3);
3349 if (winvarname != NULL)
3350 {
3351 STRCPY(winvarname, "w:");
3352 STRCPY(winvarname + 2, varname);
3353 set_var(winvarname, varp, TRUE);
3354 vim_free(winvarname);
3355 }
3356 }
3357 }
3358 if (need_switch_win)
3359 restore_win(save_curwin, save_curtab, TRUE);
3360 }
3361}
3362
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003363/*
3364 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3365 * v:option_type, and v:option_command.
3366 */
3367 void
3368reset_v_option_vars(void)
3369{
3370 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3371 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3372 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3373 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3374 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3375 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3376}
3377
3378/*
3379 * Add an assert error to v:errors.
3380 */
3381 void
3382assert_error(garray_T *gap)
3383{
3384 struct vimvar *vp = &vimvars[VV_ERRORS];
3385
3386 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003387 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003388 set_vim_var_list(VV_ERRORS, list_alloc());
3389 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3390}
3391
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003392 int
3393var_exists(char_u *var)
3394{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003395 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003396 char_u *name;
3397 char_u *tofree;
3398 typval_T tv;
3399 int len = 0;
3400 int n = FALSE;
3401
3402 // get_name_len() takes care of expanding curly braces
3403 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003404 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003405 if (len > 0)
3406 {
3407 if (tofree != NULL)
3408 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003409 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003410 if (n)
3411 {
3412 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003413 arg = skipwhite(arg);
3414 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003415 if (n)
3416 clear_tv(&tv);
3417 }
3418 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003419 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003420 n = FALSE;
3421
3422 vim_free(tofree);
3423 return n;
3424}
3425
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003426static lval_T *redir_lval = NULL;
3427#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3428static garray_T redir_ga; // only valid when redir_lval is not NULL
3429static char_u *redir_endp = NULL;
3430static char_u *redir_varname = NULL;
3431
3432/*
3433 * Start recording command output to a variable
3434 * When "append" is TRUE append to an existing variable.
3435 * Returns OK if successfully completed the setup. FAIL otherwise.
3436 */
3437 int
3438var_redir_start(char_u *name, int append)
3439{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003440 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003441 typval_T tv;
3442
3443 // Catch a bad name early.
3444 if (!eval_isnamec1(*name))
3445 {
3446 emsg(_(e_invarg));
3447 return FAIL;
3448 }
3449
3450 // Make a copy of the name, it is used in redir_lval until redir ends.
3451 redir_varname = vim_strsave(name);
3452 if (redir_varname == NULL)
3453 return FAIL;
3454
3455 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3456 if (redir_lval == NULL)
3457 {
3458 var_redir_stop();
3459 return FAIL;
3460 }
3461
3462 // The output is stored in growarray "redir_ga" until redirection ends.
3463 ga_init2(&redir_ga, (int)sizeof(char), 500);
3464
3465 // Parse the variable name (can be a dict or list entry).
3466 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3467 FNE_CHECK_START);
3468 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3469 {
3470 clear_lval(redir_lval);
3471 if (redir_endp != NULL && *redir_endp != NUL)
3472 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003473 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003474 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003475 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003476 redir_endp = NULL; // don't store a value, only cleanup
3477 var_redir_stop();
3478 return FAIL;
3479 }
3480
3481 // check if we can write to the variable: set it to or append an empty
3482 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003483 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003484 tv.v_type = VAR_STRING;
3485 tv.vval.v_string = (char_u *)"";
3486 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003488 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003490 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003491 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003492 {
3493 redir_endp = NULL; // don't store a value, only cleanup
3494 var_redir_stop();
3495 return FAIL;
3496 }
3497
3498 return OK;
3499}
3500
3501/*
3502 * Append "value[value_len]" to the variable set by var_redir_start().
3503 * The actual appending is postponed until redirection ends, because the value
3504 * appended may in fact be the string we write to, changing it may cause freed
3505 * memory to be used:
3506 * :redir => foo
3507 * :let foo
3508 * :redir END
3509 */
3510 void
3511var_redir_str(char_u *value, int value_len)
3512{
3513 int len;
3514
3515 if (redir_lval == NULL)
3516 return;
3517
3518 if (value_len == -1)
3519 len = (int)STRLEN(value); // Append the entire string
3520 else
3521 len = value_len; // Append only "value_len" characters
3522
3523 if (ga_grow(&redir_ga, len) == OK)
3524 {
3525 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3526 redir_ga.ga_len += len;
3527 }
3528 else
3529 var_redir_stop();
3530}
3531
3532/*
3533 * Stop redirecting command output to a variable.
3534 * Frees the allocated memory.
3535 */
3536 void
3537var_redir_stop(void)
3538{
3539 typval_T tv;
3540
3541 if (EVALCMD_BUSY)
3542 {
3543 redir_lval = NULL;
3544 return;
3545 }
3546
3547 if (redir_lval != NULL)
3548 {
3549 // If there was no error: assign the text to the variable.
3550 if (redir_endp != NULL)
3551 {
3552 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3553 tv.v_type = VAR_STRING;
3554 tv.vval.v_string = redir_ga.ga_data;
3555 // Call get_lval() again, if it's inside a Dict or List it may
3556 // have changed.
3557 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3558 FALSE, FALSE, 0, FNE_CHECK_START);
3559 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003561 (char_u *)".");
3562 clear_lval(redir_lval);
3563 }
3564
3565 // free the collected output
3566 VIM_CLEAR(redir_ga.ga_data);
3567
3568 VIM_CLEAR(redir_lval);
3569 }
3570 VIM_CLEAR(redir_varname);
3571}
3572
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003573/*
3574 * "gettabvar()" function
3575 */
3576 void
3577f_gettabvar(typval_T *argvars, typval_T *rettv)
3578{
3579 win_T *oldcurwin;
3580 tabpage_T *tp, *oldtabpage;
3581 dictitem_T *v;
3582 char_u *varname;
3583 int done = FALSE;
3584
3585 rettv->v_type = VAR_STRING;
3586 rettv->vval.v_string = NULL;
3587
3588 varname = tv_get_string_chk(&argvars[1]);
3589 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3590 if (tp != NULL && varname != NULL)
3591 {
3592 // Set tp to be our tabpage, temporarily. Also set the window to the
3593 // first window in the tabpage, otherwise the window is not valid.
3594 if (switch_win(&oldcurwin, &oldtabpage,
3595 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3596 : tp->tp_firstwin, tp, TRUE) == OK)
3597 {
3598 // look up the variable
3599 // Let gettabvar({nr}, "") return the "t:" dictionary.
3600 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3601 if (v != NULL)
3602 {
3603 copy_tv(&v->di_tv, rettv);
3604 done = TRUE;
3605 }
3606 }
3607
3608 // restore previous notion of curwin
3609 restore_win(oldcurwin, oldtabpage, TRUE);
3610 }
3611
3612 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3613 copy_tv(&argvars[2], rettv);
3614}
3615
3616/*
3617 * "gettabwinvar()" function
3618 */
3619 void
3620f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3621{
3622 getwinvar(argvars, rettv, 1);
3623}
3624
3625/*
3626 * "getwinvar()" function
3627 */
3628 void
3629f_getwinvar(typval_T *argvars, typval_T *rettv)
3630{
3631 getwinvar(argvars, rettv, 0);
3632}
3633
3634/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003635 * "getbufvar()" function
3636 */
3637 void
3638f_getbufvar(typval_T *argvars, typval_T *rettv)
3639{
3640 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003641 char_u *varname;
3642 dictitem_T *v;
3643 int done = FALSE;
3644
3645 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3646 varname = tv_get_string_chk(&argvars[1]);
3647 ++emsg_off;
3648 buf = tv_get_buf(&argvars[0], FALSE);
3649
3650 rettv->v_type = VAR_STRING;
3651 rettv->vval.v_string = NULL;
3652
3653 if (buf != NULL && varname != NULL)
3654 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003655 if (*varname == '&')
3656 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003657 buf_T *save_curbuf = curbuf;
3658
3659 // set curbuf to be our buf, temporarily
3660 curbuf = buf;
3661
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003662 if (varname[1] == NUL)
3663 {
3664 // get all buffer-local options in a dict
3665 dict_T *opts = get_winbuf_options(TRUE);
3666
3667 if (opts != NULL)
3668 {
3669 rettv_dict_set(rettv, opts);
3670 done = TRUE;
3671 }
3672 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003673 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003674 // buffer-local-option
3675 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003676
3677 // restore previous notion of curbuf
3678 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003679 }
3680 else
3681 {
3682 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003683 if (*varname == NUL)
3684 // Let getbufvar({nr}, "") return the "b:" dictionary.
3685 v = &buf->b_bufvar;
3686 else
3687 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3688 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003689 if (v != NULL)
3690 {
3691 copy_tv(&v->di_tv, rettv);
3692 done = TRUE;
3693 }
3694 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003695 }
3696
3697 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3698 // use the default value
3699 copy_tv(&argvars[2], rettv);
3700
3701 --emsg_off;
3702}
3703
3704/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003705 * "settabvar()" function
3706 */
3707 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003708f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003709{
3710 tabpage_T *save_curtab;
3711 tabpage_T *tp;
3712 char_u *varname, *tabvarname;
3713 typval_T *varp;
3714
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003715 if (check_secure())
3716 return;
3717
3718 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3719 varname = tv_get_string_chk(&argvars[1]);
3720 varp = &argvars[2];
3721
3722 if (varname != NULL && varp != NULL && tp != NULL)
3723 {
3724 save_curtab = curtab;
3725 goto_tabpage_tp(tp, FALSE, FALSE);
3726
3727 tabvarname = alloc(STRLEN(varname) + 3);
3728 if (tabvarname != NULL)
3729 {
3730 STRCPY(tabvarname, "t:");
3731 STRCPY(tabvarname + 2, varname);
3732 set_var(tabvarname, varp, TRUE);
3733 vim_free(tabvarname);
3734 }
3735
3736 // Restore current tabpage
3737 if (valid_tabpage(save_curtab))
3738 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3739 }
3740}
3741
3742/*
3743 * "settabwinvar()" function
3744 */
3745 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003746f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003747{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003748 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003749}
3750
3751/*
3752 * "setwinvar()" function
3753 */
3754 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003755f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003756{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003757 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003758}
3759
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003760/*
3761 * "setbufvar()" function
3762 */
3763 void
3764f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3765{
3766 buf_T *buf;
3767 char_u *varname, *bufvarname;
3768 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003769
3770 if (check_secure())
3771 return;
3772 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3773 varname = tv_get_string_chk(&argvars[1]);
3774 buf = tv_get_buf(&argvars[0], FALSE);
3775 varp = &argvars[2];
3776
3777 if (buf != NULL && varname != NULL && varp != NULL)
3778 {
3779 if (*varname == '&')
3780 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003781 aco_save_T aco;
3782
3783 // set curbuf to be our buf, temporarily
3784 aucmd_prepbuf(&aco, buf);
3785
Bram Moolenaar191929b2020-08-19 21:20:49 +02003786 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003787
3788 // reset notion of buffer
3789 aucmd_restbuf(&aco);
3790 }
3791 else
3792 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003793 bufvarname = alloc(STRLEN(varname) + 3);
3794 if (bufvarname != NULL)
3795 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003796 buf_T *save_curbuf = curbuf;
3797
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003798 curbuf = buf;
3799 STRCPY(bufvarname, "b:");
3800 STRCPY(bufvarname + 2, varname);
3801 set_var(bufvarname, varp, TRUE);
3802 vim_free(bufvarname);
3803 curbuf = save_curbuf;
3804 }
3805 }
3806 }
3807}
3808
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003809/*
3810 * Get a callback from "arg". It can be a Funcref or a function name.
3811 * When "arg" is zero return an empty string.
3812 * "cb_name" is not allocated.
3813 * "cb_name" is set to NULL for an invalid argument.
3814 */
3815 callback_T
3816get_callback(typval_T *arg)
3817{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003818 callback_T res;
3819 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003820
3821 res.cb_free_name = FALSE;
3822 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3823 {
3824 res.cb_partial = arg->vval.v_partial;
3825 ++res.cb_partial->pt_refcount;
3826 res.cb_name = partial_name(res.cb_partial);
3827 }
3828 else
3829 {
3830 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003831 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3832 && isdigit(*arg->vval.v_string))
3833 r = FAIL;
3834 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003835 {
3836 // Note that we don't make a copy of the string.
3837 res.cb_name = arg->vval.v_string;
3838 func_ref(res.cb_name);
3839 }
3840 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003841 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003842 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003843 r = FAIL;
3844
3845 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003846 {
3847 emsg(_("E921: Invalid callback argument"));
3848 res.cb_name = NULL;
3849 }
3850 }
3851 return res;
3852}
3853
3854/*
3855 * Copy a callback into a typval_T.
3856 */
3857 void
3858put_callback(callback_T *cb, typval_T *tv)
3859{
3860 if (cb->cb_partial != NULL)
3861 {
3862 tv->v_type = VAR_PARTIAL;
3863 tv->vval.v_partial = cb->cb_partial;
3864 ++tv->vval.v_partial->pt_refcount;
3865 }
3866 else
3867 {
3868 tv->v_type = VAR_FUNC;
3869 tv->vval.v_string = vim_strsave(cb->cb_name);
3870 func_ref(cb->cb_name);
3871 }
3872}
3873
3874/*
3875 * Make a copy of "src" into "dest", allocating the function name if needed,
3876 * without incrementing the refcount.
3877 */
3878 void
3879set_callback(callback_T *dest, callback_T *src)
3880{
3881 if (src->cb_partial == NULL)
3882 {
3883 // just a function name, make a copy
3884 dest->cb_name = vim_strsave(src->cb_name);
3885 dest->cb_free_name = TRUE;
3886 }
3887 else
3888 {
3889 // cb_name is a pointer into cb_partial
3890 dest->cb_name = src->cb_name;
3891 dest->cb_free_name = FALSE;
3892 }
3893 dest->cb_partial = src->cb_partial;
3894}
3895
3896/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02003897 * Copy callback from "src" to "dest", incrementing the refcounts.
3898 */
3899 void
3900copy_callback(callback_T *dest, callback_T *src)
3901{
3902 dest->cb_partial = src->cb_partial;
3903 if (dest->cb_partial != NULL)
3904 {
3905 dest->cb_name = src->cb_name;
3906 dest->cb_free_name = FALSE;
3907 ++dest->cb_partial->pt_refcount;
3908 }
3909 else
3910 {
3911 dest->cb_name = vim_strsave(src->cb_name);
3912 dest->cb_free_name = TRUE;
3913 func_ref(src->cb_name);
3914 }
3915}
3916
3917/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003918 * Unref/free "callback" returned by get_callback() or set_callback().
3919 */
3920 void
3921free_callback(callback_T *callback)
3922{
3923 if (callback->cb_partial != NULL)
3924 {
3925 partial_unref(callback->cb_partial);
3926 callback->cb_partial = NULL;
3927 }
3928 else if (callback->cb_name != NULL)
3929 func_unref(callback->cb_name);
3930 if (callback->cb_free_name)
3931 {
3932 vim_free(callback->cb_name);
3933 callback->cb_free_name = FALSE;
3934 }
3935 callback->cb_name = NULL;
3936}
3937
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003938#endif // FEAT_EVAL