blob: f2e875cc8ab83f2f41955f769a16591d967deb49 [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
48 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
49} vimvars[VV_LEN] =
50{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020051 // The order here must match the VV_ defines in vim.h!
52 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaare5cdf152019-08-29 22:09:46 +020053 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
54 {VV_NAME("count1", VAR_NUMBER), VV_RO},
55 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
56 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
57 {VV_NAME("warningmsg", VAR_STRING), 0},
58 {VV_NAME("statusmsg", VAR_STRING), 0},
59 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
60 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
61 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
62 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
63 {VV_NAME("termresponse", VAR_STRING), VV_RO},
64 {VV_NAME("fname", VAR_STRING), VV_RO},
65 {VV_NAME("lang", VAR_STRING), VV_RO},
66 {VV_NAME("lc_time", VAR_STRING), VV_RO},
67 {VV_NAME("ctype", VAR_STRING), VV_RO},
68 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
69 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
70 {VV_NAME("fname_in", VAR_STRING), VV_RO},
71 {VV_NAME("fname_out", VAR_STRING), VV_RO},
72 {VV_NAME("fname_new", VAR_STRING), VV_RO},
73 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
74 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
75 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
76 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
77 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
78 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
79 {VV_NAME("progname", VAR_STRING), VV_RO},
80 {VV_NAME("servername", VAR_STRING), VV_RO},
81 {VV_NAME("dying", VAR_NUMBER), VV_RO},
82 {VV_NAME("exception", VAR_STRING), VV_RO},
83 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
84 {VV_NAME("register", VAR_STRING), VV_RO},
85 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
86 {VV_NAME("insertmode", VAR_STRING), VV_RO},
87 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
88 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
89 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
90 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
91 {VV_NAME("fcs_choice", VAR_STRING), 0},
92 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
93 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
94 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
95 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
96 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
97 {VV_NAME("beval_text", VAR_STRING), VV_RO},
98 {VV_NAME("scrollstart", VAR_STRING), 0},
99 {VV_NAME("swapname", VAR_STRING), VV_RO},
100 {VV_NAME("swapchoice", VAR_STRING), 0},
101 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
102 {VV_NAME("char", VAR_STRING), 0},
103 {VV_NAME("mouse_win", VAR_NUMBER), 0},
104 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
105 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
106 {VV_NAME("mouse_col", VAR_NUMBER), 0},
107 {VV_NAME("operator", VAR_STRING), VV_RO},
108 {VV_NAME("searchforward", VAR_NUMBER), 0},
109 {VV_NAME("hlsearch", VAR_NUMBER), 0},
110 {VV_NAME("oldfiles", VAR_LIST), 0},
111 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
112 {VV_NAME("progpath", VAR_STRING), VV_RO},
113 {VV_NAME("completed_item", VAR_DICT), VV_RO},
114 {VV_NAME("option_new", VAR_STRING), VV_RO},
115 {VV_NAME("option_old", VAR_STRING), VV_RO},
116 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
117 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
118 {VV_NAME("option_command", VAR_STRING), VV_RO},
119 {VV_NAME("option_type", VAR_STRING), VV_RO},
120 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100121 {VV_NAME("false", VAR_BOOL), VV_RO},
122 {VV_NAME("true", VAR_BOOL), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200123 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100124 {VV_NAME("null", VAR_SPECIAL), VV_RO},
125 {VV_NAME("numbersize", VAR_NUMBER), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200126 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
127 {VV_NAME("testing", VAR_NUMBER), 0},
128 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
129 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
130 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
131 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
132 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
133 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
134 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
135 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
136 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
137 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
138 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
139 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
140 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
141 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
142 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
143 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
144 {VV_NAME("event", VAR_DICT), VV_RO},
145 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
146 {VV_NAME("echospace", VAR_NUMBER), VV_RO},
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100147 {VV_NAME("argv", VAR_LIST), VV_RO},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200148 {VV_NAME("collate", VAR_STRING), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200149};
150
151// shorthand
152#define vv_type vv_di.di_tv.v_type
153#define vv_nr vv_di.di_tv.vval.v_number
154#define vv_float vv_di.di_tv.vval.v_float
155#define vv_str vv_di.di_tv.vval.v_string
156#define vv_list vv_di.di_tv.vval.v_list
157#define vv_dict vv_di.di_tv.vval.v_dict
158#define vv_blob vv_di.di_tv.vval.v_blob
159#define vv_tv vv_di.di_tv
160
161static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200162static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200163#define vimvarht vimvardict.dv_hashtab
164
165// for VIM_VERSION_ defines
166#include "version.h"
167
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200168static void list_glob_vars(int *first);
169static void list_buf_vars(int *first);
170static void list_win_vars(int *first);
171static void list_tab_vars(int *first);
172static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200174static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
175static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaar021bda52020-08-17 21:07:22 +0200176static void item_lock(typval_T *tv, int deep, int lock, int check_refcount);
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200177static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200178static void list_one_var(dictitem_T *v, char *prefix, int *first);
179static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
180
181/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200182 * Initialize global and vim special variables
183 */
184 void
185evalvars_init(void)
186{
187 int i;
188 struct vimvar *p;
189
190 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
191 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
192 vimvardict.dv_lock = VAR_FIXED;
193 hash_init(&compat_hashtab);
194
195 for (i = 0; i < VV_LEN; ++i)
196 {
197 p = &vimvars[i];
198 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
199 {
200 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
201 getout(1);
202 }
203 STRCPY(p->vv_di.di_key, p->vv_name);
204 if (p->vv_flags & VV_RO)
205 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
206 else if (p->vv_flags & VV_RO_SBX)
207 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
208 else
209 p->vv_di.di_flags = DI_FLAGS_FIX;
210
211 // add to v: scope dict, unless the value is not always available
212 if (p->vv_type != VAR_UNKNOWN)
213 hash_add(&vimvarht, p->vv_di.di_key);
214 if (p->vv_flags & VV_COMPAT)
215 // add to compat scope dict
216 hash_add(&compat_hashtab, p->vv_di.di_key);
217 }
218 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
219 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
220
221 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
222 set_vim_var_nr(VV_HLSEARCH, 1L);
223 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
224 set_vim_var_list(VV_ERRORS, list_alloc());
225 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
226
227 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
228 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
229 set_vim_var_nr(VV_NONE, VVAL_NONE);
230 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100231 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200232
233 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
234 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
235 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
236 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
237 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
238 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
239 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
240 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
241 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
242 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
243 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
244
245 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
246
Bram Moolenaar439c0362020-06-06 15:58:03 +0200247 // Default for v:register is not 0 but '"'. This is adjusted once the
248 // clipboard has been setup by calling reset_reg_var().
249 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200250}
251
252#if defined(EXITFREE) || defined(PROTO)
253/*
254 * Free all vim variables information on exit
255 */
256 void
257evalvars_clear(void)
258{
259 int i;
260 struct vimvar *p;
261
262 for (i = 0; i < VV_LEN; ++i)
263 {
264 p = &vimvars[i];
265 if (p->vv_di.di_tv.v_type == VAR_STRING)
266 VIM_CLEAR(p->vv_str);
267 else if (p->vv_di.di_tv.v_type == VAR_LIST)
268 {
269 list_unref(p->vv_list);
270 p->vv_list = NULL;
271 }
272 }
273 hash_clear(&vimvarht);
274 hash_init(&vimvarht); // garbage_collect() will access it
275 hash_clear(&compat_hashtab);
276
277 // global variables
278 vars_clear(&globvarht);
279
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100280 // Script-local variables. Clear all the variables here.
281 // The scriptvar_T is cleared later in free_scriptnames(), because a
282 // variable in one script might hold a reference to the whole scope of
283 // another script.
284 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200285 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200286}
287#endif
288
289 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200290garbage_collect_globvars(int copyID)
291{
292 return set_ref_in_ht(&globvarht, copyID, NULL);
293}
294
295 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200296garbage_collect_vimvars(int copyID)
297{
298 return set_ref_in_ht(&vimvarht, copyID, NULL);
299}
300
301 int
302garbage_collect_scriptvars(int copyID)
303{
304 int i;
305 int abort = FALSE;
306
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100307 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200308 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
309
310 return abort;
311}
312
313/*
314 * Set an internal variable to a string value. Creates the variable if it does
315 * not already exist.
316 */
317 void
318set_internal_string_var(char_u *name, char_u *value)
319{
320 char_u *val;
321 typval_T *tvp;
322
323 val = vim_strsave(value);
324 if (val != NULL)
325 {
326 tvp = alloc_string_tv(val);
327 if (tvp != NULL)
328 {
329 set_var(name, tvp, FALSE);
330 free_tv(tvp);
331 }
332 }
333}
334
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200335 int
336eval_charconvert(
337 char_u *enc_from,
338 char_u *enc_to,
339 char_u *fname_from,
340 char_u *fname_to)
341{
342 int err = FALSE;
343
344 set_vim_var_string(VV_CC_FROM, enc_from, -1);
345 set_vim_var_string(VV_CC_TO, enc_to, -1);
346 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
347 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
348 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
349 err = TRUE;
350 set_vim_var_string(VV_CC_FROM, NULL, -1);
351 set_vim_var_string(VV_CC_TO, NULL, -1);
352 set_vim_var_string(VV_FNAME_IN, NULL, -1);
353 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
354
355 if (err)
356 return FAIL;
357 return OK;
358}
359
360# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
361 int
362eval_printexpr(char_u *fname, char_u *args)
363{
364 int err = FALSE;
365
366 set_vim_var_string(VV_FNAME_IN, fname, -1);
367 set_vim_var_string(VV_CMDARG, args, -1);
368 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
369 err = TRUE;
370 set_vim_var_string(VV_FNAME_IN, NULL, -1);
371 set_vim_var_string(VV_CMDARG, NULL, -1);
372
373 if (err)
374 {
375 mch_remove(fname);
376 return FAIL;
377 }
378 return OK;
379}
380# endif
381
382# if defined(FEAT_DIFF) || defined(PROTO)
383 void
384eval_diff(
385 char_u *origfile,
386 char_u *newfile,
387 char_u *outfile)
388{
389 int err = FALSE;
390
391 set_vim_var_string(VV_FNAME_IN, origfile, -1);
392 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
393 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
394 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
395 set_vim_var_string(VV_FNAME_IN, NULL, -1);
396 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
397 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
398}
399
400 void
401eval_patch(
402 char_u *origfile,
403 char_u *difffile,
404 char_u *outfile)
405{
406 int err;
407
408 set_vim_var_string(VV_FNAME_IN, origfile, -1);
409 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
410 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
411 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
412 set_vim_var_string(VV_FNAME_IN, NULL, -1);
413 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
414 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
415}
416# endif
417
418#if defined(FEAT_SPELL) || defined(PROTO)
419/*
420 * Evaluate an expression to a list with suggestions.
421 * For the "expr:" part of 'spellsuggest'.
422 * Returns NULL when there is an error.
423 */
424 list_T *
425eval_spell_expr(char_u *badword, char_u *expr)
426{
427 typval_T save_val;
428 typval_T rettv;
429 list_T *list = NULL;
430 char_u *p = skipwhite(expr);
431
432 // Set "v:val" to the bad word.
433 prepare_vimvar(VV_VAL, &save_val);
434 set_vim_var_string(VV_VAL, badword, -1);
435 if (p_verbose == 0)
436 ++emsg_off;
437
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200438 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200439 {
440 if (rettv.v_type != VAR_LIST)
441 clear_tv(&rettv);
442 else
443 list = rettv.vval.v_list;
444 }
445
446 if (p_verbose == 0)
447 --emsg_off;
448 clear_tv(get_vim_var_tv(VV_VAL));
449 restore_vimvar(VV_VAL, &save_val);
450
451 return list;
452}
453
454/*
455 * "list" is supposed to contain two items: a word and a number. Return the
456 * word in "pp" and the number as the return value.
457 * Return -1 if anything isn't right.
458 * Used to get the good word and score from the eval_spell_expr() result.
459 */
460 int
461get_spellword(list_T *list, char_u **pp)
462{
463 listitem_T *li;
464
465 li = list->lv_first;
466 if (li == NULL)
467 return -1;
468 *pp = tv_get_string(&li->li_tv);
469
470 li = li->li_next;
471 if (li == NULL)
472 return -1;
473 return (int)tv_get_number(&li->li_tv);
474}
475#endif
476
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200477/*
478 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200479 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200480 * When not used yet add the variable to the v: hashtable.
481 */
482 void
483prepare_vimvar(int idx, typval_T *save_tv)
484{
485 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200486 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200487 if (vimvars[idx].vv_type == VAR_UNKNOWN)
488 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
489}
490
491/*
492 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200493 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200494 * When no longer defined, remove the variable from the v: hashtable.
495 */
496 void
497restore_vimvar(int idx, typval_T *save_tv)
498{
499 hashitem_T *hi;
500
501 vimvars[idx].vv_tv = *save_tv;
502 if (vimvars[idx].vv_type == VAR_UNKNOWN)
503 {
504 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
505 if (HASHITEM_EMPTY(hi))
506 internal_error("restore_vimvar()");
507 else
508 hash_remove(&vimvarht, hi);
509 }
510}
511
512/*
513 * List Vim variables.
514 */
515 static void
516list_vim_vars(int *first)
517{
518 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
519}
520
521/*
522 * List script-local variables, if there is a script.
523 */
524 static void
525list_script_vars(int *first)
526{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200527 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
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 Moolenaarc1ec0422020-09-09 22:27:58 +0200781 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200782 i = FAIL;
783 if (has_assign || concat)
784 {
785 op[0] = '=';
786 op[1] = NUL;
787 if (*expr != '=')
788 {
Bram Moolenaar122616d2020-08-21 21:32:50 +0200789 if (vim9script && (flags & LET_NO_COMMAND) == 0)
790 {
791 // +=, /=, etc. require an existing variable
792 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
793 i = FAIL;
794 }
795 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200796 {
797 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200798 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200799 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200800 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200801 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200802 ++len;
803 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200804 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200805 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200806 }
807 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200808 ++expr;
809
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200810 if (vim9script && (!VIM_ISWHITE(*argend)
811 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200812 {
813 vim_strncpy(op, expr - len, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200814 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200815 i = FAIL;
816 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200817
818 if (eap->skip)
819 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200820 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200821 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200822 if (getline_equal(eap->getline, eap->cookie, getsourceline))
823 {
824 evalarg.eval_getline = eap->getline;
825 evalarg.eval_cookie = eap->cookie;
826 }
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200827 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200828 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200829 if (eap->skip)
830 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200831 clear_evalarg(&evalarg, eap);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200832 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200833 if (eap->skip)
834 {
835 if (i != FAIL)
836 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200837 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200838 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200839 {
840 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200841 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200842 clear_tv(&rettv);
843 }
844 }
845}
846
847/*
848 * Assign the typevalue "tv" to the variable or variables at "arg_start".
849 * Handles both "var" with any type and "[var, var; var]" with a list type.
850 * When "op" is not NULL it points to a string with characters that
851 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
852 * or concatenate.
853 * Returns OK or FAIL;
854 */
855 int
856ex_let_vars(
857 char_u *arg_start,
858 typval_T *tv,
859 int copy, // copy values from "tv", don't move
860 int semicolon, // from skip_var_list()
861 int var_count, // from skip_var_list()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200863 char_u *op)
864{
865 char_u *arg = arg_start;
866 list_T *l;
867 int i;
868 listitem_T *item;
869 typval_T ltv;
870
871 if (*arg != '[')
872 {
873 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200875 return FAIL;
876 return OK;
877 }
878
879 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
880 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
881 {
882 emsg(_(e_listreq));
883 return FAIL;
884 }
885
886 i = list_len(l);
887 if (semicolon == 0 && var_count < i)
888 {
889 emsg(_("E687: Less targets than List items"));
890 return FAIL;
891 }
892 if (var_count - semicolon > i)
893 {
894 emsg(_("E688: More targets than List items"));
895 return FAIL;
896 }
897
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200898 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200899 item = l->lv_first;
900 while (*arg != ']')
901 {
902 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100903 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200904 item = item->li_next;
905 if (arg == NULL)
906 return FAIL;
907
908 arg = skipwhite(arg);
909 if (*arg == ';')
910 {
911 // Put the rest of the list (may be empty) in the var after ';'.
912 // Create a new list for this.
913 l = list_alloc();
914 if (l == NULL)
915 return FAIL;
916 while (item != NULL)
917 {
918 list_append_tv(l, &item->li_tv);
919 item = item->li_next;
920 }
921
922 ltv.v_type = VAR_LIST;
923 ltv.v_lock = 0;
924 ltv.vval.v_list = l;
925 l->lv_refcount = 1;
926
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200928 (char_u *)"]", op);
929 clear_tv(&ltv);
930 if (arg == NULL)
931 return FAIL;
932 break;
933 }
934 else if (*arg != ',' && *arg != ']')
935 {
936 internal_error("ex_let_vars()");
937 return FAIL;
938 }
939 }
940
941 return OK;
942}
943
944/*
945 * Skip over assignable variable "var" or list of variables "[var, var]".
946 * Used for ":let varvar = expr" and ":for varvar in expr".
947 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200948 * for "[var, var; var]" set "semicolon" to 1.
949 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200950 * Return NULL for an error.
951 */
952 char_u *
953skip_var_list(
954 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200956 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200957 int *semicolon,
958 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200959{
960 char_u *p, *s;
961
962 if (*arg == '[')
963 {
964 // "[var, var]": find the matching ']'.
965 p = arg;
966 for (;;)
967 {
968 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200969 s = skip_var_one(p, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200970 if (s == p)
971 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200972 if (!silent)
973 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200974 return NULL;
975 }
976 ++*var_count;
977
978 p = skipwhite(s);
979 if (*p == ']')
980 break;
981 else if (*p == ';')
982 {
983 if (*semicolon == 1)
984 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200985 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200986 return NULL;
987 }
988 *semicolon = 1;
989 }
990 else if (*p != ',')
991 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200992 if (!silent)
993 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200994 return NULL;
995 }
996 }
997 return p + 1;
998 }
999 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001000 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001001}
1002
1003/*
1004 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1005 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001006 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001007 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001008 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001009skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001010{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011 char_u *end;
1012
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001013 if (*arg == '@' && arg[1] != NUL)
1014 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001015 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001016 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001017 if (include_type && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001018 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001019 // "a: type" is declaring variable "a" with a type, not "a:".
1020 if (end == arg + 2 && end[-1] == ':')
1021 --end;
1022 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001023 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001024 }
1025 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001026}
1027
1028/*
1029 * List variables for hashtab "ht" with prefix "prefix".
1030 * If "empty" is TRUE also list NULL strings as empty strings.
1031 */
1032 void
1033list_hashtable_vars(
1034 hashtab_T *ht,
1035 char *prefix,
1036 int empty,
1037 int *first)
1038{
1039 hashitem_T *hi;
1040 dictitem_T *di;
1041 int todo;
1042 char_u buf[IOSIZE];
1043
1044 todo = (int)ht->ht_used;
1045 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1046 {
1047 if (!HASHITEM_EMPTY(hi))
1048 {
1049 --todo;
1050 di = HI2DI(hi);
1051
1052 // apply :filter /pat/ to variable name
1053 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1054 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1055 if (message_filtered(buf))
1056 continue;
1057
1058 if (empty || di->di_tv.v_type != VAR_STRING
1059 || di->di_tv.vval.v_string != NULL)
1060 list_one_var(di, prefix, first);
1061 }
1062 }
1063}
1064
1065/*
1066 * List global variables.
1067 */
1068 static void
1069list_glob_vars(int *first)
1070{
1071 list_hashtable_vars(&globvarht, "", TRUE, first);
1072}
1073
1074/*
1075 * List buffer variables.
1076 */
1077 static void
1078list_buf_vars(int *first)
1079{
1080 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1081}
1082
1083/*
1084 * List window variables.
1085 */
1086 static void
1087list_win_vars(int *first)
1088{
1089 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1090}
1091
1092/*
1093 * List tab page variables.
1094 */
1095 static void
1096list_tab_vars(int *first)
1097{
1098 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1099}
1100
1101/*
1102 * List variables in "arg".
1103 */
1104 static char_u *
1105list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1106{
1107 int error = FALSE;
1108 int len;
1109 char_u *name;
1110 char_u *name_start;
1111 char_u *arg_subsc;
1112 char_u *tofree;
1113 typval_T tv;
1114
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001115 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001116 {
1117 if (error || eap->skip)
1118 {
1119 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1120 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1121 {
1122 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001123 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001124 break;
1125 }
1126 }
1127 else
1128 {
1129 // get_name_len() takes care of expanding curly braces
1130 name_start = name = arg;
1131 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1132 if (len <= 0)
1133 {
1134 // This is mainly to keep test 49 working: when expanding
1135 // curly braces fails overrule the exception error message.
1136 if (len < 0 && !aborting())
1137 {
1138 emsg_severe = TRUE;
1139 semsg(_(e_invarg2), arg);
1140 break;
1141 }
1142 error = TRUE;
1143 }
1144 else
1145 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001146 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001147 if (tofree != NULL)
1148 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001149 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001150 error = TRUE;
1151 else
1152 {
1153 // handle d.key, l[idx], f(expr)
1154 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001155 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001156 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001157 error = TRUE;
1158 else
1159 {
1160 if (arg == arg_subsc && len == 2 && name[1] == ':')
1161 {
1162 switch (*name)
1163 {
1164 case 'g': list_glob_vars(first); break;
1165 case 'b': list_buf_vars(first); break;
1166 case 'w': list_win_vars(first); break;
1167 case 't': list_tab_vars(first); break;
1168 case 'v': list_vim_vars(first); break;
1169 case 's': list_script_vars(first); break;
1170 case 'l': list_func_vars(first); break;
1171 default:
1172 semsg(_("E738: Can't list variables for %s"), name);
1173 }
1174 }
1175 else
1176 {
1177 char_u numbuf[NUMBUFLEN];
1178 char_u *tf;
1179 int c;
1180 char_u *s;
1181
1182 s = echo_string(&tv, &tf, numbuf, 0);
1183 c = *arg;
1184 *arg = NUL;
1185 list_one_var_a("",
1186 arg == arg_subsc ? name : name_start,
1187 tv.v_type,
1188 s == NULL ? (char_u *)"" : s,
1189 first);
1190 *arg = c;
1191 vim_free(tf);
1192 }
1193 clear_tv(&tv);
1194 }
1195 }
1196 }
1197
1198 vim_free(tofree);
1199 }
1200
1201 arg = skipwhite(arg);
1202 }
1203
1204 return arg;
1205}
1206
1207/*
1208 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1209 * Returns a pointer to the char just after the var name.
1210 * Returns NULL if there is an error.
1211 */
1212 static char_u *
1213ex_let_one(
1214 char_u *arg, // points to variable name
1215 typval_T *tv, // value to assign to variable
1216 int copy, // copy value from "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001218 char_u *endchars, // valid chars after variable name or NULL
1219 char_u *op) // "+", "-", "." or NULL
1220{
1221 int c1;
1222 char_u *name;
1223 char_u *p;
1224 char_u *arg_end = NULL;
1225 int len;
1226 int opt_flags;
1227 char_u *tofree = NULL;
1228
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001229 if (in_vim9script() && (flags & LET_NO_COMMAND) == 0
1230 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1231 {
1232 vim9_declare_error(arg);
1233 return NULL;
1234 }
1235
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001236 // ":let $VAR = expr": Set environment variable.
1237 if (*arg == '$')
1238 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001240 {
1241 emsg(_("E996: Cannot lock an environment variable"));
1242 return NULL;
1243 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001244
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001245 // Find the end of the name.
1246 ++arg;
1247 name = arg;
1248 len = get_env_len(&arg);
1249 if (len == 0)
1250 semsg(_(e_invarg2), name - 1);
1251 else
1252 {
1253 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1254 semsg(_(e_letwrong), op);
1255 else if (endchars != NULL
1256 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1257 emsg(_(e_letunexp));
1258 else if (!check_secure())
1259 {
1260 c1 = name[len];
1261 name[len] = NUL;
1262 p = tv_get_string_chk(tv);
1263 if (p != NULL && op != NULL && *op == '.')
1264 {
1265 int mustfree = FALSE;
1266 char_u *s = vim_getenv(name, &mustfree);
1267
1268 if (s != NULL)
1269 {
1270 p = tofree = concat_str(s, p);
1271 if (mustfree)
1272 vim_free(s);
1273 }
1274 }
1275 if (p != NULL)
1276 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001277 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001278 arg_end = arg;
1279 }
1280 name[len] = c1;
1281 vim_free(tofree);
1282 }
1283 }
1284 }
1285
1286 // ":let &option = expr": Set option value.
1287 // ":let &l:option = expr": Set local option value.
1288 // ":let &g:option = expr": Set global option value.
1289 else if (*arg == '&')
1290 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001292 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001293 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001294 return NULL;
1295 }
1296 // Find the end of the name.
1297 p = find_option_end(&arg, &opt_flags);
1298 if (p == NULL || (endchars != NULL
1299 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1300 emsg(_(e_letunexp));
1301 else
1302 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001303 long n = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001304 int opt_type;
1305 long numval;
1306 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001307 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001308 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001309
1310 c1 = *p;
1311 *p = NUL;
1312
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001313 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
1314 if ((opt_type == 1 || opt_type == -1)
1315 && (tv->v_type != VAR_STRING || !in_vim9script()))
1316 // number, possibly hidden
1317 n = (long)tv_get_number(tv);
1318
1319 // Avoid setting a string option to the text "v:false" or similar.
1320 // In Vim9 script also don't convert a number to string.
1321 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1322 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1323 s = tv_get_string_chk(tv);
1324
1325 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001326 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001327 if ((opt_type == 1 && *op == '.')
1328 || (opt_type == 0 && *op != '.'))
1329 {
1330 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001331 failed = TRUE; // don't set the value
1332
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001333 }
1334 else
1335 {
1336 if (opt_type == 1) // number
1337 {
1338 switch (*op)
1339 {
1340 case '+': n = numval + n; break;
1341 case '-': n = numval - n; break;
1342 case '*': n = numval * n; break;
1343 case '/': n = (long)num_divide(numval, n); break;
1344 case '%': n = (long)num_modulus(numval, n); break;
1345 }
1346 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001347 else if (opt_type == 0 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001348 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001349 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001350 s = concat_str(stringval, s);
1351 vim_free(stringval);
1352 stringval = s;
1353 }
1354 }
1355 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001356
1357 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001358 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001359 if (opt_type != 0 || s != NULL)
1360 {
1361 set_option_value(arg, n, s, opt_flags);
1362 arg_end = p;
1363 }
1364 else
1365 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001366 }
1367 *p = c1;
1368 vim_free(stringval);
1369 }
1370 }
1371
1372 // ":let @r = expr": Set register contents.
1373 else if (*arg == '@')
1374 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001375 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001376 {
1377 emsg(_("E996: Cannot lock a register"));
1378 return NULL;
1379 }
1380 ++arg;
1381 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1382 semsg(_(e_letwrong), op);
1383 else if (endchars != NULL
1384 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1385 emsg(_(e_letunexp));
1386 else
1387 {
1388 char_u *ptofree = NULL;
1389 char_u *s;
1390
1391 p = tv_get_string_chk(tv);
1392 if (p != NULL && op != NULL && *op == '.')
1393 {
1394 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1395 if (s != NULL)
1396 {
1397 p = ptofree = concat_str(s, p);
1398 vim_free(s);
1399 }
1400 }
1401 if (p != NULL)
1402 {
1403 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1404 arg_end = arg + 1;
1405 }
1406 vim_free(ptofree);
1407 }
1408 }
1409
1410 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001412 // ":let {expr} = expr": Idem, name made with curly braces
1413 else if (eval_isnamec1(*arg) || *arg == '{')
1414 {
1415 lval_T lv;
1416
1417 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001418 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001419 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420 if (endchars != NULL && vim_strchr(endchars,
1421 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001422 emsg(_(e_letunexp));
1423 else
1424 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001426 arg_end = p;
1427 }
1428 }
1429 clear_lval(&lv);
1430 }
1431
1432 else
1433 semsg(_(e_invarg2), arg);
1434
1435 return arg_end;
1436}
1437
1438/*
1439 * ":unlet[!] var1 ... " command.
1440 */
1441 void
1442ex_unlet(exarg_T *eap)
1443{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001444 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001445}
1446
1447/*
1448 * ":lockvar" and ":unlockvar" commands
1449 */
1450 void
1451ex_lockvar(exarg_T *eap)
1452{
1453 char_u *arg = eap->arg;
1454 int deep = 2;
1455
1456 if (eap->forceit)
1457 deep = -1;
1458 else if (vim_isdigit(*arg))
1459 {
1460 deep = getdigits(&arg);
1461 arg = skipwhite(arg);
1462 }
1463
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001464 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001465}
1466
1467/*
1468 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001469 * Also used for Vim9 script. "callback" is invoked as:
1470 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001471 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001472 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001473ex_unletlock(
1474 exarg_T *eap,
1475 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001476 int deep,
1477 int glv_flags,
1478 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1479 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001480{
1481 char_u *arg = argstart;
1482 char_u *name_end;
1483 int error = FALSE;
1484 lval_T lv;
1485
1486 do
1487 {
1488 if (*arg == '$')
1489 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001490 lv.ll_name = arg;
1491 lv.ll_tv = NULL;
1492 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001493 if (get_env_len(&arg) == 0)
1494 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001495 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001496 return;
1497 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001498 if (!error && !eap->skip
1499 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1500 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001501 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001502 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001503 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001504 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001505 // Parse the name and find the end.
1506 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1507 glv_flags, FNE_CHECK_START);
1508 if (lv.ll_name == NULL)
1509 error = TRUE; // error but continue parsing
1510 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1511 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001512 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001513 if (name_end != NULL)
1514 {
1515 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001516 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001517 }
1518 if (!(eap->skip || error))
1519 clear_lval(&lv);
1520 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001521 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001522
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001523 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001524 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001525 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001526
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001527 if (!eap->skip)
1528 clear_lval(&lv);
1529 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001530
1531 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001532 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001533
1534 eap->nextcmd = check_nextcmd(arg);
1535}
1536
1537 static int
1538do_unlet_var(
1539 lval_T *lp,
1540 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001541 exarg_T *eap,
1542 int deep UNUSED,
1543 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001544{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001545 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001546 int ret = OK;
1547 int cc;
1548
1549 if (lp->ll_tv == NULL)
1550 {
1551 cc = *name_end;
1552 *name_end = NUL;
1553
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001554 // Environment variable, normal name or expanded name.
1555 if (*lp->ll_name == '$')
1556 vim_unsetenv(lp->ll_name + 1);
1557 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001558 ret = FAIL;
1559 *name_end = cc;
1560 }
1561 else if ((lp->ll_list != NULL
1562 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
1563 || (lp->ll_dict != NULL
1564 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
1565 return FAIL;
1566 else if (lp->ll_range)
1567 {
1568 listitem_T *li;
1569 listitem_T *ll_li = lp->ll_li;
1570 int ll_n1 = lp->ll_n1;
1571
1572 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1573 {
1574 li = ll_li->li_next;
1575 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
1576 return FAIL;
1577 ll_li = li;
1578 ++ll_n1;
1579 }
1580
1581 // Delete a range of List items.
1582 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1583 {
1584 li = lp->ll_li->li_next;
1585 listitem_remove(lp->ll_list, lp->ll_li);
1586 lp->ll_li = li;
1587 ++lp->ll_n1;
1588 }
1589 }
1590 else
1591 {
1592 if (lp->ll_list != NULL)
1593 // unlet a List item.
1594 listitem_remove(lp->ll_list, lp->ll_li);
1595 else
1596 // unlet a Dictionary item.
1597 dictitem_remove(lp->ll_dict, lp->ll_di);
1598 }
1599
1600 return ret;
1601}
1602
1603/*
1604 * "unlet" a variable. Return OK if it existed, FAIL if not.
1605 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1606 */
1607 int
1608do_unlet(char_u *name, int forceit)
1609{
1610 hashtab_T *ht;
1611 hashitem_T *hi;
1612 char_u *varname;
1613 dict_T *d;
1614 dictitem_T *di;
1615
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001616 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001617 return FAIL;
1618
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001619 ht = find_var_ht(name, &varname);
1620 if (ht != NULL && *varname != NUL)
1621 {
1622 d = get_current_funccal_dict(ht);
1623 if (d == NULL)
1624 {
1625 if (ht == &globvarht)
1626 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001627 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001628 d = &vimvardict;
1629 else
1630 {
1631 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1632 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1633 }
1634 if (d == NULL)
1635 {
1636 internal_error("do_unlet()");
1637 return FAIL;
1638 }
1639 }
1640 hi = hash_find(ht, varname);
1641 if (HASHITEM_EMPTY(hi))
1642 hi = find_hi_in_scoped_ht(name, &ht);
1643 if (hi != NULL && !HASHITEM_EMPTY(hi))
1644 {
1645 di = HI2DI(hi);
1646 if (var_check_fixed(di->di_flags, name, FALSE)
1647 || var_check_ro(di->di_flags, name, FALSE)
1648 || var_check_lock(d->dv_lock, name, FALSE))
1649 return FAIL;
1650
1651 delete_var(ht, hi);
1652 return OK;
1653 }
1654 }
1655 if (forceit)
1656 return OK;
1657 semsg(_("E108: No such variable: \"%s\""), name);
1658 return FAIL;
1659}
1660
1661/*
1662 * Lock or unlock variable indicated by "lp".
1663 * "deep" is the levels to go (-1 for unlimited);
1664 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1665 */
1666 static int
1667do_lock_var(
1668 lval_T *lp,
1669 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001670 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001671 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001672 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001673{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001674 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001675 int ret = OK;
1676 int cc;
1677 dictitem_T *di;
1678
1679 if (deep == 0) // nothing to do
1680 return OK;
1681
1682 if (lp->ll_tv == NULL)
1683 {
1684 cc = *name_end;
1685 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001686 if (*lp->ll_name == '$')
1687 {
1688 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001689 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001690 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001691 else
1692 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001693 // Normal name or expanded name.
1694 di = find_var(lp->ll_name, NULL, TRUE);
1695 if (di == NULL)
1696 ret = FAIL;
1697 else if ((di->di_flags & DI_FLAGS_FIX)
1698 && di->di_tv.v_type != VAR_DICT
1699 && di->di_tv.v_type != VAR_LIST)
1700 {
1701 // For historic reasons this error is not given for a list or
1702 // dict. E.g., the b: dict could be locked/unlocked.
1703 semsg(_(e_lock_unlock), lp->ll_name);
1704 ret = FAIL;
1705 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001706 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001707 {
1708 if (lock)
1709 di->di_flags |= DI_FLAGS_LOCK;
1710 else
1711 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001712 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001713 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001714 }
1715 *name_end = cc;
1716 }
1717 else if (lp->ll_range)
1718 {
1719 listitem_T *li = lp->ll_li;
1720
1721 // (un)lock a range of List items.
1722 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1723 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001724 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001725 li = li->li_next;
1726 ++lp->ll_n1;
1727 }
1728 }
1729 else if (lp->ll_list != NULL)
1730 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001731 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001732 else
1733 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001734 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001735
1736 return ret;
1737}
1738
1739/*
1740 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001741 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1742 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001743 */
1744 static void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001745item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001746{
1747 static int recurse = 0;
1748 list_T *l;
1749 listitem_T *li;
1750 dict_T *d;
1751 blob_T *b;
1752 hashitem_T *hi;
1753 int todo;
1754
1755 if (recurse >= DICT_MAXNEST)
1756 {
1757 emsg(_("E743: variable nested too deep for (un)lock"));
1758 return;
1759 }
1760 if (deep == 0)
1761 return;
1762 ++recurse;
1763
1764 // lock/unlock the item itself
1765 if (lock)
1766 tv->v_lock |= VAR_LOCKED;
1767 else
1768 tv->v_lock &= ~VAR_LOCKED;
1769
1770 switch (tv->v_type)
1771 {
1772 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001773 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001775 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001777 case VAR_STRING:
1778 case VAR_FUNC:
1779 case VAR_PARTIAL:
1780 case VAR_FLOAT:
1781 case VAR_SPECIAL:
1782 case VAR_JOB:
1783 case VAR_CHANNEL:
1784 break;
1785
1786 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001787 if ((b = tv->vval.v_blob) != NULL
1788 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001789 {
1790 if (lock)
1791 b->bv_lock |= VAR_LOCKED;
1792 else
1793 b->bv_lock &= ~VAR_LOCKED;
1794 }
1795 break;
1796 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001797 if ((l = tv->vval.v_list) != NULL
1798 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001799 {
1800 if (lock)
1801 l->lv_lock |= VAR_LOCKED;
1802 else
1803 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001804 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001805 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001806 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001807 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001808 }
1809 break;
1810 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001811 if ((d = tv->vval.v_dict) != NULL
1812 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001813 {
1814 if (lock)
1815 d->dv_lock |= VAR_LOCKED;
1816 else
1817 d->dv_lock &= ~VAR_LOCKED;
1818 if (deep < 0 || deep > 1)
1819 {
1820 // recursive: lock/unlock the items the List contains
1821 todo = (int)d->dv_hashtab.ht_used;
1822 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1823 {
1824 if (!HASHITEM_EMPTY(hi))
1825 {
1826 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001827 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1828 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001829 }
1830 }
1831 }
1832 }
1833 }
1834 --recurse;
1835}
1836
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001837#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1838/*
1839 * Delete all "menutrans_" variables.
1840 */
1841 void
1842del_menutrans_vars(void)
1843{
1844 hashitem_T *hi;
1845 int todo;
1846
1847 hash_lock(&globvarht);
1848 todo = (int)globvarht.ht_used;
1849 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1850 {
1851 if (!HASHITEM_EMPTY(hi))
1852 {
1853 --todo;
1854 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1855 delete_var(&globvarht, hi);
1856 }
1857 }
1858 hash_unlock(&globvarht);
1859}
1860#endif
1861
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001862/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001863 * Local string buffer for the next two functions to store a variable name
1864 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1865 * get_user_var_name().
1866 */
1867
1868static char_u *varnamebuf = NULL;
1869static int varnamebuflen = 0;
1870
1871/*
1872 * Function to concatenate a prefix and a variable name.
1873 */
1874 static char_u *
1875cat_prefix_varname(int prefix, char_u *name)
1876{
1877 int len;
1878
1879 len = (int)STRLEN(name) + 3;
1880 if (len > varnamebuflen)
1881 {
1882 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001883 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001884 varnamebuf = alloc(len);
1885 if (varnamebuf == NULL)
1886 {
1887 varnamebuflen = 0;
1888 return NULL;
1889 }
1890 varnamebuflen = len;
1891 }
1892 *varnamebuf = prefix;
1893 varnamebuf[1] = ':';
1894 STRCPY(varnamebuf + 2, name);
1895 return varnamebuf;
1896}
1897
1898/*
1899 * Function given to ExpandGeneric() to obtain the list of user defined
1900 * (global/buffer/window/built-in) variable names.
1901 */
1902 char_u *
1903get_user_var_name(expand_T *xp, int idx)
1904{
1905 static long_u gdone;
1906 static long_u bdone;
1907 static long_u wdone;
1908 static long_u tdone;
1909 static int vidx;
1910 static hashitem_T *hi;
1911 hashtab_T *ht;
1912
1913 if (idx == 0)
1914 {
1915 gdone = bdone = wdone = vidx = 0;
1916 tdone = 0;
1917 }
1918
1919 // Global variables
1920 if (gdone < globvarht.ht_used)
1921 {
1922 if (gdone++ == 0)
1923 hi = globvarht.ht_array;
1924 else
1925 ++hi;
1926 while (HASHITEM_EMPTY(hi))
1927 ++hi;
1928 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1929 return cat_prefix_varname('g', hi->hi_key);
1930 return hi->hi_key;
1931 }
1932
1933 // b: variables
1934 ht = &curbuf->b_vars->dv_hashtab;
1935 if (bdone < ht->ht_used)
1936 {
1937 if (bdone++ == 0)
1938 hi = ht->ht_array;
1939 else
1940 ++hi;
1941 while (HASHITEM_EMPTY(hi))
1942 ++hi;
1943 return cat_prefix_varname('b', hi->hi_key);
1944 }
1945
1946 // w: variables
1947 ht = &curwin->w_vars->dv_hashtab;
1948 if (wdone < ht->ht_used)
1949 {
1950 if (wdone++ == 0)
1951 hi = ht->ht_array;
1952 else
1953 ++hi;
1954 while (HASHITEM_EMPTY(hi))
1955 ++hi;
1956 return cat_prefix_varname('w', hi->hi_key);
1957 }
1958
1959 // t: variables
1960 ht = &curtab->tp_vars->dv_hashtab;
1961 if (tdone < ht->ht_used)
1962 {
1963 if (tdone++ == 0)
1964 hi = ht->ht_array;
1965 else
1966 ++hi;
1967 while (HASHITEM_EMPTY(hi))
1968 ++hi;
1969 return cat_prefix_varname('t', hi->hi_key);
1970 }
1971
1972 // v: variables
1973 if (vidx < VV_LEN)
1974 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
1975
1976 VIM_CLEAR(varnamebuf);
1977 varnamebuflen = 0;
1978 return NULL;
1979}
1980
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001981 char *
1982get_var_special_name(int nr)
1983{
1984 switch (nr)
1985 {
1986 case VVAL_FALSE: return "v:false";
1987 case VVAL_TRUE: return "v:true";
1988 case VVAL_NONE: return "v:none";
1989 case VVAL_NULL: return "v:null";
1990 }
1991 internal_error("get_var_special_name()");
1992 return "42";
1993}
1994
1995/*
1996 * Returns the global variable dictionary
1997 */
1998 dict_T *
1999get_globvar_dict(void)
2000{
2001 return &globvardict;
2002}
2003
2004/*
2005 * Returns the global variable hash table
2006 */
2007 hashtab_T *
2008get_globvar_ht(void)
2009{
2010 return &globvarht;
2011}
2012
2013/*
2014 * Returns the v: variable dictionary
2015 */
2016 dict_T *
2017get_vimvar_dict(void)
2018{
2019 return &vimvardict;
2020}
2021
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002022/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002024 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 */
2026 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002027find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002028{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002029 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2030 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031
2032 if (di == NULL)
2033 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002034 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2036 return (int)(vv - vimvars);
2037}
2038
2039
2040/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002041 * Set type of v: variable to "type".
2042 */
2043 void
2044set_vim_var_type(int idx, vartype_T type)
2045{
2046 vimvars[idx].vv_type = type;
2047}
2048
2049/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002050 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002051 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002052 */
2053 void
2054set_vim_var_nr(int idx, varnumber_T val)
2055{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002056 vimvars[idx].vv_nr = val;
2057}
2058
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002059 char *
2060get_vim_var_name(int idx)
2061{
2062 return vimvars[idx].vv_name;
2063}
2064
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002065/*
2066 * Get typval_T v: variable value.
2067 */
2068 typval_T *
2069get_vim_var_tv(int idx)
2070{
2071 return &vimvars[idx].vv_tv;
2072}
2073
2074/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002075 * Set v: variable to "tv". Only accepts the same type.
2076 * Takes over the value of "tv".
2077 */
2078 int
2079set_vim_var_tv(int idx, typval_T *tv)
2080{
2081 if (vimvars[idx].vv_type != tv->v_type)
2082 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002083 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002084 clear_tv(tv);
2085 return FAIL;
2086 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002087 // VV_RO is also checked when compiling, but let's check here as well.
2088 if (vimvars[idx].vv_flags & VV_RO)
2089 {
2090 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2091 return FAIL;
2092 }
2093 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2094 {
2095 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2096 return FAIL;
2097 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002098 clear_tv(&vimvars[idx].vv_di.di_tv);
2099 vimvars[idx].vv_di.di_tv = *tv;
2100 return OK;
2101}
2102
2103/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002104 * Get number v: variable value.
2105 */
2106 varnumber_T
2107get_vim_var_nr(int idx)
2108{
2109 return vimvars[idx].vv_nr;
2110}
2111
2112/*
2113 * Get string v: variable value. Uses a static buffer, can only be used once.
2114 * If the String variable has never been set, return an empty string.
2115 * Never returns NULL;
2116 */
2117 char_u *
2118get_vim_var_str(int idx)
2119{
2120 return tv_get_string(&vimvars[idx].vv_tv);
2121}
2122
2123/*
2124 * Get List v: variable value. Caller must take care of reference count when
2125 * needed.
2126 */
2127 list_T *
2128get_vim_var_list(int idx)
2129{
2130 return vimvars[idx].vv_list;
2131}
2132
2133/*
2134 * Get Dict v: variable value. Caller must take care of reference count when
2135 * needed.
2136 */
2137 dict_T *
2138get_vim_var_dict(int idx)
2139{
2140 return vimvars[idx].vv_dict;
2141}
2142
2143/*
2144 * Set v:char to character "c".
2145 */
2146 void
2147set_vim_var_char(int c)
2148{
2149 char_u buf[MB_MAXBYTES + 1];
2150
2151 if (has_mbyte)
2152 buf[(*mb_char2bytes)(c, buf)] = NUL;
2153 else
2154 {
2155 buf[0] = c;
2156 buf[1] = NUL;
2157 }
2158 set_vim_var_string(VV_CHAR, buf, -1);
2159}
2160
2161/*
2162 * Set v:count to "count" and v:count1 to "count1".
2163 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2164 */
2165 void
2166set_vcount(
2167 long count,
2168 long count1,
2169 int set_prevcount)
2170{
2171 if (set_prevcount)
2172 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2173 vimvars[VV_COUNT].vv_nr = count;
2174 vimvars[VV_COUNT1].vv_nr = count1;
2175}
2176
2177/*
2178 * Save variables that might be changed as a side effect. Used when executing
2179 * a timer callback.
2180 */
2181 void
2182save_vimvars(vimvars_save_T *vvsave)
2183{
2184 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2185 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2186 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2187}
2188
2189/*
2190 * Restore variables saved by save_vimvars().
2191 */
2192 void
2193restore_vimvars(vimvars_save_T *vvsave)
2194{
2195 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2196 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2197 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2198}
2199
2200/*
2201 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2202 * value.
2203 */
2204 void
2205set_vim_var_string(
2206 int idx,
2207 char_u *val,
2208 int len) // length of "val" to use or -1 (whole string)
2209{
2210 clear_tv(&vimvars[idx].vv_di.di_tv);
2211 vimvars[idx].vv_type = VAR_STRING;
2212 if (val == NULL)
2213 vimvars[idx].vv_str = NULL;
2214 else if (len == -1)
2215 vimvars[idx].vv_str = vim_strsave(val);
2216 else
2217 vimvars[idx].vv_str = vim_strnsave(val, len);
2218}
2219
2220/*
2221 * Set List v: variable to "val".
2222 */
2223 void
2224set_vim_var_list(int idx, list_T *val)
2225{
2226 clear_tv(&vimvars[idx].vv_di.di_tv);
2227 vimvars[idx].vv_type = VAR_LIST;
2228 vimvars[idx].vv_list = val;
2229 if (val != NULL)
2230 ++val->lv_refcount;
2231}
2232
2233/*
2234 * Set Dictionary v: variable to "val".
2235 */
2236 void
2237set_vim_var_dict(int idx, dict_T *val)
2238{
2239 clear_tv(&vimvars[idx].vv_di.di_tv);
2240 vimvars[idx].vv_type = VAR_DICT;
2241 vimvars[idx].vv_dict = val;
2242 if (val != NULL)
2243 {
2244 ++val->dv_refcount;
2245 dict_set_items_ro(val);
2246 }
2247}
2248
2249/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002250 * Set the v:argv list.
2251 */
2252 void
2253set_argv_var(char **argv, int argc)
2254{
2255 list_T *l = list_alloc();
2256 int i;
2257
2258 if (l == NULL)
2259 getout(1);
2260 l->lv_lock = VAR_FIXED;
2261 for (i = 0; i < argc; ++i)
2262 {
2263 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2264 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002265 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002266 }
2267 set_vim_var_list(VV_ARGV, l);
2268}
2269
2270/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002271 * Reset v:register, taking the 'clipboard' setting into account.
2272 */
2273 void
2274reset_reg_var(void)
2275{
2276 int regname = 0;
2277
2278 // Adjust the register according to 'clipboard', so that when
2279 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2280#ifdef FEAT_CLIPBOARD
2281 adjust_clip_reg(&regname);
2282#endif
2283 set_reg_var(regname);
2284}
2285
2286/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002287 * Set v:register if needed.
2288 */
2289 void
2290set_reg_var(int c)
2291{
2292 char_u regname;
2293
2294 if (c == 0 || c == ' ')
2295 regname = '"';
2296 else
2297 regname = c;
2298 // Avoid free/alloc when the value is already right.
2299 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2300 set_vim_var_string(VV_REG, &regname, 1);
2301}
2302
2303/*
2304 * Get or set v:exception. If "oldval" == NULL, return the current value.
2305 * Otherwise, restore the value to "oldval" and return NULL.
2306 * Must always be called in pairs to save and restore v:exception! Does not
2307 * take care of memory allocations.
2308 */
2309 char_u *
2310v_exception(char_u *oldval)
2311{
2312 if (oldval == NULL)
2313 return vimvars[VV_EXCEPTION].vv_str;
2314
2315 vimvars[VV_EXCEPTION].vv_str = oldval;
2316 return NULL;
2317}
2318
2319/*
2320 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2321 * Otherwise, restore the value to "oldval" and return NULL.
2322 * Must always be called in pairs to save and restore v:throwpoint! Does not
2323 * take care of memory allocations.
2324 */
2325 char_u *
2326v_throwpoint(char_u *oldval)
2327{
2328 if (oldval == NULL)
2329 return vimvars[VV_THROWPOINT].vv_str;
2330
2331 vimvars[VV_THROWPOINT].vv_str = oldval;
2332 return NULL;
2333}
2334
2335/*
2336 * Set v:cmdarg.
2337 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2338 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2339 * Must always be called in pairs!
2340 */
2341 char_u *
2342set_cmdarg(exarg_T *eap, char_u *oldarg)
2343{
2344 char_u *oldval;
2345 char_u *newval;
2346 unsigned len;
2347
2348 oldval = vimvars[VV_CMDARG].vv_str;
2349 if (eap == NULL)
2350 {
2351 vim_free(oldval);
2352 vimvars[VV_CMDARG].vv_str = oldarg;
2353 return NULL;
2354 }
2355
2356 if (eap->force_bin == FORCE_BIN)
2357 len = 6;
2358 else if (eap->force_bin == FORCE_NOBIN)
2359 len = 8;
2360 else
2361 len = 0;
2362
2363 if (eap->read_edit)
2364 len += 7;
2365
2366 if (eap->force_ff != 0)
2367 len += 10; // " ++ff=unix"
2368 if (eap->force_enc != 0)
2369 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2370 if (eap->bad_char != 0)
2371 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2372
2373 newval = alloc(len + 1);
2374 if (newval == NULL)
2375 return NULL;
2376
2377 if (eap->force_bin == FORCE_BIN)
2378 sprintf((char *)newval, " ++bin");
2379 else if (eap->force_bin == FORCE_NOBIN)
2380 sprintf((char *)newval, " ++nobin");
2381 else
2382 *newval = NUL;
2383
2384 if (eap->read_edit)
2385 STRCAT(newval, " ++edit");
2386
2387 if (eap->force_ff != 0)
2388 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2389 eap->force_ff == 'u' ? "unix"
2390 : eap->force_ff == 'd' ? "dos"
2391 : "mac");
2392 if (eap->force_enc != 0)
2393 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2394 eap->cmd + eap->force_enc);
2395 if (eap->bad_char == BAD_KEEP)
2396 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2397 else if (eap->bad_char == BAD_DROP)
2398 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2399 else if (eap->bad_char != 0)
2400 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2401 vimvars[VV_CMDARG].vv_str = newval;
2402 return oldval;
2403}
2404
2405/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002406 * Get the value of internal variable "name".
2407 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2408 */
2409 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002410eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002411 char_u *name,
2412 int len, // length of "name"
2413 typval_T *rettv, // NULL when only checking existence
2414 dictitem_T **dip, // non-NULL when typval's dict item is needed
2415 int verbose, // may give error message
2416 int no_autoload) // do not use script autoloading
2417{
2418 int ret = OK;
2419 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002420 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002421 dictitem_T *v;
2422 int cc;
2423
2424 // truncate the name, so that we can use strcmp()
2425 cc = name[len];
2426 name[len] = NUL;
2427
2428 // Check for user-defined variables.
2429 v = find_var(name, NULL, no_autoload);
2430 if (v != NULL)
2431 {
2432 tv = &v->di_tv;
2433 if (dip != NULL)
2434 *dip = v;
2435 }
2436
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002437 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002439 imported_T *import;
2440 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2441
2442 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443
2444 // imported variable from another script
2445 if (import != NULL)
2446 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002447 if (import->imp_funcname != NULL)
2448 {
2449 foundFunc = TRUE;
2450 if (rettv != NULL)
2451 {
2452 rettv->v_type = VAR_FUNC;
2453 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2454 }
2455 }
2456 else
2457 {
2458 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002459 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002460 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002461 tv = sv->sv_tv;
2462 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002464 else if (in_vim9script())
2465 {
2466 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2467
2468 if (ufunc != NULL)
2469 {
2470 foundFunc = TRUE;
2471 if (rettv != NULL)
2472 {
2473 rettv->v_type = VAR_FUNC;
2474 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2475 }
2476 }
2477 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478 }
2479
Bram Moolenaarc620c052020-07-08 15:16:19 +02002480 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002481 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002482 if (tv == NULL)
2483 {
2484 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002485 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002486 ret = FAIL;
2487 }
2488 else if (rettv != NULL)
2489 copy_tv(tv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002490 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002491
2492 name[len] = cc;
2493
2494 return ret;
2495}
2496
2497/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002498 * Check if variable "name[len]" is a local variable or an argument.
2499 * If so, "*eval_lavars_used" is set to TRUE.
2500 */
2501 void
2502check_vars(char_u *name, int len)
2503{
2504 int cc;
2505 char_u *varname;
2506 hashtab_T *ht;
2507
2508 if (eval_lavars_used == NULL)
2509 return;
2510
2511 // truncate the name, so that we can use strcmp()
2512 cc = name[len];
2513 name[len] = NUL;
2514
2515 ht = find_var_ht(name, &varname);
2516 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2517 {
2518 if (find_var(name, NULL, TRUE) != NULL)
2519 *eval_lavars_used = TRUE;
2520 }
2521
2522 name[len] = cc;
2523}
2524
2525/*
2526 * Find variable "name" in the list of variables.
2527 * Return a pointer to it if found, NULL if not found.
2528 * Careful: "a:0" variables don't have a name.
2529 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2530 * hashtab_T used.
2531 */
2532 dictitem_T *
2533find_var(char_u *name, hashtab_T **htp, int no_autoload)
2534{
2535 char_u *varname;
2536 hashtab_T *ht;
2537 dictitem_T *ret = NULL;
2538
2539 ht = find_var_ht(name, &varname);
2540 if (htp != NULL)
2541 *htp = ht;
2542 if (ht == NULL)
2543 return NULL;
2544 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2545 if (ret != NULL)
2546 return ret;
2547
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002548 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002549 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2550}
2551
2552/*
2553 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002554 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002555 * Returns NULL if not found.
2556 */
2557 dictitem_T *
2558find_var_in_ht(
2559 hashtab_T *ht,
2560 int htname,
2561 char_u *varname,
2562 int no_autoload)
2563{
2564 hashitem_T *hi;
2565
2566 if (*varname == NUL)
2567 {
2568 // Must be something like "s:", otherwise "ht" would be NULL.
2569 switch (htname)
2570 {
2571 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2572 case 'g': return &globvars_var;
2573 case 'v': return &vimvars_var;
2574 case 'b': return &curbuf->b_bufvar;
2575 case 'w': return &curwin->w_winvar;
2576 case 't': return &curtab->tp_winvar;
2577 case 'l': return get_funccal_local_var();
2578 case 'a': return get_funccal_args_var();
2579 }
2580 return NULL;
2581 }
2582
2583 hi = hash_find(ht, varname);
2584 if (HASHITEM_EMPTY(hi))
2585 {
2586 // For global variables we may try auto-loading the script. If it
2587 // worked find the variable again. Don't auto-load a script if it was
2588 // loaded already, otherwise it would be loaded every time when
2589 // checking if a function name is a Funcref variable.
2590 if (ht == &globvarht && !no_autoload)
2591 {
2592 // Note: script_autoload() may make "hi" invalid. It must either
2593 // be obtained again or not used.
2594 if (!script_autoload(varname, FALSE) || aborting())
2595 return NULL;
2596 hi = hash_find(ht, varname);
2597 }
2598 if (HASHITEM_EMPTY(hi))
2599 return NULL;
2600 }
2601 return HI2DI(hi);
2602}
2603
2604/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 * Get the script-local hashtab. NULL if not in a script context.
2606 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02002607 static hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608get_script_local_ht(void)
2609{
2610 scid_T sid = current_sctx.sc_sid;
2611
Bram Moolenaare3d46852020-08-29 13:39:17 +02002612 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002613 return &SCRIPT_VARS(sid);
2614 return NULL;
2615}
2616
2617/*
2618 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002619 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002620 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002621 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002622lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2623{
2624 hashtab_T *ht = get_script_local_ht();
2625 char_u buffer[30];
2626 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002627 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 hashitem_T *hi;
2629
2630 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002631 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 if (len < sizeof(buffer) - 1)
2633 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002634 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635 vim_strncpy(buffer, name, len);
2636 p = buffer;
2637 }
2638 else
2639 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002640 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002641 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002642 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643 }
2644
2645 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002646 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647
2648 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002649 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2650 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651
2652 if (p != buffer)
2653 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002654 // Don't return "buffer", gcc complains.
2655 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002656}
2657
2658/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002659 * Find the hashtab used for a variable name.
2660 * Return NULL if the name is not valid.
2661 * Set "varname" to the start of name without ':'.
2662 */
2663 hashtab_T *
2664find_var_ht(char_u *name, char_u **varname)
2665{
2666 hashitem_T *hi;
2667 hashtab_T *ht;
2668
2669 if (name[0] == NUL)
2670 return NULL;
2671 if (name[1] != ':')
2672 {
2673 // The name must not start with a colon or #.
2674 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2675 return NULL;
2676 *varname = name;
2677
2678 // "version" is "v:version" in all scopes if scriptversion < 3.
2679 // Same for a few other variables marked with VV_COMPAT.
2680 if (current_sctx.sc_version < 3)
2681 {
2682 hi = hash_find(&compat_hashtab, name);
2683 if (!HASHITEM_EMPTY(hi))
2684 return &compat_hashtab;
2685 }
2686
2687 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002688 if (ht != NULL)
2689 return ht; // local variable
2690
2691 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002692 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693 {
2694 ht = get_script_local_ht();
2695 if (ht != NULL)
2696 return ht;
2697 }
2698
2699 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002700 }
2701 *varname = name + 2;
2702 if (*name == 'g') // global variable
2703 return &globvarht;
2704 // There must be no ':' or '#' in the rest of the name, unless g: is used
2705 if (vim_strchr(name + 2, ':') != NULL
2706 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2707 return NULL;
2708 if (*name == 'b') // buffer variable
2709 return &curbuf->b_vars->dv_hashtab;
2710 if (*name == 'w') // window variable
2711 return &curwin->w_vars->dv_hashtab;
2712 if (*name == 't') // tab page variable
2713 return &curtab->tp_vars->dv_hashtab;
2714 if (*name == 'v') // v: variable
2715 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002716 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002717 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002719 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720 if (*name == 'a') // a: function argument
2721 return get_funccal_args_ht();
2722 if (*name == 'l') // l: local function variable
2723 return get_funccal_local_ht();
2724 }
2725 if (*name == 's') // script variable
2726 {
2727 ht = get_script_local_ht();
2728 if (ht != NULL)
2729 return ht;
2730 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002731 return NULL;
2732}
2733
2734/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002735 * Get the string value of a (global/local) variable.
2736 * Note: see tv_get_string() for how long the pointer remains valid.
2737 * Returns NULL when it doesn't exist.
2738 */
2739 char_u *
2740get_var_value(char_u *name)
2741{
2742 dictitem_T *v;
2743
2744 v = find_var(name, NULL, FALSE);
2745 if (v == NULL)
2746 return NULL;
2747 return tv_get_string(&v->di_tv);
2748}
2749
2750/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002751 * Allocate a new hashtab for a sourced script. It will be used while
2752 * sourcing this script and when executing functions defined in the script.
2753 */
2754 void
2755new_script_vars(scid_T id)
2756{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002757 scriptvar_T *sv;
2758
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002759 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2760 if (sv == NULL)
2761 return;
2762 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002763 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002764}
2765
2766/*
2767 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2768 * point to it.
2769 */
2770 void
2771init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2772{
2773 hash_init(&dict->dv_hashtab);
2774 dict->dv_lock = 0;
2775 dict->dv_scope = scope;
2776 dict->dv_refcount = DO_NOT_FREE_CNT;
2777 dict->dv_copyID = 0;
2778 dict_var->di_tv.vval.v_dict = dict;
2779 dict_var->di_tv.v_type = VAR_DICT;
2780 dict_var->di_tv.v_lock = VAR_FIXED;
2781 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2782 dict_var->di_key[0] = NUL;
2783}
2784
2785/*
2786 * Unreference a dictionary initialized by init_var_dict().
2787 */
2788 void
2789unref_var_dict(dict_T *dict)
2790{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002791 // Now the dict needs to be freed if no one else is using it, go back to
2792 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002793 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2794 dict_unref(dict);
2795}
2796
2797/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002798 * Clean up a list of internal variables.
2799 * Frees all allocated variables and the value they contain.
2800 * Clears hashtab "ht", does not free it.
2801 */
2802 void
2803vars_clear(hashtab_T *ht)
2804{
2805 vars_clear_ext(ht, TRUE);
2806}
2807
2808/*
2809 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2810 */
2811 void
2812vars_clear_ext(hashtab_T *ht, int free_val)
2813{
2814 int todo;
2815 hashitem_T *hi;
2816 dictitem_T *v;
2817
2818 hash_lock(ht);
2819 todo = (int)ht->ht_used;
2820 for (hi = ht->ht_array; todo > 0; ++hi)
2821 {
2822 if (!HASHITEM_EMPTY(hi))
2823 {
2824 --todo;
2825
2826 // Free the variable. Don't remove it from the hashtab,
2827 // ht_array might change then. hash_clear() takes care of it
2828 // later.
2829 v = HI2DI(hi);
2830 if (free_val)
2831 clear_tv(&v->di_tv);
2832 if (v->di_flags & DI_FLAGS_ALLOC)
2833 vim_free(v);
2834 }
2835 }
2836 hash_clear(ht);
2837 ht->ht_used = 0;
2838}
2839
2840/*
2841 * Delete a variable from hashtab "ht" at item "hi".
2842 * Clear the variable value and free the dictitem.
2843 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002844 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002845delete_var(hashtab_T *ht, hashitem_T *hi)
2846{
2847 dictitem_T *di = HI2DI(hi);
2848
2849 hash_remove(ht, hi);
2850 clear_tv(&di->di_tv);
2851 vim_free(di);
2852}
2853
2854/*
2855 * List the value of one internal variable.
2856 */
2857 static void
2858list_one_var(dictitem_T *v, char *prefix, int *first)
2859{
2860 char_u *tofree;
2861 char_u *s;
2862 char_u numbuf[NUMBUFLEN];
2863
2864 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2865 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2866 s == NULL ? (char_u *)"" : s, first);
2867 vim_free(tofree);
2868}
2869
2870 static void
2871list_one_var_a(
2872 char *prefix,
2873 char_u *name,
2874 int type,
2875 char_u *string,
2876 int *first) // when TRUE clear rest of screen and set to FALSE
2877{
2878 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2879 msg_start();
2880 msg_puts(prefix);
2881 if (name != NULL) // "a:" vars don't have a name stored
2882 msg_puts((char *)name);
2883 msg_putchar(' ');
2884 msg_advance(22);
2885 if (type == VAR_NUMBER)
2886 msg_putchar('#');
2887 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2888 msg_putchar('*');
2889 else if (type == VAR_LIST)
2890 {
2891 msg_putchar('[');
2892 if (*string == '[')
2893 ++string;
2894 }
2895 else if (type == VAR_DICT)
2896 {
2897 msg_putchar('{');
2898 if (*string == '{')
2899 ++string;
2900 }
2901 else
2902 msg_putchar(' ');
2903
2904 msg_outtrans(string);
2905
2906 if (type == VAR_FUNC || type == VAR_PARTIAL)
2907 msg_puts("()");
2908 if (*first)
2909 {
2910 msg_clr_eos();
2911 *first = FALSE;
2912 }
2913}
2914
2915/*
2916 * Set variable "name" to value in "tv".
2917 * If the variable already exists, the value is updated.
2918 * Otherwise the variable is created.
2919 */
2920 void
2921set_var(
2922 char_u *name,
2923 typval_T *tv,
2924 int copy) // make copy of value in "tv"
2925{
Bram Moolenaareeb27bf2020-07-04 17:39:10 +02002926 set_var_const(name, NULL, tv, copy, LET_NO_COMMAND);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002927}
2928
2929/*
2930 * Set variable "name" to value in "tv".
2931 * If the variable already exists and "is_const" is FALSE the value is updated.
2932 * Otherwise the variable is created.
2933 */
2934 void
2935set_var_const(
2936 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002938 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002939 int copy, // make copy of value in "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 int flags) // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002941{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002942 typval_T *tv = tv_arg;
2943 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002945 char_u *varname;
2946 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 int is_script_local;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002948
2949 ht = find_var_ht(name, &varname);
2950 if (ht == NULL || *varname == NUL)
2951 {
2952 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02002953 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002954 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002955 is_script_local = ht == get_script_local_ht();
2956
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002957 if (in_vim9script()
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002958 && !is_script_local
2959 && (flags & LET_NO_COMMAND) == 0
2960 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02002961 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002962 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02002963 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02002964 }
2965
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002967
2968 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 if (di == NULL)
2970 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002971
2972 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02002973 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02002974 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002975
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002976 if (need_convert_to_bool(type, tv))
2977 {
2978 // Destination is a bool and the value is not, but it can be converted.
2979 CLEAR_FIELD(bool_tv);
2980 bool_tv.v_type = VAR_BOOL;
2981 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
2982 tv = &bool_tv;
2983 }
2984
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002986 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002988 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 if (flags & LET_IS_CONST)
2990 {
2991 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02002992 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 }
2994
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002995 if (is_script_local && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996 {
Bram Moolenaar34db91f2020-06-13 19:00:10 +02002997 if ((flags & LET_NO_COMMAND) == 0)
2998 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002999 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003000 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003001 }
3002
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003003 // check the type and adjust to bool if needed
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003004 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003005 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003007
3008 if (var_check_ro(di->di_flags, name, FALSE)
3009 || var_check_lock(di->di_tv.v_lock, name, FALSE))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003010 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003011 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 else
3013 // can only redefine once
3014 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003015
3016 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003017
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003019 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003020 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003021 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003023 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003025 if (copy || tv->v_type != VAR_STRING)
3026 {
3027 char_u *val = tv_get_string(tv);
3028
3029 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003030 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 if (di->di_tv.vval.v_string == NULL)
3032 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003033 }
3034 else
3035 {
3036 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003038 tv->vval.v_string = NULL;
3039 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003040 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003041 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003043 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003045 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003047#ifdef FEAT_SEARCH_EXTRA
3048 else if (STRCMP(varname, "hlsearch") == 0)
3049 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003051 redraw_all_later(SOME_VALID);
3052 }
3053#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003054 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003055 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003057 {
3058 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003059 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003060 }
3061 }
3062
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003064 }
3065 else // add a new variable
3066 {
3067 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003068 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003069 {
3070 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003071 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003072 }
3073
3074 // Make sure the variable name is valid.
3075 if (!valid_varname(varname))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003076 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003077
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3079 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003080 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081 STRCPY(di->di_key, varname);
3082 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003083 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003085 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003086 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 di->di_flags = DI_FLAGS_ALLOC;
3088 if (flags & LET_IS_CONST)
3089 di->di_flags |= DI_FLAGS_LOCK;
3090
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003091 if (is_script_local && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003093 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094
3095 // Store a pointer to the typval_T, so that it can be found by
3096 // index instead of using a hastab lookup.
3097 if (ga_grow(&si->sn_var_vals, 1) == OK)
3098 {
3099 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3100 + si->sn_var_vals.ga_len;
3101 sv->sv_name = di->di_key;
3102 sv->sv_tv = &di->di_tv;
Bram Moolenaar53b29e42020-08-15 14:31:20 +02003103 if (type == NULL)
3104 sv->sv_type = typval2type(tv, &si->sn_type_list);
3105 else
3106 sv->sv_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 sv->sv_const = (flags & LET_IS_CONST);
3108 sv->sv_export = is_export;
3109 ++si->sn_var_vals.ga_len;
3110
3111 // let ex_export() know the export worked.
3112 is_export = FALSE;
3113 }
3114 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003115 }
3116
3117 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003119 else
3120 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 di->di_tv = *tv;
3122 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003123 init_tv(tv);
3124 }
3125
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 if (flags & LET_IS_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003127 // Like :lockvar! name: lock the value and what it contains, but only
3128 // if the reference count is up to one. That locks only literal
3129 // values.
3130 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003131 return;
3132failed:
3133 if (!copy)
3134 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003135}
3136
3137/*
3138 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3139 * Also give an error message.
3140 */
3141 int
3142var_check_ro(int flags, char_u *name, int use_gettext)
3143{
3144 if (flags & DI_FLAGS_RO)
3145 {
3146 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3147 return TRUE;
3148 }
3149 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3150 {
3151 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3152 return TRUE;
3153 }
3154 return FALSE;
3155}
3156
3157/*
3158 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3159 * Also give an error message.
3160 */
3161 int
3162var_check_fixed(int flags, char_u *name, int use_gettext)
3163{
3164 if (flags & DI_FLAGS_FIX)
3165 {
3166 semsg(_("E795: Cannot delete variable %s"),
3167 use_gettext ? (char_u *)_(name) : name);
3168 return TRUE;
3169 }
3170 return FALSE;
3171}
3172
3173/*
3174 * Check if a funcref is assigned to a valid variable name.
3175 * Return TRUE and give an error if not.
3176 */
3177 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003178var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003179 char_u *name, // points to start of variable name
3180 int new_var) // TRUE when creating the variable
3181{
3182 // Allow for w: b: s: and t:.
3183 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3184 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3185 ? name[2] : name[0]))
3186 {
3187 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3188 name);
3189 return TRUE;
3190 }
3191 // Don't allow hiding a function. When "v" is not NULL we might be
3192 // assigning another function to the same var, the type is checked
3193 // below.
3194 if (new_var && function_exists(name, FALSE))
3195 {
3196 semsg(_("E705: Variable name conflicts with existing function: %s"),
3197 name);
3198 return TRUE;
3199 }
3200 return FALSE;
3201}
3202
3203/*
3204 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
3205 * Also give an error message, using "name" or _("name") when use_gettext is
3206 * TRUE.
3207 */
3208 int
3209var_check_lock(int lock, char_u *name, int use_gettext)
3210{
3211 if (lock & VAR_LOCKED)
3212 {
3213 semsg(_("E741: Value is locked: %s"),
3214 name == NULL ? (char_u *)_("Unknown")
3215 : use_gettext ? (char_u *)_(name)
3216 : name);
3217 return TRUE;
3218 }
3219 if (lock & VAR_FIXED)
3220 {
3221 semsg(_("E742: Cannot change value of %s"),
3222 name == NULL ? (char_u *)_("Unknown")
3223 : use_gettext ? (char_u *)_(name)
3224 : name);
3225 return TRUE;
3226 }
3227 return FALSE;
3228}
3229
3230/*
3231 * Check if a variable name is valid.
3232 * Return FALSE and give an error if not.
3233 */
3234 int
3235valid_varname(char_u *varname)
3236{
3237 char_u *p;
3238
3239 for (p = varname; *p != NUL; ++p)
3240 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3241 && *p != AUTOLOAD_CHAR)
3242 {
3243 semsg(_(e_illvar), varname);
3244 return FALSE;
3245 }
3246 return TRUE;
3247}
3248
3249/*
3250 * getwinvar() and gettabwinvar()
3251 */
3252 static void
3253getwinvar(
3254 typval_T *argvars,
3255 typval_T *rettv,
3256 int off) // 1 for gettabwinvar()
3257{
3258 win_T *win;
3259 char_u *varname;
3260 dictitem_T *v;
3261 tabpage_T *tp = NULL;
3262 int done = FALSE;
3263 win_T *oldcurwin;
3264 tabpage_T *oldtabpage;
3265 int need_switch_win;
3266
3267 if (off == 1)
3268 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3269 else
3270 tp = curtab;
3271 win = find_win_by_nr(&argvars[off], tp);
3272 varname = tv_get_string_chk(&argvars[off + 1]);
3273 ++emsg_off;
3274
3275 rettv->v_type = VAR_STRING;
3276 rettv->vval.v_string = NULL;
3277
3278 if (win != NULL && varname != NULL)
3279 {
3280 // Set curwin to be our win, temporarily. Also set the tabpage,
3281 // otherwise the window is not valid. Only do this when needed,
3282 // autocommands get blocked.
3283 need_switch_win = !(tp == curtab && win == curwin);
3284 if (!need_switch_win
3285 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3286 {
3287 if (*varname == '&')
3288 {
3289 if (varname[1] == NUL)
3290 {
3291 // get all window-local options in a dict
3292 dict_T *opts = get_winbuf_options(FALSE);
3293
3294 if (opts != NULL)
3295 {
3296 rettv_dict_set(rettv, opts);
3297 done = TRUE;
3298 }
3299 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003300 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003301 // window-local-option
3302 done = TRUE;
3303 }
3304 else
3305 {
3306 // Look up the variable.
3307 // Let getwinvar({nr}, "") return the "w:" dictionary.
3308 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3309 varname, FALSE);
3310 if (v != NULL)
3311 {
3312 copy_tv(&v->di_tv, rettv);
3313 done = TRUE;
3314 }
3315 }
3316 }
3317
3318 if (need_switch_win)
3319 // restore previous notion of curwin
3320 restore_win(oldcurwin, oldtabpage, TRUE);
3321 }
3322
3323 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3324 // use the default return value
3325 copy_tv(&argvars[off + 2], rettv);
3326
3327 --emsg_off;
3328}
3329
3330/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003331 * Set option "varname" to the value of "varp" for the current buffer/window.
3332 */
3333 static void
3334set_option_from_tv(char_u *varname, typval_T *varp)
3335{
3336 long numval = 0;
3337 char_u *strval;
3338 char_u nbuf[NUMBUFLEN];
3339 int error = FALSE;
3340
3341 if (!in_vim9script() || varp->v_type != VAR_STRING)
3342 numval = (long)tv_get_number_chk(varp, &error);
3343 strval = tv_get_string_buf_chk(varp, nbuf);
3344 if (!error && strval != NULL)
3345 set_option_value(varname, numval, strval, OPT_LOCAL);
3346}
3347
3348/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003349 * "setwinvar()" and "settabwinvar()" functions
3350 */
3351 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003352setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003353{
3354 win_T *win;
3355 win_T *save_curwin;
3356 tabpage_T *save_curtab;
3357 int need_switch_win;
3358 char_u *varname, *winvarname;
3359 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003360 tabpage_T *tp = NULL;
3361
3362 if (check_secure())
3363 return;
3364
3365 if (off == 1)
3366 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3367 else
3368 tp = curtab;
3369 win = find_win_by_nr(&argvars[off], tp);
3370 varname = tv_get_string_chk(&argvars[off + 1]);
3371 varp = &argvars[off + 2];
3372
3373 if (win != NULL && varname != NULL && varp != NULL)
3374 {
3375 need_switch_win = !(tp == curtab && win == curwin);
3376 if (!need_switch_win
3377 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3378 {
3379 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003380 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003381 else
3382 {
3383 winvarname = alloc(STRLEN(varname) + 3);
3384 if (winvarname != NULL)
3385 {
3386 STRCPY(winvarname, "w:");
3387 STRCPY(winvarname + 2, varname);
3388 set_var(winvarname, varp, TRUE);
3389 vim_free(winvarname);
3390 }
3391 }
3392 }
3393 if (need_switch_win)
3394 restore_win(save_curwin, save_curtab, TRUE);
3395 }
3396}
3397
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003398/*
3399 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3400 * v:option_type, and v:option_command.
3401 */
3402 void
3403reset_v_option_vars(void)
3404{
3405 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3406 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3407 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3408 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3409 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3410 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3411}
3412
3413/*
3414 * Add an assert error to v:errors.
3415 */
3416 void
3417assert_error(garray_T *gap)
3418{
3419 struct vimvar *vp = &vimvars[VV_ERRORS];
3420
3421 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003422 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003423 set_vim_var_list(VV_ERRORS, list_alloc());
3424 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3425}
3426
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003427 int
3428var_exists(char_u *var)
3429{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003430 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003431 char_u *name;
3432 char_u *tofree;
3433 typval_T tv;
3434 int len = 0;
3435 int n = FALSE;
3436
3437 // get_name_len() takes care of expanding curly braces
3438 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003439 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003440 if (len > 0)
3441 {
3442 if (tofree != NULL)
3443 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003444 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003445 if (n)
3446 {
3447 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003448 arg = skipwhite(arg);
3449 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003450 if (n)
3451 clear_tv(&tv);
3452 }
3453 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003454 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003455 n = FALSE;
3456
3457 vim_free(tofree);
3458 return n;
3459}
3460
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003461static lval_T *redir_lval = NULL;
3462#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3463static garray_T redir_ga; // only valid when redir_lval is not NULL
3464static char_u *redir_endp = NULL;
3465static char_u *redir_varname = NULL;
3466
3467/*
3468 * Start recording command output to a variable
3469 * When "append" is TRUE append to an existing variable.
3470 * Returns OK if successfully completed the setup. FAIL otherwise.
3471 */
3472 int
3473var_redir_start(char_u *name, int append)
3474{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003475 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003476 typval_T tv;
3477
3478 // Catch a bad name early.
3479 if (!eval_isnamec1(*name))
3480 {
3481 emsg(_(e_invarg));
3482 return FAIL;
3483 }
3484
3485 // Make a copy of the name, it is used in redir_lval until redir ends.
3486 redir_varname = vim_strsave(name);
3487 if (redir_varname == NULL)
3488 return FAIL;
3489
3490 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3491 if (redir_lval == NULL)
3492 {
3493 var_redir_stop();
3494 return FAIL;
3495 }
3496
3497 // The output is stored in growarray "redir_ga" until redirection ends.
3498 ga_init2(&redir_ga, (int)sizeof(char), 500);
3499
3500 // Parse the variable name (can be a dict or list entry).
3501 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3502 FNE_CHECK_START);
3503 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3504 {
3505 clear_lval(redir_lval);
3506 if (redir_endp != NULL && *redir_endp != NUL)
3507 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003508 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003509 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003510 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003511 redir_endp = NULL; // don't store a value, only cleanup
3512 var_redir_stop();
3513 return FAIL;
3514 }
3515
3516 // check if we can write to the variable: set it to or append an empty
3517 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003518 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003519 tv.v_type = VAR_STRING;
3520 tv.vval.v_string = (char_u *)"";
3521 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003523 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003525 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003526 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003527 {
3528 redir_endp = NULL; // don't store a value, only cleanup
3529 var_redir_stop();
3530 return FAIL;
3531 }
3532
3533 return OK;
3534}
3535
3536/*
3537 * Append "value[value_len]" to the variable set by var_redir_start().
3538 * The actual appending is postponed until redirection ends, because the value
3539 * appended may in fact be the string we write to, changing it may cause freed
3540 * memory to be used:
3541 * :redir => foo
3542 * :let foo
3543 * :redir END
3544 */
3545 void
3546var_redir_str(char_u *value, int value_len)
3547{
3548 int len;
3549
3550 if (redir_lval == NULL)
3551 return;
3552
3553 if (value_len == -1)
3554 len = (int)STRLEN(value); // Append the entire string
3555 else
3556 len = value_len; // Append only "value_len" characters
3557
3558 if (ga_grow(&redir_ga, len) == OK)
3559 {
3560 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3561 redir_ga.ga_len += len;
3562 }
3563 else
3564 var_redir_stop();
3565}
3566
3567/*
3568 * Stop redirecting command output to a variable.
3569 * Frees the allocated memory.
3570 */
3571 void
3572var_redir_stop(void)
3573{
3574 typval_T tv;
3575
3576 if (EVALCMD_BUSY)
3577 {
3578 redir_lval = NULL;
3579 return;
3580 }
3581
3582 if (redir_lval != NULL)
3583 {
3584 // If there was no error: assign the text to the variable.
3585 if (redir_endp != NULL)
3586 {
3587 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3588 tv.v_type = VAR_STRING;
3589 tv.vval.v_string = redir_ga.ga_data;
3590 // Call get_lval() again, if it's inside a Dict or List it may
3591 // have changed.
3592 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3593 FALSE, FALSE, 0, FNE_CHECK_START);
3594 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003596 (char_u *)".");
3597 clear_lval(redir_lval);
3598 }
3599
3600 // free the collected output
3601 VIM_CLEAR(redir_ga.ga_data);
3602
3603 VIM_CLEAR(redir_lval);
3604 }
3605 VIM_CLEAR(redir_varname);
3606}
3607
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003608/*
3609 * "gettabvar()" function
3610 */
3611 void
3612f_gettabvar(typval_T *argvars, typval_T *rettv)
3613{
3614 win_T *oldcurwin;
3615 tabpage_T *tp, *oldtabpage;
3616 dictitem_T *v;
3617 char_u *varname;
3618 int done = FALSE;
3619
3620 rettv->v_type = VAR_STRING;
3621 rettv->vval.v_string = NULL;
3622
3623 varname = tv_get_string_chk(&argvars[1]);
3624 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3625 if (tp != NULL && varname != NULL)
3626 {
3627 // Set tp to be our tabpage, temporarily. Also set the window to the
3628 // first window in the tabpage, otherwise the window is not valid.
3629 if (switch_win(&oldcurwin, &oldtabpage,
3630 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3631 : tp->tp_firstwin, tp, TRUE) == OK)
3632 {
3633 // look up the variable
3634 // Let gettabvar({nr}, "") return the "t:" dictionary.
3635 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3636 if (v != NULL)
3637 {
3638 copy_tv(&v->di_tv, rettv);
3639 done = TRUE;
3640 }
3641 }
3642
3643 // restore previous notion of curwin
3644 restore_win(oldcurwin, oldtabpage, TRUE);
3645 }
3646
3647 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3648 copy_tv(&argvars[2], rettv);
3649}
3650
3651/*
3652 * "gettabwinvar()" function
3653 */
3654 void
3655f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3656{
3657 getwinvar(argvars, rettv, 1);
3658}
3659
3660/*
3661 * "getwinvar()" function
3662 */
3663 void
3664f_getwinvar(typval_T *argvars, typval_T *rettv)
3665{
3666 getwinvar(argvars, rettv, 0);
3667}
3668
3669/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003670 * "getbufvar()" function
3671 */
3672 void
3673f_getbufvar(typval_T *argvars, typval_T *rettv)
3674{
3675 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003676 char_u *varname;
3677 dictitem_T *v;
3678 int done = FALSE;
3679
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003680 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003681 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003682
3683 rettv->v_type = VAR_STRING;
3684 rettv->vval.v_string = NULL;
3685
3686 if (buf != NULL && varname != NULL)
3687 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003688 if (*varname == '&')
3689 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003690 buf_T *save_curbuf = curbuf;
3691
3692 // set curbuf to be our buf, temporarily
3693 curbuf = buf;
3694
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003695 if (varname[1] == NUL)
3696 {
3697 // get all buffer-local options in a dict
3698 dict_T *opts = get_winbuf_options(TRUE);
3699
3700 if (opts != NULL)
3701 {
3702 rettv_dict_set(rettv, opts);
3703 done = TRUE;
3704 }
3705 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003706 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003707 // buffer-local-option
3708 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003709
3710 // restore previous notion of curbuf
3711 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003712 }
3713 else
3714 {
3715 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003716 if (*varname == NUL)
3717 // Let getbufvar({nr}, "") return the "b:" dictionary.
3718 v = &buf->b_bufvar;
3719 else
3720 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3721 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003722 if (v != NULL)
3723 {
3724 copy_tv(&v->di_tv, rettv);
3725 done = TRUE;
3726 }
3727 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003728 }
3729
3730 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3731 // use the default value
3732 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003733}
3734
3735/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003736 * "settabvar()" function
3737 */
3738 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003739f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003740{
3741 tabpage_T *save_curtab;
3742 tabpage_T *tp;
3743 char_u *varname, *tabvarname;
3744 typval_T *varp;
3745
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003746 if (check_secure())
3747 return;
3748
3749 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3750 varname = tv_get_string_chk(&argvars[1]);
3751 varp = &argvars[2];
3752
3753 if (varname != NULL && varp != NULL && tp != NULL)
3754 {
3755 save_curtab = curtab;
3756 goto_tabpage_tp(tp, FALSE, FALSE);
3757
3758 tabvarname = alloc(STRLEN(varname) + 3);
3759 if (tabvarname != NULL)
3760 {
3761 STRCPY(tabvarname, "t:");
3762 STRCPY(tabvarname + 2, varname);
3763 set_var(tabvarname, varp, TRUE);
3764 vim_free(tabvarname);
3765 }
3766
3767 // Restore current tabpage
3768 if (valid_tabpage(save_curtab))
3769 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3770 }
3771}
3772
3773/*
3774 * "settabwinvar()" function
3775 */
3776 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003777f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003778{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003779 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003780}
3781
3782/*
3783 * "setwinvar()" function
3784 */
3785 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003786f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003787{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003788 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003789}
3790
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003791/*
3792 * "setbufvar()" function
3793 */
3794 void
3795f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3796{
3797 buf_T *buf;
3798 char_u *varname, *bufvarname;
3799 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003800
3801 if (check_secure())
3802 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003803 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003804 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003805 varp = &argvars[2];
3806
3807 if (buf != NULL && varname != NULL && varp != NULL)
3808 {
3809 if (*varname == '&')
3810 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003811 aco_save_T aco;
3812
3813 // set curbuf to be our buf, temporarily
3814 aucmd_prepbuf(&aco, buf);
3815
Bram Moolenaar191929b2020-08-19 21:20:49 +02003816 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003817
3818 // reset notion of buffer
3819 aucmd_restbuf(&aco);
3820 }
3821 else
3822 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003823 bufvarname = alloc(STRLEN(varname) + 3);
3824 if (bufvarname != NULL)
3825 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003826 buf_T *save_curbuf = curbuf;
3827
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003828 curbuf = buf;
3829 STRCPY(bufvarname, "b:");
3830 STRCPY(bufvarname + 2, varname);
3831 set_var(bufvarname, varp, TRUE);
3832 vim_free(bufvarname);
3833 curbuf = save_curbuf;
3834 }
3835 }
3836 }
3837}
3838
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003839/*
3840 * Get a callback from "arg". It can be a Funcref or a function name.
3841 * When "arg" is zero return an empty string.
3842 * "cb_name" is not allocated.
3843 * "cb_name" is set to NULL for an invalid argument.
3844 */
3845 callback_T
3846get_callback(typval_T *arg)
3847{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003848 callback_T res;
3849 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003850
3851 res.cb_free_name = FALSE;
3852 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3853 {
3854 res.cb_partial = arg->vval.v_partial;
3855 ++res.cb_partial->pt_refcount;
3856 res.cb_name = partial_name(res.cb_partial);
3857 }
3858 else
3859 {
3860 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003861 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3862 && isdigit(*arg->vval.v_string))
3863 r = FAIL;
3864 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003865 {
3866 // Note that we don't make a copy of the string.
3867 res.cb_name = arg->vval.v_string;
3868 func_ref(res.cb_name);
3869 }
3870 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003871 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003872 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003873 r = FAIL;
3874
3875 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003876 {
3877 emsg(_("E921: Invalid callback argument"));
3878 res.cb_name = NULL;
3879 }
3880 }
3881 return res;
3882}
3883
3884/*
3885 * Copy a callback into a typval_T.
3886 */
3887 void
3888put_callback(callback_T *cb, typval_T *tv)
3889{
3890 if (cb->cb_partial != NULL)
3891 {
3892 tv->v_type = VAR_PARTIAL;
3893 tv->vval.v_partial = cb->cb_partial;
3894 ++tv->vval.v_partial->pt_refcount;
3895 }
3896 else
3897 {
3898 tv->v_type = VAR_FUNC;
3899 tv->vval.v_string = vim_strsave(cb->cb_name);
3900 func_ref(cb->cb_name);
3901 }
3902}
3903
3904/*
3905 * Make a copy of "src" into "dest", allocating the function name if needed,
3906 * without incrementing the refcount.
3907 */
3908 void
3909set_callback(callback_T *dest, callback_T *src)
3910{
3911 if (src->cb_partial == NULL)
3912 {
3913 // just a function name, make a copy
3914 dest->cb_name = vim_strsave(src->cb_name);
3915 dest->cb_free_name = TRUE;
3916 }
3917 else
3918 {
3919 // cb_name is a pointer into cb_partial
3920 dest->cb_name = src->cb_name;
3921 dest->cb_free_name = FALSE;
3922 }
3923 dest->cb_partial = src->cb_partial;
3924}
3925
3926/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02003927 * Copy callback from "src" to "dest", incrementing the refcounts.
3928 */
3929 void
3930copy_callback(callback_T *dest, callback_T *src)
3931{
3932 dest->cb_partial = src->cb_partial;
3933 if (dest->cb_partial != NULL)
3934 {
3935 dest->cb_name = src->cb_name;
3936 dest->cb_free_name = FALSE;
3937 ++dest->cb_partial->pt_refcount;
3938 }
3939 else
3940 {
3941 dest->cb_name = vim_strsave(src->cb_name);
3942 dest->cb_free_name = TRUE;
3943 func_ref(src->cb_name);
3944 }
3945}
3946
3947/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003948 * Unref/free "callback" returned by get_callback() or set_callback().
3949 */
3950 void
3951free_callback(callback_T *callback)
3952{
3953 if (callback->cb_partial != NULL)
3954 {
3955 partial_unref(callback->cb_partial);
3956 callback->cb_partial = NULL;
3957 }
3958 else if (callback->cb_name != NULL)
3959 func_unref(callback->cb_name);
3960 if (callback->cb_free_name)
3961 {
3962 vim_free(callback->cb_name);
3963 callback->cb_free_name = FALSE;
3964 }
3965 callback->cb_name = NULL;
3966}
3967
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003968#endif // FEAT_EVAL