blob: 6ab33184b437a42b832a6bfbedba514d9cfd3836 [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
48 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
49} vimvars[VV_LEN] =
50{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020051 // The order here must match the VV_ defines in vim.h!
52 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaare5cdf152019-08-29 22:09:46 +020053 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
54 {VV_NAME("count1", VAR_NUMBER), VV_RO},
55 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
56 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
57 {VV_NAME("warningmsg", VAR_STRING), 0},
58 {VV_NAME("statusmsg", VAR_STRING), 0},
59 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
60 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
61 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
62 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
63 {VV_NAME("termresponse", VAR_STRING), VV_RO},
64 {VV_NAME("fname", VAR_STRING), VV_RO},
65 {VV_NAME("lang", VAR_STRING), VV_RO},
66 {VV_NAME("lc_time", VAR_STRING), VV_RO},
67 {VV_NAME("ctype", VAR_STRING), VV_RO},
68 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
69 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
70 {VV_NAME("fname_in", VAR_STRING), VV_RO},
71 {VV_NAME("fname_out", VAR_STRING), VV_RO},
72 {VV_NAME("fname_new", VAR_STRING), VV_RO},
73 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
74 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
75 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
76 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
77 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
78 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
79 {VV_NAME("progname", VAR_STRING), VV_RO},
80 {VV_NAME("servername", VAR_STRING), VV_RO},
81 {VV_NAME("dying", VAR_NUMBER), VV_RO},
82 {VV_NAME("exception", VAR_STRING), VV_RO},
83 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
84 {VV_NAME("register", VAR_STRING), VV_RO},
85 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
86 {VV_NAME("insertmode", VAR_STRING), VV_RO},
87 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
88 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
89 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
90 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
91 {VV_NAME("fcs_choice", VAR_STRING), 0},
92 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
93 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
94 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
95 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
96 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
97 {VV_NAME("beval_text", VAR_STRING), VV_RO},
98 {VV_NAME("scrollstart", VAR_STRING), 0},
99 {VV_NAME("swapname", VAR_STRING), VV_RO},
100 {VV_NAME("swapchoice", VAR_STRING), 0},
101 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
102 {VV_NAME("char", VAR_STRING), 0},
103 {VV_NAME("mouse_win", VAR_NUMBER), 0},
104 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
105 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
106 {VV_NAME("mouse_col", VAR_NUMBER), 0},
107 {VV_NAME("operator", VAR_STRING), VV_RO},
108 {VV_NAME("searchforward", VAR_NUMBER), 0},
109 {VV_NAME("hlsearch", VAR_NUMBER), 0},
110 {VV_NAME("oldfiles", VAR_LIST), 0},
111 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
112 {VV_NAME("progpath", VAR_STRING), VV_RO},
113 {VV_NAME("completed_item", VAR_DICT), VV_RO},
114 {VV_NAME("option_new", VAR_STRING), VV_RO},
115 {VV_NAME("option_old", VAR_STRING), VV_RO},
116 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
117 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
118 {VV_NAME("option_command", VAR_STRING), VV_RO},
119 {VV_NAME("option_type", VAR_STRING), VV_RO},
120 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100121 {VV_NAME("false", VAR_BOOL), VV_RO},
122 {VV_NAME("true", VAR_BOOL), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200123 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100124 {VV_NAME("null", VAR_SPECIAL), VV_RO},
125 {VV_NAME("numbersize", VAR_NUMBER), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200126 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
127 {VV_NAME("testing", VAR_NUMBER), 0},
128 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
129 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
130 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
131 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
132 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
133 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
134 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
135 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
136 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
137 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
138 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
139 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
140 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
141 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
142 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
143 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
144 {VV_NAME("event", VAR_DICT), VV_RO},
145 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
146 {VV_NAME("echospace", VAR_NUMBER), VV_RO},
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100147 {VV_NAME("argv", VAR_LIST), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200148};
149
150// shorthand
151#define vv_type vv_di.di_tv.v_type
152#define vv_nr vv_di.di_tv.vval.v_number
153#define vv_float vv_di.di_tv.vval.v_float
154#define vv_str vv_di.di_tv.vval.v_string
155#define vv_list vv_di.di_tv.vval.v_list
156#define vv_dict vv_di.di_tv.vval.v_dict
157#define vv_blob vv_di.di_tv.vval.v_blob
158#define vv_tv vv_di.di_tv
159
160static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200161static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200162#define vimvarht vimvardict.dv_hashtab
163
164// for VIM_VERSION_ defines
165#include "version.h"
166
Bram Moolenaar822ba242020-05-24 23:00:18 +0200167static void ex_let_const(exarg_T *eap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168static char_u *skip_var_one(char_u *arg, int include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200169static void list_glob_vars(int *first);
170static void list_buf_vars(int *first);
171static void list_win_vars(int *first);
172static void list_tab_vars(int *first);
173static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100174static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200175static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
176static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200177static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200178static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200179static void list_one_var(dictitem_T *v, char *prefix, int *first);
180static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
181
182/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200183 * Initialize global and vim special variables
184 */
185 void
186evalvars_init(void)
187{
188 int i;
189 struct vimvar *p;
190
191 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
192 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
193 vimvardict.dv_lock = VAR_FIXED;
194 hash_init(&compat_hashtab);
195
196 for (i = 0; i < VV_LEN; ++i)
197 {
198 p = &vimvars[i];
199 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
200 {
201 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
202 getout(1);
203 }
204 STRCPY(p->vv_di.di_key, p->vv_name);
205 if (p->vv_flags & VV_RO)
206 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
207 else if (p->vv_flags & VV_RO_SBX)
208 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
209 else
210 p->vv_di.di_flags = DI_FLAGS_FIX;
211
212 // add to v: scope dict, unless the value is not always available
213 if (p->vv_type != VAR_UNKNOWN)
214 hash_add(&vimvarht, p->vv_di.di_key);
215 if (p->vv_flags & VV_COMPAT)
216 // add to compat scope dict
217 hash_add(&compat_hashtab, p->vv_di.di_key);
218 }
219 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
220 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
221
222 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
223 set_vim_var_nr(VV_HLSEARCH, 1L);
224 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
225 set_vim_var_list(VV_ERRORS, list_alloc());
226 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
227
228 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
229 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
230 set_vim_var_nr(VV_NONE, VVAL_NONE);
231 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100232 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200233
234 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
235 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
236 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
237 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
238 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
239 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
240 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
241 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
242 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
243 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
244 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
245
246 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
247
248 set_reg_var(0); // default for v:register is not 0 but '"'
249}
250
251#if defined(EXITFREE) || defined(PROTO)
252/*
253 * Free all vim variables information on exit
254 */
255 void
256evalvars_clear(void)
257{
258 int i;
259 struct vimvar *p;
260
261 for (i = 0; i < VV_LEN; ++i)
262 {
263 p = &vimvars[i];
264 if (p->vv_di.di_tv.v_type == VAR_STRING)
265 VIM_CLEAR(p->vv_str);
266 else if (p->vv_di.di_tv.v_type == VAR_LIST)
267 {
268 list_unref(p->vv_list);
269 p->vv_list = NULL;
270 }
271 }
272 hash_clear(&vimvarht);
273 hash_init(&vimvarht); // garbage_collect() will access it
274 hash_clear(&compat_hashtab);
275
276 // global variables
277 vars_clear(&globvarht);
278
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100279 // Script-local variables. Clear all the variables here.
280 // The scriptvar_T is cleared later in free_scriptnames(), because a
281 // variable in one script might hold a reference to the whole scope of
282 // another script.
283 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200284 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200285}
286#endif
287
288 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200289garbage_collect_globvars(int copyID)
290{
291 return set_ref_in_ht(&globvarht, copyID, NULL);
292}
293
294 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200295garbage_collect_vimvars(int copyID)
296{
297 return set_ref_in_ht(&vimvarht, copyID, NULL);
298}
299
300 int
301garbage_collect_scriptvars(int copyID)
302{
303 int i;
304 int abort = FALSE;
305
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100306 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200307 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
308
309 return abort;
310}
311
312/*
313 * Set an internal variable to a string value. Creates the variable if it does
314 * not already exist.
315 */
316 void
317set_internal_string_var(char_u *name, char_u *value)
318{
319 char_u *val;
320 typval_T *tvp;
321
322 val = vim_strsave(value);
323 if (val != NULL)
324 {
325 tvp = alloc_string_tv(val);
326 if (tvp != NULL)
327 {
328 set_var(name, tvp, FALSE);
329 free_tv(tvp);
330 }
331 }
332}
333
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200334 int
335eval_charconvert(
336 char_u *enc_from,
337 char_u *enc_to,
338 char_u *fname_from,
339 char_u *fname_to)
340{
341 int err = FALSE;
342
343 set_vim_var_string(VV_CC_FROM, enc_from, -1);
344 set_vim_var_string(VV_CC_TO, enc_to, -1);
345 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
346 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
347 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
348 err = TRUE;
349 set_vim_var_string(VV_CC_FROM, NULL, -1);
350 set_vim_var_string(VV_CC_TO, NULL, -1);
351 set_vim_var_string(VV_FNAME_IN, NULL, -1);
352 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
353
354 if (err)
355 return FAIL;
356 return OK;
357}
358
359# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
360 int
361eval_printexpr(char_u *fname, char_u *args)
362{
363 int err = FALSE;
364
365 set_vim_var_string(VV_FNAME_IN, fname, -1);
366 set_vim_var_string(VV_CMDARG, args, -1);
367 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
368 err = TRUE;
369 set_vim_var_string(VV_FNAME_IN, NULL, -1);
370 set_vim_var_string(VV_CMDARG, NULL, -1);
371
372 if (err)
373 {
374 mch_remove(fname);
375 return FAIL;
376 }
377 return OK;
378}
379# endif
380
381# if defined(FEAT_DIFF) || defined(PROTO)
382 void
383eval_diff(
384 char_u *origfile,
385 char_u *newfile,
386 char_u *outfile)
387{
388 int err = FALSE;
389
390 set_vim_var_string(VV_FNAME_IN, origfile, -1);
391 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
392 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
393 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
394 set_vim_var_string(VV_FNAME_IN, NULL, -1);
395 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
396 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
397}
398
399 void
400eval_patch(
401 char_u *origfile,
402 char_u *difffile,
403 char_u *outfile)
404{
405 int err;
406
407 set_vim_var_string(VV_FNAME_IN, origfile, -1);
408 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
409 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
410 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
411 set_vim_var_string(VV_FNAME_IN, NULL, -1);
412 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
413 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
414}
415# endif
416
417#if defined(FEAT_SPELL) || defined(PROTO)
418/*
419 * Evaluate an expression to a list with suggestions.
420 * For the "expr:" part of 'spellsuggest'.
421 * Returns NULL when there is an error.
422 */
423 list_T *
424eval_spell_expr(char_u *badword, char_u *expr)
425{
426 typval_T save_val;
427 typval_T rettv;
428 list_T *list = NULL;
429 char_u *p = skipwhite(expr);
430
431 // Set "v:val" to the bad word.
432 prepare_vimvar(VV_VAL, &save_val);
433 set_vim_var_string(VV_VAL, badword, -1);
434 if (p_verbose == 0)
435 ++emsg_off;
436
Bram Moolenaar32e35112020-05-14 22:41:15 +0200437 if (eval1(&p, &rettv, EVAL_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200438 {
439 if (rettv.v_type != VAR_LIST)
440 clear_tv(&rettv);
441 else
442 list = rettv.vval.v_list;
443 }
444
445 if (p_verbose == 0)
446 --emsg_off;
447 clear_tv(get_vim_var_tv(VV_VAL));
448 restore_vimvar(VV_VAL, &save_val);
449
450 return list;
451}
452
453/*
454 * "list" is supposed to contain two items: a word and a number. Return the
455 * word in "pp" and the number as the return value.
456 * Return -1 if anything isn't right.
457 * Used to get the good word and score from the eval_spell_expr() result.
458 */
459 int
460get_spellword(list_T *list, char_u **pp)
461{
462 listitem_T *li;
463
464 li = list->lv_first;
465 if (li == NULL)
466 return -1;
467 *pp = tv_get_string(&li->li_tv);
468
469 li = li->li_next;
470 if (li == NULL)
471 return -1;
472 return (int)tv_get_number(&li->li_tv);
473}
474#endif
475
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200476/*
477 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200478 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200479 * When not used yet add the variable to the v: hashtable.
480 */
481 void
482prepare_vimvar(int idx, typval_T *save_tv)
483{
484 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200485 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200486 if (vimvars[idx].vv_type == VAR_UNKNOWN)
487 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
488}
489
490/*
491 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200492 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200493 * When no longer defined, remove the variable from the v: hashtable.
494 */
495 void
496restore_vimvar(int idx, typval_T *save_tv)
497{
498 hashitem_T *hi;
499
500 vimvars[idx].vv_tv = *save_tv;
501 if (vimvars[idx].vv_type == VAR_UNKNOWN)
502 {
503 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
504 if (HASHITEM_EMPTY(hi))
505 internal_error("restore_vimvar()");
506 else
507 hash_remove(&vimvarht, hi);
508 }
509}
510
511/*
512 * List Vim variables.
513 */
514 static void
515list_vim_vars(int *first)
516{
517 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
518}
519
520/*
521 * List script-local variables, if there is a script.
522 */
523 static void
524list_script_vars(int *first)
525{
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100526 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= script_items.ga_len)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200527 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
528 "s:", FALSE, first);
529}
530
531/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200532 * Get a list of lines from a HERE document. The here document is a list of
533 * lines surrounded by a marker.
534 * cmd << {marker}
535 * {line1}
536 * {line2}
537 * ....
538 * {marker}
539 *
540 * The {marker} is a string. If the optional 'trim' word is supplied before the
541 * marker, then the leading indentation before the lines (matching the
542 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200543 *
544 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
545 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
546 * missing, then '.' is accepted as a marker.
547 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200548 * Returns a List with {lines} or NULL.
549 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100550 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200551heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200552{
553 char_u *theline;
554 char_u *marker;
555 list_T *l;
556 char_u *p;
557 int marker_indent_len = 0;
558 int text_indent_len = 0;
559 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200560 char_u dot[] = ".";
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200561
562 if (eap->getline == NULL)
563 {
564 emsg(_("E991: cannot use =<< here"));
565 return NULL;
566 }
567
568 // Check for the optional 'trim' word before the marker
569 cmd = skipwhite(cmd);
570 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
571 {
572 cmd = skipwhite(cmd + 4);
573
574 // Trim the indentation from all the lines in the here document.
575 // The amount of indentation trimmed is the same as the indentation of
576 // the first line after the :let command line. To find the end marker
577 // the indent of the :let command line is trimmed.
578 p = *eap->cmdlinep;
579 while (VIM_ISWHITE(*p))
580 {
581 p++;
582 marker_indent_len++;
583 }
584 text_indent_len = -1;
585 }
586
587 // The marker is the next word.
588 if (*cmd != NUL && *cmd != '"')
589 {
590 marker = skipwhite(cmd);
591 p = skiptowhite(marker);
592 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
593 {
594 emsg(_(e_trailing));
595 return NULL;
596 }
597 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200598 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200599 {
600 emsg(_("E221: Marker cannot start with lower case letter"));
601 return NULL;
602 }
603 }
604 else
605 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200606 // When getting lines for an embedded script, if the marker is missing,
607 // accept '.' as the marker.
608 if (script_get)
609 marker = dot;
610 else
611 {
612 emsg(_("E172: Missing marker"));
613 return NULL;
614 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200615 }
616
617 l = list_alloc();
618 if (l == NULL)
619 return NULL;
620
621 for (;;)
622 {
623 int mi = 0;
624 int ti = 0;
625
626 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
627 if (theline == NULL)
628 {
629 semsg(_("E990: Missing end marker '%s'"), marker);
630 break;
631 }
632
633 // with "trim": skip the indent matching the :let line to find the
634 // marker
635 if (marker_indent_len > 0
636 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
637 mi = marker_indent_len;
638 if (STRCMP(marker, theline + mi) == 0)
639 {
640 vim_free(theline);
641 break;
642 }
643
644 if (text_indent_len == -1 && *theline != NUL)
645 {
646 // set the text indent from the first line.
647 p = theline;
648 text_indent_len = 0;
649 while (VIM_ISWHITE(*p))
650 {
651 p++;
652 text_indent_len++;
653 }
654 text_indent = vim_strnsave(theline, text_indent_len);
655 }
656 // with "trim": skip the indent matching the first line
657 if (text_indent != NULL)
658 for (ti = 0; ti < text_indent_len; ++ti)
659 if (theline[ti] != text_indent[ti])
660 break;
661
662 if (list_append_string(l, theline + ti, -1) == FAIL)
663 break;
664 vim_free(theline);
665 }
666 vim_free(text_indent);
667
668 return l;
669}
670
671/*
672 * ":let" list all variable values
673 * ":let var1 var2" list variable values
674 * ":let var = expr" assignment command.
675 * ":let var += expr" assignment command.
676 * ":let var -= expr" assignment command.
677 * ":let var *= expr" assignment command.
678 * ":let var /= expr" assignment command.
679 * ":let var %= expr" assignment command.
680 * ":let var .= expr" assignment command.
681 * ":let var ..= expr" assignment command.
682 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100684 * ":let var: string" Vim9 declaration
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200685 */
686 void
687ex_let(exarg_T *eap)
688{
Bram Moolenaar822ba242020-05-24 23:00:18 +0200689 ex_let_const(eap);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200690}
691
692/*
693 * ":const" list all variable values
694 * ":const var1 var2" list variable values
695 * ":const var = expr" assignment command.
696 * ":const [var1, var2] = expr" unpack list.
697 */
698 void
699ex_const(exarg_T *eap)
700{
Bram Moolenaar822ba242020-05-24 23:00:18 +0200701 ex_let_const(eap);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200702}
703
Bram Moolenaar822ba242020-05-24 23:00:18 +0200704 static void
705ex_let_const(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200706{
707 char_u *arg = eap->arg;
708 char_u *expr = NULL;
709 typval_T rettv;
710 int i;
711 int var_count = 0;
712 int semicolon = 0;
713 char_u op[2];
714 char_u *argend;
715 int first = TRUE;
716 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200717 int has_assign;
Bram Moolenaar09689a02020-05-09 22:50:08 +0200718 int flags = eap->cmdidx == CMD_const ? LET_IS_CONST : 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200719
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720 // detect Vim9 assignment without ":let" or ":const"
721 if (eap->arg == eap->cmd)
722 flags |= LET_NO_COMMAND;
723
724 argend = skip_var_list(arg, TRUE, &var_count, &semicolon);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200725 if (argend == NULL)
726 return;
727 if (argend > arg && argend[-1] == '.') // for var.='str'
728 --argend;
729 expr = skipwhite(argend);
730 concat = expr[0] == '.'
731 && ((expr[1] == '=' && current_sctx.sc_version < 2)
732 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200733 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
734 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200735 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200736 {
737 // ":let" without "=": list variables
738 if (*arg == '[')
739 emsg(_(e_invarg));
740 else if (expr[0] == '.')
741 emsg(_("E985: .= is not supported with script version 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200742 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200743 // ":let var1 var2"
744 arg = list_arg_vars(eap, arg, &first);
745 else if (!eap->skip)
746 {
747 // ":let"
748 list_glob_vars(&first);
749 list_buf_vars(&first);
750 list_win_vars(&first);
751 list_tab_vars(&first);
752 list_script_vars(&first);
753 list_func_vars(&first);
754 list_vim_vars(&first);
755 }
756 eap->nextcmd = check_nextcmd(arg);
757 }
758 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
759 {
760 list_T *l;
761
762 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200763 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200764 if (l != NULL)
765 {
766 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200767 if (!eap->skip)
768 {
769 op[0] = '=';
770 op[1] = NUL;
771 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200773 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200774 clear_tv(&rettv);
775 }
776 }
777 else
778 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200779 int eval_flags;
780 int save_called_emsg = called_emsg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200781
Bram Moolenaar32e35112020-05-14 22:41:15 +0200782 rettv.v_type = VAR_UNKNOWN;
783 i = FAIL;
784 if (has_assign || concat)
785 {
786 op[0] = '=';
787 op[1] = NUL;
788 if (*expr != '=')
789 {
790 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
791 {
792 op[0] = *expr; // +=, -=, *=, /=, %= or .=
793 if (expr[0] == '.' && expr[1] == '.') // ..=
794 ++expr;
795 }
796 expr = skipwhite(expr + 2);
797 }
798 else
799 expr = skipwhite(expr + 1);
800
801 if (eap->skip)
802 ++emsg_skip;
803 eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200804 i = eval0(expr, &rettv, &eap->nextcmd, eval_flags);
805 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200806 if (eap->skip)
807 {
808 if (i != FAIL)
809 clear_tv(&rettv);
810 --emsg_skip;
811 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200812 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200813 {
814 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100815 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200816 clear_tv(&rettv);
817 }
818 }
819}
820
821/*
822 * Assign the typevalue "tv" to the variable or variables at "arg_start".
823 * Handles both "var" with any type and "[var, var; var]" with a list type.
824 * When "op" is not NULL it points to a string with characters that
825 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
826 * or concatenate.
827 * Returns OK or FAIL;
828 */
829 int
830ex_let_vars(
831 char_u *arg_start,
832 typval_T *tv,
833 int copy, // copy values from "tv", don't move
834 int semicolon, // from skip_var_list()
835 int var_count, // from skip_var_list()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100836 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200837 char_u *op)
838{
839 char_u *arg = arg_start;
840 list_T *l;
841 int i;
842 listitem_T *item;
843 typval_T ltv;
844
845 if (*arg != '[')
846 {
847 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100848 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200849 return FAIL;
850 return OK;
851 }
852
853 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
854 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
855 {
856 emsg(_(e_listreq));
857 return FAIL;
858 }
859
860 i = list_len(l);
861 if (semicolon == 0 && var_count < i)
862 {
863 emsg(_("E687: Less targets than List items"));
864 return FAIL;
865 }
866 if (var_count - semicolon > i)
867 {
868 emsg(_("E688: More targets than List items"));
869 return FAIL;
870 }
871
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200872 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200873 item = l->lv_first;
874 while (*arg != ']')
875 {
876 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100877 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200878 item = item->li_next;
879 if (arg == NULL)
880 return FAIL;
881
882 arg = skipwhite(arg);
883 if (*arg == ';')
884 {
885 // Put the rest of the list (may be empty) in the var after ';'.
886 // Create a new list for this.
887 l = list_alloc();
888 if (l == NULL)
889 return FAIL;
890 while (item != NULL)
891 {
892 list_append_tv(l, &item->li_tv);
893 item = item->li_next;
894 }
895
896 ltv.v_type = VAR_LIST;
897 ltv.v_lock = 0;
898 ltv.vval.v_list = l;
899 l->lv_refcount = 1;
900
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100901 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200902 (char_u *)"]", op);
903 clear_tv(&ltv);
904 if (arg == NULL)
905 return FAIL;
906 break;
907 }
908 else if (*arg != ',' && *arg != ']')
909 {
910 internal_error("ex_let_vars()");
911 return FAIL;
912 }
913 }
914
915 return OK;
916}
917
918/*
919 * Skip over assignable variable "var" or list of variables "[var, var]".
920 * Used for ":let varvar = expr" and ":for varvar in expr".
921 * For "[var, var]" increment "*var_count" for each variable.
922 * for "[var, var; var]" set "semicolon".
923 * Return NULL for an error.
924 */
925 char_u *
926skip_var_list(
927 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200929 int *var_count,
930 int *semicolon)
931{
932 char_u *p, *s;
933
934 if (*arg == '[')
935 {
936 // "[var, var]": find the matching ']'.
937 p = arg;
938 for (;;)
939 {
940 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100941 s = skip_var_one(p, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200942 if (s == p)
943 {
944 semsg(_(e_invarg2), p);
945 return NULL;
946 }
947 ++*var_count;
948
949 p = skipwhite(s);
950 if (*p == ']')
951 break;
952 else if (*p == ';')
953 {
954 if (*semicolon == 1)
955 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200956 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200957 return NULL;
958 }
959 *semicolon = 1;
960 }
961 else if (*p != ',')
962 {
963 semsg(_(e_invarg2), p);
964 return NULL;
965 }
966 }
967 return p + 1;
968 }
969 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100970 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200971}
972
973/*
974 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
975 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100976 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200977 */
978 static char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100979skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200980{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 char_u *end;
982
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200983 if (*arg == '@' && arg[1] != NUL)
984 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200986 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100987 if (include_type && current_sctx.sc_version == SCRIPT_VERSION_VIM9
988 && *end == ':')
989 {
990 end = skip_type(skipwhite(end + 1));
991 }
992 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200993}
994
995/*
996 * List variables for hashtab "ht" with prefix "prefix".
997 * If "empty" is TRUE also list NULL strings as empty strings.
998 */
999 void
1000list_hashtable_vars(
1001 hashtab_T *ht,
1002 char *prefix,
1003 int empty,
1004 int *first)
1005{
1006 hashitem_T *hi;
1007 dictitem_T *di;
1008 int todo;
1009 char_u buf[IOSIZE];
1010
1011 todo = (int)ht->ht_used;
1012 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1013 {
1014 if (!HASHITEM_EMPTY(hi))
1015 {
1016 --todo;
1017 di = HI2DI(hi);
1018
1019 // apply :filter /pat/ to variable name
1020 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1021 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1022 if (message_filtered(buf))
1023 continue;
1024
1025 if (empty || di->di_tv.v_type != VAR_STRING
1026 || di->di_tv.vval.v_string != NULL)
1027 list_one_var(di, prefix, first);
1028 }
1029 }
1030}
1031
1032/*
1033 * List global variables.
1034 */
1035 static void
1036list_glob_vars(int *first)
1037{
1038 list_hashtable_vars(&globvarht, "", TRUE, first);
1039}
1040
1041/*
1042 * List buffer variables.
1043 */
1044 static void
1045list_buf_vars(int *first)
1046{
1047 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1048}
1049
1050/*
1051 * List window variables.
1052 */
1053 static void
1054list_win_vars(int *first)
1055{
1056 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1057}
1058
1059/*
1060 * List tab page variables.
1061 */
1062 static void
1063list_tab_vars(int *first)
1064{
1065 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1066}
1067
1068/*
1069 * List variables in "arg".
1070 */
1071 static char_u *
1072list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1073{
1074 int error = FALSE;
1075 int len;
1076 char_u *name;
1077 char_u *name_start;
1078 char_u *arg_subsc;
1079 char_u *tofree;
1080 typval_T tv;
1081
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001082 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001083 {
1084 if (error || eap->skip)
1085 {
1086 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1087 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1088 {
1089 emsg_severe = TRUE;
1090 emsg(_(e_trailing));
1091 break;
1092 }
1093 }
1094 else
1095 {
1096 // get_name_len() takes care of expanding curly braces
1097 name_start = name = arg;
1098 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1099 if (len <= 0)
1100 {
1101 // This is mainly to keep test 49 working: when expanding
1102 // curly braces fails overrule the exception error message.
1103 if (len < 0 && !aborting())
1104 {
1105 emsg_severe = TRUE;
1106 semsg(_(e_invarg2), arg);
1107 break;
1108 }
1109 error = TRUE;
1110 }
1111 else
1112 {
1113 if (tofree != NULL)
1114 name = tofree;
1115 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
1116 error = TRUE;
1117 else
1118 {
1119 // handle d.key, l[idx], f(expr)
1120 arg_subsc = arg;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001121 if (handle_subscript(&arg, &tv, EVAL_EVALUATE, TRUE,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001122 name, &name) == FAIL)
1123 error = TRUE;
1124 else
1125 {
1126 if (arg == arg_subsc && len == 2 && name[1] == ':')
1127 {
1128 switch (*name)
1129 {
1130 case 'g': list_glob_vars(first); break;
1131 case 'b': list_buf_vars(first); break;
1132 case 'w': list_win_vars(first); break;
1133 case 't': list_tab_vars(first); break;
1134 case 'v': list_vim_vars(first); break;
1135 case 's': list_script_vars(first); break;
1136 case 'l': list_func_vars(first); break;
1137 default:
1138 semsg(_("E738: Can't list variables for %s"), name);
1139 }
1140 }
1141 else
1142 {
1143 char_u numbuf[NUMBUFLEN];
1144 char_u *tf;
1145 int c;
1146 char_u *s;
1147
1148 s = echo_string(&tv, &tf, numbuf, 0);
1149 c = *arg;
1150 *arg = NUL;
1151 list_one_var_a("",
1152 arg == arg_subsc ? name : name_start,
1153 tv.v_type,
1154 s == NULL ? (char_u *)"" : s,
1155 first);
1156 *arg = c;
1157 vim_free(tf);
1158 }
1159 clear_tv(&tv);
1160 }
1161 }
1162 }
1163
1164 vim_free(tofree);
1165 }
1166
1167 arg = skipwhite(arg);
1168 }
1169
1170 return arg;
1171}
1172
1173/*
1174 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1175 * Returns a pointer to the char just after the var name.
1176 * Returns NULL if there is an error.
1177 */
1178 static char_u *
1179ex_let_one(
1180 char_u *arg, // points to variable name
1181 typval_T *tv, // value to assign to variable
1182 int copy, // copy value from "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001184 char_u *endchars, // valid chars after variable name or NULL
1185 char_u *op) // "+", "-", "." or NULL
1186{
1187 int c1;
1188 char_u *name;
1189 char_u *p;
1190 char_u *arg_end = NULL;
1191 int len;
1192 int opt_flags;
1193 char_u *tofree = NULL;
1194
1195 // ":let $VAR = expr": Set environment variable.
1196 if (*arg == '$')
1197 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001199 {
1200 emsg(_("E996: Cannot lock an environment variable"));
1201 return NULL;
1202 }
1203 // Find the end of the name.
1204 ++arg;
1205 name = arg;
1206 len = get_env_len(&arg);
1207 if (len == 0)
1208 semsg(_(e_invarg2), name - 1);
1209 else
1210 {
1211 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1212 semsg(_(e_letwrong), op);
1213 else if (endchars != NULL
1214 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1215 emsg(_(e_letunexp));
1216 else if (!check_secure())
1217 {
1218 c1 = name[len];
1219 name[len] = NUL;
1220 p = tv_get_string_chk(tv);
1221 if (p != NULL && op != NULL && *op == '.')
1222 {
1223 int mustfree = FALSE;
1224 char_u *s = vim_getenv(name, &mustfree);
1225
1226 if (s != NULL)
1227 {
1228 p = tofree = concat_str(s, p);
1229 if (mustfree)
1230 vim_free(s);
1231 }
1232 }
1233 if (p != NULL)
1234 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001235 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001236 arg_end = arg;
1237 }
1238 name[len] = c1;
1239 vim_free(tofree);
1240 }
1241 }
1242 }
1243
1244 // ":let &option = expr": Set option value.
1245 // ":let &l:option = expr": Set local option value.
1246 // ":let &g:option = expr": Set global option value.
1247 else if (*arg == '&')
1248 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001250 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001252 return NULL;
1253 }
1254 // Find the end of the name.
1255 p = find_option_end(&arg, &opt_flags);
1256 if (p == NULL || (endchars != NULL
1257 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1258 emsg(_(e_letunexp));
1259 else
1260 {
1261 long n;
1262 int opt_type;
1263 long numval;
1264 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001265 char_u *s = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001266
1267 c1 = *p;
1268 *p = NUL;
1269
1270 n = (long)tv_get_number(tv);
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001271 // avoid setting a string option to the text "v:false" or similar.
1272 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL)
1273 s = tv_get_string_chk(tv); // != NULL if number or string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001274 if (s != NULL && op != NULL && *op != '=')
1275 {
1276 opt_type = get_option_value(arg, &numval,
1277 &stringval, opt_flags);
1278 if ((opt_type == 1 && *op == '.')
1279 || (opt_type == 0 && *op != '.'))
1280 {
1281 semsg(_(e_letwrong), op);
1282 s = NULL; // don't set the value
1283 }
1284 else
1285 {
1286 if (opt_type == 1) // number
1287 {
1288 switch (*op)
1289 {
1290 case '+': n = numval + n; break;
1291 case '-': n = numval - n; break;
1292 case '*': n = numval * n; break;
1293 case '/': n = (long)num_divide(numval, n); break;
1294 case '%': n = (long)num_modulus(numval, n); break;
1295 }
1296 }
1297 else if (opt_type == 0 && stringval != NULL) // string
1298 {
1299 s = concat_str(stringval, s);
1300 vim_free(stringval);
1301 stringval = s;
1302 }
1303 }
1304 }
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001305 if (s != NULL || tv->v_type == VAR_BOOL
1306 || tv->v_type == VAR_SPECIAL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001307 {
1308 set_option_value(arg, n, s, opt_flags);
1309 arg_end = p;
1310 }
1311 *p = c1;
1312 vim_free(stringval);
1313 }
1314 }
1315
1316 // ":let @r = expr": Set register contents.
1317 else if (*arg == '@')
1318 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001320 {
1321 emsg(_("E996: Cannot lock a register"));
1322 return NULL;
1323 }
1324 ++arg;
1325 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1326 semsg(_(e_letwrong), op);
1327 else if (endchars != NULL
1328 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1329 emsg(_(e_letunexp));
1330 else
1331 {
1332 char_u *ptofree = NULL;
1333 char_u *s;
1334
1335 p = tv_get_string_chk(tv);
1336 if (p != NULL && op != NULL && *op == '.')
1337 {
1338 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1339 if (s != NULL)
1340 {
1341 p = ptofree = concat_str(s, p);
1342 vim_free(s);
1343 }
1344 }
1345 if (p != NULL)
1346 {
1347 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1348 arg_end = arg + 1;
1349 }
1350 vim_free(ptofree);
1351 }
1352 }
1353
1354 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001356 // ":let {expr} = expr": Idem, name made with curly braces
1357 else if (eval_isnamec1(*arg) || *arg == '{')
1358 {
1359 lval_T lv;
1360
1361 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001362 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001363 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364 if (endchars != NULL && vim_strchr(endchars,
1365 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001366 emsg(_(e_letunexp));
1367 else
1368 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001369 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001370 arg_end = p;
1371 }
1372 }
1373 clear_lval(&lv);
1374 }
1375
1376 else
1377 semsg(_(e_invarg2), arg);
1378
1379 return arg_end;
1380}
1381
1382/*
1383 * ":unlet[!] var1 ... " command.
1384 */
1385 void
1386ex_unlet(exarg_T *eap)
1387{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001388 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001389}
1390
1391/*
1392 * ":lockvar" and ":unlockvar" commands
1393 */
1394 void
1395ex_lockvar(exarg_T *eap)
1396{
1397 char_u *arg = eap->arg;
1398 int deep = 2;
1399
1400 if (eap->forceit)
1401 deep = -1;
1402 else if (vim_isdigit(*arg))
1403 {
1404 deep = getdigits(&arg);
1405 arg = skipwhite(arg);
1406 }
1407
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001408 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001409}
1410
1411/*
1412 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001413 * Also used for Vim9 script. "callback" is invoked as:
1414 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001415 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001416 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001417ex_unletlock(
1418 exarg_T *eap,
1419 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001420 int deep,
1421 int glv_flags,
1422 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1423 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001424{
1425 char_u *arg = argstart;
1426 char_u *name_end;
1427 int error = FALSE;
1428 lval_T lv;
1429
1430 do
1431 {
1432 if (*arg == '$')
1433 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001434 lv.ll_name = arg;
1435 lv.ll_tv = NULL;
1436 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001437 if (get_env_len(&arg) == 0)
1438 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001439 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001440 return;
1441 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001442 if (!error && !eap->skip
1443 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1444 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001445 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001446 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001447 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001448 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001449 // Parse the name and find the end.
1450 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1451 glv_flags, FNE_CHECK_START);
1452 if (lv.ll_name == NULL)
1453 error = TRUE; // error but continue parsing
1454 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1455 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001456 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001457 if (name_end != NULL)
1458 {
1459 emsg_severe = TRUE;
1460 emsg(_(e_trailing));
1461 }
1462 if (!(eap->skip || error))
1463 clear_lval(&lv);
1464 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001465 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001466
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001467 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001468 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001469 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001470
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001471 if (!eap->skip)
1472 clear_lval(&lv);
1473 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001474
1475 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001476 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001477
1478 eap->nextcmd = check_nextcmd(arg);
1479}
1480
1481 static int
1482do_unlet_var(
1483 lval_T *lp,
1484 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001485 exarg_T *eap,
1486 int deep UNUSED,
1487 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001488{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001489 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001490 int ret = OK;
1491 int cc;
1492
1493 if (lp->ll_tv == NULL)
1494 {
1495 cc = *name_end;
1496 *name_end = NUL;
1497
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001498 // Environment variable, normal name or expanded name.
1499 if (*lp->ll_name == '$')
1500 vim_unsetenv(lp->ll_name + 1);
1501 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001502 ret = FAIL;
1503 *name_end = cc;
1504 }
1505 else if ((lp->ll_list != NULL
1506 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
1507 || (lp->ll_dict != NULL
1508 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
1509 return FAIL;
1510 else if (lp->ll_range)
1511 {
1512 listitem_T *li;
1513 listitem_T *ll_li = lp->ll_li;
1514 int ll_n1 = lp->ll_n1;
1515
1516 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1517 {
1518 li = ll_li->li_next;
1519 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
1520 return FAIL;
1521 ll_li = li;
1522 ++ll_n1;
1523 }
1524
1525 // Delete a range of List items.
1526 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1527 {
1528 li = lp->ll_li->li_next;
1529 listitem_remove(lp->ll_list, lp->ll_li);
1530 lp->ll_li = li;
1531 ++lp->ll_n1;
1532 }
1533 }
1534 else
1535 {
1536 if (lp->ll_list != NULL)
1537 // unlet a List item.
1538 listitem_remove(lp->ll_list, lp->ll_li);
1539 else
1540 // unlet a Dictionary item.
1541 dictitem_remove(lp->ll_dict, lp->ll_di);
1542 }
1543
1544 return ret;
1545}
1546
1547/*
1548 * "unlet" a variable. Return OK if it existed, FAIL if not.
1549 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1550 */
1551 int
1552do_unlet(char_u *name, int forceit)
1553{
1554 hashtab_T *ht;
1555 hashitem_T *hi;
1556 char_u *varname;
1557 dict_T *d;
1558 dictitem_T *di;
1559
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001560 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1561 && check_vim9_unlet(name) == FAIL)
1562 return FAIL;
1563
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001564 ht = find_var_ht(name, &varname);
1565 if (ht != NULL && *varname != NUL)
1566 {
1567 d = get_current_funccal_dict(ht);
1568 if (d == NULL)
1569 {
1570 if (ht == &globvarht)
1571 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001572 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001573 d = &vimvardict;
1574 else
1575 {
1576 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1577 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1578 }
1579 if (d == NULL)
1580 {
1581 internal_error("do_unlet()");
1582 return FAIL;
1583 }
1584 }
1585 hi = hash_find(ht, varname);
1586 if (HASHITEM_EMPTY(hi))
1587 hi = find_hi_in_scoped_ht(name, &ht);
1588 if (hi != NULL && !HASHITEM_EMPTY(hi))
1589 {
1590 di = HI2DI(hi);
1591 if (var_check_fixed(di->di_flags, name, FALSE)
1592 || var_check_ro(di->di_flags, name, FALSE)
1593 || var_check_lock(d->dv_lock, name, FALSE))
1594 return FAIL;
1595
1596 delete_var(ht, hi);
1597 return OK;
1598 }
1599 }
1600 if (forceit)
1601 return OK;
1602 semsg(_("E108: No such variable: \"%s\""), name);
1603 return FAIL;
1604}
1605
1606/*
1607 * Lock or unlock variable indicated by "lp".
1608 * "deep" is the levels to go (-1 for unlimited);
1609 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1610 */
1611 static int
1612do_lock_var(
1613 lval_T *lp,
1614 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001615 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001616 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001617 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001618{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001619 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001620 int ret = OK;
1621 int cc;
1622 dictitem_T *di;
1623
1624 if (deep == 0) // nothing to do
1625 return OK;
1626
1627 if (lp->ll_tv == NULL)
1628 {
1629 cc = *name_end;
1630 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001631 if (*lp->ll_name == '$')
1632 {
1633 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001634 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001635 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001636 else
1637 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001638 // Normal name or expanded name.
1639 di = find_var(lp->ll_name, NULL, TRUE);
1640 if (di == NULL)
1641 ret = FAIL;
1642 else if ((di->di_flags & DI_FLAGS_FIX)
1643 && di->di_tv.v_type != VAR_DICT
1644 && di->di_tv.v_type != VAR_LIST)
1645 {
1646 // For historic reasons this error is not given for a list or
1647 // dict. E.g., the b: dict could be locked/unlocked.
1648 semsg(_(e_lock_unlock), lp->ll_name);
1649 ret = FAIL;
1650 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001651 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001652 {
1653 if (lock)
1654 di->di_flags |= DI_FLAGS_LOCK;
1655 else
1656 di->di_flags &= ~DI_FLAGS_LOCK;
1657 item_lock(&di->di_tv, deep, lock);
1658 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001659 }
1660 *name_end = cc;
1661 }
1662 else if (lp->ll_range)
1663 {
1664 listitem_T *li = lp->ll_li;
1665
1666 // (un)lock a range of List items.
1667 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1668 {
1669 item_lock(&li->li_tv, deep, lock);
1670 li = li->li_next;
1671 ++lp->ll_n1;
1672 }
1673 }
1674 else if (lp->ll_list != NULL)
1675 // (un)lock a List item.
1676 item_lock(&lp->ll_li->li_tv, deep, lock);
1677 else
1678 // (un)lock a Dictionary item.
1679 item_lock(&lp->ll_di->di_tv, deep, lock);
1680
1681 return ret;
1682}
1683
1684/*
1685 * Lock or unlock an item. "deep" is nr of levels to go.
1686 */
1687 static void
1688item_lock(typval_T *tv, int deep, int lock)
1689{
1690 static int recurse = 0;
1691 list_T *l;
1692 listitem_T *li;
1693 dict_T *d;
1694 blob_T *b;
1695 hashitem_T *hi;
1696 int todo;
1697
1698 if (recurse >= DICT_MAXNEST)
1699 {
1700 emsg(_("E743: variable nested too deep for (un)lock"));
1701 return;
1702 }
1703 if (deep == 0)
1704 return;
1705 ++recurse;
1706
1707 // lock/unlock the item itself
1708 if (lock)
1709 tv->v_lock |= VAR_LOCKED;
1710 else
1711 tv->v_lock &= ~VAR_LOCKED;
1712
1713 switch (tv->v_type)
1714 {
1715 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001716 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001717 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001718 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001720 case VAR_STRING:
1721 case VAR_FUNC:
1722 case VAR_PARTIAL:
1723 case VAR_FLOAT:
1724 case VAR_SPECIAL:
1725 case VAR_JOB:
1726 case VAR_CHANNEL:
1727 break;
1728
1729 case VAR_BLOB:
1730 if ((b = tv->vval.v_blob) != NULL)
1731 {
1732 if (lock)
1733 b->bv_lock |= VAR_LOCKED;
1734 else
1735 b->bv_lock &= ~VAR_LOCKED;
1736 }
1737 break;
1738 case VAR_LIST:
1739 if ((l = tv->vval.v_list) != NULL)
1740 {
1741 if (lock)
1742 l->lv_lock |= VAR_LOCKED;
1743 else
1744 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001745 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001746 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001747 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001748 item_lock(&li->li_tv, deep - 1, lock);
1749 }
1750 break;
1751 case VAR_DICT:
1752 if ((d = tv->vval.v_dict) != NULL)
1753 {
1754 if (lock)
1755 d->dv_lock |= VAR_LOCKED;
1756 else
1757 d->dv_lock &= ~VAR_LOCKED;
1758 if (deep < 0 || deep > 1)
1759 {
1760 // recursive: lock/unlock the items the List contains
1761 todo = (int)d->dv_hashtab.ht_used;
1762 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1763 {
1764 if (!HASHITEM_EMPTY(hi))
1765 {
1766 --todo;
1767 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
1768 }
1769 }
1770 }
1771 }
1772 }
1773 --recurse;
1774}
1775
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001776#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1777/*
1778 * Delete all "menutrans_" variables.
1779 */
1780 void
1781del_menutrans_vars(void)
1782{
1783 hashitem_T *hi;
1784 int todo;
1785
1786 hash_lock(&globvarht);
1787 todo = (int)globvarht.ht_used;
1788 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1789 {
1790 if (!HASHITEM_EMPTY(hi))
1791 {
1792 --todo;
1793 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1794 delete_var(&globvarht, hi);
1795 }
1796 }
1797 hash_unlock(&globvarht);
1798}
1799#endif
1800
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001801/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001802 * Local string buffer for the next two functions to store a variable name
1803 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1804 * get_user_var_name().
1805 */
1806
1807static char_u *varnamebuf = NULL;
1808static int varnamebuflen = 0;
1809
1810/*
1811 * Function to concatenate a prefix and a variable name.
1812 */
1813 static char_u *
1814cat_prefix_varname(int prefix, char_u *name)
1815{
1816 int len;
1817
1818 len = (int)STRLEN(name) + 3;
1819 if (len > varnamebuflen)
1820 {
1821 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001822 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001823 varnamebuf = alloc(len);
1824 if (varnamebuf == NULL)
1825 {
1826 varnamebuflen = 0;
1827 return NULL;
1828 }
1829 varnamebuflen = len;
1830 }
1831 *varnamebuf = prefix;
1832 varnamebuf[1] = ':';
1833 STRCPY(varnamebuf + 2, name);
1834 return varnamebuf;
1835}
1836
1837/*
1838 * Function given to ExpandGeneric() to obtain the list of user defined
1839 * (global/buffer/window/built-in) variable names.
1840 */
1841 char_u *
1842get_user_var_name(expand_T *xp, int idx)
1843{
1844 static long_u gdone;
1845 static long_u bdone;
1846 static long_u wdone;
1847 static long_u tdone;
1848 static int vidx;
1849 static hashitem_T *hi;
1850 hashtab_T *ht;
1851
1852 if (idx == 0)
1853 {
1854 gdone = bdone = wdone = vidx = 0;
1855 tdone = 0;
1856 }
1857
1858 // Global variables
1859 if (gdone < globvarht.ht_used)
1860 {
1861 if (gdone++ == 0)
1862 hi = globvarht.ht_array;
1863 else
1864 ++hi;
1865 while (HASHITEM_EMPTY(hi))
1866 ++hi;
1867 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1868 return cat_prefix_varname('g', hi->hi_key);
1869 return hi->hi_key;
1870 }
1871
1872 // b: variables
1873 ht = &curbuf->b_vars->dv_hashtab;
1874 if (bdone < ht->ht_used)
1875 {
1876 if (bdone++ == 0)
1877 hi = ht->ht_array;
1878 else
1879 ++hi;
1880 while (HASHITEM_EMPTY(hi))
1881 ++hi;
1882 return cat_prefix_varname('b', hi->hi_key);
1883 }
1884
1885 // w: variables
1886 ht = &curwin->w_vars->dv_hashtab;
1887 if (wdone < ht->ht_used)
1888 {
1889 if (wdone++ == 0)
1890 hi = ht->ht_array;
1891 else
1892 ++hi;
1893 while (HASHITEM_EMPTY(hi))
1894 ++hi;
1895 return cat_prefix_varname('w', hi->hi_key);
1896 }
1897
1898 // t: variables
1899 ht = &curtab->tp_vars->dv_hashtab;
1900 if (tdone < ht->ht_used)
1901 {
1902 if (tdone++ == 0)
1903 hi = ht->ht_array;
1904 else
1905 ++hi;
1906 while (HASHITEM_EMPTY(hi))
1907 ++hi;
1908 return cat_prefix_varname('t', hi->hi_key);
1909 }
1910
1911 // v: variables
1912 if (vidx < VV_LEN)
1913 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
1914
1915 VIM_CLEAR(varnamebuf);
1916 varnamebuflen = 0;
1917 return NULL;
1918}
1919
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001920 char *
1921get_var_special_name(int nr)
1922{
1923 switch (nr)
1924 {
1925 case VVAL_FALSE: return "v:false";
1926 case VVAL_TRUE: return "v:true";
1927 case VVAL_NONE: return "v:none";
1928 case VVAL_NULL: return "v:null";
1929 }
1930 internal_error("get_var_special_name()");
1931 return "42";
1932}
1933
1934/*
1935 * Returns the global variable dictionary
1936 */
1937 dict_T *
1938get_globvar_dict(void)
1939{
1940 return &globvardict;
1941}
1942
1943/*
1944 * Returns the global variable hash table
1945 */
1946 hashtab_T *
1947get_globvar_ht(void)
1948{
1949 return &globvarht;
1950}
1951
1952/*
1953 * Returns the v: variable dictionary
1954 */
1955 dict_T *
1956get_vimvar_dict(void)
1957{
1958 return &vimvardict;
1959}
1960
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001961/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001962 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001963 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001964 */
1965 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001966find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001967{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001968 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
1969 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001970
1971 if (di == NULL)
1972 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001973 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001974 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
1975 return (int)(vv - vimvars);
1976}
1977
1978
1979/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02001980 * Set type of v: variable to "type".
1981 */
1982 void
1983set_vim_var_type(int idx, vartype_T type)
1984{
1985 vimvars[idx].vv_type = type;
1986}
1987
1988/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001989 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001990 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001991 */
1992 void
1993set_vim_var_nr(int idx, varnumber_T val)
1994{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001995 vimvars[idx].vv_nr = val;
1996}
1997
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 char *
1999get_vim_var_name(int idx)
2000{
2001 return vimvars[idx].vv_name;
2002}
2003
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002004/*
2005 * Get typval_T v: variable value.
2006 */
2007 typval_T *
2008get_vim_var_tv(int idx)
2009{
2010 return &vimvars[idx].vv_tv;
2011}
2012
2013/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002014 * Set v: variable to "tv". Only accepts the same type.
2015 * Takes over the value of "tv".
2016 */
2017 int
2018set_vim_var_tv(int idx, typval_T *tv)
2019{
2020 if (vimvars[idx].vv_type != tv->v_type)
2021 {
2022 emsg(_("E1063: type mismatch for v: variable"));
2023 clear_tv(tv);
2024 return FAIL;
2025 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002026 // VV_RO is also checked when compiling, but let's check here as well.
2027 if (vimvars[idx].vv_flags & VV_RO)
2028 {
2029 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2030 return FAIL;
2031 }
2032 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2033 {
2034 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2035 return FAIL;
2036 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002037 clear_tv(&vimvars[idx].vv_di.di_tv);
2038 vimvars[idx].vv_di.di_tv = *tv;
2039 return OK;
2040}
2041
2042/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002043 * Get number v: variable value.
2044 */
2045 varnumber_T
2046get_vim_var_nr(int idx)
2047{
2048 return vimvars[idx].vv_nr;
2049}
2050
2051/*
2052 * Get string v: variable value. Uses a static buffer, can only be used once.
2053 * If the String variable has never been set, return an empty string.
2054 * Never returns NULL;
2055 */
2056 char_u *
2057get_vim_var_str(int idx)
2058{
2059 return tv_get_string(&vimvars[idx].vv_tv);
2060}
2061
2062/*
2063 * Get List v: variable value. Caller must take care of reference count when
2064 * needed.
2065 */
2066 list_T *
2067get_vim_var_list(int idx)
2068{
2069 return vimvars[idx].vv_list;
2070}
2071
2072/*
2073 * Get Dict v: variable value. Caller must take care of reference count when
2074 * needed.
2075 */
2076 dict_T *
2077get_vim_var_dict(int idx)
2078{
2079 return vimvars[idx].vv_dict;
2080}
2081
2082/*
2083 * Set v:char to character "c".
2084 */
2085 void
2086set_vim_var_char(int c)
2087{
2088 char_u buf[MB_MAXBYTES + 1];
2089
2090 if (has_mbyte)
2091 buf[(*mb_char2bytes)(c, buf)] = NUL;
2092 else
2093 {
2094 buf[0] = c;
2095 buf[1] = NUL;
2096 }
2097 set_vim_var_string(VV_CHAR, buf, -1);
2098}
2099
2100/*
2101 * Set v:count to "count" and v:count1 to "count1".
2102 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2103 */
2104 void
2105set_vcount(
2106 long count,
2107 long count1,
2108 int set_prevcount)
2109{
2110 if (set_prevcount)
2111 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2112 vimvars[VV_COUNT].vv_nr = count;
2113 vimvars[VV_COUNT1].vv_nr = count1;
2114}
2115
2116/*
2117 * Save variables that might be changed as a side effect. Used when executing
2118 * a timer callback.
2119 */
2120 void
2121save_vimvars(vimvars_save_T *vvsave)
2122{
2123 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2124 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2125 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2126}
2127
2128/*
2129 * Restore variables saved by save_vimvars().
2130 */
2131 void
2132restore_vimvars(vimvars_save_T *vvsave)
2133{
2134 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2135 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2136 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2137}
2138
2139/*
2140 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2141 * value.
2142 */
2143 void
2144set_vim_var_string(
2145 int idx,
2146 char_u *val,
2147 int len) // length of "val" to use or -1 (whole string)
2148{
2149 clear_tv(&vimvars[idx].vv_di.di_tv);
2150 vimvars[idx].vv_type = VAR_STRING;
2151 if (val == NULL)
2152 vimvars[idx].vv_str = NULL;
2153 else if (len == -1)
2154 vimvars[idx].vv_str = vim_strsave(val);
2155 else
2156 vimvars[idx].vv_str = vim_strnsave(val, len);
2157}
2158
2159/*
2160 * Set List v: variable to "val".
2161 */
2162 void
2163set_vim_var_list(int idx, list_T *val)
2164{
2165 clear_tv(&vimvars[idx].vv_di.di_tv);
2166 vimvars[idx].vv_type = VAR_LIST;
2167 vimvars[idx].vv_list = val;
2168 if (val != NULL)
2169 ++val->lv_refcount;
2170}
2171
2172/*
2173 * Set Dictionary v: variable to "val".
2174 */
2175 void
2176set_vim_var_dict(int idx, dict_T *val)
2177{
2178 clear_tv(&vimvars[idx].vv_di.di_tv);
2179 vimvars[idx].vv_type = VAR_DICT;
2180 vimvars[idx].vv_dict = val;
2181 if (val != NULL)
2182 {
2183 ++val->dv_refcount;
2184 dict_set_items_ro(val);
2185 }
2186}
2187
2188/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002189 * Set the v:argv list.
2190 */
2191 void
2192set_argv_var(char **argv, int argc)
2193{
2194 list_T *l = list_alloc();
2195 int i;
2196
2197 if (l == NULL)
2198 getout(1);
2199 l->lv_lock = VAR_FIXED;
2200 for (i = 0; i < argc; ++i)
2201 {
2202 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2203 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002204 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002205 }
2206 set_vim_var_list(VV_ARGV, l);
2207}
2208
2209/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002210 * Set v:register if needed.
2211 */
2212 void
2213set_reg_var(int c)
2214{
2215 char_u regname;
2216
2217 if (c == 0 || c == ' ')
2218 regname = '"';
2219 else
2220 regname = c;
2221 // Avoid free/alloc when the value is already right.
2222 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2223 set_vim_var_string(VV_REG, &regname, 1);
2224}
2225
2226/*
2227 * Get or set v:exception. If "oldval" == NULL, return the current value.
2228 * Otherwise, restore the value to "oldval" and return NULL.
2229 * Must always be called in pairs to save and restore v:exception! Does not
2230 * take care of memory allocations.
2231 */
2232 char_u *
2233v_exception(char_u *oldval)
2234{
2235 if (oldval == NULL)
2236 return vimvars[VV_EXCEPTION].vv_str;
2237
2238 vimvars[VV_EXCEPTION].vv_str = oldval;
2239 return NULL;
2240}
2241
2242/*
2243 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2244 * Otherwise, restore the value to "oldval" and return NULL.
2245 * Must always be called in pairs to save and restore v:throwpoint! Does not
2246 * take care of memory allocations.
2247 */
2248 char_u *
2249v_throwpoint(char_u *oldval)
2250{
2251 if (oldval == NULL)
2252 return vimvars[VV_THROWPOINT].vv_str;
2253
2254 vimvars[VV_THROWPOINT].vv_str = oldval;
2255 return NULL;
2256}
2257
2258/*
2259 * Set v:cmdarg.
2260 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2261 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2262 * Must always be called in pairs!
2263 */
2264 char_u *
2265set_cmdarg(exarg_T *eap, char_u *oldarg)
2266{
2267 char_u *oldval;
2268 char_u *newval;
2269 unsigned len;
2270
2271 oldval = vimvars[VV_CMDARG].vv_str;
2272 if (eap == NULL)
2273 {
2274 vim_free(oldval);
2275 vimvars[VV_CMDARG].vv_str = oldarg;
2276 return NULL;
2277 }
2278
2279 if (eap->force_bin == FORCE_BIN)
2280 len = 6;
2281 else if (eap->force_bin == FORCE_NOBIN)
2282 len = 8;
2283 else
2284 len = 0;
2285
2286 if (eap->read_edit)
2287 len += 7;
2288
2289 if (eap->force_ff != 0)
2290 len += 10; // " ++ff=unix"
2291 if (eap->force_enc != 0)
2292 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2293 if (eap->bad_char != 0)
2294 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2295
2296 newval = alloc(len + 1);
2297 if (newval == NULL)
2298 return NULL;
2299
2300 if (eap->force_bin == FORCE_BIN)
2301 sprintf((char *)newval, " ++bin");
2302 else if (eap->force_bin == FORCE_NOBIN)
2303 sprintf((char *)newval, " ++nobin");
2304 else
2305 *newval = NUL;
2306
2307 if (eap->read_edit)
2308 STRCAT(newval, " ++edit");
2309
2310 if (eap->force_ff != 0)
2311 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2312 eap->force_ff == 'u' ? "unix"
2313 : eap->force_ff == 'd' ? "dos"
2314 : "mac");
2315 if (eap->force_enc != 0)
2316 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2317 eap->cmd + eap->force_enc);
2318 if (eap->bad_char == BAD_KEEP)
2319 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2320 else if (eap->bad_char == BAD_DROP)
2321 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2322 else if (eap->bad_char != 0)
2323 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2324 vimvars[VV_CMDARG].vv_str = newval;
2325 return oldval;
2326}
2327
2328/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002329 * Get the value of internal variable "name".
2330 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2331 */
2332 int
2333get_var_tv(
2334 char_u *name,
2335 int len, // length of "name"
2336 typval_T *rettv, // NULL when only checking existence
2337 dictitem_T **dip, // non-NULL when typval's dict item is needed
2338 int verbose, // may give error message
2339 int no_autoload) // do not use script autoloading
2340{
2341 int ret = OK;
2342 typval_T *tv = NULL;
2343 dictitem_T *v;
2344 int cc;
2345
2346 // truncate the name, so that we can use strcmp()
2347 cc = name[len];
2348 name[len] = NUL;
2349
2350 // Check for user-defined variables.
2351 v = find_var(name, NULL, no_autoload);
2352 if (v != NULL)
2353 {
2354 tv = &v->di_tv;
2355 if (dip != NULL)
2356 *dip = v;
2357 }
2358
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002359 if (tv == NULL && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2360 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002361 imported_T *import = find_imported(name, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002362
2363 // imported variable from another script
2364 if (import != NULL)
2365 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002366 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002367 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2368 + import->imp_var_vals_idx;
2369 tv = sv->sv_tv;
2370 }
2371 }
2372
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002373 if (tv == NULL)
2374 {
2375 if (rettv != NULL && verbose)
2376 semsg(_(e_undefvar), name);
2377 ret = FAIL;
2378 }
2379 else if (rettv != NULL)
2380 copy_tv(tv, rettv);
2381
2382 name[len] = cc;
2383
2384 return ret;
2385}
2386
2387/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002388 * Check if variable "name[len]" is a local variable or an argument.
2389 * If so, "*eval_lavars_used" is set to TRUE.
2390 */
2391 void
2392check_vars(char_u *name, int len)
2393{
2394 int cc;
2395 char_u *varname;
2396 hashtab_T *ht;
2397
2398 if (eval_lavars_used == NULL)
2399 return;
2400
2401 // truncate the name, so that we can use strcmp()
2402 cc = name[len];
2403 name[len] = NUL;
2404
2405 ht = find_var_ht(name, &varname);
2406 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2407 {
2408 if (find_var(name, NULL, TRUE) != NULL)
2409 *eval_lavars_used = TRUE;
2410 }
2411
2412 name[len] = cc;
2413}
2414
2415/*
2416 * Find variable "name" in the list of variables.
2417 * Return a pointer to it if found, NULL if not found.
2418 * Careful: "a:0" variables don't have a name.
2419 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2420 * hashtab_T used.
2421 */
2422 dictitem_T *
2423find_var(char_u *name, hashtab_T **htp, int no_autoload)
2424{
2425 char_u *varname;
2426 hashtab_T *ht;
2427 dictitem_T *ret = NULL;
2428
2429 ht = find_var_ht(name, &varname);
2430 if (htp != NULL)
2431 *htp = ht;
2432 if (ht == NULL)
2433 return NULL;
2434 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2435 if (ret != NULL)
2436 return ret;
2437
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002438 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002439 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2440}
2441
2442/*
2443 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002444 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002445 * Returns NULL if not found.
2446 */
2447 dictitem_T *
2448find_var_in_ht(
2449 hashtab_T *ht,
2450 int htname,
2451 char_u *varname,
2452 int no_autoload)
2453{
2454 hashitem_T *hi;
2455
2456 if (*varname == NUL)
2457 {
2458 // Must be something like "s:", otherwise "ht" would be NULL.
2459 switch (htname)
2460 {
2461 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2462 case 'g': return &globvars_var;
2463 case 'v': return &vimvars_var;
2464 case 'b': return &curbuf->b_bufvar;
2465 case 'w': return &curwin->w_winvar;
2466 case 't': return &curtab->tp_winvar;
2467 case 'l': return get_funccal_local_var();
2468 case 'a': return get_funccal_args_var();
2469 }
2470 return NULL;
2471 }
2472
2473 hi = hash_find(ht, varname);
2474 if (HASHITEM_EMPTY(hi))
2475 {
2476 // For global variables we may try auto-loading the script. If it
2477 // worked find the variable again. Don't auto-load a script if it was
2478 // loaded already, otherwise it would be loaded every time when
2479 // checking if a function name is a Funcref variable.
2480 if (ht == &globvarht && !no_autoload)
2481 {
2482 // Note: script_autoload() may make "hi" invalid. It must either
2483 // be obtained again or not used.
2484 if (!script_autoload(varname, FALSE) || aborting())
2485 return NULL;
2486 hi = hash_find(ht, varname);
2487 }
2488 if (HASHITEM_EMPTY(hi))
2489 return NULL;
2490 }
2491 return HI2DI(hi);
2492}
2493
2494/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 * Get the script-local hashtab. NULL if not in a script context.
2496 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02002497 static hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498get_script_local_ht(void)
2499{
2500 scid_T sid = current_sctx.sc_sid;
2501
2502 if (sid > 0 && sid <= script_items.ga_len)
2503 return &SCRIPT_VARS(sid);
2504 return NULL;
2505}
2506
2507/*
2508 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002509 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002511 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2513{
2514 hashtab_T *ht = get_script_local_ht();
2515 char_u buffer[30];
2516 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002517 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 hashitem_T *hi;
2519
2520 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002521 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 if (len < sizeof(buffer) - 1)
2523 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002524 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 vim_strncpy(buffer, name, len);
2526 p = buffer;
2527 }
2528 else
2529 {
2530 p = vim_strnsave(name, (int)len);
2531 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002532 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 }
2534
2535 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002536 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537
2538 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002539 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2540 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541
2542 if (p != buffer)
2543 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002544 // Don't return "buffer", gcc complains.
2545 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546}
2547
2548/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002549 * Find the hashtab used for a variable name.
2550 * Return NULL if the name is not valid.
2551 * Set "varname" to the start of name without ':'.
2552 */
2553 hashtab_T *
2554find_var_ht(char_u *name, char_u **varname)
2555{
2556 hashitem_T *hi;
2557 hashtab_T *ht;
2558
2559 if (name[0] == NUL)
2560 return NULL;
2561 if (name[1] != ':')
2562 {
2563 // The name must not start with a colon or #.
2564 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2565 return NULL;
2566 *varname = name;
2567
2568 // "version" is "v:version" in all scopes if scriptversion < 3.
2569 // Same for a few other variables marked with VV_COMPAT.
2570 if (current_sctx.sc_version < 3)
2571 {
2572 hi = hash_find(&compat_hashtab, name);
2573 if (!HASHITEM_EMPTY(hi))
2574 return &compat_hashtab;
2575 }
2576
2577 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578 if (ht != NULL)
2579 return ht; // local variable
2580
2581 // in Vim9 script items at the script level are script-local
2582 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2583 {
2584 ht = get_script_local_ht();
2585 if (ht != NULL)
2586 return ht;
2587 }
2588
2589 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002590 }
2591 *varname = name + 2;
2592 if (*name == 'g') // global variable
2593 return &globvarht;
2594 // There must be no ':' or '#' in the rest of the name, unless g: is used
2595 if (vim_strchr(name + 2, ':') != NULL
2596 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2597 return NULL;
2598 if (*name == 'b') // buffer variable
2599 return &curbuf->b_vars->dv_hashtab;
2600 if (*name == 'w') // window variable
2601 return &curwin->w_vars->dv_hashtab;
2602 if (*name == 't') // tab page variable
2603 return &curtab->tp_vars->dv_hashtab;
2604 if (*name == 'v') // v: variable
2605 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002606 if (get_current_funccal() != NULL
Bram Moolenaar822ba242020-05-24 23:00:18 +02002607 && get_current_funccal()->func->uf_dfunc_idx == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002609 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610 if (*name == 'a') // a: function argument
2611 return get_funccal_args_ht();
2612 if (*name == 'l') // l: local function variable
2613 return get_funccal_local_ht();
2614 }
2615 if (*name == 's') // script variable
2616 {
2617 ht = get_script_local_ht();
2618 if (ht != NULL)
2619 return ht;
2620 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002621 return NULL;
2622}
2623
2624/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002625 * Get the string value of a (global/local) variable.
2626 * Note: see tv_get_string() for how long the pointer remains valid.
2627 * Returns NULL when it doesn't exist.
2628 */
2629 char_u *
2630get_var_value(char_u *name)
2631{
2632 dictitem_T *v;
2633
2634 v = find_var(name, NULL, FALSE);
2635 if (v == NULL)
2636 return NULL;
2637 return tv_get_string(&v->di_tv);
2638}
2639
2640/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002641 * Allocate a new hashtab for a sourced script. It will be used while
2642 * sourcing this script and when executing functions defined in the script.
2643 */
2644 void
2645new_script_vars(scid_T id)
2646{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002647 scriptvar_T *sv;
2648
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002649 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2650 if (sv == NULL)
2651 return;
2652 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002653 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002654}
2655
2656/*
2657 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2658 * point to it.
2659 */
2660 void
2661init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2662{
2663 hash_init(&dict->dv_hashtab);
2664 dict->dv_lock = 0;
2665 dict->dv_scope = scope;
2666 dict->dv_refcount = DO_NOT_FREE_CNT;
2667 dict->dv_copyID = 0;
2668 dict_var->di_tv.vval.v_dict = dict;
2669 dict_var->di_tv.v_type = VAR_DICT;
2670 dict_var->di_tv.v_lock = VAR_FIXED;
2671 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2672 dict_var->di_key[0] = NUL;
2673}
2674
2675/*
2676 * Unreference a dictionary initialized by init_var_dict().
2677 */
2678 void
2679unref_var_dict(dict_T *dict)
2680{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002681 // Now the dict needs to be freed if no one else is using it, go back to
2682 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002683 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2684 dict_unref(dict);
2685}
2686
2687/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002688 * Clean up a list of internal variables.
2689 * Frees all allocated variables and the value they contain.
2690 * Clears hashtab "ht", does not free it.
2691 */
2692 void
2693vars_clear(hashtab_T *ht)
2694{
2695 vars_clear_ext(ht, TRUE);
2696}
2697
2698/*
2699 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2700 */
2701 void
2702vars_clear_ext(hashtab_T *ht, int free_val)
2703{
2704 int todo;
2705 hashitem_T *hi;
2706 dictitem_T *v;
2707
2708 hash_lock(ht);
2709 todo = (int)ht->ht_used;
2710 for (hi = ht->ht_array; todo > 0; ++hi)
2711 {
2712 if (!HASHITEM_EMPTY(hi))
2713 {
2714 --todo;
2715
2716 // Free the variable. Don't remove it from the hashtab,
2717 // ht_array might change then. hash_clear() takes care of it
2718 // later.
2719 v = HI2DI(hi);
2720 if (free_val)
2721 clear_tv(&v->di_tv);
2722 if (v->di_flags & DI_FLAGS_ALLOC)
2723 vim_free(v);
2724 }
2725 }
2726 hash_clear(ht);
2727 ht->ht_used = 0;
2728}
2729
2730/*
2731 * Delete a variable from hashtab "ht" at item "hi".
2732 * Clear the variable value and free the dictitem.
2733 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002734 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002735delete_var(hashtab_T *ht, hashitem_T *hi)
2736{
2737 dictitem_T *di = HI2DI(hi);
2738
2739 hash_remove(ht, hi);
2740 clear_tv(&di->di_tv);
2741 vim_free(di);
2742}
2743
2744/*
2745 * List the value of one internal variable.
2746 */
2747 static void
2748list_one_var(dictitem_T *v, char *prefix, int *first)
2749{
2750 char_u *tofree;
2751 char_u *s;
2752 char_u numbuf[NUMBUFLEN];
2753
2754 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2755 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2756 s == NULL ? (char_u *)"" : s, first);
2757 vim_free(tofree);
2758}
2759
2760 static void
2761list_one_var_a(
2762 char *prefix,
2763 char_u *name,
2764 int type,
2765 char_u *string,
2766 int *first) // when TRUE clear rest of screen and set to FALSE
2767{
2768 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2769 msg_start();
2770 msg_puts(prefix);
2771 if (name != NULL) // "a:" vars don't have a name stored
2772 msg_puts((char *)name);
2773 msg_putchar(' ');
2774 msg_advance(22);
2775 if (type == VAR_NUMBER)
2776 msg_putchar('#');
2777 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2778 msg_putchar('*');
2779 else if (type == VAR_LIST)
2780 {
2781 msg_putchar('[');
2782 if (*string == '[')
2783 ++string;
2784 }
2785 else if (type == VAR_DICT)
2786 {
2787 msg_putchar('{');
2788 if (*string == '{')
2789 ++string;
2790 }
2791 else
2792 msg_putchar(' ');
2793
2794 msg_outtrans(string);
2795
2796 if (type == VAR_FUNC || type == VAR_PARTIAL)
2797 msg_puts("()");
2798 if (*first)
2799 {
2800 msg_clr_eos();
2801 *first = FALSE;
2802 }
2803}
2804
2805/*
2806 * Set variable "name" to value in "tv".
2807 * If the variable already exists, the value is updated.
2808 * Otherwise the variable is created.
2809 */
2810 void
2811set_var(
2812 char_u *name,
2813 typval_T *tv,
2814 int copy) // make copy of value in "tv"
2815{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 set_var_const(name, NULL, tv, copy, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002817}
2818
2819/*
2820 * Set variable "name" to value in "tv".
2821 * If the variable already exists and "is_const" is FALSE the value is updated.
2822 * Otherwise the variable is created.
2823 */
2824 void
2825set_var_const(
2826 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827 type_T *type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002828 typval_T *tv,
2829 int copy, // make copy of value in "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 int flags) // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002831{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002832 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002833 char_u *varname;
2834 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 int is_script_local;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002836
2837 ht = find_var_ht(name, &varname);
2838 if (ht == NULL || *varname == NUL)
2839 {
2840 semsg(_(e_illvar), name);
2841 return;
2842 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 is_script_local = ht == get_script_local_ht();
2844
2845 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002846
2847 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848 if (di == NULL)
2849 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002850
2851 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 && var_check_func_name(name, di == NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002853 return;
2854
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002856 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002858 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859 if (flags & LET_IS_CONST)
2860 {
2861 emsg(_(e_cannot_mod));
2862 return;
2863 }
2864
2865 if (var_check_ro(di->di_flags, name, FALSE)
2866 || var_check_lock(di->di_tv.v_lock, name, FALSE))
2867 return;
2868
2869 if ((flags & LET_NO_COMMAND) == 0
2870 && is_script_local
2871 && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2872 {
2873 semsg(_("E1041: Redefining script item %s"), name);
2874 return;
2875 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002876 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 else
2878 // can only redefine once
2879 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002880
2881 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002882
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002884 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002885 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002886 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002888 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002890 if (copy || tv->v_type != VAR_STRING)
2891 {
2892 char_u *val = tv_get_string(tv);
2893
2894 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002895 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 if (di->di_tv.vval.v_string == NULL)
2897 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002898 }
2899 else
2900 {
2901 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002903 tv->vval.v_string = NULL;
2904 }
2905 return;
2906 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002908 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002910 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002912#ifdef FEAT_SEARCH_EXTRA
2913 else if (STRCMP(varname, "hlsearch") == 0)
2914 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002916 redraw_all_later(SOME_VALID);
2917 }
2918#endif
2919 return;
2920 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002922 {
2923 semsg(_("E963: setting %s to value with wrong type"), name);
2924 return;
2925 }
2926 }
2927
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002929 }
2930 else // add a new variable
2931 {
2932 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002933 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002934 {
2935 semsg(_(e_illvar), name);
2936 return;
2937 }
2938
2939 // Make sure the variable name is valid.
2940 if (!valid_varname(varname))
2941 return;
2942
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
2944 if (di == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002945 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 STRCPY(di->di_key, varname);
2947 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002948 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002950 return;
2951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952 di->di_flags = DI_FLAGS_ALLOC;
2953 if (flags & LET_IS_CONST)
2954 di->di_flags |= DI_FLAGS_LOCK;
2955
2956 if (is_script_local && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2957 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002958 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959
2960 // Store a pointer to the typval_T, so that it can be found by
2961 // index instead of using a hastab lookup.
2962 if (ga_grow(&si->sn_var_vals, 1) == OK)
2963 {
2964 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2965 + si->sn_var_vals.ga_len;
2966 sv->sv_name = di->di_key;
2967 sv->sv_tv = &di->di_tv;
2968 sv->sv_type = type == NULL ? &t_any : type;
2969 sv->sv_const = (flags & LET_IS_CONST);
2970 sv->sv_export = is_export;
2971 ++si->sn_var_vals.ga_len;
2972
2973 // let ex_export() know the export worked.
2974 is_export = FALSE;
2975 }
2976 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002977 }
2978
2979 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002981 else
2982 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 di->di_tv = *tv;
2984 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002985 init_tv(tv);
2986 }
2987
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002988 if (flags & LET_IS_CONST)
2989 di->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002990}
2991
2992/*
2993 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
2994 * Also give an error message.
2995 */
2996 int
2997var_check_ro(int flags, char_u *name, int use_gettext)
2998{
2999 if (flags & DI_FLAGS_RO)
3000 {
3001 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3002 return TRUE;
3003 }
3004 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3005 {
3006 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3007 return TRUE;
3008 }
3009 return FALSE;
3010}
3011
3012/*
3013 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3014 * Also give an error message.
3015 */
3016 int
3017var_check_fixed(int flags, char_u *name, int use_gettext)
3018{
3019 if (flags & DI_FLAGS_FIX)
3020 {
3021 semsg(_("E795: Cannot delete variable %s"),
3022 use_gettext ? (char_u *)_(name) : name);
3023 return TRUE;
3024 }
3025 return FALSE;
3026}
3027
3028/*
3029 * Check if a funcref is assigned to a valid variable name.
3030 * Return TRUE and give an error if not.
3031 */
3032 int
3033var_check_func_name(
3034 char_u *name, // points to start of variable name
3035 int new_var) // TRUE when creating the variable
3036{
3037 // Allow for w: b: s: and t:.
3038 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3039 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3040 ? name[2] : name[0]))
3041 {
3042 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3043 name);
3044 return TRUE;
3045 }
3046 // Don't allow hiding a function. When "v" is not NULL we might be
3047 // assigning another function to the same var, the type is checked
3048 // below.
3049 if (new_var && function_exists(name, FALSE))
3050 {
3051 semsg(_("E705: Variable name conflicts with existing function: %s"),
3052 name);
3053 return TRUE;
3054 }
3055 return FALSE;
3056}
3057
3058/*
3059 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
3060 * Also give an error message, using "name" or _("name") when use_gettext is
3061 * TRUE.
3062 */
3063 int
3064var_check_lock(int lock, char_u *name, int use_gettext)
3065{
3066 if (lock & VAR_LOCKED)
3067 {
3068 semsg(_("E741: Value is locked: %s"),
3069 name == NULL ? (char_u *)_("Unknown")
3070 : use_gettext ? (char_u *)_(name)
3071 : name);
3072 return TRUE;
3073 }
3074 if (lock & VAR_FIXED)
3075 {
3076 semsg(_("E742: Cannot change value of %s"),
3077 name == NULL ? (char_u *)_("Unknown")
3078 : use_gettext ? (char_u *)_(name)
3079 : name);
3080 return TRUE;
3081 }
3082 return FALSE;
3083}
3084
3085/*
3086 * Check if a variable name is valid.
3087 * Return FALSE and give an error if not.
3088 */
3089 int
3090valid_varname(char_u *varname)
3091{
3092 char_u *p;
3093
3094 for (p = varname; *p != NUL; ++p)
3095 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3096 && *p != AUTOLOAD_CHAR)
3097 {
3098 semsg(_(e_illvar), varname);
3099 return FALSE;
3100 }
3101 return TRUE;
3102}
3103
3104/*
3105 * getwinvar() and gettabwinvar()
3106 */
3107 static void
3108getwinvar(
3109 typval_T *argvars,
3110 typval_T *rettv,
3111 int off) // 1 for gettabwinvar()
3112{
3113 win_T *win;
3114 char_u *varname;
3115 dictitem_T *v;
3116 tabpage_T *tp = NULL;
3117 int done = FALSE;
3118 win_T *oldcurwin;
3119 tabpage_T *oldtabpage;
3120 int need_switch_win;
3121
3122 if (off == 1)
3123 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3124 else
3125 tp = curtab;
3126 win = find_win_by_nr(&argvars[off], tp);
3127 varname = tv_get_string_chk(&argvars[off + 1]);
3128 ++emsg_off;
3129
3130 rettv->v_type = VAR_STRING;
3131 rettv->vval.v_string = NULL;
3132
3133 if (win != NULL && varname != NULL)
3134 {
3135 // Set curwin to be our win, temporarily. Also set the tabpage,
3136 // otherwise the window is not valid. Only do this when needed,
3137 // autocommands get blocked.
3138 need_switch_win = !(tp == curtab && win == curwin);
3139 if (!need_switch_win
3140 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3141 {
3142 if (*varname == '&')
3143 {
3144 if (varname[1] == NUL)
3145 {
3146 // get all window-local options in a dict
3147 dict_T *opts = get_winbuf_options(FALSE);
3148
3149 if (opts != NULL)
3150 {
3151 rettv_dict_set(rettv, opts);
3152 done = TRUE;
3153 }
3154 }
3155 else if (get_option_tv(&varname, rettv, 1) == OK)
3156 // window-local-option
3157 done = TRUE;
3158 }
3159 else
3160 {
3161 // Look up the variable.
3162 // Let getwinvar({nr}, "") return the "w:" dictionary.
3163 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3164 varname, FALSE);
3165 if (v != NULL)
3166 {
3167 copy_tv(&v->di_tv, rettv);
3168 done = TRUE;
3169 }
3170 }
3171 }
3172
3173 if (need_switch_win)
3174 // restore previous notion of curwin
3175 restore_win(oldcurwin, oldtabpage, TRUE);
3176 }
3177
3178 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3179 // use the default return value
3180 copy_tv(&argvars[off + 2], rettv);
3181
3182 --emsg_off;
3183}
3184
3185/*
3186 * "setwinvar()" and "settabwinvar()" functions
3187 */
3188 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003189setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003190{
3191 win_T *win;
3192 win_T *save_curwin;
3193 tabpage_T *save_curtab;
3194 int need_switch_win;
3195 char_u *varname, *winvarname;
3196 typval_T *varp;
3197 char_u nbuf[NUMBUFLEN];
3198 tabpage_T *tp = NULL;
3199
3200 if (check_secure())
3201 return;
3202
3203 if (off == 1)
3204 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3205 else
3206 tp = curtab;
3207 win = find_win_by_nr(&argvars[off], tp);
3208 varname = tv_get_string_chk(&argvars[off + 1]);
3209 varp = &argvars[off + 2];
3210
3211 if (win != NULL && varname != NULL && varp != NULL)
3212 {
3213 need_switch_win = !(tp == curtab && win == curwin);
3214 if (!need_switch_win
3215 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3216 {
3217 if (*varname == '&')
3218 {
3219 long numval;
3220 char_u *strval;
3221 int error = FALSE;
3222
3223 ++varname;
3224 numval = (long)tv_get_number_chk(varp, &error);
3225 strval = tv_get_string_buf_chk(varp, nbuf);
3226 if (!error && strval != NULL)
3227 set_option_value(varname, numval, strval, OPT_LOCAL);
3228 }
3229 else
3230 {
3231 winvarname = alloc(STRLEN(varname) + 3);
3232 if (winvarname != NULL)
3233 {
3234 STRCPY(winvarname, "w:");
3235 STRCPY(winvarname + 2, varname);
3236 set_var(winvarname, varp, TRUE);
3237 vim_free(winvarname);
3238 }
3239 }
3240 }
3241 if (need_switch_win)
3242 restore_win(save_curwin, save_curtab, TRUE);
3243 }
3244}
3245
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003246/*
3247 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3248 * v:option_type, and v:option_command.
3249 */
3250 void
3251reset_v_option_vars(void)
3252{
3253 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3254 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3255 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3256 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3257 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3258 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3259}
3260
3261/*
3262 * Add an assert error to v:errors.
3263 */
3264 void
3265assert_error(garray_T *gap)
3266{
3267 struct vimvar *vp = &vimvars[VV_ERRORS];
3268
3269 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003270 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003271 set_vim_var_list(VV_ERRORS, list_alloc());
3272 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3273}
3274
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003275 int
3276var_exists(char_u *var)
3277{
3278 char_u *name;
3279 char_u *tofree;
3280 typval_T tv;
3281 int len = 0;
3282 int n = FALSE;
3283
3284 // get_name_len() takes care of expanding curly braces
3285 name = var;
3286 len = get_name_len(&var, &tofree, TRUE, FALSE);
3287 if (len > 0)
3288 {
3289 if (tofree != NULL)
3290 name = tofree;
3291 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
3292 if (n)
3293 {
3294 // handle d.key, l[idx], f(expr)
Bram Moolenaar32e35112020-05-14 22:41:15 +02003295 n = (handle_subscript(&var, &tv, EVAL_EVALUATE,
3296 FALSE, name, &name) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003297 if (n)
3298 clear_tv(&tv);
3299 }
3300 }
3301 if (*var != NUL)
3302 n = FALSE;
3303
3304 vim_free(tofree);
3305 return n;
3306}
3307
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003308static lval_T *redir_lval = NULL;
3309#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3310static garray_T redir_ga; // only valid when redir_lval is not NULL
3311static char_u *redir_endp = NULL;
3312static char_u *redir_varname = NULL;
3313
3314/*
3315 * Start recording command output to a variable
3316 * When "append" is TRUE append to an existing variable.
3317 * Returns OK if successfully completed the setup. FAIL otherwise.
3318 */
3319 int
3320var_redir_start(char_u *name, int append)
3321{
3322 int save_emsg;
3323 int err;
3324 typval_T tv;
3325
3326 // Catch a bad name early.
3327 if (!eval_isnamec1(*name))
3328 {
3329 emsg(_(e_invarg));
3330 return FAIL;
3331 }
3332
3333 // Make a copy of the name, it is used in redir_lval until redir ends.
3334 redir_varname = vim_strsave(name);
3335 if (redir_varname == NULL)
3336 return FAIL;
3337
3338 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3339 if (redir_lval == NULL)
3340 {
3341 var_redir_stop();
3342 return FAIL;
3343 }
3344
3345 // The output is stored in growarray "redir_ga" until redirection ends.
3346 ga_init2(&redir_ga, (int)sizeof(char), 500);
3347
3348 // Parse the variable name (can be a dict or list entry).
3349 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3350 FNE_CHECK_START);
3351 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3352 {
3353 clear_lval(redir_lval);
3354 if (redir_endp != NULL && *redir_endp != NUL)
3355 // Trailing characters are present after the variable name
3356 emsg(_(e_trailing));
3357 else
3358 emsg(_(e_invarg));
3359 redir_endp = NULL; // don't store a value, only cleanup
3360 var_redir_stop();
3361 return FAIL;
3362 }
3363
3364 // check if we can write to the variable: set it to or append an empty
3365 // string
3366 save_emsg = did_emsg;
3367 did_emsg = FALSE;
3368 tv.v_type = VAR_STRING;
3369 tv.vval.v_string = (char_u *)"";
3370 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003372 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003374 clear_lval(redir_lval);
3375 err = did_emsg;
3376 did_emsg |= save_emsg;
3377 if (err)
3378 {
3379 redir_endp = NULL; // don't store a value, only cleanup
3380 var_redir_stop();
3381 return FAIL;
3382 }
3383
3384 return OK;
3385}
3386
3387/*
3388 * Append "value[value_len]" to the variable set by var_redir_start().
3389 * The actual appending is postponed until redirection ends, because the value
3390 * appended may in fact be the string we write to, changing it may cause freed
3391 * memory to be used:
3392 * :redir => foo
3393 * :let foo
3394 * :redir END
3395 */
3396 void
3397var_redir_str(char_u *value, int value_len)
3398{
3399 int len;
3400
3401 if (redir_lval == NULL)
3402 return;
3403
3404 if (value_len == -1)
3405 len = (int)STRLEN(value); // Append the entire string
3406 else
3407 len = value_len; // Append only "value_len" characters
3408
3409 if (ga_grow(&redir_ga, len) == OK)
3410 {
3411 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3412 redir_ga.ga_len += len;
3413 }
3414 else
3415 var_redir_stop();
3416}
3417
3418/*
3419 * Stop redirecting command output to a variable.
3420 * Frees the allocated memory.
3421 */
3422 void
3423var_redir_stop(void)
3424{
3425 typval_T tv;
3426
3427 if (EVALCMD_BUSY)
3428 {
3429 redir_lval = NULL;
3430 return;
3431 }
3432
3433 if (redir_lval != NULL)
3434 {
3435 // If there was no error: assign the text to the variable.
3436 if (redir_endp != NULL)
3437 {
3438 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3439 tv.v_type = VAR_STRING;
3440 tv.vval.v_string = redir_ga.ga_data;
3441 // Call get_lval() again, if it's inside a Dict or List it may
3442 // have changed.
3443 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3444 FALSE, FALSE, 0, FNE_CHECK_START);
3445 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003447 (char_u *)".");
3448 clear_lval(redir_lval);
3449 }
3450
3451 // free the collected output
3452 VIM_CLEAR(redir_ga.ga_data);
3453
3454 VIM_CLEAR(redir_lval);
3455 }
3456 VIM_CLEAR(redir_varname);
3457}
3458
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003459/*
3460 * "gettabvar()" function
3461 */
3462 void
3463f_gettabvar(typval_T *argvars, typval_T *rettv)
3464{
3465 win_T *oldcurwin;
3466 tabpage_T *tp, *oldtabpage;
3467 dictitem_T *v;
3468 char_u *varname;
3469 int done = FALSE;
3470
3471 rettv->v_type = VAR_STRING;
3472 rettv->vval.v_string = NULL;
3473
3474 varname = tv_get_string_chk(&argvars[1]);
3475 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3476 if (tp != NULL && varname != NULL)
3477 {
3478 // Set tp to be our tabpage, temporarily. Also set the window to the
3479 // first window in the tabpage, otherwise the window is not valid.
3480 if (switch_win(&oldcurwin, &oldtabpage,
3481 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3482 : tp->tp_firstwin, tp, TRUE) == OK)
3483 {
3484 // look up the variable
3485 // Let gettabvar({nr}, "") return the "t:" dictionary.
3486 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3487 if (v != NULL)
3488 {
3489 copy_tv(&v->di_tv, rettv);
3490 done = TRUE;
3491 }
3492 }
3493
3494 // restore previous notion of curwin
3495 restore_win(oldcurwin, oldtabpage, TRUE);
3496 }
3497
3498 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3499 copy_tv(&argvars[2], rettv);
3500}
3501
3502/*
3503 * "gettabwinvar()" function
3504 */
3505 void
3506f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3507{
3508 getwinvar(argvars, rettv, 1);
3509}
3510
3511/*
3512 * "getwinvar()" function
3513 */
3514 void
3515f_getwinvar(typval_T *argvars, typval_T *rettv)
3516{
3517 getwinvar(argvars, rettv, 0);
3518}
3519
3520/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003521 * "getbufvar()" function
3522 */
3523 void
3524f_getbufvar(typval_T *argvars, typval_T *rettv)
3525{
3526 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003527 char_u *varname;
3528 dictitem_T *v;
3529 int done = FALSE;
3530
3531 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3532 varname = tv_get_string_chk(&argvars[1]);
3533 ++emsg_off;
3534 buf = tv_get_buf(&argvars[0], FALSE);
3535
3536 rettv->v_type = VAR_STRING;
3537 rettv->vval.v_string = NULL;
3538
3539 if (buf != NULL && varname != NULL)
3540 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003541 if (*varname == '&')
3542 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003543 buf_T *save_curbuf = curbuf;
3544
3545 // set curbuf to be our buf, temporarily
3546 curbuf = buf;
3547
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003548 if (varname[1] == NUL)
3549 {
3550 // get all buffer-local options in a dict
3551 dict_T *opts = get_winbuf_options(TRUE);
3552
3553 if (opts != NULL)
3554 {
3555 rettv_dict_set(rettv, opts);
3556 done = TRUE;
3557 }
3558 }
3559 else if (get_option_tv(&varname, rettv, TRUE) == OK)
3560 // buffer-local-option
3561 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003562
3563 // restore previous notion of curbuf
3564 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003565 }
3566 else
3567 {
3568 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003569 if (*varname == NUL)
3570 // Let getbufvar({nr}, "") return the "b:" dictionary.
3571 v = &buf->b_bufvar;
3572 else
3573 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3574 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003575 if (v != NULL)
3576 {
3577 copy_tv(&v->di_tv, rettv);
3578 done = TRUE;
3579 }
3580 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003581 }
3582
3583 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3584 // use the default value
3585 copy_tv(&argvars[2], rettv);
3586
3587 --emsg_off;
3588}
3589
3590/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003591 * "settabvar()" function
3592 */
3593 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003594f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003595{
3596 tabpage_T *save_curtab;
3597 tabpage_T *tp;
3598 char_u *varname, *tabvarname;
3599 typval_T *varp;
3600
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003601 if (check_secure())
3602 return;
3603
3604 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3605 varname = tv_get_string_chk(&argvars[1]);
3606 varp = &argvars[2];
3607
3608 if (varname != NULL && varp != NULL && tp != NULL)
3609 {
3610 save_curtab = curtab;
3611 goto_tabpage_tp(tp, FALSE, FALSE);
3612
3613 tabvarname = alloc(STRLEN(varname) + 3);
3614 if (tabvarname != NULL)
3615 {
3616 STRCPY(tabvarname, "t:");
3617 STRCPY(tabvarname + 2, varname);
3618 set_var(tabvarname, varp, TRUE);
3619 vim_free(tabvarname);
3620 }
3621
3622 // Restore current tabpage
3623 if (valid_tabpage(save_curtab))
3624 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3625 }
3626}
3627
3628/*
3629 * "settabwinvar()" function
3630 */
3631 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003632f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003633{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003634 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003635}
3636
3637/*
3638 * "setwinvar()" function
3639 */
3640 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003641f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003642{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003643 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003644}
3645
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003646/*
3647 * "setbufvar()" function
3648 */
3649 void
3650f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3651{
3652 buf_T *buf;
3653 char_u *varname, *bufvarname;
3654 typval_T *varp;
3655 char_u nbuf[NUMBUFLEN];
3656
3657 if (check_secure())
3658 return;
3659 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3660 varname = tv_get_string_chk(&argvars[1]);
3661 buf = tv_get_buf(&argvars[0], FALSE);
3662 varp = &argvars[2];
3663
3664 if (buf != NULL && varname != NULL && varp != NULL)
3665 {
3666 if (*varname == '&')
3667 {
3668 long numval;
3669 char_u *strval;
3670 int error = FALSE;
3671 aco_save_T aco;
3672
3673 // set curbuf to be our buf, temporarily
3674 aucmd_prepbuf(&aco, buf);
3675
3676 ++varname;
3677 numval = (long)tv_get_number_chk(varp, &error);
3678 strval = tv_get_string_buf_chk(varp, nbuf);
3679 if (!error && strval != NULL)
3680 set_option_value(varname, numval, strval, OPT_LOCAL);
3681
3682 // reset notion of buffer
3683 aucmd_restbuf(&aco);
3684 }
3685 else
3686 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003687 bufvarname = alloc(STRLEN(varname) + 3);
3688 if (bufvarname != NULL)
3689 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003690 buf_T *save_curbuf = curbuf;
3691
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003692 curbuf = buf;
3693 STRCPY(bufvarname, "b:");
3694 STRCPY(bufvarname + 2, varname);
3695 set_var(bufvarname, varp, TRUE);
3696 vim_free(bufvarname);
3697 curbuf = save_curbuf;
3698 }
3699 }
3700 }
3701}
3702
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003703/*
3704 * Get a callback from "arg". It can be a Funcref or a function name.
3705 * When "arg" is zero return an empty string.
3706 * "cb_name" is not allocated.
3707 * "cb_name" is set to NULL for an invalid argument.
3708 */
3709 callback_T
3710get_callback(typval_T *arg)
3711{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003712 callback_T res;
3713 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003714
3715 res.cb_free_name = FALSE;
3716 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3717 {
3718 res.cb_partial = arg->vval.v_partial;
3719 ++res.cb_partial->pt_refcount;
3720 res.cb_name = partial_name(res.cb_partial);
3721 }
3722 else
3723 {
3724 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003725 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3726 && isdigit(*arg->vval.v_string))
3727 r = FAIL;
3728 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003729 {
3730 // Note that we don't make a copy of the string.
3731 res.cb_name = arg->vval.v_string;
3732 func_ref(res.cb_name);
3733 }
3734 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003735 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003736 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003737 r = FAIL;
3738
3739 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003740 {
3741 emsg(_("E921: Invalid callback argument"));
3742 res.cb_name = NULL;
3743 }
3744 }
3745 return res;
3746}
3747
3748/*
3749 * Copy a callback into a typval_T.
3750 */
3751 void
3752put_callback(callback_T *cb, typval_T *tv)
3753{
3754 if (cb->cb_partial != NULL)
3755 {
3756 tv->v_type = VAR_PARTIAL;
3757 tv->vval.v_partial = cb->cb_partial;
3758 ++tv->vval.v_partial->pt_refcount;
3759 }
3760 else
3761 {
3762 tv->v_type = VAR_FUNC;
3763 tv->vval.v_string = vim_strsave(cb->cb_name);
3764 func_ref(cb->cb_name);
3765 }
3766}
3767
3768/*
3769 * Make a copy of "src" into "dest", allocating the function name if needed,
3770 * without incrementing the refcount.
3771 */
3772 void
3773set_callback(callback_T *dest, callback_T *src)
3774{
3775 if (src->cb_partial == NULL)
3776 {
3777 // just a function name, make a copy
3778 dest->cb_name = vim_strsave(src->cb_name);
3779 dest->cb_free_name = TRUE;
3780 }
3781 else
3782 {
3783 // cb_name is a pointer into cb_partial
3784 dest->cb_name = src->cb_name;
3785 dest->cb_free_name = FALSE;
3786 }
3787 dest->cb_partial = src->cb_partial;
3788}
3789
3790/*
3791 * Unref/free "callback" returned by get_callback() or set_callback().
3792 */
3793 void
3794free_callback(callback_T *callback)
3795{
3796 if (callback->cb_partial != NULL)
3797 {
3798 partial_unref(callback->cb_partial);
3799 callback->cb_partial = NULL;
3800 }
3801 else if (callback->cb_name != NULL)
3802 func_unref(callback->cb_name);
3803 if (callback->cb_free_name)
3804 {
3805 vim_free(callback->cb_name);
3806 callback->cb_free_name = FALSE;
3807 }
3808 callback->cb_name = NULL;
3809}
3810
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003811#endif // FEAT_EVAL