blob: 22d39a72f650cd91486791b4754ced8b28e81593 [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},
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100125 {VV_NAME("numbermax", VAR_NUMBER), VV_RO},
126 {VV_NAME("numbermin", VAR_NUMBER), VV_RO},
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100127 {VV_NAME("numbersize", VAR_NUMBER), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200128 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
129 {VV_NAME("testing", VAR_NUMBER), 0},
130 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
131 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
132 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
133 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
134 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
135 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
136 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
137 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
138 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
139 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
140 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
141 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
142 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
143 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
144 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
145 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
146 {VV_NAME("event", VAR_DICT), VV_RO},
147 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
148 {VV_NAME("echospace", VAR_NUMBER), VV_RO},
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100149 {VV_NAME("argv", VAR_LIST), VV_RO},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200150 {VV_NAME("collate", VAR_STRING), VV_RO},
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100151 {VV_NAME("exiting", VAR_SPECIAL), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200152};
153
154// shorthand
155#define vv_type vv_di.di_tv.v_type
156#define vv_nr vv_di.di_tv.vval.v_number
157#define vv_float vv_di.di_tv.vval.v_float
158#define vv_str vv_di.di_tv.vval.v_string
159#define vv_list vv_di.di_tv.vval.v_list
160#define vv_dict vv_di.di_tv.vval.v_dict
161#define vv_blob vv_di.di_tv.vval.v_blob
162#define vv_tv vv_di.di_tv
163
164static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200165static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200166#define vimvarht vimvardict.dv_hashtab
167
168// for VIM_VERSION_ defines
169#include "version.h"
170
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200171static void list_glob_vars(int *first);
172static void list_buf_vars(int *first);
173static void list_win_vars(int *first);
174static void list_tab_vars(int *first);
175static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100176static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op, int var_idx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200177static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
178static 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 +0200179static void list_one_var(dictitem_T *v, char *prefix, int *first);
180static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
181
182/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200183 * Initialize global and vim special variables
184 */
185 void
186evalvars_init(void)
187{
188 int i;
189 struct vimvar *p;
190
191 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
192 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
193 vimvardict.dv_lock = VAR_FIXED;
194 hash_init(&compat_hashtab);
195
196 for (i = 0; i < VV_LEN; ++i)
197 {
198 p = &vimvars[i];
199 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
200 {
201 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
202 getout(1);
203 }
204 STRCPY(p->vv_di.di_key, p->vv_name);
205 if (p->vv_flags & VV_RO)
206 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
207 else if (p->vv_flags & VV_RO_SBX)
208 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
209 else
210 p->vv_di.di_flags = DI_FLAGS_FIX;
211
212 // add to v: scope dict, unless the value is not always available
213 if (p->vv_type != VAR_UNKNOWN)
214 hash_add(&vimvarht, p->vv_di.di_key);
215 if (p->vv_flags & VV_COMPAT)
216 // add to compat scope dict
217 hash_add(&compat_hashtab, p->vv_di.di_key);
218 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200219 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
220 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200221
222 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
223 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100224 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200225 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
226 set_vim_var_list(VV_ERRORS, list_alloc());
227 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
228
229 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
230 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
231 set_vim_var_nr(VV_NONE, VVAL_NONE);
232 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100233 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
234 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100235 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200236
237 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
238 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
239 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
240 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
241 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
242 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
243 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
244 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
245 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
246 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
247 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
248
249 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
250
Bram Moolenaar439c0362020-06-06 15:58:03 +0200251 // Default for v:register is not 0 but '"'. This is adjusted once the
252 // clipboard has been setup by calling reset_reg_var().
253 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200254}
255
256#if defined(EXITFREE) || defined(PROTO)
257/*
258 * Free all vim variables information on exit
259 */
260 void
261evalvars_clear(void)
262{
263 int i;
264 struct vimvar *p;
265
266 for (i = 0; i < VV_LEN; ++i)
267 {
268 p = &vimvars[i];
269 if (p->vv_di.di_tv.v_type == VAR_STRING)
270 VIM_CLEAR(p->vv_str);
271 else if (p->vv_di.di_tv.v_type == VAR_LIST)
272 {
273 list_unref(p->vv_list);
274 p->vv_list = NULL;
275 }
276 }
277 hash_clear(&vimvarht);
278 hash_init(&vimvarht); // garbage_collect() will access it
279 hash_clear(&compat_hashtab);
280
281 // global variables
282 vars_clear(&globvarht);
283
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100284 // Script-local variables. Clear all the variables here.
285 // The scriptvar_T is cleared later in free_scriptnames(), because a
286 // variable in one script might hold a reference to the whole scope of
287 // another script.
288 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200289 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200290}
291#endif
292
293 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200294garbage_collect_globvars(int copyID)
295{
296 return set_ref_in_ht(&globvarht, copyID, NULL);
297}
298
299 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200300garbage_collect_vimvars(int copyID)
301{
302 return set_ref_in_ht(&vimvarht, copyID, NULL);
303}
304
305 int
306garbage_collect_scriptvars(int copyID)
307{
Bram Moolenaared234f22020-10-15 20:42:20 +0200308 int i;
309 int idx;
310 int abort = FALSE;
311 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200312
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100313 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200314 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200315 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
316
Bram Moolenaared234f22020-10-15 20:42:20 +0200317 si = SCRIPT_ITEM(i);
318 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
319 {
320 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
321
322 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
323 }
324 }
325
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200326 return abort;
327}
328
329/*
330 * Set an internal variable to a string value. Creates the variable if it does
331 * not already exist.
332 */
333 void
334set_internal_string_var(char_u *name, char_u *value)
335{
336 char_u *val;
337 typval_T *tvp;
338
339 val = vim_strsave(value);
340 if (val != NULL)
341 {
342 tvp = alloc_string_tv(val);
343 if (tvp != NULL)
344 {
345 set_var(name, tvp, FALSE);
346 free_tv(tvp);
347 }
348 }
349}
350
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200351 int
352eval_charconvert(
353 char_u *enc_from,
354 char_u *enc_to,
355 char_u *fname_from,
356 char_u *fname_to)
357{
358 int err = FALSE;
359
360 set_vim_var_string(VV_CC_FROM, enc_from, -1);
361 set_vim_var_string(VV_CC_TO, enc_to, -1);
362 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
363 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
364 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
365 err = TRUE;
366 set_vim_var_string(VV_CC_FROM, NULL, -1);
367 set_vim_var_string(VV_CC_TO, NULL, -1);
368 set_vim_var_string(VV_FNAME_IN, NULL, -1);
369 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
370
371 if (err)
372 return FAIL;
373 return OK;
374}
375
376# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
377 int
378eval_printexpr(char_u *fname, char_u *args)
379{
380 int err = FALSE;
381
382 set_vim_var_string(VV_FNAME_IN, fname, -1);
383 set_vim_var_string(VV_CMDARG, args, -1);
384 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
385 err = TRUE;
386 set_vim_var_string(VV_FNAME_IN, NULL, -1);
387 set_vim_var_string(VV_CMDARG, NULL, -1);
388
389 if (err)
390 {
391 mch_remove(fname);
392 return FAIL;
393 }
394 return OK;
395}
396# endif
397
398# if defined(FEAT_DIFF) || defined(PROTO)
399 void
400eval_diff(
401 char_u *origfile,
402 char_u *newfile,
403 char_u *outfile)
404{
405 int err = FALSE;
406
407 set_vim_var_string(VV_FNAME_IN, origfile, -1);
408 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
409 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
410 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
411 set_vim_var_string(VV_FNAME_IN, NULL, -1);
412 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
413 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
414}
415
416 void
417eval_patch(
418 char_u *origfile,
419 char_u *difffile,
420 char_u *outfile)
421{
422 int err;
423
424 set_vim_var_string(VV_FNAME_IN, origfile, -1);
425 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
426 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
427 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
428 set_vim_var_string(VV_FNAME_IN, NULL, -1);
429 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
430 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
431}
432# endif
433
434#if defined(FEAT_SPELL) || defined(PROTO)
435/*
436 * Evaluate an expression to a list with suggestions.
437 * For the "expr:" part of 'spellsuggest'.
438 * Returns NULL when there is an error.
439 */
440 list_T *
441eval_spell_expr(char_u *badword, char_u *expr)
442{
443 typval_T save_val;
444 typval_T rettv;
445 list_T *list = NULL;
446 char_u *p = skipwhite(expr);
447
448 // Set "v:val" to the bad word.
449 prepare_vimvar(VV_VAL, &save_val);
450 set_vim_var_string(VV_VAL, badword, -1);
451 if (p_verbose == 0)
452 ++emsg_off;
453
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200454 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200455 {
456 if (rettv.v_type != VAR_LIST)
457 clear_tv(&rettv);
458 else
459 list = rettv.vval.v_list;
460 }
461
462 if (p_verbose == 0)
463 --emsg_off;
464 clear_tv(get_vim_var_tv(VV_VAL));
465 restore_vimvar(VV_VAL, &save_val);
466
467 return list;
468}
469
470/*
471 * "list" is supposed to contain two items: a word and a number. Return the
472 * word in "pp" and the number as the return value.
473 * Return -1 if anything isn't right.
474 * Used to get the good word and score from the eval_spell_expr() result.
475 */
476 int
477get_spellword(list_T *list, char_u **pp)
478{
479 listitem_T *li;
480
481 li = list->lv_first;
482 if (li == NULL)
483 return -1;
484 *pp = tv_get_string(&li->li_tv);
485
486 li = li->li_next;
487 if (li == NULL)
488 return -1;
489 return (int)tv_get_number(&li->li_tv);
490}
491#endif
492
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200493/*
494 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200495 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200496 * When not used yet add the variable to the v: hashtable.
497 */
498 void
499prepare_vimvar(int idx, typval_T *save_tv)
500{
501 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200502 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200503 if (vimvars[idx].vv_type == VAR_UNKNOWN)
504 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
505}
506
507/*
508 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200509 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200510 * When no longer defined, remove the variable from the v: hashtable.
511 */
512 void
513restore_vimvar(int idx, typval_T *save_tv)
514{
515 hashitem_T *hi;
516
517 vimvars[idx].vv_tv = *save_tv;
518 if (vimvars[idx].vv_type == VAR_UNKNOWN)
519 {
520 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
521 if (HASHITEM_EMPTY(hi))
522 internal_error("restore_vimvar()");
523 else
524 hash_remove(&vimvarht, hi);
525 }
526}
527
528/*
529 * List Vim variables.
530 */
531 static void
532list_vim_vars(int *first)
533{
534 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
535}
536
537/*
538 * List script-local variables, if there is a script.
539 */
540 static void
541list_script_vars(int *first)
542{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200543 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200544 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
545 "s:", FALSE, first);
546}
547
548/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200549 * Get a list of lines from a HERE document. The here document is a list of
550 * lines surrounded by a marker.
551 * cmd << {marker}
552 * {line1}
553 * {line2}
554 * ....
555 * {marker}
556 *
557 * The {marker} is a string. If the optional 'trim' word is supplied before the
558 * marker, then the leading indentation before the lines (matching the
559 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200560 *
561 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
562 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
563 * missing, then '.' is accepted as a marker.
564 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200565 * Returns a List with {lines} or NULL.
566 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200568heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200569{
570 char_u *theline;
571 char_u *marker;
572 list_T *l;
573 char_u *p;
574 int marker_indent_len = 0;
575 int text_indent_len = 0;
576 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200577 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200578 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200579
580 if (eap->getline == NULL)
581 {
582 emsg(_("E991: cannot use =<< here"));
583 return NULL;
584 }
585
586 // Check for the optional 'trim' word before the marker
587 cmd = skipwhite(cmd);
588 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
589 {
590 cmd = skipwhite(cmd + 4);
591
592 // Trim the indentation from all the lines in the here document.
593 // The amount of indentation trimmed is the same as the indentation of
594 // the first line after the :let command line. To find the end marker
595 // the indent of the :let command line is trimmed.
596 p = *eap->cmdlinep;
597 while (VIM_ISWHITE(*p))
598 {
599 p++;
600 marker_indent_len++;
601 }
602 text_indent_len = -1;
603 }
604
605 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200606 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200607 {
608 marker = skipwhite(cmd);
609 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200610 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200611 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200612 semsg(_(e_trailing_arg), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200613 return NULL;
614 }
615 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200616 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200617 {
618 emsg(_("E221: Marker cannot start with lower case letter"));
619 return NULL;
620 }
621 }
622 else
623 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200624 // When getting lines for an embedded script, if the marker is missing,
625 // accept '.' as the marker.
626 if (script_get)
627 marker = dot;
628 else
629 {
630 emsg(_("E172: Missing marker"));
631 return NULL;
632 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200633 }
634
635 l = list_alloc();
636 if (l == NULL)
637 return NULL;
638
639 for (;;)
640 {
641 int mi = 0;
642 int ti = 0;
643
644 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
645 if (theline == NULL)
646 {
647 semsg(_("E990: Missing end marker '%s'"), marker);
648 break;
649 }
650
651 // with "trim": skip the indent matching the :let line to find the
652 // marker
653 if (marker_indent_len > 0
654 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
655 mi = marker_indent_len;
656 if (STRCMP(marker, theline + mi) == 0)
657 {
658 vim_free(theline);
659 break;
660 }
661
662 if (text_indent_len == -1 && *theline != NUL)
663 {
664 // set the text indent from the first line.
665 p = theline;
666 text_indent_len = 0;
667 while (VIM_ISWHITE(*p))
668 {
669 p++;
670 text_indent_len++;
671 }
672 text_indent = vim_strnsave(theline, text_indent_len);
673 }
674 // with "trim": skip the indent matching the first line
675 if (text_indent != NULL)
676 for (ti = 0; ti < text_indent_len; ++ti)
677 if (theline[ti] != text_indent[ti])
678 break;
679
680 if (list_append_string(l, theline + ti, -1) == FAIL)
681 break;
682 vim_free(theline);
683 }
684 vim_free(text_indent);
685
686 return l;
687}
688
689/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200690 * Vim9 variable declaration:
691 * ":var name"
692 * ":var name: type"
693 * ":var name = expr"
694 * ":var name: type = expr"
695 * etc.
696 */
697 void
698ex_var(exarg_T *eap)
699{
700 if (!in_vim9script())
701 {
702 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
703 return;
704 }
705 ex_let(eap);
706}
707
708/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200709 * ":let" list all variable values
710 * ":let var1 var2" list variable values
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 var /= expr" assignment command.
716 * ":let var %= expr" assignment command.
717 * ":let var .= expr" assignment command.
718 * ":let var ..= expr" assignment command.
719 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100721 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200722 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200723 * ":final var = expr" assignment command.
724 * ":final [var1, var2] = expr" unpack list.
725 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200726 * ":const" list all variable values
727 * ":const var1 var2" list variable values
728 * ":const var = expr" assignment command.
729 * ":const [var1, var2] = expr" unpack list.
730 */
731 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200732ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200733{
734 char_u *arg = eap->arg;
735 char_u *expr = NULL;
736 typval_T rettv;
737 int i;
738 int var_count = 0;
739 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200740 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200741 char_u *argend;
742 int first = TRUE;
743 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200744 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100745 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200746 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200747
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200748 if (eap->cmdidx == CMD_final && !vim9script)
749 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100750 // In legacy Vim script ":final" is short for ":finally".
751 ex_finally(eap);
752 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200753 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200754 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200755 {
756 emsg(_(e_cannot_use_let_in_vim9_script));
757 return;
758 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200759
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100760 if (eap->cmdidx == CMD_const)
761 flags |= ASSIGN_CONST;
762 else if (eap->cmdidx == CMD_final)
763 flags |= ASSIGN_FINAL;
764
765 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100766 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200767 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100768
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200769 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200770 if (argend == NULL)
771 return;
772 if (argend > arg && argend[-1] == '.') // for var.='str'
773 --argend;
774 expr = skipwhite(argend);
775 concat = expr[0] == '.'
776 && ((expr[1] == '=' && current_sctx.sc_version < 2)
777 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200778 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
779 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200780 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200781 {
782 // ":let" without "=": list variables
783 if (*arg == '[')
784 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200785 else if (expr[0] == '.' && expr[1] == '=')
786 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200787 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200788 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200789 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200790 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100791 // Vim9 declaration ":var name: type"
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200792 arg = vim9_declare_scriptvar(eap, arg);
793 }
794 else
795 {
796 // ":let var1 var2" - list values
797 arg = list_arg_vars(eap, arg, &first);
798 }
799 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200800 else if (!eap->skip)
801 {
802 // ":let"
803 list_glob_vars(&first);
804 list_buf_vars(&first);
805 list_win_vars(&first);
806 list_tab_vars(&first);
807 list_script_vars(&first);
808 list_func_vars(&first);
809 list_vim_vars(&first);
810 }
811 eap->nextcmd = check_nextcmd(arg);
812 }
813 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
814 {
815 list_T *l;
816
817 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200818 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200819 if (l != NULL)
820 {
821 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200822 if (!eap->skip)
823 {
824 op[0] = '=';
825 op[1] = NUL;
826 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200828 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200829 clear_tv(&rettv);
830 }
831 }
832 else
833 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200834 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200835 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200836
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200837 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200838 i = FAIL;
839 if (has_assign || concat)
840 {
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100841 int cur_lnum;
842
Bram Moolenaar32e35112020-05-14 22:41:15 +0200843 op[0] = '=';
844 op[1] = NUL;
845 if (*expr != '=')
846 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200847 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200848 {
849 // +=, /=, etc. require an existing variable
850 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
851 i = FAIL;
852 }
853 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200854 {
855 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200856 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200857 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200858 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200859 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200860 ++len;
861 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200862 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200863 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200864 }
865 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200866 ++expr;
867
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200868 if (vim9script && (!VIM_ISWHITE(*argend)
869 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200870 {
871 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100872 semsg(_(e_white_space_required_before_and_after_str_at_str),
873 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200874 i = FAIL;
875 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200876
877 if (eap->skip)
878 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200879 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200880 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200881 if (getline_equal(eap->getline, eap->cookie, getsourceline))
882 {
883 evalarg.eval_getline = eap->getline;
884 evalarg.eval_cookie = eap->cookie;
885 }
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200886 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100887 cur_lnum = SOURCING_LNUM;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200888 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200889 if (eap->skip)
890 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200891 clear_evalarg(&evalarg, eap);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100892
893 // Restore the line number so that any type error is given for the
894 // declaration, not the expression.
895 SOURCING_LNUM = cur_lnum;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200896 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200897 if (eap->skip)
898 {
899 if (i != FAIL)
900 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200901 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200902 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200903 {
904 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200905 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200906 clear_tv(&rettv);
907 }
908 }
909}
910
911/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +0100912 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200913 * Handles both "var" with any type and "[var, var; var]" with a list type.
914 * When "op" is not NULL it points to a string with characters that
915 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
916 * or concatenate.
917 * Returns OK or FAIL;
918 */
919 int
920ex_let_vars(
921 char_u *arg_start,
922 typval_T *tv,
923 int copy, // copy values from "tv", don't move
924 int semicolon, // from skip_var_list()
925 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100926 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200927 char_u *op)
928{
929 char_u *arg = arg_start;
930 list_T *l;
931 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100932 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200933 listitem_T *item;
934 typval_T ltv;
935
936 if (*arg != '[')
937 {
938 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100939 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200940 return FAIL;
941 return OK;
942 }
943
944 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
945 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
946 {
947 emsg(_(e_listreq));
948 return FAIL;
949 }
950
951 i = list_len(l);
952 if (semicolon == 0 && var_count < i)
953 {
954 emsg(_("E687: Less targets than List items"));
955 return FAIL;
956 }
957 if (var_count - semicolon > i)
958 {
959 emsg(_("E688: More targets than List items"));
960 return FAIL;
961 }
962
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200963 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200964 item = l->lv_first;
965 while (*arg != ']')
966 {
967 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100968 ++var_idx;
969 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]",
970 op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200971 item = item->li_next;
972 if (arg == NULL)
973 return FAIL;
974
975 arg = skipwhite(arg);
976 if (*arg == ';')
977 {
978 // Put the rest of the list (may be empty) in the var after ';'.
979 // Create a new list for this.
980 l = list_alloc();
981 if (l == NULL)
982 return FAIL;
983 while (item != NULL)
984 {
985 list_append_tv(l, &item->li_tv);
986 item = item->li_next;
987 }
988
989 ltv.v_type = VAR_LIST;
990 ltv.v_lock = 0;
991 ltv.vval.v_list = l;
992 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100993 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200994
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100996 (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200997 clear_tv(&ltv);
998 if (arg == NULL)
999 return FAIL;
1000 break;
1001 }
1002 else if (*arg != ',' && *arg != ']')
1003 {
1004 internal_error("ex_let_vars()");
1005 return FAIL;
1006 }
1007 }
1008
1009 return OK;
1010}
1011
1012/*
1013 * Skip over assignable variable "var" or list of variables "[var, var]".
1014 * Used for ":let varvar = expr" and ":for varvar in expr".
1015 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001016 * for "[var, var; var]" set "semicolon" to 1.
1017 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001018 * Return NULL for an error.
1019 */
1020 char_u *
1021skip_var_list(
1022 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001024 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001025 int *semicolon,
1026 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001027{
1028 char_u *p, *s;
1029
1030 if (*arg == '[')
1031 {
1032 // "[var, var]": find the matching ']'.
1033 p = arg;
1034 for (;;)
1035 {
1036 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001037 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001038 if (s == p)
1039 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001040 if (!silent)
1041 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001042 return NULL;
1043 }
1044 ++*var_count;
1045
1046 p = skipwhite(s);
1047 if (*p == ']')
1048 break;
1049 else if (*p == ';')
1050 {
1051 if (*semicolon == 1)
1052 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001053 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001054 return NULL;
1055 }
1056 *semicolon = 1;
1057 }
1058 else if (*p != ',')
1059 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001060 if (!silent)
1061 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001062 return NULL;
1063 }
1064 }
1065 return p + 1;
1066 }
1067 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001069}
1070
1071/*
1072 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1073 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001074 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001075 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001076 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001078{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001079 char_u *end;
1080 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001082 if (*arg == '@' && arg[1] != NUL)
1083 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001085 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001086
1087 // "a: type" is declaring variable "a" with a type, not "a:".
1088 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001089 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001090 --end;
1091
Bram Moolenaar585587d2021-01-17 20:52:13 +01001092 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001093 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001094 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001095 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096 }
1097 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001098}
1099
1100/*
1101 * List variables for hashtab "ht" with prefix "prefix".
1102 * If "empty" is TRUE also list NULL strings as empty strings.
1103 */
1104 void
1105list_hashtable_vars(
1106 hashtab_T *ht,
1107 char *prefix,
1108 int empty,
1109 int *first)
1110{
1111 hashitem_T *hi;
1112 dictitem_T *di;
1113 int todo;
1114 char_u buf[IOSIZE];
1115
1116 todo = (int)ht->ht_used;
1117 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1118 {
1119 if (!HASHITEM_EMPTY(hi))
1120 {
1121 --todo;
1122 di = HI2DI(hi);
1123
1124 // apply :filter /pat/ to variable name
1125 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1126 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1127 if (message_filtered(buf))
1128 continue;
1129
1130 if (empty || di->di_tv.v_type != VAR_STRING
1131 || di->di_tv.vval.v_string != NULL)
1132 list_one_var(di, prefix, first);
1133 }
1134 }
1135}
1136
1137/*
1138 * List global variables.
1139 */
1140 static void
1141list_glob_vars(int *first)
1142{
1143 list_hashtable_vars(&globvarht, "", TRUE, first);
1144}
1145
1146/*
1147 * List buffer variables.
1148 */
1149 static void
1150list_buf_vars(int *first)
1151{
1152 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1153}
1154
1155/*
1156 * List window variables.
1157 */
1158 static void
1159list_win_vars(int *first)
1160{
1161 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1162}
1163
1164/*
1165 * List tab page variables.
1166 */
1167 static void
1168list_tab_vars(int *first)
1169{
1170 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1171}
1172
1173/*
1174 * List variables in "arg".
1175 */
1176 static char_u *
1177list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1178{
1179 int error = FALSE;
1180 int len;
1181 char_u *name;
1182 char_u *name_start;
1183 char_u *arg_subsc;
1184 char_u *tofree;
1185 typval_T tv;
1186
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001187 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001188 {
1189 if (error || eap->skip)
1190 {
1191 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1192 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1193 {
1194 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001195 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001196 break;
1197 }
1198 }
1199 else
1200 {
1201 // get_name_len() takes care of expanding curly braces
1202 name_start = name = arg;
1203 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1204 if (len <= 0)
1205 {
1206 // This is mainly to keep test 49 working: when expanding
1207 // curly braces fails overrule the exception error message.
1208 if (len < 0 && !aborting())
1209 {
1210 emsg_severe = TRUE;
1211 semsg(_(e_invarg2), arg);
1212 break;
1213 }
1214 error = TRUE;
1215 }
1216 else
1217 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001218 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001219 if (tofree != NULL)
1220 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001221 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001222 error = TRUE;
1223 else
1224 {
1225 // handle d.key, l[idx], f(expr)
1226 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001227 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001228 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001229 error = TRUE;
1230 else
1231 {
1232 if (arg == arg_subsc && len == 2 && name[1] == ':')
1233 {
1234 switch (*name)
1235 {
1236 case 'g': list_glob_vars(first); break;
1237 case 'b': list_buf_vars(first); break;
1238 case 'w': list_win_vars(first); break;
1239 case 't': list_tab_vars(first); break;
1240 case 'v': list_vim_vars(first); break;
1241 case 's': list_script_vars(first); break;
1242 case 'l': list_func_vars(first); break;
1243 default:
1244 semsg(_("E738: Can't list variables for %s"), name);
1245 }
1246 }
1247 else
1248 {
1249 char_u numbuf[NUMBUFLEN];
1250 char_u *tf;
1251 int c;
1252 char_u *s;
1253
1254 s = echo_string(&tv, &tf, numbuf, 0);
1255 c = *arg;
1256 *arg = NUL;
1257 list_one_var_a("",
1258 arg == arg_subsc ? name : name_start,
1259 tv.v_type,
1260 s == NULL ? (char_u *)"" : s,
1261 first);
1262 *arg = c;
1263 vim_free(tf);
1264 }
1265 clear_tv(&tv);
1266 }
1267 }
1268 }
1269
1270 vim_free(tofree);
1271 }
1272
1273 arg = skipwhite(arg);
1274 }
1275
1276 return arg;
1277}
1278
1279/*
1280 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1281 * Returns a pointer to the char just after the var name.
1282 * Returns NULL if there is an error.
1283 */
1284 static char_u *
1285ex_let_one(
1286 char_u *arg, // points to variable name
1287 typval_T *tv, // value to assign to variable
1288 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001289 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001290 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001291 char_u *op, // "+", "-", "." or NULL
1292 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001293{
1294 int c1;
1295 char_u *name;
1296 char_u *p;
1297 char_u *arg_end = NULL;
1298 int len;
1299 int opt_flags;
1300 char_u *tofree = NULL;
1301
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001302 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001303 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001304 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1305 {
1306 vim9_declare_error(arg);
1307 return NULL;
1308 }
1309
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001310 // ":let $VAR = expr": Set environment variable.
1311 if (*arg == '$')
1312 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001313 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001314 {
1315 emsg(_("E996: Cannot lock an environment variable"));
1316 return NULL;
1317 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001318
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001319 // Find the end of the name.
1320 ++arg;
1321 name = arg;
1322 len = get_env_len(&arg);
1323 if (len == 0)
1324 semsg(_(e_invarg2), name - 1);
1325 else
1326 {
1327 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1328 semsg(_(e_letwrong), op);
1329 else if (endchars != NULL
1330 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1331 emsg(_(e_letunexp));
1332 else if (!check_secure())
1333 {
1334 c1 = name[len];
1335 name[len] = NUL;
1336 p = tv_get_string_chk(tv);
1337 if (p != NULL && op != NULL && *op == '.')
1338 {
1339 int mustfree = FALSE;
1340 char_u *s = vim_getenv(name, &mustfree);
1341
1342 if (s != NULL)
1343 {
1344 p = tofree = concat_str(s, p);
1345 if (mustfree)
1346 vim_free(s);
1347 }
1348 }
1349 if (p != NULL)
1350 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001351 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001352 arg_end = arg;
1353 }
1354 name[len] = c1;
1355 vim_free(tofree);
1356 }
1357 }
1358 }
1359
1360 // ":let &option = expr": Set option value.
1361 // ":let &l:option = expr": Set local option value.
1362 // ":let &g:option = expr": Set global option value.
1363 else if (*arg == '&')
1364 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001365 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001366 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001367 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001368 return NULL;
1369 }
1370 // Find the end of the name.
1371 p = find_option_end(&arg, &opt_flags);
1372 if (p == NULL || (endchars != NULL
1373 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1374 emsg(_(e_letunexp));
1375 else
1376 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001377 long n = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001378 getoption_T opt_type;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001379 long numval;
1380 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001381 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001382 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001383
1384 c1 = *p;
1385 *p = NUL;
1386
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001387 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001388 if ((opt_type == gov_bool
1389 || opt_type == gov_number
1390 || opt_type == gov_hidden_bool
1391 || opt_type == gov_hidden_number)
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001392 && (tv->v_type != VAR_STRING || !in_vim9script()))
Bram Moolenaar0ea04402021-01-04 13:37:54 +01001393 {
1394 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1395 // bool, possibly hidden
1396 n = (long)tv_get_bool(tv);
1397 else
1398 // number, possibly hidden
1399 n = (long)tv_get_number(tv);
1400 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001401
1402 // Avoid setting a string option to the text "v:false" or similar.
1403 // In Vim9 script also don't convert a number to string.
1404 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1405 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1406 s = tv_get_string_chk(tv);
1407
1408 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001409 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001410 if (((opt_type == gov_bool || opt_type == gov_number)
1411 && *op == '.')
1412 || (opt_type == gov_string && *op != '.'))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001413 {
1414 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001415 failed = TRUE; // don't set the value
1416
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001417 }
1418 else
1419 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001420 // number, in legacy script also bool
1421 if (opt_type == gov_number
1422 || (opt_type == gov_bool && !in_vim9script()))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001423 {
1424 switch (*op)
1425 {
1426 case '+': n = numval + n; break;
1427 case '-': n = numval - n; break;
1428 case '*': n = numval * n; break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001429 case '/': n = (long)num_divide(numval, n,
1430 &failed); break;
1431 case '%': n = (long)num_modulus(numval, n,
1432 &failed); break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001433 }
1434 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001435 else if (opt_type == gov_string
1436 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001437 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001438 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001439 s = concat_str(stringval, s);
1440 vim_free(stringval);
1441 stringval = s;
1442 }
1443 }
1444 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001445
1446 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001447 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001448 if (opt_type != gov_string || s != NULL)
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001449 {
1450 set_option_value(arg, n, s, opt_flags);
1451 arg_end = p;
1452 }
1453 else
1454 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001455 }
1456 *p = c1;
1457 vim_free(stringval);
1458 }
1459 }
1460
1461 // ":let @r = expr": Set register contents.
1462 else if (*arg == '@')
1463 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001464 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001465 {
1466 emsg(_("E996: Cannot lock a register"));
1467 return NULL;
1468 }
1469 ++arg;
1470 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1471 semsg(_(e_letwrong), op);
1472 else if (endchars != NULL
1473 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1474 emsg(_(e_letunexp));
1475 else
1476 {
1477 char_u *ptofree = NULL;
1478 char_u *s;
1479
1480 p = tv_get_string_chk(tv);
1481 if (p != NULL && op != NULL && *op == '.')
1482 {
1483 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1484 if (s != NULL)
1485 {
1486 p = ptofree = concat_str(s, p);
1487 vim_free(s);
1488 }
1489 }
1490 if (p != NULL)
1491 {
1492 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1493 arg_end = arg + 1;
1494 }
1495 vim_free(ptofree);
1496 }
1497 }
1498
1499 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001501 // ":let {expr} = expr": Idem, name made with curly braces
1502 else if (eval_isnamec1(*arg) || *arg == '{')
1503 {
1504 lval_T lv;
1505
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001506 p = get_lval(arg, tv, &lv, FALSE, FALSE,
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001507 (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1508 ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001509 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001510 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511 if (endchars != NULL && vim_strchr(endchars,
1512 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001513 emsg(_(e_letunexp));
1514 else
1515 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001516 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001517 arg_end = p;
1518 }
1519 }
1520 clear_lval(&lv);
1521 }
1522
1523 else
1524 semsg(_(e_invarg2), arg);
1525
1526 return arg_end;
1527}
1528
1529/*
1530 * ":unlet[!] var1 ... " command.
1531 */
1532 void
1533ex_unlet(exarg_T *eap)
1534{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001535 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001536}
1537
1538/*
1539 * ":lockvar" and ":unlockvar" commands
1540 */
1541 void
1542ex_lockvar(exarg_T *eap)
1543{
1544 char_u *arg = eap->arg;
1545 int deep = 2;
1546
1547 if (eap->forceit)
1548 deep = -1;
1549 else if (vim_isdigit(*arg))
1550 {
1551 deep = getdigits(&arg);
1552 arg = skipwhite(arg);
1553 }
1554
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001555 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001556}
1557
1558/*
1559 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001560 * Also used for Vim9 script. "callback" is invoked as:
1561 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001562 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001563 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001564ex_unletlock(
1565 exarg_T *eap,
1566 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001567 int deep,
1568 int glv_flags,
1569 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1570 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001571{
1572 char_u *arg = argstart;
1573 char_u *name_end;
1574 int error = FALSE;
1575 lval_T lv;
1576
1577 do
1578 {
1579 if (*arg == '$')
1580 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001581 lv.ll_name = arg;
1582 lv.ll_tv = NULL;
1583 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001584 if (get_env_len(&arg) == 0)
1585 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001586 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001587 return;
1588 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001589 if (!error && !eap->skip
1590 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1591 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001592 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001593 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001594 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001595 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001596 // Parse the name and find the end.
1597 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001598 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001599 if (lv.ll_name == NULL)
1600 error = TRUE; // error but continue parsing
1601 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1602 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001603 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001604 if (name_end != NULL)
1605 {
1606 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001607 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001608 }
1609 if (!(eap->skip || error))
1610 clear_lval(&lv);
1611 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001612 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001613
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001614 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001615 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001616 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001617
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001618 if (!eap->skip)
1619 clear_lval(&lv);
1620 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001621
1622 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001623 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001624
1625 eap->nextcmd = check_nextcmd(arg);
1626}
1627
1628 static int
1629do_unlet_var(
1630 lval_T *lp,
1631 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001632 exarg_T *eap,
1633 int deep UNUSED,
1634 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001635{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001636 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001637 int ret = OK;
1638 int cc;
1639
1640 if (lp->ll_tv == NULL)
1641 {
1642 cc = *name_end;
1643 *name_end = NUL;
1644
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001645 // Environment variable, normal name or expanded name.
1646 if (*lp->ll_name == '$')
1647 vim_unsetenv(lp->ll_name + 1);
1648 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001649 ret = FAIL;
1650 *name_end = cc;
1651 }
1652 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001653 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001654 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001655 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001656 return FAIL;
1657 else if (lp->ll_range)
1658 {
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001659 if (list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_name, lp->ll_n1,
1660 !lp->ll_empty2, lp->ll_n2) == FAIL)
1661 return FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001662 }
1663 else
1664 {
1665 if (lp->ll_list != NULL)
1666 // unlet a List item.
1667 listitem_remove(lp->ll_list, lp->ll_li);
1668 else
1669 // unlet a Dictionary item.
1670 dictitem_remove(lp->ll_dict, lp->ll_di);
1671 }
1672
1673 return ret;
1674}
1675
1676/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001677 * Unlet one item or a range of items from a list.
1678 * Return OK or FAIL.
1679 */
1680 int
1681list_unlet_range(
1682 list_T *l,
1683 listitem_T *li_first,
1684 char_u *name,
1685 long n1_arg,
1686 int has_n2,
1687 long n2)
1688{
1689 listitem_T *li = li_first;
1690 int n1 = n1_arg;
1691
1692 while (li != NULL && (!has_n2 || n2 >= n1))
1693 {
1694 if (value_check_lock(li->li_tv.v_lock, name, FALSE))
1695 return FAIL;
1696 li = li->li_next;
1697 ++n1;
1698 }
1699
1700 // Delete a range of List items.
1701 li = li_first;
1702 n1 = n1_arg;
1703 while (li != NULL && (!has_n2 || n2 >= n1))
1704 {
1705 listitem_T *next = li->li_next;
1706
1707 listitem_remove(l, li);
1708 li = next;
1709 ++n1;
1710 }
1711 return OK;
1712}
1713/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001714 * "unlet" a variable. Return OK if it existed, FAIL if not.
1715 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1716 */
1717 int
1718do_unlet(char_u *name, int forceit)
1719{
1720 hashtab_T *ht;
1721 hashitem_T *hi;
1722 char_u *varname;
1723 dict_T *d;
1724 dictitem_T *di;
1725
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001726 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001727 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001728 return FAIL;
1729
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001730 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001731
1732 // can't :unlet a script variable in Vim9 script from a function
1733 if (ht == get_script_local_ht()
1734 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1735 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1736 == SCRIPT_VERSION_VIM9
1737 && check_vim9_unlet(name) == FAIL)
1738 return FAIL;
1739
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001740 if (ht != NULL && *varname != NUL)
1741 {
1742 d = get_current_funccal_dict(ht);
1743 if (d == NULL)
1744 {
1745 if (ht == &globvarht)
1746 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001747 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001748 d = &vimvardict;
1749 else
1750 {
1751 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1752 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1753 }
1754 if (d == NULL)
1755 {
1756 internal_error("do_unlet()");
1757 return FAIL;
1758 }
1759 }
1760 hi = hash_find(ht, varname);
1761 if (HASHITEM_EMPTY(hi))
1762 hi = find_hi_in_scoped_ht(name, &ht);
1763 if (hi != NULL && !HASHITEM_EMPTY(hi))
1764 {
1765 di = HI2DI(hi);
1766 if (var_check_fixed(di->di_flags, name, FALSE)
1767 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001768 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001769 return FAIL;
1770
1771 delete_var(ht, hi);
1772 return OK;
1773 }
1774 }
1775 if (forceit)
1776 return OK;
1777 semsg(_("E108: No such variable: \"%s\""), name);
1778 return FAIL;
1779}
1780
1781/*
1782 * Lock or unlock variable indicated by "lp".
1783 * "deep" is the levels to go (-1 for unlimited);
1784 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1785 */
1786 static int
1787do_lock_var(
1788 lval_T *lp,
1789 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001790 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001791 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001792 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001793{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001794 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001795 int ret = OK;
1796 int cc;
1797 dictitem_T *di;
1798
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001799 if (lp->ll_tv == NULL)
1800 {
1801 cc = *name_end;
1802 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001803 if (*lp->ll_name == '$')
1804 {
1805 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001806 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001807 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001808 else
1809 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001810 // Normal name or expanded name.
1811 di = find_var(lp->ll_name, NULL, TRUE);
1812 if (di == NULL)
1813 ret = FAIL;
1814 else if ((di->di_flags & DI_FLAGS_FIX)
1815 && di->di_tv.v_type != VAR_DICT
1816 && di->di_tv.v_type != VAR_LIST)
1817 {
1818 // For historic reasons this error is not given for a list or
1819 // dict. E.g., the b: dict could be locked/unlocked.
1820 semsg(_(e_lock_unlock), lp->ll_name);
1821 ret = FAIL;
1822 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001823 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001824 {
1825 if (lock)
1826 di->di_flags |= DI_FLAGS_LOCK;
1827 else
1828 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001829 if (deep != 0)
1830 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001831 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001832 }
1833 *name_end = cc;
1834 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001835 else if (deep == 0)
1836 {
1837 // nothing to do
1838 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001839 else if (lp->ll_range)
1840 {
1841 listitem_T *li = lp->ll_li;
1842
1843 // (un)lock a range of List items.
1844 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1845 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001846 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001847 li = li->li_next;
1848 ++lp->ll_n1;
1849 }
1850 }
1851 else if (lp->ll_list != NULL)
1852 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001853 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001854 else
1855 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001856 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001857
1858 return ret;
1859}
1860
1861/*
1862 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001863 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1864 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001865 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001866 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001867item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001868{
1869 static int recurse = 0;
1870 list_T *l;
1871 listitem_T *li;
1872 dict_T *d;
1873 blob_T *b;
1874 hashitem_T *hi;
1875 int todo;
1876
1877 if (recurse >= DICT_MAXNEST)
1878 {
1879 emsg(_("E743: variable nested too deep for (un)lock"));
1880 return;
1881 }
1882 if (deep == 0)
1883 return;
1884 ++recurse;
1885
1886 // lock/unlock the item itself
1887 if (lock)
1888 tv->v_lock |= VAR_LOCKED;
1889 else
1890 tv->v_lock &= ~VAR_LOCKED;
1891
1892 switch (tv->v_type)
1893 {
1894 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001895 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001896 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001897 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001898 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001899 case VAR_STRING:
1900 case VAR_FUNC:
1901 case VAR_PARTIAL:
1902 case VAR_FLOAT:
1903 case VAR_SPECIAL:
1904 case VAR_JOB:
1905 case VAR_CHANNEL:
1906 break;
1907
1908 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001909 if ((b = tv->vval.v_blob) != NULL
1910 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001911 {
1912 if (lock)
1913 b->bv_lock |= VAR_LOCKED;
1914 else
1915 b->bv_lock &= ~VAR_LOCKED;
1916 }
1917 break;
1918 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001919 if ((l = tv->vval.v_list) != NULL
1920 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001921 {
1922 if (lock)
1923 l->lv_lock |= VAR_LOCKED;
1924 else
1925 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001926 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001927 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001928 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001929 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001930 }
1931 break;
1932 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001933 if ((d = tv->vval.v_dict) != NULL
1934 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001935 {
1936 if (lock)
1937 d->dv_lock |= VAR_LOCKED;
1938 else
1939 d->dv_lock &= ~VAR_LOCKED;
1940 if (deep < 0 || deep > 1)
1941 {
1942 // recursive: lock/unlock the items the List contains
1943 todo = (int)d->dv_hashtab.ht_used;
1944 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1945 {
1946 if (!HASHITEM_EMPTY(hi))
1947 {
1948 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001949 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1950 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001951 }
1952 }
1953 }
1954 }
1955 }
1956 --recurse;
1957}
1958
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001959#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1960/*
1961 * Delete all "menutrans_" variables.
1962 */
1963 void
1964del_menutrans_vars(void)
1965{
1966 hashitem_T *hi;
1967 int todo;
1968
1969 hash_lock(&globvarht);
1970 todo = (int)globvarht.ht_used;
1971 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1972 {
1973 if (!HASHITEM_EMPTY(hi))
1974 {
1975 --todo;
1976 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1977 delete_var(&globvarht, hi);
1978 }
1979 }
1980 hash_unlock(&globvarht);
1981}
1982#endif
1983
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001984/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001985 * Local string buffer for the next two functions to store a variable name
1986 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1987 * get_user_var_name().
1988 */
1989
1990static char_u *varnamebuf = NULL;
1991static int varnamebuflen = 0;
1992
1993/*
1994 * Function to concatenate a prefix and a variable name.
1995 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001996 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001997cat_prefix_varname(int prefix, char_u *name)
1998{
1999 int len;
2000
2001 len = (int)STRLEN(name) + 3;
2002 if (len > varnamebuflen)
2003 {
2004 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002005 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002006 varnamebuf = alloc(len);
2007 if (varnamebuf == NULL)
2008 {
2009 varnamebuflen = 0;
2010 return NULL;
2011 }
2012 varnamebuflen = len;
2013 }
2014 *varnamebuf = prefix;
2015 varnamebuf[1] = ':';
2016 STRCPY(varnamebuf + 2, name);
2017 return varnamebuf;
2018}
2019
2020/*
2021 * Function given to ExpandGeneric() to obtain the list of user defined
2022 * (global/buffer/window/built-in) variable names.
2023 */
2024 char_u *
2025get_user_var_name(expand_T *xp, int idx)
2026{
2027 static long_u gdone;
2028 static long_u bdone;
2029 static long_u wdone;
2030 static long_u tdone;
2031 static int vidx;
2032 static hashitem_T *hi;
2033 hashtab_T *ht;
2034
2035 if (idx == 0)
2036 {
2037 gdone = bdone = wdone = vidx = 0;
2038 tdone = 0;
2039 }
2040
2041 // Global variables
2042 if (gdone < globvarht.ht_used)
2043 {
2044 if (gdone++ == 0)
2045 hi = globvarht.ht_array;
2046 else
2047 ++hi;
2048 while (HASHITEM_EMPTY(hi))
2049 ++hi;
2050 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2051 return cat_prefix_varname('g', hi->hi_key);
2052 return hi->hi_key;
2053 }
2054
2055 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002056 ht =
2057#ifdef FEAT_CMDWIN
2058 // In cmdwin, the alternative buffer should be used.
2059 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2060 &prevwin->w_buffer->b_vars->dv_hashtab :
2061#endif
2062 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002063 if (bdone < ht->ht_used)
2064 {
2065 if (bdone++ == 0)
2066 hi = ht->ht_array;
2067 else
2068 ++hi;
2069 while (HASHITEM_EMPTY(hi))
2070 ++hi;
2071 return cat_prefix_varname('b', hi->hi_key);
2072 }
2073
2074 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002075 ht =
2076#ifdef FEAT_CMDWIN
2077 // In cmdwin, the alternative window should be used.
2078 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2079 &prevwin->w_vars->dv_hashtab :
2080#endif
2081 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002082 if (wdone < ht->ht_used)
2083 {
2084 if (wdone++ == 0)
2085 hi = ht->ht_array;
2086 else
2087 ++hi;
2088 while (HASHITEM_EMPTY(hi))
2089 ++hi;
2090 return cat_prefix_varname('w', hi->hi_key);
2091 }
2092
2093 // t: variables
2094 ht = &curtab->tp_vars->dv_hashtab;
2095 if (tdone < ht->ht_used)
2096 {
2097 if (tdone++ == 0)
2098 hi = ht->ht_array;
2099 else
2100 ++hi;
2101 while (HASHITEM_EMPTY(hi))
2102 ++hi;
2103 return cat_prefix_varname('t', hi->hi_key);
2104 }
2105
2106 // v: variables
2107 if (vidx < VV_LEN)
2108 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2109
2110 VIM_CLEAR(varnamebuf);
2111 varnamebuflen = 0;
2112 return NULL;
2113}
2114
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002115 char *
2116get_var_special_name(int nr)
2117{
2118 switch (nr)
2119 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002120 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2121 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002122 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002123 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002124 }
2125 internal_error("get_var_special_name()");
2126 return "42";
2127}
2128
2129/*
2130 * Returns the global variable dictionary
2131 */
2132 dict_T *
2133get_globvar_dict(void)
2134{
2135 return &globvardict;
2136}
2137
2138/*
2139 * Returns the global variable hash table
2140 */
2141 hashtab_T *
2142get_globvar_ht(void)
2143{
2144 return &globvarht;
2145}
2146
2147/*
2148 * Returns the v: variable dictionary
2149 */
2150 dict_T *
2151get_vimvar_dict(void)
2152{
2153 return &vimvardict;
2154}
2155
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002156/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002157 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002158 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159 */
2160 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002161find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002163 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2164 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002165
2166 if (di == NULL)
2167 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002168 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2170 return (int)(vv - vimvars);
2171}
2172
2173
2174/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002175 * Set type of v: variable to "type".
2176 */
2177 void
2178set_vim_var_type(int idx, vartype_T type)
2179{
2180 vimvars[idx].vv_type = type;
2181}
2182
2183/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002184 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002185 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002186 */
2187 void
2188set_vim_var_nr(int idx, varnumber_T val)
2189{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002190 vimvars[idx].vv_nr = val;
2191}
2192
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193 char *
2194get_vim_var_name(int idx)
2195{
2196 return vimvars[idx].vv_name;
2197}
2198
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002199/*
2200 * Get typval_T v: variable value.
2201 */
2202 typval_T *
2203get_vim_var_tv(int idx)
2204{
2205 return &vimvars[idx].vv_tv;
2206}
2207
2208/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002209 * Set v: variable to "tv". Only accepts the same type.
2210 * Takes over the value of "tv".
2211 */
2212 int
2213set_vim_var_tv(int idx, typval_T *tv)
2214{
2215 if (vimvars[idx].vv_type != tv->v_type)
2216 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002217 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002218 clear_tv(tv);
2219 return FAIL;
2220 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002221 // VV_RO is also checked when compiling, but let's check here as well.
2222 if (vimvars[idx].vv_flags & VV_RO)
2223 {
2224 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2225 return FAIL;
2226 }
2227 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2228 {
2229 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2230 return FAIL;
2231 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002232 clear_tv(&vimvars[idx].vv_di.di_tv);
2233 vimvars[idx].vv_di.di_tv = *tv;
2234 return OK;
2235}
2236
2237/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002238 * Get number v: variable value.
2239 */
2240 varnumber_T
2241get_vim_var_nr(int idx)
2242{
2243 return vimvars[idx].vv_nr;
2244}
2245
2246/*
2247 * Get string v: variable value. Uses a static buffer, can only be used once.
2248 * If the String variable has never been set, return an empty string.
2249 * Never returns NULL;
2250 */
2251 char_u *
2252get_vim_var_str(int idx)
2253{
2254 return tv_get_string(&vimvars[idx].vv_tv);
2255}
2256
2257/*
2258 * Get List v: variable value. Caller must take care of reference count when
2259 * needed.
2260 */
2261 list_T *
2262get_vim_var_list(int idx)
2263{
2264 return vimvars[idx].vv_list;
2265}
2266
2267/*
2268 * Get Dict v: variable value. Caller must take care of reference count when
2269 * needed.
2270 */
2271 dict_T *
2272get_vim_var_dict(int idx)
2273{
2274 return vimvars[idx].vv_dict;
2275}
2276
2277/*
2278 * Set v:char to character "c".
2279 */
2280 void
2281set_vim_var_char(int c)
2282{
2283 char_u buf[MB_MAXBYTES + 1];
2284
2285 if (has_mbyte)
2286 buf[(*mb_char2bytes)(c, buf)] = NUL;
2287 else
2288 {
2289 buf[0] = c;
2290 buf[1] = NUL;
2291 }
2292 set_vim_var_string(VV_CHAR, buf, -1);
2293}
2294
2295/*
2296 * Set v:count to "count" and v:count1 to "count1".
2297 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2298 */
2299 void
2300set_vcount(
2301 long count,
2302 long count1,
2303 int set_prevcount)
2304{
2305 if (set_prevcount)
2306 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2307 vimvars[VV_COUNT].vv_nr = count;
2308 vimvars[VV_COUNT1].vv_nr = count1;
2309}
2310
2311/*
2312 * Save variables that might be changed as a side effect. Used when executing
2313 * a timer callback.
2314 */
2315 void
2316save_vimvars(vimvars_save_T *vvsave)
2317{
2318 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2319 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2320 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2321}
2322
2323/*
2324 * Restore variables saved by save_vimvars().
2325 */
2326 void
2327restore_vimvars(vimvars_save_T *vvsave)
2328{
2329 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2330 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2331 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2332}
2333
2334/*
2335 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2336 * value.
2337 */
2338 void
2339set_vim_var_string(
2340 int idx,
2341 char_u *val,
2342 int len) // length of "val" to use or -1 (whole string)
2343{
2344 clear_tv(&vimvars[idx].vv_di.di_tv);
2345 vimvars[idx].vv_type = VAR_STRING;
2346 if (val == NULL)
2347 vimvars[idx].vv_str = NULL;
2348 else if (len == -1)
2349 vimvars[idx].vv_str = vim_strsave(val);
2350 else
2351 vimvars[idx].vv_str = vim_strnsave(val, len);
2352}
2353
2354/*
2355 * Set List v: variable to "val".
2356 */
2357 void
2358set_vim_var_list(int idx, list_T *val)
2359{
2360 clear_tv(&vimvars[idx].vv_di.di_tv);
2361 vimvars[idx].vv_type = VAR_LIST;
2362 vimvars[idx].vv_list = val;
2363 if (val != NULL)
2364 ++val->lv_refcount;
2365}
2366
2367/*
2368 * Set Dictionary v: variable to "val".
2369 */
2370 void
2371set_vim_var_dict(int idx, dict_T *val)
2372{
2373 clear_tv(&vimvars[idx].vv_di.di_tv);
2374 vimvars[idx].vv_type = VAR_DICT;
2375 vimvars[idx].vv_dict = val;
2376 if (val != NULL)
2377 {
2378 ++val->dv_refcount;
2379 dict_set_items_ro(val);
2380 }
2381}
2382
2383/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002384 * Set the v:argv list.
2385 */
2386 void
2387set_argv_var(char **argv, int argc)
2388{
2389 list_T *l = list_alloc();
2390 int i;
2391
2392 if (l == NULL)
2393 getout(1);
2394 l->lv_lock = VAR_FIXED;
2395 for (i = 0; i < argc; ++i)
2396 {
2397 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2398 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002399 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002400 }
2401 set_vim_var_list(VV_ARGV, l);
2402}
2403
2404/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002405 * Reset v:register, taking the 'clipboard' setting into account.
2406 */
2407 void
2408reset_reg_var(void)
2409{
2410 int regname = 0;
2411
2412 // Adjust the register according to 'clipboard', so that when
2413 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2414#ifdef FEAT_CLIPBOARD
2415 adjust_clip_reg(&regname);
2416#endif
2417 set_reg_var(regname);
2418}
2419
2420/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002421 * Set v:register if needed.
2422 */
2423 void
2424set_reg_var(int c)
2425{
2426 char_u regname;
2427
2428 if (c == 0 || c == ' ')
2429 regname = '"';
2430 else
2431 regname = c;
2432 // Avoid free/alloc when the value is already right.
2433 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2434 set_vim_var_string(VV_REG, &regname, 1);
2435}
2436
2437/*
2438 * Get or set v:exception. If "oldval" == NULL, return the current value.
2439 * Otherwise, restore the value to "oldval" and return NULL.
2440 * Must always be called in pairs to save and restore v:exception! Does not
2441 * take care of memory allocations.
2442 */
2443 char_u *
2444v_exception(char_u *oldval)
2445{
2446 if (oldval == NULL)
2447 return vimvars[VV_EXCEPTION].vv_str;
2448
2449 vimvars[VV_EXCEPTION].vv_str = oldval;
2450 return NULL;
2451}
2452
2453/*
2454 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2455 * Otherwise, restore the value to "oldval" and return NULL.
2456 * Must always be called in pairs to save and restore v:throwpoint! Does not
2457 * take care of memory allocations.
2458 */
2459 char_u *
2460v_throwpoint(char_u *oldval)
2461{
2462 if (oldval == NULL)
2463 return vimvars[VV_THROWPOINT].vv_str;
2464
2465 vimvars[VV_THROWPOINT].vv_str = oldval;
2466 return NULL;
2467}
2468
2469/*
2470 * Set v:cmdarg.
2471 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2472 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2473 * Must always be called in pairs!
2474 */
2475 char_u *
2476set_cmdarg(exarg_T *eap, char_u *oldarg)
2477{
2478 char_u *oldval;
2479 char_u *newval;
2480 unsigned len;
2481
2482 oldval = vimvars[VV_CMDARG].vv_str;
2483 if (eap == NULL)
2484 {
2485 vim_free(oldval);
2486 vimvars[VV_CMDARG].vv_str = oldarg;
2487 return NULL;
2488 }
2489
2490 if (eap->force_bin == FORCE_BIN)
2491 len = 6;
2492 else if (eap->force_bin == FORCE_NOBIN)
2493 len = 8;
2494 else
2495 len = 0;
2496
2497 if (eap->read_edit)
2498 len += 7;
2499
2500 if (eap->force_ff != 0)
2501 len += 10; // " ++ff=unix"
2502 if (eap->force_enc != 0)
2503 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2504 if (eap->bad_char != 0)
2505 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2506
2507 newval = alloc(len + 1);
2508 if (newval == NULL)
2509 return NULL;
2510
2511 if (eap->force_bin == FORCE_BIN)
2512 sprintf((char *)newval, " ++bin");
2513 else if (eap->force_bin == FORCE_NOBIN)
2514 sprintf((char *)newval, " ++nobin");
2515 else
2516 *newval = NUL;
2517
2518 if (eap->read_edit)
2519 STRCAT(newval, " ++edit");
2520
2521 if (eap->force_ff != 0)
2522 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2523 eap->force_ff == 'u' ? "unix"
2524 : eap->force_ff == 'd' ? "dos"
2525 : "mac");
2526 if (eap->force_enc != 0)
2527 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2528 eap->cmd + eap->force_enc);
2529 if (eap->bad_char == BAD_KEEP)
2530 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2531 else if (eap->bad_char == BAD_DROP)
2532 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2533 else if (eap->bad_char != 0)
2534 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2535 vimvars[VV_CMDARG].vv_str = newval;
2536 return oldval;
2537}
2538
2539/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002540 * Get the value of internal variable "name".
2541 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2542 */
2543 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002544eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002545 char_u *name,
2546 int len, // length of "name"
2547 typval_T *rettv, // NULL when only checking existence
2548 dictitem_T **dip, // non-NULL when typval's dict item is needed
2549 int verbose, // may give error message
2550 int no_autoload) // do not use script autoloading
2551{
2552 int ret = OK;
2553 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002554 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002555 dictitem_T *v;
2556 int cc;
2557
2558 // truncate the name, so that we can use strcmp()
2559 cc = name[len];
2560 name[len] = NUL;
2561
2562 // Check for user-defined variables.
2563 v = find_var(name, NULL, no_autoload);
2564 if (v != NULL)
2565 {
2566 tv = &v->di_tv;
2567 if (dip != NULL)
2568 *dip = v;
2569 }
2570
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002571 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002573 imported_T *import;
2574 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2575
2576 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577
2578 // imported variable from another script
2579 if (import != NULL)
2580 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002581 if (import->imp_funcname != NULL)
2582 {
2583 foundFunc = TRUE;
2584 if (rettv != NULL)
2585 {
2586 rettv->v_type = VAR_FUNC;
2587 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2588 }
2589 }
Bram Moolenaara6294952020-12-27 13:39:50 +01002590 else if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002591 {
2592 emsg("Sorry, 'import * as X' not implemented yet");
2593 ret = FAIL;
2594 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002595 else
2596 {
2597 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002598 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002600 tv = sv->sv_tv;
2601 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002603 else if (in_vim9script())
2604 {
2605 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2606
2607 if (ufunc != NULL)
2608 {
2609 foundFunc = TRUE;
2610 if (rettv != NULL)
2611 {
2612 rettv->v_type = VAR_FUNC;
2613 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2614 }
2615 }
2616 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002617 }
2618
Bram Moolenaarc620c052020-07-08 15:16:19 +02002619 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002620 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002621 if (tv == NULL)
2622 {
2623 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002624 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002625 ret = FAIL;
2626 }
2627 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002628 {
2629 // If a list or dict variable wasn't initialized, do it now.
2630 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2631 {
2632 tv->vval.v_dict = dict_alloc();
2633 if (tv->vval.v_dict != NULL)
2634 ++tv->vval.v_dict->dv_refcount;
2635 }
2636 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2637 {
2638 tv->vval.v_list = list_alloc();
2639 if (tv->vval.v_list != NULL)
2640 ++tv->vval.v_list->lv_refcount;
2641 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002642 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002643 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002644 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002645
2646 name[len] = cc;
2647
2648 return ret;
2649}
2650
2651/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002652 * Check if variable "name[len]" is a local variable or an argument.
2653 * If so, "*eval_lavars_used" is set to TRUE.
2654 */
2655 void
2656check_vars(char_u *name, int len)
2657{
2658 int cc;
2659 char_u *varname;
2660 hashtab_T *ht;
2661
2662 if (eval_lavars_used == NULL)
2663 return;
2664
2665 // truncate the name, so that we can use strcmp()
2666 cc = name[len];
2667 name[len] = NUL;
2668
2669 ht = find_var_ht(name, &varname);
2670 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2671 {
2672 if (find_var(name, NULL, TRUE) != NULL)
2673 *eval_lavars_used = TRUE;
2674 }
2675
2676 name[len] = cc;
2677}
2678
2679/*
2680 * Find variable "name" in the list of variables.
2681 * Return a pointer to it if found, NULL if not found.
2682 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002683 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002684 */
2685 dictitem_T *
2686find_var(char_u *name, hashtab_T **htp, int no_autoload)
2687{
2688 char_u *varname;
2689 hashtab_T *ht;
2690 dictitem_T *ret = NULL;
2691
2692 ht = find_var_ht(name, &varname);
2693 if (htp != NULL)
2694 *htp = ht;
2695 if (ht == NULL)
2696 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002697 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002698 if (ret != NULL)
2699 return ret;
2700
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002701 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002702 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002703 if (ret != NULL)
2704 return ret;
2705
2706 // in Vim9 script items without a scope can be script-local
2707 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2708 {
2709 ht = get_script_local_ht();
2710 if (ht != NULL)
2711 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002712 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002713 if (ret != NULL)
2714 {
2715 if (htp != NULL)
2716 *htp = ht;
2717 return ret;
2718 }
2719 }
2720 }
2721
2722 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002723}
2724
2725/*
2726 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002727 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002728 * Returns NULL if not found.
2729 */
2730 dictitem_T *
2731find_var_in_ht(
2732 hashtab_T *ht,
2733 int htname,
2734 char_u *varname,
2735 int no_autoload)
2736{
2737 hashitem_T *hi;
2738
2739 if (*varname == NUL)
2740 {
2741 // Must be something like "s:", otherwise "ht" would be NULL.
2742 switch (htname)
2743 {
2744 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2745 case 'g': return &globvars_var;
2746 case 'v': return &vimvars_var;
2747 case 'b': return &curbuf->b_bufvar;
2748 case 'w': return &curwin->w_winvar;
2749 case 't': return &curtab->tp_winvar;
2750 case 'l': return get_funccal_local_var();
2751 case 'a': return get_funccal_args_var();
2752 }
2753 return NULL;
2754 }
2755
2756 hi = hash_find(ht, varname);
2757 if (HASHITEM_EMPTY(hi))
2758 {
2759 // For global variables we may try auto-loading the script. If it
2760 // worked find the variable again. Don't auto-load a script if it was
2761 // loaded already, otherwise it would be loaded every time when
2762 // checking if a function name is a Funcref variable.
2763 if (ht == &globvarht && !no_autoload)
2764 {
2765 // Note: script_autoload() may make "hi" invalid. It must either
2766 // be obtained again or not used.
2767 if (!script_autoload(varname, FALSE) || aborting())
2768 return NULL;
2769 hi = hash_find(ht, varname);
2770 }
2771 if (HASHITEM_EMPTY(hi))
2772 return NULL;
2773 }
2774 return HI2DI(hi);
2775}
2776
2777/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 * Get the script-local hashtab. NULL if not in a script context.
2779 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002780 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781get_script_local_ht(void)
2782{
2783 scid_T sid = current_sctx.sc_sid;
2784
Bram Moolenaare3d46852020-08-29 13:39:17 +02002785 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002786 return &SCRIPT_VARS(sid);
2787 return NULL;
2788}
2789
2790/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002791 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002792 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01002794 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002795lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01002796 char_u *name,
2797 size_t len,
Bram Moolenaar709664c2020-12-12 14:33:41 +01002798 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799{
2800 hashtab_T *ht = get_script_local_ht();
2801 char_u buffer[30];
2802 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01002803 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002805 int is_global = FALSE;
2806 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807
2808 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002809 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810 if (len < sizeof(buffer) - 1)
2811 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002812 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 vim_strncpy(buffer, name, len);
2814 p = buffer;
2815 }
2816 else
2817 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002818 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002820 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 }
2822
2823 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002824 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825
2826 // if not script-local, then perhaps imported
Bram Moolenaar709664c2020-12-12 14:33:41 +01002827 if (res == FAIL && find_imported(p, 0, NULL) != NULL)
2828 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 if (p != buffer)
2830 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002831
2832 if (res != OK)
2833 {
2834 // Find a function, so that a following "->" works. Skip "g:" before a
2835 // function name.
2836 // Do not check for an internal function, since it might also be a
2837 // valid command, such as ":split" versuse "split()".
2838 if (name[0] == 'g' && name[1] == ':')
2839 {
2840 is_global = TRUE;
2841 fname = name + 2;
2842 }
2843 if (find_func(fname, is_global, NULL) != NULL)
2844 res = OK;
2845 }
2846
Bram Moolenaar709664c2020-12-12 14:33:41 +01002847 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848}
2849
2850/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002851 * Find the hashtab used for a variable name.
2852 * Return NULL if the name is not valid.
2853 * Set "varname" to the start of name without ':'.
2854 */
2855 hashtab_T *
2856find_var_ht(char_u *name, char_u **varname)
2857{
2858 hashitem_T *hi;
2859 hashtab_T *ht;
2860
2861 if (name[0] == NUL)
2862 return NULL;
2863 if (name[1] != ':')
2864 {
2865 // The name must not start with a colon or #.
2866 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2867 return NULL;
2868 *varname = name;
2869
2870 // "version" is "v:version" in all scopes if scriptversion < 3.
2871 // Same for a few other variables marked with VV_COMPAT.
2872 if (current_sctx.sc_version < 3)
2873 {
2874 hi = hash_find(&compat_hashtab, name);
2875 if (!HASHITEM_EMPTY(hi))
2876 return &compat_hashtab;
2877 }
2878
2879 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002880 if (ht != NULL)
2881 return ht; // local variable
2882
2883 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002884 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 {
2886 ht = get_script_local_ht();
2887 if (ht != NULL)
2888 return ht;
2889 }
2890
2891 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002892 }
2893 *varname = name + 2;
2894 if (*name == 'g') // global variable
2895 return &globvarht;
2896 // There must be no ':' or '#' in the rest of the name, unless g: is used
2897 if (vim_strchr(name + 2, ':') != NULL
2898 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2899 return NULL;
2900 if (*name == 'b') // buffer variable
2901 return &curbuf->b_vars->dv_hashtab;
2902 if (*name == 'w') // window variable
2903 return &curwin->w_vars->dv_hashtab;
2904 if (*name == 't') // tab page variable
2905 return &curtab->tp_vars->dv_hashtab;
2906 if (*name == 'v') // v: variable
2907 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002908 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002909 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002911 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 if (*name == 'a') // a: function argument
2913 return get_funccal_args_ht();
2914 if (*name == 'l') // l: local function variable
2915 return get_funccal_local_ht();
2916 }
2917 if (*name == 's') // script variable
2918 {
2919 ht = get_script_local_ht();
2920 if (ht != NULL)
2921 return ht;
2922 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002923 return NULL;
2924}
2925
2926/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002927 * Get the string value of a (global/local) variable.
2928 * Note: see tv_get_string() for how long the pointer remains valid.
2929 * Returns NULL when it doesn't exist.
2930 */
2931 char_u *
2932get_var_value(char_u *name)
2933{
2934 dictitem_T *v;
2935
2936 v = find_var(name, NULL, FALSE);
2937 if (v == NULL)
2938 return NULL;
2939 return tv_get_string(&v->di_tv);
2940}
2941
2942/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002943 * Allocate a new hashtab for a sourced script. It will be used while
2944 * sourcing this script and when executing functions defined in the script.
2945 */
2946 void
2947new_script_vars(scid_T id)
2948{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002949 scriptvar_T *sv;
2950
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002951 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2952 if (sv == NULL)
2953 return;
2954 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002955 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002956}
2957
2958/*
2959 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2960 * point to it.
2961 */
2962 void
2963init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2964{
2965 hash_init(&dict->dv_hashtab);
2966 dict->dv_lock = 0;
2967 dict->dv_scope = scope;
2968 dict->dv_refcount = DO_NOT_FREE_CNT;
2969 dict->dv_copyID = 0;
2970 dict_var->di_tv.vval.v_dict = dict;
2971 dict_var->di_tv.v_type = VAR_DICT;
2972 dict_var->di_tv.v_lock = VAR_FIXED;
2973 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2974 dict_var->di_key[0] = NUL;
2975}
2976
2977/*
2978 * Unreference a dictionary initialized by init_var_dict().
2979 */
2980 void
2981unref_var_dict(dict_T *dict)
2982{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002983 // Now the dict needs to be freed if no one else is using it, go back to
2984 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002985 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2986 dict_unref(dict);
2987}
2988
2989/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002990 * Clean up a list of internal variables.
2991 * Frees all allocated variables and the value they contain.
2992 * Clears hashtab "ht", does not free it.
2993 */
2994 void
2995vars_clear(hashtab_T *ht)
2996{
2997 vars_clear_ext(ht, TRUE);
2998}
2999
3000/*
3001 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3002 */
3003 void
3004vars_clear_ext(hashtab_T *ht, int free_val)
3005{
3006 int todo;
3007 hashitem_T *hi;
3008 dictitem_T *v;
3009
3010 hash_lock(ht);
3011 todo = (int)ht->ht_used;
3012 for (hi = ht->ht_array; todo > 0; ++hi)
3013 {
3014 if (!HASHITEM_EMPTY(hi))
3015 {
3016 --todo;
3017
3018 // Free the variable. Don't remove it from the hashtab,
3019 // ht_array might change then. hash_clear() takes care of it
3020 // later.
3021 v = HI2DI(hi);
3022 if (free_val)
3023 clear_tv(&v->di_tv);
3024 if (v->di_flags & DI_FLAGS_ALLOC)
3025 vim_free(v);
3026 }
3027 }
3028 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003029 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003030}
3031
3032/*
3033 * Delete a variable from hashtab "ht" at item "hi".
3034 * Clear the variable value and free the dictitem.
3035 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003036 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003037delete_var(hashtab_T *ht, hashitem_T *hi)
3038{
3039 dictitem_T *di = HI2DI(hi);
3040
3041 hash_remove(ht, hi);
3042 clear_tv(&di->di_tv);
3043 vim_free(di);
3044}
3045
3046/*
3047 * List the value of one internal variable.
3048 */
3049 static void
3050list_one_var(dictitem_T *v, char *prefix, int *first)
3051{
3052 char_u *tofree;
3053 char_u *s;
3054 char_u numbuf[NUMBUFLEN];
3055
3056 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3057 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3058 s == NULL ? (char_u *)"" : s, first);
3059 vim_free(tofree);
3060}
3061
3062 static void
3063list_one_var_a(
3064 char *prefix,
3065 char_u *name,
3066 int type,
3067 char_u *string,
3068 int *first) // when TRUE clear rest of screen and set to FALSE
3069{
3070 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3071 msg_start();
3072 msg_puts(prefix);
3073 if (name != NULL) // "a:" vars don't have a name stored
3074 msg_puts((char *)name);
3075 msg_putchar(' ');
3076 msg_advance(22);
3077 if (type == VAR_NUMBER)
3078 msg_putchar('#');
3079 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3080 msg_putchar('*');
3081 else if (type == VAR_LIST)
3082 {
3083 msg_putchar('[');
3084 if (*string == '[')
3085 ++string;
3086 }
3087 else if (type == VAR_DICT)
3088 {
3089 msg_putchar('{');
3090 if (*string == '{')
3091 ++string;
3092 }
3093 else
3094 msg_putchar(' ');
3095
3096 msg_outtrans(string);
3097
3098 if (type == VAR_FUNC || type == VAR_PARTIAL)
3099 msg_puts("()");
3100 if (*first)
3101 {
3102 msg_clr_eos();
3103 *first = FALSE;
3104 }
3105}
3106
3107/*
3108 * Set variable "name" to value in "tv".
3109 * If the variable already exists, the value is updated.
3110 * Otherwise the variable is created.
3111 */
3112 void
3113set_var(
3114 char_u *name,
3115 typval_T *tv,
3116 int copy) // make copy of value in "tv"
3117{
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003118 set_var_const(name, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003119}
3120
3121/*
3122 * Set variable "name" to value in "tv".
3123 * If the variable already exists and "is_const" is FALSE the value is updated.
3124 * Otherwise the variable is created.
3125 */
3126 void
3127set_var_const(
3128 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003130 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003131 int copy, // make copy of value in "tv"
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003132 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
3133 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003134{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003135 typval_T *tv = tv_arg;
3136 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003138 char_u *varname;
3139 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003141 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003142
3143 ht = find_var_ht(name, &varname);
3144 if (ht == NULL || *varname == NUL)
3145 {
3146 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003147 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003148 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003149 is_script_local = ht == get_script_local_ht();
3150
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003151 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003152 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003153 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003154 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003155 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003156 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003157 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003158 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003159 }
3160
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003162
3163 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 if (di == NULL)
3165 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003166
3167 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003168 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003169 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003170
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003171 if (need_convert_to_bool(type, tv))
3172 {
3173 // Destination is a bool and the value is not, but it can be converted.
3174 CLEAR_FIELD(bool_tv);
3175 bool_tv.v_type = VAR_BOOL;
3176 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3177 tv = &bool_tv;
3178 }
3179
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003181 {
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01003182 // Item already exists. Allowed to replace when reloading.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003184 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003185 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186 {
3187 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003188 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 }
3190
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003191 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003193 where_T where;
3194
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003195 if ((flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003196 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003197 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003198 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003199 }
3200
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003201 // check the type and adjust to bool if needed
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003202 where.wt_index = var_idx;
3203 where.wt_variable = TRUE;
3204 if (check_script_var_type(&di->di_tv, tv, name, where) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003205 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003207
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003208 if (var_check_permission(di, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003209 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003210 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 else
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003212 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 // can only redefine once
3214 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003215
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003216 // A Vim9 script-local variable is also present in sn_all_vars and
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003217 // sn_var_vals. It may set "type" from "tv".
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003218 if (is_script_local && vim9script)
Bram Moolenaar08251752021-01-11 21:20:18 +01003219 update_vim9_script_var(FALSE, di, flags, tv, &type);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003220 }
3221
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003222 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003223
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003225 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003226 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003227 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003229 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003231 if (copy || tv->v_type != VAR_STRING)
3232 {
3233 char_u *val = tv_get_string(tv);
3234
3235 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003236 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 if (di->di_tv.vval.v_string == NULL)
3238 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003239 }
3240 else
3241 {
3242 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003244 tv->vval.v_string = NULL;
3245 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003246 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003247 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003249 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003251 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003253#ifdef FEAT_SEARCH_EXTRA
3254 else if (STRCMP(varname, "hlsearch") == 0)
3255 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003257 redraw_all_later(SOME_VALID);
3258 }
3259#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003260 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003261 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003263 {
3264 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003265 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003266 }
3267 }
3268
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003270 }
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003271 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003272 {
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01003273 // Item not found, check if a function already exists.
3274 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
3275 && lookup_scriptitem(name, STRLEN(name), NULL) == OK)
3276 {
3277 semsg(_(e_redefining_script_item_str), name);
3278 goto failed;
3279 }
3280
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003281 // add a new variable
3282 if (vim9script && is_script_local && (flags & ASSIGN_NO_DECL))
3283 {
3284 semsg(_(e_unknown_variable_str), name);
3285 goto failed;
3286 }
3287
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003288 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003289 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003290 {
3291 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003292 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003293 }
3294
Bram Moolenaar03290b82020-12-19 16:30:44 +01003295 // Make sure the variable name is valid. In Vim9 script an autoload
3296 // variable must be prefixed with "g:".
3297 if (!valid_varname(varname, !vim9script
3298 || STRNCMP(name, "g:", 2) == 0))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003299 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003300
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3302 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003303 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304 STRCPY(di->di_key, varname);
3305 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003306 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003308 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003309 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003311 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 di->di_flags |= DI_FLAGS_LOCK;
3313
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003314 // A Vim9 script-local variable is also added to sn_all_vars and
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003315 // sn_var_vals. It may set "type" from "tv".
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003316 if (is_script_local && vim9script)
Bram Moolenaar08251752021-01-11 21:20:18 +01003317 update_vim9_script_var(TRUE, di, flags, tv, &type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003318 }
3319
3320 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003322 else
3323 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324 di->di_tv = *tv;
3325 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003326 init_tv(tv);
3327 }
3328
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003329 if (vim9script && type != NULL)
3330 {
3331 if (type->tt_type == VAR_DICT && di->di_tv.vval.v_dict != NULL)
3332 di->di_tv.vval.v_dict->dv_type = alloc_type(type);
3333 else if (type->tt_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
3334 di->di_tv.vval.v_list->lv_type = alloc_type(type);
3335 }
3336
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003337 // ":const var = value" locks the value
3338 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003339 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003340 // Like :lockvar! name: lock the value and what it contains, but only
3341 // if the reference count is up to one. That locks only literal
3342 // values.
3343 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003344 return;
3345failed:
3346 if (!copy)
3347 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003348}
3349
3350/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003351 * Check in this order for backwards compatibility:
3352 * - Whether the variable is read-only
3353 * - Whether the variable value is locked
3354 * - Whether the variable is locked
3355 */
3356 int
3357var_check_permission(dictitem_T *di, char_u *name)
3358{
3359 if (var_check_ro(di->di_flags, name, FALSE)
3360 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3361 || var_check_lock(di->di_flags, name, FALSE))
3362 return FAIL;
3363 return OK;
3364}
3365
3366/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003367 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3368 * Also give an error message.
3369 */
3370 int
3371var_check_ro(int flags, char_u *name, int use_gettext)
3372{
3373 if (flags & DI_FLAGS_RO)
3374 {
3375 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3376 return TRUE;
3377 }
3378 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3379 {
3380 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3381 return TRUE;
3382 }
3383 return FALSE;
3384}
3385
3386/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003387 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3388 * Also give an error message.
3389 */
3390 int
3391var_check_lock(int flags, char_u *name, int use_gettext)
3392{
3393 if (flags & DI_FLAGS_LOCK)
3394 {
3395 semsg(_(e_variable_is_locked_str),
3396 use_gettext ? (char_u *)_(name) : name);
3397 return TRUE;
3398 }
3399 return FALSE;
3400}
3401
3402/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003403 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3404 * Also give an error message.
3405 */
3406 int
3407var_check_fixed(int flags, char_u *name, int use_gettext)
3408{
3409 if (flags & DI_FLAGS_FIX)
3410 {
3411 semsg(_("E795: Cannot delete variable %s"),
3412 use_gettext ? (char_u *)_(name) : name);
3413 return TRUE;
3414 }
3415 return FALSE;
3416}
3417
3418/*
3419 * Check if a funcref is assigned to a valid variable name.
3420 * Return TRUE and give an error if not.
3421 */
3422 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003423var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003424 char_u *name, // points to start of variable name
3425 int new_var) // TRUE when creating the variable
3426{
3427 // Allow for w: b: s: and t:.
3428 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3429 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3430 ? name[2] : name[0]))
3431 {
3432 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3433 name);
3434 return TRUE;
3435 }
3436 // Don't allow hiding a function. When "v" is not NULL we might be
3437 // assigning another function to the same var, the type is checked
3438 // below.
3439 if (new_var && function_exists(name, FALSE))
3440 {
3441 semsg(_("E705: Variable name conflicts with existing function: %s"),
3442 name);
3443 return TRUE;
3444 }
3445 return FALSE;
3446}
3447
3448/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003449 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3450 * value. Also give an error message, using "name" or _("name") when
3451 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003452 */
3453 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003454value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003455{
3456 if (lock & VAR_LOCKED)
3457 {
3458 semsg(_("E741: Value is locked: %s"),
3459 name == NULL ? (char_u *)_("Unknown")
3460 : use_gettext ? (char_u *)_(name)
3461 : name);
3462 return TRUE;
3463 }
3464 if (lock & VAR_FIXED)
3465 {
3466 semsg(_("E742: Cannot change value of %s"),
3467 name == NULL ? (char_u *)_("Unknown")
3468 : use_gettext ? (char_u *)_(name)
3469 : name);
3470 return TRUE;
3471 }
3472 return FALSE;
3473}
3474
3475/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003476 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003477 * Return FALSE and give an error if not.
3478 */
3479 int
Bram Moolenaar03290b82020-12-19 16:30:44 +01003480valid_varname(char_u *varname, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003481{
3482 char_u *p;
3483
3484 for (p = varname; *p != NUL; ++p)
3485 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003486 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003487 {
3488 semsg(_(e_illvar), varname);
3489 return FALSE;
3490 }
3491 return TRUE;
3492}
3493
3494/*
3495 * getwinvar() and gettabwinvar()
3496 */
3497 static void
3498getwinvar(
3499 typval_T *argvars,
3500 typval_T *rettv,
3501 int off) // 1 for gettabwinvar()
3502{
3503 win_T *win;
3504 char_u *varname;
3505 dictitem_T *v;
3506 tabpage_T *tp = NULL;
3507 int done = FALSE;
3508 win_T *oldcurwin;
3509 tabpage_T *oldtabpage;
3510 int need_switch_win;
3511
3512 if (off == 1)
3513 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3514 else
3515 tp = curtab;
3516 win = find_win_by_nr(&argvars[off], tp);
3517 varname = tv_get_string_chk(&argvars[off + 1]);
3518 ++emsg_off;
3519
3520 rettv->v_type = VAR_STRING;
3521 rettv->vval.v_string = NULL;
3522
3523 if (win != NULL && varname != NULL)
3524 {
3525 // Set curwin to be our win, temporarily. Also set the tabpage,
3526 // otherwise the window is not valid. Only do this when needed,
3527 // autocommands get blocked.
3528 need_switch_win = !(tp == curtab && win == curwin);
3529 if (!need_switch_win
3530 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3531 {
3532 if (*varname == '&')
3533 {
3534 if (varname[1] == NUL)
3535 {
3536 // get all window-local options in a dict
3537 dict_T *opts = get_winbuf_options(FALSE);
3538
3539 if (opts != NULL)
3540 {
3541 rettv_dict_set(rettv, opts);
3542 done = TRUE;
3543 }
3544 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003545 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003546 // window-local-option
3547 done = TRUE;
3548 }
3549 else
3550 {
3551 // Look up the variable.
3552 // Let getwinvar({nr}, "") return the "w:" dictionary.
3553 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3554 varname, FALSE);
3555 if (v != NULL)
3556 {
3557 copy_tv(&v->di_tv, rettv);
3558 done = TRUE;
3559 }
3560 }
3561 }
3562
3563 if (need_switch_win)
3564 // restore previous notion of curwin
3565 restore_win(oldcurwin, oldtabpage, TRUE);
3566 }
3567
3568 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3569 // use the default return value
3570 copy_tv(&argvars[off + 2], rettv);
3571
3572 --emsg_off;
3573}
3574
3575/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003576 * Set option "varname" to the value of "varp" for the current buffer/window.
3577 */
3578 static void
3579set_option_from_tv(char_u *varname, typval_T *varp)
3580{
3581 long numval = 0;
3582 char_u *strval;
3583 char_u nbuf[NUMBUFLEN];
3584 int error = FALSE;
3585
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003586 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003587 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003588 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003589 strval = (char_u *)"0"; // avoid using "false"
3590 }
3591 else
3592 {
3593 if (!in_vim9script() || varp->v_type != VAR_STRING)
3594 numval = (long)tv_get_number_chk(varp, &error);
3595 strval = tv_get_string_buf_chk(varp, nbuf);
3596 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02003597 if (!error && strval != NULL)
3598 set_option_value(varname, numval, strval, OPT_LOCAL);
3599}
3600
3601/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003602 * "setwinvar()" and "settabwinvar()" functions
3603 */
3604 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003605setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003606{
3607 win_T *win;
3608 win_T *save_curwin;
3609 tabpage_T *save_curtab;
3610 int need_switch_win;
3611 char_u *varname, *winvarname;
3612 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003613 tabpage_T *tp = NULL;
3614
3615 if (check_secure())
3616 return;
3617
3618 if (off == 1)
3619 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3620 else
3621 tp = curtab;
3622 win = find_win_by_nr(&argvars[off], tp);
3623 varname = tv_get_string_chk(&argvars[off + 1]);
3624 varp = &argvars[off + 2];
3625
3626 if (win != NULL && varname != NULL && varp != NULL)
3627 {
3628 need_switch_win = !(tp == curtab && win == curwin);
3629 if (!need_switch_win
3630 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3631 {
3632 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003633 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003634 else
3635 {
3636 winvarname = alloc(STRLEN(varname) + 3);
3637 if (winvarname != NULL)
3638 {
3639 STRCPY(winvarname, "w:");
3640 STRCPY(winvarname + 2, varname);
3641 set_var(winvarname, varp, TRUE);
3642 vim_free(winvarname);
3643 }
3644 }
3645 }
3646 if (need_switch_win)
3647 restore_win(save_curwin, save_curtab, TRUE);
3648 }
3649}
3650
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003651/*
3652 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3653 * v:option_type, and v:option_command.
3654 */
3655 void
3656reset_v_option_vars(void)
3657{
3658 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3659 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3660 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3661 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3662 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3663 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3664}
3665
3666/*
3667 * Add an assert error to v:errors.
3668 */
3669 void
3670assert_error(garray_T *gap)
3671{
3672 struct vimvar *vp = &vimvars[VV_ERRORS];
3673
3674 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003675 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003676 set_vim_var_list(VV_ERRORS, list_alloc());
3677 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3678}
3679
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003680 int
3681var_exists(char_u *var)
3682{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003683 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003684 char_u *name;
3685 char_u *tofree;
3686 typval_T tv;
3687 int len = 0;
3688 int n = FALSE;
3689
3690 // get_name_len() takes care of expanding curly braces
3691 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003692 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003693 if (len > 0)
3694 {
3695 if (tofree != NULL)
3696 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003697 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003698 if (n)
3699 {
3700 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003701 arg = skipwhite(arg);
3702 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003703 if (n)
3704 clear_tv(&tv);
3705 }
3706 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003707 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003708 n = FALSE;
3709
3710 vim_free(tofree);
3711 return n;
3712}
3713
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003714static lval_T *redir_lval = NULL;
3715#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3716static garray_T redir_ga; // only valid when redir_lval is not NULL
3717static char_u *redir_endp = NULL;
3718static char_u *redir_varname = NULL;
3719
3720/*
3721 * Start recording command output to a variable
3722 * When "append" is TRUE append to an existing variable.
3723 * Returns OK if successfully completed the setup. FAIL otherwise.
3724 */
3725 int
3726var_redir_start(char_u *name, int append)
3727{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003728 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003729 typval_T tv;
3730
3731 // Catch a bad name early.
3732 if (!eval_isnamec1(*name))
3733 {
3734 emsg(_(e_invarg));
3735 return FAIL;
3736 }
3737
3738 // Make a copy of the name, it is used in redir_lval until redir ends.
3739 redir_varname = vim_strsave(name);
3740 if (redir_varname == NULL)
3741 return FAIL;
3742
3743 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3744 if (redir_lval == NULL)
3745 {
3746 var_redir_stop();
3747 return FAIL;
3748 }
3749
3750 // The output is stored in growarray "redir_ga" until redirection ends.
3751 ga_init2(&redir_ga, (int)sizeof(char), 500);
3752
3753 // Parse the variable name (can be a dict or list entry).
3754 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3755 FNE_CHECK_START);
3756 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3757 {
3758 clear_lval(redir_lval);
3759 if (redir_endp != NULL && *redir_endp != NUL)
3760 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003761 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003762 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003763 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003764 redir_endp = NULL; // don't store a value, only cleanup
3765 var_redir_stop();
3766 return FAIL;
3767 }
3768
3769 // check if we can write to the variable: set it to or append an empty
3770 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003771 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003772 tv.v_type = VAR_STRING;
3773 tv.vval.v_string = (char_u *)"";
3774 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003775 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003776 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003777 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003778 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003779 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003780 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003781 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003782 {
3783 redir_endp = NULL; // don't store a value, only cleanup
3784 var_redir_stop();
3785 return FAIL;
3786 }
3787
3788 return OK;
3789}
3790
3791/*
3792 * Append "value[value_len]" to the variable set by var_redir_start().
3793 * The actual appending is postponed until redirection ends, because the value
3794 * appended may in fact be the string we write to, changing it may cause freed
3795 * memory to be used:
3796 * :redir => foo
3797 * :let foo
3798 * :redir END
3799 */
3800 void
3801var_redir_str(char_u *value, int value_len)
3802{
3803 int len;
3804
3805 if (redir_lval == NULL)
3806 return;
3807
3808 if (value_len == -1)
3809 len = (int)STRLEN(value); // Append the entire string
3810 else
3811 len = value_len; // Append only "value_len" characters
3812
3813 if (ga_grow(&redir_ga, len) == OK)
3814 {
3815 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3816 redir_ga.ga_len += len;
3817 }
3818 else
3819 var_redir_stop();
3820}
3821
3822/*
3823 * Stop redirecting command output to a variable.
3824 * Frees the allocated memory.
3825 */
3826 void
3827var_redir_stop(void)
3828{
3829 typval_T tv;
3830
3831 if (EVALCMD_BUSY)
3832 {
3833 redir_lval = NULL;
3834 return;
3835 }
3836
3837 if (redir_lval != NULL)
3838 {
3839 // If there was no error: assign the text to the variable.
3840 if (redir_endp != NULL)
3841 {
3842 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3843 tv.v_type = VAR_STRING;
3844 tv.vval.v_string = redir_ga.ga_data;
3845 // Call get_lval() again, if it's inside a Dict or List it may
3846 // have changed.
3847 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3848 FALSE, FALSE, 0, FNE_CHECK_START);
3849 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003851 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003852 clear_lval(redir_lval);
3853 }
3854
3855 // free the collected output
3856 VIM_CLEAR(redir_ga.ga_data);
3857
3858 VIM_CLEAR(redir_lval);
3859 }
3860 VIM_CLEAR(redir_varname);
3861}
3862
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003863/*
3864 * "gettabvar()" function
3865 */
3866 void
3867f_gettabvar(typval_T *argvars, typval_T *rettv)
3868{
3869 win_T *oldcurwin;
3870 tabpage_T *tp, *oldtabpage;
3871 dictitem_T *v;
3872 char_u *varname;
3873 int done = FALSE;
3874
3875 rettv->v_type = VAR_STRING;
3876 rettv->vval.v_string = NULL;
3877
3878 varname = tv_get_string_chk(&argvars[1]);
3879 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3880 if (tp != NULL && varname != NULL)
3881 {
3882 // Set tp to be our tabpage, temporarily. Also set the window to the
3883 // first window in the tabpage, otherwise the window is not valid.
3884 if (switch_win(&oldcurwin, &oldtabpage,
3885 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3886 : tp->tp_firstwin, tp, TRUE) == OK)
3887 {
3888 // look up the variable
3889 // Let gettabvar({nr}, "") return the "t:" dictionary.
3890 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3891 if (v != NULL)
3892 {
3893 copy_tv(&v->di_tv, rettv);
3894 done = TRUE;
3895 }
3896 }
3897
3898 // restore previous notion of curwin
3899 restore_win(oldcurwin, oldtabpage, TRUE);
3900 }
3901
3902 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3903 copy_tv(&argvars[2], rettv);
3904}
3905
3906/*
3907 * "gettabwinvar()" function
3908 */
3909 void
3910f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3911{
3912 getwinvar(argvars, rettv, 1);
3913}
3914
3915/*
3916 * "getwinvar()" function
3917 */
3918 void
3919f_getwinvar(typval_T *argvars, typval_T *rettv)
3920{
3921 getwinvar(argvars, rettv, 0);
3922}
3923
3924/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003925 * "getbufvar()" function
3926 */
3927 void
3928f_getbufvar(typval_T *argvars, typval_T *rettv)
3929{
3930 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003931 char_u *varname;
3932 dictitem_T *v;
3933 int done = FALSE;
3934
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003935 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003936 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003937
3938 rettv->v_type = VAR_STRING;
3939 rettv->vval.v_string = NULL;
3940
3941 if (buf != NULL && varname != NULL)
3942 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003943 if (*varname == '&')
3944 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003945 buf_T *save_curbuf = curbuf;
3946
3947 // set curbuf to be our buf, temporarily
3948 curbuf = buf;
3949
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003950 if (varname[1] == NUL)
3951 {
3952 // get all buffer-local options in a dict
3953 dict_T *opts = get_winbuf_options(TRUE);
3954
3955 if (opts != NULL)
3956 {
3957 rettv_dict_set(rettv, opts);
3958 done = TRUE;
3959 }
3960 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003961 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003962 // buffer-local-option
3963 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003964
3965 // restore previous notion of curbuf
3966 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003967 }
3968 else
3969 {
3970 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003971 if (*varname == NUL)
3972 // Let getbufvar({nr}, "") return the "b:" dictionary.
3973 v = &buf->b_bufvar;
3974 else
3975 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3976 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003977 if (v != NULL)
3978 {
3979 copy_tv(&v->di_tv, rettv);
3980 done = TRUE;
3981 }
3982 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003983 }
3984
3985 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3986 // use the default value
3987 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003988}
3989
3990/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003991 * "settabvar()" function
3992 */
3993 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003994f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003995{
3996 tabpage_T *save_curtab;
3997 tabpage_T *tp;
3998 char_u *varname, *tabvarname;
3999 typval_T *varp;
4000
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004001 if (check_secure())
4002 return;
4003
4004 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4005 varname = tv_get_string_chk(&argvars[1]);
4006 varp = &argvars[2];
4007
4008 if (varname != NULL && varp != NULL && tp != NULL)
4009 {
4010 save_curtab = curtab;
4011 goto_tabpage_tp(tp, FALSE, FALSE);
4012
4013 tabvarname = alloc(STRLEN(varname) + 3);
4014 if (tabvarname != NULL)
4015 {
4016 STRCPY(tabvarname, "t:");
4017 STRCPY(tabvarname + 2, varname);
4018 set_var(tabvarname, varp, TRUE);
4019 vim_free(tabvarname);
4020 }
4021
4022 // Restore current tabpage
4023 if (valid_tabpage(save_curtab))
4024 goto_tabpage_tp(save_curtab, FALSE, FALSE);
4025 }
4026}
4027
4028/*
4029 * "settabwinvar()" function
4030 */
4031 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004032f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004033{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004034 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004035}
4036
4037/*
4038 * "setwinvar()" function
4039 */
4040 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004041f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004042{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004043 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004044}
4045
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004046/*
4047 * "setbufvar()" function
4048 */
4049 void
4050f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4051{
4052 buf_T *buf;
4053 char_u *varname, *bufvarname;
4054 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004055
4056 if (check_secure())
4057 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004058 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004059 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004060 varp = &argvars[2];
4061
4062 if (buf != NULL && varname != NULL && varp != NULL)
4063 {
4064 if (*varname == '&')
4065 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004066 aco_save_T aco;
4067
4068 // set curbuf to be our buf, temporarily
4069 aucmd_prepbuf(&aco, buf);
4070
Bram Moolenaar191929b2020-08-19 21:20:49 +02004071 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004072
4073 // reset notion of buffer
4074 aucmd_restbuf(&aco);
4075 }
4076 else
4077 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004078 bufvarname = alloc(STRLEN(varname) + 3);
4079 if (bufvarname != NULL)
4080 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004081 buf_T *save_curbuf = curbuf;
4082
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004083 curbuf = buf;
4084 STRCPY(bufvarname, "b:");
4085 STRCPY(bufvarname + 2, varname);
4086 set_var(bufvarname, varp, TRUE);
4087 vim_free(bufvarname);
4088 curbuf = save_curbuf;
4089 }
4090 }
4091 }
4092}
4093
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004094/*
4095 * Get a callback from "arg". It can be a Funcref or a function name.
4096 * When "arg" is zero return an empty string.
4097 * "cb_name" is not allocated.
4098 * "cb_name" is set to NULL for an invalid argument.
4099 */
4100 callback_T
4101get_callback(typval_T *arg)
4102{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004103 callback_T res;
4104 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004105
4106 res.cb_free_name = FALSE;
4107 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4108 {
4109 res.cb_partial = arg->vval.v_partial;
4110 ++res.cb_partial->pt_refcount;
4111 res.cb_name = partial_name(res.cb_partial);
4112 }
4113 else
4114 {
4115 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004116 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4117 && isdigit(*arg->vval.v_string))
4118 r = FAIL;
4119 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004120 {
4121 // Note that we don't make a copy of the string.
4122 res.cb_name = arg->vval.v_string;
4123 func_ref(res.cb_name);
4124 }
4125 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004126 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004127 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004128 r = FAIL;
4129
4130 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004131 {
4132 emsg(_("E921: Invalid callback argument"));
4133 res.cb_name = NULL;
4134 }
4135 }
4136 return res;
4137}
4138
4139/*
4140 * Copy a callback into a typval_T.
4141 */
4142 void
4143put_callback(callback_T *cb, typval_T *tv)
4144{
4145 if (cb->cb_partial != NULL)
4146 {
4147 tv->v_type = VAR_PARTIAL;
4148 tv->vval.v_partial = cb->cb_partial;
4149 ++tv->vval.v_partial->pt_refcount;
4150 }
4151 else
4152 {
4153 tv->v_type = VAR_FUNC;
4154 tv->vval.v_string = vim_strsave(cb->cb_name);
4155 func_ref(cb->cb_name);
4156 }
4157}
4158
4159/*
4160 * Make a copy of "src" into "dest", allocating the function name if needed,
4161 * without incrementing the refcount.
4162 */
4163 void
4164set_callback(callback_T *dest, callback_T *src)
4165{
4166 if (src->cb_partial == NULL)
4167 {
4168 // just a function name, make a copy
4169 dest->cb_name = vim_strsave(src->cb_name);
4170 dest->cb_free_name = TRUE;
4171 }
4172 else
4173 {
4174 // cb_name is a pointer into cb_partial
4175 dest->cb_name = src->cb_name;
4176 dest->cb_free_name = FALSE;
4177 }
4178 dest->cb_partial = src->cb_partial;
4179}
4180
4181/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004182 * Copy callback from "src" to "dest", incrementing the refcounts.
4183 */
4184 void
4185copy_callback(callback_T *dest, callback_T *src)
4186{
4187 dest->cb_partial = src->cb_partial;
4188 if (dest->cb_partial != NULL)
4189 {
4190 dest->cb_name = src->cb_name;
4191 dest->cb_free_name = FALSE;
4192 ++dest->cb_partial->pt_refcount;
4193 }
4194 else
4195 {
4196 dest->cb_name = vim_strsave(src->cb_name);
4197 dest->cb_free_name = TRUE;
4198 func_ref(src->cb_name);
4199 }
4200}
4201
4202/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004203 * Unref/free "callback" returned by get_callback() or set_callback().
4204 */
4205 void
4206free_callback(callback_T *callback)
4207{
4208 if (callback->cb_partial != NULL)
4209 {
4210 partial_unref(callback->cb_partial);
4211 callback->cb_partial = NULL;
4212 }
4213 else if (callback->cb_name != NULL)
4214 func_unref(callback->cb_name);
4215 if (callback->cb_free_name)
4216 {
4217 vim_free(callback->cb_name);
4218 callback->cb_free_name = FALSE;
4219 }
4220 callback->cb_name = NULL;
4221}
4222
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004223#endif // FEAT_EVAL