blob: 1952eb88f4361e1d0894281f7e820a402fca6ee7 [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 Moolenaare5cdf152019-08-29 22:09:46 +0200148};
149
150// shorthand
151#define vv_type vv_di.di_tv.v_type
152#define vv_nr vv_di.di_tv.vval.v_number
153#define vv_float vv_di.di_tv.vval.v_float
154#define vv_str vv_di.di_tv.vval.v_string
155#define vv_list vv_di.di_tv.vval.v_list
156#define vv_dict vv_di.di_tv.vval.v_dict
157#define vv_blob vv_di.di_tv.vval.v_blob
158#define vv_tv vv_di.di_tv
159
160static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200161static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200162#define vimvarht vimvardict.dv_hashtab
163
164// for VIM_VERSION_ defines
165#include "version.h"
166
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200167static void ex_let_const(exarg_T *eap, int is_const);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168static char_u *skip_var_one(char_u *arg, int include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200169static void list_glob_vars(int *first);
170static void list_buf_vars(int *first);
171static void list_win_vars(int *first);
172static void list_tab_vars(int *first);
173static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100174static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200175static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
176static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200177static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200178static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200179static void list_one_var(dictitem_T *v, char *prefix, int *first);
180static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
181
182/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200183 * Initialize global and vim special variables
184 */
185 void
186evalvars_init(void)
187{
188 int i;
189 struct vimvar *p;
190
191 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
192 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
193 vimvardict.dv_lock = VAR_FIXED;
194 hash_init(&compat_hashtab);
195
196 for (i = 0; i < VV_LEN; ++i)
197 {
198 p = &vimvars[i];
199 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
200 {
201 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
202 getout(1);
203 }
204 STRCPY(p->vv_di.di_key, p->vv_name);
205 if (p->vv_flags & VV_RO)
206 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
207 else if (p->vv_flags & VV_RO_SBX)
208 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
209 else
210 p->vv_di.di_flags = DI_FLAGS_FIX;
211
212 // add to v: scope dict, unless the value is not always available
213 if (p->vv_type != VAR_UNKNOWN)
214 hash_add(&vimvarht, p->vv_di.di_key);
215 if (p->vv_flags & VV_COMPAT)
216 // add to compat scope dict
217 hash_add(&compat_hashtab, p->vv_di.di_key);
218 }
219 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
220 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
221
222 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
223 set_vim_var_nr(VV_HLSEARCH, 1L);
224 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
225 set_vim_var_list(VV_ERRORS, list_alloc());
226 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
227
228 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
229 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
230 set_vim_var_nr(VV_NONE, VVAL_NONE);
231 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100232 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200233
234 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
235 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
236 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
237 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
238 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
239 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
240 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
241 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
242 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
243 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
244 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
245
246 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
247
248 set_reg_var(0); // default for v:register is not 0 but '"'
249}
250
251#if defined(EXITFREE) || defined(PROTO)
252/*
253 * Free all vim variables information on exit
254 */
255 void
256evalvars_clear(void)
257{
258 int i;
259 struct vimvar *p;
260
261 for (i = 0; i < VV_LEN; ++i)
262 {
263 p = &vimvars[i];
264 if (p->vv_di.di_tv.v_type == VAR_STRING)
265 VIM_CLEAR(p->vv_str);
266 else if (p->vv_di.di_tv.v_type == VAR_LIST)
267 {
268 list_unref(p->vv_list);
269 p->vv_list = NULL;
270 }
271 }
272 hash_clear(&vimvarht);
273 hash_init(&vimvarht); // garbage_collect() will access it
274 hash_clear(&compat_hashtab);
275
276 // global variables
277 vars_clear(&globvarht);
278
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100279 // Script-local variables. Clear all the variables here.
280 // The scriptvar_T is cleared later in free_scriptnames(), because a
281 // variable in one script might hold a reference to the whole scope of
282 // another script.
283 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200284 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200285}
286#endif
287
288 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200289garbage_collect_globvars(int copyID)
290{
291 return set_ref_in_ht(&globvarht, copyID, NULL);
292}
293
294 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200295garbage_collect_vimvars(int copyID)
296{
297 return set_ref_in_ht(&vimvarht, copyID, NULL);
298}
299
300 int
301garbage_collect_scriptvars(int copyID)
302{
303 int i;
304 int abort = FALSE;
305
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100306 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200307 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
308
309 return abort;
310}
311
312/*
313 * Set an internal variable to a string value. Creates the variable if it does
314 * not already exist.
315 */
316 void
317set_internal_string_var(char_u *name, char_u *value)
318{
319 char_u *val;
320 typval_T *tvp;
321
322 val = vim_strsave(value);
323 if (val != NULL)
324 {
325 tvp = alloc_string_tv(val);
326 if (tvp != NULL)
327 {
328 set_var(name, tvp, FALSE);
329 free_tv(tvp);
330 }
331 }
332}
333
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200334 int
335eval_charconvert(
336 char_u *enc_from,
337 char_u *enc_to,
338 char_u *fname_from,
339 char_u *fname_to)
340{
341 int err = FALSE;
342
343 set_vim_var_string(VV_CC_FROM, enc_from, -1);
344 set_vim_var_string(VV_CC_TO, enc_to, -1);
345 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
346 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
347 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
348 err = TRUE;
349 set_vim_var_string(VV_CC_FROM, NULL, -1);
350 set_vim_var_string(VV_CC_TO, NULL, -1);
351 set_vim_var_string(VV_FNAME_IN, NULL, -1);
352 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
353
354 if (err)
355 return FAIL;
356 return OK;
357}
358
359# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
360 int
361eval_printexpr(char_u *fname, char_u *args)
362{
363 int err = FALSE;
364
365 set_vim_var_string(VV_FNAME_IN, fname, -1);
366 set_vim_var_string(VV_CMDARG, args, -1);
367 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
368 err = TRUE;
369 set_vim_var_string(VV_FNAME_IN, NULL, -1);
370 set_vim_var_string(VV_CMDARG, NULL, -1);
371
372 if (err)
373 {
374 mch_remove(fname);
375 return FAIL;
376 }
377 return OK;
378}
379# endif
380
381# if defined(FEAT_DIFF) || defined(PROTO)
382 void
383eval_diff(
384 char_u *origfile,
385 char_u *newfile,
386 char_u *outfile)
387{
388 int err = FALSE;
389
390 set_vim_var_string(VV_FNAME_IN, origfile, -1);
391 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
392 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
393 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
394 set_vim_var_string(VV_FNAME_IN, NULL, -1);
395 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
396 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
397}
398
399 void
400eval_patch(
401 char_u *origfile,
402 char_u *difffile,
403 char_u *outfile)
404{
405 int err;
406
407 set_vim_var_string(VV_FNAME_IN, origfile, -1);
408 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
409 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
410 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
411 set_vim_var_string(VV_FNAME_IN, NULL, -1);
412 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
413 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
414}
415# endif
416
417#if defined(FEAT_SPELL) || defined(PROTO)
418/*
419 * Evaluate an expression to a list with suggestions.
420 * For the "expr:" part of 'spellsuggest'.
421 * Returns NULL when there is an error.
422 */
423 list_T *
424eval_spell_expr(char_u *badword, char_u *expr)
425{
426 typval_T save_val;
427 typval_T rettv;
428 list_T *list = NULL;
429 char_u *p = skipwhite(expr);
430
431 // Set "v:val" to the bad word.
432 prepare_vimvar(VV_VAL, &save_val);
433 set_vim_var_string(VV_VAL, badword, -1);
434 if (p_verbose == 0)
435 ++emsg_off;
436
437 if (eval1(&p, &rettv, TRUE) == OK)
438 {
439 if (rettv.v_type != VAR_LIST)
440 clear_tv(&rettv);
441 else
442 list = rettv.vval.v_list;
443 }
444
445 if (p_verbose == 0)
446 --emsg_off;
447 clear_tv(get_vim_var_tv(VV_VAL));
448 restore_vimvar(VV_VAL, &save_val);
449
450 return list;
451}
452
453/*
454 * "list" is supposed to contain two items: a word and a number. Return the
455 * word in "pp" and the number as the return value.
456 * Return -1 if anything isn't right.
457 * Used to get the good word and score from the eval_spell_expr() result.
458 */
459 int
460get_spellword(list_T *list, char_u **pp)
461{
462 listitem_T *li;
463
464 li = list->lv_first;
465 if (li == NULL)
466 return -1;
467 *pp = tv_get_string(&li->li_tv);
468
469 li = li->li_next;
470 if (li == NULL)
471 return -1;
472 return (int)tv_get_number(&li->li_tv);
473}
474#endif
475
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200476/*
477 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200478 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200479 * When not used yet add the variable to the v: hashtable.
480 */
481 void
482prepare_vimvar(int idx, typval_T *save_tv)
483{
484 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200485 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200486 if (vimvars[idx].vv_type == VAR_UNKNOWN)
487 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
488}
489
490/*
491 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200492 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200493 * When no longer defined, remove the variable from the v: hashtable.
494 */
495 void
496restore_vimvar(int idx, typval_T *save_tv)
497{
498 hashitem_T *hi;
499
500 vimvars[idx].vv_tv = *save_tv;
501 if (vimvars[idx].vv_type == VAR_UNKNOWN)
502 {
503 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
504 if (HASHITEM_EMPTY(hi))
505 internal_error("restore_vimvar()");
506 else
507 hash_remove(&vimvarht, hi);
508 }
509}
510
511/*
512 * List Vim variables.
513 */
514 static void
515list_vim_vars(int *first)
516{
517 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
518}
519
520/*
521 * List script-local variables, if there is a script.
522 */
523 static void
524list_script_vars(int *first)
525{
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100526 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= script_items.ga_len)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200527 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
528 "s:", FALSE, first);
529}
530
531/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200532 * Get a list of lines from a HERE document. The here document is a list of
533 * lines surrounded by a marker.
534 * cmd << {marker}
535 * {line1}
536 * {line2}
537 * ....
538 * {marker}
539 *
540 * The {marker} is a string. If the optional 'trim' word is supplied before the
541 * marker, then the leading indentation before the lines (matching the
542 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200543 *
544 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
545 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
546 * missing, then '.' is accepted as a marker.
547 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200548 * Returns a List with {lines} or NULL.
549 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100550 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200551heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200552{
553 char_u *theline;
554 char_u *marker;
555 list_T *l;
556 char_u *p;
557 int marker_indent_len = 0;
558 int text_indent_len = 0;
559 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200560 char_u dot[] = ".";
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200561
562 if (eap->getline == NULL)
563 {
564 emsg(_("E991: cannot use =<< here"));
565 return NULL;
566 }
567
568 // Check for the optional 'trim' word before the marker
569 cmd = skipwhite(cmd);
570 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
571 {
572 cmd = skipwhite(cmd + 4);
573
574 // Trim the indentation from all the lines in the here document.
575 // The amount of indentation trimmed is the same as the indentation of
576 // the first line after the :let command line. To find the end marker
577 // the indent of the :let command line is trimmed.
578 p = *eap->cmdlinep;
579 while (VIM_ISWHITE(*p))
580 {
581 p++;
582 marker_indent_len++;
583 }
584 text_indent_len = -1;
585 }
586
587 // The marker is the next word.
588 if (*cmd != NUL && *cmd != '"')
589 {
590 marker = skipwhite(cmd);
591 p = skiptowhite(marker);
592 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
593 {
594 emsg(_(e_trailing));
595 return NULL;
596 }
597 *p = NUL;
598 if (vim_islower(*marker))
599 {
600 emsg(_("E221: Marker cannot start with lower case letter"));
601 return NULL;
602 }
603 }
604 else
605 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200606 // When getting lines for an embedded script, if the marker is missing,
607 // accept '.' as the marker.
608 if (script_get)
609 marker = dot;
610 else
611 {
612 emsg(_("E172: Missing marker"));
613 return NULL;
614 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200615 }
616
617 l = list_alloc();
618 if (l == NULL)
619 return NULL;
620
621 for (;;)
622 {
623 int mi = 0;
624 int ti = 0;
625
626 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
627 if (theline == NULL)
628 {
629 semsg(_("E990: Missing end marker '%s'"), marker);
630 break;
631 }
632
633 // with "trim": skip the indent matching the :let line to find the
634 // marker
635 if (marker_indent_len > 0
636 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
637 mi = marker_indent_len;
638 if (STRCMP(marker, theline + mi) == 0)
639 {
640 vim_free(theline);
641 break;
642 }
643
644 if (text_indent_len == -1 && *theline != NUL)
645 {
646 // set the text indent from the first line.
647 p = theline;
648 text_indent_len = 0;
649 while (VIM_ISWHITE(*p))
650 {
651 p++;
652 text_indent_len++;
653 }
654 text_indent = vim_strnsave(theline, text_indent_len);
655 }
656 // with "trim": skip the indent matching the first line
657 if (text_indent != NULL)
658 for (ti = 0; ti < text_indent_len; ++ti)
659 if (theline[ti] != text_indent[ti])
660 break;
661
662 if (list_append_string(l, theline + ti, -1) == FAIL)
663 break;
664 vim_free(theline);
665 }
666 vim_free(text_indent);
667
668 return l;
669}
670
671/*
672 * ":let" list all variable values
673 * ":let var1 var2" list variable values
674 * ":let var = expr" assignment command.
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 [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100684 * ":let var: string" Vim9 declaration
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200685 */
686 void
687ex_let(exarg_T *eap)
688{
689 ex_let_const(eap, FALSE);
690}
691
692/*
693 * ":const" list all variable values
694 * ":const var1 var2" list variable values
695 * ":const var = expr" assignment command.
696 * ":const [var1, var2] = expr" unpack list.
697 */
698 void
699ex_const(exarg_T *eap)
700{
701 ex_let_const(eap, TRUE);
702}
703
704 static void
705ex_let_const(exarg_T *eap, int is_const)
706{
707 char_u *arg = eap->arg;
708 char_u *expr = NULL;
709 typval_T rettv;
710 int i;
711 int var_count = 0;
712 int semicolon = 0;
713 char_u op[2];
714 char_u *argend;
715 int first = TRUE;
716 int concat;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100717 int flags = is_const ? LET_IS_CONST : 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200718
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100719 // detect Vim9 assignment without ":let" or ":const"
720 if (eap->arg == eap->cmd)
721 flags |= LET_NO_COMMAND;
722
723 argend = skip_var_list(arg, TRUE, &var_count, &semicolon);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200724 if (argend == NULL)
725 return;
726 if (argend > arg && argend[-1] == '.') // for var.='str'
727 --argend;
728 expr = skipwhite(argend);
729 concat = expr[0] == '.'
730 && ((expr[1] == '=' && current_sctx.sc_version < 2)
731 || (expr[1] == '.' && expr[2] == '='));
732 if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%", *expr) != NULL
733 && expr[1] == '=') || concat))
734 {
735 // ":let" without "=": list variables
736 if (*arg == '[')
737 emsg(_(e_invarg));
738 else if (expr[0] == '.')
739 emsg(_("E985: .= is not supported with script version 2"));
740 else if (!ends_excmd(*arg))
741 // ":let var1 var2"
742 arg = list_arg_vars(eap, arg, &first);
743 else if (!eap->skip)
744 {
745 // ":let"
746 list_glob_vars(&first);
747 list_buf_vars(&first);
748 list_win_vars(&first);
749 list_tab_vars(&first);
750 list_script_vars(&first);
751 list_func_vars(&first);
752 list_vim_vars(&first);
753 }
754 eap->nextcmd = check_nextcmd(arg);
755 }
756 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
757 {
758 list_T *l;
759
760 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200761 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200762 if (l != NULL)
763 {
764 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200765 if (!eap->skip)
766 {
767 op[0] = '=';
768 op[1] = NUL;
769 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200771 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200772 clear_tv(&rettv);
773 }
774 }
775 else
776 {
777 op[0] = '=';
778 op[1] = NUL;
779 if (*expr != '=')
780 {
781 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
782 {
783 op[0] = *expr; // +=, -=, *=, /=, %= or .=
784 if (expr[0] == '.' && expr[1] == '.') // ..=
785 ++expr;
786 }
787 expr = skipwhite(expr + 2);
788 }
789 else
790 expr = skipwhite(expr + 1);
791
792 if (eap->skip)
793 ++emsg_skip;
794 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
795 if (eap->skip)
796 {
797 if (i != FAIL)
798 clear_tv(&rettv);
799 --emsg_skip;
800 }
801 else if (i != FAIL)
802 {
803 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100804 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200805 clear_tv(&rettv);
806 }
807 }
808}
809
810/*
811 * Assign the typevalue "tv" to the variable or variables at "arg_start".
812 * Handles both "var" with any type and "[var, var; var]" with a list type.
813 * When "op" is not NULL it points to a string with characters that
814 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
815 * or concatenate.
816 * Returns OK or FAIL;
817 */
818 int
819ex_let_vars(
820 char_u *arg_start,
821 typval_T *tv,
822 int copy, // copy values from "tv", don't move
823 int semicolon, // from skip_var_list()
824 int var_count, // from skip_var_list()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200826 char_u *op)
827{
828 char_u *arg = arg_start;
829 list_T *l;
830 int i;
831 listitem_T *item;
832 typval_T ltv;
833
834 if (*arg != '[')
835 {
836 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200838 return FAIL;
839 return OK;
840 }
841
842 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
843 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
844 {
845 emsg(_(e_listreq));
846 return FAIL;
847 }
848
849 i = list_len(l);
850 if (semicolon == 0 && var_count < i)
851 {
852 emsg(_("E687: Less targets than List items"));
853 return FAIL;
854 }
855 if (var_count - semicolon > i)
856 {
857 emsg(_("E688: More targets than List items"));
858 return FAIL;
859 }
860
Bram Moolenaar50985eb2020-01-27 22:09:39 +0100861 range_list_materialize(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200862 item = l->lv_first;
863 while (*arg != ']')
864 {
865 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100866 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200867 item = item->li_next;
868 if (arg == NULL)
869 return FAIL;
870
871 arg = skipwhite(arg);
872 if (*arg == ';')
873 {
874 // Put the rest of the list (may be empty) in the var after ';'.
875 // Create a new list for this.
876 l = list_alloc();
877 if (l == NULL)
878 return FAIL;
879 while (item != NULL)
880 {
881 list_append_tv(l, &item->li_tv);
882 item = item->li_next;
883 }
884
885 ltv.v_type = VAR_LIST;
886 ltv.v_lock = 0;
887 ltv.vval.v_list = l;
888 l->lv_refcount = 1;
889
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200891 (char_u *)"]", op);
892 clear_tv(&ltv);
893 if (arg == NULL)
894 return FAIL;
895 break;
896 }
897 else if (*arg != ',' && *arg != ']')
898 {
899 internal_error("ex_let_vars()");
900 return FAIL;
901 }
902 }
903
904 return OK;
905}
906
907/*
908 * Skip over assignable variable "var" or list of variables "[var, var]".
909 * Used for ":let varvar = expr" and ":for varvar in expr".
910 * For "[var, var]" increment "*var_count" for each variable.
911 * for "[var, var; var]" set "semicolon".
912 * Return NULL for an error.
913 */
914 char_u *
915skip_var_list(
916 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100917 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200918 int *var_count,
919 int *semicolon)
920{
921 char_u *p, *s;
922
923 if (*arg == '[')
924 {
925 // "[var, var]": find the matching ']'.
926 p = arg;
927 for (;;)
928 {
929 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930 s = skip_var_one(p, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200931 if (s == p)
932 {
933 semsg(_(e_invarg2), p);
934 return NULL;
935 }
936 ++*var_count;
937
938 p = skipwhite(s);
939 if (*p == ']')
940 break;
941 else if (*p == ';')
942 {
943 if (*semicolon == 1)
944 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200945 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200946 return NULL;
947 }
948 *semicolon = 1;
949 }
950 else if (*p != ',')
951 {
952 semsg(_(e_invarg2), p);
953 return NULL;
954 }
955 }
956 return p + 1;
957 }
958 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200960}
961
962/*
963 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
964 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100965 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200966 */
967 static char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100968skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200969{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100970 char_u *end;
971
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200972 if (*arg == '@' && arg[1] != NUL)
973 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100974 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200975 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100976 if (include_type && current_sctx.sc_version == SCRIPT_VERSION_VIM9
977 && *end == ':')
978 {
979 end = skip_type(skipwhite(end + 1));
980 }
981 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200982}
983
984/*
985 * List variables for hashtab "ht" with prefix "prefix".
986 * If "empty" is TRUE also list NULL strings as empty strings.
987 */
988 void
989list_hashtable_vars(
990 hashtab_T *ht,
991 char *prefix,
992 int empty,
993 int *first)
994{
995 hashitem_T *hi;
996 dictitem_T *di;
997 int todo;
998 char_u buf[IOSIZE];
999
1000 todo = (int)ht->ht_used;
1001 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1002 {
1003 if (!HASHITEM_EMPTY(hi))
1004 {
1005 --todo;
1006 di = HI2DI(hi);
1007
1008 // apply :filter /pat/ to variable name
1009 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1010 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1011 if (message_filtered(buf))
1012 continue;
1013
1014 if (empty || di->di_tv.v_type != VAR_STRING
1015 || di->di_tv.vval.v_string != NULL)
1016 list_one_var(di, prefix, first);
1017 }
1018 }
1019}
1020
1021/*
1022 * List global variables.
1023 */
1024 static void
1025list_glob_vars(int *first)
1026{
1027 list_hashtable_vars(&globvarht, "", TRUE, first);
1028}
1029
1030/*
1031 * List buffer variables.
1032 */
1033 static void
1034list_buf_vars(int *first)
1035{
1036 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1037}
1038
1039/*
1040 * List window variables.
1041 */
1042 static void
1043list_win_vars(int *first)
1044{
1045 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1046}
1047
1048/*
1049 * List tab page variables.
1050 */
1051 static void
1052list_tab_vars(int *first)
1053{
1054 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1055}
1056
1057/*
1058 * List variables in "arg".
1059 */
1060 static char_u *
1061list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1062{
1063 int error = FALSE;
1064 int len;
1065 char_u *name;
1066 char_u *name_start;
1067 char_u *arg_subsc;
1068 char_u *tofree;
1069 typval_T tv;
1070
1071 while (!ends_excmd(*arg) && !got_int)
1072 {
1073 if (error || eap->skip)
1074 {
1075 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1076 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1077 {
1078 emsg_severe = TRUE;
1079 emsg(_(e_trailing));
1080 break;
1081 }
1082 }
1083 else
1084 {
1085 // get_name_len() takes care of expanding curly braces
1086 name_start = name = arg;
1087 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1088 if (len <= 0)
1089 {
1090 // This is mainly to keep test 49 working: when expanding
1091 // curly braces fails overrule the exception error message.
1092 if (len < 0 && !aborting())
1093 {
1094 emsg_severe = TRUE;
1095 semsg(_(e_invarg2), arg);
1096 break;
1097 }
1098 error = TRUE;
1099 }
1100 else
1101 {
1102 if (tofree != NULL)
1103 name = tofree;
1104 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
1105 error = TRUE;
1106 else
1107 {
1108 // handle d.key, l[idx], f(expr)
1109 arg_subsc = arg;
1110 if (handle_subscript(&arg, &tv, TRUE, TRUE,
1111 name, &name) == FAIL)
1112 error = TRUE;
1113 else
1114 {
1115 if (arg == arg_subsc && len == 2 && name[1] == ':')
1116 {
1117 switch (*name)
1118 {
1119 case 'g': list_glob_vars(first); break;
1120 case 'b': list_buf_vars(first); break;
1121 case 'w': list_win_vars(first); break;
1122 case 't': list_tab_vars(first); break;
1123 case 'v': list_vim_vars(first); break;
1124 case 's': list_script_vars(first); break;
1125 case 'l': list_func_vars(first); break;
1126 default:
1127 semsg(_("E738: Can't list variables for %s"), name);
1128 }
1129 }
1130 else
1131 {
1132 char_u numbuf[NUMBUFLEN];
1133 char_u *tf;
1134 int c;
1135 char_u *s;
1136
1137 s = echo_string(&tv, &tf, numbuf, 0);
1138 c = *arg;
1139 *arg = NUL;
1140 list_one_var_a("",
1141 arg == arg_subsc ? name : name_start,
1142 tv.v_type,
1143 s == NULL ? (char_u *)"" : s,
1144 first);
1145 *arg = c;
1146 vim_free(tf);
1147 }
1148 clear_tv(&tv);
1149 }
1150 }
1151 }
1152
1153 vim_free(tofree);
1154 }
1155
1156 arg = skipwhite(arg);
1157 }
1158
1159 return arg;
1160}
1161
1162/*
1163 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1164 * Returns a pointer to the char just after the var name.
1165 * Returns NULL if there is an error.
1166 */
1167 static char_u *
1168ex_let_one(
1169 char_u *arg, // points to variable name
1170 typval_T *tv, // value to assign to variable
1171 int copy, // copy value from "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001172 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001173 char_u *endchars, // valid chars after variable name or NULL
1174 char_u *op) // "+", "-", "." or NULL
1175{
1176 int c1;
1177 char_u *name;
1178 char_u *p;
1179 char_u *arg_end = NULL;
1180 int len;
1181 int opt_flags;
1182 char_u *tofree = NULL;
1183
1184 // ":let $VAR = expr": Set environment variable.
1185 if (*arg == '$')
1186 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001187 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001188 {
1189 emsg(_("E996: Cannot lock an environment variable"));
1190 return NULL;
1191 }
1192 // Find the end of the name.
1193 ++arg;
1194 name = arg;
1195 len = get_env_len(&arg);
1196 if (len == 0)
1197 semsg(_(e_invarg2), name - 1);
1198 else
1199 {
1200 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1201 semsg(_(e_letwrong), op);
1202 else if (endchars != NULL
1203 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1204 emsg(_(e_letunexp));
1205 else if (!check_secure())
1206 {
1207 c1 = name[len];
1208 name[len] = NUL;
1209 p = tv_get_string_chk(tv);
1210 if (p != NULL && op != NULL && *op == '.')
1211 {
1212 int mustfree = FALSE;
1213 char_u *s = vim_getenv(name, &mustfree);
1214
1215 if (s != NULL)
1216 {
1217 p = tofree = concat_str(s, p);
1218 if (mustfree)
1219 vim_free(s);
1220 }
1221 }
1222 if (p != NULL)
1223 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001224 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001225 arg_end = arg;
1226 }
1227 name[len] = c1;
1228 vim_free(tofree);
1229 }
1230 }
1231 }
1232
1233 // ":let &option = expr": Set option value.
1234 // ":let &l:option = expr": Set local option value.
1235 // ":let &g:option = expr": Set global option value.
1236 else if (*arg == '&')
1237 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001238 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001239 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001241 return NULL;
1242 }
1243 // Find the end of the name.
1244 p = find_option_end(&arg, &opt_flags);
1245 if (p == NULL || (endchars != NULL
1246 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1247 emsg(_(e_letunexp));
1248 else
1249 {
1250 long n;
1251 int opt_type;
1252 long numval;
1253 char_u *stringval = NULL;
1254 char_u *s;
1255
1256 c1 = *p;
1257 *p = NUL;
1258
1259 n = (long)tv_get_number(tv);
1260 s = tv_get_string_chk(tv); // != NULL if number or string
1261 if (s != NULL && op != NULL && *op != '=')
1262 {
1263 opt_type = get_option_value(arg, &numval,
1264 &stringval, opt_flags);
1265 if ((opt_type == 1 && *op == '.')
1266 || (opt_type == 0 && *op != '.'))
1267 {
1268 semsg(_(e_letwrong), op);
1269 s = NULL; // don't set the value
1270 }
1271 else
1272 {
1273 if (opt_type == 1) // number
1274 {
1275 switch (*op)
1276 {
1277 case '+': n = numval + n; break;
1278 case '-': n = numval - n; break;
1279 case '*': n = numval * n; break;
1280 case '/': n = (long)num_divide(numval, n); break;
1281 case '%': n = (long)num_modulus(numval, n); break;
1282 }
1283 }
1284 else if (opt_type == 0 && stringval != NULL) // string
1285 {
1286 s = concat_str(stringval, s);
1287 vim_free(stringval);
1288 stringval = s;
1289 }
1290 }
1291 }
1292 if (s != NULL)
1293 {
1294 set_option_value(arg, n, s, opt_flags);
1295 arg_end = p;
1296 }
1297 *p = c1;
1298 vim_free(stringval);
1299 }
1300 }
1301
1302 // ":let @r = expr": Set register contents.
1303 else if (*arg == '@')
1304 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001306 {
1307 emsg(_("E996: Cannot lock a register"));
1308 return NULL;
1309 }
1310 ++arg;
1311 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1312 semsg(_(e_letwrong), op);
1313 else if (endchars != NULL
1314 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1315 emsg(_(e_letunexp));
1316 else
1317 {
1318 char_u *ptofree = NULL;
1319 char_u *s;
1320
1321 p = tv_get_string_chk(tv);
1322 if (p != NULL && op != NULL && *op == '.')
1323 {
1324 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1325 if (s != NULL)
1326 {
1327 p = ptofree = concat_str(s, p);
1328 vim_free(s);
1329 }
1330 }
1331 if (p != NULL)
1332 {
1333 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1334 arg_end = arg + 1;
1335 }
1336 vim_free(ptofree);
1337 }
1338 }
1339
1340 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001342 // ":let {expr} = expr": Idem, name made with curly braces
1343 else if (eval_isnamec1(*arg) || *arg == '{')
1344 {
1345 lval_T lv;
1346
1347 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
1348 if (p != NULL && lv.ll_name != NULL)
1349 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350 if (endchars != NULL && vim_strchr(endchars,
1351 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001352 emsg(_(e_letunexp));
1353 else
1354 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001356 arg_end = p;
1357 }
1358 }
1359 clear_lval(&lv);
1360 }
1361
1362 else
1363 semsg(_(e_invarg2), arg);
1364
1365 return arg_end;
1366}
1367
1368/*
1369 * ":unlet[!] var1 ... " command.
1370 */
1371 void
1372ex_unlet(exarg_T *eap)
1373{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001374 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001375}
1376
1377/*
1378 * ":lockvar" and ":unlockvar" commands
1379 */
1380 void
1381ex_lockvar(exarg_T *eap)
1382{
1383 char_u *arg = eap->arg;
1384 int deep = 2;
1385
1386 if (eap->forceit)
1387 deep = -1;
1388 else if (vim_isdigit(*arg))
1389 {
1390 deep = getdigits(&arg);
1391 arg = skipwhite(arg);
1392 }
1393
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001394 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001395}
1396
1397/*
1398 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001399 * Also used for Vim9 script. "callback" is invoked as:
1400 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001401 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001402 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001403ex_unletlock(
1404 exarg_T *eap,
1405 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001406 int deep,
1407 int glv_flags,
1408 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1409 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001410{
1411 char_u *arg = argstart;
1412 char_u *name_end;
1413 int error = FALSE;
1414 lval_T lv;
1415
1416 do
1417 {
1418 if (*arg == '$')
1419 {
1420 char_u *name = ++arg;
1421
1422 if (get_env_len(&arg) == 0)
1423 {
1424 semsg(_(e_invarg2), name - 1);
1425 return;
1426 }
1427 vim_unsetenv(name);
1428 arg = skipwhite(arg);
1429 continue;
1430 }
1431
1432 // Parse the name and find the end.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001433 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1434 glv_flags, FNE_CHECK_START);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001435 if (lv.ll_name == NULL)
1436 error = TRUE; // error but continue parsing
1437 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1438 && !ends_excmd(*name_end)))
1439 {
1440 if (name_end != NULL)
1441 {
1442 emsg_severe = TRUE;
1443 emsg(_(e_trailing));
1444 }
1445 if (!(eap->skip || error))
1446 clear_lval(&lv);
1447 break;
1448 }
1449
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001450 if (!error && !eap->skip
1451 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
1452 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001453
1454 if (!eap->skip)
1455 clear_lval(&lv);
1456
1457 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001458 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001459
1460 eap->nextcmd = check_nextcmd(arg);
1461}
1462
1463 static int
1464do_unlet_var(
1465 lval_T *lp,
1466 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001467 exarg_T *eap,
1468 int deep UNUSED,
1469 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001470{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001471 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001472 int ret = OK;
1473 int cc;
1474
1475 if (lp->ll_tv == NULL)
1476 {
1477 cc = *name_end;
1478 *name_end = NUL;
1479
1480 // Normal name or expanded name.
1481 if (do_unlet(lp->ll_name, forceit) == FAIL)
1482 ret = FAIL;
1483 *name_end = cc;
1484 }
1485 else if ((lp->ll_list != NULL
1486 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
1487 || (lp->ll_dict != NULL
1488 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
1489 return FAIL;
1490 else if (lp->ll_range)
1491 {
1492 listitem_T *li;
1493 listitem_T *ll_li = lp->ll_li;
1494 int ll_n1 = lp->ll_n1;
1495
1496 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1497 {
1498 li = ll_li->li_next;
1499 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
1500 return FAIL;
1501 ll_li = li;
1502 ++ll_n1;
1503 }
1504
1505 // Delete a range of List items.
1506 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1507 {
1508 li = lp->ll_li->li_next;
1509 listitem_remove(lp->ll_list, lp->ll_li);
1510 lp->ll_li = li;
1511 ++lp->ll_n1;
1512 }
1513 }
1514 else
1515 {
1516 if (lp->ll_list != NULL)
1517 // unlet a List item.
1518 listitem_remove(lp->ll_list, lp->ll_li);
1519 else
1520 // unlet a Dictionary item.
1521 dictitem_remove(lp->ll_dict, lp->ll_di);
1522 }
1523
1524 return ret;
1525}
1526
1527/*
1528 * "unlet" a variable. Return OK if it existed, FAIL if not.
1529 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1530 */
1531 int
1532do_unlet(char_u *name, int forceit)
1533{
1534 hashtab_T *ht;
1535 hashitem_T *hi;
1536 char_u *varname;
1537 dict_T *d;
1538 dictitem_T *di;
1539
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001540 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1541 && check_vim9_unlet(name) == FAIL)
1542 return FAIL;
1543
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001544 ht = find_var_ht(name, &varname);
1545 if (ht != NULL && *varname != NUL)
1546 {
1547 d = get_current_funccal_dict(ht);
1548 if (d == NULL)
1549 {
1550 if (ht == &globvarht)
1551 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001552 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001553 d = &vimvardict;
1554 else
1555 {
1556 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1557 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1558 }
1559 if (d == NULL)
1560 {
1561 internal_error("do_unlet()");
1562 return FAIL;
1563 }
1564 }
1565 hi = hash_find(ht, varname);
1566 if (HASHITEM_EMPTY(hi))
1567 hi = find_hi_in_scoped_ht(name, &ht);
1568 if (hi != NULL && !HASHITEM_EMPTY(hi))
1569 {
1570 di = HI2DI(hi);
1571 if (var_check_fixed(di->di_flags, name, FALSE)
1572 || var_check_ro(di->di_flags, name, FALSE)
1573 || var_check_lock(d->dv_lock, name, FALSE))
1574 return FAIL;
1575
1576 delete_var(ht, hi);
1577 return OK;
1578 }
1579 }
1580 if (forceit)
1581 return OK;
1582 semsg(_("E108: No such variable: \"%s\""), name);
1583 return FAIL;
1584}
1585
1586/*
1587 * Lock or unlock variable indicated by "lp".
1588 * "deep" is the levels to go (-1 for unlimited);
1589 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1590 */
1591 static int
1592do_lock_var(
1593 lval_T *lp,
1594 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001595 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001596 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001597 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001598{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001599 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001600 int ret = OK;
1601 int cc;
1602 dictitem_T *di;
1603
1604 if (deep == 0) // nothing to do
1605 return OK;
1606
1607 if (lp->ll_tv == NULL)
1608 {
1609 cc = *name_end;
1610 *name_end = NUL;
1611
1612 // Normal name or expanded name.
1613 di = find_var(lp->ll_name, NULL, TRUE);
1614 if (di == NULL)
1615 ret = FAIL;
1616 else if ((di->di_flags & DI_FLAGS_FIX)
1617 && di->di_tv.v_type != VAR_DICT
1618 && di->di_tv.v_type != VAR_LIST)
1619 // For historic reasons this error is not given for a list or dict.
1620 // E.g., the b: dict could be locked/unlocked.
1621 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
1622 else
1623 {
1624 if (lock)
1625 di->di_flags |= DI_FLAGS_LOCK;
1626 else
1627 di->di_flags &= ~DI_FLAGS_LOCK;
1628 item_lock(&di->di_tv, deep, lock);
1629 }
1630 *name_end = cc;
1631 }
1632 else if (lp->ll_range)
1633 {
1634 listitem_T *li = lp->ll_li;
1635
1636 // (un)lock a range of List items.
1637 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1638 {
1639 item_lock(&li->li_tv, deep, lock);
1640 li = li->li_next;
1641 ++lp->ll_n1;
1642 }
1643 }
1644 else if (lp->ll_list != NULL)
1645 // (un)lock a List item.
1646 item_lock(&lp->ll_li->li_tv, deep, lock);
1647 else
1648 // (un)lock a Dictionary item.
1649 item_lock(&lp->ll_di->di_tv, deep, lock);
1650
1651 return ret;
1652}
1653
1654/*
1655 * Lock or unlock an item. "deep" is nr of levels to go.
1656 */
1657 static void
1658item_lock(typval_T *tv, int deep, int lock)
1659{
1660 static int recurse = 0;
1661 list_T *l;
1662 listitem_T *li;
1663 dict_T *d;
1664 blob_T *b;
1665 hashitem_T *hi;
1666 int todo;
1667
1668 if (recurse >= DICT_MAXNEST)
1669 {
1670 emsg(_("E743: variable nested too deep for (un)lock"));
1671 return;
1672 }
1673 if (deep == 0)
1674 return;
1675 ++recurse;
1676
1677 // lock/unlock the item itself
1678 if (lock)
1679 tv->v_lock |= VAR_LOCKED;
1680 else
1681 tv->v_lock &= ~VAR_LOCKED;
1682
1683 switch (tv->v_type)
1684 {
1685 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001686 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001688 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001690 case VAR_STRING:
1691 case VAR_FUNC:
1692 case VAR_PARTIAL:
1693 case VAR_FLOAT:
1694 case VAR_SPECIAL:
1695 case VAR_JOB:
1696 case VAR_CHANNEL:
1697 break;
1698
1699 case VAR_BLOB:
1700 if ((b = tv->vval.v_blob) != NULL)
1701 {
1702 if (lock)
1703 b->bv_lock |= VAR_LOCKED;
1704 else
1705 b->bv_lock &= ~VAR_LOCKED;
1706 }
1707 break;
1708 case VAR_LIST:
1709 if ((l = tv->vval.v_list) != NULL)
1710 {
1711 if (lock)
1712 l->lv_lock |= VAR_LOCKED;
1713 else
1714 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001715 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001716 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001717 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001718 item_lock(&li->li_tv, deep - 1, lock);
1719 }
1720 break;
1721 case VAR_DICT:
1722 if ((d = tv->vval.v_dict) != NULL)
1723 {
1724 if (lock)
1725 d->dv_lock |= VAR_LOCKED;
1726 else
1727 d->dv_lock &= ~VAR_LOCKED;
1728 if (deep < 0 || deep > 1)
1729 {
1730 // recursive: lock/unlock the items the List contains
1731 todo = (int)d->dv_hashtab.ht_used;
1732 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1733 {
1734 if (!HASHITEM_EMPTY(hi))
1735 {
1736 --todo;
1737 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
1738 }
1739 }
1740 }
1741 }
1742 }
1743 --recurse;
1744}
1745
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001746#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1747/*
1748 * Delete all "menutrans_" variables.
1749 */
1750 void
1751del_menutrans_vars(void)
1752{
1753 hashitem_T *hi;
1754 int todo;
1755
1756 hash_lock(&globvarht);
1757 todo = (int)globvarht.ht_used;
1758 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1759 {
1760 if (!HASHITEM_EMPTY(hi))
1761 {
1762 --todo;
1763 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1764 delete_var(&globvarht, hi);
1765 }
1766 }
1767 hash_unlock(&globvarht);
1768}
1769#endif
1770
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001771/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001772 * Local string buffer for the next two functions to store a variable name
1773 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1774 * get_user_var_name().
1775 */
1776
1777static char_u *varnamebuf = NULL;
1778static int varnamebuflen = 0;
1779
1780/*
1781 * Function to concatenate a prefix and a variable name.
1782 */
1783 static char_u *
1784cat_prefix_varname(int prefix, char_u *name)
1785{
1786 int len;
1787
1788 len = (int)STRLEN(name) + 3;
1789 if (len > varnamebuflen)
1790 {
1791 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001792 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001793 varnamebuf = alloc(len);
1794 if (varnamebuf == NULL)
1795 {
1796 varnamebuflen = 0;
1797 return NULL;
1798 }
1799 varnamebuflen = len;
1800 }
1801 *varnamebuf = prefix;
1802 varnamebuf[1] = ':';
1803 STRCPY(varnamebuf + 2, name);
1804 return varnamebuf;
1805}
1806
1807/*
1808 * Function given to ExpandGeneric() to obtain the list of user defined
1809 * (global/buffer/window/built-in) variable names.
1810 */
1811 char_u *
1812get_user_var_name(expand_T *xp, int idx)
1813{
1814 static long_u gdone;
1815 static long_u bdone;
1816 static long_u wdone;
1817 static long_u tdone;
1818 static int vidx;
1819 static hashitem_T *hi;
1820 hashtab_T *ht;
1821
1822 if (idx == 0)
1823 {
1824 gdone = bdone = wdone = vidx = 0;
1825 tdone = 0;
1826 }
1827
1828 // Global variables
1829 if (gdone < globvarht.ht_used)
1830 {
1831 if (gdone++ == 0)
1832 hi = globvarht.ht_array;
1833 else
1834 ++hi;
1835 while (HASHITEM_EMPTY(hi))
1836 ++hi;
1837 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1838 return cat_prefix_varname('g', hi->hi_key);
1839 return hi->hi_key;
1840 }
1841
1842 // b: variables
1843 ht = &curbuf->b_vars->dv_hashtab;
1844 if (bdone < ht->ht_used)
1845 {
1846 if (bdone++ == 0)
1847 hi = ht->ht_array;
1848 else
1849 ++hi;
1850 while (HASHITEM_EMPTY(hi))
1851 ++hi;
1852 return cat_prefix_varname('b', hi->hi_key);
1853 }
1854
1855 // w: variables
1856 ht = &curwin->w_vars->dv_hashtab;
1857 if (wdone < ht->ht_used)
1858 {
1859 if (wdone++ == 0)
1860 hi = ht->ht_array;
1861 else
1862 ++hi;
1863 while (HASHITEM_EMPTY(hi))
1864 ++hi;
1865 return cat_prefix_varname('w', hi->hi_key);
1866 }
1867
1868 // t: variables
1869 ht = &curtab->tp_vars->dv_hashtab;
1870 if (tdone < ht->ht_used)
1871 {
1872 if (tdone++ == 0)
1873 hi = ht->ht_array;
1874 else
1875 ++hi;
1876 while (HASHITEM_EMPTY(hi))
1877 ++hi;
1878 return cat_prefix_varname('t', hi->hi_key);
1879 }
1880
1881 // v: variables
1882 if (vidx < VV_LEN)
1883 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
1884
1885 VIM_CLEAR(varnamebuf);
1886 varnamebuflen = 0;
1887 return NULL;
1888}
1889
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001890 char *
1891get_var_special_name(int nr)
1892{
1893 switch (nr)
1894 {
1895 case VVAL_FALSE: return "v:false";
1896 case VVAL_TRUE: return "v:true";
1897 case VVAL_NONE: return "v:none";
1898 case VVAL_NULL: return "v:null";
1899 }
1900 internal_error("get_var_special_name()");
1901 return "42";
1902}
1903
1904/*
1905 * Returns the global variable dictionary
1906 */
1907 dict_T *
1908get_globvar_dict(void)
1909{
1910 return &globvardict;
1911}
1912
1913/*
1914 * Returns the global variable hash table
1915 */
1916 hashtab_T *
1917get_globvar_ht(void)
1918{
1919 return &globvarht;
1920}
1921
1922/*
1923 * Returns the v: variable dictionary
1924 */
1925 dict_T *
1926get_vimvar_dict(void)
1927{
1928 return &vimvardict;
1929}
1930
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001931/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001933 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001934 */
1935 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001936find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001938 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
1939 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001940
1941 if (di == NULL)
1942 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001943 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
1945 return (int)(vv - vimvars);
1946}
1947
1948
1949/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02001950 * Set type of v: variable to "type".
1951 */
1952 void
1953set_vim_var_type(int idx, vartype_T type)
1954{
1955 vimvars[idx].vv_type = type;
1956}
1957
1958/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001959 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001960 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001961 */
1962 void
1963set_vim_var_nr(int idx, varnumber_T val)
1964{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001965 vimvars[idx].vv_nr = val;
1966}
1967
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 char *
1969get_vim_var_name(int idx)
1970{
1971 return vimvars[idx].vv_name;
1972}
1973
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001974/*
1975 * Get typval_T v: variable value.
1976 */
1977 typval_T *
1978get_vim_var_tv(int idx)
1979{
1980 return &vimvars[idx].vv_tv;
1981}
1982
1983/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001984 * Set v: variable to "tv". Only accepts the same type.
1985 * Takes over the value of "tv".
1986 */
1987 int
1988set_vim_var_tv(int idx, typval_T *tv)
1989{
1990 if (vimvars[idx].vv_type != tv->v_type)
1991 {
1992 emsg(_("E1063: type mismatch for v: variable"));
1993 clear_tv(tv);
1994 return FAIL;
1995 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02001996 // VV_RO is also checked when compiling, but let's check here as well.
1997 if (vimvars[idx].vv_flags & VV_RO)
1998 {
1999 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2000 return FAIL;
2001 }
2002 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2003 {
2004 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2005 return FAIL;
2006 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002007 clear_tv(&vimvars[idx].vv_di.di_tv);
2008 vimvars[idx].vv_di.di_tv = *tv;
2009 return OK;
2010}
2011
2012/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002013 * Get number v: variable value.
2014 */
2015 varnumber_T
2016get_vim_var_nr(int idx)
2017{
2018 return vimvars[idx].vv_nr;
2019}
2020
2021/*
2022 * Get string v: variable value. Uses a static buffer, can only be used once.
2023 * If the String variable has never been set, return an empty string.
2024 * Never returns NULL;
2025 */
2026 char_u *
2027get_vim_var_str(int idx)
2028{
2029 return tv_get_string(&vimvars[idx].vv_tv);
2030}
2031
2032/*
2033 * Get List v: variable value. Caller must take care of reference count when
2034 * needed.
2035 */
2036 list_T *
2037get_vim_var_list(int idx)
2038{
2039 return vimvars[idx].vv_list;
2040}
2041
2042/*
2043 * Get Dict v: variable value. Caller must take care of reference count when
2044 * needed.
2045 */
2046 dict_T *
2047get_vim_var_dict(int idx)
2048{
2049 return vimvars[idx].vv_dict;
2050}
2051
2052/*
2053 * Set v:char to character "c".
2054 */
2055 void
2056set_vim_var_char(int c)
2057{
2058 char_u buf[MB_MAXBYTES + 1];
2059
2060 if (has_mbyte)
2061 buf[(*mb_char2bytes)(c, buf)] = NUL;
2062 else
2063 {
2064 buf[0] = c;
2065 buf[1] = NUL;
2066 }
2067 set_vim_var_string(VV_CHAR, buf, -1);
2068}
2069
2070/*
2071 * Set v:count to "count" and v:count1 to "count1".
2072 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2073 */
2074 void
2075set_vcount(
2076 long count,
2077 long count1,
2078 int set_prevcount)
2079{
2080 if (set_prevcount)
2081 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2082 vimvars[VV_COUNT].vv_nr = count;
2083 vimvars[VV_COUNT1].vv_nr = count1;
2084}
2085
2086/*
2087 * Save variables that might be changed as a side effect. Used when executing
2088 * a timer callback.
2089 */
2090 void
2091save_vimvars(vimvars_save_T *vvsave)
2092{
2093 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2094 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2095 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2096}
2097
2098/*
2099 * Restore variables saved by save_vimvars().
2100 */
2101 void
2102restore_vimvars(vimvars_save_T *vvsave)
2103{
2104 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2105 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2106 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2107}
2108
2109/*
2110 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2111 * value.
2112 */
2113 void
2114set_vim_var_string(
2115 int idx,
2116 char_u *val,
2117 int len) // length of "val" to use or -1 (whole string)
2118{
2119 clear_tv(&vimvars[idx].vv_di.di_tv);
2120 vimvars[idx].vv_type = VAR_STRING;
2121 if (val == NULL)
2122 vimvars[idx].vv_str = NULL;
2123 else if (len == -1)
2124 vimvars[idx].vv_str = vim_strsave(val);
2125 else
2126 vimvars[idx].vv_str = vim_strnsave(val, len);
2127}
2128
2129/*
2130 * Set List v: variable to "val".
2131 */
2132 void
2133set_vim_var_list(int idx, list_T *val)
2134{
2135 clear_tv(&vimvars[idx].vv_di.di_tv);
2136 vimvars[idx].vv_type = VAR_LIST;
2137 vimvars[idx].vv_list = val;
2138 if (val != NULL)
2139 ++val->lv_refcount;
2140}
2141
2142/*
2143 * Set Dictionary v: variable to "val".
2144 */
2145 void
2146set_vim_var_dict(int idx, dict_T *val)
2147{
2148 clear_tv(&vimvars[idx].vv_di.di_tv);
2149 vimvars[idx].vv_type = VAR_DICT;
2150 vimvars[idx].vv_dict = val;
2151 if (val != NULL)
2152 {
2153 ++val->dv_refcount;
2154 dict_set_items_ro(val);
2155 }
2156}
2157
2158/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002159 * Set the v:argv list.
2160 */
2161 void
2162set_argv_var(char **argv, int argc)
2163{
2164 list_T *l = list_alloc();
2165 int i;
2166
2167 if (l == NULL)
2168 getout(1);
2169 l->lv_lock = VAR_FIXED;
2170 for (i = 0; i < argc; ++i)
2171 {
2172 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2173 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002174 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002175 }
2176 set_vim_var_list(VV_ARGV, l);
2177}
2178
2179/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002180 * Set v:register if needed.
2181 */
2182 void
2183set_reg_var(int c)
2184{
2185 char_u regname;
2186
2187 if (c == 0 || c == ' ')
2188 regname = '"';
2189 else
2190 regname = c;
2191 // Avoid free/alloc when the value is already right.
2192 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2193 set_vim_var_string(VV_REG, &regname, 1);
2194}
2195
2196/*
2197 * Get or set v:exception. If "oldval" == NULL, return the current value.
2198 * Otherwise, restore the value to "oldval" and return NULL.
2199 * Must always be called in pairs to save and restore v:exception! Does not
2200 * take care of memory allocations.
2201 */
2202 char_u *
2203v_exception(char_u *oldval)
2204{
2205 if (oldval == NULL)
2206 return vimvars[VV_EXCEPTION].vv_str;
2207
2208 vimvars[VV_EXCEPTION].vv_str = oldval;
2209 return NULL;
2210}
2211
2212/*
2213 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2214 * Otherwise, restore the value to "oldval" and return NULL.
2215 * Must always be called in pairs to save and restore v:throwpoint! Does not
2216 * take care of memory allocations.
2217 */
2218 char_u *
2219v_throwpoint(char_u *oldval)
2220{
2221 if (oldval == NULL)
2222 return vimvars[VV_THROWPOINT].vv_str;
2223
2224 vimvars[VV_THROWPOINT].vv_str = oldval;
2225 return NULL;
2226}
2227
2228/*
2229 * Set v:cmdarg.
2230 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2231 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2232 * Must always be called in pairs!
2233 */
2234 char_u *
2235set_cmdarg(exarg_T *eap, char_u *oldarg)
2236{
2237 char_u *oldval;
2238 char_u *newval;
2239 unsigned len;
2240
2241 oldval = vimvars[VV_CMDARG].vv_str;
2242 if (eap == NULL)
2243 {
2244 vim_free(oldval);
2245 vimvars[VV_CMDARG].vv_str = oldarg;
2246 return NULL;
2247 }
2248
2249 if (eap->force_bin == FORCE_BIN)
2250 len = 6;
2251 else if (eap->force_bin == FORCE_NOBIN)
2252 len = 8;
2253 else
2254 len = 0;
2255
2256 if (eap->read_edit)
2257 len += 7;
2258
2259 if (eap->force_ff != 0)
2260 len += 10; // " ++ff=unix"
2261 if (eap->force_enc != 0)
2262 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2263 if (eap->bad_char != 0)
2264 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2265
2266 newval = alloc(len + 1);
2267 if (newval == NULL)
2268 return NULL;
2269
2270 if (eap->force_bin == FORCE_BIN)
2271 sprintf((char *)newval, " ++bin");
2272 else if (eap->force_bin == FORCE_NOBIN)
2273 sprintf((char *)newval, " ++nobin");
2274 else
2275 *newval = NUL;
2276
2277 if (eap->read_edit)
2278 STRCAT(newval, " ++edit");
2279
2280 if (eap->force_ff != 0)
2281 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2282 eap->force_ff == 'u' ? "unix"
2283 : eap->force_ff == 'd' ? "dos"
2284 : "mac");
2285 if (eap->force_enc != 0)
2286 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2287 eap->cmd + eap->force_enc);
2288 if (eap->bad_char == BAD_KEEP)
2289 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2290 else if (eap->bad_char == BAD_DROP)
2291 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2292 else if (eap->bad_char != 0)
2293 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2294 vimvars[VV_CMDARG].vv_str = newval;
2295 return oldval;
2296}
2297
2298/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002299 * Get the value of internal variable "name".
2300 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2301 */
2302 int
2303get_var_tv(
2304 char_u *name,
2305 int len, // length of "name"
2306 typval_T *rettv, // NULL when only checking existence
2307 dictitem_T **dip, // non-NULL when typval's dict item is needed
2308 int verbose, // may give error message
2309 int no_autoload) // do not use script autoloading
2310{
2311 int ret = OK;
2312 typval_T *tv = NULL;
2313 dictitem_T *v;
2314 int cc;
2315
2316 // truncate the name, so that we can use strcmp()
2317 cc = name[len];
2318 name[len] = NUL;
2319
2320 // Check for user-defined variables.
2321 v = find_var(name, NULL, no_autoload);
2322 if (v != NULL)
2323 {
2324 tv = &v->di_tv;
2325 if (dip != NULL)
2326 *dip = v;
2327 }
2328
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002329 if (tv == NULL && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2330 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002331 imported_T *import = find_imported(name, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332
2333 // imported variable from another script
2334 if (import != NULL)
2335 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002336 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2338 + import->imp_var_vals_idx;
2339 tv = sv->sv_tv;
2340 }
2341 }
2342
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002343 if (tv == NULL)
2344 {
2345 if (rettv != NULL && verbose)
2346 semsg(_(e_undefvar), name);
2347 ret = FAIL;
2348 }
2349 else if (rettv != NULL)
2350 copy_tv(tv, rettv);
2351
2352 name[len] = cc;
2353
2354 return ret;
2355}
2356
2357/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002358 * Check if variable "name[len]" is a local variable or an argument.
2359 * If so, "*eval_lavars_used" is set to TRUE.
2360 */
2361 void
2362check_vars(char_u *name, int len)
2363{
2364 int cc;
2365 char_u *varname;
2366 hashtab_T *ht;
2367
2368 if (eval_lavars_used == NULL)
2369 return;
2370
2371 // truncate the name, so that we can use strcmp()
2372 cc = name[len];
2373 name[len] = NUL;
2374
2375 ht = find_var_ht(name, &varname);
2376 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2377 {
2378 if (find_var(name, NULL, TRUE) != NULL)
2379 *eval_lavars_used = TRUE;
2380 }
2381
2382 name[len] = cc;
2383}
2384
2385/*
2386 * Find variable "name" in the list of variables.
2387 * Return a pointer to it if found, NULL if not found.
2388 * Careful: "a:0" variables don't have a name.
2389 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2390 * hashtab_T used.
2391 */
2392 dictitem_T *
2393find_var(char_u *name, hashtab_T **htp, int no_autoload)
2394{
2395 char_u *varname;
2396 hashtab_T *ht;
2397 dictitem_T *ret = NULL;
2398
2399 ht = find_var_ht(name, &varname);
2400 if (htp != NULL)
2401 *htp = ht;
2402 if (ht == NULL)
2403 return NULL;
2404 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2405 if (ret != NULL)
2406 return ret;
2407
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002408 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002409 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2410}
2411
2412/*
2413 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002414 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002415 * Returns NULL if not found.
2416 */
2417 dictitem_T *
2418find_var_in_ht(
2419 hashtab_T *ht,
2420 int htname,
2421 char_u *varname,
2422 int no_autoload)
2423{
2424 hashitem_T *hi;
2425
2426 if (*varname == NUL)
2427 {
2428 // Must be something like "s:", otherwise "ht" would be NULL.
2429 switch (htname)
2430 {
2431 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2432 case 'g': return &globvars_var;
2433 case 'v': return &vimvars_var;
2434 case 'b': return &curbuf->b_bufvar;
2435 case 'w': return &curwin->w_winvar;
2436 case 't': return &curtab->tp_winvar;
2437 case 'l': return get_funccal_local_var();
2438 case 'a': return get_funccal_args_var();
2439 }
2440 return NULL;
2441 }
2442
2443 hi = hash_find(ht, varname);
2444 if (HASHITEM_EMPTY(hi))
2445 {
2446 // For global variables we may try auto-loading the script. If it
2447 // worked find the variable again. Don't auto-load a script if it was
2448 // loaded already, otherwise it would be loaded every time when
2449 // checking if a function name is a Funcref variable.
2450 if (ht == &globvarht && !no_autoload)
2451 {
2452 // Note: script_autoload() may make "hi" invalid. It must either
2453 // be obtained again or not used.
2454 if (!script_autoload(varname, FALSE) || aborting())
2455 return NULL;
2456 hi = hash_find(ht, varname);
2457 }
2458 if (HASHITEM_EMPTY(hi))
2459 return NULL;
2460 }
2461 return HI2DI(hi);
2462}
2463
2464/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002465 * Get the script-local hashtab. NULL if not in a script context.
2466 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02002467 static hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002468get_script_local_ht(void)
2469{
2470 scid_T sid = current_sctx.sc_sid;
2471
2472 if (sid > 0 && sid <= script_items.ga_len)
2473 return &SCRIPT_VARS(sid);
2474 return NULL;
2475}
2476
2477/*
2478 * Look for "name[len]" in script-local variables.
2479 * Return -1 when not found.
2480 */
2481 int
2482lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2483{
2484 hashtab_T *ht = get_script_local_ht();
2485 char_u buffer[30];
2486 char_u *p;
2487 int res;
2488 hashitem_T *hi;
2489
2490 if (ht == NULL)
2491 return -1;
2492 if (len < sizeof(buffer) - 1)
2493 {
2494 vim_strncpy(buffer, name, len);
2495 p = buffer;
2496 }
2497 else
2498 {
2499 p = vim_strnsave(name, (int)len);
2500 if (p == NULL)
2501 return -1;
2502 }
2503
2504 hi = hash_find(ht, p);
2505 res = HASHITEM_EMPTY(hi) ? -1 : 1;
2506
2507 // if not script-local, then perhaps imported
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002508 if (res == -1 && find_imported(p, 0, NULL) != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 res = 1;
2510
2511 if (p != buffer)
2512 vim_free(p);
2513 return res;
2514}
2515
2516/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002517 * Find the hashtab used for a variable name.
2518 * Return NULL if the name is not valid.
2519 * Set "varname" to the start of name without ':'.
2520 */
2521 hashtab_T *
2522find_var_ht(char_u *name, char_u **varname)
2523{
2524 hashitem_T *hi;
2525 hashtab_T *ht;
2526
2527 if (name[0] == NUL)
2528 return NULL;
2529 if (name[1] != ':')
2530 {
2531 // The name must not start with a colon or #.
2532 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2533 return NULL;
2534 *varname = name;
2535
2536 // "version" is "v:version" in all scopes if scriptversion < 3.
2537 // Same for a few other variables marked with VV_COMPAT.
2538 if (current_sctx.sc_version < 3)
2539 {
2540 hi = hash_find(&compat_hashtab, name);
2541 if (!HASHITEM_EMPTY(hi))
2542 return &compat_hashtab;
2543 }
2544
2545 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546 if (ht != NULL)
2547 return ht; // local variable
2548
2549 // in Vim9 script items at the script level are script-local
2550 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2551 {
2552 ht = get_script_local_ht();
2553 if (ht != NULL)
2554 return ht;
2555 }
2556
2557 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002558 }
2559 *varname = name + 2;
2560 if (*name == 'g') // global variable
2561 return &globvarht;
2562 // There must be no ':' or '#' in the rest of the name, unless g: is used
2563 if (vim_strchr(name + 2, ':') != NULL
2564 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2565 return NULL;
2566 if (*name == 'b') // buffer variable
2567 return &curbuf->b_vars->dv_hashtab;
2568 if (*name == 'w') // window variable
2569 return &curwin->w_vars->dv_hashtab;
2570 if (*name == 't') // tab page variable
2571 return &curtab->tp_vars->dv_hashtab;
2572 if (*name == 'v') // v: variable
2573 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002574 if (get_current_funccal() != NULL
2575 && get_current_funccal()->func->uf_dfunc_idx < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002577 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578 if (*name == 'a') // a: function argument
2579 return get_funccal_args_ht();
2580 if (*name == 'l') // l: local function variable
2581 return get_funccal_local_ht();
2582 }
2583 if (*name == 's') // script variable
2584 {
2585 ht = get_script_local_ht();
2586 if (ht != NULL)
2587 return ht;
2588 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002589 return NULL;
2590}
2591
2592/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002593 * Get the string value of a (global/local) variable.
2594 * Note: see tv_get_string() for how long the pointer remains valid.
2595 * Returns NULL when it doesn't exist.
2596 */
2597 char_u *
2598get_var_value(char_u *name)
2599{
2600 dictitem_T *v;
2601
2602 v = find_var(name, NULL, FALSE);
2603 if (v == NULL)
2604 return NULL;
2605 return tv_get_string(&v->di_tv);
2606}
2607
2608/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002609 * Allocate a new hashtab for a sourced script. It will be used while
2610 * sourcing this script and when executing functions defined in the script.
2611 */
2612 void
2613new_script_vars(scid_T id)
2614{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002615 scriptvar_T *sv;
2616
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002617 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2618 if (sv == NULL)
2619 return;
2620 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002621 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002622}
2623
2624/*
2625 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2626 * point to it.
2627 */
2628 void
2629init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2630{
2631 hash_init(&dict->dv_hashtab);
2632 dict->dv_lock = 0;
2633 dict->dv_scope = scope;
2634 dict->dv_refcount = DO_NOT_FREE_CNT;
2635 dict->dv_copyID = 0;
2636 dict_var->di_tv.vval.v_dict = dict;
2637 dict_var->di_tv.v_type = VAR_DICT;
2638 dict_var->di_tv.v_lock = VAR_FIXED;
2639 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2640 dict_var->di_key[0] = NUL;
2641}
2642
2643/*
2644 * Unreference a dictionary initialized by init_var_dict().
2645 */
2646 void
2647unref_var_dict(dict_T *dict)
2648{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002649 // Now the dict needs to be freed if no one else is using it, go back to
2650 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002651 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2652 dict_unref(dict);
2653}
2654
2655/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002656 * Clean up a list of internal variables.
2657 * Frees all allocated variables and the value they contain.
2658 * Clears hashtab "ht", does not free it.
2659 */
2660 void
2661vars_clear(hashtab_T *ht)
2662{
2663 vars_clear_ext(ht, TRUE);
2664}
2665
2666/*
2667 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2668 */
2669 void
2670vars_clear_ext(hashtab_T *ht, int free_val)
2671{
2672 int todo;
2673 hashitem_T *hi;
2674 dictitem_T *v;
2675
2676 hash_lock(ht);
2677 todo = (int)ht->ht_used;
2678 for (hi = ht->ht_array; todo > 0; ++hi)
2679 {
2680 if (!HASHITEM_EMPTY(hi))
2681 {
2682 --todo;
2683
2684 // Free the variable. Don't remove it from the hashtab,
2685 // ht_array might change then. hash_clear() takes care of it
2686 // later.
2687 v = HI2DI(hi);
2688 if (free_val)
2689 clear_tv(&v->di_tv);
2690 if (v->di_flags & DI_FLAGS_ALLOC)
2691 vim_free(v);
2692 }
2693 }
2694 hash_clear(ht);
2695 ht->ht_used = 0;
2696}
2697
2698/*
2699 * Delete a variable from hashtab "ht" at item "hi".
2700 * Clear the variable value and free the dictitem.
2701 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002702 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002703delete_var(hashtab_T *ht, hashitem_T *hi)
2704{
2705 dictitem_T *di = HI2DI(hi);
2706
2707 hash_remove(ht, hi);
2708 clear_tv(&di->di_tv);
2709 vim_free(di);
2710}
2711
2712/*
2713 * List the value of one internal variable.
2714 */
2715 static void
2716list_one_var(dictitem_T *v, char *prefix, int *first)
2717{
2718 char_u *tofree;
2719 char_u *s;
2720 char_u numbuf[NUMBUFLEN];
2721
2722 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2723 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2724 s == NULL ? (char_u *)"" : s, first);
2725 vim_free(tofree);
2726}
2727
2728 static void
2729list_one_var_a(
2730 char *prefix,
2731 char_u *name,
2732 int type,
2733 char_u *string,
2734 int *first) // when TRUE clear rest of screen and set to FALSE
2735{
2736 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2737 msg_start();
2738 msg_puts(prefix);
2739 if (name != NULL) // "a:" vars don't have a name stored
2740 msg_puts((char *)name);
2741 msg_putchar(' ');
2742 msg_advance(22);
2743 if (type == VAR_NUMBER)
2744 msg_putchar('#');
2745 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2746 msg_putchar('*');
2747 else if (type == VAR_LIST)
2748 {
2749 msg_putchar('[');
2750 if (*string == '[')
2751 ++string;
2752 }
2753 else if (type == VAR_DICT)
2754 {
2755 msg_putchar('{');
2756 if (*string == '{')
2757 ++string;
2758 }
2759 else
2760 msg_putchar(' ');
2761
2762 msg_outtrans(string);
2763
2764 if (type == VAR_FUNC || type == VAR_PARTIAL)
2765 msg_puts("()");
2766 if (*first)
2767 {
2768 msg_clr_eos();
2769 *first = FALSE;
2770 }
2771}
2772
2773/*
2774 * Set variable "name" to value in "tv".
2775 * If the variable already exists, the value is updated.
2776 * Otherwise the variable is created.
2777 */
2778 void
2779set_var(
2780 char_u *name,
2781 typval_T *tv,
2782 int copy) // make copy of value in "tv"
2783{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784 set_var_const(name, NULL, tv, copy, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002785}
2786
2787/*
2788 * Set variable "name" to value in "tv".
2789 * If the variable already exists and "is_const" is FALSE the value is updated.
2790 * Otherwise the variable is created.
2791 */
2792 void
2793set_var_const(
2794 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002795 type_T *type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002796 typval_T *tv,
2797 int copy, // make copy of value in "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 int flags) // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002799{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002801 char_u *varname;
2802 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 int is_script_local;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002804
2805 ht = find_var_ht(name, &varname);
2806 if (ht == NULL || *varname == NUL)
2807 {
2808 semsg(_(e_illvar), name);
2809 return;
2810 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 is_script_local = ht == get_script_local_ht();
2812
2813 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002814
2815 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 if (di == NULL)
2817 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002818
2819 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 && var_check_func_name(name, di == NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002821 return;
2822
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002823 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002824 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002826 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827 if (flags & LET_IS_CONST)
2828 {
2829 emsg(_(e_cannot_mod));
2830 return;
2831 }
2832
2833 if (var_check_ro(di->di_flags, name, FALSE)
2834 || var_check_lock(di->di_tv.v_lock, name, FALSE))
2835 return;
2836
2837 if ((flags & LET_NO_COMMAND) == 0
2838 && is_script_local
2839 && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2840 {
2841 semsg(_("E1041: Redefining script item %s"), name);
2842 return;
2843 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002844 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 else
2846 // can only redefine once
2847 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002848
2849 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002852 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002853 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002854 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002856 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002858 if (copy || tv->v_type != VAR_STRING)
2859 {
2860 char_u *val = tv_get_string(tv);
2861
2862 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002863 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864 if (di->di_tv.vval.v_string == NULL)
2865 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002866 }
2867 else
2868 {
2869 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002871 tv->vval.v_string = NULL;
2872 }
2873 return;
2874 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002876 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002878 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002879 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002880#ifdef FEAT_SEARCH_EXTRA
2881 else if (STRCMP(varname, "hlsearch") == 0)
2882 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002884 redraw_all_later(SOME_VALID);
2885 }
2886#endif
2887 return;
2888 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002890 {
2891 semsg(_("E963: setting %s to value with wrong type"), name);
2892 return;
2893 }
2894 }
2895
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002897 }
2898 else // add a new variable
2899 {
2900 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002901 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002902 {
2903 semsg(_(e_illvar), name);
2904 return;
2905 }
2906
2907 // Make sure the variable name is valid.
2908 if (!valid_varname(varname))
2909 return;
2910
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
2912 if (di == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002913 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 STRCPY(di->di_key, varname);
2915 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002916 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002918 return;
2919 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 di->di_flags = DI_FLAGS_ALLOC;
2921 if (flags & LET_IS_CONST)
2922 di->di_flags |= DI_FLAGS_LOCK;
2923
2924 if (is_script_local && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2925 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002926 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927
2928 // Store a pointer to the typval_T, so that it can be found by
2929 // index instead of using a hastab lookup.
2930 if (ga_grow(&si->sn_var_vals, 1) == OK)
2931 {
2932 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2933 + si->sn_var_vals.ga_len;
2934 sv->sv_name = di->di_key;
2935 sv->sv_tv = &di->di_tv;
2936 sv->sv_type = type == NULL ? &t_any : type;
2937 sv->sv_const = (flags & LET_IS_CONST);
2938 sv->sv_export = is_export;
2939 ++si->sn_var_vals.ga_len;
2940
2941 // let ex_export() know the export worked.
2942 is_export = FALSE;
2943 }
2944 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002945 }
2946
2947 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002949 else
2950 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 di->di_tv = *tv;
2952 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002953 init_tv(tv);
2954 }
2955
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 if (flags & LET_IS_CONST)
2957 di->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002958}
2959
2960/*
2961 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
2962 * Also give an error message.
2963 */
2964 int
2965var_check_ro(int flags, char_u *name, int use_gettext)
2966{
2967 if (flags & DI_FLAGS_RO)
2968 {
2969 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
2970 return TRUE;
2971 }
2972 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
2973 {
2974 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
2975 return TRUE;
2976 }
2977 return FALSE;
2978}
2979
2980/*
2981 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
2982 * Also give an error message.
2983 */
2984 int
2985var_check_fixed(int flags, char_u *name, int use_gettext)
2986{
2987 if (flags & DI_FLAGS_FIX)
2988 {
2989 semsg(_("E795: Cannot delete variable %s"),
2990 use_gettext ? (char_u *)_(name) : name);
2991 return TRUE;
2992 }
2993 return FALSE;
2994}
2995
2996/*
2997 * Check if a funcref is assigned to a valid variable name.
2998 * Return TRUE and give an error if not.
2999 */
3000 int
3001var_check_func_name(
3002 char_u *name, // points to start of variable name
3003 int new_var) // TRUE when creating the variable
3004{
3005 // Allow for w: b: s: and t:.
3006 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3007 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3008 ? name[2] : name[0]))
3009 {
3010 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3011 name);
3012 return TRUE;
3013 }
3014 // Don't allow hiding a function. When "v" is not NULL we might be
3015 // assigning another function to the same var, the type is checked
3016 // below.
3017 if (new_var && function_exists(name, FALSE))
3018 {
3019 semsg(_("E705: Variable name conflicts with existing function: %s"),
3020 name);
3021 return TRUE;
3022 }
3023 return FALSE;
3024}
3025
3026/*
3027 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
3028 * Also give an error message, using "name" or _("name") when use_gettext is
3029 * TRUE.
3030 */
3031 int
3032var_check_lock(int lock, char_u *name, int use_gettext)
3033{
3034 if (lock & VAR_LOCKED)
3035 {
3036 semsg(_("E741: Value is locked: %s"),
3037 name == NULL ? (char_u *)_("Unknown")
3038 : use_gettext ? (char_u *)_(name)
3039 : name);
3040 return TRUE;
3041 }
3042 if (lock & VAR_FIXED)
3043 {
3044 semsg(_("E742: Cannot change value of %s"),
3045 name == NULL ? (char_u *)_("Unknown")
3046 : use_gettext ? (char_u *)_(name)
3047 : name);
3048 return TRUE;
3049 }
3050 return FALSE;
3051}
3052
3053/*
3054 * Check if a variable name is valid.
3055 * Return FALSE and give an error if not.
3056 */
3057 int
3058valid_varname(char_u *varname)
3059{
3060 char_u *p;
3061
3062 for (p = varname; *p != NUL; ++p)
3063 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3064 && *p != AUTOLOAD_CHAR)
3065 {
3066 semsg(_(e_illvar), varname);
3067 return FALSE;
3068 }
3069 return TRUE;
3070}
3071
3072/*
3073 * getwinvar() and gettabwinvar()
3074 */
3075 static void
3076getwinvar(
3077 typval_T *argvars,
3078 typval_T *rettv,
3079 int off) // 1 for gettabwinvar()
3080{
3081 win_T *win;
3082 char_u *varname;
3083 dictitem_T *v;
3084 tabpage_T *tp = NULL;
3085 int done = FALSE;
3086 win_T *oldcurwin;
3087 tabpage_T *oldtabpage;
3088 int need_switch_win;
3089
3090 if (off == 1)
3091 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3092 else
3093 tp = curtab;
3094 win = find_win_by_nr(&argvars[off], tp);
3095 varname = tv_get_string_chk(&argvars[off + 1]);
3096 ++emsg_off;
3097
3098 rettv->v_type = VAR_STRING;
3099 rettv->vval.v_string = NULL;
3100
3101 if (win != NULL && varname != NULL)
3102 {
3103 // Set curwin to be our win, temporarily. Also set the tabpage,
3104 // otherwise the window is not valid. Only do this when needed,
3105 // autocommands get blocked.
3106 need_switch_win = !(tp == curtab && win == curwin);
3107 if (!need_switch_win
3108 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3109 {
3110 if (*varname == '&')
3111 {
3112 if (varname[1] == NUL)
3113 {
3114 // get all window-local options in a dict
3115 dict_T *opts = get_winbuf_options(FALSE);
3116
3117 if (opts != NULL)
3118 {
3119 rettv_dict_set(rettv, opts);
3120 done = TRUE;
3121 }
3122 }
3123 else if (get_option_tv(&varname, rettv, 1) == OK)
3124 // window-local-option
3125 done = TRUE;
3126 }
3127 else
3128 {
3129 // Look up the variable.
3130 // Let getwinvar({nr}, "") return the "w:" dictionary.
3131 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3132 varname, FALSE);
3133 if (v != NULL)
3134 {
3135 copy_tv(&v->di_tv, rettv);
3136 done = TRUE;
3137 }
3138 }
3139 }
3140
3141 if (need_switch_win)
3142 // restore previous notion of curwin
3143 restore_win(oldcurwin, oldtabpage, TRUE);
3144 }
3145
3146 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3147 // use the default return value
3148 copy_tv(&argvars[off + 2], rettv);
3149
3150 --emsg_off;
3151}
3152
3153/*
3154 * "setwinvar()" and "settabwinvar()" functions
3155 */
3156 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003157setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003158{
3159 win_T *win;
3160 win_T *save_curwin;
3161 tabpage_T *save_curtab;
3162 int need_switch_win;
3163 char_u *varname, *winvarname;
3164 typval_T *varp;
3165 char_u nbuf[NUMBUFLEN];
3166 tabpage_T *tp = NULL;
3167
3168 if (check_secure())
3169 return;
3170
3171 if (off == 1)
3172 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3173 else
3174 tp = curtab;
3175 win = find_win_by_nr(&argvars[off], tp);
3176 varname = tv_get_string_chk(&argvars[off + 1]);
3177 varp = &argvars[off + 2];
3178
3179 if (win != NULL && varname != NULL && varp != NULL)
3180 {
3181 need_switch_win = !(tp == curtab && win == curwin);
3182 if (!need_switch_win
3183 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3184 {
3185 if (*varname == '&')
3186 {
3187 long numval;
3188 char_u *strval;
3189 int error = FALSE;
3190
3191 ++varname;
3192 numval = (long)tv_get_number_chk(varp, &error);
3193 strval = tv_get_string_buf_chk(varp, nbuf);
3194 if (!error && strval != NULL)
3195 set_option_value(varname, numval, strval, OPT_LOCAL);
3196 }
3197 else
3198 {
3199 winvarname = alloc(STRLEN(varname) + 3);
3200 if (winvarname != NULL)
3201 {
3202 STRCPY(winvarname, "w:");
3203 STRCPY(winvarname + 2, varname);
3204 set_var(winvarname, varp, TRUE);
3205 vim_free(winvarname);
3206 }
3207 }
3208 }
3209 if (need_switch_win)
3210 restore_win(save_curwin, save_curtab, TRUE);
3211 }
3212}
3213
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003214/*
3215 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3216 * v:option_type, and v:option_command.
3217 */
3218 void
3219reset_v_option_vars(void)
3220{
3221 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3222 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3223 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3224 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3225 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3226 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3227}
3228
3229/*
3230 * Add an assert error to v:errors.
3231 */
3232 void
3233assert_error(garray_T *gap)
3234{
3235 struct vimvar *vp = &vimvars[VV_ERRORS];
3236
3237 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003238 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003239 set_vim_var_list(VV_ERRORS, list_alloc());
3240 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3241}
3242
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003243 int
3244var_exists(char_u *var)
3245{
3246 char_u *name;
3247 char_u *tofree;
3248 typval_T tv;
3249 int len = 0;
3250 int n = FALSE;
3251
3252 // get_name_len() takes care of expanding curly braces
3253 name = var;
3254 len = get_name_len(&var, &tofree, TRUE, FALSE);
3255 if (len > 0)
3256 {
3257 if (tofree != NULL)
3258 name = tofree;
3259 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
3260 if (n)
3261 {
3262 // handle d.key, l[idx], f(expr)
3263 n = (handle_subscript(&var, &tv, TRUE, FALSE, name, &name) == OK);
3264 if (n)
3265 clear_tv(&tv);
3266 }
3267 }
3268 if (*var != NUL)
3269 n = FALSE;
3270
3271 vim_free(tofree);
3272 return n;
3273}
3274
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003275static lval_T *redir_lval = NULL;
3276#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3277static garray_T redir_ga; // only valid when redir_lval is not NULL
3278static char_u *redir_endp = NULL;
3279static char_u *redir_varname = NULL;
3280
3281/*
3282 * Start recording command output to a variable
3283 * When "append" is TRUE append to an existing variable.
3284 * Returns OK if successfully completed the setup. FAIL otherwise.
3285 */
3286 int
3287var_redir_start(char_u *name, int append)
3288{
3289 int save_emsg;
3290 int err;
3291 typval_T tv;
3292
3293 // Catch a bad name early.
3294 if (!eval_isnamec1(*name))
3295 {
3296 emsg(_(e_invarg));
3297 return FAIL;
3298 }
3299
3300 // Make a copy of the name, it is used in redir_lval until redir ends.
3301 redir_varname = vim_strsave(name);
3302 if (redir_varname == NULL)
3303 return FAIL;
3304
3305 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3306 if (redir_lval == NULL)
3307 {
3308 var_redir_stop();
3309 return FAIL;
3310 }
3311
3312 // The output is stored in growarray "redir_ga" until redirection ends.
3313 ga_init2(&redir_ga, (int)sizeof(char), 500);
3314
3315 // Parse the variable name (can be a dict or list entry).
3316 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3317 FNE_CHECK_START);
3318 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3319 {
3320 clear_lval(redir_lval);
3321 if (redir_endp != NULL && *redir_endp != NUL)
3322 // Trailing characters are present after the variable name
3323 emsg(_(e_trailing));
3324 else
3325 emsg(_(e_invarg));
3326 redir_endp = NULL; // don't store a value, only cleanup
3327 var_redir_stop();
3328 return FAIL;
3329 }
3330
3331 // check if we can write to the variable: set it to or append an empty
3332 // string
3333 save_emsg = did_emsg;
3334 did_emsg = FALSE;
3335 tv.v_type = VAR_STRING;
3336 tv.vval.v_string = (char_u *)"";
3337 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003339 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003341 clear_lval(redir_lval);
3342 err = did_emsg;
3343 did_emsg |= save_emsg;
3344 if (err)
3345 {
3346 redir_endp = NULL; // don't store a value, only cleanup
3347 var_redir_stop();
3348 return FAIL;
3349 }
3350
3351 return OK;
3352}
3353
3354/*
3355 * Append "value[value_len]" to the variable set by var_redir_start().
3356 * The actual appending is postponed until redirection ends, because the value
3357 * appended may in fact be the string we write to, changing it may cause freed
3358 * memory to be used:
3359 * :redir => foo
3360 * :let foo
3361 * :redir END
3362 */
3363 void
3364var_redir_str(char_u *value, int value_len)
3365{
3366 int len;
3367
3368 if (redir_lval == NULL)
3369 return;
3370
3371 if (value_len == -1)
3372 len = (int)STRLEN(value); // Append the entire string
3373 else
3374 len = value_len; // Append only "value_len" characters
3375
3376 if (ga_grow(&redir_ga, len) == OK)
3377 {
3378 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3379 redir_ga.ga_len += len;
3380 }
3381 else
3382 var_redir_stop();
3383}
3384
3385/*
3386 * Stop redirecting command output to a variable.
3387 * Frees the allocated memory.
3388 */
3389 void
3390var_redir_stop(void)
3391{
3392 typval_T tv;
3393
3394 if (EVALCMD_BUSY)
3395 {
3396 redir_lval = NULL;
3397 return;
3398 }
3399
3400 if (redir_lval != NULL)
3401 {
3402 // If there was no error: assign the text to the variable.
3403 if (redir_endp != NULL)
3404 {
3405 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3406 tv.v_type = VAR_STRING;
3407 tv.vval.v_string = redir_ga.ga_data;
3408 // Call get_lval() again, if it's inside a Dict or List it may
3409 // have changed.
3410 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3411 FALSE, FALSE, 0, FNE_CHECK_START);
3412 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003414 (char_u *)".");
3415 clear_lval(redir_lval);
3416 }
3417
3418 // free the collected output
3419 VIM_CLEAR(redir_ga.ga_data);
3420
3421 VIM_CLEAR(redir_lval);
3422 }
3423 VIM_CLEAR(redir_varname);
3424}
3425
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003426/*
3427 * "gettabvar()" function
3428 */
3429 void
3430f_gettabvar(typval_T *argvars, typval_T *rettv)
3431{
3432 win_T *oldcurwin;
3433 tabpage_T *tp, *oldtabpage;
3434 dictitem_T *v;
3435 char_u *varname;
3436 int done = FALSE;
3437
3438 rettv->v_type = VAR_STRING;
3439 rettv->vval.v_string = NULL;
3440
3441 varname = tv_get_string_chk(&argvars[1]);
3442 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3443 if (tp != NULL && varname != NULL)
3444 {
3445 // Set tp to be our tabpage, temporarily. Also set the window to the
3446 // first window in the tabpage, otherwise the window is not valid.
3447 if (switch_win(&oldcurwin, &oldtabpage,
3448 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3449 : tp->tp_firstwin, tp, TRUE) == OK)
3450 {
3451 // look up the variable
3452 // Let gettabvar({nr}, "") return the "t:" dictionary.
3453 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3454 if (v != NULL)
3455 {
3456 copy_tv(&v->di_tv, rettv);
3457 done = TRUE;
3458 }
3459 }
3460
3461 // restore previous notion of curwin
3462 restore_win(oldcurwin, oldtabpage, TRUE);
3463 }
3464
3465 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3466 copy_tv(&argvars[2], rettv);
3467}
3468
3469/*
3470 * "gettabwinvar()" function
3471 */
3472 void
3473f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3474{
3475 getwinvar(argvars, rettv, 1);
3476}
3477
3478/*
3479 * "getwinvar()" function
3480 */
3481 void
3482f_getwinvar(typval_T *argvars, typval_T *rettv)
3483{
3484 getwinvar(argvars, rettv, 0);
3485}
3486
3487/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003488 * "getbufvar()" function
3489 */
3490 void
3491f_getbufvar(typval_T *argvars, typval_T *rettv)
3492{
3493 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003494 char_u *varname;
3495 dictitem_T *v;
3496 int done = FALSE;
3497
3498 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3499 varname = tv_get_string_chk(&argvars[1]);
3500 ++emsg_off;
3501 buf = tv_get_buf(&argvars[0], FALSE);
3502
3503 rettv->v_type = VAR_STRING;
3504 rettv->vval.v_string = NULL;
3505
3506 if (buf != NULL && varname != NULL)
3507 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003508 if (*varname == '&')
3509 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003510 buf_T *save_curbuf = curbuf;
3511
3512 // set curbuf to be our buf, temporarily
3513 curbuf = buf;
3514
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003515 if (varname[1] == NUL)
3516 {
3517 // get all buffer-local options in a dict
3518 dict_T *opts = get_winbuf_options(TRUE);
3519
3520 if (opts != NULL)
3521 {
3522 rettv_dict_set(rettv, opts);
3523 done = TRUE;
3524 }
3525 }
3526 else if (get_option_tv(&varname, rettv, TRUE) == OK)
3527 // buffer-local-option
3528 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003529
3530 // restore previous notion of curbuf
3531 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003532 }
3533 else
3534 {
3535 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003536 if (*varname == NUL)
3537 // Let getbufvar({nr}, "") return the "b:" dictionary.
3538 v = &buf->b_bufvar;
3539 else
3540 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3541 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003542 if (v != NULL)
3543 {
3544 copy_tv(&v->di_tv, rettv);
3545 done = TRUE;
3546 }
3547 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003548 }
3549
3550 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3551 // use the default value
3552 copy_tv(&argvars[2], rettv);
3553
3554 --emsg_off;
3555}
3556
3557/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003558 * "settabvar()" function
3559 */
3560 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003561f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003562{
3563 tabpage_T *save_curtab;
3564 tabpage_T *tp;
3565 char_u *varname, *tabvarname;
3566 typval_T *varp;
3567
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003568 if (check_secure())
3569 return;
3570
3571 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3572 varname = tv_get_string_chk(&argvars[1]);
3573 varp = &argvars[2];
3574
3575 if (varname != NULL && varp != NULL && tp != NULL)
3576 {
3577 save_curtab = curtab;
3578 goto_tabpage_tp(tp, FALSE, FALSE);
3579
3580 tabvarname = alloc(STRLEN(varname) + 3);
3581 if (tabvarname != NULL)
3582 {
3583 STRCPY(tabvarname, "t:");
3584 STRCPY(tabvarname + 2, varname);
3585 set_var(tabvarname, varp, TRUE);
3586 vim_free(tabvarname);
3587 }
3588
3589 // Restore current tabpage
3590 if (valid_tabpage(save_curtab))
3591 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3592 }
3593}
3594
3595/*
3596 * "settabwinvar()" function
3597 */
3598 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003599f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003600{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003601 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003602}
3603
3604/*
3605 * "setwinvar()" function
3606 */
3607 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003608f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003609{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003610 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003611}
3612
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003613/*
3614 * "setbufvar()" function
3615 */
3616 void
3617f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3618{
3619 buf_T *buf;
3620 char_u *varname, *bufvarname;
3621 typval_T *varp;
3622 char_u nbuf[NUMBUFLEN];
3623
3624 if (check_secure())
3625 return;
3626 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3627 varname = tv_get_string_chk(&argvars[1]);
3628 buf = tv_get_buf(&argvars[0], FALSE);
3629 varp = &argvars[2];
3630
3631 if (buf != NULL && varname != NULL && varp != NULL)
3632 {
3633 if (*varname == '&')
3634 {
3635 long numval;
3636 char_u *strval;
3637 int error = FALSE;
3638 aco_save_T aco;
3639
3640 // set curbuf to be our buf, temporarily
3641 aucmd_prepbuf(&aco, buf);
3642
3643 ++varname;
3644 numval = (long)tv_get_number_chk(varp, &error);
3645 strval = tv_get_string_buf_chk(varp, nbuf);
3646 if (!error && strval != NULL)
3647 set_option_value(varname, numval, strval, OPT_LOCAL);
3648
3649 // reset notion of buffer
3650 aucmd_restbuf(&aco);
3651 }
3652 else
3653 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003654 bufvarname = alloc(STRLEN(varname) + 3);
3655 if (bufvarname != NULL)
3656 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003657 buf_T *save_curbuf = curbuf;
3658
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003659 curbuf = buf;
3660 STRCPY(bufvarname, "b:");
3661 STRCPY(bufvarname + 2, varname);
3662 set_var(bufvarname, varp, TRUE);
3663 vim_free(bufvarname);
3664 curbuf = save_curbuf;
3665 }
3666 }
3667 }
3668}
3669
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003670/*
3671 * Get a callback from "arg". It can be a Funcref or a function name.
3672 * When "arg" is zero return an empty string.
3673 * "cb_name" is not allocated.
3674 * "cb_name" is set to NULL for an invalid argument.
3675 */
3676 callback_T
3677get_callback(typval_T *arg)
3678{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003679 callback_T res;
3680 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003681
3682 res.cb_free_name = FALSE;
3683 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3684 {
3685 res.cb_partial = arg->vval.v_partial;
3686 ++res.cb_partial->pt_refcount;
3687 res.cb_name = partial_name(res.cb_partial);
3688 }
3689 else
3690 {
3691 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003692 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3693 && isdigit(*arg->vval.v_string))
3694 r = FAIL;
3695 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003696 {
3697 // Note that we don't make a copy of the string.
3698 res.cb_name = arg->vval.v_string;
3699 func_ref(res.cb_name);
3700 }
3701 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003702 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003703 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003704 r = FAIL;
3705
3706 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003707 {
3708 emsg(_("E921: Invalid callback argument"));
3709 res.cb_name = NULL;
3710 }
3711 }
3712 return res;
3713}
3714
3715/*
3716 * Copy a callback into a typval_T.
3717 */
3718 void
3719put_callback(callback_T *cb, typval_T *tv)
3720{
3721 if (cb->cb_partial != NULL)
3722 {
3723 tv->v_type = VAR_PARTIAL;
3724 tv->vval.v_partial = cb->cb_partial;
3725 ++tv->vval.v_partial->pt_refcount;
3726 }
3727 else
3728 {
3729 tv->v_type = VAR_FUNC;
3730 tv->vval.v_string = vim_strsave(cb->cb_name);
3731 func_ref(cb->cb_name);
3732 }
3733}
3734
3735/*
3736 * Make a copy of "src" into "dest", allocating the function name if needed,
3737 * without incrementing the refcount.
3738 */
3739 void
3740set_callback(callback_T *dest, callback_T *src)
3741{
3742 if (src->cb_partial == NULL)
3743 {
3744 // just a function name, make a copy
3745 dest->cb_name = vim_strsave(src->cb_name);
3746 dest->cb_free_name = TRUE;
3747 }
3748 else
3749 {
3750 // cb_name is a pointer into cb_partial
3751 dest->cb_name = src->cb_name;
3752 dest->cb_free_name = FALSE;
3753 }
3754 dest->cb_partial = src->cb_partial;
3755}
3756
3757/*
3758 * Unref/free "callback" returned by get_callback() or set_callback().
3759 */
3760 void
3761free_callback(callback_T *callback)
3762{
3763 if (callback->cb_partial != NULL)
3764 {
3765 partial_unref(callback->cb_partial);
3766 callback->cb_partial = NULL;
3767 }
3768 else if (callback->cb_name != NULL)
3769 func_unref(callback->cb_name);
3770 if (callback->cb_free_name)
3771 {
3772 vim_free(callback->cb_name);
3773 callback->cb_free_name = FALSE;
3774 }
3775 callback->cb_name = NULL;
3776}
3777
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003778#endif // FEAT_EVAL