blob: e11fdac41fb3af1d4926112ca5f764da2f3232df [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 Moolenaar0522ba02019-08-27 22:48:30 +0200176static void item_lock(typval_T *tv, int deep, int lock);
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
804 if (vim9script && (!VIM_ISWHITE(*argend) || !VIM_ISWHITE(*expr)))
805 {
806 vim_strncpy(op, expr - len, len);
807 semsg(_(e_white_both), op);
808 i = FAIL;
809 }
810 expr = skipwhite(expr);
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 Moolenaarb171fb12020-06-24 20:34:03 +0200821 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200822 if (eap->skip)
823 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200824 clear_evalarg(&evalarg, eap);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200825 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200826 if (eap->skip)
827 {
828 if (i != FAIL)
829 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200830 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200831 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200832 {
833 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200834 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200835 clear_tv(&rettv);
836 }
837 }
838}
839
840/*
841 * Assign the typevalue "tv" to the variable or variables at "arg_start".
842 * Handles both "var" with any type and "[var, var; var]" with a list type.
843 * When "op" is not NULL it points to a string with characters that
844 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
845 * or concatenate.
846 * Returns OK or FAIL;
847 */
848 int
849ex_let_vars(
850 char_u *arg_start,
851 typval_T *tv,
852 int copy, // copy values from "tv", don't move
853 int semicolon, // from skip_var_list()
854 int var_count, // from skip_var_list()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100855 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200856 char_u *op)
857{
858 char_u *arg = arg_start;
859 list_T *l;
860 int i;
861 listitem_T *item;
862 typval_T ltv;
863
864 if (*arg != '[')
865 {
866 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100867 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200868 return FAIL;
869 return OK;
870 }
871
872 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
873 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
874 {
875 emsg(_(e_listreq));
876 return FAIL;
877 }
878
879 i = list_len(l);
880 if (semicolon == 0 && var_count < i)
881 {
882 emsg(_("E687: Less targets than List items"));
883 return FAIL;
884 }
885 if (var_count - semicolon > i)
886 {
887 emsg(_("E688: More targets than List items"));
888 return FAIL;
889 }
890
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200891 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200892 item = l->lv_first;
893 while (*arg != ']')
894 {
895 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200897 item = item->li_next;
898 if (arg == NULL)
899 return FAIL;
900
901 arg = skipwhite(arg);
902 if (*arg == ';')
903 {
904 // Put the rest of the list (may be empty) in the var after ';'.
905 // Create a new list for this.
906 l = list_alloc();
907 if (l == NULL)
908 return FAIL;
909 while (item != NULL)
910 {
911 list_append_tv(l, &item->li_tv);
912 item = item->li_next;
913 }
914
915 ltv.v_type = VAR_LIST;
916 ltv.v_lock = 0;
917 ltv.vval.v_list = l;
918 l->lv_refcount = 1;
919
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200921 (char_u *)"]", op);
922 clear_tv(&ltv);
923 if (arg == NULL)
924 return FAIL;
925 break;
926 }
927 else if (*arg != ',' && *arg != ']')
928 {
929 internal_error("ex_let_vars()");
930 return FAIL;
931 }
932 }
933
934 return OK;
935}
936
937/*
938 * Skip over assignable variable "var" or list of variables "[var, var]".
939 * Used for ":let varvar = expr" and ":for varvar in expr".
940 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200941 * for "[var, var; var]" set "semicolon" to 1.
942 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200943 * Return NULL for an error.
944 */
945 char_u *
946skip_var_list(
947 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100948 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200949 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200950 int *semicolon,
951 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200952{
953 char_u *p, *s;
954
955 if (*arg == '[')
956 {
957 // "[var, var]": find the matching ']'.
958 p = arg;
959 for (;;)
960 {
961 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200962 s = skip_var_one(p, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200963 if (s == p)
964 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200965 if (!silent)
966 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200967 return NULL;
968 }
969 ++*var_count;
970
971 p = skipwhite(s);
972 if (*p == ']')
973 break;
974 else if (*p == ';')
975 {
976 if (*semicolon == 1)
977 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200978 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200979 return NULL;
980 }
981 *semicolon = 1;
982 }
983 else if (*p != ',')
984 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200985 if (!silent)
986 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200987 return NULL;
988 }
989 }
990 return p + 1;
991 }
992 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200994}
995
996/*
997 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
998 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100999 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001000 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001001 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001003{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001004 char_u *end;
1005
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001006 if (*arg == '@' && arg[1] != NUL)
1007 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001008 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001009 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001010 if (include_type && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001012 // "a: type" is declaring variable "a" with a type, not "a:".
1013 if (end == arg + 2 && end[-1] == ':')
1014 --end;
1015 if (*end == ':')
1016 end = skip_type(skipwhite(end + 1));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001017 }
1018 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001019}
1020
1021/*
1022 * List variables for hashtab "ht" with prefix "prefix".
1023 * If "empty" is TRUE also list NULL strings as empty strings.
1024 */
1025 void
1026list_hashtable_vars(
1027 hashtab_T *ht,
1028 char *prefix,
1029 int empty,
1030 int *first)
1031{
1032 hashitem_T *hi;
1033 dictitem_T *di;
1034 int todo;
1035 char_u buf[IOSIZE];
1036
1037 todo = (int)ht->ht_used;
1038 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1039 {
1040 if (!HASHITEM_EMPTY(hi))
1041 {
1042 --todo;
1043 di = HI2DI(hi);
1044
1045 // apply :filter /pat/ to variable name
1046 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1047 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1048 if (message_filtered(buf))
1049 continue;
1050
1051 if (empty || di->di_tv.v_type != VAR_STRING
1052 || di->di_tv.vval.v_string != NULL)
1053 list_one_var(di, prefix, first);
1054 }
1055 }
1056}
1057
1058/*
1059 * List global variables.
1060 */
1061 static void
1062list_glob_vars(int *first)
1063{
1064 list_hashtable_vars(&globvarht, "", TRUE, first);
1065}
1066
1067/*
1068 * List buffer variables.
1069 */
1070 static void
1071list_buf_vars(int *first)
1072{
1073 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1074}
1075
1076/*
1077 * List window variables.
1078 */
1079 static void
1080list_win_vars(int *first)
1081{
1082 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1083}
1084
1085/*
1086 * List tab page variables.
1087 */
1088 static void
1089list_tab_vars(int *first)
1090{
1091 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1092}
1093
1094/*
1095 * List variables in "arg".
1096 */
1097 static char_u *
1098list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1099{
1100 int error = FALSE;
1101 int len;
1102 char_u *name;
1103 char_u *name_start;
1104 char_u *arg_subsc;
1105 char_u *tofree;
1106 typval_T tv;
1107
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001108 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001109 {
1110 if (error || eap->skip)
1111 {
1112 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1113 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1114 {
1115 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001116 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001117 break;
1118 }
1119 }
1120 else
1121 {
1122 // get_name_len() takes care of expanding curly braces
1123 name_start = name = arg;
1124 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1125 if (len <= 0)
1126 {
1127 // This is mainly to keep test 49 working: when expanding
1128 // curly braces fails overrule the exception error message.
1129 if (len < 0 && !aborting())
1130 {
1131 emsg_severe = TRUE;
1132 semsg(_(e_invarg2), arg);
1133 break;
1134 }
1135 error = TRUE;
1136 }
1137 else
1138 {
1139 if (tofree != NULL)
1140 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001141 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001142 error = TRUE;
1143 else
1144 {
1145 // handle d.key, l[idx], f(expr)
1146 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001147 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001148 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001149 error = TRUE;
1150 else
1151 {
1152 if (arg == arg_subsc && len == 2 && name[1] == ':')
1153 {
1154 switch (*name)
1155 {
1156 case 'g': list_glob_vars(first); break;
1157 case 'b': list_buf_vars(first); break;
1158 case 'w': list_win_vars(first); break;
1159 case 't': list_tab_vars(first); break;
1160 case 'v': list_vim_vars(first); break;
1161 case 's': list_script_vars(first); break;
1162 case 'l': list_func_vars(first); break;
1163 default:
1164 semsg(_("E738: Can't list variables for %s"), name);
1165 }
1166 }
1167 else
1168 {
1169 char_u numbuf[NUMBUFLEN];
1170 char_u *tf;
1171 int c;
1172 char_u *s;
1173
1174 s = echo_string(&tv, &tf, numbuf, 0);
1175 c = *arg;
1176 *arg = NUL;
1177 list_one_var_a("",
1178 arg == arg_subsc ? name : name_start,
1179 tv.v_type,
1180 s == NULL ? (char_u *)"" : s,
1181 first);
1182 *arg = c;
1183 vim_free(tf);
1184 }
1185 clear_tv(&tv);
1186 }
1187 }
1188 }
1189
1190 vim_free(tofree);
1191 }
1192
1193 arg = skipwhite(arg);
1194 }
1195
1196 return arg;
1197}
1198
1199/*
1200 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1201 * Returns a pointer to the char just after the var name.
1202 * Returns NULL if there is an error.
1203 */
1204 static char_u *
1205ex_let_one(
1206 char_u *arg, // points to variable name
1207 typval_T *tv, // value to assign to variable
1208 int copy, // copy value from "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001209 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001210 char_u *endchars, // valid chars after variable name or NULL
1211 char_u *op) // "+", "-", "." or NULL
1212{
1213 int c1;
1214 char_u *name;
1215 char_u *p;
1216 char_u *arg_end = NULL;
1217 int len;
1218 int opt_flags;
1219 char_u *tofree = NULL;
1220
1221 // ":let $VAR = expr": Set environment variable.
1222 if (*arg == '$')
1223 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001224 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001225 {
1226 emsg(_("E996: Cannot lock an environment variable"));
1227 return NULL;
1228 }
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001229 if (in_vim9script() && (flags & LET_NO_COMMAND) == 0)
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001230 {
1231 vim9_declare_error(arg);
1232 return NULL;
1233 }
1234
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001235 // Find the end of the name.
1236 ++arg;
1237 name = arg;
1238 len = get_env_len(&arg);
1239 if (len == 0)
1240 semsg(_(e_invarg2), name - 1);
1241 else
1242 {
1243 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1244 semsg(_(e_letwrong), op);
1245 else if (endchars != NULL
1246 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1247 emsg(_(e_letunexp));
1248 else if (!check_secure())
1249 {
1250 c1 = name[len];
1251 name[len] = NUL;
1252 p = tv_get_string_chk(tv);
1253 if (p != NULL && op != NULL && *op == '.')
1254 {
1255 int mustfree = FALSE;
1256 char_u *s = vim_getenv(name, &mustfree);
1257
1258 if (s != NULL)
1259 {
1260 p = tofree = concat_str(s, p);
1261 if (mustfree)
1262 vim_free(s);
1263 }
1264 }
1265 if (p != NULL)
1266 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001267 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001268 arg_end = arg;
1269 }
1270 name[len] = c1;
1271 vim_free(tofree);
1272 }
1273 }
1274 }
1275
1276 // ":let &option = expr": Set option value.
1277 // ":let &l:option = expr": Set local option value.
1278 // ":let &g:option = expr": Set global option value.
1279 else if (*arg == '&')
1280 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001282 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001284 return NULL;
1285 }
1286 // Find the end of the name.
1287 p = find_option_end(&arg, &opt_flags);
1288 if (p == NULL || (endchars != NULL
1289 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1290 emsg(_(e_letunexp));
1291 else
1292 {
1293 long n;
1294 int opt_type;
1295 long numval;
1296 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001297 char_u *s = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001298
1299 c1 = *p;
1300 *p = NUL;
1301
1302 n = (long)tv_get_number(tv);
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001303 // avoid setting a string option to the text "v:false" or similar.
1304 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL)
1305 s = tv_get_string_chk(tv); // != NULL if number or string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001306 if (s != NULL && op != NULL && *op != '=')
1307 {
1308 opt_type = get_option_value(arg, &numval,
1309 &stringval, opt_flags);
1310 if ((opt_type == 1 && *op == '.')
1311 || (opt_type == 0 && *op != '.'))
1312 {
1313 semsg(_(e_letwrong), op);
1314 s = NULL; // don't set the value
1315 }
1316 else
1317 {
1318 if (opt_type == 1) // number
1319 {
1320 switch (*op)
1321 {
1322 case '+': n = numval + n; break;
1323 case '-': n = numval - n; break;
1324 case '*': n = numval * n; break;
1325 case '/': n = (long)num_divide(numval, n); break;
1326 case '%': n = (long)num_modulus(numval, n); break;
1327 }
1328 }
1329 else if (opt_type == 0 && stringval != NULL) // string
1330 {
1331 s = concat_str(stringval, s);
1332 vim_free(stringval);
1333 stringval = s;
1334 }
1335 }
1336 }
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001337 if (s != NULL || tv->v_type == VAR_BOOL
1338 || tv->v_type == VAR_SPECIAL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001339 {
1340 set_option_value(arg, n, s, opt_flags);
1341 arg_end = p;
1342 }
1343 *p = c1;
1344 vim_free(stringval);
1345 }
1346 }
1347
1348 // ":let @r = expr": Set register contents.
1349 else if (*arg == '@')
1350 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001351 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001352 {
1353 emsg(_("E996: Cannot lock a register"));
1354 return NULL;
1355 }
1356 ++arg;
1357 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1358 semsg(_(e_letwrong), op);
1359 else if (endchars != NULL
1360 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1361 emsg(_(e_letunexp));
1362 else
1363 {
1364 char_u *ptofree = NULL;
1365 char_u *s;
1366
1367 p = tv_get_string_chk(tv);
1368 if (p != NULL && op != NULL && *op == '.')
1369 {
1370 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1371 if (s != NULL)
1372 {
1373 p = ptofree = concat_str(s, p);
1374 vim_free(s);
1375 }
1376 }
1377 if (p != NULL)
1378 {
1379 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1380 arg_end = arg + 1;
1381 }
1382 vim_free(ptofree);
1383 }
1384 }
1385
1386 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001388 // ":let {expr} = expr": Idem, name made with curly braces
1389 else if (eval_isnamec1(*arg) || *arg == '{')
1390 {
1391 lval_T lv;
1392
1393 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001394 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001395 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001396 if (endchars != NULL && vim_strchr(endchars,
1397 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001398 emsg(_(e_letunexp));
1399 else
1400 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001401 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001402 arg_end = p;
1403 }
1404 }
1405 clear_lval(&lv);
1406 }
1407
1408 else
1409 semsg(_(e_invarg2), arg);
1410
1411 return arg_end;
1412}
1413
1414/*
1415 * ":unlet[!] var1 ... " command.
1416 */
1417 void
1418ex_unlet(exarg_T *eap)
1419{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001420 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001421}
1422
1423/*
1424 * ":lockvar" and ":unlockvar" commands
1425 */
1426 void
1427ex_lockvar(exarg_T *eap)
1428{
1429 char_u *arg = eap->arg;
1430 int deep = 2;
1431
1432 if (eap->forceit)
1433 deep = -1;
1434 else if (vim_isdigit(*arg))
1435 {
1436 deep = getdigits(&arg);
1437 arg = skipwhite(arg);
1438 }
1439
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001440 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001441}
1442
1443/*
1444 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001445 * Also used for Vim9 script. "callback" is invoked as:
1446 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001447 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001448 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001449ex_unletlock(
1450 exarg_T *eap,
1451 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001452 int deep,
1453 int glv_flags,
1454 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1455 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001456{
1457 char_u *arg = argstart;
1458 char_u *name_end;
1459 int error = FALSE;
1460 lval_T lv;
1461
1462 do
1463 {
1464 if (*arg == '$')
1465 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001466 lv.ll_name = arg;
1467 lv.ll_tv = NULL;
1468 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001469 if (get_env_len(&arg) == 0)
1470 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001471 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001472 return;
1473 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001474 if (!error && !eap->skip
1475 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1476 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001477 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001478 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001479 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001480 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001481 // Parse the name and find the end.
1482 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1483 glv_flags, FNE_CHECK_START);
1484 if (lv.ll_name == NULL)
1485 error = TRUE; // error but continue parsing
1486 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1487 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001488 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001489 if (name_end != NULL)
1490 {
1491 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001492 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001493 }
1494 if (!(eap->skip || error))
1495 clear_lval(&lv);
1496 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001497 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001498
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001499 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001500 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001501 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001502
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001503 if (!eap->skip)
1504 clear_lval(&lv);
1505 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001506
1507 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001508 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001509
1510 eap->nextcmd = check_nextcmd(arg);
1511}
1512
1513 static int
1514do_unlet_var(
1515 lval_T *lp,
1516 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001517 exarg_T *eap,
1518 int deep UNUSED,
1519 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001520{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001521 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001522 int ret = OK;
1523 int cc;
1524
1525 if (lp->ll_tv == NULL)
1526 {
1527 cc = *name_end;
1528 *name_end = NUL;
1529
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001530 // Environment variable, normal name or expanded name.
1531 if (*lp->ll_name == '$')
1532 vim_unsetenv(lp->ll_name + 1);
1533 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001534 ret = FAIL;
1535 *name_end = cc;
1536 }
1537 else if ((lp->ll_list != NULL
1538 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
1539 || (lp->ll_dict != NULL
1540 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
1541 return FAIL;
1542 else if (lp->ll_range)
1543 {
1544 listitem_T *li;
1545 listitem_T *ll_li = lp->ll_li;
1546 int ll_n1 = lp->ll_n1;
1547
1548 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1549 {
1550 li = ll_li->li_next;
1551 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
1552 return FAIL;
1553 ll_li = li;
1554 ++ll_n1;
1555 }
1556
1557 // Delete a range of List items.
1558 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1559 {
1560 li = lp->ll_li->li_next;
1561 listitem_remove(lp->ll_list, lp->ll_li);
1562 lp->ll_li = li;
1563 ++lp->ll_n1;
1564 }
1565 }
1566 else
1567 {
1568 if (lp->ll_list != NULL)
1569 // unlet a List item.
1570 listitem_remove(lp->ll_list, lp->ll_li);
1571 else
1572 // unlet a Dictionary item.
1573 dictitem_remove(lp->ll_dict, lp->ll_di);
1574 }
1575
1576 return ret;
1577}
1578
1579/*
1580 * "unlet" a variable. Return OK if it existed, FAIL if not.
1581 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1582 */
1583 int
1584do_unlet(char_u *name, int forceit)
1585{
1586 hashtab_T *ht;
1587 hashitem_T *hi;
1588 char_u *varname;
1589 dict_T *d;
1590 dictitem_T *di;
1591
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001592 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001593 return FAIL;
1594
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001595 ht = find_var_ht(name, &varname);
1596 if (ht != NULL && *varname != NUL)
1597 {
1598 d = get_current_funccal_dict(ht);
1599 if (d == NULL)
1600 {
1601 if (ht == &globvarht)
1602 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001603 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001604 d = &vimvardict;
1605 else
1606 {
1607 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1608 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1609 }
1610 if (d == NULL)
1611 {
1612 internal_error("do_unlet()");
1613 return FAIL;
1614 }
1615 }
1616 hi = hash_find(ht, varname);
1617 if (HASHITEM_EMPTY(hi))
1618 hi = find_hi_in_scoped_ht(name, &ht);
1619 if (hi != NULL && !HASHITEM_EMPTY(hi))
1620 {
1621 di = HI2DI(hi);
1622 if (var_check_fixed(di->di_flags, name, FALSE)
1623 || var_check_ro(di->di_flags, name, FALSE)
1624 || var_check_lock(d->dv_lock, name, FALSE))
1625 return FAIL;
1626
1627 delete_var(ht, hi);
1628 return OK;
1629 }
1630 }
1631 if (forceit)
1632 return OK;
1633 semsg(_("E108: No such variable: \"%s\""), name);
1634 return FAIL;
1635}
1636
1637/*
1638 * Lock or unlock variable indicated by "lp".
1639 * "deep" is the levels to go (-1 for unlimited);
1640 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1641 */
1642 static int
1643do_lock_var(
1644 lval_T *lp,
1645 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001646 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001647 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001648 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001649{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001650 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001651 int ret = OK;
1652 int cc;
1653 dictitem_T *di;
1654
1655 if (deep == 0) // nothing to do
1656 return OK;
1657
1658 if (lp->ll_tv == NULL)
1659 {
1660 cc = *name_end;
1661 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001662 if (*lp->ll_name == '$')
1663 {
1664 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001665 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001666 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001667 else
1668 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001669 // Normal name or expanded name.
1670 di = find_var(lp->ll_name, NULL, TRUE);
1671 if (di == NULL)
1672 ret = FAIL;
1673 else if ((di->di_flags & DI_FLAGS_FIX)
1674 && di->di_tv.v_type != VAR_DICT
1675 && di->di_tv.v_type != VAR_LIST)
1676 {
1677 // For historic reasons this error is not given for a list or
1678 // dict. E.g., the b: dict could be locked/unlocked.
1679 semsg(_(e_lock_unlock), lp->ll_name);
1680 ret = FAIL;
1681 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001682 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001683 {
1684 if (lock)
1685 di->di_flags |= DI_FLAGS_LOCK;
1686 else
1687 di->di_flags &= ~DI_FLAGS_LOCK;
1688 item_lock(&di->di_tv, deep, lock);
1689 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001690 }
1691 *name_end = cc;
1692 }
1693 else if (lp->ll_range)
1694 {
1695 listitem_T *li = lp->ll_li;
1696
1697 // (un)lock a range of List items.
1698 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1699 {
1700 item_lock(&li->li_tv, deep, lock);
1701 li = li->li_next;
1702 ++lp->ll_n1;
1703 }
1704 }
1705 else if (lp->ll_list != NULL)
1706 // (un)lock a List item.
1707 item_lock(&lp->ll_li->li_tv, deep, lock);
1708 else
1709 // (un)lock a Dictionary item.
1710 item_lock(&lp->ll_di->di_tv, deep, lock);
1711
1712 return ret;
1713}
1714
1715/*
1716 * Lock or unlock an item. "deep" is nr of levels to go.
1717 */
1718 static void
1719item_lock(typval_T *tv, int deep, int lock)
1720{
1721 static int recurse = 0;
1722 list_T *l;
1723 listitem_T *li;
1724 dict_T *d;
1725 blob_T *b;
1726 hashitem_T *hi;
1727 int todo;
1728
1729 if (recurse >= DICT_MAXNEST)
1730 {
1731 emsg(_("E743: variable nested too deep for (un)lock"));
1732 return;
1733 }
1734 if (deep == 0)
1735 return;
1736 ++recurse;
1737
1738 // lock/unlock the item itself
1739 if (lock)
1740 tv->v_lock |= VAR_LOCKED;
1741 else
1742 tv->v_lock &= ~VAR_LOCKED;
1743
1744 switch (tv->v_type)
1745 {
1746 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001747 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001749 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001751 case VAR_STRING:
1752 case VAR_FUNC:
1753 case VAR_PARTIAL:
1754 case VAR_FLOAT:
1755 case VAR_SPECIAL:
1756 case VAR_JOB:
1757 case VAR_CHANNEL:
1758 break;
1759
1760 case VAR_BLOB:
1761 if ((b = tv->vval.v_blob) != NULL)
1762 {
1763 if (lock)
1764 b->bv_lock |= VAR_LOCKED;
1765 else
1766 b->bv_lock &= ~VAR_LOCKED;
1767 }
1768 break;
1769 case VAR_LIST:
1770 if ((l = tv->vval.v_list) != NULL)
1771 {
1772 if (lock)
1773 l->lv_lock |= VAR_LOCKED;
1774 else
1775 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001776 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001777 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001778 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001779 item_lock(&li->li_tv, deep - 1, lock);
1780 }
1781 break;
1782 case VAR_DICT:
1783 if ((d = tv->vval.v_dict) != NULL)
1784 {
1785 if (lock)
1786 d->dv_lock |= VAR_LOCKED;
1787 else
1788 d->dv_lock &= ~VAR_LOCKED;
1789 if (deep < 0 || deep > 1)
1790 {
1791 // recursive: lock/unlock the items the List contains
1792 todo = (int)d->dv_hashtab.ht_used;
1793 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1794 {
1795 if (!HASHITEM_EMPTY(hi))
1796 {
1797 --todo;
1798 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
1799 }
1800 }
1801 }
1802 }
1803 }
1804 --recurse;
1805}
1806
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001807#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1808/*
1809 * Delete all "menutrans_" variables.
1810 */
1811 void
1812del_menutrans_vars(void)
1813{
1814 hashitem_T *hi;
1815 int todo;
1816
1817 hash_lock(&globvarht);
1818 todo = (int)globvarht.ht_used;
1819 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1820 {
1821 if (!HASHITEM_EMPTY(hi))
1822 {
1823 --todo;
1824 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1825 delete_var(&globvarht, hi);
1826 }
1827 }
1828 hash_unlock(&globvarht);
1829}
1830#endif
1831
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001832/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001833 * Local string buffer for the next two functions to store a variable name
1834 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1835 * get_user_var_name().
1836 */
1837
1838static char_u *varnamebuf = NULL;
1839static int varnamebuflen = 0;
1840
1841/*
1842 * Function to concatenate a prefix and a variable name.
1843 */
1844 static char_u *
1845cat_prefix_varname(int prefix, char_u *name)
1846{
1847 int len;
1848
1849 len = (int)STRLEN(name) + 3;
1850 if (len > varnamebuflen)
1851 {
1852 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001853 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001854 varnamebuf = alloc(len);
1855 if (varnamebuf == NULL)
1856 {
1857 varnamebuflen = 0;
1858 return NULL;
1859 }
1860 varnamebuflen = len;
1861 }
1862 *varnamebuf = prefix;
1863 varnamebuf[1] = ':';
1864 STRCPY(varnamebuf + 2, name);
1865 return varnamebuf;
1866}
1867
1868/*
1869 * Function given to ExpandGeneric() to obtain the list of user defined
1870 * (global/buffer/window/built-in) variable names.
1871 */
1872 char_u *
1873get_user_var_name(expand_T *xp, int idx)
1874{
1875 static long_u gdone;
1876 static long_u bdone;
1877 static long_u wdone;
1878 static long_u tdone;
1879 static int vidx;
1880 static hashitem_T *hi;
1881 hashtab_T *ht;
1882
1883 if (idx == 0)
1884 {
1885 gdone = bdone = wdone = vidx = 0;
1886 tdone = 0;
1887 }
1888
1889 // Global variables
1890 if (gdone < globvarht.ht_used)
1891 {
1892 if (gdone++ == 0)
1893 hi = globvarht.ht_array;
1894 else
1895 ++hi;
1896 while (HASHITEM_EMPTY(hi))
1897 ++hi;
1898 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1899 return cat_prefix_varname('g', hi->hi_key);
1900 return hi->hi_key;
1901 }
1902
1903 // b: variables
1904 ht = &curbuf->b_vars->dv_hashtab;
1905 if (bdone < ht->ht_used)
1906 {
1907 if (bdone++ == 0)
1908 hi = ht->ht_array;
1909 else
1910 ++hi;
1911 while (HASHITEM_EMPTY(hi))
1912 ++hi;
1913 return cat_prefix_varname('b', hi->hi_key);
1914 }
1915
1916 // w: variables
1917 ht = &curwin->w_vars->dv_hashtab;
1918 if (wdone < ht->ht_used)
1919 {
1920 if (wdone++ == 0)
1921 hi = ht->ht_array;
1922 else
1923 ++hi;
1924 while (HASHITEM_EMPTY(hi))
1925 ++hi;
1926 return cat_prefix_varname('w', hi->hi_key);
1927 }
1928
1929 // t: variables
1930 ht = &curtab->tp_vars->dv_hashtab;
1931 if (tdone < ht->ht_used)
1932 {
1933 if (tdone++ == 0)
1934 hi = ht->ht_array;
1935 else
1936 ++hi;
1937 while (HASHITEM_EMPTY(hi))
1938 ++hi;
1939 return cat_prefix_varname('t', hi->hi_key);
1940 }
1941
1942 // v: variables
1943 if (vidx < VV_LEN)
1944 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
1945
1946 VIM_CLEAR(varnamebuf);
1947 varnamebuflen = 0;
1948 return NULL;
1949}
1950
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001951 char *
1952get_var_special_name(int nr)
1953{
1954 switch (nr)
1955 {
1956 case VVAL_FALSE: return "v:false";
1957 case VVAL_TRUE: return "v:true";
1958 case VVAL_NONE: return "v:none";
1959 case VVAL_NULL: return "v:null";
1960 }
1961 internal_error("get_var_special_name()");
1962 return "42";
1963}
1964
1965/*
1966 * Returns the global variable dictionary
1967 */
1968 dict_T *
1969get_globvar_dict(void)
1970{
1971 return &globvardict;
1972}
1973
1974/*
1975 * Returns the global variable hash table
1976 */
1977 hashtab_T *
1978get_globvar_ht(void)
1979{
1980 return &globvarht;
1981}
1982
1983/*
1984 * Returns the v: variable dictionary
1985 */
1986 dict_T *
1987get_vimvar_dict(void)
1988{
1989 return &vimvardict;
1990}
1991
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001992/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001994 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001995 */
1996 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001997find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001999 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2000 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002001
2002 if (di == NULL)
2003 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002004 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2006 return (int)(vv - vimvars);
2007}
2008
2009
2010/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002011 * Set type of v: variable to "type".
2012 */
2013 void
2014set_vim_var_type(int idx, vartype_T type)
2015{
2016 vimvars[idx].vv_type = type;
2017}
2018
2019/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002020 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002021 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002022 */
2023 void
2024set_vim_var_nr(int idx, varnumber_T val)
2025{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002026 vimvars[idx].vv_nr = val;
2027}
2028
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029 char *
2030get_vim_var_name(int idx)
2031{
2032 return vimvars[idx].vv_name;
2033}
2034
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002035/*
2036 * Get typval_T v: variable value.
2037 */
2038 typval_T *
2039get_vim_var_tv(int idx)
2040{
2041 return &vimvars[idx].vv_tv;
2042}
2043
2044/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002045 * Set v: variable to "tv". Only accepts the same type.
2046 * Takes over the value of "tv".
2047 */
2048 int
2049set_vim_var_tv(int idx, typval_T *tv)
2050{
2051 if (vimvars[idx].vv_type != tv->v_type)
2052 {
2053 emsg(_("E1063: type mismatch for v: variable"));
2054 clear_tv(tv);
2055 return FAIL;
2056 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002057 // VV_RO is also checked when compiling, but let's check here as well.
2058 if (vimvars[idx].vv_flags & VV_RO)
2059 {
2060 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2061 return FAIL;
2062 }
2063 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2064 {
2065 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2066 return FAIL;
2067 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002068 clear_tv(&vimvars[idx].vv_di.di_tv);
2069 vimvars[idx].vv_di.di_tv = *tv;
2070 return OK;
2071}
2072
2073/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002074 * Get number v: variable value.
2075 */
2076 varnumber_T
2077get_vim_var_nr(int idx)
2078{
2079 return vimvars[idx].vv_nr;
2080}
2081
2082/*
2083 * Get string v: variable value. Uses a static buffer, can only be used once.
2084 * If the String variable has never been set, return an empty string.
2085 * Never returns NULL;
2086 */
2087 char_u *
2088get_vim_var_str(int idx)
2089{
2090 return tv_get_string(&vimvars[idx].vv_tv);
2091}
2092
2093/*
2094 * Get List v: variable value. Caller must take care of reference count when
2095 * needed.
2096 */
2097 list_T *
2098get_vim_var_list(int idx)
2099{
2100 return vimvars[idx].vv_list;
2101}
2102
2103/*
2104 * Get Dict v: variable value. Caller must take care of reference count when
2105 * needed.
2106 */
2107 dict_T *
2108get_vim_var_dict(int idx)
2109{
2110 return vimvars[idx].vv_dict;
2111}
2112
2113/*
2114 * Set v:char to character "c".
2115 */
2116 void
2117set_vim_var_char(int c)
2118{
2119 char_u buf[MB_MAXBYTES + 1];
2120
2121 if (has_mbyte)
2122 buf[(*mb_char2bytes)(c, buf)] = NUL;
2123 else
2124 {
2125 buf[0] = c;
2126 buf[1] = NUL;
2127 }
2128 set_vim_var_string(VV_CHAR, buf, -1);
2129}
2130
2131/*
2132 * Set v:count to "count" and v:count1 to "count1".
2133 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2134 */
2135 void
2136set_vcount(
2137 long count,
2138 long count1,
2139 int set_prevcount)
2140{
2141 if (set_prevcount)
2142 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2143 vimvars[VV_COUNT].vv_nr = count;
2144 vimvars[VV_COUNT1].vv_nr = count1;
2145}
2146
2147/*
2148 * Save variables that might be changed as a side effect. Used when executing
2149 * a timer callback.
2150 */
2151 void
2152save_vimvars(vimvars_save_T *vvsave)
2153{
2154 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2155 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2156 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2157}
2158
2159/*
2160 * Restore variables saved by save_vimvars().
2161 */
2162 void
2163restore_vimvars(vimvars_save_T *vvsave)
2164{
2165 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2166 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2167 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2168}
2169
2170/*
2171 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2172 * value.
2173 */
2174 void
2175set_vim_var_string(
2176 int idx,
2177 char_u *val,
2178 int len) // length of "val" to use or -1 (whole string)
2179{
2180 clear_tv(&vimvars[idx].vv_di.di_tv);
2181 vimvars[idx].vv_type = VAR_STRING;
2182 if (val == NULL)
2183 vimvars[idx].vv_str = NULL;
2184 else if (len == -1)
2185 vimvars[idx].vv_str = vim_strsave(val);
2186 else
2187 vimvars[idx].vv_str = vim_strnsave(val, len);
2188}
2189
2190/*
2191 * Set List v: variable to "val".
2192 */
2193 void
2194set_vim_var_list(int idx, list_T *val)
2195{
2196 clear_tv(&vimvars[idx].vv_di.di_tv);
2197 vimvars[idx].vv_type = VAR_LIST;
2198 vimvars[idx].vv_list = val;
2199 if (val != NULL)
2200 ++val->lv_refcount;
2201}
2202
2203/*
2204 * Set Dictionary v: variable to "val".
2205 */
2206 void
2207set_vim_var_dict(int idx, dict_T *val)
2208{
2209 clear_tv(&vimvars[idx].vv_di.di_tv);
2210 vimvars[idx].vv_type = VAR_DICT;
2211 vimvars[idx].vv_dict = val;
2212 if (val != NULL)
2213 {
2214 ++val->dv_refcount;
2215 dict_set_items_ro(val);
2216 }
2217}
2218
2219/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002220 * Set the v:argv list.
2221 */
2222 void
2223set_argv_var(char **argv, int argc)
2224{
2225 list_T *l = list_alloc();
2226 int i;
2227
2228 if (l == NULL)
2229 getout(1);
2230 l->lv_lock = VAR_FIXED;
2231 for (i = 0; i < argc; ++i)
2232 {
2233 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2234 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002235 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002236 }
2237 set_vim_var_list(VV_ARGV, l);
2238}
2239
2240/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002241 * Reset v:register, taking the 'clipboard' setting into account.
2242 */
2243 void
2244reset_reg_var(void)
2245{
2246 int regname = 0;
2247
2248 // Adjust the register according to 'clipboard', so that when
2249 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2250#ifdef FEAT_CLIPBOARD
2251 adjust_clip_reg(&regname);
2252#endif
2253 set_reg_var(regname);
2254}
2255
2256/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002257 * Set v:register if needed.
2258 */
2259 void
2260set_reg_var(int c)
2261{
2262 char_u regname;
2263
2264 if (c == 0 || c == ' ')
2265 regname = '"';
2266 else
2267 regname = c;
2268 // Avoid free/alloc when the value is already right.
2269 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2270 set_vim_var_string(VV_REG, &regname, 1);
2271}
2272
2273/*
2274 * Get or set v:exception. If "oldval" == NULL, return the current value.
2275 * Otherwise, restore the value to "oldval" and return NULL.
2276 * Must always be called in pairs to save and restore v:exception! Does not
2277 * take care of memory allocations.
2278 */
2279 char_u *
2280v_exception(char_u *oldval)
2281{
2282 if (oldval == NULL)
2283 return vimvars[VV_EXCEPTION].vv_str;
2284
2285 vimvars[VV_EXCEPTION].vv_str = oldval;
2286 return NULL;
2287}
2288
2289/*
2290 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2291 * Otherwise, restore the value to "oldval" and return NULL.
2292 * Must always be called in pairs to save and restore v:throwpoint! Does not
2293 * take care of memory allocations.
2294 */
2295 char_u *
2296v_throwpoint(char_u *oldval)
2297{
2298 if (oldval == NULL)
2299 return vimvars[VV_THROWPOINT].vv_str;
2300
2301 vimvars[VV_THROWPOINT].vv_str = oldval;
2302 return NULL;
2303}
2304
2305/*
2306 * Set v:cmdarg.
2307 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2308 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2309 * Must always be called in pairs!
2310 */
2311 char_u *
2312set_cmdarg(exarg_T *eap, char_u *oldarg)
2313{
2314 char_u *oldval;
2315 char_u *newval;
2316 unsigned len;
2317
2318 oldval = vimvars[VV_CMDARG].vv_str;
2319 if (eap == NULL)
2320 {
2321 vim_free(oldval);
2322 vimvars[VV_CMDARG].vv_str = oldarg;
2323 return NULL;
2324 }
2325
2326 if (eap->force_bin == FORCE_BIN)
2327 len = 6;
2328 else if (eap->force_bin == FORCE_NOBIN)
2329 len = 8;
2330 else
2331 len = 0;
2332
2333 if (eap->read_edit)
2334 len += 7;
2335
2336 if (eap->force_ff != 0)
2337 len += 10; // " ++ff=unix"
2338 if (eap->force_enc != 0)
2339 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2340 if (eap->bad_char != 0)
2341 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2342
2343 newval = alloc(len + 1);
2344 if (newval == NULL)
2345 return NULL;
2346
2347 if (eap->force_bin == FORCE_BIN)
2348 sprintf((char *)newval, " ++bin");
2349 else if (eap->force_bin == FORCE_NOBIN)
2350 sprintf((char *)newval, " ++nobin");
2351 else
2352 *newval = NUL;
2353
2354 if (eap->read_edit)
2355 STRCAT(newval, " ++edit");
2356
2357 if (eap->force_ff != 0)
2358 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2359 eap->force_ff == 'u' ? "unix"
2360 : eap->force_ff == 'd' ? "dos"
2361 : "mac");
2362 if (eap->force_enc != 0)
2363 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2364 eap->cmd + eap->force_enc);
2365 if (eap->bad_char == BAD_KEEP)
2366 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2367 else if (eap->bad_char == BAD_DROP)
2368 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2369 else if (eap->bad_char != 0)
2370 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2371 vimvars[VV_CMDARG].vv_str = newval;
2372 return oldval;
2373}
2374
2375/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002376 * Get the value of internal variable "name".
2377 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2378 */
2379 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002380eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002381 char_u *name,
2382 int len, // length of "name"
2383 typval_T *rettv, // NULL when only checking existence
2384 dictitem_T **dip, // non-NULL when typval's dict item is needed
2385 int verbose, // may give error message
2386 int no_autoload) // do not use script autoloading
2387{
2388 int ret = OK;
2389 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002390 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002391 dictitem_T *v;
2392 int cc;
2393
2394 // truncate the name, so that we can use strcmp()
2395 cc = name[len];
2396 name[len] = NUL;
2397
2398 // Check for user-defined variables.
2399 v = find_var(name, NULL, no_autoload);
2400 if (v != NULL)
2401 {
2402 tv = &v->di_tv;
2403 if (dip != NULL)
2404 *dip = v;
2405 }
2406
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002407 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002409 imported_T *import;
2410 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2411
2412 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413
2414 // imported variable from another script
2415 if (import != NULL)
2416 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002417 if (import->imp_funcname != NULL)
2418 {
2419 foundFunc = TRUE;
2420 if (rettv != NULL)
2421 {
2422 rettv->v_type = VAR_FUNC;
2423 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2424 }
2425 }
2426 else
2427 {
2428 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
2429 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002430 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002431 tv = sv->sv_tv;
2432 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002433 }
2434 }
2435
Bram Moolenaarc620c052020-07-08 15:16:19 +02002436 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002437 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002438 if (tv == NULL)
2439 {
2440 if (rettv != NULL && verbose)
2441 semsg(_(e_undefvar), name);
2442 ret = FAIL;
2443 }
2444 else if (rettv != NULL)
2445 copy_tv(tv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002446 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002447
2448 name[len] = cc;
2449
2450 return ret;
2451}
2452
2453/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002454 * Check if variable "name[len]" is a local variable or an argument.
2455 * If so, "*eval_lavars_used" is set to TRUE.
2456 */
2457 void
2458check_vars(char_u *name, int len)
2459{
2460 int cc;
2461 char_u *varname;
2462 hashtab_T *ht;
2463
2464 if (eval_lavars_used == NULL)
2465 return;
2466
2467 // truncate the name, so that we can use strcmp()
2468 cc = name[len];
2469 name[len] = NUL;
2470
2471 ht = find_var_ht(name, &varname);
2472 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2473 {
2474 if (find_var(name, NULL, TRUE) != NULL)
2475 *eval_lavars_used = TRUE;
2476 }
2477
2478 name[len] = cc;
2479}
2480
2481/*
2482 * Find variable "name" in the list of variables.
2483 * Return a pointer to it if found, NULL if not found.
2484 * Careful: "a:0" variables don't have a name.
2485 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2486 * hashtab_T used.
2487 */
2488 dictitem_T *
2489find_var(char_u *name, hashtab_T **htp, int no_autoload)
2490{
2491 char_u *varname;
2492 hashtab_T *ht;
2493 dictitem_T *ret = NULL;
2494
2495 ht = find_var_ht(name, &varname);
2496 if (htp != NULL)
2497 *htp = ht;
2498 if (ht == NULL)
2499 return NULL;
2500 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2501 if (ret != NULL)
2502 return ret;
2503
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002504 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002505 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2506}
2507
2508/*
2509 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002510 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002511 * Returns NULL if not found.
2512 */
2513 dictitem_T *
2514find_var_in_ht(
2515 hashtab_T *ht,
2516 int htname,
2517 char_u *varname,
2518 int no_autoload)
2519{
2520 hashitem_T *hi;
2521
2522 if (*varname == NUL)
2523 {
2524 // Must be something like "s:", otherwise "ht" would be NULL.
2525 switch (htname)
2526 {
2527 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2528 case 'g': return &globvars_var;
2529 case 'v': return &vimvars_var;
2530 case 'b': return &curbuf->b_bufvar;
2531 case 'w': return &curwin->w_winvar;
2532 case 't': return &curtab->tp_winvar;
2533 case 'l': return get_funccal_local_var();
2534 case 'a': return get_funccal_args_var();
2535 }
2536 return NULL;
2537 }
2538
2539 hi = hash_find(ht, varname);
2540 if (HASHITEM_EMPTY(hi))
2541 {
2542 // For global variables we may try auto-loading the script. If it
2543 // worked find the variable again. Don't auto-load a script if it was
2544 // loaded already, otherwise it would be loaded every time when
2545 // checking if a function name is a Funcref variable.
2546 if (ht == &globvarht && !no_autoload)
2547 {
2548 // Note: script_autoload() may make "hi" invalid. It must either
2549 // be obtained again or not used.
2550 if (!script_autoload(varname, FALSE) || aborting())
2551 return NULL;
2552 hi = hash_find(ht, varname);
2553 }
2554 if (HASHITEM_EMPTY(hi))
2555 return NULL;
2556 }
2557 return HI2DI(hi);
2558}
2559
2560/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 * Get the script-local hashtab. NULL if not in a script context.
2562 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02002563 static hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564get_script_local_ht(void)
2565{
2566 scid_T sid = current_sctx.sc_sid;
2567
2568 if (sid > 0 && sid <= script_items.ga_len)
2569 return &SCRIPT_VARS(sid);
2570 return NULL;
2571}
2572
2573/*
2574 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002575 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002577 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2579{
2580 hashtab_T *ht = get_script_local_ht();
2581 char_u buffer[30];
2582 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002583 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584 hashitem_T *hi;
2585
2586 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002587 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588 if (len < sizeof(buffer) - 1)
2589 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002590 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 vim_strncpy(buffer, name, len);
2592 p = buffer;
2593 }
2594 else
2595 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002596 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002598 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 }
2600
2601 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002602 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603
2604 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002605 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2606 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002607
2608 if (p != buffer)
2609 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002610 // Don't return "buffer", gcc complains.
2611 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612}
2613
2614/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002615 * Find the hashtab used for a variable name.
2616 * Return NULL if the name is not valid.
2617 * Set "varname" to the start of name without ':'.
2618 */
2619 hashtab_T *
2620find_var_ht(char_u *name, char_u **varname)
2621{
2622 hashitem_T *hi;
2623 hashtab_T *ht;
2624
2625 if (name[0] == NUL)
2626 return NULL;
2627 if (name[1] != ':')
2628 {
2629 // The name must not start with a colon or #.
2630 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2631 return NULL;
2632 *varname = name;
2633
2634 // "version" is "v:version" in all scopes if scriptversion < 3.
2635 // Same for a few other variables marked with VV_COMPAT.
2636 if (current_sctx.sc_version < 3)
2637 {
2638 hi = hash_find(&compat_hashtab, name);
2639 if (!HASHITEM_EMPTY(hi))
2640 return &compat_hashtab;
2641 }
2642
2643 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 if (ht != NULL)
2645 return ht; // local variable
2646
2647 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002648 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649 {
2650 ht = get_script_local_ht();
2651 if (ht != NULL)
2652 return ht;
2653 }
2654
2655 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002656 }
2657 *varname = name + 2;
2658 if (*name == 'g') // global variable
2659 return &globvarht;
2660 // There must be no ':' or '#' in the rest of the name, unless g: is used
2661 if (vim_strchr(name + 2, ':') != NULL
2662 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2663 return NULL;
2664 if (*name == 'b') // buffer variable
2665 return &curbuf->b_vars->dv_hashtab;
2666 if (*name == 'w') // window variable
2667 return &curwin->w_vars->dv_hashtab;
2668 if (*name == 't') // tab page variable
2669 return &curtab->tp_vars->dv_hashtab;
2670 if (*name == 'v') // v: variable
2671 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002672 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002673 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002674 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002675 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676 if (*name == 'a') // a: function argument
2677 return get_funccal_args_ht();
2678 if (*name == 'l') // l: local function variable
2679 return get_funccal_local_ht();
2680 }
2681 if (*name == 's') // script variable
2682 {
2683 ht = get_script_local_ht();
2684 if (ht != NULL)
2685 return ht;
2686 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002687 return NULL;
2688}
2689
2690/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002691 * Get the string value of a (global/local) variable.
2692 * Note: see tv_get_string() for how long the pointer remains valid.
2693 * Returns NULL when it doesn't exist.
2694 */
2695 char_u *
2696get_var_value(char_u *name)
2697{
2698 dictitem_T *v;
2699
2700 v = find_var(name, NULL, FALSE);
2701 if (v == NULL)
2702 return NULL;
2703 return tv_get_string(&v->di_tv);
2704}
2705
2706/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002707 * Allocate a new hashtab for a sourced script. It will be used while
2708 * sourcing this script and when executing functions defined in the script.
2709 */
2710 void
2711new_script_vars(scid_T id)
2712{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002713 scriptvar_T *sv;
2714
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002715 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2716 if (sv == NULL)
2717 return;
2718 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002719 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002720}
2721
2722/*
2723 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2724 * point to it.
2725 */
2726 void
2727init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2728{
2729 hash_init(&dict->dv_hashtab);
2730 dict->dv_lock = 0;
2731 dict->dv_scope = scope;
2732 dict->dv_refcount = DO_NOT_FREE_CNT;
2733 dict->dv_copyID = 0;
2734 dict_var->di_tv.vval.v_dict = dict;
2735 dict_var->di_tv.v_type = VAR_DICT;
2736 dict_var->di_tv.v_lock = VAR_FIXED;
2737 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2738 dict_var->di_key[0] = NUL;
2739}
2740
2741/*
2742 * Unreference a dictionary initialized by init_var_dict().
2743 */
2744 void
2745unref_var_dict(dict_T *dict)
2746{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002747 // Now the dict needs to be freed if no one else is using it, go back to
2748 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002749 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2750 dict_unref(dict);
2751}
2752
2753/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002754 * Clean up a list of internal variables.
2755 * Frees all allocated variables and the value they contain.
2756 * Clears hashtab "ht", does not free it.
2757 */
2758 void
2759vars_clear(hashtab_T *ht)
2760{
2761 vars_clear_ext(ht, TRUE);
2762}
2763
2764/*
2765 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2766 */
2767 void
2768vars_clear_ext(hashtab_T *ht, int free_val)
2769{
2770 int todo;
2771 hashitem_T *hi;
2772 dictitem_T *v;
2773
2774 hash_lock(ht);
2775 todo = (int)ht->ht_used;
2776 for (hi = ht->ht_array; todo > 0; ++hi)
2777 {
2778 if (!HASHITEM_EMPTY(hi))
2779 {
2780 --todo;
2781
2782 // Free the variable. Don't remove it from the hashtab,
2783 // ht_array might change then. hash_clear() takes care of it
2784 // later.
2785 v = HI2DI(hi);
2786 if (free_val)
2787 clear_tv(&v->di_tv);
2788 if (v->di_flags & DI_FLAGS_ALLOC)
2789 vim_free(v);
2790 }
2791 }
2792 hash_clear(ht);
2793 ht->ht_used = 0;
2794}
2795
2796/*
2797 * Delete a variable from hashtab "ht" at item "hi".
2798 * Clear the variable value and free the dictitem.
2799 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002800 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002801delete_var(hashtab_T *ht, hashitem_T *hi)
2802{
2803 dictitem_T *di = HI2DI(hi);
2804
2805 hash_remove(ht, hi);
2806 clear_tv(&di->di_tv);
2807 vim_free(di);
2808}
2809
2810/*
2811 * List the value of one internal variable.
2812 */
2813 static void
2814list_one_var(dictitem_T *v, char *prefix, int *first)
2815{
2816 char_u *tofree;
2817 char_u *s;
2818 char_u numbuf[NUMBUFLEN];
2819
2820 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2821 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2822 s == NULL ? (char_u *)"" : s, first);
2823 vim_free(tofree);
2824}
2825
2826 static void
2827list_one_var_a(
2828 char *prefix,
2829 char_u *name,
2830 int type,
2831 char_u *string,
2832 int *first) // when TRUE clear rest of screen and set to FALSE
2833{
2834 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2835 msg_start();
2836 msg_puts(prefix);
2837 if (name != NULL) // "a:" vars don't have a name stored
2838 msg_puts((char *)name);
2839 msg_putchar(' ');
2840 msg_advance(22);
2841 if (type == VAR_NUMBER)
2842 msg_putchar('#');
2843 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2844 msg_putchar('*');
2845 else if (type == VAR_LIST)
2846 {
2847 msg_putchar('[');
2848 if (*string == '[')
2849 ++string;
2850 }
2851 else if (type == VAR_DICT)
2852 {
2853 msg_putchar('{');
2854 if (*string == '{')
2855 ++string;
2856 }
2857 else
2858 msg_putchar(' ');
2859
2860 msg_outtrans(string);
2861
2862 if (type == VAR_FUNC || type == VAR_PARTIAL)
2863 msg_puts("()");
2864 if (*first)
2865 {
2866 msg_clr_eos();
2867 *first = FALSE;
2868 }
2869}
2870
2871/*
2872 * Set variable "name" to value in "tv".
2873 * If the variable already exists, the value is updated.
2874 * Otherwise the variable is created.
2875 */
2876 void
2877set_var(
2878 char_u *name,
2879 typval_T *tv,
2880 int copy) // make copy of value in "tv"
2881{
Bram Moolenaareeb27bf2020-07-04 17:39:10 +02002882 set_var_const(name, NULL, tv, copy, LET_NO_COMMAND);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002883}
2884
2885/*
2886 * Set variable "name" to value in "tv".
2887 * If the variable already exists and "is_const" is FALSE the value is updated.
2888 * Otherwise the variable is created.
2889 */
2890 void
2891set_var_const(
2892 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893 type_T *type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002894 typval_T *tv,
2895 int copy, // make copy of value in "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 int flags) // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002897{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002899 char_u *varname;
2900 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 int is_script_local;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002902
2903 ht = find_var_ht(name, &varname);
2904 if (ht == NULL || *varname == NUL)
2905 {
2906 semsg(_(e_illvar), name);
2907 return;
2908 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002909 is_script_local = ht == get_script_local_ht();
2910
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002911 if (in_vim9script()
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002912 && !is_script_local
2913 && (flags & LET_NO_COMMAND) == 0
2914 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02002915 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002916 vim9_declare_error(name);
Bram Moolenaar67979662020-06-20 22:50:47 +02002917 return;
2918 }
2919
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002921
2922 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923 if (di == NULL)
2924 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002925
2926 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 && var_check_func_name(name, di == NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002928 return;
2929
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002931 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002933 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002934 if (flags & LET_IS_CONST)
2935 {
2936 emsg(_(e_cannot_mod));
2937 return;
2938 }
2939
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002940 if (is_script_local && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 {
Bram Moolenaar34db91f2020-06-13 19:00:10 +02002942 if ((flags & LET_NO_COMMAND) == 0)
2943 {
2944 semsg(_("E1041: Redefining script item %s"), name);
2945 return;
2946 }
2947
2948 // check the type
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002949 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
2950 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002952
2953 if (var_check_ro(di->di_flags, name, FALSE)
2954 || var_check_lock(di->di_tv.v_lock, name, FALSE))
2955 return;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002956 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 else
2958 // can only redefine once
2959 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002960
2961 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002962
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002964 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002965 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002966 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002968 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002970 if (copy || tv->v_type != VAR_STRING)
2971 {
2972 char_u *val = tv_get_string(tv);
2973
2974 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002975 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976 if (di->di_tv.vval.v_string == NULL)
2977 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002978 }
2979 else
2980 {
2981 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002983 tv->vval.v_string = NULL;
2984 }
2985 return;
2986 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002988 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002990 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002992#ifdef FEAT_SEARCH_EXTRA
2993 else if (STRCMP(varname, "hlsearch") == 0)
2994 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002996 redraw_all_later(SOME_VALID);
2997 }
2998#endif
2999 return;
3000 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003002 {
3003 semsg(_("E963: setting %s to value with wrong type"), name);
3004 return;
3005 }
3006 }
3007
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003009 }
3010 else // add a new variable
3011 {
3012 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003013 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003014 {
3015 semsg(_(e_illvar), name);
3016 return;
3017 }
3018
3019 // Make sure the variable name is valid.
3020 if (!valid_varname(varname))
3021 return;
3022
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3024 if (di == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003025 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 STRCPY(di->di_key, varname);
3027 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003028 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003030 return;
3031 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 di->di_flags = DI_FLAGS_ALLOC;
3033 if (flags & LET_IS_CONST)
3034 di->di_flags |= DI_FLAGS_LOCK;
3035
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003036 if (is_script_local && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003038 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039
3040 // Store a pointer to the typval_T, so that it can be found by
3041 // index instead of using a hastab lookup.
3042 if (ga_grow(&si->sn_var_vals, 1) == OK)
3043 {
3044 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3045 + si->sn_var_vals.ga_len;
3046 sv->sv_name = di->di_key;
3047 sv->sv_tv = &di->di_tv;
3048 sv->sv_type = type == NULL ? &t_any : type;
3049 sv->sv_const = (flags & LET_IS_CONST);
3050 sv->sv_export = is_export;
3051 ++si->sn_var_vals.ga_len;
3052
3053 // let ex_export() know the export worked.
3054 is_export = FALSE;
3055 }
3056 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003057 }
3058
3059 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003061 else
3062 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 di->di_tv = *tv;
3064 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003065 init_tv(tv);
3066 }
3067
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 if (flags & LET_IS_CONST)
3069 di->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003070}
3071
3072/*
3073 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3074 * Also give an error message.
3075 */
3076 int
3077var_check_ro(int flags, char_u *name, int use_gettext)
3078{
3079 if (flags & DI_FLAGS_RO)
3080 {
3081 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3082 return TRUE;
3083 }
3084 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3085 {
3086 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3087 return TRUE;
3088 }
3089 return FALSE;
3090}
3091
3092/*
3093 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3094 * Also give an error message.
3095 */
3096 int
3097var_check_fixed(int flags, char_u *name, int use_gettext)
3098{
3099 if (flags & DI_FLAGS_FIX)
3100 {
3101 semsg(_("E795: Cannot delete variable %s"),
3102 use_gettext ? (char_u *)_(name) : name);
3103 return TRUE;
3104 }
3105 return FALSE;
3106}
3107
3108/*
3109 * Check if a funcref is assigned to a valid variable name.
3110 * Return TRUE and give an error if not.
3111 */
3112 int
3113var_check_func_name(
3114 char_u *name, // points to start of variable name
3115 int new_var) // TRUE when creating the variable
3116{
3117 // Allow for w: b: s: and t:.
3118 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3119 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3120 ? name[2] : name[0]))
3121 {
3122 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3123 name);
3124 return TRUE;
3125 }
3126 // Don't allow hiding a function. When "v" is not NULL we might be
3127 // assigning another function to the same var, the type is checked
3128 // below.
3129 if (new_var && function_exists(name, FALSE))
3130 {
3131 semsg(_("E705: Variable name conflicts with existing function: %s"),
3132 name);
3133 return TRUE;
3134 }
3135 return FALSE;
3136}
3137
3138/*
3139 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
3140 * Also give an error message, using "name" or _("name") when use_gettext is
3141 * TRUE.
3142 */
3143 int
3144var_check_lock(int lock, char_u *name, int use_gettext)
3145{
3146 if (lock & VAR_LOCKED)
3147 {
3148 semsg(_("E741: Value is locked: %s"),
3149 name == NULL ? (char_u *)_("Unknown")
3150 : use_gettext ? (char_u *)_(name)
3151 : name);
3152 return TRUE;
3153 }
3154 if (lock & VAR_FIXED)
3155 {
3156 semsg(_("E742: Cannot change value of %s"),
3157 name == NULL ? (char_u *)_("Unknown")
3158 : use_gettext ? (char_u *)_(name)
3159 : name);
3160 return TRUE;
3161 }
3162 return FALSE;
3163}
3164
3165/*
3166 * Check if a variable name is valid.
3167 * Return FALSE and give an error if not.
3168 */
3169 int
3170valid_varname(char_u *varname)
3171{
3172 char_u *p;
3173
3174 for (p = varname; *p != NUL; ++p)
3175 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3176 && *p != AUTOLOAD_CHAR)
3177 {
3178 semsg(_(e_illvar), varname);
3179 return FALSE;
3180 }
3181 return TRUE;
3182}
3183
3184/*
3185 * getwinvar() and gettabwinvar()
3186 */
3187 static void
3188getwinvar(
3189 typval_T *argvars,
3190 typval_T *rettv,
3191 int off) // 1 for gettabwinvar()
3192{
3193 win_T *win;
3194 char_u *varname;
3195 dictitem_T *v;
3196 tabpage_T *tp = NULL;
3197 int done = FALSE;
3198 win_T *oldcurwin;
3199 tabpage_T *oldtabpage;
3200 int need_switch_win;
3201
3202 if (off == 1)
3203 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3204 else
3205 tp = curtab;
3206 win = find_win_by_nr(&argvars[off], tp);
3207 varname = tv_get_string_chk(&argvars[off + 1]);
3208 ++emsg_off;
3209
3210 rettv->v_type = VAR_STRING;
3211 rettv->vval.v_string = NULL;
3212
3213 if (win != NULL && varname != NULL)
3214 {
3215 // Set curwin to be our win, temporarily. Also set the tabpage,
3216 // otherwise the window is not valid. Only do this when needed,
3217 // autocommands get blocked.
3218 need_switch_win = !(tp == curtab && win == curwin);
3219 if (!need_switch_win
3220 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3221 {
3222 if (*varname == '&')
3223 {
3224 if (varname[1] == NUL)
3225 {
3226 // get all window-local options in a dict
3227 dict_T *opts = get_winbuf_options(FALSE);
3228
3229 if (opts != NULL)
3230 {
3231 rettv_dict_set(rettv, opts);
3232 done = TRUE;
3233 }
3234 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003235 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003236 // window-local-option
3237 done = TRUE;
3238 }
3239 else
3240 {
3241 // Look up the variable.
3242 // Let getwinvar({nr}, "") return the "w:" dictionary.
3243 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3244 varname, FALSE);
3245 if (v != NULL)
3246 {
3247 copy_tv(&v->di_tv, rettv);
3248 done = TRUE;
3249 }
3250 }
3251 }
3252
3253 if (need_switch_win)
3254 // restore previous notion of curwin
3255 restore_win(oldcurwin, oldtabpage, TRUE);
3256 }
3257
3258 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3259 // use the default return value
3260 copy_tv(&argvars[off + 2], rettv);
3261
3262 --emsg_off;
3263}
3264
3265/*
3266 * "setwinvar()" and "settabwinvar()" functions
3267 */
3268 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003269setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003270{
3271 win_T *win;
3272 win_T *save_curwin;
3273 tabpage_T *save_curtab;
3274 int need_switch_win;
3275 char_u *varname, *winvarname;
3276 typval_T *varp;
3277 char_u nbuf[NUMBUFLEN];
3278 tabpage_T *tp = NULL;
3279
3280 if (check_secure())
3281 return;
3282
3283 if (off == 1)
3284 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3285 else
3286 tp = curtab;
3287 win = find_win_by_nr(&argvars[off], tp);
3288 varname = tv_get_string_chk(&argvars[off + 1]);
3289 varp = &argvars[off + 2];
3290
3291 if (win != NULL && varname != NULL && varp != NULL)
3292 {
3293 need_switch_win = !(tp == curtab && win == curwin);
3294 if (!need_switch_win
3295 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3296 {
3297 if (*varname == '&')
3298 {
3299 long numval;
3300 char_u *strval;
3301 int error = FALSE;
3302
3303 ++varname;
3304 numval = (long)tv_get_number_chk(varp, &error);
3305 strval = tv_get_string_buf_chk(varp, nbuf);
3306 if (!error && strval != NULL)
3307 set_option_value(varname, numval, strval, OPT_LOCAL);
3308 }
3309 else
3310 {
3311 winvarname = alloc(STRLEN(varname) + 3);
3312 if (winvarname != NULL)
3313 {
3314 STRCPY(winvarname, "w:");
3315 STRCPY(winvarname + 2, varname);
3316 set_var(winvarname, varp, TRUE);
3317 vim_free(winvarname);
3318 }
3319 }
3320 }
3321 if (need_switch_win)
3322 restore_win(save_curwin, save_curtab, TRUE);
3323 }
3324}
3325
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003326/*
3327 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3328 * v:option_type, and v:option_command.
3329 */
3330 void
3331reset_v_option_vars(void)
3332{
3333 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3334 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3335 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3336 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3337 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3338 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3339}
3340
3341/*
3342 * Add an assert error to v:errors.
3343 */
3344 void
3345assert_error(garray_T *gap)
3346{
3347 struct vimvar *vp = &vimvars[VV_ERRORS];
3348
3349 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003350 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003351 set_vim_var_list(VV_ERRORS, list_alloc());
3352 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3353}
3354
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003355 int
3356var_exists(char_u *var)
3357{
3358 char_u *name;
3359 char_u *tofree;
3360 typval_T tv;
3361 int len = 0;
3362 int n = FALSE;
3363
3364 // get_name_len() takes care of expanding curly braces
3365 name = var;
3366 len = get_name_len(&var, &tofree, TRUE, FALSE);
3367 if (len > 0)
3368 {
3369 if (tofree != NULL)
3370 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003371 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003372 if (n)
3373 {
3374 // handle d.key, l[idx], f(expr)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003375 n = (handle_subscript(&var, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003376 if (n)
3377 clear_tv(&tv);
3378 }
3379 }
3380 if (*var != NUL)
3381 n = FALSE;
3382
3383 vim_free(tofree);
3384 return n;
3385}
3386
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003387static lval_T *redir_lval = NULL;
3388#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3389static garray_T redir_ga; // only valid when redir_lval is not NULL
3390static char_u *redir_endp = NULL;
3391static char_u *redir_varname = NULL;
3392
3393/*
3394 * Start recording command output to a variable
3395 * When "append" is TRUE append to an existing variable.
3396 * Returns OK if successfully completed the setup. FAIL otherwise.
3397 */
3398 int
3399var_redir_start(char_u *name, int append)
3400{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003401 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003402 typval_T tv;
3403
3404 // Catch a bad name early.
3405 if (!eval_isnamec1(*name))
3406 {
3407 emsg(_(e_invarg));
3408 return FAIL;
3409 }
3410
3411 // Make a copy of the name, it is used in redir_lval until redir ends.
3412 redir_varname = vim_strsave(name);
3413 if (redir_varname == NULL)
3414 return FAIL;
3415
3416 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3417 if (redir_lval == NULL)
3418 {
3419 var_redir_stop();
3420 return FAIL;
3421 }
3422
3423 // The output is stored in growarray "redir_ga" until redirection ends.
3424 ga_init2(&redir_ga, (int)sizeof(char), 500);
3425
3426 // Parse the variable name (can be a dict or list entry).
3427 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3428 FNE_CHECK_START);
3429 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3430 {
3431 clear_lval(redir_lval);
3432 if (redir_endp != NULL && *redir_endp != NUL)
3433 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003434 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003435 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003436 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003437 redir_endp = NULL; // don't store a value, only cleanup
3438 var_redir_stop();
3439 return FAIL;
3440 }
3441
3442 // check if we can write to the variable: set it to or append an empty
3443 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003444 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003445 tv.v_type = VAR_STRING;
3446 tv.vval.v_string = (char_u *)"";
3447 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003449 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003451 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003452 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003453 {
3454 redir_endp = NULL; // don't store a value, only cleanup
3455 var_redir_stop();
3456 return FAIL;
3457 }
3458
3459 return OK;
3460}
3461
3462/*
3463 * Append "value[value_len]" to the variable set by var_redir_start().
3464 * The actual appending is postponed until redirection ends, because the value
3465 * appended may in fact be the string we write to, changing it may cause freed
3466 * memory to be used:
3467 * :redir => foo
3468 * :let foo
3469 * :redir END
3470 */
3471 void
3472var_redir_str(char_u *value, int value_len)
3473{
3474 int len;
3475
3476 if (redir_lval == NULL)
3477 return;
3478
3479 if (value_len == -1)
3480 len = (int)STRLEN(value); // Append the entire string
3481 else
3482 len = value_len; // Append only "value_len" characters
3483
3484 if (ga_grow(&redir_ga, len) == OK)
3485 {
3486 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3487 redir_ga.ga_len += len;
3488 }
3489 else
3490 var_redir_stop();
3491}
3492
3493/*
3494 * Stop redirecting command output to a variable.
3495 * Frees the allocated memory.
3496 */
3497 void
3498var_redir_stop(void)
3499{
3500 typval_T tv;
3501
3502 if (EVALCMD_BUSY)
3503 {
3504 redir_lval = NULL;
3505 return;
3506 }
3507
3508 if (redir_lval != NULL)
3509 {
3510 // If there was no error: assign the text to the variable.
3511 if (redir_endp != NULL)
3512 {
3513 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3514 tv.v_type = VAR_STRING;
3515 tv.vval.v_string = redir_ga.ga_data;
3516 // Call get_lval() again, if it's inside a Dict or List it may
3517 // have changed.
3518 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3519 FALSE, FALSE, 0, FNE_CHECK_START);
3520 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003522 (char_u *)".");
3523 clear_lval(redir_lval);
3524 }
3525
3526 // free the collected output
3527 VIM_CLEAR(redir_ga.ga_data);
3528
3529 VIM_CLEAR(redir_lval);
3530 }
3531 VIM_CLEAR(redir_varname);
3532}
3533
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003534/*
3535 * "gettabvar()" function
3536 */
3537 void
3538f_gettabvar(typval_T *argvars, typval_T *rettv)
3539{
3540 win_T *oldcurwin;
3541 tabpage_T *tp, *oldtabpage;
3542 dictitem_T *v;
3543 char_u *varname;
3544 int done = FALSE;
3545
3546 rettv->v_type = VAR_STRING;
3547 rettv->vval.v_string = NULL;
3548
3549 varname = tv_get_string_chk(&argvars[1]);
3550 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3551 if (tp != NULL && varname != NULL)
3552 {
3553 // Set tp to be our tabpage, temporarily. Also set the window to the
3554 // first window in the tabpage, otherwise the window is not valid.
3555 if (switch_win(&oldcurwin, &oldtabpage,
3556 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3557 : tp->tp_firstwin, tp, TRUE) == OK)
3558 {
3559 // look up the variable
3560 // Let gettabvar({nr}, "") return the "t:" dictionary.
3561 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3562 if (v != NULL)
3563 {
3564 copy_tv(&v->di_tv, rettv);
3565 done = TRUE;
3566 }
3567 }
3568
3569 // restore previous notion of curwin
3570 restore_win(oldcurwin, oldtabpage, TRUE);
3571 }
3572
3573 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3574 copy_tv(&argvars[2], rettv);
3575}
3576
3577/*
3578 * "gettabwinvar()" function
3579 */
3580 void
3581f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3582{
3583 getwinvar(argvars, rettv, 1);
3584}
3585
3586/*
3587 * "getwinvar()" function
3588 */
3589 void
3590f_getwinvar(typval_T *argvars, typval_T *rettv)
3591{
3592 getwinvar(argvars, rettv, 0);
3593}
3594
3595/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003596 * "getbufvar()" function
3597 */
3598 void
3599f_getbufvar(typval_T *argvars, typval_T *rettv)
3600{
3601 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003602 char_u *varname;
3603 dictitem_T *v;
3604 int done = FALSE;
3605
3606 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3607 varname = tv_get_string_chk(&argvars[1]);
3608 ++emsg_off;
3609 buf = tv_get_buf(&argvars[0], FALSE);
3610
3611 rettv->v_type = VAR_STRING;
3612 rettv->vval.v_string = NULL;
3613
3614 if (buf != NULL && varname != NULL)
3615 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003616 if (*varname == '&')
3617 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003618 buf_T *save_curbuf = curbuf;
3619
3620 // set curbuf to be our buf, temporarily
3621 curbuf = buf;
3622
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003623 if (varname[1] == NUL)
3624 {
3625 // get all buffer-local options in a dict
3626 dict_T *opts = get_winbuf_options(TRUE);
3627
3628 if (opts != NULL)
3629 {
3630 rettv_dict_set(rettv, opts);
3631 done = TRUE;
3632 }
3633 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003634 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003635 // buffer-local-option
3636 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003637
3638 // restore previous notion of curbuf
3639 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003640 }
3641 else
3642 {
3643 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003644 if (*varname == NUL)
3645 // Let getbufvar({nr}, "") return the "b:" dictionary.
3646 v = &buf->b_bufvar;
3647 else
3648 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3649 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003650 if (v != NULL)
3651 {
3652 copy_tv(&v->di_tv, rettv);
3653 done = TRUE;
3654 }
3655 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003656 }
3657
3658 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3659 // use the default value
3660 copy_tv(&argvars[2], rettv);
3661
3662 --emsg_off;
3663}
3664
3665/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003666 * "settabvar()" function
3667 */
3668 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003669f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003670{
3671 tabpage_T *save_curtab;
3672 tabpage_T *tp;
3673 char_u *varname, *tabvarname;
3674 typval_T *varp;
3675
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003676 if (check_secure())
3677 return;
3678
3679 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3680 varname = tv_get_string_chk(&argvars[1]);
3681 varp = &argvars[2];
3682
3683 if (varname != NULL && varp != NULL && tp != NULL)
3684 {
3685 save_curtab = curtab;
3686 goto_tabpage_tp(tp, FALSE, FALSE);
3687
3688 tabvarname = alloc(STRLEN(varname) + 3);
3689 if (tabvarname != NULL)
3690 {
3691 STRCPY(tabvarname, "t:");
3692 STRCPY(tabvarname + 2, varname);
3693 set_var(tabvarname, varp, TRUE);
3694 vim_free(tabvarname);
3695 }
3696
3697 // Restore current tabpage
3698 if (valid_tabpage(save_curtab))
3699 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3700 }
3701}
3702
3703/*
3704 * "settabwinvar()" function
3705 */
3706 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003707f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003708{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003709 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003710}
3711
3712/*
3713 * "setwinvar()" function
3714 */
3715 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003716f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003717{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003718 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003719}
3720
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003721/*
3722 * "setbufvar()" function
3723 */
3724 void
3725f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3726{
3727 buf_T *buf;
3728 char_u *varname, *bufvarname;
3729 typval_T *varp;
3730 char_u nbuf[NUMBUFLEN];
3731
3732 if (check_secure())
3733 return;
3734 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3735 varname = tv_get_string_chk(&argvars[1]);
3736 buf = tv_get_buf(&argvars[0], FALSE);
3737 varp = &argvars[2];
3738
3739 if (buf != NULL && varname != NULL && varp != NULL)
3740 {
3741 if (*varname == '&')
3742 {
3743 long numval;
3744 char_u *strval;
3745 int error = FALSE;
3746 aco_save_T aco;
3747
3748 // set curbuf to be our buf, temporarily
3749 aucmd_prepbuf(&aco, buf);
3750
3751 ++varname;
3752 numval = (long)tv_get_number_chk(varp, &error);
3753 strval = tv_get_string_buf_chk(varp, nbuf);
3754 if (!error && strval != NULL)
3755 set_option_value(varname, numval, strval, OPT_LOCAL);
3756
3757 // reset notion of buffer
3758 aucmd_restbuf(&aco);
3759 }
3760 else
3761 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003762 bufvarname = alloc(STRLEN(varname) + 3);
3763 if (bufvarname != NULL)
3764 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003765 buf_T *save_curbuf = curbuf;
3766
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003767 curbuf = buf;
3768 STRCPY(bufvarname, "b:");
3769 STRCPY(bufvarname + 2, varname);
3770 set_var(bufvarname, varp, TRUE);
3771 vim_free(bufvarname);
3772 curbuf = save_curbuf;
3773 }
3774 }
3775 }
3776}
3777
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003778/*
3779 * Get a callback from "arg". It can be a Funcref or a function name.
3780 * When "arg" is zero return an empty string.
3781 * "cb_name" is not allocated.
3782 * "cb_name" is set to NULL for an invalid argument.
3783 */
3784 callback_T
3785get_callback(typval_T *arg)
3786{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003787 callback_T res;
3788 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003789
3790 res.cb_free_name = FALSE;
3791 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3792 {
3793 res.cb_partial = arg->vval.v_partial;
3794 ++res.cb_partial->pt_refcount;
3795 res.cb_name = partial_name(res.cb_partial);
3796 }
3797 else
3798 {
3799 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003800 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3801 && isdigit(*arg->vval.v_string))
3802 r = FAIL;
3803 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003804 {
3805 // Note that we don't make a copy of the string.
3806 res.cb_name = arg->vval.v_string;
3807 func_ref(res.cb_name);
3808 }
3809 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003810 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003811 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003812 r = FAIL;
3813
3814 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003815 {
3816 emsg(_("E921: Invalid callback argument"));
3817 res.cb_name = NULL;
3818 }
3819 }
3820 return res;
3821}
3822
3823/*
3824 * Copy a callback into a typval_T.
3825 */
3826 void
3827put_callback(callback_T *cb, typval_T *tv)
3828{
3829 if (cb->cb_partial != NULL)
3830 {
3831 tv->v_type = VAR_PARTIAL;
3832 tv->vval.v_partial = cb->cb_partial;
3833 ++tv->vval.v_partial->pt_refcount;
3834 }
3835 else
3836 {
3837 tv->v_type = VAR_FUNC;
3838 tv->vval.v_string = vim_strsave(cb->cb_name);
3839 func_ref(cb->cb_name);
3840 }
3841}
3842
3843/*
3844 * Make a copy of "src" into "dest", allocating the function name if needed,
3845 * without incrementing the refcount.
3846 */
3847 void
3848set_callback(callback_T *dest, callback_T *src)
3849{
3850 if (src->cb_partial == NULL)
3851 {
3852 // just a function name, make a copy
3853 dest->cb_name = vim_strsave(src->cb_name);
3854 dest->cb_free_name = TRUE;
3855 }
3856 else
3857 {
3858 // cb_name is a pointer into cb_partial
3859 dest->cb_name = src->cb_name;
3860 dest->cb_free_name = FALSE;
3861 }
3862 dest->cb_partial = src->cb_partial;
3863}
3864
3865/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02003866 * Copy callback from "src" to "dest", incrementing the refcounts.
3867 */
3868 void
3869copy_callback(callback_T *dest, callback_T *src)
3870{
3871 dest->cb_partial = src->cb_partial;
3872 if (dest->cb_partial != NULL)
3873 {
3874 dest->cb_name = src->cb_name;
3875 dest->cb_free_name = FALSE;
3876 ++dest->cb_partial->pt_refcount;
3877 }
3878 else
3879 {
3880 dest->cb_name = vim_strsave(src->cb_name);
3881 dest->cb_free_name = TRUE;
3882 func_ref(src->cb_name);
3883 }
3884}
3885
3886/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003887 * Unref/free "callback" returned by get_callback() or set_callback().
3888 */
3889 void
3890free_callback(callback_T *callback)
3891{
3892 if (callback->cb_partial != NULL)
3893 {
3894 partial_unref(callback->cb_partial);
3895 callback->cb_partial = NULL;
3896 }
3897 else if (callback->cb_name != NULL)
3898 func_unref(callback->cb_name);
3899 if (callback->cb_free_name)
3900 {
3901 vim_free(callback->cb_name);
3902 callback->cb_free_name = FALSE;
3903 }
3904 callback->cb_name = NULL;
3905}
3906
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003907#endif // FEAT_EVAL