blob: 7c4cc7a1a815a58d981cc70f678892cc8aefbbd9 [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 if (eap->cmdidx == CMD_const && !vim9script && !eap->forceit)
756 // In legacy Vim script ":const" works like ":final".
757 eap->cmdidx = CMD_final;
758
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100759 if (eap->cmdidx == CMD_const)
760 flags |= ASSIGN_CONST;
761 else if (eap->cmdidx == CMD_final)
762 flags |= ASSIGN_FINAL;
763
764 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200766 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100767
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200768 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200769 if (argend == NULL)
770 return;
771 if (argend > arg && argend[-1] == '.') // for var.='str'
772 --argend;
773 expr = skipwhite(argend);
774 concat = expr[0] == '.'
775 && ((expr[1] == '=' && current_sctx.sc_version < 2)
776 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200777 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
778 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200779 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200780 {
781 // ":let" without "=": list variables
782 if (*arg == '[')
783 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200784 else if (expr[0] == '.' && expr[1] == '=')
785 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200786 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200787 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200788 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200789 {
790 // Vim9 declaration ":let var: type"
791 arg = vim9_declare_scriptvar(eap, arg);
792 }
793 else
794 {
795 // ":let var1 var2" - list values
796 arg = list_arg_vars(eap, arg, &first);
797 }
798 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200799 else if (!eap->skip)
800 {
801 // ":let"
802 list_glob_vars(&first);
803 list_buf_vars(&first);
804 list_win_vars(&first);
805 list_tab_vars(&first);
806 list_script_vars(&first);
807 list_func_vars(&first);
808 list_vim_vars(&first);
809 }
810 eap->nextcmd = check_nextcmd(arg);
811 }
812 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
813 {
814 list_T *l;
815
816 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200817 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200818 if (l != NULL)
819 {
820 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200821 if (!eap->skip)
822 {
823 op[0] = '=';
824 op[1] = NUL;
825 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200827 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200828 clear_tv(&rettv);
829 }
830 }
831 else
832 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200833 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200834 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200835
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200836 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200837 i = FAIL;
838 if (has_assign || concat)
839 {
840 op[0] = '=';
841 op[1] = NUL;
842 if (*expr != '=')
843 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200844 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200845 {
846 // +=, /=, etc. require an existing variable
847 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
848 i = FAIL;
849 }
850 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200851 {
852 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200853 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200854 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200855 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200856 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200857 ++len;
858 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200859 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200860 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200861 }
862 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200863 ++expr;
864
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200865 if (vim9script && (!VIM_ISWHITE(*argend)
866 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200867 {
868 vim_strncpy(op, expr - len, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200869 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200870 i = FAIL;
871 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200872
873 if (eap->skip)
874 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200875 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200876 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200877 if (getline_equal(eap->getline, eap->cookie, getsourceline))
878 {
879 evalarg.eval_getline = eap->getline;
880 evalarg.eval_cookie = eap->cookie;
881 }
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200882 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200883 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200884 if (eap->skip)
885 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200886 clear_evalarg(&evalarg, eap);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200887 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200888 if (eap->skip)
889 {
890 if (i != FAIL)
891 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200892 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200893 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200894 {
895 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200896 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200897 clear_tv(&rettv);
898 }
899 }
900}
901
902/*
903 * Assign the typevalue "tv" to the variable or variables at "arg_start".
904 * Handles both "var" with any type and "[var, var; var]" with a list type.
905 * When "op" is not NULL it points to a string with characters that
906 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
907 * or concatenate.
908 * Returns OK or FAIL;
909 */
910 int
911ex_let_vars(
912 char_u *arg_start,
913 typval_T *tv,
914 int copy, // copy values from "tv", don't move
915 int semicolon, // from skip_var_list()
916 int var_count, // from skip_var_list()
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100917 int flags, // ASSIGN_FINAL, ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200918 char_u *op)
919{
920 char_u *arg = arg_start;
921 list_T *l;
922 int i;
923 listitem_T *item;
924 typval_T ltv;
925
926 if (*arg != '[')
927 {
928 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100929 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200930 return FAIL;
931 return OK;
932 }
933
934 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
935 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
936 {
937 emsg(_(e_listreq));
938 return FAIL;
939 }
940
941 i = list_len(l);
942 if (semicolon == 0 && var_count < i)
943 {
944 emsg(_("E687: Less targets than List items"));
945 return FAIL;
946 }
947 if (var_count - semicolon > i)
948 {
949 emsg(_("E688: More targets than List items"));
950 return FAIL;
951 }
952
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200953 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200954 item = l->lv_first;
955 while (*arg != ']')
956 {
957 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200959 item = item->li_next;
960 if (arg == NULL)
961 return FAIL;
962
963 arg = skipwhite(arg);
964 if (*arg == ';')
965 {
966 // Put the rest of the list (may be empty) in the var after ';'.
967 // Create a new list for this.
968 l = list_alloc();
969 if (l == NULL)
970 return FAIL;
971 while (item != NULL)
972 {
973 list_append_tv(l, &item->li_tv);
974 item = item->li_next;
975 }
976
977 ltv.v_type = VAR_LIST;
978 ltv.v_lock = 0;
979 ltv.vval.v_list = l;
980 l->lv_refcount = 1;
981
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100982 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200983 (char_u *)"]", op);
984 clear_tv(&ltv);
985 if (arg == NULL)
986 return FAIL;
987 break;
988 }
989 else if (*arg != ',' && *arg != ']')
990 {
991 internal_error("ex_let_vars()");
992 return FAIL;
993 }
994 }
995
996 return OK;
997}
998
999/*
1000 * Skip over assignable variable "var" or list of variables "[var, var]".
1001 * Used for ":let varvar = expr" and ":for varvar in expr".
1002 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001003 * for "[var, var; var]" set "semicolon" to 1.
1004 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001005 * Return NULL for an error.
1006 */
1007 char_u *
1008skip_var_list(
1009 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001010 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001011 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001012 int *semicolon,
1013 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001014{
1015 char_u *p, *s;
1016
1017 if (*arg == '[')
1018 {
1019 // "[var, var]": find the matching ']'.
1020 p = arg;
1021 for (;;)
1022 {
1023 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001024 s = skip_var_one(p, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001025 if (s == p)
1026 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001027 if (!silent)
1028 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001029 return NULL;
1030 }
1031 ++*var_count;
1032
1033 p = skipwhite(s);
1034 if (*p == ']')
1035 break;
1036 else if (*p == ';')
1037 {
1038 if (*semicolon == 1)
1039 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001040 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001041 return NULL;
1042 }
1043 *semicolon = 1;
1044 }
1045 else if (*p != ',')
1046 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001047 if (!silent)
1048 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001049 return NULL;
1050 }
1051 }
1052 return p + 1;
1053 }
1054 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001056}
1057
1058/*
1059 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1060 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001061 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001062 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001063 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001064skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001065{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066 char_u *end;
1067
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001068 if (*arg == '@' && arg[1] != NUL)
1069 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001070 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001071 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001072 if (include_type && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001073 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001074 // "a: type" is declaring variable "a" with a type, not "a:".
1075 if (end == arg + 2 && end[-1] == ':')
1076 --end;
1077 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001078 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001079 }
1080 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001081}
1082
1083/*
1084 * List variables for hashtab "ht" with prefix "prefix".
1085 * If "empty" is TRUE also list NULL strings as empty strings.
1086 */
1087 void
1088list_hashtable_vars(
1089 hashtab_T *ht,
1090 char *prefix,
1091 int empty,
1092 int *first)
1093{
1094 hashitem_T *hi;
1095 dictitem_T *di;
1096 int todo;
1097 char_u buf[IOSIZE];
1098
1099 todo = (int)ht->ht_used;
1100 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1101 {
1102 if (!HASHITEM_EMPTY(hi))
1103 {
1104 --todo;
1105 di = HI2DI(hi);
1106
1107 // apply :filter /pat/ to variable name
1108 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1109 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1110 if (message_filtered(buf))
1111 continue;
1112
1113 if (empty || di->di_tv.v_type != VAR_STRING
1114 || di->di_tv.vval.v_string != NULL)
1115 list_one_var(di, prefix, first);
1116 }
1117 }
1118}
1119
1120/*
1121 * List global variables.
1122 */
1123 static void
1124list_glob_vars(int *first)
1125{
1126 list_hashtable_vars(&globvarht, "", TRUE, first);
1127}
1128
1129/*
1130 * List buffer variables.
1131 */
1132 static void
1133list_buf_vars(int *first)
1134{
1135 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1136}
1137
1138/*
1139 * List window variables.
1140 */
1141 static void
1142list_win_vars(int *first)
1143{
1144 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1145}
1146
1147/*
1148 * List tab page variables.
1149 */
1150 static void
1151list_tab_vars(int *first)
1152{
1153 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1154}
1155
1156/*
1157 * List variables in "arg".
1158 */
1159 static char_u *
1160list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1161{
1162 int error = FALSE;
1163 int len;
1164 char_u *name;
1165 char_u *name_start;
1166 char_u *arg_subsc;
1167 char_u *tofree;
1168 typval_T tv;
1169
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001170 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001171 {
1172 if (error || eap->skip)
1173 {
1174 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1175 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1176 {
1177 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001178 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001179 break;
1180 }
1181 }
1182 else
1183 {
1184 // get_name_len() takes care of expanding curly braces
1185 name_start = name = arg;
1186 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1187 if (len <= 0)
1188 {
1189 // This is mainly to keep test 49 working: when expanding
1190 // curly braces fails overrule the exception error message.
1191 if (len < 0 && !aborting())
1192 {
1193 emsg_severe = TRUE;
1194 semsg(_(e_invarg2), arg);
1195 break;
1196 }
1197 error = TRUE;
1198 }
1199 else
1200 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001201 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001202 if (tofree != NULL)
1203 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001204 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001205 error = TRUE;
1206 else
1207 {
1208 // handle d.key, l[idx], f(expr)
1209 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001210 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001211 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001212 error = TRUE;
1213 else
1214 {
1215 if (arg == arg_subsc && len == 2 && name[1] == ':')
1216 {
1217 switch (*name)
1218 {
1219 case 'g': list_glob_vars(first); break;
1220 case 'b': list_buf_vars(first); break;
1221 case 'w': list_win_vars(first); break;
1222 case 't': list_tab_vars(first); break;
1223 case 'v': list_vim_vars(first); break;
1224 case 's': list_script_vars(first); break;
1225 case 'l': list_func_vars(first); break;
1226 default:
1227 semsg(_("E738: Can't list variables for %s"), name);
1228 }
1229 }
1230 else
1231 {
1232 char_u numbuf[NUMBUFLEN];
1233 char_u *tf;
1234 int c;
1235 char_u *s;
1236
1237 s = echo_string(&tv, &tf, numbuf, 0);
1238 c = *arg;
1239 *arg = NUL;
1240 list_one_var_a("",
1241 arg == arg_subsc ? name : name_start,
1242 tv.v_type,
1243 s == NULL ? (char_u *)"" : s,
1244 first);
1245 *arg = c;
1246 vim_free(tf);
1247 }
1248 clear_tv(&tv);
1249 }
1250 }
1251 }
1252
1253 vim_free(tofree);
1254 }
1255
1256 arg = skipwhite(arg);
1257 }
1258
1259 return arg;
1260}
1261
1262/*
1263 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1264 * Returns a pointer to the char just after the var name.
1265 * Returns NULL if there is an error.
1266 */
1267 static char_u *
1268ex_let_one(
1269 char_u *arg, // points to variable name
1270 typval_T *tv, // value to assign to variable
1271 int copy, // copy value from "tv"
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001272 int flags, // ASSIGN_CONST, ASSIGN_FINAL, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001273 char_u *endchars, // valid chars after variable name or NULL
1274 char_u *op) // "+", "-", "." or NULL
1275{
1276 int c1;
1277 char_u *name;
1278 char_u *p;
1279 char_u *arg_end = NULL;
1280 int len;
1281 int opt_flags;
1282 char_u *tofree = NULL;
1283
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001284 if (in_vim9script() && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001285 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001286 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1287 {
1288 vim9_declare_error(arg);
1289 return NULL;
1290 }
1291
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001292 // ":let $VAR = expr": Set environment variable.
1293 if (*arg == '$')
1294 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001295 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001296 {
1297 emsg(_("E996: Cannot lock an environment variable"));
1298 return NULL;
1299 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001300
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001301 // Find the end of the name.
1302 ++arg;
1303 name = arg;
1304 len = get_env_len(&arg);
1305 if (len == 0)
1306 semsg(_(e_invarg2), name - 1);
1307 else
1308 {
1309 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1310 semsg(_(e_letwrong), op);
1311 else if (endchars != NULL
1312 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1313 emsg(_(e_letunexp));
1314 else if (!check_secure())
1315 {
1316 c1 = name[len];
1317 name[len] = NUL;
1318 p = tv_get_string_chk(tv);
1319 if (p != NULL && op != NULL && *op == '.')
1320 {
1321 int mustfree = FALSE;
1322 char_u *s = vim_getenv(name, &mustfree);
1323
1324 if (s != NULL)
1325 {
1326 p = tofree = concat_str(s, p);
1327 if (mustfree)
1328 vim_free(s);
1329 }
1330 }
1331 if (p != NULL)
1332 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001333 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001334 arg_end = arg;
1335 }
1336 name[len] = c1;
1337 vim_free(tofree);
1338 }
1339 }
1340 }
1341
1342 // ":let &option = expr": Set option value.
1343 // ":let &l:option = expr": Set local option value.
1344 // ":let &g:option = expr": Set global option value.
1345 else if (*arg == '&')
1346 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001347 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001348 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001350 return NULL;
1351 }
1352 // Find the end of the name.
1353 p = find_option_end(&arg, &opt_flags);
1354 if (p == NULL || (endchars != NULL
1355 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1356 emsg(_(e_letunexp));
1357 else
1358 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001359 long n = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001360 int opt_type;
1361 long numval;
1362 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001363 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001364 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001365
1366 c1 = *p;
1367 *p = NUL;
1368
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001369 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
1370 if ((opt_type == 1 || opt_type == -1)
1371 && (tv->v_type != VAR_STRING || !in_vim9script()))
1372 // number, possibly hidden
1373 n = (long)tv_get_number(tv);
1374
1375 // Avoid setting a string option to the text "v:false" or similar.
1376 // In Vim9 script also don't convert a number to string.
1377 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1378 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1379 s = tv_get_string_chk(tv);
1380
1381 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001382 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001383 if ((opt_type == 1 && *op == '.')
1384 || (opt_type == 0 && *op != '.'))
1385 {
1386 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001387 failed = TRUE; // don't set the value
1388
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001389 }
1390 else
1391 {
1392 if (opt_type == 1) // number
1393 {
1394 switch (*op)
1395 {
1396 case '+': n = numval + n; break;
1397 case '-': n = numval - n; break;
1398 case '*': n = numval * n; break;
1399 case '/': n = (long)num_divide(numval, n); break;
1400 case '%': n = (long)num_modulus(numval, n); break;
1401 }
1402 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001403 else if (opt_type == 0 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001404 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001405 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001406 s = concat_str(stringval, s);
1407 vim_free(stringval);
1408 stringval = s;
1409 }
1410 }
1411 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001412
1413 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001414 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001415 if (opt_type != 0 || s != NULL)
1416 {
1417 set_option_value(arg, n, s, opt_flags);
1418 arg_end = p;
1419 }
1420 else
1421 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001422 }
1423 *p = c1;
1424 vim_free(stringval);
1425 }
1426 }
1427
1428 // ":let @r = expr": Set register contents.
1429 else if (*arg == '@')
1430 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001431 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001432 {
1433 emsg(_("E996: Cannot lock a register"));
1434 return NULL;
1435 }
1436 ++arg;
1437 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1438 semsg(_(e_letwrong), op);
1439 else if (endchars != NULL
1440 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1441 emsg(_(e_letunexp));
1442 else
1443 {
1444 char_u *ptofree = NULL;
1445 char_u *s;
1446
1447 p = tv_get_string_chk(tv);
1448 if (p != NULL && op != NULL && *op == '.')
1449 {
1450 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1451 if (s != NULL)
1452 {
1453 p = ptofree = concat_str(s, p);
1454 vim_free(s);
1455 }
1456 }
1457 if (p != NULL)
1458 {
1459 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1460 arg_end = arg + 1;
1461 }
1462 vim_free(ptofree);
1463 }
1464 }
1465
1466 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001467 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001468 // ":let {expr} = expr": Idem, name made with curly braces
1469 else if (eval_isnamec1(*arg) || *arg == '{')
1470 {
1471 lval_T lv;
1472
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001473 p = get_lval(arg, tv, &lv, FALSE, FALSE,
1474 (flags & ASSIGN_NO_DECL) ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001475 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001476 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001477 if (endchars != NULL && vim_strchr(endchars,
1478 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001479 emsg(_(e_letunexp));
1480 else
1481 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001483 arg_end = p;
1484 }
1485 }
1486 clear_lval(&lv);
1487 }
1488
1489 else
1490 semsg(_(e_invarg2), arg);
1491
1492 return arg_end;
1493}
1494
1495/*
1496 * ":unlet[!] var1 ... " command.
1497 */
1498 void
1499ex_unlet(exarg_T *eap)
1500{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001501 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001502}
1503
1504/*
1505 * ":lockvar" and ":unlockvar" commands
1506 */
1507 void
1508ex_lockvar(exarg_T *eap)
1509{
1510 char_u *arg = eap->arg;
1511 int deep = 2;
1512
1513 if (eap->forceit)
1514 deep = -1;
1515 else if (vim_isdigit(*arg))
1516 {
1517 deep = getdigits(&arg);
1518 arg = skipwhite(arg);
1519 }
1520
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001521 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001522}
1523
1524/*
1525 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001526 * Also used for Vim9 script. "callback" is invoked as:
1527 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001528 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001529 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001530ex_unletlock(
1531 exarg_T *eap,
1532 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001533 int deep,
1534 int glv_flags,
1535 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1536 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001537{
1538 char_u *arg = argstart;
1539 char_u *name_end;
1540 int error = FALSE;
1541 lval_T lv;
1542
1543 do
1544 {
1545 if (*arg == '$')
1546 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001547 lv.ll_name = arg;
1548 lv.ll_tv = NULL;
1549 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001550 if (get_env_len(&arg) == 0)
1551 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001552 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001553 return;
1554 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001555 if (!error && !eap->skip
1556 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1557 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001558 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001559 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001560 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001561 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001562 // Parse the name and find the end.
1563 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1564 glv_flags, FNE_CHECK_START);
1565 if (lv.ll_name == NULL)
1566 error = TRUE; // error but continue parsing
1567 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1568 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001569 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001570 if (name_end != NULL)
1571 {
1572 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001573 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001574 }
1575 if (!(eap->skip || error))
1576 clear_lval(&lv);
1577 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001578 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001579
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001580 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001581 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001582 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001583
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001584 if (!eap->skip)
1585 clear_lval(&lv);
1586 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001587
1588 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001589 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001590
1591 eap->nextcmd = check_nextcmd(arg);
1592}
1593
1594 static int
1595do_unlet_var(
1596 lval_T *lp,
1597 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001598 exarg_T *eap,
1599 int deep UNUSED,
1600 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001601{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001602 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001603 int ret = OK;
1604 int cc;
1605
1606 if (lp->ll_tv == NULL)
1607 {
1608 cc = *name_end;
1609 *name_end = NUL;
1610
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001611 // Environment variable, normal name or expanded name.
1612 if (*lp->ll_name == '$')
1613 vim_unsetenv(lp->ll_name + 1);
1614 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001615 ret = FAIL;
1616 *name_end = cc;
1617 }
1618 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001619 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001620 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001621 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001622 return FAIL;
1623 else if (lp->ll_range)
1624 {
1625 listitem_T *li;
1626 listitem_T *ll_li = lp->ll_li;
1627 int ll_n1 = lp->ll_n1;
1628
1629 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1630 {
1631 li = ll_li->li_next;
Bram Moolenaara187c432020-09-16 21:08:28 +02001632 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001633 return FAIL;
1634 ll_li = li;
1635 ++ll_n1;
1636 }
1637
1638 // Delete a range of List items.
1639 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1640 {
1641 li = lp->ll_li->li_next;
1642 listitem_remove(lp->ll_list, lp->ll_li);
1643 lp->ll_li = li;
1644 ++lp->ll_n1;
1645 }
1646 }
1647 else
1648 {
1649 if (lp->ll_list != NULL)
1650 // unlet a List item.
1651 listitem_remove(lp->ll_list, lp->ll_li);
1652 else
1653 // unlet a Dictionary item.
1654 dictitem_remove(lp->ll_dict, lp->ll_di);
1655 }
1656
1657 return ret;
1658}
1659
1660/*
1661 * "unlet" a variable. Return OK if it existed, FAIL if not.
1662 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1663 */
1664 int
1665do_unlet(char_u *name, int forceit)
1666{
1667 hashtab_T *ht;
1668 hashitem_T *hi;
1669 char_u *varname;
1670 dict_T *d;
1671 dictitem_T *di;
1672
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001673 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001674 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001675 return FAIL;
1676
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001677 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001678
1679 // can't :unlet a script variable in Vim9 script from a function
1680 if (ht == get_script_local_ht()
1681 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1682 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1683 == SCRIPT_VERSION_VIM9
1684 && check_vim9_unlet(name) == FAIL)
1685 return FAIL;
1686
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001687 if (ht != NULL && *varname != NUL)
1688 {
1689 d = get_current_funccal_dict(ht);
1690 if (d == NULL)
1691 {
1692 if (ht == &globvarht)
1693 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001694 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001695 d = &vimvardict;
1696 else
1697 {
1698 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1699 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1700 }
1701 if (d == NULL)
1702 {
1703 internal_error("do_unlet()");
1704 return FAIL;
1705 }
1706 }
1707 hi = hash_find(ht, varname);
1708 if (HASHITEM_EMPTY(hi))
1709 hi = find_hi_in_scoped_ht(name, &ht);
1710 if (hi != NULL && !HASHITEM_EMPTY(hi))
1711 {
1712 di = HI2DI(hi);
1713 if (var_check_fixed(di->di_flags, name, FALSE)
1714 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001715 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001716 return FAIL;
1717
1718 delete_var(ht, hi);
1719 return OK;
1720 }
1721 }
1722 if (forceit)
1723 return OK;
1724 semsg(_("E108: No such variable: \"%s\""), name);
1725 return FAIL;
1726}
1727
1728/*
1729 * Lock or unlock variable indicated by "lp".
1730 * "deep" is the levels to go (-1 for unlimited);
1731 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1732 */
1733 static int
1734do_lock_var(
1735 lval_T *lp,
1736 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001737 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001738 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001739 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001740{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001741 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001742 int ret = OK;
1743 int cc;
1744 dictitem_T *di;
1745
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001746 if (lp->ll_tv == NULL)
1747 {
1748 cc = *name_end;
1749 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001750 if (*lp->ll_name == '$')
1751 {
1752 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001753 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001754 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001755 else
1756 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001757 // Normal name or expanded name.
1758 di = find_var(lp->ll_name, NULL, TRUE);
1759 if (di == NULL)
1760 ret = FAIL;
1761 else if ((di->di_flags & DI_FLAGS_FIX)
1762 && di->di_tv.v_type != VAR_DICT
1763 && di->di_tv.v_type != VAR_LIST)
1764 {
1765 // For historic reasons this error is not given for a list or
1766 // dict. E.g., the b: dict could be locked/unlocked.
1767 semsg(_(e_lock_unlock), lp->ll_name);
1768 ret = FAIL;
1769 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001770 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001771 {
1772 if (lock)
1773 di->di_flags |= DI_FLAGS_LOCK;
1774 else
1775 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001776 if (deep != 0)
1777 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001778 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001779 }
1780 *name_end = cc;
1781 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001782 else if (deep == 0)
1783 {
1784 // nothing to do
1785 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001786 else if (lp->ll_range)
1787 {
1788 listitem_T *li = lp->ll_li;
1789
1790 // (un)lock a range of List items.
1791 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1792 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001793 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001794 li = li->li_next;
1795 ++lp->ll_n1;
1796 }
1797 }
1798 else if (lp->ll_list != NULL)
1799 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001800 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001801 else
1802 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001803 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001804
1805 return ret;
1806}
1807
1808/*
1809 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001810 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1811 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001812 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001813 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001814item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001815{
1816 static int recurse = 0;
1817 list_T *l;
1818 listitem_T *li;
1819 dict_T *d;
1820 blob_T *b;
1821 hashitem_T *hi;
1822 int todo;
1823
1824 if (recurse >= DICT_MAXNEST)
1825 {
1826 emsg(_("E743: variable nested too deep for (un)lock"));
1827 return;
1828 }
1829 if (deep == 0)
1830 return;
1831 ++recurse;
1832
1833 // lock/unlock the item itself
1834 if (lock)
1835 tv->v_lock |= VAR_LOCKED;
1836 else
1837 tv->v_lock &= ~VAR_LOCKED;
1838
1839 switch (tv->v_type)
1840 {
1841 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001842 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001844 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001845 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001846 case VAR_STRING:
1847 case VAR_FUNC:
1848 case VAR_PARTIAL:
1849 case VAR_FLOAT:
1850 case VAR_SPECIAL:
1851 case VAR_JOB:
1852 case VAR_CHANNEL:
1853 break;
1854
1855 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001856 if ((b = tv->vval.v_blob) != NULL
1857 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001858 {
1859 if (lock)
1860 b->bv_lock |= VAR_LOCKED;
1861 else
1862 b->bv_lock &= ~VAR_LOCKED;
1863 }
1864 break;
1865 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001866 if ((l = tv->vval.v_list) != NULL
1867 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001868 {
1869 if (lock)
1870 l->lv_lock |= VAR_LOCKED;
1871 else
1872 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001873 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001874 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001875 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001876 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001877 }
1878 break;
1879 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001880 if ((d = tv->vval.v_dict) != NULL
1881 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001882 {
1883 if (lock)
1884 d->dv_lock |= VAR_LOCKED;
1885 else
1886 d->dv_lock &= ~VAR_LOCKED;
1887 if (deep < 0 || deep > 1)
1888 {
1889 // recursive: lock/unlock the items the List contains
1890 todo = (int)d->dv_hashtab.ht_used;
1891 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1892 {
1893 if (!HASHITEM_EMPTY(hi))
1894 {
1895 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001896 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1897 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001898 }
1899 }
1900 }
1901 }
1902 }
1903 --recurse;
1904}
1905
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001906#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1907/*
1908 * Delete all "menutrans_" variables.
1909 */
1910 void
1911del_menutrans_vars(void)
1912{
1913 hashitem_T *hi;
1914 int todo;
1915
1916 hash_lock(&globvarht);
1917 todo = (int)globvarht.ht_used;
1918 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1919 {
1920 if (!HASHITEM_EMPTY(hi))
1921 {
1922 --todo;
1923 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1924 delete_var(&globvarht, hi);
1925 }
1926 }
1927 hash_unlock(&globvarht);
1928}
1929#endif
1930
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001931/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001932 * Local string buffer for the next two functions to store a variable name
1933 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1934 * get_user_var_name().
1935 */
1936
1937static char_u *varnamebuf = NULL;
1938static int varnamebuflen = 0;
1939
1940/*
1941 * Function to concatenate a prefix and a variable name.
1942 */
1943 static char_u *
1944cat_prefix_varname(int prefix, char_u *name)
1945{
1946 int len;
1947
1948 len = (int)STRLEN(name) + 3;
1949 if (len > varnamebuflen)
1950 {
1951 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001952 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001953 varnamebuf = alloc(len);
1954 if (varnamebuf == NULL)
1955 {
1956 varnamebuflen = 0;
1957 return NULL;
1958 }
1959 varnamebuflen = len;
1960 }
1961 *varnamebuf = prefix;
1962 varnamebuf[1] = ':';
1963 STRCPY(varnamebuf + 2, name);
1964 return varnamebuf;
1965}
1966
1967/*
1968 * Function given to ExpandGeneric() to obtain the list of user defined
1969 * (global/buffer/window/built-in) variable names.
1970 */
1971 char_u *
1972get_user_var_name(expand_T *xp, int idx)
1973{
1974 static long_u gdone;
1975 static long_u bdone;
1976 static long_u wdone;
1977 static long_u tdone;
1978 static int vidx;
1979 static hashitem_T *hi;
1980 hashtab_T *ht;
1981
1982 if (idx == 0)
1983 {
1984 gdone = bdone = wdone = vidx = 0;
1985 tdone = 0;
1986 }
1987
1988 // Global variables
1989 if (gdone < globvarht.ht_used)
1990 {
1991 if (gdone++ == 0)
1992 hi = globvarht.ht_array;
1993 else
1994 ++hi;
1995 while (HASHITEM_EMPTY(hi))
1996 ++hi;
1997 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1998 return cat_prefix_varname('g', hi->hi_key);
1999 return hi->hi_key;
2000 }
2001
2002 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002003 ht =
2004#ifdef FEAT_CMDWIN
2005 // In cmdwin, the alternative buffer should be used.
2006 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2007 &prevwin->w_buffer->b_vars->dv_hashtab :
2008#endif
2009 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002010 if (bdone < ht->ht_used)
2011 {
2012 if (bdone++ == 0)
2013 hi = ht->ht_array;
2014 else
2015 ++hi;
2016 while (HASHITEM_EMPTY(hi))
2017 ++hi;
2018 return cat_prefix_varname('b', hi->hi_key);
2019 }
2020
2021 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002022 ht =
2023#ifdef FEAT_CMDWIN
2024 // In cmdwin, the alternative window should be used.
2025 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2026 &prevwin->w_vars->dv_hashtab :
2027#endif
2028 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002029 if (wdone < ht->ht_used)
2030 {
2031 if (wdone++ == 0)
2032 hi = ht->ht_array;
2033 else
2034 ++hi;
2035 while (HASHITEM_EMPTY(hi))
2036 ++hi;
2037 return cat_prefix_varname('w', hi->hi_key);
2038 }
2039
2040 // t: variables
2041 ht = &curtab->tp_vars->dv_hashtab;
2042 if (tdone < ht->ht_used)
2043 {
2044 if (tdone++ == 0)
2045 hi = ht->ht_array;
2046 else
2047 ++hi;
2048 while (HASHITEM_EMPTY(hi))
2049 ++hi;
2050 return cat_prefix_varname('t', hi->hi_key);
2051 }
2052
2053 // v: variables
2054 if (vidx < VV_LEN)
2055 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2056
2057 VIM_CLEAR(varnamebuf);
2058 varnamebuflen = 0;
2059 return NULL;
2060}
2061
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002062 char *
2063get_var_special_name(int nr)
2064{
2065 switch (nr)
2066 {
2067 case VVAL_FALSE: return "v:false";
2068 case VVAL_TRUE: return "v:true";
2069 case VVAL_NONE: return "v:none";
2070 case VVAL_NULL: return "v:null";
2071 }
2072 internal_error("get_var_special_name()");
2073 return "42";
2074}
2075
2076/*
2077 * Returns the global variable dictionary
2078 */
2079 dict_T *
2080get_globvar_dict(void)
2081{
2082 return &globvardict;
2083}
2084
2085/*
2086 * Returns the global variable hash table
2087 */
2088 hashtab_T *
2089get_globvar_ht(void)
2090{
2091 return &globvarht;
2092}
2093
2094/*
2095 * Returns the v: variable dictionary
2096 */
2097 dict_T *
2098get_vimvar_dict(void)
2099{
2100 return &vimvardict;
2101}
2102
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002103/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002105 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002106 */
2107 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002108find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002110 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2111 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112
2113 if (di == NULL)
2114 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002115 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2117 return (int)(vv - vimvars);
2118}
2119
2120
2121/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002122 * Set type of v: variable to "type".
2123 */
2124 void
2125set_vim_var_type(int idx, vartype_T type)
2126{
2127 vimvars[idx].vv_type = type;
2128}
2129
2130/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002131 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002132 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002133 */
2134 void
2135set_vim_var_nr(int idx, varnumber_T val)
2136{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002137 vimvars[idx].vv_nr = val;
2138}
2139
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 char *
2141get_vim_var_name(int idx)
2142{
2143 return vimvars[idx].vv_name;
2144}
2145
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002146/*
2147 * Get typval_T v: variable value.
2148 */
2149 typval_T *
2150get_vim_var_tv(int idx)
2151{
2152 return &vimvars[idx].vv_tv;
2153}
2154
2155/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002156 * Set v: variable to "tv". Only accepts the same type.
2157 * Takes over the value of "tv".
2158 */
2159 int
2160set_vim_var_tv(int idx, typval_T *tv)
2161{
2162 if (vimvars[idx].vv_type != tv->v_type)
2163 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002164 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002165 clear_tv(tv);
2166 return FAIL;
2167 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002168 // VV_RO is also checked when compiling, but let's check here as well.
2169 if (vimvars[idx].vv_flags & VV_RO)
2170 {
2171 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2172 return FAIL;
2173 }
2174 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2175 {
2176 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2177 return FAIL;
2178 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002179 clear_tv(&vimvars[idx].vv_di.di_tv);
2180 vimvars[idx].vv_di.di_tv = *tv;
2181 return OK;
2182}
2183
2184/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002185 * Get number v: variable value.
2186 */
2187 varnumber_T
2188get_vim_var_nr(int idx)
2189{
2190 return vimvars[idx].vv_nr;
2191}
2192
2193/*
2194 * Get string v: variable value. Uses a static buffer, can only be used once.
2195 * If the String variable has never been set, return an empty string.
2196 * Never returns NULL;
2197 */
2198 char_u *
2199get_vim_var_str(int idx)
2200{
2201 return tv_get_string(&vimvars[idx].vv_tv);
2202}
2203
2204/*
2205 * Get List v: variable value. Caller must take care of reference count when
2206 * needed.
2207 */
2208 list_T *
2209get_vim_var_list(int idx)
2210{
2211 return vimvars[idx].vv_list;
2212}
2213
2214/*
2215 * Get Dict v: variable value. Caller must take care of reference count when
2216 * needed.
2217 */
2218 dict_T *
2219get_vim_var_dict(int idx)
2220{
2221 return vimvars[idx].vv_dict;
2222}
2223
2224/*
2225 * Set v:char to character "c".
2226 */
2227 void
2228set_vim_var_char(int c)
2229{
2230 char_u buf[MB_MAXBYTES + 1];
2231
2232 if (has_mbyte)
2233 buf[(*mb_char2bytes)(c, buf)] = NUL;
2234 else
2235 {
2236 buf[0] = c;
2237 buf[1] = NUL;
2238 }
2239 set_vim_var_string(VV_CHAR, buf, -1);
2240}
2241
2242/*
2243 * Set v:count to "count" and v:count1 to "count1".
2244 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2245 */
2246 void
2247set_vcount(
2248 long count,
2249 long count1,
2250 int set_prevcount)
2251{
2252 if (set_prevcount)
2253 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2254 vimvars[VV_COUNT].vv_nr = count;
2255 vimvars[VV_COUNT1].vv_nr = count1;
2256}
2257
2258/*
2259 * Save variables that might be changed as a side effect. Used when executing
2260 * a timer callback.
2261 */
2262 void
2263save_vimvars(vimvars_save_T *vvsave)
2264{
2265 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2266 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2267 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2268}
2269
2270/*
2271 * Restore variables saved by save_vimvars().
2272 */
2273 void
2274restore_vimvars(vimvars_save_T *vvsave)
2275{
2276 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2277 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2278 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2279}
2280
2281/*
2282 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2283 * value.
2284 */
2285 void
2286set_vim_var_string(
2287 int idx,
2288 char_u *val,
2289 int len) // length of "val" to use or -1 (whole string)
2290{
2291 clear_tv(&vimvars[idx].vv_di.di_tv);
2292 vimvars[idx].vv_type = VAR_STRING;
2293 if (val == NULL)
2294 vimvars[idx].vv_str = NULL;
2295 else if (len == -1)
2296 vimvars[idx].vv_str = vim_strsave(val);
2297 else
2298 vimvars[idx].vv_str = vim_strnsave(val, len);
2299}
2300
2301/*
2302 * Set List v: variable to "val".
2303 */
2304 void
2305set_vim_var_list(int idx, list_T *val)
2306{
2307 clear_tv(&vimvars[idx].vv_di.di_tv);
2308 vimvars[idx].vv_type = VAR_LIST;
2309 vimvars[idx].vv_list = val;
2310 if (val != NULL)
2311 ++val->lv_refcount;
2312}
2313
2314/*
2315 * Set Dictionary v: variable to "val".
2316 */
2317 void
2318set_vim_var_dict(int idx, dict_T *val)
2319{
2320 clear_tv(&vimvars[idx].vv_di.di_tv);
2321 vimvars[idx].vv_type = VAR_DICT;
2322 vimvars[idx].vv_dict = val;
2323 if (val != NULL)
2324 {
2325 ++val->dv_refcount;
2326 dict_set_items_ro(val);
2327 }
2328}
2329
2330/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002331 * Set the v:argv list.
2332 */
2333 void
2334set_argv_var(char **argv, int argc)
2335{
2336 list_T *l = list_alloc();
2337 int i;
2338
2339 if (l == NULL)
2340 getout(1);
2341 l->lv_lock = VAR_FIXED;
2342 for (i = 0; i < argc; ++i)
2343 {
2344 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2345 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002346 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002347 }
2348 set_vim_var_list(VV_ARGV, l);
2349}
2350
2351/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002352 * Reset v:register, taking the 'clipboard' setting into account.
2353 */
2354 void
2355reset_reg_var(void)
2356{
2357 int regname = 0;
2358
2359 // Adjust the register according to 'clipboard', so that when
2360 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2361#ifdef FEAT_CLIPBOARD
2362 adjust_clip_reg(&regname);
2363#endif
2364 set_reg_var(regname);
2365}
2366
2367/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002368 * Set v:register if needed.
2369 */
2370 void
2371set_reg_var(int c)
2372{
2373 char_u regname;
2374
2375 if (c == 0 || c == ' ')
2376 regname = '"';
2377 else
2378 regname = c;
2379 // Avoid free/alloc when the value is already right.
2380 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2381 set_vim_var_string(VV_REG, &regname, 1);
2382}
2383
2384/*
2385 * Get or set v:exception. If "oldval" == NULL, return the current value.
2386 * Otherwise, restore the value to "oldval" and return NULL.
2387 * Must always be called in pairs to save and restore v:exception! Does not
2388 * take care of memory allocations.
2389 */
2390 char_u *
2391v_exception(char_u *oldval)
2392{
2393 if (oldval == NULL)
2394 return vimvars[VV_EXCEPTION].vv_str;
2395
2396 vimvars[VV_EXCEPTION].vv_str = oldval;
2397 return NULL;
2398}
2399
2400/*
2401 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2402 * Otherwise, restore the value to "oldval" and return NULL.
2403 * Must always be called in pairs to save and restore v:throwpoint! Does not
2404 * take care of memory allocations.
2405 */
2406 char_u *
2407v_throwpoint(char_u *oldval)
2408{
2409 if (oldval == NULL)
2410 return vimvars[VV_THROWPOINT].vv_str;
2411
2412 vimvars[VV_THROWPOINT].vv_str = oldval;
2413 return NULL;
2414}
2415
2416/*
2417 * Set v:cmdarg.
2418 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2419 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2420 * Must always be called in pairs!
2421 */
2422 char_u *
2423set_cmdarg(exarg_T *eap, char_u *oldarg)
2424{
2425 char_u *oldval;
2426 char_u *newval;
2427 unsigned len;
2428
2429 oldval = vimvars[VV_CMDARG].vv_str;
2430 if (eap == NULL)
2431 {
2432 vim_free(oldval);
2433 vimvars[VV_CMDARG].vv_str = oldarg;
2434 return NULL;
2435 }
2436
2437 if (eap->force_bin == FORCE_BIN)
2438 len = 6;
2439 else if (eap->force_bin == FORCE_NOBIN)
2440 len = 8;
2441 else
2442 len = 0;
2443
2444 if (eap->read_edit)
2445 len += 7;
2446
2447 if (eap->force_ff != 0)
2448 len += 10; // " ++ff=unix"
2449 if (eap->force_enc != 0)
2450 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2451 if (eap->bad_char != 0)
2452 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2453
2454 newval = alloc(len + 1);
2455 if (newval == NULL)
2456 return NULL;
2457
2458 if (eap->force_bin == FORCE_BIN)
2459 sprintf((char *)newval, " ++bin");
2460 else if (eap->force_bin == FORCE_NOBIN)
2461 sprintf((char *)newval, " ++nobin");
2462 else
2463 *newval = NUL;
2464
2465 if (eap->read_edit)
2466 STRCAT(newval, " ++edit");
2467
2468 if (eap->force_ff != 0)
2469 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2470 eap->force_ff == 'u' ? "unix"
2471 : eap->force_ff == 'd' ? "dos"
2472 : "mac");
2473 if (eap->force_enc != 0)
2474 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2475 eap->cmd + eap->force_enc);
2476 if (eap->bad_char == BAD_KEEP)
2477 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2478 else if (eap->bad_char == BAD_DROP)
2479 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2480 else if (eap->bad_char != 0)
2481 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2482 vimvars[VV_CMDARG].vv_str = newval;
2483 return oldval;
2484}
2485
2486/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002487 * Get the value of internal variable "name".
2488 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2489 */
2490 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002491eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002492 char_u *name,
2493 int len, // length of "name"
2494 typval_T *rettv, // NULL when only checking existence
2495 dictitem_T **dip, // non-NULL when typval's dict item is needed
2496 int verbose, // may give error message
2497 int no_autoload) // do not use script autoloading
2498{
2499 int ret = OK;
2500 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002501 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002502 dictitem_T *v;
2503 int cc;
2504
2505 // truncate the name, so that we can use strcmp()
2506 cc = name[len];
2507 name[len] = NUL;
2508
2509 // Check for user-defined variables.
2510 v = find_var(name, NULL, no_autoload);
2511 if (v != NULL)
2512 {
2513 tv = &v->di_tv;
2514 if (dip != NULL)
2515 *dip = v;
2516 }
2517
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002518 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002520 imported_T *import;
2521 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2522
2523 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524
2525 // imported variable from another script
2526 if (import != NULL)
2527 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002528 if (import->imp_funcname != NULL)
2529 {
2530 foundFunc = TRUE;
2531 if (rettv != NULL)
2532 {
2533 rettv->v_type = VAR_FUNC;
2534 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2535 }
2536 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002537 else if (import->imp_all)
2538 {
2539 emsg("Sorry, 'import * as X' not implemented yet");
2540 ret = FAIL;
2541 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002542 else
2543 {
2544 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002545 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002547 tv = sv->sv_tv;
2548 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002550 else if (in_vim9script())
2551 {
2552 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2553
2554 if (ufunc != NULL)
2555 {
2556 foundFunc = TRUE;
2557 if (rettv != NULL)
2558 {
2559 rettv->v_type = VAR_FUNC;
2560 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2561 }
2562 }
2563 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 }
2565
Bram Moolenaarc620c052020-07-08 15:16:19 +02002566 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002567 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002568 if (tv == NULL)
2569 {
2570 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002571 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002572 ret = FAIL;
2573 }
2574 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002575 {
2576 // If a list or dict variable wasn't initialized, do it now.
2577 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2578 {
2579 tv->vval.v_dict = dict_alloc();
2580 if (tv->vval.v_dict != NULL)
2581 ++tv->vval.v_dict->dv_refcount;
2582 }
2583 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2584 {
2585 tv->vval.v_list = list_alloc();
2586 if (tv->vval.v_list != NULL)
2587 ++tv->vval.v_list->lv_refcount;
2588 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002589 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002590 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002591 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002592
2593 name[len] = cc;
2594
2595 return ret;
2596}
2597
2598/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002599 * Check if variable "name[len]" is a local variable or an argument.
2600 * If so, "*eval_lavars_used" is set to TRUE.
2601 */
2602 void
2603check_vars(char_u *name, int len)
2604{
2605 int cc;
2606 char_u *varname;
2607 hashtab_T *ht;
2608
2609 if (eval_lavars_used == NULL)
2610 return;
2611
2612 // truncate the name, so that we can use strcmp()
2613 cc = name[len];
2614 name[len] = NUL;
2615
2616 ht = find_var_ht(name, &varname);
2617 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2618 {
2619 if (find_var(name, NULL, TRUE) != NULL)
2620 *eval_lavars_used = TRUE;
2621 }
2622
2623 name[len] = cc;
2624}
2625
2626/*
2627 * Find variable "name" in the list of variables.
2628 * Return a pointer to it if found, NULL if not found.
2629 * Careful: "a:0" variables don't have a name.
2630 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2631 * hashtab_T used.
2632 */
2633 dictitem_T *
2634find_var(char_u *name, hashtab_T **htp, int no_autoload)
2635{
2636 char_u *varname;
2637 hashtab_T *ht;
2638 dictitem_T *ret = NULL;
2639
2640 ht = find_var_ht(name, &varname);
2641 if (htp != NULL)
2642 *htp = ht;
2643 if (ht == NULL)
2644 return NULL;
2645 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2646 if (ret != NULL)
2647 return ret;
2648
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002649 // Search in parent scope for lambda
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002650 ret = find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2651 if (ret != NULL)
2652 return ret;
2653
2654 // in Vim9 script items without a scope can be script-local
2655 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2656 {
2657 ht = get_script_local_ht();
2658 if (ht != NULL)
2659 {
2660 ret = find_var_in_ht(ht, *name, varname,
2661 no_autoload || htp != NULL);
2662 if (ret != NULL)
2663 {
2664 if (htp != NULL)
2665 *htp = ht;
2666 return ret;
2667 }
2668 }
2669 }
2670
2671 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002672}
2673
2674/*
2675 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002676 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002677 * Returns NULL if not found.
2678 */
2679 dictitem_T *
2680find_var_in_ht(
2681 hashtab_T *ht,
2682 int htname,
2683 char_u *varname,
2684 int no_autoload)
2685{
2686 hashitem_T *hi;
2687
2688 if (*varname == NUL)
2689 {
2690 // Must be something like "s:", otherwise "ht" would be NULL.
2691 switch (htname)
2692 {
2693 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2694 case 'g': return &globvars_var;
2695 case 'v': return &vimvars_var;
2696 case 'b': return &curbuf->b_bufvar;
2697 case 'w': return &curwin->w_winvar;
2698 case 't': return &curtab->tp_winvar;
2699 case 'l': return get_funccal_local_var();
2700 case 'a': return get_funccal_args_var();
2701 }
2702 return NULL;
2703 }
2704
2705 hi = hash_find(ht, varname);
2706 if (HASHITEM_EMPTY(hi))
2707 {
2708 // For global variables we may try auto-loading the script. If it
2709 // worked find the variable again. Don't auto-load a script if it was
2710 // loaded already, otherwise it would be loaded every time when
2711 // checking if a function name is a Funcref variable.
2712 if (ht == &globvarht && !no_autoload)
2713 {
2714 // Note: script_autoload() may make "hi" invalid. It must either
2715 // be obtained again or not used.
2716 if (!script_autoload(varname, FALSE) || aborting())
2717 return NULL;
2718 hi = hash_find(ht, varname);
2719 }
2720 if (HASHITEM_EMPTY(hi))
2721 return NULL;
2722 }
2723 return HI2DI(hi);
2724}
2725
2726/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 * Get the script-local hashtab. NULL if not in a script context.
2728 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002729 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730get_script_local_ht(void)
2731{
2732 scid_T sid = current_sctx.sc_sid;
2733
Bram Moolenaare3d46852020-08-29 13:39:17 +02002734 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002735 return &SCRIPT_VARS(sid);
2736 return NULL;
2737}
2738
2739/*
2740 * Look for "name[len]" in script-local variables.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002741 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01002743 int
2744lookup_scriptvar(
2745 char_u *name,
2746 size_t len,
2747 void *lvar UNUSED,
2748 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749{
2750 hashtab_T *ht = get_script_local_ht();
2751 char_u buffer[30];
2752 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01002753 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002754 hashitem_T *hi;
2755
2756 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002757 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 if (len < sizeof(buffer) - 1)
2759 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002760 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002761 vim_strncpy(buffer, name, len);
2762 p = buffer;
2763 }
2764 else
2765 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002766 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002768 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002769 }
2770
2771 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002772 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773
2774 // if not script-local, then perhaps imported
Bram Moolenaar709664c2020-12-12 14:33:41 +01002775 if (res == FAIL && find_imported(p, 0, NULL) != NULL)
2776 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777
2778 if (p != buffer)
2779 vim_free(p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002780 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781}
2782
2783/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002784 * Find the hashtab used for a variable name.
2785 * Return NULL if the name is not valid.
2786 * Set "varname" to the start of name without ':'.
2787 */
2788 hashtab_T *
2789find_var_ht(char_u *name, char_u **varname)
2790{
2791 hashitem_T *hi;
2792 hashtab_T *ht;
2793
2794 if (name[0] == NUL)
2795 return NULL;
2796 if (name[1] != ':')
2797 {
2798 // The name must not start with a colon or #.
2799 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2800 return NULL;
2801 *varname = name;
2802
2803 // "version" is "v:version" in all scopes if scriptversion < 3.
2804 // Same for a few other variables marked with VV_COMPAT.
2805 if (current_sctx.sc_version < 3)
2806 {
2807 hi = hash_find(&compat_hashtab, name);
2808 if (!HASHITEM_EMPTY(hi))
2809 return &compat_hashtab;
2810 }
2811
2812 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 if (ht != NULL)
2814 return ht; // local variable
2815
2816 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002817 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818 {
2819 ht = get_script_local_ht();
2820 if (ht != NULL)
2821 return ht;
2822 }
2823
2824 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002825 }
2826 *varname = name + 2;
2827 if (*name == 'g') // global variable
2828 return &globvarht;
2829 // There must be no ':' or '#' in the rest of the name, unless g: is used
2830 if (vim_strchr(name + 2, ':') != NULL
2831 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2832 return NULL;
2833 if (*name == 'b') // buffer variable
2834 return &curbuf->b_vars->dv_hashtab;
2835 if (*name == 'w') // window variable
2836 return &curwin->w_vars->dv_hashtab;
2837 if (*name == 't') // tab page variable
2838 return &curtab->tp_vars->dv_hashtab;
2839 if (*name == 'v') // v: variable
2840 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002841 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002842 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002844 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 if (*name == 'a') // a: function argument
2846 return get_funccal_args_ht();
2847 if (*name == 'l') // l: local function variable
2848 return get_funccal_local_ht();
2849 }
2850 if (*name == 's') // script variable
2851 {
2852 ht = get_script_local_ht();
2853 if (ht != NULL)
2854 return ht;
2855 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002856 return NULL;
2857}
2858
2859/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002860 * Get the string value of a (global/local) variable.
2861 * Note: see tv_get_string() for how long the pointer remains valid.
2862 * Returns NULL when it doesn't exist.
2863 */
2864 char_u *
2865get_var_value(char_u *name)
2866{
2867 dictitem_T *v;
2868
2869 v = find_var(name, NULL, FALSE);
2870 if (v == NULL)
2871 return NULL;
2872 return tv_get_string(&v->di_tv);
2873}
2874
2875/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002876 * Allocate a new hashtab for a sourced script. It will be used while
2877 * sourcing this script and when executing functions defined in the script.
2878 */
2879 void
2880new_script_vars(scid_T id)
2881{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002882 scriptvar_T *sv;
2883
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002884 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2885 if (sv == NULL)
2886 return;
2887 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002888 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002889}
2890
2891/*
2892 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2893 * point to it.
2894 */
2895 void
2896init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2897{
2898 hash_init(&dict->dv_hashtab);
2899 dict->dv_lock = 0;
2900 dict->dv_scope = scope;
2901 dict->dv_refcount = DO_NOT_FREE_CNT;
2902 dict->dv_copyID = 0;
2903 dict_var->di_tv.vval.v_dict = dict;
2904 dict_var->di_tv.v_type = VAR_DICT;
2905 dict_var->di_tv.v_lock = VAR_FIXED;
2906 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2907 dict_var->di_key[0] = NUL;
2908}
2909
2910/*
2911 * Unreference a dictionary initialized by init_var_dict().
2912 */
2913 void
2914unref_var_dict(dict_T *dict)
2915{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002916 // Now the dict needs to be freed if no one else is using it, go back to
2917 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002918 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2919 dict_unref(dict);
2920}
2921
2922/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002923 * Clean up a list of internal variables.
2924 * Frees all allocated variables and the value they contain.
2925 * Clears hashtab "ht", does not free it.
2926 */
2927 void
2928vars_clear(hashtab_T *ht)
2929{
2930 vars_clear_ext(ht, TRUE);
2931}
2932
2933/*
2934 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2935 */
2936 void
2937vars_clear_ext(hashtab_T *ht, int free_val)
2938{
2939 int todo;
2940 hashitem_T *hi;
2941 dictitem_T *v;
2942
2943 hash_lock(ht);
2944 todo = (int)ht->ht_used;
2945 for (hi = ht->ht_array; todo > 0; ++hi)
2946 {
2947 if (!HASHITEM_EMPTY(hi))
2948 {
2949 --todo;
2950
2951 // Free the variable. Don't remove it from the hashtab,
2952 // ht_array might change then. hash_clear() takes care of it
2953 // later.
2954 v = HI2DI(hi);
2955 if (free_val)
2956 clear_tv(&v->di_tv);
2957 if (v->di_flags & DI_FLAGS_ALLOC)
2958 vim_free(v);
2959 }
2960 }
2961 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02002962 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002963}
2964
2965/*
2966 * Delete a variable from hashtab "ht" at item "hi".
2967 * Clear the variable value and free the dictitem.
2968 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02002969 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002970delete_var(hashtab_T *ht, hashitem_T *hi)
2971{
2972 dictitem_T *di = HI2DI(hi);
2973
2974 hash_remove(ht, hi);
2975 clear_tv(&di->di_tv);
2976 vim_free(di);
2977}
2978
2979/*
2980 * List the value of one internal variable.
2981 */
2982 static void
2983list_one_var(dictitem_T *v, char *prefix, int *first)
2984{
2985 char_u *tofree;
2986 char_u *s;
2987 char_u numbuf[NUMBUFLEN];
2988
2989 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2990 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2991 s == NULL ? (char_u *)"" : s, first);
2992 vim_free(tofree);
2993}
2994
2995 static void
2996list_one_var_a(
2997 char *prefix,
2998 char_u *name,
2999 int type,
3000 char_u *string,
3001 int *first) // when TRUE clear rest of screen and set to FALSE
3002{
3003 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3004 msg_start();
3005 msg_puts(prefix);
3006 if (name != NULL) // "a:" vars don't have a name stored
3007 msg_puts((char *)name);
3008 msg_putchar(' ');
3009 msg_advance(22);
3010 if (type == VAR_NUMBER)
3011 msg_putchar('#');
3012 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3013 msg_putchar('*');
3014 else if (type == VAR_LIST)
3015 {
3016 msg_putchar('[');
3017 if (*string == '[')
3018 ++string;
3019 }
3020 else if (type == VAR_DICT)
3021 {
3022 msg_putchar('{');
3023 if (*string == '{')
3024 ++string;
3025 }
3026 else
3027 msg_putchar(' ');
3028
3029 msg_outtrans(string);
3030
3031 if (type == VAR_FUNC || type == VAR_PARTIAL)
3032 msg_puts("()");
3033 if (*first)
3034 {
3035 msg_clr_eos();
3036 *first = FALSE;
3037 }
3038}
3039
3040/*
3041 * Set variable "name" to value in "tv".
3042 * If the variable already exists, the value is updated.
3043 * Otherwise the variable is created.
3044 */
3045 void
3046set_var(
3047 char_u *name,
3048 typval_T *tv,
3049 int copy) // make copy of value in "tv"
3050{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003051 set_var_const(name, NULL, tv, copy, ASSIGN_NO_DECL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003052}
3053
3054/*
3055 * Set variable "name" to value in "tv".
3056 * If the variable already exists and "is_const" is FALSE the value is updated.
3057 * Otherwise the variable is created.
3058 */
3059 void
3060set_var_const(
3061 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003063 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003064 int copy, // make copy of value in "tv"
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003065 int flags) // ASSIGN_CONST, ASSIGN_FINAL, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003066{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003067 typval_T *tv = tv_arg;
3068 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003070 char_u *varname;
3071 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003073 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003074
3075 ht = find_var_ht(name, &varname);
3076 if (ht == NULL || *varname == NUL)
3077 {
3078 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003079 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003080 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003081 is_script_local = ht == get_script_local_ht();
3082
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003083 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003084 && !is_script_local
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003085 && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003086 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003087 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003088 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003089 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003090 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003091 }
3092
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003094
3095 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 if (di == NULL)
3097 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003098
3099 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003100 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003101 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003102
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003103 if (need_convert_to_bool(type, tv))
3104 {
3105 // Destination is a bool and the value is not, but it can be converted.
3106 CLEAR_FIELD(bool_tv);
3107 bool_tv.v_type = VAR_BOOL;
3108 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3109 tv = &bool_tv;
3110 }
3111
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003113 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003115 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003116 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 {
3118 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003119 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 }
3121
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003122 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003124 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003125 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003126 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003127 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003128 }
3129
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003130 // check the type and adjust to bool if needed
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003131 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003132 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003134
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003135 if (var_check_permission(di, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003136 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003137 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 else
3139 // can only redefine once
3140 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003141
3142 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003143
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003145 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003146 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003147 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003149 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003151 if (copy || tv->v_type != VAR_STRING)
3152 {
3153 char_u *val = tv_get_string(tv);
3154
3155 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003156 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 if (di->di_tv.vval.v_string == NULL)
3158 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003159 }
3160 else
3161 {
3162 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003164 tv->vval.v_string = NULL;
3165 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003166 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003167 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003169 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003171 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003173#ifdef FEAT_SEARCH_EXTRA
3174 else if (STRCMP(varname, "hlsearch") == 0)
3175 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003177 redraw_all_later(SOME_VALID);
3178 }
3179#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003180 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003183 {
3184 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003185 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003186 }
3187 }
3188
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003190 }
3191 else // add a new variable
3192 {
3193 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003194 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003195 {
3196 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003197 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003198 }
3199
Bram Moolenaar03290b82020-12-19 16:30:44 +01003200 // Make sure the variable name is valid. In Vim9 script an autoload
3201 // variable must be prefixed with "g:".
3202 if (!valid_varname(varname, !vim9script
3203 || STRNCMP(name, "g:", 2) == 0))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003204 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3207 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003208 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 STRCPY(di->di_key, varname);
3210 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003211 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003213 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003214 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003216 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 di->di_flags |= DI_FLAGS_LOCK;
3218
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003219 // A Vim9 script-local variable is also added to sn_all_vars and
3220 // sn_var_vals.
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003221 if (is_script_local && vim9script)
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003222 add_vim9_script_var(di, tv, type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003223 }
3224
3225 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003227 else
3228 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 di->di_tv = *tv;
3230 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003231 init_tv(tv);
3232 }
3233
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003234 // ":const var = val" locks the value
3235 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003236 // Like :lockvar! name: lock the value and what it contains, but only
3237 // if the reference count is up to one. That locks only literal
3238 // values.
3239 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003240 return;
3241failed:
3242 if (!copy)
3243 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003244}
3245
3246/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003247 * Check in this order for backwards compatibility:
3248 * - Whether the variable is read-only
3249 * - Whether the variable value is locked
3250 * - Whether the variable is locked
3251 */
3252 int
3253var_check_permission(dictitem_T *di, char_u *name)
3254{
3255 if (var_check_ro(di->di_flags, name, FALSE)
3256 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3257 || var_check_lock(di->di_flags, name, FALSE))
3258 return FAIL;
3259 return OK;
3260}
3261
3262/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003263 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3264 * Also give an error message.
3265 */
3266 int
3267var_check_ro(int flags, char_u *name, int use_gettext)
3268{
3269 if (flags & DI_FLAGS_RO)
3270 {
3271 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3272 return TRUE;
3273 }
3274 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3275 {
3276 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3277 return TRUE;
3278 }
3279 return FALSE;
3280}
3281
3282/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003283 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3284 * Also give an error message.
3285 */
3286 int
3287var_check_lock(int flags, char_u *name, int use_gettext)
3288{
3289 if (flags & DI_FLAGS_LOCK)
3290 {
3291 semsg(_(e_variable_is_locked_str),
3292 use_gettext ? (char_u *)_(name) : name);
3293 return TRUE;
3294 }
3295 return FALSE;
3296}
3297
3298/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003299 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3300 * Also give an error message.
3301 */
3302 int
3303var_check_fixed(int flags, char_u *name, int use_gettext)
3304{
3305 if (flags & DI_FLAGS_FIX)
3306 {
3307 semsg(_("E795: Cannot delete variable %s"),
3308 use_gettext ? (char_u *)_(name) : name);
3309 return TRUE;
3310 }
3311 return FALSE;
3312}
3313
3314/*
3315 * Check if a funcref is assigned to a valid variable name.
3316 * Return TRUE and give an error if not.
3317 */
3318 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003319var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003320 char_u *name, // points to start of variable name
3321 int new_var) // TRUE when creating the variable
3322{
3323 // Allow for w: b: s: and t:.
3324 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3325 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3326 ? name[2] : name[0]))
3327 {
3328 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3329 name);
3330 return TRUE;
3331 }
3332 // Don't allow hiding a function. When "v" is not NULL we might be
3333 // assigning another function to the same var, the type is checked
3334 // below.
3335 if (new_var && function_exists(name, FALSE))
3336 {
3337 semsg(_("E705: Variable name conflicts with existing function: %s"),
3338 name);
3339 return TRUE;
3340 }
3341 return FALSE;
3342}
3343
3344/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003345 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3346 * value. Also give an error message, using "name" or _("name") when
3347 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003348 */
3349 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003350value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003351{
3352 if (lock & VAR_LOCKED)
3353 {
3354 semsg(_("E741: Value is locked: %s"),
3355 name == NULL ? (char_u *)_("Unknown")
3356 : use_gettext ? (char_u *)_(name)
3357 : name);
3358 return TRUE;
3359 }
3360 if (lock & VAR_FIXED)
3361 {
3362 semsg(_("E742: Cannot change value of %s"),
3363 name == NULL ? (char_u *)_("Unknown")
3364 : use_gettext ? (char_u *)_(name)
3365 : name);
3366 return TRUE;
3367 }
3368 return FALSE;
3369}
3370
3371/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003372 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003373 * Return FALSE and give an error if not.
3374 */
3375 int
Bram Moolenaar03290b82020-12-19 16:30:44 +01003376valid_varname(char_u *varname, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003377{
3378 char_u *p;
3379
3380 for (p = varname; *p != NUL; ++p)
3381 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003382 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003383 {
3384 semsg(_(e_illvar), varname);
3385 return FALSE;
3386 }
3387 return TRUE;
3388}
3389
3390/*
3391 * getwinvar() and gettabwinvar()
3392 */
3393 static void
3394getwinvar(
3395 typval_T *argvars,
3396 typval_T *rettv,
3397 int off) // 1 for gettabwinvar()
3398{
3399 win_T *win;
3400 char_u *varname;
3401 dictitem_T *v;
3402 tabpage_T *tp = NULL;
3403 int done = FALSE;
3404 win_T *oldcurwin;
3405 tabpage_T *oldtabpage;
3406 int need_switch_win;
3407
3408 if (off == 1)
3409 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3410 else
3411 tp = curtab;
3412 win = find_win_by_nr(&argvars[off], tp);
3413 varname = tv_get_string_chk(&argvars[off + 1]);
3414 ++emsg_off;
3415
3416 rettv->v_type = VAR_STRING;
3417 rettv->vval.v_string = NULL;
3418
3419 if (win != NULL && varname != NULL)
3420 {
3421 // Set curwin to be our win, temporarily. Also set the tabpage,
3422 // otherwise the window is not valid. Only do this when needed,
3423 // autocommands get blocked.
3424 need_switch_win = !(tp == curtab && win == curwin);
3425 if (!need_switch_win
3426 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3427 {
3428 if (*varname == '&')
3429 {
3430 if (varname[1] == NUL)
3431 {
3432 // get all window-local options in a dict
3433 dict_T *opts = get_winbuf_options(FALSE);
3434
3435 if (opts != NULL)
3436 {
3437 rettv_dict_set(rettv, opts);
3438 done = TRUE;
3439 }
3440 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003441 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003442 // window-local-option
3443 done = TRUE;
3444 }
3445 else
3446 {
3447 // Look up the variable.
3448 // Let getwinvar({nr}, "") return the "w:" dictionary.
3449 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3450 varname, FALSE);
3451 if (v != NULL)
3452 {
3453 copy_tv(&v->di_tv, rettv);
3454 done = TRUE;
3455 }
3456 }
3457 }
3458
3459 if (need_switch_win)
3460 // restore previous notion of curwin
3461 restore_win(oldcurwin, oldtabpage, TRUE);
3462 }
3463
3464 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3465 // use the default return value
3466 copy_tv(&argvars[off + 2], rettv);
3467
3468 --emsg_off;
3469}
3470
3471/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003472 * Set option "varname" to the value of "varp" for the current buffer/window.
3473 */
3474 static void
3475set_option_from_tv(char_u *varname, typval_T *varp)
3476{
3477 long numval = 0;
3478 char_u *strval;
3479 char_u nbuf[NUMBUFLEN];
3480 int error = FALSE;
3481
3482 if (!in_vim9script() || varp->v_type != VAR_STRING)
3483 numval = (long)tv_get_number_chk(varp, &error);
3484 strval = tv_get_string_buf_chk(varp, nbuf);
3485 if (!error && strval != NULL)
3486 set_option_value(varname, numval, strval, OPT_LOCAL);
3487}
3488
3489/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003490 * "setwinvar()" and "settabwinvar()" functions
3491 */
3492 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003493setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003494{
3495 win_T *win;
3496 win_T *save_curwin;
3497 tabpage_T *save_curtab;
3498 int need_switch_win;
3499 char_u *varname, *winvarname;
3500 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003501 tabpage_T *tp = NULL;
3502
3503 if (check_secure())
3504 return;
3505
3506 if (off == 1)
3507 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3508 else
3509 tp = curtab;
3510 win = find_win_by_nr(&argvars[off], tp);
3511 varname = tv_get_string_chk(&argvars[off + 1]);
3512 varp = &argvars[off + 2];
3513
3514 if (win != NULL && varname != NULL && varp != NULL)
3515 {
3516 need_switch_win = !(tp == curtab && win == curwin);
3517 if (!need_switch_win
3518 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3519 {
3520 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003521 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003522 else
3523 {
3524 winvarname = alloc(STRLEN(varname) + 3);
3525 if (winvarname != NULL)
3526 {
3527 STRCPY(winvarname, "w:");
3528 STRCPY(winvarname + 2, varname);
3529 set_var(winvarname, varp, TRUE);
3530 vim_free(winvarname);
3531 }
3532 }
3533 }
3534 if (need_switch_win)
3535 restore_win(save_curwin, save_curtab, TRUE);
3536 }
3537}
3538
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003539/*
3540 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3541 * v:option_type, and v:option_command.
3542 */
3543 void
3544reset_v_option_vars(void)
3545{
3546 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3547 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3548 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3549 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3550 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3551 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3552}
3553
3554/*
3555 * Add an assert error to v:errors.
3556 */
3557 void
3558assert_error(garray_T *gap)
3559{
3560 struct vimvar *vp = &vimvars[VV_ERRORS];
3561
3562 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003563 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003564 set_vim_var_list(VV_ERRORS, list_alloc());
3565 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3566}
3567
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003568 int
3569var_exists(char_u *var)
3570{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003571 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003572 char_u *name;
3573 char_u *tofree;
3574 typval_T tv;
3575 int len = 0;
3576 int n = FALSE;
3577
3578 // get_name_len() takes care of expanding curly braces
3579 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003580 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003581 if (len > 0)
3582 {
3583 if (tofree != NULL)
3584 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003585 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003586 if (n)
3587 {
3588 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003589 arg = skipwhite(arg);
3590 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003591 if (n)
3592 clear_tv(&tv);
3593 }
3594 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003595 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003596 n = FALSE;
3597
3598 vim_free(tofree);
3599 return n;
3600}
3601
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003602static lval_T *redir_lval = NULL;
3603#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3604static garray_T redir_ga; // only valid when redir_lval is not NULL
3605static char_u *redir_endp = NULL;
3606static char_u *redir_varname = NULL;
3607
3608/*
3609 * Start recording command output to a variable
3610 * When "append" is TRUE append to an existing variable.
3611 * Returns OK if successfully completed the setup. FAIL otherwise.
3612 */
3613 int
3614var_redir_start(char_u *name, int append)
3615{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003616 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003617 typval_T tv;
3618
3619 // Catch a bad name early.
3620 if (!eval_isnamec1(*name))
3621 {
3622 emsg(_(e_invarg));
3623 return FAIL;
3624 }
3625
3626 // Make a copy of the name, it is used in redir_lval until redir ends.
3627 redir_varname = vim_strsave(name);
3628 if (redir_varname == NULL)
3629 return FAIL;
3630
3631 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3632 if (redir_lval == NULL)
3633 {
3634 var_redir_stop();
3635 return FAIL;
3636 }
3637
3638 // The output is stored in growarray "redir_ga" until redirection ends.
3639 ga_init2(&redir_ga, (int)sizeof(char), 500);
3640
3641 // Parse the variable name (can be a dict or list entry).
3642 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3643 FNE_CHECK_START);
3644 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3645 {
3646 clear_lval(redir_lval);
3647 if (redir_endp != NULL && *redir_endp != NUL)
3648 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003649 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003650 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003651 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003652 redir_endp = NULL; // don't store a value, only cleanup
3653 var_redir_stop();
3654 return FAIL;
3655 }
3656
3657 // check if we can write to the variable: set it to or append an empty
3658 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003659 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003660 tv.v_type = VAR_STRING;
3661 tv.vval.v_string = (char_u *)"";
3662 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003663 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3664 ASSIGN_NO_DECL, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003665 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003666 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3667 ASSIGN_NO_DECL, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003668 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003669 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003670 {
3671 redir_endp = NULL; // don't store a value, only cleanup
3672 var_redir_stop();
3673 return FAIL;
3674 }
3675
3676 return OK;
3677}
3678
3679/*
3680 * Append "value[value_len]" to the variable set by var_redir_start().
3681 * The actual appending is postponed until redirection ends, because the value
3682 * appended may in fact be the string we write to, changing it may cause freed
3683 * memory to be used:
3684 * :redir => foo
3685 * :let foo
3686 * :redir END
3687 */
3688 void
3689var_redir_str(char_u *value, int value_len)
3690{
3691 int len;
3692
3693 if (redir_lval == NULL)
3694 return;
3695
3696 if (value_len == -1)
3697 len = (int)STRLEN(value); // Append the entire string
3698 else
3699 len = value_len; // Append only "value_len" characters
3700
3701 if (ga_grow(&redir_ga, len) == OK)
3702 {
3703 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3704 redir_ga.ga_len += len;
3705 }
3706 else
3707 var_redir_stop();
3708}
3709
3710/*
3711 * Stop redirecting command output to a variable.
3712 * Frees the allocated memory.
3713 */
3714 void
3715var_redir_stop(void)
3716{
3717 typval_T tv;
3718
3719 if (EVALCMD_BUSY)
3720 {
3721 redir_lval = NULL;
3722 return;
3723 }
3724
3725 if (redir_lval != NULL)
3726 {
3727 // If there was no error: assign the text to the variable.
3728 if (redir_endp != NULL)
3729 {
3730 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3731 tv.v_type = VAR_STRING;
3732 tv.vval.v_string = redir_ga.ga_data;
3733 // Call get_lval() again, if it's inside a Dict or List it may
3734 // have changed.
3735 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3736 FALSE, FALSE, 0, FNE_CHECK_START);
3737 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003739 (char_u *)".");
3740 clear_lval(redir_lval);
3741 }
3742
3743 // free the collected output
3744 VIM_CLEAR(redir_ga.ga_data);
3745
3746 VIM_CLEAR(redir_lval);
3747 }
3748 VIM_CLEAR(redir_varname);
3749}
3750
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003751/*
3752 * "gettabvar()" function
3753 */
3754 void
3755f_gettabvar(typval_T *argvars, typval_T *rettv)
3756{
3757 win_T *oldcurwin;
3758 tabpage_T *tp, *oldtabpage;
3759 dictitem_T *v;
3760 char_u *varname;
3761 int done = FALSE;
3762
3763 rettv->v_type = VAR_STRING;
3764 rettv->vval.v_string = NULL;
3765
3766 varname = tv_get_string_chk(&argvars[1]);
3767 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3768 if (tp != NULL && varname != NULL)
3769 {
3770 // Set tp to be our tabpage, temporarily. Also set the window to the
3771 // first window in the tabpage, otherwise the window is not valid.
3772 if (switch_win(&oldcurwin, &oldtabpage,
3773 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3774 : tp->tp_firstwin, tp, TRUE) == OK)
3775 {
3776 // look up the variable
3777 // Let gettabvar({nr}, "") return the "t:" dictionary.
3778 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3779 if (v != NULL)
3780 {
3781 copy_tv(&v->di_tv, rettv);
3782 done = TRUE;
3783 }
3784 }
3785
3786 // restore previous notion of curwin
3787 restore_win(oldcurwin, oldtabpage, TRUE);
3788 }
3789
3790 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3791 copy_tv(&argvars[2], rettv);
3792}
3793
3794/*
3795 * "gettabwinvar()" function
3796 */
3797 void
3798f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3799{
3800 getwinvar(argvars, rettv, 1);
3801}
3802
3803/*
3804 * "getwinvar()" function
3805 */
3806 void
3807f_getwinvar(typval_T *argvars, typval_T *rettv)
3808{
3809 getwinvar(argvars, rettv, 0);
3810}
3811
3812/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003813 * "getbufvar()" function
3814 */
3815 void
3816f_getbufvar(typval_T *argvars, typval_T *rettv)
3817{
3818 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003819 char_u *varname;
3820 dictitem_T *v;
3821 int done = FALSE;
3822
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003823 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003824 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003825
3826 rettv->v_type = VAR_STRING;
3827 rettv->vval.v_string = NULL;
3828
3829 if (buf != NULL && varname != NULL)
3830 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003831 if (*varname == '&')
3832 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003833 buf_T *save_curbuf = curbuf;
3834
3835 // set curbuf to be our buf, temporarily
3836 curbuf = buf;
3837
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003838 if (varname[1] == NUL)
3839 {
3840 // get all buffer-local options in a dict
3841 dict_T *opts = get_winbuf_options(TRUE);
3842
3843 if (opts != NULL)
3844 {
3845 rettv_dict_set(rettv, opts);
3846 done = TRUE;
3847 }
3848 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003849 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003850 // buffer-local-option
3851 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003852
3853 // restore previous notion of curbuf
3854 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003855 }
3856 else
3857 {
3858 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003859 if (*varname == NUL)
3860 // Let getbufvar({nr}, "") return the "b:" dictionary.
3861 v = &buf->b_bufvar;
3862 else
3863 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3864 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003865 if (v != NULL)
3866 {
3867 copy_tv(&v->di_tv, rettv);
3868 done = TRUE;
3869 }
3870 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003871 }
3872
3873 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3874 // use the default value
3875 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003876}
3877
3878/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003879 * "settabvar()" function
3880 */
3881 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003882f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003883{
3884 tabpage_T *save_curtab;
3885 tabpage_T *tp;
3886 char_u *varname, *tabvarname;
3887 typval_T *varp;
3888
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003889 if (check_secure())
3890 return;
3891
3892 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3893 varname = tv_get_string_chk(&argvars[1]);
3894 varp = &argvars[2];
3895
3896 if (varname != NULL && varp != NULL && tp != NULL)
3897 {
3898 save_curtab = curtab;
3899 goto_tabpage_tp(tp, FALSE, FALSE);
3900
3901 tabvarname = alloc(STRLEN(varname) + 3);
3902 if (tabvarname != NULL)
3903 {
3904 STRCPY(tabvarname, "t:");
3905 STRCPY(tabvarname + 2, varname);
3906 set_var(tabvarname, varp, TRUE);
3907 vim_free(tabvarname);
3908 }
3909
3910 // Restore current tabpage
3911 if (valid_tabpage(save_curtab))
3912 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3913 }
3914}
3915
3916/*
3917 * "settabwinvar()" function
3918 */
3919 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003920f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003921{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003922 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003923}
3924
3925/*
3926 * "setwinvar()" function
3927 */
3928 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003929f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003930{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003931 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003932}
3933
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003934/*
3935 * "setbufvar()" function
3936 */
3937 void
3938f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3939{
3940 buf_T *buf;
3941 char_u *varname, *bufvarname;
3942 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003943
3944 if (check_secure())
3945 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003946 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003947 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003948 varp = &argvars[2];
3949
3950 if (buf != NULL && varname != NULL && varp != NULL)
3951 {
3952 if (*varname == '&')
3953 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003954 aco_save_T aco;
3955
3956 // set curbuf to be our buf, temporarily
3957 aucmd_prepbuf(&aco, buf);
3958
Bram Moolenaar191929b2020-08-19 21:20:49 +02003959 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003960
3961 // reset notion of buffer
3962 aucmd_restbuf(&aco);
3963 }
3964 else
3965 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003966 bufvarname = alloc(STRLEN(varname) + 3);
3967 if (bufvarname != NULL)
3968 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003969 buf_T *save_curbuf = curbuf;
3970
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003971 curbuf = buf;
3972 STRCPY(bufvarname, "b:");
3973 STRCPY(bufvarname + 2, varname);
3974 set_var(bufvarname, varp, TRUE);
3975 vim_free(bufvarname);
3976 curbuf = save_curbuf;
3977 }
3978 }
3979 }
3980}
3981
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003982/*
3983 * Get a callback from "arg". It can be a Funcref or a function name.
3984 * When "arg" is zero return an empty string.
3985 * "cb_name" is not allocated.
3986 * "cb_name" is set to NULL for an invalid argument.
3987 */
3988 callback_T
3989get_callback(typval_T *arg)
3990{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003991 callback_T res;
3992 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003993
3994 res.cb_free_name = FALSE;
3995 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3996 {
3997 res.cb_partial = arg->vval.v_partial;
3998 ++res.cb_partial->pt_refcount;
3999 res.cb_name = partial_name(res.cb_partial);
4000 }
4001 else
4002 {
4003 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004004 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4005 && isdigit(*arg->vval.v_string))
4006 r = FAIL;
4007 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004008 {
4009 // Note that we don't make a copy of the string.
4010 res.cb_name = arg->vval.v_string;
4011 func_ref(res.cb_name);
4012 }
4013 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004014 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004015 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004016 r = FAIL;
4017
4018 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004019 {
4020 emsg(_("E921: Invalid callback argument"));
4021 res.cb_name = NULL;
4022 }
4023 }
4024 return res;
4025}
4026
4027/*
4028 * Copy a callback into a typval_T.
4029 */
4030 void
4031put_callback(callback_T *cb, typval_T *tv)
4032{
4033 if (cb->cb_partial != NULL)
4034 {
4035 tv->v_type = VAR_PARTIAL;
4036 tv->vval.v_partial = cb->cb_partial;
4037 ++tv->vval.v_partial->pt_refcount;
4038 }
4039 else
4040 {
4041 tv->v_type = VAR_FUNC;
4042 tv->vval.v_string = vim_strsave(cb->cb_name);
4043 func_ref(cb->cb_name);
4044 }
4045}
4046
4047/*
4048 * Make a copy of "src" into "dest", allocating the function name if needed,
4049 * without incrementing the refcount.
4050 */
4051 void
4052set_callback(callback_T *dest, callback_T *src)
4053{
4054 if (src->cb_partial == NULL)
4055 {
4056 // just a function name, make a copy
4057 dest->cb_name = vim_strsave(src->cb_name);
4058 dest->cb_free_name = TRUE;
4059 }
4060 else
4061 {
4062 // cb_name is a pointer into cb_partial
4063 dest->cb_name = src->cb_name;
4064 dest->cb_free_name = FALSE;
4065 }
4066 dest->cb_partial = src->cb_partial;
4067}
4068
4069/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004070 * Copy callback from "src" to "dest", incrementing the refcounts.
4071 */
4072 void
4073copy_callback(callback_T *dest, callback_T *src)
4074{
4075 dest->cb_partial = src->cb_partial;
4076 if (dest->cb_partial != NULL)
4077 {
4078 dest->cb_name = src->cb_name;
4079 dest->cb_free_name = FALSE;
4080 ++dest->cb_partial->pt_refcount;
4081 }
4082 else
4083 {
4084 dest->cb_name = vim_strsave(src->cb_name);
4085 dest->cb_free_name = TRUE;
4086 func_ref(src->cb_name);
4087 }
4088}
4089
4090/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004091 * Unref/free "callback" returned by get_callback() or set_callback().
4092 */
4093 void
4094free_callback(callback_T *callback)
4095{
4096 if (callback->cb_partial != NULL)
4097 {
4098 partial_unref(callback->cb_partial);
4099 callback->cb_partial = NULL;
4100 }
4101 else if (callback->cb_name != NULL)
4102 func_unref(callback->cb_name);
4103 if (callback->cb_free_name)
4104 {
4105 vim_free(callback->cb_name);
4106 callback->cb_free_name = FALSE;
4107 }
4108 callback->cb_name = NULL;
4109}
4110
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004111#endif // FEAT_EVAL