blob: 94b857ed9bcfe2cf2d1753ccecee844c331b80bd [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
48 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
49} vimvars[VV_LEN] =
50{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020051 // The order here must match the VV_ defines in vim.h!
52 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaare5cdf152019-08-29 22:09:46 +020053 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
54 {VV_NAME("count1", VAR_NUMBER), VV_RO},
55 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
56 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
57 {VV_NAME("warningmsg", VAR_STRING), 0},
58 {VV_NAME("statusmsg", VAR_STRING), 0},
59 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
60 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
61 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
62 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
63 {VV_NAME("termresponse", VAR_STRING), VV_RO},
64 {VV_NAME("fname", VAR_STRING), VV_RO},
65 {VV_NAME("lang", VAR_STRING), VV_RO},
66 {VV_NAME("lc_time", VAR_STRING), VV_RO},
67 {VV_NAME("ctype", VAR_STRING), VV_RO},
68 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
69 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
70 {VV_NAME("fname_in", VAR_STRING), VV_RO},
71 {VV_NAME("fname_out", VAR_STRING), VV_RO},
72 {VV_NAME("fname_new", VAR_STRING), VV_RO},
73 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
74 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
75 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
76 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
77 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
78 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
79 {VV_NAME("progname", VAR_STRING), VV_RO},
80 {VV_NAME("servername", VAR_STRING), VV_RO},
81 {VV_NAME("dying", VAR_NUMBER), VV_RO},
82 {VV_NAME("exception", VAR_STRING), VV_RO},
83 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
84 {VV_NAME("register", VAR_STRING), VV_RO},
85 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
86 {VV_NAME("insertmode", VAR_STRING), VV_RO},
87 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
88 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
89 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
90 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
91 {VV_NAME("fcs_choice", VAR_STRING), 0},
92 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
93 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
94 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
95 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
96 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
97 {VV_NAME("beval_text", VAR_STRING), VV_RO},
98 {VV_NAME("scrollstart", VAR_STRING), 0},
99 {VV_NAME("swapname", VAR_STRING), VV_RO},
100 {VV_NAME("swapchoice", VAR_STRING), 0},
101 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
102 {VV_NAME("char", VAR_STRING), 0},
103 {VV_NAME("mouse_win", VAR_NUMBER), 0},
104 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
105 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
106 {VV_NAME("mouse_col", VAR_NUMBER), 0},
107 {VV_NAME("operator", VAR_STRING), VV_RO},
108 {VV_NAME("searchforward", VAR_NUMBER), 0},
109 {VV_NAME("hlsearch", VAR_NUMBER), 0},
110 {VV_NAME("oldfiles", VAR_LIST), 0},
111 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
112 {VV_NAME("progpath", VAR_STRING), VV_RO},
113 {VV_NAME("completed_item", VAR_DICT), VV_RO},
114 {VV_NAME("option_new", VAR_STRING), VV_RO},
115 {VV_NAME("option_old", VAR_STRING), VV_RO},
116 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
117 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
118 {VV_NAME("option_command", VAR_STRING), VV_RO},
119 {VV_NAME("option_type", VAR_STRING), VV_RO},
120 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100121 {VV_NAME("false", VAR_BOOL), VV_RO},
122 {VV_NAME("true", VAR_BOOL), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200123 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100124 {VV_NAME("null", VAR_SPECIAL), VV_RO},
125 {VV_NAME("numbersize", VAR_NUMBER), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200126 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
127 {VV_NAME("testing", VAR_NUMBER), 0},
128 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
129 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
130 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
131 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
132 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
133 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
134 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
135 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
136 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
137 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
138 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
139 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
140 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
141 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
142 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
143 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
144 {VV_NAME("event", VAR_DICT), VV_RO},
145 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
146 {VV_NAME("echospace", VAR_NUMBER), VV_RO},
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100147 {VV_NAME("argv", VAR_LIST), VV_RO},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200148 {VV_NAME("collate", VAR_STRING), VV_RO},
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100149 {VV_NAME("exiting", VAR_SPECIAL), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200150};
151
152// shorthand
153#define vv_type vv_di.di_tv.v_type
154#define vv_nr vv_di.di_tv.vval.v_number
155#define vv_float vv_di.di_tv.vval.v_float
156#define vv_str vv_di.di_tv.vval.v_string
157#define vv_list vv_di.di_tv.vval.v_list
158#define vv_dict vv_di.di_tv.vval.v_dict
159#define vv_blob vv_di.di_tv.vval.v_blob
160#define vv_tv vv_di.di_tv
161
162static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200163static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200164#define vimvarht vimvardict.dv_hashtab
165
166// for VIM_VERSION_ defines
167#include "version.h"
168
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200169static void list_glob_vars(int *first);
170static void list_buf_vars(int *first);
171static void list_win_vars(int *first);
172static void list_tab_vars(int *first);
173static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100174static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200175static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
176static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200177static void list_one_var(dictitem_T *v, char *prefix, int *first);
178static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
179
180/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200181 * Initialize global and vim special variables
182 */
183 void
184evalvars_init(void)
185{
186 int i;
187 struct vimvar *p;
188
189 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
190 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
191 vimvardict.dv_lock = VAR_FIXED;
192 hash_init(&compat_hashtab);
193
194 for (i = 0; i < VV_LEN; ++i)
195 {
196 p = &vimvars[i];
197 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
198 {
199 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
200 getout(1);
201 }
202 STRCPY(p->vv_di.di_key, p->vv_name);
203 if (p->vv_flags & VV_RO)
204 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
205 else if (p->vv_flags & VV_RO_SBX)
206 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
207 else
208 p->vv_di.di_flags = DI_FLAGS_FIX;
209
210 // add to v: scope dict, unless the value is not always available
211 if (p->vv_type != VAR_UNKNOWN)
212 hash_add(&vimvarht, p->vv_di.di_key);
213 if (p->vv_flags & VV_COMPAT)
214 // add to compat scope dict
215 hash_add(&compat_hashtab, p->vv_di.di_key);
216 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200217 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
218 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200219
220 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
221 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100222 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200223 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
224 set_vim_var_list(VV_ERRORS, list_alloc());
225 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
226
227 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
228 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
229 set_vim_var_nr(VV_NONE, VVAL_NONE);
230 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100231 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200232
233 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
234 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
235 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
236 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
237 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
238 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
239 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
240 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
241 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
242 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
243 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
244
245 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
246
Bram Moolenaar439c0362020-06-06 15:58:03 +0200247 // Default for v:register is not 0 but '"'. This is adjusted once the
248 // clipboard has been setup by calling reset_reg_var().
249 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200250}
251
252#if defined(EXITFREE) || defined(PROTO)
253/*
254 * Free all vim variables information on exit
255 */
256 void
257evalvars_clear(void)
258{
259 int i;
260 struct vimvar *p;
261
262 for (i = 0; i < VV_LEN; ++i)
263 {
264 p = &vimvars[i];
265 if (p->vv_di.di_tv.v_type == VAR_STRING)
266 VIM_CLEAR(p->vv_str);
267 else if (p->vv_di.di_tv.v_type == VAR_LIST)
268 {
269 list_unref(p->vv_list);
270 p->vv_list = NULL;
271 }
272 }
273 hash_clear(&vimvarht);
274 hash_init(&vimvarht); // garbage_collect() will access it
275 hash_clear(&compat_hashtab);
276
277 // global variables
278 vars_clear(&globvarht);
279
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100280 // Script-local variables. Clear all the variables here.
281 // The scriptvar_T is cleared later in free_scriptnames(), because a
282 // variable in one script might hold a reference to the whole scope of
283 // another script.
284 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200285 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200286}
287#endif
288
289 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200290garbage_collect_globvars(int copyID)
291{
292 return set_ref_in_ht(&globvarht, copyID, NULL);
293}
294
295 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200296garbage_collect_vimvars(int copyID)
297{
298 return set_ref_in_ht(&vimvarht, copyID, NULL);
299}
300
301 int
302garbage_collect_scriptvars(int copyID)
303{
Bram Moolenaared234f22020-10-15 20:42:20 +0200304 int i;
305 int idx;
306 int abort = FALSE;
307 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200308
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100309 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200310 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200311 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
312
Bram Moolenaared234f22020-10-15 20:42:20 +0200313 si = SCRIPT_ITEM(i);
314 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
315 {
316 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
317
318 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
319 }
320 }
321
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200322 return abort;
323}
324
325/*
326 * Set an internal variable to a string value. Creates the variable if it does
327 * not already exist.
328 */
329 void
330set_internal_string_var(char_u *name, char_u *value)
331{
332 char_u *val;
333 typval_T *tvp;
334
335 val = vim_strsave(value);
336 if (val != NULL)
337 {
338 tvp = alloc_string_tv(val);
339 if (tvp != NULL)
340 {
341 set_var(name, tvp, FALSE);
342 free_tv(tvp);
343 }
344 }
345}
346
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200347 int
348eval_charconvert(
349 char_u *enc_from,
350 char_u *enc_to,
351 char_u *fname_from,
352 char_u *fname_to)
353{
354 int err = FALSE;
355
356 set_vim_var_string(VV_CC_FROM, enc_from, -1);
357 set_vim_var_string(VV_CC_TO, enc_to, -1);
358 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
359 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
360 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
361 err = TRUE;
362 set_vim_var_string(VV_CC_FROM, NULL, -1);
363 set_vim_var_string(VV_CC_TO, NULL, -1);
364 set_vim_var_string(VV_FNAME_IN, NULL, -1);
365 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
366
367 if (err)
368 return FAIL;
369 return OK;
370}
371
372# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
373 int
374eval_printexpr(char_u *fname, char_u *args)
375{
376 int err = FALSE;
377
378 set_vim_var_string(VV_FNAME_IN, fname, -1);
379 set_vim_var_string(VV_CMDARG, args, -1);
380 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
381 err = TRUE;
382 set_vim_var_string(VV_FNAME_IN, NULL, -1);
383 set_vim_var_string(VV_CMDARG, NULL, -1);
384
385 if (err)
386 {
387 mch_remove(fname);
388 return FAIL;
389 }
390 return OK;
391}
392# endif
393
394# if defined(FEAT_DIFF) || defined(PROTO)
395 void
396eval_diff(
397 char_u *origfile,
398 char_u *newfile,
399 char_u *outfile)
400{
401 int err = FALSE;
402
403 set_vim_var_string(VV_FNAME_IN, origfile, -1);
404 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
405 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
406 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
407 set_vim_var_string(VV_FNAME_IN, NULL, -1);
408 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
409 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
410}
411
412 void
413eval_patch(
414 char_u *origfile,
415 char_u *difffile,
416 char_u *outfile)
417{
418 int err;
419
420 set_vim_var_string(VV_FNAME_IN, origfile, -1);
421 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
422 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
423 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
424 set_vim_var_string(VV_FNAME_IN, NULL, -1);
425 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
426 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
427}
428# endif
429
430#if defined(FEAT_SPELL) || defined(PROTO)
431/*
432 * Evaluate an expression to a list with suggestions.
433 * For the "expr:" part of 'spellsuggest'.
434 * Returns NULL when there is an error.
435 */
436 list_T *
437eval_spell_expr(char_u *badword, char_u *expr)
438{
439 typval_T save_val;
440 typval_T rettv;
441 list_T *list = NULL;
442 char_u *p = skipwhite(expr);
443
444 // Set "v:val" to the bad word.
445 prepare_vimvar(VV_VAL, &save_val);
446 set_vim_var_string(VV_VAL, badword, -1);
447 if (p_verbose == 0)
448 ++emsg_off;
449
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200450 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200451 {
452 if (rettv.v_type != VAR_LIST)
453 clear_tv(&rettv);
454 else
455 list = rettv.vval.v_list;
456 }
457
458 if (p_verbose == 0)
459 --emsg_off;
460 clear_tv(get_vim_var_tv(VV_VAL));
461 restore_vimvar(VV_VAL, &save_val);
462
463 return list;
464}
465
466/*
467 * "list" is supposed to contain two items: a word and a number. Return the
468 * word in "pp" and the number as the return value.
469 * Return -1 if anything isn't right.
470 * Used to get the good word and score from the eval_spell_expr() result.
471 */
472 int
473get_spellword(list_T *list, char_u **pp)
474{
475 listitem_T *li;
476
477 li = list->lv_first;
478 if (li == NULL)
479 return -1;
480 *pp = tv_get_string(&li->li_tv);
481
482 li = li->li_next;
483 if (li == NULL)
484 return -1;
485 return (int)tv_get_number(&li->li_tv);
486}
487#endif
488
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200489/*
490 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200491 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200492 * When not used yet add the variable to the v: hashtable.
493 */
494 void
495prepare_vimvar(int idx, typval_T *save_tv)
496{
497 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200498 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200499 if (vimvars[idx].vv_type == VAR_UNKNOWN)
500 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
501}
502
503/*
504 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200505 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200506 * When no longer defined, remove the variable from the v: hashtable.
507 */
508 void
509restore_vimvar(int idx, typval_T *save_tv)
510{
511 hashitem_T *hi;
512
513 vimvars[idx].vv_tv = *save_tv;
514 if (vimvars[idx].vv_type == VAR_UNKNOWN)
515 {
516 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
517 if (HASHITEM_EMPTY(hi))
518 internal_error("restore_vimvar()");
519 else
520 hash_remove(&vimvarht, hi);
521 }
522}
523
524/*
525 * List Vim variables.
526 */
527 static void
528list_vim_vars(int *first)
529{
530 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
531}
532
533/*
534 * List script-local variables, if there is a script.
535 */
536 static void
537list_script_vars(int *first)
538{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200539 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200540 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
541 "s:", FALSE, first);
542}
543
544/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200545 * Get a list of lines from a HERE document. The here document is a list of
546 * lines surrounded by a marker.
547 * cmd << {marker}
548 * {line1}
549 * {line2}
550 * ....
551 * {marker}
552 *
553 * The {marker} is a string. If the optional 'trim' word is supplied before the
554 * marker, then the leading indentation before the lines (matching the
555 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200556 *
557 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
558 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
559 * missing, then '.' is accepted as a marker.
560 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200561 * Returns a List with {lines} or NULL.
562 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200564heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200565{
566 char_u *theline;
567 char_u *marker;
568 list_T *l;
569 char_u *p;
570 int marker_indent_len = 0;
571 int text_indent_len = 0;
572 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200573 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200574 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200575
576 if (eap->getline == NULL)
577 {
578 emsg(_("E991: cannot use =<< here"));
579 return NULL;
580 }
581
582 // Check for the optional 'trim' word before the marker
583 cmd = skipwhite(cmd);
584 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
585 {
586 cmd = skipwhite(cmd + 4);
587
588 // Trim the indentation from all the lines in the here document.
589 // The amount of indentation trimmed is the same as the indentation of
590 // the first line after the :let command line. To find the end marker
591 // the indent of the :let command line is trimmed.
592 p = *eap->cmdlinep;
593 while (VIM_ISWHITE(*p))
594 {
595 p++;
596 marker_indent_len++;
597 }
598 text_indent_len = -1;
599 }
600
601 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200602 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200603 {
604 marker = skipwhite(cmd);
605 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200606 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200607 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200608 semsg(_(e_trailing_arg), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200609 return NULL;
610 }
611 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200612 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200613 {
614 emsg(_("E221: Marker cannot start with lower case letter"));
615 return NULL;
616 }
617 }
618 else
619 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200620 // When getting lines for an embedded script, if the marker is missing,
621 // accept '.' as the marker.
622 if (script_get)
623 marker = dot;
624 else
625 {
626 emsg(_("E172: Missing marker"));
627 return NULL;
628 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200629 }
630
631 l = list_alloc();
632 if (l == NULL)
633 return NULL;
634
635 for (;;)
636 {
637 int mi = 0;
638 int ti = 0;
639
640 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
641 if (theline == NULL)
642 {
643 semsg(_("E990: Missing end marker '%s'"), marker);
644 break;
645 }
646
647 // with "trim": skip the indent matching the :let line to find the
648 // marker
649 if (marker_indent_len > 0
650 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
651 mi = marker_indent_len;
652 if (STRCMP(marker, theline + mi) == 0)
653 {
654 vim_free(theline);
655 break;
656 }
657
658 if (text_indent_len == -1 && *theline != NUL)
659 {
660 // set the text indent from the first line.
661 p = theline;
662 text_indent_len = 0;
663 while (VIM_ISWHITE(*p))
664 {
665 p++;
666 text_indent_len++;
667 }
668 text_indent = vim_strnsave(theline, text_indent_len);
669 }
670 // with "trim": skip the indent matching the first line
671 if (text_indent != NULL)
672 for (ti = 0; ti < text_indent_len; ++ti)
673 if (theline[ti] != text_indent[ti])
674 break;
675
676 if (list_append_string(l, theline + ti, -1) == FAIL)
677 break;
678 vim_free(theline);
679 }
680 vim_free(text_indent);
681
682 return l;
683}
684
685/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200686 * Vim9 variable declaration:
687 * ":var name"
688 * ":var name: type"
689 * ":var name = expr"
690 * ":var name: type = expr"
691 * etc.
692 */
693 void
694ex_var(exarg_T *eap)
695{
696 if (!in_vim9script())
697 {
698 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
699 return;
700 }
701 ex_let(eap);
702}
703
704/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200705 * ":let" list all variable values
706 * ":let var1 var2" list variable values
707 * ":let var = expr" assignment command.
708 * ":let var += expr" assignment command.
709 * ":let var -= expr" assignment command.
710 * ":let var *= expr" assignment command.
711 * ":let var /= expr" assignment command.
712 * ":let var %= expr" assignment command.
713 * ":let var .= expr" assignment command.
714 * ":let var ..= expr" assignment command.
715 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100716 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100717 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200718 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200719 * ":final var = expr" assignment command.
720 * ":final [var1, var2] = expr" unpack list.
721 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200722 * ":const" list all variable values
723 * ":const var1 var2" list variable values
724 * ":const var = expr" assignment command.
725 * ":const [var1, var2] = expr" unpack list.
726 */
727 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200728ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200729{
730 char_u *arg = eap->arg;
731 char_u *expr = NULL;
732 typval_T rettv;
733 int i;
734 int var_count = 0;
735 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200736 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200737 char_u *argend;
738 int first = TRUE;
739 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200740 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100741 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200742 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200743
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200744 if (eap->cmdidx == CMD_final && !vim9script)
745 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100746 // In legacy Vim script ":final" is short for ":finally".
747 ex_finally(eap);
748 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200749 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200750 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200751 {
752 emsg(_(e_cannot_use_let_in_vim9_script));
753 return;
754 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200755
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100756 if (eap->cmdidx == CMD_const)
757 flags |= ASSIGN_CONST;
758 else if (eap->cmdidx == CMD_final)
759 flags |= ASSIGN_FINAL;
760
761 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200763 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200765 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200766 if (argend == NULL)
767 return;
768 if (argend > arg && argend[-1] == '.') // for var.='str'
769 --argend;
770 expr = skipwhite(argend);
771 concat = expr[0] == '.'
772 && ((expr[1] == '=' && current_sctx.sc_version < 2)
773 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200774 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
775 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200776 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200777 {
778 // ":let" without "=": list variables
779 if (*arg == '[')
780 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200781 else if (expr[0] == '.' && expr[1] == '=')
782 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200783 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200784 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200785 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200786 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100787 // Vim9 declaration ":var name: type"
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200788 arg = vim9_declare_scriptvar(eap, arg);
789 }
790 else
791 {
792 // ":let var1 var2" - list values
793 arg = list_arg_vars(eap, arg, &first);
794 }
795 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200796 else if (!eap->skip)
797 {
798 // ":let"
799 list_glob_vars(&first);
800 list_buf_vars(&first);
801 list_win_vars(&first);
802 list_tab_vars(&first);
803 list_script_vars(&first);
804 list_func_vars(&first);
805 list_vim_vars(&first);
806 }
807 eap->nextcmd = check_nextcmd(arg);
808 }
809 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
810 {
811 list_T *l;
812
813 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200814 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200815 if (l != NULL)
816 {
817 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200818 if (!eap->skip)
819 {
820 op[0] = '=';
821 op[1] = NUL;
822 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200824 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200825 clear_tv(&rettv);
826 }
827 }
828 else
829 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200830 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200831 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200832
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200833 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200834 i = FAIL;
835 if (has_assign || concat)
836 {
837 op[0] = '=';
838 op[1] = NUL;
839 if (*expr != '=')
840 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200841 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200842 {
843 // +=, /=, etc. require an existing variable
844 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
845 i = FAIL;
846 }
847 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200848 {
849 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200850 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200851 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200852 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200853 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200854 ++len;
855 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200856 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200857 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200858 }
859 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200860 ++expr;
861
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200862 if (vim9script && (!VIM_ISWHITE(*argend)
863 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200864 {
865 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100866 semsg(_(e_white_space_required_before_and_after_str_at_str),
867 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200868 i = FAIL;
869 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200870
871 if (eap->skip)
872 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200873 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200874 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200875 if (getline_equal(eap->getline, eap->cookie, getsourceline))
876 {
877 evalarg.eval_getline = eap->getline;
878 evalarg.eval_cookie = eap->cookie;
879 }
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200880 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200881 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200882 if (eap->skip)
883 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200884 clear_evalarg(&evalarg, eap);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200885 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200886 if (eap->skip)
887 {
888 if (i != FAIL)
889 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200890 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200891 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200892 {
893 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200894 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200895 clear_tv(&rettv);
896 }
897 }
898}
899
900/*
901 * Assign the typevalue "tv" to the variable or variables at "arg_start".
902 * Handles both "var" with any type and "[var, var; var]" with a list type.
903 * When "op" is not NULL it points to a string with characters that
904 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
905 * or concatenate.
906 * Returns OK or FAIL;
907 */
908 int
909ex_let_vars(
910 char_u *arg_start,
911 typval_T *tv,
912 int copy, // copy values from "tv", don't move
913 int semicolon, // from skip_var_list()
914 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100915 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200916 char_u *op)
917{
918 char_u *arg = arg_start;
919 list_T *l;
920 int i;
921 listitem_T *item;
922 typval_T ltv;
923
924 if (*arg != '[')
925 {
926 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200928 return FAIL;
929 return OK;
930 }
931
932 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
933 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
934 {
935 emsg(_(e_listreq));
936 return FAIL;
937 }
938
939 i = list_len(l);
940 if (semicolon == 0 && var_count < i)
941 {
942 emsg(_("E687: Less targets than List items"));
943 return FAIL;
944 }
945 if (var_count - semicolon > i)
946 {
947 emsg(_("E688: More targets than List items"));
948 return FAIL;
949 }
950
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200951 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200952 item = l->lv_first;
953 while (*arg != ']')
954 {
955 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200957 item = item->li_next;
958 if (arg == NULL)
959 return FAIL;
960
961 arg = skipwhite(arg);
962 if (*arg == ';')
963 {
964 // Put the rest of the list (may be empty) in the var after ';'.
965 // Create a new list for this.
966 l = list_alloc();
967 if (l == NULL)
968 return FAIL;
969 while (item != NULL)
970 {
971 list_append_tv(l, &item->li_tv);
972 item = item->li_next;
973 }
974
975 ltv.v_type = VAR_LIST;
976 ltv.v_lock = 0;
977 ltv.vval.v_list = l;
978 l->lv_refcount = 1;
979
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200981 (char_u *)"]", op);
982 clear_tv(&ltv);
983 if (arg == NULL)
984 return FAIL;
985 break;
986 }
987 else if (*arg != ',' && *arg != ']')
988 {
989 internal_error("ex_let_vars()");
990 return FAIL;
991 }
992 }
993
994 return OK;
995}
996
997/*
998 * Skip over assignable variable "var" or list of variables "[var, var]".
999 * Used for ":let varvar = expr" and ":for varvar in expr".
1000 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001001 * for "[var, var; var]" set "semicolon" to 1.
1002 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001003 * Return NULL for an error.
1004 */
1005 char_u *
1006skip_var_list(
1007 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001008 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001009 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001010 int *semicolon,
1011 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001012{
1013 char_u *p, *s;
1014
1015 if (*arg == '[')
1016 {
1017 // "[var, var]": find the matching ']'.
1018 p = arg;
1019 for (;;)
1020 {
1021 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001022 s = skip_var_one(p, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001023 if (s == p)
1024 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001025 if (!silent)
1026 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001027 return NULL;
1028 }
1029 ++*var_count;
1030
1031 p = skipwhite(s);
1032 if (*p == ']')
1033 break;
1034 else if (*p == ';')
1035 {
1036 if (*semicolon == 1)
1037 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001038 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001039 return NULL;
1040 }
1041 *semicolon = 1;
1042 }
1043 else if (*p != ',')
1044 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001045 if (!silent)
1046 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001047 return NULL;
1048 }
1049 }
1050 return p + 1;
1051 }
1052 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001054}
1055
1056/*
1057 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1058 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001060 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001061 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001062skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001063{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001064 char_u *end;
1065
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001066 if (*arg == '@' && arg[1] != NUL)
1067 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001069 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001070 if (include_type && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001071 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001072 // "a: type" is declaring variable "a" with a type, not "a:".
1073 if (end == arg + 2 && end[-1] == ':')
1074 --end;
1075 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001076 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077 }
1078 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001079}
1080
1081/*
1082 * List variables for hashtab "ht" with prefix "prefix".
1083 * If "empty" is TRUE also list NULL strings as empty strings.
1084 */
1085 void
1086list_hashtable_vars(
1087 hashtab_T *ht,
1088 char *prefix,
1089 int empty,
1090 int *first)
1091{
1092 hashitem_T *hi;
1093 dictitem_T *di;
1094 int todo;
1095 char_u buf[IOSIZE];
1096
1097 todo = (int)ht->ht_used;
1098 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1099 {
1100 if (!HASHITEM_EMPTY(hi))
1101 {
1102 --todo;
1103 di = HI2DI(hi);
1104
1105 // apply :filter /pat/ to variable name
1106 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1107 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1108 if (message_filtered(buf))
1109 continue;
1110
1111 if (empty || di->di_tv.v_type != VAR_STRING
1112 || di->di_tv.vval.v_string != NULL)
1113 list_one_var(di, prefix, first);
1114 }
1115 }
1116}
1117
1118/*
1119 * List global variables.
1120 */
1121 static void
1122list_glob_vars(int *first)
1123{
1124 list_hashtable_vars(&globvarht, "", TRUE, first);
1125}
1126
1127/*
1128 * List buffer variables.
1129 */
1130 static void
1131list_buf_vars(int *first)
1132{
1133 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1134}
1135
1136/*
1137 * List window variables.
1138 */
1139 static void
1140list_win_vars(int *first)
1141{
1142 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1143}
1144
1145/*
1146 * List tab page variables.
1147 */
1148 static void
1149list_tab_vars(int *first)
1150{
1151 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1152}
1153
1154/*
1155 * List variables in "arg".
1156 */
1157 static char_u *
1158list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1159{
1160 int error = FALSE;
1161 int len;
1162 char_u *name;
1163 char_u *name_start;
1164 char_u *arg_subsc;
1165 char_u *tofree;
1166 typval_T tv;
1167
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001168 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001169 {
1170 if (error || eap->skip)
1171 {
1172 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1173 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1174 {
1175 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001176 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001177 break;
1178 }
1179 }
1180 else
1181 {
1182 // get_name_len() takes care of expanding curly braces
1183 name_start = name = arg;
1184 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1185 if (len <= 0)
1186 {
1187 // This is mainly to keep test 49 working: when expanding
1188 // curly braces fails overrule the exception error message.
1189 if (len < 0 && !aborting())
1190 {
1191 emsg_severe = TRUE;
1192 semsg(_(e_invarg2), arg);
1193 break;
1194 }
1195 error = TRUE;
1196 }
1197 else
1198 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001199 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001200 if (tofree != NULL)
1201 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001202 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001203 error = TRUE;
1204 else
1205 {
1206 // handle d.key, l[idx], f(expr)
1207 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001208 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001209 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001210 error = TRUE;
1211 else
1212 {
1213 if (arg == arg_subsc && len == 2 && name[1] == ':')
1214 {
1215 switch (*name)
1216 {
1217 case 'g': list_glob_vars(first); break;
1218 case 'b': list_buf_vars(first); break;
1219 case 'w': list_win_vars(first); break;
1220 case 't': list_tab_vars(first); break;
1221 case 'v': list_vim_vars(first); break;
1222 case 's': list_script_vars(first); break;
1223 case 'l': list_func_vars(first); break;
1224 default:
1225 semsg(_("E738: Can't list variables for %s"), name);
1226 }
1227 }
1228 else
1229 {
1230 char_u numbuf[NUMBUFLEN];
1231 char_u *tf;
1232 int c;
1233 char_u *s;
1234
1235 s = echo_string(&tv, &tf, numbuf, 0);
1236 c = *arg;
1237 *arg = NUL;
1238 list_one_var_a("",
1239 arg == arg_subsc ? name : name_start,
1240 tv.v_type,
1241 s == NULL ? (char_u *)"" : s,
1242 first);
1243 *arg = c;
1244 vim_free(tf);
1245 }
1246 clear_tv(&tv);
1247 }
1248 }
1249 }
1250
1251 vim_free(tofree);
1252 }
1253
1254 arg = skipwhite(arg);
1255 }
1256
1257 return arg;
1258}
1259
1260/*
1261 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1262 * Returns a pointer to the char just after the var name.
1263 * Returns NULL if there is an error.
1264 */
1265 static char_u *
1266ex_let_one(
1267 char_u *arg, // points to variable name
1268 typval_T *tv, // value to assign to variable
1269 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001270 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001271 char_u *endchars, // valid chars after variable name or NULL
1272 char_u *op) // "+", "-", "." or NULL
1273{
1274 int c1;
1275 char_u *name;
1276 char_u *p;
1277 char_u *arg_end = NULL;
1278 int len;
1279 int opt_flags;
1280 char_u *tofree = NULL;
1281
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001282 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001283 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001284 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1285 {
1286 vim9_declare_error(arg);
1287 return NULL;
1288 }
1289
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001290 // ":let $VAR = expr": Set environment variable.
1291 if (*arg == '$')
1292 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001293 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001294 {
1295 emsg(_("E996: Cannot lock an environment variable"));
1296 return NULL;
1297 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001298
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001299 // Find the end of the name.
1300 ++arg;
1301 name = arg;
1302 len = get_env_len(&arg);
1303 if (len == 0)
1304 semsg(_(e_invarg2), name - 1);
1305 else
1306 {
1307 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1308 semsg(_(e_letwrong), op);
1309 else if (endchars != NULL
1310 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1311 emsg(_(e_letunexp));
1312 else if (!check_secure())
1313 {
1314 c1 = name[len];
1315 name[len] = NUL;
1316 p = tv_get_string_chk(tv);
1317 if (p != NULL && op != NULL && *op == '.')
1318 {
1319 int mustfree = FALSE;
1320 char_u *s = vim_getenv(name, &mustfree);
1321
1322 if (s != NULL)
1323 {
1324 p = tofree = concat_str(s, p);
1325 if (mustfree)
1326 vim_free(s);
1327 }
1328 }
1329 if (p != NULL)
1330 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001331 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001332 arg_end = arg;
1333 }
1334 name[len] = c1;
1335 vim_free(tofree);
1336 }
1337 }
1338 }
1339
1340 // ":let &option = expr": Set option value.
1341 // ":let &l:option = expr": Set local option value.
1342 // ":let &g:option = expr": Set global option value.
1343 else if (*arg == '&')
1344 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001345 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001346 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001348 return NULL;
1349 }
1350 // Find the end of the name.
1351 p = find_option_end(&arg, &opt_flags);
1352 if (p == NULL || (endchars != NULL
1353 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1354 emsg(_(e_letunexp));
1355 else
1356 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001357 long n = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001358 getoption_T opt_type;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001359 long numval;
1360 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001361 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001362 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001363
1364 c1 = *p;
1365 *p = NUL;
1366
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001367 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001368 if ((opt_type == gov_bool
1369 || opt_type == gov_number
1370 || opt_type == gov_hidden_bool
1371 || opt_type == gov_hidden_number)
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001372 && (tv->v_type != VAR_STRING || !in_vim9script()))
1373 // number, possibly hidden
1374 n = (long)tv_get_number(tv);
1375
1376 // Avoid setting a string option to the text "v:false" or similar.
1377 // In Vim9 script also don't convert a number to string.
1378 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1379 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1380 s = tv_get_string_chk(tv);
1381
1382 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001383 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001384 if (((opt_type == gov_bool || opt_type == gov_number)
1385 && *op == '.')
1386 || (opt_type == gov_string && *op != '.'))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001387 {
1388 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001389 failed = TRUE; // don't set the value
1390
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001391 }
1392 else
1393 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001394 // number, in legacy script also bool
1395 if (opt_type == gov_number
1396 || (opt_type == gov_bool && !in_vim9script()))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001397 {
1398 switch (*op)
1399 {
1400 case '+': n = numval + n; break;
1401 case '-': n = numval - n; break;
1402 case '*': n = numval * n; break;
1403 case '/': n = (long)num_divide(numval, n); break;
1404 case '%': n = (long)num_modulus(numval, n); break;
1405 }
1406 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001407 else if (opt_type == gov_string
1408 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001409 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001410 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001411 s = concat_str(stringval, s);
1412 vim_free(stringval);
1413 stringval = s;
1414 }
1415 }
1416 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001417
1418 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001419 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001420 if (opt_type != gov_string || s != NULL)
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001421 {
1422 set_option_value(arg, n, s, opt_flags);
1423 arg_end = p;
1424 }
1425 else
1426 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001427 }
1428 *p = c1;
1429 vim_free(stringval);
1430 }
1431 }
1432
1433 // ":let @r = expr": Set register contents.
1434 else if (*arg == '@')
1435 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001436 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001437 {
1438 emsg(_("E996: Cannot lock a register"));
1439 return NULL;
1440 }
1441 ++arg;
1442 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1443 semsg(_(e_letwrong), op);
1444 else if (endchars != NULL
1445 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1446 emsg(_(e_letunexp));
1447 else
1448 {
1449 char_u *ptofree = NULL;
1450 char_u *s;
1451
1452 p = tv_get_string_chk(tv);
1453 if (p != NULL && op != NULL && *op == '.')
1454 {
1455 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1456 if (s != NULL)
1457 {
1458 p = ptofree = concat_str(s, p);
1459 vim_free(s);
1460 }
1461 }
1462 if (p != NULL)
1463 {
1464 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1465 arg_end = arg + 1;
1466 }
1467 vim_free(ptofree);
1468 }
1469 }
1470
1471 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001473 // ":let {expr} = expr": Idem, name made with curly braces
1474 else if (eval_isnamec1(*arg) || *arg == '{')
1475 {
1476 lval_T lv;
1477
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001478 p = get_lval(arg, tv, &lv, FALSE, FALSE,
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001479 (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1480 ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001481 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001482 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483 if (endchars != NULL && vim_strchr(endchars,
1484 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001485 emsg(_(e_letunexp));
1486 else
1487 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001489 arg_end = p;
1490 }
1491 }
1492 clear_lval(&lv);
1493 }
1494
1495 else
1496 semsg(_(e_invarg2), arg);
1497
1498 return arg_end;
1499}
1500
1501/*
1502 * ":unlet[!] var1 ... " command.
1503 */
1504 void
1505ex_unlet(exarg_T *eap)
1506{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001507 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001508}
1509
1510/*
1511 * ":lockvar" and ":unlockvar" commands
1512 */
1513 void
1514ex_lockvar(exarg_T *eap)
1515{
1516 char_u *arg = eap->arg;
1517 int deep = 2;
1518
1519 if (eap->forceit)
1520 deep = -1;
1521 else if (vim_isdigit(*arg))
1522 {
1523 deep = getdigits(&arg);
1524 arg = skipwhite(arg);
1525 }
1526
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001527 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001528}
1529
1530/*
1531 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001532 * Also used for Vim9 script. "callback" is invoked as:
1533 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001534 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001535 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001536ex_unletlock(
1537 exarg_T *eap,
1538 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001539 int deep,
1540 int glv_flags,
1541 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1542 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001543{
1544 char_u *arg = argstart;
1545 char_u *name_end;
1546 int error = FALSE;
1547 lval_T lv;
1548
1549 do
1550 {
1551 if (*arg == '$')
1552 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001553 lv.ll_name = arg;
1554 lv.ll_tv = NULL;
1555 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001556 if (get_env_len(&arg) == 0)
1557 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001558 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001559 return;
1560 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001561 if (!error && !eap->skip
1562 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1563 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001564 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001565 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001566 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001567 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001568 // Parse the name and find the end.
1569 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001570 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001571 if (lv.ll_name == NULL)
1572 error = TRUE; // error but continue parsing
1573 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1574 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001575 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001576 if (name_end != NULL)
1577 {
1578 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001579 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001580 }
1581 if (!(eap->skip || error))
1582 clear_lval(&lv);
1583 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001584 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001585
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001586 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001587 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001588 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001589
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001590 if (!eap->skip)
1591 clear_lval(&lv);
1592 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001593
1594 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001595 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001596
1597 eap->nextcmd = check_nextcmd(arg);
1598}
1599
1600 static int
1601do_unlet_var(
1602 lval_T *lp,
1603 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001604 exarg_T *eap,
1605 int deep UNUSED,
1606 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001607{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001608 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001609 int ret = OK;
1610 int cc;
1611
1612 if (lp->ll_tv == NULL)
1613 {
1614 cc = *name_end;
1615 *name_end = NUL;
1616
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001617 // Environment variable, normal name or expanded name.
1618 if (*lp->ll_name == '$')
1619 vim_unsetenv(lp->ll_name + 1);
1620 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001621 ret = FAIL;
1622 *name_end = cc;
1623 }
1624 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001625 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001626 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001627 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001628 return FAIL;
1629 else if (lp->ll_range)
1630 {
1631 listitem_T *li;
1632 listitem_T *ll_li = lp->ll_li;
1633 int ll_n1 = lp->ll_n1;
1634
1635 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1636 {
1637 li = ll_li->li_next;
Bram Moolenaara187c432020-09-16 21:08:28 +02001638 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001639 return FAIL;
1640 ll_li = li;
1641 ++ll_n1;
1642 }
1643
1644 // Delete a range of List items.
1645 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1646 {
1647 li = lp->ll_li->li_next;
1648 listitem_remove(lp->ll_list, lp->ll_li);
1649 lp->ll_li = li;
1650 ++lp->ll_n1;
1651 }
1652 }
1653 else
1654 {
1655 if (lp->ll_list != NULL)
1656 // unlet a List item.
1657 listitem_remove(lp->ll_list, lp->ll_li);
1658 else
1659 // unlet a Dictionary item.
1660 dictitem_remove(lp->ll_dict, lp->ll_di);
1661 }
1662
1663 return ret;
1664}
1665
1666/*
1667 * "unlet" a variable. Return OK if it existed, FAIL if not.
1668 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1669 */
1670 int
1671do_unlet(char_u *name, int forceit)
1672{
1673 hashtab_T *ht;
1674 hashitem_T *hi;
1675 char_u *varname;
1676 dict_T *d;
1677 dictitem_T *di;
1678
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001679 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001680 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001681 return FAIL;
1682
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001683 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001684
1685 // can't :unlet a script variable in Vim9 script from a function
1686 if (ht == get_script_local_ht()
1687 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1688 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1689 == SCRIPT_VERSION_VIM9
1690 && check_vim9_unlet(name) == FAIL)
1691 return FAIL;
1692
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001693 if (ht != NULL && *varname != NUL)
1694 {
1695 d = get_current_funccal_dict(ht);
1696 if (d == NULL)
1697 {
1698 if (ht == &globvarht)
1699 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001700 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001701 d = &vimvardict;
1702 else
1703 {
1704 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1705 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1706 }
1707 if (d == NULL)
1708 {
1709 internal_error("do_unlet()");
1710 return FAIL;
1711 }
1712 }
1713 hi = hash_find(ht, varname);
1714 if (HASHITEM_EMPTY(hi))
1715 hi = find_hi_in_scoped_ht(name, &ht);
1716 if (hi != NULL && !HASHITEM_EMPTY(hi))
1717 {
1718 di = HI2DI(hi);
1719 if (var_check_fixed(di->di_flags, name, FALSE)
1720 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001721 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001722 return FAIL;
1723
1724 delete_var(ht, hi);
1725 return OK;
1726 }
1727 }
1728 if (forceit)
1729 return OK;
1730 semsg(_("E108: No such variable: \"%s\""), name);
1731 return FAIL;
1732}
1733
1734/*
1735 * Lock or unlock variable indicated by "lp".
1736 * "deep" is the levels to go (-1 for unlimited);
1737 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1738 */
1739 static int
1740do_lock_var(
1741 lval_T *lp,
1742 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001743 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001744 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001745 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001746{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001747 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001748 int ret = OK;
1749 int cc;
1750 dictitem_T *di;
1751
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001752 if (lp->ll_tv == NULL)
1753 {
1754 cc = *name_end;
1755 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001756 if (*lp->ll_name == '$')
1757 {
1758 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001759 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001760 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001761 else
1762 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001763 // Normal name or expanded name.
1764 di = find_var(lp->ll_name, NULL, TRUE);
1765 if (di == NULL)
1766 ret = FAIL;
1767 else if ((di->di_flags & DI_FLAGS_FIX)
1768 && di->di_tv.v_type != VAR_DICT
1769 && di->di_tv.v_type != VAR_LIST)
1770 {
1771 // For historic reasons this error is not given for a list or
1772 // dict. E.g., the b: dict could be locked/unlocked.
1773 semsg(_(e_lock_unlock), lp->ll_name);
1774 ret = FAIL;
1775 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001776 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001777 {
1778 if (lock)
1779 di->di_flags |= DI_FLAGS_LOCK;
1780 else
1781 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001782 if (deep != 0)
1783 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001784 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001785 }
1786 *name_end = cc;
1787 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001788 else if (deep == 0)
1789 {
1790 // nothing to do
1791 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001792 else if (lp->ll_range)
1793 {
1794 listitem_T *li = lp->ll_li;
1795
1796 // (un)lock a range of List items.
1797 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1798 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001799 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001800 li = li->li_next;
1801 ++lp->ll_n1;
1802 }
1803 }
1804 else if (lp->ll_list != NULL)
1805 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001806 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001807 else
1808 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001809 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001810
1811 return ret;
1812}
1813
1814/*
1815 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001816 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1817 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001818 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001819 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001820item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001821{
1822 static int recurse = 0;
1823 list_T *l;
1824 listitem_T *li;
1825 dict_T *d;
1826 blob_T *b;
1827 hashitem_T *hi;
1828 int todo;
1829
1830 if (recurse >= DICT_MAXNEST)
1831 {
1832 emsg(_("E743: variable nested too deep for (un)lock"));
1833 return;
1834 }
1835 if (deep == 0)
1836 return;
1837 ++recurse;
1838
1839 // lock/unlock the item itself
1840 if (lock)
1841 tv->v_lock |= VAR_LOCKED;
1842 else
1843 tv->v_lock &= ~VAR_LOCKED;
1844
1845 switch (tv->v_type)
1846 {
1847 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001848 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001849 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001850 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001851 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001852 case VAR_STRING:
1853 case VAR_FUNC:
1854 case VAR_PARTIAL:
1855 case VAR_FLOAT:
1856 case VAR_SPECIAL:
1857 case VAR_JOB:
1858 case VAR_CHANNEL:
1859 break;
1860
1861 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001862 if ((b = tv->vval.v_blob) != NULL
1863 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001864 {
1865 if (lock)
1866 b->bv_lock |= VAR_LOCKED;
1867 else
1868 b->bv_lock &= ~VAR_LOCKED;
1869 }
1870 break;
1871 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001872 if ((l = tv->vval.v_list) != NULL
1873 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001874 {
1875 if (lock)
1876 l->lv_lock |= VAR_LOCKED;
1877 else
1878 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001879 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001880 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001881 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001882 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001883 }
1884 break;
1885 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001886 if ((d = tv->vval.v_dict) != NULL
1887 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001888 {
1889 if (lock)
1890 d->dv_lock |= VAR_LOCKED;
1891 else
1892 d->dv_lock &= ~VAR_LOCKED;
1893 if (deep < 0 || deep > 1)
1894 {
1895 // recursive: lock/unlock the items the List contains
1896 todo = (int)d->dv_hashtab.ht_used;
1897 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1898 {
1899 if (!HASHITEM_EMPTY(hi))
1900 {
1901 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001902 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1903 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001904 }
1905 }
1906 }
1907 }
1908 }
1909 --recurse;
1910}
1911
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001912#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1913/*
1914 * Delete all "menutrans_" variables.
1915 */
1916 void
1917del_menutrans_vars(void)
1918{
1919 hashitem_T *hi;
1920 int todo;
1921
1922 hash_lock(&globvarht);
1923 todo = (int)globvarht.ht_used;
1924 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1925 {
1926 if (!HASHITEM_EMPTY(hi))
1927 {
1928 --todo;
1929 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1930 delete_var(&globvarht, hi);
1931 }
1932 }
1933 hash_unlock(&globvarht);
1934}
1935#endif
1936
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001937/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001938 * Local string buffer for the next two functions to store a variable name
1939 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1940 * get_user_var_name().
1941 */
1942
1943static char_u *varnamebuf = NULL;
1944static int varnamebuflen = 0;
1945
1946/*
1947 * Function to concatenate a prefix and a variable name.
1948 */
1949 static char_u *
1950cat_prefix_varname(int prefix, char_u *name)
1951{
1952 int len;
1953
1954 len = (int)STRLEN(name) + 3;
1955 if (len > varnamebuflen)
1956 {
1957 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001958 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001959 varnamebuf = alloc(len);
1960 if (varnamebuf == NULL)
1961 {
1962 varnamebuflen = 0;
1963 return NULL;
1964 }
1965 varnamebuflen = len;
1966 }
1967 *varnamebuf = prefix;
1968 varnamebuf[1] = ':';
1969 STRCPY(varnamebuf + 2, name);
1970 return varnamebuf;
1971}
1972
1973/*
1974 * Function given to ExpandGeneric() to obtain the list of user defined
1975 * (global/buffer/window/built-in) variable names.
1976 */
1977 char_u *
1978get_user_var_name(expand_T *xp, int idx)
1979{
1980 static long_u gdone;
1981 static long_u bdone;
1982 static long_u wdone;
1983 static long_u tdone;
1984 static int vidx;
1985 static hashitem_T *hi;
1986 hashtab_T *ht;
1987
1988 if (idx == 0)
1989 {
1990 gdone = bdone = wdone = vidx = 0;
1991 tdone = 0;
1992 }
1993
1994 // Global variables
1995 if (gdone < globvarht.ht_used)
1996 {
1997 if (gdone++ == 0)
1998 hi = globvarht.ht_array;
1999 else
2000 ++hi;
2001 while (HASHITEM_EMPTY(hi))
2002 ++hi;
2003 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2004 return cat_prefix_varname('g', hi->hi_key);
2005 return hi->hi_key;
2006 }
2007
2008 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002009 ht =
2010#ifdef FEAT_CMDWIN
2011 // In cmdwin, the alternative buffer should be used.
2012 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2013 &prevwin->w_buffer->b_vars->dv_hashtab :
2014#endif
2015 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002016 if (bdone < ht->ht_used)
2017 {
2018 if (bdone++ == 0)
2019 hi = ht->ht_array;
2020 else
2021 ++hi;
2022 while (HASHITEM_EMPTY(hi))
2023 ++hi;
2024 return cat_prefix_varname('b', hi->hi_key);
2025 }
2026
2027 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002028 ht =
2029#ifdef FEAT_CMDWIN
2030 // In cmdwin, the alternative window should be used.
2031 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2032 &prevwin->w_vars->dv_hashtab :
2033#endif
2034 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002035 if (wdone < ht->ht_used)
2036 {
2037 if (wdone++ == 0)
2038 hi = ht->ht_array;
2039 else
2040 ++hi;
2041 while (HASHITEM_EMPTY(hi))
2042 ++hi;
2043 return cat_prefix_varname('w', hi->hi_key);
2044 }
2045
2046 // t: variables
2047 ht = &curtab->tp_vars->dv_hashtab;
2048 if (tdone < ht->ht_used)
2049 {
2050 if (tdone++ == 0)
2051 hi = ht->ht_array;
2052 else
2053 ++hi;
2054 while (HASHITEM_EMPTY(hi))
2055 ++hi;
2056 return cat_prefix_varname('t', hi->hi_key);
2057 }
2058
2059 // v: variables
2060 if (vidx < VV_LEN)
2061 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2062
2063 VIM_CLEAR(varnamebuf);
2064 varnamebuflen = 0;
2065 return NULL;
2066}
2067
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002068 char *
2069get_var_special_name(int nr)
2070{
2071 switch (nr)
2072 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002073 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2074 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002075 case VVAL_NONE: return "v:none";
2076 case VVAL_NULL: return "v:null";
2077 }
2078 internal_error("get_var_special_name()");
2079 return "42";
2080}
2081
2082/*
2083 * Returns the global variable dictionary
2084 */
2085 dict_T *
2086get_globvar_dict(void)
2087{
2088 return &globvardict;
2089}
2090
2091/*
2092 * Returns the global variable hash table
2093 */
2094 hashtab_T *
2095get_globvar_ht(void)
2096{
2097 return &globvarht;
2098}
2099
2100/*
2101 * Returns the v: variable dictionary
2102 */
2103 dict_T *
2104get_vimvar_dict(void)
2105{
2106 return &vimvardict;
2107}
2108
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002109/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002110 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002111 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112 */
2113 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002114find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002115{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002116 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2117 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118
2119 if (di == NULL)
2120 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002121 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2123 return (int)(vv - vimvars);
2124}
2125
2126
2127/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002128 * Set type of v: variable to "type".
2129 */
2130 void
2131set_vim_var_type(int idx, vartype_T type)
2132{
2133 vimvars[idx].vv_type = type;
2134}
2135
2136/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002137 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002138 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002139 */
2140 void
2141set_vim_var_nr(int idx, varnumber_T val)
2142{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002143 vimvars[idx].vv_nr = val;
2144}
2145
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002146 char *
2147get_vim_var_name(int idx)
2148{
2149 return vimvars[idx].vv_name;
2150}
2151
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002152/*
2153 * Get typval_T v: variable value.
2154 */
2155 typval_T *
2156get_vim_var_tv(int idx)
2157{
2158 return &vimvars[idx].vv_tv;
2159}
2160
2161/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002162 * Set v: variable to "tv". Only accepts the same type.
2163 * Takes over the value of "tv".
2164 */
2165 int
2166set_vim_var_tv(int idx, typval_T *tv)
2167{
2168 if (vimvars[idx].vv_type != tv->v_type)
2169 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002170 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002171 clear_tv(tv);
2172 return FAIL;
2173 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002174 // VV_RO is also checked when compiling, but let's check here as well.
2175 if (vimvars[idx].vv_flags & VV_RO)
2176 {
2177 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2178 return FAIL;
2179 }
2180 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2181 {
2182 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2183 return FAIL;
2184 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002185 clear_tv(&vimvars[idx].vv_di.di_tv);
2186 vimvars[idx].vv_di.di_tv = *tv;
2187 return OK;
2188}
2189
2190/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002191 * Get number v: variable value.
2192 */
2193 varnumber_T
2194get_vim_var_nr(int idx)
2195{
2196 return vimvars[idx].vv_nr;
2197}
2198
2199/*
2200 * Get string v: variable value. Uses a static buffer, can only be used once.
2201 * If the String variable has never been set, return an empty string.
2202 * Never returns NULL;
2203 */
2204 char_u *
2205get_vim_var_str(int idx)
2206{
2207 return tv_get_string(&vimvars[idx].vv_tv);
2208}
2209
2210/*
2211 * Get List v: variable value. Caller must take care of reference count when
2212 * needed.
2213 */
2214 list_T *
2215get_vim_var_list(int idx)
2216{
2217 return vimvars[idx].vv_list;
2218}
2219
2220/*
2221 * Get Dict v: variable value. Caller must take care of reference count when
2222 * needed.
2223 */
2224 dict_T *
2225get_vim_var_dict(int idx)
2226{
2227 return vimvars[idx].vv_dict;
2228}
2229
2230/*
2231 * Set v:char to character "c".
2232 */
2233 void
2234set_vim_var_char(int c)
2235{
2236 char_u buf[MB_MAXBYTES + 1];
2237
2238 if (has_mbyte)
2239 buf[(*mb_char2bytes)(c, buf)] = NUL;
2240 else
2241 {
2242 buf[0] = c;
2243 buf[1] = NUL;
2244 }
2245 set_vim_var_string(VV_CHAR, buf, -1);
2246}
2247
2248/*
2249 * Set v:count to "count" and v:count1 to "count1".
2250 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2251 */
2252 void
2253set_vcount(
2254 long count,
2255 long count1,
2256 int set_prevcount)
2257{
2258 if (set_prevcount)
2259 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2260 vimvars[VV_COUNT].vv_nr = count;
2261 vimvars[VV_COUNT1].vv_nr = count1;
2262}
2263
2264/*
2265 * Save variables that might be changed as a side effect. Used when executing
2266 * a timer callback.
2267 */
2268 void
2269save_vimvars(vimvars_save_T *vvsave)
2270{
2271 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2272 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2273 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2274}
2275
2276/*
2277 * Restore variables saved by save_vimvars().
2278 */
2279 void
2280restore_vimvars(vimvars_save_T *vvsave)
2281{
2282 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2283 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2284 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2285}
2286
2287/*
2288 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2289 * value.
2290 */
2291 void
2292set_vim_var_string(
2293 int idx,
2294 char_u *val,
2295 int len) // length of "val" to use or -1 (whole string)
2296{
2297 clear_tv(&vimvars[idx].vv_di.di_tv);
2298 vimvars[idx].vv_type = VAR_STRING;
2299 if (val == NULL)
2300 vimvars[idx].vv_str = NULL;
2301 else if (len == -1)
2302 vimvars[idx].vv_str = vim_strsave(val);
2303 else
2304 vimvars[idx].vv_str = vim_strnsave(val, len);
2305}
2306
2307/*
2308 * Set List v: variable to "val".
2309 */
2310 void
2311set_vim_var_list(int idx, list_T *val)
2312{
2313 clear_tv(&vimvars[idx].vv_di.di_tv);
2314 vimvars[idx].vv_type = VAR_LIST;
2315 vimvars[idx].vv_list = val;
2316 if (val != NULL)
2317 ++val->lv_refcount;
2318}
2319
2320/*
2321 * Set Dictionary v: variable to "val".
2322 */
2323 void
2324set_vim_var_dict(int idx, dict_T *val)
2325{
2326 clear_tv(&vimvars[idx].vv_di.di_tv);
2327 vimvars[idx].vv_type = VAR_DICT;
2328 vimvars[idx].vv_dict = val;
2329 if (val != NULL)
2330 {
2331 ++val->dv_refcount;
2332 dict_set_items_ro(val);
2333 }
2334}
2335
2336/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002337 * Set the v:argv list.
2338 */
2339 void
2340set_argv_var(char **argv, int argc)
2341{
2342 list_T *l = list_alloc();
2343 int i;
2344
2345 if (l == NULL)
2346 getout(1);
2347 l->lv_lock = VAR_FIXED;
2348 for (i = 0; i < argc; ++i)
2349 {
2350 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2351 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002352 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002353 }
2354 set_vim_var_list(VV_ARGV, l);
2355}
2356
2357/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002358 * Reset v:register, taking the 'clipboard' setting into account.
2359 */
2360 void
2361reset_reg_var(void)
2362{
2363 int regname = 0;
2364
2365 // Adjust the register according to 'clipboard', so that when
2366 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2367#ifdef FEAT_CLIPBOARD
2368 adjust_clip_reg(&regname);
2369#endif
2370 set_reg_var(regname);
2371}
2372
2373/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002374 * Set v:register if needed.
2375 */
2376 void
2377set_reg_var(int c)
2378{
2379 char_u regname;
2380
2381 if (c == 0 || c == ' ')
2382 regname = '"';
2383 else
2384 regname = c;
2385 // Avoid free/alloc when the value is already right.
2386 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2387 set_vim_var_string(VV_REG, &regname, 1);
2388}
2389
2390/*
2391 * Get or set v:exception. If "oldval" == NULL, return the current value.
2392 * Otherwise, restore the value to "oldval" and return NULL.
2393 * Must always be called in pairs to save and restore v:exception! Does not
2394 * take care of memory allocations.
2395 */
2396 char_u *
2397v_exception(char_u *oldval)
2398{
2399 if (oldval == NULL)
2400 return vimvars[VV_EXCEPTION].vv_str;
2401
2402 vimvars[VV_EXCEPTION].vv_str = oldval;
2403 return NULL;
2404}
2405
2406/*
2407 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2408 * Otherwise, restore the value to "oldval" and return NULL.
2409 * Must always be called in pairs to save and restore v:throwpoint! Does not
2410 * take care of memory allocations.
2411 */
2412 char_u *
2413v_throwpoint(char_u *oldval)
2414{
2415 if (oldval == NULL)
2416 return vimvars[VV_THROWPOINT].vv_str;
2417
2418 vimvars[VV_THROWPOINT].vv_str = oldval;
2419 return NULL;
2420}
2421
2422/*
2423 * Set v:cmdarg.
2424 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2425 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2426 * Must always be called in pairs!
2427 */
2428 char_u *
2429set_cmdarg(exarg_T *eap, char_u *oldarg)
2430{
2431 char_u *oldval;
2432 char_u *newval;
2433 unsigned len;
2434
2435 oldval = vimvars[VV_CMDARG].vv_str;
2436 if (eap == NULL)
2437 {
2438 vim_free(oldval);
2439 vimvars[VV_CMDARG].vv_str = oldarg;
2440 return NULL;
2441 }
2442
2443 if (eap->force_bin == FORCE_BIN)
2444 len = 6;
2445 else if (eap->force_bin == FORCE_NOBIN)
2446 len = 8;
2447 else
2448 len = 0;
2449
2450 if (eap->read_edit)
2451 len += 7;
2452
2453 if (eap->force_ff != 0)
2454 len += 10; // " ++ff=unix"
2455 if (eap->force_enc != 0)
2456 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2457 if (eap->bad_char != 0)
2458 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2459
2460 newval = alloc(len + 1);
2461 if (newval == NULL)
2462 return NULL;
2463
2464 if (eap->force_bin == FORCE_BIN)
2465 sprintf((char *)newval, " ++bin");
2466 else if (eap->force_bin == FORCE_NOBIN)
2467 sprintf((char *)newval, " ++nobin");
2468 else
2469 *newval = NUL;
2470
2471 if (eap->read_edit)
2472 STRCAT(newval, " ++edit");
2473
2474 if (eap->force_ff != 0)
2475 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2476 eap->force_ff == 'u' ? "unix"
2477 : eap->force_ff == 'd' ? "dos"
2478 : "mac");
2479 if (eap->force_enc != 0)
2480 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2481 eap->cmd + eap->force_enc);
2482 if (eap->bad_char == BAD_KEEP)
2483 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2484 else if (eap->bad_char == BAD_DROP)
2485 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2486 else if (eap->bad_char != 0)
2487 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2488 vimvars[VV_CMDARG].vv_str = newval;
2489 return oldval;
2490}
2491
2492/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002493 * Get the value of internal variable "name".
2494 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2495 */
2496 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002497eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002498 char_u *name,
2499 int len, // length of "name"
2500 typval_T *rettv, // NULL when only checking existence
2501 dictitem_T **dip, // non-NULL when typval's dict item is needed
2502 int verbose, // may give error message
2503 int no_autoload) // do not use script autoloading
2504{
2505 int ret = OK;
2506 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002507 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002508 dictitem_T *v;
2509 int cc;
2510
2511 // truncate the name, so that we can use strcmp()
2512 cc = name[len];
2513 name[len] = NUL;
2514
2515 // Check for user-defined variables.
2516 v = find_var(name, NULL, no_autoload);
2517 if (v != NULL)
2518 {
2519 tv = &v->di_tv;
2520 if (dip != NULL)
2521 *dip = v;
2522 }
2523
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002524 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002526 imported_T *import;
2527 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2528
2529 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530
2531 // imported variable from another script
2532 if (import != NULL)
2533 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002534 if (import->imp_funcname != NULL)
2535 {
2536 foundFunc = TRUE;
2537 if (rettv != NULL)
2538 {
2539 rettv->v_type = VAR_FUNC;
2540 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2541 }
2542 }
Bram Moolenaara6294952020-12-27 13:39:50 +01002543 else if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002544 {
2545 emsg("Sorry, 'import * as X' not implemented yet");
2546 ret = FAIL;
2547 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002548 else
2549 {
2550 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002551 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002553 tv = sv->sv_tv;
2554 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002556 else if (in_vim9script())
2557 {
2558 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2559
2560 if (ufunc != NULL)
2561 {
2562 foundFunc = TRUE;
2563 if (rettv != NULL)
2564 {
2565 rettv->v_type = VAR_FUNC;
2566 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2567 }
2568 }
2569 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570 }
2571
Bram Moolenaarc620c052020-07-08 15:16:19 +02002572 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002573 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002574 if (tv == NULL)
2575 {
2576 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002577 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002578 ret = FAIL;
2579 }
2580 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002581 {
2582 // If a list or dict variable wasn't initialized, do it now.
2583 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2584 {
2585 tv->vval.v_dict = dict_alloc();
2586 if (tv->vval.v_dict != NULL)
2587 ++tv->vval.v_dict->dv_refcount;
2588 }
2589 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2590 {
2591 tv->vval.v_list = list_alloc();
2592 if (tv->vval.v_list != NULL)
2593 ++tv->vval.v_list->lv_refcount;
2594 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002595 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002596 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002597 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002598
2599 name[len] = cc;
2600
2601 return ret;
2602}
2603
2604/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002605 * Check if variable "name[len]" is a local variable or an argument.
2606 * If so, "*eval_lavars_used" is set to TRUE.
2607 */
2608 void
2609check_vars(char_u *name, int len)
2610{
2611 int cc;
2612 char_u *varname;
2613 hashtab_T *ht;
2614
2615 if (eval_lavars_used == NULL)
2616 return;
2617
2618 // truncate the name, so that we can use strcmp()
2619 cc = name[len];
2620 name[len] = NUL;
2621
2622 ht = find_var_ht(name, &varname);
2623 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2624 {
2625 if (find_var(name, NULL, TRUE) != NULL)
2626 *eval_lavars_used = TRUE;
2627 }
2628
2629 name[len] = cc;
2630}
2631
2632/*
2633 * Find variable "name" in the list of variables.
2634 * Return a pointer to it if found, NULL if not found.
2635 * Careful: "a:0" variables don't have a name.
2636 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2637 * hashtab_T used.
2638 */
2639 dictitem_T *
2640find_var(char_u *name, hashtab_T **htp, int no_autoload)
2641{
2642 char_u *varname;
2643 hashtab_T *ht;
2644 dictitem_T *ret = NULL;
2645
2646 ht = find_var_ht(name, &varname);
2647 if (htp != NULL)
2648 *htp = ht;
2649 if (ht == NULL)
2650 return NULL;
2651 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2652 if (ret != NULL)
2653 return ret;
2654
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002655 // Search in parent scope for lambda
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002656 ret = find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2657 if (ret != NULL)
2658 return ret;
2659
2660 // in Vim9 script items without a scope can be script-local
2661 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2662 {
2663 ht = get_script_local_ht();
2664 if (ht != NULL)
2665 {
2666 ret = find_var_in_ht(ht, *name, varname,
2667 no_autoload || htp != NULL);
2668 if (ret != NULL)
2669 {
2670 if (htp != NULL)
2671 *htp = ht;
2672 return ret;
2673 }
2674 }
2675 }
2676
2677 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002678}
2679
2680/*
2681 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002682 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002683 * Returns NULL if not found.
2684 */
2685 dictitem_T *
2686find_var_in_ht(
2687 hashtab_T *ht,
2688 int htname,
2689 char_u *varname,
2690 int no_autoload)
2691{
2692 hashitem_T *hi;
2693
2694 if (*varname == NUL)
2695 {
2696 // Must be something like "s:", otherwise "ht" would be NULL.
2697 switch (htname)
2698 {
2699 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2700 case 'g': return &globvars_var;
2701 case 'v': return &vimvars_var;
2702 case 'b': return &curbuf->b_bufvar;
2703 case 'w': return &curwin->w_winvar;
2704 case 't': return &curtab->tp_winvar;
2705 case 'l': return get_funccal_local_var();
2706 case 'a': return get_funccal_args_var();
2707 }
2708 return NULL;
2709 }
2710
2711 hi = hash_find(ht, varname);
2712 if (HASHITEM_EMPTY(hi))
2713 {
2714 // For global variables we may try auto-loading the script. If it
2715 // worked find the variable again. Don't auto-load a script if it was
2716 // loaded already, otherwise it would be loaded every time when
2717 // checking if a function name is a Funcref variable.
2718 if (ht == &globvarht && !no_autoload)
2719 {
2720 // Note: script_autoload() may make "hi" invalid. It must either
2721 // be obtained again or not used.
2722 if (!script_autoload(varname, FALSE) || aborting())
2723 return NULL;
2724 hi = hash_find(ht, varname);
2725 }
2726 if (HASHITEM_EMPTY(hi))
2727 return NULL;
2728 }
2729 return HI2DI(hi);
2730}
2731
2732/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733 * Get the script-local hashtab. NULL if not in a script context.
2734 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002735 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002736get_script_local_ht(void)
2737{
2738 scid_T sid = current_sctx.sc_sid;
2739
Bram Moolenaare3d46852020-08-29 13:39:17 +02002740 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 return &SCRIPT_VARS(sid);
2742 return NULL;
2743}
2744
2745/*
2746 * Look for "name[len]" in script-local variables.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002747 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01002749 int
2750lookup_scriptvar(
2751 char_u *name,
2752 size_t len,
2753 void *lvar UNUSED,
2754 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755{
2756 hashtab_T *ht = get_script_local_ht();
2757 char_u buffer[30];
2758 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01002759 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760 hashitem_T *hi;
2761
2762 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002763 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 if (len < sizeof(buffer) - 1)
2765 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002766 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767 vim_strncpy(buffer, name, len);
2768 p = buffer;
2769 }
2770 else
2771 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002772 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002774 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775 }
2776
2777 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002778 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002779
2780 // if not script-local, then perhaps imported
Bram Moolenaar709664c2020-12-12 14:33:41 +01002781 if (res == FAIL && find_imported(p, 0, NULL) != NULL)
2782 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783
2784 if (p != buffer)
2785 vim_free(p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002786 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787}
2788
2789/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002790 * Find the hashtab used for a variable name.
2791 * Return NULL if the name is not valid.
2792 * Set "varname" to the start of name without ':'.
2793 */
2794 hashtab_T *
2795find_var_ht(char_u *name, char_u **varname)
2796{
2797 hashitem_T *hi;
2798 hashtab_T *ht;
2799
2800 if (name[0] == NUL)
2801 return NULL;
2802 if (name[1] != ':')
2803 {
2804 // The name must not start with a colon or #.
2805 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2806 return NULL;
2807 *varname = name;
2808
2809 // "version" is "v:version" in all scopes if scriptversion < 3.
2810 // Same for a few other variables marked with VV_COMPAT.
2811 if (current_sctx.sc_version < 3)
2812 {
2813 hi = hash_find(&compat_hashtab, name);
2814 if (!HASHITEM_EMPTY(hi))
2815 return &compat_hashtab;
2816 }
2817
2818 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 if (ht != NULL)
2820 return ht; // local variable
2821
2822 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002823 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 {
2825 ht = get_script_local_ht();
2826 if (ht != NULL)
2827 return ht;
2828 }
2829
2830 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002831 }
2832 *varname = name + 2;
2833 if (*name == 'g') // global variable
2834 return &globvarht;
2835 // There must be no ':' or '#' in the rest of the name, unless g: is used
2836 if (vim_strchr(name + 2, ':') != NULL
2837 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2838 return NULL;
2839 if (*name == 'b') // buffer variable
2840 return &curbuf->b_vars->dv_hashtab;
2841 if (*name == 'w') // window variable
2842 return &curwin->w_vars->dv_hashtab;
2843 if (*name == 't') // tab page variable
2844 return &curtab->tp_vars->dv_hashtab;
2845 if (*name == 'v') // v: variable
2846 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002847 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002848 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002850 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 if (*name == 'a') // a: function argument
2852 return get_funccal_args_ht();
2853 if (*name == 'l') // l: local function variable
2854 return get_funccal_local_ht();
2855 }
2856 if (*name == 's') // script variable
2857 {
2858 ht = get_script_local_ht();
2859 if (ht != NULL)
2860 return ht;
2861 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002862 return NULL;
2863}
2864
2865/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002866 * Get the string value of a (global/local) variable.
2867 * Note: see tv_get_string() for how long the pointer remains valid.
2868 * Returns NULL when it doesn't exist.
2869 */
2870 char_u *
2871get_var_value(char_u *name)
2872{
2873 dictitem_T *v;
2874
2875 v = find_var(name, NULL, FALSE);
2876 if (v == NULL)
2877 return NULL;
2878 return tv_get_string(&v->di_tv);
2879}
2880
2881/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002882 * Allocate a new hashtab for a sourced script. It will be used while
2883 * sourcing this script and when executing functions defined in the script.
2884 */
2885 void
2886new_script_vars(scid_T id)
2887{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002888 scriptvar_T *sv;
2889
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002890 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2891 if (sv == NULL)
2892 return;
2893 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002894 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002895}
2896
2897/*
2898 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2899 * point to it.
2900 */
2901 void
2902init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2903{
2904 hash_init(&dict->dv_hashtab);
2905 dict->dv_lock = 0;
2906 dict->dv_scope = scope;
2907 dict->dv_refcount = DO_NOT_FREE_CNT;
2908 dict->dv_copyID = 0;
2909 dict_var->di_tv.vval.v_dict = dict;
2910 dict_var->di_tv.v_type = VAR_DICT;
2911 dict_var->di_tv.v_lock = VAR_FIXED;
2912 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2913 dict_var->di_key[0] = NUL;
2914}
2915
2916/*
2917 * Unreference a dictionary initialized by init_var_dict().
2918 */
2919 void
2920unref_var_dict(dict_T *dict)
2921{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002922 // Now the dict needs to be freed if no one else is using it, go back to
2923 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002924 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2925 dict_unref(dict);
2926}
2927
2928/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002929 * Clean up a list of internal variables.
2930 * Frees all allocated variables and the value they contain.
2931 * Clears hashtab "ht", does not free it.
2932 */
2933 void
2934vars_clear(hashtab_T *ht)
2935{
2936 vars_clear_ext(ht, TRUE);
2937}
2938
2939/*
2940 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2941 */
2942 void
2943vars_clear_ext(hashtab_T *ht, int free_val)
2944{
2945 int todo;
2946 hashitem_T *hi;
2947 dictitem_T *v;
2948
2949 hash_lock(ht);
2950 todo = (int)ht->ht_used;
2951 for (hi = ht->ht_array; todo > 0; ++hi)
2952 {
2953 if (!HASHITEM_EMPTY(hi))
2954 {
2955 --todo;
2956
2957 // Free the variable. Don't remove it from the hashtab,
2958 // ht_array might change then. hash_clear() takes care of it
2959 // later.
2960 v = HI2DI(hi);
2961 if (free_val)
2962 clear_tv(&v->di_tv);
2963 if (v->di_flags & DI_FLAGS_ALLOC)
2964 vim_free(v);
2965 }
2966 }
2967 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02002968 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002969}
2970
2971/*
2972 * Delete a variable from hashtab "ht" at item "hi".
2973 * Clear the variable value and free the dictitem.
2974 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02002975 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002976delete_var(hashtab_T *ht, hashitem_T *hi)
2977{
2978 dictitem_T *di = HI2DI(hi);
2979
2980 hash_remove(ht, hi);
2981 clear_tv(&di->di_tv);
2982 vim_free(di);
2983}
2984
2985/*
2986 * List the value of one internal variable.
2987 */
2988 static void
2989list_one_var(dictitem_T *v, char *prefix, int *first)
2990{
2991 char_u *tofree;
2992 char_u *s;
2993 char_u numbuf[NUMBUFLEN];
2994
2995 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2996 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2997 s == NULL ? (char_u *)"" : s, first);
2998 vim_free(tofree);
2999}
3000
3001 static void
3002list_one_var_a(
3003 char *prefix,
3004 char_u *name,
3005 int type,
3006 char_u *string,
3007 int *first) // when TRUE clear rest of screen and set to FALSE
3008{
3009 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3010 msg_start();
3011 msg_puts(prefix);
3012 if (name != NULL) // "a:" vars don't have a name stored
3013 msg_puts((char *)name);
3014 msg_putchar(' ');
3015 msg_advance(22);
3016 if (type == VAR_NUMBER)
3017 msg_putchar('#');
3018 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3019 msg_putchar('*');
3020 else if (type == VAR_LIST)
3021 {
3022 msg_putchar('[');
3023 if (*string == '[')
3024 ++string;
3025 }
3026 else if (type == VAR_DICT)
3027 {
3028 msg_putchar('{');
3029 if (*string == '{')
3030 ++string;
3031 }
3032 else
3033 msg_putchar(' ');
3034
3035 msg_outtrans(string);
3036
3037 if (type == VAR_FUNC || type == VAR_PARTIAL)
3038 msg_puts("()");
3039 if (*first)
3040 {
3041 msg_clr_eos();
3042 *first = FALSE;
3043 }
3044}
3045
3046/*
3047 * Set variable "name" to value in "tv".
3048 * If the variable already exists, the value is updated.
3049 * Otherwise the variable is created.
3050 */
3051 void
3052set_var(
3053 char_u *name,
3054 typval_T *tv,
3055 int copy) // make copy of value in "tv"
3056{
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003057 set_var_const(name, NULL, tv, copy, ASSIGN_DECL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003058}
3059
3060/*
3061 * Set variable "name" to value in "tv".
3062 * If the variable already exists and "is_const" is FALSE the value is updated.
3063 * Otherwise the variable is created.
3064 */
3065 void
3066set_var_const(
3067 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003069 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003070 int copy, // make copy of value in "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003071 int flags) // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003072{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003073 typval_T *tv = tv_arg;
3074 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003076 char_u *varname;
3077 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003079 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003080
3081 ht = find_var_ht(name, &varname);
3082 if (ht == NULL || *varname == NUL)
3083 {
3084 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003085 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003086 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003087 is_script_local = ht == get_script_local_ht();
3088
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003089 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003090 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003091 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003092 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003093 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003094 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003095 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003096 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003097 }
3098
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003100
3101 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 if (di == NULL)
3103 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003104
3105 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003106 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003107 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003108
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003109 if (need_convert_to_bool(type, tv))
3110 {
3111 // Destination is a bool and the value is not, but it can be converted.
3112 CLEAR_FIELD(bool_tv);
3113 bool_tv.v_type = VAR_BOOL;
3114 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3115 tv = &bool_tv;
3116 }
3117
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003119 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003121 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003122 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 {
3124 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003125 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 }
3127
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003128 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 {
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003130 if ((flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003131 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003132 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003133 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003134 }
3135
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003136 // check the type and adjust to bool if needed
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003137 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003138 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003140
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003141 if (var_check_permission(di, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003142 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003143 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 else
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003145 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 // can only redefine once
3147 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003148
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003149 // A Vim9 script-local variable is also present in sn_all_vars and
3150 // sn_var_vals.
3151 if (is_script_local && vim9script)
3152 update_vim9_script_var(FALSE, di, tv, type);
3153 }
3154
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003155 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003156
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003158 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003159 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003160 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003162 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003164 if (copy || tv->v_type != VAR_STRING)
3165 {
3166 char_u *val = tv_get_string(tv);
3167
3168 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003169 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 if (di->di_tv.vval.v_string == NULL)
3171 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003172 }
3173 else
3174 {
3175 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003177 tv->vval.v_string = NULL;
3178 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003179 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003180 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003182 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003184 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003186#ifdef FEAT_SEARCH_EXTRA
3187 else if (STRCMP(varname, "hlsearch") == 0)
3188 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003190 redraw_all_later(SOME_VALID);
3191 }
3192#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003193 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003194 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003196 {
3197 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003198 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003199 }
3200 }
3201
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003203 }
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003204 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003205 {
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003206 // add a new variable
3207 if (vim9script && is_script_local && (flags & ASSIGN_NO_DECL))
3208 {
3209 semsg(_(e_unknown_variable_str), name);
3210 goto failed;
3211 }
3212
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003213 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003214 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003215 {
3216 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003217 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003218 }
3219
Bram Moolenaar03290b82020-12-19 16:30:44 +01003220 // Make sure the variable name is valid. In Vim9 script an autoload
3221 // variable must be prefixed with "g:".
3222 if (!valid_varname(varname, !vim9script
3223 || STRNCMP(name, "g:", 2) == 0))
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 = alloc(sizeof(dictitem_T) + STRLEN(varname));
3227 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003228 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 STRCPY(di->di_key, varname);
3230 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003231 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003233 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003236 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 di->di_flags |= DI_FLAGS_LOCK;
3238
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003239 // A Vim9 script-local variable is also added to sn_all_vars and
3240 // sn_var_vals.
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003241 if (is_script_local && vim9script)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003242 update_vim9_script_var(TRUE, di, tv, type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003243 }
3244
3245 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003247 else
3248 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 di->di_tv = *tv;
3250 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003251 init_tv(tv);
3252 }
3253
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003254 // ":const var = value" locks the value
3255 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003256 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003257 // Like :lockvar! name: lock the value and what it contains, but only
3258 // if the reference count is up to one. That locks only literal
3259 // values.
3260 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003261 return;
3262failed:
3263 if (!copy)
3264 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003265}
3266
3267/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003268 * Check in this order for backwards compatibility:
3269 * - Whether the variable is read-only
3270 * - Whether the variable value is locked
3271 * - Whether the variable is locked
3272 */
3273 int
3274var_check_permission(dictitem_T *di, char_u *name)
3275{
3276 if (var_check_ro(di->di_flags, name, FALSE)
3277 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3278 || var_check_lock(di->di_flags, name, FALSE))
3279 return FAIL;
3280 return OK;
3281}
3282
3283/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003284 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3285 * Also give an error message.
3286 */
3287 int
3288var_check_ro(int flags, char_u *name, int use_gettext)
3289{
3290 if (flags & DI_FLAGS_RO)
3291 {
3292 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3293 return TRUE;
3294 }
3295 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3296 {
3297 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3298 return TRUE;
3299 }
3300 return FALSE;
3301}
3302
3303/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003304 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3305 * Also give an error message.
3306 */
3307 int
3308var_check_lock(int flags, char_u *name, int use_gettext)
3309{
3310 if (flags & DI_FLAGS_LOCK)
3311 {
3312 semsg(_(e_variable_is_locked_str),
3313 use_gettext ? (char_u *)_(name) : name);
3314 return TRUE;
3315 }
3316 return FALSE;
3317}
3318
3319/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003320 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3321 * Also give an error message.
3322 */
3323 int
3324var_check_fixed(int flags, char_u *name, int use_gettext)
3325{
3326 if (flags & DI_FLAGS_FIX)
3327 {
3328 semsg(_("E795: Cannot delete variable %s"),
3329 use_gettext ? (char_u *)_(name) : name);
3330 return TRUE;
3331 }
3332 return FALSE;
3333}
3334
3335/*
3336 * Check if a funcref is assigned to a valid variable name.
3337 * Return TRUE and give an error if not.
3338 */
3339 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003340var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003341 char_u *name, // points to start of variable name
3342 int new_var) // TRUE when creating the variable
3343{
3344 // Allow for w: b: s: and t:.
3345 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3346 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3347 ? name[2] : name[0]))
3348 {
3349 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3350 name);
3351 return TRUE;
3352 }
3353 // Don't allow hiding a function. When "v" is not NULL we might be
3354 // assigning another function to the same var, the type is checked
3355 // below.
3356 if (new_var && function_exists(name, FALSE))
3357 {
3358 semsg(_("E705: Variable name conflicts with existing function: %s"),
3359 name);
3360 return TRUE;
3361 }
3362 return FALSE;
3363}
3364
3365/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003366 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3367 * value. Also give an error message, using "name" or _("name") when
3368 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003369 */
3370 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003371value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003372{
3373 if (lock & VAR_LOCKED)
3374 {
3375 semsg(_("E741: Value is locked: %s"),
3376 name == NULL ? (char_u *)_("Unknown")
3377 : use_gettext ? (char_u *)_(name)
3378 : name);
3379 return TRUE;
3380 }
3381 if (lock & VAR_FIXED)
3382 {
3383 semsg(_("E742: Cannot change value of %s"),
3384 name == NULL ? (char_u *)_("Unknown")
3385 : use_gettext ? (char_u *)_(name)
3386 : name);
3387 return TRUE;
3388 }
3389 return FALSE;
3390}
3391
3392/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003393 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003394 * Return FALSE and give an error if not.
3395 */
3396 int
Bram Moolenaar03290b82020-12-19 16:30:44 +01003397valid_varname(char_u *varname, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003398{
3399 char_u *p;
3400
3401 for (p = varname; *p != NUL; ++p)
3402 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003403 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003404 {
3405 semsg(_(e_illvar), varname);
3406 return FALSE;
3407 }
3408 return TRUE;
3409}
3410
3411/*
3412 * getwinvar() and gettabwinvar()
3413 */
3414 static void
3415getwinvar(
3416 typval_T *argvars,
3417 typval_T *rettv,
3418 int off) // 1 for gettabwinvar()
3419{
3420 win_T *win;
3421 char_u *varname;
3422 dictitem_T *v;
3423 tabpage_T *tp = NULL;
3424 int done = FALSE;
3425 win_T *oldcurwin;
3426 tabpage_T *oldtabpage;
3427 int need_switch_win;
3428
3429 if (off == 1)
3430 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3431 else
3432 tp = curtab;
3433 win = find_win_by_nr(&argvars[off], tp);
3434 varname = tv_get_string_chk(&argvars[off + 1]);
3435 ++emsg_off;
3436
3437 rettv->v_type = VAR_STRING;
3438 rettv->vval.v_string = NULL;
3439
3440 if (win != NULL && varname != NULL)
3441 {
3442 // Set curwin to be our win, temporarily. Also set the tabpage,
3443 // otherwise the window is not valid. Only do this when needed,
3444 // autocommands get blocked.
3445 need_switch_win = !(tp == curtab && win == curwin);
3446 if (!need_switch_win
3447 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3448 {
3449 if (*varname == '&')
3450 {
3451 if (varname[1] == NUL)
3452 {
3453 // get all window-local options in a dict
3454 dict_T *opts = get_winbuf_options(FALSE);
3455
3456 if (opts != NULL)
3457 {
3458 rettv_dict_set(rettv, opts);
3459 done = TRUE;
3460 }
3461 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003462 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003463 // window-local-option
3464 done = TRUE;
3465 }
3466 else
3467 {
3468 // Look up the variable.
3469 // Let getwinvar({nr}, "") return the "w:" dictionary.
3470 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3471 varname, FALSE);
3472 if (v != NULL)
3473 {
3474 copy_tv(&v->di_tv, rettv);
3475 done = TRUE;
3476 }
3477 }
3478 }
3479
3480 if (need_switch_win)
3481 // restore previous notion of curwin
3482 restore_win(oldcurwin, oldtabpage, TRUE);
3483 }
3484
3485 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3486 // use the default return value
3487 copy_tv(&argvars[off + 2], rettv);
3488
3489 --emsg_off;
3490}
3491
3492/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003493 * Set option "varname" to the value of "varp" for the current buffer/window.
3494 */
3495 static void
3496set_option_from_tv(char_u *varname, typval_T *varp)
3497{
3498 long numval = 0;
3499 char_u *strval;
3500 char_u nbuf[NUMBUFLEN];
3501 int error = FALSE;
3502
3503 if (!in_vim9script() || varp->v_type != VAR_STRING)
3504 numval = (long)tv_get_number_chk(varp, &error);
3505 strval = tv_get_string_buf_chk(varp, nbuf);
3506 if (!error && strval != NULL)
3507 set_option_value(varname, numval, strval, OPT_LOCAL);
3508}
3509
3510/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003511 * "setwinvar()" and "settabwinvar()" functions
3512 */
3513 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003514setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003515{
3516 win_T *win;
3517 win_T *save_curwin;
3518 tabpage_T *save_curtab;
3519 int need_switch_win;
3520 char_u *varname, *winvarname;
3521 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003522 tabpage_T *tp = NULL;
3523
3524 if (check_secure())
3525 return;
3526
3527 if (off == 1)
3528 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3529 else
3530 tp = curtab;
3531 win = find_win_by_nr(&argvars[off], tp);
3532 varname = tv_get_string_chk(&argvars[off + 1]);
3533 varp = &argvars[off + 2];
3534
3535 if (win != NULL && varname != NULL && varp != NULL)
3536 {
3537 need_switch_win = !(tp == curtab && win == curwin);
3538 if (!need_switch_win
3539 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3540 {
3541 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003542 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003543 else
3544 {
3545 winvarname = alloc(STRLEN(varname) + 3);
3546 if (winvarname != NULL)
3547 {
3548 STRCPY(winvarname, "w:");
3549 STRCPY(winvarname + 2, varname);
3550 set_var(winvarname, varp, TRUE);
3551 vim_free(winvarname);
3552 }
3553 }
3554 }
3555 if (need_switch_win)
3556 restore_win(save_curwin, save_curtab, TRUE);
3557 }
3558}
3559
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003560/*
3561 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3562 * v:option_type, and v:option_command.
3563 */
3564 void
3565reset_v_option_vars(void)
3566{
3567 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3568 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3569 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3570 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3571 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3572 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3573}
3574
3575/*
3576 * Add an assert error to v:errors.
3577 */
3578 void
3579assert_error(garray_T *gap)
3580{
3581 struct vimvar *vp = &vimvars[VV_ERRORS];
3582
3583 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003584 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003585 set_vim_var_list(VV_ERRORS, list_alloc());
3586 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3587}
3588
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003589 int
3590var_exists(char_u *var)
3591{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003592 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003593 char_u *name;
3594 char_u *tofree;
3595 typval_T tv;
3596 int len = 0;
3597 int n = FALSE;
3598
3599 // get_name_len() takes care of expanding curly braces
3600 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003601 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003602 if (len > 0)
3603 {
3604 if (tofree != NULL)
3605 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003606 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003607 if (n)
3608 {
3609 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003610 arg = skipwhite(arg);
3611 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003612 if (n)
3613 clear_tv(&tv);
3614 }
3615 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003616 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003617 n = FALSE;
3618
3619 vim_free(tofree);
3620 return n;
3621}
3622
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003623static lval_T *redir_lval = NULL;
3624#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3625static garray_T redir_ga; // only valid when redir_lval is not NULL
3626static char_u *redir_endp = NULL;
3627static char_u *redir_varname = NULL;
3628
3629/*
3630 * Start recording command output to a variable
3631 * When "append" is TRUE append to an existing variable.
3632 * Returns OK if successfully completed the setup. FAIL otherwise.
3633 */
3634 int
3635var_redir_start(char_u *name, int append)
3636{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003637 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003638 typval_T tv;
3639
3640 // Catch a bad name early.
3641 if (!eval_isnamec1(*name))
3642 {
3643 emsg(_(e_invarg));
3644 return FAIL;
3645 }
3646
3647 // Make a copy of the name, it is used in redir_lval until redir ends.
3648 redir_varname = vim_strsave(name);
3649 if (redir_varname == NULL)
3650 return FAIL;
3651
3652 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3653 if (redir_lval == NULL)
3654 {
3655 var_redir_stop();
3656 return FAIL;
3657 }
3658
3659 // The output is stored in growarray "redir_ga" until redirection ends.
3660 ga_init2(&redir_ga, (int)sizeof(char), 500);
3661
3662 // Parse the variable name (can be a dict or list entry).
3663 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3664 FNE_CHECK_START);
3665 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3666 {
3667 clear_lval(redir_lval);
3668 if (redir_endp != NULL && *redir_endp != NUL)
3669 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003670 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003671 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003672 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003673 redir_endp = NULL; // don't store a value, only cleanup
3674 var_redir_stop();
3675 return FAIL;
3676 }
3677
3678 // check if we can write to the variable: set it to or append an empty
3679 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003680 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003681 tv.v_type = VAR_STRING;
3682 tv.vval.v_string = (char_u *)"";
3683 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003684 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3685 ASSIGN_NO_DECL, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003686 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003687 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3688 ASSIGN_NO_DECL, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003689 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003690 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003691 {
3692 redir_endp = NULL; // don't store a value, only cleanup
3693 var_redir_stop();
3694 return FAIL;
3695 }
3696
3697 return OK;
3698}
3699
3700/*
3701 * Append "value[value_len]" to the variable set by var_redir_start().
3702 * The actual appending is postponed until redirection ends, because the value
3703 * appended may in fact be the string we write to, changing it may cause freed
3704 * memory to be used:
3705 * :redir => foo
3706 * :let foo
3707 * :redir END
3708 */
3709 void
3710var_redir_str(char_u *value, int value_len)
3711{
3712 int len;
3713
3714 if (redir_lval == NULL)
3715 return;
3716
3717 if (value_len == -1)
3718 len = (int)STRLEN(value); // Append the entire string
3719 else
3720 len = value_len; // Append only "value_len" characters
3721
3722 if (ga_grow(&redir_ga, len) == OK)
3723 {
3724 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3725 redir_ga.ga_len += len;
3726 }
3727 else
3728 var_redir_stop();
3729}
3730
3731/*
3732 * Stop redirecting command output to a variable.
3733 * Frees the allocated memory.
3734 */
3735 void
3736var_redir_stop(void)
3737{
3738 typval_T tv;
3739
3740 if (EVALCMD_BUSY)
3741 {
3742 redir_lval = NULL;
3743 return;
3744 }
3745
3746 if (redir_lval != NULL)
3747 {
3748 // If there was no error: assign the text to the variable.
3749 if (redir_endp != NULL)
3750 {
3751 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3752 tv.v_type = VAR_STRING;
3753 tv.vval.v_string = redir_ga.ga_data;
3754 // Call get_lval() again, if it's inside a Dict or List it may
3755 // have changed.
3756 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3757 FALSE, FALSE, 0, FNE_CHECK_START);
3758 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003759 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003760 (char_u *)".");
3761 clear_lval(redir_lval);
3762 }
3763
3764 // free the collected output
3765 VIM_CLEAR(redir_ga.ga_data);
3766
3767 VIM_CLEAR(redir_lval);
3768 }
3769 VIM_CLEAR(redir_varname);
3770}
3771
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003772/*
3773 * "gettabvar()" function
3774 */
3775 void
3776f_gettabvar(typval_T *argvars, typval_T *rettv)
3777{
3778 win_T *oldcurwin;
3779 tabpage_T *tp, *oldtabpage;
3780 dictitem_T *v;
3781 char_u *varname;
3782 int done = FALSE;
3783
3784 rettv->v_type = VAR_STRING;
3785 rettv->vval.v_string = NULL;
3786
3787 varname = tv_get_string_chk(&argvars[1]);
3788 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3789 if (tp != NULL && varname != NULL)
3790 {
3791 // Set tp to be our tabpage, temporarily. Also set the window to the
3792 // first window in the tabpage, otherwise the window is not valid.
3793 if (switch_win(&oldcurwin, &oldtabpage,
3794 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3795 : tp->tp_firstwin, tp, TRUE) == OK)
3796 {
3797 // look up the variable
3798 // Let gettabvar({nr}, "") return the "t:" dictionary.
3799 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3800 if (v != NULL)
3801 {
3802 copy_tv(&v->di_tv, rettv);
3803 done = TRUE;
3804 }
3805 }
3806
3807 // restore previous notion of curwin
3808 restore_win(oldcurwin, oldtabpage, TRUE);
3809 }
3810
3811 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3812 copy_tv(&argvars[2], rettv);
3813}
3814
3815/*
3816 * "gettabwinvar()" function
3817 */
3818 void
3819f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3820{
3821 getwinvar(argvars, rettv, 1);
3822}
3823
3824/*
3825 * "getwinvar()" function
3826 */
3827 void
3828f_getwinvar(typval_T *argvars, typval_T *rettv)
3829{
3830 getwinvar(argvars, rettv, 0);
3831}
3832
3833/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003834 * "getbufvar()" function
3835 */
3836 void
3837f_getbufvar(typval_T *argvars, typval_T *rettv)
3838{
3839 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003840 char_u *varname;
3841 dictitem_T *v;
3842 int done = FALSE;
3843
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003844 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003845 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003846
3847 rettv->v_type = VAR_STRING;
3848 rettv->vval.v_string = NULL;
3849
3850 if (buf != NULL && varname != NULL)
3851 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003852 if (*varname == '&')
3853 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003854 buf_T *save_curbuf = curbuf;
3855
3856 // set curbuf to be our buf, temporarily
3857 curbuf = buf;
3858
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003859 if (varname[1] == NUL)
3860 {
3861 // get all buffer-local options in a dict
3862 dict_T *opts = get_winbuf_options(TRUE);
3863
3864 if (opts != NULL)
3865 {
3866 rettv_dict_set(rettv, opts);
3867 done = TRUE;
3868 }
3869 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003870 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003871 // buffer-local-option
3872 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003873
3874 // restore previous notion of curbuf
3875 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003876 }
3877 else
3878 {
3879 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003880 if (*varname == NUL)
3881 // Let getbufvar({nr}, "") return the "b:" dictionary.
3882 v = &buf->b_bufvar;
3883 else
3884 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3885 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003886 if (v != NULL)
3887 {
3888 copy_tv(&v->di_tv, rettv);
3889 done = TRUE;
3890 }
3891 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003892 }
3893
3894 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3895 // use the default value
3896 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003897}
3898
3899/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003900 * "settabvar()" function
3901 */
3902 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003903f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003904{
3905 tabpage_T *save_curtab;
3906 tabpage_T *tp;
3907 char_u *varname, *tabvarname;
3908 typval_T *varp;
3909
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003910 if (check_secure())
3911 return;
3912
3913 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3914 varname = tv_get_string_chk(&argvars[1]);
3915 varp = &argvars[2];
3916
3917 if (varname != NULL && varp != NULL && tp != NULL)
3918 {
3919 save_curtab = curtab;
3920 goto_tabpage_tp(tp, FALSE, FALSE);
3921
3922 tabvarname = alloc(STRLEN(varname) + 3);
3923 if (tabvarname != NULL)
3924 {
3925 STRCPY(tabvarname, "t:");
3926 STRCPY(tabvarname + 2, varname);
3927 set_var(tabvarname, varp, TRUE);
3928 vim_free(tabvarname);
3929 }
3930
3931 // Restore current tabpage
3932 if (valid_tabpage(save_curtab))
3933 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3934 }
3935}
3936
3937/*
3938 * "settabwinvar()" function
3939 */
3940 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003941f_settabwinvar(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, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003944}
3945
3946/*
3947 * "setwinvar()" function
3948 */
3949 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003950f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003951{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003952 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003953}
3954
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003955/*
3956 * "setbufvar()" function
3957 */
3958 void
3959f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3960{
3961 buf_T *buf;
3962 char_u *varname, *bufvarname;
3963 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003964
3965 if (check_secure())
3966 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003967 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003968 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003969 varp = &argvars[2];
3970
3971 if (buf != NULL && varname != NULL && varp != NULL)
3972 {
3973 if (*varname == '&')
3974 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003975 aco_save_T aco;
3976
3977 // set curbuf to be our buf, temporarily
3978 aucmd_prepbuf(&aco, buf);
3979
Bram Moolenaar191929b2020-08-19 21:20:49 +02003980 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003981
3982 // reset notion of buffer
3983 aucmd_restbuf(&aco);
3984 }
3985 else
3986 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003987 bufvarname = alloc(STRLEN(varname) + 3);
3988 if (bufvarname != NULL)
3989 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003990 buf_T *save_curbuf = curbuf;
3991
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003992 curbuf = buf;
3993 STRCPY(bufvarname, "b:");
3994 STRCPY(bufvarname + 2, varname);
3995 set_var(bufvarname, varp, TRUE);
3996 vim_free(bufvarname);
3997 curbuf = save_curbuf;
3998 }
3999 }
4000 }
4001}
4002
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004003/*
4004 * Get a callback from "arg". It can be a Funcref or a function name.
4005 * When "arg" is zero return an empty string.
4006 * "cb_name" is not allocated.
4007 * "cb_name" is set to NULL for an invalid argument.
4008 */
4009 callback_T
4010get_callback(typval_T *arg)
4011{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004012 callback_T res;
4013 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004014
4015 res.cb_free_name = FALSE;
4016 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4017 {
4018 res.cb_partial = arg->vval.v_partial;
4019 ++res.cb_partial->pt_refcount;
4020 res.cb_name = partial_name(res.cb_partial);
4021 }
4022 else
4023 {
4024 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004025 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4026 && isdigit(*arg->vval.v_string))
4027 r = FAIL;
4028 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004029 {
4030 // Note that we don't make a copy of the string.
4031 res.cb_name = arg->vval.v_string;
4032 func_ref(res.cb_name);
4033 }
4034 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004035 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004036 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004037 r = FAIL;
4038
4039 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004040 {
4041 emsg(_("E921: Invalid callback argument"));
4042 res.cb_name = NULL;
4043 }
4044 }
4045 return res;
4046}
4047
4048/*
4049 * Copy a callback into a typval_T.
4050 */
4051 void
4052put_callback(callback_T *cb, typval_T *tv)
4053{
4054 if (cb->cb_partial != NULL)
4055 {
4056 tv->v_type = VAR_PARTIAL;
4057 tv->vval.v_partial = cb->cb_partial;
4058 ++tv->vval.v_partial->pt_refcount;
4059 }
4060 else
4061 {
4062 tv->v_type = VAR_FUNC;
4063 tv->vval.v_string = vim_strsave(cb->cb_name);
4064 func_ref(cb->cb_name);
4065 }
4066}
4067
4068/*
4069 * Make a copy of "src" into "dest", allocating the function name if needed,
4070 * without incrementing the refcount.
4071 */
4072 void
4073set_callback(callback_T *dest, callback_T *src)
4074{
4075 if (src->cb_partial == NULL)
4076 {
4077 // just a function name, make a copy
4078 dest->cb_name = vim_strsave(src->cb_name);
4079 dest->cb_free_name = TRUE;
4080 }
4081 else
4082 {
4083 // cb_name is a pointer into cb_partial
4084 dest->cb_name = src->cb_name;
4085 dest->cb_free_name = FALSE;
4086 }
4087 dest->cb_partial = src->cb_partial;
4088}
4089
4090/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004091 * Copy callback from "src" to "dest", incrementing the refcounts.
4092 */
4093 void
4094copy_callback(callback_T *dest, callback_T *src)
4095{
4096 dest->cb_partial = src->cb_partial;
4097 if (dest->cb_partial != NULL)
4098 {
4099 dest->cb_name = src->cb_name;
4100 dest->cb_free_name = FALSE;
4101 ++dest->cb_partial->pt_refcount;
4102 }
4103 else
4104 {
4105 dest->cb_name = vim_strsave(src->cb_name);
4106 dest->cb_free_name = TRUE;
4107 func_ref(src->cb_name);
4108 }
4109}
4110
4111/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004112 * Unref/free "callback" returned by get_callback() or set_callback().
4113 */
4114 void
4115free_callback(callback_T *callback)
4116{
4117 if (callback->cb_partial != NULL)
4118 {
4119 partial_unref(callback->cb_partial);
4120 callback->cb_partial = NULL;
4121 }
4122 else if (callback->cb_name != NULL)
4123 func_unref(callback->cb_name);
4124 if (callback->cb_free_name)
4125 {
4126 vim_free(callback->cb_name);
4127 callback->cb_free_name = FALSE;
4128 }
4129 callback->cb_name = NULL;
4130}
4131
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004132#endif // FEAT_EVAL