blob: d2c71d290bc2eef959d107e5f66775e0cd45f69a [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
48 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
49} vimvars[VV_LEN] =
50{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020051 // The order here must match the VV_ defines in vim.h!
52 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaare5cdf152019-08-29 22:09:46 +020053 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
54 {VV_NAME("count1", VAR_NUMBER), VV_RO},
55 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
56 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
57 {VV_NAME("warningmsg", VAR_STRING), 0},
58 {VV_NAME("statusmsg", VAR_STRING), 0},
59 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
60 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
61 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
62 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
63 {VV_NAME("termresponse", VAR_STRING), VV_RO},
64 {VV_NAME("fname", VAR_STRING), VV_RO},
65 {VV_NAME("lang", VAR_STRING), VV_RO},
66 {VV_NAME("lc_time", VAR_STRING), VV_RO},
67 {VV_NAME("ctype", VAR_STRING), VV_RO},
68 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
69 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
70 {VV_NAME("fname_in", VAR_STRING), VV_RO},
71 {VV_NAME("fname_out", VAR_STRING), VV_RO},
72 {VV_NAME("fname_new", VAR_STRING), VV_RO},
73 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
74 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
75 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
76 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
77 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
78 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
79 {VV_NAME("progname", VAR_STRING), VV_RO},
80 {VV_NAME("servername", VAR_STRING), VV_RO},
81 {VV_NAME("dying", VAR_NUMBER), VV_RO},
82 {VV_NAME("exception", VAR_STRING), VV_RO},
83 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
84 {VV_NAME("register", VAR_STRING), VV_RO},
85 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
86 {VV_NAME("insertmode", VAR_STRING), VV_RO},
87 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
88 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
89 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
90 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
91 {VV_NAME("fcs_choice", VAR_STRING), 0},
92 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
93 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
94 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
95 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
96 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
97 {VV_NAME("beval_text", VAR_STRING), VV_RO},
98 {VV_NAME("scrollstart", VAR_STRING), 0},
99 {VV_NAME("swapname", VAR_STRING), VV_RO},
100 {VV_NAME("swapchoice", VAR_STRING), 0},
101 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
102 {VV_NAME("char", VAR_STRING), 0},
103 {VV_NAME("mouse_win", VAR_NUMBER), 0},
104 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
105 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
106 {VV_NAME("mouse_col", VAR_NUMBER), 0},
107 {VV_NAME("operator", VAR_STRING), VV_RO},
108 {VV_NAME("searchforward", VAR_NUMBER), 0},
109 {VV_NAME("hlsearch", VAR_NUMBER), 0},
110 {VV_NAME("oldfiles", VAR_LIST), 0},
111 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
112 {VV_NAME("progpath", VAR_STRING), VV_RO},
113 {VV_NAME("completed_item", VAR_DICT), VV_RO},
114 {VV_NAME("option_new", VAR_STRING), VV_RO},
115 {VV_NAME("option_old", VAR_STRING), VV_RO},
116 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
117 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
118 {VV_NAME("option_command", VAR_STRING), VV_RO},
119 {VV_NAME("option_type", VAR_STRING), VV_RO},
120 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100121 {VV_NAME("false", VAR_BOOL), VV_RO},
122 {VV_NAME("true", VAR_BOOL), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200123 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100124 {VV_NAME("null", VAR_SPECIAL), VV_RO},
125 {VV_NAME("numbersize", VAR_NUMBER), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200126 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
127 {VV_NAME("testing", VAR_NUMBER), 0},
128 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
129 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
130 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
131 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
132 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
133 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
134 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
135 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
136 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
137 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
138 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
139 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
140 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
141 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
142 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
143 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
144 {VV_NAME("event", VAR_DICT), VV_RO},
145 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
146 {VV_NAME("echospace", VAR_NUMBER), VV_RO},
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100147 {VV_NAME("argv", VAR_LIST), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200148};
149
150// shorthand
151#define vv_type vv_di.di_tv.v_type
152#define vv_nr vv_di.di_tv.vval.v_number
153#define vv_float vv_di.di_tv.vval.v_float
154#define vv_str vv_di.di_tv.vval.v_string
155#define vv_list vv_di.di_tv.vval.v_list
156#define vv_dict vv_di.di_tv.vval.v_dict
157#define vv_blob vv_di.di_tv.vval.v_blob
158#define vv_tv vv_di.di_tv
159
160static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200161static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200162#define vimvarht vimvardict.dv_hashtab
163
164// for VIM_VERSION_ defines
165#include "version.h"
166
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200167static void list_glob_vars(int *first);
168static void list_buf_vars(int *first);
169static void list_win_vars(int *first);
170static void list_tab_vars(int *first);
171static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100172static 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 +0200173static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
174static 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 +0200175static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200176static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200177static void list_one_var(dictitem_T *v, char *prefix, int *first);
178static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
179
180/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200181 * Initialize global and vim special variables
182 */
183 void
184evalvars_init(void)
185{
186 int i;
187 struct vimvar *p;
188
189 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
190 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
191 vimvardict.dv_lock = VAR_FIXED;
192 hash_init(&compat_hashtab);
193
194 for (i = 0; i < VV_LEN; ++i)
195 {
196 p = &vimvars[i];
197 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
198 {
199 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
200 getout(1);
201 }
202 STRCPY(p->vv_di.di_key, p->vv_name);
203 if (p->vv_flags & VV_RO)
204 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
205 else if (p->vv_flags & VV_RO_SBX)
206 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
207 else
208 p->vv_di.di_flags = DI_FLAGS_FIX;
209
210 // add to v: scope dict, unless the value is not always available
211 if (p->vv_type != VAR_UNKNOWN)
212 hash_add(&vimvarht, p->vv_di.di_key);
213 if (p->vv_flags & VV_COMPAT)
214 // add to compat scope dict
215 hash_add(&compat_hashtab, p->vv_di.di_key);
216 }
217 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
218 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
219
220 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
221 set_vim_var_nr(VV_HLSEARCH, 1L);
222 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
223 set_vim_var_list(VV_ERRORS, list_alloc());
224 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
225
226 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
227 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
228 set_vim_var_nr(VV_NONE, VVAL_NONE);
229 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100230 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200231
232 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
233 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
234 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
235 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
236 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
237 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
238 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
239 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
240 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
241 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
242 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
243
244 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
245
Bram Moolenaar439c0362020-06-06 15:58:03 +0200246 // Default for v:register is not 0 but '"'. This is adjusted once the
247 // clipboard has been setup by calling reset_reg_var().
248 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200249}
250
251#if defined(EXITFREE) || defined(PROTO)
252/*
253 * Free all vim variables information on exit
254 */
255 void
256evalvars_clear(void)
257{
258 int i;
259 struct vimvar *p;
260
261 for (i = 0; i < VV_LEN; ++i)
262 {
263 p = &vimvars[i];
264 if (p->vv_di.di_tv.v_type == VAR_STRING)
265 VIM_CLEAR(p->vv_str);
266 else if (p->vv_di.di_tv.v_type == VAR_LIST)
267 {
268 list_unref(p->vv_list);
269 p->vv_list = NULL;
270 }
271 }
272 hash_clear(&vimvarht);
273 hash_init(&vimvarht); // garbage_collect() will access it
274 hash_clear(&compat_hashtab);
275
276 // global variables
277 vars_clear(&globvarht);
278
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100279 // Script-local variables. Clear all the variables here.
280 // The scriptvar_T is cleared later in free_scriptnames(), because a
281 // variable in one script might hold a reference to the whole scope of
282 // another script.
283 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200284 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200285}
286#endif
287
288 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200289garbage_collect_globvars(int copyID)
290{
291 return set_ref_in_ht(&globvarht, copyID, NULL);
292}
293
294 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200295garbage_collect_vimvars(int copyID)
296{
297 return set_ref_in_ht(&vimvarht, copyID, NULL);
298}
299
300 int
301garbage_collect_scriptvars(int copyID)
302{
303 int i;
304 int abort = FALSE;
305
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100306 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200307 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
308
309 return abort;
310}
311
312/*
313 * Set an internal variable to a string value. Creates the variable if it does
314 * not already exist.
315 */
316 void
317set_internal_string_var(char_u *name, char_u *value)
318{
319 char_u *val;
320 typval_T *tvp;
321
322 val = vim_strsave(value);
323 if (val != NULL)
324 {
325 tvp = alloc_string_tv(val);
326 if (tvp != NULL)
327 {
328 set_var(name, tvp, FALSE);
329 free_tv(tvp);
330 }
331 }
332}
333
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200334 int
335eval_charconvert(
336 char_u *enc_from,
337 char_u *enc_to,
338 char_u *fname_from,
339 char_u *fname_to)
340{
341 int err = FALSE;
342
343 set_vim_var_string(VV_CC_FROM, enc_from, -1);
344 set_vim_var_string(VV_CC_TO, enc_to, -1);
345 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
346 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
347 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
348 err = TRUE;
349 set_vim_var_string(VV_CC_FROM, NULL, -1);
350 set_vim_var_string(VV_CC_TO, NULL, -1);
351 set_vim_var_string(VV_FNAME_IN, NULL, -1);
352 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
353
354 if (err)
355 return FAIL;
356 return OK;
357}
358
359# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
360 int
361eval_printexpr(char_u *fname, char_u *args)
362{
363 int err = FALSE;
364
365 set_vim_var_string(VV_FNAME_IN, fname, -1);
366 set_vim_var_string(VV_CMDARG, args, -1);
367 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
368 err = TRUE;
369 set_vim_var_string(VV_FNAME_IN, NULL, -1);
370 set_vim_var_string(VV_CMDARG, NULL, -1);
371
372 if (err)
373 {
374 mch_remove(fname);
375 return FAIL;
376 }
377 return OK;
378}
379# endif
380
381# if defined(FEAT_DIFF) || defined(PROTO)
382 void
383eval_diff(
384 char_u *origfile,
385 char_u *newfile,
386 char_u *outfile)
387{
388 int err = FALSE;
389
390 set_vim_var_string(VV_FNAME_IN, origfile, -1);
391 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
392 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
393 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
394 set_vim_var_string(VV_FNAME_IN, NULL, -1);
395 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
396 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
397}
398
399 void
400eval_patch(
401 char_u *origfile,
402 char_u *difffile,
403 char_u *outfile)
404{
405 int err;
406
407 set_vim_var_string(VV_FNAME_IN, origfile, -1);
408 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
409 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
410 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
411 set_vim_var_string(VV_FNAME_IN, NULL, -1);
412 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
413 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
414}
415# endif
416
417#if defined(FEAT_SPELL) || defined(PROTO)
418/*
419 * Evaluate an expression to a list with suggestions.
420 * For the "expr:" part of 'spellsuggest'.
421 * Returns NULL when there is an error.
422 */
423 list_T *
424eval_spell_expr(char_u *badword, char_u *expr)
425{
426 typval_T save_val;
427 typval_T rettv;
428 list_T *list = NULL;
429 char_u *p = skipwhite(expr);
430
431 // Set "v:val" to the bad word.
432 prepare_vimvar(VV_VAL, &save_val);
433 set_vim_var_string(VV_VAL, badword, -1);
434 if (p_verbose == 0)
435 ++emsg_off;
436
Bram 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 Moolenaar2eec3792020-05-25 20:33:55 +0200685 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200686 * ":const" list all variable values
687 * ":const var1 var2" list variable values
688 * ":const var = expr" assignment command.
689 * ":const [var1, var2] = expr" unpack list.
690 */
691 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200692ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200693{
694 char_u *arg = eap->arg;
695 char_u *expr = NULL;
696 typval_T rettv;
697 int i;
698 int var_count = 0;
699 int semicolon = 0;
700 char_u op[2];
701 char_u *argend;
702 int first = TRUE;
703 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200704 int has_assign;
Bram Moolenaar09689a02020-05-09 22:50:08 +0200705 int flags = eap->cmdidx == CMD_const ? LET_IS_CONST : 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 // detect Vim9 assignment without ":let" or ":const"
708 if (eap->arg == eap->cmd)
709 flags |= LET_NO_COMMAND;
710
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200711 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200712 if (argend == NULL)
713 return;
714 if (argend > arg && argend[-1] == '.') // for var.='str'
715 --argend;
716 expr = skipwhite(argend);
717 concat = expr[0] == '.'
718 && ((expr[1] == '=' && current_sctx.sc_version < 2)
719 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200720 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
721 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200722 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200723 {
724 // ":let" without "=": list variables
725 if (*arg == '[')
726 emsg(_(e_invarg));
727 else if (expr[0] == '.')
728 emsg(_("E985: .= is not supported with script version 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200729 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200730 {
731 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9)
732 {
733 // Vim9 declaration ":let var: type"
734 arg = vim9_declare_scriptvar(eap, arg);
735 }
736 else
737 {
738 // ":let var1 var2" - list values
739 arg = list_arg_vars(eap, arg, &first);
740 }
741 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200742 else if (!eap->skip)
743 {
744 // ":let"
745 list_glob_vars(&first);
746 list_buf_vars(&first);
747 list_win_vars(&first);
748 list_tab_vars(&first);
749 list_script_vars(&first);
750 list_func_vars(&first);
751 list_vim_vars(&first);
752 }
753 eap->nextcmd = check_nextcmd(arg);
754 }
755 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
756 {
757 list_T *l;
758
759 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200760 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200761 if (l != NULL)
762 {
763 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200764 if (!eap->skip)
765 {
766 op[0] = '=';
767 op[1] = NUL;
768 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200770 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200771 clear_tv(&rettv);
772 }
773 }
774 else
775 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200776 int eval_flags;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200777
Bram Moolenaar32e35112020-05-14 22:41:15 +0200778 rettv.v_type = VAR_UNKNOWN;
779 i = FAIL;
780 if (has_assign || concat)
781 {
782 op[0] = '=';
783 op[1] = NUL;
784 if (*expr != '=')
785 {
786 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
787 {
788 op[0] = *expr; // +=, -=, *=, /=, %= or .=
789 if (expr[0] == '.' && expr[1] == '.') // ..=
790 ++expr;
791 }
792 expr = skipwhite(expr + 2);
793 }
794 else
795 expr = skipwhite(expr + 1);
796
797 if (eap->skip)
798 ++emsg_skip;
799 eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200800 i = eval0(expr, &rettv, &eap->nextcmd, eval_flags);
801 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200802 if (eap->skip)
803 {
804 if (i != FAIL)
805 clear_tv(&rettv);
806 --emsg_skip;
807 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200808 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200809 {
810 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200812 clear_tv(&rettv);
813 }
814 }
815}
816
817/*
818 * Assign the typevalue "tv" to the variable or variables at "arg_start".
819 * Handles both "var" with any type and "[var, var; var]" with a list type.
820 * When "op" is not NULL it points to a string with characters that
821 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
822 * or concatenate.
823 * Returns OK or FAIL;
824 */
825 int
826ex_let_vars(
827 char_u *arg_start,
828 typval_T *tv,
829 int copy, // copy values from "tv", don't move
830 int semicolon, // from skip_var_list()
831 int var_count, // from skip_var_list()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100832 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200833 char_u *op)
834{
835 char_u *arg = arg_start;
836 list_T *l;
837 int i;
838 listitem_T *item;
839 typval_T ltv;
840
841 if (*arg != '[')
842 {
843 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100844 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200845 return FAIL;
846 return OK;
847 }
848
849 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
850 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
851 {
852 emsg(_(e_listreq));
853 return FAIL;
854 }
855
856 i = list_len(l);
857 if (semicolon == 0 && var_count < i)
858 {
859 emsg(_("E687: Less targets than List items"));
860 return FAIL;
861 }
862 if (var_count - semicolon > i)
863 {
864 emsg(_("E688: More targets than List items"));
865 return FAIL;
866 }
867
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200868 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200869 item = l->lv_first;
870 while (*arg != ']')
871 {
872 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100873 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200874 item = item->li_next;
875 if (arg == NULL)
876 return FAIL;
877
878 arg = skipwhite(arg);
879 if (*arg == ';')
880 {
881 // Put the rest of the list (may be empty) in the var after ';'.
882 // Create a new list for this.
883 l = list_alloc();
884 if (l == NULL)
885 return FAIL;
886 while (item != NULL)
887 {
888 list_append_tv(l, &item->li_tv);
889 item = item->li_next;
890 }
891
892 ltv.v_type = VAR_LIST;
893 ltv.v_lock = 0;
894 ltv.vval.v_list = l;
895 l->lv_refcount = 1;
896
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100897 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200898 (char_u *)"]", op);
899 clear_tv(&ltv);
900 if (arg == NULL)
901 return FAIL;
902 break;
903 }
904 else if (*arg != ',' && *arg != ']')
905 {
906 internal_error("ex_let_vars()");
907 return FAIL;
908 }
909 }
910
911 return OK;
912}
913
914/*
915 * Skip over assignable variable "var" or list of variables "[var, var]".
916 * Used for ":let varvar = expr" and ":for varvar in expr".
917 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200918 * for "[var, var; var]" set "semicolon" to 1.
919 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200920 * Return NULL for an error.
921 */
922 char_u *
923skip_var_list(
924 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100925 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200926 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200927 int *semicolon,
928 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200929{
930 char_u *p, *s;
931
932 if (*arg == '[')
933 {
934 // "[var, var]": find the matching ']'.
935 p = arg;
936 for (;;)
937 {
938 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200939 s = skip_var_one(p, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200940 if (s == p)
941 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200942 if (!silent)
943 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200944 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 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200962 if (!silent)
963 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200964 return NULL;
965 }
966 }
967 return p + 1;
968 }
969 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100970 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200971}
972
973/*
974 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
975 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100976 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200977 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200978 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100979skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200980{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 char_u *end;
982
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200983 if (*arg == '@' && arg[1] != NUL)
984 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200986 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200987 if (include_type && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100988 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200989 // "a: type" is declaring variable "a" with a type, not "a:".
990 if (end == arg + 2 && end[-1] == ':')
991 --end;
992 if (*end == ':')
993 end = skip_type(skipwhite(end + 1));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100994 }
995 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200996}
997
998/*
999 * List variables for hashtab "ht" with prefix "prefix".
1000 * If "empty" is TRUE also list NULL strings as empty strings.
1001 */
1002 void
1003list_hashtable_vars(
1004 hashtab_T *ht,
1005 char *prefix,
1006 int empty,
1007 int *first)
1008{
1009 hashitem_T *hi;
1010 dictitem_T *di;
1011 int todo;
1012 char_u buf[IOSIZE];
1013
1014 todo = (int)ht->ht_used;
1015 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1016 {
1017 if (!HASHITEM_EMPTY(hi))
1018 {
1019 --todo;
1020 di = HI2DI(hi);
1021
1022 // apply :filter /pat/ to variable name
1023 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1024 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1025 if (message_filtered(buf))
1026 continue;
1027
1028 if (empty || di->di_tv.v_type != VAR_STRING
1029 || di->di_tv.vval.v_string != NULL)
1030 list_one_var(di, prefix, first);
1031 }
1032 }
1033}
1034
1035/*
1036 * List global variables.
1037 */
1038 static void
1039list_glob_vars(int *first)
1040{
1041 list_hashtable_vars(&globvarht, "", TRUE, first);
1042}
1043
1044/*
1045 * List buffer variables.
1046 */
1047 static void
1048list_buf_vars(int *first)
1049{
1050 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1051}
1052
1053/*
1054 * List window variables.
1055 */
1056 static void
1057list_win_vars(int *first)
1058{
1059 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1060}
1061
1062/*
1063 * List tab page variables.
1064 */
1065 static void
1066list_tab_vars(int *first)
1067{
1068 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1069}
1070
1071/*
1072 * List variables in "arg".
1073 */
1074 static char_u *
1075list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1076{
1077 int error = FALSE;
1078 int len;
1079 char_u *name;
1080 char_u *name_start;
1081 char_u *arg_subsc;
1082 char_u *tofree;
1083 typval_T tv;
1084
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001085 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001086 {
1087 if (error || eap->skip)
1088 {
1089 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1090 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1091 {
1092 emsg_severe = TRUE;
1093 emsg(_(e_trailing));
1094 break;
1095 }
1096 }
1097 else
1098 {
1099 // get_name_len() takes care of expanding curly braces
1100 name_start = name = arg;
1101 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1102 if (len <= 0)
1103 {
1104 // This is mainly to keep test 49 working: when expanding
1105 // curly braces fails overrule the exception error message.
1106 if (len < 0 && !aborting())
1107 {
1108 emsg_severe = TRUE;
1109 semsg(_(e_invarg2), arg);
1110 break;
1111 }
1112 error = TRUE;
1113 }
1114 else
1115 {
1116 if (tofree != NULL)
1117 name = tofree;
1118 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
1119 error = TRUE;
1120 else
1121 {
1122 // handle d.key, l[idx], f(expr)
1123 arg_subsc = arg;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001124 if (handle_subscript(&arg, &tv, EVAL_EVALUATE, TRUE,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001125 name, &name) == FAIL)
1126 error = TRUE;
1127 else
1128 {
1129 if (arg == arg_subsc && len == 2 && name[1] == ':')
1130 {
1131 switch (*name)
1132 {
1133 case 'g': list_glob_vars(first); break;
1134 case 'b': list_buf_vars(first); break;
1135 case 'w': list_win_vars(first); break;
1136 case 't': list_tab_vars(first); break;
1137 case 'v': list_vim_vars(first); break;
1138 case 's': list_script_vars(first); break;
1139 case 'l': list_func_vars(first); break;
1140 default:
1141 semsg(_("E738: Can't list variables for %s"), name);
1142 }
1143 }
1144 else
1145 {
1146 char_u numbuf[NUMBUFLEN];
1147 char_u *tf;
1148 int c;
1149 char_u *s;
1150
1151 s = echo_string(&tv, &tf, numbuf, 0);
1152 c = *arg;
1153 *arg = NUL;
1154 list_one_var_a("",
1155 arg == arg_subsc ? name : name_start,
1156 tv.v_type,
1157 s == NULL ? (char_u *)"" : s,
1158 first);
1159 *arg = c;
1160 vim_free(tf);
1161 }
1162 clear_tv(&tv);
1163 }
1164 }
1165 }
1166
1167 vim_free(tofree);
1168 }
1169
1170 arg = skipwhite(arg);
1171 }
1172
1173 return arg;
1174}
1175
1176/*
1177 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1178 * Returns a pointer to the char just after the var name.
1179 * Returns NULL if there is an error.
1180 */
1181 static char_u *
1182ex_let_one(
1183 char_u *arg, // points to variable name
1184 typval_T *tv, // value to assign to variable
1185 int copy, // copy value from "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001186 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001187 char_u *endchars, // valid chars after variable name or NULL
1188 char_u *op) // "+", "-", "." or NULL
1189{
1190 int c1;
1191 char_u *name;
1192 char_u *p;
1193 char_u *arg_end = NULL;
1194 int len;
1195 int opt_flags;
1196 char_u *tofree = NULL;
1197
1198 // ":let $VAR = expr": Set environment variable.
1199 if (*arg == '$')
1200 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001202 {
1203 emsg(_("E996: Cannot lock an environment variable"));
1204 return NULL;
1205 }
1206 // Find the end of the name.
1207 ++arg;
1208 name = arg;
1209 len = get_env_len(&arg);
1210 if (len == 0)
1211 semsg(_(e_invarg2), name - 1);
1212 else
1213 {
1214 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1215 semsg(_(e_letwrong), op);
1216 else if (endchars != NULL
1217 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1218 emsg(_(e_letunexp));
1219 else if (!check_secure())
1220 {
1221 c1 = name[len];
1222 name[len] = NUL;
1223 p = tv_get_string_chk(tv);
1224 if (p != NULL && op != NULL && *op == '.')
1225 {
1226 int mustfree = FALSE;
1227 char_u *s = vim_getenv(name, &mustfree);
1228
1229 if (s != NULL)
1230 {
1231 p = tofree = concat_str(s, p);
1232 if (mustfree)
1233 vim_free(s);
1234 }
1235 }
1236 if (p != NULL)
1237 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001238 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001239 arg_end = arg;
1240 }
1241 name[len] = c1;
1242 vim_free(tofree);
1243 }
1244 }
1245 }
1246
1247 // ":let &option = expr": Set option value.
1248 // ":let &l:option = expr": Set local option value.
1249 // ":let &g:option = expr": Set global option value.
1250 else if (*arg == '&')
1251 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001252 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001253 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001254 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001255 return NULL;
1256 }
1257 // Find the end of the name.
1258 p = find_option_end(&arg, &opt_flags);
1259 if (p == NULL || (endchars != NULL
1260 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1261 emsg(_(e_letunexp));
1262 else
1263 {
1264 long n;
1265 int opt_type;
1266 long numval;
1267 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001268 char_u *s = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001269
1270 c1 = *p;
1271 *p = NUL;
1272
1273 n = (long)tv_get_number(tv);
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001274 // avoid setting a string option to the text "v:false" or similar.
1275 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL)
1276 s = tv_get_string_chk(tv); // != NULL if number or string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001277 if (s != NULL && op != NULL && *op != '=')
1278 {
1279 opt_type = get_option_value(arg, &numval,
1280 &stringval, opt_flags);
1281 if ((opt_type == 1 && *op == '.')
1282 || (opt_type == 0 && *op != '.'))
1283 {
1284 semsg(_(e_letwrong), op);
1285 s = NULL; // don't set the value
1286 }
1287 else
1288 {
1289 if (opt_type == 1) // number
1290 {
1291 switch (*op)
1292 {
1293 case '+': n = numval + n; break;
1294 case '-': n = numval - n; break;
1295 case '*': n = numval * n; break;
1296 case '/': n = (long)num_divide(numval, n); break;
1297 case '%': n = (long)num_modulus(numval, n); break;
1298 }
1299 }
1300 else if (opt_type == 0 && stringval != NULL) // string
1301 {
1302 s = concat_str(stringval, s);
1303 vim_free(stringval);
1304 stringval = s;
1305 }
1306 }
1307 }
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001308 if (s != NULL || tv->v_type == VAR_BOOL
1309 || tv->v_type == VAR_SPECIAL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001310 {
1311 set_option_value(arg, n, s, opt_flags);
1312 arg_end = p;
1313 }
1314 *p = c1;
1315 vim_free(stringval);
1316 }
1317 }
1318
1319 // ":let @r = expr": Set register contents.
1320 else if (*arg == '@')
1321 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001323 {
1324 emsg(_("E996: Cannot lock a register"));
1325 return NULL;
1326 }
1327 ++arg;
1328 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1329 semsg(_(e_letwrong), op);
1330 else if (endchars != NULL
1331 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1332 emsg(_(e_letunexp));
1333 else
1334 {
1335 char_u *ptofree = NULL;
1336 char_u *s;
1337
1338 p = tv_get_string_chk(tv);
1339 if (p != NULL && op != NULL && *op == '.')
1340 {
1341 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1342 if (s != NULL)
1343 {
1344 p = ptofree = concat_str(s, p);
1345 vim_free(s);
1346 }
1347 }
1348 if (p != NULL)
1349 {
1350 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1351 arg_end = arg + 1;
1352 }
1353 vim_free(ptofree);
1354 }
1355 }
1356
1357 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001358 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001359 // ":let {expr} = expr": Idem, name made with curly braces
1360 else if (eval_isnamec1(*arg) || *arg == '{')
1361 {
1362 lval_T lv;
1363
1364 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001365 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001366 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001367 if (endchars != NULL && vim_strchr(endchars,
1368 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001369 emsg(_(e_letunexp));
1370 else
1371 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001372 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001373 arg_end = p;
1374 }
1375 }
1376 clear_lval(&lv);
1377 }
1378
1379 else
1380 semsg(_(e_invarg2), arg);
1381
1382 return arg_end;
1383}
1384
1385/*
1386 * ":unlet[!] var1 ... " command.
1387 */
1388 void
1389ex_unlet(exarg_T *eap)
1390{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001391 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001392}
1393
1394/*
1395 * ":lockvar" and ":unlockvar" commands
1396 */
1397 void
1398ex_lockvar(exarg_T *eap)
1399{
1400 char_u *arg = eap->arg;
1401 int deep = 2;
1402
1403 if (eap->forceit)
1404 deep = -1;
1405 else if (vim_isdigit(*arg))
1406 {
1407 deep = getdigits(&arg);
1408 arg = skipwhite(arg);
1409 }
1410
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001411 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001412}
1413
1414/*
1415 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001416 * Also used for Vim9 script. "callback" is invoked as:
1417 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001418 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001419 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001420ex_unletlock(
1421 exarg_T *eap,
1422 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001423 int deep,
1424 int glv_flags,
1425 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1426 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001427{
1428 char_u *arg = argstart;
1429 char_u *name_end;
1430 int error = FALSE;
1431 lval_T lv;
1432
1433 do
1434 {
1435 if (*arg == '$')
1436 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001437 lv.ll_name = arg;
1438 lv.ll_tv = NULL;
1439 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001440 if (get_env_len(&arg) == 0)
1441 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001442 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001443 return;
1444 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001445 if (!error && !eap->skip
1446 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1447 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001448 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001449 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001450 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001451 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001452 // Parse the name and find the end.
1453 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1454 glv_flags, FNE_CHECK_START);
1455 if (lv.ll_name == NULL)
1456 error = TRUE; // error but continue parsing
1457 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1458 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001459 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001460 if (name_end != NULL)
1461 {
1462 emsg_severe = TRUE;
1463 emsg(_(e_trailing));
1464 }
1465 if (!(eap->skip || error))
1466 clear_lval(&lv);
1467 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001468 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001469
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001470 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001471 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001472 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001473
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001474 if (!eap->skip)
1475 clear_lval(&lv);
1476 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001477
1478 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001479 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001480
1481 eap->nextcmd = check_nextcmd(arg);
1482}
1483
1484 static int
1485do_unlet_var(
1486 lval_T *lp,
1487 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001488 exarg_T *eap,
1489 int deep UNUSED,
1490 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001491{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001492 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001493 int ret = OK;
1494 int cc;
1495
1496 if (lp->ll_tv == NULL)
1497 {
1498 cc = *name_end;
1499 *name_end = NUL;
1500
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001501 // Environment variable, normal name or expanded name.
1502 if (*lp->ll_name == '$')
1503 vim_unsetenv(lp->ll_name + 1);
1504 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001505 ret = FAIL;
1506 *name_end = cc;
1507 }
1508 else if ((lp->ll_list != NULL
1509 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
1510 || (lp->ll_dict != NULL
1511 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
1512 return FAIL;
1513 else if (lp->ll_range)
1514 {
1515 listitem_T *li;
1516 listitem_T *ll_li = lp->ll_li;
1517 int ll_n1 = lp->ll_n1;
1518
1519 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1520 {
1521 li = ll_li->li_next;
1522 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
1523 return FAIL;
1524 ll_li = li;
1525 ++ll_n1;
1526 }
1527
1528 // Delete a range of List items.
1529 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1530 {
1531 li = lp->ll_li->li_next;
1532 listitem_remove(lp->ll_list, lp->ll_li);
1533 lp->ll_li = li;
1534 ++lp->ll_n1;
1535 }
1536 }
1537 else
1538 {
1539 if (lp->ll_list != NULL)
1540 // unlet a List item.
1541 listitem_remove(lp->ll_list, lp->ll_li);
1542 else
1543 // unlet a Dictionary item.
1544 dictitem_remove(lp->ll_dict, lp->ll_di);
1545 }
1546
1547 return ret;
1548}
1549
1550/*
1551 * "unlet" a variable. Return OK if it existed, FAIL if not.
1552 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1553 */
1554 int
1555do_unlet(char_u *name, int forceit)
1556{
1557 hashtab_T *ht;
1558 hashitem_T *hi;
1559 char_u *varname;
1560 dict_T *d;
1561 dictitem_T *di;
1562
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001563 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1564 && check_vim9_unlet(name) == FAIL)
1565 return FAIL;
1566
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001567 ht = find_var_ht(name, &varname);
1568 if (ht != NULL && *varname != NUL)
1569 {
1570 d = get_current_funccal_dict(ht);
1571 if (d == NULL)
1572 {
1573 if (ht == &globvarht)
1574 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001575 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001576 d = &vimvardict;
1577 else
1578 {
1579 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1580 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1581 }
1582 if (d == NULL)
1583 {
1584 internal_error("do_unlet()");
1585 return FAIL;
1586 }
1587 }
1588 hi = hash_find(ht, varname);
1589 if (HASHITEM_EMPTY(hi))
1590 hi = find_hi_in_scoped_ht(name, &ht);
1591 if (hi != NULL && !HASHITEM_EMPTY(hi))
1592 {
1593 di = HI2DI(hi);
1594 if (var_check_fixed(di->di_flags, name, FALSE)
1595 || var_check_ro(di->di_flags, name, FALSE)
1596 || var_check_lock(d->dv_lock, name, FALSE))
1597 return FAIL;
1598
1599 delete_var(ht, hi);
1600 return OK;
1601 }
1602 }
1603 if (forceit)
1604 return OK;
1605 semsg(_("E108: No such variable: \"%s\""), name);
1606 return FAIL;
1607}
1608
1609/*
1610 * Lock or unlock variable indicated by "lp".
1611 * "deep" is the levels to go (-1 for unlimited);
1612 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1613 */
1614 static int
1615do_lock_var(
1616 lval_T *lp,
1617 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001618 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001619 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001620 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001621{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001622 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001623 int ret = OK;
1624 int cc;
1625 dictitem_T *di;
1626
1627 if (deep == 0) // nothing to do
1628 return OK;
1629
1630 if (lp->ll_tv == NULL)
1631 {
1632 cc = *name_end;
1633 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001634 if (*lp->ll_name == '$')
1635 {
1636 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001637 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001638 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001639 else
1640 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001641 // Normal name or expanded name.
1642 di = find_var(lp->ll_name, NULL, TRUE);
1643 if (di == NULL)
1644 ret = FAIL;
1645 else if ((di->di_flags & DI_FLAGS_FIX)
1646 && di->di_tv.v_type != VAR_DICT
1647 && di->di_tv.v_type != VAR_LIST)
1648 {
1649 // For historic reasons this error is not given for a list or
1650 // dict. E.g., the b: dict could be locked/unlocked.
1651 semsg(_(e_lock_unlock), lp->ll_name);
1652 ret = FAIL;
1653 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001654 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001655 {
1656 if (lock)
1657 di->di_flags |= DI_FLAGS_LOCK;
1658 else
1659 di->di_flags &= ~DI_FLAGS_LOCK;
1660 item_lock(&di->di_tv, deep, lock);
1661 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001662 }
1663 *name_end = cc;
1664 }
1665 else if (lp->ll_range)
1666 {
1667 listitem_T *li = lp->ll_li;
1668
1669 // (un)lock a range of List items.
1670 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1671 {
1672 item_lock(&li->li_tv, deep, lock);
1673 li = li->li_next;
1674 ++lp->ll_n1;
1675 }
1676 }
1677 else if (lp->ll_list != NULL)
1678 // (un)lock a List item.
1679 item_lock(&lp->ll_li->li_tv, deep, lock);
1680 else
1681 // (un)lock a Dictionary item.
1682 item_lock(&lp->ll_di->di_tv, deep, lock);
1683
1684 return ret;
1685}
1686
1687/*
1688 * Lock or unlock an item. "deep" is nr of levels to go.
1689 */
1690 static void
1691item_lock(typval_T *tv, int deep, int lock)
1692{
1693 static int recurse = 0;
1694 list_T *l;
1695 listitem_T *li;
1696 dict_T *d;
1697 blob_T *b;
1698 hashitem_T *hi;
1699 int todo;
1700
1701 if (recurse >= DICT_MAXNEST)
1702 {
1703 emsg(_("E743: variable nested too deep for (un)lock"));
1704 return;
1705 }
1706 if (deep == 0)
1707 return;
1708 ++recurse;
1709
1710 // lock/unlock the item itself
1711 if (lock)
1712 tv->v_lock |= VAR_LOCKED;
1713 else
1714 tv->v_lock &= ~VAR_LOCKED;
1715
1716 switch (tv->v_type)
1717 {
1718 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001719 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001721 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001722 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001723 case VAR_STRING:
1724 case VAR_FUNC:
1725 case VAR_PARTIAL:
1726 case VAR_FLOAT:
1727 case VAR_SPECIAL:
1728 case VAR_JOB:
1729 case VAR_CHANNEL:
1730 break;
1731
1732 case VAR_BLOB:
1733 if ((b = tv->vval.v_blob) != NULL)
1734 {
1735 if (lock)
1736 b->bv_lock |= VAR_LOCKED;
1737 else
1738 b->bv_lock &= ~VAR_LOCKED;
1739 }
1740 break;
1741 case VAR_LIST:
1742 if ((l = tv->vval.v_list) != NULL)
1743 {
1744 if (lock)
1745 l->lv_lock |= VAR_LOCKED;
1746 else
1747 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001748 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001749 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001750 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001751 item_lock(&li->li_tv, deep - 1, lock);
1752 }
1753 break;
1754 case VAR_DICT:
1755 if ((d = tv->vval.v_dict) != NULL)
1756 {
1757 if (lock)
1758 d->dv_lock |= VAR_LOCKED;
1759 else
1760 d->dv_lock &= ~VAR_LOCKED;
1761 if (deep < 0 || deep > 1)
1762 {
1763 // recursive: lock/unlock the items the List contains
1764 todo = (int)d->dv_hashtab.ht_used;
1765 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1766 {
1767 if (!HASHITEM_EMPTY(hi))
1768 {
1769 --todo;
1770 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
1771 }
1772 }
1773 }
1774 }
1775 }
1776 --recurse;
1777}
1778
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001779#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1780/*
1781 * Delete all "menutrans_" variables.
1782 */
1783 void
1784del_menutrans_vars(void)
1785{
1786 hashitem_T *hi;
1787 int todo;
1788
1789 hash_lock(&globvarht);
1790 todo = (int)globvarht.ht_used;
1791 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1792 {
1793 if (!HASHITEM_EMPTY(hi))
1794 {
1795 --todo;
1796 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1797 delete_var(&globvarht, hi);
1798 }
1799 }
1800 hash_unlock(&globvarht);
1801}
1802#endif
1803
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001804/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001805 * Local string buffer for the next two functions to store a variable name
1806 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1807 * get_user_var_name().
1808 */
1809
1810static char_u *varnamebuf = NULL;
1811static int varnamebuflen = 0;
1812
1813/*
1814 * Function to concatenate a prefix and a variable name.
1815 */
1816 static char_u *
1817cat_prefix_varname(int prefix, char_u *name)
1818{
1819 int len;
1820
1821 len = (int)STRLEN(name) + 3;
1822 if (len > varnamebuflen)
1823 {
1824 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001825 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001826 varnamebuf = alloc(len);
1827 if (varnamebuf == NULL)
1828 {
1829 varnamebuflen = 0;
1830 return NULL;
1831 }
1832 varnamebuflen = len;
1833 }
1834 *varnamebuf = prefix;
1835 varnamebuf[1] = ':';
1836 STRCPY(varnamebuf + 2, name);
1837 return varnamebuf;
1838}
1839
1840/*
1841 * Function given to ExpandGeneric() to obtain the list of user defined
1842 * (global/buffer/window/built-in) variable names.
1843 */
1844 char_u *
1845get_user_var_name(expand_T *xp, int idx)
1846{
1847 static long_u gdone;
1848 static long_u bdone;
1849 static long_u wdone;
1850 static long_u tdone;
1851 static int vidx;
1852 static hashitem_T *hi;
1853 hashtab_T *ht;
1854
1855 if (idx == 0)
1856 {
1857 gdone = bdone = wdone = vidx = 0;
1858 tdone = 0;
1859 }
1860
1861 // Global variables
1862 if (gdone < globvarht.ht_used)
1863 {
1864 if (gdone++ == 0)
1865 hi = globvarht.ht_array;
1866 else
1867 ++hi;
1868 while (HASHITEM_EMPTY(hi))
1869 ++hi;
1870 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1871 return cat_prefix_varname('g', hi->hi_key);
1872 return hi->hi_key;
1873 }
1874
1875 // b: variables
1876 ht = &curbuf->b_vars->dv_hashtab;
1877 if (bdone < ht->ht_used)
1878 {
1879 if (bdone++ == 0)
1880 hi = ht->ht_array;
1881 else
1882 ++hi;
1883 while (HASHITEM_EMPTY(hi))
1884 ++hi;
1885 return cat_prefix_varname('b', hi->hi_key);
1886 }
1887
1888 // w: variables
1889 ht = &curwin->w_vars->dv_hashtab;
1890 if (wdone < ht->ht_used)
1891 {
1892 if (wdone++ == 0)
1893 hi = ht->ht_array;
1894 else
1895 ++hi;
1896 while (HASHITEM_EMPTY(hi))
1897 ++hi;
1898 return cat_prefix_varname('w', hi->hi_key);
1899 }
1900
1901 // t: variables
1902 ht = &curtab->tp_vars->dv_hashtab;
1903 if (tdone < ht->ht_used)
1904 {
1905 if (tdone++ == 0)
1906 hi = ht->ht_array;
1907 else
1908 ++hi;
1909 while (HASHITEM_EMPTY(hi))
1910 ++hi;
1911 return cat_prefix_varname('t', hi->hi_key);
1912 }
1913
1914 // v: variables
1915 if (vidx < VV_LEN)
1916 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
1917
1918 VIM_CLEAR(varnamebuf);
1919 varnamebuflen = 0;
1920 return NULL;
1921}
1922
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001923 char *
1924get_var_special_name(int nr)
1925{
1926 switch (nr)
1927 {
1928 case VVAL_FALSE: return "v:false";
1929 case VVAL_TRUE: return "v:true";
1930 case VVAL_NONE: return "v:none";
1931 case VVAL_NULL: return "v:null";
1932 }
1933 internal_error("get_var_special_name()");
1934 return "42";
1935}
1936
1937/*
1938 * Returns the global variable dictionary
1939 */
1940 dict_T *
1941get_globvar_dict(void)
1942{
1943 return &globvardict;
1944}
1945
1946/*
1947 * Returns the global variable hash table
1948 */
1949 hashtab_T *
1950get_globvar_ht(void)
1951{
1952 return &globvarht;
1953}
1954
1955/*
1956 * Returns the v: variable dictionary
1957 */
1958 dict_T *
1959get_vimvar_dict(void)
1960{
1961 return &vimvardict;
1962}
1963
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001964/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001966 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001967 */
1968 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001969find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001970{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001971 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
1972 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001973
1974 if (di == NULL)
1975 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001976 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
1978 return (int)(vv - vimvars);
1979}
1980
1981
1982/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02001983 * Set type of v: variable to "type".
1984 */
1985 void
1986set_vim_var_type(int idx, vartype_T type)
1987{
1988 vimvars[idx].vv_type = type;
1989}
1990
1991/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001992 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001993 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001994 */
1995 void
1996set_vim_var_nr(int idx, varnumber_T val)
1997{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001998 vimvars[idx].vv_nr = val;
1999}
2000
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002001 char *
2002get_vim_var_name(int idx)
2003{
2004 return vimvars[idx].vv_name;
2005}
2006
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002007/*
2008 * Get typval_T v: variable value.
2009 */
2010 typval_T *
2011get_vim_var_tv(int idx)
2012{
2013 return &vimvars[idx].vv_tv;
2014}
2015
2016/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002017 * Set v: variable to "tv". Only accepts the same type.
2018 * Takes over the value of "tv".
2019 */
2020 int
2021set_vim_var_tv(int idx, typval_T *tv)
2022{
2023 if (vimvars[idx].vv_type != tv->v_type)
2024 {
2025 emsg(_("E1063: type mismatch for v: variable"));
2026 clear_tv(tv);
2027 return FAIL;
2028 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002029 // VV_RO is also checked when compiling, but let's check here as well.
2030 if (vimvars[idx].vv_flags & VV_RO)
2031 {
2032 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2033 return FAIL;
2034 }
2035 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2036 {
2037 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2038 return FAIL;
2039 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002040 clear_tv(&vimvars[idx].vv_di.di_tv);
2041 vimvars[idx].vv_di.di_tv = *tv;
2042 return OK;
2043}
2044
2045/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002046 * Get number v: variable value.
2047 */
2048 varnumber_T
2049get_vim_var_nr(int idx)
2050{
2051 return vimvars[idx].vv_nr;
2052}
2053
2054/*
2055 * Get string v: variable value. Uses a static buffer, can only be used once.
2056 * If the String variable has never been set, return an empty string.
2057 * Never returns NULL;
2058 */
2059 char_u *
2060get_vim_var_str(int idx)
2061{
2062 return tv_get_string(&vimvars[idx].vv_tv);
2063}
2064
2065/*
2066 * Get List v: variable value. Caller must take care of reference count when
2067 * needed.
2068 */
2069 list_T *
2070get_vim_var_list(int idx)
2071{
2072 return vimvars[idx].vv_list;
2073}
2074
2075/*
2076 * Get Dict v: variable value. Caller must take care of reference count when
2077 * needed.
2078 */
2079 dict_T *
2080get_vim_var_dict(int idx)
2081{
2082 return vimvars[idx].vv_dict;
2083}
2084
2085/*
2086 * Set v:char to character "c".
2087 */
2088 void
2089set_vim_var_char(int c)
2090{
2091 char_u buf[MB_MAXBYTES + 1];
2092
2093 if (has_mbyte)
2094 buf[(*mb_char2bytes)(c, buf)] = NUL;
2095 else
2096 {
2097 buf[0] = c;
2098 buf[1] = NUL;
2099 }
2100 set_vim_var_string(VV_CHAR, buf, -1);
2101}
2102
2103/*
2104 * Set v:count to "count" and v:count1 to "count1".
2105 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2106 */
2107 void
2108set_vcount(
2109 long count,
2110 long count1,
2111 int set_prevcount)
2112{
2113 if (set_prevcount)
2114 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2115 vimvars[VV_COUNT].vv_nr = count;
2116 vimvars[VV_COUNT1].vv_nr = count1;
2117}
2118
2119/*
2120 * Save variables that might be changed as a side effect. Used when executing
2121 * a timer callback.
2122 */
2123 void
2124save_vimvars(vimvars_save_T *vvsave)
2125{
2126 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2127 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2128 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2129}
2130
2131/*
2132 * Restore variables saved by save_vimvars().
2133 */
2134 void
2135restore_vimvars(vimvars_save_T *vvsave)
2136{
2137 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2138 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2139 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2140}
2141
2142/*
2143 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2144 * value.
2145 */
2146 void
2147set_vim_var_string(
2148 int idx,
2149 char_u *val,
2150 int len) // length of "val" to use or -1 (whole string)
2151{
2152 clear_tv(&vimvars[idx].vv_di.di_tv);
2153 vimvars[idx].vv_type = VAR_STRING;
2154 if (val == NULL)
2155 vimvars[idx].vv_str = NULL;
2156 else if (len == -1)
2157 vimvars[idx].vv_str = vim_strsave(val);
2158 else
2159 vimvars[idx].vv_str = vim_strnsave(val, len);
2160}
2161
2162/*
2163 * Set List v: variable to "val".
2164 */
2165 void
2166set_vim_var_list(int idx, list_T *val)
2167{
2168 clear_tv(&vimvars[idx].vv_di.di_tv);
2169 vimvars[idx].vv_type = VAR_LIST;
2170 vimvars[idx].vv_list = val;
2171 if (val != NULL)
2172 ++val->lv_refcount;
2173}
2174
2175/*
2176 * Set Dictionary v: variable to "val".
2177 */
2178 void
2179set_vim_var_dict(int idx, dict_T *val)
2180{
2181 clear_tv(&vimvars[idx].vv_di.di_tv);
2182 vimvars[idx].vv_type = VAR_DICT;
2183 vimvars[idx].vv_dict = val;
2184 if (val != NULL)
2185 {
2186 ++val->dv_refcount;
2187 dict_set_items_ro(val);
2188 }
2189}
2190
2191/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002192 * Set the v:argv list.
2193 */
2194 void
2195set_argv_var(char **argv, int argc)
2196{
2197 list_T *l = list_alloc();
2198 int i;
2199
2200 if (l == NULL)
2201 getout(1);
2202 l->lv_lock = VAR_FIXED;
2203 for (i = 0; i < argc; ++i)
2204 {
2205 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2206 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002207 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002208 }
2209 set_vim_var_list(VV_ARGV, l);
2210}
2211
2212/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002213 * Reset v:register, taking the 'clipboard' setting into account.
2214 */
2215 void
2216reset_reg_var(void)
2217{
2218 int regname = 0;
2219
2220 // Adjust the register according to 'clipboard', so that when
2221 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2222#ifdef FEAT_CLIPBOARD
2223 adjust_clip_reg(&regname);
2224#endif
2225 set_reg_var(regname);
2226}
2227
2228/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002229 * Set v:register if needed.
2230 */
2231 void
2232set_reg_var(int c)
2233{
2234 char_u regname;
2235
2236 if (c == 0 || c == ' ')
2237 regname = '"';
2238 else
2239 regname = c;
2240 // Avoid free/alloc when the value is already right.
2241 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2242 set_vim_var_string(VV_REG, &regname, 1);
2243}
2244
2245/*
2246 * Get or set v:exception. If "oldval" == NULL, return the current value.
2247 * Otherwise, restore the value to "oldval" and return NULL.
2248 * Must always be called in pairs to save and restore v:exception! Does not
2249 * take care of memory allocations.
2250 */
2251 char_u *
2252v_exception(char_u *oldval)
2253{
2254 if (oldval == NULL)
2255 return vimvars[VV_EXCEPTION].vv_str;
2256
2257 vimvars[VV_EXCEPTION].vv_str = oldval;
2258 return NULL;
2259}
2260
2261/*
2262 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2263 * Otherwise, restore the value to "oldval" and return NULL.
2264 * Must always be called in pairs to save and restore v:throwpoint! Does not
2265 * take care of memory allocations.
2266 */
2267 char_u *
2268v_throwpoint(char_u *oldval)
2269{
2270 if (oldval == NULL)
2271 return vimvars[VV_THROWPOINT].vv_str;
2272
2273 vimvars[VV_THROWPOINT].vv_str = oldval;
2274 return NULL;
2275}
2276
2277/*
2278 * Set v:cmdarg.
2279 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2280 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2281 * Must always be called in pairs!
2282 */
2283 char_u *
2284set_cmdarg(exarg_T *eap, char_u *oldarg)
2285{
2286 char_u *oldval;
2287 char_u *newval;
2288 unsigned len;
2289
2290 oldval = vimvars[VV_CMDARG].vv_str;
2291 if (eap == NULL)
2292 {
2293 vim_free(oldval);
2294 vimvars[VV_CMDARG].vv_str = oldarg;
2295 return NULL;
2296 }
2297
2298 if (eap->force_bin == FORCE_BIN)
2299 len = 6;
2300 else if (eap->force_bin == FORCE_NOBIN)
2301 len = 8;
2302 else
2303 len = 0;
2304
2305 if (eap->read_edit)
2306 len += 7;
2307
2308 if (eap->force_ff != 0)
2309 len += 10; // " ++ff=unix"
2310 if (eap->force_enc != 0)
2311 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2312 if (eap->bad_char != 0)
2313 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2314
2315 newval = alloc(len + 1);
2316 if (newval == NULL)
2317 return NULL;
2318
2319 if (eap->force_bin == FORCE_BIN)
2320 sprintf((char *)newval, " ++bin");
2321 else if (eap->force_bin == FORCE_NOBIN)
2322 sprintf((char *)newval, " ++nobin");
2323 else
2324 *newval = NUL;
2325
2326 if (eap->read_edit)
2327 STRCAT(newval, " ++edit");
2328
2329 if (eap->force_ff != 0)
2330 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2331 eap->force_ff == 'u' ? "unix"
2332 : eap->force_ff == 'd' ? "dos"
2333 : "mac");
2334 if (eap->force_enc != 0)
2335 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2336 eap->cmd + eap->force_enc);
2337 if (eap->bad_char == BAD_KEEP)
2338 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2339 else if (eap->bad_char == BAD_DROP)
2340 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2341 else if (eap->bad_char != 0)
2342 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2343 vimvars[VV_CMDARG].vv_str = newval;
2344 return oldval;
2345}
2346
2347/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002348 * Get the value of internal variable "name".
2349 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2350 */
2351 int
2352get_var_tv(
2353 char_u *name,
2354 int len, // length of "name"
2355 typval_T *rettv, // NULL when only checking existence
2356 dictitem_T **dip, // non-NULL when typval's dict item is needed
2357 int verbose, // may give error message
2358 int no_autoload) // do not use script autoloading
2359{
2360 int ret = OK;
2361 typval_T *tv = NULL;
2362 dictitem_T *v;
2363 int cc;
2364
2365 // truncate the name, so that we can use strcmp()
2366 cc = name[len];
2367 name[len] = NUL;
2368
2369 // Check for user-defined variables.
2370 v = find_var(name, NULL, no_autoload);
2371 if (v != NULL)
2372 {
2373 tv = &v->di_tv;
2374 if (dip != NULL)
2375 *dip = v;
2376 }
2377
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002378 if (tv == NULL && (current_sctx.sc_version == SCRIPT_VERSION_VIM9
2379 || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002380 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002381 imported_T *import;
2382 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2383
2384 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385
2386 // imported variable from another script
2387 if (import != NULL)
2388 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002389 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2391 + import->imp_var_vals_idx;
2392 tv = sv->sv_tv;
2393 }
2394 }
2395
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002396 if (tv == NULL)
2397 {
2398 if (rettv != NULL && verbose)
2399 semsg(_(e_undefvar), name);
2400 ret = FAIL;
2401 }
2402 else if (rettv != NULL)
2403 copy_tv(tv, rettv);
2404
2405 name[len] = cc;
2406
2407 return ret;
2408}
2409
2410/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002411 * Check if variable "name[len]" is a local variable or an argument.
2412 * If so, "*eval_lavars_used" is set to TRUE.
2413 */
2414 void
2415check_vars(char_u *name, int len)
2416{
2417 int cc;
2418 char_u *varname;
2419 hashtab_T *ht;
2420
2421 if (eval_lavars_used == NULL)
2422 return;
2423
2424 // truncate the name, so that we can use strcmp()
2425 cc = name[len];
2426 name[len] = NUL;
2427
2428 ht = find_var_ht(name, &varname);
2429 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2430 {
2431 if (find_var(name, NULL, TRUE) != NULL)
2432 *eval_lavars_used = TRUE;
2433 }
2434
2435 name[len] = cc;
2436}
2437
2438/*
2439 * Find variable "name" in the list of variables.
2440 * Return a pointer to it if found, NULL if not found.
2441 * Careful: "a:0" variables don't have a name.
2442 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2443 * hashtab_T used.
2444 */
2445 dictitem_T *
2446find_var(char_u *name, hashtab_T **htp, int no_autoload)
2447{
2448 char_u *varname;
2449 hashtab_T *ht;
2450 dictitem_T *ret = NULL;
2451
2452 ht = find_var_ht(name, &varname);
2453 if (htp != NULL)
2454 *htp = ht;
2455 if (ht == NULL)
2456 return NULL;
2457 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2458 if (ret != NULL)
2459 return ret;
2460
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002461 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002462 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2463}
2464
2465/*
2466 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002467 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002468 * Returns NULL if not found.
2469 */
2470 dictitem_T *
2471find_var_in_ht(
2472 hashtab_T *ht,
2473 int htname,
2474 char_u *varname,
2475 int no_autoload)
2476{
2477 hashitem_T *hi;
2478
2479 if (*varname == NUL)
2480 {
2481 // Must be something like "s:", otherwise "ht" would be NULL.
2482 switch (htname)
2483 {
2484 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2485 case 'g': return &globvars_var;
2486 case 'v': return &vimvars_var;
2487 case 'b': return &curbuf->b_bufvar;
2488 case 'w': return &curwin->w_winvar;
2489 case 't': return &curtab->tp_winvar;
2490 case 'l': return get_funccal_local_var();
2491 case 'a': return get_funccal_args_var();
2492 }
2493 return NULL;
2494 }
2495
2496 hi = hash_find(ht, varname);
2497 if (HASHITEM_EMPTY(hi))
2498 {
2499 // For global variables we may try auto-loading the script. If it
2500 // worked find the variable again. Don't auto-load a script if it was
2501 // loaded already, otherwise it would be loaded every time when
2502 // checking if a function name is a Funcref variable.
2503 if (ht == &globvarht && !no_autoload)
2504 {
2505 // Note: script_autoload() may make "hi" invalid. It must either
2506 // be obtained again or not used.
2507 if (!script_autoload(varname, FALSE) || aborting())
2508 return NULL;
2509 hi = hash_find(ht, varname);
2510 }
2511 if (HASHITEM_EMPTY(hi))
2512 return NULL;
2513 }
2514 return HI2DI(hi);
2515}
2516
2517/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 * Get the script-local hashtab. NULL if not in a script context.
2519 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02002520 static hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521get_script_local_ht(void)
2522{
2523 scid_T sid = current_sctx.sc_sid;
2524
2525 if (sid > 0 && sid <= script_items.ga_len)
2526 return &SCRIPT_VARS(sid);
2527 return NULL;
2528}
2529
2530/*
2531 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002532 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002534 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2536{
2537 hashtab_T *ht = get_script_local_ht();
2538 char_u buffer[30];
2539 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002540 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541 hashitem_T *hi;
2542
2543 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002544 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 if (len < sizeof(buffer) - 1)
2546 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002547 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002548 vim_strncpy(buffer, name, len);
2549 p = buffer;
2550 }
2551 else
2552 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002553 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002555 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002556 }
2557
2558 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002559 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560
2561 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002562 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2563 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564
2565 if (p != buffer)
2566 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002567 // Don't return "buffer", gcc complains.
2568 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002569}
2570
2571/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002572 * Find the hashtab used for a variable name.
2573 * Return NULL if the name is not valid.
2574 * Set "varname" to the start of name without ':'.
2575 */
2576 hashtab_T *
2577find_var_ht(char_u *name, char_u **varname)
2578{
2579 hashitem_T *hi;
2580 hashtab_T *ht;
2581
2582 if (name[0] == NUL)
2583 return NULL;
2584 if (name[1] != ':')
2585 {
2586 // The name must not start with a colon or #.
2587 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2588 return NULL;
2589 *varname = name;
2590
2591 // "version" is "v:version" in all scopes if scriptversion < 3.
2592 // Same for a few other variables marked with VV_COMPAT.
2593 if (current_sctx.sc_version < 3)
2594 {
2595 hi = hash_find(&compat_hashtab, name);
2596 if (!HASHITEM_EMPTY(hi))
2597 return &compat_hashtab;
2598 }
2599
2600 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601 if (ht != NULL)
2602 return ht; // local variable
2603
2604 // in Vim9 script items at the script level are script-local
2605 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2606 {
2607 ht = get_script_local_ht();
2608 if (ht != NULL)
2609 return ht;
2610 }
2611
2612 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002613 }
2614 *varname = name + 2;
2615 if (*name == 'g') // global variable
2616 return &globvarht;
2617 // There must be no ':' or '#' in the rest of the name, unless g: is used
2618 if (vim_strchr(name + 2, ':') != NULL
2619 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2620 return NULL;
2621 if (*name == 'b') // buffer variable
2622 return &curbuf->b_vars->dv_hashtab;
2623 if (*name == 'w') // window variable
2624 return &curwin->w_vars->dv_hashtab;
2625 if (*name == 't') // tab page variable
2626 return &curtab->tp_vars->dv_hashtab;
2627 if (*name == 'v') // v: variable
2628 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002629 if (get_current_funccal() != NULL
Bram Moolenaar822ba242020-05-24 23:00:18 +02002630 && get_current_funccal()->func->uf_dfunc_idx == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002631 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002632 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 if (*name == 'a') // a: function argument
2634 return get_funccal_args_ht();
2635 if (*name == 'l') // l: local function variable
2636 return get_funccal_local_ht();
2637 }
2638 if (*name == 's') // script variable
2639 {
2640 ht = get_script_local_ht();
2641 if (ht != NULL)
2642 return ht;
2643 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002644 return NULL;
2645}
2646
2647/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002648 * Get the string value of a (global/local) variable.
2649 * Note: see tv_get_string() for how long the pointer remains valid.
2650 * Returns NULL when it doesn't exist.
2651 */
2652 char_u *
2653get_var_value(char_u *name)
2654{
2655 dictitem_T *v;
2656
2657 v = find_var(name, NULL, FALSE);
2658 if (v == NULL)
2659 return NULL;
2660 return tv_get_string(&v->di_tv);
2661}
2662
2663/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002664 * Allocate a new hashtab for a sourced script. It will be used while
2665 * sourcing this script and when executing functions defined in the script.
2666 */
2667 void
2668new_script_vars(scid_T id)
2669{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002670 scriptvar_T *sv;
2671
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002672 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2673 if (sv == NULL)
2674 return;
2675 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002676 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002677}
2678
2679/*
2680 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2681 * point to it.
2682 */
2683 void
2684init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2685{
2686 hash_init(&dict->dv_hashtab);
2687 dict->dv_lock = 0;
2688 dict->dv_scope = scope;
2689 dict->dv_refcount = DO_NOT_FREE_CNT;
2690 dict->dv_copyID = 0;
2691 dict_var->di_tv.vval.v_dict = dict;
2692 dict_var->di_tv.v_type = VAR_DICT;
2693 dict_var->di_tv.v_lock = VAR_FIXED;
2694 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2695 dict_var->di_key[0] = NUL;
2696}
2697
2698/*
2699 * Unreference a dictionary initialized by init_var_dict().
2700 */
2701 void
2702unref_var_dict(dict_T *dict)
2703{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002704 // Now the dict needs to be freed if no one else is using it, go back to
2705 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002706 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2707 dict_unref(dict);
2708}
2709
2710/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002711 * Clean up a list of internal variables.
2712 * Frees all allocated variables and the value they contain.
2713 * Clears hashtab "ht", does not free it.
2714 */
2715 void
2716vars_clear(hashtab_T *ht)
2717{
2718 vars_clear_ext(ht, TRUE);
2719}
2720
2721/*
2722 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2723 */
2724 void
2725vars_clear_ext(hashtab_T *ht, int free_val)
2726{
2727 int todo;
2728 hashitem_T *hi;
2729 dictitem_T *v;
2730
2731 hash_lock(ht);
2732 todo = (int)ht->ht_used;
2733 for (hi = ht->ht_array; todo > 0; ++hi)
2734 {
2735 if (!HASHITEM_EMPTY(hi))
2736 {
2737 --todo;
2738
2739 // Free the variable. Don't remove it from the hashtab,
2740 // ht_array might change then. hash_clear() takes care of it
2741 // later.
2742 v = HI2DI(hi);
2743 if (free_val)
2744 clear_tv(&v->di_tv);
2745 if (v->di_flags & DI_FLAGS_ALLOC)
2746 vim_free(v);
2747 }
2748 }
2749 hash_clear(ht);
2750 ht->ht_used = 0;
2751}
2752
2753/*
2754 * Delete a variable from hashtab "ht" at item "hi".
2755 * Clear the variable value and free the dictitem.
2756 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002757 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002758delete_var(hashtab_T *ht, hashitem_T *hi)
2759{
2760 dictitem_T *di = HI2DI(hi);
2761
2762 hash_remove(ht, hi);
2763 clear_tv(&di->di_tv);
2764 vim_free(di);
2765}
2766
2767/*
2768 * List the value of one internal variable.
2769 */
2770 static void
2771list_one_var(dictitem_T *v, char *prefix, int *first)
2772{
2773 char_u *tofree;
2774 char_u *s;
2775 char_u numbuf[NUMBUFLEN];
2776
2777 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2778 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2779 s == NULL ? (char_u *)"" : s, first);
2780 vim_free(tofree);
2781}
2782
2783 static void
2784list_one_var_a(
2785 char *prefix,
2786 char_u *name,
2787 int type,
2788 char_u *string,
2789 int *first) // when TRUE clear rest of screen and set to FALSE
2790{
2791 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2792 msg_start();
2793 msg_puts(prefix);
2794 if (name != NULL) // "a:" vars don't have a name stored
2795 msg_puts((char *)name);
2796 msg_putchar(' ');
2797 msg_advance(22);
2798 if (type == VAR_NUMBER)
2799 msg_putchar('#');
2800 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2801 msg_putchar('*');
2802 else if (type == VAR_LIST)
2803 {
2804 msg_putchar('[');
2805 if (*string == '[')
2806 ++string;
2807 }
2808 else if (type == VAR_DICT)
2809 {
2810 msg_putchar('{');
2811 if (*string == '{')
2812 ++string;
2813 }
2814 else
2815 msg_putchar(' ');
2816
2817 msg_outtrans(string);
2818
2819 if (type == VAR_FUNC || type == VAR_PARTIAL)
2820 msg_puts("()");
2821 if (*first)
2822 {
2823 msg_clr_eos();
2824 *first = FALSE;
2825 }
2826}
2827
2828/*
2829 * Set variable "name" to value in "tv".
2830 * If the variable already exists, the value is updated.
2831 * Otherwise the variable is created.
2832 */
2833 void
2834set_var(
2835 char_u *name,
2836 typval_T *tv,
2837 int copy) // make copy of value in "tv"
2838{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839 set_var_const(name, NULL, tv, copy, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002840}
2841
2842/*
2843 * Set variable "name" to value in "tv".
2844 * If the variable already exists and "is_const" is FALSE the value is updated.
2845 * Otherwise the variable is created.
2846 */
2847 void
2848set_var_const(
2849 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 type_T *type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002851 typval_T *tv,
2852 int copy, // make copy of value in "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 int flags) // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002854{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002856 char_u *varname;
2857 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 int is_script_local;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002859
2860 ht = find_var_ht(name, &varname);
2861 if (ht == NULL || *varname == NUL)
2862 {
2863 semsg(_(e_illvar), name);
2864 return;
2865 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 is_script_local = ht == get_script_local_ht();
2867
2868 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002869
2870 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871 if (di == NULL)
2872 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002873
2874 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 && var_check_func_name(name, di == NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002876 return;
2877
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002879 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002880 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002881 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 if (flags & LET_IS_CONST)
2883 {
2884 emsg(_(e_cannot_mod));
2885 return;
2886 }
2887
2888 if (var_check_ro(di->di_flags, name, FALSE)
2889 || var_check_lock(di->di_tv.v_lock, name, FALSE))
2890 return;
2891
Bram Moolenaar34db91f2020-06-13 19:00:10 +02002892 if (is_script_local
2893 && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 {
Bram Moolenaar34db91f2020-06-13 19:00:10 +02002895 if ((flags & LET_NO_COMMAND) == 0)
2896 {
2897 semsg(_("E1041: Redefining script item %s"), name);
2898 return;
2899 }
2900
2901 // check the type
2902 check_script_var_type(&di->di_tv, tv, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002904 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905 else
2906 // can only redefine once
2907 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002908
2909 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002910
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002912 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002913 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002914 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002916 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002918 if (copy || tv->v_type != VAR_STRING)
2919 {
2920 char_u *val = tv_get_string(tv);
2921
2922 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002923 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 if (di->di_tv.vval.v_string == NULL)
2925 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002926 }
2927 else
2928 {
2929 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002931 tv->vval.v_string = NULL;
2932 }
2933 return;
2934 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002936 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002938 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002940#ifdef FEAT_SEARCH_EXTRA
2941 else if (STRCMP(varname, "hlsearch") == 0)
2942 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002944 redraw_all_later(SOME_VALID);
2945 }
2946#endif
2947 return;
2948 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002950 {
2951 semsg(_("E963: setting %s to value with wrong type"), name);
2952 return;
2953 }
2954 }
2955
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002957 }
2958 else // add a new variable
2959 {
2960 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002961 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002962 {
2963 semsg(_(e_illvar), name);
2964 return;
2965 }
2966
2967 // Make sure the variable name is valid.
2968 if (!valid_varname(varname))
2969 return;
2970
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
2972 if (di == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002973 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 STRCPY(di->di_key, varname);
2975 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002976 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002978 return;
2979 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 di->di_flags = DI_FLAGS_ALLOC;
2981 if (flags & LET_IS_CONST)
2982 di->di_flags |= DI_FLAGS_LOCK;
2983
2984 if (is_script_local && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2985 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002986 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987
2988 // Store a pointer to the typval_T, so that it can be found by
2989 // index instead of using a hastab lookup.
2990 if (ga_grow(&si->sn_var_vals, 1) == OK)
2991 {
2992 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2993 + si->sn_var_vals.ga_len;
2994 sv->sv_name = di->di_key;
2995 sv->sv_tv = &di->di_tv;
2996 sv->sv_type = type == NULL ? &t_any : type;
2997 sv->sv_const = (flags & LET_IS_CONST);
2998 sv->sv_export = is_export;
2999 ++si->sn_var_vals.ga_len;
3000
3001 // let ex_export() know the export worked.
3002 is_export = FALSE;
3003 }
3004 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003005 }
3006
3007 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003009 else
3010 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 di->di_tv = *tv;
3012 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003013 init_tv(tv);
3014 }
3015
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 if (flags & LET_IS_CONST)
3017 di->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003018}
3019
3020/*
3021 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3022 * Also give an error message.
3023 */
3024 int
3025var_check_ro(int flags, char_u *name, int use_gettext)
3026{
3027 if (flags & DI_FLAGS_RO)
3028 {
3029 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3030 return TRUE;
3031 }
3032 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3033 {
3034 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3035 return TRUE;
3036 }
3037 return FALSE;
3038}
3039
3040/*
3041 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3042 * Also give an error message.
3043 */
3044 int
3045var_check_fixed(int flags, char_u *name, int use_gettext)
3046{
3047 if (flags & DI_FLAGS_FIX)
3048 {
3049 semsg(_("E795: Cannot delete variable %s"),
3050 use_gettext ? (char_u *)_(name) : name);
3051 return TRUE;
3052 }
3053 return FALSE;
3054}
3055
3056/*
3057 * Check if a funcref is assigned to a valid variable name.
3058 * Return TRUE and give an error if not.
3059 */
3060 int
3061var_check_func_name(
3062 char_u *name, // points to start of variable name
3063 int new_var) // TRUE when creating the variable
3064{
3065 // Allow for w: b: s: and t:.
3066 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3067 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3068 ? name[2] : name[0]))
3069 {
3070 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3071 name);
3072 return TRUE;
3073 }
3074 // Don't allow hiding a function. When "v" is not NULL we might be
3075 // assigning another function to the same var, the type is checked
3076 // below.
3077 if (new_var && function_exists(name, FALSE))
3078 {
3079 semsg(_("E705: Variable name conflicts with existing function: %s"),
3080 name);
3081 return TRUE;
3082 }
3083 return FALSE;
3084}
3085
3086/*
3087 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
3088 * Also give an error message, using "name" or _("name") when use_gettext is
3089 * TRUE.
3090 */
3091 int
3092var_check_lock(int lock, char_u *name, int use_gettext)
3093{
3094 if (lock & VAR_LOCKED)
3095 {
3096 semsg(_("E741: Value is locked: %s"),
3097 name == NULL ? (char_u *)_("Unknown")
3098 : use_gettext ? (char_u *)_(name)
3099 : name);
3100 return TRUE;
3101 }
3102 if (lock & VAR_FIXED)
3103 {
3104 semsg(_("E742: Cannot change value of %s"),
3105 name == NULL ? (char_u *)_("Unknown")
3106 : use_gettext ? (char_u *)_(name)
3107 : name);
3108 return TRUE;
3109 }
3110 return FALSE;
3111}
3112
3113/*
3114 * Check if a variable name is valid.
3115 * Return FALSE and give an error if not.
3116 */
3117 int
3118valid_varname(char_u *varname)
3119{
3120 char_u *p;
3121
3122 for (p = varname; *p != NUL; ++p)
3123 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3124 && *p != AUTOLOAD_CHAR)
3125 {
3126 semsg(_(e_illvar), varname);
3127 return FALSE;
3128 }
3129 return TRUE;
3130}
3131
3132/*
3133 * getwinvar() and gettabwinvar()
3134 */
3135 static void
3136getwinvar(
3137 typval_T *argvars,
3138 typval_T *rettv,
3139 int off) // 1 for gettabwinvar()
3140{
3141 win_T *win;
3142 char_u *varname;
3143 dictitem_T *v;
3144 tabpage_T *tp = NULL;
3145 int done = FALSE;
3146 win_T *oldcurwin;
3147 tabpage_T *oldtabpage;
3148 int need_switch_win;
3149
3150 if (off == 1)
3151 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3152 else
3153 tp = curtab;
3154 win = find_win_by_nr(&argvars[off], tp);
3155 varname = tv_get_string_chk(&argvars[off + 1]);
3156 ++emsg_off;
3157
3158 rettv->v_type = VAR_STRING;
3159 rettv->vval.v_string = NULL;
3160
3161 if (win != NULL && varname != NULL)
3162 {
3163 // Set curwin to be our win, temporarily. Also set the tabpage,
3164 // otherwise the window is not valid. Only do this when needed,
3165 // autocommands get blocked.
3166 need_switch_win = !(tp == curtab && win == curwin);
3167 if (!need_switch_win
3168 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3169 {
3170 if (*varname == '&')
3171 {
3172 if (varname[1] == NUL)
3173 {
3174 // get all window-local options in a dict
3175 dict_T *opts = get_winbuf_options(FALSE);
3176
3177 if (opts != NULL)
3178 {
3179 rettv_dict_set(rettv, opts);
3180 done = TRUE;
3181 }
3182 }
3183 else if (get_option_tv(&varname, rettv, 1) == OK)
3184 // window-local-option
3185 done = TRUE;
3186 }
3187 else
3188 {
3189 // Look up the variable.
3190 // Let getwinvar({nr}, "") return the "w:" dictionary.
3191 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3192 varname, FALSE);
3193 if (v != NULL)
3194 {
3195 copy_tv(&v->di_tv, rettv);
3196 done = TRUE;
3197 }
3198 }
3199 }
3200
3201 if (need_switch_win)
3202 // restore previous notion of curwin
3203 restore_win(oldcurwin, oldtabpage, TRUE);
3204 }
3205
3206 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3207 // use the default return value
3208 copy_tv(&argvars[off + 2], rettv);
3209
3210 --emsg_off;
3211}
3212
3213/*
3214 * "setwinvar()" and "settabwinvar()" functions
3215 */
3216 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003217setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003218{
3219 win_T *win;
3220 win_T *save_curwin;
3221 tabpage_T *save_curtab;
3222 int need_switch_win;
3223 char_u *varname, *winvarname;
3224 typval_T *varp;
3225 char_u nbuf[NUMBUFLEN];
3226 tabpage_T *tp = NULL;
3227
3228 if (check_secure())
3229 return;
3230
3231 if (off == 1)
3232 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3233 else
3234 tp = curtab;
3235 win = find_win_by_nr(&argvars[off], tp);
3236 varname = tv_get_string_chk(&argvars[off + 1]);
3237 varp = &argvars[off + 2];
3238
3239 if (win != NULL && varname != NULL && varp != NULL)
3240 {
3241 need_switch_win = !(tp == curtab && win == curwin);
3242 if (!need_switch_win
3243 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3244 {
3245 if (*varname == '&')
3246 {
3247 long numval;
3248 char_u *strval;
3249 int error = FALSE;
3250
3251 ++varname;
3252 numval = (long)tv_get_number_chk(varp, &error);
3253 strval = tv_get_string_buf_chk(varp, nbuf);
3254 if (!error && strval != NULL)
3255 set_option_value(varname, numval, strval, OPT_LOCAL);
3256 }
3257 else
3258 {
3259 winvarname = alloc(STRLEN(varname) + 3);
3260 if (winvarname != NULL)
3261 {
3262 STRCPY(winvarname, "w:");
3263 STRCPY(winvarname + 2, varname);
3264 set_var(winvarname, varp, TRUE);
3265 vim_free(winvarname);
3266 }
3267 }
3268 }
3269 if (need_switch_win)
3270 restore_win(save_curwin, save_curtab, TRUE);
3271 }
3272}
3273
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003274/*
3275 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3276 * v:option_type, and v:option_command.
3277 */
3278 void
3279reset_v_option_vars(void)
3280{
3281 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3282 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3283 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3284 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3285 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3286 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3287}
3288
3289/*
3290 * Add an assert error to v:errors.
3291 */
3292 void
3293assert_error(garray_T *gap)
3294{
3295 struct vimvar *vp = &vimvars[VV_ERRORS];
3296
3297 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003298 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003299 set_vim_var_list(VV_ERRORS, list_alloc());
3300 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3301}
3302
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003303 int
3304var_exists(char_u *var)
3305{
3306 char_u *name;
3307 char_u *tofree;
3308 typval_T tv;
3309 int len = 0;
3310 int n = FALSE;
3311
3312 // get_name_len() takes care of expanding curly braces
3313 name = var;
3314 len = get_name_len(&var, &tofree, TRUE, FALSE);
3315 if (len > 0)
3316 {
3317 if (tofree != NULL)
3318 name = tofree;
3319 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
3320 if (n)
3321 {
3322 // handle d.key, l[idx], f(expr)
Bram Moolenaar32e35112020-05-14 22:41:15 +02003323 n = (handle_subscript(&var, &tv, EVAL_EVALUATE,
3324 FALSE, name, &name) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003325 if (n)
3326 clear_tv(&tv);
3327 }
3328 }
3329 if (*var != NUL)
3330 n = FALSE;
3331
3332 vim_free(tofree);
3333 return n;
3334}
3335
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003336static lval_T *redir_lval = NULL;
3337#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3338static garray_T redir_ga; // only valid when redir_lval is not NULL
3339static char_u *redir_endp = NULL;
3340static char_u *redir_varname = NULL;
3341
3342/*
3343 * Start recording command output to a variable
3344 * When "append" is TRUE append to an existing variable.
3345 * Returns OK if successfully completed the setup. FAIL otherwise.
3346 */
3347 int
3348var_redir_start(char_u *name, int append)
3349{
3350 int save_emsg;
3351 int err;
3352 typval_T tv;
3353
3354 // Catch a bad name early.
3355 if (!eval_isnamec1(*name))
3356 {
3357 emsg(_(e_invarg));
3358 return FAIL;
3359 }
3360
3361 // Make a copy of the name, it is used in redir_lval until redir ends.
3362 redir_varname = vim_strsave(name);
3363 if (redir_varname == NULL)
3364 return FAIL;
3365
3366 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3367 if (redir_lval == NULL)
3368 {
3369 var_redir_stop();
3370 return FAIL;
3371 }
3372
3373 // The output is stored in growarray "redir_ga" until redirection ends.
3374 ga_init2(&redir_ga, (int)sizeof(char), 500);
3375
3376 // Parse the variable name (can be a dict or list entry).
3377 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3378 FNE_CHECK_START);
3379 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3380 {
3381 clear_lval(redir_lval);
3382 if (redir_endp != NULL && *redir_endp != NUL)
3383 // Trailing characters are present after the variable name
3384 emsg(_(e_trailing));
3385 else
3386 emsg(_(e_invarg));
3387 redir_endp = NULL; // don't store a value, only cleanup
3388 var_redir_stop();
3389 return FAIL;
3390 }
3391
3392 // check if we can write to the variable: set it to or append an empty
3393 // string
3394 save_emsg = did_emsg;
3395 did_emsg = FALSE;
3396 tv.v_type = VAR_STRING;
3397 tv.vval.v_string = (char_u *)"";
3398 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003400 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003402 clear_lval(redir_lval);
3403 err = did_emsg;
3404 did_emsg |= save_emsg;
3405 if (err)
3406 {
3407 redir_endp = NULL; // don't store a value, only cleanup
3408 var_redir_stop();
3409 return FAIL;
3410 }
3411
3412 return OK;
3413}
3414
3415/*
3416 * Append "value[value_len]" to the variable set by var_redir_start().
3417 * The actual appending is postponed until redirection ends, because the value
3418 * appended may in fact be the string we write to, changing it may cause freed
3419 * memory to be used:
3420 * :redir => foo
3421 * :let foo
3422 * :redir END
3423 */
3424 void
3425var_redir_str(char_u *value, int value_len)
3426{
3427 int len;
3428
3429 if (redir_lval == NULL)
3430 return;
3431
3432 if (value_len == -1)
3433 len = (int)STRLEN(value); // Append the entire string
3434 else
3435 len = value_len; // Append only "value_len" characters
3436
3437 if (ga_grow(&redir_ga, len) == OK)
3438 {
3439 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3440 redir_ga.ga_len += len;
3441 }
3442 else
3443 var_redir_stop();
3444}
3445
3446/*
3447 * Stop redirecting command output to a variable.
3448 * Frees the allocated memory.
3449 */
3450 void
3451var_redir_stop(void)
3452{
3453 typval_T tv;
3454
3455 if (EVALCMD_BUSY)
3456 {
3457 redir_lval = NULL;
3458 return;
3459 }
3460
3461 if (redir_lval != NULL)
3462 {
3463 // If there was no error: assign the text to the variable.
3464 if (redir_endp != NULL)
3465 {
3466 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3467 tv.v_type = VAR_STRING;
3468 tv.vval.v_string = redir_ga.ga_data;
3469 // Call get_lval() again, if it's inside a Dict or List it may
3470 // have changed.
3471 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3472 FALSE, FALSE, 0, FNE_CHECK_START);
3473 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003475 (char_u *)".");
3476 clear_lval(redir_lval);
3477 }
3478
3479 // free the collected output
3480 VIM_CLEAR(redir_ga.ga_data);
3481
3482 VIM_CLEAR(redir_lval);
3483 }
3484 VIM_CLEAR(redir_varname);
3485}
3486
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003487/*
3488 * "gettabvar()" function
3489 */
3490 void
3491f_gettabvar(typval_T *argvars, typval_T *rettv)
3492{
3493 win_T *oldcurwin;
3494 tabpage_T *tp, *oldtabpage;
3495 dictitem_T *v;
3496 char_u *varname;
3497 int done = FALSE;
3498
3499 rettv->v_type = VAR_STRING;
3500 rettv->vval.v_string = NULL;
3501
3502 varname = tv_get_string_chk(&argvars[1]);
3503 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3504 if (tp != NULL && varname != NULL)
3505 {
3506 // Set tp to be our tabpage, temporarily. Also set the window to the
3507 // first window in the tabpage, otherwise the window is not valid.
3508 if (switch_win(&oldcurwin, &oldtabpage,
3509 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3510 : tp->tp_firstwin, tp, TRUE) == OK)
3511 {
3512 // look up the variable
3513 // Let gettabvar({nr}, "") return the "t:" dictionary.
3514 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3515 if (v != NULL)
3516 {
3517 copy_tv(&v->di_tv, rettv);
3518 done = TRUE;
3519 }
3520 }
3521
3522 // restore previous notion of curwin
3523 restore_win(oldcurwin, oldtabpage, TRUE);
3524 }
3525
3526 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3527 copy_tv(&argvars[2], rettv);
3528}
3529
3530/*
3531 * "gettabwinvar()" function
3532 */
3533 void
3534f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3535{
3536 getwinvar(argvars, rettv, 1);
3537}
3538
3539/*
3540 * "getwinvar()" function
3541 */
3542 void
3543f_getwinvar(typval_T *argvars, typval_T *rettv)
3544{
3545 getwinvar(argvars, rettv, 0);
3546}
3547
3548/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003549 * "getbufvar()" function
3550 */
3551 void
3552f_getbufvar(typval_T *argvars, typval_T *rettv)
3553{
3554 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003555 char_u *varname;
3556 dictitem_T *v;
3557 int done = FALSE;
3558
3559 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3560 varname = tv_get_string_chk(&argvars[1]);
3561 ++emsg_off;
3562 buf = tv_get_buf(&argvars[0], FALSE);
3563
3564 rettv->v_type = VAR_STRING;
3565 rettv->vval.v_string = NULL;
3566
3567 if (buf != NULL && varname != NULL)
3568 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003569 if (*varname == '&')
3570 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003571 buf_T *save_curbuf = curbuf;
3572
3573 // set curbuf to be our buf, temporarily
3574 curbuf = buf;
3575
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003576 if (varname[1] == NUL)
3577 {
3578 // get all buffer-local options in a dict
3579 dict_T *opts = get_winbuf_options(TRUE);
3580
3581 if (opts != NULL)
3582 {
3583 rettv_dict_set(rettv, opts);
3584 done = TRUE;
3585 }
3586 }
3587 else if (get_option_tv(&varname, rettv, TRUE) == OK)
3588 // buffer-local-option
3589 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003590
3591 // restore previous notion of curbuf
3592 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003593 }
3594 else
3595 {
3596 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003597 if (*varname == NUL)
3598 // Let getbufvar({nr}, "") return the "b:" dictionary.
3599 v = &buf->b_bufvar;
3600 else
3601 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3602 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003603 if (v != NULL)
3604 {
3605 copy_tv(&v->di_tv, rettv);
3606 done = TRUE;
3607 }
3608 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003609 }
3610
3611 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3612 // use the default value
3613 copy_tv(&argvars[2], rettv);
3614
3615 --emsg_off;
3616}
3617
3618/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003619 * "settabvar()" function
3620 */
3621 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003622f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003623{
3624 tabpage_T *save_curtab;
3625 tabpage_T *tp;
3626 char_u *varname, *tabvarname;
3627 typval_T *varp;
3628
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003629 if (check_secure())
3630 return;
3631
3632 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3633 varname = tv_get_string_chk(&argvars[1]);
3634 varp = &argvars[2];
3635
3636 if (varname != NULL && varp != NULL && tp != NULL)
3637 {
3638 save_curtab = curtab;
3639 goto_tabpage_tp(tp, FALSE, FALSE);
3640
3641 tabvarname = alloc(STRLEN(varname) + 3);
3642 if (tabvarname != NULL)
3643 {
3644 STRCPY(tabvarname, "t:");
3645 STRCPY(tabvarname + 2, varname);
3646 set_var(tabvarname, varp, TRUE);
3647 vim_free(tabvarname);
3648 }
3649
3650 // Restore current tabpage
3651 if (valid_tabpage(save_curtab))
3652 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3653 }
3654}
3655
3656/*
3657 * "settabwinvar()" function
3658 */
3659 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003660f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003661{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003662 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003663}
3664
3665/*
3666 * "setwinvar()" function
3667 */
3668 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003669f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003670{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003671 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003672}
3673
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003674/*
3675 * "setbufvar()" function
3676 */
3677 void
3678f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3679{
3680 buf_T *buf;
3681 char_u *varname, *bufvarname;
3682 typval_T *varp;
3683 char_u nbuf[NUMBUFLEN];
3684
3685 if (check_secure())
3686 return;
3687 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3688 varname = tv_get_string_chk(&argvars[1]);
3689 buf = tv_get_buf(&argvars[0], FALSE);
3690 varp = &argvars[2];
3691
3692 if (buf != NULL && varname != NULL && varp != NULL)
3693 {
3694 if (*varname == '&')
3695 {
3696 long numval;
3697 char_u *strval;
3698 int error = FALSE;
3699 aco_save_T aco;
3700
3701 // set curbuf to be our buf, temporarily
3702 aucmd_prepbuf(&aco, buf);
3703
3704 ++varname;
3705 numval = (long)tv_get_number_chk(varp, &error);
3706 strval = tv_get_string_buf_chk(varp, nbuf);
3707 if (!error && strval != NULL)
3708 set_option_value(varname, numval, strval, OPT_LOCAL);
3709
3710 // reset notion of buffer
3711 aucmd_restbuf(&aco);
3712 }
3713 else
3714 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003715 bufvarname = alloc(STRLEN(varname) + 3);
3716 if (bufvarname != NULL)
3717 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003718 buf_T *save_curbuf = curbuf;
3719
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003720 curbuf = buf;
3721 STRCPY(bufvarname, "b:");
3722 STRCPY(bufvarname + 2, varname);
3723 set_var(bufvarname, varp, TRUE);
3724 vim_free(bufvarname);
3725 curbuf = save_curbuf;
3726 }
3727 }
3728 }
3729}
3730
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003731/*
3732 * Get a callback from "arg". It can be a Funcref or a function name.
3733 * When "arg" is zero return an empty string.
3734 * "cb_name" is not allocated.
3735 * "cb_name" is set to NULL for an invalid argument.
3736 */
3737 callback_T
3738get_callback(typval_T *arg)
3739{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003740 callback_T res;
3741 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003742
3743 res.cb_free_name = FALSE;
3744 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3745 {
3746 res.cb_partial = arg->vval.v_partial;
3747 ++res.cb_partial->pt_refcount;
3748 res.cb_name = partial_name(res.cb_partial);
3749 }
3750 else
3751 {
3752 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003753 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3754 && isdigit(*arg->vval.v_string))
3755 r = FAIL;
3756 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003757 {
3758 // Note that we don't make a copy of the string.
3759 res.cb_name = arg->vval.v_string;
3760 func_ref(res.cb_name);
3761 }
3762 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003763 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003764 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003765 r = FAIL;
3766
3767 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003768 {
3769 emsg(_("E921: Invalid callback argument"));
3770 res.cb_name = NULL;
3771 }
3772 }
3773 return res;
3774}
3775
3776/*
3777 * Copy a callback into a typval_T.
3778 */
3779 void
3780put_callback(callback_T *cb, typval_T *tv)
3781{
3782 if (cb->cb_partial != NULL)
3783 {
3784 tv->v_type = VAR_PARTIAL;
3785 tv->vval.v_partial = cb->cb_partial;
3786 ++tv->vval.v_partial->pt_refcount;
3787 }
3788 else
3789 {
3790 tv->v_type = VAR_FUNC;
3791 tv->vval.v_string = vim_strsave(cb->cb_name);
3792 func_ref(cb->cb_name);
3793 }
3794}
3795
3796/*
3797 * Make a copy of "src" into "dest", allocating the function name if needed,
3798 * without incrementing the refcount.
3799 */
3800 void
3801set_callback(callback_T *dest, callback_T *src)
3802{
3803 if (src->cb_partial == NULL)
3804 {
3805 // just a function name, make a copy
3806 dest->cb_name = vim_strsave(src->cb_name);
3807 dest->cb_free_name = TRUE;
3808 }
3809 else
3810 {
3811 // cb_name is a pointer into cb_partial
3812 dest->cb_name = src->cb_name;
3813 dest->cb_free_name = FALSE;
3814 }
3815 dest->cb_partial = src->cb_partial;
3816}
3817
3818/*
3819 * Unref/free "callback" returned by get_callback() or set_callback().
3820 */
3821 void
3822free_callback(callback_T *callback)
3823{
3824 if (callback->cb_partial != NULL)
3825 {
3826 partial_unref(callback->cb_partial);
3827 callback->cb_partial = NULL;
3828 }
3829 else if (callback->cb_name != NULL)
3830 func_unref(callback->cb_name);
3831 if (callback->cb_free_name)
3832 {
3833 vim_free(callback->cb_name);
3834 callback->cb_free_name = FALSE;
3835 }
3836 callback->cb_name = NULL;
3837}
3838
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003839#endif // FEAT_EVAL