blob: a45080b540aefcfd8c2306377a7b16ac1df48f24 [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 Moolenaar822ba242020-05-24 23:00:18 +0200167static void ex_let_const(exarg_T *eap);
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
Bram Moolenaar32e35112020-05-14 22:41:15 +0200437 if (eval1(&p, &rettv, EVAL_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200438 {
439 if (rettv.v_type != VAR_LIST)
440 clear_tv(&rettv);
441 else
442 list = rettv.vval.v_list;
443 }
444
445 if (p_verbose == 0)
446 --emsg_off;
447 clear_tv(get_vim_var_tv(VV_VAL));
448 restore_vimvar(VV_VAL, &save_val);
449
450 return list;
451}
452
453/*
454 * "list" is supposed to contain two items: a word and a number. Return the
455 * word in "pp" and the number as the return value.
456 * Return -1 if anything isn't right.
457 * Used to get the good word and score from the eval_spell_expr() result.
458 */
459 int
460get_spellword(list_T *list, char_u **pp)
461{
462 listitem_T *li;
463
464 li = list->lv_first;
465 if (li == NULL)
466 return -1;
467 *pp = tv_get_string(&li->li_tv);
468
469 li = li->li_next;
470 if (li == NULL)
471 return -1;
472 return (int)tv_get_number(&li->li_tv);
473}
474#endif
475
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200476/*
477 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200478 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200479 * When not used yet add the variable to the v: hashtable.
480 */
481 void
482prepare_vimvar(int idx, typval_T *save_tv)
483{
484 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200485 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200486 if (vimvars[idx].vv_type == VAR_UNKNOWN)
487 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
488}
489
490/*
491 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200492 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200493 * When no longer defined, remove the variable from the v: hashtable.
494 */
495 void
496restore_vimvar(int idx, typval_T *save_tv)
497{
498 hashitem_T *hi;
499
500 vimvars[idx].vv_tv = *save_tv;
501 if (vimvars[idx].vv_type == VAR_UNKNOWN)
502 {
503 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
504 if (HASHITEM_EMPTY(hi))
505 internal_error("restore_vimvar()");
506 else
507 hash_remove(&vimvarht, hi);
508 }
509}
510
511/*
512 * List Vim variables.
513 */
514 static void
515list_vim_vars(int *first)
516{
517 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
518}
519
520/*
521 * List script-local variables, if there is a script.
522 */
523 static void
524list_script_vars(int *first)
525{
Bram 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;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200598 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200599 {
600 emsg(_("E221: Marker cannot start with lower case letter"));
601 return NULL;
602 }
603 }
604 else
605 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200606 // When getting lines for an embedded script, if the marker is missing,
607 // accept '.' as the marker.
608 if (script_get)
609 marker = dot;
610 else
611 {
612 emsg(_("E172: Missing marker"));
613 return NULL;
614 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200615 }
616
617 l = list_alloc();
618 if (l == NULL)
619 return NULL;
620
621 for (;;)
622 {
623 int mi = 0;
624 int ti = 0;
625
626 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
627 if (theline == NULL)
628 {
629 semsg(_("E990: Missing end marker '%s'"), marker);
630 break;
631 }
632
633 // with "trim": skip the indent matching the :let line to find the
634 // marker
635 if (marker_indent_len > 0
636 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
637 mi = marker_indent_len;
638 if (STRCMP(marker, theline + mi) == 0)
639 {
640 vim_free(theline);
641 break;
642 }
643
644 if (text_indent_len == -1 && *theline != NUL)
645 {
646 // set the text indent from the first line.
647 p = theline;
648 text_indent_len = 0;
649 while (VIM_ISWHITE(*p))
650 {
651 p++;
652 text_indent_len++;
653 }
654 text_indent = vim_strnsave(theline, text_indent_len);
655 }
656 // with "trim": skip the indent matching the first line
657 if (text_indent != NULL)
658 for (ti = 0; ti < text_indent_len; ++ti)
659 if (theline[ti] != text_indent[ti])
660 break;
661
662 if (list_append_string(l, theline + ti, -1) == FAIL)
663 break;
664 vim_free(theline);
665 }
666 vim_free(text_indent);
667
668 return l;
669}
670
671/*
672 * ":let" list all variable values
673 * ":let var1 var2" list variable values
674 * ":let var = expr" assignment command.
675 * ":let var += expr" assignment command.
676 * ":let var -= expr" assignment command.
677 * ":let var *= expr" assignment command.
678 * ":let var /= expr" assignment command.
679 * ":let var %= expr" assignment command.
680 * ":let var .= expr" assignment command.
681 * ":let var ..= expr" assignment command.
682 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100684 * ":let var: string" Vim9 declaration
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200685 */
686 void
687ex_let(exarg_T *eap)
688{
Bram Moolenaar822ba242020-05-24 23:00:18 +0200689 ex_let_const(eap);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200690}
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{
Bram Moolenaar822ba242020-05-24 23:00:18 +0200701 ex_let_const(eap);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200702}
703
Bram Moolenaar822ba242020-05-24 23:00:18 +0200704 static void
705ex_let_const(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200706{
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 Moolenaar32e35112020-05-14 22:41:15 +0200717 int has_assign;
Bram Moolenaar09689a02020-05-09 22:50:08 +0200718 int flags = eap->cmdidx == CMD_const ? LET_IS_CONST : 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200719
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720 // detect Vim9 assignment without ":let" or ":const"
721 if (eap->arg == eap->cmd)
722 flags |= LET_NO_COMMAND;
723
724 argend = skip_var_list(arg, TRUE, &var_count, &semicolon);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200725 if (argend == NULL)
726 return;
727 if (argend > arg && argend[-1] == '.') // for var.='str'
728 --argend;
729 expr = skipwhite(argend);
730 concat = expr[0] == '.'
731 && ((expr[1] == '=' && current_sctx.sc_version < 2)
732 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200733 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
734 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200735 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200736 {
737 // ":let" without "=": list variables
738 if (*arg == '[')
739 emsg(_(e_invarg));
740 else if (expr[0] == '.')
741 emsg(_("E985: .= is not supported with script version 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200742 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200743 // ":let var1 var2"
744 arg = list_arg_vars(eap, arg, &first);
745 else if (!eap->skip)
746 {
747 // ":let"
748 list_glob_vars(&first);
749 list_buf_vars(&first);
750 list_win_vars(&first);
751 list_tab_vars(&first);
752 list_script_vars(&first);
753 list_func_vars(&first);
754 list_vim_vars(&first);
755 }
756 eap->nextcmd = check_nextcmd(arg);
757 }
758 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
759 {
760 list_T *l;
761
762 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200763 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200764 if (l != NULL)
765 {
766 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200767 if (!eap->skip)
768 {
769 op[0] = '=';
770 op[1] = NUL;
771 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200773 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200774 clear_tv(&rettv);
775 }
776 }
777 else
778 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200779 int eval_flags;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200780
Bram Moolenaar32e35112020-05-14 22:41:15 +0200781 rettv.v_type = VAR_UNKNOWN;
782 i = FAIL;
783 if (has_assign || concat)
784 {
785 op[0] = '=';
786 op[1] = NUL;
787 if (*expr != '=')
788 {
789 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
790 {
791 op[0] = *expr; // +=, -=, *=, /=, %= or .=
792 if (expr[0] == '.' && expr[1] == '.') // ..=
793 ++expr;
794 }
795 expr = skipwhite(expr + 2);
796 }
797 else
798 expr = skipwhite(expr + 1);
799
800 if (eap->skip)
801 ++emsg_skip;
802 eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200803 i = eval0(expr, &rettv, &eap->nextcmd, eval_flags);
804 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200805 if (eap->skip)
806 {
807 if (i != FAIL)
808 clear_tv(&rettv);
809 --emsg_skip;
810 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200811 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200812 {
813 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100814 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200815 clear_tv(&rettv);
816 }
817 }
818}
819
820/*
821 * Assign the typevalue "tv" to the variable or variables at "arg_start".
822 * Handles both "var" with any type and "[var, var; var]" with a list type.
823 * When "op" is not NULL it points to a string with characters that
824 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
825 * or concatenate.
826 * Returns OK or FAIL;
827 */
828 int
829ex_let_vars(
830 char_u *arg_start,
831 typval_T *tv,
832 int copy, // copy values from "tv", don't move
833 int semicolon, // from skip_var_list()
834 int var_count, // from skip_var_list()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200836 char_u *op)
837{
838 char_u *arg = arg_start;
839 list_T *l;
840 int i;
841 listitem_T *item;
842 typval_T ltv;
843
844 if (*arg != '[')
845 {
846 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100847 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200848 return FAIL;
849 return OK;
850 }
851
852 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
853 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
854 {
855 emsg(_(e_listreq));
856 return FAIL;
857 }
858
859 i = list_len(l);
860 if (semicolon == 0 && var_count < i)
861 {
862 emsg(_("E687: Less targets than List items"));
863 return FAIL;
864 }
865 if (var_count - semicolon > i)
866 {
867 emsg(_("E688: More targets than List items"));
868 return FAIL;
869 }
870
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200871 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200872 item = l->lv_first;
873 while (*arg != ']')
874 {
875 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100876 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200877 item = item->li_next;
878 if (arg == NULL)
879 return FAIL;
880
881 arg = skipwhite(arg);
882 if (*arg == ';')
883 {
884 // Put the rest of the list (may be empty) in the var after ';'.
885 // Create a new list for this.
886 l = list_alloc();
887 if (l == NULL)
888 return FAIL;
889 while (item != NULL)
890 {
891 list_append_tv(l, &item->li_tv);
892 item = item->li_next;
893 }
894
895 ltv.v_type = VAR_LIST;
896 ltv.v_lock = 0;
897 ltv.vval.v_list = l;
898 l->lv_refcount = 1;
899
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200901 (char_u *)"]", op);
902 clear_tv(&ltv);
903 if (arg == NULL)
904 return FAIL;
905 break;
906 }
907 else if (*arg != ',' && *arg != ']')
908 {
909 internal_error("ex_let_vars()");
910 return FAIL;
911 }
912 }
913
914 return OK;
915}
916
917/*
918 * Skip over assignable variable "var" or list of variables "[var, var]".
919 * Used for ":let varvar = expr" and ":for varvar in expr".
920 * For "[var, var]" increment "*var_count" for each variable.
921 * for "[var, var; var]" set "semicolon".
922 * Return NULL for an error.
923 */
924 char_u *
925skip_var_list(
926 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200928 int *var_count,
929 int *semicolon)
930{
931 char_u *p, *s;
932
933 if (*arg == '[')
934 {
935 // "[var, var]": find the matching ']'.
936 p = arg;
937 for (;;)
938 {
939 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100940 s = skip_var_one(p, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200941 if (s == p)
942 {
943 semsg(_(e_invarg2), p);
944 return NULL;
945 }
946 ++*var_count;
947
948 p = skipwhite(s);
949 if (*p == ']')
950 break;
951 else if (*p == ';')
952 {
953 if (*semicolon == 1)
954 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200955 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200956 return NULL;
957 }
958 *semicolon = 1;
959 }
960 else if (*p != ',')
961 {
962 semsg(_(e_invarg2), p);
963 return NULL;
964 }
965 }
966 return p + 1;
967 }
968 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100969 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200970}
971
972/*
973 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
974 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100975 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200976 */
977 static char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100978skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200979{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980 char_u *end;
981
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200982 if (*arg == '@' && arg[1] != NUL)
983 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100984 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200985 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100986 if (include_type && current_sctx.sc_version == SCRIPT_VERSION_VIM9
987 && *end == ':')
988 {
989 end = skip_type(skipwhite(end + 1));
990 }
991 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200992}
993
994/*
995 * List variables for hashtab "ht" with prefix "prefix".
996 * If "empty" is TRUE also list NULL strings as empty strings.
997 */
998 void
999list_hashtable_vars(
1000 hashtab_T *ht,
1001 char *prefix,
1002 int empty,
1003 int *first)
1004{
1005 hashitem_T *hi;
1006 dictitem_T *di;
1007 int todo;
1008 char_u buf[IOSIZE];
1009
1010 todo = (int)ht->ht_used;
1011 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1012 {
1013 if (!HASHITEM_EMPTY(hi))
1014 {
1015 --todo;
1016 di = HI2DI(hi);
1017
1018 // apply :filter /pat/ to variable name
1019 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1020 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1021 if (message_filtered(buf))
1022 continue;
1023
1024 if (empty || di->di_tv.v_type != VAR_STRING
1025 || di->di_tv.vval.v_string != NULL)
1026 list_one_var(di, prefix, first);
1027 }
1028 }
1029}
1030
1031/*
1032 * List global variables.
1033 */
1034 static void
1035list_glob_vars(int *first)
1036{
1037 list_hashtable_vars(&globvarht, "", TRUE, first);
1038}
1039
1040/*
1041 * List buffer variables.
1042 */
1043 static void
1044list_buf_vars(int *first)
1045{
1046 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1047}
1048
1049/*
1050 * List window variables.
1051 */
1052 static void
1053list_win_vars(int *first)
1054{
1055 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1056}
1057
1058/*
1059 * List tab page variables.
1060 */
1061 static void
1062list_tab_vars(int *first)
1063{
1064 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1065}
1066
1067/*
1068 * List variables in "arg".
1069 */
1070 static char_u *
1071list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1072{
1073 int error = FALSE;
1074 int len;
1075 char_u *name;
1076 char_u *name_start;
1077 char_u *arg_subsc;
1078 char_u *tofree;
1079 typval_T tv;
1080
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001081 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001082 {
1083 if (error || eap->skip)
1084 {
1085 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1086 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1087 {
1088 emsg_severe = TRUE;
1089 emsg(_(e_trailing));
1090 break;
1091 }
1092 }
1093 else
1094 {
1095 // get_name_len() takes care of expanding curly braces
1096 name_start = name = arg;
1097 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1098 if (len <= 0)
1099 {
1100 // This is mainly to keep test 49 working: when expanding
1101 // curly braces fails overrule the exception error message.
1102 if (len < 0 && !aborting())
1103 {
1104 emsg_severe = TRUE;
1105 semsg(_(e_invarg2), arg);
1106 break;
1107 }
1108 error = TRUE;
1109 }
1110 else
1111 {
1112 if (tofree != NULL)
1113 name = tofree;
1114 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
1115 error = TRUE;
1116 else
1117 {
1118 // handle d.key, l[idx], f(expr)
1119 arg_subsc = arg;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001120 if (handle_subscript(&arg, &tv, EVAL_EVALUATE, TRUE,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001121 name, &name) == FAIL)
1122 error = TRUE;
1123 else
1124 {
1125 if (arg == arg_subsc && len == 2 && name[1] == ':')
1126 {
1127 switch (*name)
1128 {
1129 case 'g': list_glob_vars(first); break;
1130 case 'b': list_buf_vars(first); break;
1131 case 'w': list_win_vars(first); break;
1132 case 't': list_tab_vars(first); break;
1133 case 'v': list_vim_vars(first); break;
1134 case 's': list_script_vars(first); break;
1135 case 'l': list_func_vars(first); break;
1136 default:
1137 semsg(_("E738: Can't list variables for %s"), name);
1138 }
1139 }
1140 else
1141 {
1142 char_u numbuf[NUMBUFLEN];
1143 char_u *tf;
1144 int c;
1145 char_u *s;
1146
1147 s = echo_string(&tv, &tf, numbuf, 0);
1148 c = *arg;
1149 *arg = NUL;
1150 list_one_var_a("",
1151 arg == arg_subsc ? name : name_start,
1152 tv.v_type,
1153 s == NULL ? (char_u *)"" : s,
1154 first);
1155 *arg = c;
1156 vim_free(tf);
1157 }
1158 clear_tv(&tv);
1159 }
1160 }
1161 }
1162
1163 vim_free(tofree);
1164 }
1165
1166 arg = skipwhite(arg);
1167 }
1168
1169 return arg;
1170}
1171
1172/*
1173 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1174 * Returns a pointer to the char just after the var name.
1175 * Returns NULL if there is an error.
1176 */
1177 static char_u *
1178ex_let_one(
1179 char_u *arg, // points to variable name
1180 typval_T *tv, // value to assign to variable
1181 int copy, // copy value from "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001183 char_u *endchars, // valid chars after variable name or NULL
1184 char_u *op) // "+", "-", "." or NULL
1185{
1186 int c1;
1187 char_u *name;
1188 char_u *p;
1189 char_u *arg_end = NULL;
1190 int len;
1191 int opt_flags;
1192 char_u *tofree = NULL;
1193
1194 // ":let $VAR = expr": Set environment variable.
1195 if (*arg == '$')
1196 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001197 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001198 {
1199 emsg(_("E996: Cannot lock an environment variable"));
1200 return NULL;
1201 }
1202 // Find the end of the name.
1203 ++arg;
1204 name = arg;
1205 len = get_env_len(&arg);
1206 if (len == 0)
1207 semsg(_(e_invarg2), name - 1);
1208 else
1209 {
1210 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1211 semsg(_(e_letwrong), op);
1212 else if (endchars != NULL
1213 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1214 emsg(_(e_letunexp));
1215 else if (!check_secure())
1216 {
1217 c1 = name[len];
1218 name[len] = NUL;
1219 p = tv_get_string_chk(tv);
1220 if (p != NULL && op != NULL && *op == '.')
1221 {
1222 int mustfree = FALSE;
1223 char_u *s = vim_getenv(name, &mustfree);
1224
1225 if (s != NULL)
1226 {
1227 p = tofree = concat_str(s, p);
1228 if (mustfree)
1229 vim_free(s);
1230 }
1231 }
1232 if (p != NULL)
1233 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001234 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001235 arg_end = arg;
1236 }
1237 name[len] = c1;
1238 vim_free(tofree);
1239 }
1240 }
1241 }
1242
1243 // ":let &option = expr": Set option value.
1244 // ":let &l:option = expr": Set local option value.
1245 // ":let &g:option = expr": Set global option value.
1246 else if (*arg == '&')
1247 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001248 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001249 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001250 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001251 return NULL;
1252 }
1253 // Find the end of the name.
1254 p = find_option_end(&arg, &opt_flags);
1255 if (p == NULL || (endchars != NULL
1256 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1257 emsg(_(e_letunexp));
1258 else
1259 {
1260 long n;
1261 int opt_type;
1262 long numval;
1263 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001264 char_u *s = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001265
1266 c1 = *p;
1267 *p = NUL;
1268
1269 n = (long)tv_get_number(tv);
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001270 // avoid setting a string option to the text "v:false" or similar.
1271 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL)
1272 s = tv_get_string_chk(tv); // != NULL if number or string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001273 if (s != NULL && op != NULL && *op != '=')
1274 {
1275 opt_type = get_option_value(arg, &numval,
1276 &stringval, opt_flags);
1277 if ((opt_type == 1 && *op == '.')
1278 || (opt_type == 0 && *op != '.'))
1279 {
1280 semsg(_(e_letwrong), op);
1281 s = NULL; // don't set the value
1282 }
1283 else
1284 {
1285 if (opt_type == 1) // number
1286 {
1287 switch (*op)
1288 {
1289 case '+': n = numval + n; break;
1290 case '-': n = numval - n; break;
1291 case '*': n = numval * n; break;
1292 case '/': n = (long)num_divide(numval, n); break;
1293 case '%': n = (long)num_modulus(numval, n); break;
1294 }
1295 }
1296 else if (opt_type == 0 && stringval != NULL) // string
1297 {
1298 s = concat_str(stringval, s);
1299 vim_free(stringval);
1300 stringval = s;
1301 }
1302 }
1303 }
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001304 if (s != NULL || tv->v_type == VAR_BOOL
1305 || tv->v_type == VAR_SPECIAL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001306 {
1307 set_option_value(arg, n, s, opt_flags);
1308 arg_end = p;
1309 }
1310 *p = c1;
1311 vim_free(stringval);
1312 }
1313 }
1314
1315 // ":let @r = expr": Set register contents.
1316 else if (*arg == '@')
1317 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001319 {
1320 emsg(_("E996: Cannot lock a register"));
1321 return NULL;
1322 }
1323 ++arg;
1324 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1325 semsg(_(e_letwrong), op);
1326 else if (endchars != NULL
1327 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1328 emsg(_(e_letunexp));
1329 else
1330 {
1331 char_u *ptofree = NULL;
1332 char_u *s;
1333
1334 p = tv_get_string_chk(tv);
1335 if (p != NULL && op != NULL && *op == '.')
1336 {
1337 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1338 if (s != NULL)
1339 {
1340 p = ptofree = concat_str(s, p);
1341 vim_free(s);
1342 }
1343 }
1344 if (p != NULL)
1345 {
1346 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1347 arg_end = arg + 1;
1348 }
1349 vim_free(ptofree);
1350 }
1351 }
1352
1353 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001355 // ":let {expr} = expr": Idem, name made with curly braces
1356 else if (eval_isnamec1(*arg) || *arg == '{')
1357 {
1358 lval_T lv;
1359
1360 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001361 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001362 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363 if (endchars != NULL && vim_strchr(endchars,
1364 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001365 emsg(_(e_letunexp));
1366 else
1367 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001368 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001369 arg_end = p;
1370 }
1371 }
1372 clear_lval(&lv);
1373 }
1374
1375 else
1376 semsg(_(e_invarg2), arg);
1377
1378 return arg_end;
1379}
1380
1381/*
1382 * ":unlet[!] var1 ... " command.
1383 */
1384 void
1385ex_unlet(exarg_T *eap)
1386{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001387 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001388}
1389
1390/*
1391 * ":lockvar" and ":unlockvar" commands
1392 */
1393 void
1394ex_lockvar(exarg_T *eap)
1395{
1396 char_u *arg = eap->arg;
1397 int deep = 2;
1398
1399 if (eap->forceit)
1400 deep = -1;
1401 else if (vim_isdigit(*arg))
1402 {
1403 deep = getdigits(&arg);
1404 arg = skipwhite(arg);
1405 }
1406
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001407 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001408}
1409
1410/*
1411 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001412 * Also used for Vim9 script. "callback" is invoked as:
1413 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001414 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001415 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001416ex_unletlock(
1417 exarg_T *eap,
1418 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001419 int deep,
1420 int glv_flags,
1421 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1422 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001423{
1424 char_u *arg = argstart;
1425 char_u *name_end;
1426 int error = FALSE;
1427 lval_T lv;
1428
1429 do
1430 {
1431 if (*arg == '$')
1432 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001433 lv.ll_name = arg;
1434 lv.ll_tv = NULL;
1435 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001436 if (get_env_len(&arg) == 0)
1437 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001438 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001439 return;
1440 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001441 if (!error && !eap->skip
1442 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1443 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001444 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001445 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001446 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001447 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001448 // Parse the name and find the end.
1449 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1450 glv_flags, FNE_CHECK_START);
1451 if (lv.ll_name == NULL)
1452 error = TRUE; // error but continue parsing
1453 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1454 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001455 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001456 if (name_end != NULL)
1457 {
1458 emsg_severe = TRUE;
1459 emsg(_(e_trailing));
1460 }
1461 if (!(eap->skip || error))
1462 clear_lval(&lv);
1463 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001464 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001465
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001466 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001467 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001468 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001469
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001470 if (!eap->skip)
1471 clear_lval(&lv);
1472 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001473
1474 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001475 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001476
1477 eap->nextcmd = check_nextcmd(arg);
1478}
1479
1480 static int
1481do_unlet_var(
1482 lval_T *lp,
1483 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001484 exarg_T *eap,
1485 int deep UNUSED,
1486 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001487{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001488 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001489 int ret = OK;
1490 int cc;
1491
1492 if (lp->ll_tv == NULL)
1493 {
1494 cc = *name_end;
1495 *name_end = NUL;
1496
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001497 // Environment variable, normal name or expanded name.
1498 if (*lp->ll_name == '$')
1499 vim_unsetenv(lp->ll_name + 1);
1500 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001501 ret = FAIL;
1502 *name_end = cc;
1503 }
1504 else if ((lp->ll_list != NULL
1505 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
1506 || (lp->ll_dict != NULL
1507 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
1508 return FAIL;
1509 else if (lp->ll_range)
1510 {
1511 listitem_T *li;
1512 listitem_T *ll_li = lp->ll_li;
1513 int ll_n1 = lp->ll_n1;
1514
1515 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1516 {
1517 li = ll_li->li_next;
1518 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
1519 return FAIL;
1520 ll_li = li;
1521 ++ll_n1;
1522 }
1523
1524 // Delete a range of List items.
1525 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1526 {
1527 li = lp->ll_li->li_next;
1528 listitem_remove(lp->ll_list, lp->ll_li);
1529 lp->ll_li = li;
1530 ++lp->ll_n1;
1531 }
1532 }
1533 else
1534 {
1535 if (lp->ll_list != NULL)
1536 // unlet a List item.
1537 listitem_remove(lp->ll_list, lp->ll_li);
1538 else
1539 // unlet a Dictionary item.
1540 dictitem_remove(lp->ll_dict, lp->ll_di);
1541 }
1542
1543 return ret;
1544}
1545
1546/*
1547 * "unlet" a variable. Return OK if it existed, FAIL if not.
1548 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1549 */
1550 int
1551do_unlet(char_u *name, int forceit)
1552{
1553 hashtab_T *ht;
1554 hashitem_T *hi;
1555 char_u *varname;
1556 dict_T *d;
1557 dictitem_T *di;
1558
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001559 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1560 && check_vim9_unlet(name) == FAIL)
1561 return FAIL;
1562
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001563 ht = find_var_ht(name, &varname);
1564 if (ht != NULL && *varname != NUL)
1565 {
1566 d = get_current_funccal_dict(ht);
1567 if (d == NULL)
1568 {
1569 if (ht == &globvarht)
1570 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001571 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001572 d = &vimvardict;
1573 else
1574 {
1575 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1576 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1577 }
1578 if (d == NULL)
1579 {
1580 internal_error("do_unlet()");
1581 return FAIL;
1582 }
1583 }
1584 hi = hash_find(ht, varname);
1585 if (HASHITEM_EMPTY(hi))
1586 hi = find_hi_in_scoped_ht(name, &ht);
1587 if (hi != NULL && !HASHITEM_EMPTY(hi))
1588 {
1589 di = HI2DI(hi);
1590 if (var_check_fixed(di->di_flags, name, FALSE)
1591 || var_check_ro(di->di_flags, name, FALSE)
1592 || var_check_lock(d->dv_lock, name, FALSE))
1593 return FAIL;
1594
1595 delete_var(ht, hi);
1596 return OK;
1597 }
1598 }
1599 if (forceit)
1600 return OK;
1601 semsg(_("E108: No such variable: \"%s\""), name);
1602 return FAIL;
1603}
1604
1605/*
1606 * Lock or unlock variable indicated by "lp".
1607 * "deep" is the levels to go (-1 for unlimited);
1608 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1609 */
1610 static int
1611do_lock_var(
1612 lval_T *lp,
1613 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001614 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001615 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001616 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001617{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001618 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001619 int ret = OK;
1620 int cc;
1621 dictitem_T *di;
1622
1623 if (deep == 0) // nothing to do
1624 return OK;
1625
1626 if (lp->ll_tv == NULL)
1627 {
1628 cc = *name_end;
1629 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001630 if (*lp->ll_name == '$')
1631 {
1632 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001633 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001634 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001635 else
1636 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001637 // Normal name or expanded name.
1638 di = find_var(lp->ll_name, NULL, TRUE);
1639 if (di == NULL)
1640 ret = FAIL;
1641 else if ((di->di_flags & DI_FLAGS_FIX)
1642 && di->di_tv.v_type != VAR_DICT
1643 && di->di_tv.v_type != VAR_LIST)
1644 {
1645 // For historic reasons this error is not given for a list or
1646 // dict. E.g., the b: dict could be locked/unlocked.
1647 semsg(_(e_lock_unlock), lp->ll_name);
1648 ret = FAIL;
1649 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001650 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001651 {
1652 if (lock)
1653 di->di_flags |= DI_FLAGS_LOCK;
1654 else
1655 di->di_flags &= ~DI_FLAGS_LOCK;
1656 item_lock(&di->di_tv, deep, lock);
1657 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001658 }
1659 *name_end = cc;
1660 }
1661 else if (lp->ll_range)
1662 {
1663 listitem_T *li = lp->ll_li;
1664
1665 // (un)lock a range of List items.
1666 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1667 {
1668 item_lock(&li->li_tv, deep, lock);
1669 li = li->li_next;
1670 ++lp->ll_n1;
1671 }
1672 }
1673 else if (lp->ll_list != NULL)
1674 // (un)lock a List item.
1675 item_lock(&lp->ll_li->li_tv, deep, lock);
1676 else
1677 // (un)lock a Dictionary item.
1678 item_lock(&lp->ll_di->di_tv, deep, lock);
1679
1680 return ret;
1681}
1682
1683/*
1684 * Lock or unlock an item. "deep" is nr of levels to go.
1685 */
1686 static void
1687item_lock(typval_T *tv, int deep, int lock)
1688{
1689 static int recurse = 0;
1690 list_T *l;
1691 listitem_T *li;
1692 dict_T *d;
1693 blob_T *b;
1694 hashitem_T *hi;
1695 int todo;
1696
1697 if (recurse >= DICT_MAXNEST)
1698 {
1699 emsg(_("E743: variable nested too deep for (un)lock"));
1700 return;
1701 }
1702 if (deep == 0)
1703 return;
1704 ++recurse;
1705
1706 // lock/unlock the item itself
1707 if (lock)
1708 tv->v_lock |= VAR_LOCKED;
1709 else
1710 tv->v_lock &= ~VAR_LOCKED;
1711
1712 switch (tv->v_type)
1713 {
1714 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001715 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001717 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001719 case VAR_STRING:
1720 case VAR_FUNC:
1721 case VAR_PARTIAL:
1722 case VAR_FLOAT:
1723 case VAR_SPECIAL:
1724 case VAR_JOB:
1725 case VAR_CHANNEL:
1726 break;
1727
1728 case VAR_BLOB:
1729 if ((b = tv->vval.v_blob) != NULL)
1730 {
1731 if (lock)
1732 b->bv_lock |= VAR_LOCKED;
1733 else
1734 b->bv_lock &= ~VAR_LOCKED;
1735 }
1736 break;
1737 case VAR_LIST:
1738 if ((l = tv->vval.v_list) != NULL)
1739 {
1740 if (lock)
1741 l->lv_lock |= VAR_LOCKED;
1742 else
1743 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001744 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001745 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001746 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001747 item_lock(&li->li_tv, deep - 1, lock);
1748 }
1749 break;
1750 case VAR_DICT:
1751 if ((d = tv->vval.v_dict) != NULL)
1752 {
1753 if (lock)
1754 d->dv_lock |= VAR_LOCKED;
1755 else
1756 d->dv_lock &= ~VAR_LOCKED;
1757 if (deep < 0 || deep > 1)
1758 {
1759 // recursive: lock/unlock the items the List contains
1760 todo = (int)d->dv_hashtab.ht_used;
1761 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1762 {
1763 if (!HASHITEM_EMPTY(hi))
1764 {
1765 --todo;
1766 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
1767 }
1768 }
1769 }
1770 }
1771 }
1772 --recurse;
1773}
1774
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001775#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1776/*
1777 * Delete all "menutrans_" variables.
1778 */
1779 void
1780del_menutrans_vars(void)
1781{
1782 hashitem_T *hi;
1783 int todo;
1784
1785 hash_lock(&globvarht);
1786 todo = (int)globvarht.ht_used;
1787 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1788 {
1789 if (!HASHITEM_EMPTY(hi))
1790 {
1791 --todo;
1792 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1793 delete_var(&globvarht, hi);
1794 }
1795 }
1796 hash_unlock(&globvarht);
1797}
1798#endif
1799
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001800/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001801 * Local string buffer for the next two functions to store a variable name
1802 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1803 * get_user_var_name().
1804 */
1805
1806static char_u *varnamebuf = NULL;
1807static int varnamebuflen = 0;
1808
1809/*
1810 * Function to concatenate a prefix and a variable name.
1811 */
1812 static char_u *
1813cat_prefix_varname(int prefix, char_u *name)
1814{
1815 int len;
1816
1817 len = (int)STRLEN(name) + 3;
1818 if (len > varnamebuflen)
1819 {
1820 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001821 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001822 varnamebuf = alloc(len);
1823 if (varnamebuf == NULL)
1824 {
1825 varnamebuflen = 0;
1826 return NULL;
1827 }
1828 varnamebuflen = len;
1829 }
1830 *varnamebuf = prefix;
1831 varnamebuf[1] = ':';
1832 STRCPY(varnamebuf + 2, name);
1833 return varnamebuf;
1834}
1835
1836/*
1837 * Function given to ExpandGeneric() to obtain the list of user defined
1838 * (global/buffer/window/built-in) variable names.
1839 */
1840 char_u *
1841get_user_var_name(expand_T *xp, int idx)
1842{
1843 static long_u gdone;
1844 static long_u bdone;
1845 static long_u wdone;
1846 static long_u tdone;
1847 static int vidx;
1848 static hashitem_T *hi;
1849 hashtab_T *ht;
1850
1851 if (idx == 0)
1852 {
1853 gdone = bdone = wdone = vidx = 0;
1854 tdone = 0;
1855 }
1856
1857 // Global variables
1858 if (gdone < globvarht.ht_used)
1859 {
1860 if (gdone++ == 0)
1861 hi = globvarht.ht_array;
1862 else
1863 ++hi;
1864 while (HASHITEM_EMPTY(hi))
1865 ++hi;
1866 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1867 return cat_prefix_varname('g', hi->hi_key);
1868 return hi->hi_key;
1869 }
1870
1871 // b: variables
1872 ht = &curbuf->b_vars->dv_hashtab;
1873 if (bdone < ht->ht_used)
1874 {
1875 if (bdone++ == 0)
1876 hi = ht->ht_array;
1877 else
1878 ++hi;
1879 while (HASHITEM_EMPTY(hi))
1880 ++hi;
1881 return cat_prefix_varname('b', hi->hi_key);
1882 }
1883
1884 // w: variables
1885 ht = &curwin->w_vars->dv_hashtab;
1886 if (wdone < ht->ht_used)
1887 {
1888 if (wdone++ == 0)
1889 hi = ht->ht_array;
1890 else
1891 ++hi;
1892 while (HASHITEM_EMPTY(hi))
1893 ++hi;
1894 return cat_prefix_varname('w', hi->hi_key);
1895 }
1896
1897 // t: variables
1898 ht = &curtab->tp_vars->dv_hashtab;
1899 if (tdone < ht->ht_used)
1900 {
1901 if (tdone++ == 0)
1902 hi = ht->ht_array;
1903 else
1904 ++hi;
1905 while (HASHITEM_EMPTY(hi))
1906 ++hi;
1907 return cat_prefix_varname('t', hi->hi_key);
1908 }
1909
1910 // v: variables
1911 if (vidx < VV_LEN)
1912 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
1913
1914 VIM_CLEAR(varnamebuf);
1915 varnamebuflen = 0;
1916 return NULL;
1917}
1918
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001919 char *
1920get_var_special_name(int nr)
1921{
1922 switch (nr)
1923 {
1924 case VVAL_FALSE: return "v:false";
1925 case VVAL_TRUE: return "v:true";
1926 case VVAL_NONE: return "v:none";
1927 case VVAL_NULL: return "v:null";
1928 }
1929 internal_error("get_var_special_name()");
1930 return "42";
1931}
1932
1933/*
1934 * Returns the global variable dictionary
1935 */
1936 dict_T *
1937get_globvar_dict(void)
1938{
1939 return &globvardict;
1940}
1941
1942/*
1943 * Returns the global variable hash table
1944 */
1945 hashtab_T *
1946get_globvar_ht(void)
1947{
1948 return &globvarht;
1949}
1950
1951/*
1952 * Returns the v: variable dictionary
1953 */
1954 dict_T *
1955get_vimvar_dict(void)
1956{
1957 return &vimvardict;
1958}
1959
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001960/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001962 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001963 */
1964 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001965find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001967 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
1968 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001969
1970 if (di == NULL)
1971 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001972 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001973 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
1974 return (int)(vv - vimvars);
1975}
1976
1977
1978/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02001979 * Set type of v: variable to "type".
1980 */
1981 void
1982set_vim_var_type(int idx, vartype_T type)
1983{
1984 vimvars[idx].vv_type = type;
1985}
1986
1987/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001988 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001989 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001990 */
1991 void
1992set_vim_var_nr(int idx, varnumber_T val)
1993{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001994 vimvars[idx].vv_nr = val;
1995}
1996
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 char *
1998get_vim_var_name(int idx)
1999{
2000 return vimvars[idx].vv_name;
2001}
2002
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002003/*
2004 * Get typval_T v: variable value.
2005 */
2006 typval_T *
2007get_vim_var_tv(int idx)
2008{
2009 return &vimvars[idx].vv_tv;
2010}
2011
2012/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002013 * Set v: variable to "tv". Only accepts the same type.
2014 * Takes over the value of "tv".
2015 */
2016 int
2017set_vim_var_tv(int idx, typval_T *tv)
2018{
2019 if (vimvars[idx].vv_type != tv->v_type)
2020 {
2021 emsg(_("E1063: type mismatch for v: variable"));
2022 clear_tv(tv);
2023 return FAIL;
2024 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002025 // VV_RO is also checked when compiling, but let's check here as well.
2026 if (vimvars[idx].vv_flags & VV_RO)
2027 {
2028 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2029 return FAIL;
2030 }
2031 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2032 {
2033 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2034 return FAIL;
2035 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002036 clear_tv(&vimvars[idx].vv_di.di_tv);
2037 vimvars[idx].vv_di.di_tv = *tv;
2038 return OK;
2039}
2040
2041/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002042 * Get number v: variable value.
2043 */
2044 varnumber_T
2045get_vim_var_nr(int idx)
2046{
2047 return vimvars[idx].vv_nr;
2048}
2049
2050/*
2051 * Get string v: variable value. Uses a static buffer, can only be used once.
2052 * If the String variable has never been set, return an empty string.
2053 * Never returns NULL;
2054 */
2055 char_u *
2056get_vim_var_str(int idx)
2057{
2058 return tv_get_string(&vimvars[idx].vv_tv);
2059}
2060
2061/*
2062 * Get List v: variable value. Caller must take care of reference count when
2063 * needed.
2064 */
2065 list_T *
2066get_vim_var_list(int idx)
2067{
2068 return vimvars[idx].vv_list;
2069}
2070
2071/*
2072 * Get Dict v: variable value. Caller must take care of reference count when
2073 * needed.
2074 */
2075 dict_T *
2076get_vim_var_dict(int idx)
2077{
2078 return vimvars[idx].vv_dict;
2079}
2080
2081/*
2082 * Set v:char to character "c".
2083 */
2084 void
2085set_vim_var_char(int c)
2086{
2087 char_u buf[MB_MAXBYTES + 1];
2088
2089 if (has_mbyte)
2090 buf[(*mb_char2bytes)(c, buf)] = NUL;
2091 else
2092 {
2093 buf[0] = c;
2094 buf[1] = NUL;
2095 }
2096 set_vim_var_string(VV_CHAR, buf, -1);
2097}
2098
2099/*
2100 * Set v:count to "count" and v:count1 to "count1".
2101 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2102 */
2103 void
2104set_vcount(
2105 long count,
2106 long count1,
2107 int set_prevcount)
2108{
2109 if (set_prevcount)
2110 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2111 vimvars[VV_COUNT].vv_nr = count;
2112 vimvars[VV_COUNT1].vv_nr = count1;
2113}
2114
2115/*
2116 * Save variables that might be changed as a side effect. Used when executing
2117 * a timer callback.
2118 */
2119 void
2120save_vimvars(vimvars_save_T *vvsave)
2121{
2122 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2123 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2124 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2125}
2126
2127/*
2128 * Restore variables saved by save_vimvars().
2129 */
2130 void
2131restore_vimvars(vimvars_save_T *vvsave)
2132{
2133 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2134 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2135 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2136}
2137
2138/*
2139 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2140 * value.
2141 */
2142 void
2143set_vim_var_string(
2144 int idx,
2145 char_u *val,
2146 int len) // length of "val" to use or -1 (whole string)
2147{
2148 clear_tv(&vimvars[idx].vv_di.di_tv);
2149 vimvars[idx].vv_type = VAR_STRING;
2150 if (val == NULL)
2151 vimvars[idx].vv_str = NULL;
2152 else if (len == -1)
2153 vimvars[idx].vv_str = vim_strsave(val);
2154 else
2155 vimvars[idx].vv_str = vim_strnsave(val, len);
2156}
2157
2158/*
2159 * Set List v: variable to "val".
2160 */
2161 void
2162set_vim_var_list(int idx, list_T *val)
2163{
2164 clear_tv(&vimvars[idx].vv_di.di_tv);
2165 vimvars[idx].vv_type = VAR_LIST;
2166 vimvars[idx].vv_list = val;
2167 if (val != NULL)
2168 ++val->lv_refcount;
2169}
2170
2171/*
2172 * Set Dictionary v: variable to "val".
2173 */
2174 void
2175set_vim_var_dict(int idx, dict_T *val)
2176{
2177 clear_tv(&vimvars[idx].vv_di.di_tv);
2178 vimvars[idx].vv_type = VAR_DICT;
2179 vimvars[idx].vv_dict = val;
2180 if (val != NULL)
2181 {
2182 ++val->dv_refcount;
2183 dict_set_items_ro(val);
2184 }
2185}
2186
2187/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002188 * Set the v:argv list.
2189 */
2190 void
2191set_argv_var(char **argv, int argc)
2192{
2193 list_T *l = list_alloc();
2194 int i;
2195
2196 if (l == NULL)
2197 getout(1);
2198 l->lv_lock = VAR_FIXED;
2199 for (i = 0; i < argc; ++i)
2200 {
2201 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2202 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002203 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002204 }
2205 set_vim_var_list(VV_ARGV, l);
2206}
2207
2208/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002209 * Set v:register if needed.
2210 */
2211 void
2212set_reg_var(int c)
2213{
2214 char_u regname;
2215
2216 if (c == 0 || c == ' ')
2217 regname = '"';
2218 else
2219 regname = c;
2220 // Avoid free/alloc when the value is already right.
2221 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2222 set_vim_var_string(VV_REG, &regname, 1);
2223}
2224
2225/*
2226 * Get or set v:exception. If "oldval" == NULL, return the current value.
2227 * Otherwise, restore the value to "oldval" and return NULL.
2228 * Must always be called in pairs to save and restore v:exception! Does not
2229 * take care of memory allocations.
2230 */
2231 char_u *
2232v_exception(char_u *oldval)
2233{
2234 if (oldval == NULL)
2235 return vimvars[VV_EXCEPTION].vv_str;
2236
2237 vimvars[VV_EXCEPTION].vv_str = oldval;
2238 return NULL;
2239}
2240
2241/*
2242 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2243 * Otherwise, restore the value to "oldval" and return NULL.
2244 * Must always be called in pairs to save and restore v:throwpoint! Does not
2245 * take care of memory allocations.
2246 */
2247 char_u *
2248v_throwpoint(char_u *oldval)
2249{
2250 if (oldval == NULL)
2251 return vimvars[VV_THROWPOINT].vv_str;
2252
2253 vimvars[VV_THROWPOINT].vv_str = oldval;
2254 return NULL;
2255}
2256
2257/*
2258 * Set v:cmdarg.
2259 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2260 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2261 * Must always be called in pairs!
2262 */
2263 char_u *
2264set_cmdarg(exarg_T *eap, char_u *oldarg)
2265{
2266 char_u *oldval;
2267 char_u *newval;
2268 unsigned len;
2269
2270 oldval = vimvars[VV_CMDARG].vv_str;
2271 if (eap == NULL)
2272 {
2273 vim_free(oldval);
2274 vimvars[VV_CMDARG].vv_str = oldarg;
2275 return NULL;
2276 }
2277
2278 if (eap->force_bin == FORCE_BIN)
2279 len = 6;
2280 else if (eap->force_bin == FORCE_NOBIN)
2281 len = 8;
2282 else
2283 len = 0;
2284
2285 if (eap->read_edit)
2286 len += 7;
2287
2288 if (eap->force_ff != 0)
2289 len += 10; // " ++ff=unix"
2290 if (eap->force_enc != 0)
2291 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2292 if (eap->bad_char != 0)
2293 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2294
2295 newval = alloc(len + 1);
2296 if (newval == NULL)
2297 return NULL;
2298
2299 if (eap->force_bin == FORCE_BIN)
2300 sprintf((char *)newval, " ++bin");
2301 else if (eap->force_bin == FORCE_NOBIN)
2302 sprintf((char *)newval, " ++nobin");
2303 else
2304 *newval = NUL;
2305
2306 if (eap->read_edit)
2307 STRCAT(newval, " ++edit");
2308
2309 if (eap->force_ff != 0)
2310 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2311 eap->force_ff == 'u' ? "unix"
2312 : eap->force_ff == 'd' ? "dos"
2313 : "mac");
2314 if (eap->force_enc != 0)
2315 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2316 eap->cmd + eap->force_enc);
2317 if (eap->bad_char == BAD_KEEP)
2318 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2319 else if (eap->bad_char == BAD_DROP)
2320 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2321 else if (eap->bad_char != 0)
2322 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2323 vimvars[VV_CMDARG].vv_str = newval;
2324 return oldval;
2325}
2326
2327/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002328 * Get the value of internal variable "name".
2329 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2330 */
2331 int
2332get_var_tv(
2333 char_u *name,
2334 int len, // length of "name"
2335 typval_T *rettv, // NULL when only checking existence
2336 dictitem_T **dip, // non-NULL when typval's dict item is needed
2337 int verbose, // may give error message
2338 int no_autoload) // do not use script autoloading
2339{
2340 int ret = OK;
2341 typval_T *tv = NULL;
2342 dictitem_T *v;
2343 int cc;
2344
2345 // truncate the name, so that we can use strcmp()
2346 cc = name[len];
2347 name[len] = NUL;
2348
2349 // Check for user-defined variables.
2350 v = find_var(name, NULL, no_autoload);
2351 if (v != NULL)
2352 {
2353 tv = &v->di_tv;
2354 if (dip != NULL)
2355 *dip = v;
2356 }
2357
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002358 if (tv == NULL && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2359 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002360 imported_T *import = find_imported(name, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361
2362 // imported variable from another script
2363 if (import != NULL)
2364 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002365 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002366 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2367 + import->imp_var_vals_idx;
2368 tv = sv->sv_tv;
2369 }
2370 }
2371
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002372 if (tv == NULL)
2373 {
2374 if (rettv != NULL && verbose)
2375 semsg(_(e_undefvar), name);
2376 ret = FAIL;
2377 }
2378 else if (rettv != NULL)
2379 copy_tv(tv, rettv);
2380
2381 name[len] = cc;
2382
2383 return ret;
2384}
2385
2386/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002387 * Check if variable "name[len]" is a local variable or an argument.
2388 * If so, "*eval_lavars_used" is set to TRUE.
2389 */
2390 void
2391check_vars(char_u *name, int len)
2392{
2393 int cc;
2394 char_u *varname;
2395 hashtab_T *ht;
2396
2397 if (eval_lavars_used == NULL)
2398 return;
2399
2400 // truncate the name, so that we can use strcmp()
2401 cc = name[len];
2402 name[len] = NUL;
2403
2404 ht = find_var_ht(name, &varname);
2405 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2406 {
2407 if (find_var(name, NULL, TRUE) != NULL)
2408 *eval_lavars_used = TRUE;
2409 }
2410
2411 name[len] = cc;
2412}
2413
2414/*
2415 * Find variable "name" in the list of variables.
2416 * Return a pointer to it if found, NULL if not found.
2417 * Careful: "a:0" variables don't have a name.
2418 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2419 * hashtab_T used.
2420 */
2421 dictitem_T *
2422find_var(char_u *name, hashtab_T **htp, int no_autoload)
2423{
2424 char_u *varname;
2425 hashtab_T *ht;
2426 dictitem_T *ret = NULL;
2427
2428 ht = find_var_ht(name, &varname);
2429 if (htp != NULL)
2430 *htp = ht;
2431 if (ht == NULL)
2432 return NULL;
2433 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2434 if (ret != NULL)
2435 return ret;
2436
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002437 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002438 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2439}
2440
2441/*
2442 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002443 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002444 * Returns NULL if not found.
2445 */
2446 dictitem_T *
2447find_var_in_ht(
2448 hashtab_T *ht,
2449 int htname,
2450 char_u *varname,
2451 int no_autoload)
2452{
2453 hashitem_T *hi;
2454
2455 if (*varname == NUL)
2456 {
2457 // Must be something like "s:", otherwise "ht" would be NULL.
2458 switch (htname)
2459 {
2460 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2461 case 'g': return &globvars_var;
2462 case 'v': return &vimvars_var;
2463 case 'b': return &curbuf->b_bufvar;
2464 case 'w': return &curwin->w_winvar;
2465 case 't': return &curtab->tp_winvar;
2466 case 'l': return get_funccal_local_var();
2467 case 'a': return get_funccal_args_var();
2468 }
2469 return NULL;
2470 }
2471
2472 hi = hash_find(ht, varname);
2473 if (HASHITEM_EMPTY(hi))
2474 {
2475 // For global variables we may try auto-loading the script. If it
2476 // worked find the variable again. Don't auto-load a script if it was
2477 // loaded already, otherwise it would be loaded every time when
2478 // checking if a function name is a Funcref variable.
2479 if (ht == &globvarht && !no_autoload)
2480 {
2481 // Note: script_autoload() may make "hi" invalid. It must either
2482 // be obtained again or not used.
2483 if (!script_autoload(varname, FALSE) || aborting())
2484 return NULL;
2485 hi = hash_find(ht, varname);
2486 }
2487 if (HASHITEM_EMPTY(hi))
2488 return NULL;
2489 }
2490 return HI2DI(hi);
2491}
2492
2493/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494 * Get the script-local hashtab. NULL if not in a script context.
2495 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02002496 static hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497get_script_local_ht(void)
2498{
2499 scid_T sid = current_sctx.sc_sid;
2500
2501 if (sid > 0 && sid <= script_items.ga_len)
2502 return &SCRIPT_VARS(sid);
2503 return NULL;
2504}
2505
2506/*
2507 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002508 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002510 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002511lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2512{
2513 hashtab_T *ht = get_script_local_ht();
2514 char_u buffer[30];
2515 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002516 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 hashitem_T *hi;
2518
2519 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002520 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 if (len < sizeof(buffer) - 1)
2522 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002523 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524 vim_strncpy(buffer, name, len);
2525 p = buffer;
2526 }
2527 else
2528 {
2529 p = vim_strnsave(name, (int)len);
2530 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002531 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 }
2533
2534 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002535 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536
2537 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002538 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2539 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540
2541 if (p != buffer)
2542 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002543 // Don't return "buffer", gcc complains.
2544 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545}
2546
2547/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002548 * Find the hashtab used for a variable name.
2549 * Return NULL if the name is not valid.
2550 * Set "varname" to the start of name without ':'.
2551 */
2552 hashtab_T *
2553find_var_ht(char_u *name, char_u **varname)
2554{
2555 hashitem_T *hi;
2556 hashtab_T *ht;
2557
2558 if (name[0] == NUL)
2559 return NULL;
2560 if (name[1] != ':')
2561 {
2562 // The name must not start with a colon or #.
2563 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2564 return NULL;
2565 *varname = name;
2566
2567 // "version" is "v:version" in all scopes if scriptversion < 3.
2568 // Same for a few other variables marked with VV_COMPAT.
2569 if (current_sctx.sc_version < 3)
2570 {
2571 hi = hash_find(&compat_hashtab, name);
2572 if (!HASHITEM_EMPTY(hi))
2573 return &compat_hashtab;
2574 }
2575
2576 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 if (ht != NULL)
2578 return ht; // local variable
2579
2580 // in Vim9 script items at the script level are script-local
2581 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2582 {
2583 ht = get_script_local_ht();
2584 if (ht != NULL)
2585 return ht;
2586 }
2587
2588 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002589 }
2590 *varname = name + 2;
2591 if (*name == 'g') // global variable
2592 return &globvarht;
2593 // There must be no ':' or '#' in the rest of the name, unless g: is used
2594 if (vim_strchr(name + 2, ':') != NULL
2595 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2596 return NULL;
2597 if (*name == 'b') // buffer variable
2598 return &curbuf->b_vars->dv_hashtab;
2599 if (*name == 'w') // window variable
2600 return &curwin->w_vars->dv_hashtab;
2601 if (*name == 't') // tab page variable
2602 return &curtab->tp_vars->dv_hashtab;
2603 if (*name == 'v') // v: variable
2604 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002605 if (get_current_funccal() != NULL
Bram Moolenaar822ba242020-05-24 23:00:18 +02002606 && get_current_funccal()->func->uf_dfunc_idx == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002607 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002608 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 if (*name == 'a') // a: function argument
2610 return get_funccal_args_ht();
2611 if (*name == 'l') // l: local function variable
2612 return get_funccal_local_ht();
2613 }
2614 if (*name == 's') // script variable
2615 {
2616 ht = get_script_local_ht();
2617 if (ht != NULL)
2618 return ht;
2619 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002620 return NULL;
2621}
2622
2623/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002624 * Get the string value of a (global/local) variable.
2625 * Note: see tv_get_string() for how long the pointer remains valid.
2626 * Returns NULL when it doesn't exist.
2627 */
2628 char_u *
2629get_var_value(char_u *name)
2630{
2631 dictitem_T *v;
2632
2633 v = find_var(name, NULL, FALSE);
2634 if (v == NULL)
2635 return NULL;
2636 return tv_get_string(&v->di_tv);
2637}
2638
2639/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002640 * Allocate a new hashtab for a sourced script. It will be used while
2641 * sourcing this script and when executing functions defined in the script.
2642 */
2643 void
2644new_script_vars(scid_T id)
2645{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002646 scriptvar_T *sv;
2647
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002648 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2649 if (sv == NULL)
2650 return;
2651 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002652 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002653}
2654
2655/*
2656 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2657 * point to it.
2658 */
2659 void
2660init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2661{
2662 hash_init(&dict->dv_hashtab);
2663 dict->dv_lock = 0;
2664 dict->dv_scope = scope;
2665 dict->dv_refcount = DO_NOT_FREE_CNT;
2666 dict->dv_copyID = 0;
2667 dict_var->di_tv.vval.v_dict = dict;
2668 dict_var->di_tv.v_type = VAR_DICT;
2669 dict_var->di_tv.v_lock = VAR_FIXED;
2670 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2671 dict_var->di_key[0] = NUL;
2672}
2673
2674/*
2675 * Unreference a dictionary initialized by init_var_dict().
2676 */
2677 void
2678unref_var_dict(dict_T *dict)
2679{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002680 // Now the dict needs to be freed if no one else is using it, go back to
2681 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002682 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2683 dict_unref(dict);
2684}
2685
2686/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002687 * Clean up a list of internal variables.
2688 * Frees all allocated variables and the value they contain.
2689 * Clears hashtab "ht", does not free it.
2690 */
2691 void
2692vars_clear(hashtab_T *ht)
2693{
2694 vars_clear_ext(ht, TRUE);
2695}
2696
2697/*
2698 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2699 */
2700 void
2701vars_clear_ext(hashtab_T *ht, int free_val)
2702{
2703 int todo;
2704 hashitem_T *hi;
2705 dictitem_T *v;
2706
2707 hash_lock(ht);
2708 todo = (int)ht->ht_used;
2709 for (hi = ht->ht_array; todo > 0; ++hi)
2710 {
2711 if (!HASHITEM_EMPTY(hi))
2712 {
2713 --todo;
2714
2715 // Free the variable. Don't remove it from the hashtab,
2716 // ht_array might change then. hash_clear() takes care of it
2717 // later.
2718 v = HI2DI(hi);
2719 if (free_val)
2720 clear_tv(&v->di_tv);
2721 if (v->di_flags & DI_FLAGS_ALLOC)
2722 vim_free(v);
2723 }
2724 }
2725 hash_clear(ht);
2726 ht->ht_used = 0;
2727}
2728
2729/*
2730 * Delete a variable from hashtab "ht" at item "hi".
2731 * Clear the variable value and free the dictitem.
2732 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002733 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002734delete_var(hashtab_T *ht, hashitem_T *hi)
2735{
2736 dictitem_T *di = HI2DI(hi);
2737
2738 hash_remove(ht, hi);
2739 clear_tv(&di->di_tv);
2740 vim_free(di);
2741}
2742
2743/*
2744 * List the value of one internal variable.
2745 */
2746 static void
2747list_one_var(dictitem_T *v, char *prefix, int *first)
2748{
2749 char_u *tofree;
2750 char_u *s;
2751 char_u numbuf[NUMBUFLEN];
2752
2753 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2754 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2755 s == NULL ? (char_u *)"" : s, first);
2756 vim_free(tofree);
2757}
2758
2759 static void
2760list_one_var_a(
2761 char *prefix,
2762 char_u *name,
2763 int type,
2764 char_u *string,
2765 int *first) // when TRUE clear rest of screen and set to FALSE
2766{
2767 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2768 msg_start();
2769 msg_puts(prefix);
2770 if (name != NULL) // "a:" vars don't have a name stored
2771 msg_puts((char *)name);
2772 msg_putchar(' ');
2773 msg_advance(22);
2774 if (type == VAR_NUMBER)
2775 msg_putchar('#');
2776 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2777 msg_putchar('*');
2778 else if (type == VAR_LIST)
2779 {
2780 msg_putchar('[');
2781 if (*string == '[')
2782 ++string;
2783 }
2784 else if (type == VAR_DICT)
2785 {
2786 msg_putchar('{');
2787 if (*string == '{')
2788 ++string;
2789 }
2790 else
2791 msg_putchar(' ');
2792
2793 msg_outtrans(string);
2794
2795 if (type == VAR_FUNC || type == VAR_PARTIAL)
2796 msg_puts("()");
2797 if (*first)
2798 {
2799 msg_clr_eos();
2800 *first = FALSE;
2801 }
2802}
2803
2804/*
2805 * Set variable "name" to value in "tv".
2806 * If the variable already exists, the value is updated.
2807 * Otherwise the variable is created.
2808 */
2809 void
2810set_var(
2811 char_u *name,
2812 typval_T *tv,
2813 int copy) // make copy of value in "tv"
2814{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 set_var_const(name, NULL, tv, copy, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002816}
2817
2818/*
2819 * Set variable "name" to value in "tv".
2820 * If the variable already exists and "is_const" is FALSE the value is updated.
2821 * Otherwise the variable is created.
2822 */
2823 void
2824set_var_const(
2825 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826 type_T *type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002827 typval_T *tv,
2828 int copy, // make copy of value in "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 int flags) // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002830{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002832 char_u *varname;
2833 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834 int is_script_local;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002835
2836 ht = find_var_ht(name, &varname);
2837 if (ht == NULL || *varname == NUL)
2838 {
2839 semsg(_(e_illvar), name);
2840 return;
2841 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 is_script_local = ht == get_script_local_ht();
2843
2844 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002845
2846 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847 if (di == NULL)
2848 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002849
2850 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 && var_check_func_name(name, di == NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002852 return;
2853
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002855 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002857 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 if (flags & LET_IS_CONST)
2859 {
2860 emsg(_(e_cannot_mod));
2861 return;
2862 }
2863
2864 if (var_check_ro(di->di_flags, name, FALSE)
2865 || var_check_lock(di->di_tv.v_lock, name, FALSE))
2866 return;
2867
2868 if ((flags & LET_NO_COMMAND) == 0
2869 && is_script_local
2870 && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2871 {
2872 semsg(_("E1041: Redefining script item %s"), name);
2873 return;
2874 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002875 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876 else
2877 // can only redefine once
2878 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002879
2880 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002881
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002883 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002884 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002885 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002887 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002889 if (copy || tv->v_type != VAR_STRING)
2890 {
2891 char_u *val = tv_get_string(tv);
2892
2893 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002894 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002895 if (di->di_tv.vval.v_string == NULL)
2896 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002897 }
2898 else
2899 {
2900 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002902 tv->vval.v_string = NULL;
2903 }
2904 return;
2905 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002907 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002909 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002911#ifdef FEAT_SEARCH_EXTRA
2912 else if (STRCMP(varname, "hlsearch") == 0)
2913 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002915 redraw_all_later(SOME_VALID);
2916 }
2917#endif
2918 return;
2919 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002921 {
2922 semsg(_("E963: setting %s to value with wrong type"), name);
2923 return;
2924 }
2925 }
2926
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002928 }
2929 else // add a new variable
2930 {
2931 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002932 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002933 {
2934 semsg(_(e_illvar), name);
2935 return;
2936 }
2937
2938 // Make sure the variable name is valid.
2939 if (!valid_varname(varname))
2940 return;
2941
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
2943 if (di == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002944 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 STRCPY(di->di_key, varname);
2946 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002947 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002949 return;
2950 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 di->di_flags = DI_FLAGS_ALLOC;
2952 if (flags & LET_IS_CONST)
2953 di->di_flags |= DI_FLAGS_LOCK;
2954
2955 if (is_script_local && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2956 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002957 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958
2959 // Store a pointer to the typval_T, so that it can be found by
2960 // index instead of using a hastab lookup.
2961 if (ga_grow(&si->sn_var_vals, 1) == OK)
2962 {
2963 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2964 + si->sn_var_vals.ga_len;
2965 sv->sv_name = di->di_key;
2966 sv->sv_tv = &di->di_tv;
2967 sv->sv_type = type == NULL ? &t_any : type;
2968 sv->sv_const = (flags & LET_IS_CONST);
2969 sv->sv_export = is_export;
2970 ++si->sn_var_vals.ga_len;
2971
2972 // let ex_export() know the export worked.
2973 is_export = FALSE;
2974 }
2975 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002976 }
2977
2978 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002980 else
2981 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 di->di_tv = *tv;
2983 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002984 init_tv(tv);
2985 }
2986
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 if (flags & LET_IS_CONST)
2988 di->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002989}
2990
2991/*
2992 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
2993 * Also give an error message.
2994 */
2995 int
2996var_check_ro(int flags, char_u *name, int use_gettext)
2997{
2998 if (flags & DI_FLAGS_RO)
2999 {
3000 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3001 return TRUE;
3002 }
3003 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3004 {
3005 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3006 return TRUE;
3007 }
3008 return FALSE;
3009}
3010
3011/*
3012 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3013 * Also give an error message.
3014 */
3015 int
3016var_check_fixed(int flags, char_u *name, int use_gettext)
3017{
3018 if (flags & DI_FLAGS_FIX)
3019 {
3020 semsg(_("E795: Cannot delete variable %s"),
3021 use_gettext ? (char_u *)_(name) : name);
3022 return TRUE;
3023 }
3024 return FALSE;
3025}
3026
3027/*
3028 * Check if a funcref is assigned to a valid variable name.
3029 * Return TRUE and give an error if not.
3030 */
3031 int
3032var_check_func_name(
3033 char_u *name, // points to start of variable name
3034 int new_var) // TRUE when creating the variable
3035{
3036 // Allow for w: b: s: and t:.
3037 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3038 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3039 ? name[2] : name[0]))
3040 {
3041 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3042 name);
3043 return TRUE;
3044 }
3045 // Don't allow hiding a function. When "v" is not NULL we might be
3046 // assigning another function to the same var, the type is checked
3047 // below.
3048 if (new_var && function_exists(name, FALSE))
3049 {
3050 semsg(_("E705: Variable name conflicts with existing function: %s"),
3051 name);
3052 return TRUE;
3053 }
3054 return FALSE;
3055}
3056
3057/*
3058 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
3059 * Also give an error message, using "name" or _("name") when use_gettext is
3060 * TRUE.
3061 */
3062 int
3063var_check_lock(int lock, char_u *name, int use_gettext)
3064{
3065 if (lock & VAR_LOCKED)
3066 {
3067 semsg(_("E741: Value is locked: %s"),
3068 name == NULL ? (char_u *)_("Unknown")
3069 : use_gettext ? (char_u *)_(name)
3070 : name);
3071 return TRUE;
3072 }
3073 if (lock & VAR_FIXED)
3074 {
3075 semsg(_("E742: Cannot change value of %s"),
3076 name == NULL ? (char_u *)_("Unknown")
3077 : use_gettext ? (char_u *)_(name)
3078 : name);
3079 return TRUE;
3080 }
3081 return FALSE;
3082}
3083
3084/*
3085 * Check if a variable name is valid.
3086 * Return FALSE and give an error if not.
3087 */
3088 int
3089valid_varname(char_u *varname)
3090{
3091 char_u *p;
3092
3093 for (p = varname; *p != NUL; ++p)
3094 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3095 && *p != AUTOLOAD_CHAR)
3096 {
3097 semsg(_(e_illvar), varname);
3098 return FALSE;
3099 }
3100 return TRUE;
3101}
3102
3103/*
3104 * getwinvar() and gettabwinvar()
3105 */
3106 static void
3107getwinvar(
3108 typval_T *argvars,
3109 typval_T *rettv,
3110 int off) // 1 for gettabwinvar()
3111{
3112 win_T *win;
3113 char_u *varname;
3114 dictitem_T *v;
3115 tabpage_T *tp = NULL;
3116 int done = FALSE;
3117 win_T *oldcurwin;
3118 tabpage_T *oldtabpage;
3119 int need_switch_win;
3120
3121 if (off == 1)
3122 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3123 else
3124 tp = curtab;
3125 win = find_win_by_nr(&argvars[off], tp);
3126 varname = tv_get_string_chk(&argvars[off + 1]);
3127 ++emsg_off;
3128
3129 rettv->v_type = VAR_STRING;
3130 rettv->vval.v_string = NULL;
3131
3132 if (win != NULL && varname != NULL)
3133 {
3134 // Set curwin to be our win, temporarily. Also set the tabpage,
3135 // otherwise the window is not valid. Only do this when needed,
3136 // autocommands get blocked.
3137 need_switch_win = !(tp == curtab && win == curwin);
3138 if (!need_switch_win
3139 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3140 {
3141 if (*varname == '&')
3142 {
3143 if (varname[1] == NUL)
3144 {
3145 // get all window-local options in a dict
3146 dict_T *opts = get_winbuf_options(FALSE);
3147
3148 if (opts != NULL)
3149 {
3150 rettv_dict_set(rettv, opts);
3151 done = TRUE;
3152 }
3153 }
3154 else if (get_option_tv(&varname, rettv, 1) == OK)
3155 // window-local-option
3156 done = TRUE;
3157 }
3158 else
3159 {
3160 // Look up the variable.
3161 // Let getwinvar({nr}, "") return the "w:" dictionary.
3162 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3163 varname, FALSE);
3164 if (v != NULL)
3165 {
3166 copy_tv(&v->di_tv, rettv);
3167 done = TRUE;
3168 }
3169 }
3170 }
3171
3172 if (need_switch_win)
3173 // restore previous notion of curwin
3174 restore_win(oldcurwin, oldtabpage, TRUE);
3175 }
3176
3177 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3178 // use the default return value
3179 copy_tv(&argvars[off + 2], rettv);
3180
3181 --emsg_off;
3182}
3183
3184/*
3185 * "setwinvar()" and "settabwinvar()" functions
3186 */
3187 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003188setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003189{
3190 win_T *win;
3191 win_T *save_curwin;
3192 tabpage_T *save_curtab;
3193 int need_switch_win;
3194 char_u *varname, *winvarname;
3195 typval_T *varp;
3196 char_u nbuf[NUMBUFLEN];
3197 tabpage_T *tp = NULL;
3198
3199 if (check_secure())
3200 return;
3201
3202 if (off == 1)
3203 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3204 else
3205 tp = curtab;
3206 win = find_win_by_nr(&argvars[off], tp);
3207 varname = tv_get_string_chk(&argvars[off + 1]);
3208 varp = &argvars[off + 2];
3209
3210 if (win != NULL && varname != NULL && varp != NULL)
3211 {
3212 need_switch_win = !(tp == curtab && win == curwin);
3213 if (!need_switch_win
3214 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3215 {
3216 if (*varname == '&')
3217 {
3218 long numval;
3219 char_u *strval;
3220 int error = FALSE;
3221
3222 ++varname;
3223 numval = (long)tv_get_number_chk(varp, &error);
3224 strval = tv_get_string_buf_chk(varp, nbuf);
3225 if (!error && strval != NULL)
3226 set_option_value(varname, numval, strval, OPT_LOCAL);
3227 }
3228 else
3229 {
3230 winvarname = alloc(STRLEN(varname) + 3);
3231 if (winvarname != NULL)
3232 {
3233 STRCPY(winvarname, "w:");
3234 STRCPY(winvarname + 2, varname);
3235 set_var(winvarname, varp, TRUE);
3236 vim_free(winvarname);
3237 }
3238 }
3239 }
3240 if (need_switch_win)
3241 restore_win(save_curwin, save_curtab, TRUE);
3242 }
3243}
3244
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003245/*
3246 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3247 * v:option_type, and v:option_command.
3248 */
3249 void
3250reset_v_option_vars(void)
3251{
3252 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3253 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3254 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3255 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3256 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3257 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3258}
3259
3260/*
3261 * Add an assert error to v:errors.
3262 */
3263 void
3264assert_error(garray_T *gap)
3265{
3266 struct vimvar *vp = &vimvars[VV_ERRORS];
3267
3268 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003269 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003270 set_vim_var_list(VV_ERRORS, list_alloc());
3271 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3272}
3273
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003274 int
3275var_exists(char_u *var)
3276{
3277 char_u *name;
3278 char_u *tofree;
3279 typval_T tv;
3280 int len = 0;
3281 int n = FALSE;
3282
3283 // get_name_len() takes care of expanding curly braces
3284 name = var;
3285 len = get_name_len(&var, &tofree, TRUE, FALSE);
3286 if (len > 0)
3287 {
3288 if (tofree != NULL)
3289 name = tofree;
3290 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
3291 if (n)
3292 {
3293 // handle d.key, l[idx], f(expr)
Bram Moolenaar32e35112020-05-14 22:41:15 +02003294 n = (handle_subscript(&var, &tv, EVAL_EVALUATE,
3295 FALSE, name, &name) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003296 if (n)
3297 clear_tv(&tv);
3298 }
3299 }
3300 if (*var != NUL)
3301 n = FALSE;
3302
3303 vim_free(tofree);
3304 return n;
3305}
3306
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003307static lval_T *redir_lval = NULL;
3308#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3309static garray_T redir_ga; // only valid when redir_lval is not NULL
3310static char_u *redir_endp = NULL;
3311static char_u *redir_varname = NULL;
3312
3313/*
3314 * Start recording command output to a variable
3315 * When "append" is TRUE append to an existing variable.
3316 * Returns OK if successfully completed the setup. FAIL otherwise.
3317 */
3318 int
3319var_redir_start(char_u *name, int append)
3320{
3321 int save_emsg;
3322 int err;
3323 typval_T tv;
3324
3325 // Catch a bad name early.
3326 if (!eval_isnamec1(*name))
3327 {
3328 emsg(_(e_invarg));
3329 return FAIL;
3330 }
3331
3332 // Make a copy of the name, it is used in redir_lval until redir ends.
3333 redir_varname = vim_strsave(name);
3334 if (redir_varname == NULL)
3335 return FAIL;
3336
3337 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3338 if (redir_lval == NULL)
3339 {
3340 var_redir_stop();
3341 return FAIL;
3342 }
3343
3344 // The output is stored in growarray "redir_ga" until redirection ends.
3345 ga_init2(&redir_ga, (int)sizeof(char), 500);
3346
3347 // Parse the variable name (can be a dict or list entry).
3348 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3349 FNE_CHECK_START);
3350 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3351 {
3352 clear_lval(redir_lval);
3353 if (redir_endp != NULL && *redir_endp != NUL)
3354 // Trailing characters are present after the variable name
3355 emsg(_(e_trailing));
3356 else
3357 emsg(_(e_invarg));
3358 redir_endp = NULL; // don't store a value, only cleanup
3359 var_redir_stop();
3360 return FAIL;
3361 }
3362
3363 // check if we can write to the variable: set it to or append an empty
3364 // string
3365 save_emsg = did_emsg;
3366 did_emsg = FALSE;
3367 tv.v_type = VAR_STRING;
3368 tv.vval.v_string = (char_u *)"";
3369 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003371 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003372 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003373 clear_lval(redir_lval);
3374 err = did_emsg;
3375 did_emsg |= save_emsg;
3376 if (err)
3377 {
3378 redir_endp = NULL; // don't store a value, only cleanup
3379 var_redir_stop();
3380 return FAIL;
3381 }
3382
3383 return OK;
3384}
3385
3386/*
3387 * Append "value[value_len]" to the variable set by var_redir_start().
3388 * The actual appending is postponed until redirection ends, because the value
3389 * appended may in fact be the string we write to, changing it may cause freed
3390 * memory to be used:
3391 * :redir => foo
3392 * :let foo
3393 * :redir END
3394 */
3395 void
3396var_redir_str(char_u *value, int value_len)
3397{
3398 int len;
3399
3400 if (redir_lval == NULL)
3401 return;
3402
3403 if (value_len == -1)
3404 len = (int)STRLEN(value); // Append the entire string
3405 else
3406 len = value_len; // Append only "value_len" characters
3407
3408 if (ga_grow(&redir_ga, len) == OK)
3409 {
3410 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3411 redir_ga.ga_len += len;
3412 }
3413 else
3414 var_redir_stop();
3415}
3416
3417/*
3418 * Stop redirecting command output to a variable.
3419 * Frees the allocated memory.
3420 */
3421 void
3422var_redir_stop(void)
3423{
3424 typval_T tv;
3425
3426 if (EVALCMD_BUSY)
3427 {
3428 redir_lval = NULL;
3429 return;
3430 }
3431
3432 if (redir_lval != NULL)
3433 {
3434 // If there was no error: assign the text to the variable.
3435 if (redir_endp != NULL)
3436 {
3437 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3438 tv.v_type = VAR_STRING;
3439 tv.vval.v_string = redir_ga.ga_data;
3440 // Call get_lval() again, if it's inside a Dict or List it may
3441 // have changed.
3442 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3443 FALSE, FALSE, 0, FNE_CHECK_START);
3444 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003446 (char_u *)".");
3447 clear_lval(redir_lval);
3448 }
3449
3450 // free the collected output
3451 VIM_CLEAR(redir_ga.ga_data);
3452
3453 VIM_CLEAR(redir_lval);
3454 }
3455 VIM_CLEAR(redir_varname);
3456}
3457
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003458/*
3459 * "gettabvar()" function
3460 */
3461 void
3462f_gettabvar(typval_T *argvars, typval_T *rettv)
3463{
3464 win_T *oldcurwin;
3465 tabpage_T *tp, *oldtabpage;
3466 dictitem_T *v;
3467 char_u *varname;
3468 int done = FALSE;
3469
3470 rettv->v_type = VAR_STRING;
3471 rettv->vval.v_string = NULL;
3472
3473 varname = tv_get_string_chk(&argvars[1]);
3474 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3475 if (tp != NULL && varname != NULL)
3476 {
3477 // Set tp to be our tabpage, temporarily. Also set the window to the
3478 // first window in the tabpage, otherwise the window is not valid.
3479 if (switch_win(&oldcurwin, &oldtabpage,
3480 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3481 : tp->tp_firstwin, tp, TRUE) == OK)
3482 {
3483 // look up the variable
3484 // Let gettabvar({nr}, "") return the "t:" dictionary.
3485 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3486 if (v != NULL)
3487 {
3488 copy_tv(&v->di_tv, rettv);
3489 done = TRUE;
3490 }
3491 }
3492
3493 // restore previous notion of curwin
3494 restore_win(oldcurwin, oldtabpage, TRUE);
3495 }
3496
3497 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3498 copy_tv(&argvars[2], rettv);
3499}
3500
3501/*
3502 * "gettabwinvar()" function
3503 */
3504 void
3505f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3506{
3507 getwinvar(argvars, rettv, 1);
3508}
3509
3510/*
3511 * "getwinvar()" function
3512 */
3513 void
3514f_getwinvar(typval_T *argvars, typval_T *rettv)
3515{
3516 getwinvar(argvars, rettv, 0);
3517}
3518
3519/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003520 * "getbufvar()" function
3521 */
3522 void
3523f_getbufvar(typval_T *argvars, typval_T *rettv)
3524{
3525 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003526 char_u *varname;
3527 dictitem_T *v;
3528 int done = FALSE;
3529
3530 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3531 varname = tv_get_string_chk(&argvars[1]);
3532 ++emsg_off;
3533 buf = tv_get_buf(&argvars[0], FALSE);
3534
3535 rettv->v_type = VAR_STRING;
3536 rettv->vval.v_string = NULL;
3537
3538 if (buf != NULL && varname != NULL)
3539 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003540 if (*varname == '&')
3541 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003542 buf_T *save_curbuf = curbuf;
3543
3544 // set curbuf to be our buf, temporarily
3545 curbuf = buf;
3546
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003547 if (varname[1] == NUL)
3548 {
3549 // get all buffer-local options in a dict
3550 dict_T *opts = get_winbuf_options(TRUE);
3551
3552 if (opts != NULL)
3553 {
3554 rettv_dict_set(rettv, opts);
3555 done = TRUE;
3556 }
3557 }
3558 else if (get_option_tv(&varname, rettv, TRUE) == OK)
3559 // buffer-local-option
3560 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003561
3562 // restore previous notion of curbuf
3563 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003564 }
3565 else
3566 {
3567 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003568 if (*varname == NUL)
3569 // Let getbufvar({nr}, "") return the "b:" dictionary.
3570 v = &buf->b_bufvar;
3571 else
3572 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3573 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003574 if (v != NULL)
3575 {
3576 copy_tv(&v->di_tv, rettv);
3577 done = TRUE;
3578 }
3579 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003580 }
3581
3582 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3583 // use the default value
3584 copy_tv(&argvars[2], rettv);
3585
3586 --emsg_off;
3587}
3588
3589/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003590 * "settabvar()" function
3591 */
3592 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003593f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003594{
3595 tabpage_T *save_curtab;
3596 tabpage_T *tp;
3597 char_u *varname, *tabvarname;
3598 typval_T *varp;
3599
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003600 if (check_secure())
3601 return;
3602
3603 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3604 varname = tv_get_string_chk(&argvars[1]);
3605 varp = &argvars[2];
3606
3607 if (varname != NULL && varp != NULL && tp != NULL)
3608 {
3609 save_curtab = curtab;
3610 goto_tabpage_tp(tp, FALSE, FALSE);
3611
3612 tabvarname = alloc(STRLEN(varname) + 3);
3613 if (tabvarname != NULL)
3614 {
3615 STRCPY(tabvarname, "t:");
3616 STRCPY(tabvarname + 2, varname);
3617 set_var(tabvarname, varp, TRUE);
3618 vim_free(tabvarname);
3619 }
3620
3621 // Restore current tabpage
3622 if (valid_tabpage(save_curtab))
3623 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3624 }
3625}
3626
3627/*
3628 * "settabwinvar()" function
3629 */
3630 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003631f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003632{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003633 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003634}
3635
3636/*
3637 * "setwinvar()" function
3638 */
3639 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003640f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003641{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003642 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003643}
3644
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003645/*
3646 * "setbufvar()" function
3647 */
3648 void
3649f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3650{
3651 buf_T *buf;
3652 char_u *varname, *bufvarname;
3653 typval_T *varp;
3654 char_u nbuf[NUMBUFLEN];
3655
3656 if (check_secure())
3657 return;
3658 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3659 varname = tv_get_string_chk(&argvars[1]);
3660 buf = tv_get_buf(&argvars[0], FALSE);
3661 varp = &argvars[2];
3662
3663 if (buf != NULL && varname != NULL && varp != NULL)
3664 {
3665 if (*varname == '&')
3666 {
3667 long numval;
3668 char_u *strval;
3669 int error = FALSE;
3670 aco_save_T aco;
3671
3672 // set curbuf to be our buf, temporarily
3673 aucmd_prepbuf(&aco, buf);
3674
3675 ++varname;
3676 numval = (long)tv_get_number_chk(varp, &error);
3677 strval = tv_get_string_buf_chk(varp, nbuf);
3678 if (!error && strval != NULL)
3679 set_option_value(varname, numval, strval, OPT_LOCAL);
3680
3681 // reset notion of buffer
3682 aucmd_restbuf(&aco);
3683 }
3684 else
3685 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003686 bufvarname = alloc(STRLEN(varname) + 3);
3687 if (bufvarname != NULL)
3688 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003689 buf_T *save_curbuf = curbuf;
3690
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003691 curbuf = buf;
3692 STRCPY(bufvarname, "b:");
3693 STRCPY(bufvarname + 2, varname);
3694 set_var(bufvarname, varp, TRUE);
3695 vim_free(bufvarname);
3696 curbuf = save_curbuf;
3697 }
3698 }
3699 }
3700}
3701
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003702/*
3703 * Get a callback from "arg". It can be a Funcref or a function name.
3704 * When "arg" is zero return an empty string.
3705 * "cb_name" is not allocated.
3706 * "cb_name" is set to NULL for an invalid argument.
3707 */
3708 callback_T
3709get_callback(typval_T *arg)
3710{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003711 callback_T res;
3712 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003713
3714 res.cb_free_name = FALSE;
3715 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3716 {
3717 res.cb_partial = arg->vval.v_partial;
3718 ++res.cb_partial->pt_refcount;
3719 res.cb_name = partial_name(res.cb_partial);
3720 }
3721 else
3722 {
3723 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003724 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3725 && isdigit(*arg->vval.v_string))
3726 r = FAIL;
3727 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003728 {
3729 // Note that we don't make a copy of the string.
3730 res.cb_name = arg->vval.v_string;
3731 func_ref(res.cb_name);
3732 }
3733 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003734 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003735 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003736 r = FAIL;
3737
3738 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003739 {
3740 emsg(_("E921: Invalid callback argument"));
3741 res.cb_name = NULL;
3742 }
3743 }
3744 return res;
3745}
3746
3747/*
3748 * Copy a callback into a typval_T.
3749 */
3750 void
3751put_callback(callback_T *cb, typval_T *tv)
3752{
3753 if (cb->cb_partial != NULL)
3754 {
3755 tv->v_type = VAR_PARTIAL;
3756 tv->vval.v_partial = cb->cb_partial;
3757 ++tv->vval.v_partial->pt_refcount;
3758 }
3759 else
3760 {
3761 tv->v_type = VAR_FUNC;
3762 tv->vval.v_string = vim_strsave(cb->cb_name);
3763 func_ref(cb->cb_name);
3764 }
3765}
3766
3767/*
3768 * Make a copy of "src" into "dest", allocating the function name if needed,
3769 * without incrementing the refcount.
3770 */
3771 void
3772set_callback(callback_T *dest, callback_T *src)
3773{
3774 if (src->cb_partial == NULL)
3775 {
3776 // just a function name, make a copy
3777 dest->cb_name = vim_strsave(src->cb_name);
3778 dest->cb_free_name = TRUE;
3779 }
3780 else
3781 {
3782 // cb_name is a pointer into cb_partial
3783 dest->cb_name = src->cb_name;
3784 dest->cb_free_name = FALSE;
3785 }
3786 dest->cb_partial = src->cb_partial;
3787}
3788
3789/*
3790 * Unref/free "callback" returned by get_callback() or set_callback().
3791 */
3792 void
3793free_callback(callback_T *callback)
3794{
3795 if (callback->cb_partial != NULL)
3796 {
3797 partial_unref(callback->cb_partial);
3798 callback->cb_partial = NULL;
3799 }
3800 else if (callback->cb_name != NULL)
3801 func_unref(callback->cb_name);
3802 if (callback->cb_free_name)
3803 {
3804 vim_free(callback->cb_name);
3805 callback->cb_free_name = FALSE;
3806 }
3807 callback->cb_name = NULL;
3808}
3809
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003810#endif // FEAT_EVAL