blob: 8a7162c99cd580ac74b8e005ca9e2d3f8bf88c0b [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 Moolenaarc82a5b52020-06-13 18:09:19 +0200731 {
732 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9)
733 {
734 // Vim9 declaration ":let var: type"
735 arg = vim9_declare_scriptvar(eap, arg);
736 }
737 else
738 {
739 // ":let var1 var2" - list values
740 arg = list_arg_vars(eap, arg, &first);
741 }
742 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200743 else if (!eap->skip)
744 {
745 // ":let"
746 list_glob_vars(&first);
747 list_buf_vars(&first);
748 list_win_vars(&first);
749 list_tab_vars(&first);
750 list_script_vars(&first);
751 list_func_vars(&first);
752 list_vim_vars(&first);
753 }
754 eap->nextcmd = check_nextcmd(arg);
755 }
756 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
757 {
758 list_T *l;
759
760 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200761 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200762 if (l != NULL)
763 {
764 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200765 if (!eap->skip)
766 {
767 op[0] = '=';
768 op[1] = NUL;
769 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200771 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200772 clear_tv(&rettv);
773 }
774 }
775 else
776 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200777 int eval_flags;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200778
Bram Moolenaar32e35112020-05-14 22:41:15 +0200779 rettv.v_type = VAR_UNKNOWN;
780 i = FAIL;
781 if (has_assign || concat)
782 {
783 op[0] = '=';
784 op[1] = NUL;
785 if (*expr != '=')
786 {
787 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
788 {
789 op[0] = *expr; // +=, -=, *=, /=, %= or .=
790 if (expr[0] == '.' && expr[1] == '.') // ..=
791 ++expr;
792 }
793 expr = skipwhite(expr + 2);
794 }
795 else
796 expr = skipwhite(expr + 1);
797
798 if (eap->skip)
799 ++emsg_skip;
800 eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200801 i = eval0(expr, &rettv, &eap->nextcmd, eval_flags);
802 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200803 if (eap->skip)
804 {
805 if (i != FAIL)
806 clear_tv(&rettv);
807 --emsg_skip;
808 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200809 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200810 {
811 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100812 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200813 clear_tv(&rettv);
814 }
815 }
816}
817
818/*
819 * Assign the typevalue "tv" to the variable or variables at "arg_start".
820 * Handles both "var" with any type and "[var, var; var]" with a list type.
821 * When "op" is not NULL it points to a string with characters that
822 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
823 * or concatenate.
824 * Returns OK or FAIL;
825 */
826 int
827ex_let_vars(
828 char_u *arg_start,
829 typval_T *tv,
830 int copy, // copy values from "tv", don't move
831 int semicolon, // from skip_var_list()
832 int var_count, // from skip_var_list()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100833 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200834 char_u *op)
835{
836 char_u *arg = arg_start;
837 list_T *l;
838 int i;
839 listitem_T *item;
840 typval_T ltv;
841
842 if (*arg != '[')
843 {
844 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200846 return FAIL;
847 return OK;
848 }
849
850 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
851 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
852 {
853 emsg(_(e_listreq));
854 return FAIL;
855 }
856
857 i = list_len(l);
858 if (semicolon == 0 && var_count < i)
859 {
860 emsg(_("E687: Less targets than List items"));
861 return FAIL;
862 }
863 if (var_count - semicolon > i)
864 {
865 emsg(_("E688: More targets than List items"));
866 return FAIL;
867 }
868
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200869 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200870 item = l->lv_first;
871 while (*arg != ']')
872 {
873 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200875 item = item->li_next;
876 if (arg == NULL)
877 return FAIL;
878
879 arg = skipwhite(arg);
880 if (*arg == ';')
881 {
882 // Put the rest of the list (may be empty) in the var after ';'.
883 // Create a new list for this.
884 l = list_alloc();
885 if (l == NULL)
886 return FAIL;
887 while (item != NULL)
888 {
889 list_append_tv(l, &item->li_tv);
890 item = item->li_next;
891 }
892
893 ltv.v_type = VAR_LIST;
894 ltv.v_lock = 0;
895 ltv.vval.v_list = l;
896 l->lv_refcount = 1;
897
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200899 (char_u *)"]", op);
900 clear_tv(&ltv);
901 if (arg == NULL)
902 return FAIL;
903 break;
904 }
905 else if (*arg != ',' && *arg != ']')
906 {
907 internal_error("ex_let_vars()");
908 return FAIL;
909 }
910 }
911
912 return OK;
913}
914
915/*
916 * Skip over assignable variable "var" or list of variables "[var, var]".
917 * Used for ":let varvar = expr" and ":for varvar in expr".
918 * For "[var, var]" increment "*var_count" for each variable.
919 * for "[var, var; var]" set "semicolon".
920 * Return NULL for an error.
921 */
922 char_u *
923skip_var_list(
924 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100925 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200926 int *var_count,
927 int *semicolon)
928{
929 char_u *p, *s;
930
931 if (*arg == '[')
932 {
933 // "[var, var]": find the matching ']'.
934 p = arg;
935 for (;;)
936 {
937 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100938 s = skip_var_one(p, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200939 if (s == p)
940 {
941 semsg(_(e_invarg2), p);
942 return NULL;
943 }
944 ++*var_count;
945
946 p = skipwhite(s);
947 if (*p == ']')
948 break;
949 else if (*p == ';')
950 {
951 if (*semicolon == 1)
952 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200953 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200954 return NULL;
955 }
956 *semicolon = 1;
957 }
958 else if (*p != ',')
959 {
960 semsg(_(e_invarg2), p);
961 return NULL;
962 }
963 }
964 return p + 1;
965 }
966 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100967 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200968}
969
970/*
971 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
972 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100973 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200974 */
975 static char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100976skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200977{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100978 char_u *end;
979
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200980 if (*arg == '@' && arg[1] != NUL)
981 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100982 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200983 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100984 if (include_type && current_sctx.sc_version == SCRIPT_VERSION_VIM9
985 && *end == ':')
986 {
987 end = skip_type(skipwhite(end + 1));
988 }
989 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200990}
991
992/*
993 * List variables for hashtab "ht" with prefix "prefix".
994 * If "empty" is TRUE also list NULL strings as empty strings.
995 */
996 void
997list_hashtable_vars(
998 hashtab_T *ht,
999 char *prefix,
1000 int empty,
1001 int *first)
1002{
1003 hashitem_T *hi;
1004 dictitem_T *di;
1005 int todo;
1006 char_u buf[IOSIZE];
1007
1008 todo = (int)ht->ht_used;
1009 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1010 {
1011 if (!HASHITEM_EMPTY(hi))
1012 {
1013 --todo;
1014 di = HI2DI(hi);
1015
1016 // apply :filter /pat/ to variable name
1017 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1018 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1019 if (message_filtered(buf))
1020 continue;
1021
1022 if (empty || di->di_tv.v_type != VAR_STRING
1023 || di->di_tv.vval.v_string != NULL)
1024 list_one_var(di, prefix, first);
1025 }
1026 }
1027}
1028
1029/*
1030 * List global variables.
1031 */
1032 static void
1033list_glob_vars(int *first)
1034{
1035 list_hashtable_vars(&globvarht, "", TRUE, first);
1036}
1037
1038/*
1039 * List buffer variables.
1040 */
1041 static void
1042list_buf_vars(int *first)
1043{
1044 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1045}
1046
1047/*
1048 * List window variables.
1049 */
1050 static void
1051list_win_vars(int *first)
1052{
1053 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1054}
1055
1056/*
1057 * List tab page variables.
1058 */
1059 static void
1060list_tab_vars(int *first)
1061{
1062 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1063}
1064
1065/*
1066 * List variables in "arg".
1067 */
1068 static char_u *
1069list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1070{
1071 int error = FALSE;
1072 int len;
1073 char_u *name;
1074 char_u *name_start;
1075 char_u *arg_subsc;
1076 char_u *tofree;
1077 typval_T tv;
1078
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001079 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001080 {
1081 if (error || eap->skip)
1082 {
1083 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1084 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1085 {
1086 emsg_severe = TRUE;
1087 emsg(_(e_trailing));
1088 break;
1089 }
1090 }
1091 else
1092 {
1093 // get_name_len() takes care of expanding curly braces
1094 name_start = name = arg;
1095 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1096 if (len <= 0)
1097 {
1098 // This is mainly to keep test 49 working: when expanding
1099 // curly braces fails overrule the exception error message.
1100 if (len < 0 && !aborting())
1101 {
1102 emsg_severe = TRUE;
1103 semsg(_(e_invarg2), arg);
1104 break;
1105 }
1106 error = TRUE;
1107 }
1108 else
1109 {
1110 if (tofree != NULL)
1111 name = tofree;
1112 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
1113 error = TRUE;
1114 else
1115 {
1116 // handle d.key, l[idx], f(expr)
1117 arg_subsc = arg;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001118 if (handle_subscript(&arg, &tv, EVAL_EVALUATE, TRUE,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001119 name, &name) == FAIL)
1120 error = TRUE;
1121 else
1122 {
1123 if (arg == arg_subsc && len == 2 && name[1] == ':')
1124 {
1125 switch (*name)
1126 {
1127 case 'g': list_glob_vars(first); break;
1128 case 'b': list_buf_vars(first); break;
1129 case 'w': list_win_vars(first); break;
1130 case 't': list_tab_vars(first); break;
1131 case 'v': list_vim_vars(first); break;
1132 case 's': list_script_vars(first); break;
1133 case 'l': list_func_vars(first); break;
1134 default:
1135 semsg(_("E738: Can't list variables for %s"), name);
1136 }
1137 }
1138 else
1139 {
1140 char_u numbuf[NUMBUFLEN];
1141 char_u *tf;
1142 int c;
1143 char_u *s;
1144
1145 s = echo_string(&tv, &tf, numbuf, 0);
1146 c = *arg;
1147 *arg = NUL;
1148 list_one_var_a("",
1149 arg == arg_subsc ? name : name_start,
1150 tv.v_type,
1151 s == NULL ? (char_u *)"" : s,
1152 first);
1153 *arg = c;
1154 vim_free(tf);
1155 }
1156 clear_tv(&tv);
1157 }
1158 }
1159 }
1160
1161 vim_free(tofree);
1162 }
1163
1164 arg = skipwhite(arg);
1165 }
1166
1167 return arg;
1168}
1169
1170/*
1171 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1172 * Returns a pointer to the char just after the var name.
1173 * Returns NULL if there is an error.
1174 */
1175 static char_u *
1176ex_let_one(
1177 char_u *arg, // points to variable name
1178 typval_T *tv, // value to assign to variable
1179 int copy, // copy value from "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001181 char_u *endchars, // valid chars after variable name or NULL
1182 char_u *op) // "+", "-", "." or NULL
1183{
1184 int c1;
1185 char_u *name;
1186 char_u *p;
1187 char_u *arg_end = NULL;
1188 int len;
1189 int opt_flags;
1190 char_u *tofree = NULL;
1191
1192 // ":let $VAR = expr": Set environment variable.
1193 if (*arg == '$')
1194 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001195 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001196 {
1197 emsg(_("E996: Cannot lock an environment variable"));
1198 return NULL;
1199 }
1200 // Find the end of the name.
1201 ++arg;
1202 name = arg;
1203 len = get_env_len(&arg);
1204 if (len == 0)
1205 semsg(_(e_invarg2), name - 1);
1206 else
1207 {
1208 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1209 semsg(_(e_letwrong), op);
1210 else if (endchars != NULL
1211 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1212 emsg(_(e_letunexp));
1213 else if (!check_secure())
1214 {
1215 c1 = name[len];
1216 name[len] = NUL;
1217 p = tv_get_string_chk(tv);
1218 if (p != NULL && op != NULL && *op == '.')
1219 {
1220 int mustfree = FALSE;
1221 char_u *s = vim_getenv(name, &mustfree);
1222
1223 if (s != NULL)
1224 {
1225 p = tofree = concat_str(s, p);
1226 if (mustfree)
1227 vim_free(s);
1228 }
1229 }
1230 if (p != NULL)
1231 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001232 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001233 arg_end = arg;
1234 }
1235 name[len] = c1;
1236 vim_free(tofree);
1237 }
1238 }
1239 }
1240
1241 // ":let &option = expr": Set option value.
1242 // ":let &l:option = expr": Set local option value.
1243 // ":let &g:option = expr": Set global option value.
1244 else if (*arg == '&')
1245 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001246 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001247 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001248 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001249 return NULL;
1250 }
1251 // Find the end of the name.
1252 p = find_option_end(&arg, &opt_flags);
1253 if (p == NULL || (endchars != NULL
1254 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1255 emsg(_(e_letunexp));
1256 else
1257 {
1258 long n;
1259 int opt_type;
1260 long numval;
1261 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001262 char_u *s = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001263
1264 c1 = *p;
1265 *p = NUL;
1266
1267 n = (long)tv_get_number(tv);
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001268 // avoid setting a string option to the text "v:false" or similar.
1269 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL)
1270 s = tv_get_string_chk(tv); // != NULL if number or string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001271 if (s != NULL && op != NULL && *op != '=')
1272 {
1273 opt_type = get_option_value(arg, &numval,
1274 &stringval, opt_flags);
1275 if ((opt_type == 1 && *op == '.')
1276 || (opt_type == 0 && *op != '.'))
1277 {
1278 semsg(_(e_letwrong), op);
1279 s = NULL; // don't set the value
1280 }
1281 else
1282 {
1283 if (opt_type == 1) // number
1284 {
1285 switch (*op)
1286 {
1287 case '+': n = numval + n; break;
1288 case '-': n = numval - n; break;
1289 case '*': n = numval * n; break;
1290 case '/': n = (long)num_divide(numval, n); break;
1291 case '%': n = (long)num_modulus(numval, n); break;
1292 }
1293 }
1294 else if (opt_type == 0 && stringval != NULL) // string
1295 {
1296 s = concat_str(stringval, s);
1297 vim_free(stringval);
1298 stringval = s;
1299 }
1300 }
1301 }
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001302 if (s != NULL || tv->v_type == VAR_BOOL
1303 || tv->v_type == VAR_SPECIAL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001304 {
1305 set_option_value(arg, n, s, opt_flags);
1306 arg_end = p;
1307 }
1308 *p = c1;
1309 vim_free(stringval);
1310 }
1311 }
1312
1313 // ":let @r = expr": Set register contents.
1314 else if (*arg == '@')
1315 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001317 {
1318 emsg(_("E996: Cannot lock a register"));
1319 return NULL;
1320 }
1321 ++arg;
1322 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1323 semsg(_(e_letwrong), op);
1324 else if (endchars != NULL
1325 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1326 emsg(_(e_letunexp));
1327 else
1328 {
1329 char_u *ptofree = NULL;
1330 char_u *s;
1331
1332 p = tv_get_string_chk(tv);
1333 if (p != NULL && op != NULL && *op == '.')
1334 {
1335 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1336 if (s != NULL)
1337 {
1338 p = ptofree = concat_str(s, p);
1339 vim_free(s);
1340 }
1341 }
1342 if (p != NULL)
1343 {
1344 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1345 arg_end = arg + 1;
1346 }
1347 vim_free(ptofree);
1348 }
1349 }
1350
1351 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001353 // ":let {expr} = expr": Idem, name made with curly braces
1354 else if (eval_isnamec1(*arg) || *arg == '{')
1355 {
1356 lval_T lv;
1357
1358 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001359 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001360 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 if (endchars != NULL && vim_strchr(endchars,
1362 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001363 emsg(_(e_letunexp));
1364 else
1365 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001367 arg_end = p;
1368 }
1369 }
1370 clear_lval(&lv);
1371 }
1372
1373 else
1374 semsg(_(e_invarg2), arg);
1375
1376 return arg_end;
1377}
1378
1379/*
1380 * ":unlet[!] var1 ... " command.
1381 */
1382 void
1383ex_unlet(exarg_T *eap)
1384{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001385 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001386}
1387
1388/*
1389 * ":lockvar" and ":unlockvar" commands
1390 */
1391 void
1392ex_lockvar(exarg_T *eap)
1393{
1394 char_u *arg = eap->arg;
1395 int deep = 2;
1396
1397 if (eap->forceit)
1398 deep = -1;
1399 else if (vim_isdigit(*arg))
1400 {
1401 deep = getdigits(&arg);
1402 arg = skipwhite(arg);
1403 }
1404
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001405 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001406}
1407
1408/*
1409 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001410 * Also used for Vim9 script. "callback" is invoked as:
1411 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001412 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001413 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001414ex_unletlock(
1415 exarg_T *eap,
1416 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001417 int deep,
1418 int glv_flags,
1419 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1420 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001421{
1422 char_u *arg = argstart;
1423 char_u *name_end;
1424 int error = FALSE;
1425 lval_T lv;
1426
1427 do
1428 {
1429 if (*arg == '$')
1430 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001431 lv.ll_name = arg;
1432 lv.ll_tv = NULL;
1433 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001434 if (get_env_len(&arg) == 0)
1435 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001436 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001437 return;
1438 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001439 if (!error && !eap->skip
1440 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1441 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001442 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001443 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001444 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001445 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001446 // Parse the name and find the end.
1447 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1448 glv_flags, FNE_CHECK_START);
1449 if (lv.ll_name == NULL)
1450 error = TRUE; // error but continue parsing
1451 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1452 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001453 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001454 if (name_end != NULL)
1455 {
1456 emsg_severe = TRUE;
1457 emsg(_(e_trailing));
1458 }
1459 if (!(eap->skip || error))
1460 clear_lval(&lv);
1461 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001462 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001463
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001464 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001465 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001466 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001467
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001468 if (!eap->skip)
1469 clear_lval(&lv);
1470 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001471
1472 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001473 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001474
1475 eap->nextcmd = check_nextcmd(arg);
1476}
1477
1478 static int
1479do_unlet_var(
1480 lval_T *lp,
1481 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001482 exarg_T *eap,
1483 int deep UNUSED,
1484 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001485{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001486 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001487 int ret = OK;
1488 int cc;
1489
1490 if (lp->ll_tv == NULL)
1491 {
1492 cc = *name_end;
1493 *name_end = NUL;
1494
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001495 // Environment variable, normal name or expanded name.
1496 if (*lp->ll_name == '$')
1497 vim_unsetenv(lp->ll_name + 1);
1498 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001499 ret = FAIL;
1500 *name_end = cc;
1501 }
1502 else if ((lp->ll_list != NULL
1503 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
1504 || (lp->ll_dict != NULL
1505 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
1506 return FAIL;
1507 else if (lp->ll_range)
1508 {
1509 listitem_T *li;
1510 listitem_T *ll_li = lp->ll_li;
1511 int ll_n1 = lp->ll_n1;
1512
1513 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1514 {
1515 li = ll_li->li_next;
1516 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
1517 return FAIL;
1518 ll_li = li;
1519 ++ll_n1;
1520 }
1521
1522 // Delete a range of List items.
1523 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1524 {
1525 li = lp->ll_li->li_next;
1526 listitem_remove(lp->ll_list, lp->ll_li);
1527 lp->ll_li = li;
1528 ++lp->ll_n1;
1529 }
1530 }
1531 else
1532 {
1533 if (lp->ll_list != NULL)
1534 // unlet a List item.
1535 listitem_remove(lp->ll_list, lp->ll_li);
1536 else
1537 // unlet a Dictionary item.
1538 dictitem_remove(lp->ll_dict, lp->ll_di);
1539 }
1540
1541 return ret;
1542}
1543
1544/*
1545 * "unlet" a variable. Return OK if it existed, FAIL if not.
1546 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1547 */
1548 int
1549do_unlet(char_u *name, int forceit)
1550{
1551 hashtab_T *ht;
1552 hashitem_T *hi;
1553 char_u *varname;
1554 dict_T *d;
1555 dictitem_T *di;
1556
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001557 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1558 && check_vim9_unlet(name) == FAIL)
1559 return FAIL;
1560
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001561 ht = find_var_ht(name, &varname);
1562 if (ht != NULL && *varname != NUL)
1563 {
1564 d = get_current_funccal_dict(ht);
1565 if (d == NULL)
1566 {
1567 if (ht == &globvarht)
1568 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001569 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001570 d = &vimvardict;
1571 else
1572 {
1573 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1574 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1575 }
1576 if (d == NULL)
1577 {
1578 internal_error("do_unlet()");
1579 return FAIL;
1580 }
1581 }
1582 hi = hash_find(ht, varname);
1583 if (HASHITEM_EMPTY(hi))
1584 hi = find_hi_in_scoped_ht(name, &ht);
1585 if (hi != NULL && !HASHITEM_EMPTY(hi))
1586 {
1587 di = HI2DI(hi);
1588 if (var_check_fixed(di->di_flags, name, FALSE)
1589 || var_check_ro(di->di_flags, name, FALSE)
1590 || var_check_lock(d->dv_lock, name, FALSE))
1591 return FAIL;
1592
1593 delete_var(ht, hi);
1594 return OK;
1595 }
1596 }
1597 if (forceit)
1598 return OK;
1599 semsg(_("E108: No such variable: \"%s\""), name);
1600 return FAIL;
1601}
1602
1603/*
1604 * Lock or unlock variable indicated by "lp".
1605 * "deep" is the levels to go (-1 for unlimited);
1606 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1607 */
1608 static int
1609do_lock_var(
1610 lval_T *lp,
1611 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001612 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001613 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001614 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001615{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001616 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001617 int ret = OK;
1618 int cc;
1619 dictitem_T *di;
1620
1621 if (deep == 0) // nothing to do
1622 return OK;
1623
1624 if (lp->ll_tv == NULL)
1625 {
1626 cc = *name_end;
1627 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001628 if (*lp->ll_name == '$')
1629 {
1630 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001631 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001632 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001633 else
1634 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001635 // Normal name or expanded name.
1636 di = find_var(lp->ll_name, NULL, TRUE);
1637 if (di == NULL)
1638 ret = FAIL;
1639 else if ((di->di_flags & DI_FLAGS_FIX)
1640 && di->di_tv.v_type != VAR_DICT
1641 && di->di_tv.v_type != VAR_LIST)
1642 {
1643 // For historic reasons this error is not given for a list or
1644 // dict. E.g., the b: dict could be locked/unlocked.
1645 semsg(_(e_lock_unlock), lp->ll_name);
1646 ret = FAIL;
1647 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001648 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001649 {
1650 if (lock)
1651 di->di_flags |= DI_FLAGS_LOCK;
1652 else
1653 di->di_flags &= ~DI_FLAGS_LOCK;
1654 item_lock(&di->di_tv, deep, lock);
1655 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001656 }
1657 *name_end = cc;
1658 }
1659 else if (lp->ll_range)
1660 {
1661 listitem_T *li = lp->ll_li;
1662
1663 // (un)lock a range of List items.
1664 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1665 {
1666 item_lock(&li->li_tv, deep, lock);
1667 li = li->li_next;
1668 ++lp->ll_n1;
1669 }
1670 }
1671 else if (lp->ll_list != NULL)
1672 // (un)lock a List item.
1673 item_lock(&lp->ll_li->li_tv, deep, lock);
1674 else
1675 // (un)lock a Dictionary item.
1676 item_lock(&lp->ll_di->di_tv, deep, lock);
1677
1678 return ret;
1679}
1680
1681/*
1682 * Lock or unlock an item. "deep" is nr of levels to go.
1683 */
1684 static void
1685item_lock(typval_T *tv, int deep, int lock)
1686{
1687 static int recurse = 0;
1688 list_T *l;
1689 listitem_T *li;
1690 dict_T *d;
1691 blob_T *b;
1692 hashitem_T *hi;
1693 int todo;
1694
1695 if (recurse >= DICT_MAXNEST)
1696 {
1697 emsg(_("E743: variable nested too deep for (un)lock"));
1698 return;
1699 }
1700 if (deep == 0)
1701 return;
1702 ++recurse;
1703
1704 // lock/unlock the item itself
1705 if (lock)
1706 tv->v_lock |= VAR_LOCKED;
1707 else
1708 tv->v_lock &= ~VAR_LOCKED;
1709
1710 switch (tv->v_type)
1711 {
1712 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001713 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001715 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001717 case VAR_STRING:
1718 case VAR_FUNC:
1719 case VAR_PARTIAL:
1720 case VAR_FLOAT:
1721 case VAR_SPECIAL:
1722 case VAR_JOB:
1723 case VAR_CHANNEL:
1724 break;
1725
1726 case VAR_BLOB:
1727 if ((b = tv->vval.v_blob) != NULL)
1728 {
1729 if (lock)
1730 b->bv_lock |= VAR_LOCKED;
1731 else
1732 b->bv_lock &= ~VAR_LOCKED;
1733 }
1734 break;
1735 case VAR_LIST:
1736 if ((l = tv->vval.v_list) != NULL)
1737 {
1738 if (lock)
1739 l->lv_lock |= VAR_LOCKED;
1740 else
1741 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001742 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001743 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001744 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001745 item_lock(&li->li_tv, deep - 1, lock);
1746 }
1747 break;
1748 case VAR_DICT:
1749 if ((d = tv->vval.v_dict) != NULL)
1750 {
1751 if (lock)
1752 d->dv_lock |= VAR_LOCKED;
1753 else
1754 d->dv_lock &= ~VAR_LOCKED;
1755 if (deep < 0 || deep > 1)
1756 {
1757 // recursive: lock/unlock the items the List contains
1758 todo = (int)d->dv_hashtab.ht_used;
1759 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1760 {
1761 if (!HASHITEM_EMPTY(hi))
1762 {
1763 --todo;
1764 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
1765 }
1766 }
1767 }
1768 }
1769 }
1770 --recurse;
1771}
1772
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001773#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1774/*
1775 * Delete all "menutrans_" variables.
1776 */
1777 void
1778del_menutrans_vars(void)
1779{
1780 hashitem_T *hi;
1781 int todo;
1782
1783 hash_lock(&globvarht);
1784 todo = (int)globvarht.ht_used;
1785 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1786 {
1787 if (!HASHITEM_EMPTY(hi))
1788 {
1789 --todo;
1790 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1791 delete_var(&globvarht, hi);
1792 }
1793 }
1794 hash_unlock(&globvarht);
1795}
1796#endif
1797
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001798/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001799 * Local string buffer for the next two functions to store a variable name
1800 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1801 * get_user_var_name().
1802 */
1803
1804static char_u *varnamebuf = NULL;
1805static int varnamebuflen = 0;
1806
1807/*
1808 * Function to concatenate a prefix and a variable name.
1809 */
1810 static char_u *
1811cat_prefix_varname(int prefix, char_u *name)
1812{
1813 int len;
1814
1815 len = (int)STRLEN(name) + 3;
1816 if (len > varnamebuflen)
1817 {
1818 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001819 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001820 varnamebuf = alloc(len);
1821 if (varnamebuf == NULL)
1822 {
1823 varnamebuflen = 0;
1824 return NULL;
1825 }
1826 varnamebuflen = len;
1827 }
1828 *varnamebuf = prefix;
1829 varnamebuf[1] = ':';
1830 STRCPY(varnamebuf + 2, name);
1831 return varnamebuf;
1832}
1833
1834/*
1835 * Function given to ExpandGeneric() to obtain the list of user defined
1836 * (global/buffer/window/built-in) variable names.
1837 */
1838 char_u *
1839get_user_var_name(expand_T *xp, int idx)
1840{
1841 static long_u gdone;
1842 static long_u bdone;
1843 static long_u wdone;
1844 static long_u tdone;
1845 static int vidx;
1846 static hashitem_T *hi;
1847 hashtab_T *ht;
1848
1849 if (idx == 0)
1850 {
1851 gdone = bdone = wdone = vidx = 0;
1852 tdone = 0;
1853 }
1854
1855 // Global variables
1856 if (gdone < globvarht.ht_used)
1857 {
1858 if (gdone++ == 0)
1859 hi = globvarht.ht_array;
1860 else
1861 ++hi;
1862 while (HASHITEM_EMPTY(hi))
1863 ++hi;
1864 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1865 return cat_prefix_varname('g', hi->hi_key);
1866 return hi->hi_key;
1867 }
1868
1869 // b: variables
1870 ht = &curbuf->b_vars->dv_hashtab;
1871 if (bdone < ht->ht_used)
1872 {
1873 if (bdone++ == 0)
1874 hi = ht->ht_array;
1875 else
1876 ++hi;
1877 while (HASHITEM_EMPTY(hi))
1878 ++hi;
1879 return cat_prefix_varname('b', hi->hi_key);
1880 }
1881
1882 // w: variables
1883 ht = &curwin->w_vars->dv_hashtab;
1884 if (wdone < ht->ht_used)
1885 {
1886 if (wdone++ == 0)
1887 hi = ht->ht_array;
1888 else
1889 ++hi;
1890 while (HASHITEM_EMPTY(hi))
1891 ++hi;
1892 return cat_prefix_varname('w', hi->hi_key);
1893 }
1894
1895 // t: variables
1896 ht = &curtab->tp_vars->dv_hashtab;
1897 if (tdone < ht->ht_used)
1898 {
1899 if (tdone++ == 0)
1900 hi = ht->ht_array;
1901 else
1902 ++hi;
1903 while (HASHITEM_EMPTY(hi))
1904 ++hi;
1905 return cat_prefix_varname('t', hi->hi_key);
1906 }
1907
1908 // v: variables
1909 if (vidx < VV_LEN)
1910 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
1911
1912 VIM_CLEAR(varnamebuf);
1913 varnamebuflen = 0;
1914 return NULL;
1915}
1916
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001917 char *
1918get_var_special_name(int nr)
1919{
1920 switch (nr)
1921 {
1922 case VVAL_FALSE: return "v:false";
1923 case VVAL_TRUE: return "v:true";
1924 case VVAL_NONE: return "v:none";
1925 case VVAL_NULL: return "v:null";
1926 }
1927 internal_error("get_var_special_name()");
1928 return "42";
1929}
1930
1931/*
1932 * Returns the global variable dictionary
1933 */
1934 dict_T *
1935get_globvar_dict(void)
1936{
1937 return &globvardict;
1938}
1939
1940/*
1941 * Returns the global variable hash table
1942 */
1943 hashtab_T *
1944get_globvar_ht(void)
1945{
1946 return &globvarht;
1947}
1948
1949/*
1950 * Returns the v: variable dictionary
1951 */
1952 dict_T *
1953get_vimvar_dict(void)
1954{
1955 return &vimvardict;
1956}
1957
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001958/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001960 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961 */
1962 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001963find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001964{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001965 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
1966 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001967
1968 if (di == NULL)
1969 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001970 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001971 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
1972 return (int)(vv - vimvars);
1973}
1974
1975
1976/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02001977 * Set type of v: variable to "type".
1978 */
1979 void
1980set_vim_var_type(int idx, vartype_T type)
1981{
1982 vimvars[idx].vv_type = type;
1983}
1984
1985/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001986 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001987 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001988 */
1989 void
1990set_vim_var_nr(int idx, varnumber_T val)
1991{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001992 vimvars[idx].vv_nr = val;
1993}
1994
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001995 char *
1996get_vim_var_name(int idx)
1997{
1998 return vimvars[idx].vv_name;
1999}
2000
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002001/*
2002 * Get typval_T v: variable value.
2003 */
2004 typval_T *
2005get_vim_var_tv(int idx)
2006{
2007 return &vimvars[idx].vv_tv;
2008}
2009
2010/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002011 * Set v: variable to "tv". Only accepts the same type.
2012 * Takes over the value of "tv".
2013 */
2014 int
2015set_vim_var_tv(int idx, typval_T *tv)
2016{
2017 if (vimvars[idx].vv_type != tv->v_type)
2018 {
2019 emsg(_("E1063: type mismatch for v: variable"));
2020 clear_tv(tv);
2021 return FAIL;
2022 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002023 // VV_RO is also checked when compiling, but let's check here as well.
2024 if (vimvars[idx].vv_flags & VV_RO)
2025 {
2026 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2027 return FAIL;
2028 }
2029 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2030 {
2031 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2032 return FAIL;
2033 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002034 clear_tv(&vimvars[idx].vv_di.di_tv);
2035 vimvars[idx].vv_di.di_tv = *tv;
2036 return OK;
2037}
2038
2039/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002040 * Get number v: variable value.
2041 */
2042 varnumber_T
2043get_vim_var_nr(int idx)
2044{
2045 return vimvars[idx].vv_nr;
2046}
2047
2048/*
2049 * Get string v: variable value. Uses a static buffer, can only be used once.
2050 * If the String variable has never been set, return an empty string.
2051 * Never returns NULL;
2052 */
2053 char_u *
2054get_vim_var_str(int idx)
2055{
2056 return tv_get_string(&vimvars[idx].vv_tv);
2057}
2058
2059/*
2060 * Get List v: variable value. Caller must take care of reference count when
2061 * needed.
2062 */
2063 list_T *
2064get_vim_var_list(int idx)
2065{
2066 return vimvars[idx].vv_list;
2067}
2068
2069/*
2070 * Get Dict v: variable value. Caller must take care of reference count when
2071 * needed.
2072 */
2073 dict_T *
2074get_vim_var_dict(int idx)
2075{
2076 return vimvars[idx].vv_dict;
2077}
2078
2079/*
2080 * Set v:char to character "c".
2081 */
2082 void
2083set_vim_var_char(int c)
2084{
2085 char_u buf[MB_MAXBYTES + 1];
2086
2087 if (has_mbyte)
2088 buf[(*mb_char2bytes)(c, buf)] = NUL;
2089 else
2090 {
2091 buf[0] = c;
2092 buf[1] = NUL;
2093 }
2094 set_vim_var_string(VV_CHAR, buf, -1);
2095}
2096
2097/*
2098 * Set v:count to "count" and v:count1 to "count1".
2099 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2100 */
2101 void
2102set_vcount(
2103 long count,
2104 long count1,
2105 int set_prevcount)
2106{
2107 if (set_prevcount)
2108 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2109 vimvars[VV_COUNT].vv_nr = count;
2110 vimvars[VV_COUNT1].vv_nr = count1;
2111}
2112
2113/*
2114 * Save variables that might be changed as a side effect. Used when executing
2115 * a timer callback.
2116 */
2117 void
2118save_vimvars(vimvars_save_T *vvsave)
2119{
2120 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2121 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2122 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2123}
2124
2125/*
2126 * Restore variables saved by save_vimvars().
2127 */
2128 void
2129restore_vimvars(vimvars_save_T *vvsave)
2130{
2131 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2132 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2133 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2134}
2135
2136/*
2137 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2138 * value.
2139 */
2140 void
2141set_vim_var_string(
2142 int idx,
2143 char_u *val,
2144 int len) // length of "val" to use or -1 (whole string)
2145{
2146 clear_tv(&vimvars[idx].vv_di.di_tv);
2147 vimvars[idx].vv_type = VAR_STRING;
2148 if (val == NULL)
2149 vimvars[idx].vv_str = NULL;
2150 else if (len == -1)
2151 vimvars[idx].vv_str = vim_strsave(val);
2152 else
2153 vimvars[idx].vv_str = vim_strnsave(val, len);
2154}
2155
2156/*
2157 * Set List v: variable to "val".
2158 */
2159 void
2160set_vim_var_list(int idx, list_T *val)
2161{
2162 clear_tv(&vimvars[idx].vv_di.di_tv);
2163 vimvars[idx].vv_type = VAR_LIST;
2164 vimvars[idx].vv_list = val;
2165 if (val != NULL)
2166 ++val->lv_refcount;
2167}
2168
2169/*
2170 * Set Dictionary v: variable to "val".
2171 */
2172 void
2173set_vim_var_dict(int idx, dict_T *val)
2174{
2175 clear_tv(&vimvars[idx].vv_di.di_tv);
2176 vimvars[idx].vv_type = VAR_DICT;
2177 vimvars[idx].vv_dict = val;
2178 if (val != NULL)
2179 {
2180 ++val->dv_refcount;
2181 dict_set_items_ro(val);
2182 }
2183}
2184
2185/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002186 * Set the v:argv list.
2187 */
2188 void
2189set_argv_var(char **argv, int argc)
2190{
2191 list_T *l = list_alloc();
2192 int i;
2193
2194 if (l == NULL)
2195 getout(1);
2196 l->lv_lock = VAR_FIXED;
2197 for (i = 0; i < argc; ++i)
2198 {
2199 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2200 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002201 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002202 }
2203 set_vim_var_list(VV_ARGV, l);
2204}
2205
2206/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002207 * Reset v:register, taking the 'clipboard' setting into account.
2208 */
2209 void
2210reset_reg_var(void)
2211{
2212 int regname = 0;
2213
2214 // Adjust the register according to 'clipboard', so that when
2215 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2216#ifdef FEAT_CLIPBOARD
2217 adjust_clip_reg(&regname);
2218#endif
2219 set_reg_var(regname);
2220}
2221
2222/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002223 * Set v:register if needed.
2224 */
2225 void
2226set_reg_var(int c)
2227{
2228 char_u regname;
2229
2230 if (c == 0 || c == ' ')
2231 regname = '"';
2232 else
2233 regname = c;
2234 // Avoid free/alloc when the value is already right.
2235 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2236 set_vim_var_string(VV_REG, &regname, 1);
2237}
2238
2239/*
2240 * Get or set v:exception. If "oldval" == NULL, return the current value.
2241 * Otherwise, restore the value to "oldval" and return NULL.
2242 * Must always be called in pairs to save and restore v:exception! Does not
2243 * take care of memory allocations.
2244 */
2245 char_u *
2246v_exception(char_u *oldval)
2247{
2248 if (oldval == NULL)
2249 return vimvars[VV_EXCEPTION].vv_str;
2250
2251 vimvars[VV_EXCEPTION].vv_str = oldval;
2252 return NULL;
2253}
2254
2255/*
2256 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2257 * Otherwise, restore the value to "oldval" and return NULL.
2258 * Must always be called in pairs to save and restore v:throwpoint! Does not
2259 * take care of memory allocations.
2260 */
2261 char_u *
2262v_throwpoint(char_u *oldval)
2263{
2264 if (oldval == NULL)
2265 return vimvars[VV_THROWPOINT].vv_str;
2266
2267 vimvars[VV_THROWPOINT].vv_str = oldval;
2268 return NULL;
2269}
2270
2271/*
2272 * Set v:cmdarg.
2273 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2274 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2275 * Must always be called in pairs!
2276 */
2277 char_u *
2278set_cmdarg(exarg_T *eap, char_u *oldarg)
2279{
2280 char_u *oldval;
2281 char_u *newval;
2282 unsigned len;
2283
2284 oldval = vimvars[VV_CMDARG].vv_str;
2285 if (eap == NULL)
2286 {
2287 vim_free(oldval);
2288 vimvars[VV_CMDARG].vv_str = oldarg;
2289 return NULL;
2290 }
2291
2292 if (eap->force_bin == FORCE_BIN)
2293 len = 6;
2294 else if (eap->force_bin == FORCE_NOBIN)
2295 len = 8;
2296 else
2297 len = 0;
2298
2299 if (eap->read_edit)
2300 len += 7;
2301
2302 if (eap->force_ff != 0)
2303 len += 10; // " ++ff=unix"
2304 if (eap->force_enc != 0)
2305 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2306 if (eap->bad_char != 0)
2307 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2308
2309 newval = alloc(len + 1);
2310 if (newval == NULL)
2311 return NULL;
2312
2313 if (eap->force_bin == FORCE_BIN)
2314 sprintf((char *)newval, " ++bin");
2315 else if (eap->force_bin == FORCE_NOBIN)
2316 sprintf((char *)newval, " ++nobin");
2317 else
2318 *newval = NUL;
2319
2320 if (eap->read_edit)
2321 STRCAT(newval, " ++edit");
2322
2323 if (eap->force_ff != 0)
2324 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2325 eap->force_ff == 'u' ? "unix"
2326 : eap->force_ff == 'd' ? "dos"
2327 : "mac");
2328 if (eap->force_enc != 0)
2329 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2330 eap->cmd + eap->force_enc);
2331 if (eap->bad_char == BAD_KEEP)
2332 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2333 else if (eap->bad_char == BAD_DROP)
2334 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2335 else if (eap->bad_char != 0)
2336 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2337 vimvars[VV_CMDARG].vv_str = newval;
2338 return oldval;
2339}
2340
2341/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002342 * Get the value of internal variable "name".
2343 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2344 */
2345 int
2346get_var_tv(
2347 char_u *name,
2348 int len, // length of "name"
2349 typval_T *rettv, // NULL when only checking existence
2350 dictitem_T **dip, // non-NULL when typval's dict item is needed
2351 int verbose, // may give error message
2352 int no_autoload) // do not use script autoloading
2353{
2354 int ret = OK;
2355 typval_T *tv = NULL;
2356 dictitem_T *v;
2357 int cc;
2358
2359 // truncate the name, so that we can use strcmp()
2360 cc = name[len];
2361 name[len] = NUL;
2362
2363 // Check for user-defined variables.
2364 v = find_var(name, NULL, no_autoload);
2365 if (v != NULL)
2366 {
2367 tv = &v->di_tv;
2368 if (dip != NULL)
2369 *dip = v;
2370 }
2371
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002372 if (tv == NULL && (current_sctx.sc_version == SCRIPT_VERSION_VIM9
2373 || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002375 imported_T *import;
2376 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2377
2378 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002379
2380 // imported variable from another script
2381 if (import != NULL)
2382 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002383 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2385 + import->imp_var_vals_idx;
2386 tv = sv->sv_tv;
2387 }
2388 }
2389
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002390 if (tv == NULL)
2391 {
2392 if (rettv != NULL && verbose)
2393 semsg(_(e_undefvar), name);
2394 ret = FAIL;
2395 }
2396 else if (rettv != NULL)
2397 copy_tv(tv, rettv);
2398
2399 name[len] = cc;
2400
2401 return ret;
2402}
2403
2404/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002405 * Check if variable "name[len]" is a local variable or an argument.
2406 * If so, "*eval_lavars_used" is set to TRUE.
2407 */
2408 void
2409check_vars(char_u *name, int len)
2410{
2411 int cc;
2412 char_u *varname;
2413 hashtab_T *ht;
2414
2415 if (eval_lavars_used == NULL)
2416 return;
2417
2418 // truncate the name, so that we can use strcmp()
2419 cc = name[len];
2420 name[len] = NUL;
2421
2422 ht = find_var_ht(name, &varname);
2423 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2424 {
2425 if (find_var(name, NULL, TRUE) != NULL)
2426 *eval_lavars_used = TRUE;
2427 }
2428
2429 name[len] = cc;
2430}
2431
2432/*
2433 * Find variable "name" in the list of variables.
2434 * Return a pointer to it if found, NULL if not found.
2435 * Careful: "a:0" variables don't have a name.
2436 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2437 * hashtab_T used.
2438 */
2439 dictitem_T *
2440find_var(char_u *name, hashtab_T **htp, int no_autoload)
2441{
2442 char_u *varname;
2443 hashtab_T *ht;
2444 dictitem_T *ret = NULL;
2445
2446 ht = find_var_ht(name, &varname);
2447 if (htp != NULL)
2448 *htp = ht;
2449 if (ht == NULL)
2450 return NULL;
2451 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2452 if (ret != NULL)
2453 return ret;
2454
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002455 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002456 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2457}
2458
2459/*
2460 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002461 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002462 * Returns NULL if not found.
2463 */
2464 dictitem_T *
2465find_var_in_ht(
2466 hashtab_T *ht,
2467 int htname,
2468 char_u *varname,
2469 int no_autoload)
2470{
2471 hashitem_T *hi;
2472
2473 if (*varname == NUL)
2474 {
2475 // Must be something like "s:", otherwise "ht" would be NULL.
2476 switch (htname)
2477 {
2478 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2479 case 'g': return &globvars_var;
2480 case 'v': return &vimvars_var;
2481 case 'b': return &curbuf->b_bufvar;
2482 case 'w': return &curwin->w_winvar;
2483 case 't': return &curtab->tp_winvar;
2484 case 'l': return get_funccal_local_var();
2485 case 'a': return get_funccal_args_var();
2486 }
2487 return NULL;
2488 }
2489
2490 hi = hash_find(ht, varname);
2491 if (HASHITEM_EMPTY(hi))
2492 {
2493 // For global variables we may try auto-loading the script. If it
2494 // worked find the variable again. Don't auto-load a script if it was
2495 // loaded already, otherwise it would be loaded every time when
2496 // checking if a function name is a Funcref variable.
2497 if (ht == &globvarht && !no_autoload)
2498 {
2499 // Note: script_autoload() may make "hi" invalid. It must either
2500 // be obtained again or not used.
2501 if (!script_autoload(varname, FALSE) || aborting())
2502 return NULL;
2503 hi = hash_find(ht, varname);
2504 }
2505 if (HASHITEM_EMPTY(hi))
2506 return NULL;
2507 }
2508 return HI2DI(hi);
2509}
2510
2511/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 * Get the script-local hashtab. NULL if not in a script context.
2513 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02002514 static hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515get_script_local_ht(void)
2516{
2517 scid_T sid = current_sctx.sc_sid;
2518
2519 if (sid > 0 && sid <= script_items.ga_len)
2520 return &SCRIPT_VARS(sid);
2521 return NULL;
2522}
2523
2524/*
2525 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002526 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002528 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2530{
2531 hashtab_T *ht = get_script_local_ht();
2532 char_u buffer[30];
2533 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002534 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535 hashitem_T *hi;
2536
2537 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002538 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 if (len < sizeof(buffer) - 1)
2540 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002541 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 vim_strncpy(buffer, name, len);
2543 p = buffer;
2544 }
2545 else
2546 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002547 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002548 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002549 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 }
2551
2552 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002553 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554
2555 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002556 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2557 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558
2559 if (p != buffer)
2560 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002561 // Don't return "buffer", gcc complains.
2562 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002563}
2564
2565/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002566 * Find the hashtab used for a variable name.
2567 * Return NULL if the name is not valid.
2568 * Set "varname" to the start of name without ':'.
2569 */
2570 hashtab_T *
2571find_var_ht(char_u *name, char_u **varname)
2572{
2573 hashitem_T *hi;
2574 hashtab_T *ht;
2575
2576 if (name[0] == NUL)
2577 return NULL;
2578 if (name[1] != ':')
2579 {
2580 // The name must not start with a colon or #.
2581 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2582 return NULL;
2583 *varname = name;
2584
2585 // "version" is "v:version" in all scopes if scriptversion < 3.
2586 // Same for a few other variables marked with VV_COMPAT.
2587 if (current_sctx.sc_version < 3)
2588 {
2589 hi = hash_find(&compat_hashtab, name);
2590 if (!HASHITEM_EMPTY(hi))
2591 return &compat_hashtab;
2592 }
2593
2594 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 if (ht != NULL)
2596 return ht; // local variable
2597
2598 // in Vim9 script items at the script level are script-local
2599 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2600 {
2601 ht = get_script_local_ht();
2602 if (ht != NULL)
2603 return ht;
2604 }
2605
2606 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002607 }
2608 *varname = name + 2;
2609 if (*name == 'g') // global variable
2610 return &globvarht;
2611 // There must be no ':' or '#' in the rest of the name, unless g: is used
2612 if (vim_strchr(name + 2, ':') != NULL
2613 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2614 return NULL;
2615 if (*name == 'b') // buffer variable
2616 return &curbuf->b_vars->dv_hashtab;
2617 if (*name == 'w') // window variable
2618 return &curwin->w_vars->dv_hashtab;
2619 if (*name == 't') // tab page variable
2620 return &curtab->tp_vars->dv_hashtab;
2621 if (*name == 'v') // v: variable
2622 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002623 if (get_current_funccal() != NULL
Bram Moolenaar822ba242020-05-24 23:00:18 +02002624 && get_current_funccal()->func->uf_dfunc_idx == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002626 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002627 if (*name == 'a') // a: function argument
2628 return get_funccal_args_ht();
2629 if (*name == 'l') // l: local function variable
2630 return get_funccal_local_ht();
2631 }
2632 if (*name == 's') // script variable
2633 {
2634 ht = get_script_local_ht();
2635 if (ht != NULL)
2636 return ht;
2637 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002638 return NULL;
2639}
2640
2641/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002642 * Get the string value of a (global/local) variable.
2643 * Note: see tv_get_string() for how long the pointer remains valid.
2644 * Returns NULL when it doesn't exist.
2645 */
2646 char_u *
2647get_var_value(char_u *name)
2648{
2649 dictitem_T *v;
2650
2651 v = find_var(name, NULL, FALSE);
2652 if (v == NULL)
2653 return NULL;
2654 return tv_get_string(&v->di_tv);
2655}
2656
2657/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002658 * Allocate a new hashtab for a sourced script. It will be used while
2659 * sourcing this script and when executing functions defined in the script.
2660 */
2661 void
2662new_script_vars(scid_T id)
2663{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002664 scriptvar_T *sv;
2665
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002666 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2667 if (sv == NULL)
2668 return;
2669 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002670 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002671}
2672
2673/*
2674 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2675 * point to it.
2676 */
2677 void
2678init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2679{
2680 hash_init(&dict->dv_hashtab);
2681 dict->dv_lock = 0;
2682 dict->dv_scope = scope;
2683 dict->dv_refcount = DO_NOT_FREE_CNT;
2684 dict->dv_copyID = 0;
2685 dict_var->di_tv.vval.v_dict = dict;
2686 dict_var->di_tv.v_type = VAR_DICT;
2687 dict_var->di_tv.v_lock = VAR_FIXED;
2688 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2689 dict_var->di_key[0] = NUL;
2690}
2691
2692/*
2693 * Unreference a dictionary initialized by init_var_dict().
2694 */
2695 void
2696unref_var_dict(dict_T *dict)
2697{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002698 // Now the dict needs to be freed if no one else is using it, go back to
2699 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002700 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2701 dict_unref(dict);
2702}
2703
2704/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002705 * Clean up a list of internal variables.
2706 * Frees all allocated variables and the value they contain.
2707 * Clears hashtab "ht", does not free it.
2708 */
2709 void
2710vars_clear(hashtab_T *ht)
2711{
2712 vars_clear_ext(ht, TRUE);
2713}
2714
2715/*
2716 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2717 */
2718 void
2719vars_clear_ext(hashtab_T *ht, int free_val)
2720{
2721 int todo;
2722 hashitem_T *hi;
2723 dictitem_T *v;
2724
2725 hash_lock(ht);
2726 todo = (int)ht->ht_used;
2727 for (hi = ht->ht_array; todo > 0; ++hi)
2728 {
2729 if (!HASHITEM_EMPTY(hi))
2730 {
2731 --todo;
2732
2733 // Free the variable. Don't remove it from the hashtab,
2734 // ht_array might change then. hash_clear() takes care of it
2735 // later.
2736 v = HI2DI(hi);
2737 if (free_val)
2738 clear_tv(&v->di_tv);
2739 if (v->di_flags & DI_FLAGS_ALLOC)
2740 vim_free(v);
2741 }
2742 }
2743 hash_clear(ht);
2744 ht->ht_used = 0;
2745}
2746
2747/*
2748 * Delete a variable from hashtab "ht" at item "hi".
2749 * Clear the variable value and free the dictitem.
2750 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002751 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002752delete_var(hashtab_T *ht, hashitem_T *hi)
2753{
2754 dictitem_T *di = HI2DI(hi);
2755
2756 hash_remove(ht, hi);
2757 clear_tv(&di->di_tv);
2758 vim_free(di);
2759}
2760
2761/*
2762 * List the value of one internal variable.
2763 */
2764 static void
2765list_one_var(dictitem_T *v, char *prefix, int *first)
2766{
2767 char_u *tofree;
2768 char_u *s;
2769 char_u numbuf[NUMBUFLEN];
2770
2771 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2772 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2773 s == NULL ? (char_u *)"" : s, first);
2774 vim_free(tofree);
2775}
2776
2777 static void
2778list_one_var_a(
2779 char *prefix,
2780 char_u *name,
2781 int type,
2782 char_u *string,
2783 int *first) // when TRUE clear rest of screen and set to FALSE
2784{
2785 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2786 msg_start();
2787 msg_puts(prefix);
2788 if (name != NULL) // "a:" vars don't have a name stored
2789 msg_puts((char *)name);
2790 msg_putchar(' ');
2791 msg_advance(22);
2792 if (type == VAR_NUMBER)
2793 msg_putchar('#');
2794 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2795 msg_putchar('*');
2796 else if (type == VAR_LIST)
2797 {
2798 msg_putchar('[');
2799 if (*string == '[')
2800 ++string;
2801 }
2802 else if (type == VAR_DICT)
2803 {
2804 msg_putchar('{');
2805 if (*string == '{')
2806 ++string;
2807 }
2808 else
2809 msg_putchar(' ');
2810
2811 msg_outtrans(string);
2812
2813 if (type == VAR_FUNC || type == VAR_PARTIAL)
2814 msg_puts("()");
2815 if (*first)
2816 {
2817 msg_clr_eos();
2818 *first = FALSE;
2819 }
2820}
2821
2822/*
2823 * Set variable "name" to value in "tv".
2824 * If the variable already exists, the value is updated.
2825 * Otherwise the variable is created.
2826 */
2827 void
2828set_var(
2829 char_u *name,
2830 typval_T *tv,
2831 int copy) // make copy of value in "tv"
2832{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833 set_var_const(name, NULL, tv, copy, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002834}
2835
2836/*
2837 * Set variable "name" to value in "tv".
2838 * If the variable already exists and "is_const" is FALSE the value is updated.
2839 * Otherwise the variable is created.
2840 */
2841 void
2842set_var_const(
2843 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002844 type_T *type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002845 typval_T *tv,
2846 int copy, // make copy of value in "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847 int flags) // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002848{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002850 char_u *varname;
2851 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 int is_script_local;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002853
2854 ht = find_var_ht(name, &varname);
2855 if (ht == NULL || *varname == NUL)
2856 {
2857 semsg(_(e_illvar), name);
2858 return;
2859 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860 is_script_local = ht == get_script_local_ht();
2861
2862 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002863
2864 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865 if (di == NULL)
2866 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002867
2868 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 && var_check_func_name(name, di == NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002870 return;
2871
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002873 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002875 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876 if (flags & LET_IS_CONST)
2877 {
2878 emsg(_(e_cannot_mod));
2879 return;
2880 }
2881
2882 if (var_check_ro(di->di_flags, name, FALSE)
2883 || var_check_lock(di->di_tv.v_lock, name, FALSE))
2884 return;
2885
2886 if ((flags & LET_NO_COMMAND) == 0
2887 && is_script_local
2888 && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2889 {
2890 semsg(_("E1041: Redefining script item %s"), name);
2891 return;
2892 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002893 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 else
2895 // can only redefine once
2896 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002897
2898 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002899
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002901 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002902 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002903 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002905 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002907 if (copy || tv->v_type != VAR_STRING)
2908 {
2909 char_u *val = tv_get_string(tv);
2910
2911 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002912 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 if (di->di_tv.vval.v_string == NULL)
2914 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002915 }
2916 else
2917 {
2918 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002920 tv->vval.v_string = NULL;
2921 }
2922 return;
2923 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002925 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002927 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002929#ifdef FEAT_SEARCH_EXTRA
2930 else if (STRCMP(varname, "hlsearch") == 0)
2931 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002933 redraw_all_later(SOME_VALID);
2934 }
2935#endif
2936 return;
2937 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002939 {
2940 semsg(_("E963: setting %s to value with wrong type"), name);
2941 return;
2942 }
2943 }
2944
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002946 }
2947 else // add a new variable
2948 {
2949 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002950 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002951 {
2952 semsg(_(e_illvar), name);
2953 return;
2954 }
2955
2956 // Make sure the variable name is valid.
2957 if (!valid_varname(varname))
2958 return;
2959
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
2961 if (di == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002962 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 STRCPY(di->di_key, varname);
2964 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002965 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002967 return;
2968 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 di->di_flags = DI_FLAGS_ALLOC;
2970 if (flags & LET_IS_CONST)
2971 di->di_flags |= DI_FLAGS_LOCK;
2972
2973 if (is_script_local && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2974 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002975 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976
2977 // Store a pointer to the typval_T, so that it can be found by
2978 // index instead of using a hastab lookup.
2979 if (ga_grow(&si->sn_var_vals, 1) == OK)
2980 {
2981 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2982 + si->sn_var_vals.ga_len;
2983 sv->sv_name = di->di_key;
2984 sv->sv_tv = &di->di_tv;
2985 sv->sv_type = type == NULL ? &t_any : type;
2986 sv->sv_const = (flags & LET_IS_CONST);
2987 sv->sv_export = is_export;
2988 ++si->sn_var_vals.ga_len;
2989
2990 // let ex_export() know the export worked.
2991 is_export = FALSE;
2992 }
2993 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002994 }
2995
2996 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002998 else
2999 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 di->di_tv = *tv;
3001 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003002 init_tv(tv);
3003 }
3004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 if (flags & LET_IS_CONST)
3006 di->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003007}
3008
3009/*
3010 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3011 * Also give an error message.
3012 */
3013 int
3014var_check_ro(int flags, char_u *name, int use_gettext)
3015{
3016 if (flags & DI_FLAGS_RO)
3017 {
3018 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3019 return TRUE;
3020 }
3021 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3022 {
3023 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3024 return TRUE;
3025 }
3026 return FALSE;
3027}
3028
3029/*
3030 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3031 * Also give an error message.
3032 */
3033 int
3034var_check_fixed(int flags, char_u *name, int use_gettext)
3035{
3036 if (flags & DI_FLAGS_FIX)
3037 {
3038 semsg(_("E795: Cannot delete variable %s"),
3039 use_gettext ? (char_u *)_(name) : name);
3040 return TRUE;
3041 }
3042 return FALSE;
3043}
3044
3045/*
3046 * Check if a funcref is assigned to a valid variable name.
3047 * Return TRUE and give an error if not.
3048 */
3049 int
3050var_check_func_name(
3051 char_u *name, // points to start of variable name
3052 int new_var) // TRUE when creating the variable
3053{
3054 // Allow for w: b: s: and t:.
3055 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3056 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3057 ? name[2] : name[0]))
3058 {
3059 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3060 name);
3061 return TRUE;
3062 }
3063 // Don't allow hiding a function. When "v" is not NULL we might be
3064 // assigning another function to the same var, the type is checked
3065 // below.
3066 if (new_var && function_exists(name, FALSE))
3067 {
3068 semsg(_("E705: Variable name conflicts with existing function: %s"),
3069 name);
3070 return TRUE;
3071 }
3072 return FALSE;
3073}
3074
3075/*
3076 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
3077 * Also give an error message, using "name" or _("name") when use_gettext is
3078 * TRUE.
3079 */
3080 int
3081var_check_lock(int lock, char_u *name, int use_gettext)
3082{
3083 if (lock & VAR_LOCKED)
3084 {
3085 semsg(_("E741: Value is locked: %s"),
3086 name == NULL ? (char_u *)_("Unknown")
3087 : use_gettext ? (char_u *)_(name)
3088 : name);
3089 return TRUE;
3090 }
3091 if (lock & VAR_FIXED)
3092 {
3093 semsg(_("E742: Cannot change value of %s"),
3094 name == NULL ? (char_u *)_("Unknown")
3095 : use_gettext ? (char_u *)_(name)
3096 : name);
3097 return TRUE;
3098 }
3099 return FALSE;
3100}
3101
3102/*
3103 * Check if a variable name is valid.
3104 * Return FALSE and give an error if not.
3105 */
3106 int
3107valid_varname(char_u *varname)
3108{
3109 char_u *p;
3110
3111 for (p = varname; *p != NUL; ++p)
3112 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3113 && *p != AUTOLOAD_CHAR)
3114 {
3115 semsg(_(e_illvar), varname);
3116 return FALSE;
3117 }
3118 return TRUE;
3119}
3120
3121/*
3122 * getwinvar() and gettabwinvar()
3123 */
3124 static void
3125getwinvar(
3126 typval_T *argvars,
3127 typval_T *rettv,
3128 int off) // 1 for gettabwinvar()
3129{
3130 win_T *win;
3131 char_u *varname;
3132 dictitem_T *v;
3133 tabpage_T *tp = NULL;
3134 int done = FALSE;
3135 win_T *oldcurwin;
3136 tabpage_T *oldtabpage;
3137 int need_switch_win;
3138
3139 if (off == 1)
3140 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3141 else
3142 tp = curtab;
3143 win = find_win_by_nr(&argvars[off], tp);
3144 varname = tv_get_string_chk(&argvars[off + 1]);
3145 ++emsg_off;
3146
3147 rettv->v_type = VAR_STRING;
3148 rettv->vval.v_string = NULL;
3149
3150 if (win != NULL && varname != NULL)
3151 {
3152 // Set curwin to be our win, temporarily. Also set the tabpage,
3153 // otherwise the window is not valid. Only do this when needed,
3154 // autocommands get blocked.
3155 need_switch_win = !(tp == curtab && win == curwin);
3156 if (!need_switch_win
3157 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3158 {
3159 if (*varname == '&')
3160 {
3161 if (varname[1] == NUL)
3162 {
3163 // get all window-local options in a dict
3164 dict_T *opts = get_winbuf_options(FALSE);
3165
3166 if (opts != NULL)
3167 {
3168 rettv_dict_set(rettv, opts);
3169 done = TRUE;
3170 }
3171 }
3172 else if (get_option_tv(&varname, rettv, 1) == OK)
3173 // window-local-option
3174 done = TRUE;
3175 }
3176 else
3177 {
3178 // Look up the variable.
3179 // Let getwinvar({nr}, "") return the "w:" dictionary.
3180 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3181 varname, FALSE);
3182 if (v != NULL)
3183 {
3184 copy_tv(&v->di_tv, rettv);
3185 done = TRUE;
3186 }
3187 }
3188 }
3189
3190 if (need_switch_win)
3191 // restore previous notion of curwin
3192 restore_win(oldcurwin, oldtabpage, TRUE);
3193 }
3194
3195 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3196 // use the default return value
3197 copy_tv(&argvars[off + 2], rettv);
3198
3199 --emsg_off;
3200}
3201
3202/*
3203 * "setwinvar()" and "settabwinvar()" functions
3204 */
3205 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003206setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003207{
3208 win_T *win;
3209 win_T *save_curwin;
3210 tabpage_T *save_curtab;
3211 int need_switch_win;
3212 char_u *varname, *winvarname;
3213 typval_T *varp;
3214 char_u nbuf[NUMBUFLEN];
3215 tabpage_T *tp = NULL;
3216
3217 if (check_secure())
3218 return;
3219
3220 if (off == 1)
3221 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3222 else
3223 tp = curtab;
3224 win = find_win_by_nr(&argvars[off], tp);
3225 varname = tv_get_string_chk(&argvars[off + 1]);
3226 varp = &argvars[off + 2];
3227
3228 if (win != NULL && varname != NULL && varp != NULL)
3229 {
3230 need_switch_win = !(tp == curtab && win == curwin);
3231 if (!need_switch_win
3232 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3233 {
3234 if (*varname == '&')
3235 {
3236 long numval;
3237 char_u *strval;
3238 int error = FALSE;
3239
3240 ++varname;
3241 numval = (long)tv_get_number_chk(varp, &error);
3242 strval = tv_get_string_buf_chk(varp, nbuf);
3243 if (!error && strval != NULL)
3244 set_option_value(varname, numval, strval, OPT_LOCAL);
3245 }
3246 else
3247 {
3248 winvarname = alloc(STRLEN(varname) + 3);
3249 if (winvarname != NULL)
3250 {
3251 STRCPY(winvarname, "w:");
3252 STRCPY(winvarname + 2, varname);
3253 set_var(winvarname, varp, TRUE);
3254 vim_free(winvarname);
3255 }
3256 }
3257 }
3258 if (need_switch_win)
3259 restore_win(save_curwin, save_curtab, TRUE);
3260 }
3261}
3262
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003263/*
3264 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3265 * v:option_type, and v:option_command.
3266 */
3267 void
3268reset_v_option_vars(void)
3269{
3270 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3271 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3272 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3273 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3274 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3275 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3276}
3277
3278/*
3279 * Add an assert error to v:errors.
3280 */
3281 void
3282assert_error(garray_T *gap)
3283{
3284 struct vimvar *vp = &vimvars[VV_ERRORS];
3285
3286 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003287 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003288 set_vim_var_list(VV_ERRORS, list_alloc());
3289 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3290}
3291
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003292 int
3293var_exists(char_u *var)
3294{
3295 char_u *name;
3296 char_u *tofree;
3297 typval_T tv;
3298 int len = 0;
3299 int n = FALSE;
3300
3301 // get_name_len() takes care of expanding curly braces
3302 name = var;
3303 len = get_name_len(&var, &tofree, TRUE, FALSE);
3304 if (len > 0)
3305 {
3306 if (tofree != NULL)
3307 name = tofree;
3308 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
3309 if (n)
3310 {
3311 // handle d.key, l[idx], f(expr)
Bram Moolenaar32e35112020-05-14 22:41:15 +02003312 n = (handle_subscript(&var, &tv, EVAL_EVALUATE,
3313 FALSE, name, &name) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003314 if (n)
3315 clear_tv(&tv);
3316 }
3317 }
3318 if (*var != NUL)
3319 n = FALSE;
3320
3321 vim_free(tofree);
3322 return n;
3323}
3324
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003325static lval_T *redir_lval = NULL;
3326#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3327static garray_T redir_ga; // only valid when redir_lval is not NULL
3328static char_u *redir_endp = NULL;
3329static char_u *redir_varname = NULL;
3330
3331/*
3332 * Start recording command output to a variable
3333 * When "append" is TRUE append to an existing variable.
3334 * Returns OK if successfully completed the setup. FAIL otherwise.
3335 */
3336 int
3337var_redir_start(char_u *name, int append)
3338{
3339 int save_emsg;
3340 int err;
3341 typval_T tv;
3342
3343 // Catch a bad name early.
3344 if (!eval_isnamec1(*name))
3345 {
3346 emsg(_(e_invarg));
3347 return FAIL;
3348 }
3349
3350 // Make a copy of the name, it is used in redir_lval until redir ends.
3351 redir_varname = vim_strsave(name);
3352 if (redir_varname == NULL)
3353 return FAIL;
3354
3355 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3356 if (redir_lval == NULL)
3357 {
3358 var_redir_stop();
3359 return FAIL;
3360 }
3361
3362 // The output is stored in growarray "redir_ga" until redirection ends.
3363 ga_init2(&redir_ga, (int)sizeof(char), 500);
3364
3365 // Parse the variable name (can be a dict or list entry).
3366 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3367 FNE_CHECK_START);
3368 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3369 {
3370 clear_lval(redir_lval);
3371 if (redir_endp != NULL && *redir_endp != NUL)
3372 // Trailing characters are present after the variable name
3373 emsg(_(e_trailing));
3374 else
3375 emsg(_(e_invarg));
3376 redir_endp = NULL; // don't store a value, only cleanup
3377 var_redir_stop();
3378 return FAIL;
3379 }
3380
3381 // check if we can write to the variable: set it to or append an empty
3382 // string
3383 save_emsg = did_emsg;
3384 did_emsg = FALSE;
3385 tv.v_type = VAR_STRING;
3386 tv.vval.v_string = (char_u *)"";
3387 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003389 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003391 clear_lval(redir_lval);
3392 err = did_emsg;
3393 did_emsg |= save_emsg;
3394 if (err)
3395 {
3396 redir_endp = NULL; // don't store a value, only cleanup
3397 var_redir_stop();
3398 return FAIL;
3399 }
3400
3401 return OK;
3402}
3403
3404/*
3405 * Append "value[value_len]" to the variable set by var_redir_start().
3406 * The actual appending is postponed until redirection ends, because the value
3407 * appended may in fact be the string we write to, changing it may cause freed
3408 * memory to be used:
3409 * :redir => foo
3410 * :let foo
3411 * :redir END
3412 */
3413 void
3414var_redir_str(char_u *value, int value_len)
3415{
3416 int len;
3417
3418 if (redir_lval == NULL)
3419 return;
3420
3421 if (value_len == -1)
3422 len = (int)STRLEN(value); // Append the entire string
3423 else
3424 len = value_len; // Append only "value_len" characters
3425
3426 if (ga_grow(&redir_ga, len) == OK)
3427 {
3428 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3429 redir_ga.ga_len += len;
3430 }
3431 else
3432 var_redir_stop();
3433}
3434
3435/*
3436 * Stop redirecting command output to a variable.
3437 * Frees the allocated memory.
3438 */
3439 void
3440var_redir_stop(void)
3441{
3442 typval_T tv;
3443
3444 if (EVALCMD_BUSY)
3445 {
3446 redir_lval = NULL;
3447 return;
3448 }
3449
3450 if (redir_lval != NULL)
3451 {
3452 // If there was no error: assign the text to the variable.
3453 if (redir_endp != NULL)
3454 {
3455 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3456 tv.v_type = VAR_STRING;
3457 tv.vval.v_string = redir_ga.ga_data;
3458 // Call get_lval() again, if it's inside a Dict or List it may
3459 // have changed.
3460 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3461 FALSE, FALSE, 0, FNE_CHECK_START);
3462 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003464 (char_u *)".");
3465 clear_lval(redir_lval);
3466 }
3467
3468 // free the collected output
3469 VIM_CLEAR(redir_ga.ga_data);
3470
3471 VIM_CLEAR(redir_lval);
3472 }
3473 VIM_CLEAR(redir_varname);
3474}
3475
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003476/*
3477 * "gettabvar()" function
3478 */
3479 void
3480f_gettabvar(typval_T *argvars, typval_T *rettv)
3481{
3482 win_T *oldcurwin;
3483 tabpage_T *tp, *oldtabpage;
3484 dictitem_T *v;
3485 char_u *varname;
3486 int done = FALSE;
3487
3488 rettv->v_type = VAR_STRING;
3489 rettv->vval.v_string = NULL;
3490
3491 varname = tv_get_string_chk(&argvars[1]);
3492 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3493 if (tp != NULL && varname != NULL)
3494 {
3495 // Set tp to be our tabpage, temporarily. Also set the window to the
3496 // first window in the tabpage, otherwise the window is not valid.
3497 if (switch_win(&oldcurwin, &oldtabpage,
3498 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3499 : tp->tp_firstwin, tp, TRUE) == OK)
3500 {
3501 // look up the variable
3502 // Let gettabvar({nr}, "") return the "t:" dictionary.
3503 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3504 if (v != NULL)
3505 {
3506 copy_tv(&v->di_tv, rettv);
3507 done = TRUE;
3508 }
3509 }
3510
3511 // restore previous notion of curwin
3512 restore_win(oldcurwin, oldtabpage, TRUE);
3513 }
3514
3515 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3516 copy_tv(&argvars[2], rettv);
3517}
3518
3519/*
3520 * "gettabwinvar()" function
3521 */
3522 void
3523f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3524{
3525 getwinvar(argvars, rettv, 1);
3526}
3527
3528/*
3529 * "getwinvar()" function
3530 */
3531 void
3532f_getwinvar(typval_T *argvars, typval_T *rettv)
3533{
3534 getwinvar(argvars, rettv, 0);
3535}
3536
3537/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003538 * "getbufvar()" function
3539 */
3540 void
3541f_getbufvar(typval_T *argvars, typval_T *rettv)
3542{
3543 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003544 char_u *varname;
3545 dictitem_T *v;
3546 int done = FALSE;
3547
3548 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3549 varname = tv_get_string_chk(&argvars[1]);
3550 ++emsg_off;
3551 buf = tv_get_buf(&argvars[0], FALSE);
3552
3553 rettv->v_type = VAR_STRING;
3554 rettv->vval.v_string = NULL;
3555
3556 if (buf != NULL && varname != NULL)
3557 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003558 if (*varname == '&')
3559 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003560 buf_T *save_curbuf = curbuf;
3561
3562 // set curbuf to be our buf, temporarily
3563 curbuf = buf;
3564
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003565 if (varname[1] == NUL)
3566 {
3567 // get all buffer-local options in a dict
3568 dict_T *opts = get_winbuf_options(TRUE);
3569
3570 if (opts != NULL)
3571 {
3572 rettv_dict_set(rettv, opts);
3573 done = TRUE;
3574 }
3575 }
3576 else if (get_option_tv(&varname, rettv, TRUE) == OK)
3577 // buffer-local-option
3578 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003579
3580 // restore previous notion of curbuf
3581 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003582 }
3583 else
3584 {
3585 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003586 if (*varname == NUL)
3587 // Let getbufvar({nr}, "") return the "b:" dictionary.
3588 v = &buf->b_bufvar;
3589 else
3590 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3591 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003592 if (v != NULL)
3593 {
3594 copy_tv(&v->di_tv, rettv);
3595 done = TRUE;
3596 }
3597 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003598 }
3599
3600 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3601 // use the default value
3602 copy_tv(&argvars[2], rettv);
3603
3604 --emsg_off;
3605}
3606
3607/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003608 * "settabvar()" function
3609 */
3610 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003611f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003612{
3613 tabpage_T *save_curtab;
3614 tabpage_T *tp;
3615 char_u *varname, *tabvarname;
3616 typval_T *varp;
3617
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003618 if (check_secure())
3619 return;
3620
3621 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3622 varname = tv_get_string_chk(&argvars[1]);
3623 varp = &argvars[2];
3624
3625 if (varname != NULL && varp != NULL && tp != NULL)
3626 {
3627 save_curtab = curtab;
3628 goto_tabpage_tp(tp, FALSE, FALSE);
3629
3630 tabvarname = alloc(STRLEN(varname) + 3);
3631 if (tabvarname != NULL)
3632 {
3633 STRCPY(tabvarname, "t:");
3634 STRCPY(tabvarname + 2, varname);
3635 set_var(tabvarname, varp, TRUE);
3636 vim_free(tabvarname);
3637 }
3638
3639 // Restore current tabpage
3640 if (valid_tabpage(save_curtab))
3641 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3642 }
3643}
3644
3645/*
3646 * "settabwinvar()" function
3647 */
3648 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003649f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003650{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003651 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003652}
3653
3654/*
3655 * "setwinvar()" function
3656 */
3657 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003658f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003659{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003660 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003661}
3662
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003663/*
3664 * "setbufvar()" function
3665 */
3666 void
3667f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3668{
3669 buf_T *buf;
3670 char_u *varname, *bufvarname;
3671 typval_T *varp;
3672 char_u nbuf[NUMBUFLEN];
3673
3674 if (check_secure())
3675 return;
3676 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3677 varname = tv_get_string_chk(&argvars[1]);
3678 buf = tv_get_buf(&argvars[0], FALSE);
3679 varp = &argvars[2];
3680
3681 if (buf != NULL && varname != NULL && varp != NULL)
3682 {
3683 if (*varname == '&')
3684 {
3685 long numval;
3686 char_u *strval;
3687 int error = FALSE;
3688 aco_save_T aco;
3689
3690 // set curbuf to be our buf, temporarily
3691 aucmd_prepbuf(&aco, buf);
3692
3693 ++varname;
3694 numval = (long)tv_get_number_chk(varp, &error);
3695 strval = tv_get_string_buf_chk(varp, nbuf);
3696 if (!error && strval != NULL)
3697 set_option_value(varname, numval, strval, OPT_LOCAL);
3698
3699 // reset notion of buffer
3700 aucmd_restbuf(&aco);
3701 }
3702 else
3703 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003704 bufvarname = alloc(STRLEN(varname) + 3);
3705 if (bufvarname != NULL)
3706 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003707 buf_T *save_curbuf = curbuf;
3708
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003709 curbuf = buf;
3710 STRCPY(bufvarname, "b:");
3711 STRCPY(bufvarname + 2, varname);
3712 set_var(bufvarname, varp, TRUE);
3713 vim_free(bufvarname);
3714 curbuf = save_curbuf;
3715 }
3716 }
3717 }
3718}
3719
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003720/*
3721 * Get a callback from "arg". It can be a Funcref or a function name.
3722 * When "arg" is zero return an empty string.
3723 * "cb_name" is not allocated.
3724 * "cb_name" is set to NULL for an invalid argument.
3725 */
3726 callback_T
3727get_callback(typval_T *arg)
3728{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003729 callback_T res;
3730 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003731
3732 res.cb_free_name = FALSE;
3733 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3734 {
3735 res.cb_partial = arg->vval.v_partial;
3736 ++res.cb_partial->pt_refcount;
3737 res.cb_name = partial_name(res.cb_partial);
3738 }
3739 else
3740 {
3741 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003742 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3743 && isdigit(*arg->vval.v_string))
3744 r = FAIL;
3745 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003746 {
3747 // Note that we don't make a copy of the string.
3748 res.cb_name = arg->vval.v_string;
3749 func_ref(res.cb_name);
3750 }
3751 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003752 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003753 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003754 r = FAIL;
3755
3756 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003757 {
3758 emsg(_("E921: Invalid callback argument"));
3759 res.cb_name = NULL;
3760 }
3761 }
3762 return res;
3763}
3764
3765/*
3766 * Copy a callback into a typval_T.
3767 */
3768 void
3769put_callback(callback_T *cb, typval_T *tv)
3770{
3771 if (cb->cb_partial != NULL)
3772 {
3773 tv->v_type = VAR_PARTIAL;
3774 tv->vval.v_partial = cb->cb_partial;
3775 ++tv->vval.v_partial->pt_refcount;
3776 }
3777 else
3778 {
3779 tv->v_type = VAR_FUNC;
3780 tv->vval.v_string = vim_strsave(cb->cb_name);
3781 func_ref(cb->cb_name);
3782 }
3783}
3784
3785/*
3786 * Make a copy of "src" into "dest", allocating the function name if needed,
3787 * without incrementing the refcount.
3788 */
3789 void
3790set_callback(callback_T *dest, callback_T *src)
3791{
3792 if (src->cb_partial == NULL)
3793 {
3794 // just a function name, make a copy
3795 dest->cb_name = vim_strsave(src->cb_name);
3796 dest->cb_free_name = TRUE;
3797 }
3798 else
3799 {
3800 // cb_name is a pointer into cb_partial
3801 dest->cb_name = src->cb_name;
3802 dest->cb_free_name = FALSE;
3803 }
3804 dest->cb_partial = src->cb_partial;
3805}
3806
3807/*
3808 * Unref/free "callback" returned by get_callback() or set_callback().
3809 */
3810 void
3811free_callback(callback_T *callback)
3812{
3813 if (callback->cb_partial != NULL)
3814 {
3815 partial_unref(callback->cb_partial);
3816 callback->cb_partial = NULL;
3817 }
3818 else if (callback->cb_name != NULL)
3819 func_unref(callback->cb_name);
3820 if (callback->cb_free_name)
3821 {
3822 vim_free(callback->cb_name);
3823 callback->cb_free_name = FALSE;
3824 }
3825 callback->cb_name = NULL;
3826}
3827
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003828#endif // FEAT_EVAL