blob: f9b3c486cbe87ae1500b48ae4823aa3b8cc75221 [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
48 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
49} vimvars[VV_LEN] =
50{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020051 // The order here must match the VV_ defines in vim.h!
52 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaare5cdf152019-08-29 22:09:46 +020053 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
54 {VV_NAME("count1", VAR_NUMBER), VV_RO},
55 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
56 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
57 {VV_NAME("warningmsg", VAR_STRING), 0},
58 {VV_NAME("statusmsg", VAR_STRING), 0},
59 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
60 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
61 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
62 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
63 {VV_NAME("termresponse", VAR_STRING), VV_RO},
64 {VV_NAME("fname", VAR_STRING), VV_RO},
65 {VV_NAME("lang", VAR_STRING), VV_RO},
66 {VV_NAME("lc_time", VAR_STRING), VV_RO},
67 {VV_NAME("ctype", VAR_STRING), VV_RO},
68 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
69 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
70 {VV_NAME("fname_in", VAR_STRING), VV_RO},
71 {VV_NAME("fname_out", VAR_STRING), VV_RO},
72 {VV_NAME("fname_new", VAR_STRING), VV_RO},
73 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
74 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
75 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
76 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
77 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
78 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
79 {VV_NAME("progname", VAR_STRING), VV_RO},
80 {VV_NAME("servername", VAR_STRING), VV_RO},
81 {VV_NAME("dying", VAR_NUMBER), VV_RO},
82 {VV_NAME("exception", VAR_STRING), VV_RO},
83 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
84 {VV_NAME("register", VAR_STRING), VV_RO},
85 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
86 {VV_NAME("insertmode", VAR_STRING), VV_RO},
87 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
88 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
89 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
90 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
91 {VV_NAME("fcs_choice", VAR_STRING), 0},
92 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
93 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
94 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
95 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
96 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
97 {VV_NAME("beval_text", VAR_STRING), VV_RO},
98 {VV_NAME("scrollstart", VAR_STRING), 0},
99 {VV_NAME("swapname", VAR_STRING), VV_RO},
100 {VV_NAME("swapchoice", VAR_STRING), 0},
101 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
102 {VV_NAME("char", VAR_STRING), 0},
103 {VV_NAME("mouse_win", VAR_NUMBER), 0},
104 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
105 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
106 {VV_NAME("mouse_col", VAR_NUMBER), 0},
107 {VV_NAME("operator", VAR_STRING), VV_RO},
108 {VV_NAME("searchforward", VAR_NUMBER), 0},
109 {VV_NAME("hlsearch", VAR_NUMBER), 0},
110 {VV_NAME("oldfiles", VAR_LIST), 0},
111 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
112 {VV_NAME("progpath", VAR_STRING), VV_RO},
113 {VV_NAME("completed_item", VAR_DICT), VV_RO},
114 {VV_NAME("option_new", VAR_STRING), VV_RO},
115 {VV_NAME("option_old", VAR_STRING), VV_RO},
116 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
117 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
118 {VV_NAME("option_command", VAR_STRING), VV_RO},
119 {VV_NAME("option_type", VAR_STRING), VV_RO},
120 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100121 {VV_NAME("false", VAR_BOOL), VV_RO},
122 {VV_NAME("true", VAR_BOOL), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200123 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100124 {VV_NAME("null", VAR_SPECIAL), VV_RO},
125 {VV_NAME("numbersize", VAR_NUMBER), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200126 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
127 {VV_NAME("testing", VAR_NUMBER), 0},
128 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
129 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
130 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
131 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
132 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
133 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
134 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
135 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
136 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
137 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
138 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
139 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
140 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
141 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
142 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
143 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
144 {VV_NAME("event", VAR_DICT), VV_RO},
145 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
146 {VV_NAME("echospace", VAR_NUMBER), VV_RO},
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100147 {VV_NAME("argv", VAR_LIST), VV_RO},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200148 {VV_NAME("collate", VAR_STRING), VV_RO},
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100149 {VV_NAME("exiting", VAR_SPECIAL), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200150};
151
152// shorthand
153#define vv_type vv_di.di_tv.v_type
154#define vv_nr vv_di.di_tv.vval.v_number
155#define vv_float vv_di.di_tv.vval.v_float
156#define vv_str vv_di.di_tv.vval.v_string
157#define vv_list vv_di.di_tv.vval.v_list
158#define vv_dict vv_di.di_tv.vval.v_dict
159#define vv_blob vv_di.di_tv.vval.v_blob
160#define vv_tv vv_di.di_tv
161
162static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200163static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200164#define vimvarht vimvardict.dv_hashtab
165
166// for VIM_VERSION_ defines
167#include "version.h"
168
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200169static void list_glob_vars(int *first);
170static void list_buf_vars(int *first);
171static void list_win_vars(int *first);
172static void list_tab_vars(int *first);
173static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100174static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200175static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
176static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200177static void list_one_var(dictitem_T *v, char *prefix, int *first);
178static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
179
180/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200181 * Initialize global and vim special variables
182 */
183 void
184evalvars_init(void)
185{
186 int i;
187 struct vimvar *p;
188
189 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
190 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
191 vimvardict.dv_lock = VAR_FIXED;
192 hash_init(&compat_hashtab);
193
194 for (i = 0; i < VV_LEN; ++i)
195 {
196 p = &vimvars[i];
197 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
198 {
199 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
200 getout(1);
201 }
202 STRCPY(p->vv_di.di_key, p->vv_name);
203 if (p->vv_flags & VV_RO)
204 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
205 else if (p->vv_flags & VV_RO_SBX)
206 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
207 else
208 p->vv_di.di_flags = DI_FLAGS_FIX;
209
210 // add to v: scope dict, unless the value is not always available
211 if (p->vv_type != VAR_UNKNOWN)
212 hash_add(&vimvarht, p->vv_di.di_key);
213 if (p->vv_flags & VV_COMPAT)
214 // add to compat scope dict
215 hash_add(&compat_hashtab, p->vv_di.di_key);
216 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200217 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
218 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200219
220 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
221 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100222 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200223 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
224 set_vim_var_list(VV_ERRORS, list_alloc());
225 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
226
227 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
228 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
229 set_vim_var_nr(VV_NONE, VVAL_NONE);
230 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100231 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200232
233 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
234 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
235 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
236 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
237 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
238 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
239 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
240 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
241 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
242 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
243 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
244
245 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
246
Bram Moolenaar439c0362020-06-06 15:58:03 +0200247 // Default for v:register is not 0 but '"'. This is adjusted once the
248 // clipboard has been setup by calling reset_reg_var().
249 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200250}
251
252#if defined(EXITFREE) || defined(PROTO)
253/*
254 * Free all vim variables information on exit
255 */
256 void
257evalvars_clear(void)
258{
259 int i;
260 struct vimvar *p;
261
262 for (i = 0; i < VV_LEN; ++i)
263 {
264 p = &vimvars[i];
265 if (p->vv_di.di_tv.v_type == VAR_STRING)
266 VIM_CLEAR(p->vv_str);
267 else if (p->vv_di.di_tv.v_type == VAR_LIST)
268 {
269 list_unref(p->vv_list);
270 p->vv_list = NULL;
271 }
272 }
273 hash_clear(&vimvarht);
274 hash_init(&vimvarht); // garbage_collect() will access it
275 hash_clear(&compat_hashtab);
276
277 // global variables
278 vars_clear(&globvarht);
279
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100280 // Script-local variables. Clear all the variables here.
281 // The scriptvar_T is cleared later in free_scriptnames(), because a
282 // variable in one script might hold a reference to the whole scope of
283 // another script.
284 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200285 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200286}
287#endif
288
289 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200290garbage_collect_globvars(int copyID)
291{
292 return set_ref_in_ht(&globvarht, copyID, NULL);
293}
294
295 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200296garbage_collect_vimvars(int copyID)
297{
298 return set_ref_in_ht(&vimvarht, copyID, NULL);
299}
300
301 int
302garbage_collect_scriptvars(int copyID)
303{
Bram Moolenaared234f22020-10-15 20:42:20 +0200304 int i;
305 int idx;
306 int abort = FALSE;
307 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200308
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100309 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200310 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200311 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
312
Bram Moolenaared234f22020-10-15 20:42:20 +0200313 si = SCRIPT_ITEM(i);
314 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
315 {
316 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
317
318 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
319 }
320 }
321
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200322 return abort;
323}
324
325/*
326 * Set an internal variable to a string value. Creates the variable if it does
327 * not already exist.
328 */
329 void
330set_internal_string_var(char_u *name, char_u *value)
331{
332 char_u *val;
333 typval_T *tvp;
334
335 val = vim_strsave(value);
336 if (val != NULL)
337 {
338 tvp = alloc_string_tv(val);
339 if (tvp != NULL)
340 {
341 set_var(name, tvp, FALSE);
342 free_tv(tvp);
343 }
344 }
345}
346
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200347 int
348eval_charconvert(
349 char_u *enc_from,
350 char_u *enc_to,
351 char_u *fname_from,
352 char_u *fname_to)
353{
354 int err = FALSE;
355
356 set_vim_var_string(VV_CC_FROM, enc_from, -1);
357 set_vim_var_string(VV_CC_TO, enc_to, -1);
358 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
359 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
360 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
361 err = TRUE;
362 set_vim_var_string(VV_CC_FROM, NULL, -1);
363 set_vim_var_string(VV_CC_TO, NULL, -1);
364 set_vim_var_string(VV_FNAME_IN, NULL, -1);
365 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
366
367 if (err)
368 return FAIL;
369 return OK;
370}
371
372# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
373 int
374eval_printexpr(char_u *fname, char_u *args)
375{
376 int err = FALSE;
377
378 set_vim_var_string(VV_FNAME_IN, fname, -1);
379 set_vim_var_string(VV_CMDARG, args, -1);
380 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
381 err = TRUE;
382 set_vim_var_string(VV_FNAME_IN, NULL, -1);
383 set_vim_var_string(VV_CMDARG, NULL, -1);
384
385 if (err)
386 {
387 mch_remove(fname);
388 return FAIL;
389 }
390 return OK;
391}
392# endif
393
394# if defined(FEAT_DIFF) || defined(PROTO)
395 void
396eval_diff(
397 char_u *origfile,
398 char_u *newfile,
399 char_u *outfile)
400{
401 int err = FALSE;
402
403 set_vim_var_string(VV_FNAME_IN, origfile, -1);
404 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
405 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
406 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
407 set_vim_var_string(VV_FNAME_IN, NULL, -1);
408 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
409 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
410}
411
412 void
413eval_patch(
414 char_u *origfile,
415 char_u *difffile,
416 char_u *outfile)
417{
418 int err;
419
420 set_vim_var_string(VV_FNAME_IN, origfile, -1);
421 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
422 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
423 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
424 set_vim_var_string(VV_FNAME_IN, NULL, -1);
425 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
426 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
427}
428# endif
429
430#if defined(FEAT_SPELL) || defined(PROTO)
431/*
432 * Evaluate an expression to a list with suggestions.
433 * For the "expr:" part of 'spellsuggest'.
434 * Returns NULL when there is an error.
435 */
436 list_T *
437eval_spell_expr(char_u *badword, char_u *expr)
438{
439 typval_T save_val;
440 typval_T rettv;
441 list_T *list = NULL;
442 char_u *p = skipwhite(expr);
443
444 // Set "v:val" to the bad word.
445 prepare_vimvar(VV_VAL, &save_val);
446 set_vim_var_string(VV_VAL, badword, -1);
447 if (p_verbose == 0)
448 ++emsg_off;
449
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200450 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200451 {
452 if (rettv.v_type != VAR_LIST)
453 clear_tv(&rettv);
454 else
455 list = rettv.vval.v_list;
456 }
457
458 if (p_verbose == 0)
459 --emsg_off;
460 clear_tv(get_vim_var_tv(VV_VAL));
461 restore_vimvar(VV_VAL, &save_val);
462
463 return list;
464}
465
466/*
467 * "list" is supposed to contain two items: a word and a number. Return the
468 * word in "pp" and the number as the return value.
469 * Return -1 if anything isn't right.
470 * Used to get the good word and score from the eval_spell_expr() result.
471 */
472 int
473get_spellword(list_T *list, char_u **pp)
474{
475 listitem_T *li;
476
477 li = list->lv_first;
478 if (li == NULL)
479 return -1;
480 *pp = tv_get_string(&li->li_tv);
481
482 li = li->li_next;
483 if (li == NULL)
484 return -1;
485 return (int)tv_get_number(&li->li_tv);
486}
487#endif
488
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200489/*
490 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200491 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200492 * When not used yet add the variable to the v: hashtable.
493 */
494 void
495prepare_vimvar(int idx, typval_T *save_tv)
496{
497 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200498 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200499 if (vimvars[idx].vv_type == VAR_UNKNOWN)
500 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
501}
502
503/*
504 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200505 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200506 * When no longer defined, remove the variable from the v: hashtable.
507 */
508 void
509restore_vimvar(int idx, typval_T *save_tv)
510{
511 hashitem_T *hi;
512
513 vimvars[idx].vv_tv = *save_tv;
514 if (vimvars[idx].vv_type == VAR_UNKNOWN)
515 {
516 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
517 if (HASHITEM_EMPTY(hi))
518 internal_error("restore_vimvar()");
519 else
520 hash_remove(&vimvarht, hi);
521 }
522}
523
524/*
525 * List Vim variables.
526 */
527 static void
528list_vim_vars(int *first)
529{
530 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
531}
532
533/*
534 * List script-local variables, if there is a script.
535 */
536 static void
537list_script_vars(int *first)
538{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200539 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200540 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
541 "s:", FALSE, first);
542}
543
544/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200545 * Get a list of lines from a HERE document. The here document is a list of
546 * lines surrounded by a marker.
547 * cmd << {marker}
548 * {line1}
549 * {line2}
550 * ....
551 * {marker}
552 *
553 * The {marker} is a string. If the optional 'trim' word is supplied before the
554 * marker, then the leading indentation before the lines (matching the
555 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200556 *
557 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
558 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
559 * missing, then '.' is accepted as a marker.
560 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200561 * Returns a List with {lines} or NULL.
562 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200564heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200565{
566 char_u *theline;
567 char_u *marker;
568 list_T *l;
569 char_u *p;
570 int marker_indent_len = 0;
571 int text_indent_len = 0;
572 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200573 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200574 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200575
576 if (eap->getline == NULL)
577 {
578 emsg(_("E991: cannot use =<< here"));
579 return NULL;
580 }
581
582 // Check for the optional 'trim' word before the marker
583 cmd = skipwhite(cmd);
584 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
585 {
586 cmd = skipwhite(cmd + 4);
587
588 // Trim the indentation from all the lines in the here document.
589 // The amount of indentation trimmed is the same as the indentation of
590 // the first line after the :let command line. To find the end marker
591 // the indent of the :let command line is trimmed.
592 p = *eap->cmdlinep;
593 while (VIM_ISWHITE(*p))
594 {
595 p++;
596 marker_indent_len++;
597 }
598 text_indent_len = -1;
599 }
600
601 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200602 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200603 {
604 marker = skipwhite(cmd);
605 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200606 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200607 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200608 semsg(_(e_trailing_arg), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200609 return NULL;
610 }
611 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200612 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200613 {
614 emsg(_("E221: Marker cannot start with lower case letter"));
615 return NULL;
616 }
617 }
618 else
619 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200620 // When getting lines for an embedded script, if the marker is missing,
621 // accept '.' as the marker.
622 if (script_get)
623 marker = dot;
624 else
625 {
626 emsg(_("E172: Missing marker"));
627 return NULL;
628 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200629 }
630
631 l = list_alloc();
632 if (l == NULL)
633 return NULL;
634
635 for (;;)
636 {
637 int mi = 0;
638 int ti = 0;
639
640 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
641 if (theline == NULL)
642 {
643 semsg(_("E990: Missing end marker '%s'"), marker);
644 break;
645 }
646
647 // with "trim": skip the indent matching the :let line to find the
648 // marker
649 if (marker_indent_len > 0
650 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
651 mi = marker_indent_len;
652 if (STRCMP(marker, theline + mi) == 0)
653 {
654 vim_free(theline);
655 break;
656 }
657
658 if (text_indent_len == -1 && *theline != NUL)
659 {
660 // set the text indent from the first line.
661 p = theline;
662 text_indent_len = 0;
663 while (VIM_ISWHITE(*p))
664 {
665 p++;
666 text_indent_len++;
667 }
668 text_indent = vim_strnsave(theline, text_indent_len);
669 }
670 // with "trim": skip the indent matching the first line
671 if (text_indent != NULL)
672 for (ti = 0; ti < text_indent_len; ++ti)
673 if (theline[ti] != text_indent[ti])
674 break;
675
676 if (list_append_string(l, theline + ti, -1) == FAIL)
677 break;
678 vim_free(theline);
679 }
680 vim_free(text_indent);
681
682 return l;
683}
684
685/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200686 * Vim9 variable declaration:
687 * ":var name"
688 * ":var name: type"
689 * ":var name = expr"
690 * ":var name: type = expr"
691 * etc.
692 */
693 void
694ex_var(exarg_T *eap)
695{
696 if (!in_vim9script())
697 {
698 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
699 return;
700 }
701 ex_let(eap);
702}
703
704/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200705 * ":let" list all variable values
706 * ":let var1 var2" list variable values
707 * ":let var = expr" assignment command.
708 * ":let var += expr" assignment command.
709 * ":let var -= expr" assignment command.
710 * ":let var *= expr" assignment command.
711 * ":let var /= expr" assignment command.
712 * ":let var %= expr" assignment command.
713 * ":let var .= expr" assignment command.
714 * ":let var ..= expr" assignment command.
715 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100716 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100717 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200718 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200719 * ":final var = expr" assignment command.
720 * ":final [var1, var2] = expr" unpack list.
721 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200722 * ":const" list all variable values
723 * ":const var1 var2" list variable values
724 * ":const var = expr" assignment command.
725 * ":const [var1, var2] = expr" unpack list.
726 */
727 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200728ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200729{
730 char_u *arg = eap->arg;
731 char_u *expr = NULL;
732 typval_T rettv;
733 int i;
734 int var_count = 0;
735 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200736 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200737 char_u *argend;
738 int first = TRUE;
739 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200740 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100741 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200742 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200743
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200744 if (eap->cmdidx == CMD_final && !vim9script)
745 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100746 // In legacy Vim script ":final" is short for ":finally".
747 ex_finally(eap);
748 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200749 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200750 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200751 {
752 emsg(_(e_cannot_use_let_in_vim9_script));
753 return;
754 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200755
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100756 if (eap->cmdidx == CMD_const)
757 flags |= ASSIGN_CONST;
758 else if (eap->cmdidx == CMD_final)
759 flags |= ASSIGN_FINAL;
760
761 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200763 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200765 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200766 if (argend == NULL)
767 return;
768 if (argend > arg && argend[-1] == '.') // for var.='str'
769 --argend;
770 expr = skipwhite(argend);
771 concat = expr[0] == '.'
772 && ((expr[1] == '=' && current_sctx.sc_version < 2)
773 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200774 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
775 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200776 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200777 {
778 // ":let" without "=": list variables
779 if (*arg == '[')
780 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200781 else if (expr[0] == '.' && expr[1] == '=')
782 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200783 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200784 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200785 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200786 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100787 // Vim9 declaration ":var name: type"
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200788 arg = vim9_declare_scriptvar(eap, arg);
789 }
790 else
791 {
792 // ":let var1 var2" - list values
793 arg = list_arg_vars(eap, arg, &first);
794 }
795 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200796 else if (!eap->skip)
797 {
798 // ":let"
799 list_glob_vars(&first);
800 list_buf_vars(&first);
801 list_win_vars(&first);
802 list_tab_vars(&first);
803 list_script_vars(&first);
804 list_func_vars(&first);
805 list_vim_vars(&first);
806 }
807 eap->nextcmd = check_nextcmd(arg);
808 }
809 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
810 {
811 list_T *l;
812
813 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200814 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200815 if (l != NULL)
816 {
817 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200818 if (!eap->skip)
819 {
820 op[0] = '=';
821 op[1] = NUL;
822 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200824 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200825 clear_tv(&rettv);
826 }
827 }
828 else
829 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200830 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200831 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200832
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200833 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200834 i = FAIL;
835 if (has_assign || concat)
836 {
837 op[0] = '=';
838 op[1] = NUL;
839 if (*expr != '=')
840 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200841 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200842 {
843 // +=, /=, etc. require an existing variable
844 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
845 i = FAIL;
846 }
847 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200848 {
849 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200850 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200851 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200852 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200853 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200854 ++len;
855 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200856 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200857 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200858 }
859 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200860 ++expr;
861
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200862 if (vim9script && (!VIM_ISWHITE(*argend)
863 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200864 {
865 vim_strncpy(op, expr - len, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200866 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200867 i = FAIL;
868 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200869
870 if (eap->skip)
871 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200872 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200873 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200874 if (getline_equal(eap->getline, eap->cookie, getsourceline))
875 {
876 evalarg.eval_getline = eap->getline;
877 evalarg.eval_cookie = eap->cookie;
878 }
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200879 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200880 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200881 if (eap->skip)
882 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200883 clear_evalarg(&evalarg, eap);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200884 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200885 if (eap->skip)
886 {
887 if (i != FAIL)
888 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200889 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200890 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200891 {
892 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200893 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200894 clear_tv(&rettv);
895 }
896 }
897}
898
899/*
900 * Assign the typevalue "tv" to the variable or variables at "arg_start".
901 * Handles both "var" with any type and "[var, var; var]" with a list type.
902 * When "op" is not NULL it points to a string with characters that
903 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
904 * or concatenate.
905 * Returns OK or FAIL;
906 */
907 int
908ex_let_vars(
909 char_u *arg_start,
910 typval_T *tv,
911 int copy, // copy values from "tv", don't move
912 int semicolon, // from skip_var_list()
913 int var_count, // from skip_var_list()
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100914 int flags, // ASSIGN_FINAL, ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200915 char_u *op)
916{
917 char_u *arg = arg_start;
918 list_T *l;
919 int i;
920 listitem_T *item;
921 typval_T ltv;
922
923 if (*arg != '[')
924 {
925 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100926 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200927 return FAIL;
928 return OK;
929 }
930
931 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
932 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
933 {
934 emsg(_(e_listreq));
935 return FAIL;
936 }
937
938 i = list_len(l);
939 if (semicolon == 0 && var_count < i)
940 {
941 emsg(_("E687: Less targets than List items"));
942 return FAIL;
943 }
944 if (var_count - semicolon > i)
945 {
946 emsg(_("E688: More targets than List items"));
947 return FAIL;
948 }
949
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200950 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200951 item = l->lv_first;
952 while (*arg != ']')
953 {
954 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200956 item = item->li_next;
957 if (arg == NULL)
958 return FAIL;
959
960 arg = skipwhite(arg);
961 if (*arg == ';')
962 {
963 // Put the rest of the list (may be empty) in the var after ';'.
964 // Create a new list for this.
965 l = list_alloc();
966 if (l == NULL)
967 return FAIL;
968 while (item != NULL)
969 {
970 list_append_tv(l, &item->li_tv);
971 item = item->li_next;
972 }
973
974 ltv.v_type = VAR_LIST;
975 ltv.v_lock = 0;
976 ltv.vval.v_list = l;
977 l->lv_refcount = 1;
978
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100979 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200980 (char_u *)"]", op);
981 clear_tv(&ltv);
982 if (arg == NULL)
983 return FAIL;
984 break;
985 }
986 else if (*arg != ',' && *arg != ']')
987 {
988 internal_error("ex_let_vars()");
989 return FAIL;
990 }
991 }
992
993 return OK;
994}
995
996/*
997 * Skip over assignable variable "var" or list of variables "[var, var]".
998 * Used for ":let varvar = expr" and ":for varvar in expr".
999 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001000 * for "[var, var; var]" set "semicolon" to 1.
1001 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001002 * Return NULL for an error.
1003 */
1004 char_u *
1005skip_var_list(
1006 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001008 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001009 int *semicolon,
1010 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001011{
1012 char_u *p, *s;
1013
1014 if (*arg == '[')
1015 {
1016 // "[var, var]": find the matching ']'.
1017 p = arg;
1018 for (;;)
1019 {
1020 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001021 s = skip_var_one(p, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001022 if (s == p)
1023 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001024 if (!silent)
1025 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001026 return NULL;
1027 }
1028 ++*var_count;
1029
1030 p = skipwhite(s);
1031 if (*p == ']')
1032 break;
1033 else if (*p == ';')
1034 {
1035 if (*semicolon == 1)
1036 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001037 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001038 return NULL;
1039 }
1040 *semicolon = 1;
1041 }
1042 else if (*p != ',')
1043 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001044 if (!silent)
1045 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001046 return NULL;
1047 }
1048 }
1049 return p + 1;
1050 }
1051 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001052 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001053}
1054
1055/*
1056 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1057 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001058 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001059 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001060 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001061skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001062{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001063 char_u *end;
1064
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001065 if (*arg == '@' && arg[1] != NUL)
1066 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001067 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001068 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001069 if (include_type && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001070 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001071 // "a: type" is declaring variable "a" with a type, not "a:".
1072 if (end == arg + 2 && end[-1] == ':')
1073 --end;
1074 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001075 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076 }
1077 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001078}
1079
1080/*
1081 * List variables for hashtab "ht" with prefix "prefix".
1082 * If "empty" is TRUE also list NULL strings as empty strings.
1083 */
1084 void
1085list_hashtable_vars(
1086 hashtab_T *ht,
1087 char *prefix,
1088 int empty,
1089 int *first)
1090{
1091 hashitem_T *hi;
1092 dictitem_T *di;
1093 int todo;
1094 char_u buf[IOSIZE];
1095
1096 todo = (int)ht->ht_used;
1097 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1098 {
1099 if (!HASHITEM_EMPTY(hi))
1100 {
1101 --todo;
1102 di = HI2DI(hi);
1103
1104 // apply :filter /pat/ to variable name
1105 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1106 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1107 if (message_filtered(buf))
1108 continue;
1109
1110 if (empty || di->di_tv.v_type != VAR_STRING
1111 || di->di_tv.vval.v_string != NULL)
1112 list_one_var(di, prefix, first);
1113 }
1114 }
1115}
1116
1117/*
1118 * List global variables.
1119 */
1120 static void
1121list_glob_vars(int *first)
1122{
1123 list_hashtable_vars(&globvarht, "", TRUE, first);
1124}
1125
1126/*
1127 * List buffer variables.
1128 */
1129 static void
1130list_buf_vars(int *first)
1131{
1132 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1133}
1134
1135/*
1136 * List window variables.
1137 */
1138 static void
1139list_win_vars(int *first)
1140{
1141 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1142}
1143
1144/*
1145 * List tab page variables.
1146 */
1147 static void
1148list_tab_vars(int *first)
1149{
1150 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1151}
1152
1153/*
1154 * List variables in "arg".
1155 */
1156 static char_u *
1157list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1158{
1159 int error = FALSE;
1160 int len;
1161 char_u *name;
1162 char_u *name_start;
1163 char_u *arg_subsc;
1164 char_u *tofree;
1165 typval_T tv;
1166
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001167 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001168 {
1169 if (error || eap->skip)
1170 {
1171 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1172 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1173 {
1174 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001175 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001176 break;
1177 }
1178 }
1179 else
1180 {
1181 // get_name_len() takes care of expanding curly braces
1182 name_start = name = arg;
1183 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1184 if (len <= 0)
1185 {
1186 // This is mainly to keep test 49 working: when expanding
1187 // curly braces fails overrule the exception error message.
1188 if (len < 0 && !aborting())
1189 {
1190 emsg_severe = TRUE;
1191 semsg(_(e_invarg2), arg);
1192 break;
1193 }
1194 error = TRUE;
1195 }
1196 else
1197 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001198 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001199 if (tofree != NULL)
1200 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001201 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001202 error = TRUE;
1203 else
1204 {
1205 // handle d.key, l[idx], f(expr)
1206 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001207 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001208 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001209 error = TRUE;
1210 else
1211 {
1212 if (arg == arg_subsc && len == 2 && name[1] == ':')
1213 {
1214 switch (*name)
1215 {
1216 case 'g': list_glob_vars(first); break;
1217 case 'b': list_buf_vars(first); break;
1218 case 'w': list_win_vars(first); break;
1219 case 't': list_tab_vars(first); break;
1220 case 'v': list_vim_vars(first); break;
1221 case 's': list_script_vars(first); break;
1222 case 'l': list_func_vars(first); break;
1223 default:
1224 semsg(_("E738: Can't list variables for %s"), name);
1225 }
1226 }
1227 else
1228 {
1229 char_u numbuf[NUMBUFLEN];
1230 char_u *tf;
1231 int c;
1232 char_u *s;
1233
1234 s = echo_string(&tv, &tf, numbuf, 0);
1235 c = *arg;
1236 *arg = NUL;
1237 list_one_var_a("",
1238 arg == arg_subsc ? name : name_start,
1239 tv.v_type,
1240 s == NULL ? (char_u *)"" : s,
1241 first);
1242 *arg = c;
1243 vim_free(tf);
1244 }
1245 clear_tv(&tv);
1246 }
1247 }
1248 }
1249
1250 vim_free(tofree);
1251 }
1252
1253 arg = skipwhite(arg);
1254 }
1255
1256 return arg;
1257}
1258
1259/*
1260 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1261 * Returns a pointer to the char just after the var name.
1262 * Returns NULL if there is an error.
1263 */
1264 static char_u *
1265ex_let_one(
1266 char_u *arg, // points to variable name
1267 typval_T *tv, // value to assign to variable
1268 int copy, // copy value from "tv"
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001269 int flags, // ASSIGN_CONST, ASSIGN_FINAL, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001270 char_u *endchars, // valid chars after variable name or NULL
1271 char_u *op) // "+", "-", "." or NULL
1272{
1273 int c1;
1274 char_u *name;
1275 char_u *p;
1276 char_u *arg_end = NULL;
1277 int len;
1278 int opt_flags;
1279 char_u *tofree = NULL;
1280
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001281 if (in_vim9script() && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001282 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001283 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1284 {
1285 vim9_declare_error(arg);
1286 return NULL;
1287 }
1288
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001289 // ":let $VAR = expr": Set environment variable.
1290 if (*arg == '$')
1291 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001292 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001293 {
1294 emsg(_("E996: Cannot lock an environment variable"));
1295 return NULL;
1296 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001297
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001298 // Find the end of the name.
1299 ++arg;
1300 name = arg;
1301 len = get_env_len(&arg);
1302 if (len == 0)
1303 semsg(_(e_invarg2), name - 1);
1304 else
1305 {
1306 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1307 semsg(_(e_letwrong), op);
1308 else if (endchars != NULL
1309 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1310 emsg(_(e_letunexp));
1311 else if (!check_secure())
1312 {
1313 c1 = name[len];
1314 name[len] = NUL;
1315 p = tv_get_string_chk(tv);
1316 if (p != NULL && op != NULL && *op == '.')
1317 {
1318 int mustfree = FALSE;
1319 char_u *s = vim_getenv(name, &mustfree);
1320
1321 if (s != NULL)
1322 {
1323 p = tofree = concat_str(s, p);
1324 if (mustfree)
1325 vim_free(s);
1326 }
1327 }
1328 if (p != NULL)
1329 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001330 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001331 arg_end = arg;
1332 }
1333 name[len] = c1;
1334 vim_free(tofree);
1335 }
1336 }
1337 }
1338
1339 // ":let &option = expr": Set option value.
1340 // ":let &l:option = expr": Set local option value.
1341 // ":let &g:option = expr": Set global option value.
1342 else if (*arg == '&')
1343 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001344 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001345 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001347 return NULL;
1348 }
1349 // Find the end of the name.
1350 p = find_option_end(&arg, &opt_flags);
1351 if (p == NULL || (endchars != NULL
1352 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1353 emsg(_(e_letunexp));
1354 else
1355 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001356 long n = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001357 getoption_T opt_type;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001358 long numval;
1359 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001360 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001361 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001362
1363 c1 = *p;
1364 *p = NUL;
1365
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001366 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001367 if ((opt_type == gov_bool
1368 || opt_type == gov_number
1369 || opt_type == gov_hidden_bool
1370 || opt_type == gov_hidden_number)
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001371 && (tv->v_type != VAR_STRING || !in_vim9script()))
1372 // number, possibly hidden
1373 n = (long)tv_get_number(tv);
1374
1375 // Avoid setting a string option to the text "v:false" or similar.
1376 // In Vim9 script also don't convert a number to string.
1377 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1378 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1379 s = tv_get_string_chk(tv);
1380
1381 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001382 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001383 if (((opt_type == gov_bool || opt_type == gov_number)
1384 && *op == '.')
1385 || (opt_type == gov_string && *op != '.'))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001386 {
1387 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001388 failed = TRUE; // don't set the value
1389
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001390 }
1391 else
1392 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001393 // number, in legacy script also bool
1394 if (opt_type == gov_number
1395 || (opt_type == gov_bool && !in_vim9script()))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001396 {
1397 switch (*op)
1398 {
1399 case '+': n = numval + n; break;
1400 case '-': n = numval - n; break;
1401 case '*': n = numval * n; break;
1402 case '/': n = (long)num_divide(numval, n); break;
1403 case '%': n = (long)num_modulus(numval, n); break;
1404 }
1405 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001406 else if (opt_type == gov_string
1407 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001408 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001409 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001410 s = concat_str(stringval, s);
1411 vim_free(stringval);
1412 stringval = s;
1413 }
1414 }
1415 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001416
1417 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001418 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001419 if (opt_type != gov_string || s != NULL)
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001420 {
1421 set_option_value(arg, n, s, opt_flags);
1422 arg_end = p;
1423 }
1424 else
1425 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001426 }
1427 *p = c1;
1428 vim_free(stringval);
1429 }
1430 }
1431
1432 // ":let @r = expr": Set register contents.
1433 else if (*arg == '@')
1434 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001435 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001436 {
1437 emsg(_("E996: Cannot lock a register"));
1438 return NULL;
1439 }
1440 ++arg;
1441 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1442 semsg(_(e_letwrong), op);
1443 else if (endchars != NULL
1444 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1445 emsg(_(e_letunexp));
1446 else
1447 {
1448 char_u *ptofree = NULL;
1449 char_u *s;
1450
1451 p = tv_get_string_chk(tv);
1452 if (p != NULL && op != NULL && *op == '.')
1453 {
1454 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1455 if (s != NULL)
1456 {
1457 p = ptofree = concat_str(s, p);
1458 vim_free(s);
1459 }
1460 }
1461 if (p != NULL)
1462 {
1463 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1464 arg_end = arg + 1;
1465 }
1466 vim_free(ptofree);
1467 }
1468 }
1469
1470 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001472 // ":let {expr} = expr": Idem, name made with curly braces
1473 else if (eval_isnamec1(*arg) || *arg == '{')
1474 {
1475 lval_T lv;
1476
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001477 p = get_lval(arg, tv, &lv, FALSE, FALSE,
1478 (flags & ASSIGN_NO_DECL) ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001479 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001480 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001481 if (endchars != NULL && vim_strchr(endchars,
1482 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001483 emsg(_(e_letunexp));
1484 else
1485 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001487 arg_end = p;
1488 }
1489 }
1490 clear_lval(&lv);
1491 }
1492
1493 else
1494 semsg(_(e_invarg2), arg);
1495
1496 return arg_end;
1497}
1498
1499/*
1500 * ":unlet[!] var1 ... " command.
1501 */
1502 void
1503ex_unlet(exarg_T *eap)
1504{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001505 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001506}
1507
1508/*
1509 * ":lockvar" and ":unlockvar" commands
1510 */
1511 void
1512ex_lockvar(exarg_T *eap)
1513{
1514 char_u *arg = eap->arg;
1515 int deep = 2;
1516
1517 if (eap->forceit)
1518 deep = -1;
1519 else if (vim_isdigit(*arg))
1520 {
1521 deep = getdigits(&arg);
1522 arg = skipwhite(arg);
1523 }
1524
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001525 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001526}
1527
1528/*
1529 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001530 * Also used for Vim9 script. "callback" is invoked as:
1531 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001532 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001533 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001534ex_unletlock(
1535 exarg_T *eap,
1536 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001537 int deep,
1538 int glv_flags,
1539 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1540 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001541{
1542 char_u *arg = argstart;
1543 char_u *name_end;
1544 int error = FALSE;
1545 lval_T lv;
1546
1547 do
1548 {
1549 if (*arg == '$')
1550 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001551 lv.ll_name = arg;
1552 lv.ll_tv = NULL;
1553 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001554 if (get_env_len(&arg) == 0)
1555 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001556 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001557 return;
1558 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001559 if (!error && !eap->skip
1560 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1561 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001562 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001563 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001564 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001565 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001566 // Parse the name and find the end.
1567 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1568 glv_flags, FNE_CHECK_START);
1569 if (lv.ll_name == NULL)
1570 error = TRUE; // error but continue parsing
1571 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1572 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001573 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001574 if (name_end != NULL)
1575 {
1576 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001577 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001578 }
1579 if (!(eap->skip || error))
1580 clear_lval(&lv);
1581 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001582 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001583
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001584 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001585 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001586 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001587
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001588 if (!eap->skip)
1589 clear_lval(&lv);
1590 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001591
1592 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001593 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001594
1595 eap->nextcmd = check_nextcmd(arg);
1596}
1597
1598 static int
1599do_unlet_var(
1600 lval_T *lp,
1601 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001602 exarg_T *eap,
1603 int deep UNUSED,
1604 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001605{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001606 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001607 int ret = OK;
1608 int cc;
1609
1610 if (lp->ll_tv == NULL)
1611 {
1612 cc = *name_end;
1613 *name_end = NUL;
1614
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001615 // Environment variable, normal name or expanded name.
1616 if (*lp->ll_name == '$')
1617 vim_unsetenv(lp->ll_name + 1);
1618 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001619 ret = FAIL;
1620 *name_end = cc;
1621 }
1622 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001623 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001624 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001625 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001626 return FAIL;
1627 else if (lp->ll_range)
1628 {
1629 listitem_T *li;
1630 listitem_T *ll_li = lp->ll_li;
1631 int ll_n1 = lp->ll_n1;
1632
1633 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1634 {
1635 li = ll_li->li_next;
Bram Moolenaara187c432020-09-16 21:08:28 +02001636 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001637 return FAIL;
1638 ll_li = li;
1639 ++ll_n1;
1640 }
1641
1642 // Delete a range of List items.
1643 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1644 {
1645 li = lp->ll_li->li_next;
1646 listitem_remove(lp->ll_list, lp->ll_li);
1647 lp->ll_li = li;
1648 ++lp->ll_n1;
1649 }
1650 }
1651 else
1652 {
1653 if (lp->ll_list != NULL)
1654 // unlet a List item.
1655 listitem_remove(lp->ll_list, lp->ll_li);
1656 else
1657 // unlet a Dictionary item.
1658 dictitem_remove(lp->ll_dict, lp->ll_di);
1659 }
1660
1661 return ret;
1662}
1663
1664/*
1665 * "unlet" a variable. Return OK if it existed, FAIL if not.
1666 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1667 */
1668 int
1669do_unlet(char_u *name, int forceit)
1670{
1671 hashtab_T *ht;
1672 hashitem_T *hi;
1673 char_u *varname;
1674 dict_T *d;
1675 dictitem_T *di;
1676
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001677 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001678 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001679 return FAIL;
1680
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001681 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001682
1683 // can't :unlet a script variable in Vim9 script from a function
1684 if (ht == get_script_local_ht()
1685 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1686 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1687 == SCRIPT_VERSION_VIM9
1688 && check_vim9_unlet(name) == FAIL)
1689 return FAIL;
1690
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001691 if (ht != NULL && *varname != NUL)
1692 {
1693 d = get_current_funccal_dict(ht);
1694 if (d == NULL)
1695 {
1696 if (ht == &globvarht)
1697 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001698 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001699 d = &vimvardict;
1700 else
1701 {
1702 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1703 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1704 }
1705 if (d == NULL)
1706 {
1707 internal_error("do_unlet()");
1708 return FAIL;
1709 }
1710 }
1711 hi = hash_find(ht, varname);
1712 if (HASHITEM_EMPTY(hi))
1713 hi = find_hi_in_scoped_ht(name, &ht);
1714 if (hi != NULL && !HASHITEM_EMPTY(hi))
1715 {
1716 di = HI2DI(hi);
1717 if (var_check_fixed(di->di_flags, name, FALSE)
1718 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001719 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001720 return FAIL;
1721
1722 delete_var(ht, hi);
1723 return OK;
1724 }
1725 }
1726 if (forceit)
1727 return OK;
1728 semsg(_("E108: No such variable: \"%s\""), name);
1729 return FAIL;
1730}
1731
1732/*
1733 * Lock or unlock variable indicated by "lp".
1734 * "deep" is the levels to go (-1 for unlimited);
1735 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1736 */
1737 static int
1738do_lock_var(
1739 lval_T *lp,
1740 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001741 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001742 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001743 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001744{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001745 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001746 int ret = OK;
1747 int cc;
1748 dictitem_T *di;
1749
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001750 if (lp->ll_tv == NULL)
1751 {
1752 cc = *name_end;
1753 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001754 if (*lp->ll_name == '$')
1755 {
1756 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001757 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001758 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001759 else
1760 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001761 // Normal name or expanded name.
1762 di = find_var(lp->ll_name, NULL, TRUE);
1763 if (di == NULL)
1764 ret = FAIL;
1765 else if ((di->di_flags & DI_FLAGS_FIX)
1766 && di->di_tv.v_type != VAR_DICT
1767 && di->di_tv.v_type != VAR_LIST)
1768 {
1769 // For historic reasons this error is not given for a list or
1770 // dict. E.g., the b: dict could be locked/unlocked.
1771 semsg(_(e_lock_unlock), lp->ll_name);
1772 ret = FAIL;
1773 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001774 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001775 {
1776 if (lock)
1777 di->di_flags |= DI_FLAGS_LOCK;
1778 else
1779 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001780 if (deep != 0)
1781 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001782 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001783 }
1784 *name_end = cc;
1785 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001786 else if (deep == 0)
1787 {
1788 // nothing to do
1789 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001790 else if (lp->ll_range)
1791 {
1792 listitem_T *li = lp->ll_li;
1793
1794 // (un)lock a range of List items.
1795 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1796 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001797 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001798 li = li->li_next;
1799 ++lp->ll_n1;
1800 }
1801 }
1802 else if (lp->ll_list != NULL)
1803 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001804 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001805 else
1806 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001807 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001808
1809 return ret;
1810}
1811
1812/*
1813 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001814 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1815 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001816 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001817 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001818item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001819{
1820 static int recurse = 0;
1821 list_T *l;
1822 listitem_T *li;
1823 dict_T *d;
1824 blob_T *b;
1825 hashitem_T *hi;
1826 int todo;
1827
1828 if (recurse >= DICT_MAXNEST)
1829 {
1830 emsg(_("E743: variable nested too deep for (un)lock"));
1831 return;
1832 }
1833 if (deep == 0)
1834 return;
1835 ++recurse;
1836
1837 // lock/unlock the item itself
1838 if (lock)
1839 tv->v_lock |= VAR_LOCKED;
1840 else
1841 tv->v_lock &= ~VAR_LOCKED;
1842
1843 switch (tv->v_type)
1844 {
1845 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001846 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001848 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001849 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001850 case VAR_STRING:
1851 case VAR_FUNC:
1852 case VAR_PARTIAL:
1853 case VAR_FLOAT:
1854 case VAR_SPECIAL:
1855 case VAR_JOB:
1856 case VAR_CHANNEL:
1857 break;
1858
1859 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001860 if ((b = tv->vval.v_blob) != NULL
1861 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001862 {
1863 if (lock)
1864 b->bv_lock |= VAR_LOCKED;
1865 else
1866 b->bv_lock &= ~VAR_LOCKED;
1867 }
1868 break;
1869 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001870 if ((l = tv->vval.v_list) != NULL
1871 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001872 {
1873 if (lock)
1874 l->lv_lock |= VAR_LOCKED;
1875 else
1876 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001877 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001878 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001879 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001880 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001881 }
1882 break;
1883 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001884 if ((d = tv->vval.v_dict) != NULL
1885 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001886 {
1887 if (lock)
1888 d->dv_lock |= VAR_LOCKED;
1889 else
1890 d->dv_lock &= ~VAR_LOCKED;
1891 if (deep < 0 || deep > 1)
1892 {
1893 // recursive: lock/unlock the items the List contains
1894 todo = (int)d->dv_hashtab.ht_used;
1895 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1896 {
1897 if (!HASHITEM_EMPTY(hi))
1898 {
1899 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001900 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1901 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001902 }
1903 }
1904 }
1905 }
1906 }
1907 --recurse;
1908}
1909
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001910#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1911/*
1912 * Delete all "menutrans_" variables.
1913 */
1914 void
1915del_menutrans_vars(void)
1916{
1917 hashitem_T *hi;
1918 int todo;
1919
1920 hash_lock(&globvarht);
1921 todo = (int)globvarht.ht_used;
1922 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1923 {
1924 if (!HASHITEM_EMPTY(hi))
1925 {
1926 --todo;
1927 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1928 delete_var(&globvarht, hi);
1929 }
1930 }
1931 hash_unlock(&globvarht);
1932}
1933#endif
1934
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001935/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001936 * Local string buffer for the next two functions to store a variable name
1937 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1938 * get_user_var_name().
1939 */
1940
1941static char_u *varnamebuf = NULL;
1942static int varnamebuflen = 0;
1943
1944/*
1945 * Function to concatenate a prefix and a variable name.
1946 */
1947 static char_u *
1948cat_prefix_varname(int prefix, char_u *name)
1949{
1950 int len;
1951
1952 len = (int)STRLEN(name) + 3;
1953 if (len > varnamebuflen)
1954 {
1955 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001956 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001957 varnamebuf = alloc(len);
1958 if (varnamebuf == NULL)
1959 {
1960 varnamebuflen = 0;
1961 return NULL;
1962 }
1963 varnamebuflen = len;
1964 }
1965 *varnamebuf = prefix;
1966 varnamebuf[1] = ':';
1967 STRCPY(varnamebuf + 2, name);
1968 return varnamebuf;
1969}
1970
1971/*
1972 * Function given to ExpandGeneric() to obtain the list of user defined
1973 * (global/buffer/window/built-in) variable names.
1974 */
1975 char_u *
1976get_user_var_name(expand_T *xp, int idx)
1977{
1978 static long_u gdone;
1979 static long_u bdone;
1980 static long_u wdone;
1981 static long_u tdone;
1982 static int vidx;
1983 static hashitem_T *hi;
1984 hashtab_T *ht;
1985
1986 if (idx == 0)
1987 {
1988 gdone = bdone = wdone = vidx = 0;
1989 tdone = 0;
1990 }
1991
1992 // Global variables
1993 if (gdone < globvarht.ht_used)
1994 {
1995 if (gdone++ == 0)
1996 hi = globvarht.ht_array;
1997 else
1998 ++hi;
1999 while (HASHITEM_EMPTY(hi))
2000 ++hi;
2001 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2002 return cat_prefix_varname('g', hi->hi_key);
2003 return hi->hi_key;
2004 }
2005
2006 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002007 ht =
2008#ifdef FEAT_CMDWIN
2009 // In cmdwin, the alternative buffer should be used.
2010 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2011 &prevwin->w_buffer->b_vars->dv_hashtab :
2012#endif
2013 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002014 if (bdone < ht->ht_used)
2015 {
2016 if (bdone++ == 0)
2017 hi = ht->ht_array;
2018 else
2019 ++hi;
2020 while (HASHITEM_EMPTY(hi))
2021 ++hi;
2022 return cat_prefix_varname('b', hi->hi_key);
2023 }
2024
2025 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002026 ht =
2027#ifdef FEAT_CMDWIN
2028 // In cmdwin, the alternative window should be used.
2029 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2030 &prevwin->w_vars->dv_hashtab :
2031#endif
2032 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002033 if (wdone < ht->ht_used)
2034 {
2035 if (wdone++ == 0)
2036 hi = ht->ht_array;
2037 else
2038 ++hi;
2039 while (HASHITEM_EMPTY(hi))
2040 ++hi;
2041 return cat_prefix_varname('w', hi->hi_key);
2042 }
2043
2044 // t: variables
2045 ht = &curtab->tp_vars->dv_hashtab;
2046 if (tdone < ht->ht_used)
2047 {
2048 if (tdone++ == 0)
2049 hi = ht->ht_array;
2050 else
2051 ++hi;
2052 while (HASHITEM_EMPTY(hi))
2053 ++hi;
2054 return cat_prefix_varname('t', hi->hi_key);
2055 }
2056
2057 // v: variables
2058 if (vidx < VV_LEN)
2059 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2060
2061 VIM_CLEAR(varnamebuf);
2062 varnamebuflen = 0;
2063 return NULL;
2064}
2065
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002066 char *
2067get_var_special_name(int nr)
2068{
2069 switch (nr)
2070 {
2071 case VVAL_FALSE: return "v:false";
2072 case VVAL_TRUE: return "v:true";
2073 case VVAL_NONE: return "v:none";
2074 case VVAL_NULL: return "v:null";
2075 }
2076 internal_error("get_var_special_name()");
2077 return "42";
2078}
2079
2080/*
2081 * Returns the global variable dictionary
2082 */
2083 dict_T *
2084get_globvar_dict(void)
2085{
2086 return &globvardict;
2087}
2088
2089/*
2090 * Returns the global variable hash table
2091 */
2092 hashtab_T *
2093get_globvar_ht(void)
2094{
2095 return &globvarht;
2096}
2097
2098/*
2099 * Returns the v: variable dictionary
2100 */
2101 dict_T *
2102get_vimvar_dict(void)
2103{
2104 return &vimvardict;
2105}
2106
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002107/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002108 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002109 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002110 */
2111 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002112find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002114 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2115 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116
2117 if (di == NULL)
2118 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002119 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002120 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2121 return (int)(vv - vimvars);
2122}
2123
2124
2125/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002126 * Set type of v: variable to "type".
2127 */
2128 void
2129set_vim_var_type(int idx, vartype_T type)
2130{
2131 vimvars[idx].vv_type = type;
2132}
2133
2134/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002135 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002136 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002137 */
2138 void
2139set_vim_var_nr(int idx, varnumber_T val)
2140{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002141 vimvars[idx].vv_nr = val;
2142}
2143
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002144 char *
2145get_vim_var_name(int idx)
2146{
2147 return vimvars[idx].vv_name;
2148}
2149
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002150/*
2151 * Get typval_T v: variable value.
2152 */
2153 typval_T *
2154get_vim_var_tv(int idx)
2155{
2156 return &vimvars[idx].vv_tv;
2157}
2158
2159/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002160 * Set v: variable to "tv". Only accepts the same type.
2161 * Takes over the value of "tv".
2162 */
2163 int
2164set_vim_var_tv(int idx, typval_T *tv)
2165{
2166 if (vimvars[idx].vv_type != tv->v_type)
2167 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002168 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002169 clear_tv(tv);
2170 return FAIL;
2171 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002172 // VV_RO is also checked when compiling, but let's check here as well.
2173 if (vimvars[idx].vv_flags & VV_RO)
2174 {
2175 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2176 return FAIL;
2177 }
2178 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2179 {
2180 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2181 return FAIL;
2182 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002183 clear_tv(&vimvars[idx].vv_di.di_tv);
2184 vimvars[idx].vv_di.di_tv = *tv;
2185 return OK;
2186}
2187
2188/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002189 * Get number v: variable value.
2190 */
2191 varnumber_T
2192get_vim_var_nr(int idx)
2193{
2194 return vimvars[idx].vv_nr;
2195}
2196
2197/*
2198 * Get string v: variable value. Uses a static buffer, can only be used once.
2199 * If the String variable has never been set, return an empty string.
2200 * Never returns NULL;
2201 */
2202 char_u *
2203get_vim_var_str(int idx)
2204{
2205 return tv_get_string(&vimvars[idx].vv_tv);
2206}
2207
2208/*
2209 * Get List v: variable value. Caller must take care of reference count when
2210 * needed.
2211 */
2212 list_T *
2213get_vim_var_list(int idx)
2214{
2215 return vimvars[idx].vv_list;
2216}
2217
2218/*
2219 * Get Dict v: variable value. Caller must take care of reference count when
2220 * needed.
2221 */
2222 dict_T *
2223get_vim_var_dict(int idx)
2224{
2225 return vimvars[idx].vv_dict;
2226}
2227
2228/*
2229 * Set v:char to character "c".
2230 */
2231 void
2232set_vim_var_char(int c)
2233{
2234 char_u buf[MB_MAXBYTES + 1];
2235
2236 if (has_mbyte)
2237 buf[(*mb_char2bytes)(c, buf)] = NUL;
2238 else
2239 {
2240 buf[0] = c;
2241 buf[1] = NUL;
2242 }
2243 set_vim_var_string(VV_CHAR, buf, -1);
2244}
2245
2246/*
2247 * Set v:count to "count" and v:count1 to "count1".
2248 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2249 */
2250 void
2251set_vcount(
2252 long count,
2253 long count1,
2254 int set_prevcount)
2255{
2256 if (set_prevcount)
2257 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2258 vimvars[VV_COUNT].vv_nr = count;
2259 vimvars[VV_COUNT1].vv_nr = count1;
2260}
2261
2262/*
2263 * Save variables that might be changed as a side effect. Used when executing
2264 * a timer callback.
2265 */
2266 void
2267save_vimvars(vimvars_save_T *vvsave)
2268{
2269 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2270 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2271 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2272}
2273
2274/*
2275 * Restore variables saved by save_vimvars().
2276 */
2277 void
2278restore_vimvars(vimvars_save_T *vvsave)
2279{
2280 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2281 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2282 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2283}
2284
2285/*
2286 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2287 * value.
2288 */
2289 void
2290set_vim_var_string(
2291 int idx,
2292 char_u *val,
2293 int len) // length of "val" to use or -1 (whole string)
2294{
2295 clear_tv(&vimvars[idx].vv_di.di_tv);
2296 vimvars[idx].vv_type = VAR_STRING;
2297 if (val == NULL)
2298 vimvars[idx].vv_str = NULL;
2299 else if (len == -1)
2300 vimvars[idx].vv_str = vim_strsave(val);
2301 else
2302 vimvars[idx].vv_str = vim_strnsave(val, len);
2303}
2304
2305/*
2306 * Set List v: variable to "val".
2307 */
2308 void
2309set_vim_var_list(int idx, list_T *val)
2310{
2311 clear_tv(&vimvars[idx].vv_di.di_tv);
2312 vimvars[idx].vv_type = VAR_LIST;
2313 vimvars[idx].vv_list = val;
2314 if (val != NULL)
2315 ++val->lv_refcount;
2316}
2317
2318/*
2319 * Set Dictionary v: variable to "val".
2320 */
2321 void
2322set_vim_var_dict(int idx, dict_T *val)
2323{
2324 clear_tv(&vimvars[idx].vv_di.di_tv);
2325 vimvars[idx].vv_type = VAR_DICT;
2326 vimvars[idx].vv_dict = val;
2327 if (val != NULL)
2328 {
2329 ++val->dv_refcount;
2330 dict_set_items_ro(val);
2331 }
2332}
2333
2334/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002335 * Set the v:argv list.
2336 */
2337 void
2338set_argv_var(char **argv, int argc)
2339{
2340 list_T *l = list_alloc();
2341 int i;
2342
2343 if (l == NULL)
2344 getout(1);
2345 l->lv_lock = VAR_FIXED;
2346 for (i = 0; i < argc; ++i)
2347 {
2348 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2349 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002350 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002351 }
2352 set_vim_var_list(VV_ARGV, l);
2353}
2354
2355/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002356 * Reset v:register, taking the 'clipboard' setting into account.
2357 */
2358 void
2359reset_reg_var(void)
2360{
2361 int regname = 0;
2362
2363 // Adjust the register according to 'clipboard', so that when
2364 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2365#ifdef FEAT_CLIPBOARD
2366 adjust_clip_reg(&regname);
2367#endif
2368 set_reg_var(regname);
2369}
2370
2371/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002372 * Set v:register if needed.
2373 */
2374 void
2375set_reg_var(int c)
2376{
2377 char_u regname;
2378
2379 if (c == 0 || c == ' ')
2380 regname = '"';
2381 else
2382 regname = c;
2383 // Avoid free/alloc when the value is already right.
2384 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2385 set_vim_var_string(VV_REG, &regname, 1);
2386}
2387
2388/*
2389 * Get or set v:exception. If "oldval" == NULL, return the current value.
2390 * Otherwise, restore the value to "oldval" and return NULL.
2391 * Must always be called in pairs to save and restore v:exception! Does not
2392 * take care of memory allocations.
2393 */
2394 char_u *
2395v_exception(char_u *oldval)
2396{
2397 if (oldval == NULL)
2398 return vimvars[VV_EXCEPTION].vv_str;
2399
2400 vimvars[VV_EXCEPTION].vv_str = oldval;
2401 return NULL;
2402}
2403
2404/*
2405 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2406 * Otherwise, restore the value to "oldval" and return NULL.
2407 * Must always be called in pairs to save and restore v:throwpoint! Does not
2408 * take care of memory allocations.
2409 */
2410 char_u *
2411v_throwpoint(char_u *oldval)
2412{
2413 if (oldval == NULL)
2414 return vimvars[VV_THROWPOINT].vv_str;
2415
2416 vimvars[VV_THROWPOINT].vv_str = oldval;
2417 return NULL;
2418}
2419
2420/*
2421 * Set v:cmdarg.
2422 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2423 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2424 * Must always be called in pairs!
2425 */
2426 char_u *
2427set_cmdarg(exarg_T *eap, char_u *oldarg)
2428{
2429 char_u *oldval;
2430 char_u *newval;
2431 unsigned len;
2432
2433 oldval = vimvars[VV_CMDARG].vv_str;
2434 if (eap == NULL)
2435 {
2436 vim_free(oldval);
2437 vimvars[VV_CMDARG].vv_str = oldarg;
2438 return NULL;
2439 }
2440
2441 if (eap->force_bin == FORCE_BIN)
2442 len = 6;
2443 else if (eap->force_bin == FORCE_NOBIN)
2444 len = 8;
2445 else
2446 len = 0;
2447
2448 if (eap->read_edit)
2449 len += 7;
2450
2451 if (eap->force_ff != 0)
2452 len += 10; // " ++ff=unix"
2453 if (eap->force_enc != 0)
2454 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2455 if (eap->bad_char != 0)
2456 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2457
2458 newval = alloc(len + 1);
2459 if (newval == NULL)
2460 return NULL;
2461
2462 if (eap->force_bin == FORCE_BIN)
2463 sprintf((char *)newval, " ++bin");
2464 else if (eap->force_bin == FORCE_NOBIN)
2465 sprintf((char *)newval, " ++nobin");
2466 else
2467 *newval = NUL;
2468
2469 if (eap->read_edit)
2470 STRCAT(newval, " ++edit");
2471
2472 if (eap->force_ff != 0)
2473 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2474 eap->force_ff == 'u' ? "unix"
2475 : eap->force_ff == 'd' ? "dos"
2476 : "mac");
2477 if (eap->force_enc != 0)
2478 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2479 eap->cmd + eap->force_enc);
2480 if (eap->bad_char == BAD_KEEP)
2481 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2482 else if (eap->bad_char == BAD_DROP)
2483 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2484 else if (eap->bad_char != 0)
2485 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2486 vimvars[VV_CMDARG].vv_str = newval;
2487 return oldval;
2488}
2489
2490/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002491 * Get the value of internal variable "name".
2492 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2493 */
2494 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002495eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002496 char_u *name,
2497 int len, // length of "name"
2498 typval_T *rettv, // NULL when only checking existence
2499 dictitem_T **dip, // non-NULL when typval's dict item is needed
2500 int verbose, // may give error message
2501 int no_autoload) // do not use script autoloading
2502{
2503 int ret = OK;
2504 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002505 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002506 dictitem_T *v;
2507 int cc;
2508
2509 // truncate the name, so that we can use strcmp()
2510 cc = name[len];
2511 name[len] = NUL;
2512
2513 // Check for user-defined variables.
2514 v = find_var(name, NULL, no_autoload);
2515 if (v != NULL)
2516 {
2517 tv = &v->di_tv;
2518 if (dip != NULL)
2519 *dip = v;
2520 }
2521
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002522 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002524 imported_T *import;
2525 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2526
2527 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528
2529 // imported variable from another script
2530 if (import != NULL)
2531 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002532 if (import->imp_funcname != NULL)
2533 {
2534 foundFunc = TRUE;
2535 if (rettv != NULL)
2536 {
2537 rettv->v_type = VAR_FUNC;
2538 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2539 }
2540 }
Bram Moolenaara6294952020-12-27 13:39:50 +01002541 else if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002542 {
2543 emsg("Sorry, 'import * as X' not implemented yet");
2544 ret = FAIL;
2545 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002546 else
2547 {
2548 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002549 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002551 tv = sv->sv_tv;
2552 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002554 else if (in_vim9script())
2555 {
2556 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2557
2558 if (ufunc != NULL)
2559 {
2560 foundFunc = TRUE;
2561 if (rettv != NULL)
2562 {
2563 rettv->v_type = VAR_FUNC;
2564 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2565 }
2566 }
2567 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 }
2569
Bram Moolenaarc620c052020-07-08 15:16:19 +02002570 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002571 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002572 if (tv == NULL)
2573 {
2574 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002575 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002576 ret = FAIL;
2577 }
2578 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002579 {
2580 // If a list or dict variable wasn't initialized, do it now.
2581 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2582 {
2583 tv->vval.v_dict = dict_alloc();
2584 if (tv->vval.v_dict != NULL)
2585 ++tv->vval.v_dict->dv_refcount;
2586 }
2587 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2588 {
2589 tv->vval.v_list = list_alloc();
2590 if (tv->vval.v_list != NULL)
2591 ++tv->vval.v_list->lv_refcount;
2592 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002593 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002594 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002595 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002596
2597 name[len] = cc;
2598
2599 return ret;
2600}
2601
2602/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002603 * Check if variable "name[len]" is a local variable or an argument.
2604 * If so, "*eval_lavars_used" is set to TRUE.
2605 */
2606 void
2607check_vars(char_u *name, int len)
2608{
2609 int cc;
2610 char_u *varname;
2611 hashtab_T *ht;
2612
2613 if (eval_lavars_used == NULL)
2614 return;
2615
2616 // truncate the name, so that we can use strcmp()
2617 cc = name[len];
2618 name[len] = NUL;
2619
2620 ht = find_var_ht(name, &varname);
2621 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2622 {
2623 if (find_var(name, NULL, TRUE) != NULL)
2624 *eval_lavars_used = TRUE;
2625 }
2626
2627 name[len] = cc;
2628}
2629
2630/*
2631 * Find variable "name" in the list of variables.
2632 * Return a pointer to it if found, NULL if not found.
2633 * Careful: "a:0" variables don't have a name.
2634 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2635 * hashtab_T used.
2636 */
2637 dictitem_T *
2638find_var(char_u *name, hashtab_T **htp, int no_autoload)
2639{
2640 char_u *varname;
2641 hashtab_T *ht;
2642 dictitem_T *ret = NULL;
2643
2644 ht = find_var_ht(name, &varname);
2645 if (htp != NULL)
2646 *htp = ht;
2647 if (ht == NULL)
2648 return NULL;
2649 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2650 if (ret != NULL)
2651 return ret;
2652
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002653 // Search in parent scope for lambda
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002654 ret = find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2655 if (ret != NULL)
2656 return ret;
2657
2658 // in Vim9 script items without a scope can be script-local
2659 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2660 {
2661 ht = get_script_local_ht();
2662 if (ht != NULL)
2663 {
2664 ret = find_var_in_ht(ht, *name, varname,
2665 no_autoload || htp != NULL);
2666 if (ret != NULL)
2667 {
2668 if (htp != NULL)
2669 *htp = ht;
2670 return ret;
2671 }
2672 }
2673 }
2674
2675 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002676}
2677
2678/*
2679 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002680 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002681 * Returns NULL if not found.
2682 */
2683 dictitem_T *
2684find_var_in_ht(
2685 hashtab_T *ht,
2686 int htname,
2687 char_u *varname,
2688 int no_autoload)
2689{
2690 hashitem_T *hi;
2691
2692 if (*varname == NUL)
2693 {
2694 // Must be something like "s:", otherwise "ht" would be NULL.
2695 switch (htname)
2696 {
2697 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2698 case 'g': return &globvars_var;
2699 case 'v': return &vimvars_var;
2700 case 'b': return &curbuf->b_bufvar;
2701 case 'w': return &curwin->w_winvar;
2702 case 't': return &curtab->tp_winvar;
2703 case 'l': return get_funccal_local_var();
2704 case 'a': return get_funccal_args_var();
2705 }
2706 return NULL;
2707 }
2708
2709 hi = hash_find(ht, varname);
2710 if (HASHITEM_EMPTY(hi))
2711 {
2712 // For global variables we may try auto-loading the script. If it
2713 // worked find the variable again. Don't auto-load a script if it was
2714 // loaded already, otherwise it would be loaded every time when
2715 // checking if a function name is a Funcref variable.
2716 if (ht == &globvarht && !no_autoload)
2717 {
2718 // Note: script_autoload() may make "hi" invalid. It must either
2719 // be obtained again or not used.
2720 if (!script_autoload(varname, FALSE) || aborting())
2721 return NULL;
2722 hi = hash_find(ht, varname);
2723 }
2724 if (HASHITEM_EMPTY(hi))
2725 return NULL;
2726 }
2727 return HI2DI(hi);
2728}
2729
2730/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002731 * Get the script-local hashtab. NULL if not in a script context.
2732 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002733 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734get_script_local_ht(void)
2735{
2736 scid_T sid = current_sctx.sc_sid;
2737
Bram Moolenaare3d46852020-08-29 13:39:17 +02002738 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002739 return &SCRIPT_VARS(sid);
2740 return NULL;
2741}
2742
2743/*
2744 * Look for "name[len]" in script-local variables.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002745 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01002747 int
2748lookup_scriptvar(
2749 char_u *name,
2750 size_t len,
2751 void *lvar UNUSED,
2752 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753{
2754 hashtab_T *ht = get_script_local_ht();
2755 char_u buffer[30];
2756 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01002757 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 hashitem_T *hi;
2759
2760 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002761 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 if (len < sizeof(buffer) - 1)
2763 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002764 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 vim_strncpy(buffer, name, len);
2766 p = buffer;
2767 }
2768 else
2769 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002770 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002772 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 }
2774
2775 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002776 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777
2778 // if not script-local, then perhaps imported
Bram Moolenaar709664c2020-12-12 14:33:41 +01002779 if (res == FAIL && find_imported(p, 0, NULL) != NULL)
2780 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781
2782 if (p != buffer)
2783 vim_free(p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002784 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785}
2786
2787/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002788 * Find the hashtab used for a variable name.
2789 * Return NULL if the name is not valid.
2790 * Set "varname" to the start of name without ':'.
2791 */
2792 hashtab_T *
2793find_var_ht(char_u *name, char_u **varname)
2794{
2795 hashitem_T *hi;
2796 hashtab_T *ht;
2797
2798 if (name[0] == NUL)
2799 return NULL;
2800 if (name[1] != ':')
2801 {
2802 // The name must not start with a colon or #.
2803 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2804 return NULL;
2805 *varname = name;
2806
2807 // "version" is "v:version" in all scopes if scriptversion < 3.
2808 // Same for a few other variables marked with VV_COMPAT.
2809 if (current_sctx.sc_version < 3)
2810 {
2811 hi = hash_find(&compat_hashtab, name);
2812 if (!HASHITEM_EMPTY(hi))
2813 return &compat_hashtab;
2814 }
2815
2816 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817 if (ht != NULL)
2818 return ht; // local variable
2819
2820 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002821 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002822 {
2823 ht = get_script_local_ht();
2824 if (ht != NULL)
2825 return ht;
2826 }
2827
2828 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002829 }
2830 *varname = name + 2;
2831 if (*name == 'g') // global variable
2832 return &globvarht;
2833 // There must be no ':' or '#' in the rest of the name, unless g: is used
2834 if (vim_strchr(name + 2, ':') != NULL
2835 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2836 return NULL;
2837 if (*name == 'b') // buffer variable
2838 return &curbuf->b_vars->dv_hashtab;
2839 if (*name == 'w') // window variable
2840 return &curwin->w_vars->dv_hashtab;
2841 if (*name == 't') // tab page variable
2842 return &curtab->tp_vars->dv_hashtab;
2843 if (*name == 'v') // v: variable
2844 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002845 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002846 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002848 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 if (*name == 'a') // a: function argument
2850 return get_funccal_args_ht();
2851 if (*name == 'l') // l: local function variable
2852 return get_funccal_local_ht();
2853 }
2854 if (*name == 's') // script variable
2855 {
2856 ht = get_script_local_ht();
2857 if (ht != NULL)
2858 return ht;
2859 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002860 return NULL;
2861}
2862
2863/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002864 * Get the string value of a (global/local) variable.
2865 * Note: see tv_get_string() for how long the pointer remains valid.
2866 * Returns NULL when it doesn't exist.
2867 */
2868 char_u *
2869get_var_value(char_u *name)
2870{
2871 dictitem_T *v;
2872
2873 v = find_var(name, NULL, FALSE);
2874 if (v == NULL)
2875 return NULL;
2876 return tv_get_string(&v->di_tv);
2877}
2878
2879/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002880 * Allocate a new hashtab for a sourced script. It will be used while
2881 * sourcing this script and when executing functions defined in the script.
2882 */
2883 void
2884new_script_vars(scid_T id)
2885{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002886 scriptvar_T *sv;
2887
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002888 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2889 if (sv == NULL)
2890 return;
2891 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002892 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002893}
2894
2895/*
2896 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2897 * point to it.
2898 */
2899 void
2900init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2901{
2902 hash_init(&dict->dv_hashtab);
2903 dict->dv_lock = 0;
2904 dict->dv_scope = scope;
2905 dict->dv_refcount = DO_NOT_FREE_CNT;
2906 dict->dv_copyID = 0;
2907 dict_var->di_tv.vval.v_dict = dict;
2908 dict_var->di_tv.v_type = VAR_DICT;
2909 dict_var->di_tv.v_lock = VAR_FIXED;
2910 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2911 dict_var->di_key[0] = NUL;
2912}
2913
2914/*
2915 * Unreference a dictionary initialized by init_var_dict().
2916 */
2917 void
2918unref_var_dict(dict_T *dict)
2919{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002920 // Now the dict needs to be freed if no one else is using it, go back to
2921 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002922 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2923 dict_unref(dict);
2924}
2925
2926/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002927 * Clean up a list of internal variables.
2928 * Frees all allocated variables and the value they contain.
2929 * Clears hashtab "ht", does not free it.
2930 */
2931 void
2932vars_clear(hashtab_T *ht)
2933{
2934 vars_clear_ext(ht, TRUE);
2935}
2936
2937/*
2938 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2939 */
2940 void
2941vars_clear_ext(hashtab_T *ht, int free_val)
2942{
2943 int todo;
2944 hashitem_T *hi;
2945 dictitem_T *v;
2946
2947 hash_lock(ht);
2948 todo = (int)ht->ht_used;
2949 for (hi = ht->ht_array; todo > 0; ++hi)
2950 {
2951 if (!HASHITEM_EMPTY(hi))
2952 {
2953 --todo;
2954
2955 // Free the variable. Don't remove it from the hashtab,
2956 // ht_array might change then. hash_clear() takes care of it
2957 // later.
2958 v = HI2DI(hi);
2959 if (free_val)
2960 clear_tv(&v->di_tv);
2961 if (v->di_flags & DI_FLAGS_ALLOC)
2962 vim_free(v);
2963 }
2964 }
2965 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02002966 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002967}
2968
2969/*
2970 * Delete a variable from hashtab "ht" at item "hi".
2971 * Clear the variable value and free the dictitem.
2972 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02002973 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002974delete_var(hashtab_T *ht, hashitem_T *hi)
2975{
2976 dictitem_T *di = HI2DI(hi);
2977
2978 hash_remove(ht, hi);
2979 clear_tv(&di->di_tv);
2980 vim_free(di);
2981}
2982
2983/*
2984 * List the value of one internal variable.
2985 */
2986 static void
2987list_one_var(dictitem_T *v, char *prefix, int *first)
2988{
2989 char_u *tofree;
2990 char_u *s;
2991 char_u numbuf[NUMBUFLEN];
2992
2993 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2994 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2995 s == NULL ? (char_u *)"" : s, first);
2996 vim_free(tofree);
2997}
2998
2999 static void
3000list_one_var_a(
3001 char *prefix,
3002 char_u *name,
3003 int type,
3004 char_u *string,
3005 int *first) // when TRUE clear rest of screen and set to FALSE
3006{
3007 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3008 msg_start();
3009 msg_puts(prefix);
3010 if (name != NULL) // "a:" vars don't have a name stored
3011 msg_puts((char *)name);
3012 msg_putchar(' ');
3013 msg_advance(22);
3014 if (type == VAR_NUMBER)
3015 msg_putchar('#');
3016 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3017 msg_putchar('*');
3018 else if (type == VAR_LIST)
3019 {
3020 msg_putchar('[');
3021 if (*string == '[')
3022 ++string;
3023 }
3024 else if (type == VAR_DICT)
3025 {
3026 msg_putchar('{');
3027 if (*string == '{')
3028 ++string;
3029 }
3030 else
3031 msg_putchar(' ');
3032
3033 msg_outtrans(string);
3034
3035 if (type == VAR_FUNC || type == VAR_PARTIAL)
3036 msg_puts("()");
3037 if (*first)
3038 {
3039 msg_clr_eos();
3040 *first = FALSE;
3041 }
3042}
3043
3044/*
3045 * Set variable "name" to value in "tv".
3046 * If the variable already exists, the value is updated.
3047 * Otherwise the variable is created.
3048 */
3049 void
3050set_var(
3051 char_u *name,
3052 typval_T *tv,
3053 int copy) // make copy of value in "tv"
3054{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003055 set_var_const(name, NULL, tv, copy, ASSIGN_NO_DECL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003056}
3057
3058/*
3059 * Set variable "name" to value in "tv".
3060 * If the variable already exists and "is_const" is FALSE the value is updated.
3061 * Otherwise the variable is created.
3062 */
3063 void
3064set_var_const(
3065 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003067 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003068 int copy, // make copy of value in "tv"
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003069 int flags) // ASSIGN_CONST, ASSIGN_FINAL, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003070{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003071 typval_T *tv = tv_arg;
3072 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003074 char_u *varname;
3075 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003077 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003078
3079 ht = find_var_ht(name, &varname);
3080 if (ht == NULL || *varname == NUL)
3081 {
3082 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003083 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003084 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003085 is_script_local = ht == get_script_local_ht();
3086
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003087 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003088 && !is_script_local
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003089 && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003090 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003091 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003092 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003093 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003094 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003095 }
3096
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003098
3099 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 if (di == NULL)
3101 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003102
3103 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003104 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003105 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003106
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003107 if (need_convert_to_bool(type, tv))
3108 {
3109 // Destination is a bool and the value is not, but it can be converted.
3110 CLEAR_FIELD(bool_tv);
3111 bool_tv.v_type = VAR_BOOL;
3112 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3113 tv = &bool_tv;
3114 }
3115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003117 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003119 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003120 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 {
3122 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003123 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124 }
3125
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003126 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003128 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003129 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003130 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003131 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003132 }
3133
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003134 // check the type and adjust to bool if needed
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003135 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003136 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003138
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003139 if (var_check_permission(di, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003140 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003141 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 else
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003143 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 // can only redefine once
3145 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003146
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003147 // A Vim9 script-local variable is also present in sn_all_vars and
3148 // sn_var_vals.
3149 if (is_script_local && vim9script)
3150 update_vim9_script_var(FALSE, di, tv, type);
3151 }
3152
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003153 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003156 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003157 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003158 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003160 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003162 if (copy || tv->v_type != VAR_STRING)
3163 {
3164 char_u *val = tv_get_string(tv);
3165
3166 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003167 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168 if (di->di_tv.vval.v_string == NULL)
3169 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003170 }
3171 else
3172 {
3173 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003175 tv->vval.v_string = NULL;
3176 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003177 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003180 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003182 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003184#ifdef FEAT_SEARCH_EXTRA
3185 else if (STRCMP(varname, "hlsearch") == 0)
3186 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003188 redraw_all_later(SOME_VALID);
3189 }
3190#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003191 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003192 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003194 {
3195 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003196 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003197 }
3198 }
3199
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003201 }
3202 else // add a new variable
3203 {
3204 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003205 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003206 {
3207 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003208 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003209 }
3210
Bram Moolenaar03290b82020-12-19 16:30:44 +01003211 // Make sure the variable name is valid. In Vim9 script an autoload
3212 // variable must be prefixed with "g:".
3213 if (!valid_varname(varname, !vim9script
3214 || STRNCMP(name, "g:", 2) == 0))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003215 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003216
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3218 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003219 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 STRCPY(di->di_key, varname);
3221 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003222 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003224 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003225 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003227 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 di->di_flags |= DI_FLAGS_LOCK;
3229
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003230 // A Vim9 script-local variable is also added to sn_all_vars and
3231 // sn_var_vals.
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003232 if (is_script_local && vim9script)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003233 update_vim9_script_var(TRUE, di, tv, type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003234 }
3235
3236 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003238 else
3239 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 di->di_tv = *tv;
3241 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003242 init_tv(tv);
3243 }
3244
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003245 // ":const var = value" locks the value
3246 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003247 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003248 // Like :lockvar! name: lock the value and what it contains, but only
3249 // if the reference count is up to one. That locks only literal
3250 // values.
3251 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003252 return;
3253failed:
3254 if (!copy)
3255 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003256}
3257
3258/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003259 * Check in this order for backwards compatibility:
3260 * - Whether the variable is read-only
3261 * - Whether the variable value is locked
3262 * - Whether the variable is locked
3263 */
3264 int
3265var_check_permission(dictitem_T *di, char_u *name)
3266{
3267 if (var_check_ro(di->di_flags, name, FALSE)
3268 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3269 || var_check_lock(di->di_flags, name, FALSE))
3270 return FAIL;
3271 return OK;
3272}
3273
3274/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003275 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3276 * Also give an error message.
3277 */
3278 int
3279var_check_ro(int flags, char_u *name, int use_gettext)
3280{
3281 if (flags & DI_FLAGS_RO)
3282 {
3283 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3284 return TRUE;
3285 }
3286 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3287 {
3288 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3289 return TRUE;
3290 }
3291 return FALSE;
3292}
3293
3294/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003295 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3296 * Also give an error message.
3297 */
3298 int
3299var_check_lock(int flags, char_u *name, int use_gettext)
3300{
3301 if (flags & DI_FLAGS_LOCK)
3302 {
3303 semsg(_(e_variable_is_locked_str),
3304 use_gettext ? (char_u *)_(name) : name);
3305 return TRUE;
3306 }
3307 return FALSE;
3308}
3309
3310/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003311 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3312 * Also give an error message.
3313 */
3314 int
3315var_check_fixed(int flags, char_u *name, int use_gettext)
3316{
3317 if (flags & DI_FLAGS_FIX)
3318 {
3319 semsg(_("E795: Cannot delete variable %s"),
3320 use_gettext ? (char_u *)_(name) : name);
3321 return TRUE;
3322 }
3323 return FALSE;
3324}
3325
3326/*
3327 * Check if a funcref is assigned to a valid variable name.
3328 * Return TRUE and give an error if not.
3329 */
3330 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003331var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003332 char_u *name, // points to start of variable name
3333 int new_var) // TRUE when creating the variable
3334{
3335 // Allow for w: b: s: and t:.
3336 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3337 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3338 ? name[2] : name[0]))
3339 {
3340 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3341 name);
3342 return TRUE;
3343 }
3344 // Don't allow hiding a function. When "v" is not NULL we might be
3345 // assigning another function to the same var, the type is checked
3346 // below.
3347 if (new_var && function_exists(name, FALSE))
3348 {
3349 semsg(_("E705: Variable name conflicts with existing function: %s"),
3350 name);
3351 return TRUE;
3352 }
3353 return FALSE;
3354}
3355
3356/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003357 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3358 * value. Also give an error message, using "name" or _("name") when
3359 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003360 */
3361 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003362value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003363{
3364 if (lock & VAR_LOCKED)
3365 {
3366 semsg(_("E741: Value is locked: %s"),
3367 name == NULL ? (char_u *)_("Unknown")
3368 : use_gettext ? (char_u *)_(name)
3369 : name);
3370 return TRUE;
3371 }
3372 if (lock & VAR_FIXED)
3373 {
3374 semsg(_("E742: Cannot change value of %s"),
3375 name == NULL ? (char_u *)_("Unknown")
3376 : use_gettext ? (char_u *)_(name)
3377 : name);
3378 return TRUE;
3379 }
3380 return FALSE;
3381}
3382
3383/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003384 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003385 * Return FALSE and give an error if not.
3386 */
3387 int
Bram Moolenaar03290b82020-12-19 16:30:44 +01003388valid_varname(char_u *varname, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003389{
3390 char_u *p;
3391
3392 for (p = varname; *p != NUL; ++p)
3393 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003394 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003395 {
3396 semsg(_(e_illvar), varname);
3397 return FALSE;
3398 }
3399 return TRUE;
3400}
3401
3402/*
3403 * getwinvar() and gettabwinvar()
3404 */
3405 static void
3406getwinvar(
3407 typval_T *argvars,
3408 typval_T *rettv,
3409 int off) // 1 for gettabwinvar()
3410{
3411 win_T *win;
3412 char_u *varname;
3413 dictitem_T *v;
3414 tabpage_T *tp = NULL;
3415 int done = FALSE;
3416 win_T *oldcurwin;
3417 tabpage_T *oldtabpage;
3418 int need_switch_win;
3419
3420 if (off == 1)
3421 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3422 else
3423 tp = curtab;
3424 win = find_win_by_nr(&argvars[off], tp);
3425 varname = tv_get_string_chk(&argvars[off + 1]);
3426 ++emsg_off;
3427
3428 rettv->v_type = VAR_STRING;
3429 rettv->vval.v_string = NULL;
3430
3431 if (win != NULL && varname != NULL)
3432 {
3433 // Set curwin to be our win, temporarily. Also set the tabpage,
3434 // otherwise the window is not valid. Only do this when needed,
3435 // autocommands get blocked.
3436 need_switch_win = !(tp == curtab && win == curwin);
3437 if (!need_switch_win
3438 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3439 {
3440 if (*varname == '&')
3441 {
3442 if (varname[1] == NUL)
3443 {
3444 // get all window-local options in a dict
3445 dict_T *opts = get_winbuf_options(FALSE);
3446
3447 if (opts != NULL)
3448 {
3449 rettv_dict_set(rettv, opts);
3450 done = TRUE;
3451 }
3452 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003453 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003454 // window-local-option
3455 done = TRUE;
3456 }
3457 else
3458 {
3459 // Look up the variable.
3460 // Let getwinvar({nr}, "") return the "w:" dictionary.
3461 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3462 varname, FALSE);
3463 if (v != NULL)
3464 {
3465 copy_tv(&v->di_tv, rettv);
3466 done = TRUE;
3467 }
3468 }
3469 }
3470
3471 if (need_switch_win)
3472 // restore previous notion of curwin
3473 restore_win(oldcurwin, oldtabpage, TRUE);
3474 }
3475
3476 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3477 // use the default return value
3478 copy_tv(&argvars[off + 2], rettv);
3479
3480 --emsg_off;
3481}
3482
3483/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003484 * Set option "varname" to the value of "varp" for the current buffer/window.
3485 */
3486 static void
3487set_option_from_tv(char_u *varname, typval_T *varp)
3488{
3489 long numval = 0;
3490 char_u *strval;
3491 char_u nbuf[NUMBUFLEN];
3492 int error = FALSE;
3493
3494 if (!in_vim9script() || varp->v_type != VAR_STRING)
3495 numval = (long)tv_get_number_chk(varp, &error);
3496 strval = tv_get_string_buf_chk(varp, nbuf);
3497 if (!error && strval != NULL)
3498 set_option_value(varname, numval, strval, OPT_LOCAL);
3499}
3500
3501/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003502 * "setwinvar()" and "settabwinvar()" functions
3503 */
3504 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003505setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003506{
3507 win_T *win;
3508 win_T *save_curwin;
3509 tabpage_T *save_curtab;
3510 int need_switch_win;
3511 char_u *varname, *winvarname;
3512 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003513 tabpage_T *tp = NULL;
3514
3515 if (check_secure())
3516 return;
3517
3518 if (off == 1)
3519 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3520 else
3521 tp = curtab;
3522 win = find_win_by_nr(&argvars[off], tp);
3523 varname = tv_get_string_chk(&argvars[off + 1]);
3524 varp = &argvars[off + 2];
3525
3526 if (win != NULL && varname != NULL && varp != NULL)
3527 {
3528 need_switch_win = !(tp == curtab && win == curwin);
3529 if (!need_switch_win
3530 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3531 {
3532 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003533 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003534 else
3535 {
3536 winvarname = alloc(STRLEN(varname) + 3);
3537 if (winvarname != NULL)
3538 {
3539 STRCPY(winvarname, "w:");
3540 STRCPY(winvarname + 2, varname);
3541 set_var(winvarname, varp, TRUE);
3542 vim_free(winvarname);
3543 }
3544 }
3545 }
3546 if (need_switch_win)
3547 restore_win(save_curwin, save_curtab, TRUE);
3548 }
3549}
3550
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003551/*
3552 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3553 * v:option_type, and v:option_command.
3554 */
3555 void
3556reset_v_option_vars(void)
3557{
3558 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3559 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3560 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3561 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3562 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3563 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3564}
3565
3566/*
3567 * Add an assert error to v:errors.
3568 */
3569 void
3570assert_error(garray_T *gap)
3571{
3572 struct vimvar *vp = &vimvars[VV_ERRORS];
3573
3574 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003575 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003576 set_vim_var_list(VV_ERRORS, list_alloc());
3577 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3578}
3579
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003580 int
3581var_exists(char_u *var)
3582{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003583 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003584 char_u *name;
3585 char_u *tofree;
3586 typval_T tv;
3587 int len = 0;
3588 int n = FALSE;
3589
3590 // get_name_len() takes care of expanding curly braces
3591 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003592 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003593 if (len > 0)
3594 {
3595 if (tofree != NULL)
3596 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003597 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003598 if (n)
3599 {
3600 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003601 arg = skipwhite(arg);
3602 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003603 if (n)
3604 clear_tv(&tv);
3605 }
3606 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003607 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003608 n = FALSE;
3609
3610 vim_free(tofree);
3611 return n;
3612}
3613
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003614static lval_T *redir_lval = NULL;
3615#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3616static garray_T redir_ga; // only valid when redir_lval is not NULL
3617static char_u *redir_endp = NULL;
3618static char_u *redir_varname = NULL;
3619
3620/*
3621 * Start recording command output to a variable
3622 * When "append" is TRUE append to an existing variable.
3623 * Returns OK if successfully completed the setup. FAIL otherwise.
3624 */
3625 int
3626var_redir_start(char_u *name, int append)
3627{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003628 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003629 typval_T tv;
3630
3631 // Catch a bad name early.
3632 if (!eval_isnamec1(*name))
3633 {
3634 emsg(_(e_invarg));
3635 return FAIL;
3636 }
3637
3638 // Make a copy of the name, it is used in redir_lval until redir ends.
3639 redir_varname = vim_strsave(name);
3640 if (redir_varname == NULL)
3641 return FAIL;
3642
3643 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3644 if (redir_lval == NULL)
3645 {
3646 var_redir_stop();
3647 return FAIL;
3648 }
3649
3650 // The output is stored in growarray "redir_ga" until redirection ends.
3651 ga_init2(&redir_ga, (int)sizeof(char), 500);
3652
3653 // Parse the variable name (can be a dict or list entry).
3654 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3655 FNE_CHECK_START);
3656 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3657 {
3658 clear_lval(redir_lval);
3659 if (redir_endp != NULL && *redir_endp != NUL)
3660 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003661 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003662 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003663 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003664 redir_endp = NULL; // don't store a value, only cleanup
3665 var_redir_stop();
3666 return FAIL;
3667 }
3668
3669 // check if we can write to the variable: set it to or append an empty
3670 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003671 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003672 tv.v_type = VAR_STRING;
3673 tv.vval.v_string = (char_u *)"";
3674 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003675 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3676 ASSIGN_NO_DECL, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003677 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003678 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3679 ASSIGN_NO_DECL, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003680 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003681 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003682 {
3683 redir_endp = NULL; // don't store a value, only cleanup
3684 var_redir_stop();
3685 return FAIL;
3686 }
3687
3688 return OK;
3689}
3690
3691/*
3692 * Append "value[value_len]" to the variable set by var_redir_start().
3693 * The actual appending is postponed until redirection ends, because the value
3694 * appended may in fact be the string we write to, changing it may cause freed
3695 * memory to be used:
3696 * :redir => foo
3697 * :let foo
3698 * :redir END
3699 */
3700 void
3701var_redir_str(char_u *value, int value_len)
3702{
3703 int len;
3704
3705 if (redir_lval == NULL)
3706 return;
3707
3708 if (value_len == -1)
3709 len = (int)STRLEN(value); // Append the entire string
3710 else
3711 len = value_len; // Append only "value_len" characters
3712
3713 if (ga_grow(&redir_ga, len) == OK)
3714 {
3715 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3716 redir_ga.ga_len += len;
3717 }
3718 else
3719 var_redir_stop();
3720}
3721
3722/*
3723 * Stop redirecting command output to a variable.
3724 * Frees the allocated memory.
3725 */
3726 void
3727var_redir_stop(void)
3728{
3729 typval_T tv;
3730
3731 if (EVALCMD_BUSY)
3732 {
3733 redir_lval = NULL;
3734 return;
3735 }
3736
3737 if (redir_lval != NULL)
3738 {
3739 // If there was no error: assign the text to the variable.
3740 if (redir_endp != NULL)
3741 {
3742 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3743 tv.v_type = VAR_STRING;
3744 tv.vval.v_string = redir_ga.ga_data;
3745 // Call get_lval() again, if it's inside a Dict or List it may
3746 // have changed.
3747 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3748 FALSE, FALSE, 0, FNE_CHECK_START);
3749 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003750 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003751 (char_u *)".");
3752 clear_lval(redir_lval);
3753 }
3754
3755 // free the collected output
3756 VIM_CLEAR(redir_ga.ga_data);
3757
3758 VIM_CLEAR(redir_lval);
3759 }
3760 VIM_CLEAR(redir_varname);
3761}
3762
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003763/*
3764 * "gettabvar()" function
3765 */
3766 void
3767f_gettabvar(typval_T *argvars, typval_T *rettv)
3768{
3769 win_T *oldcurwin;
3770 tabpage_T *tp, *oldtabpage;
3771 dictitem_T *v;
3772 char_u *varname;
3773 int done = FALSE;
3774
3775 rettv->v_type = VAR_STRING;
3776 rettv->vval.v_string = NULL;
3777
3778 varname = tv_get_string_chk(&argvars[1]);
3779 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3780 if (tp != NULL && varname != NULL)
3781 {
3782 // Set tp to be our tabpage, temporarily. Also set the window to the
3783 // first window in the tabpage, otherwise the window is not valid.
3784 if (switch_win(&oldcurwin, &oldtabpage,
3785 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3786 : tp->tp_firstwin, tp, TRUE) == OK)
3787 {
3788 // look up the variable
3789 // Let gettabvar({nr}, "") return the "t:" dictionary.
3790 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3791 if (v != NULL)
3792 {
3793 copy_tv(&v->di_tv, rettv);
3794 done = TRUE;
3795 }
3796 }
3797
3798 // restore previous notion of curwin
3799 restore_win(oldcurwin, oldtabpage, TRUE);
3800 }
3801
3802 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3803 copy_tv(&argvars[2], rettv);
3804}
3805
3806/*
3807 * "gettabwinvar()" function
3808 */
3809 void
3810f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3811{
3812 getwinvar(argvars, rettv, 1);
3813}
3814
3815/*
3816 * "getwinvar()" function
3817 */
3818 void
3819f_getwinvar(typval_T *argvars, typval_T *rettv)
3820{
3821 getwinvar(argvars, rettv, 0);
3822}
3823
3824/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003825 * "getbufvar()" function
3826 */
3827 void
3828f_getbufvar(typval_T *argvars, typval_T *rettv)
3829{
3830 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003831 char_u *varname;
3832 dictitem_T *v;
3833 int done = FALSE;
3834
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003835 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003836 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003837
3838 rettv->v_type = VAR_STRING;
3839 rettv->vval.v_string = NULL;
3840
3841 if (buf != NULL && varname != NULL)
3842 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003843 if (*varname == '&')
3844 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003845 buf_T *save_curbuf = curbuf;
3846
3847 // set curbuf to be our buf, temporarily
3848 curbuf = buf;
3849
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003850 if (varname[1] == NUL)
3851 {
3852 // get all buffer-local options in a dict
3853 dict_T *opts = get_winbuf_options(TRUE);
3854
3855 if (opts != NULL)
3856 {
3857 rettv_dict_set(rettv, opts);
3858 done = TRUE;
3859 }
3860 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003861 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003862 // buffer-local-option
3863 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003864
3865 // restore previous notion of curbuf
3866 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003867 }
3868 else
3869 {
3870 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003871 if (*varname == NUL)
3872 // Let getbufvar({nr}, "") return the "b:" dictionary.
3873 v = &buf->b_bufvar;
3874 else
3875 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3876 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003877 if (v != NULL)
3878 {
3879 copy_tv(&v->di_tv, rettv);
3880 done = TRUE;
3881 }
3882 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003883 }
3884
3885 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3886 // use the default value
3887 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003888}
3889
3890/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003891 * "settabvar()" function
3892 */
3893 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003894f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003895{
3896 tabpage_T *save_curtab;
3897 tabpage_T *tp;
3898 char_u *varname, *tabvarname;
3899 typval_T *varp;
3900
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003901 if (check_secure())
3902 return;
3903
3904 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3905 varname = tv_get_string_chk(&argvars[1]);
3906 varp = &argvars[2];
3907
3908 if (varname != NULL && varp != NULL && tp != NULL)
3909 {
3910 save_curtab = curtab;
3911 goto_tabpage_tp(tp, FALSE, FALSE);
3912
3913 tabvarname = alloc(STRLEN(varname) + 3);
3914 if (tabvarname != NULL)
3915 {
3916 STRCPY(tabvarname, "t:");
3917 STRCPY(tabvarname + 2, varname);
3918 set_var(tabvarname, varp, TRUE);
3919 vim_free(tabvarname);
3920 }
3921
3922 // Restore current tabpage
3923 if (valid_tabpage(save_curtab))
3924 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3925 }
3926}
3927
3928/*
3929 * "settabwinvar()" function
3930 */
3931 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003932f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003933{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003934 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003935}
3936
3937/*
3938 * "setwinvar()" function
3939 */
3940 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003941f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003942{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003943 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003944}
3945
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003946/*
3947 * "setbufvar()" function
3948 */
3949 void
3950f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3951{
3952 buf_T *buf;
3953 char_u *varname, *bufvarname;
3954 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003955
3956 if (check_secure())
3957 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003958 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003959 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003960 varp = &argvars[2];
3961
3962 if (buf != NULL && varname != NULL && varp != NULL)
3963 {
3964 if (*varname == '&')
3965 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003966 aco_save_T aco;
3967
3968 // set curbuf to be our buf, temporarily
3969 aucmd_prepbuf(&aco, buf);
3970
Bram Moolenaar191929b2020-08-19 21:20:49 +02003971 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003972
3973 // reset notion of buffer
3974 aucmd_restbuf(&aco);
3975 }
3976 else
3977 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003978 bufvarname = alloc(STRLEN(varname) + 3);
3979 if (bufvarname != NULL)
3980 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003981 buf_T *save_curbuf = curbuf;
3982
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003983 curbuf = buf;
3984 STRCPY(bufvarname, "b:");
3985 STRCPY(bufvarname + 2, varname);
3986 set_var(bufvarname, varp, TRUE);
3987 vim_free(bufvarname);
3988 curbuf = save_curbuf;
3989 }
3990 }
3991 }
3992}
3993
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003994/*
3995 * Get a callback from "arg". It can be a Funcref or a function name.
3996 * When "arg" is zero return an empty string.
3997 * "cb_name" is not allocated.
3998 * "cb_name" is set to NULL for an invalid argument.
3999 */
4000 callback_T
4001get_callback(typval_T *arg)
4002{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004003 callback_T res;
4004 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004005
4006 res.cb_free_name = FALSE;
4007 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4008 {
4009 res.cb_partial = arg->vval.v_partial;
4010 ++res.cb_partial->pt_refcount;
4011 res.cb_name = partial_name(res.cb_partial);
4012 }
4013 else
4014 {
4015 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004016 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4017 && isdigit(*arg->vval.v_string))
4018 r = FAIL;
4019 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004020 {
4021 // Note that we don't make a copy of the string.
4022 res.cb_name = arg->vval.v_string;
4023 func_ref(res.cb_name);
4024 }
4025 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004026 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004027 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004028 r = FAIL;
4029
4030 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004031 {
4032 emsg(_("E921: Invalid callback argument"));
4033 res.cb_name = NULL;
4034 }
4035 }
4036 return res;
4037}
4038
4039/*
4040 * Copy a callback into a typval_T.
4041 */
4042 void
4043put_callback(callback_T *cb, typval_T *tv)
4044{
4045 if (cb->cb_partial != NULL)
4046 {
4047 tv->v_type = VAR_PARTIAL;
4048 tv->vval.v_partial = cb->cb_partial;
4049 ++tv->vval.v_partial->pt_refcount;
4050 }
4051 else
4052 {
4053 tv->v_type = VAR_FUNC;
4054 tv->vval.v_string = vim_strsave(cb->cb_name);
4055 func_ref(cb->cb_name);
4056 }
4057}
4058
4059/*
4060 * Make a copy of "src" into "dest", allocating the function name if needed,
4061 * without incrementing the refcount.
4062 */
4063 void
4064set_callback(callback_T *dest, callback_T *src)
4065{
4066 if (src->cb_partial == NULL)
4067 {
4068 // just a function name, make a copy
4069 dest->cb_name = vim_strsave(src->cb_name);
4070 dest->cb_free_name = TRUE;
4071 }
4072 else
4073 {
4074 // cb_name is a pointer into cb_partial
4075 dest->cb_name = src->cb_name;
4076 dest->cb_free_name = FALSE;
4077 }
4078 dest->cb_partial = src->cb_partial;
4079}
4080
4081/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004082 * Copy callback from "src" to "dest", incrementing the refcounts.
4083 */
4084 void
4085copy_callback(callback_T *dest, callback_T *src)
4086{
4087 dest->cb_partial = src->cb_partial;
4088 if (dest->cb_partial != NULL)
4089 {
4090 dest->cb_name = src->cb_name;
4091 dest->cb_free_name = FALSE;
4092 ++dest->cb_partial->pt_refcount;
4093 }
4094 else
4095 {
4096 dest->cb_name = vim_strsave(src->cb_name);
4097 dest->cb_free_name = TRUE;
4098 func_ref(src->cb_name);
4099 }
4100}
4101
4102/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004103 * Unref/free "callback" returned by get_callback() or set_callback().
4104 */
4105 void
4106free_callback(callback_T *callback)
4107{
4108 if (callback->cb_partial != NULL)
4109 {
4110 partial_unref(callback->cb_partial);
4111 callback->cb_partial = NULL;
4112 }
4113 else if (callback->cb_name != NULL)
4114 func_unref(callback->cb_name);
4115 if (callback->cb_free_name)
4116 {
4117 vim_free(callback->cb_name);
4118 callback->cb_free_name = FALSE;
4119 }
4120 callback->cb_name = NULL;
4121}
4122
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004123#endif // FEAT_EVAL