blob: 1c40f4111f78f7f8e331121038fcb24c0b544114 [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
48 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
49} vimvars[VV_LEN] =
50{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020051 // The order here must match the VV_ defines in vim.h!
52 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaare5cdf152019-08-29 22:09:46 +020053 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
54 {VV_NAME("count1", VAR_NUMBER), VV_RO},
55 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
56 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
57 {VV_NAME("warningmsg", VAR_STRING), 0},
58 {VV_NAME("statusmsg", VAR_STRING), 0},
59 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
60 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
61 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
62 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
63 {VV_NAME("termresponse", VAR_STRING), VV_RO},
64 {VV_NAME("fname", VAR_STRING), VV_RO},
65 {VV_NAME("lang", VAR_STRING), VV_RO},
66 {VV_NAME("lc_time", VAR_STRING), VV_RO},
67 {VV_NAME("ctype", VAR_STRING), VV_RO},
68 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
69 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
70 {VV_NAME("fname_in", VAR_STRING), VV_RO},
71 {VV_NAME("fname_out", VAR_STRING), VV_RO},
72 {VV_NAME("fname_new", VAR_STRING), VV_RO},
73 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
74 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
75 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
76 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
77 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
78 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
79 {VV_NAME("progname", VAR_STRING), VV_RO},
80 {VV_NAME("servername", VAR_STRING), VV_RO},
81 {VV_NAME("dying", VAR_NUMBER), VV_RO},
82 {VV_NAME("exception", VAR_STRING), VV_RO},
83 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
84 {VV_NAME("register", VAR_STRING), VV_RO},
85 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
86 {VV_NAME("insertmode", VAR_STRING), VV_RO},
87 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
88 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
89 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
90 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
91 {VV_NAME("fcs_choice", VAR_STRING), 0},
92 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
93 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
94 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
95 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
96 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
97 {VV_NAME("beval_text", VAR_STRING), VV_RO},
98 {VV_NAME("scrollstart", VAR_STRING), 0},
99 {VV_NAME("swapname", VAR_STRING), VV_RO},
100 {VV_NAME("swapchoice", VAR_STRING), 0},
101 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
102 {VV_NAME("char", VAR_STRING), 0},
103 {VV_NAME("mouse_win", VAR_NUMBER), 0},
104 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
105 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
106 {VV_NAME("mouse_col", VAR_NUMBER), 0},
107 {VV_NAME("operator", VAR_STRING), VV_RO},
108 {VV_NAME("searchforward", VAR_NUMBER), 0},
109 {VV_NAME("hlsearch", VAR_NUMBER), 0},
110 {VV_NAME("oldfiles", VAR_LIST), 0},
111 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
112 {VV_NAME("progpath", VAR_STRING), VV_RO},
113 {VV_NAME("completed_item", VAR_DICT), VV_RO},
114 {VV_NAME("option_new", VAR_STRING), VV_RO},
115 {VV_NAME("option_old", VAR_STRING), VV_RO},
116 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
117 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
118 {VV_NAME("option_command", VAR_STRING), VV_RO},
119 {VV_NAME("option_type", VAR_STRING), VV_RO},
120 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100121 {VV_NAME("false", VAR_BOOL), VV_RO},
122 {VV_NAME("true", VAR_BOOL), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200123 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100124 {VV_NAME("null", VAR_SPECIAL), VV_RO},
125 {VV_NAME("numbersize", VAR_NUMBER), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200126 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
127 {VV_NAME("testing", VAR_NUMBER), 0},
128 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
129 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
130 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
131 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
132 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
133 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
134 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
135 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
136 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
137 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
138 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
139 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
140 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
141 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
142 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
143 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
144 {VV_NAME("event", VAR_DICT), VV_RO},
145 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
146 {VV_NAME("echospace", VAR_NUMBER), VV_RO},
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100147 {VV_NAME("argv", VAR_LIST), VV_RO},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200148 {VV_NAME("collate", VAR_STRING), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200149};
150
151// shorthand
152#define vv_type vv_di.di_tv.v_type
153#define vv_nr vv_di.di_tv.vval.v_number
154#define vv_float vv_di.di_tv.vval.v_float
155#define vv_str vv_di.di_tv.vval.v_string
156#define vv_list vv_di.di_tv.vval.v_list
157#define vv_dict vv_di.di_tv.vval.v_dict
158#define vv_blob vv_di.di_tv.vval.v_blob
159#define vv_tv vv_di.di_tv
160
161static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200162static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200163#define vimvarht vimvardict.dv_hashtab
164
165// for VIM_VERSION_ defines
166#include "version.h"
167
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200168static void list_glob_vars(int *first);
169static void list_buf_vars(int *first);
170static void list_win_vars(int *first);
171static void list_tab_vars(int *first);
172static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200174static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
175static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200176static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200177static void list_one_var(dictitem_T *v, char *prefix, int *first);
178static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
179
180/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200181 * Initialize global and vim special variables
182 */
183 void
184evalvars_init(void)
185{
186 int i;
187 struct vimvar *p;
188
189 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
190 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
191 vimvardict.dv_lock = VAR_FIXED;
192 hash_init(&compat_hashtab);
193
194 for (i = 0; i < VV_LEN; ++i)
195 {
196 p = &vimvars[i];
197 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
198 {
199 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
200 getout(1);
201 }
202 STRCPY(p->vv_di.di_key, p->vv_name);
203 if (p->vv_flags & VV_RO)
204 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
205 else if (p->vv_flags & VV_RO_SBX)
206 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
207 else
208 p->vv_di.di_flags = DI_FLAGS_FIX;
209
210 // add to v: scope dict, unless the value is not always available
211 if (p->vv_type != VAR_UNKNOWN)
212 hash_add(&vimvarht, p->vv_di.di_key);
213 if (p->vv_flags & VV_COMPAT)
214 // add to compat scope dict
215 hash_add(&compat_hashtab, p->vv_di.di_key);
216 }
217 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
218 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
219
220 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
221 set_vim_var_nr(VV_HLSEARCH, 1L);
222 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
223 set_vim_var_list(VV_ERRORS, list_alloc());
224 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
225
226 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
227 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
228 set_vim_var_nr(VV_NONE, VVAL_NONE);
229 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100230 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200231
232 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
233 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
234 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
235 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
236 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
237 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
238 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
239 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
240 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
241 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
242 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
243
244 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
245
Bram Moolenaar439c0362020-06-06 15:58:03 +0200246 // Default for v:register is not 0 but '"'. This is adjusted once the
247 // clipboard has been setup by calling reset_reg_var().
248 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200249}
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
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200437 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200438 {
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 Moolenaare3d46852020-08-29 13:39:17 +0200526 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
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 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200594 semsg(_(e_trailing_arg), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200595 return NULL;
596 }
597 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200598 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200599 {
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 Moolenaar2eec3792020-05-25 20:33:55 +0200685 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200686 * ":const" list all variable values
687 * ":const var1 var2" list variable values
688 * ":const var = expr" assignment command.
689 * ":const [var1, var2] = expr" unpack list.
690 */
691 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200692ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200693{
694 char_u *arg = eap->arg;
695 char_u *expr = NULL;
696 typval_T rettv;
697 int i;
698 int var_count = 0;
699 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200700 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200701 char_u *argend;
702 int first = TRUE;
703 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200704 int has_assign;
Bram Moolenaar09689a02020-05-09 22:50:08 +0200705 int flags = eap->cmdidx == CMD_const ? LET_IS_CONST : 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200706 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200707
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100708 // detect Vim9 assignment without ":let" or ":const"
709 if (eap->arg == eap->cmd)
710 flags |= LET_NO_COMMAND;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200711 if (eap->forceit)
712 flags |= LET_FORCEIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100713
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200714 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200715 if (argend == NULL)
716 return;
717 if (argend > arg && argend[-1] == '.') // for var.='str'
718 --argend;
719 expr = skipwhite(argend);
720 concat = expr[0] == '.'
721 && ((expr[1] == '=' && current_sctx.sc_version < 2)
722 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200723 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
724 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200725 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200726 {
727 // ":let" without "=": list variables
728 if (*arg == '[')
729 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200730 else if (expr[0] == '.' && expr[1] == '=')
731 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200732 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200733 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200734 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200735 {
736 // Vim9 declaration ":let var: type"
737 arg = vim9_declare_scriptvar(eap, arg);
738 }
739 else
740 {
741 // ":let var1 var2" - list values
742 arg = list_arg_vars(eap, arg, &first);
743 }
744 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200745 else if (!eap->skip)
746 {
747 // ":let"
748 list_glob_vars(&first);
749 list_buf_vars(&first);
750 list_win_vars(&first);
751 list_tab_vars(&first);
752 list_script_vars(&first);
753 list_func_vars(&first);
754 list_vim_vars(&first);
755 }
756 eap->nextcmd = check_nextcmd(arg);
757 }
758 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
759 {
760 list_T *l;
761
762 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200763 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200764 if (l != NULL)
765 {
766 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200767 if (!eap->skip)
768 {
769 op[0] = '=';
770 op[1] = NUL;
771 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200773 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200774 clear_tv(&rettv);
775 }
776 }
777 else
778 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200779 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200780 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200781
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200782 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200783 i = FAIL;
784 if (has_assign || concat)
785 {
786 op[0] = '=';
787 op[1] = NUL;
788 if (*expr != '=')
789 {
Bram Moolenaar122616d2020-08-21 21:32:50 +0200790 if (vim9script && (flags & LET_NO_COMMAND) == 0)
791 {
792 // +=, /=, etc. require an existing variable
793 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
794 i = FAIL;
795 }
796 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200797 {
798 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200799 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200800 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200801 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200802 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200803 ++len;
804 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200805 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200806 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200807 }
808 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200809 ++expr;
810
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200811 if (vim9script && (!VIM_ISWHITE(*argend)
812 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200813 {
814 vim_strncpy(op, expr - len, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200815 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200816 i = FAIL;
817 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200818
819 if (eap->skip)
820 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200821 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200822 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200823 if (getline_equal(eap->getline, eap->cookie, getsourceline))
824 {
825 evalarg.eval_getline = eap->getline;
826 evalarg.eval_cookie = eap->cookie;
827 }
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200828 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200829 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200830 if (eap->skip)
831 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200832 clear_evalarg(&evalarg, eap);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200833 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200834 if (eap->skip)
835 {
836 if (i != FAIL)
837 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200838 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200839 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200840 {
841 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200842 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200843 clear_tv(&rettv);
844 }
845 }
846}
847
848/*
849 * Assign the typevalue "tv" to the variable or variables at "arg_start".
850 * Handles both "var" with any type and "[var, var; var]" with a list type.
851 * When "op" is not NULL it points to a string with characters that
852 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
853 * or concatenate.
854 * Returns OK or FAIL;
855 */
856 int
857ex_let_vars(
858 char_u *arg_start,
859 typval_T *tv,
860 int copy, // copy values from "tv", don't move
861 int semicolon, // from skip_var_list()
862 int var_count, // from skip_var_list()
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200863 int flags, // LET_IS_CONST, LET_FORCEIT, LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200864 char_u *op)
865{
866 char_u *arg = arg_start;
867 list_T *l;
868 int i;
869 listitem_T *item;
870 typval_T ltv;
871
872 if (*arg != '[')
873 {
874 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100875 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200876 return FAIL;
877 return OK;
878 }
879
880 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
881 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
882 {
883 emsg(_(e_listreq));
884 return FAIL;
885 }
886
887 i = list_len(l);
888 if (semicolon == 0 && var_count < i)
889 {
890 emsg(_("E687: Less targets than List items"));
891 return FAIL;
892 }
893 if (var_count - semicolon > i)
894 {
895 emsg(_("E688: More targets than List items"));
896 return FAIL;
897 }
898
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200899 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200900 item = l->lv_first;
901 while (*arg != ']')
902 {
903 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200905 item = item->li_next;
906 if (arg == NULL)
907 return FAIL;
908
909 arg = skipwhite(arg);
910 if (*arg == ';')
911 {
912 // Put the rest of the list (may be empty) in the var after ';'.
913 // Create a new list for this.
914 l = list_alloc();
915 if (l == NULL)
916 return FAIL;
917 while (item != NULL)
918 {
919 list_append_tv(l, &item->li_tv);
920 item = item->li_next;
921 }
922
923 ltv.v_type = VAR_LIST;
924 ltv.v_lock = 0;
925 ltv.vval.v_list = l;
926 l->lv_refcount = 1;
927
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200929 (char_u *)"]", op);
930 clear_tv(&ltv);
931 if (arg == NULL)
932 return FAIL;
933 break;
934 }
935 else if (*arg != ',' && *arg != ']')
936 {
937 internal_error("ex_let_vars()");
938 return FAIL;
939 }
940 }
941
942 return OK;
943}
944
945/*
946 * Skip over assignable variable "var" or list of variables "[var, var]".
947 * Used for ":let varvar = expr" and ":for varvar in expr".
948 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200949 * for "[var, var; var]" set "semicolon" to 1.
950 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200951 * Return NULL for an error.
952 */
953 char_u *
954skip_var_list(
955 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200957 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200958 int *semicolon,
959 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200960{
961 char_u *p, *s;
962
963 if (*arg == '[')
964 {
965 // "[var, var]": find the matching ']'.
966 p = arg;
967 for (;;)
968 {
969 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200970 s = skip_var_one(p, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200971 if (s == p)
972 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200973 if (!silent)
974 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200975 return NULL;
976 }
977 ++*var_count;
978
979 p = skipwhite(s);
980 if (*p == ']')
981 break;
982 else if (*p == ';')
983 {
984 if (*semicolon == 1)
985 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200986 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200987 return NULL;
988 }
989 *semicolon = 1;
990 }
991 else if (*p != ',')
992 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200993 if (!silent)
994 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200995 return NULL;
996 }
997 }
998 return p + 1;
999 }
1000 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001001 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001002}
1003
1004/*
1005 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1006 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001008 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001009 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001010skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001011{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001012 char_u *end;
1013
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001014 if (*arg == '@' && arg[1] != NUL)
1015 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001016 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001017 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001018 if (include_type && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001019 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001020 // "a: type" is declaring variable "a" with a type, not "a:".
1021 if (end == arg + 2 && end[-1] == ':')
1022 --end;
1023 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001024 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001025 }
1026 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001027}
1028
1029/*
1030 * List variables for hashtab "ht" with prefix "prefix".
1031 * If "empty" is TRUE also list NULL strings as empty strings.
1032 */
1033 void
1034list_hashtable_vars(
1035 hashtab_T *ht,
1036 char *prefix,
1037 int empty,
1038 int *first)
1039{
1040 hashitem_T *hi;
1041 dictitem_T *di;
1042 int todo;
1043 char_u buf[IOSIZE];
1044
1045 todo = (int)ht->ht_used;
1046 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1047 {
1048 if (!HASHITEM_EMPTY(hi))
1049 {
1050 --todo;
1051 di = HI2DI(hi);
1052
1053 // apply :filter /pat/ to variable name
1054 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1055 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1056 if (message_filtered(buf))
1057 continue;
1058
1059 if (empty || di->di_tv.v_type != VAR_STRING
1060 || di->di_tv.vval.v_string != NULL)
1061 list_one_var(di, prefix, first);
1062 }
1063 }
1064}
1065
1066/*
1067 * List global variables.
1068 */
1069 static void
1070list_glob_vars(int *first)
1071{
1072 list_hashtable_vars(&globvarht, "", TRUE, first);
1073}
1074
1075/*
1076 * List buffer variables.
1077 */
1078 static void
1079list_buf_vars(int *first)
1080{
1081 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1082}
1083
1084/*
1085 * List window variables.
1086 */
1087 static void
1088list_win_vars(int *first)
1089{
1090 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1091}
1092
1093/*
1094 * List tab page variables.
1095 */
1096 static void
1097list_tab_vars(int *first)
1098{
1099 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1100}
1101
1102/*
1103 * List variables in "arg".
1104 */
1105 static char_u *
1106list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1107{
1108 int error = FALSE;
1109 int len;
1110 char_u *name;
1111 char_u *name_start;
1112 char_u *arg_subsc;
1113 char_u *tofree;
1114 typval_T tv;
1115
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001116 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001117 {
1118 if (error || eap->skip)
1119 {
1120 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1121 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1122 {
1123 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001124 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001125 break;
1126 }
1127 }
1128 else
1129 {
1130 // get_name_len() takes care of expanding curly braces
1131 name_start = name = arg;
1132 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1133 if (len <= 0)
1134 {
1135 // This is mainly to keep test 49 working: when expanding
1136 // curly braces fails overrule the exception error message.
1137 if (len < 0 && !aborting())
1138 {
1139 emsg_severe = TRUE;
1140 semsg(_(e_invarg2), arg);
1141 break;
1142 }
1143 error = TRUE;
1144 }
1145 else
1146 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001147 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001148 if (tofree != NULL)
1149 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001150 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001151 error = TRUE;
1152 else
1153 {
1154 // handle d.key, l[idx], f(expr)
1155 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001156 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001157 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001158 error = TRUE;
1159 else
1160 {
1161 if (arg == arg_subsc && len == 2 && name[1] == ':')
1162 {
1163 switch (*name)
1164 {
1165 case 'g': list_glob_vars(first); break;
1166 case 'b': list_buf_vars(first); break;
1167 case 'w': list_win_vars(first); break;
1168 case 't': list_tab_vars(first); break;
1169 case 'v': list_vim_vars(first); break;
1170 case 's': list_script_vars(first); break;
1171 case 'l': list_func_vars(first); break;
1172 default:
1173 semsg(_("E738: Can't list variables for %s"), name);
1174 }
1175 }
1176 else
1177 {
1178 char_u numbuf[NUMBUFLEN];
1179 char_u *tf;
1180 int c;
1181 char_u *s;
1182
1183 s = echo_string(&tv, &tf, numbuf, 0);
1184 c = *arg;
1185 *arg = NUL;
1186 list_one_var_a("",
1187 arg == arg_subsc ? name : name_start,
1188 tv.v_type,
1189 s == NULL ? (char_u *)"" : s,
1190 first);
1191 *arg = c;
1192 vim_free(tf);
1193 }
1194 clear_tv(&tv);
1195 }
1196 }
1197 }
1198
1199 vim_free(tofree);
1200 }
1201
1202 arg = skipwhite(arg);
1203 }
1204
1205 return arg;
1206}
1207
1208/*
1209 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1210 * Returns a pointer to the char just after the var name.
1211 * Returns NULL if there is an error.
1212 */
1213 static char_u *
1214ex_let_one(
1215 char_u *arg, // points to variable name
1216 typval_T *tv, // value to assign to variable
1217 int copy, // copy value from "tv"
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001218 int flags, // LET_IS_CONST, LET_FORCEIT, LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001219 char_u *endchars, // valid chars after variable name or NULL
1220 char_u *op) // "+", "-", "." or NULL
1221{
1222 int c1;
1223 char_u *name;
1224 char_u *p;
1225 char_u *arg_end = NULL;
1226 int len;
1227 int opt_flags;
1228 char_u *tofree = NULL;
1229
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001230 if (in_vim9script() && (flags & LET_NO_COMMAND) == 0
1231 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1232 {
1233 vim9_declare_error(arg);
1234 return NULL;
1235 }
1236
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001237 // ":let $VAR = expr": Set environment variable.
1238 if (*arg == '$')
1239 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001241 {
1242 emsg(_("E996: Cannot lock an environment variable"));
1243 return NULL;
1244 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001245
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001246 // Find the end of the name.
1247 ++arg;
1248 name = arg;
1249 len = get_env_len(&arg);
1250 if (len == 0)
1251 semsg(_(e_invarg2), name - 1);
1252 else
1253 {
1254 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1255 semsg(_(e_letwrong), op);
1256 else if (endchars != NULL
1257 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1258 emsg(_(e_letunexp));
1259 else if (!check_secure())
1260 {
1261 c1 = name[len];
1262 name[len] = NUL;
1263 p = tv_get_string_chk(tv);
1264 if (p != NULL && op != NULL && *op == '.')
1265 {
1266 int mustfree = FALSE;
1267 char_u *s = vim_getenv(name, &mustfree);
1268
1269 if (s != NULL)
1270 {
1271 p = tofree = concat_str(s, p);
1272 if (mustfree)
1273 vim_free(s);
1274 }
1275 }
1276 if (p != NULL)
1277 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001278 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001279 arg_end = arg;
1280 }
1281 name[len] = c1;
1282 vim_free(tofree);
1283 }
1284 }
1285 }
1286
1287 // ":let &option = expr": Set option value.
1288 // ":let &l:option = expr": Set local option value.
1289 // ":let &g:option = expr": Set global option value.
1290 else if (*arg == '&')
1291 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001292 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001293 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001294 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001295 return NULL;
1296 }
1297 // Find the end of the name.
1298 p = find_option_end(&arg, &opt_flags);
1299 if (p == NULL || (endchars != NULL
1300 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1301 emsg(_(e_letunexp));
1302 else
1303 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001304 long n = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001305 int opt_type;
1306 long numval;
1307 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001308 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001309 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001310
1311 c1 = *p;
1312 *p = NUL;
1313
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001314 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
1315 if ((opt_type == 1 || opt_type == -1)
1316 && (tv->v_type != VAR_STRING || !in_vim9script()))
1317 // number, possibly hidden
1318 n = (long)tv_get_number(tv);
1319
1320 // Avoid setting a string option to the text "v:false" or similar.
1321 // In Vim9 script also don't convert a number to string.
1322 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1323 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1324 s = tv_get_string_chk(tv);
1325
1326 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001327 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001328 if ((opt_type == 1 && *op == '.')
1329 || (opt_type == 0 && *op != '.'))
1330 {
1331 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001332 failed = TRUE; // don't set the value
1333
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001334 }
1335 else
1336 {
1337 if (opt_type == 1) // number
1338 {
1339 switch (*op)
1340 {
1341 case '+': n = numval + n; break;
1342 case '-': n = numval - n; break;
1343 case '*': n = numval * n; break;
1344 case '/': n = (long)num_divide(numval, n); break;
1345 case '%': n = (long)num_modulus(numval, n); break;
1346 }
1347 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001348 else if (opt_type == 0 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001349 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001350 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001351 s = concat_str(stringval, s);
1352 vim_free(stringval);
1353 stringval = s;
1354 }
1355 }
1356 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001357
1358 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001359 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001360 if (opt_type != 0 || s != NULL)
1361 {
1362 set_option_value(arg, n, s, opt_flags);
1363 arg_end = p;
1364 }
1365 else
1366 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001367 }
1368 *p = c1;
1369 vim_free(stringval);
1370 }
1371 }
1372
1373 // ":let @r = expr": Set register contents.
1374 else if (*arg == '@')
1375 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001377 {
1378 emsg(_("E996: Cannot lock a register"));
1379 return NULL;
1380 }
1381 ++arg;
1382 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1383 semsg(_(e_letwrong), op);
1384 else if (endchars != NULL
1385 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1386 emsg(_(e_letunexp));
1387 else
1388 {
1389 char_u *ptofree = NULL;
1390 char_u *s;
1391
1392 p = tv_get_string_chk(tv);
1393 if (p != NULL && op != NULL && *op == '.')
1394 {
1395 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1396 if (s != NULL)
1397 {
1398 p = ptofree = concat_str(s, p);
1399 vim_free(s);
1400 }
1401 }
1402 if (p != NULL)
1403 {
1404 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1405 arg_end = arg + 1;
1406 }
1407 vim_free(ptofree);
1408 }
1409 }
1410
1411 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001413 // ":let {expr} = expr": Idem, name made with curly braces
1414 else if (eval_isnamec1(*arg) || *arg == '{')
1415 {
1416 lval_T lv;
1417
1418 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001419 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001420 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421 if (endchars != NULL && vim_strchr(endchars,
1422 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001423 emsg(_(e_letunexp));
1424 else
1425 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001426 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001427 arg_end = p;
1428 }
1429 }
1430 clear_lval(&lv);
1431 }
1432
1433 else
1434 semsg(_(e_invarg2), arg);
1435
1436 return arg_end;
1437}
1438
1439/*
1440 * ":unlet[!] var1 ... " command.
1441 */
1442 void
1443ex_unlet(exarg_T *eap)
1444{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001445 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001446}
1447
1448/*
1449 * ":lockvar" and ":unlockvar" commands
1450 */
1451 void
1452ex_lockvar(exarg_T *eap)
1453{
1454 char_u *arg = eap->arg;
1455 int deep = 2;
1456
1457 if (eap->forceit)
1458 deep = -1;
1459 else if (vim_isdigit(*arg))
1460 {
1461 deep = getdigits(&arg);
1462 arg = skipwhite(arg);
1463 }
1464
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001465 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001466}
1467
1468/*
1469 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001470 * Also used for Vim9 script. "callback" is invoked as:
1471 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001472 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001473 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001474ex_unletlock(
1475 exarg_T *eap,
1476 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001477 int deep,
1478 int glv_flags,
1479 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1480 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001481{
1482 char_u *arg = argstart;
1483 char_u *name_end;
1484 int error = FALSE;
1485 lval_T lv;
1486
1487 do
1488 {
1489 if (*arg == '$')
1490 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001491 lv.ll_name = arg;
1492 lv.ll_tv = NULL;
1493 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001494 if (get_env_len(&arg) == 0)
1495 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001496 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001497 return;
1498 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001499 if (!error && !eap->skip
1500 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1501 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001502 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001503 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001504 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001505 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001506 // Parse the name and find the end.
1507 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1508 glv_flags, FNE_CHECK_START);
1509 if (lv.ll_name == NULL)
1510 error = TRUE; // error but continue parsing
1511 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1512 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001513 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001514 if (name_end != NULL)
1515 {
1516 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001517 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001518 }
1519 if (!(eap->skip || error))
1520 clear_lval(&lv);
1521 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001522 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001523
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001524 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001525 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001526 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001527
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001528 if (!eap->skip)
1529 clear_lval(&lv);
1530 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001531
1532 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001533 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001534
1535 eap->nextcmd = check_nextcmd(arg);
1536}
1537
1538 static int
1539do_unlet_var(
1540 lval_T *lp,
1541 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001542 exarg_T *eap,
1543 int deep UNUSED,
1544 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001545{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001546 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001547 int ret = OK;
1548 int cc;
1549
1550 if (lp->ll_tv == NULL)
1551 {
1552 cc = *name_end;
1553 *name_end = NUL;
1554
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001555 // Environment variable, normal name or expanded name.
1556 if (*lp->ll_name == '$')
1557 vim_unsetenv(lp->ll_name + 1);
1558 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001559 ret = FAIL;
1560 *name_end = cc;
1561 }
1562 else if ((lp->ll_list != NULL
1563 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
1564 || (lp->ll_dict != NULL
1565 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
1566 return FAIL;
1567 else if (lp->ll_range)
1568 {
1569 listitem_T *li;
1570 listitem_T *ll_li = lp->ll_li;
1571 int ll_n1 = lp->ll_n1;
1572
1573 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1574 {
1575 li = ll_li->li_next;
1576 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
1577 return FAIL;
1578 ll_li = li;
1579 ++ll_n1;
1580 }
1581
1582 // Delete a range of List items.
1583 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1584 {
1585 li = lp->ll_li->li_next;
1586 listitem_remove(lp->ll_list, lp->ll_li);
1587 lp->ll_li = li;
1588 ++lp->ll_n1;
1589 }
1590 }
1591 else
1592 {
1593 if (lp->ll_list != NULL)
1594 // unlet a List item.
1595 listitem_remove(lp->ll_list, lp->ll_li);
1596 else
1597 // unlet a Dictionary item.
1598 dictitem_remove(lp->ll_dict, lp->ll_di);
1599 }
1600
1601 return ret;
1602}
1603
1604/*
1605 * "unlet" a variable. Return OK if it existed, FAIL if not.
1606 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1607 */
1608 int
1609do_unlet(char_u *name, int forceit)
1610{
1611 hashtab_T *ht;
1612 hashitem_T *hi;
1613 char_u *varname;
1614 dict_T *d;
1615 dictitem_T *di;
1616
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001617 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001618 return FAIL;
1619
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001620 ht = find_var_ht(name, &varname);
1621 if (ht != NULL && *varname != NUL)
1622 {
1623 d = get_current_funccal_dict(ht);
1624 if (d == NULL)
1625 {
1626 if (ht == &globvarht)
1627 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001628 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001629 d = &vimvardict;
1630 else
1631 {
1632 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1633 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1634 }
1635 if (d == NULL)
1636 {
1637 internal_error("do_unlet()");
1638 return FAIL;
1639 }
1640 }
1641 hi = hash_find(ht, varname);
1642 if (HASHITEM_EMPTY(hi))
1643 hi = find_hi_in_scoped_ht(name, &ht);
1644 if (hi != NULL && !HASHITEM_EMPTY(hi))
1645 {
1646 di = HI2DI(hi);
1647 if (var_check_fixed(di->di_flags, name, FALSE)
1648 || var_check_ro(di->di_flags, name, FALSE)
1649 || var_check_lock(d->dv_lock, name, FALSE))
1650 return FAIL;
1651
1652 delete_var(ht, hi);
1653 return OK;
1654 }
1655 }
1656 if (forceit)
1657 return OK;
1658 semsg(_("E108: No such variable: \"%s\""), name);
1659 return FAIL;
1660}
1661
1662/*
1663 * Lock or unlock variable indicated by "lp".
1664 * "deep" is the levels to go (-1 for unlimited);
1665 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1666 */
1667 static int
1668do_lock_var(
1669 lval_T *lp,
1670 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001671 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001672 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001673 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001674{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001675 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001676 int ret = OK;
1677 int cc;
1678 dictitem_T *di;
1679
1680 if (deep == 0) // nothing to do
1681 return OK;
1682
1683 if (lp->ll_tv == NULL)
1684 {
1685 cc = *name_end;
1686 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001687 if (*lp->ll_name == '$')
1688 {
1689 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001690 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001691 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001692 else
1693 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001694 // Normal name or expanded name.
1695 di = find_var(lp->ll_name, NULL, TRUE);
1696 if (di == NULL)
1697 ret = FAIL;
1698 else if ((di->di_flags & DI_FLAGS_FIX)
1699 && di->di_tv.v_type != VAR_DICT
1700 && di->di_tv.v_type != VAR_LIST)
1701 {
1702 // For historic reasons this error is not given for a list or
1703 // dict. E.g., the b: dict could be locked/unlocked.
1704 semsg(_(e_lock_unlock), lp->ll_name);
1705 ret = FAIL;
1706 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001707 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001708 {
1709 if (lock)
1710 di->di_flags |= DI_FLAGS_LOCK;
1711 else
1712 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001713 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001714 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001715 }
1716 *name_end = cc;
1717 }
1718 else if (lp->ll_range)
1719 {
1720 listitem_T *li = lp->ll_li;
1721
1722 // (un)lock a range of List items.
1723 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1724 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001725 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001726 li = li->li_next;
1727 ++lp->ll_n1;
1728 }
1729 }
1730 else if (lp->ll_list != NULL)
1731 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001732 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001733 else
1734 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001735 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001736
1737 return ret;
1738}
1739
1740/*
1741 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001742 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1743 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001744 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001745 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001746item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001747{
1748 static int recurse = 0;
1749 list_T *l;
1750 listitem_T *li;
1751 dict_T *d;
1752 blob_T *b;
1753 hashitem_T *hi;
1754 int todo;
1755
1756 if (recurse >= DICT_MAXNEST)
1757 {
1758 emsg(_("E743: variable nested too deep for (un)lock"));
1759 return;
1760 }
1761 if (deep == 0)
1762 return;
1763 ++recurse;
1764
1765 // lock/unlock the item itself
1766 if (lock)
1767 tv->v_lock |= VAR_LOCKED;
1768 else
1769 tv->v_lock &= ~VAR_LOCKED;
1770
1771 switch (tv->v_type)
1772 {
1773 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001774 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001776 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001777 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001778 case VAR_STRING:
1779 case VAR_FUNC:
1780 case VAR_PARTIAL:
1781 case VAR_FLOAT:
1782 case VAR_SPECIAL:
1783 case VAR_JOB:
1784 case VAR_CHANNEL:
1785 break;
1786
1787 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001788 if ((b = tv->vval.v_blob) != NULL
1789 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001790 {
1791 if (lock)
1792 b->bv_lock |= VAR_LOCKED;
1793 else
1794 b->bv_lock &= ~VAR_LOCKED;
1795 }
1796 break;
1797 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001798 if ((l = tv->vval.v_list) != NULL
1799 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001800 {
1801 if (lock)
1802 l->lv_lock |= VAR_LOCKED;
1803 else
1804 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001805 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001806 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001807 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001808 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001809 }
1810 break;
1811 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001812 if ((d = tv->vval.v_dict) != NULL
1813 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001814 {
1815 if (lock)
1816 d->dv_lock |= VAR_LOCKED;
1817 else
1818 d->dv_lock &= ~VAR_LOCKED;
1819 if (deep < 0 || deep > 1)
1820 {
1821 // recursive: lock/unlock the items the List contains
1822 todo = (int)d->dv_hashtab.ht_used;
1823 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1824 {
1825 if (!HASHITEM_EMPTY(hi))
1826 {
1827 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001828 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1829 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001830 }
1831 }
1832 }
1833 }
1834 }
1835 --recurse;
1836}
1837
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001838#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1839/*
1840 * Delete all "menutrans_" variables.
1841 */
1842 void
1843del_menutrans_vars(void)
1844{
1845 hashitem_T *hi;
1846 int todo;
1847
1848 hash_lock(&globvarht);
1849 todo = (int)globvarht.ht_used;
1850 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1851 {
1852 if (!HASHITEM_EMPTY(hi))
1853 {
1854 --todo;
1855 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1856 delete_var(&globvarht, hi);
1857 }
1858 }
1859 hash_unlock(&globvarht);
1860}
1861#endif
1862
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001863/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001864 * Local string buffer for the next two functions to store a variable name
1865 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1866 * get_user_var_name().
1867 */
1868
1869static char_u *varnamebuf = NULL;
1870static int varnamebuflen = 0;
1871
1872/*
1873 * Function to concatenate a prefix and a variable name.
1874 */
1875 static char_u *
1876cat_prefix_varname(int prefix, char_u *name)
1877{
1878 int len;
1879
1880 len = (int)STRLEN(name) + 3;
1881 if (len > varnamebuflen)
1882 {
1883 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001884 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001885 varnamebuf = alloc(len);
1886 if (varnamebuf == NULL)
1887 {
1888 varnamebuflen = 0;
1889 return NULL;
1890 }
1891 varnamebuflen = len;
1892 }
1893 *varnamebuf = prefix;
1894 varnamebuf[1] = ':';
1895 STRCPY(varnamebuf + 2, name);
1896 return varnamebuf;
1897}
1898
1899/*
1900 * Function given to ExpandGeneric() to obtain the list of user defined
1901 * (global/buffer/window/built-in) variable names.
1902 */
1903 char_u *
1904get_user_var_name(expand_T *xp, int idx)
1905{
1906 static long_u gdone;
1907 static long_u bdone;
1908 static long_u wdone;
1909 static long_u tdone;
1910 static int vidx;
1911 static hashitem_T *hi;
1912 hashtab_T *ht;
1913
1914 if (idx == 0)
1915 {
1916 gdone = bdone = wdone = vidx = 0;
1917 tdone = 0;
1918 }
1919
1920 // Global variables
1921 if (gdone < globvarht.ht_used)
1922 {
1923 if (gdone++ == 0)
1924 hi = globvarht.ht_array;
1925 else
1926 ++hi;
1927 while (HASHITEM_EMPTY(hi))
1928 ++hi;
1929 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1930 return cat_prefix_varname('g', hi->hi_key);
1931 return hi->hi_key;
1932 }
1933
1934 // b: variables
1935 ht = &curbuf->b_vars->dv_hashtab;
1936 if (bdone < ht->ht_used)
1937 {
1938 if (bdone++ == 0)
1939 hi = ht->ht_array;
1940 else
1941 ++hi;
1942 while (HASHITEM_EMPTY(hi))
1943 ++hi;
1944 return cat_prefix_varname('b', hi->hi_key);
1945 }
1946
1947 // w: variables
1948 ht = &curwin->w_vars->dv_hashtab;
1949 if (wdone < ht->ht_used)
1950 {
1951 if (wdone++ == 0)
1952 hi = ht->ht_array;
1953 else
1954 ++hi;
1955 while (HASHITEM_EMPTY(hi))
1956 ++hi;
1957 return cat_prefix_varname('w', hi->hi_key);
1958 }
1959
1960 // t: variables
1961 ht = &curtab->tp_vars->dv_hashtab;
1962 if (tdone < ht->ht_used)
1963 {
1964 if (tdone++ == 0)
1965 hi = ht->ht_array;
1966 else
1967 ++hi;
1968 while (HASHITEM_EMPTY(hi))
1969 ++hi;
1970 return cat_prefix_varname('t', hi->hi_key);
1971 }
1972
1973 // v: variables
1974 if (vidx < VV_LEN)
1975 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
1976
1977 VIM_CLEAR(varnamebuf);
1978 varnamebuflen = 0;
1979 return NULL;
1980}
1981
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001982 char *
1983get_var_special_name(int nr)
1984{
1985 switch (nr)
1986 {
1987 case VVAL_FALSE: return "v:false";
1988 case VVAL_TRUE: return "v:true";
1989 case VVAL_NONE: return "v:none";
1990 case VVAL_NULL: return "v:null";
1991 }
1992 internal_error("get_var_special_name()");
1993 return "42";
1994}
1995
1996/*
1997 * Returns the global variable dictionary
1998 */
1999 dict_T *
2000get_globvar_dict(void)
2001{
2002 return &globvardict;
2003}
2004
2005/*
2006 * Returns the global variable hash table
2007 */
2008 hashtab_T *
2009get_globvar_ht(void)
2010{
2011 return &globvarht;
2012}
2013
2014/*
2015 * Returns the v: variable dictionary
2016 */
2017 dict_T *
2018get_vimvar_dict(void)
2019{
2020 return &vimvardict;
2021}
2022
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002023/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002025 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002026 */
2027 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002028find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002030 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2031 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032
2033 if (di == NULL)
2034 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002035 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002036 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2037 return (int)(vv - vimvars);
2038}
2039
2040
2041/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002042 * Set type of v: variable to "type".
2043 */
2044 void
2045set_vim_var_type(int idx, vartype_T type)
2046{
2047 vimvars[idx].vv_type = type;
2048}
2049
2050/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002051 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002052 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002053 */
2054 void
2055set_vim_var_nr(int idx, varnumber_T val)
2056{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002057 vimvars[idx].vv_nr = val;
2058}
2059
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002060 char *
2061get_vim_var_name(int idx)
2062{
2063 return vimvars[idx].vv_name;
2064}
2065
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002066/*
2067 * Get typval_T v: variable value.
2068 */
2069 typval_T *
2070get_vim_var_tv(int idx)
2071{
2072 return &vimvars[idx].vv_tv;
2073}
2074
2075/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002076 * Set v: variable to "tv". Only accepts the same type.
2077 * Takes over the value of "tv".
2078 */
2079 int
2080set_vim_var_tv(int idx, typval_T *tv)
2081{
2082 if (vimvars[idx].vv_type != tv->v_type)
2083 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002084 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002085 clear_tv(tv);
2086 return FAIL;
2087 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002088 // VV_RO is also checked when compiling, but let's check here as well.
2089 if (vimvars[idx].vv_flags & VV_RO)
2090 {
2091 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2092 return FAIL;
2093 }
2094 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2095 {
2096 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2097 return FAIL;
2098 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002099 clear_tv(&vimvars[idx].vv_di.di_tv);
2100 vimvars[idx].vv_di.di_tv = *tv;
2101 return OK;
2102}
2103
2104/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002105 * Get number v: variable value.
2106 */
2107 varnumber_T
2108get_vim_var_nr(int idx)
2109{
2110 return vimvars[idx].vv_nr;
2111}
2112
2113/*
2114 * Get string v: variable value. Uses a static buffer, can only be used once.
2115 * If the String variable has never been set, return an empty string.
2116 * Never returns NULL;
2117 */
2118 char_u *
2119get_vim_var_str(int idx)
2120{
2121 return tv_get_string(&vimvars[idx].vv_tv);
2122}
2123
2124/*
2125 * Get List v: variable value. Caller must take care of reference count when
2126 * needed.
2127 */
2128 list_T *
2129get_vim_var_list(int idx)
2130{
2131 return vimvars[idx].vv_list;
2132}
2133
2134/*
2135 * Get Dict v: variable value. Caller must take care of reference count when
2136 * needed.
2137 */
2138 dict_T *
2139get_vim_var_dict(int idx)
2140{
2141 return vimvars[idx].vv_dict;
2142}
2143
2144/*
2145 * Set v:char to character "c".
2146 */
2147 void
2148set_vim_var_char(int c)
2149{
2150 char_u buf[MB_MAXBYTES + 1];
2151
2152 if (has_mbyte)
2153 buf[(*mb_char2bytes)(c, buf)] = NUL;
2154 else
2155 {
2156 buf[0] = c;
2157 buf[1] = NUL;
2158 }
2159 set_vim_var_string(VV_CHAR, buf, -1);
2160}
2161
2162/*
2163 * Set v:count to "count" and v:count1 to "count1".
2164 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2165 */
2166 void
2167set_vcount(
2168 long count,
2169 long count1,
2170 int set_prevcount)
2171{
2172 if (set_prevcount)
2173 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2174 vimvars[VV_COUNT].vv_nr = count;
2175 vimvars[VV_COUNT1].vv_nr = count1;
2176}
2177
2178/*
2179 * Save variables that might be changed as a side effect. Used when executing
2180 * a timer callback.
2181 */
2182 void
2183save_vimvars(vimvars_save_T *vvsave)
2184{
2185 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2186 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2187 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2188}
2189
2190/*
2191 * Restore variables saved by save_vimvars().
2192 */
2193 void
2194restore_vimvars(vimvars_save_T *vvsave)
2195{
2196 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2197 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2198 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2199}
2200
2201/*
2202 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2203 * value.
2204 */
2205 void
2206set_vim_var_string(
2207 int idx,
2208 char_u *val,
2209 int len) // length of "val" to use or -1 (whole string)
2210{
2211 clear_tv(&vimvars[idx].vv_di.di_tv);
2212 vimvars[idx].vv_type = VAR_STRING;
2213 if (val == NULL)
2214 vimvars[idx].vv_str = NULL;
2215 else if (len == -1)
2216 vimvars[idx].vv_str = vim_strsave(val);
2217 else
2218 vimvars[idx].vv_str = vim_strnsave(val, len);
2219}
2220
2221/*
2222 * Set List v: variable to "val".
2223 */
2224 void
2225set_vim_var_list(int idx, list_T *val)
2226{
2227 clear_tv(&vimvars[idx].vv_di.di_tv);
2228 vimvars[idx].vv_type = VAR_LIST;
2229 vimvars[idx].vv_list = val;
2230 if (val != NULL)
2231 ++val->lv_refcount;
2232}
2233
2234/*
2235 * Set Dictionary v: variable to "val".
2236 */
2237 void
2238set_vim_var_dict(int idx, dict_T *val)
2239{
2240 clear_tv(&vimvars[idx].vv_di.di_tv);
2241 vimvars[idx].vv_type = VAR_DICT;
2242 vimvars[idx].vv_dict = val;
2243 if (val != NULL)
2244 {
2245 ++val->dv_refcount;
2246 dict_set_items_ro(val);
2247 }
2248}
2249
2250/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002251 * Set the v:argv list.
2252 */
2253 void
2254set_argv_var(char **argv, int argc)
2255{
2256 list_T *l = list_alloc();
2257 int i;
2258
2259 if (l == NULL)
2260 getout(1);
2261 l->lv_lock = VAR_FIXED;
2262 for (i = 0; i < argc; ++i)
2263 {
2264 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2265 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002266 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002267 }
2268 set_vim_var_list(VV_ARGV, l);
2269}
2270
2271/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002272 * Reset v:register, taking the 'clipboard' setting into account.
2273 */
2274 void
2275reset_reg_var(void)
2276{
2277 int regname = 0;
2278
2279 // Adjust the register according to 'clipboard', so that when
2280 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2281#ifdef FEAT_CLIPBOARD
2282 adjust_clip_reg(&regname);
2283#endif
2284 set_reg_var(regname);
2285}
2286
2287/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002288 * Set v:register if needed.
2289 */
2290 void
2291set_reg_var(int c)
2292{
2293 char_u regname;
2294
2295 if (c == 0 || c == ' ')
2296 regname = '"';
2297 else
2298 regname = c;
2299 // Avoid free/alloc when the value is already right.
2300 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2301 set_vim_var_string(VV_REG, &regname, 1);
2302}
2303
2304/*
2305 * Get or set v:exception. If "oldval" == NULL, return the current value.
2306 * Otherwise, restore the value to "oldval" and return NULL.
2307 * Must always be called in pairs to save and restore v:exception! Does not
2308 * take care of memory allocations.
2309 */
2310 char_u *
2311v_exception(char_u *oldval)
2312{
2313 if (oldval == NULL)
2314 return vimvars[VV_EXCEPTION].vv_str;
2315
2316 vimvars[VV_EXCEPTION].vv_str = oldval;
2317 return NULL;
2318}
2319
2320/*
2321 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2322 * Otherwise, restore the value to "oldval" and return NULL.
2323 * Must always be called in pairs to save and restore v:throwpoint! Does not
2324 * take care of memory allocations.
2325 */
2326 char_u *
2327v_throwpoint(char_u *oldval)
2328{
2329 if (oldval == NULL)
2330 return vimvars[VV_THROWPOINT].vv_str;
2331
2332 vimvars[VV_THROWPOINT].vv_str = oldval;
2333 return NULL;
2334}
2335
2336/*
2337 * Set v:cmdarg.
2338 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2339 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2340 * Must always be called in pairs!
2341 */
2342 char_u *
2343set_cmdarg(exarg_T *eap, char_u *oldarg)
2344{
2345 char_u *oldval;
2346 char_u *newval;
2347 unsigned len;
2348
2349 oldval = vimvars[VV_CMDARG].vv_str;
2350 if (eap == NULL)
2351 {
2352 vim_free(oldval);
2353 vimvars[VV_CMDARG].vv_str = oldarg;
2354 return NULL;
2355 }
2356
2357 if (eap->force_bin == FORCE_BIN)
2358 len = 6;
2359 else if (eap->force_bin == FORCE_NOBIN)
2360 len = 8;
2361 else
2362 len = 0;
2363
2364 if (eap->read_edit)
2365 len += 7;
2366
2367 if (eap->force_ff != 0)
2368 len += 10; // " ++ff=unix"
2369 if (eap->force_enc != 0)
2370 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2371 if (eap->bad_char != 0)
2372 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2373
2374 newval = alloc(len + 1);
2375 if (newval == NULL)
2376 return NULL;
2377
2378 if (eap->force_bin == FORCE_BIN)
2379 sprintf((char *)newval, " ++bin");
2380 else if (eap->force_bin == FORCE_NOBIN)
2381 sprintf((char *)newval, " ++nobin");
2382 else
2383 *newval = NUL;
2384
2385 if (eap->read_edit)
2386 STRCAT(newval, " ++edit");
2387
2388 if (eap->force_ff != 0)
2389 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2390 eap->force_ff == 'u' ? "unix"
2391 : eap->force_ff == 'd' ? "dos"
2392 : "mac");
2393 if (eap->force_enc != 0)
2394 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2395 eap->cmd + eap->force_enc);
2396 if (eap->bad_char == BAD_KEEP)
2397 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2398 else if (eap->bad_char == BAD_DROP)
2399 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2400 else if (eap->bad_char != 0)
2401 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2402 vimvars[VV_CMDARG].vv_str = newval;
2403 return oldval;
2404}
2405
2406/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002407 * Get the value of internal variable "name".
2408 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2409 */
2410 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002411eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002412 char_u *name,
2413 int len, // length of "name"
2414 typval_T *rettv, // NULL when only checking existence
2415 dictitem_T **dip, // non-NULL when typval's dict item is needed
2416 int verbose, // may give error message
2417 int no_autoload) // do not use script autoloading
2418{
2419 int ret = OK;
2420 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002421 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002422 dictitem_T *v;
2423 int cc;
2424
2425 // truncate the name, so that we can use strcmp()
2426 cc = name[len];
2427 name[len] = NUL;
2428
2429 // Check for user-defined variables.
2430 v = find_var(name, NULL, no_autoload);
2431 if (v != NULL)
2432 {
2433 tv = &v->di_tv;
2434 if (dip != NULL)
2435 *dip = v;
2436 }
2437
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002438 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002439 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002440 imported_T *import;
2441 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2442
2443 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002444
2445 // imported variable from another script
2446 if (import != NULL)
2447 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002448 if (import->imp_funcname != NULL)
2449 {
2450 foundFunc = TRUE;
2451 if (rettv != NULL)
2452 {
2453 rettv->v_type = VAR_FUNC;
2454 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2455 }
2456 }
2457 else
2458 {
2459 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002460 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002461 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002462 tv = sv->sv_tv;
2463 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002464 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002465 else if (in_vim9script())
2466 {
2467 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2468
2469 if (ufunc != NULL)
2470 {
2471 foundFunc = TRUE;
2472 if (rettv != NULL)
2473 {
2474 rettv->v_type = VAR_FUNC;
2475 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2476 }
2477 }
2478 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 }
2480
Bram Moolenaarc620c052020-07-08 15:16:19 +02002481 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002482 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002483 if (tv == NULL)
2484 {
2485 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002486 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002487 ret = FAIL;
2488 }
2489 else if (rettv != NULL)
2490 copy_tv(tv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002491 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002492
2493 name[len] = cc;
2494
2495 return ret;
2496}
2497
2498/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002499 * Check if variable "name[len]" is a local variable or an argument.
2500 * If so, "*eval_lavars_used" is set to TRUE.
2501 */
2502 void
2503check_vars(char_u *name, int len)
2504{
2505 int cc;
2506 char_u *varname;
2507 hashtab_T *ht;
2508
2509 if (eval_lavars_used == NULL)
2510 return;
2511
2512 // truncate the name, so that we can use strcmp()
2513 cc = name[len];
2514 name[len] = NUL;
2515
2516 ht = find_var_ht(name, &varname);
2517 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2518 {
2519 if (find_var(name, NULL, TRUE) != NULL)
2520 *eval_lavars_used = TRUE;
2521 }
2522
2523 name[len] = cc;
2524}
2525
2526/*
2527 * Find variable "name" in the list of variables.
2528 * Return a pointer to it if found, NULL if not found.
2529 * Careful: "a:0" variables don't have a name.
2530 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2531 * hashtab_T used.
2532 */
2533 dictitem_T *
2534find_var(char_u *name, hashtab_T **htp, int no_autoload)
2535{
2536 char_u *varname;
2537 hashtab_T *ht;
2538 dictitem_T *ret = NULL;
2539
2540 ht = find_var_ht(name, &varname);
2541 if (htp != NULL)
2542 *htp = ht;
2543 if (ht == NULL)
2544 return NULL;
2545 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2546 if (ret != NULL)
2547 return ret;
2548
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002549 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002550 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2551}
2552
2553/*
2554 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002555 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002556 * Returns NULL if not found.
2557 */
2558 dictitem_T *
2559find_var_in_ht(
2560 hashtab_T *ht,
2561 int htname,
2562 char_u *varname,
2563 int no_autoload)
2564{
2565 hashitem_T *hi;
2566
2567 if (*varname == NUL)
2568 {
2569 // Must be something like "s:", otherwise "ht" would be NULL.
2570 switch (htname)
2571 {
2572 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2573 case 'g': return &globvars_var;
2574 case 'v': return &vimvars_var;
2575 case 'b': return &curbuf->b_bufvar;
2576 case 'w': return &curwin->w_winvar;
2577 case 't': return &curtab->tp_winvar;
2578 case 'l': return get_funccal_local_var();
2579 case 'a': return get_funccal_args_var();
2580 }
2581 return NULL;
2582 }
2583
2584 hi = hash_find(ht, varname);
2585 if (HASHITEM_EMPTY(hi))
2586 {
2587 // For global variables we may try auto-loading the script. If it
2588 // worked find the variable again. Don't auto-load a script if it was
2589 // loaded already, otherwise it would be loaded every time when
2590 // checking if a function name is a Funcref variable.
2591 if (ht == &globvarht && !no_autoload)
2592 {
2593 // Note: script_autoload() may make "hi" invalid. It must either
2594 // be obtained again or not used.
2595 if (!script_autoload(varname, FALSE) || aborting())
2596 return NULL;
2597 hi = hash_find(ht, varname);
2598 }
2599 if (HASHITEM_EMPTY(hi))
2600 return NULL;
2601 }
2602 return HI2DI(hi);
2603}
2604
2605/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606 * Get the script-local hashtab. NULL if not in a script context.
2607 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02002608 static hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609get_script_local_ht(void)
2610{
2611 scid_T sid = current_sctx.sc_sid;
2612
Bram Moolenaare3d46852020-08-29 13:39:17 +02002613 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614 return &SCRIPT_VARS(sid);
2615 return NULL;
2616}
2617
2618/*
2619 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002620 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002622 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002623lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2624{
2625 hashtab_T *ht = get_script_local_ht();
2626 char_u buffer[30];
2627 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002628 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629 hashitem_T *hi;
2630
2631 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002632 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 if (len < sizeof(buffer) - 1)
2634 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002635 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636 vim_strncpy(buffer, name, len);
2637 p = buffer;
2638 }
2639 else
2640 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002641 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002643 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 }
2645
2646 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002647 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648
2649 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002650 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2651 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002652
2653 if (p != buffer)
2654 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002655 // Don't return "buffer", gcc complains.
2656 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657}
2658
2659/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002660 * Find the hashtab used for a variable name.
2661 * Return NULL if the name is not valid.
2662 * Set "varname" to the start of name without ':'.
2663 */
2664 hashtab_T *
2665find_var_ht(char_u *name, char_u **varname)
2666{
2667 hashitem_T *hi;
2668 hashtab_T *ht;
2669
2670 if (name[0] == NUL)
2671 return NULL;
2672 if (name[1] != ':')
2673 {
2674 // The name must not start with a colon or #.
2675 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2676 return NULL;
2677 *varname = name;
2678
2679 // "version" is "v:version" in all scopes if scriptversion < 3.
2680 // Same for a few other variables marked with VV_COMPAT.
2681 if (current_sctx.sc_version < 3)
2682 {
2683 hi = hash_find(&compat_hashtab, name);
2684 if (!HASHITEM_EMPTY(hi))
2685 return &compat_hashtab;
2686 }
2687
2688 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002689 if (ht != NULL)
2690 return ht; // local variable
2691
2692 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002693 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694 {
2695 ht = get_script_local_ht();
2696 if (ht != NULL)
2697 return ht;
2698 }
2699
2700 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002701 }
2702 *varname = name + 2;
2703 if (*name == 'g') // global variable
2704 return &globvarht;
2705 // There must be no ':' or '#' in the rest of the name, unless g: is used
2706 if (vim_strchr(name + 2, ':') != NULL
2707 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2708 return NULL;
2709 if (*name == 'b') // buffer variable
2710 return &curbuf->b_vars->dv_hashtab;
2711 if (*name == 'w') // window variable
2712 return &curwin->w_vars->dv_hashtab;
2713 if (*name == 't') // tab page variable
2714 return &curtab->tp_vars->dv_hashtab;
2715 if (*name == 'v') // v: variable
2716 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002717 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002718 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002720 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002721 if (*name == 'a') // a: function argument
2722 return get_funccal_args_ht();
2723 if (*name == 'l') // l: local function variable
2724 return get_funccal_local_ht();
2725 }
2726 if (*name == 's') // script variable
2727 {
2728 ht = get_script_local_ht();
2729 if (ht != NULL)
2730 return ht;
2731 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002732 return NULL;
2733}
2734
2735/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002736 * Get the string value of a (global/local) variable.
2737 * Note: see tv_get_string() for how long the pointer remains valid.
2738 * Returns NULL when it doesn't exist.
2739 */
2740 char_u *
2741get_var_value(char_u *name)
2742{
2743 dictitem_T *v;
2744
2745 v = find_var(name, NULL, FALSE);
2746 if (v == NULL)
2747 return NULL;
2748 return tv_get_string(&v->di_tv);
2749}
2750
2751/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002752 * Allocate a new hashtab for a sourced script. It will be used while
2753 * sourcing this script and when executing functions defined in the script.
2754 */
2755 void
2756new_script_vars(scid_T id)
2757{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002758 scriptvar_T *sv;
2759
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002760 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2761 if (sv == NULL)
2762 return;
2763 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002764 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002765}
2766
2767/*
2768 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2769 * point to it.
2770 */
2771 void
2772init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2773{
2774 hash_init(&dict->dv_hashtab);
2775 dict->dv_lock = 0;
2776 dict->dv_scope = scope;
2777 dict->dv_refcount = DO_NOT_FREE_CNT;
2778 dict->dv_copyID = 0;
2779 dict_var->di_tv.vval.v_dict = dict;
2780 dict_var->di_tv.v_type = VAR_DICT;
2781 dict_var->di_tv.v_lock = VAR_FIXED;
2782 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2783 dict_var->di_key[0] = NUL;
2784}
2785
2786/*
2787 * Unreference a dictionary initialized by init_var_dict().
2788 */
2789 void
2790unref_var_dict(dict_T *dict)
2791{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002792 // Now the dict needs to be freed if no one else is using it, go back to
2793 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002794 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2795 dict_unref(dict);
2796}
2797
2798/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002799 * Clean up a list of internal variables.
2800 * Frees all allocated variables and the value they contain.
2801 * Clears hashtab "ht", does not free it.
2802 */
2803 void
2804vars_clear(hashtab_T *ht)
2805{
2806 vars_clear_ext(ht, TRUE);
2807}
2808
2809/*
2810 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2811 */
2812 void
2813vars_clear_ext(hashtab_T *ht, int free_val)
2814{
2815 int todo;
2816 hashitem_T *hi;
2817 dictitem_T *v;
2818
2819 hash_lock(ht);
2820 todo = (int)ht->ht_used;
2821 for (hi = ht->ht_array; todo > 0; ++hi)
2822 {
2823 if (!HASHITEM_EMPTY(hi))
2824 {
2825 --todo;
2826
2827 // Free the variable. Don't remove it from the hashtab,
2828 // ht_array might change then. hash_clear() takes care of it
2829 // later.
2830 v = HI2DI(hi);
2831 if (free_val)
2832 clear_tv(&v->di_tv);
2833 if (v->di_flags & DI_FLAGS_ALLOC)
2834 vim_free(v);
2835 }
2836 }
2837 hash_clear(ht);
2838 ht->ht_used = 0;
2839}
2840
2841/*
2842 * Delete a variable from hashtab "ht" at item "hi".
2843 * Clear the variable value and free the dictitem.
2844 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002845 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002846delete_var(hashtab_T *ht, hashitem_T *hi)
2847{
2848 dictitem_T *di = HI2DI(hi);
2849
2850 hash_remove(ht, hi);
2851 clear_tv(&di->di_tv);
2852 vim_free(di);
2853}
2854
2855/*
2856 * List the value of one internal variable.
2857 */
2858 static void
2859list_one_var(dictitem_T *v, char *prefix, int *first)
2860{
2861 char_u *tofree;
2862 char_u *s;
2863 char_u numbuf[NUMBUFLEN];
2864
2865 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2866 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2867 s == NULL ? (char_u *)"" : s, first);
2868 vim_free(tofree);
2869}
2870
2871 static void
2872list_one_var_a(
2873 char *prefix,
2874 char_u *name,
2875 int type,
2876 char_u *string,
2877 int *first) // when TRUE clear rest of screen and set to FALSE
2878{
2879 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2880 msg_start();
2881 msg_puts(prefix);
2882 if (name != NULL) // "a:" vars don't have a name stored
2883 msg_puts((char *)name);
2884 msg_putchar(' ');
2885 msg_advance(22);
2886 if (type == VAR_NUMBER)
2887 msg_putchar('#');
2888 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2889 msg_putchar('*');
2890 else if (type == VAR_LIST)
2891 {
2892 msg_putchar('[');
2893 if (*string == '[')
2894 ++string;
2895 }
2896 else if (type == VAR_DICT)
2897 {
2898 msg_putchar('{');
2899 if (*string == '{')
2900 ++string;
2901 }
2902 else
2903 msg_putchar(' ');
2904
2905 msg_outtrans(string);
2906
2907 if (type == VAR_FUNC || type == VAR_PARTIAL)
2908 msg_puts("()");
2909 if (*first)
2910 {
2911 msg_clr_eos();
2912 *first = FALSE;
2913 }
2914}
2915
2916/*
2917 * Set variable "name" to value in "tv".
2918 * If the variable already exists, the value is updated.
2919 * Otherwise the variable is created.
2920 */
2921 void
2922set_var(
2923 char_u *name,
2924 typval_T *tv,
2925 int copy) // make copy of value in "tv"
2926{
Bram Moolenaareeb27bf2020-07-04 17:39:10 +02002927 set_var_const(name, NULL, tv, copy, LET_NO_COMMAND);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002928}
2929
2930/*
2931 * Set variable "name" to value in "tv".
2932 * If the variable already exists and "is_const" is FALSE the value is updated.
2933 * Otherwise the variable is created.
2934 */
2935 void
2936set_var_const(
2937 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002939 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002940 int copy, // make copy of value in "tv"
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002941 int flags) // LET_IS_CONST, LET_FORCEIT, LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002942{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002943 typval_T *tv = tv_arg;
2944 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002946 char_u *varname;
2947 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02002949 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002950
2951 ht = find_var_ht(name, &varname);
2952 if (ht == NULL || *varname == NUL)
2953 {
2954 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02002955 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002956 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002957 is_script_local = ht == get_script_local_ht();
2958
Bram Moolenaardbeecb22020-09-14 18:15:09 +02002959 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002960 && !is_script_local
2961 && (flags & LET_NO_COMMAND) == 0
2962 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02002963 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002964 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02002965 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02002966 }
2967
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002969
2970 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 if (di == NULL)
2972 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002973
2974 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02002975 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02002976 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002977
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002978 if (need_convert_to_bool(type, tv))
2979 {
2980 // Destination is a bool and the value is not, but it can be converted.
2981 CLEAR_FIELD(bool_tv);
2982 bool_tv.v_type = VAR_BOOL;
2983 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
2984 tv = &bool_tv;
2985 }
2986
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002988 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002990 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 if (flags & LET_IS_CONST)
2992 {
2993 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02002994 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 }
2996
Bram Moolenaardbeecb22020-09-14 18:15:09 +02002997 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998 {
Bram Moolenaar34db91f2020-06-13 19:00:10 +02002999 if ((flags & LET_NO_COMMAND) == 0)
3000 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003001 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003002 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003003 }
3004
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003005 // check the type and adjust to bool if needed
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003006 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003007 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003009
3010 if (var_check_ro(di->di_flags, name, FALSE)
3011 || var_check_lock(di->di_tv.v_lock, name, FALSE))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003012 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003013 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 else
3015 // can only redefine once
3016 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003017
3018 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003019
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003021 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003022 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003023 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003025 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003027 if (copy || tv->v_type != VAR_STRING)
3028 {
3029 char_u *val = tv_get_string(tv);
3030
3031 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003032 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 if (di->di_tv.vval.v_string == NULL)
3034 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003035 }
3036 else
3037 {
3038 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003040 tv->vval.v_string = NULL;
3041 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003042 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003043 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003045 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003047 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003049#ifdef FEAT_SEARCH_EXTRA
3050 else if (STRCMP(varname, "hlsearch") == 0)
3051 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003053 redraw_all_later(SOME_VALID);
3054 }
3055#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003056 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003057 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003059 {
3060 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003061 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003062 }
3063 }
3064
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003066 }
3067 else // add a new variable
3068 {
3069 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003070 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003071 {
3072 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003073 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003074 }
3075
3076 // Make sure the variable name is valid.
3077 if (!valid_varname(varname))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003078 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003079
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3081 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003082 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 STRCPY(di->di_key, varname);
3084 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003085 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003087 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003088 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 di->di_flags = DI_FLAGS_ALLOC;
3090 if (flags & LET_IS_CONST)
3091 di->di_flags |= DI_FLAGS_LOCK;
3092
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003093 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003095 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096
3097 // Store a pointer to the typval_T, so that it can be found by
3098 // index instead of using a hastab lookup.
3099 if (ga_grow(&si->sn_var_vals, 1) == OK)
3100 {
3101 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3102 + si->sn_var_vals.ga_len;
3103 sv->sv_name = di->di_key;
3104 sv->sv_tv = &di->di_tv;
Bram Moolenaar53b29e42020-08-15 14:31:20 +02003105 if (type == NULL)
3106 sv->sv_type = typval2type(tv, &si->sn_type_list);
3107 else
3108 sv->sv_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 sv->sv_const = (flags & LET_IS_CONST);
3110 sv->sv_export = is_export;
3111 ++si->sn_var_vals.ga_len;
3112
3113 // let ex_export() know the export worked.
3114 is_export = FALSE;
3115 }
3116 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003117 }
3118
3119 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003121 else
3122 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 di->di_tv = *tv;
3124 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003125 init_tv(tv);
3126 }
3127
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003128 // ":const var = val" locks the value; in Vim9 script only with ":const!"
3129 if ((flags & LET_IS_CONST) && (!vim9script || (flags & LET_FORCEIT)))
Bram Moolenaar021bda52020-08-17 21:07:22 +02003130 // Like :lockvar! name: lock the value and what it contains, but only
3131 // if the reference count is up to one. That locks only literal
3132 // values.
3133 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003134 return;
3135failed:
3136 if (!copy)
3137 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003138}
3139
3140/*
3141 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3142 * Also give an error message.
3143 */
3144 int
3145var_check_ro(int flags, char_u *name, int use_gettext)
3146{
3147 if (flags & DI_FLAGS_RO)
3148 {
3149 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3150 return TRUE;
3151 }
3152 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3153 {
3154 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3155 return TRUE;
3156 }
3157 return FALSE;
3158}
3159
3160/*
3161 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3162 * Also give an error message.
3163 */
3164 int
3165var_check_fixed(int flags, char_u *name, int use_gettext)
3166{
3167 if (flags & DI_FLAGS_FIX)
3168 {
3169 semsg(_("E795: Cannot delete variable %s"),
3170 use_gettext ? (char_u *)_(name) : name);
3171 return TRUE;
3172 }
3173 return FALSE;
3174}
3175
3176/*
3177 * Check if a funcref is assigned to a valid variable name.
3178 * Return TRUE and give an error if not.
3179 */
3180 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003181var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003182 char_u *name, // points to start of variable name
3183 int new_var) // TRUE when creating the variable
3184{
3185 // Allow for w: b: s: and t:.
3186 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3187 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3188 ? name[2] : name[0]))
3189 {
3190 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3191 name);
3192 return TRUE;
3193 }
3194 // Don't allow hiding a function. When "v" is not NULL we might be
3195 // assigning another function to the same var, the type is checked
3196 // below.
3197 if (new_var && function_exists(name, FALSE))
3198 {
3199 semsg(_("E705: Variable name conflicts with existing function: %s"),
3200 name);
3201 return TRUE;
3202 }
3203 return FALSE;
3204}
3205
3206/*
3207 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
3208 * Also give an error message, using "name" or _("name") when use_gettext is
3209 * TRUE.
3210 */
3211 int
3212var_check_lock(int lock, char_u *name, int use_gettext)
3213{
3214 if (lock & VAR_LOCKED)
3215 {
3216 semsg(_("E741: Value is locked: %s"),
3217 name == NULL ? (char_u *)_("Unknown")
3218 : use_gettext ? (char_u *)_(name)
3219 : name);
3220 return TRUE;
3221 }
3222 if (lock & VAR_FIXED)
3223 {
3224 semsg(_("E742: Cannot change value of %s"),
3225 name == NULL ? (char_u *)_("Unknown")
3226 : use_gettext ? (char_u *)_(name)
3227 : name);
3228 return TRUE;
3229 }
3230 return FALSE;
3231}
3232
3233/*
3234 * Check if a variable name is valid.
3235 * Return FALSE and give an error if not.
3236 */
3237 int
3238valid_varname(char_u *varname)
3239{
3240 char_u *p;
3241
3242 for (p = varname; *p != NUL; ++p)
3243 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3244 && *p != AUTOLOAD_CHAR)
3245 {
3246 semsg(_(e_illvar), varname);
3247 return FALSE;
3248 }
3249 return TRUE;
3250}
3251
3252/*
3253 * getwinvar() and gettabwinvar()
3254 */
3255 static void
3256getwinvar(
3257 typval_T *argvars,
3258 typval_T *rettv,
3259 int off) // 1 for gettabwinvar()
3260{
3261 win_T *win;
3262 char_u *varname;
3263 dictitem_T *v;
3264 tabpage_T *tp = NULL;
3265 int done = FALSE;
3266 win_T *oldcurwin;
3267 tabpage_T *oldtabpage;
3268 int need_switch_win;
3269
3270 if (off == 1)
3271 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3272 else
3273 tp = curtab;
3274 win = find_win_by_nr(&argvars[off], tp);
3275 varname = tv_get_string_chk(&argvars[off + 1]);
3276 ++emsg_off;
3277
3278 rettv->v_type = VAR_STRING;
3279 rettv->vval.v_string = NULL;
3280
3281 if (win != NULL && varname != NULL)
3282 {
3283 // Set curwin to be our win, temporarily. Also set the tabpage,
3284 // otherwise the window is not valid. Only do this when needed,
3285 // autocommands get blocked.
3286 need_switch_win = !(tp == curtab && win == curwin);
3287 if (!need_switch_win
3288 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3289 {
3290 if (*varname == '&')
3291 {
3292 if (varname[1] == NUL)
3293 {
3294 // get all window-local options in a dict
3295 dict_T *opts = get_winbuf_options(FALSE);
3296
3297 if (opts != NULL)
3298 {
3299 rettv_dict_set(rettv, opts);
3300 done = TRUE;
3301 }
3302 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003303 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003304 // window-local-option
3305 done = TRUE;
3306 }
3307 else
3308 {
3309 // Look up the variable.
3310 // Let getwinvar({nr}, "") return the "w:" dictionary.
3311 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3312 varname, FALSE);
3313 if (v != NULL)
3314 {
3315 copy_tv(&v->di_tv, rettv);
3316 done = TRUE;
3317 }
3318 }
3319 }
3320
3321 if (need_switch_win)
3322 // restore previous notion of curwin
3323 restore_win(oldcurwin, oldtabpage, TRUE);
3324 }
3325
3326 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3327 // use the default return value
3328 copy_tv(&argvars[off + 2], rettv);
3329
3330 --emsg_off;
3331}
3332
3333/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003334 * Set option "varname" to the value of "varp" for the current buffer/window.
3335 */
3336 static void
3337set_option_from_tv(char_u *varname, typval_T *varp)
3338{
3339 long numval = 0;
3340 char_u *strval;
3341 char_u nbuf[NUMBUFLEN];
3342 int error = FALSE;
3343
3344 if (!in_vim9script() || varp->v_type != VAR_STRING)
3345 numval = (long)tv_get_number_chk(varp, &error);
3346 strval = tv_get_string_buf_chk(varp, nbuf);
3347 if (!error && strval != NULL)
3348 set_option_value(varname, numval, strval, OPT_LOCAL);
3349}
3350
3351/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003352 * "setwinvar()" and "settabwinvar()" functions
3353 */
3354 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003355setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003356{
3357 win_T *win;
3358 win_T *save_curwin;
3359 tabpage_T *save_curtab;
3360 int need_switch_win;
3361 char_u *varname, *winvarname;
3362 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003363 tabpage_T *tp = NULL;
3364
3365 if (check_secure())
3366 return;
3367
3368 if (off == 1)
3369 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3370 else
3371 tp = curtab;
3372 win = find_win_by_nr(&argvars[off], tp);
3373 varname = tv_get_string_chk(&argvars[off + 1]);
3374 varp = &argvars[off + 2];
3375
3376 if (win != NULL && varname != NULL && varp != NULL)
3377 {
3378 need_switch_win = !(tp == curtab && win == curwin);
3379 if (!need_switch_win
3380 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3381 {
3382 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003383 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003384 else
3385 {
3386 winvarname = alloc(STRLEN(varname) + 3);
3387 if (winvarname != NULL)
3388 {
3389 STRCPY(winvarname, "w:");
3390 STRCPY(winvarname + 2, varname);
3391 set_var(winvarname, varp, TRUE);
3392 vim_free(winvarname);
3393 }
3394 }
3395 }
3396 if (need_switch_win)
3397 restore_win(save_curwin, save_curtab, TRUE);
3398 }
3399}
3400
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003401/*
3402 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3403 * v:option_type, and v:option_command.
3404 */
3405 void
3406reset_v_option_vars(void)
3407{
3408 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3409 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3410 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3411 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3412 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3413 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3414}
3415
3416/*
3417 * Add an assert error to v:errors.
3418 */
3419 void
3420assert_error(garray_T *gap)
3421{
3422 struct vimvar *vp = &vimvars[VV_ERRORS];
3423
3424 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003425 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003426 set_vim_var_list(VV_ERRORS, list_alloc());
3427 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3428}
3429
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003430 int
3431var_exists(char_u *var)
3432{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003433 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003434 char_u *name;
3435 char_u *tofree;
3436 typval_T tv;
3437 int len = 0;
3438 int n = FALSE;
3439
3440 // get_name_len() takes care of expanding curly braces
3441 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003442 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003443 if (len > 0)
3444 {
3445 if (tofree != NULL)
3446 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003447 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003448 if (n)
3449 {
3450 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003451 arg = skipwhite(arg);
3452 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003453 if (n)
3454 clear_tv(&tv);
3455 }
3456 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003457 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003458 n = FALSE;
3459
3460 vim_free(tofree);
3461 return n;
3462}
3463
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003464static lval_T *redir_lval = NULL;
3465#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3466static garray_T redir_ga; // only valid when redir_lval is not NULL
3467static char_u *redir_endp = NULL;
3468static char_u *redir_varname = NULL;
3469
3470/*
3471 * Start recording command output to a variable
3472 * When "append" is TRUE append to an existing variable.
3473 * Returns OK if successfully completed the setup. FAIL otherwise.
3474 */
3475 int
3476var_redir_start(char_u *name, int append)
3477{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003478 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003479 typval_T tv;
3480
3481 // Catch a bad name early.
3482 if (!eval_isnamec1(*name))
3483 {
3484 emsg(_(e_invarg));
3485 return FAIL;
3486 }
3487
3488 // Make a copy of the name, it is used in redir_lval until redir ends.
3489 redir_varname = vim_strsave(name);
3490 if (redir_varname == NULL)
3491 return FAIL;
3492
3493 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3494 if (redir_lval == NULL)
3495 {
3496 var_redir_stop();
3497 return FAIL;
3498 }
3499
3500 // The output is stored in growarray "redir_ga" until redirection ends.
3501 ga_init2(&redir_ga, (int)sizeof(char), 500);
3502
3503 // Parse the variable name (can be a dict or list entry).
3504 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3505 FNE_CHECK_START);
3506 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3507 {
3508 clear_lval(redir_lval);
3509 if (redir_endp != NULL && *redir_endp != NUL)
3510 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003511 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003512 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003513 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003514 redir_endp = NULL; // don't store a value, only cleanup
3515 var_redir_stop();
3516 return FAIL;
3517 }
3518
3519 // check if we can write to the variable: set it to or append an empty
3520 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003521 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003522 tv.v_type = VAR_STRING;
3523 tv.vval.v_string = (char_u *)"";
3524 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003526 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003528 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003529 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003530 {
3531 redir_endp = NULL; // don't store a value, only cleanup
3532 var_redir_stop();
3533 return FAIL;
3534 }
3535
3536 return OK;
3537}
3538
3539/*
3540 * Append "value[value_len]" to the variable set by var_redir_start().
3541 * The actual appending is postponed until redirection ends, because the value
3542 * appended may in fact be the string we write to, changing it may cause freed
3543 * memory to be used:
3544 * :redir => foo
3545 * :let foo
3546 * :redir END
3547 */
3548 void
3549var_redir_str(char_u *value, int value_len)
3550{
3551 int len;
3552
3553 if (redir_lval == NULL)
3554 return;
3555
3556 if (value_len == -1)
3557 len = (int)STRLEN(value); // Append the entire string
3558 else
3559 len = value_len; // Append only "value_len" characters
3560
3561 if (ga_grow(&redir_ga, len) == OK)
3562 {
3563 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3564 redir_ga.ga_len += len;
3565 }
3566 else
3567 var_redir_stop();
3568}
3569
3570/*
3571 * Stop redirecting command output to a variable.
3572 * Frees the allocated memory.
3573 */
3574 void
3575var_redir_stop(void)
3576{
3577 typval_T tv;
3578
3579 if (EVALCMD_BUSY)
3580 {
3581 redir_lval = NULL;
3582 return;
3583 }
3584
3585 if (redir_lval != NULL)
3586 {
3587 // If there was no error: assign the text to the variable.
3588 if (redir_endp != NULL)
3589 {
3590 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3591 tv.v_type = VAR_STRING;
3592 tv.vval.v_string = redir_ga.ga_data;
3593 // Call get_lval() again, if it's inside a Dict or List it may
3594 // have changed.
3595 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3596 FALSE, FALSE, 0, FNE_CHECK_START);
3597 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003599 (char_u *)".");
3600 clear_lval(redir_lval);
3601 }
3602
3603 // free the collected output
3604 VIM_CLEAR(redir_ga.ga_data);
3605
3606 VIM_CLEAR(redir_lval);
3607 }
3608 VIM_CLEAR(redir_varname);
3609}
3610
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003611/*
3612 * "gettabvar()" function
3613 */
3614 void
3615f_gettabvar(typval_T *argvars, typval_T *rettv)
3616{
3617 win_T *oldcurwin;
3618 tabpage_T *tp, *oldtabpage;
3619 dictitem_T *v;
3620 char_u *varname;
3621 int done = FALSE;
3622
3623 rettv->v_type = VAR_STRING;
3624 rettv->vval.v_string = NULL;
3625
3626 varname = tv_get_string_chk(&argvars[1]);
3627 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3628 if (tp != NULL && varname != NULL)
3629 {
3630 // Set tp to be our tabpage, temporarily. Also set the window to the
3631 // first window in the tabpage, otherwise the window is not valid.
3632 if (switch_win(&oldcurwin, &oldtabpage,
3633 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3634 : tp->tp_firstwin, tp, TRUE) == OK)
3635 {
3636 // look up the variable
3637 // Let gettabvar({nr}, "") return the "t:" dictionary.
3638 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3639 if (v != NULL)
3640 {
3641 copy_tv(&v->di_tv, rettv);
3642 done = TRUE;
3643 }
3644 }
3645
3646 // restore previous notion of curwin
3647 restore_win(oldcurwin, oldtabpage, TRUE);
3648 }
3649
3650 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3651 copy_tv(&argvars[2], rettv);
3652}
3653
3654/*
3655 * "gettabwinvar()" function
3656 */
3657 void
3658f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3659{
3660 getwinvar(argvars, rettv, 1);
3661}
3662
3663/*
3664 * "getwinvar()" function
3665 */
3666 void
3667f_getwinvar(typval_T *argvars, typval_T *rettv)
3668{
3669 getwinvar(argvars, rettv, 0);
3670}
3671
3672/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003673 * "getbufvar()" function
3674 */
3675 void
3676f_getbufvar(typval_T *argvars, typval_T *rettv)
3677{
3678 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003679 char_u *varname;
3680 dictitem_T *v;
3681 int done = FALSE;
3682
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003683 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003684 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003685
3686 rettv->v_type = VAR_STRING;
3687 rettv->vval.v_string = NULL;
3688
3689 if (buf != NULL && varname != NULL)
3690 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003691 if (*varname == '&')
3692 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003693 buf_T *save_curbuf = curbuf;
3694
3695 // set curbuf to be our buf, temporarily
3696 curbuf = buf;
3697
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003698 if (varname[1] == NUL)
3699 {
3700 // get all buffer-local options in a dict
3701 dict_T *opts = get_winbuf_options(TRUE);
3702
3703 if (opts != NULL)
3704 {
3705 rettv_dict_set(rettv, opts);
3706 done = TRUE;
3707 }
3708 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003709 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003710 // buffer-local-option
3711 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003712
3713 // restore previous notion of curbuf
3714 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003715 }
3716 else
3717 {
3718 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003719 if (*varname == NUL)
3720 // Let getbufvar({nr}, "") return the "b:" dictionary.
3721 v = &buf->b_bufvar;
3722 else
3723 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3724 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003725 if (v != NULL)
3726 {
3727 copy_tv(&v->di_tv, rettv);
3728 done = TRUE;
3729 }
3730 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003731 }
3732
3733 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3734 // use the default value
3735 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003736}
3737
3738/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003739 * "settabvar()" function
3740 */
3741 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003742f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003743{
3744 tabpage_T *save_curtab;
3745 tabpage_T *tp;
3746 char_u *varname, *tabvarname;
3747 typval_T *varp;
3748
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003749 if (check_secure())
3750 return;
3751
3752 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3753 varname = tv_get_string_chk(&argvars[1]);
3754 varp = &argvars[2];
3755
3756 if (varname != NULL && varp != NULL && tp != NULL)
3757 {
3758 save_curtab = curtab;
3759 goto_tabpage_tp(tp, FALSE, FALSE);
3760
3761 tabvarname = alloc(STRLEN(varname) + 3);
3762 if (tabvarname != NULL)
3763 {
3764 STRCPY(tabvarname, "t:");
3765 STRCPY(tabvarname + 2, varname);
3766 set_var(tabvarname, varp, TRUE);
3767 vim_free(tabvarname);
3768 }
3769
3770 // Restore current tabpage
3771 if (valid_tabpage(save_curtab))
3772 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3773 }
3774}
3775
3776/*
3777 * "settabwinvar()" function
3778 */
3779 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003780f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003781{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003782 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003783}
3784
3785/*
3786 * "setwinvar()" function
3787 */
3788 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003789f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003790{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003791 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003792}
3793
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003794/*
3795 * "setbufvar()" function
3796 */
3797 void
3798f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3799{
3800 buf_T *buf;
3801 char_u *varname, *bufvarname;
3802 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003803
3804 if (check_secure())
3805 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003806 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003807 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003808 varp = &argvars[2];
3809
3810 if (buf != NULL && varname != NULL && varp != NULL)
3811 {
3812 if (*varname == '&')
3813 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003814 aco_save_T aco;
3815
3816 // set curbuf to be our buf, temporarily
3817 aucmd_prepbuf(&aco, buf);
3818
Bram Moolenaar191929b2020-08-19 21:20:49 +02003819 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003820
3821 // reset notion of buffer
3822 aucmd_restbuf(&aco);
3823 }
3824 else
3825 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003826 bufvarname = alloc(STRLEN(varname) + 3);
3827 if (bufvarname != NULL)
3828 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003829 buf_T *save_curbuf = curbuf;
3830
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003831 curbuf = buf;
3832 STRCPY(bufvarname, "b:");
3833 STRCPY(bufvarname + 2, varname);
3834 set_var(bufvarname, varp, TRUE);
3835 vim_free(bufvarname);
3836 curbuf = save_curbuf;
3837 }
3838 }
3839 }
3840}
3841
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003842/*
3843 * Get a callback from "arg". It can be a Funcref or a function name.
3844 * When "arg" is zero return an empty string.
3845 * "cb_name" is not allocated.
3846 * "cb_name" is set to NULL for an invalid argument.
3847 */
3848 callback_T
3849get_callback(typval_T *arg)
3850{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003851 callback_T res;
3852 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003853
3854 res.cb_free_name = FALSE;
3855 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3856 {
3857 res.cb_partial = arg->vval.v_partial;
3858 ++res.cb_partial->pt_refcount;
3859 res.cb_name = partial_name(res.cb_partial);
3860 }
3861 else
3862 {
3863 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003864 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3865 && isdigit(*arg->vval.v_string))
3866 r = FAIL;
3867 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003868 {
3869 // Note that we don't make a copy of the string.
3870 res.cb_name = arg->vval.v_string;
3871 func_ref(res.cb_name);
3872 }
3873 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003874 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003875 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003876 r = FAIL;
3877
3878 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003879 {
3880 emsg(_("E921: Invalid callback argument"));
3881 res.cb_name = NULL;
3882 }
3883 }
3884 return res;
3885}
3886
3887/*
3888 * Copy a callback into a typval_T.
3889 */
3890 void
3891put_callback(callback_T *cb, typval_T *tv)
3892{
3893 if (cb->cb_partial != NULL)
3894 {
3895 tv->v_type = VAR_PARTIAL;
3896 tv->vval.v_partial = cb->cb_partial;
3897 ++tv->vval.v_partial->pt_refcount;
3898 }
3899 else
3900 {
3901 tv->v_type = VAR_FUNC;
3902 tv->vval.v_string = vim_strsave(cb->cb_name);
3903 func_ref(cb->cb_name);
3904 }
3905}
3906
3907/*
3908 * Make a copy of "src" into "dest", allocating the function name if needed,
3909 * without incrementing the refcount.
3910 */
3911 void
3912set_callback(callback_T *dest, callback_T *src)
3913{
3914 if (src->cb_partial == NULL)
3915 {
3916 // just a function name, make a copy
3917 dest->cb_name = vim_strsave(src->cb_name);
3918 dest->cb_free_name = TRUE;
3919 }
3920 else
3921 {
3922 // cb_name is a pointer into cb_partial
3923 dest->cb_name = src->cb_name;
3924 dest->cb_free_name = FALSE;
3925 }
3926 dest->cb_partial = src->cb_partial;
3927}
3928
3929/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02003930 * Copy callback from "src" to "dest", incrementing the refcounts.
3931 */
3932 void
3933copy_callback(callback_T *dest, callback_T *src)
3934{
3935 dest->cb_partial = src->cb_partial;
3936 if (dest->cb_partial != NULL)
3937 {
3938 dest->cb_name = src->cb_name;
3939 dest->cb_free_name = FALSE;
3940 ++dest->cb_partial->pt_refcount;
3941 }
3942 else
3943 {
3944 dest->cb_name = vim_strsave(src->cb_name);
3945 dest->cb_free_name = TRUE;
3946 func_ref(src->cb_name);
3947 }
3948}
3949
3950/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003951 * Unref/free "callback" returned by get_callback() or set_callback().
3952 */
3953 void
3954free_callback(callback_T *callback)
3955{
3956 if (callback->cb_partial != NULL)
3957 {
3958 partial_unref(callback->cb_partial);
3959 callback->cb_partial = NULL;
3960 }
3961 else if (callback->cb_name != NULL)
3962 func_unref(callback->cb_name);
3963 if (callback->cb_free_name)
3964 {
3965 vim_free(callback->cb_name);
3966 callback->cb_free_name = FALSE;
3967 }
3968 callback->cb_name = NULL;
3969}
3970
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003971#endif // FEAT_EVAL