blob: 2455046d93ab00abeaec5fb965f887264ea3805c [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 Moolenaar84cf6bd2020-06-16 20:03:43 +0200148 {VV_NAME("collate", VAR_STRING), VV_RO},
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200149 {VV_NAME("disallow_let", VAR_NUMBER), 0}, // TODO: remove
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200150};
151
152// shorthand
153#define vv_type vv_di.di_tv.v_type
154#define vv_nr vv_di.di_tv.vval.v_number
155#define vv_float vv_di.di_tv.vval.v_float
156#define vv_str vv_di.di_tv.vval.v_string
157#define vv_list vv_di.di_tv.vval.v_list
158#define vv_dict vv_di.di_tv.vval.v_dict
159#define vv_blob vv_di.di_tv.vval.v_blob
160#define vv_tv vv_di.di_tv
161
162static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200163static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200164#define vimvarht vimvardict.dv_hashtab
165
166// for VIM_VERSION_ defines
167#include "version.h"
168
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 list_one_var(dictitem_T *v, char *prefix, int *first);
178static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
179
180/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200181 * Initialize global and vim special variables
182 */
183 void
184evalvars_init(void)
185{
186 int i;
187 struct vimvar *p;
188
189 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
190 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
191 vimvardict.dv_lock = VAR_FIXED;
192 hash_init(&compat_hashtab);
193
194 for (i = 0; i < VV_LEN; ++i)
195 {
196 p = &vimvars[i];
197 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
198 {
199 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
200 getout(1);
201 }
202 STRCPY(p->vv_di.di_key, p->vv_name);
203 if (p->vv_flags & VV_RO)
204 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
205 else if (p->vv_flags & VV_RO_SBX)
206 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
207 else
208 p->vv_di.di_flags = DI_FLAGS_FIX;
209
210 // add to v: scope dict, unless the value is not always available
211 if (p->vv_type != VAR_UNKNOWN)
212 hash_add(&vimvarht, p->vv_di.di_key);
213 if (p->vv_flags & VV_COMPAT)
214 // add to compat scope dict
215 hash_add(&compat_hashtab, p->vv_di.di_key);
216 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200217 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
218 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200219
220 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
221 set_vim_var_nr(VV_HLSEARCH, 1L);
222 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
223 set_vim_var_list(VV_ERRORS, list_alloc());
224 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
225
226 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
227 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
228 set_vim_var_nr(VV_NONE, VVAL_NONE);
229 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100230 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200231
232 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
233 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
234 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
235 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
236 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
237 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
238 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
239 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
240 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
241 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
242 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
243
244 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
245
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200246 // TODO: remove later
247 set_vim_var_nr(VV_DISALLOW_LET, 1);
248
Bram Moolenaar439c0362020-06-06 15:58:03 +0200249 // Default for v:register is not 0 but '"'. This is adjusted once the
250 // clipboard has been setup by calling reset_reg_var().
251 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200252}
253
254#if defined(EXITFREE) || defined(PROTO)
255/*
256 * Free all vim variables information on exit
257 */
258 void
259evalvars_clear(void)
260{
261 int i;
262 struct vimvar *p;
263
264 for (i = 0; i < VV_LEN; ++i)
265 {
266 p = &vimvars[i];
267 if (p->vv_di.di_tv.v_type == VAR_STRING)
268 VIM_CLEAR(p->vv_str);
269 else if (p->vv_di.di_tv.v_type == VAR_LIST)
270 {
271 list_unref(p->vv_list);
272 p->vv_list = NULL;
273 }
274 }
275 hash_clear(&vimvarht);
276 hash_init(&vimvarht); // garbage_collect() will access it
277 hash_clear(&compat_hashtab);
278
279 // global variables
280 vars_clear(&globvarht);
281
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100282 // Script-local variables. Clear all the variables here.
283 // The scriptvar_T is cleared later in free_scriptnames(), because a
284 // variable in one script might hold a reference to the whole scope of
285 // another script.
286 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200287 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200288}
289#endif
290
291 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200292garbage_collect_globvars(int copyID)
293{
294 return set_ref_in_ht(&globvarht, copyID, NULL);
295}
296
297 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200298garbage_collect_vimvars(int copyID)
299{
300 return set_ref_in_ht(&vimvarht, copyID, NULL);
301}
302
303 int
304garbage_collect_scriptvars(int copyID)
305{
306 int i;
307 int abort = FALSE;
308
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100309 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200310 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
311
312 return abort;
313}
314
315/*
316 * Set an internal variable to a string value. Creates the variable if it does
317 * not already exist.
318 */
319 void
320set_internal_string_var(char_u *name, char_u *value)
321{
322 char_u *val;
323 typval_T *tvp;
324
325 val = vim_strsave(value);
326 if (val != NULL)
327 {
328 tvp = alloc_string_tv(val);
329 if (tvp != NULL)
330 {
331 set_var(name, tvp, FALSE);
332 free_tv(tvp);
333 }
334 }
335}
336
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200337 int
338eval_charconvert(
339 char_u *enc_from,
340 char_u *enc_to,
341 char_u *fname_from,
342 char_u *fname_to)
343{
344 int err = FALSE;
345
346 set_vim_var_string(VV_CC_FROM, enc_from, -1);
347 set_vim_var_string(VV_CC_TO, enc_to, -1);
348 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
349 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
350 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
351 err = TRUE;
352 set_vim_var_string(VV_CC_FROM, NULL, -1);
353 set_vim_var_string(VV_CC_TO, NULL, -1);
354 set_vim_var_string(VV_FNAME_IN, NULL, -1);
355 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
356
357 if (err)
358 return FAIL;
359 return OK;
360}
361
362# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
363 int
364eval_printexpr(char_u *fname, char_u *args)
365{
366 int err = FALSE;
367
368 set_vim_var_string(VV_FNAME_IN, fname, -1);
369 set_vim_var_string(VV_CMDARG, args, -1);
370 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
371 err = TRUE;
372 set_vim_var_string(VV_FNAME_IN, NULL, -1);
373 set_vim_var_string(VV_CMDARG, NULL, -1);
374
375 if (err)
376 {
377 mch_remove(fname);
378 return FAIL;
379 }
380 return OK;
381}
382# endif
383
384# if defined(FEAT_DIFF) || defined(PROTO)
385 void
386eval_diff(
387 char_u *origfile,
388 char_u *newfile,
389 char_u *outfile)
390{
391 int err = FALSE;
392
393 set_vim_var_string(VV_FNAME_IN, origfile, -1);
394 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
395 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
396 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
397 set_vim_var_string(VV_FNAME_IN, NULL, -1);
398 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
399 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
400}
401
402 void
403eval_patch(
404 char_u *origfile,
405 char_u *difffile,
406 char_u *outfile)
407{
408 int err;
409
410 set_vim_var_string(VV_FNAME_IN, origfile, -1);
411 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
412 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
413 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
414 set_vim_var_string(VV_FNAME_IN, NULL, -1);
415 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
416 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
417}
418# endif
419
420#if defined(FEAT_SPELL) || defined(PROTO)
421/*
422 * Evaluate an expression to a list with suggestions.
423 * For the "expr:" part of 'spellsuggest'.
424 * Returns NULL when there is an error.
425 */
426 list_T *
427eval_spell_expr(char_u *badword, char_u *expr)
428{
429 typval_T save_val;
430 typval_T rettv;
431 list_T *list = NULL;
432 char_u *p = skipwhite(expr);
433
434 // Set "v:val" to the bad word.
435 prepare_vimvar(VV_VAL, &save_val);
436 set_vim_var_string(VV_VAL, badword, -1);
437 if (p_verbose == 0)
438 ++emsg_off;
439
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200440 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200441 {
442 if (rettv.v_type != VAR_LIST)
443 clear_tv(&rettv);
444 else
445 list = rettv.vval.v_list;
446 }
447
448 if (p_verbose == 0)
449 --emsg_off;
450 clear_tv(get_vim_var_tv(VV_VAL));
451 restore_vimvar(VV_VAL, &save_val);
452
453 return list;
454}
455
456/*
457 * "list" is supposed to contain two items: a word and a number. Return the
458 * word in "pp" and the number as the return value.
459 * Return -1 if anything isn't right.
460 * Used to get the good word and score from the eval_spell_expr() result.
461 */
462 int
463get_spellword(list_T *list, char_u **pp)
464{
465 listitem_T *li;
466
467 li = list->lv_first;
468 if (li == NULL)
469 return -1;
470 *pp = tv_get_string(&li->li_tv);
471
472 li = li->li_next;
473 if (li == NULL)
474 return -1;
475 return (int)tv_get_number(&li->li_tv);
476}
477#endif
478
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200479/*
480 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200481 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200482 * When not used yet add the variable to the v: hashtable.
483 */
484 void
485prepare_vimvar(int idx, typval_T *save_tv)
486{
487 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200488 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200489 if (vimvars[idx].vv_type == VAR_UNKNOWN)
490 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
491}
492
493/*
494 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200495 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200496 * When no longer defined, remove the variable from the v: hashtable.
497 */
498 void
499restore_vimvar(int idx, typval_T *save_tv)
500{
501 hashitem_T *hi;
502
503 vimvars[idx].vv_tv = *save_tv;
504 if (vimvars[idx].vv_type == VAR_UNKNOWN)
505 {
506 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
507 if (HASHITEM_EMPTY(hi))
508 internal_error("restore_vimvar()");
509 else
510 hash_remove(&vimvarht, hi);
511 }
512}
513
514/*
515 * List Vim variables.
516 */
517 static void
518list_vim_vars(int *first)
519{
520 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
521}
522
523/*
524 * List script-local variables, if there is a script.
525 */
526 static void
527list_script_vars(int *first)
528{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200529 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200530 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
531 "s:", FALSE, first);
532}
533
534/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200535 * Get a list of lines from a HERE document. The here document is a list of
536 * lines surrounded by a marker.
537 * cmd << {marker}
538 * {line1}
539 * {line2}
540 * ....
541 * {marker}
542 *
543 * The {marker} is a string. If the optional 'trim' word is supplied before the
544 * marker, then the leading indentation before the lines (matching the
545 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200546 *
547 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
548 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
549 * missing, then '.' is accepted as a marker.
550 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200551 * Returns a List with {lines} or NULL.
552 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100553 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200554heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200555{
556 char_u *theline;
557 char_u *marker;
558 list_T *l;
559 char_u *p;
560 int marker_indent_len = 0;
561 int text_indent_len = 0;
562 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200563 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200564 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200565
566 if (eap->getline == NULL)
567 {
568 emsg(_("E991: cannot use =<< here"));
569 return NULL;
570 }
571
572 // Check for the optional 'trim' word before the marker
573 cmd = skipwhite(cmd);
574 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
575 {
576 cmd = skipwhite(cmd + 4);
577
578 // Trim the indentation from all the lines in the here document.
579 // The amount of indentation trimmed is the same as the indentation of
580 // the first line after the :let command line. To find the end marker
581 // the indent of the :let command line is trimmed.
582 p = *eap->cmdlinep;
583 while (VIM_ISWHITE(*p))
584 {
585 p++;
586 marker_indent_len++;
587 }
588 text_indent_len = -1;
589 }
590
591 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200592 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200593 {
594 marker = skipwhite(cmd);
595 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200596 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200597 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200598 semsg(_(e_trailing_arg), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200599 return NULL;
600 }
601 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200602 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200603 {
604 emsg(_("E221: Marker cannot start with lower case letter"));
605 return NULL;
606 }
607 }
608 else
609 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200610 // When getting lines for an embedded script, if the marker is missing,
611 // accept '.' as the marker.
612 if (script_get)
613 marker = dot;
614 else
615 {
616 emsg(_("E172: Missing marker"));
617 return NULL;
618 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200619 }
620
621 l = list_alloc();
622 if (l == NULL)
623 return NULL;
624
625 for (;;)
626 {
627 int mi = 0;
628 int ti = 0;
629
630 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
631 if (theline == NULL)
632 {
633 semsg(_("E990: Missing end marker '%s'"), marker);
634 break;
635 }
636
637 // with "trim": skip the indent matching the :let line to find the
638 // marker
639 if (marker_indent_len > 0
640 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
641 mi = marker_indent_len;
642 if (STRCMP(marker, theline + mi) == 0)
643 {
644 vim_free(theline);
645 break;
646 }
647
648 if (text_indent_len == -1 && *theline != NUL)
649 {
650 // set the text indent from the first line.
651 p = theline;
652 text_indent_len = 0;
653 while (VIM_ISWHITE(*p))
654 {
655 p++;
656 text_indent_len++;
657 }
658 text_indent = vim_strnsave(theline, text_indent_len);
659 }
660 // with "trim": skip the indent matching the first line
661 if (text_indent != NULL)
662 for (ti = 0; ti < text_indent_len; ++ti)
663 if (theline[ti] != text_indent[ti])
664 break;
665
666 if (list_append_string(l, theline + ti, -1) == FAIL)
667 break;
668 vim_free(theline);
669 }
670 vim_free(text_indent);
671
672 return l;
673}
674
675/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200676 * Vim9 variable declaration:
677 * ":var name"
678 * ":var name: type"
679 * ":var name = expr"
680 * ":var name: type = expr"
681 * etc.
682 */
683 void
684ex_var(exarg_T *eap)
685{
686 if (!in_vim9script())
687 {
688 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
689 return;
690 }
691 ex_let(eap);
692}
693
694/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200695 * ":let" list all variable values
696 * ":let var1 var2" list variable values
697 * ":let var = expr" assignment command.
698 * ":let var += expr" assignment command.
699 * ":let var -= expr" assignment command.
700 * ":let var *= expr" assignment command.
701 * ":let var /= expr" assignment command.
702 * ":let var %= expr" assignment command.
703 * ":let var .= expr" assignment command.
704 * ":let var ..= expr" assignment command.
705 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100707 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200708 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200709 * ":final var = expr" assignment command.
710 * ":final [var1, var2] = expr" unpack list.
711 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200712 * ":const" list all variable values
713 * ":const var1 var2" list variable values
714 * ":const var = expr" assignment command.
715 * ":const [var1, var2] = expr" unpack list.
716 */
717 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200718ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200719{
720 char_u *arg = eap->arg;
721 char_u *expr = NULL;
722 typval_T rettv;
723 int i;
724 int var_count = 0;
725 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200726 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200727 char_u *argend;
728 int first = TRUE;
729 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200730 int has_assign;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200731 int flags = eap->cmdidx == CMD_const ? ASSIGN_CONST : 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200732 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200733
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200734 if (eap->cmdidx == CMD_final && !vim9script)
735 {
736 // In legacy Vim script ":final" is short for ":finally".
737 ex_finally(eap);
738 return;
739 }
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200740 if (get_vim_var_nr(VV_DISALLOW_LET)
741 && eap->cmdidx == CMD_let && vim9script)
742 {
743 emsg(_(e_cannot_use_let_in_vim9_script));
744 return;
745 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200746 if (eap->cmdidx == CMD_const && !vim9script && !eap->forceit)
747 // In legacy Vim script ":const" works like ":final".
748 eap->cmdidx = CMD_final;
749
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750 // detect Vim9 assignment without ":let" or ":const"
751 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200752 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200754 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200755 if (argend == NULL)
756 return;
757 if (argend > arg && argend[-1] == '.') // for var.='str'
758 --argend;
759 expr = skipwhite(argend);
760 concat = expr[0] == '.'
761 && ((expr[1] == '=' && current_sctx.sc_version < 2)
762 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200763 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
764 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200765 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200766 {
767 // ":let" without "=": list variables
768 if (*arg == '[')
769 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200770 else if (expr[0] == '.' && expr[1] == '=')
771 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200772 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200773 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200774 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200775 {
776 // Vim9 declaration ":let var: type"
777 arg = vim9_declare_scriptvar(eap, arg);
778 }
779 else
780 {
781 // ":let var1 var2" - list values
782 arg = list_arg_vars(eap, arg, &first);
783 }
784 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200785 else if (!eap->skip)
786 {
787 // ":let"
788 list_glob_vars(&first);
789 list_buf_vars(&first);
790 list_win_vars(&first);
791 list_tab_vars(&first);
792 list_script_vars(&first);
793 list_func_vars(&first);
794 list_vim_vars(&first);
795 }
796 eap->nextcmd = check_nextcmd(arg);
797 }
798 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
799 {
800 list_T *l;
801
802 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200803 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200804 if (l != NULL)
805 {
806 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200807 if (!eap->skip)
808 {
809 op[0] = '=';
810 op[1] = NUL;
811 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100812 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200813 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200814 clear_tv(&rettv);
815 }
816 }
817 else
818 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200819 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200820 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200821
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200822 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200823 i = FAIL;
824 if (has_assign || concat)
825 {
826 op[0] = '=';
827 op[1] = NUL;
828 if (*expr != '=')
829 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200830 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200831 {
832 // +=, /=, etc. require an existing variable
833 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
834 i = FAIL;
835 }
836 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200837 {
838 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200839 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200840 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200841 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200842 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200843 ++len;
844 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200845 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200846 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200847 }
848 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200849 ++expr;
850
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200851 if (vim9script && (!VIM_ISWHITE(*argend)
852 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200853 {
854 vim_strncpy(op, expr - len, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200855 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200856 i = FAIL;
857 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200858
859 if (eap->skip)
860 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200861 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200862 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200863 if (getline_equal(eap->getline, eap->cookie, getsourceline))
864 {
865 evalarg.eval_getline = eap->getline;
866 evalarg.eval_cookie = eap->cookie;
867 }
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200868 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200869 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200870 if (eap->skip)
871 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200872 clear_evalarg(&evalarg, eap);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200873 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200874 if (eap->skip)
875 {
876 if (i != FAIL)
877 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200878 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200879 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200880 {
881 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200882 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200883 clear_tv(&rettv);
884 }
885 }
886}
887
888/*
889 * Assign the typevalue "tv" to the variable or variables at "arg_start".
890 * Handles both "var" with any type and "[var, var; var]" with a list type.
891 * When "op" is not NULL it points to a string with characters that
892 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
893 * or concatenate.
894 * Returns OK or FAIL;
895 */
896 int
897ex_let_vars(
898 char_u *arg_start,
899 typval_T *tv,
900 int copy, // copy values from "tv", don't move
901 int semicolon, // from skip_var_list()
902 int var_count, // from skip_var_list()
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200903 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200904 char_u *op)
905{
906 char_u *arg = arg_start;
907 list_T *l;
908 int i;
909 listitem_T *item;
910 typval_T ltv;
911
912 if (*arg != '[')
913 {
914 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100915 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200916 return FAIL;
917 return OK;
918 }
919
920 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
921 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
922 {
923 emsg(_(e_listreq));
924 return FAIL;
925 }
926
927 i = list_len(l);
928 if (semicolon == 0 && var_count < i)
929 {
930 emsg(_("E687: Less targets than List items"));
931 return FAIL;
932 }
933 if (var_count - semicolon > i)
934 {
935 emsg(_("E688: More targets than List items"));
936 return FAIL;
937 }
938
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200939 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200940 item = l->lv_first;
941 while (*arg != ']')
942 {
943 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100944 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200945 item = item->li_next;
946 if (arg == NULL)
947 return FAIL;
948
949 arg = skipwhite(arg);
950 if (*arg == ';')
951 {
952 // Put the rest of the list (may be empty) in the var after ';'.
953 // Create a new list for this.
954 l = list_alloc();
955 if (l == NULL)
956 return FAIL;
957 while (item != NULL)
958 {
959 list_append_tv(l, &item->li_tv);
960 item = item->li_next;
961 }
962
963 ltv.v_type = VAR_LIST;
964 ltv.v_lock = 0;
965 ltv.vval.v_list = l;
966 l->lv_refcount = 1;
967
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100968 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200969 (char_u *)"]", op);
970 clear_tv(&ltv);
971 if (arg == NULL)
972 return FAIL;
973 break;
974 }
975 else if (*arg != ',' && *arg != ']')
976 {
977 internal_error("ex_let_vars()");
978 return FAIL;
979 }
980 }
981
982 return OK;
983}
984
985/*
986 * Skip over assignable variable "var" or list of variables "[var, var]".
987 * Used for ":let varvar = expr" and ":for varvar in expr".
988 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200989 * for "[var, var; var]" set "semicolon" to 1.
990 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200991 * Return NULL for an error.
992 */
993 char_u *
994skip_var_list(
995 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100996 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200997 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200998 int *semicolon,
999 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001000{
1001 char_u *p, *s;
1002
1003 if (*arg == '[')
1004 {
1005 // "[var, var]": find the matching ']'.
1006 p = arg;
1007 for (;;)
1008 {
1009 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001010 s = skip_var_one(p, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001011 if (s == p)
1012 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001013 if (!silent)
1014 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001015 return NULL;
1016 }
1017 ++*var_count;
1018
1019 p = skipwhite(s);
1020 if (*p == ']')
1021 break;
1022 else if (*p == ';')
1023 {
1024 if (*semicolon == 1)
1025 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001026 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001027 return NULL;
1028 }
1029 *semicolon = 1;
1030 }
1031 else if (*p != ',')
1032 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001033 if (!silent)
1034 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001035 return NULL;
1036 }
1037 }
1038 return p + 1;
1039 }
1040 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001041 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001042}
1043
1044/*
1045 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1046 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001047 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001048 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001049 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001051{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001052 char_u *end;
1053
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001054 if (*arg == '@' && arg[1] != NUL)
1055 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001057 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001058 if (include_type && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001060 // "a: type" is declaring variable "a" with a type, not "a:".
1061 if (end == arg + 2 && end[-1] == ':')
1062 --end;
1063 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001064 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001065 }
1066 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001067}
1068
1069/*
1070 * List variables for hashtab "ht" with prefix "prefix".
1071 * If "empty" is TRUE also list NULL strings as empty strings.
1072 */
1073 void
1074list_hashtable_vars(
1075 hashtab_T *ht,
1076 char *prefix,
1077 int empty,
1078 int *first)
1079{
1080 hashitem_T *hi;
1081 dictitem_T *di;
1082 int todo;
1083 char_u buf[IOSIZE];
1084
1085 todo = (int)ht->ht_used;
1086 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1087 {
1088 if (!HASHITEM_EMPTY(hi))
1089 {
1090 --todo;
1091 di = HI2DI(hi);
1092
1093 // apply :filter /pat/ to variable name
1094 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1095 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1096 if (message_filtered(buf))
1097 continue;
1098
1099 if (empty || di->di_tv.v_type != VAR_STRING
1100 || di->di_tv.vval.v_string != NULL)
1101 list_one_var(di, prefix, first);
1102 }
1103 }
1104}
1105
1106/*
1107 * List global variables.
1108 */
1109 static void
1110list_glob_vars(int *first)
1111{
1112 list_hashtable_vars(&globvarht, "", TRUE, first);
1113}
1114
1115/*
1116 * List buffer variables.
1117 */
1118 static void
1119list_buf_vars(int *first)
1120{
1121 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1122}
1123
1124/*
1125 * List window variables.
1126 */
1127 static void
1128list_win_vars(int *first)
1129{
1130 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1131}
1132
1133/*
1134 * List tab page variables.
1135 */
1136 static void
1137list_tab_vars(int *first)
1138{
1139 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1140}
1141
1142/*
1143 * List variables in "arg".
1144 */
1145 static char_u *
1146list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1147{
1148 int error = FALSE;
1149 int len;
1150 char_u *name;
1151 char_u *name_start;
1152 char_u *arg_subsc;
1153 char_u *tofree;
1154 typval_T tv;
1155
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001156 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001157 {
1158 if (error || eap->skip)
1159 {
1160 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1161 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1162 {
1163 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001164 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001165 break;
1166 }
1167 }
1168 else
1169 {
1170 // get_name_len() takes care of expanding curly braces
1171 name_start = name = arg;
1172 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1173 if (len <= 0)
1174 {
1175 // This is mainly to keep test 49 working: when expanding
1176 // curly braces fails overrule the exception error message.
1177 if (len < 0 && !aborting())
1178 {
1179 emsg_severe = TRUE;
1180 semsg(_(e_invarg2), arg);
1181 break;
1182 }
1183 error = TRUE;
1184 }
1185 else
1186 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001187 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001188 if (tofree != NULL)
1189 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001190 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001191 error = TRUE;
1192 else
1193 {
1194 // handle d.key, l[idx], f(expr)
1195 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001196 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001197 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001198 error = TRUE;
1199 else
1200 {
1201 if (arg == arg_subsc && len == 2 && name[1] == ':')
1202 {
1203 switch (*name)
1204 {
1205 case 'g': list_glob_vars(first); break;
1206 case 'b': list_buf_vars(first); break;
1207 case 'w': list_win_vars(first); break;
1208 case 't': list_tab_vars(first); break;
1209 case 'v': list_vim_vars(first); break;
1210 case 's': list_script_vars(first); break;
1211 case 'l': list_func_vars(first); break;
1212 default:
1213 semsg(_("E738: Can't list variables for %s"), name);
1214 }
1215 }
1216 else
1217 {
1218 char_u numbuf[NUMBUFLEN];
1219 char_u *tf;
1220 int c;
1221 char_u *s;
1222
1223 s = echo_string(&tv, &tf, numbuf, 0);
1224 c = *arg;
1225 *arg = NUL;
1226 list_one_var_a("",
1227 arg == arg_subsc ? name : name_start,
1228 tv.v_type,
1229 s == NULL ? (char_u *)"" : s,
1230 first);
1231 *arg = c;
1232 vim_free(tf);
1233 }
1234 clear_tv(&tv);
1235 }
1236 }
1237 }
1238
1239 vim_free(tofree);
1240 }
1241
1242 arg = skipwhite(arg);
1243 }
1244
1245 return arg;
1246}
1247
1248/*
1249 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1250 * Returns a pointer to the char just after the var name.
1251 * Returns NULL if there is an error.
1252 */
1253 static char_u *
1254ex_let_one(
1255 char_u *arg, // points to variable name
1256 typval_T *tv, // value to assign to variable
1257 int copy, // copy value from "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001258 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001259 char_u *endchars, // valid chars after variable name or NULL
1260 char_u *op) // "+", "-", "." or NULL
1261{
1262 int c1;
1263 char_u *name;
1264 char_u *p;
1265 char_u *arg_end = NULL;
1266 int len;
1267 int opt_flags;
1268 char_u *tofree = NULL;
1269
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001270 if (in_vim9script() && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001271 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1272 {
1273 vim9_declare_error(arg);
1274 return NULL;
1275 }
1276
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001277 // ":let $VAR = expr": Set environment variable.
1278 if (*arg == '$')
1279 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001280 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001281 {
1282 emsg(_("E996: Cannot lock an environment variable"));
1283 return NULL;
1284 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001285
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001286 // Find the end of the name.
1287 ++arg;
1288 name = arg;
1289 len = get_env_len(&arg);
1290 if (len == 0)
1291 semsg(_(e_invarg2), name - 1);
1292 else
1293 {
1294 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1295 semsg(_(e_letwrong), op);
1296 else if (endchars != NULL
1297 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1298 emsg(_(e_letunexp));
1299 else if (!check_secure())
1300 {
1301 c1 = name[len];
1302 name[len] = NUL;
1303 p = tv_get_string_chk(tv);
1304 if (p != NULL && op != NULL && *op == '.')
1305 {
1306 int mustfree = FALSE;
1307 char_u *s = vim_getenv(name, &mustfree);
1308
1309 if (s != NULL)
1310 {
1311 p = tofree = concat_str(s, p);
1312 if (mustfree)
1313 vim_free(s);
1314 }
1315 }
1316 if (p != NULL)
1317 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001318 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001319 arg_end = arg;
1320 }
1321 name[len] = c1;
1322 vim_free(tofree);
1323 }
1324 }
1325 }
1326
1327 // ":let &option = expr": Set option value.
1328 // ":let &l:option = expr": Set local option value.
1329 // ":let &g:option = expr": Set global option value.
1330 else if (*arg == '&')
1331 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001332 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001333 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001334 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001335 return NULL;
1336 }
1337 // Find the end of the name.
1338 p = find_option_end(&arg, &opt_flags);
1339 if (p == NULL || (endchars != NULL
1340 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1341 emsg(_(e_letunexp));
1342 else
1343 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001344 long n = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001345 int opt_type;
1346 long numval;
1347 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001348 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001349 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001350
1351 c1 = *p;
1352 *p = NUL;
1353
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001354 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
1355 if ((opt_type == 1 || opt_type == -1)
1356 && (tv->v_type != VAR_STRING || !in_vim9script()))
1357 // number, possibly hidden
1358 n = (long)tv_get_number(tv);
1359
1360 // Avoid setting a string option to the text "v:false" or similar.
1361 // In Vim9 script also don't convert a number to string.
1362 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1363 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1364 s = tv_get_string_chk(tv);
1365
1366 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001367 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001368 if ((opt_type == 1 && *op == '.')
1369 || (opt_type == 0 && *op != '.'))
1370 {
1371 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001372 failed = TRUE; // don't set the value
1373
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001374 }
1375 else
1376 {
1377 if (opt_type == 1) // number
1378 {
1379 switch (*op)
1380 {
1381 case '+': n = numval + n; break;
1382 case '-': n = numval - n; break;
1383 case '*': n = numval * n; break;
1384 case '/': n = (long)num_divide(numval, n); break;
1385 case '%': n = (long)num_modulus(numval, n); break;
1386 }
1387 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001388 else if (opt_type == 0 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001389 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001390 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001391 s = concat_str(stringval, s);
1392 vim_free(stringval);
1393 stringval = s;
1394 }
1395 }
1396 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001397
1398 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001399 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001400 if (opt_type != 0 || s != NULL)
1401 {
1402 set_option_value(arg, n, s, opt_flags);
1403 arg_end = p;
1404 }
1405 else
1406 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001407 }
1408 *p = c1;
1409 vim_free(stringval);
1410 }
1411 }
1412
1413 // ":let @r = expr": Set register contents.
1414 else if (*arg == '@')
1415 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001416 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001417 {
1418 emsg(_("E996: Cannot lock a register"));
1419 return NULL;
1420 }
1421 ++arg;
1422 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1423 semsg(_(e_letwrong), op);
1424 else if (endchars != NULL
1425 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1426 emsg(_(e_letunexp));
1427 else
1428 {
1429 char_u *ptofree = NULL;
1430 char_u *s;
1431
1432 p = tv_get_string_chk(tv);
1433 if (p != NULL && op != NULL && *op == '.')
1434 {
1435 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1436 if (s != NULL)
1437 {
1438 p = ptofree = concat_str(s, p);
1439 vim_free(s);
1440 }
1441 }
1442 if (p != NULL)
1443 {
1444 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1445 arg_end = arg + 1;
1446 }
1447 vim_free(ptofree);
1448 }
1449 }
1450
1451 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001452 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001453 // ":let {expr} = expr": Idem, name made with curly braces
1454 else if (eval_isnamec1(*arg) || *arg == '{')
1455 {
1456 lval_T lv;
1457
1458 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001459 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001460 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461 if (endchars != NULL && vim_strchr(endchars,
1462 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001463 emsg(_(e_letunexp));
1464 else
1465 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001467 arg_end = p;
1468 }
1469 }
1470 clear_lval(&lv);
1471 }
1472
1473 else
1474 semsg(_(e_invarg2), arg);
1475
1476 return arg_end;
1477}
1478
1479/*
1480 * ":unlet[!] var1 ... " command.
1481 */
1482 void
1483ex_unlet(exarg_T *eap)
1484{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001485 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001486}
1487
1488/*
1489 * ":lockvar" and ":unlockvar" commands
1490 */
1491 void
1492ex_lockvar(exarg_T *eap)
1493{
1494 char_u *arg = eap->arg;
1495 int deep = 2;
1496
1497 if (eap->forceit)
1498 deep = -1;
1499 else if (vim_isdigit(*arg))
1500 {
1501 deep = getdigits(&arg);
1502 arg = skipwhite(arg);
1503 }
1504
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001505 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001506}
1507
1508/*
1509 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001510 * Also used for Vim9 script. "callback" is invoked as:
1511 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001512 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001513 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001514ex_unletlock(
1515 exarg_T *eap,
1516 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001517 int deep,
1518 int glv_flags,
1519 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1520 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001521{
1522 char_u *arg = argstart;
1523 char_u *name_end;
1524 int error = FALSE;
1525 lval_T lv;
1526
1527 do
1528 {
1529 if (*arg == '$')
1530 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001531 lv.ll_name = arg;
1532 lv.ll_tv = NULL;
1533 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001534 if (get_env_len(&arg) == 0)
1535 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001536 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001537 return;
1538 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001539 if (!error && !eap->skip
1540 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1541 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001542 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001543 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001544 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001545 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001546 // Parse the name and find the end.
1547 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1548 glv_flags, FNE_CHECK_START);
1549 if (lv.ll_name == NULL)
1550 error = TRUE; // error but continue parsing
1551 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1552 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001553 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001554 if (name_end != NULL)
1555 {
1556 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001557 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001558 }
1559 if (!(eap->skip || error))
1560 clear_lval(&lv);
1561 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001562 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001563
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001564 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001565 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001566 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001567
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001568 if (!eap->skip)
1569 clear_lval(&lv);
1570 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001571
1572 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001573 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001574
1575 eap->nextcmd = check_nextcmd(arg);
1576}
1577
1578 static int
1579do_unlet_var(
1580 lval_T *lp,
1581 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001582 exarg_T *eap,
1583 int deep UNUSED,
1584 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001585{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001586 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001587 int ret = OK;
1588 int cc;
1589
1590 if (lp->ll_tv == NULL)
1591 {
1592 cc = *name_end;
1593 *name_end = NUL;
1594
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001595 // Environment variable, normal name or expanded name.
1596 if (*lp->ll_name == '$')
1597 vim_unsetenv(lp->ll_name + 1);
1598 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001599 ret = FAIL;
1600 *name_end = cc;
1601 }
1602 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001603 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001604 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001605 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001606 return FAIL;
1607 else if (lp->ll_range)
1608 {
1609 listitem_T *li;
1610 listitem_T *ll_li = lp->ll_li;
1611 int ll_n1 = lp->ll_n1;
1612
1613 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1614 {
1615 li = ll_li->li_next;
Bram Moolenaara187c432020-09-16 21:08:28 +02001616 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001617 return FAIL;
1618 ll_li = li;
1619 ++ll_n1;
1620 }
1621
1622 // Delete a range of List items.
1623 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1624 {
1625 li = lp->ll_li->li_next;
1626 listitem_remove(lp->ll_list, lp->ll_li);
1627 lp->ll_li = li;
1628 ++lp->ll_n1;
1629 }
1630 }
1631 else
1632 {
1633 if (lp->ll_list != NULL)
1634 // unlet a List item.
1635 listitem_remove(lp->ll_list, lp->ll_li);
1636 else
1637 // unlet a Dictionary item.
1638 dictitem_remove(lp->ll_dict, lp->ll_di);
1639 }
1640
1641 return ret;
1642}
1643
1644/*
1645 * "unlet" a variable. Return OK if it existed, FAIL if not.
1646 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1647 */
1648 int
1649do_unlet(char_u *name, int forceit)
1650{
1651 hashtab_T *ht;
1652 hashitem_T *hi;
1653 char_u *varname;
1654 dict_T *d;
1655 dictitem_T *di;
1656
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001657 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001658 return FAIL;
1659
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001660 ht = find_var_ht(name, &varname);
1661 if (ht != NULL && *varname != NUL)
1662 {
1663 d = get_current_funccal_dict(ht);
1664 if (d == NULL)
1665 {
1666 if (ht == &globvarht)
1667 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001668 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001669 d = &vimvardict;
1670 else
1671 {
1672 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1673 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1674 }
1675 if (d == NULL)
1676 {
1677 internal_error("do_unlet()");
1678 return FAIL;
1679 }
1680 }
1681 hi = hash_find(ht, varname);
1682 if (HASHITEM_EMPTY(hi))
1683 hi = find_hi_in_scoped_ht(name, &ht);
1684 if (hi != NULL && !HASHITEM_EMPTY(hi))
1685 {
1686 di = HI2DI(hi);
1687 if (var_check_fixed(di->di_flags, name, FALSE)
1688 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001689 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001690 return FAIL;
1691
1692 delete_var(ht, hi);
1693 return OK;
1694 }
1695 }
1696 if (forceit)
1697 return OK;
1698 semsg(_("E108: No such variable: \"%s\""), name);
1699 return FAIL;
1700}
1701
1702/*
1703 * Lock or unlock variable indicated by "lp".
1704 * "deep" is the levels to go (-1 for unlimited);
1705 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1706 */
1707 static int
1708do_lock_var(
1709 lval_T *lp,
1710 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001711 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001712 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001713 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001714{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001715 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001716 int ret = OK;
1717 int cc;
1718 dictitem_T *di;
1719
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001720 if (lp->ll_tv == NULL)
1721 {
1722 cc = *name_end;
1723 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001724 if (*lp->ll_name == '$')
1725 {
1726 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001727 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001728 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001729 else
1730 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001731 // Normal name or expanded name.
1732 di = find_var(lp->ll_name, NULL, TRUE);
1733 if (di == NULL)
1734 ret = FAIL;
1735 else if ((di->di_flags & DI_FLAGS_FIX)
1736 && di->di_tv.v_type != VAR_DICT
1737 && di->di_tv.v_type != VAR_LIST)
1738 {
1739 // For historic reasons this error is not given for a list or
1740 // dict. E.g., the b: dict could be locked/unlocked.
1741 semsg(_(e_lock_unlock), lp->ll_name);
1742 ret = FAIL;
1743 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001744 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001745 {
1746 if (lock)
1747 di->di_flags |= DI_FLAGS_LOCK;
1748 else
1749 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001750 if (deep != 0)
1751 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001752 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001753 }
1754 *name_end = cc;
1755 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001756 else if (deep == 0)
1757 {
1758 // nothing to do
1759 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001760 else if (lp->ll_range)
1761 {
1762 listitem_T *li = lp->ll_li;
1763
1764 // (un)lock a range of List items.
1765 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1766 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001767 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001768 li = li->li_next;
1769 ++lp->ll_n1;
1770 }
1771 }
1772 else if (lp->ll_list != NULL)
1773 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001774 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001775 else
1776 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001777 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001778
1779 return ret;
1780}
1781
1782/*
1783 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001784 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1785 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001786 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001787 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001788item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001789{
1790 static int recurse = 0;
1791 list_T *l;
1792 listitem_T *li;
1793 dict_T *d;
1794 blob_T *b;
1795 hashitem_T *hi;
1796 int todo;
1797
1798 if (recurse >= DICT_MAXNEST)
1799 {
1800 emsg(_("E743: variable nested too deep for (un)lock"));
1801 return;
1802 }
1803 if (deep == 0)
1804 return;
1805 ++recurse;
1806
1807 // lock/unlock the item itself
1808 if (lock)
1809 tv->v_lock |= VAR_LOCKED;
1810 else
1811 tv->v_lock &= ~VAR_LOCKED;
1812
1813 switch (tv->v_type)
1814 {
1815 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001816 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001818 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001820 case VAR_STRING:
1821 case VAR_FUNC:
1822 case VAR_PARTIAL:
1823 case VAR_FLOAT:
1824 case VAR_SPECIAL:
1825 case VAR_JOB:
1826 case VAR_CHANNEL:
1827 break;
1828
1829 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001830 if ((b = tv->vval.v_blob) != NULL
1831 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001832 {
1833 if (lock)
1834 b->bv_lock |= VAR_LOCKED;
1835 else
1836 b->bv_lock &= ~VAR_LOCKED;
1837 }
1838 break;
1839 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001840 if ((l = tv->vval.v_list) != NULL
1841 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001842 {
1843 if (lock)
1844 l->lv_lock |= VAR_LOCKED;
1845 else
1846 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001847 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001848 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001849 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001850 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001851 }
1852 break;
1853 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001854 if ((d = tv->vval.v_dict) != NULL
1855 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001856 {
1857 if (lock)
1858 d->dv_lock |= VAR_LOCKED;
1859 else
1860 d->dv_lock &= ~VAR_LOCKED;
1861 if (deep < 0 || deep > 1)
1862 {
1863 // recursive: lock/unlock the items the List contains
1864 todo = (int)d->dv_hashtab.ht_used;
1865 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1866 {
1867 if (!HASHITEM_EMPTY(hi))
1868 {
1869 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001870 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1871 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001872 }
1873 }
1874 }
1875 }
1876 }
1877 --recurse;
1878}
1879
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001880#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1881/*
1882 * Delete all "menutrans_" variables.
1883 */
1884 void
1885del_menutrans_vars(void)
1886{
1887 hashitem_T *hi;
1888 int todo;
1889
1890 hash_lock(&globvarht);
1891 todo = (int)globvarht.ht_used;
1892 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1893 {
1894 if (!HASHITEM_EMPTY(hi))
1895 {
1896 --todo;
1897 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1898 delete_var(&globvarht, hi);
1899 }
1900 }
1901 hash_unlock(&globvarht);
1902}
1903#endif
1904
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001905/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001906 * Local string buffer for the next two functions to store a variable name
1907 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1908 * get_user_var_name().
1909 */
1910
1911static char_u *varnamebuf = NULL;
1912static int varnamebuflen = 0;
1913
1914/*
1915 * Function to concatenate a prefix and a variable name.
1916 */
1917 static char_u *
1918cat_prefix_varname(int prefix, char_u *name)
1919{
1920 int len;
1921
1922 len = (int)STRLEN(name) + 3;
1923 if (len > varnamebuflen)
1924 {
1925 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001926 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001927 varnamebuf = alloc(len);
1928 if (varnamebuf == NULL)
1929 {
1930 varnamebuflen = 0;
1931 return NULL;
1932 }
1933 varnamebuflen = len;
1934 }
1935 *varnamebuf = prefix;
1936 varnamebuf[1] = ':';
1937 STRCPY(varnamebuf + 2, name);
1938 return varnamebuf;
1939}
1940
1941/*
1942 * Function given to ExpandGeneric() to obtain the list of user defined
1943 * (global/buffer/window/built-in) variable names.
1944 */
1945 char_u *
1946get_user_var_name(expand_T *xp, int idx)
1947{
1948 static long_u gdone;
1949 static long_u bdone;
1950 static long_u wdone;
1951 static long_u tdone;
1952 static int vidx;
1953 static hashitem_T *hi;
1954 hashtab_T *ht;
1955
1956 if (idx == 0)
1957 {
1958 gdone = bdone = wdone = vidx = 0;
1959 tdone = 0;
1960 }
1961
1962 // Global variables
1963 if (gdone < globvarht.ht_used)
1964 {
1965 if (gdone++ == 0)
1966 hi = globvarht.ht_array;
1967 else
1968 ++hi;
1969 while (HASHITEM_EMPTY(hi))
1970 ++hi;
1971 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1972 return cat_prefix_varname('g', hi->hi_key);
1973 return hi->hi_key;
1974 }
1975
1976 // b: variables
1977 ht = &curbuf->b_vars->dv_hashtab;
1978 if (bdone < ht->ht_used)
1979 {
1980 if (bdone++ == 0)
1981 hi = ht->ht_array;
1982 else
1983 ++hi;
1984 while (HASHITEM_EMPTY(hi))
1985 ++hi;
1986 return cat_prefix_varname('b', hi->hi_key);
1987 }
1988
1989 // w: variables
1990 ht = &curwin->w_vars->dv_hashtab;
1991 if (wdone < ht->ht_used)
1992 {
1993 if (wdone++ == 0)
1994 hi = ht->ht_array;
1995 else
1996 ++hi;
1997 while (HASHITEM_EMPTY(hi))
1998 ++hi;
1999 return cat_prefix_varname('w', hi->hi_key);
2000 }
2001
2002 // t: variables
2003 ht = &curtab->tp_vars->dv_hashtab;
2004 if (tdone < ht->ht_used)
2005 {
2006 if (tdone++ == 0)
2007 hi = ht->ht_array;
2008 else
2009 ++hi;
2010 while (HASHITEM_EMPTY(hi))
2011 ++hi;
2012 return cat_prefix_varname('t', hi->hi_key);
2013 }
2014
2015 // v: variables
2016 if (vidx < VV_LEN)
2017 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2018
2019 VIM_CLEAR(varnamebuf);
2020 varnamebuflen = 0;
2021 return NULL;
2022}
2023
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002024 char *
2025get_var_special_name(int nr)
2026{
2027 switch (nr)
2028 {
2029 case VVAL_FALSE: return "v:false";
2030 case VVAL_TRUE: return "v:true";
2031 case VVAL_NONE: return "v:none";
2032 case VVAL_NULL: return "v:null";
2033 }
2034 internal_error("get_var_special_name()");
2035 return "42";
2036}
2037
2038/*
2039 * Returns the global variable dictionary
2040 */
2041 dict_T *
2042get_globvar_dict(void)
2043{
2044 return &globvardict;
2045}
2046
2047/*
2048 * Returns the global variable hash table
2049 */
2050 hashtab_T *
2051get_globvar_ht(void)
2052{
2053 return &globvarht;
2054}
2055
2056/*
2057 * Returns the v: variable dictionary
2058 */
2059 dict_T *
2060get_vimvar_dict(void)
2061{
2062 return &vimvardict;
2063}
2064
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002065/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002067 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068 */
2069 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002070find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002072 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2073 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074
2075 if (di == NULL)
2076 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002077 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2079 return (int)(vv - vimvars);
2080}
2081
2082
2083/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002084 * Set type of v: variable to "type".
2085 */
2086 void
2087set_vim_var_type(int idx, vartype_T type)
2088{
2089 vimvars[idx].vv_type = type;
2090}
2091
2092/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002093 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002094 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002095 */
2096 void
2097set_vim_var_nr(int idx, varnumber_T val)
2098{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002099 vimvars[idx].vv_nr = val;
2100}
2101
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002102 char *
2103get_vim_var_name(int idx)
2104{
2105 return vimvars[idx].vv_name;
2106}
2107
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002108/*
2109 * Get typval_T v: variable value.
2110 */
2111 typval_T *
2112get_vim_var_tv(int idx)
2113{
2114 return &vimvars[idx].vv_tv;
2115}
2116
2117/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002118 * Set v: variable to "tv". Only accepts the same type.
2119 * Takes over the value of "tv".
2120 */
2121 int
2122set_vim_var_tv(int idx, typval_T *tv)
2123{
2124 if (vimvars[idx].vv_type != tv->v_type)
2125 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002126 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002127 clear_tv(tv);
2128 return FAIL;
2129 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002130 // VV_RO is also checked when compiling, but let's check here as well.
2131 if (vimvars[idx].vv_flags & VV_RO)
2132 {
2133 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2134 return FAIL;
2135 }
2136 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2137 {
2138 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2139 return FAIL;
2140 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002141 clear_tv(&vimvars[idx].vv_di.di_tv);
2142 vimvars[idx].vv_di.di_tv = *tv;
2143 return OK;
2144}
2145
2146/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002147 * Get number v: variable value.
2148 */
2149 varnumber_T
2150get_vim_var_nr(int idx)
2151{
2152 return vimvars[idx].vv_nr;
2153}
2154
2155/*
2156 * Get string v: variable value. Uses a static buffer, can only be used once.
2157 * If the String variable has never been set, return an empty string.
2158 * Never returns NULL;
2159 */
2160 char_u *
2161get_vim_var_str(int idx)
2162{
2163 return tv_get_string(&vimvars[idx].vv_tv);
2164}
2165
2166/*
2167 * Get List v: variable value. Caller must take care of reference count when
2168 * needed.
2169 */
2170 list_T *
2171get_vim_var_list(int idx)
2172{
2173 return vimvars[idx].vv_list;
2174}
2175
2176/*
2177 * Get Dict v: variable value. Caller must take care of reference count when
2178 * needed.
2179 */
2180 dict_T *
2181get_vim_var_dict(int idx)
2182{
2183 return vimvars[idx].vv_dict;
2184}
2185
2186/*
2187 * Set v:char to character "c".
2188 */
2189 void
2190set_vim_var_char(int c)
2191{
2192 char_u buf[MB_MAXBYTES + 1];
2193
2194 if (has_mbyte)
2195 buf[(*mb_char2bytes)(c, buf)] = NUL;
2196 else
2197 {
2198 buf[0] = c;
2199 buf[1] = NUL;
2200 }
2201 set_vim_var_string(VV_CHAR, buf, -1);
2202}
2203
2204/*
2205 * Set v:count to "count" and v:count1 to "count1".
2206 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2207 */
2208 void
2209set_vcount(
2210 long count,
2211 long count1,
2212 int set_prevcount)
2213{
2214 if (set_prevcount)
2215 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2216 vimvars[VV_COUNT].vv_nr = count;
2217 vimvars[VV_COUNT1].vv_nr = count1;
2218}
2219
2220/*
2221 * Save variables that might be changed as a side effect. Used when executing
2222 * a timer callback.
2223 */
2224 void
2225save_vimvars(vimvars_save_T *vvsave)
2226{
2227 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2228 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2229 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2230}
2231
2232/*
2233 * Restore variables saved by save_vimvars().
2234 */
2235 void
2236restore_vimvars(vimvars_save_T *vvsave)
2237{
2238 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2239 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2240 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2241}
2242
2243/*
2244 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2245 * value.
2246 */
2247 void
2248set_vim_var_string(
2249 int idx,
2250 char_u *val,
2251 int len) // length of "val" to use or -1 (whole string)
2252{
2253 clear_tv(&vimvars[idx].vv_di.di_tv);
2254 vimvars[idx].vv_type = VAR_STRING;
2255 if (val == NULL)
2256 vimvars[idx].vv_str = NULL;
2257 else if (len == -1)
2258 vimvars[idx].vv_str = vim_strsave(val);
2259 else
2260 vimvars[idx].vv_str = vim_strnsave(val, len);
2261}
2262
2263/*
2264 * Set List v: variable to "val".
2265 */
2266 void
2267set_vim_var_list(int idx, list_T *val)
2268{
2269 clear_tv(&vimvars[idx].vv_di.di_tv);
2270 vimvars[idx].vv_type = VAR_LIST;
2271 vimvars[idx].vv_list = val;
2272 if (val != NULL)
2273 ++val->lv_refcount;
2274}
2275
2276/*
2277 * Set Dictionary v: variable to "val".
2278 */
2279 void
2280set_vim_var_dict(int idx, dict_T *val)
2281{
2282 clear_tv(&vimvars[idx].vv_di.di_tv);
2283 vimvars[idx].vv_type = VAR_DICT;
2284 vimvars[idx].vv_dict = val;
2285 if (val != NULL)
2286 {
2287 ++val->dv_refcount;
2288 dict_set_items_ro(val);
2289 }
2290}
2291
2292/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002293 * Set the v:argv list.
2294 */
2295 void
2296set_argv_var(char **argv, int argc)
2297{
2298 list_T *l = list_alloc();
2299 int i;
2300
2301 if (l == NULL)
2302 getout(1);
2303 l->lv_lock = VAR_FIXED;
2304 for (i = 0; i < argc; ++i)
2305 {
2306 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2307 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002308 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002309 }
2310 set_vim_var_list(VV_ARGV, l);
2311}
2312
2313/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002314 * Reset v:register, taking the 'clipboard' setting into account.
2315 */
2316 void
2317reset_reg_var(void)
2318{
2319 int regname = 0;
2320
2321 // Adjust the register according to 'clipboard', so that when
2322 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2323#ifdef FEAT_CLIPBOARD
2324 adjust_clip_reg(&regname);
2325#endif
2326 set_reg_var(regname);
2327}
2328
2329/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002330 * Set v:register if needed.
2331 */
2332 void
2333set_reg_var(int c)
2334{
2335 char_u regname;
2336
2337 if (c == 0 || c == ' ')
2338 regname = '"';
2339 else
2340 regname = c;
2341 // Avoid free/alloc when the value is already right.
2342 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2343 set_vim_var_string(VV_REG, &regname, 1);
2344}
2345
2346/*
2347 * Get or set v:exception. If "oldval" == NULL, return the current value.
2348 * Otherwise, restore the value to "oldval" and return NULL.
2349 * Must always be called in pairs to save and restore v:exception! Does not
2350 * take care of memory allocations.
2351 */
2352 char_u *
2353v_exception(char_u *oldval)
2354{
2355 if (oldval == NULL)
2356 return vimvars[VV_EXCEPTION].vv_str;
2357
2358 vimvars[VV_EXCEPTION].vv_str = oldval;
2359 return NULL;
2360}
2361
2362/*
2363 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2364 * Otherwise, restore the value to "oldval" and return NULL.
2365 * Must always be called in pairs to save and restore v:throwpoint! Does not
2366 * take care of memory allocations.
2367 */
2368 char_u *
2369v_throwpoint(char_u *oldval)
2370{
2371 if (oldval == NULL)
2372 return vimvars[VV_THROWPOINT].vv_str;
2373
2374 vimvars[VV_THROWPOINT].vv_str = oldval;
2375 return NULL;
2376}
2377
2378/*
2379 * Set v:cmdarg.
2380 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2381 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2382 * Must always be called in pairs!
2383 */
2384 char_u *
2385set_cmdarg(exarg_T *eap, char_u *oldarg)
2386{
2387 char_u *oldval;
2388 char_u *newval;
2389 unsigned len;
2390
2391 oldval = vimvars[VV_CMDARG].vv_str;
2392 if (eap == NULL)
2393 {
2394 vim_free(oldval);
2395 vimvars[VV_CMDARG].vv_str = oldarg;
2396 return NULL;
2397 }
2398
2399 if (eap->force_bin == FORCE_BIN)
2400 len = 6;
2401 else if (eap->force_bin == FORCE_NOBIN)
2402 len = 8;
2403 else
2404 len = 0;
2405
2406 if (eap->read_edit)
2407 len += 7;
2408
2409 if (eap->force_ff != 0)
2410 len += 10; // " ++ff=unix"
2411 if (eap->force_enc != 0)
2412 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2413 if (eap->bad_char != 0)
2414 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2415
2416 newval = alloc(len + 1);
2417 if (newval == NULL)
2418 return NULL;
2419
2420 if (eap->force_bin == FORCE_BIN)
2421 sprintf((char *)newval, " ++bin");
2422 else if (eap->force_bin == FORCE_NOBIN)
2423 sprintf((char *)newval, " ++nobin");
2424 else
2425 *newval = NUL;
2426
2427 if (eap->read_edit)
2428 STRCAT(newval, " ++edit");
2429
2430 if (eap->force_ff != 0)
2431 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2432 eap->force_ff == 'u' ? "unix"
2433 : eap->force_ff == 'd' ? "dos"
2434 : "mac");
2435 if (eap->force_enc != 0)
2436 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2437 eap->cmd + eap->force_enc);
2438 if (eap->bad_char == BAD_KEEP)
2439 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2440 else if (eap->bad_char == BAD_DROP)
2441 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2442 else if (eap->bad_char != 0)
2443 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2444 vimvars[VV_CMDARG].vv_str = newval;
2445 return oldval;
2446}
2447
2448/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002449 * Get the value of internal variable "name".
2450 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2451 */
2452 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002453eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002454 char_u *name,
2455 int len, // length of "name"
2456 typval_T *rettv, // NULL when only checking existence
2457 dictitem_T **dip, // non-NULL when typval's dict item is needed
2458 int verbose, // may give error message
2459 int no_autoload) // do not use script autoloading
2460{
2461 int ret = OK;
2462 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002463 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002464 dictitem_T *v;
2465 int cc;
2466
2467 // truncate the name, so that we can use strcmp()
2468 cc = name[len];
2469 name[len] = NUL;
2470
2471 // Check for user-defined variables.
2472 v = find_var(name, NULL, no_autoload);
2473 if (v != NULL)
2474 {
2475 tv = &v->di_tv;
2476 if (dip != NULL)
2477 *dip = v;
2478 }
2479
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002480 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002482 imported_T *import;
2483 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2484
2485 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486
2487 // imported variable from another script
2488 if (import != NULL)
2489 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002490 if (import->imp_funcname != NULL)
2491 {
2492 foundFunc = TRUE;
2493 if (rettv != NULL)
2494 {
2495 rettv->v_type = VAR_FUNC;
2496 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2497 }
2498 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002499 else if (import->imp_all)
2500 {
2501 emsg("Sorry, 'import * as X' not implemented yet");
2502 ret = FAIL;
2503 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002504 else
2505 {
2506 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002507 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002509 tv = sv->sv_tv;
2510 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002511 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002512 else if (in_vim9script())
2513 {
2514 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2515
2516 if (ufunc != NULL)
2517 {
2518 foundFunc = TRUE;
2519 if (rettv != NULL)
2520 {
2521 rettv->v_type = VAR_FUNC;
2522 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2523 }
2524 }
2525 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526 }
2527
Bram Moolenaarc620c052020-07-08 15:16:19 +02002528 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002529 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002530 if (tv == NULL)
2531 {
2532 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002533 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002534 ret = FAIL;
2535 }
2536 else if (rettv != NULL)
2537 copy_tv(tv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002538 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002539
2540 name[len] = cc;
2541
2542 return ret;
2543}
2544
2545/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002546 * Check if variable "name[len]" is a local variable or an argument.
2547 * If so, "*eval_lavars_used" is set to TRUE.
2548 */
2549 void
2550check_vars(char_u *name, int len)
2551{
2552 int cc;
2553 char_u *varname;
2554 hashtab_T *ht;
2555
2556 if (eval_lavars_used == NULL)
2557 return;
2558
2559 // truncate the name, so that we can use strcmp()
2560 cc = name[len];
2561 name[len] = NUL;
2562
2563 ht = find_var_ht(name, &varname);
2564 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2565 {
2566 if (find_var(name, NULL, TRUE) != NULL)
2567 *eval_lavars_used = TRUE;
2568 }
2569
2570 name[len] = cc;
2571}
2572
2573/*
2574 * Find variable "name" in the list of variables.
2575 * Return a pointer to it if found, NULL if not found.
2576 * Careful: "a:0" variables don't have a name.
2577 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2578 * hashtab_T used.
2579 */
2580 dictitem_T *
2581find_var(char_u *name, hashtab_T **htp, int no_autoload)
2582{
2583 char_u *varname;
2584 hashtab_T *ht;
2585 dictitem_T *ret = NULL;
2586
2587 ht = find_var_ht(name, &varname);
2588 if (htp != NULL)
2589 *htp = ht;
2590 if (ht == NULL)
2591 return NULL;
2592 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2593 if (ret != NULL)
2594 return ret;
2595
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002596 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002597 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2598}
2599
2600/*
2601 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002602 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002603 * Returns NULL if not found.
2604 */
2605 dictitem_T *
2606find_var_in_ht(
2607 hashtab_T *ht,
2608 int htname,
2609 char_u *varname,
2610 int no_autoload)
2611{
2612 hashitem_T *hi;
2613
2614 if (*varname == NUL)
2615 {
2616 // Must be something like "s:", otherwise "ht" would be NULL.
2617 switch (htname)
2618 {
2619 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2620 case 'g': return &globvars_var;
2621 case 'v': return &vimvars_var;
2622 case 'b': return &curbuf->b_bufvar;
2623 case 'w': return &curwin->w_winvar;
2624 case 't': return &curtab->tp_winvar;
2625 case 'l': return get_funccal_local_var();
2626 case 'a': return get_funccal_args_var();
2627 }
2628 return NULL;
2629 }
2630
2631 hi = hash_find(ht, varname);
2632 if (HASHITEM_EMPTY(hi))
2633 {
2634 // For global variables we may try auto-loading the script. If it
2635 // worked find the variable again. Don't auto-load a script if it was
2636 // loaded already, otherwise it would be loaded every time when
2637 // checking if a function name is a Funcref variable.
2638 if (ht == &globvarht && !no_autoload)
2639 {
2640 // Note: script_autoload() may make "hi" invalid. It must either
2641 // be obtained again or not used.
2642 if (!script_autoload(varname, FALSE) || aborting())
2643 return NULL;
2644 hi = hash_find(ht, varname);
2645 }
2646 if (HASHITEM_EMPTY(hi))
2647 return NULL;
2648 }
2649 return HI2DI(hi);
2650}
2651
2652/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002653 * Get the script-local hashtab. NULL if not in a script context.
2654 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002655 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002656get_script_local_ht(void)
2657{
2658 scid_T sid = current_sctx.sc_sid;
2659
Bram Moolenaare3d46852020-08-29 13:39:17 +02002660 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 return &SCRIPT_VARS(sid);
2662 return NULL;
2663}
2664
2665/*
2666 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002667 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002669 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2671{
2672 hashtab_T *ht = get_script_local_ht();
2673 char_u buffer[30];
2674 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002675 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676 hashitem_T *hi;
2677
2678 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002679 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680 if (len < sizeof(buffer) - 1)
2681 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002682 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683 vim_strncpy(buffer, name, len);
2684 p = buffer;
2685 }
2686 else
2687 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002688 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002689 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002690 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691 }
2692
2693 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002694 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002695
2696 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002697 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2698 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699
2700 if (p != buffer)
2701 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002702 // Don't return "buffer", gcc complains.
2703 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704}
2705
2706/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002707 * Find the hashtab used for a variable name.
2708 * Return NULL if the name is not valid.
2709 * Set "varname" to the start of name without ':'.
2710 */
2711 hashtab_T *
2712find_var_ht(char_u *name, char_u **varname)
2713{
2714 hashitem_T *hi;
2715 hashtab_T *ht;
2716
2717 if (name[0] == NUL)
2718 return NULL;
2719 if (name[1] != ':')
2720 {
2721 // The name must not start with a colon or #.
2722 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2723 return NULL;
2724 *varname = name;
2725
2726 // "version" is "v:version" in all scopes if scriptversion < 3.
2727 // Same for a few other variables marked with VV_COMPAT.
2728 if (current_sctx.sc_version < 3)
2729 {
2730 hi = hash_find(&compat_hashtab, name);
2731 if (!HASHITEM_EMPTY(hi))
2732 return &compat_hashtab;
2733 }
2734
2735 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002736 if (ht != NULL)
2737 return ht; // local variable
2738
2739 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002740 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 {
2742 ht = get_script_local_ht();
2743 if (ht != NULL)
2744 return ht;
2745 }
2746
2747 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002748 }
2749 *varname = name + 2;
2750 if (*name == 'g') // global variable
2751 return &globvarht;
2752 // There must be no ':' or '#' in the rest of the name, unless g: is used
2753 if (vim_strchr(name + 2, ':') != NULL
2754 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2755 return NULL;
2756 if (*name == 'b') // buffer variable
2757 return &curbuf->b_vars->dv_hashtab;
2758 if (*name == 'w') // window variable
2759 return &curwin->w_vars->dv_hashtab;
2760 if (*name == 't') // tab page variable
2761 return &curtab->tp_vars->dv_hashtab;
2762 if (*name == 'v') // v: variable
2763 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002764 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002765 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002767 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 if (*name == 'a') // a: function argument
2769 return get_funccal_args_ht();
2770 if (*name == 'l') // l: local function variable
2771 return get_funccal_local_ht();
2772 }
2773 if (*name == 's') // script variable
2774 {
2775 ht = get_script_local_ht();
2776 if (ht != NULL)
2777 return ht;
2778 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002779 return NULL;
2780}
2781
2782/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002783 * Get the string value of a (global/local) variable.
2784 * Note: see tv_get_string() for how long the pointer remains valid.
2785 * Returns NULL when it doesn't exist.
2786 */
2787 char_u *
2788get_var_value(char_u *name)
2789{
2790 dictitem_T *v;
2791
2792 v = find_var(name, NULL, FALSE);
2793 if (v == NULL)
2794 return NULL;
2795 return tv_get_string(&v->di_tv);
2796}
2797
2798/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002799 * Allocate a new hashtab for a sourced script. It will be used while
2800 * sourcing this script and when executing functions defined in the script.
2801 */
2802 void
2803new_script_vars(scid_T id)
2804{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002805 scriptvar_T *sv;
2806
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002807 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2808 if (sv == NULL)
2809 return;
2810 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002811 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002812}
2813
2814/*
2815 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2816 * point to it.
2817 */
2818 void
2819init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2820{
2821 hash_init(&dict->dv_hashtab);
2822 dict->dv_lock = 0;
2823 dict->dv_scope = scope;
2824 dict->dv_refcount = DO_NOT_FREE_CNT;
2825 dict->dv_copyID = 0;
2826 dict_var->di_tv.vval.v_dict = dict;
2827 dict_var->di_tv.v_type = VAR_DICT;
2828 dict_var->di_tv.v_lock = VAR_FIXED;
2829 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2830 dict_var->di_key[0] = NUL;
2831}
2832
2833/*
2834 * Unreference a dictionary initialized by init_var_dict().
2835 */
2836 void
2837unref_var_dict(dict_T *dict)
2838{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002839 // Now the dict needs to be freed if no one else is using it, go back to
2840 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002841 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2842 dict_unref(dict);
2843}
2844
2845/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002846 * Clean up a list of internal variables.
2847 * Frees all allocated variables and the value they contain.
2848 * Clears hashtab "ht", does not free it.
2849 */
2850 void
2851vars_clear(hashtab_T *ht)
2852{
2853 vars_clear_ext(ht, TRUE);
2854}
2855
2856/*
2857 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2858 */
2859 void
2860vars_clear_ext(hashtab_T *ht, int free_val)
2861{
2862 int todo;
2863 hashitem_T *hi;
2864 dictitem_T *v;
2865
2866 hash_lock(ht);
2867 todo = (int)ht->ht_used;
2868 for (hi = ht->ht_array; todo > 0; ++hi)
2869 {
2870 if (!HASHITEM_EMPTY(hi))
2871 {
2872 --todo;
2873
2874 // Free the variable. Don't remove it from the hashtab,
2875 // ht_array might change then. hash_clear() takes care of it
2876 // later.
2877 v = HI2DI(hi);
2878 if (free_val)
2879 clear_tv(&v->di_tv);
2880 if (v->di_flags & DI_FLAGS_ALLOC)
2881 vim_free(v);
2882 }
2883 }
2884 hash_clear(ht);
2885 ht->ht_used = 0;
2886}
2887
2888/*
2889 * Delete a variable from hashtab "ht" at item "hi".
2890 * Clear the variable value and free the dictitem.
2891 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02002892 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002893delete_var(hashtab_T *ht, hashitem_T *hi)
2894{
2895 dictitem_T *di = HI2DI(hi);
2896
2897 hash_remove(ht, hi);
2898 clear_tv(&di->di_tv);
2899 vim_free(di);
2900}
2901
2902/*
2903 * List the value of one internal variable.
2904 */
2905 static void
2906list_one_var(dictitem_T *v, char *prefix, int *first)
2907{
2908 char_u *tofree;
2909 char_u *s;
2910 char_u numbuf[NUMBUFLEN];
2911
2912 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2913 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2914 s == NULL ? (char_u *)"" : s, first);
2915 vim_free(tofree);
2916}
2917
2918 static void
2919list_one_var_a(
2920 char *prefix,
2921 char_u *name,
2922 int type,
2923 char_u *string,
2924 int *first) // when TRUE clear rest of screen and set to FALSE
2925{
2926 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2927 msg_start();
2928 msg_puts(prefix);
2929 if (name != NULL) // "a:" vars don't have a name stored
2930 msg_puts((char *)name);
2931 msg_putchar(' ');
2932 msg_advance(22);
2933 if (type == VAR_NUMBER)
2934 msg_putchar('#');
2935 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2936 msg_putchar('*');
2937 else if (type == VAR_LIST)
2938 {
2939 msg_putchar('[');
2940 if (*string == '[')
2941 ++string;
2942 }
2943 else if (type == VAR_DICT)
2944 {
2945 msg_putchar('{');
2946 if (*string == '{')
2947 ++string;
2948 }
2949 else
2950 msg_putchar(' ');
2951
2952 msg_outtrans(string);
2953
2954 if (type == VAR_FUNC || type == VAR_PARTIAL)
2955 msg_puts("()");
2956 if (*first)
2957 {
2958 msg_clr_eos();
2959 *first = FALSE;
2960 }
2961}
2962
2963/*
2964 * Set variable "name" to value in "tv".
2965 * If the variable already exists, the value is updated.
2966 * Otherwise the variable is created.
2967 */
2968 void
2969set_var(
2970 char_u *name,
2971 typval_T *tv,
2972 int copy) // make copy of value in "tv"
2973{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002974 set_var_const(name, NULL, tv, copy, ASSIGN_NO_DECL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002975}
2976
2977/*
2978 * Set variable "name" to value in "tv".
2979 * If the variable already exists and "is_const" is FALSE the value is updated.
2980 * Otherwise the variable is created.
2981 */
2982 void
2983set_var_const(
2984 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002986 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002987 int copy, // make copy of value in "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002988 int flags) // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002989{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002990 typval_T *tv = tv_arg;
2991 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002993 char_u *varname;
2994 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02002996 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002997
2998 ht = find_var_ht(name, &varname);
2999 if (ht == NULL || *varname == NUL)
3000 {
3001 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003002 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003003 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003004 is_script_local = ht == get_script_local_ht();
3005
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003006 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003007 && !is_script_local
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003008 && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003009 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003010 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003011 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003012 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003013 }
3014
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003016
3017 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018 if (di == NULL)
3019 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003020
3021 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003022 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003023 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003024
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003025 if (need_convert_to_bool(type, tv))
3026 {
3027 // Destination is a bool and the value is not, but it can be converted.
3028 CLEAR_FIELD(bool_tv);
3029 bool_tv.v_type = VAR_BOOL;
3030 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3031 tv = &bool_tv;
3032 }
3033
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003035 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003037 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003038 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 {
3040 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003041 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 }
3043
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003044 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003046 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003047 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003048 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003049 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003050 }
3051
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003052 // check the type and adjust to bool if needed
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003053 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003054 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003056
Bram Moolenaara187c432020-09-16 21:08:28 +02003057 // Check in this order for backwards compatibility:
3058 // - Whether the variable is read-only
3059 // - Whether the variable value is locked
3060 // - Whether the variable is locked
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003061 if (var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02003062 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3063 || var_check_lock(di->di_flags, name, FALSE))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003064 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003065 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 else
3067 // can only redefine once
3068 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003069
3070 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003071
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003073 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003074 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003075 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003077 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003079 if (copy || tv->v_type != VAR_STRING)
3080 {
3081 char_u *val = tv_get_string(tv);
3082
3083 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003084 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085 if (di->di_tv.vval.v_string == NULL)
3086 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003087 }
3088 else
3089 {
3090 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003092 tv->vval.v_string = NULL;
3093 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003094 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003095 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003097 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003099 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003101#ifdef FEAT_SEARCH_EXTRA
3102 else if (STRCMP(varname, "hlsearch") == 0)
3103 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003105 redraw_all_later(SOME_VALID);
3106 }
3107#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003108 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003109 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003111 {
3112 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003113 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003114 }
3115 }
3116
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003118 }
3119 else // add a new variable
3120 {
3121 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003122 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003123 {
3124 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003125 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003126 }
3127
3128 // Make sure the variable name is valid.
3129 if (!valid_varname(varname))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003130 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3133 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003134 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 STRCPY(di->di_key, varname);
3136 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003137 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003139 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003140 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003142 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 di->di_flags |= DI_FLAGS_LOCK;
3144
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003145 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003147 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148
3149 // Store a pointer to the typval_T, so that it can be found by
3150 // index instead of using a hastab lookup.
3151 if (ga_grow(&si->sn_var_vals, 1) == OK)
3152 {
3153 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3154 + si->sn_var_vals.ga_len;
3155 sv->sv_name = di->di_key;
3156 sv->sv_tv = &di->di_tv;
Bram Moolenaar53b29e42020-08-15 14:31:20 +02003157 if (type == NULL)
3158 sv->sv_type = typval2type(tv, &si->sn_type_list);
3159 else
3160 sv->sv_type = type;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003161 sv->sv_const = (flags & ASSIGN_CONST);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 sv->sv_export = is_export;
3163 ++si->sn_var_vals.ga_len;
3164
3165 // let ex_export() know the export worked.
3166 is_export = FALSE;
3167 }
3168 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003169 }
3170
3171 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003173 else
3174 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 di->di_tv = *tv;
3176 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003177 init_tv(tv);
3178 }
3179
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003180 // ":const var = val" locks the value
3181 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003182 // Like :lockvar! name: lock the value and what it contains, but only
3183 // if the reference count is up to one. That locks only literal
3184 // values.
3185 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003186 return;
3187failed:
3188 if (!copy)
3189 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003190}
3191
3192/*
3193 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3194 * Also give an error message.
3195 */
3196 int
3197var_check_ro(int flags, char_u *name, int use_gettext)
3198{
3199 if (flags & DI_FLAGS_RO)
3200 {
3201 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3202 return TRUE;
3203 }
3204 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3205 {
3206 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3207 return TRUE;
3208 }
3209 return FALSE;
3210}
3211
3212/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003213 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3214 * Also give an error message.
3215 */
3216 int
3217var_check_lock(int flags, char_u *name, int use_gettext)
3218{
3219 if (flags & DI_FLAGS_LOCK)
3220 {
3221 semsg(_(e_variable_is_locked_str),
3222 use_gettext ? (char_u *)_(name) : name);
3223 return TRUE;
3224 }
3225 return FALSE;
3226}
3227
3228/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003229 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3230 * Also give an error message.
3231 */
3232 int
3233var_check_fixed(int flags, char_u *name, int use_gettext)
3234{
3235 if (flags & DI_FLAGS_FIX)
3236 {
3237 semsg(_("E795: Cannot delete variable %s"),
3238 use_gettext ? (char_u *)_(name) : name);
3239 return TRUE;
3240 }
3241 return FALSE;
3242}
3243
3244/*
3245 * Check if a funcref is assigned to a valid variable name.
3246 * Return TRUE and give an error if not.
3247 */
3248 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003249var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003250 char_u *name, // points to start of variable name
3251 int new_var) // TRUE when creating the variable
3252{
3253 // Allow for w: b: s: and t:.
3254 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3255 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3256 ? name[2] : name[0]))
3257 {
3258 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3259 name);
3260 return TRUE;
3261 }
3262 // Don't allow hiding a function. When "v" is not NULL we might be
3263 // assigning another function to the same var, the type is checked
3264 // below.
3265 if (new_var && function_exists(name, FALSE))
3266 {
3267 semsg(_("E705: Variable name conflicts with existing function: %s"),
3268 name);
3269 return TRUE;
3270 }
3271 return FALSE;
3272}
3273
3274/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003275 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3276 * value. Also give an error message, using "name" or _("name") when
3277 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003278 */
3279 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003280value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003281{
3282 if (lock & VAR_LOCKED)
3283 {
3284 semsg(_("E741: Value is locked: %s"),
3285 name == NULL ? (char_u *)_("Unknown")
3286 : use_gettext ? (char_u *)_(name)
3287 : name);
3288 return TRUE;
3289 }
3290 if (lock & VAR_FIXED)
3291 {
3292 semsg(_("E742: Cannot change value of %s"),
3293 name == NULL ? (char_u *)_("Unknown")
3294 : use_gettext ? (char_u *)_(name)
3295 : name);
3296 return TRUE;
3297 }
3298 return FALSE;
3299}
3300
3301/*
3302 * Check if a variable name is valid.
3303 * Return FALSE and give an error if not.
3304 */
3305 int
3306valid_varname(char_u *varname)
3307{
3308 char_u *p;
3309
3310 for (p = varname; *p != NUL; ++p)
3311 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3312 && *p != AUTOLOAD_CHAR)
3313 {
3314 semsg(_(e_illvar), varname);
3315 return FALSE;
3316 }
3317 return TRUE;
3318}
3319
3320/*
3321 * getwinvar() and gettabwinvar()
3322 */
3323 static void
3324getwinvar(
3325 typval_T *argvars,
3326 typval_T *rettv,
3327 int off) // 1 for gettabwinvar()
3328{
3329 win_T *win;
3330 char_u *varname;
3331 dictitem_T *v;
3332 tabpage_T *tp = NULL;
3333 int done = FALSE;
3334 win_T *oldcurwin;
3335 tabpage_T *oldtabpage;
3336 int need_switch_win;
3337
3338 if (off == 1)
3339 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3340 else
3341 tp = curtab;
3342 win = find_win_by_nr(&argvars[off], tp);
3343 varname = tv_get_string_chk(&argvars[off + 1]);
3344 ++emsg_off;
3345
3346 rettv->v_type = VAR_STRING;
3347 rettv->vval.v_string = NULL;
3348
3349 if (win != NULL && varname != NULL)
3350 {
3351 // Set curwin to be our win, temporarily. Also set the tabpage,
3352 // otherwise the window is not valid. Only do this when needed,
3353 // autocommands get blocked.
3354 need_switch_win = !(tp == curtab && win == curwin);
3355 if (!need_switch_win
3356 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3357 {
3358 if (*varname == '&')
3359 {
3360 if (varname[1] == NUL)
3361 {
3362 // get all window-local options in a dict
3363 dict_T *opts = get_winbuf_options(FALSE);
3364
3365 if (opts != NULL)
3366 {
3367 rettv_dict_set(rettv, opts);
3368 done = TRUE;
3369 }
3370 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003371 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003372 // window-local-option
3373 done = TRUE;
3374 }
3375 else
3376 {
3377 // Look up the variable.
3378 // Let getwinvar({nr}, "") return the "w:" dictionary.
3379 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3380 varname, FALSE);
3381 if (v != NULL)
3382 {
3383 copy_tv(&v->di_tv, rettv);
3384 done = TRUE;
3385 }
3386 }
3387 }
3388
3389 if (need_switch_win)
3390 // restore previous notion of curwin
3391 restore_win(oldcurwin, oldtabpage, TRUE);
3392 }
3393
3394 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3395 // use the default return value
3396 copy_tv(&argvars[off + 2], rettv);
3397
3398 --emsg_off;
3399}
3400
3401/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003402 * Set option "varname" to the value of "varp" for the current buffer/window.
3403 */
3404 static void
3405set_option_from_tv(char_u *varname, typval_T *varp)
3406{
3407 long numval = 0;
3408 char_u *strval;
3409 char_u nbuf[NUMBUFLEN];
3410 int error = FALSE;
3411
3412 if (!in_vim9script() || varp->v_type != VAR_STRING)
3413 numval = (long)tv_get_number_chk(varp, &error);
3414 strval = tv_get_string_buf_chk(varp, nbuf);
3415 if (!error && strval != NULL)
3416 set_option_value(varname, numval, strval, OPT_LOCAL);
3417}
3418
3419/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003420 * "setwinvar()" and "settabwinvar()" functions
3421 */
3422 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003423setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003424{
3425 win_T *win;
3426 win_T *save_curwin;
3427 tabpage_T *save_curtab;
3428 int need_switch_win;
3429 char_u *varname, *winvarname;
3430 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003431 tabpage_T *tp = NULL;
3432
3433 if (check_secure())
3434 return;
3435
3436 if (off == 1)
3437 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3438 else
3439 tp = curtab;
3440 win = find_win_by_nr(&argvars[off], tp);
3441 varname = tv_get_string_chk(&argvars[off + 1]);
3442 varp = &argvars[off + 2];
3443
3444 if (win != NULL && varname != NULL && varp != NULL)
3445 {
3446 need_switch_win = !(tp == curtab && win == curwin);
3447 if (!need_switch_win
3448 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3449 {
3450 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003451 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003452 else
3453 {
3454 winvarname = alloc(STRLEN(varname) + 3);
3455 if (winvarname != NULL)
3456 {
3457 STRCPY(winvarname, "w:");
3458 STRCPY(winvarname + 2, varname);
3459 set_var(winvarname, varp, TRUE);
3460 vim_free(winvarname);
3461 }
3462 }
3463 }
3464 if (need_switch_win)
3465 restore_win(save_curwin, save_curtab, TRUE);
3466 }
3467}
3468
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003469/*
3470 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3471 * v:option_type, and v:option_command.
3472 */
3473 void
3474reset_v_option_vars(void)
3475{
3476 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3477 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3478 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3479 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3480 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3481 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3482}
3483
3484/*
3485 * Add an assert error to v:errors.
3486 */
3487 void
3488assert_error(garray_T *gap)
3489{
3490 struct vimvar *vp = &vimvars[VV_ERRORS];
3491
3492 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003493 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003494 set_vim_var_list(VV_ERRORS, list_alloc());
3495 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3496}
3497
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003498 int
3499var_exists(char_u *var)
3500{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003501 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003502 char_u *name;
3503 char_u *tofree;
3504 typval_T tv;
3505 int len = 0;
3506 int n = FALSE;
3507
3508 // get_name_len() takes care of expanding curly braces
3509 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003510 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003511 if (len > 0)
3512 {
3513 if (tofree != NULL)
3514 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003515 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003516 if (n)
3517 {
3518 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003519 arg = skipwhite(arg);
3520 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003521 if (n)
3522 clear_tv(&tv);
3523 }
3524 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003525 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003526 n = FALSE;
3527
3528 vim_free(tofree);
3529 return n;
3530}
3531
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003532static lval_T *redir_lval = NULL;
3533#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3534static garray_T redir_ga; // only valid when redir_lval is not NULL
3535static char_u *redir_endp = NULL;
3536static char_u *redir_varname = NULL;
3537
3538/*
3539 * Start recording command output to a variable
3540 * When "append" is TRUE append to an existing variable.
3541 * Returns OK if successfully completed the setup. FAIL otherwise.
3542 */
3543 int
3544var_redir_start(char_u *name, int append)
3545{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003546 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003547 typval_T tv;
3548
3549 // Catch a bad name early.
3550 if (!eval_isnamec1(*name))
3551 {
3552 emsg(_(e_invarg));
3553 return FAIL;
3554 }
3555
3556 // Make a copy of the name, it is used in redir_lval until redir ends.
3557 redir_varname = vim_strsave(name);
3558 if (redir_varname == NULL)
3559 return FAIL;
3560
3561 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3562 if (redir_lval == NULL)
3563 {
3564 var_redir_stop();
3565 return FAIL;
3566 }
3567
3568 // The output is stored in growarray "redir_ga" until redirection ends.
3569 ga_init2(&redir_ga, (int)sizeof(char), 500);
3570
3571 // Parse the variable name (can be a dict or list entry).
3572 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3573 FNE_CHECK_START);
3574 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3575 {
3576 clear_lval(redir_lval);
3577 if (redir_endp != NULL && *redir_endp != NUL)
3578 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003579 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003580 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003581 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003582 redir_endp = NULL; // don't store a value, only cleanup
3583 var_redir_stop();
3584 return FAIL;
3585 }
3586
3587 // check if we can write to the variable: set it to or append an empty
3588 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003589 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003590 tv.v_type = VAR_STRING;
3591 tv.vval.v_string = (char_u *)"";
3592 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003594 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003596 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003597 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003598 {
3599 redir_endp = NULL; // don't store a value, only cleanup
3600 var_redir_stop();
3601 return FAIL;
3602 }
3603
3604 return OK;
3605}
3606
3607/*
3608 * Append "value[value_len]" to the variable set by var_redir_start().
3609 * The actual appending is postponed until redirection ends, because the value
3610 * appended may in fact be the string we write to, changing it may cause freed
3611 * memory to be used:
3612 * :redir => foo
3613 * :let foo
3614 * :redir END
3615 */
3616 void
3617var_redir_str(char_u *value, int value_len)
3618{
3619 int len;
3620
3621 if (redir_lval == NULL)
3622 return;
3623
3624 if (value_len == -1)
3625 len = (int)STRLEN(value); // Append the entire string
3626 else
3627 len = value_len; // Append only "value_len" characters
3628
3629 if (ga_grow(&redir_ga, len) == OK)
3630 {
3631 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3632 redir_ga.ga_len += len;
3633 }
3634 else
3635 var_redir_stop();
3636}
3637
3638/*
3639 * Stop redirecting command output to a variable.
3640 * Frees the allocated memory.
3641 */
3642 void
3643var_redir_stop(void)
3644{
3645 typval_T tv;
3646
3647 if (EVALCMD_BUSY)
3648 {
3649 redir_lval = NULL;
3650 return;
3651 }
3652
3653 if (redir_lval != NULL)
3654 {
3655 // If there was no error: assign the text to the variable.
3656 if (redir_endp != NULL)
3657 {
3658 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3659 tv.v_type = VAR_STRING;
3660 tv.vval.v_string = redir_ga.ga_data;
3661 // Call get_lval() again, if it's inside a Dict or List it may
3662 // have changed.
3663 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3664 FALSE, FALSE, 0, FNE_CHECK_START);
3665 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003667 (char_u *)".");
3668 clear_lval(redir_lval);
3669 }
3670
3671 // free the collected output
3672 VIM_CLEAR(redir_ga.ga_data);
3673
3674 VIM_CLEAR(redir_lval);
3675 }
3676 VIM_CLEAR(redir_varname);
3677}
3678
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003679/*
3680 * "gettabvar()" function
3681 */
3682 void
3683f_gettabvar(typval_T *argvars, typval_T *rettv)
3684{
3685 win_T *oldcurwin;
3686 tabpage_T *tp, *oldtabpage;
3687 dictitem_T *v;
3688 char_u *varname;
3689 int done = FALSE;
3690
3691 rettv->v_type = VAR_STRING;
3692 rettv->vval.v_string = NULL;
3693
3694 varname = tv_get_string_chk(&argvars[1]);
3695 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3696 if (tp != NULL && varname != NULL)
3697 {
3698 // Set tp to be our tabpage, temporarily. Also set the window to the
3699 // first window in the tabpage, otherwise the window is not valid.
3700 if (switch_win(&oldcurwin, &oldtabpage,
3701 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3702 : tp->tp_firstwin, tp, TRUE) == OK)
3703 {
3704 // look up the variable
3705 // Let gettabvar({nr}, "") return the "t:" dictionary.
3706 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3707 if (v != NULL)
3708 {
3709 copy_tv(&v->di_tv, rettv);
3710 done = TRUE;
3711 }
3712 }
3713
3714 // restore previous notion of curwin
3715 restore_win(oldcurwin, oldtabpage, TRUE);
3716 }
3717
3718 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3719 copy_tv(&argvars[2], rettv);
3720}
3721
3722/*
3723 * "gettabwinvar()" function
3724 */
3725 void
3726f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3727{
3728 getwinvar(argvars, rettv, 1);
3729}
3730
3731/*
3732 * "getwinvar()" function
3733 */
3734 void
3735f_getwinvar(typval_T *argvars, typval_T *rettv)
3736{
3737 getwinvar(argvars, rettv, 0);
3738}
3739
3740/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003741 * "getbufvar()" function
3742 */
3743 void
3744f_getbufvar(typval_T *argvars, typval_T *rettv)
3745{
3746 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003747 char_u *varname;
3748 dictitem_T *v;
3749 int done = FALSE;
3750
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003751 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003752 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003753
3754 rettv->v_type = VAR_STRING;
3755 rettv->vval.v_string = NULL;
3756
3757 if (buf != NULL && varname != NULL)
3758 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003759 if (*varname == '&')
3760 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003761 buf_T *save_curbuf = curbuf;
3762
3763 // set curbuf to be our buf, temporarily
3764 curbuf = buf;
3765
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003766 if (varname[1] == NUL)
3767 {
3768 // get all buffer-local options in a dict
3769 dict_T *opts = get_winbuf_options(TRUE);
3770
3771 if (opts != NULL)
3772 {
3773 rettv_dict_set(rettv, opts);
3774 done = TRUE;
3775 }
3776 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003777 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003778 // buffer-local-option
3779 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003780
3781 // restore previous notion of curbuf
3782 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003783 }
3784 else
3785 {
3786 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003787 if (*varname == NUL)
3788 // Let getbufvar({nr}, "") return the "b:" dictionary.
3789 v = &buf->b_bufvar;
3790 else
3791 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3792 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003793 if (v != NULL)
3794 {
3795 copy_tv(&v->di_tv, rettv);
3796 done = TRUE;
3797 }
3798 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003799 }
3800
3801 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3802 // use the default value
3803 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003804}
3805
3806/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003807 * "settabvar()" function
3808 */
3809 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003810f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003811{
3812 tabpage_T *save_curtab;
3813 tabpage_T *tp;
3814 char_u *varname, *tabvarname;
3815 typval_T *varp;
3816
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003817 if (check_secure())
3818 return;
3819
3820 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3821 varname = tv_get_string_chk(&argvars[1]);
3822 varp = &argvars[2];
3823
3824 if (varname != NULL && varp != NULL && tp != NULL)
3825 {
3826 save_curtab = curtab;
3827 goto_tabpage_tp(tp, FALSE, FALSE);
3828
3829 tabvarname = alloc(STRLEN(varname) + 3);
3830 if (tabvarname != NULL)
3831 {
3832 STRCPY(tabvarname, "t:");
3833 STRCPY(tabvarname + 2, varname);
3834 set_var(tabvarname, varp, TRUE);
3835 vim_free(tabvarname);
3836 }
3837
3838 // Restore current tabpage
3839 if (valid_tabpage(save_curtab))
3840 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3841 }
3842}
3843
3844/*
3845 * "settabwinvar()" function
3846 */
3847 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003848f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003849{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003850 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003851}
3852
3853/*
3854 * "setwinvar()" function
3855 */
3856 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003857f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003858{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003859 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003860}
3861
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003862/*
3863 * "setbufvar()" function
3864 */
3865 void
3866f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3867{
3868 buf_T *buf;
3869 char_u *varname, *bufvarname;
3870 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003871
3872 if (check_secure())
3873 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003874 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003875 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003876 varp = &argvars[2];
3877
3878 if (buf != NULL && varname != NULL && varp != NULL)
3879 {
3880 if (*varname == '&')
3881 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003882 aco_save_T aco;
3883
3884 // set curbuf to be our buf, temporarily
3885 aucmd_prepbuf(&aco, buf);
3886
Bram Moolenaar191929b2020-08-19 21:20:49 +02003887 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003888
3889 // reset notion of buffer
3890 aucmd_restbuf(&aco);
3891 }
3892 else
3893 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003894 bufvarname = alloc(STRLEN(varname) + 3);
3895 if (bufvarname != NULL)
3896 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003897 buf_T *save_curbuf = curbuf;
3898
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003899 curbuf = buf;
3900 STRCPY(bufvarname, "b:");
3901 STRCPY(bufvarname + 2, varname);
3902 set_var(bufvarname, varp, TRUE);
3903 vim_free(bufvarname);
3904 curbuf = save_curbuf;
3905 }
3906 }
3907 }
3908}
3909
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003910/*
3911 * Get a callback from "arg". It can be a Funcref or a function name.
3912 * When "arg" is zero return an empty string.
3913 * "cb_name" is not allocated.
3914 * "cb_name" is set to NULL for an invalid argument.
3915 */
3916 callback_T
3917get_callback(typval_T *arg)
3918{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003919 callback_T res;
3920 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003921
3922 res.cb_free_name = FALSE;
3923 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3924 {
3925 res.cb_partial = arg->vval.v_partial;
3926 ++res.cb_partial->pt_refcount;
3927 res.cb_name = partial_name(res.cb_partial);
3928 }
3929 else
3930 {
3931 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003932 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3933 && isdigit(*arg->vval.v_string))
3934 r = FAIL;
3935 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003936 {
3937 // Note that we don't make a copy of the string.
3938 res.cb_name = arg->vval.v_string;
3939 func_ref(res.cb_name);
3940 }
3941 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003942 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003943 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003944 r = FAIL;
3945
3946 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003947 {
3948 emsg(_("E921: Invalid callback argument"));
3949 res.cb_name = NULL;
3950 }
3951 }
3952 return res;
3953}
3954
3955/*
3956 * Copy a callback into a typval_T.
3957 */
3958 void
3959put_callback(callback_T *cb, typval_T *tv)
3960{
3961 if (cb->cb_partial != NULL)
3962 {
3963 tv->v_type = VAR_PARTIAL;
3964 tv->vval.v_partial = cb->cb_partial;
3965 ++tv->vval.v_partial->pt_refcount;
3966 }
3967 else
3968 {
3969 tv->v_type = VAR_FUNC;
3970 tv->vval.v_string = vim_strsave(cb->cb_name);
3971 func_ref(cb->cb_name);
3972 }
3973}
3974
3975/*
3976 * Make a copy of "src" into "dest", allocating the function name if needed,
3977 * without incrementing the refcount.
3978 */
3979 void
3980set_callback(callback_T *dest, callback_T *src)
3981{
3982 if (src->cb_partial == NULL)
3983 {
3984 // just a function name, make a copy
3985 dest->cb_name = vim_strsave(src->cb_name);
3986 dest->cb_free_name = TRUE;
3987 }
3988 else
3989 {
3990 // cb_name is a pointer into cb_partial
3991 dest->cb_name = src->cb_name;
3992 dest->cb_free_name = FALSE;
3993 }
3994 dest->cb_partial = src->cb_partial;
3995}
3996
3997/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02003998 * Copy callback from "src" to "dest", incrementing the refcounts.
3999 */
4000 void
4001copy_callback(callback_T *dest, callback_T *src)
4002{
4003 dest->cb_partial = src->cb_partial;
4004 if (dest->cb_partial != NULL)
4005 {
4006 dest->cb_name = src->cb_name;
4007 dest->cb_free_name = FALSE;
4008 ++dest->cb_partial->pt_refcount;
4009 }
4010 else
4011 {
4012 dest->cb_name = vim_strsave(src->cb_name);
4013 dest->cb_free_name = TRUE;
4014 func_ref(src->cb_name);
4015 }
4016}
4017
4018/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004019 * Unref/free "callback" returned by get_callback() or set_callback().
4020 */
4021 void
4022free_callback(callback_T *callback)
4023{
4024 if (callback->cb_partial != NULL)
4025 {
4026 partial_unref(callback->cb_partial);
4027 callback->cb_partial = NULL;
4028 }
4029 else if (callback->cb_name != NULL)
4030 func_unref(callback->cb_name);
4031 if (callback->cb_free_name)
4032 {
4033 vim_free(callback->cb_name);
4034 callback->cb_free_name = FALSE;
4035 }
4036 callback->cb_name = NULL;
4037}
4038
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004039#endif // FEAT_EVAL