blob: d98d9e4708fada5576efb9418e73aedc83acf2aa [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 Moolenaarf0068c52020-11-30 17:42:10 +0100149 {VV_NAME("exiting", VAR_SPECIAL), VV_RO},
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);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100222 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200223 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
224 set_vim_var_list(VV_ERRORS, list_alloc());
225 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
226
227 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
228 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
229 set_vim_var_nr(VV_NONE, VVAL_NONE);
230 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100231 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200232
233 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
234 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
235 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
236 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
237 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
238 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
239 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
240 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
241 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
242 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
243 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
244
245 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
246
Bram Moolenaar439c0362020-06-06 15:58:03 +0200247 // Default for v:register is not 0 but '"'. This is adjusted once the
248 // clipboard has been setup by calling reset_reg_var().
249 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200250}
251
252#if defined(EXITFREE) || defined(PROTO)
253/*
254 * Free all vim variables information on exit
255 */
256 void
257evalvars_clear(void)
258{
259 int i;
260 struct vimvar *p;
261
262 for (i = 0; i < VV_LEN; ++i)
263 {
264 p = &vimvars[i];
265 if (p->vv_di.di_tv.v_type == VAR_STRING)
266 VIM_CLEAR(p->vv_str);
267 else if (p->vv_di.di_tv.v_type == VAR_LIST)
268 {
269 list_unref(p->vv_list);
270 p->vv_list = NULL;
271 }
272 }
273 hash_clear(&vimvarht);
274 hash_init(&vimvarht); // garbage_collect() will access it
275 hash_clear(&compat_hashtab);
276
277 // global variables
278 vars_clear(&globvarht);
279
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100280 // Script-local variables. Clear all the variables here.
281 // The scriptvar_T is cleared later in free_scriptnames(), because a
282 // variable in one script might hold a reference to the whole scope of
283 // another script.
284 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200285 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200286}
287#endif
288
289 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200290garbage_collect_globvars(int copyID)
291{
292 return set_ref_in_ht(&globvarht, copyID, NULL);
293}
294
295 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200296garbage_collect_vimvars(int copyID)
297{
298 return set_ref_in_ht(&vimvarht, copyID, NULL);
299}
300
301 int
302garbage_collect_scriptvars(int copyID)
303{
Bram Moolenaared234f22020-10-15 20:42:20 +0200304 int i;
305 int idx;
306 int abort = FALSE;
307 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200308
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100309 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200310 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200311 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
312
Bram Moolenaared234f22020-10-15 20:42:20 +0200313 si = SCRIPT_ITEM(i);
314 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
315 {
316 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
317
318 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
319 }
320 }
321
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200322 return abort;
323}
324
325/*
326 * Set an internal variable to a string value. Creates the variable if it does
327 * not already exist.
328 */
329 void
330set_internal_string_var(char_u *name, char_u *value)
331{
332 char_u *val;
333 typval_T *tvp;
334
335 val = vim_strsave(value);
336 if (val != NULL)
337 {
338 tvp = alloc_string_tv(val);
339 if (tvp != NULL)
340 {
341 set_var(name, tvp, FALSE);
342 free_tv(tvp);
343 }
344 }
345}
346
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200347 int
348eval_charconvert(
349 char_u *enc_from,
350 char_u *enc_to,
351 char_u *fname_from,
352 char_u *fname_to)
353{
354 int err = FALSE;
355
356 set_vim_var_string(VV_CC_FROM, enc_from, -1);
357 set_vim_var_string(VV_CC_TO, enc_to, -1);
358 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
359 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
360 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
361 err = TRUE;
362 set_vim_var_string(VV_CC_FROM, NULL, -1);
363 set_vim_var_string(VV_CC_TO, NULL, -1);
364 set_vim_var_string(VV_FNAME_IN, NULL, -1);
365 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
366
367 if (err)
368 return FAIL;
369 return OK;
370}
371
372# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
373 int
374eval_printexpr(char_u *fname, char_u *args)
375{
376 int err = FALSE;
377
378 set_vim_var_string(VV_FNAME_IN, fname, -1);
379 set_vim_var_string(VV_CMDARG, args, -1);
380 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
381 err = TRUE;
382 set_vim_var_string(VV_FNAME_IN, NULL, -1);
383 set_vim_var_string(VV_CMDARG, NULL, -1);
384
385 if (err)
386 {
387 mch_remove(fname);
388 return FAIL;
389 }
390 return OK;
391}
392# endif
393
394# if defined(FEAT_DIFF) || defined(PROTO)
395 void
396eval_diff(
397 char_u *origfile,
398 char_u *newfile,
399 char_u *outfile)
400{
401 int err = FALSE;
402
403 set_vim_var_string(VV_FNAME_IN, origfile, -1);
404 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
405 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
406 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
407 set_vim_var_string(VV_FNAME_IN, NULL, -1);
408 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
409 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
410}
411
412 void
413eval_patch(
414 char_u *origfile,
415 char_u *difffile,
416 char_u *outfile)
417{
418 int err;
419
420 set_vim_var_string(VV_FNAME_IN, origfile, -1);
421 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
422 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
423 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
424 set_vim_var_string(VV_FNAME_IN, NULL, -1);
425 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
426 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
427}
428# endif
429
430#if defined(FEAT_SPELL) || defined(PROTO)
431/*
432 * Evaluate an expression to a list with suggestions.
433 * For the "expr:" part of 'spellsuggest'.
434 * Returns NULL when there is an error.
435 */
436 list_T *
437eval_spell_expr(char_u *badword, char_u *expr)
438{
439 typval_T save_val;
440 typval_T rettv;
441 list_T *list = NULL;
442 char_u *p = skipwhite(expr);
443
444 // Set "v:val" to the bad word.
445 prepare_vimvar(VV_VAL, &save_val);
446 set_vim_var_string(VV_VAL, badword, -1);
447 if (p_verbose == 0)
448 ++emsg_off;
449
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200450 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200451 {
452 if (rettv.v_type != VAR_LIST)
453 clear_tv(&rettv);
454 else
455 list = rettv.vval.v_list;
456 }
457
458 if (p_verbose == 0)
459 --emsg_off;
460 clear_tv(get_vim_var_tv(VV_VAL));
461 restore_vimvar(VV_VAL, &save_val);
462
463 return list;
464}
465
466/*
467 * "list" is supposed to contain two items: a word and a number. Return the
468 * word in "pp" and the number as the return value.
469 * Return -1 if anything isn't right.
470 * Used to get the good word and score from the eval_spell_expr() result.
471 */
472 int
473get_spellword(list_T *list, char_u **pp)
474{
475 listitem_T *li;
476
477 li = list->lv_first;
478 if (li == NULL)
479 return -1;
480 *pp = tv_get_string(&li->li_tv);
481
482 li = li->li_next;
483 if (li == NULL)
484 return -1;
485 return (int)tv_get_number(&li->li_tv);
486}
487#endif
488
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200489/*
490 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200491 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200492 * When not used yet add the variable to the v: hashtable.
493 */
494 void
495prepare_vimvar(int idx, typval_T *save_tv)
496{
497 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200498 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200499 if (vimvars[idx].vv_type == VAR_UNKNOWN)
500 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
501}
502
503/*
504 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200505 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200506 * When no longer defined, remove the variable from the v: hashtable.
507 */
508 void
509restore_vimvar(int idx, typval_T *save_tv)
510{
511 hashitem_T *hi;
512
513 vimvars[idx].vv_tv = *save_tv;
514 if (vimvars[idx].vv_type == VAR_UNKNOWN)
515 {
516 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
517 if (HASHITEM_EMPTY(hi))
518 internal_error("restore_vimvar()");
519 else
520 hash_remove(&vimvarht, hi);
521 }
522}
523
524/*
525 * List Vim variables.
526 */
527 static void
528list_vim_vars(int *first)
529{
530 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
531}
532
533/*
534 * List script-local variables, if there is a script.
535 */
536 static void
537list_script_vars(int *first)
538{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200539 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200540 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
541 "s:", FALSE, first);
542}
543
544/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200545 * Get a list of lines from a HERE document. The here document is a list of
546 * lines surrounded by a marker.
547 * cmd << {marker}
548 * {line1}
549 * {line2}
550 * ....
551 * {marker}
552 *
553 * The {marker} is a string. If the optional 'trim' word is supplied before the
554 * marker, then the leading indentation before the lines (matching the
555 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200556 *
557 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
558 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
559 * missing, then '.' is accepted as a marker.
560 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200561 * Returns a List with {lines} or NULL.
562 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200564heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200565{
566 char_u *theline;
567 char_u *marker;
568 list_T *l;
569 char_u *p;
570 int marker_indent_len = 0;
571 int text_indent_len = 0;
572 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200573 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200574 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200575
576 if (eap->getline == NULL)
577 {
578 emsg(_("E991: cannot use =<< here"));
579 return NULL;
580 }
581
582 // Check for the optional 'trim' word before the marker
583 cmd = skipwhite(cmd);
584 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
585 {
586 cmd = skipwhite(cmd + 4);
587
588 // Trim the indentation from all the lines in the here document.
589 // The amount of indentation trimmed is the same as the indentation of
590 // the first line after the :let command line. To find the end marker
591 // the indent of the :let command line is trimmed.
592 p = *eap->cmdlinep;
593 while (VIM_ISWHITE(*p))
594 {
595 p++;
596 marker_indent_len++;
597 }
598 text_indent_len = -1;
599 }
600
601 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200602 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200603 {
604 marker = skipwhite(cmd);
605 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200606 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200607 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200608 semsg(_(e_trailing_arg), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200609 return NULL;
610 }
611 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200612 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200613 {
614 emsg(_("E221: Marker cannot start with lower case letter"));
615 return NULL;
616 }
617 }
618 else
619 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200620 // When getting lines for an embedded script, if the marker is missing,
621 // accept '.' as the marker.
622 if (script_get)
623 marker = dot;
624 else
625 {
626 emsg(_("E172: Missing marker"));
627 return NULL;
628 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200629 }
630
631 l = list_alloc();
632 if (l == NULL)
633 return NULL;
634
635 for (;;)
636 {
637 int mi = 0;
638 int ti = 0;
639
640 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
641 if (theline == NULL)
642 {
643 semsg(_("E990: Missing end marker '%s'"), marker);
644 break;
645 }
646
647 // with "trim": skip the indent matching the :let line to find the
648 // marker
649 if (marker_indent_len > 0
650 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
651 mi = marker_indent_len;
652 if (STRCMP(marker, theline + mi) == 0)
653 {
654 vim_free(theline);
655 break;
656 }
657
658 if (text_indent_len == -1 && *theline != NUL)
659 {
660 // set the text indent from the first line.
661 p = theline;
662 text_indent_len = 0;
663 while (VIM_ISWHITE(*p))
664 {
665 p++;
666 text_indent_len++;
667 }
668 text_indent = vim_strnsave(theline, text_indent_len);
669 }
670 // with "trim": skip the indent matching the first line
671 if (text_indent != NULL)
672 for (ti = 0; ti < text_indent_len; ++ti)
673 if (theline[ti] != text_indent[ti])
674 break;
675
676 if (list_append_string(l, theline + ti, -1) == FAIL)
677 break;
678 vim_free(theline);
679 }
680 vim_free(text_indent);
681
682 return l;
683}
684
685/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200686 * Vim9 variable declaration:
687 * ":var name"
688 * ":var name: type"
689 * ":var name = expr"
690 * ":var name: type = expr"
691 * etc.
692 */
693 void
694ex_var(exarg_T *eap)
695{
696 if (!in_vim9script())
697 {
698 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
699 return;
700 }
701 ex_let(eap);
702}
703
704/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200705 * ":let" list all variable values
706 * ":let var1 var2" list variable values
707 * ":let var = expr" assignment command.
708 * ":let var += expr" assignment command.
709 * ":let var -= expr" assignment command.
710 * ":let var *= expr" assignment command.
711 * ":let var /= expr" assignment command.
712 * ":let var %= expr" assignment command.
713 * ":let var .= expr" assignment command.
714 * ":let var ..= expr" assignment command.
715 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100716 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100717 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200718 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200719 * ":final var = expr" assignment command.
720 * ":final [var1, var2] = expr" unpack list.
721 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200722 * ":const" list all variable values
723 * ":const var1 var2" list variable values
724 * ":const var = expr" assignment command.
725 * ":const [var1, var2] = expr" unpack list.
726 */
727 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200728ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200729{
730 char_u *arg = eap->arg;
731 char_u *expr = NULL;
732 typval_T rettv;
733 int i;
734 int var_count = 0;
735 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200736 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200737 char_u *argend;
738 int first = TRUE;
739 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200740 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100741 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200742 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200743
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200744 if (eap->cmdidx == CMD_final && !vim9script)
745 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100746 // In legacy Vim script ":final" is short for ":finally".
747 ex_finally(eap);
748 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200749 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200750 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200751 {
752 emsg(_(e_cannot_use_let_in_vim9_script));
753 return;
754 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200755
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100756 if (eap->cmdidx == CMD_const)
757 flags |= ASSIGN_CONST;
758 else if (eap->cmdidx == CMD_final)
759 flags |= ASSIGN_FINAL;
760
761 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200763 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200765 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200766 if (argend == NULL)
767 return;
768 if (argend > arg && argend[-1] == '.') // for var.='str'
769 --argend;
770 expr = skipwhite(argend);
771 concat = expr[0] == '.'
772 && ((expr[1] == '=' && current_sctx.sc_version < 2)
773 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200774 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
775 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200776 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200777 {
778 // ":let" without "=": list variables
779 if (*arg == '[')
780 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200781 else if (expr[0] == '.' && expr[1] == '=')
782 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200783 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200784 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200785 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200786 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100787 // Vim9 declaration ":var name: type"
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200788 arg = vim9_declare_scriptvar(eap, arg);
789 }
790 else
791 {
792 // ":let var1 var2" - list values
793 arg = list_arg_vars(eap, arg, &first);
794 }
795 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200796 else if (!eap->skip)
797 {
798 // ":let"
799 list_glob_vars(&first);
800 list_buf_vars(&first);
801 list_win_vars(&first);
802 list_tab_vars(&first);
803 list_script_vars(&first);
804 list_func_vars(&first);
805 list_vim_vars(&first);
806 }
807 eap->nextcmd = check_nextcmd(arg);
808 }
809 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
810 {
811 list_T *l;
812
813 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200814 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200815 if (l != NULL)
816 {
817 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200818 if (!eap->skip)
819 {
820 op[0] = '=';
821 op[1] = NUL;
822 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200824 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200825 clear_tv(&rettv);
826 }
827 }
828 else
829 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200830 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200831 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200832
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200833 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200834 i = FAIL;
835 if (has_assign || concat)
836 {
837 op[0] = '=';
838 op[1] = NUL;
839 if (*expr != '=')
840 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200841 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200842 {
843 // +=, /=, etc. require an existing variable
844 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
845 i = FAIL;
846 }
847 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200848 {
849 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200850 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200851 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200852 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200853 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200854 ++len;
855 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200856 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200857 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200858 }
859 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200860 ++expr;
861
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200862 if (vim9script && (!VIM_ISWHITE(*argend)
863 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200864 {
865 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100866 semsg(_(e_white_space_required_before_and_after_str_at_str),
867 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200868 i = FAIL;
869 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200870
871 if (eap->skip)
872 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200873 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200874 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200875 if (getline_equal(eap->getline, eap->cookie, getsourceline))
876 {
877 evalarg.eval_getline = eap->getline;
878 evalarg.eval_cookie = eap->cookie;
879 }
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200880 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200881 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200882 if (eap->skip)
883 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200884 clear_evalarg(&evalarg, eap);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200885 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200886 if (eap->skip)
887 {
888 if (i != FAIL)
889 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200890 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200891 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200892 {
893 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200894 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200895 clear_tv(&rettv);
896 }
897 }
898}
899
900/*
901 * Assign the typevalue "tv" to the variable or variables at "arg_start".
902 * Handles both "var" with any type and "[var, var; var]" with a list type.
903 * When "op" is not NULL it points to a string with characters that
904 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
905 * or concatenate.
906 * Returns OK or FAIL;
907 */
908 int
909ex_let_vars(
910 char_u *arg_start,
911 typval_T *tv,
912 int copy, // copy values from "tv", don't move
913 int semicolon, // from skip_var_list()
914 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100915 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200916 char_u *op)
917{
918 char_u *arg = arg_start;
919 list_T *l;
920 int i;
921 listitem_T *item;
922 typval_T ltv;
923
924 if (*arg != '[')
925 {
926 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200928 return FAIL;
929 return OK;
930 }
931
932 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
933 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
934 {
935 emsg(_(e_listreq));
936 return FAIL;
937 }
938
939 i = list_len(l);
940 if (semicolon == 0 && var_count < i)
941 {
942 emsg(_("E687: Less targets than List items"));
943 return FAIL;
944 }
945 if (var_count - semicolon > i)
946 {
947 emsg(_("E688: More targets than List items"));
948 return FAIL;
949 }
950
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200951 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200952 item = l->lv_first;
953 while (*arg != ']')
954 {
955 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200957 item = item->li_next;
958 if (arg == NULL)
959 return FAIL;
960
961 arg = skipwhite(arg);
962 if (*arg == ';')
963 {
964 // Put the rest of the list (may be empty) in the var after ';'.
965 // Create a new list for this.
966 l = list_alloc();
967 if (l == NULL)
968 return FAIL;
969 while (item != NULL)
970 {
971 list_append_tv(l, &item->li_tv);
972 item = item->li_next;
973 }
974
975 ltv.v_type = VAR_LIST;
976 ltv.v_lock = 0;
977 ltv.vval.v_list = l;
978 l->lv_refcount = 1;
979
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200981 (char_u *)"]", op);
982 clear_tv(&ltv);
983 if (arg == NULL)
984 return FAIL;
985 break;
986 }
987 else if (*arg != ',' && *arg != ']')
988 {
989 internal_error("ex_let_vars()");
990 return FAIL;
991 }
992 }
993
994 return OK;
995}
996
997/*
998 * Skip over assignable variable "var" or list of variables "[var, var]".
999 * Used for ":let varvar = expr" and ":for varvar in expr".
1000 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001001 * for "[var, var; var]" set "semicolon" to 1.
1002 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001003 * Return NULL for an error.
1004 */
1005 char_u *
1006skip_var_list(
1007 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001008 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001009 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001010 int *semicolon,
1011 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001012{
1013 char_u *p, *s;
1014
1015 if (*arg == '[')
1016 {
1017 // "[var, var]": find the matching ']'.
1018 p = arg;
1019 for (;;)
1020 {
1021 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001022 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001023 if (s == p)
1024 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001025 if (!silent)
1026 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001027 return NULL;
1028 }
1029 ++*var_count;
1030
1031 p = skipwhite(s);
1032 if (*p == ']')
1033 break;
1034 else if (*p == ';')
1035 {
1036 if (*semicolon == 1)
1037 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001038 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001039 return NULL;
1040 }
1041 *semicolon = 1;
1042 }
1043 else if (*p != ',')
1044 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001045 if (!silent)
1046 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001047 return NULL;
1048 }
1049 }
1050 return p + 1;
1051 }
1052 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001054}
1055
1056/*
1057 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1058 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001060 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001061 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001062skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001063{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001064 char_u *end;
1065
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001066 if (*arg == '@' && arg[1] != NUL)
1067 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001069 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001070
1071 // "a: type" is declaring variable "a" with a type, not "a:".
1072 // Same for "s: type".
1073 if (end == arg + 2 && end[-1] == ':')
1074 --end;
1075
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001076 if (include_type && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001078 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001079 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001080 }
1081 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001082}
1083
1084/*
1085 * List variables for hashtab "ht" with prefix "prefix".
1086 * If "empty" is TRUE also list NULL strings as empty strings.
1087 */
1088 void
1089list_hashtable_vars(
1090 hashtab_T *ht,
1091 char *prefix,
1092 int empty,
1093 int *first)
1094{
1095 hashitem_T *hi;
1096 dictitem_T *di;
1097 int todo;
1098 char_u buf[IOSIZE];
1099
1100 todo = (int)ht->ht_used;
1101 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1102 {
1103 if (!HASHITEM_EMPTY(hi))
1104 {
1105 --todo;
1106 di = HI2DI(hi);
1107
1108 // apply :filter /pat/ to variable name
1109 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1110 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1111 if (message_filtered(buf))
1112 continue;
1113
1114 if (empty || di->di_tv.v_type != VAR_STRING
1115 || di->di_tv.vval.v_string != NULL)
1116 list_one_var(di, prefix, first);
1117 }
1118 }
1119}
1120
1121/*
1122 * List global variables.
1123 */
1124 static void
1125list_glob_vars(int *first)
1126{
1127 list_hashtable_vars(&globvarht, "", TRUE, first);
1128}
1129
1130/*
1131 * List buffer variables.
1132 */
1133 static void
1134list_buf_vars(int *first)
1135{
1136 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1137}
1138
1139/*
1140 * List window variables.
1141 */
1142 static void
1143list_win_vars(int *first)
1144{
1145 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1146}
1147
1148/*
1149 * List tab page variables.
1150 */
1151 static void
1152list_tab_vars(int *first)
1153{
1154 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1155}
1156
1157/*
1158 * List variables in "arg".
1159 */
1160 static char_u *
1161list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1162{
1163 int error = FALSE;
1164 int len;
1165 char_u *name;
1166 char_u *name_start;
1167 char_u *arg_subsc;
1168 char_u *tofree;
1169 typval_T tv;
1170
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001171 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001172 {
1173 if (error || eap->skip)
1174 {
1175 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1176 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1177 {
1178 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001179 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001180 break;
1181 }
1182 }
1183 else
1184 {
1185 // get_name_len() takes care of expanding curly braces
1186 name_start = name = arg;
1187 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1188 if (len <= 0)
1189 {
1190 // This is mainly to keep test 49 working: when expanding
1191 // curly braces fails overrule the exception error message.
1192 if (len < 0 && !aborting())
1193 {
1194 emsg_severe = TRUE;
1195 semsg(_(e_invarg2), arg);
1196 break;
1197 }
1198 error = TRUE;
1199 }
1200 else
1201 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001202 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001203 if (tofree != NULL)
1204 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001205 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001206 error = TRUE;
1207 else
1208 {
1209 // handle d.key, l[idx], f(expr)
1210 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001211 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001212 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001213 error = TRUE;
1214 else
1215 {
1216 if (arg == arg_subsc && len == 2 && name[1] == ':')
1217 {
1218 switch (*name)
1219 {
1220 case 'g': list_glob_vars(first); break;
1221 case 'b': list_buf_vars(first); break;
1222 case 'w': list_win_vars(first); break;
1223 case 't': list_tab_vars(first); break;
1224 case 'v': list_vim_vars(first); break;
1225 case 's': list_script_vars(first); break;
1226 case 'l': list_func_vars(first); break;
1227 default:
1228 semsg(_("E738: Can't list variables for %s"), name);
1229 }
1230 }
1231 else
1232 {
1233 char_u numbuf[NUMBUFLEN];
1234 char_u *tf;
1235 int c;
1236 char_u *s;
1237
1238 s = echo_string(&tv, &tf, numbuf, 0);
1239 c = *arg;
1240 *arg = NUL;
1241 list_one_var_a("",
1242 arg == arg_subsc ? name : name_start,
1243 tv.v_type,
1244 s == NULL ? (char_u *)"" : s,
1245 first);
1246 *arg = c;
1247 vim_free(tf);
1248 }
1249 clear_tv(&tv);
1250 }
1251 }
1252 }
1253
1254 vim_free(tofree);
1255 }
1256
1257 arg = skipwhite(arg);
1258 }
1259
1260 return arg;
1261}
1262
1263/*
1264 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1265 * Returns a pointer to the char just after the var name.
1266 * Returns NULL if there is an error.
1267 */
1268 static char_u *
1269ex_let_one(
1270 char_u *arg, // points to variable name
1271 typval_T *tv, // value to assign to variable
1272 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001273 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001274 char_u *endchars, // valid chars after variable name or NULL
1275 char_u *op) // "+", "-", "." or NULL
1276{
1277 int c1;
1278 char_u *name;
1279 char_u *p;
1280 char_u *arg_end = NULL;
1281 int len;
1282 int opt_flags;
1283 char_u *tofree = NULL;
1284
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001285 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001286 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001287 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1288 {
1289 vim9_declare_error(arg);
1290 return NULL;
1291 }
1292
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001293 // ":let $VAR = expr": Set environment variable.
1294 if (*arg == '$')
1295 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001296 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001297 {
1298 emsg(_("E996: Cannot lock an environment variable"));
1299 return NULL;
1300 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001301
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001302 // Find the end of the name.
1303 ++arg;
1304 name = arg;
1305 len = get_env_len(&arg);
1306 if (len == 0)
1307 semsg(_(e_invarg2), name - 1);
1308 else
1309 {
1310 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1311 semsg(_(e_letwrong), op);
1312 else if (endchars != NULL
1313 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1314 emsg(_(e_letunexp));
1315 else if (!check_secure())
1316 {
1317 c1 = name[len];
1318 name[len] = NUL;
1319 p = tv_get_string_chk(tv);
1320 if (p != NULL && op != NULL && *op == '.')
1321 {
1322 int mustfree = FALSE;
1323 char_u *s = vim_getenv(name, &mustfree);
1324
1325 if (s != NULL)
1326 {
1327 p = tofree = concat_str(s, p);
1328 if (mustfree)
1329 vim_free(s);
1330 }
1331 }
1332 if (p != NULL)
1333 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001334 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001335 arg_end = arg;
1336 }
1337 name[len] = c1;
1338 vim_free(tofree);
1339 }
1340 }
1341 }
1342
1343 // ":let &option = expr": Set option value.
1344 // ":let &l:option = expr": Set local option value.
1345 // ":let &g:option = expr": Set global option value.
1346 else if (*arg == '&')
1347 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001348 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001349 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001351 return NULL;
1352 }
1353 // Find the end of the name.
1354 p = find_option_end(&arg, &opt_flags);
1355 if (p == NULL || (endchars != NULL
1356 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1357 emsg(_(e_letunexp));
1358 else
1359 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001360 long n = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001361 getoption_T opt_type;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001362 long numval;
1363 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001364 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001365 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001366
1367 c1 = *p;
1368 *p = NUL;
1369
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001370 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001371 if ((opt_type == gov_bool
1372 || opt_type == gov_number
1373 || opt_type == gov_hidden_bool
1374 || opt_type == gov_hidden_number)
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001375 && (tv->v_type != VAR_STRING || !in_vim9script()))
Bram Moolenaar0ea04402021-01-04 13:37:54 +01001376 {
1377 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1378 // bool, possibly hidden
1379 n = (long)tv_get_bool(tv);
1380 else
1381 // number, possibly hidden
1382 n = (long)tv_get_number(tv);
1383 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001384
1385 // Avoid setting a string option to the text "v:false" or similar.
1386 // In Vim9 script also don't convert a number to string.
1387 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1388 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1389 s = tv_get_string_chk(tv);
1390
1391 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001392 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001393 if (((opt_type == gov_bool || opt_type == gov_number)
1394 && *op == '.')
1395 || (opt_type == gov_string && *op != '.'))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001396 {
1397 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001398 failed = TRUE; // don't set the value
1399
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001400 }
1401 else
1402 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001403 // number, in legacy script also bool
1404 if (opt_type == gov_number
1405 || (opt_type == gov_bool && !in_vim9script()))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001406 {
1407 switch (*op)
1408 {
1409 case '+': n = numval + n; break;
1410 case '-': n = numval - n; break;
1411 case '*': n = numval * n; break;
1412 case '/': n = (long)num_divide(numval, n); break;
1413 case '%': n = (long)num_modulus(numval, n); break;
1414 }
1415 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001416 else if (opt_type == gov_string
1417 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001418 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001419 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001420 s = concat_str(stringval, s);
1421 vim_free(stringval);
1422 stringval = s;
1423 }
1424 }
1425 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001426
1427 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001428 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001429 if (opt_type != gov_string || s != NULL)
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001430 {
1431 set_option_value(arg, n, s, opt_flags);
1432 arg_end = p;
1433 }
1434 else
1435 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001436 }
1437 *p = c1;
1438 vim_free(stringval);
1439 }
1440 }
1441
1442 // ":let @r = expr": Set register contents.
1443 else if (*arg == '@')
1444 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001445 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001446 {
1447 emsg(_("E996: Cannot lock a register"));
1448 return NULL;
1449 }
1450 ++arg;
1451 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1452 semsg(_(e_letwrong), op);
1453 else if (endchars != NULL
1454 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1455 emsg(_(e_letunexp));
1456 else
1457 {
1458 char_u *ptofree = NULL;
1459 char_u *s;
1460
1461 p = tv_get_string_chk(tv);
1462 if (p != NULL && op != NULL && *op == '.')
1463 {
1464 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1465 if (s != NULL)
1466 {
1467 p = ptofree = concat_str(s, p);
1468 vim_free(s);
1469 }
1470 }
1471 if (p != NULL)
1472 {
1473 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1474 arg_end = arg + 1;
1475 }
1476 vim_free(ptofree);
1477 }
1478 }
1479
1480 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001481 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001482 // ":let {expr} = expr": Idem, name made with curly braces
1483 else if (eval_isnamec1(*arg) || *arg == '{')
1484 {
1485 lval_T lv;
1486
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001487 p = get_lval(arg, tv, &lv, FALSE, FALSE,
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001488 (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1489 ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001490 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001491 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 if (endchars != NULL && vim_strchr(endchars,
1493 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001494 emsg(_(e_letunexp));
1495 else
1496 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001497 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001498 arg_end = p;
1499 }
1500 }
1501 clear_lval(&lv);
1502 }
1503
1504 else
1505 semsg(_(e_invarg2), arg);
1506
1507 return arg_end;
1508}
1509
1510/*
1511 * ":unlet[!] var1 ... " command.
1512 */
1513 void
1514ex_unlet(exarg_T *eap)
1515{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001516 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001517}
1518
1519/*
1520 * ":lockvar" and ":unlockvar" commands
1521 */
1522 void
1523ex_lockvar(exarg_T *eap)
1524{
1525 char_u *arg = eap->arg;
1526 int deep = 2;
1527
1528 if (eap->forceit)
1529 deep = -1;
1530 else if (vim_isdigit(*arg))
1531 {
1532 deep = getdigits(&arg);
1533 arg = skipwhite(arg);
1534 }
1535
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001536 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001537}
1538
1539/*
1540 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001541 * Also used for Vim9 script. "callback" is invoked as:
1542 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001543 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001544 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001545ex_unletlock(
1546 exarg_T *eap,
1547 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001548 int deep,
1549 int glv_flags,
1550 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1551 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001552{
1553 char_u *arg = argstart;
1554 char_u *name_end;
1555 int error = FALSE;
1556 lval_T lv;
1557
1558 do
1559 {
1560 if (*arg == '$')
1561 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001562 lv.ll_name = arg;
1563 lv.ll_tv = NULL;
1564 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001565 if (get_env_len(&arg) == 0)
1566 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001567 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001568 return;
1569 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001570 if (!error && !eap->skip
1571 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1572 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001573 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001574 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001575 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001576 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001577 // Parse the name and find the end.
1578 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001579 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001580 if (lv.ll_name == NULL)
1581 error = TRUE; // error but continue parsing
1582 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1583 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001584 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001585 if (name_end != NULL)
1586 {
1587 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001588 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001589 }
1590 if (!(eap->skip || error))
1591 clear_lval(&lv);
1592 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001593 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001594
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001595 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001596 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001597 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001598
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001599 if (!eap->skip)
1600 clear_lval(&lv);
1601 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001602
1603 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001604 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001605
1606 eap->nextcmd = check_nextcmd(arg);
1607}
1608
1609 static int
1610do_unlet_var(
1611 lval_T *lp,
1612 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001613 exarg_T *eap,
1614 int deep UNUSED,
1615 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001616{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001617 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001618 int ret = OK;
1619 int cc;
1620
1621 if (lp->ll_tv == NULL)
1622 {
1623 cc = *name_end;
1624 *name_end = NUL;
1625
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001626 // Environment variable, normal name or expanded name.
1627 if (*lp->ll_name == '$')
1628 vim_unsetenv(lp->ll_name + 1);
1629 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001630 ret = FAIL;
1631 *name_end = cc;
1632 }
1633 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001634 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001635 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001636 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001637 return FAIL;
1638 else if (lp->ll_range)
1639 {
1640 listitem_T *li;
1641 listitem_T *ll_li = lp->ll_li;
1642 int ll_n1 = lp->ll_n1;
1643
1644 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1645 {
1646 li = ll_li->li_next;
Bram Moolenaara187c432020-09-16 21:08:28 +02001647 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001648 return FAIL;
1649 ll_li = li;
1650 ++ll_n1;
1651 }
1652
1653 // Delete a range of List items.
1654 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1655 {
1656 li = lp->ll_li->li_next;
1657 listitem_remove(lp->ll_list, lp->ll_li);
1658 lp->ll_li = li;
1659 ++lp->ll_n1;
1660 }
1661 }
1662 else
1663 {
1664 if (lp->ll_list != NULL)
1665 // unlet a List item.
1666 listitem_remove(lp->ll_list, lp->ll_li);
1667 else
1668 // unlet a Dictionary item.
1669 dictitem_remove(lp->ll_dict, lp->ll_di);
1670 }
1671
1672 return ret;
1673}
1674
1675/*
1676 * "unlet" a variable. Return OK if it existed, FAIL if not.
1677 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1678 */
1679 int
1680do_unlet(char_u *name, int forceit)
1681{
1682 hashtab_T *ht;
1683 hashitem_T *hi;
1684 char_u *varname;
1685 dict_T *d;
1686 dictitem_T *di;
1687
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001688 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001689 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001690 return FAIL;
1691
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001692 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001693
1694 // can't :unlet a script variable in Vim9 script from a function
1695 if (ht == get_script_local_ht()
1696 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1697 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1698 == SCRIPT_VERSION_VIM9
1699 && check_vim9_unlet(name) == FAIL)
1700 return FAIL;
1701
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001702 if (ht != NULL && *varname != NUL)
1703 {
1704 d = get_current_funccal_dict(ht);
1705 if (d == NULL)
1706 {
1707 if (ht == &globvarht)
1708 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001709 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001710 d = &vimvardict;
1711 else
1712 {
1713 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1714 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1715 }
1716 if (d == NULL)
1717 {
1718 internal_error("do_unlet()");
1719 return FAIL;
1720 }
1721 }
1722 hi = hash_find(ht, varname);
1723 if (HASHITEM_EMPTY(hi))
1724 hi = find_hi_in_scoped_ht(name, &ht);
1725 if (hi != NULL && !HASHITEM_EMPTY(hi))
1726 {
1727 di = HI2DI(hi);
1728 if (var_check_fixed(di->di_flags, name, FALSE)
1729 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001730 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001731 return FAIL;
1732
1733 delete_var(ht, hi);
1734 return OK;
1735 }
1736 }
1737 if (forceit)
1738 return OK;
1739 semsg(_("E108: No such variable: \"%s\""), name);
1740 return FAIL;
1741}
1742
1743/*
1744 * Lock or unlock variable indicated by "lp".
1745 * "deep" is the levels to go (-1 for unlimited);
1746 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1747 */
1748 static int
1749do_lock_var(
1750 lval_T *lp,
1751 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001752 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001753 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001754 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001755{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001756 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001757 int ret = OK;
1758 int cc;
1759 dictitem_T *di;
1760
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001761 if (lp->ll_tv == NULL)
1762 {
1763 cc = *name_end;
1764 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001765 if (*lp->ll_name == '$')
1766 {
1767 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001768 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001769 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001770 else
1771 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001772 // Normal name or expanded name.
1773 di = find_var(lp->ll_name, NULL, TRUE);
1774 if (di == NULL)
1775 ret = FAIL;
1776 else if ((di->di_flags & DI_FLAGS_FIX)
1777 && di->di_tv.v_type != VAR_DICT
1778 && di->di_tv.v_type != VAR_LIST)
1779 {
1780 // For historic reasons this error is not given for a list or
1781 // dict. E.g., the b: dict could be locked/unlocked.
1782 semsg(_(e_lock_unlock), lp->ll_name);
1783 ret = FAIL;
1784 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001785 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001786 {
1787 if (lock)
1788 di->di_flags |= DI_FLAGS_LOCK;
1789 else
1790 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001791 if (deep != 0)
1792 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001793 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001794 }
1795 *name_end = cc;
1796 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001797 else if (deep == 0)
1798 {
1799 // nothing to do
1800 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001801 else if (lp->ll_range)
1802 {
1803 listitem_T *li = lp->ll_li;
1804
1805 // (un)lock a range of List items.
1806 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1807 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001808 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001809 li = li->li_next;
1810 ++lp->ll_n1;
1811 }
1812 }
1813 else if (lp->ll_list != NULL)
1814 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001815 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001816 else
1817 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001818 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001819
1820 return ret;
1821}
1822
1823/*
1824 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001825 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1826 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001827 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001828 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001829item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001830{
1831 static int recurse = 0;
1832 list_T *l;
1833 listitem_T *li;
1834 dict_T *d;
1835 blob_T *b;
1836 hashitem_T *hi;
1837 int todo;
1838
1839 if (recurse >= DICT_MAXNEST)
1840 {
1841 emsg(_("E743: variable nested too deep for (un)lock"));
1842 return;
1843 }
1844 if (deep == 0)
1845 return;
1846 ++recurse;
1847
1848 // lock/unlock the item itself
1849 if (lock)
1850 tv->v_lock |= VAR_LOCKED;
1851 else
1852 tv->v_lock &= ~VAR_LOCKED;
1853
1854 switch (tv->v_type)
1855 {
1856 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001857 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001858 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001859 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001861 case VAR_STRING:
1862 case VAR_FUNC:
1863 case VAR_PARTIAL:
1864 case VAR_FLOAT:
1865 case VAR_SPECIAL:
1866 case VAR_JOB:
1867 case VAR_CHANNEL:
1868 break;
1869
1870 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001871 if ((b = tv->vval.v_blob) != NULL
1872 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001873 {
1874 if (lock)
1875 b->bv_lock |= VAR_LOCKED;
1876 else
1877 b->bv_lock &= ~VAR_LOCKED;
1878 }
1879 break;
1880 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001881 if ((l = tv->vval.v_list) != NULL
1882 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001883 {
1884 if (lock)
1885 l->lv_lock |= VAR_LOCKED;
1886 else
1887 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001888 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001889 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001890 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001891 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001892 }
1893 break;
1894 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001895 if ((d = tv->vval.v_dict) != NULL
1896 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001897 {
1898 if (lock)
1899 d->dv_lock |= VAR_LOCKED;
1900 else
1901 d->dv_lock &= ~VAR_LOCKED;
1902 if (deep < 0 || deep > 1)
1903 {
1904 // recursive: lock/unlock the items the List contains
1905 todo = (int)d->dv_hashtab.ht_used;
1906 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1907 {
1908 if (!HASHITEM_EMPTY(hi))
1909 {
1910 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001911 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1912 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001913 }
1914 }
1915 }
1916 }
1917 }
1918 --recurse;
1919}
1920
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001921#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1922/*
1923 * Delete all "menutrans_" variables.
1924 */
1925 void
1926del_menutrans_vars(void)
1927{
1928 hashitem_T *hi;
1929 int todo;
1930
1931 hash_lock(&globvarht);
1932 todo = (int)globvarht.ht_used;
1933 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1934 {
1935 if (!HASHITEM_EMPTY(hi))
1936 {
1937 --todo;
1938 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1939 delete_var(&globvarht, hi);
1940 }
1941 }
1942 hash_unlock(&globvarht);
1943}
1944#endif
1945
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001946/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001947 * Local string buffer for the next two functions to store a variable name
1948 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1949 * get_user_var_name().
1950 */
1951
1952static char_u *varnamebuf = NULL;
1953static int varnamebuflen = 0;
1954
1955/*
1956 * Function to concatenate a prefix and a variable name.
1957 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001958 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001959cat_prefix_varname(int prefix, char_u *name)
1960{
1961 int len;
1962
1963 len = (int)STRLEN(name) + 3;
1964 if (len > varnamebuflen)
1965 {
1966 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001967 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001968 varnamebuf = alloc(len);
1969 if (varnamebuf == NULL)
1970 {
1971 varnamebuflen = 0;
1972 return NULL;
1973 }
1974 varnamebuflen = len;
1975 }
1976 *varnamebuf = prefix;
1977 varnamebuf[1] = ':';
1978 STRCPY(varnamebuf + 2, name);
1979 return varnamebuf;
1980}
1981
1982/*
1983 * Function given to ExpandGeneric() to obtain the list of user defined
1984 * (global/buffer/window/built-in) variable names.
1985 */
1986 char_u *
1987get_user_var_name(expand_T *xp, int idx)
1988{
1989 static long_u gdone;
1990 static long_u bdone;
1991 static long_u wdone;
1992 static long_u tdone;
1993 static int vidx;
1994 static hashitem_T *hi;
1995 hashtab_T *ht;
1996
1997 if (idx == 0)
1998 {
1999 gdone = bdone = wdone = vidx = 0;
2000 tdone = 0;
2001 }
2002
2003 // Global variables
2004 if (gdone < globvarht.ht_used)
2005 {
2006 if (gdone++ == 0)
2007 hi = globvarht.ht_array;
2008 else
2009 ++hi;
2010 while (HASHITEM_EMPTY(hi))
2011 ++hi;
2012 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2013 return cat_prefix_varname('g', hi->hi_key);
2014 return hi->hi_key;
2015 }
2016
2017 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002018 ht =
2019#ifdef FEAT_CMDWIN
2020 // In cmdwin, the alternative buffer should be used.
2021 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2022 &prevwin->w_buffer->b_vars->dv_hashtab :
2023#endif
2024 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002025 if (bdone < ht->ht_used)
2026 {
2027 if (bdone++ == 0)
2028 hi = ht->ht_array;
2029 else
2030 ++hi;
2031 while (HASHITEM_EMPTY(hi))
2032 ++hi;
2033 return cat_prefix_varname('b', hi->hi_key);
2034 }
2035
2036 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002037 ht =
2038#ifdef FEAT_CMDWIN
2039 // In cmdwin, the alternative window should be used.
2040 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2041 &prevwin->w_vars->dv_hashtab :
2042#endif
2043 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002044 if (wdone < ht->ht_used)
2045 {
2046 if (wdone++ == 0)
2047 hi = ht->ht_array;
2048 else
2049 ++hi;
2050 while (HASHITEM_EMPTY(hi))
2051 ++hi;
2052 return cat_prefix_varname('w', hi->hi_key);
2053 }
2054
2055 // t: variables
2056 ht = &curtab->tp_vars->dv_hashtab;
2057 if (tdone < ht->ht_used)
2058 {
2059 if (tdone++ == 0)
2060 hi = ht->ht_array;
2061 else
2062 ++hi;
2063 while (HASHITEM_EMPTY(hi))
2064 ++hi;
2065 return cat_prefix_varname('t', hi->hi_key);
2066 }
2067
2068 // v: variables
2069 if (vidx < VV_LEN)
2070 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2071
2072 VIM_CLEAR(varnamebuf);
2073 varnamebuflen = 0;
2074 return NULL;
2075}
2076
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002077 char *
2078get_var_special_name(int nr)
2079{
2080 switch (nr)
2081 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002082 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2083 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002084 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002085 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002086 }
2087 internal_error("get_var_special_name()");
2088 return "42";
2089}
2090
2091/*
2092 * Returns the global variable dictionary
2093 */
2094 dict_T *
2095get_globvar_dict(void)
2096{
2097 return &globvardict;
2098}
2099
2100/*
2101 * Returns the global variable hash table
2102 */
2103 hashtab_T *
2104get_globvar_ht(void)
2105{
2106 return &globvarht;
2107}
2108
2109/*
2110 * Returns the v: variable dictionary
2111 */
2112 dict_T *
2113get_vimvar_dict(void)
2114{
2115 return &vimvardict;
2116}
2117
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002118/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002120 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121 */
2122 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002123find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002125 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2126 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002127
2128 if (di == NULL)
2129 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002130 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2132 return (int)(vv - vimvars);
2133}
2134
2135
2136/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002137 * Set type of v: variable to "type".
2138 */
2139 void
2140set_vim_var_type(int idx, vartype_T type)
2141{
2142 vimvars[idx].vv_type = type;
2143}
2144
2145/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002146 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002147 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002148 */
2149 void
2150set_vim_var_nr(int idx, varnumber_T val)
2151{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002152 vimvars[idx].vv_nr = val;
2153}
2154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155 char *
2156get_vim_var_name(int idx)
2157{
2158 return vimvars[idx].vv_name;
2159}
2160
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002161/*
2162 * Get typval_T v: variable value.
2163 */
2164 typval_T *
2165get_vim_var_tv(int idx)
2166{
2167 return &vimvars[idx].vv_tv;
2168}
2169
2170/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002171 * Set v: variable to "tv". Only accepts the same type.
2172 * Takes over the value of "tv".
2173 */
2174 int
2175set_vim_var_tv(int idx, typval_T *tv)
2176{
2177 if (vimvars[idx].vv_type != tv->v_type)
2178 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002179 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002180 clear_tv(tv);
2181 return FAIL;
2182 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002183 // VV_RO is also checked when compiling, but let's check here as well.
2184 if (vimvars[idx].vv_flags & VV_RO)
2185 {
2186 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2187 return FAIL;
2188 }
2189 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2190 {
2191 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2192 return FAIL;
2193 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002194 clear_tv(&vimvars[idx].vv_di.di_tv);
2195 vimvars[idx].vv_di.di_tv = *tv;
2196 return OK;
2197}
2198
2199/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002200 * Get number v: variable value.
2201 */
2202 varnumber_T
2203get_vim_var_nr(int idx)
2204{
2205 return vimvars[idx].vv_nr;
2206}
2207
2208/*
2209 * Get string v: variable value. Uses a static buffer, can only be used once.
2210 * If the String variable has never been set, return an empty string.
2211 * Never returns NULL;
2212 */
2213 char_u *
2214get_vim_var_str(int idx)
2215{
2216 return tv_get_string(&vimvars[idx].vv_tv);
2217}
2218
2219/*
2220 * Get List v: variable value. Caller must take care of reference count when
2221 * needed.
2222 */
2223 list_T *
2224get_vim_var_list(int idx)
2225{
2226 return vimvars[idx].vv_list;
2227}
2228
2229/*
2230 * Get Dict v: variable value. Caller must take care of reference count when
2231 * needed.
2232 */
2233 dict_T *
2234get_vim_var_dict(int idx)
2235{
2236 return vimvars[idx].vv_dict;
2237}
2238
2239/*
2240 * Set v:char to character "c".
2241 */
2242 void
2243set_vim_var_char(int c)
2244{
2245 char_u buf[MB_MAXBYTES + 1];
2246
2247 if (has_mbyte)
2248 buf[(*mb_char2bytes)(c, buf)] = NUL;
2249 else
2250 {
2251 buf[0] = c;
2252 buf[1] = NUL;
2253 }
2254 set_vim_var_string(VV_CHAR, buf, -1);
2255}
2256
2257/*
2258 * Set v:count to "count" and v:count1 to "count1".
2259 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2260 */
2261 void
2262set_vcount(
2263 long count,
2264 long count1,
2265 int set_prevcount)
2266{
2267 if (set_prevcount)
2268 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2269 vimvars[VV_COUNT].vv_nr = count;
2270 vimvars[VV_COUNT1].vv_nr = count1;
2271}
2272
2273/*
2274 * Save variables that might be changed as a side effect. Used when executing
2275 * a timer callback.
2276 */
2277 void
2278save_vimvars(vimvars_save_T *vvsave)
2279{
2280 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2281 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2282 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2283}
2284
2285/*
2286 * Restore variables saved by save_vimvars().
2287 */
2288 void
2289restore_vimvars(vimvars_save_T *vvsave)
2290{
2291 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2292 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2293 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2294}
2295
2296/*
2297 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2298 * value.
2299 */
2300 void
2301set_vim_var_string(
2302 int idx,
2303 char_u *val,
2304 int len) // length of "val" to use or -1 (whole string)
2305{
2306 clear_tv(&vimvars[idx].vv_di.di_tv);
2307 vimvars[idx].vv_type = VAR_STRING;
2308 if (val == NULL)
2309 vimvars[idx].vv_str = NULL;
2310 else if (len == -1)
2311 vimvars[idx].vv_str = vim_strsave(val);
2312 else
2313 vimvars[idx].vv_str = vim_strnsave(val, len);
2314}
2315
2316/*
2317 * Set List v: variable to "val".
2318 */
2319 void
2320set_vim_var_list(int idx, list_T *val)
2321{
2322 clear_tv(&vimvars[idx].vv_di.di_tv);
2323 vimvars[idx].vv_type = VAR_LIST;
2324 vimvars[idx].vv_list = val;
2325 if (val != NULL)
2326 ++val->lv_refcount;
2327}
2328
2329/*
2330 * Set Dictionary v: variable to "val".
2331 */
2332 void
2333set_vim_var_dict(int idx, dict_T *val)
2334{
2335 clear_tv(&vimvars[idx].vv_di.di_tv);
2336 vimvars[idx].vv_type = VAR_DICT;
2337 vimvars[idx].vv_dict = val;
2338 if (val != NULL)
2339 {
2340 ++val->dv_refcount;
2341 dict_set_items_ro(val);
2342 }
2343}
2344
2345/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002346 * Set the v:argv list.
2347 */
2348 void
2349set_argv_var(char **argv, int argc)
2350{
2351 list_T *l = list_alloc();
2352 int i;
2353
2354 if (l == NULL)
2355 getout(1);
2356 l->lv_lock = VAR_FIXED;
2357 for (i = 0; i < argc; ++i)
2358 {
2359 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2360 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002361 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002362 }
2363 set_vim_var_list(VV_ARGV, l);
2364}
2365
2366/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002367 * Reset v:register, taking the 'clipboard' setting into account.
2368 */
2369 void
2370reset_reg_var(void)
2371{
2372 int regname = 0;
2373
2374 // Adjust the register according to 'clipboard', so that when
2375 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2376#ifdef FEAT_CLIPBOARD
2377 adjust_clip_reg(&regname);
2378#endif
2379 set_reg_var(regname);
2380}
2381
2382/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002383 * Set v:register if needed.
2384 */
2385 void
2386set_reg_var(int c)
2387{
2388 char_u regname;
2389
2390 if (c == 0 || c == ' ')
2391 regname = '"';
2392 else
2393 regname = c;
2394 // Avoid free/alloc when the value is already right.
2395 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2396 set_vim_var_string(VV_REG, &regname, 1);
2397}
2398
2399/*
2400 * Get or set v:exception. If "oldval" == NULL, return the current value.
2401 * Otherwise, restore the value to "oldval" and return NULL.
2402 * Must always be called in pairs to save and restore v:exception! Does not
2403 * take care of memory allocations.
2404 */
2405 char_u *
2406v_exception(char_u *oldval)
2407{
2408 if (oldval == NULL)
2409 return vimvars[VV_EXCEPTION].vv_str;
2410
2411 vimvars[VV_EXCEPTION].vv_str = oldval;
2412 return NULL;
2413}
2414
2415/*
2416 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2417 * Otherwise, restore the value to "oldval" and return NULL.
2418 * Must always be called in pairs to save and restore v:throwpoint! Does not
2419 * take care of memory allocations.
2420 */
2421 char_u *
2422v_throwpoint(char_u *oldval)
2423{
2424 if (oldval == NULL)
2425 return vimvars[VV_THROWPOINT].vv_str;
2426
2427 vimvars[VV_THROWPOINT].vv_str = oldval;
2428 return NULL;
2429}
2430
2431/*
2432 * Set v:cmdarg.
2433 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2434 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2435 * Must always be called in pairs!
2436 */
2437 char_u *
2438set_cmdarg(exarg_T *eap, char_u *oldarg)
2439{
2440 char_u *oldval;
2441 char_u *newval;
2442 unsigned len;
2443
2444 oldval = vimvars[VV_CMDARG].vv_str;
2445 if (eap == NULL)
2446 {
2447 vim_free(oldval);
2448 vimvars[VV_CMDARG].vv_str = oldarg;
2449 return NULL;
2450 }
2451
2452 if (eap->force_bin == FORCE_BIN)
2453 len = 6;
2454 else if (eap->force_bin == FORCE_NOBIN)
2455 len = 8;
2456 else
2457 len = 0;
2458
2459 if (eap->read_edit)
2460 len += 7;
2461
2462 if (eap->force_ff != 0)
2463 len += 10; // " ++ff=unix"
2464 if (eap->force_enc != 0)
2465 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2466 if (eap->bad_char != 0)
2467 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2468
2469 newval = alloc(len + 1);
2470 if (newval == NULL)
2471 return NULL;
2472
2473 if (eap->force_bin == FORCE_BIN)
2474 sprintf((char *)newval, " ++bin");
2475 else if (eap->force_bin == FORCE_NOBIN)
2476 sprintf((char *)newval, " ++nobin");
2477 else
2478 *newval = NUL;
2479
2480 if (eap->read_edit)
2481 STRCAT(newval, " ++edit");
2482
2483 if (eap->force_ff != 0)
2484 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2485 eap->force_ff == 'u' ? "unix"
2486 : eap->force_ff == 'd' ? "dos"
2487 : "mac");
2488 if (eap->force_enc != 0)
2489 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2490 eap->cmd + eap->force_enc);
2491 if (eap->bad_char == BAD_KEEP)
2492 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2493 else if (eap->bad_char == BAD_DROP)
2494 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2495 else if (eap->bad_char != 0)
2496 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2497 vimvars[VV_CMDARG].vv_str = newval;
2498 return oldval;
2499}
2500
2501/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002502 * Get the value of internal variable "name".
2503 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2504 */
2505 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002506eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002507 char_u *name,
2508 int len, // length of "name"
2509 typval_T *rettv, // NULL when only checking existence
2510 dictitem_T **dip, // non-NULL when typval's dict item is needed
2511 int verbose, // may give error message
2512 int no_autoload) // do not use script autoloading
2513{
2514 int ret = OK;
2515 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002516 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002517 dictitem_T *v;
2518 int cc;
2519
2520 // truncate the name, so that we can use strcmp()
2521 cc = name[len];
2522 name[len] = NUL;
2523
2524 // Check for user-defined variables.
2525 v = find_var(name, NULL, no_autoload);
2526 if (v != NULL)
2527 {
2528 tv = &v->di_tv;
2529 if (dip != NULL)
2530 *dip = v;
2531 }
2532
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002533 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002535 imported_T *import;
2536 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2537
2538 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539
2540 // imported variable from another script
2541 if (import != NULL)
2542 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002543 if (import->imp_funcname != NULL)
2544 {
2545 foundFunc = TRUE;
2546 if (rettv != NULL)
2547 {
2548 rettv->v_type = VAR_FUNC;
2549 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2550 }
2551 }
Bram Moolenaara6294952020-12-27 13:39:50 +01002552 else if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002553 {
2554 emsg("Sorry, 'import * as X' not implemented yet");
2555 ret = FAIL;
2556 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002557 else
2558 {
2559 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002560 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002562 tv = sv->sv_tv;
2563 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002565 else if (in_vim9script())
2566 {
2567 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2568
2569 if (ufunc != NULL)
2570 {
2571 foundFunc = TRUE;
2572 if (rettv != NULL)
2573 {
2574 rettv->v_type = VAR_FUNC;
2575 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2576 }
2577 }
2578 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 }
2580
Bram Moolenaarc620c052020-07-08 15:16:19 +02002581 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002582 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002583 if (tv == NULL)
2584 {
2585 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002586 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002587 ret = FAIL;
2588 }
2589 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002590 {
2591 // If a list or dict variable wasn't initialized, do it now.
2592 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2593 {
2594 tv->vval.v_dict = dict_alloc();
2595 if (tv->vval.v_dict != NULL)
2596 ++tv->vval.v_dict->dv_refcount;
2597 }
2598 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2599 {
2600 tv->vval.v_list = list_alloc();
2601 if (tv->vval.v_list != NULL)
2602 ++tv->vval.v_list->lv_refcount;
2603 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002604 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002605 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002606 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002607
2608 name[len] = cc;
2609
2610 return ret;
2611}
2612
2613/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002614 * Check if variable "name[len]" is a local variable or an argument.
2615 * If so, "*eval_lavars_used" is set to TRUE.
2616 */
2617 void
2618check_vars(char_u *name, int len)
2619{
2620 int cc;
2621 char_u *varname;
2622 hashtab_T *ht;
2623
2624 if (eval_lavars_used == NULL)
2625 return;
2626
2627 // truncate the name, so that we can use strcmp()
2628 cc = name[len];
2629 name[len] = NUL;
2630
2631 ht = find_var_ht(name, &varname);
2632 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2633 {
2634 if (find_var(name, NULL, TRUE) != NULL)
2635 *eval_lavars_used = TRUE;
2636 }
2637
2638 name[len] = cc;
2639}
2640
2641/*
2642 * Find variable "name" in the list of variables.
2643 * Return a pointer to it if found, NULL if not found.
2644 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002645 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002646 */
2647 dictitem_T *
2648find_var(char_u *name, hashtab_T **htp, int no_autoload)
2649{
2650 char_u *varname;
2651 hashtab_T *ht;
2652 dictitem_T *ret = NULL;
2653
2654 ht = find_var_ht(name, &varname);
2655 if (htp != NULL)
2656 *htp = ht;
2657 if (ht == NULL)
2658 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002659 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002660 if (ret != NULL)
2661 return ret;
2662
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002663 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002664 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002665 if (ret != NULL)
2666 return ret;
2667
2668 // in Vim9 script items without a scope can be script-local
2669 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2670 {
2671 ht = get_script_local_ht();
2672 if (ht != NULL)
2673 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002674 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002675 if (ret != NULL)
2676 {
2677 if (htp != NULL)
2678 *htp = ht;
2679 return ret;
2680 }
2681 }
2682 }
2683
2684 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002685}
2686
2687/*
2688 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002689 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002690 * Returns NULL if not found.
2691 */
2692 dictitem_T *
2693find_var_in_ht(
2694 hashtab_T *ht,
2695 int htname,
2696 char_u *varname,
2697 int no_autoload)
2698{
2699 hashitem_T *hi;
2700
2701 if (*varname == NUL)
2702 {
2703 // Must be something like "s:", otherwise "ht" would be NULL.
2704 switch (htname)
2705 {
2706 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2707 case 'g': return &globvars_var;
2708 case 'v': return &vimvars_var;
2709 case 'b': return &curbuf->b_bufvar;
2710 case 'w': return &curwin->w_winvar;
2711 case 't': return &curtab->tp_winvar;
2712 case 'l': return get_funccal_local_var();
2713 case 'a': return get_funccal_args_var();
2714 }
2715 return NULL;
2716 }
2717
2718 hi = hash_find(ht, varname);
2719 if (HASHITEM_EMPTY(hi))
2720 {
2721 // For global variables we may try auto-loading the script. If it
2722 // worked find the variable again. Don't auto-load a script if it was
2723 // loaded already, otherwise it would be loaded every time when
2724 // checking if a function name is a Funcref variable.
2725 if (ht == &globvarht && !no_autoload)
2726 {
2727 // Note: script_autoload() may make "hi" invalid. It must either
2728 // be obtained again or not used.
2729 if (!script_autoload(varname, FALSE) || aborting())
2730 return NULL;
2731 hi = hash_find(ht, varname);
2732 }
2733 if (HASHITEM_EMPTY(hi))
2734 return NULL;
2735 }
2736 return HI2DI(hi);
2737}
2738
2739/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002740 * Get the script-local hashtab. NULL if not in a script context.
2741 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002742 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743get_script_local_ht(void)
2744{
2745 scid_T sid = current_sctx.sc_sid;
2746
Bram Moolenaare3d46852020-08-29 13:39:17 +02002747 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748 return &SCRIPT_VARS(sid);
2749 return NULL;
2750}
2751
2752/*
2753 * Look for "name[len]" in script-local variables.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002754 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01002756 int
2757lookup_scriptvar(
2758 char_u *name,
2759 size_t len,
2760 void *lvar UNUSED,
2761 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762{
2763 hashtab_T *ht = get_script_local_ht();
2764 char_u buffer[30];
2765 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01002766 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767 hashitem_T *hi;
2768
2769 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002770 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771 if (len < sizeof(buffer) - 1)
2772 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002773 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 vim_strncpy(buffer, name, len);
2775 p = buffer;
2776 }
2777 else
2778 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002779 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002781 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 }
2783
2784 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002785 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002786
2787 // if not script-local, then perhaps imported
Bram Moolenaar709664c2020-12-12 14:33:41 +01002788 if (res == FAIL && find_imported(p, 0, NULL) != NULL)
2789 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790
2791 if (p != buffer)
2792 vim_free(p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002793 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794}
2795
2796/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002797 * Find the hashtab used for a variable name.
2798 * Return NULL if the name is not valid.
2799 * Set "varname" to the start of name without ':'.
2800 */
2801 hashtab_T *
2802find_var_ht(char_u *name, char_u **varname)
2803{
2804 hashitem_T *hi;
2805 hashtab_T *ht;
2806
2807 if (name[0] == NUL)
2808 return NULL;
2809 if (name[1] != ':')
2810 {
2811 // The name must not start with a colon or #.
2812 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2813 return NULL;
2814 *varname = name;
2815
2816 // "version" is "v:version" in all scopes if scriptversion < 3.
2817 // Same for a few other variables marked with VV_COMPAT.
2818 if (current_sctx.sc_version < 3)
2819 {
2820 hi = hash_find(&compat_hashtab, name);
2821 if (!HASHITEM_EMPTY(hi))
2822 return &compat_hashtab;
2823 }
2824
2825 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826 if (ht != NULL)
2827 return ht; // local variable
2828
2829 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002830 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 {
2832 ht = get_script_local_ht();
2833 if (ht != NULL)
2834 return ht;
2835 }
2836
2837 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002838 }
2839 *varname = name + 2;
2840 if (*name == 'g') // global variable
2841 return &globvarht;
2842 // There must be no ':' or '#' in the rest of the name, unless g: is used
2843 if (vim_strchr(name + 2, ':') != NULL
2844 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2845 return NULL;
2846 if (*name == 'b') // buffer variable
2847 return &curbuf->b_vars->dv_hashtab;
2848 if (*name == 'w') // window variable
2849 return &curwin->w_vars->dv_hashtab;
2850 if (*name == 't') // tab page variable
2851 return &curtab->tp_vars->dv_hashtab;
2852 if (*name == 'v') // v: variable
2853 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002854 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002855 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002857 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 if (*name == 'a') // a: function argument
2859 return get_funccal_args_ht();
2860 if (*name == 'l') // l: local function variable
2861 return get_funccal_local_ht();
2862 }
2863 if (*name == 's') // script variable
2864 {
2865 ht = get_script_local_ht();
2866 if (ht != NULL)
2867 return ht;
2868 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002869 return NULL;
2870}
2871
2872/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002873 * Get the string value of a (global/local) variable.
2874 * Note: see tv_get_string() for how long the pointer remains valid.
2875 * Returns NULL when it doesn't exist.
2876 */
2877 char_u *
2878get_var_value(char_u *name)
2879{
2880 dictitem_T *v;
2881
2882 v = find_var(name, NULL, FALSE);
2883 if (v == NULL)
2884 return NULL;
2885 return tv_get_string(&v->di_tv);
2886}
2887
2888/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002889 * Allocate a new hashtab for a sourced script. It will be used while
2890 * sourcing this script and when executing functions defined in the script.
2891 */
2892 void
2893new_script_vars(scid_T id)
2894{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002895 scriptvar_T *sv;
2896
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002897 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2898 if (sv == NULL)
2899 return;
2900 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002901 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002902}
2903
2904/*
2905 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2906 * point to it.
2907 */
2908 void
2909init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2910{
2911 hash_init(&dict->dv_hashtab);
2912 dict->dv_lock = 0;
2913 dict->dv_scope = scope;
2914 dict->dv_refcount = DO_NOT_FREE_CNT;
2915 dict->dv_copyID = 0;
2916 dict_var->di_tv.vval.v_dict = dict;
2917 dict_var->di_tv.v_type = VAR_DICT;
2918 dict_var->di_tv.v_lock = VAR_FIXED;
2919 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2920 dict_var->di_key[0] = NUL;
2921}
2922
2923/*
2924 * Unreference a dictionary initialized by init_var_dict().
2925 */
2926 void
2927unref_var_dict(dict_T *dict)
2928{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002929 // Now the dict needs to be freed if no one else is using it, go back to
2930 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002931 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2932 dict_unref(dict);
2933}
2934
2935/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002936 * Clean up a list of internal variables.
2937 * Frees all allocated variables and the value they contain.
2938 * Clears hashtab "ht", does not free it.
2939 */
2940 void
2941vars_clear(hashtab_T *ht)
2942{
2943 vars_clear_ext(ht, TRUE);
2944}
2945
2946/*
2947 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2948 */
2949 void
2950vars_clear_ext(hashtab_T *ht, int free_val)
2951{
2952 int todo;
2953 hashitem_T *hi;
2954 dictitem_T *v;
2955
2956 hash_lock(ht);
2957 todo = (int)ht->ht_used;
2958 for (hi = ht->ht_array; todo > 0; ++hi)
2959 {
2960 if (!HASHITEM_EMPTY(hi))
2961 {
2962 --todo;
2963
2964 // Free the variable. Don't remove it from the hashtab,
2965 // ht_array might change then. hash_clear() takes care of it
2966 // later.
2967 v = HI2DI(hi);
2968 if (free_val)
2969 clear_tv(&v->di_tv);
2970 if (v->di_flags & DI_FLAGS_ALLOC)
2971 vim_free(v);
2972 }
2973 }
2974 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02002975 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002976}
2977
2978/*
2979 * Delete a variable from hashtab "ht" at item "hi".
2980 * Clear the variable value and free the dictitem.
2981 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02002982 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002983delete_var(hashtab_T *ht, hashitem_T *hi)
2984{
2985 dictitem_T *di = HI2DI(hi);
2986
2987 hash_remove(ht, hi);
2988 clear_tv(&di->di_tv);
2989 vim_free(di);
2990}
2991
2992/*
2993 * List the value of one internal variable.
2994 */
2995 static void
2996list_one_var(dictitem_T *v, char *prefix, int *first)
2997{
2998 char_u *tofree;
2999 char_u *s;
3000 char_u numbuf[NUMBUFLEN];
3001
3002 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3003 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3004 s == NULL ? (char_u *)"" : s, first);
3005 vim_free(tofree);
3006}
3007
3008 static void
3009list_one_var_a(
3010 char *prefix,
3011 char_u *name,
3012 int type,
3013 char_u *string,
3014 int *first) // when TRUE clear rest of screen and set to FALSE
3015{
3016 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3017 msg_start();
3018 msg_puts(prefix);
3019 if (name != NULL) // "a:" vars don't have a name stored
3020 msg_puts((char *)name);
3021 msg_putchar(' ');
3022 msg_advance(22);
3023 if (type == VAR_NUMBER)
3024 msg_putchar('#');
3025 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3026 msg_putchar('*');
3027 else if (type == VAR_LIST)
3028 {
3029 msg_putchar('[');
3030 if (*string == '[')
3031 ++string;
3032 }
3033 else if (type == VAR_DICT)
3034 {
3035 msg_putchar('{');
3036 if (*string == '{')
3037 ++string;
3038 }
3039 else
3040 msg_putchar(' ');
3041
3042 msg_outtrans(string);
3043
3044 if (type == VAR_FUNC || type == VAR_PARTIAL)
3045 msg_puts("()");
3046 if (*first)
3047 {
3048 msg_clr_eos();
3049 *first = FALSE;
3050 }
3051}
3052
3053/*
3054 * Set variable "name" to value in "tv".
3055 * If the variable already exists, the value is updated.
3056 * Otherwise the variable is created.
3057 */
3058 void
3059set_var(
3060 char_u *name,
3061 typval_T *tv,
3062 int copy) // make copy of value in "tv"
3063{
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003064 set_var_const(name, NULL, tv, copy, ASSIGN_DECL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003065}
3066
3067/*
3068 * Set variable "name" to value in "tv".
3069 * If the variable already exists and "is_const" is FALSE the value is updated.
3070 * Otherwise the variable is created.
3071 */
3072 void
3073set_var_const(
3074 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003076 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003077 int copy, // make copy of value in "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003078 int flags) // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003079{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003080 typval_T *tv = tv_arg;
3081 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003083 char_u *varname;
3084 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003086 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003087
3088 ht = find_var_ht(name, &varname);
3089 if (ht == NULL || *varname == NUL)
3090 {
3091 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003092 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003093 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003094 is_script_local = ht == get_script_local_ht();
3095
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003096 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003097 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003098 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003099 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003100 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003101 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003102 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003103 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003104 }
3105
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003107
3108 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 if (di == NULL)
3110 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003111
3112 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003113 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003114 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003115
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003116 if (need_convert_to_bool(type, tv))
3117 {
3118 // Destination is a bool and the value is not, but it can be converted.
3119 CLEAR_FIELD(bool_tv);
3120 bool_tv.v_type = VAR_BOOL;
3121 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3122 tv = &bool_tv;
3123 }
3124
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003126 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003128 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003129 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 {
3131 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003132 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 }
3134
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003135 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 {
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003137 if ((flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003138 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003139 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003140 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003141 }
3142
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003143 // check the type and adjust to bool if needed
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003144 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003145 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003147
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003148 if (var_check_permission(di, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003149 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003150 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151 else
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003152 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 // can only redefine once
3154 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003155
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003156 // A Vim9 script-local variable is also present in sn_all_vars and
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003157 // sn_var_vals. It may set "type" from "tv".
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003158 if (is_script_local && vim9script)
Bram Moolenaar08251752021-01-11 21:20:18 +01003159 update_vim9_script_var(FALSE, di, flags, tv, &type);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003160 }
3161
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003162 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003163
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003165 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003166 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003167 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003169 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003171 if (copy || tv->v_type != VAR_STRING)
3172 {
3173 char_u *val = tv_get_string(tv);
3174
3175 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003176 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 if (di->di_tv.vval.v_string == NULL)
3178 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003179 }
3180 else
3181 {
3182 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003184 tv->vval.v_string = NULL;
3185 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003186 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003187 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003189 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003191 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003193#ifdef FEAT_SEARCH_EXTRA
3194 else if (STRCMP(varname, "hlsearch") == 0)
3195 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003197 redraw_all_later(SOME_VALID);
3198 }
3199#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003200 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003201 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003203 {
3204 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003205 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003206 }
3207 }
3208
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003210 }
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003211 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003212 {
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003213 // add a new variable
3214 if (vim9script && is_script_local && (flags & ASSIGN_NO_DECL))
3215 {
3216 semsg(_(e_unknown_variable_str), name);
3217 goto failed;
3218 }
3219
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003220 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003221 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003222 {
3223 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003224 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003225 }
3226
Bram Moolenaar03290b82020-12-19 16:30:44 +01003227 // Make sure the variable name is valid. In Vim9 script an autoload
3228 // variable must be prefixed with "g:".
3229 if (!valid_varname(varname, !vim9script
3230 || STRNCMP(name, "g:", 2) == 0))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003231 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003232
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3234 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003235 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 STRCPY(di->di_key, varname);
3237 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003238 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003240 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003241 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003243 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 di->di_flags |= DI_FLAGS_LOCK;
3245
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003246 // A Vim9 script-local variable is also added to sn_all_vars and
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003247 // sn_var_vals. It may set "type" from "tv".
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003248 if (is_script_local && vim9script)
Bram Moolenaar08251752021-01-11 21:20:18 +01003249 update_vim9_script_var(TRUE, di, flags, tv, &type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003250 }
3251
3252 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003254 else
3255 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 di->di_tv = *tv;
3257 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003258 init_tv(tv);
3259 }
3260
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003261 if (vim9script && type != NULL)
3262 {
3263 if (type->tt_type == VAR_DICT && di->di_tv.vval.v_dict != NULL)
3264 di->di_tv.vval.v_dict->dv_type = alloc_type(type);
3265 else if (type->tt_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
3266 di->di_tv.vval.v_list->lv_type = alloc_type(type);
3267 }
3268
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003269 // ":const var = value" locks the value
3270 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003271 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003272 // Like :lockvar! name: lock the value and what it contains, but only
3273 // if the reference count is up to one. That locks only literal
3274 // values.
3275 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003276 return;
3277failed:
3278 if (!copy)
3279 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003280}
3281
3282/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003283 * Check in this order for backwards compatibility:
3284 * - Whether the variable is read-only
3285 * - Whether the variable value is locked
3286 * - Whether the variable is locked
3287 */
3288 int
3289var_check_permission(dictitem_T *di, char_u *name)
3290{
3291 if (var_check_ro(di->di_flags, name, FALSE)
3292 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3293 || var_check_lock(di->di_flags, name, FALSE))
3294 return FAIL;
3295 return OK;
3296}
3297
3298/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003299 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3300 * Also give an error message.
3301 */
3302 int
3303var_check_ro(int flags, char_u *name, int use_gettext)
3304{
3305 if (flags & DI_FLAGS_RO)
3306 {
3307 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3308 return TRUE;
3309 }
3310 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3311 {
3312 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3313 return TRUE;
3314 }
3315 return FALSE;
3316}
3317
3318/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003319 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3320 * Also give an error message.
3321 */
3322 int
3323var_check_lock(int flags, char_u *name, int use_gettext)
3324{
3325 if (flags & DI_FLAGS_LOCK)
3326 {
3327 semsg(_(e_variable_is_locked_str),
3328 use_gettext ? (char_u *)_(name) : name);
3329 return TRUE;
3330 }
3331 return FALSE;
3332}
3333
3334/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003335 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3336 * Also give an error message.
3337 */
3338 int
3339var_check_fixed(int flags, char_u *name, int use_gettext)
3340{
3341 if (flags & DI_FLAGS_FIX)
3342 {
3343 semsg(_("E795: Cannot delete variable %s"),
3344 use_gettext ? (char_u *)_(name) : name);
3345 return TRUE;
3346 }
3347 return FALSE;
3348}
3349
3350/*
3351 * Check if a funcref is assigned to a valid variable name.
3352 * Return TRUE and give an error if not.
3353 */
3354 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003355var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003356 char_u *name, // points to start of variable name
3357 int new_var) // TRUE when creating the variable
3358{
3359 // Allow for w: b: s: and t:.
3360 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3361 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3362 ? name[2] : name[0]))
3363 {
3364 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3365 name);
3366 return TRUE;
3367 }
3368 // Don't allow hiding a function. When "v" is not NULL we might be
3369 // assigning another function to the same var, the type is checked
3370 // below.
3371 if (new_var && function_exists(name, FALSE))
3372 {
3373 semsg(_("E705: Variable name conflicts with existing function: %s"),
3374 name);
3375 return TRUE;
3376 }
3377 return FALSE;
3378}
3379
3380/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003381 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3382 * value. Also give an error message, using "name" or _("name") when
3383 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003384 */
3385 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003386value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003387{
3388 if (lock & VAR_LOCKED)
3389 {
3390 semsg(_("E741: Value is locked: %s"),
3391 name == NULL ? (char_u *)_("Unknown")
3392 : use_gettext ? (char_u *)_(name)
3393 : name);
3394 return TRUE;
3395 }
3396 if (lock & VAR_FIXED)
3397 {
3398 semsg(_("E742: Cannot change value of %s"),
3399 name == NULL ? (char_u *)_("Unknown")
3400 : use_gettext ? (char_u *)_(name)
3401 : name);
3402 return TRUE;
3403 }
3404 return FALSE;
3405}
3406
3407/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003408 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003409 * Return FALSE and give an error if not.
3410 */
3411 int
Bram Moolenaar03290b82020-12-19 16:30:44 +01003412valid_varname(char_u *varname, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003413{
3414 char_u *p;
3415
3416 for (p = varname; *p != NUL; ++p)
3417 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003418 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003419 {
3420 semsg(_(e_illvar), varname);
3421 return FALSE;
3422 }
3423 return TRUE;
3424}
3425
3426/*
3427 * getwinvar() and gettabwinvar()
3428 */
3429 static void
3430getwinvar(
3431 typval_T *argvars,
3432 typval_T *rettv,
3433 int off) // 1 for gettabwinvar()
3434{
3435 win_T *win;
3436 char_u *varname;
3437 dictitem_T *v;
3438 tabpage_T *tp = NULL;
3439 int done = FALSE;
3440 win_T *oldcurwin;
3441 tabpage_T *oldtabpage;
3442 int need_switch_win;
3443
3444 if (off == 1)
3445 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3446 else
3447 tp = curtab;
3448 win = find_win_by_nr(&argvars[off], tp);
3449 varname = tv_get_string_chk(&argvars[off + 1]);
3450 ++emsg_off;
3451
3452 rettv->v_type = VAR_STRING;
3453 rettv->vval.v_string = NULL;
3454
3455 if (win != NULL && varname != NULL)
3456 {
3457 // Set curwin to be our win, temporarily. Also set the tabpage,
3458 // otherwise the window is not valid. Only do this when needed,
3459 // autocommands get blocked.
3460 need_switch_win = !(tp == curtab && win == curwin);
3461 if (!need_switch_win
3462 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3463 {
3464 if (*varname == '&')
3465 {
3466 if (varname[1] == NUL)
3467 {
3468 // get all window-local options in a dict
3469 dict_T *opts = get_winbuf_options(FALSE);
3470
3471 if (opts != NULL)
3472 {
3473 rettv_dict_set(rettv, opts);
3474 done = TRUE;
3475 }
3476 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003477 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003478 // window-local-option
3479 done = TRUE;
3480 }
3481 else
3482 {
3483 // Look up the variable.
3484 // Let getwinvar({nr}, "") return the "w:" dictionary.
3485 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3486 varname, FALSE);
3487 if (v != NULL)
3488 {
3489 copy_tv(&v->di_tv, rettv);
3490 done = TRUE;
3491 }
3492 }
3493 }
3494
3495 if (need_switch_win)
3496 // restore previous notion of curwin
3497 restore_win(oldcurwin, oldtabpage, TRUE);
3498 }
3499
3500 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3501 // use the default return value
3502 copy_tv(&argvars[off + 2], rettv);
3503
3504 --emsg_off;
3505}
3506
3507/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003508 * Set option "varname" to the value of "varp" for the current buffer/window.
3509 */
3510 static void
3511set_option_from_tv(char_u *varname, typval_T *varp)
3512{
3513 long numval = 0;
3514 char_u *strval;
3515 char_u nbuf[NUMBUFLEN];
3516 int error = FALSE;
3517
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003518 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003519 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003520 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003521 strval = (char_u *)"0"; // avoid using "false"
3522 }
3523 else
3524 {
3525 if (!in_vim9script() || varp->v_type != VAR_STRING)
3526 numval = (long)tv_get_number_chk(varp, &error);
3527 strval = tv_get_string_buf_chk(varp, nbuf);
3528 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02003529 if (!error && strval != NULL)
3530 set_option_value(varname, numval, strval, OPT_LOCAL);
3531}
3532
3533/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003534 * "setwinvar()" and "settabwinvar()" functions
3535 */
3536 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003537setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003538{
3539 win_T *win;
3540 win_T *save_curwin;
3541 tabpage_T *save_curtab;
3542 int need_switch_win;
3543 char_u *varname, *winvarname;
3544 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003545 tabpage_T *tp = NULL;
3546
3547 if (check_secure())
3548 return;
3549
3550 if (off == 1)
3551 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3552 else
3553 tp = curtab;
3554 win = find_win_by_nr(&argvars[off], tp);
3555 varname = tv_get_string_chk(&argvars[off + 1]);
3556 varp = &argvars[off + 2];
3557
3558 if (win != NULL && varname != NULL && varp != NULL)
3559 {
3560 need_switch_win = !(tp == curtab && win == curwin);
3561 if (!need_switch_win
3562 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3563 {
3564 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003565 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003566 else
3567 {
3568 winvarname = alloc(STRLEN(varname) + 3);
3569 if (winvarname != NULL)
3570 {
3571 STRCPY(winvarname, "w:");
3572 STRCPY(winvarname + 2, varname);
3573 set_var(winvarname, varp, TRUE);
3574 vim_free(winvarname);
3575 }
3576 }
3577 }
3578 if (need_switch_win)
3579 restore_win(save_curwin, save_curtab, TRUE);
3580 }
3581}
3582
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003583/*
3584 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3585 * v:option_type, and v:option_command.
3586 */
3587 void
3588reset_v_option_vars(void)
3589{
3590 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3591 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3592 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3593 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3594 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3595 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3596}
3597
3598/*
3599 * Add an assert error to v:errors.
3600 */
3601 void
3602assert_error(garray_T *gap)
3603{
3604 struct vimvar *vp = &vimvars[VV_ERRORS];
3605
3606 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003607 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003608 set_vim_var_list(VV_ERRORS, list_alloc());
3609 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3610}
3611
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003612 int
3613var_exists(char_u *var)
3614{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003615 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003616 char_u *name;
3617 char_u *tofree;
3618 typval_T tv;
3619 int len = 0;
3620 int n = FALSE;
3621
3622 // get_name_len() takes care of expanding curly braces
3623 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003624 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003625 if (len > 0)
3626 {
3627 if (tofree != NULL)
3628 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003629 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003630 if (n)
3631 {
3632 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003633 arg = skipwhite(arg);
3634 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003635 if (n)
3636 clear_tv(&tv);
3637 }
3638 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003639 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003640 n = FALSE;
3641
3642 vim_free(tofree);
3643 return n;
3644}
3645
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003646static lval_T *redir_lval = NULL;
3647#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3648static garray_T redir_ga; // only valid when redir_lval is not NULL
3649static char_u *redir_endp = NULL;
3650static char_u *redir_varname = NULL;
3651
3652/*
3653 * Start recording command output to a variable
3654 * When "append" is TRUE append to an existing variable.
3655 * Returns OK if successfully completed the setup. FAIL otherwise.
3656 */
3657 int
3658var_redir_start(char_u *name, int append)
3659{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003660 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003661 typval_T tv;
3662
3663 // Catch a bad name early.
3664 if (!eval_isnamec1(*name))
3665 {
3666 emsg(_(e_invarg));
3667 return FAIL;
3668 }
3669
3670 // Make a copy of the name, it is used in redir_lval until redir ends.
3671 redir_varname = vim_strsave(name);
3672 if (redir_varname == NULL)
3673 return FAIL;
3674
3675 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3676 if (redir_lval == NULL)
3677 {
3678 var_redir_stop();
3679 return FAIL;
3680 }
3681
3682 // The output is stored in growarray "redir_ga" until redirection ends.
3683 ga_init2(&redir_ga, (int)sizeof(char), 500);
3684
3685 // Parse the variable name (can be a dict or list entry).
3686 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3687 FNE_CHECK_START);
3688 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3689 {
3690 clear_lval(redir_lval);
3691 if (redir_endp != NULL && *redir_endp != NUL)
3692 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003693 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003694 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003695 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003696 redir_endp = NULL; // don't store a value, only cleanup
3697 var_redir_stop();
3698 return FAIL;
3699 }
3700
3701 // check if we can write to the variable: set it to or append an empty
3702 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003703 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003704 tv.v_type = VAR_STRING;
3705 tv.vval.v_string = (char_u *)"";
3706 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003707 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3708 ASSIGN_NO_DECL, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003709 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003710 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3711 ASSIGN_NO_DECL, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003712 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003713 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003714 {
3715 redir_endp = NULL; // don't store a value, only cleanup
3716 var_redir_stop();
3717 return FAIL;
3718 }
3719
3720 return OK;
3721}
3722
3723/*
3724 * Append "value[value_len]" to the variable set by var_redir_start().
3725 * The actual appending is postponed until redirection ends, because the value
3726 * appended may in fact be the string we write to, changing it may cause freed
3727 * memory to be used:
3728 * :redir => foo
3729 * :let foo
3730 * :redir END
3731 */
3732 void
3733var_redir_str(char_u *value, int value_len)
3734{
3735 int len;
3736
3737 if (redir_lval == NULL)
3738 return;
3739
3740 if (value_len == -1)
3741 len = (int)STRLEN(value); // Append the entire string
3742 else
3743 len = value_len; // Append only "value_len" characters
3744
3745 if (ga_grow(&redir_ga, len) == OK)
3746 {
3747 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3748 redir_ga.ga_len += len;
3749 }
3750 else
3751 var_redir_stop();
3752}
3753
3754/*
3755 * Stop redirecting command output to a variable.
3756 * Frees the allocated memory.
3757 */
3758 void
3759var_redir_stop(void)
3760{
3761 typval_T tv;
3762
3763 if (EVALCMD_BUSY)
3764 {
3765 redir_lval = NULL;
3766 return;
3767 }
3768
3769 if (redir_lval != NULL)
3770 {
3771 // If there was no error: assign the text to the variable.
3772 if (redir_endp != NULL)
3773 {
3774 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3775 tv.v_type = VAR_STRING;
3776 tv.vval.v_string = redir_ga.ga_data;
3777 // Call get_lval() again, if it's inside a Dict or List it may
3778 // have changed.
3779 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3780 FALSE, FALSE, 0, FNE_CHECK_START);
3781 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003783 (char_u *)".");
3784 clear_lval(redir_lval);
3785 }
3786
3787 // free the collected output
3788 VIM_CLEAR(redir_ga.ga_data);
3789
3790 VIM_CLEAR(redir_lval);
3791 }
3792 VIM_CLEAR(redir_varname);
3793}
3794
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003795/*
3796 * "gettabvar()" function
3797 */
3798 void
3799f_gettabvar(typval_T *argvars, typval_T *rettv)
3800{
3801 win_T *oldcurwin;
3802 tabpage_T *tp, *oldtabpage;
3803 dictitem_T *v;
3804 char_u *varname;
3805 int done = FALSE;
3806
3807 rettv->v_type = VAR_STRING;
3808 rettv->vval.v_string = NULL;
3809
3810 varname = tv_get_string_chk(&argvars[1]);
3811 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3812 if (tp != NULL && varname != NULL)
3813 {
3814 // Set tp to be our tabpage, temporarily. Also set the window to the
3815 // first window in the tabpage, otherwise the window is not valid.
3816 if (switch_win(&oldcurwin, &oldtabpage,
3817 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3818 : tp->tp_firstwin, tp, TRUE) == OK)
3819 {
3820 // look up the variable
3821 // Let gettabvar({nr}, "") return the "t:" dictionary.
3822 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3823 if (v != NULL)
3824 {
3825 copy_tv(&v->di_tv, rettv);
3826 done = TRUE;
3827 }
3828 }
3829
3830 // restore previous notion of curwin
3831 restore_win(oldcurwin, oldtabpage, TRUE);
3832 }
3833
3834 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3835 copy_tv(&argvars[2], rettv);
3836}
3837
3838/*
3839 * "gettabwinvar()" function
3840 */
3841 void
3842f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3843{
3844 getwinvar(argvars, rettv, 1);
3845}
3846
3847/*
3848 * "getwinvar()" function
3849 */
3850 void
3851f_getwinvar(typval_T *argvars, typval_T *rettv)
3852{
3853 getwinvar(argvars, rettv, 0);
3854}
3855
3856/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003857 * "getbufvar()" function
3858 */
3859 void
3860f_getbufvar(typval_T *argvars, typval_T *rettv)
3861{
3862 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003863 char_u *varname;
3864 dictitem_T *v;
3865 int done = FALSE;
3866
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003867 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003868 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003869
3870 rettv->v_type = VAR_STRING;
3871 rettv->vval.v_string = NULL;
3872
3873 if (buf != NULL && varname != NULL)
3874 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003875 if (*varname == '&')
3876 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003877 buf_T *save_curbuf = curbuf;
3878
3879 // set curbuf to be our buf, temporarily
3880 curbuf = buf;
3881
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003882 if (varname[1] == NUL)
3883 {
3884 // get all buffer-local options in a dict
3885 dict_T *opts = get_winbuf_options(TRUE);
3886
3887 if (opts != NULL)
3888 {
3889 rettv_dict_set(rettv, opts);
3890 done = TRUE;
3891 }
3892 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003893 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003894 // buffer-local-option
3895 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003896
3897 // restore previous notion of curbuf
3898 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003899 }
3900 else
3901 {
3902 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003903 if (*varname == NUL)
3904 // Let getbufvar({nr}, "") return the "b:" dictionary.
3905 v = &buf->b_bufvar;
3906 else
3907 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3908 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003909 if (v != NULL)
3910 {
3911 copy_tv(&v->di_tv, rettv);
3912 done = TRUE;
3913 }
3914 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003915 }
3916
3917 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3918 // use the default value
3919 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003920}
3921
3922/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003923 * "settabvar()" function
3924 */
3925 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003926f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003927{
3928 tabpage_T *save_curtab;
3929 tabpage_T *tp;
3930 char_u *varname, *tabvarname;
3931 typval_T *varp;
3932
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003933 if (check_secure())
3934 return;
3935
3936 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3937 varname = tv_get_string_chk(&argvars[1]);
3938 varp = &argvars[2];
3939
3940 if (varname != NULL && varp != NULL && tp != NULL)
3941 {
3942 save_curtab = curtab;
3943 goto_tabpage_tp(tp, FALSE, FALSE);
3944
3945 tabvarname = alloc(STRLEN(varname) + 3);
3946 if (tabvarname != NULL)
3947 {
3948 STRCPY(tabvarname, "t:");
3949 STRCPY(tabvarname + 2, varname);
3950 set_var(tabvarname, varp, TRUE);
3951 vim_free(tabvarname);
3952 }
3953
3954 // Restore current tabpage
3955 if (valid_tabpage(save_curtab))
3956 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3957 }
3958}
3959
3960/*
3961 * "settabwinvar()" function
3962 */
3963 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003964f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003965{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003966 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003967}
3968
3969/*
3970 * "setwinvar()" function
3971 */
3972 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003973f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003974{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003975 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003976}
3977
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003978/*
3979 * "setbufvar()" function
3980 */
3981 void
3982f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3983{
3984 buf_T *buf;
3985 char_u *varname, *bufvarname;
3986 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003987
3988 if (check_secure())
3989 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003990 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003991 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003992 varp = &argvars[2];
3993
3994 if (buf != NULL && varname != NULL && varp != NULL)
3995 {
3996 if (*varname == '&')
3997 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003998 aco_save_T aco;
3999
4000 // set curbuf to be our buf, temporarily
4001 aucmd_prepbuf(&aco, buf);
4002
Bram Moolenaar191929b2020-08-19 21:20:49 +02004003 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004004
4005 // reset notion of buffer
4006 aucmd_restbuf(&aco);
4007 }
4008 else
4009 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004010 bufvarname = alloc(STRLEN(varname) + 3);
4011 if (bufvarname != NULL)
4012 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004013 buf_T *save_curbuf = curbuf;
4014
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004015 curbuf = buf;
4016 STRCPY(bufvarname, "b:");
4017 STRCPY(bufvarname + 2, varname);
4018 set_var(bufvarname, varp, TRUE);
4019 vim_free(bufvarname);
4020 curbuf = save_curbuf;
4021 }
4022 }
4023 }
4024}
4025
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004026/*
4027 * Get a callback from "arg". It can be a Funcref or a function name.
4028 * When "arg" is zero return an empty string.
4029 * "cb_name" is not allocated.
4030 * "cb_name" is set to NULL for an invalid argument.
4031 */
4032 callback_T
4033get_callback(typval_T *arg)
4034{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004035 callback_T res;
4036 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004037
4038 res.cb_free_name = FALSE;
4039 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4040 {
4041 res.cb_partial = arg->vval.v_partial;
4042 ++res.cb_partial->pt_refcount;
4043 res.cb_name = partial_name(res.cb_partial);
4044 }
4045 else
4046 {
4047 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004048 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4049 && isdigit(*arg->vval.v_string))
4050 r = FAIL;
4051 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004052 {
4053 // Note that we don't make a copy of the string.
4054 res.cb_name = arg->vval.v_string;
4055 func_ref(res.cb_name);
4056 }
4057 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004058 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004059 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004060 r = FAIL;
4061
4062 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004063 {
4064 emsg(_("E921: Invalid callback argument"));
4065 res.cb_name = NULL;
4066 }
4067 }
4068 return res;
4069}
4070
4071/*
4072 * Copy a callback into a typval_T.
4073 */
4074 void
4075put_callback(callback_T *cb, typval_T *tv)
4076{
4077 if (cb->cb_partial != NULL)
4078 {
4079 tv->v_type = VAR_PARTIAL;
4080 tv->vval.v_partial = cb->cb_partial;
4081 ++tv->vval.v_partial->pt_refcount;
4082 }
4083 else
4084 {
4085 tv->v_type = VAR_FUNC;
4086 tv->vval.v_string = vim_strsave(cb->cb_name);
4087 func_ref(cb->cb_name);
4088 }
4089}
4090
4091/*
4092 * Make a copy of "src" into "dest", allocating the function name if needed,
4093 * without incrementing the refcount.
4094 */
4095 void
4096set_callback(callback_T *dest, callback_T *src)
4097{
4098 if (src->cb_partial == NULL)
4099 {
4100 // just a function name, make a copy
4101 dest->cb_name = vim_strsave(src->cb_name);
4102 dest->cb_free_name = TRUE;
4103 }
4104 else
4105 {
4106 // cb_name is a pointer into cb_partial
4107 dest->cb_name = src->cb_name;
4108 dest->cb_free_name = FALSE;
4109 }
4110 dest->cb_partial = src->cb_partial;
4111}
4112
4113/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004114 * Copy callback from "src" to "dest", incrementing the refcounts.
4115 */
4116 void
4117copy_callback(callback_T *dest, callback_T *src)
4118{
4119 dest->cb_partial = src->cb_partial;
4120 if (dest->cb_partial != NULL)
4121 {
4122 dest->cb_name = src->cb_name;
4123 dest->cb_free_name = FALSE;
4124 ++dest->cb_partial->pt_refcount;
4125 }
4126 else
4127 {
4128 dest->cb_name = vim_strsave(src->cb_name);
4129 dest->cb_free_name = TRUE;
4130 func_ref(src->cb_name);
4131 }
4132}
4133
4134/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004135 * Unref/free "callback" returned by get_callback() or set_callback().
4136 */
4137 void
4138free_callback(callback_T *callback)
4139{
4140 if (callback->cb_partial != NULL)
4141 {
4142 partial_unref(callback->cb_partial);
4143 callback->cb_partial = NULL;
4144 }
4145 else if (callback->cb_name != NULL)
4146 func_unref(callback->cb_name);
4147 if (callback->cb_free_name)
4148 {
4149 vim_free(callback->cb_name);
4150 callback->cb_free_name = FALSE;
4151 }
4152 callback->cb_name = NULL;
4153}
4154
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004155#endif // FEAT_EVAL