blob: 5ceaca48a04b4fc4146c21b399df8028bdfa7063 [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
Bram Moolenaara187c432020-09-16 21:08:28 +02001563 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001564 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001565 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001566 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;
Bram Moolenaara187c432020-09-16 21:08:28 +02001576 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001577 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)
Bram Moolenaara187c432020-09-16 21:08:28 +02001649 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001650 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
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001680 if (lp->ll_tv == NULL)
1681 {
1682 cc = *name_end;
1683 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001684 if (*lp->ll_name == '$')
1685 {
1686 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001687 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001688 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001689 else
1690 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001691 // Normal name or expanded name.
1692 di = find_var(lp->ll_name, NULL, TRUE);
1693 if (di == NULL)
1694 ret = FAIL;
1695 else if ((di->di_flags & DI_FLAGS_FIX)
1696 && di->di_tv.v_type != VAR_DICT
1697 && di->di_tv.v_type != VAR_LIST)
1698 {
1699 // For historic reasons this error is not given for a list or
1700 // dict. E.g., the b: dict could be locked/unlocked.
1701 semsg(_(e_lock_unlock), lp->ll_name);
1702 ret = FAIL;
1703 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001704 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001705 {
1706 if (lock)
1707 di->di_flags |= DI_FLAGS_LOCK;
1708 else
1709 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001710 if (deep != 0)
1711 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001712 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001713 }
1714 *name_end = cc;
1715 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001716 else if (deep == 0)
1717 {
1718 // nothing to do
1719 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001720 else if (lp->ll_range)
1721 {
1722 listitem_T *li = lp->ll_li;
1723
1724 // (un)lock a range of List items.
1725 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1726 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001727 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001728 li = li->li_next;
1729 ++lp->ll_n1;
1730 }
1731 }
1732 else if (lp->ll_list != NULL)
1733 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001734 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001735 else
1736 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001737 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001738
1739 return ret;
1740}
1741
1742/*
1743 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001744 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1745 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001746 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001747 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001748item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001749{
1750 static int recurse = 0;
1751 list_T *l;
1752 listitem_T *li;
1753 dict_T *d;
1754 blob_T *b;
1755 hashitem_T *hi;
1756 int todo;
1757
1758 if (recurse >= DICT_MAXNEST)
1759 {
1760 emsg(_("E743: variable nested too deep for (un)lock"));
1761 return;
1762 }
1763 if (deep == 0)
1764 return;
1765 ++recurse;
1766
1767 // lock/unlock the item itself
1768 if (lock)
1769 tv->v_lock |= VAR_LOCKED;
1770 else
1771 tv->v_lock &= ~VAR_LOCKED;
1772
1773 switch (tv->v_type)
1774 {
1775 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001776 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001777 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001778 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001780 case VAR_STRING:
1781 case VAR_FUNC:
1782 case VAR_PARTIAL:
1783 case VAR_FLOAT:
1784 case VAR_SPECIAL:
1785 case VAR_JOB:
1786 case VAR_CHANNEL:
1787 break;
1788
1789 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001790 if ((b = tv->vval.v_blob) != NULL
1791 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001792 {
1793 if (lock)
1794 b->bv_lock |= VAR_LOCKED;
1795 else
1796 b->bv_lock &= ~VAR_LOCKED;
1797 }
1798 break;
1799 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001800 if ((l = tv->vval.v_list) != NULL
1801 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001802 {
1803 if (lock)
1804 l->lv_lock |= VAR_LOCKED;
1805 else
1806 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001807 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001808 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001809 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001810 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001811 }
1812 break;
1813 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001814 if ((d = tv->vval.v_dict) != NULL
1815 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001816 {
1817 if (lock)
1818 d->dv_lock |= VAR_LOCKED;
1819 else
1820 d->dv_lock &= ~VAR_LOCKED;
1821 if (deep < 0 || deep > 1)
1822 {
1823 // recursive: lock/unlock the items the List contains
1824 todo = (int)d->dv_hashtab.ht_used;
1825 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1826 {
1827 if (!HASHITEM_EMPTY(hi))
1828 {
1829 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001830 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1831 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001832 }
1833 }
1834 }
1835 }
1836 }
1837 --recurse;
1838}
1839
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001840#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1841/*
1842 * Delete all "menutrans_" variables.
1843 */
1844 void
1845del_menutrans_vars(void)
1846{
1847 hashitem_T *hi;
1848 int todo;
1849
1850 hash_lock(&globvarht);
1851 todo = (int)globvarht.ht_used;
1852 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1853 {
1854 if (!HASHITEM_EMPTY(hi))
1855 {
1856 --todo;
1857 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1858 delete_var(&globvarht, hi);
1859 }
1860 }
1861 hash_unlock(&globvarht);
1862}
1863#endif
1864
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001865/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001866 * Local string buffer for the next two functions to store a variable name
1867 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1868 * get_user_var_name().
1869 */
1870
1871static char_u *varnamebuf = NULL;
1872static int varnamebuflen = 0;
1873
1874/*
1875 * Function to concatenate a prefix and a variable name.
1876 */
1877 static char_u *
1878cat_prefix_varname(int prefix, char_u *name)
1879{
1880 int len;
1881
1882 len = (int)STRLEN(name) + 3;
1883 if (len > varnamebuflen)
1884 {
1885 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001886 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001887 varnamebuf = alloc(len);
1888 if (varnamebuf == NULL)
1889 {
1890 varnamebuflen = 0;
1891 return NULL;
1892 }
1893 varnamebuflen = len;
1894 }
1895 *varnamebuf = prefix;
1896 varnamebuf[1] = ':';
1897 STRCPY(varnamebuf + 2, name);
1898 return varnamebuf;
1899}
1900
1901/*
1902 * Function given to ExpandGeneric() to obtain the list of user defined
1903 * (global/buffer/window/built-in) variable names.
1904 */
1905 char_u *
1906get_user_var_name(expand_T *xp, int idx)
1907{
1908 static long_u gdone;
1909 static long_u bdone;
1910 static long_u wdone;
1911 static long_u tdone;
1912 static int vidx;
1913 static hashitem_T *hi;
1914 hashtab_T *ht;
1915
1916 if (idx == 0)
1917 {
1918 gdone = bdone = wdone = vidx = 0;
1919 tdone = 0;
1920 }
1921
1922 // Global variables
1923 if (gdone < globvarht.ht_used)
1924 {
1925 if (gdone++ == 0)
1926 hi = globvarht.ht_array;
1927 else
1928 ++hi;
1929 while (HASHITEM_EMPTY(hi))
1930 ++hi;
1931 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1932 return cat_prefix_varname('g', hi->hi_key);
1933 return hi->hi_key;
1934 }
1935
1936 // b: variables
1937 ht = &curbuf->b_vars->dv_hashtab;
1938 if (bdone < ht->ht_used)
1939 {
1940 if (bdone++ == 0)
1941 hi = ht->ht_array;
1942 else
1943 ++hi;
1944 while (HASHITEM_EMPTY(hi))
1945 ++hi;
1946 return cat_prefix_varname('b', hi->hi_key);
1947 }
1948
1949 // w: variables
1950 ht = &curwin->w_vars->dv_hashtab;
1951 if (wdone < ht->ht_used)
1952 {
1953 if (wdone++ == 0)
1954 hi = ht->ht_array;
1955 else
1956 ++hi;
1957 while (HASHITEM_EMPTY(hi))
1958 ++hi;
1959 return cat_prefix_varname('w', hi->hi_key);
1960 }
1961
1962 // t: variables
1963 ht = &curtab->tp_vars->dv_hashtab;
1964 if (tdone < ht->ht_used)
1965 {
1966 if (tdone++ == 0)
1967 hi = ht->ht_array;
1968 else
1969 ++hi;
1970 while (HASHITEM_EMPTY(hi))
1971 ++hi;
1972 return cat_prefix_varname('t', hi->hi_key);
1973 }
1974
1975 // v: variables
1976 if (vidx < VV_LEN)
1977 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
1978
1979 VIM_CLEAR(varnamebuf);
1980 varnamebuflen = 0;
1981 return NULL;
1982}
1983
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001984 char *
1985get_var_special_name(int nr)
1986{
1987 switch (nr)
1988 {
1989 case VVAL_FALSE: return "v:false";
1990 case VVAL_TRUE: return "v:true";
1991 case VVAL_NONE: return "v:none";
1992 case VVAL_NULL: return "v:null";
1993 }
1994 internal_error("get_var_special_name()");
1995 return "42";
1996}
1997
1998/*
1999 * Returns the global variable dictionary
2000 */
2001 dict_T *
2002get_globvar_dict(void)
2003{
2004 return &globvardict;
2005}
2006
2007/*
2008 * Returns the global variable hash table
2009 */
2010 hashtab_T *
2011get_globvar_ht(void)
2012{
2013 return &globvarht;
2014}
2015
2016/*
2017 * Returns the v: variable dictionary
2018 */
2019 dict_T *
2020get_vimvar_dict(void)
2021{
2022 return &vimvardict;
2023}
2024
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002025/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002026 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002027 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002028 */
2029 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002030find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002032 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2033 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002034
2035 if (di == NULL)
2036 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002037 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2039 return (int)(vv - vimvars);
2040}
2041
2042
2043/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002044 * Set type of v: variable to "type".
2045 */
2046 void
2047set_vim_var_type(int idx, vartype_T type)
2048{
2049 vimvars[idx].vv_type = type;
2050}
2051
2052/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002053 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002054 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002055 */
2056 void
2057set_vim_var_nr(int idx, varnumber_T val)
2058{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002059 vimvars[idx].vv_nr = val;
2060}
2061
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002062 char *
2063get_vim_var_name(int idx)
2064{
2065 return vimvars[idx].vv_name;
2066}
2067
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002068/*
2069 * Get typval_T v: variable value.
2070 */
2071 typval_T *
2072get_vim_var_tv(int idx)
2073{
2074 return &vimvars[idx].vv_tv;
2075}
2076
2077/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002078 * Set v: variable to "tv". Only accepts the same type.
2079 * Takes over the value of "tv".
2080 */
2081 int
2082set_vim_var_tv(int idx, typval_T *tv)
2083{
2084 if (vimvars[idx].vv_type != tv->v_type)
2085 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002086 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002087 clear_tv(tv);
2088 return FAIL;
2089 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002090 // VV_RO is also checked when compiling, but let's check here as well.
2091 if (vimvars[idx].vv_flags & VV_RO)
2092 {
2093 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2094 return FAIL;
2095 }
2096 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2097 {
2098 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2099 return FAIL;
2100 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002101 clear_tv(&vimvars[idx].vv_di.di_tv);
2102 vimvars[idx].vv_di.di_tv = *tv;
2103 return OK;
2104}
2105
2106/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002107 * Get number v: variable value.
2108 */
2109 varnumber_T
2110get_vim_var_nr(int idx)
2111{
2112 return vimvars[idx].vv_nr;
2113}
2114
2115/*
2116 * Get string v: variable value. Uses a static buffer, can only be used once.
2117 * If the String variable has never been set, return an empty string.
2118 * Never returns NULL;
2119 */
2120 char_u *
2121get_vim_var_str(int idx)
2122{
2123 return tv_get_string(&vimvars[idx].vv_tv);
2124}
2125
2126/*
2127 * Get List v: variable value. Caller must take care of reference count when
2128 * needed.
2129 */
2130 list_T *
2131get_vim_var_list(int idx)
2132{
2133 return vimvars[idx].vv_list;
2134}
2135
2136/*
2137 * Get Dict v: variable value. Caller must take care of reference count when
2138 * needed.
2139 */
2140 dict_T *
2141get_vim_var_dict(int idx)
2142{
2143 return vimvars[idx].vv_dict;
2144}
2145
2146/*
2147 * Set v:char to character "c".
2148 */
2149 void
2150set_vim_var_char(int c)
2151{
2152 char_u buf[MB_MAXBYTES + 1];
2153
2154 if (has_mbyte)
2155 buf[(*mb_char2bytes)(c, buf)] = NUL;
2156 else
2157 {
2158 buf[0] = c;
2159 buf[1] = NUL;
2160 }
2161 set_vim_var_string(VV_CHAR, buf, -1);
2162}
2163
2164/*
2165 * Set v:count to "count" and v:count1 to "count1".
2166 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2167 */
2168 void
2169set_vcount(
2170 long count,
2171 long count1,
2172 int set_prevcount)
2173{
2174 if (set_prevcount)
2175 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2176 vimvars[VV_COUNT].vv_nr = count;
2177 vimvars[VV_COUNT1].vv_nr = count1;
2178}
2179
2180/*
2181 * Save variables that might be changed as a side effect. Used when executing
2182 * a timer callback.
2183 */
2184 void
2185save_vimvars(vimvars_save_T *vvsave)
2186{
2187 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2188 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2189 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2190}
2191
2192/*
2193 * Restore variables saved by save_vimvars().
2194 */
2195 void
2196restore_vimvars(vimvars_save_T *vvsave)
2197{
2198 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2199 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2200 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2201}
2202
2203/*
2204 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2205 * value.
2206 */
2207 void
2208set_vim_var_string(
2209 int idx,
2210 char_u *val,
2211 int len) // length of "val" to use or -1 (whole string)
2212{
2213 clear_tv(&vimvars[idx].vv_di.di_tv);
2214 vimvars[idx].vv_type = VAR_STRING;
2215 if (val == NULL)
2216 vimvars[idx].vv_str = NULL;
2217 else if (len == -1)
2218 vimvars[idx].vv_str = vim_strsave(val);
2219 else
2220 vimvars[idx].vv_str = vim_strnsave(val, len);
2221}
2222
2223/*
2224 * Set List v: variable to "val".
2225 */
2226 void
2227set_vim_var_list(int idx, list_T *val)
2228{
2229 clear_tv(&vimvars[idx].vv_di.di_tv);
2230 vimvars[idx].vv_type = VAR_LIST;
2231 vimvars[idx].vv_list = val;
2232 if (val != NULL)
2233 ++val->lv_refcount;
2234}
2235
2236/*
2237 * Set Dictionary v: variable to "val".
2238 */
2239 void
2240set_vim_var_dict(int idx, dict_T *val)
2241{
2242 clear_tv(&vimvars[idx].vv_di.di_tv);
2243 vimvars[idx].vv_type = VAR_DICT;
2244 vimvars[idx].vv_dict = val;
2245 if (val != NULL)
2246 {
2247 ++val->dv_refcount;
2248 dict_set_items_ro(val);
2249 }
2250}
2251
2252/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002253 * Set the v:argv list.
2254 */
2255 void
2256set_argv_var(char **argv, int argc)
2257{
2258 list_T *l = list_alloc();
2259 int i;
2260
2261 if (l == NULL)
2262 getout(1);
2263 l->lv_lock = VAR_FIXED;
2264 for (i = 0; i < argc; ++i)
2265 {
2266 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2267 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002268 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002269 }
2270 set_vim_var_list(VV_ARGV, l);
2271}
2272
2273/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002274 * Reset v:register, taking the 'clipboard' setting into account.
2275 */
2276 void
2277reset_reg_var(void)
2278{
2279 int regname = 0;
2280
2281 // Adjust the register according to 'clipboard', so that when
2282 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2283#ifdef FEAT_CLIPBOARD
2284 adjust_clip_reg(&regname);
2285#endif
2286 set_reg_var(regname);
2287}
2288
2289/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002290 * Set v:register if needed.
2291 */
2292 void
2293set_reg_var(int c)
2294{
2295 char_u regname;
2296
2297 if (c == 0 || c == ' ')
2298 regname = '"';
2299 else
2300 regname = c;
2301 // Avoid free/alloc when the value is already right.
2302 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2303 set_vim_var_string(VV_REG, &regname, 1);
2304}
2305
2306/*
2307 * Get or set v:exception. If "oldval" == NULL, return the current value.
2308 * Otherwise, restore the value to "oldval" and return NULL.
2309 * Must always be called in pairs to save and restore v:exception! Does not
2310 * take care of memory allocations.
2311 */
2312 char_u *
2313v_exception(char_u *oldval)
2314{
2315 if (oldval == NULL)
2316 return vimvars[VV_EXCEPTION].vv_str;
2317
2318 vimvars[VV_EXCEPTION].vv_str = oldval;
2319 return NULL;
2320}
2321
2322/*
2323 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2324 * Otherwise, restore the value to "oldval" and return NULL.
2325 * Must always be called in pairs to save and restore v:throwpoint! Does not
2326 * take care of memory allocations.
2327 */
2328 char_u *
2329v_throwpoint(char_u *oldval)
2330{
2331 if (oldval == NULL)
2332 return vimvars[VV_THROWPOINT].vv_str;
2333
2334 vimvars[VV_THROWPOINT].vv_str = oldval;
2335 return NULL;
2336}
2337
2338/*
2339 * Set v:cmdarg.
2340 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2341 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2342 * Must always be called in pairs!
2343 */
2344 char_u *
2345set_cmdarg(exarg_T *eap, char_u *oldarg)
2346{
2347 char_u *oldval;
2348 char_u *newval;
2349 unsigned len;
2350
2351 oldval = vimvars[VV_CMDARG].vv_str;
2352 if (eap == NULL)
2353 {
2354 vim_free(oldval);
2355 vimvars[VV_CMDARG].vv_str = oldarg;
2356 return NULL;
2357 }
2358
2359 if (eap->force_bin == FORCE_BIN)
2360 len = 6;
2361 else if (eap->force_bin == FORCE_NOBIN)
2362 len = 8;
2363 else
2364 len = 0;
2365
2366 if (eap->read_edit)
2367 len += 7;
2368
2369 if (eap->force_ff != 0)
2370 len += 10; // " ++ff=unix"
2371 if (eap->force_enc != 0)
2372 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2373 if (eap->bad_char != 0)
2374 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2375
2376 newval = alloc(len + 1);
2377 if (newval == NULL)
2378 return NULL;
2379
2380 if (eap->force_bin == FORCE_BIN)
2381 sprintf((char *)newval, " ++bin");
2382 else if (eap->force_bin == FORCE_NOBIN)
2383 sprintf((char *)newval, " ++nobin");
2384 else
2385 *newval = NUL;
2386
2387 if (eap->read_edit)
2388 STRCAT(newval, " ++edit");
2389
2390 if (eap->force_ff != 0)
2391 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2392 eap->force_ff == 'u' ? "unix"
2393 : eap->force_ff == 'd' ? "dos"
2394 : "mac");
2395 if (eap->force_enc != 0)
2396 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2397 eap->cmd + eap->force_enc);
2398 if (eap->bad_char == BAD_KEEP)
2399 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2400 else if (eap->bad_char == BAD_DROP)
2401 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2402 else if (eap->bad_char != 0)
2403 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2404 vimvars[VV_CMDARG].vv_str = newval;
2405 return oldval;
2406}
2407
2408/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002409 * Get the value of internal variable "name".
2410 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2411 */
2412 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002413eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002414 char_u *name,
2415 int len, // length of "name"
2416 typval_T *rettv, // NULL when only checking existence
2417 dictitem_T **dip, // non-NULL when typval's dict item is needed
2418 int verbose, // may give error message
2419 int no_autoload) // do not use script autoloading
2420{
2421 int ret = OK;
2422 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002423 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002424 dictitem_T *v;
2425 int cc;
2426
2427 // truncate the name, so that we can use strcmp()
2428 cc = name[len];
2429 name[len] = NUL;
2430
2431 // Check for user-defined variables.
2432 v = find_var(name, NULL, no_autoload);
2433 if (v != NULL)
2434 {
2435 tv = &v->di_tv;
2436 if (dip != NULL)
2437 *dip = v;
2438 }
2439
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002440 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002441 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002442 imported_T *import;
2443 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2444
2445 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446
2447 // imported variable from another script
2448 if (import != NULL)
2449 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002450 if (import->imp_funcname != NULL)
2451 {
2452 foundFunc = TRUE;
2453 if (rettv != NULL)
2454 {
2455 rettv->v_type = VAR_FUNC;
2456 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2457 }
2458 }
2459 else
2460 {
2461 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002462 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002464 tv = sv->sv_tv;
2465 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002467 else if (in_vim9script())
2468 {
2469 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2470
2471 if (ufunc != NULL)
2472 {
2473 foundFunc = TRUE;
2474 if (rettv != NULL)
2475 {
2476 rettv->v_type = VAR_FUNC;
2477 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2478 }
2479 }
2480 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481 }
2482
Bram Moolenaarc620c052020-07-08 15:16:19 +02002483 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002484 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002485 if (tv == NULL)
2486 {
2487 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002488 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002489 ret = FAIL;
2490 }
2491 else if (rettv != NULL)
2492 copy_tv(tv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002493 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002494
2495 name[len] = cc;
2496
2497 return ret;
2498}
2499
2500/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002501 * Check if variable "name[len]" is a local variable or an argument.
2502 * If so, "*eval_lavars_used" is set to TRUE.
2503 */
2504 void
2505check_vars(char_u *name, int len)
2506{
2507 int cc;
2508 char_u *varname;
2509 hashtab_T *ht;
2510
2511 if (eval_lavars_used == NULL)
2512 return;
2513
2514 // truncate the name, so that we can use strcmp()
2515 cc = name[len];
2516 name[len] = NUL;
2517
2518 ht = find_var_ht(name, &varname);
2519 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2520 {
2521 if (find_var(name, NULL, TRUE) != NULL)
2522 *eval_lavars_used = TRUE;
2523 }
2524
2525 name[len] = cc;
2526}
2527
2528/*
2529 * Find variable "name" in the list of variables.
2530 * Return a pointer to it if found, NULL if not found.
2531 * Careful: "a:0" variables don't have a name.
2532 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2533 * hashtab_T used.
2534 */
2535 dictitem_T *
2536find_var(char_u *name, hashtab_T **htp, int no_autoload)
2537{
2538 char_u *varname;
2539 hashtab_T *ht;
2540 dictitem_T *ret = NULL;
2541
2542 ht = find_var_ht(name, &varname);
2543 if (htp != NULL)
2544 *htp = ht;
2545 if (ht == NULL)
2546 return NULL;
2547 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2548 if (ret != NULL)
2549 return ret;
2550
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002551 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002552 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2553}
2554
2555/*
2556 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002557 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002558 * Returns NULL if not found.
2559 */
2560 dictitem_T *
2561find_var_in_ht(
2562 hashtab_T *ht,
2563 int htname,
2564 char_u *varname,
2565 int no_autoload)
2566{
2567 hashitem_T *hi;
2568
2569 if (*varname == NUL)
2570 {
2571 // Must be something like "s:", otherwise "ht" would be NULL.
2572 switch (htname)
2573 {
2574 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2575 case 'g': return &globvars_var;
2576 case 'v': return &vimvars_var;
2577 case 'b': return &curbuf->b_bufvar;
2578 case 'w': return &curwin->w_winvar;
2579 case 't': return &curtab->tp_winvar;
2580 case 'l': return get_funccal_local_var();
2581 case 'a': return get_funccal_args_var();
2582 }
2583 return NULL;
2584 }
2585
2586 hi = hash_find(ht, varname);
2587 if (HASHITEM_EMPTY(hi))
2588 {
2589 // For global variables we may try auto-loading the script. If it
2590 // worked find the variable again. Don't auto-load a script if it was
2591 // loaded already, otherwise it would be loaded every time when
2592 // checking if a function name is a Funcref variable.
2593 if (ht == &globvarht && !no_autoload)
2594 {
2595 // Note: script_autoload() may make "hi" invalid. It must either
2596 // be obtained again or not used.
2597 if (!script_autoload(varname, FALSE) || aborting())
2598 return NULL;
2599 hi = hash_find(ht, varname);
2600 }
2601 if (HASHITEM_EMPTY(hi))
2602 return NULL;
2603 }
2604 return HI2DI(hi);
2605}
2606
2607/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608 * Get the script-local hashtab. NULL if not in a script context.
2609 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02002610 static hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611get_script_local_ht(void)
2612{
2613 scid_T sid = current_sctx.sc_sid;
2614
Bram Moolenaare3d46852020-08-29 13:39:17 +02002615 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616 return &SCRIPT_VARS(sid);
2617 return NULL;
2618}
2619
2620/*
2621 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002622 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002623 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002624 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2626{
2627 hashtab_T *ht = get_script_local_ht();
2628 char_u buffer[30];
2629 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002630 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002631 hashitem_T *hi;
2632
2633 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002634 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635 if (len < sizeof(buffer) - 1)
2636 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002637 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638 vim_strncpy(buffer, name, len);
2639 p = buffer;
2640 }
2641 else
2642 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002643 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002645 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646 }
2647
2648 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002649 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002650
2651 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002652 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2653 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654
2655 if (p != buffer)
2656 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002657 // Don't return "buffer", gcc complains.
2658 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659}
2660
2661/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002662 * Find the hashtab used for a variable name.
2663 * Return NULL if the name is not valid.
2664 * Set "varname" to the start of name without ':'.
2665 */
2666 hashtab_T *
2667find_var_ht(char_u *name, char_u **varname)
2668{
2669 hashitem_T *hi;
2670 hashtab_T *ht;
2671
2672 if (name[0] == NUL)
2673 return NULL;
2674 if (name[1] != ':')
2675 {
2676 // The name must not start with a colon or #.
2677 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2678 return NULL;
2679 *varname = name;
2680
2681 // "version" is "v:version" in all scopes if scriptversion < 3.
2682 // Same for a few other variables marked with VV_COMPAT.
2683 if (current_sctx.sc_version < 3)
2684 {
2685 hi = hash_find(&compat_hashtab, name);
2686 if (!HASHITEM_EMPTY(hi))
2687 return &compat_hashtab;
2688 }
2689
2690 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691 if (ht != NULL)
2692 return ht; // local variable
2693
2694 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002695 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002696 {
2697 ht = get_script_local_ht();
2698 if (ht != NULL)
2699 return ht;
2700 }
2701
2702 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002703 }
2704 *varname = name + 2;
2705 if (*name == 'g') // global variable
2706 return &globvarht;
2707 // There must be no ':' or '#' in the rest of the name, unless g: is used
2708 if (vim_strchr(name + 2, ':') != NULL
2709 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2710 return NULL;
2711 if (*name == 'b') // buffer variable
2712 return &curbuf->b_vars->dv_hashtab;
2713 if (*name == 'w') // window variable
2714 return &curwin->w_vars->dv_hashtab;
2715 if (*name == 't') // tab page variable
2716 return &curtab->tp_vars->dv_hashtab;
2717 if (*name == 'v') // v: variable
2718 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002719 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002720 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002721 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002722 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002723 if (*name == 'a') // a: function argument
2724 return get_funccal_args_ht();
2725 if (*name == 'l') // l: local function variable
2726 return get_funccal_local_ht();
2727 }
2728 if (*name == 's') // script variable
2729 {
2730 ht = get_script_local_ht();
2731 if (ht != NULL)
2732 return ht;
2733 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002734 return NULL;
2735}
2736
2737/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002738 * Get the string value of a (global/local) variable.
2739 * Note: see tv_get_string() for how long the pointer remains valid.
2740 * Returns NULL when it doesn't exist.
2741 */
2742 char_u *
2743get_var_value(char_u *name)
2744{
2745 dictitem_T *v;
2746
2747 v = find_var(name, NULL, FALSE);
2748 if (v == NULL)
2749 return NULL;
2750 return tv_get_string(&v->di_tv);
2751}
2752
2753/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002754 * Allocate a new hashtab for a sourced script. It will be used while
2755 * sourcing this script and when executing functions defined in the script.
2756 */
2757 void
2758new_script_vars(scid_T id)
2759{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002760 scriptvar_T *sv;
2761
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002762 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2763 if (sv == NULL)
2764 return;
2765 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002766 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002767}
2768
2769/*
2770 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2771 * point to it.
2772 */
2773 void
2774init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2775{
2776 hash_init(&dict->dv_hashtab);
2777 dict->dv_lock = 0;
2778 dict->dv_scope = scope;
2779 dict->dv_refcount = DO_NOT_FREE_CNT;
2780 dict->dv_copyID = 0;
2781 dict_var->di_tv.vval.v_dict = dict;
2782 dict_var->di_tv.v_type = VAR_DICT;
2783 dict_var->di_tv.v_lock = VAR_FIXED;
2784 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2785 dict_var->di_key[0] = NUL;
2786}
2787
2788/*
2789 * Unreference a dictionary initialized by init_var_dict().
2790 */
2791 void
2792unref_var_dict(dict_T *dict)
2793{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002794 // Now the dict needs to be freed if no one else is using it, go back to
2795 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002796 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2797 dict_unref(dict);
2798}
2799
2800/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002801 * Clean up a list of internal variables.
2802 * Frees all allocated variables and the value they contain.
2803 * Clears hashtab "ht", does not free it.
2804 */
2805 void
2806vars_clear(hashtab_T *ht)
2807{
2808 vars_clear_ext(ht, TRUE);
2809}
2810
2811/*
2812 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2813 */
2814 void
2815vars_clear_ext(hashtab_T *ht, int free_val)
2816{
2817 int todo;
2818 hashitem_T *hi;
2819 dictitem_T *v;
2820
2821 hash_lock(ht);
2822 todo = (int)ht->ht_used;
2823 for (hi = ht->ht_array; todo > 0; ++hi)
2824 {
2825 if (!HASHITEM_EMPTY(hi))
2826 {
2827 --todo;
2828
2829 // Free the variable. Don't remove it from the hashtab,
2830 // ht_array might change then. hash_clear() takes care of it
2831 // later.
2832 v = HI2DI(hi);
2833 if (free_val)
2834 clear_tv(&v->di_tv);
2835 if (v->di_flags & DI_FLAGS_ALLOC)
2836 vim_free(v);
2837 }
2838 }
2839 hash_clear(ht);
2840 ht->ht_used = 0;
2841}
2842
2843/*
2844 * Delete a variable from hashtab "ht" at item "hi".
2845 * Clear the variable value and free the dictitem.
2846 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002847 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002848delete_var(hashtab_T *ht, hashitem_T *hi)
2849{
2850 dictitem_T *di = HI2DI(hi);
2851
2852 hash_remove(ht, hi);
2853 clear_tv(&di->di_tv);
2854 vim_free(di);
2855}
2856
2857/*
2858 * List the value of one internal variable.
2859 */
2860 static void
2861list_one_var(dictitem_T *v, char *prefix, int *first)
2862{
2863 char_u *tofree;
2864 char_u *s;
2865 char_u numbuf[NUMBUFLEN];
2866
2867 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2868 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2869 s == NULL ? (char_u *)"" : s, first);
2870 vim_free(tofree);
2871}
2872
2873 static void
2874list_one_var_a(
2875 char *prefix,
2876 char_u *name,
2877 int type,
2878 char_u *string,
2879 int *first) // when TRUE clear rest of screen and set to FALSE
2880{
2881 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2882 msg_start();
2883 msg_puts(prefix);
2884 if (name != NULL) // "a:" vars don't have a name stored
2885 msg_puts((char *)name);
2886 msg_putchar(' ');
2887 msg_advance(22);
2888 if (type == VAR_NUMBER)
2889 msg_putchar('#');
2890 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2891 msg_putchar('*');
2892 else if (type == VAR_LIST)
2893 {
2894 msg_putchar('[');
2895 if (*string == '[')
2896 ++string;
2897 }
2898 else if (type == VAR_DICT)
2899 {
2900 msg_putchar('{');
2901 if (*string == '{')
2902 ++string;
2903 }
2904 else
2905 msg_putchar(' ');
2906
2907 msg_outtrans(string);
2908
2909 if (type == VAR_FUNC || type == VAR_PARTIAL)
2910 msg_puts("()");
2911 if (*first)
2912 {
2913 msg_clr_eos();
2914 *first = FALSE;
2915 }
2916}
2917
2918/*
2919 * Set variable "name" to value in "tv".
2920 * If the variable already exists, the value is updated.
2921 * Otherwise the variable is created.
2922 */
2923 void
2924set_var(
2925 char_u *name,
2926 typval_T *tv,
2927 int copy) // make copy of value in "tv"
2928{
Bram Moolenaareeb27bf2020-07-04 17:39:10 +02002929 set_var_const(name, NULL, tv, copy, LET_NO_COMMAND);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002930}
2931
2932/*
2933 * Set variable "name" to value in "tv".
2934 * If the variable already exists and "is_const" is FALSE the value is updated.
2935 * Otherwise the variable is created.
2936 */
2937 void
2938set_var_const(
2939 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002941 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002942 int copy, // make copy of value in "tv"
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002943 int flags) // LET_IS_CONST, LET_FORCEIT, LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002944{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002945 typval_T *tv = tv_arg;
2946 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002948 char_u *varname;
2949 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02002951 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002952
2953 ht = find_var_ht(name, &varname);
2954 if (ht == NULL || *varname == NUL)
2955 {
2956 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02002957 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002958 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002959 is_script_local = ht == get_script_local_ht();
2960
Bram Moolenaardbeecb22020-09-14 18:15:09 +02002961 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002962 && !is_script_local
2963 && (flags & LET_NO_COMMAND) == 0
2964 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02002965 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002966 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02002967 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02002968 }
2969
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002971
2972 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 if (di == NULL)
2974 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002975
2976 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02002977 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02002978 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002979
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002980 if (need_convert_to_bool(type, tv))
2981 {
2982 // Destination is a bool and the value is not, but it can be converted.
2983 CLEAR_FIELD(bool_tv);
2984 bool_tv.v_type = VAR_BOOL;
2985 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
2986 tv = &bool_tv;
2987 }
2988
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002990 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002992 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 if (flags & LET_IS_CONST)
2994 {
2995 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02002996 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 }
2998
Bram Moolenaardbeecb22020-09-14 18:15:09 +02002999 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 {
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003001 if ((flags & LET_NO_COMMAND) == 0)
3002 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003003 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003004 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003005 }
3006
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003007 // check the type and adjust to bool if needed
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003008 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003009 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003011
Bram Moolenaara187c432020-09-16 21:08:28 +02003012 // Check in this order for backwards compatibility:
3013 // - Whether the variable is read-only
3014 // - Whether the variable value is locked
3015 // - Whether the variable is locked
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003016 if (var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02003017 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3018 || var_check_lock(di->di_flags, name, FALSE))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003019 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003020 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 else
3022 // can only redefine once
3023 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003024
3025 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003028 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003029 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003030 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003032 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003034 if (copy || tv->v_type != VAR_STRING)
3035 {
3036 char_u *val = tv_get_string(tv);
3037
3038 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003039 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 if (di->di_tv.vval.v_string == NULL)
3041 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003042 }
3043 else
3044 {
3045 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003047 tv->vval.v_string = NULL;
3048 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003049 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003050 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003052 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003054 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003056#ifdef FEAT_SEARCH_EXTRA
3057 else if (STRCMP(varname, "hlsearch") == 0)
3058 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003060 redraw_all_later(SOME_VALID);
3061 }
3062#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003063 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003064 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003066 {
3067 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003068 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003069 }
3070 }
3071
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003073 }
3074 else // add a new variable
3075 {
3076 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003077 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003078 {
3079 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003080 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003081 }
3082
3083 // Make sure the variable name is valid.
3084 if (!valid_varname(varname))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003085 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003086
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3088 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003089 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 STRCPY(di->di_key, varname);
3091 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003092 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003094 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003095 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 di->di_flags = DI_FLAGS_ALLOC;
3097 if (flags & LET_IS_CONST)
3098 di->di_flags |= DI_FLAGS_LOCK;
3099
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003100 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003102 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103
3104 // Store a pointer to the typval_T, so that it can be found by
3105 // index instead of using a hastab lookup.
3106 if (ga_grow(&si->sn_var_vals, 1) == OK)
3107 {
3108 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3109 + si->sn_var_vals.ga_len;
3110 sv->sv_name = di->di_key;
3111 sv->sv_tv = &di->di_tv;
Bram Moolenaar53b29e42020-08-15 14:31:20 +02003112 if (type == NULL)
3113 sv->sv_type = typval2type(tv, &si->sn_type_list);
3114 else
3115 sv->sv_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 sv->sv_const = (flags & LET_IS_CONST);
3117 sv->sv_export = is_export;
3118 ++si->sn_var_vals.ga_len;
3119
3120 // let ex_export() know the export worked.
3121 is_export = FALSE;
3122 }
3123 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003124 }
3125
3126 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003128 else
3129 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 di->di_tv = *tv;
3131 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003132 init_tv(tv);
3133 }
3134
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003135 // ":const var = val" locks the value; in Vim9 script only with ":const!"
3136 if ((flags & LET_IS_CONST) && (!vim9script || (flags & LET_FORCEIT)))
Bram Moolenaar021bda52020-08-17 21:07:22 +02003137 // Like :lockvar! name: lock the value and what it contains, but only
3138 // if the reference count is up to one. That locks only literal
3139 // values.
3140 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003141 return;
3142failed:
3143 if (!copy)
3144 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003145}
3146
3147/*
3148 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3149 * Also give an error message.
3150 */
3151 int
3152var_check_ro(int flags, char_u *name, int use_gettext)
3153{
3154 if (flags & DI_FLAGS_RO)
3155 {
3156 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3157 return TRUE;
3158 }
3159 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3160 {
3161 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3162 return TRUE;
3163 }
3164 return FALSE;
3165}
3166
3167/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003168 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3169 * Also give an error message.
3170 */
3171 int
3172var_check_lock(int flags, char_u *name, int use_gettext)
3173{
3174 if (flags & DI_FLAGS_LOCK)
3175 {
3176 semsg(_(e_variable_is_locked_str),
3177 use_gettext ? (char_u *)_(name) : name);
3178 return TRUE;
3179 }
3180 return FALSE;
3181}
3182
3183/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003184 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3185 * Also give an error message.
3186 */
3187 int
3188var_check_fixed(int flags, char_u *name, int use_gettext)
3189{
3190 if (flags & DI_FLAGS_FIX)
3191 {
3192 semsg(_("E795: Cannot delete variable %s"),
3193 use_gettext ? (char_u *)_(name) : name);
3194 return TRUE;
3195 }
3196 return FALSE;
3197}
3198
3199/*
3200 * Check if a funcref is assigned to a valid variable name.
3201 * Return TRUE and give an error if not.
3202 */
3203 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003204var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003205 char_u *name, // points to start of variable name
3206 int new_var) // TRUE when creating the variable
3207{
3208 // Allow for w: b: s: and t:.
3209 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3210 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3211 ? name[2] : name[0]))
3212 {
3213 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3214 name);
3215 return TRUE;
3216 }
3217 // Don't allow hiding a function. When "v" is not NULL we might be
3218 // assigning another function to the same var, the type is checked
3219 // below.
3220 if (new_var && function_exists(name, FALSE))
3221 {
3222 semsg(_("E705: Variable name conflicts with existing function: %s"),
3223 name);
3224 return TRUE;
3225 }
3226 return FALSE;
3227}
3228
3229/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003230 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3231 * value. Also give an error message, using "name" or _("name") when
3232 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003233 */
3234 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003235value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003236{
3237 if (lock & VAR_LOCKED)
3238 {
3239 semsg(_("E741: Value is locked: %s"),
3240 name == NULL ? (char_u *)_("Unknown")
3241 : use_gettext ? (char_u *)_(name)
3242 : name);
3243 return TRUE;
3244 }
3245 if (lock & VAR_FIXED)
3246 {
3247 semsg(_("E742: Cannot change value of %s"),
3248 name == NULL ? (char_u *)_("Unknown")
3249 : use_gettext ? (char_u *)_(name)
3250 : name);
3251 return TRUE;
3252 }
3253 return FALSE;
3254}
3255
3256/*
3257 * Check if a variable name is valid.
3258 * Return FALSE and give an error if not.
3259 */
3260 int
3261valid_varname(char_u *varname)
3262{
3263 char_u *p;
3264
3265 for (p = varname; *p != NUL; ++p)
3266 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3267 && *p != AUTOLOAD_CHAR)
3268 {
3269 semsg(_(e_illvar), varname);
3270 return FALSE;
3271 }
3272 return TRUE;
3273}
3274
3275/*
3276 * getwinvar() and gettabwinvar()
3277 */
3278 static void
3279getwinvar(
3280 typval_T *argvars,
3281 typval_T *rettv,
3282 int off) // 1 for gettabwinvar()
3283{
3284 win_T *win;
3285 char_u *varname;
3286 dictitem_T *v;
3287 tabpage_T *tp = NULL;
3288 int done = FALSE;
3289 win_T *oldcurwin;
3290 tabpage_T *oldtabpage;
3291 int need_switch_win;
3292
3293 if (off == 1)
3294 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3295 else
3296 tp = curtab;
3297 win = find_win_by_nr(&argvars[off], tp);
3298 varname = tv_get_string_chk(&argvars[off + 1]);
3299 ++emsg_off;
3300
3301 rettv->v_type = VAR_STRING;
3302 rettv->vval.v_string = NULL;
3303
3304 if (win != NULL && varname != NULL)
3305 {
3306 // Set curwin to be our win, temporarily. Also set the tabpage,
3307 // otherwise the window is not valid. Only do this when needed,
3308 // autocommands get blocked.
3309 need_switch_win = !(tp == curtab && win == curwin);
3310 if (!need_switch_win
3311 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3312 {
3313 if (*varname == '&')
3314 {
3315 if (varname[1] == NUL)
3316 {
3317 // get all window-local options in a dict
3318 dict_T *opts = get_winbuf_options(FALSE);
3319
3320 if (opts != NULL)
3321 {
3322 rettv_dict_set(rettv, opts);
3323 done = TRUE;
3324 }
3325 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003326 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003327 // window-local-option
3328 done = TRUE;
3329 }
3330 else
3331 {
3332 // Look up the variable.
3333 // Let getwinvar({nr}, "") return the "w:" dictionary.
3334 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3335 varname, FALSE);
3336 if (v != NULL)
3337 {
3338 copy_tv(&v->di_tv, rettv);
3339 done = TRUE;
3340 }
3341 }
3342 }
3343
3344 if (need_switch_win)
3345 // restore previous notion of curwin
3346 restore_win(oldcurwin, oldtabpage, TRUE);
3347 }
3348
3349 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3350 // use the default return value
3351 copy_tv(&argvars[off + 2], rettv);
3352
3353 --emsg_off;
3354}
3355
3356/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003357 * Set option "varname" to the value of "varp" for the current buffer/window.
3358 */
3359 static void
3360set_option_from_tv(char_u *varname, typval_T *varp)
3361{
3362 long numval = 0;
3363 char_u *strval;
3364 char_u nbuf[NUMBUFLEN];
3365 int error = FALSE;
3366
3367 if (!in_vim9script() || varp->v_type != VAR_STRING)
3368 numval = (long)tv_get_number_chk(varp, &error);
3369 strval = tv_get_string_buf_chk(varp, nbuf);
3370 if (!error && strval != NULL)
3371 set_option_value(varname, numval, strval, OPT_LOCAL);
3372}
3373
3374/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003375 * "setwinvar()" and "settabwinvar()" functions
3376 */
3377 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003378setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003379{
3380 win_T *win;
3381 win_T *save_curwin;
3382 tabpage_T *save_curtab;
3383 int need_switch_win;
3384 char_u *varname, *winvarname;
3385 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003386 tabpage_T *tp = NULL;
3387
3388 if (check_secure())
3389 return;
3390
3391 if (off == 1)
3392 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3393 else
3394 tp = curtab;
3395 win = find_win_by_nr(&argvars[off], tp);
3396 varname = tv_get_string_chk(&argvars[off + 1]);
3397 varp = &argvars[off + 2];
3398
3399 if (win != NULL && varname != NULL && varp != NULL)
3400 {
3401 need_switch_win = !(tp == curtab && win == curwin);
3402 if (!need_switch_win
3403 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3404 {
3405 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003406 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003407 else
3408 {
3409 winvarname = alloc(STRLEN(varname) + 3);
3410 if (winvarname != NULL)
3411 {
3412 STRCPY(winvarname, "w:");
3413 STRCPY(winvarname + 2, varname);
3414 set_var(winvarname, varp, TRUE);
3415 vim_free(winvarname);
3416 }
3417 }
3418 }
3419 if (need_switch_win)
3420 restore_win(save_curwin, save_curtab, TRUE);
3421 }
3422}
3423
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003424/*
3425 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3426 * v:option_type, and v:option_command.
3427 */
3428 void
3429reset_v_option_vars(void)
3430{
3431 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3432 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3433 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3434 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3435 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3436 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3437}
3438
3439/*
3440 * Add an assert error to v:errors.
3441 */
3442 void
3443assert_error(garray_T *gap)
3444{
3445 struct vimvar *vp = &vimvars[VV_ERRORS];
3446
3447 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003448 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003449 set_vim_var_list(VV_ERRORS, list_alloc());
3450 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3451}
3452
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003453 int
3454var_exists(char_u *var)
3455{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003456 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003457 char_u *name;
3458 char_u *tofree;
3459 typval_T tv;
3460 int len = 0;
3461 int n = FALSE;
3462
3463 // get_name_len() takes care of expanding curly braces
3464 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003465 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003466 if (len > 0)
3467 {
3468 if (tofree != NULL)
3469 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003470 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003471 if (n)
3472 {
3473 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003474 arg = skipwhite(arg);
3475 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003476 if (n)
3477 clear_tv(&tv);
3478 }
3479 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003480 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003481 n = FALSE;
3482
3483 vim_free(tofree);
3484 return n;
3485}
3486
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003487static lval_T *redir_lval = NULL;
3488#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3489static garray_T redir_ga; // only valid when redir_lval is not NULL
3490static char_u *redir_endp = NULL;
3491static char_u *redir_varname = NULL;
3492
3493/*
3494 * Start recording command output to a variable
3495 * When "append" is TRUE append to an existing variable.
3496 * Returns OK if successfully completed the setup. FAIL otherwise.
3497 */
3498 int
3499var_redir_start(char_u *name, int append)
3500{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003501 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003502 typval_T tv;
3503
3504 // Catch a bad name early.
3505 if (!eval_isnamec1(*name))
3506 {
3507 emsg(_(e_invarg));
3508 return FAIL;
3509 }
3510
3511 // Make a copy of the name, it is used in redir_lval until redir ends.
3512 redir_varname = vim_strsave(name);
3513 if (redir_varname == NULL)
3514 return FAIL;
3515
3516 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3517 if (redir_lval == NULL)
3518 {
3519 var_redir_stop();
3520 return FAIL;
3521 }
3522
3523 // The output is stored in growarray "redir_ga" until redirection ends.
3524 ga_init2(&redir_ga, (int)sizeof(char), 500);
3525
3526 // Parse the variable name (can be a dict or list entry).
3527 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3528 FNE_CHECK_START);
3529 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3530 {
3531 clear_lval(redir_lval);
3532 if (redir_endp != NULL && *redir_endp != NUL)
3533 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003534 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003535 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003536 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003537 redir_endp = NULL; // don't store a value, only cleanup
3538 var_redir_stop();
3539 return FAIL;
3540 }
3541
3542 // check if we can write to the variable: set it to or append an empty
3543 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003544 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003545 tv.v_type = VAR_STRING;
3546 tv.vval.v_string = (char_u *)"";
3547 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003549 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003551 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003552 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003553 {
3554 redir_endp = NULL; // don't store a value, only cleanup
3555 var_redir_stop();
3556 return FAIL;
3557 }
3558
3559 return OK;
3560}
3561
3562/*
3563 * Append "value[value_len]" to the variable set by var_redir_start().
3564 * The actual appending is postponed until redirection ends, because the value
3565 * appended may in fact be the string we write to, changing it may cause freed
3566 * memory to be used:
3567 * :redir => foo
3568 * :let foo
3569 * :redir END
3570 */
3571 void
3572var_redir_str(char_u *value, int value_len)
3573{
3574 int len;
3575
3576 if (redir_lval == NULL)
3577 return;
3578
3579 if (value_len == -1)
3580 len = (int)STRLEN(value); // Append the entire string
3581 else
3582 len = value_len; // Append only "value_len" characters
3583
3584 if (ga_grow(&redir_ga, len) == OK)
3585 {
3586 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3587 redir_ga.ga_len += len;
3588 }
3589 else
3590 var_redir_stop();
3591}
3592
3593/*
3594 * Stop redirecting command output to a variable.
3595 * Frees the allocated memory.
3596 */
3597 void
3598var_redir_stop(void)
3599{
3600 typval_T tv;
3601
3602 if (EVALCMD_BUSY)
3603 {
3604 redir_lval = NULL;
3605 return;
3606 }
3607
3608 if (redir_lval != NULL)
3609 {
3610 // If there was no error: assign the text to the variable.
3611 if (redir_endp != NULL)
3612 {
3613 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3614 tv.v_type = VAR_STRING;
3615 tv.vval.v_string = redir_ga.ga_data;
3616 // Call get_lval() again, if it's inside a Dict or List it may
3617 // have changed.
3618 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3619 FALSE, FALSE, 0, FNE_CHECK_START);
3620 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003622 (char_u *)".");
3623 clear_lval(redir_lval);
3624 }
3625
3626 // free the collected output
3627 VIM_CLEAR(redir_ga.ga_data);
3628
3629 VIM_CLEAR(redir_lval);
3630 }
3631 VIM_CLEAR(redir_varname);
3632}
3633
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003634/*
3635 * "gettabvar()" function
3636 */
3637 void
3638f_gettabvar(typval_T *argvars, typval_T *rettv)
3639{
3640 win_T *oldcurwin;
3641 tabpage_T *tp, *oldtabpage;
3642 dictitem_T *v;
3643 char_u *varname;
3644 int done = FALSE;
3645
3646 rettv->v_type = VAR_STRING;
3647 rettv->vval.v_string = NULL;
3648
3649 varname = tv_get_string_chk(&argvars[1]);
3650 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3651 if (tp != NULL && varname != NULL)
3652 {
3653 // Set tp to be our tabpage, temporarily. Also set the window to the
3654 // first window in the tabpage, otherwise the window is not valid.
3655 if (switch_win(&oldcurwin, &oldtabpage,
3656 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3657 : tp->tp_firstwin, tp, TRUE) == OK)
3658 {
3659 // look up the variable
3660 // Let gettabvar({nr}, "") return the "t:" dictionary.
3661 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3662 if (v != NULL)
3663 {
3664 copy_tv(&v->di_tv, rettv);
3665 done = TRUE;
3666 }
3667 }
3668
3669 // restore previous notion of curwin
3670 restore_win(oldcurwin, oldtabpage, TRUE);
3671 }
3672
3673 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3674 copy_tv(&argvars[2], rettv);
3675}
3676
3677/*
3678 * "gettabwinvar()" function
3679 */
3680 void
3681f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3682{
3683 getwinvar(argvars, rettv, 1);
3684}
3685
3686/*
3687 * "getwinvar()" function
3688 */
3689 void
3690f_getwinvar(typval_T *argvars, typval_T *rettv)
3691{
3692 getwinvar(argvars, rettv, 0);
3693}
3694
3695/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003696 * "getbufvar()" function
3697 */
3698 void
3699f_getbufvar(typval_T *argvars, typval_T *rettv)
3700{
3701 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003702 char_u *varname;
3703 dictitem_T *v;
3704 int done = FALSE;
3705
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003706 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003707 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003708
3709 rettv->v_type = VAR_STRING;
3710 rettv->vval.v_string = NULL;
3711
3712 if (buf != NULL && varname != NULL)
3713 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003714 if (*varname == '&')
3715 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003716 buf_T *save_curbuf = curbuf;
3717
3718 // set curbuf to be our buf, temporarily
3719 curbuf = buf;
3720
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003721 if (varname[1] == NUL)
3722 {
3723 // get all buffer-local options in a dict
3724 dict_T *opts = get_winbuf_options(TRUE);
3725
3726 if (opts != NULL)
3727 {
3728 rettv_dict_set(rettv, opts);
3729 done = TRUE;
3730 }
3731 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003732 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003733 // buffer-local-option
3734 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003735
3736 // restore previous notion of curbuf
3737 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003738 }
3739 else
3740 {
3741 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003742 if (*varname == NUL)
3743 // Let getbufvar({nr}, "") return the "b:" dictionary.
3744 v = &buf->b_bufvar;
3745 else
3746 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3747 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003748 if (v != NULL)
3749 {
3750 copy_tv(&v->di_tv, rettv);
3751 done = TRUE;
3752 }
3753 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003754 }
3755
3756 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3757 // use the default value
3758 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003759}
3760
3761/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003762 * "settabvar()" function
3763 */
3764 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003765f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003766{
3767 tabpage_T *save_curtab;
3768 tabpage_T *tp;
3769 char_u *varname, *tabvarname;
3770 typval_T *varp;
3771
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003772 if (check_secure())
3773 return;
3774
3775 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3776 varname = tv_get_string_chk(&argvars[1]);
3777 varp = &argvars[2];
3778
3779 if (varname != NULL && varp != NULL && tp != NULL)
3780 {
3781 save_curtab = curtab;
3782 goto_tabpage_tp(tp, FALSE, FALSE);
3783
3784 tabvarname = alloc(STRLEN(varname) + 3);
3785 if (tabvarname != NULL)
3786 {
3787 STRCPY(tabvarname, "t:");
3788 STRCPY(tabvarname + 2, varname);
3789 set_var(tabvarname, varp, TRUE);
3790 vim_free(tabvarname);
3791 }
3792
3793 // Restore current tabpage
3794 if (valid_tabpage(save_curtab))
3795 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3796 }
3797}
3798
3799/*
3800 * "settabwinvar()" function
3801 */
3802 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003803f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003804{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003805 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003806}
3807
3808/*
3809 * "setwinvar()" function
3810 */
3811 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003812f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003813{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003814 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003815}
3816
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003817/*
3818 * "setbufvar()" function
3819 */
3820 void
3821f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3822{
3823 buf_T *buf;
3824 char_u *varname, *bufvarname;
3825 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003826
3827 if (check_secure())
3828 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003829 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003830 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003831 varp = &argvars[2];
3832
3833 if (buf != NULL && varname != NULL && varp != NULL)
3834 {
3835 if (*varname == '&')
3836 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003837 aco_save_T aco;
3838
3839 // set curbuf to be our buf, temporarily
3840 aucmd_prepbuf(&aco, buf);
3841
Bram Moolenaar191929b2020-08-19 21:20:49 +02003842 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003843
3844 // reset notion of buffer
3845 aucmd_restbuf(&aco);
3846 }
3847 else
3848 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003849 bufvarname = alloc(STRLEN(varname) + 3);
3850 if (bufvarname != NULL)
3851 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003852 buf_T *save_curbuf = curbuf;
3853
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003854 curbuf = buf;
3855 STRCPY(bufvarname, "b:");
3856 STRCPY(bufvarname + 2, varname);
3857 set_var(bufvarname, varp, TRUE);
3858 vim_free(bufvarname);
3859 curbuf = save_curbuf;
3860 }
3861 }
3862 }
3863}
3864
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003865/*
3866 * Get a callback from "arg". It can be a Funcref or a function name.
3867 * When "arg" is zero return an empty string.
3868 * "cb_name" is not allocated.
3869 * "cb_name" is set to NULL for an invalid argument.
3870 */
3871 callback_T
3872get_callback(typval_T *arg)
3873{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003874 callback_T res;
3875 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003876
3877 res.cb_free_name = FALSE;
3878 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3879 {
3880 res.cb_partial = arg->vval.v_partial;
3881 ++res.cb_partial->pt_refcount;
3882 res.cb_name = partial_name(res.cb_partial);
3883 }
3884 else
3885 {
3886 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003887 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3888 && isdigit(*arg->vval.v_string))
3889 r = FAIL;
3890 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003891 {
3892 // Note that we don't make a copy of the string.
3893 res.cb_name = arg->vval.v_string;
3894 func_ref(res.cb_name);
3895 }
3896 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003897 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003898 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003899 r = FAIL;
3900
3901 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003902 {
3903 emsg(_("E921: Invalid callback argument"));
3904 res.cb_name = NULL;
3905 }
3906 }
3907 return res;
3908}
3909
3910/*
3911 * Copy a callback into a typval_T.
3912 */
3913 void
3914put_callback(callback_T *cb, typval_T *tv)
3915{
3916 if (cb->cb_partial != NULL)
3917 {
3918 tv->v_type = VAR_PARTIAL;
3919 tv->vval.v_partial = cb->cb_partial;
3920 ++tv->vval.v_partial->pt_refcount;
3921 }
3922 else
3923 {
3924 tv->v_type = VAR_FUNC;
3925 tv->vval.v_string = vim_strsave(cb->cb_name);
3926 func_ref(cb->cb_name);
3927 }
3928}
3929
3930/*
3931 * Make a copy of "src" into "dest", allocating the function name if needed,
3932 * without incrementing the refcount.
3933 */
3934 void
3935set_callback(callback_T *dest, callback_T *src)
3936{
3937 if (src->cb_partial == NULL)
3938 {
3939 // just a function name, make a copy
3940 dest->cb_name = vim_strsave(src->cb_name);
3941 dest->cb_free_name = TRUE;
3942 }
3943 else
3944 {
3945 // cb_name is a pointer into cb_partial
3946 dest->cb_name = src->cb_name;
3947 dest->cb_free_name = FALSE;
3948 }
3949 dest->cb_partial = src->cb_partial;
3950}
3951
3952/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02003953 * Copy callback from "src" to "dest", incrementing the refcounts.
3954 */
3955 void
3956copy_callback(callback_T *dest, callback_T *src)
3957{
3958 dest->cb_partial = src->cb_partial;
3959 if (dest->cb_partial != NULL)
3960 {
3961 dest->cb_name = src->cb_name;
3962 dest->cb_free_name = FALSE;
3963 ++dest->cb_partial->pt_refcount;
3964 }
3965 else
3966 {
3967 dest->cb_name = vim_strsave(src->cb_name);
3968 dest->cb_free_name = TRUE;
3969 func_ref(src->cb_name);
3970 }
3971}
3972
3973/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003974 * Unref/free "callback" returned by get_callback() or set_callback().
3975 */
3976 void
3977free_callback(callback_T *callback)
3978{
3979 if (callback->cb_partial != NULL)
3980 {
3981 partial_unref(callback->cb_partial);
3982 callback->cb_partial = NULL;
3983 }
3984 else if (callback->cb_name != NULL)
3985 func_unref(callback->cb_name);
3986 if (callback->cb_free_name)
3987 {
3988 vim_free(callback->cb_name);
3989 callback->cb_free_name = FALSE;
3990 }
3991 callback->cb_name = NULL;
3992}
3993
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003994#endif // FEAT_EVAL