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