blob: f2128a6fcaef7289530494724df53ad79d25d4ed [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 Moolenaar8a7d6542020-01-26 15:56:19 +0100167static char_u *skip_var_one(char_u *arg, int include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200168static void list_glob_vars(int *first);
169static void list_buf_vars(int *first);
170static void list_win_vars(int *first);
171static void list_tab_vars(int *first);
172static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200174static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
175static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200176static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200177static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200178static void list_one_var(dictitem_T *v, char *prefix, int *first);
179static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
180
181/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200182 * Initialize global and vim special variables
183 */
184 void
185evalvars_init(void)
186{
187 int i;
188 struct vimvar *p;
189
190 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
191 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
192 vimvardict.dv_lock = VAR_FIXED;
193 hash_init(&compat_hashtab);
194
195 for (i = 0; i < VV_LEN; ++i)
196 {
197 p = &vimvars[i];
198 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
199 {
200 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
201 getout(1);
202 }
203 STRCPY(p->vv_di.di_key, p->vv_name);
204 if (p->vv_flags & VV_RO)
205 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
206 else if (p->vv_flags & VV_RO_SBX)
207 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
208 else
209 p->vv_di.di_flags = DI_FLAGS_FIX;
210
211 // add to v: scope dict, unless the value is not always available
212 if (p->vv_type != VAR_UNKNOWN)
213 hash_add(&vimvarht, p->vv_di.di_key);
214 if (p->vv_flags & VV_COMPAT)
215 // add to compat scope dict
216 hash_add(&compat_hashtab, p->vv_di.di_key);
217 }
218 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
219 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
220
221 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
222 set_vim_var_nr(VV_HLSEARCH, 1L);
223 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
224 set_vim_var_list(VV_ERRORS, list_alloc());
225 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
226
227 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
228 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
229 set_vim_var_nr(VV_NONE, VVAL_NONE);
230 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100231 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200232
233 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
234 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
235 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
236 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
237 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
238 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
239 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
240 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
241 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
242 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
243 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
244
245 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
246
Bram Moolenaar439c0362020-06-06 15:58:03 +0200247 // Default for v:register is not 0 but '"'. This is adjusted once the
248 // clipboard has been setup by calling reset_reg_var().
249 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200250}
251
252#if defined(EXITFREE) || defined(PROTO)
253/*
254 * Free all vim variables information on exit
255 */
256 void
257evalvars_clear(void)
258{
259 int i;
260 struct vimvar *p;
261
262 for (i = 0; i < VV_LEN; ++i)
263 {
264 p = &vimvars[i];
265 if (p->vv_di.di_tv.v_type == VAR_STRING)
266 VIM_CLEAR(p->vv_str);
267 else if (p->vv_di.di_tv.v_type == VAR_LIST)
268 {
269 list_unref(p->vv_list);
270 p->vv_list = NULL;
271 }
272 }
273 hash_clear(&vimvarht);
274 hash_init(&vimvarht); // garbage_collect() will access it
275 hash_clear(&compat_hashtab);
276
277 // global variables
278 vars_clear(&globvarht);
279
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100280 // Script-local variables. Clear all the variables here.
281 // The scriptvar_T is cleared later in free_scriptnames(), because a
282 // variable in one script might hold a reference to the whole scope of
283 // another script.
284 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200285 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200286}
287#endif
288
289 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200290garbage_collect_globvars(int copyID)
291{
292 return set_ref_in_ht(&globvarht, copyID, NULL);
293}
294
295 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200296garbage_collect_vimvars(int copyID)
297{
298 return set_ref_in_ht(&vimvarht, copyID, NULL);
299}
300
301 int
302garbage_collect_scriptvars(int copyID)
303{
304 int i;
305 int abort = FALSE;
306
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100307 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200308 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
309
310 return abort;
311}
312
313/*
314 * Set an internal variable to a string value. Creates the variable if it does
315 * not already exist.
316 */
317 void
318set_internal_string_var(char_u *name, char_u *value)
319{
320 char_u *val;
321 typval_T *tvp;
322
323 val = vim_strsave(value);
324 if (val != NULL)
325 {
326 tvp = alloc_string_tv(val);
327 if (tvp != NULL)
328 {
329 set_var(name, tvp, FALSE);
330 free_tv(tvp);
331 }
332 }
333}
334
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200335 int
336eval_charconvert(
337 char_u *enc_from,
338 char_u *enc_to,
339 char_u *fname_from,
340 char_u *fname_to)
341{
342 int err = FALSE;
343
344 set_vim_var_string(VV_CC_FROM, enc_from, -1);
345 set_vim_var_string(VV_CC_TO, enc_to, -1);
346 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
347 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
348 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
349 err = TRUE;
350 set_vim_var_string(VV_CC_FROM, NULL, -1);
351 set_vim_var_string(VV_CC_TO, NULL, -1);
352 set_vim_var_string(VV_FNAME_IN, NULL, -1);
353 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
354
355 if (err)
356 return FAIL;
357 return OK;
358}
359
360# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
361 int
362eval_printexpr(char_u *fname, char_u *args)
363{
364 int err = FALSE;
365
366 set_vim_var_string(VV_FNAME_IN, fname, -1);
367 set_vim_var_string(VV_CMDARG, args, -1);
368 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
369 err = TRUE;
370 set_vim_var_string(VV_FNAME_IN, NULL, -1);
371 set_vim_var_string(VV_CMDARG, NULL, -1);
372
373 if (err)
374 {
375 mch_remove(fname);
376 return FAIL;
377 }
378 return OK;
379}
380# endif
381
382# if defined(FEAT_DIFF) || defined(PROTO)
383 void
384eval_diff(
385 char_u *origfile,
386 char_u *newfile,
387 char_u *outfile)
388{
389 int err = FALSE;
390
391 set_vim_var_string(VV_FNAME_IN, origfile, -1);
392 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
393 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
394 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
395 set_vim_var_string(VV_FNAME_IN, NULL, -1);
396 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
397 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
398}
399
400 void
401eval_patch(
402 char_u *origfile,
403 char_u *difffile,
404 char_u *outfile)
405{
406 int err;
407
408 set_vim_var_string(VV_FNAME_IN, origfile, -1);
409 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
410 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
411 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
412 set_vim_var_string(VV_FNAME_IN, NULL, -1);
413 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
414 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
415}
416# endif
417
418#if defined(FEAT_SPELL) || defined(PROTO)
419/*
420 * Evaluate an expression to a list with suggestions.
421 * For the "expr:" part of 'spellsuggest'.
422 * Returns NULL when there is an error.
423 */
424 list_T *
425eval_spell_expr(char_u *badword, char_u *expr)
426{
427 typval_T save_val;
428 typval_T rettv;
429 list_T *list = NULL;
430 char_u *p = skipwhite(expr);
431
432 // Set "v:val" to the bad word.
433 prepare_vimvar(VV_VAL, &save_val);
434 set_vim_var_string(VV_VAL, badword, -1);
435 if (p_verbose == 0)
436 ++emsg_off;
437
Bram Moolenaar32e35112020-05-14 22:41:15 +0200438 if (eval1(&p, &rettv, EVAL_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200439 {
440 if (rettv.v_type != VAR_LIST)
441 clear_tv(&rettv);
442 else
443 list = rettv.vval.v_list;
444 }
445
446 if (p_verbose == 0)
447 --emsg_off;
448 clear_tv(get_vim_var_tv(VV_VAL));
449 restore_vimvar(VV_VAL, &save_val);
450
451 return list;
452}
453
454/*
455 * "list" is supposed to contain two items: a word and a number. Return the
456 * word in "pp" and the number as the return value.
457 * Return -1 if anything isn't right.
458 * Used to get the good word and score from the eval_spell_expr() result.
459 */
460 int
461get_spellword(list_T *list, char_u **pp)
462{
463 listitem_T *li;
464
465 li = list->lv_first;
466 if (li == NULL)
467 return -1;
468 *pp = tv_get_string(&li->li_tv);
469
470 li = li->li_next;
471 if (li == NULL)
472 return -1;
473 return (int)tv_get_number(&li->li_tv);
474}
475#endif
476
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200477/*
478 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200479 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200480 * When not used yet add the variable to the v: hashtable.
481 */
482 void
483prepare_vimvar(int idx, typval_T *save_tv)
484{
485 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200486 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200487 if (vimvars[idx].vv_type == VAR_UNKNOWN)
488 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
489}
490
491/*
492 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200493 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200494 * When no longer defined, remove the variable from the v: hashtable.
495 */
496 void
497restore_vimvar(int idx, typval_T *save_tv)
498{
499 hashitem_T *hi;
500
501 vimvars[idx].vv_tv = *save_tv;
502 if (vimvars[idx].vv_type == VAR_UNKNOWN)
503 {
504 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
505 if (HASHITEM_EMPTY(hi))
506 internal_error("restore_vimvar()");
507 else
508 hash_remove(&vimvarht, hi);
509 }
510}
511
512/*
513 * List Vim variables.
514 */
515 static void
516list_vim_vars(int *first)
517{
518 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
519}
520
521/*
522 * List script-local variables, if there is a script.
523 */
524 static void
525list_script_vars(int *first)
526{
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100527 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= script_items.ga_len)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200528 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
529 "s:", FALSE, first);
530}
531
532/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200533 * Get a list of lines from a HERE document. The here document is a list of
534 * lines surrounded by a marker.
535 * cmd << {marker}
536 * {line1}
537 * {line2}
538 * ....
539 * {marker}
540 *
541 * The {marker} is a string. If the optional 'trim' word is supplied before the
542 * marker, then the leading indentation before the lines (matching the
543 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200544 *
545 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
546 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
547 * missing, then '.' is accepted as a marker.
548 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200549 * Returns a List with {lines} or NULL.
550 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200552heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200553{
554 char_u *theline;
555 char_u *marker;
556 list_T *l;
557 char_u *p;
558 int marker_indent_len = 0;
559 int text_indent_len = 0;
560 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200561 char_u dot[] = ".";
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200562
563 if (eap->getline == NULL)
564 {
565 emsg(_("E991: cannot use =<< here"));
566 return NULL;
567 }
568
569 // Check for the optional 'trim' word before the marker
570 cmd = skipwhite(cmd);
571 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
572 {
573 cmd = skipwhite(cmd + 4);
574
575 // Trim the indentation from all the lines in the here document.
576 // The amount of indentation trimmed is the same as the indentation of
577 // the first line after the :let command line. To find the end marker
578 // the indent of the :let command line is trimmed.
579 p = *eap->cmdlinep;
580 while (VIM_ISWHITE(*p))
581 {
582 p++;
583 marker_indent_len++;
584 }
585 text_indent_len = -1;
586 }
587
588 // The marker is the next word.
589 if (*cmd != NUL && *cmd != '"')
590 {
591 marker = skipwhite(cmd);
592 p = skiptowhite(marker);
593 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
594 {
595 emsg(_(e_trailing));
596 return NULL;
597 }
598 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200599 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200600 {
601 emsg(_("E221: Marker cannot start with lower case letter"));
602 return NULL;
603 }
604 }
605 else
606 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200607 // When getting lines for an embedded script, if the marker is missing,
608 // accept '.' as the marker.
609 if (script_get)
610 marker = dot;
611 else
612 {
613 emsg(_("E172: Missing marker"));
614 return NULL;
615 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200616 }
617
618 l = list_alloc();
619 if (l == NULL)
620 return NULL;
621
622 for (;;)
623 {
624 int mi = 0;
625 int ti = 0;
626
627 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
628 if (theline == NULL)
629 {
630 semsg(_("E990: Missing end marker '%s'"), marker);
631 break;
632 }
633
634 // with "trim": skip the indent matching the :let line to find the
635 // marker
636 if (marker_indent_len > 0
637 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
638 mi = marker_indent_len;
639 if (STRCMP(marker, theline + mi) == 0)
640 {
641 vim_free(theline);
642 break;
643 }
644
645 if (text_indent_len == -1 && *theline != NUL)
646 {
647 // set the text indent from the first line.
648 p = theline;
649 text_indent_len = 0;
650 while (VIM_ISWHITE(*p))
651 {
652 p++;
653 text_indent_len++;
654 }
655 text_indent = vim_strnsave(theline, text_indent_len);
656 }
657 // with "trim": skip the indent matching the first line
658 if (text_indent != NULL)
659 for (ti = 0; ti < text_indent_len; ++ti)
660 if (theline[ti] != text_indent[ti])
661 break;
662
663 if (list_append_string(l, theline + ti, -1) == FAIL)
664 break;
665 vim_free(theline);
666 }
667 vim_free(text_indent);
668
669 return l;
670}
671
672/*
673 * ":let" list all variable values
674 * ":let var1 var2" list variable values
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 var ..= expr" assignment command.
683 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100684 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100685 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200686 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200687 * ":const" list all variable values
688 * ":const var1 var2" list variable values
689 * ":const var = expr" assignment command.
690 * ":const [var1, var2] = expr" unpack list.
691 */
692 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200693ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200694{
695 char_u *arg = eap->arg;
696 char_u *expr = NULL;
697 typval_T rettv;
698 int i;
699 int var_count = 0;
700 int semicolon = 0;
701 char_u op[2];
702 char_u *argend;
703 int first = TRUE;
704 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200705 int has_assign;
Bram Moolenaar09689a02020-05-09 22:50:08 +0200706 int flags = eap->cmdidx == CMD_const ? LET_IS_CONST : 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200707
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100708 // detect Vim9 assignment without ":let" or ":const"
709 if (eap->arg == eap->cmd)
710 flags |= LET_NO_COMMAND;
711
712 argend = skip_var_list(arg, TRUE, &var_count, &semicolon);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200713 if (argend == NULL)
714 return;
715 if (argend > arg && argend[-1] == '.') // for var.='str'
716 --argend;
717 expr = skipwhite(argend);
718 concat = expr[0] == '.'
719 && ((expr[1] == '=' && current_sctx.sc_version < 2)
720 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200721 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
722 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200723 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200724 {
725 // ":let" without "=": list variables
726 if (*arg == '[')
727 emsg(_(e_invarg));
728 else if (expr[0] == '.')
729 emsg(_("E985: .= is not supported with script version 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200730 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200731 // ":let var1 var2"
732 arg = list_arg_vars(eap, arg, &first);
733 else if (!eap->skip)
734 {
735 // ":let"
736 list_glob_vars(&first);
737 list_buf_vars(&first);
738 list_win_vars(&first);
739 list_tab_vars(&first);
740 list_script_vars(&first);
741 list_func_vars(&first);
742 list_vim_vars(&first);
743 }
744 eap->nextcmd = check_nextcmd(arg);
745 }
746 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
747 {
748 list_T *l;
749
750 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200751 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200752 if (l != NULL)
753 {
754 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200755 if (!eap->skip)
756 {
757 op[0] = '=';
758 op[1] = NUL;
759 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200761 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200762 clear_tv(&rettv);
763 }
764 }
765 else
766 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200767 int eval_flags;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200768
Bram Moolenaar32e35112020-05-14 22:41:15 +0200769 rettv.v_type = VAR_UNKNOWN;
770 i = FAIL;
771 if (has_assign || concat)
772 {
773 op[0] = '=';
774 op[1] = NUL;
775 if (*expr != '=')
776 {
777 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
778 {
779 op[0] = *expr; // +=, -=, *=, /=, %= or .=
780 if (expr[0] == '.' && expr[1] == '.') // ..=
781 ++expr;
782 }
783 expr = skipwhite(expr + 2);
784 }
785 else
786 expr = skipwhite(expr + 1);
787
788 if (eap->skip)
789 ++emsg_skip;
790 eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200791 i = eval0(expr, &rettv, &eap->nextcmd, eval_flags);
792 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200793 if (eap->skip)
794 {
795 if (i != FAIL)
796 clear_tv(&rettv);
797 --emsg_skip;
798 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200799 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200800 {
801 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100802 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200803 clear_tv(&rettv);
804 }
805 }
806}
807
808/*
809 * Assign the typevalue "tv" to the variable or variables at "arg_start".
810 * Handles both "var" with any type and "[var, var; var]" with a list type.
811 * When "op" is not NULL it points to a string with characters that
812 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
813 * or concatenate.
814 * Returns OK or FAIL;
815 */
816 int
817ex_let_vars(
818 char_u *arg_start,
819 typval_T *tv,
820 int copy, // copy values from "tv", don't move
821 int semicolon, // from skip_var_list()
822 int var_count, // from skip_var_list()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200824 char_u *op)
825{
826 char_u *arg = arg_start;
827 list_T *l;
828 int i;
829 listitem_T *item;
830 typval_T ltv;
831
832 if (*arg != '[')
833 {
834 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200836 return FAIL;
837 return OK;
838 }
839
840 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
841 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
842 {
843 emsg(_(e_listreq));
844 return FAIL;
845 }
846
847 i = list_len(l);
848 if (semicolon == 0 && var_count < i)
849 {
850 emsg(_("E687: Less targets than List items"));
851 return FAIL;
852 }
853 if (var_count - semicolon > i)
854 {
855 emsg(_("E688: More targets than List items"));
856 return FAIL;
857 }
858
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200859 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200860 item = l->lv_first;
861 while (*arg != ']')
862 {
863 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100864 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200865 item = item->li_next;
866 if (arg == NULL)
867 return FAIL;
868
869 arg = skipwhite(arg);
870 if (*arg == ';')
871 {
872 // Put the rest of the list (may be empty) in the var after ';'.
873 // Create a new list for this.
874 l = list_alloc();
875 if (l == NULL)
876 return FAIL;
877 while (item != NULL)
878 {
879 list_append_tv(l, &item->li_tv);
880 item = item->li_next;
881 }
882
883 ltv.v_type = VAR_LIST;
884 ltv.v_lock = 0;
885 ltv.vval.v_list = l;
886 l->lv_refcount = 1;
887
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200889 (char_u *)"]", op);
890 clear_tv(&ltv);
891 if (arg == NULL)
892 return FAIL;
893 break;
894 }
895 else if (*arg != ',' && *arg != ']')
896 {
897 internal_error("ex_let_vars()");
898 return FAIL;
899 }
900 }
901
902 return OK;
903}
904
905/*
906 * Skip over assignable variable "var" or list of variables "[var, var]".
907 * Used for ":let varvar = expr" and ":for varvar in expr".
908 * For "[var, var]" increment "*var_count" for each variable.
909 * for "[var, var; var]" set "semicolon".
910 * Return NULL for an error.
911 */
912 char_u *
913skip_var_list(
914 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100915 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200916 int *var_count,
917 int *semicolon)
918{
919 char_u *p, *s;
920
921 if (*arg == '[')
922 {
923 // "[var, var]": find the matching ']'.
924 p = arg;
925 for (;;)
926 {
927 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928 s = skip_var_one(p, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200929 if (s == p)
930 {
931 semsg(_(e_invarg2), p);
932 return NULL;
933 }
934 ++*var_count;
935
936 p = skipwhite(s);
937 if (*p == ']')
938 break;
939 else if (*p == ';')
940 {
941 if (*semicolon == 1)
942 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200943 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200944 return NULL;
945 }
946 *semicolon = 1;
947 }
948 else if (*p != ',')
949 {
950 semsg(_(e_invarg2), p);
951 return NULL;
952 }
953 }
954 return p + 1;
955 }
956 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100957 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200958}
959
960/*
961 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
962 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200964 */
965 static char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100966skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200967{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100968 char_u *end;
969
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200970 if (*arg == '@' && arg[1] != NUL)
971 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100972 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200973 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100974 if (include_type && current_sctx.sc_version == SCRIPT_VERSION_VIM9
975 && *end == ':')
976 {
977 end = skip_type(skipwhite(end + 1));
978 }
979 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200980}
981
982/*
983 * List variables for hashtab "ht" with prefix "prefix".
984 * If "empty" is TRUE also list NULL strings as empty strings.
985 */
986 void
987list_hashtable_vars(
988 hashtab_T *ht,
989 char *prefix,
990 int empty,
991 int *first)
992{
993 hashitem_T *hi;
994 dictitem_T *di;
995 int todo;
996 char_u buf[IOSIZE];
997
998 todo = (int)ht->ht_used;
999 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1000 {
1001 if (!HASHITEM_EMPTY(hi))
1002 {
1003 --todo;
1004 di = HI2DI(hi);
1005
1006 // apply :filter /pat/ to variable name
1007 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1008 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1009 if (message_filtered(buf))
1010 continue;
1011
1012 if (empty || di->di_tv.v_type != VAR_STRING
1013 || di->di_tv.vval.v_string != NULL)
1014 list_one_var(di, prefix, first);
1015 }
1016 }
1017}
1018
1019/*
1020 * List global variables.
1021 */
1022 static void
1023list_glob_vars(int *first)
1024{
1025 list_hashtable_vars(&globvarht, "", TRUE, first);
1026}
1027
1028/*
1029 * List buffer variables.
1030 */
1031 static void
1032list_buf_vars(int *first)
1033{
1034 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1035}
1036
1037/*
1038 * List window variables.
1039 */
1040 static void
1041list_win_vars(int *first)
1042{
1043 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1044}
1045
1046/*
1047 * List tab page variables.
1048 */
1049 static void
1050list_tab_vars(int *first)
1051{
1052 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1053}
1054
1055/*
1056 * List variables in "arg".
1057 */
1058 static char_u *
1059list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1060{
1061 int error = FALSE;
1062 int len;
1063 char_u *name;
1064 char_u *name_start;
1065 char_u *arg_subsc;
1066 char_u *tofree;
1067 typval_T tv;
1068
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001069 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001070 {
1071 if (error || eap->skip)
1072 {
1073 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1074 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1075 {
1076 emsg_severe = TRUE;
1077 emsg(_(e_trailing));
1078 break;
1079 }
1080 }
1081 else
1082 {
1083 // get_name_len() takes care of expanding curly braces
1084 name_start = name = arg;
1085 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1086 if (len <= 0)
1087 {
1088 // This is mainly to keep test 49 working: when expanding
1089 // curly braces fails overrule the exception error message.
1090 if (len < 0 && !aborting())
1091 {
1092 emsg_severe = TRUE;
1093 semsg(_(e_invarg2), arg);
1094 break;
1095 }
1096 error = TRUE;
1097 }
1098 else
1099 {
1100 if (tofree != NULL)
1101 name = tofree;
1102 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
1103 error = TRUE;
1104 else
1105 {
1106 // handle d.key, l[idx], f(expr)
1107 arg_subsc = arg;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001108 if (handle_subscript(&arg, &tv, EVAL_EVALUATE, TRUE,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001109 name, &name) == FAIL)
1110 error = TRUE;
1111 else
1112 {
1113 if (arg == arg_subsc && len == 2 && name[1] == ':')
1114 {
1115 switch (*name)
1116 {
1117 case 'g': list_glob_vars(first); break;
1118 case 'b': list_buf_vars(first); break;
1119 case 'w': list_win_vars(first); break;
1120 case 't': list_tab_vars(first); break;
1121 case 'v': list_vim_vars(first); break;
1122 case 's': list_script_vars(first); break;
1123 case 'l': list_func_vars(first); break;
1124 default:
1125 semsg(_("E738: Can't list variables for %s"), name);
1126 }
1127 }
1128 else
1129 {
1130 char_u numbuf[NUMBUFLEN];
1131 char_u *tf;
1132 int c;
1133 char_u *s;
1134
1135 s = echo_string(&tv, &tf, numbuf, 0);
1136 c = *arg;
1137 *arg = NUL;
1138 list_one_var_a("",
1139 arg == arg_subsc ? name : name_start,
1140 tv.v_type,
1141 s == NULL ? (char_u *)"" : s,
1142 first);
1143 *arg = c;
1144 vim_free(tf);
1145 }
1146 clear_tv(&tv);
1147 }
1148 }
1149 }
1150
1151 vim_free(tofree);
1152 }
1153
1154 arg = skipwhite(arg);
1155 }
1156
1157 return arg;
1158}
1159
1160/*
1161 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1162 * Returns a pointer to the char just after the var name.
1163 * Returns NULL if there is an error.
1164 */
1165 static char_u *
1166ex_let_one(
1167 char_u *arg, // points to variable name
1168 typval_T *tv, // value to assign to variable
1169 int copy, // copy value from "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001170 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001171 char_u *endchars, // valid chars after variable name or NULL
1172 char_u *op) // "+", "-", "." or NULL
1173{
1174 int c1;
1175 char_u *name;
1176 char_u *p;
1177 char_u *arg_end = NULL;
1178 int len;
1179 int opt_flags;
1180 char_u *tofree = NULL;
1181
1182 // ":let $VAR = expr": Set environment variable.
1183 if (*arg == '$')
1184 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001186 {
1187 emsg(_("E996: Cannot lock an environment variable"));
1188 return NULL;
1189 }
1190 // Find the end of the name.
1191 ++arg;
1192 name = arg;
1193 len = get_env_len(&arg);
1194 if (len == 0)
1195 semsg(_(e_invarg2), name - 1);
1196 else
1197 {
1198 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1199 semsg(_(e_letwrong), op);
1200 else if (endchars != NULL
1201 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1202 emsg(_(e_letunexp));
1203 else if (!check_secure())
1204 {
1205 c1 = name[len];
1206 name[len] = NUL;
1207 p = tv_get_string_chk(tv);
1208 if (p != NULL && op != NULL && *op == '.')
1209 {
1210 int mustfree = FALSE;
1211 char_u *s = vim_getenv(name, &mustfree);
1212
1213 if (s != NULL)
1214 {
1215 p = tofree = concat_str(s, p);
1216 if (mustfree)
1217 vim_free(s);
1218 }
1219 }
1220 if (p != NULL)
1221 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001222 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001223 arg_end = arg;
1224 }
1225 name[len] = c1;
1226 vim_free(tofree);
1227 }
1228 }
1229 }
1230
1231 // ":let &option = expr": Set option value.
1232 // ":let &l:option = expr": Set local option value.
1233 // ":let &g:option = expr": Set global option value.
1234 else if (*arg == '&')
1235 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001236 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001237 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001238 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001239 return NULL;
1240 }
1241 // Find the end of the name.
1242 p = find_option_end(&arg, &opt_flags);
1243 if (p == NULL || (endchars != NULL
1244 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1245 emsg(_(e_letunexp));
1246 else
1247 {
1248 long n;
1249 int opt_type;
1250 long numval;
1251 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001252 char_u *s = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001253
1254 c1 = *p;
1255 *p = NUL;
1256
1257 n = (long)tv_get_number(tv);
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001258 // avoid setting a string option to the text "v:false" or similar.
1259 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL)
1260 s = tv_get_string_chk(tv); // != NULL if number or string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001261 if (s != NULL && op != NULL && *op != '=')
1262 {
1263 opt_type = get_option_value(arg, &numval,
1264 &stringval, opt_flags);
1265 if ((opt_type == 1 && *op == '.')
1266 || (opt_type == 0 && *op != '.'))
1267 {
1268 semsg(_(e_letwrong), op);
1269 s = NULL; // don't set the value
1270 }
1271 else
1272 {
1273 if (opt_type == 1) // number
1274 {
1275 switch (*op)
1276 {
1277 case '+': n = numval + n; break;
1278 case '-': n = numval - n; break;
1279 case '*': n = numval * n; break;
1280 case '/': n = (long)num_divide(numval, n); break;
1281 case '%': n = (long)num_modulus(numval, n); break;
1282 }
1283 }
1284 else if (opt_type == 0 && stringval != NULL) // string
1285 {
1286 s = concat_str(stringval, s);
1287 vim_free(stringval);
1288 stringval = s;
1289 }
1290 }
1291 }
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001292 if (s != NULL || tv->v_type == VAR_BOOL
1293 || tv->v_type == VAR_SPECIAL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001294 {
1295 set_option_value(arg, n, s, opt_flags);
1296 arg_end = p;
1297 }
1298 *p = c1;
1299 vim_free(stringval);
1300 }
1301 }
1302
1303 // ":let @r = expr": Set register contents.
1304 else if (*arg == '@')
1305 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001307 {
1308 emsg(_("E996: Cannot lock a register"));
1309 return NULL;
1310 }
1311 ++arg;
1312 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1313 semsg(_(e_letwrong), op);
1314 else if (endchars != NULL
1315 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1316 emsg(_(e_letunexp));
1317 else
1318 {
1319 char_u *ptofree = NULL;
1320 char_u *s;
1321
1322 p = tv_get_string_chk(tv);
1323 if (p != NULL && op != NULL && *op == '.')
1324 {
1325 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1326 if (s != NULL)
1327 {
1328 p = ptofree = concat_str(s, p);
1329 vim_free(s);
1330 }
1331 }
1332 if (p != NULL)
1333 {
1334 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1335 arg_end = arg + 1;
1336 }
1337 vim_free(ptofree);
1338 }
1339 }
1340
1341 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001343 // ":let {expr} = expr": Idem, name made with curly braces
1344 else if (eval_isnamec1(*arg) || *arg == '{')
1345 {
1346 lval_T lv;
1347
1348 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001349 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001350 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001351 if (endchars != NULL && vim_strchr(endchars,
1352 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001353 emsg(_(e_letunexp));
1354 else
1355 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001357 arg_end = p;
1358 }
1359 }
1360 clear_lval(&lv);
1361 }
1362
1363 else
1364 semsg(_(e_invarg2), arg);
1365
1366 return arg_end;
1367}
1368
1369/*
1370 * ":unlet[!] var1 ... " command.
1371 */
1372 void
1373ex_unlet(exarg_T *eap)
1374{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001375 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001376}
1377
1378/*
1379 * ":lockvar" and ":unlockvar" commands
1380 */
1381 void
1382ex_lockvar(exarg_T *eap)
1383{
1384 char_u *arg = eap->arg;
1385 int deep = 2;
1386
1387 if (eap->forceit)
1388 deep = -1;
1389 else if (vim_isdigit(*arg))
1390 {
1391 deep = getdigits(&arg);
1392 arg = skipwhite(arg);
1393 }
1394
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001395 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001396}
1397
1398/*
1399 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001400 * Also used for Vim9 script. "callback" is invoked as:
1401 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001402 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001403 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001404ex_unletlock(
1405 exarg_T *eap,
1406 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001407 int deep,
1408 int glv_flags,
1409 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1410 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001411{
1412 char_u *arg = argstart;
1413 char_u *name_end;
1414 int error = FALSE;
1415 lval_T lv;
1416
1417 do
1418 {
1419 if (*arg == '$')
1420 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001421 lv.ll_name = arg;
1422 lv.ll_tv = NULL;
1423 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001424 if (get_env_len(&arg) == 0)
1425 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001426 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001427 return;
1428 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001429 if (!error && !eap->skip
1430 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1431 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001432 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001433 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001434 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001435 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001436 // Parse the name and find the end.
1437 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1438 glv_flags, FNE_CHECK_START);
1439 if (lv.ll_name == NULL)
1440 error = TRUE; // error but continue parsing
1441 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1442 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001443 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001444 if (name_end != NULL)
1445 {
1446 emsg_severe = TRUE;
1447 emsg(_(e_trailing));
1448 }
1449 if (!(eap->skip || error))
1450 clear_lval(&lv);
1451 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001452 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001453
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001454 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001455 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001456 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001457
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001458 if (!eap->skip)
1459 clear_lval(&lv);
1460 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001461
1462 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001463 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001464
1465 eap->nextcmd = check_nextcmd(arg);
1466}
1467
1468 static int
1469do_unlet_var(
1470 lval_T *lp,
1471 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001472 exarg_T *eap,
1473 int deep UNUSED,
1474 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001475{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001476 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001477 int ret = OK;
1478 int cc;
1479
1480 if (lp->ll_tv == NULL)
1481 {
1482 cc = *name_end;
1483 *name_end = NUL;
1484
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001485 // Environment variable, normal name or expanded name.
1486 if (*lp->ll_name == '$')
1487 vim_unsetenv(lp->ll_name + 1);
1488 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001489 ret = FAIL;
1490 *name_end = cc;
1491 }
1492 else if ((lp->ll_list != NULL
1493 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
1494 || (lp->ll_dict != NULL
1495 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
1496 return FAIL;
1497 else if (lp->ll_range)
1498 {
1499 listitem_T *li;
1500 listitem_T *ll_li = lp->ll_li;
1501 int ll_n1 = lp->ll_n1;
1502
1503 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1504 {
1505 li = ll_li->li_next;
1506 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
1507 return FAIL;
1508 ll_li = li;
1509 ++ll_n1;
1510 }
1511
1512 // Delete a range of List items.
1513 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1514 {
1515 li = lp->ll_li->li_next;
1516 listitem_remove(lp->ll_list, lp->ll_li);
1517 lp->ll_li = li;
1518 ++lp->ll_n1;
1519 }
1520 }
1521 else
1522 {
1523 if (lp->ll_list != NULL)
1524 // unlet a List item.
1525 listitem_remove(lp->ll_list, lp->ll_li);
1526 else
1527 // unlet a Dictionary item.
1528 dictitem_remove(lp->ll_dict, lp->ll_di);
1529 }
1530
1531 return ret;
1532}
1533
1534/*
1535 * "unlet" a variable. Return OK if it existed, FAIL if not.
1536 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1537 */
1538 int
1539do_unlet(char_u *name, int forceit)
1540{
1541 hashtab_T *ht;
1542 hashitem_T *hi;
1543 char_u *varname;
1544 dict_T *d;
1545 dictitem_T *di;
1546
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001547 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1548 && check_vim9_unlet(name) == FAIL)
1549 return FAIL;
1550
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001551 ht = find_var_ht(name, &varname);
1552 if (ht != NULL && *varname != NUL)
1553 {
1554 d = get_current_funccal_dict(ht);
1555 if (d == NULL)
1556 {
1557 if (ht == &globvarht)
1558 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001559 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001560 d = &vimvardict;
1561 else
1562 {
1563 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1564 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1565 }
1566 if (d == NULL)
1567 {
1568 internal_error("do_unlet()");
1569 return FAIL;
1570 }
1571 }
1572 hi = hash_find(ht, varname);
1573 if (HASHITEM_EMPTY(hi))
1574 hi = find_hi_in_scoped_ht(name, &ht);
1575 if (hi != NULL && !HASHITEM_EMPTY(hi))
1576 {
1577 di = HI2DI(hi);
1578 if (var_check_fixed(di->di_flags, name, FALSE)
1579 || var_check_ro(di->di_flags, name, FALSE)
1580 || var_check_lock(d->dv_lock, name, FALSE))
1581 return FAIL;
1582
1583 delete_var(ht, hi);
1584 return OK;
1585 }
1586 }
1587 if (forceit)
1588 return OK;
1589 semsg(_("E108: No such variable: \"%s\""), name);
1590 return FAIL;
1591}
1592
1593/*
1594 * Lock or unlock variable indicated by "lp".
1595 * "deep" is the levels to go (-1 for unlimited);
1596 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1597 */
1598 static int
1599do_lock_var(
1600 lval_T *lp,
1601 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001602 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001603 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001604 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001605{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001606 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001607 int ret = OK;
1608 int cc;
1609 dictitem_T *di;
1610
1611 if (deep == 0) // nothing to do
1612 return OK;
1613
1614 if (lp->ll_tv == NULL)
1615 {
1616 cc = *name_end;
1617 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001618 if (*lp->ll_name == '$')
1619 {
1620 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001621 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001622 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001623 else
1624 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001625 // Normal name or expanded name.
1626 di = find_var(lp->ll_name, NULL, TRUE);
1627 if (di == NULL)
1628 ret = FAIL;
1629 else if ((di->di_flags & DI_FLAGS_FIX)
1630 && di->di_tv.v_type != VAR_DICT
1631 && di->di_tv.v_type != VAR_LIST)
1632 {
1633 // For historic reasons this error is not given for a list or
1634 // dict. E.g., the b: dict could be locked/unlocked.
1635 semsg(_(e_lock_unlock), lp->ll_name);
1636 ret = FAIL;
1637 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001638 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001639 {
1640 if (lock)
1641 di->di_flags |= DI_FLAGS_LOCK;
1642 else
1643 di->di_flags &= ~DI_FLAGS_LOCK;
1644 item_lock(&di->di_tv, deep, lock);
1645 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001646 }
1647 *name_end = cc;
1648 }
1649 else if (lp->ll_range)
1650 {
1651 listitem_T *li = lp->ll_li;
1652
1653 // (un)lock a range of List items.
1654 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1655 {
1656 item_lock(&li->li_tv, deep, lock);
1657 li = li->li_next;
1658 ++lp->ll_n1;
1659 }
1660 }
1661 else if (lp->ll_list != NULL)
1662 // (un)lock a List item.
1663 item_lock(&lp->ll_li->li_tv, deep, lock);
1664 else
1665 // (un)lock a Dictionary item.
1666 item_lock(&lp->ll_di->di_tv, deep, lock);
1667
1668 return ret;
1669}
1670
1671/*
1672 * Lock or unlock an item. "deep" is nr of levels to go.
1673 */
1674 static void
1675item_lock(typval_T *tv, int deep, int lock)
1676{
1677 static int recurse = 0;
1678 list_T *l;
1679 listitem_T *li;
1680 dict_T *d;
1681 blob_T *b;
1682 hashitem_T *hi;
1683 int todo;
1684
1685 if (recurse >= DICT_MAXNEST)
1686 {
1687 emsg(_("E743: variable nested too deep for (un)lock"));
1688 return;
1689 }
1690 if (deep == 0)
1691 return;
1692 ++recurse;
1693
1694 // lock/unlock the item itself
1695 if (lock)
1696 tv->v_lock |= VAR_LOCKED;
1697 else
1698 tv->v_lock &= ~VAR_LOCKED;
1699
1700 switch (tv->v_type)
1701 {
1702 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001703 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001705 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001707 case VAR_STRING:
1708 case VAR_FUNC:
1709 case VAR_PARTIAL:
1710 case VAR_FLOAT:
1711 case VAR_SPECIAL:
1712 case VAR_JOB:
1713 case VAR_CHANNEL:
1714 break;
1715
1716 case VAR_BLOB:
1717 if ((b = tv->vval.v_blob) != NULL)
1718 {
1719 if (lock)
1720 b->bv_lock |= VAR_LOCKED;
1721 else
1722 b->bv_lock &= ~VAR_LOCKED;
1723 }
1724 break;
1725 case VAR_LIST:
1726 if ((l = tv->vval.v_list) != NULL)
1727 {
1728 if (lock)
1729 l->lv_lock |= VAR_LOCKED;
1730 else
1731 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001732 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001733 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001734 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001735 item_lock(&li->li_tv, deep - 1, lock);
1736 }
1737 break;
1738 case VAR_DICT:
1739 if ((d = tv->vval.v_dict) != NULL)
1740 {
1741 if (lock)
1742 d->dv_lock |= VAR_LOCKED;
1743 else
1744 d->dv_lock &= ~VAR_LOCKED;
1745 if (deep < 0 || deep > 1)
1746 {
1747 // recursive: lock/unlock the items the List contains
1748 todo = (int)d->dv_hashtab.ht_used;
1749 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1750 {
1751 if (!HASHITEM_EMPTY(hi))
1752 {
1753 --todo;
1754 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
1755 }
1756 }
1757 }
1758 }
1759 }
1760 --recurse;
1761}
1762
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001763#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1764/*
1765 * Delete all "menutrans_" variables.
1766 */
1767 void
1768del_menutrans_vars(void)
1769{
1770 hashitem_T *hi;
1771 int todo;
1772
1773 hash_lock(&globvarht);
1774 todo = (int)globvarht.ht_used;
1775 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1776 {
1777 if (!HASHITEM_EMPTY(hi))
1778 {
1779 --todo;
1780 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1781 delete_var(&globvarht, hi);
1782 }
1783 }
1784 hash_unlock(&globvarht);
1785}
1786#endif
1787
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001788/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001789 * Local string buffer for the next two functions to store a variable name
1790 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1791 * get_user_var_name().
1792 */
1793
1794static char_u *varnamebuf = NULL;
1795static int varnamebuflen = 0;
1796
1797/*
1798 * Function to concatenate a prefix and a variable name.
1799 */
1800 static char_u *
1801cat_prefix_varname(int prefix, char_u *name)
1802{
1803 int len;
1804
1805 len = (int)STRLEN(name) + 3;
1806 if (len > varnamebuflen)
1807 {
1808 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001809 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001810 varnamebuf = alloc(len);
1811 if (varnamebuf == NULL)
1812 {
1813 varnamebuflen = 0;
1814 return NULL;
1815 }
1816 varnamebuflen = len;
1817 }
1818 *varnamebuf = prefix;
1819 varnamebuf[1] = ':';
1820 STRCPY(varnamebuf + 2, name);
1821 return varnamebuf;
1822}
1823
1824/*
1825 * Function given to ExpandGeneric() to obtain the list of user defined
1826 * (global/buffer/window/built-in) variable names.
1827 */
1828 char_u *
1829get_user_var_name(expand_T *xp, int idx)
1830{
1831 static long_u gdone;
1832 static long_u bdone;
1833 static long_u wdone;
1834 static long_u tdone;
1835 static int vidx;
1836 static hashitem_T *hi;
1837 hashtab_T *ht;
1838
1839 if (idx == 0)
1840 {
1841 gdone = bdone = wdone = vidx = 0;
1842 tdone = 0;
1843 }
1844
1845 // Global variables
1846 if (gdone < globvarht.ht_used)
1847 {
1848 if (gdone++ == 0)
1849 hi = globvarht.ht_array;
1850 else
1851 ++hi;
1852 while (HASHITEM_EMPTY(hi))
1853 ++hi;
1854 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1855 return cat_prefix_varname('g', hi->hi_key);
1856 return hi->hi_key;
1857 }
1858
1859 // b: variables
1860 ht = &curbuf->b_vars->dv_hashtab;
1861 if (bdone < ht->ht_used)
1862 {
1863 if (bdone++ == 0)
1864 hi = ht->ht_array;
1865 else
1866 ++hi;
1867 while (HASHITEM_EMPTY(hi))
1868 ++hi;
1869 return cat_prefix_varname('b', hi->hi_key);
1870 }
1871
1872 // w: variables
1873 ht = &curwin->w_vars->dv_hashtab;
1874 if (wdone < ht->ht_used)
1875 {
1876 if (wdone++ == 0)
1877 hi = ht->ht_array;
1878 else
1879 ++hi;
1880 while (HASHITEM_EMPTY(hi))
1881 ++hi;
1882 return cat_prefix_varname('w', hi->hi_key);
1883 }
1884
1885 // t: variables
1886 ht = &curtab->tp_vars->dv_hashtab;
1887 if (tdone < ht->ht_used)
1888 {
1889 if (tdone++ == 0)
1890 hi = ht->ht_array;
1891 else
1892 ++hi;
1893 while (HASHITEM_EMPTY(hi))
1894 ++hi;
1895 return cat_prefix_varname('t', hi->hi_key);
1896 }
1897
1898 // v: variables
1899 if (vidx < VV_LEN)
1900 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
1901
1902 VIM_CLEAR(varnamebuf);
1903 varnamebuflen = 0;
1904 return NULL;
1905}
1906
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001907 char *
1908get_var_special_name(int nr)
1909{
1910 switch (nr)
1911 {
1912 case VVAL_FALSE: return "v:false";
1913 case VVAL_TRUE: return "v:true";
1914 case VVAL_NONE: return "v:none";
1915 case VVAL_NULL: return "v:null";
1916 }
1917 internal_error("get_var_special_name()");
1918 return "42";
1919}
1920
1921/*
1922 * Returns the global variable dictionary
1923 */
1924 dict_T *
1925get_globvar_dict(void)
1926{
1927 return &globvardict;
1928}
1929
1930/*
1931 * Returns the global variable hash table
1932 */
1933 hashtab_T *
1934get_globvar_ht(void)
1935{
1936 return &globvarht;
1937}
1938
1939/*
1940 * Returns the v: variable dictionary
1941 */
1942 dict_T *
1943get_vimvar_dict(void)
1944{
1945 return &vimvardict;
1946}
1947
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001948/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001950 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001951 */
1952 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001953find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001954{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001955 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
1956 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001957
1958 if (di == NULL)
1959 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001960 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
1962 return (int)(vv - vimvars);
1963}
1964
1965
1966/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02001967 * Set type of v: variable to "type".
1968 */
1969 void
1970set_vim_var_type(int idx, vartype_T type)
1971{
1972 vimvars[idx].vv_type = type;
1973}
1974
1975/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001976 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001977 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001978 */
1979 void
1980set_vim_var_nr(int idx, varnumber_T val)
1981{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001982 vimvars[idx].vv_nr = val;
1983}
1984
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 char *
1986get_vim_var_name(int idx)
1987{
1988 return vimvars[idx].vv_name;
1989}
1990
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001991/*
1992 * Get typval_T v: variable value.
1993 */
1994 typval_T *
1995get_vim_var_tv(int idx)
1996{
1997 return &vimvars[idx].vv_tv;
1998}
1999
2000/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002001 * Set v: variable to "tv". Only accepts the same type.
2002 * Takes over the value of "tv".
2003 */
2004 int
2005set_vim_var_tv(int idx, typval_T *tv)
2006{
2007 if (vimvars[idx].vv_type != tv->v_type)
2008 {
2009 emsg(_("E1063: type mismatch for v: variable"));
2010 clear_tv(tv);
2011 return FAIL;
2012 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002013 // VV_RO is also checked when compiling, but let's check here as well.
2014 if (vimvars[idx].vv_flags & VV_RO)
2015 {
2016 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2017 return FAIL;
2018 }
2019 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2020 {
2021 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2022 return FAIL;
2023 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002024 clear_tv(&vimvars[idx].vv_di.di_tv);
2025 vimvars[idx].vv_di.di_tv = *tv;
2026 return OK;
2027}
2028
2029/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002030 * Get number v: variable value.
2031 */
2032 varnumber_T
2033get_vim_var_nr(int idx)
2034{
2035 return vimvars[idx].vv_nr;
2036}
2037
2038/*
2039 * Get string v: variable value. Uses a static buffer, can only be used once.
2040 * If the String variable has never been set, return an empty string.
2041 * Never returns NULL;
2042 */
2043 char_u *
2044get_vim_var_str(int idx)
2045{
2046 return tv_get_string(&vimvars[idx].vv_tv);
2047}
2048
2049/*
2050 * Get List v: variable value. Caller must take care of reference count when
2051 * needed.
2052 */
2053 list_T *
2054get_vim_var_list(int idx)
2055{
2056 return vimvars[idx].vv_list;
2057}
2058
2059/*
2060 * Get Dict v: variable value. Caller must take care of reference count when
2061 * needed.
2062 */
2063 dict_T *
2064get_vim_var_dict(int idx)
2065{
2066 return vimvars[idx].vv_dict;
2067}
2068
2069/*
2070 * Set v:char to character "c".
2071 */
2072 void
2073set_vim_var_char(int c)
2074{
2075 char_u buf[MB_MAXBYTES + 1];
2076
2077 if (has_mbyte)
2078 buf[(*mb_char2bytes)(c, buf)] = NUL;
2079 else
2080 {
2081 buf[0] = c;
2082 buf[1] = NUL;
2083 }
2084 set_vim_var_string(VV_CHAR, buf, -1);
2085}
2086
2087/*
2088 * Set v:count to "count" and v:count1 to "count1".
2089 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2090 */
2091 void
2092set_vcount(
2093 long count,
2094 long count1,
2095 int set_prevcount)
2096{
2097 if (set_prevcount)
2098 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2099 vimvars[VV_COUNT].vv_nr = count;
2100 vimvars[VV_COUNT1].vv_nr = count1;
2101}
2102
2103/*
2104 * Save variables that might be changed as a side effect. Used when executing
2105 * a timer callback.
2106 */
2107 void
2108save_vimvars(vimvars_save_T *vvsave)
2109{
2110 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2111 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2112 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2113}
2114
2115/*
2116 * Restore variables saved by save_vimvars().
2117 */
2118 void
2119restore_vimvars(vimvars_save_T *vvsave)
2120{
2121 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2122 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2123 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2124}
2125
2126/*
2127 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2128 * value.
2129 */
2130 void
2131set_vim_var_string(
2132 int idx,
2133 char_u *val,
2134 int len) // length of "val" to use or -1 (whole string)
2135{
2136 clear_tv(&vimvars[idx].vv_di.di_tv);
2137 vimvars[idx].vv_type = VAR_STRING;
2138 if (val == NULL)
2139 vimvars[idx].vv_str = NULL;
2140 else if (len == -1)
2141 vimvars[idx].vv_str = vim_strsave(val);
2142 else
2143 vimvars[idx].vv_str = vim_strnsave(val, len);
2144}
2145
2146/*
2147 * Set List v: variable to "val".
2148 */
2149 void
2150set_vim_var_list(int idx, list_T *val)
2151{
2152 clear_tv(&vimvars[idx].vv_di.di_tv);
2153 vimvars[idx].vv_type = VAR_LIST;
2154 vimvars[idx].vv_list = val;
2155 if (val != NULL)
2156 ++val->lv_refcount;
2157}
2158
2159/*
2160 * Set Dictionary v: variable to "val".
2161 */
2162 void
2163set_vim_var_dict(int idx, dict_T *val)
2164{
2165 clear_tv(&vimvars[idx].vv_di.di_tv);
2166 vimvars[idx].vv_type = VAR_DICT;
2167 vimvars[idx].vv_dict = val;
2168 if (val != NULL)
2169 {
2170 ++val->dv_refcount;
2171 dict_set_items_ro(val);
2172 }
2173}
2174
2175/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002176 * Set the v:argv list.
2177 */
2178 void
2179set_argv_var(char **argv, int argc)
2180{
2181 list_T *l = list_alloc();
2182 int i;
2183
2184 if (l == NULL)
2185 getout(1);
2186 l->lv_lock = VAR_FIXED;
2187 for (i = 0; i < argc; ++i)
2188 {
2189 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2190 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002191 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002192 }
2193 set_vim_var_list(VV_ARGV, l);
2194}
2195
2196/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002197 * Reset v:register, taking the 'clipboard' setting into account.
2198 */
2199 void
2200reset_reg_var(void)
2201{
2202 int regname = 0;
2203
2204 // Adjust the register according to 'clipboard', so that when
2205 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2206#ifdef FEAT_CLIPBOARD
2207 adjust_clip_reg(&regname);
2208#endif
2209 set_reg_var(regname);
2210}
2211
2212/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002213 * Set v:register if needed.
2214 */
2215 void
2216set_reg_var(int c)
2217{
2218 char_u regname;
2219
2220 if (c == 0 || c == ' ')
2221 regname = '"';
2222 else
2223 regname = c;
2224 // Avoid free/alloc when the value is already right.
2225 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2226 set_vim_var_string(VV_REG, &regname, 1);
2227}
2228
2229/*
2230 * Get or set v:exception. If "oldval" == NULL, return the current value.
2231 * Otherwise, restore the value to "oldval" and return NULL.
2232 * Must always be called in pairs to save and restore v:exception! Does not
2233 * take care of memory allocations.
2234 */
2235 char_u *
2236v_exception(char_u *oldval)
2237{
2238 if (oldval == NULL)
2239 return vimvars[VV_EXCEPTION].vv_str;
2240
2241 vimvars[VV_EXCEPTION].vv_str = oldval;
2242 return NULL;
2243}
2244
2245/*
2246 * Get or set v:throwpoint. 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:throwpoint! Does not
2249 * take care of memory allocations.
2250 */
2251 char_u *
2252v_throwpoint(char_u *oldval)
2253{
2254 if (oldval == NULL)
2255 return vimvars[VV_THROWPOINT].vv_str;
2256
2257 vimvars[VV_THROWPOINT].vv_str = oldval;
2258 return NULL;
2259}
2260
2261/*
2262 * Set v:cmdarg.
2263 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2264 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2265 * Must always be called in pairs!
2266 */
2267 char_u *
2268set_cmdarg(exarg_T *eap, char_u *oldarg)
2269{
2270 char_u *oldval;
2271 char_u *newval;
2272 unsigned len;
2273
2274 oldval = vimvars[VV_CMDARG].vv_str;
2275 if (eap == NULL)
2276 {
2277 vim_free(oldval);
2278 vimvars[VV_CMDARG].vv_str = oldarg;
2279 return NULL;
2280 }
2281
2282 if (eap->force_bin == FORCE_BIN)
2283 len = 6;
2284 else if (eap->force_bin == FORCE_NOBIN)
2285 len = 8;
2286 else
2287 len = 0;
2288
2289 if (eap->read_edit)
2290 len += 7;
2291
2292 if (eap->force_ff != 0)
2293 len += 10; // " ++ff=unix"
2294 if (eap->force_enc != 0)
2295 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2296 if (eap->bad_char != 0)
2297 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2298
2299 newval = alloc(len + 1);
2300 if (newval == NULL)
2301 return NULL;
2302
2303 if (eap->force_bin == FORCE_BIN)
2304 sprintf((char *)newval, " ++bin");
2305 else if (eap->force_bin == FORCE_NOBIN)
2306 sprintf((char *)newval, " ++nobin");
2307 else
2308 *newval = NUL;
2309
2310 if (eap->read_edit)
2311 STRCAT(newval, " ++edit");
2312
2313 if (eap->force_ff != 0)
2314 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2315 eap->force_ff == 'u' ? "unix"
2316 : eap->force_ff == 'd' ? "dos"
2317 : "mac");
2318 if (eap->force_enc != 0)
2319 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2320 eap->cmd + eap->force_enc);
2321 if (eap->bad_char == BAD_KEEP)
2322 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2323 else if (eap->bad_char == BAD_DROP)
2324 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2325 else if (eap->bad_char != 0)
2326 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2327 vimvars[VV_CMDARG].vv_str = newval;
2328 return oldval;
2329}
2330
2331/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002332 * Get the value of internal variable "name".
2333 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2334 */
2335 int
2336get_var_tv(
2337 char_u *name,
2338 int len, // length of "name"
2339 typval_T *rettv, // NULL when only checking existence
2340 dictitem_T **dip, // non-NULL when typval's dict item is needed
2341 int verbose, // may give error message
2342 int no_autoload) // do not use script autoloading
2343{
2344 int ret = OK;
2345 typval_T *tv = NULL;
2346 dictitem_T *v;
2347 int cc;
2348
2349 // truncate the name, so that we can use strcmp()
2350 cc = name[len];
2351 name[len] = NUL;
2352
2353 // Check for user-defined variables.
2354 v = find_var(name, NULL, no_autoload);
2355 if (v != NULL)
2356 {
2357 tv = &v->di_tv;
2358 if (dip != NULL)
2359 *dip = v;
2360 }
2361
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002362 if (tv == NULL && (current_sctx.sc_version == SCRIPT_VERSION_VIM9
2363 || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002365 imported_T *import;
2366 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2367
2368 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002369
2370 // imported variable from another script
2371 if (import != NULL)
2372 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002373 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2375 + import->imp_var_vals_idx;
2376 tv = sv->sv_tv;
2377 }
2378 }
2379
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002380 if (tv == NULL)
2381 {
2382 if (rettv != NULL && verbose)
2383 semsg(_(e_undefvar), name);
2384 ret = FAIL;
2385 }
2386 else if (rettv != NULL)
2387 copy_tv(tv, rettv);
2388
2389 name[len] = cc;
2390
2391 return ret;
2392}
2393
2394/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002395 * Check if variable "name[len]" is a local variable or an argument.
2396 * If so, "*eval_lavars_used" is set to TRUE.
2397 */
2398 void
2399check_vars(char_u *name, int len)
2400{
2401 int cc;
2402 char_u *varname;
2403 hashtab_T *ht;
2404
2405 if (eval_lavars_used == NULL)
2406 return;
2407
2408 // truncate the name, so that we can use strcmp()
2409 cc = name[len];
2410 name[len] = NUL;
2411
2412 ht = find_var_ht(name, &varname);
2413 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2414 {
2415 if (find_var(name, NULL, TRUE) != NULL)
2416 *eval_lavars_used = TRUE;
2417 }
2418
2419 name[len] = cc;
2420}
2421
2422/*
2423 * Find variable "name" in the list of variables.
2424 * Return a pointer to it if found, NULL if not found.
2425 * Careful: "a:0" variables don't have a name.
2426 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2427 * hashtab_T used.
2428 */
2429 dictitem_T *
2430find_var(char_u *name, hashtab_T **htp, int no_autoload)
2431{
2432 char_u *varname;
2433 hashtab_T *ht;
2434 dictitem_T *ret = NULL;
2435
2436 ht = find_var_ht(name, &varname);
2437 if (htp != NULL)
2438 *htp = ht;
2439 if (ht == NULL)
2440 return NULL;
2441 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2442 if (ret != NULL)
2443 return ret;
2444
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002445 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002446 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2447}
2448
2449/*
2450 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002451 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002452 * Returns NULL if not found.
2453 */
2454 dictitem_T *
2455find_var_in_ht(
2456 hashtab_T *ht,
2457 int htname,
2458 char_u *varname,
2459 int no_autoload)
2460{
2461 hashitem_T *hi;
2462
2463 if (*varname == NUL)
2464 {
2465 // Must be something like "s:", otherwise "ht" would be NULL.
2466 switch (htname)
2467 {
2468 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2469 case 'g': return &globvars_var;
2470 case 'v': return &vimvars_var;
2471 case 'b': return &curbuf->b_bufvar;
2472 case 'w': return &curwin->w_winvar;
2473 case 't': return &curtab->tp_winvar;
2474 case 'l': return get_funccal_local_var();
2475 case 'a': return get_funccal_args_var();
2476 }
2477 return NULL;
2478 }
2479
2480 hi = hash_find(ht, varname);
2481 if (HASHITEM_EMPTY(hi))
2482 {
2483 // For global variables we may try auto-loading the script. If it
2484 // worked find the variable again. Don't auto-load a script if it was
2485 // loaded already, otherwise it would be loaded every time when
2486 // checking if a function name is a Funcref variable.
2487 if (ht == &globvarht && !no_autoload)
2488 {
2489 // Note: script_autoload() may make "hi" invalid. It must either
2490 // be obtained again or not used.
2491 if (!script_autoload(varname, FALSE) || aborting())
2492 return NULL;
2493 hi = hash_find(ht, varname);
2494 }
2495 if (HASHITEM_EMPTY(hi))
2496 return NULL;
2497 }
2498 return HI2DI(hi);
2499}
2500
2501/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502 * Get the script-local hashtab. NULL if not in a script context.
2503 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02002504 static hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505get_script_local_ht(void)
2506{
2507 scid_T sid = current_sctx.sc_sid;
2508
2509 if (sid > 0 && sid <= script_items.ga_len)
2510 return &SCRIPT_VARS(sid);
2511 return NULL;
2512}
2513
2514/*
2515 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002516 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002518 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2520{
2521 hashtab_T *ht = get_script_local_ht();
2522 char_u buffer[30];
2523 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002524 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 hashitem_T *hi;
2526
2527 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002528 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 if (len < sizeof(buffer) - 1)
2530 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002531 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 vim_strncpy(buffer, name, len);
2533 p = buffer;
2534 }
2535 else
2536 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002537 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002539 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 }
2541
2542 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002543 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544
2545 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002546 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2547 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002548
2549 if (p != buffer)
2550 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002551 // Don't return "buffer", gcc complains.
2552 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553}
2554
2555/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002556 * Find the hashtab used for a variable name.
2557 * Return NULL if the name is not valid.
2558 * Set "varname" to the start of name without ':'.
2559 */
2560 hashtab_T *
2561find_var_ht(char_u *name, char_u **varname)
2562{
2563 hashitem_T *hi;
2564 hashtab_T *ht;
2565
2566 if (name[0] == NUL)
2567 return NULL;
2568 if (name[1] != ':')
2569 {
2570 // The name must not start with a colon or #.
2571 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2572 return NULL;
2573 *varname = name;
2574
2575 // "version" is "v:version" in all scopes if scriptversion < 3.
2576 // Same for a few other variables marked with VV_COMPAT.
2577 if (current_sctx.sc_version < 3)
2578 {
2579 hi = hash_find(&compat_hashtab, name);
2580 if (!HASHITEM_EMPTY(hi))
2581 return &compat_hashtab;
2582 }
2583
2584 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 if (ht != NULL)
2586 return ht; // local variable
2587
2588 // in Vim9 script items at the script level are script-local
2589 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2590 {
2591 ht = get_script_local_ht();
2592 if (ht != NULL)
2593 return ht;
2594 }
2595
2596 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002597 }
2598 *varname = name + 2;
2599 if (*name == 'g') // global variable
2600 return &globvarht;
2601 // There must be no ':' or '#' in the rest of the name, unless g: is used
2602 if (vim_strchr(name + 2, ':') != NULL
2603 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2604 return NULL;
2605 if (*name == 'b') // buffer variable
2606 return &curbuf->b_vars->dv_hashtab;
2607 if (*name == 'w') // window variable
2608 return &curwin->w_vars->dv_hashtab;
2609 if (*name == 't') // tab page variable
2610 return &curtab->tp_vars->dv_hashtab;
2611 if (*name == 'v') // v: variable
2612 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002613 if (get_current_funccal() != NULL
Bram Moolenaar822ba242020-05-24 23:00:18 +02002614 && get_current_funccal()->func->uf_dfunc_idx == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002616 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002617 if (*name == 'a') // a: function argument
2618 return get_funccal_args_ht();
2619 if (*name == 'l') // l: local function variable
2620 return get_funccal_local_ht();
2621 }
2622 if (*name == 's') // script variable
2623 {
2624 ht = get_script_local_ht();
2625 if (ht != NULL)
2626 return ht;
2627 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002628 return NULL;
2629}
2630
2631/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002632 * Get the string value of a (global/local) variable.
2633 * Note: see tv_get_string() for how long the pointer remains valid.
2634 * Returns NULL when it doesn't exist.
2635 */
2636 char_u *
2637get_var_value(char_u *name)
2638{
2639 dictitem_T *v;
2640
2641 v = find_var(name, NULL, FALSE);
2642 if (v == NULL)
2643 return NULL;
2644 return tv_get_string(&v->di_tv);
2645}
2646
2647/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002648 * Allocate a new hashtab for a sourced script. It will be used while
2649 * sourcing this script and when executing functions defined in the script.
2650 */
2651 void
2652new_script_vars(scid_T id)
2653{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002654 scriptvar_T *sv;
2655
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002656 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2657 if (sv == NULL)
2658 return;
2659 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002660 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002661}
2662
2663/*
2664 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2665 * point to it.
2666 */
2667 void
2668init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2669{
2670 hash_init(&dict->dv_hashtab);
2671 dict->dv_lock = 0;
2672 dict->dv_scope = scope;
2673 dict->dv_refcount = DO_NOT_FREE_CNT;
2674 dict->dv_copyID = 0;
2675 dict_var->di_tv.vval.v_dict = dict;
2676 dict_var->di_tv.v_type = VAR_DICT;
2677 dict_var->di_tv.v_lock = VAR_FIXED;
2678 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2679 dict_var->di_key[0] = NUL;
2680}
2681
2682/*
2683 * Unreference a dictionary initialized by init_var_dict().
2684 */
2685 void
2686unref_var_dict(dict_T *dict)
2687{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002688 // Now the dict needs to be freed if no one else is using it, go back to
2689 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002690 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2691 dict_unref(dict);
2692}
2693
2694/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002695 * Clean up a list of internal variables.
2696 * Frees all allocated variables and the value they contain.
2697 * Clears hashtab "ht", does not free it.
2698 */
2699 void
2700vars_clear(hashtab_T *ht)
2701{
2702 vars_clear_ext(ht, TRUE);
2703}
2704
2705/*
2706 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2707 */
2708 void
2709vars_clear_ext(hashtab_T *ht, int free_val)
2710{
2711 int todo;
2712 hashitem_T *hi;
2713 dictitem_T *v;
2714
2715 hash_lock(ht);
2716 todo = (int)ht->ht_used;
2717 for (hi = ht->ht_array; todo > 0; ++hi)
2718 {
2719 if (!HASHITEM_EMPTY(hi))
2720 {
2721 --todo;
2722
2723 // Free the variable. Don't remove it from the hashtab,
2724 // ht_array might change then. hash_clear() takes care of it
2725 // later.
2726 v = HI2DI(hi);
2727 if (free_val)
2728 clear_tv(&v->di_tv);
2729 if (v->di_flags & DI_FLAGS_ALLOC)
2730 vim_free(v);
2731 }
2732 }
2733 hash_clear(ht);
2734 ht->ht_used = 0;
2735}
2736
2737/*
2738 * Delete a variable from hashtab "ht" at item "hi".
2739 * Clear the variable value and free the dictitem.
2740 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002741 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002742delete_var(hashtab_T *ht, hashitem_T *hi)
2743{
2744 dictitem_T *di = HI2DI(hi);
2745
2746 hash_remove(ht, hi);
2747 clear_tv(&di->di_tv);
2748 vim_free(di);
2749}
2750
2751/*
2752 * List the value of one internal variable.
2753 */
2754 static void
2755list_one_var(dictitem_T *v, char *prefix, int *first)
2756{
2757 char_u *tofree;
2758 char_u *s;
2759 char_u numbuf[NUMBUFLEN];
2760
2761 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2762 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2763 s == NULL ? (char_u *)"" : s, first);
2764 vim_free(tofree);
2765}
2766
2767 static void
2768list_one_var_a(
2769 char *prefix,
2770 char_u *name,
2771 int type,
2772 char_u *string,
2773 int *first) // when TRUE clear rest of screen and set to FALSE
2774{
2775 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2776 msg_start();
2777 msg_puts(prefix);
2778 if (name != NULL) // "a:" vars don't have a name stored
2779 msg_puts((char *)name);
2780 msg_putchar(' ');
2781 msg_advance(22);
2782 if (type == VAR_NUMBER)
2783 msg_putchar('#');
2784 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2785 msg_putchar('*');
2786 else if (type == VAR_LIST)
2787 {
2788 msg_putchar('[');
2789 if (*string == '[')
2790 ++string;
2791 }
2792 else if (type == VAR_DICT)
2793 {
2794 msg_putchar('{');
2795 if (*string == '{')
2796 ++string;
2797 }
2798 else
2799 msg_putchar(' ');
2800
2801 msg_outtrans(string);
2802
2803 if (type == VAR_FUNC || type == VAR_PARTIAL)
2804 msg_puts("()");
2805 if (*first)
2806 {
2807 msg_clr_eos();
2808 *first = FALSE;
2809 }
2810}
2811
2812/*
2813 * Set variable "name" to value in "tv".
2814 * If the variable already exists, the value is updated.
2815 * Otherwise the variable is created.
2816 */
2817 void
2818set_var(
2819 char_u *name,
2820 typval_T *tv,
2821 int copy) // make copy of value in "tv"
2822{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002823 set_var_const(name, NULL, tv, copy, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002824}
2825
2826/*
2827 * Set variable "name" to value in "tv".
2828 * If the variable already exists and "is_const" is FALSE the value is updated.
2829 * Otherwise the variable is created.
2830 */
2831 void
2832set_var_const(
2833 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834 type_T *type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002835 typval_T *tv,
2836 int copy, // make copy of value in "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837 int flags) // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002838{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002840 char_u *varname;
2841 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 int is_script_local;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002843
2844 ht = find_var_ht(name, &varname);
2845 if (ht == NULL || *varname == NUL)
2846 {
2847 semsg(_(e_illvar), name);
2848 return;
2849 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 is_script_local = ht == get_script_local_ht();
2851
2852 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002853
2854 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 if (di == NULL)
2856 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002857
2858 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859 && var_check_func_name(name, di == NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002860 return;
2861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002863 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002865 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 if (flags & LET_IS_CONST)
2867 {
2868 emsg(_(e_cannot_mod));
2869 return;
2870 }
2871
2872 if (var_check_ro(di->di_flags, name, FALSE)
2873 || var_check_lock(di->di_tv.v_lock, name, FALSE))
2874 return;
2875
2876 if ((flags & LET_NO_COMMAND) == 0
2877 && is_script_local
2878 && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2879 {
2880 semsg(_("E1041: Redefining script item %s"), name);
2881 return;
2882 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002883 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884 else
2885 // can only redefine once
2886 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002887
2888 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002889
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002891 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002892 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002893 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002895 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002897 if (copy || tv->v_type != VAR_STRING)
2898 {
2899 char_u *val = tv_get_string(tv);
2900
2901 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002902 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903 if (di->di_tv.vval.v_string == NULL)
2904 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002905 }
2906 else
2907 {
2908 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002910 tv->vval.v_string = NULL;
2911 }
2912 return;
2913 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002915 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002917 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002919#ifdef FEAT_SEARCH_EXTRA
2920 else if (STRCMP(varname, "hlsearch") == 0)
2921 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002923 redraw_all_later(SOME_VALID);
2924 }
2925#endif
2926 return;
2927 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002929 {
2930 semsg(_("E963: setting %s to value with wrong type"), name);
2931 return;
2932 }
2933 }
2934
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002936 }
2937 else // add a new variable
2938 {
2939 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002940 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002941 {
2942 semsg(_(e_illvar), name);
2943 return;
2944 }
2945
2946 // Make sure the variable name is valid.
2947 if (!valid_varname(varname))
2948 return;
2949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
2951 if (di == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002952 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 STRCPY(di->di_key, varname);
2954 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002955 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002957 return;
2958 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 di->di_flags = DI_FLAGS_ALLOC;
2960 if (flags & LET_IS_CONST)
2961 di->di_flags |= DI_FLAGS_LOCK;
2962
2963 if (is_script_local && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2964 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002965 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966
2967 // Store a pointer to the typval_T, so that it can be found by
2968 // index instead of using a hastab lookup.
2969 if (ga_grow(&si->sn_var_vals, 1) == OK)
2970 {
2971 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2972 + si->sn_var_vals.ga_len;
2973 sv->sv_name = di->di_key;
2974 sv->sv_tv = &di->di_tv;
2975 sv->sv_type = type == NULL ? &t_any : type;
2976 sv->sv_const = (flags & LET_IS_CONST);
2977 sv->sv_export = is_export;
2978 ++si->sn_var_vals.ga_len;
2979
2980 // let ex_export() know the export worked.
2981 is_export = FALSE;
2982 }
2983 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002984 }
2985
2986 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002988 else
2989 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 di->di_tv = *tv;
2991 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002992 init_tv(tv);
2993 }
2994
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 if (flags & LET_IS_CONST)
2996 di->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002997}
2998
2999/*
3000 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3001 * Also give an error message.
3002 */
3003 int
3004var_check_ro(int flags, char_u *name, int use_gettext)
3005{
3006 if (flags & DI_FLAGS_RO)
3007 {
3008 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3009 return TRUE;
3010 }
3011 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3012 {
3013 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3014 return TRUE;
3015 }
3016 return FALSE;
3017}
3018
3019/*
3020 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3021 * Also give an error message.
3022 */
3023 int
3024var_check_fixed(int flags, char_u *name, int use_gettext)
3025{
3026 if (flags & DI_FLAGS_FIX)
3027 {
3028 semsg(_("E795: Cannot delete variable %s"),
3029 use_gettext ? (char_u *)_(name) : name);
3030 return TRUE;
3031 }
3032 return FALSE;
3033}
3034
3035/*
3036 * Check if a funcref is assigned to a valid variable name.
3037 * Return TRUE and give an error if not.
3038 */
3039 int
3040var_check_func_name(
3041 char_u *name, // points to start of variable name
3042 int new_var) // TRUE when creating the variable
3043{
3044 // Allow for w: b: s: and t:.
3045 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3046 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3047 ? name[2] : name[0]))
3048 {
3049 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3050 name);
3051 return TRUE;
3052 }
3053 // Don't allow hiding a function. When "v" is not NULL we might be
3054 // assigning another function to the same var, the type is checked
3055 // below.
3056 if (new_var && function_exists(name, FALSE))
3057 {
3058 semsg(_("E705: Variable name conflicts with existing function: %s"),
3059 name);
3060 return TRUE;
3061 }
3062 return FALSE;
3063}
3064
3065/*
3066 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
3067 * Also give an error message, using "name" or _("name") when use_gettext is
3068 * TRUE.
3069 */
3070 int
3071var_check_lock(int lock, char_u *name, int use_gettext)
3072{
3073 if (lock & VAR_LOCKED)
3074 {
3075 semsg(_("E741: Value is locked: %s"),
3076 name == NULL ? (char_u *)_("Unknown")
3077 : use_gettext ? (char_u *)_(name)
3078 : name);
3079 return TRUE;
3080 }
3081 if (lock & VAR_FIXED)
3082 {
3083 semsg(_("E742: Cannot change value of %s"),
3084 name == NULL ? (char_u *)_("Unknown")
3085 : use_gettext ? (char_u *)_(name)
3086 : name);
3087 return TRUE;
3088 }
3089 return FALSE;
3090}
3091
3092/*
3093 * Check if a variable name is valid.
3094 * Return FALSE and give an error if not.
3095 */
3096 int
3097valid_varname(char_u *varname)
3098{
3099 char_u *p;
3100
3101 for (p = varname; *p != NUL; ++p)
3102 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3103 && *p != AUTOLOAD_CHAR)
3104 {
3105 semsg(_(e_illvar), varname);
3106 return FALSE;
3107 }
3108 return TRUE;
3109}
3110
3111/*
3112 * getwinvar() and gettabwinvar()
3113 */
3114 static void
3115getwinvar(
3116 typval_T *argvars,
3117 typval_T *rettv,
3118 int off) // 1 for gettabwinvar()
3119{
3120 win_T *win;
3121 char_u *varname;
3122 dictitem_T *v;
3123 tabpage_T *tp = NULL;
3124 int done = FALSE;
3125 win_T *oldcurwin;
3126 tabpage_T *oldtabpage;
3127 int need_switch_win;
3128
3129 if (off == 1)
3130 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3131 else
3132 tp = curtab;
3133 win = find_win_by_nr(&argvars[off], tp);
3134 varname = tv_get_string_chk(&argvars[off + 1]);
3135 ++emsg_off;
3136
3137 rettv->v_type = VAR_STRING;
3138 rettv->vval.v_string = NULL;
3139
3140 if (win != NULL && varname != NULL)
3141 {
3142 // Set curwin to be our win, temporarily. Also set the tabpage,
3143 // otherwise the window is not valid. Only do this when needed,
3144 // autocommands get blocked.
3145 need_switch_win = !(tp == curtab && win == curwin);
3146 if (!need_switch_win
3147 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3148 {
3149 if (*varname == '&')
3150 {
3151 if (varname[1] == NUL)
3152 {
3153 // get all window-local options in a dict
3154 dict_T *opts = get_winbuf_options(FALSE);
3155
3156 if (opts != NULL)
3157 {
3158 rettv_dict_set(rettv, opts);
3159 done = TRUE;
3160 }
3161 }
3162 else if (get_option_tv(&varname, rettv, 1) == OK)
3163 // window-local-option
3164 done = TRUE;
3165 }
3166 else
3167 {
3168 // Look up the variable.
3169 // Let getwinvar({nr}, "") return the "w:" dictionary.
3170 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3171 varname, FALSE);
3172 if (v != NULL)
3173 {
3174 copy_tv(&v->di_tv, rettv);
3175 done = TRUE;
3176 }
3177 }
3178 }
3179
3180 if (need_switch_win)
3181 // restore previous notion of curwin
3182 restore_win(oldcurwin, oldtabpage, TRUE);
3183 }
3184
3185 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3186 // use the default return value
3187 copy_tv(&argvars[off + 2], rettv);
3188
3189 --emsg_off;
3190}
3191
3192/*
3193 * "setwinvar()" and "settabwinvar()" functions
3194 */
3195 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003196setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003197{
3198 win_T *win;
3199 win_T *save_curwin;
3200 tabpage_T *save_curtab;
3201 int need_switch_win;
3202 char_u *varname, *winvarname;
3203 typval_T *varp;
3204 char_u nbuf[NUMBUFLEN];
3205 tabpage_T *tp = NULL;
3206
3207 if (check_secure())
3208 return;
3209
3210 if (off == 1)
3211 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3212 else
3213 tp = curtab;
3214 win = find_win_by_nr(&argvars[off], tp);
3215 varname = tv_get_string_chk(&argvars[off + 1]);
3216 varp = &argvars[off + 2];
3217
3218 if (win != NULL && varname != NULL && varp != NULL)
3219 {
3220 need_switch_win = !(tp == curtab && win == curwin);
3221 if (!need_switch_win
3222 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3223 {
3224 if (*varname == '&')
3225 {
3226 long numval;
3227 char_u *strval;
3228 int error = FALSE;
3229
3230 ++varname;
3231 numval = (long)tv_get_number_chk(varp, &error);
3232 strval = tv_get_string_buf_chk(varp, nbuf);
3233 if (!error && strval != NULL)
3234 set_option_value(varname, numval, strval, OPT_LOCAL);
3235 }
3236 else
3237 {
3238 winvarname = alloc(STRLEN(varname) + 3);
3239 if (winvarname != NULL)
3240 {
3241 STRCPY(winvarname, "w:");
3242 STRCPY(winvarname + 2, varname);
3243 set_var(winvarname, varp, TRUE);
3244 vim_free(winvarname);
3245 }
3246 }
3247 }
3248 if (need_switch_win)
3249 restore_win(save_curwin, save_curtab, TRUE);
3250 }
3251}
3252
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003253/*
3254 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3255 * v:option_type, and v:option_command.
3256 */
3257 void
3258reset_v_option_vars(void)
3259{
3260 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3261 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3262 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3263 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3264 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3265 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3266}
3267
3268/*
3269 * Add an assert error to v:errors.
3270 */
3271 void
3272assert_error(garray_T *gap)
3273{
3274 struct vimvar *vp = &vimvars[VV_ERRORS];
3275
3276 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003277 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003278 set_vim_var_list(VV_ERRORS, list_alloc());
3279 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3280}
3281
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003282 int
3283var_exists(char_u *var)
3284{
3285 char_u *name;
3286 char_u *tofree;
3287 typval_T tv;
3288 int len = 0;
3289 int n = FALSE;
3290
3291 // get_name_len() takes care of expanding curly braces
3292 name = var;
3293 len = get_name_len(&var, &tofree, TRUE, FALSE);
3294 if (len > 0)
3295 {
3296 if (tofree != NULL)
3297 name = tofree;
3298 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
3299 if (n)
3300 {
3301 // handle d.key, l[idx], f(expr)
Bram Moolenaar32e35112020-05-14 22:41:15 +02003302 n = (handle_subscript(&var, &tv, EVAL_EVALUATE,
3303 FALSE, name, &name) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003304 if (n)
3305 clear_tv(&tv);
3306 }
3307 }
3308 if (*var != NUL)
3309 n = FALSE;
3310
3311 vim_free(tofree);
3312 return n;
3313}
3314
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003315static lval_T *redir_lval = NULL;
3316#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3317static garray_T redir_ga; // only valid when redir_lval is not NULL
3318static char_u *redir_endp = NULL;
3319static char_u *redir_varname = NULL;
3320
3321/*
3322 * Start recording command output to a variable
3323 * When "append" is TRUE append to an existing variable.
3324 * Returns OK if successfully completed the setup. FAIL otherwise.
3325 */
3326 int
3327var_redir_start(char_u *name, int append)
3328{
3329 int save_emsg;
3330 int err;
3331 typval_T tv;
3332
3333 // Catch a bad name early.
3334 if (!eval_isnamec1(*name))
3335 {
3336 emsg(_(e_invarg));
3337 return FAIL;
3338 }
3339
3340 // Make a copy of the name, it is used in redir_lval until redir ends.
3341 redir_varname = vim_strsave(name);
3342 if (redir_varname == NULL)
3343 return FAIL;
3344
3345 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3346 if (redir_lval == NULL)
3347 {
3348 var_redir_stop();
3349 return FAIL;
3350 }
3351
3352 // The output is stored in growarray "redir_ga" until redirection ends.
3353 ga_init2(&redir_ga, (int)sizeof(char), 500);
3354
3355 // Parse the variable name (can be a dict or list entry).
3356 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3357 FNE_CHECK_START);
3358 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3359 {
3360 clear_lval(redir_lval);
3361 if (redir_endp != NULL && *redir_endp != NUL)
3362 // Trailing characters are present after the variable name
3363 emsg(_(e_trailing));
3364 else
3365 emsg(_(e_invarg));
3366 redir_endp = NULL; // don't store a value, only cleanup
3367 var_redir_stop();
3368 return FAIL;
3369 }
3370
3371 // check if we can write to the variable: set it to or append an empty
3372 // string
3373 save_emsg = did_emsg;
3374 did_emsg = FALSE;
3375 tv.v_type = VAR_STRING;
3376 tv.vval.v_string = (char_u *)"";
3377 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003379 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003381 clear_lval(redir_lval);
3382 err = did_emsg;
3383 did_emsg |= save_emsg;
3384 if (err)
3385 {
3386 redir_endp = NULL; // don't store a value, only cleanup
3387 var_redir_stop();
3388 return FAIL;
3389 }
3390
3391 return OK;
3392}
3393
3394/*
3395 * Append "value[value_len]" to the variable set by var_redir_start().
3396 * The actual appending is postponed until redirection ends, because the value
3397 * appended may in fact be the string we write to, changing it may cause freed
3398 * memory to be used:
3399 * :redir => foo
3400 * :let foo
3401 * :redir END
3402 */
3403 void
3404var_redir_str(char_u *value, int value_len)
3405{
3406 int len;
3407
3408 if (redir_lval == NULL)
3409 return;
3410
3411 if (value_len == -1)
3412 len = (int)STRLEN(value); // Append the entire string
3413 else
3414 len = value_len; // Append only "value_len" characters
3415
3416 if (ga_grow(&redir_ga, len) == OK)
3417 {
3418 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3419 redir_ga.ga_len += len;
3420 }
3421 else
3422 var_redir_stop();
3423}
3424
3425/*
3426 * Stop redirecting command output to a variable.
3427 * Frees the allocated memory.
3428 */
3429 void
3430var_redir_stop(void)
3431{
3432 typval_T tv;
3433
3434 if (EVALCMD_BUSY)
3435 {
3436 redir_lval = NULL;
3437 return;
3438 }
3439
3440 if (redir_lval != NULL)
3441 {
3442 // If there was no error: assign the text to the variable.
3443 if (redir_endp != NULL)
3444 {
3445 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3446 tv.v_type = VAR_STRING;
3447 tv.vval.v_string = redir_ga.ga_data;
3448 // Call get_lval() again, if it's inside a Dict or List it may
3449 // have changed.
3450 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3451 FALSE, FALSE, 0, FNE_CHECK_START);
3452 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003454 (char_u *)".");
3455 clear_lval(redir_lval);
3456 }
3457
3458 // free the collected output
3459 VIM_CLEAR(redir_ga.ga_data);
3460
3461 VIM_CLEAR(redir_lval);
3462 }
3463 VIM_CLEAR(redir_varname);
3464}
3465
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003466/*
3467 * "gettabvar()" function
3468 */
3469 void
3470f_gettabvar(typval_T *argvars, typval_T *rettv)
3471{
3472 win_T *oldcurwin;
3473 tabpage_T *tp, *oldtabpage;
3474 dictitem_T *v;
3475 char_u *varname;
3476 int done = FALSE;
3477
3478 rettv->v_type = VAR_STRING;
3479 rettv->vval.v_string = NULL;
3480
3481 varname = tv_get_string_chk(&argvars[1]);
3482 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3483 if (tp != NULL && varname != NULL)
3484 {
3485 // Set tp to be our tabpage, temporarily. Also set the window to the
3486 // first window in the tabpage, otherwise the window is not valid.
3487 if (switch_win(&oldcurwin, &oldtabpage,
3488 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3489 : tp->tp_firstwin, tp, TRUE) == OK)
3490 {
3491 // look up the variable
3492 // Let gettabvar({nr}, "") return the "t:" dictionary.
3493 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3494 if (v != NULL)
3495 {
3496 copy_tv(&v->di_tv, rettv);
3497 done = TRUE;
3498 }
3499 }
3500
3501 // restore previous notion of curwin
3502 restore_win(oldcurwin, oldtabpage, TRUE);
3503 }
3504
3505 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3506 copy_tv(&argvars[2], rettv);
3507}
3508
3509/*
3510 * "gettabwinvar()" function
3511 */
3512 void
3513f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3514{
3515 getwinvar(argvars, rettv, 1);
3516}
3517
3518/*
3519 * "getwinvar()" function
3520 */
3521 void
3522f_getwinvar(typval_T *argvars, typval_T *rettv)
3523{
3524 getwinvar(argvars, rettv, 0);
3525}
3526
3527/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003528 * "getbufvar()" function
3529 */
3530 void
3531f_getbufvar(typval_T *argvars, typval_T *rettv)
3532{
3533 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003534 char_u *varname;
3535 dictitem_T *v;
3536 int done = FALSE;
3537
3538 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3539 varname = tv_get_string_chk(&argvars[1]);
3540 ++emsg_off;
3541 buf = tv_get_buf(&argvars[0], FALSE);
3542
3543 rettv->v_type = VAR_STRING;
3544 rettv->vval.v_string = NULL;
3545
3546 if (buf != NULL && varname != NULL)
3547 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003548 if (*varname == '&')
3549 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003550 buf_T *save_curbuf = curbuf;
3551
3552 // set curbuf to be our buf, temporarily
3553 curbuf = buf;
3554
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003555 if (varname[1] == NUL)
3556 {
3557 // get all buffer-local options in a dict
3558 dict_T *opts = get_winbuf_options(TRUE);
3559
3560 if (opts != NULL)
3561 {
3562 rettv_dict_set(rettv, opts);
3563 done = TRUE;
3564 }
3565 }
3566 else if (get_option_tv(&varname, rettv, TRUE) == OK)
3567 // buffer-local-option
3568 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003569
3570 // restore previous notion of curbuf
3571 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003572 }
3573 else
3574 {
3575 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003576 if (*varname == NUL)
3577 // Let getbufvar({nr}, "") return the "b:" dictionary.
3578 v = &buf->b_bufvar;
3579 else
3580 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3581 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003582 if (v != NULL)
3583 {
3584 copy_tv(&v->di_tv, rettv);
3585 done = TRUE;
3586 }
3587 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003588 }
3589
3590 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3591 // use the default value
3592 copy_tv(&argvars[2], rettv);
3593
3594 --emsg_off;
3595}
3596
3597/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003598 * "settabvar()" function
3599 */
3600 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003601f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003602{
3603 tabpage_T *save_curtab;
3604 tabpage_T *tp;
3605 char_u *varname, *tabvarname;
3606 typval_T *varp;
3607
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003608 if (check_secure())
3609 return;
3610
3611 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3612 varname = tv_get_string_chk(&argvars[1]);
3613 varp = &argvars[2];
3614
3615 if (varname != NULL && varp != NULL && tp != NULL)
3616 {
3617 save_curtab = curtab;
3618 goto_tabpage_tp(tp, FALSE, FALSE);
3619
3620 tabvarname = alloc(STRLEN(varname) + 3);
3621 if (tabvarname != NULL)
3622 {
3623 STRCPY(tabvarname, "t:");
3624 STRCPY(tabvarname + 2, varname);
3625 set_var(tabvarname, varp, TRUE);
3626 vim_free(tabvarname);
3627 }
3628
3629 // Restore current tabpage
3630 if (valid_tabpage(save_curtab))
3631 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3632 }
3633}
3634
3635/*
3636 * "settabwinvar()" function
3637 */
3638 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003639f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003640{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003641 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003642}
3643
3644/*
3645 * "setwinvar()" function
3646 */
3647 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003648f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003649{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003650 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003651}
3652
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003653/*
3654 * "setbufvar()" function
3655 */
3656 void
3657f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3658{
3659 buf_T *buf;
3660 char_u *varname, *bufvarname;
3661 typval_T *varp;
3662 char_u nbuf[NUMBUFLEN];
3663
3664 if (check_secure())
3665 return;
3666 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3667 varname = tv_get_string_chk(&argvars[1]);
3668 buf = tv_get_buf(&argvars[0], FALSE);
3669 varp = &argvars[2];
3670
3671 if (buf != NULL && varname != NULL && varp != NULL)
3672 {
3673 if (*varname == '&')
3674 {
3675 long numval;
3676 char_u *strval;
3677 int error = FALSE;
3678 aco_save_T aco;
3679
3680 // set curbuf to be our buf, temporarily
3681 aucmd_prepbuf(&aco, buf);
3682
3683 ++varname;
3684 numval = (long)tv_get_number_chk(varp, &error);
3685 strval = tv_get_string_buf_chk(varp, nbuf);
3686 if (!error && strval != NULL)
3687 set_option_value(varname, numval, strval, OPT_LOCAL);
3688
3689 // reset notion of buffer
3690 aucmd_restbuf(&aco);
3691 }
3692 else
3693 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003694 bufvarname = alloc(STRLEN(varname) + 3);
3695 if (bufvarname != NULL)
3696 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003697 buf_T *save_curbuf = curbuf;
3698
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003699 curbuf = buf;
3700 STRCPY(bufvarname, "b:");
3701 STRCPY(bufvarname + 2, varname);
3702 set_var(bufvarname, varp, TRUE);
3703 vim_free(bufvarname);
3704 curbuf = save_curbuf;
3705 }
3706 }
3707 }
3708}
3709
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003710/*
3711 * Get a callback from "arg". It can be a Funcref or a function name.
3712 * When "arg" is zero return an empty string.
3713 * "cb_name" is not allocated.
3714 * "cb_name" is set to NULL for an invalid argument.
3715 */
3716 callback_T
3717get_callback(typval_T *arg)
3718{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003719 callback_T res;
3720 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003721
3722 res.cb_free_name = FALSE;
3723 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3724 {
3725 res.cb_partial = arg->vval.v_partial;
3726 ++res.cb_partial->pt_refcount;
3727 res.cb_name = partial_name(res.cb_partial);
3728 }
3729 else
3730 {
3731 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003732 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3733 && isdigit(*arg->vval.v_string))
3734 r = FAIL;
3735 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003736 {
3737 // Note that we don't make a copy of the string.
3738 res.cb_name = arg->vval.v_string;
3739 func_ref(res.cb_name);
3740 }
3741 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003742 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003743 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003744 r = FAIL;
3745
3746 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003747 {
3748 emsg(_("E921: Invalid callback argument"));
3749 res.cb_name = NULL;
3750 }
3751 }
3752 return res;
3753}
3754
3755/*
3756 * Copy a callback into a typval_T.
3757 */
3758 void
3759put_callback(callback_T *cb, typval_T *tv)
3760{
3761 if (cb->cb_partial != NULL)
3762 {
3763 tv->v_type = VAR_PARTIAL;
3764 tv->vval.v_partial = cb->cb_partial;
3765 ++tv->vval.v_partial->pt_refcount;
3766 }
3767 else
3768 {
3769 tv->v_type = VAR_FUNC;
3770 tv->vval.v_string = vim_strsave(cb->cb_name);
3771 func_ref(cb->cb_name);
3772 }
3773}
3774
3775/*
3776 * Make a copy of "src" into "dest", allocating the function name if needed,
3777 * without incrementing the refcount.
3778 */
3779 void
3780set_callback(callback_T *dest, callback_T *src)
3781{
3782 if (src->cb_partial == NULL)
3783 {
3784 // just a function name, make a copy
3785 dest->cb_name = vim_strsave(src->cb_name);
3786 dest->cb_free_name = TRUE;
3787 }
3788 else
3789 {
3790 // cb_name is a pointer into cb_partial
3791 dest->cb_name = src->cb_name;
3792 dest->cb_free_name = FALSE;
3793 }
3794 dest->cb_partial = src->cb_partial;
3795}
3796
3797/*
3798 * Unref/free "callback" returned by get_callback() or set_callback().
3799 */
3800 void
3801free_callback(callback_T *callback)
3802{
3803 if (callback->cb_partial != NULL)
3804 {
3805 partial_unref(callback->cb_partial);
3806 callback->cb_partial = NULL;
3807 }
3808 else if (callback->cb_name != NULL)
3809 func_unref(callback->cb_name);
3810 if (callback->cb_free_name)
3811 {
3812 vim_free(callback->cb_name);
3813 callback->cb_free_name = FALSE;
3814 }
3815 callback->cb_name = NULL;
3816}
3817
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003818#endif // FEAT_EVAL