blob: 9e11578e13c01cfaede3d3f2f225aa517220daed [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 Moolenaar30fd8202020-09-26 15:09:30 +0200741 int flags = eap->cmdidx == CMD_const ? ASSIGN_CONST : 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 {
746 // In legacy Vim script ":final" is short for ":finally".
747 ex_finally(eap);
748 return;
749 }
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 Moolenaar8a7d6542020-01-26 15:56:19 +0100759 // detect Vim9 assignment without ":let" or ":const"
760 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200761 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200763 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200764 if (argend == NULL)
765 return;
766 if (argend > arg && argend[-1] == '.') // for var.='str'
767 --argend;
768 expr = skipwhite(argend);
769 concat = expr[0] == '.'
770 && ((expr[1] == '=' && current_sctx.sc_version < 2)
771 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200772 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
773 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200774 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200775 {
776 // ":let" without "=": list variables
777 if (*arg == '[')
778 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200779 else if (expr[0] == '.' && expr[1] == '=')
780 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200781 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200782 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200783 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200784 {
785 // Vim9 declaration ":let var: type"
786 arg = vim9_declare_scriptvar(eap, arg);
787 }
788 else
789 {
790 // ":let var1 var2" - list values
791 arg = list_arg_vars(eap, arg, &first);
792 }
793 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200794 else if (!eap->skip)
795 {
796 // ":let"
797 list_glob_vars(&first);
798 list_buf_vars(&first);
799 list_win_vars(&first);
800 list_tab_vars(&first);
801 list_script_vars(&first);
802 list_func_vars(&first);
803 list_vim_vars(&first);
804 }
805 eap->nextcmd = check_nextcmd(arg);
806 }
807 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
808 {
809 list_T *l;
810
811 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200812 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200813 if (l != NULL)
814 {
815 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200816 if (!eap->skip)
817 {
818 op[0] = '=';
819 op[1] = NUL;
820 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200822 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200823 clear_tv(&rettv);
824 }
825 }
826 else
827 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200828 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200829 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200830
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200831 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200832 i = FAIL;
833 if (has_assign || concat)
834 {
835 op[0] = '=';
836 op[1] = NUL;
837 if (*expr != '=')
838 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200839 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200840 {
841 // +=, /=, etc. require an existing variable
842 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
843 i = FAIL;
844 }
845 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200846 {
847 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200848 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200849 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200850 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200851 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200852 ++len;
853 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200854 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200855 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200856 }
857 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200858 ++expr;
859
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200860 if (vim9script && (!VIM_ISWHITE(*argend)
861 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200862 {
863 vim_strncpy(op, expr - len, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200864 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200865 i = FAIL;
866 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200867
868 if (eap->skip)
869 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200870 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200871 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200872 if (getline_equal(eap->getline, eap->cookie, getsourceline))
873 {
874 evalarg.eval_getline = eap->getline;
875 evalarg.eval_cookie = eap->cookie;
876 }
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200877 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200878 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200879 if (eap->skip)
880 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200881 clear_evalarg(&evalarg, eap);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200882 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200883 if (eap->skip)
884 {
885 if (i != FAIL)
886 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200887 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200888 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200889 {
890 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200891 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200892 clear_tv(&rettv);
893 }
894 }
895}
896
897/*
898 * Assign the typevalue "tv" to the variable or variables at "arg_start".
899 * Handles both "var" with any type and "[var, var; var]" with a list type.
900 * When "op" is not NULL it points to a string with characters that
901 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
902 * or concatenate.
903 * Returns OK or FAIL;
904 */
905 int
906ex_let_vars(
907 char_u *arg_start,
908 typval_T *tv,
909 int copy, // copy values from "tv", don't move
910 int semicolon, // from skip_var_list()
911 int var_count, // from skip_var_list()
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200912 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200913 char_u *op)
914{
915 char_u *arg = arg_start;
916 list_T *l;
917 int i;
918 listitem_T *item;
919 typval_T ltv;
920
921 if (*arg != '[')
922 {
923 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100924 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200925 return FAIL;
926 return OK;
927 }
928
929 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
930 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
931 {
932 emsg(_(e_listreq));
933 return FAIL;
934 }
935
936 i = list_len(l);
937 if (semicolon == 0 && var_count < i)
938 {
939 emsg(_("E687: Less targets than List items"));
940 return FAIL;
941 }
942 if (var_count - semicolon > i)
943 {
944 emsg(_("E688: More targets than List items"));
945 return FAIL;
946 }
947
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200948 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200949 item = l->lv_first;
950 while (*arg != ']')
951 {
952 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100953 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200954 item = item->li_next;
955 if (arg == NULL)
956 return FAIL;
957
958 arg = skipwhite(arg);
959 if (*arg == ';')
960 {
961 // Put the rest of the list (may be empty) in the var after ';'.
962 // Create a new list for this.
963 l = list_alloc();
964 if (l == NULL)
965 return FAIL;
966 while (item != NULL)
967 {
968 list_append_tv(l, &item->li_tv);
969 item = item->li_next;
970 }
971
972 ltv.v_type = VAR_LIST;
973 ltv.v_lock = 0;
974 ltv.vval.v_list = l;
975 l->lv_refcount = 1;
976
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100977 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200978 (char_u *)"]", op);
979 clear_tv(&ltv);
980 if (arg == NULL)
981 return FAIL;
982 break;
983 }
984 else if (*arg != ',' && *arg != ']')
985 {
986 internal_error("ex_let_vars()");
987 return FAIL;
988 }
989 }
990
991 return OK;
992}
993
994/*
995 * Skip over assignable variable "var" or list of variables "[var, var]".
996 * Used for ":let varvar = expr" and ":for varvar in expr".
997 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200998 * for "[var, var; var]" set "semicolon" to 1.
999 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001000 * Return NULL for an error.
1001 */
1002 char_u *
1003skip_var_list(
1004 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001006 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001007 int *semicolon,
1008 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001009{
1010 char_u *p, *s;
1011
1012 if (*arg == '[')
1013 {
1014 // "[var, var]": find the matching ']'.
1015 p = arg;
1016 for (;;)
1017 {
1018 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001019 s = skip_var_one(p, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001020 if (s == p)
1021 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001022 if (!silent)
1023 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001024 return NULL;
1025 }
1026 ++*var_count;
1027
1028 p = skipwhite(s);
1029 if (*p == ']')
1030 break;
1031 else if (*p == ';')
1032 {
1033 if (*semicolon == 1)
1034 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001035 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001036 return NULL;
1037 }
1038 *semicolon = 1;
1039 }
1040 else if (*p != ',')
1041 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001042 if (!silent)
1043 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001044 return NULL;
1045 }
1046 }
1047 return p + 1;
1048 }
1049 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001051}
1052
1053/*
1054 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1055 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001057 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001058 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001060{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001061 char_u *end;
1062
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001063 if (*arg == '@' && arg[1] != NUL)
1064 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001065 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001066 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001067 if (include_type && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001069 // "a: type" is declaring variable "a" with a type, not "a:".
1070 if (end == arg + 2 && end[-1] == ':')
1071 --end;
1072 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001073 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001074 }
1075 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001076}
1077
1078/*
1079 * List variables for hashtab "ht" with prefix "prefix".
1080 * If "empty" is TRUE also list NULL strings as empty strings.
1081 */
1082 void
1083list_hashtable_vars(
1084 hashtab_T *ht,
1085 char *prefix,
1086 int empty,
1087 int *first)
1088{
1089 hashitem_T *hi;
1090 dictitem_T *di;
1091 int todo;
1092 char_u buf[IOSIZE];
1093
1094 todo = (int)ht->ht_used;
1095 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1096 {
1097 if (!HASHITEM_EMPTY(hi))
1098 {
1099 --todo;
1100 di = HI2DI(hi);
1101
1102 // apply :filter /pat/ to variable name
1103 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1104 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1105 if (message_filtered(buf))
1106 continue;
1107
1108 if (empty || di->di_tv.v_type != VAR_STRING
1109 || di->di_tv.vval.v_string != NULL)
1110 list_one_var(di, prefix, first);
1111 }
1112 }
1113}
1114
1115/*
1116 * List global variables.
1117 */
1118 static void
1119list_glob_vars(int *first)
1120{
1121 list_hashtable_vars(&globvarht, "", TRUE, first);
1122}
1123
1124/*
1125 * List buffer variables.
1126 */
1127 static void
1128list_buf_vars(int *first)
1129{
1130 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1131}
1132
1133/*
1134 * List window variables.
1135 */
1136 static void
1137list_win_vars(int *first)
1138{
1139 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1140}
1141
1142/*
1143 * List tab page variables.
1144 */
1145 static void
1146list_tab_vars(int *first)
1147{
1148 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1149}
1150
1151/*
1152 * List variables in "arg".
1153 */
1154 static char_u *
1155list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1156{
1157 int error = FALSE;
1158 int len;
1159 char_u *name;
1160 char_u *name_start;
1161 char_u *arg_subsc;
1162 char_u *tofree;
1163 typval_T tv;
1164
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001165 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001166 {
1167 if (error || eap->skip)
1168 {
1169 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1170 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1171 {
1172 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001173 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001174 break;
1175 }
1176 }
1177 else
1178 {
1179 // get_name_len() takes care of expanding curly braces
1180 name_start = name = arg;
1181 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1182 if (len <= 0)
1183 {
1184 // This is mainly to keep test 49 working: when expanding
1185 // curly braces fails overrule the exception error message.
1186 if (len < 0 && !aborting())
1187 {
1188 emsg_severe = TRUE;
1189 semsg(_(e_invarg2), arg);
1190 break;
1191 }
1192 error = TRUE;
1193 }
1194 else
1195 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001196 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001197 if (tofree != NULL)
1198 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001199 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001200 error = TRUE;
1201 else
1202 {
1203 // handle d.key, l[idx], f(expr)
1204 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001205 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001206 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001207 error = TRUE;
1208 else
1209 {
1210 if (arg == arg_subsc && len == 2 && name[1] == ':')
1211 {
1212 switch (*name)
1213 {
1214 case 'g': list_glob_vars(first); break;
1215 case 'b': list_buf_vars(first); break;
1216 case 'w': list_win_vars(first); break;
1217 case 't': list_tab_vars(first); break;
1218 case 'v': list_vim_vars(first); break;
1219 case 's': list_script_vars(first); break;
1220 case 'l': list_func_vars(first); break;
1221 default:
1222 semsg(_("E738: Can't list variables for %s"), name);
1223 }
1224 }
1225 else
1226 {
1227 char_u numbuf[NUMBUFLEN];
1228 char_u *tf;
1229 int c;
1230 char_u *s;
1231
1232 s = echo_string(&tv, &tf, numbuf, 0);
1233 c = *arg;
1234 *arg = NUL;
1235 list_one_var_a("",
1236 arg == arg_subsc ? name : name_start,
1237 tv.v_type,
1238 s == NULL ? (char_u *)"" : s,
1239 first);
1240 *arg = c;
1241 vim_free(tf);
1242 }
1243 clear_tv(&tv);
1244 }
1245 }
1246 }
1247
1248 vim_free(tofree);
1249 }
1250
1251 arg = skipwhite(arg);
1252 }
1253
1254 return arg;
1255}
1256
1257/*
1258 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1259 * Returns a pointer to the char just after the var name.
1260 * Returns NULL if there is an error.
1261 */
1262 static char_u *
1263ex_let_one(
1264 char_u *arg, // points to variable name
1265 typval_T *tv, // value to assign to variable
1266 int copy, // copy value from "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001267 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001268 char_u *endchars, // valid chars after variable name or NULL
1269 char_u *op) // "+", "-", "." or NULL
1270{
1271 int c1;
1272 char_u *name;
1273 char_u *p;
1274 char_u *arg_end = NULL;
1275 int len;
1276 int opt_flags;
1277 char_u *tofree = NULL;
1278
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001279 if (in_vim9script() && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001280 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1281 {
1282 vim9_declare_error(arg);
1283 return NULL;
1284 }
1285
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001286 // ":let $VAR = expr": Set environment variable.
1287 if (*arg == '$')
1288 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001289 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001290 {
1291 emsg(_("E996: Cannot lock an environment variable"));
1292 return NULL;
1293 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001294
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001295 // Find the end of the name.
1296 ++arg;
1297 name = arg;
1298 len = get_env_len(&arg);
1299 if (len == 0)
1300 semsg(_(e_invarg2), name - 1);
1301 else
1302 {
1303 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1304 semsg(_(e_letwrong), op);
1305 else if (endchars != NULL
1306 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1307 emsg(_(e_letunexp));
1308 else if (!check_secure())
1309 {
1310 c1 = name[len];
1311 name[len] = NUL;
1312 p = tv_get_string_chk(tv);
1313 if (p != NULL && op != NULL && *op == '.')
1314 {
1315 int mustfree = FALSE;
1316 char_u *s = vim_getenv(name, &mustfree);
1317
1318 if (s != NULL)
1319 {
1320 p = tofree = concat_str(s, p);
1321 if (mustfree)
1322 vim_free(s);
1323 }
1324 }
1325 if (p != NULL)
1326 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001327 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001328 arg_end = arg;
1329 }
1330 name[len] = c1;
1331 vim_free(tofree);
1332 }
1333 }
1334 }
1335
1336 // ":let &option = expr": Set option value.
1337 // ":let &l:option = expr": Set local option value.
1338 // ":let &g:option = expr": Set global option value.
1339 else if (*arg == '&')
1340 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001341 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001342 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001344 return NULL;
1345 }
1346 // Find the end of the name.
1347 p = find_option_end(&arg, &opt_flags);
1348 if (p == NULL || (endchars != NULL
1349 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1350 emsg(_(e_letunexp));
1351 else
1352 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001353 long n = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001354 int opt_type;
1355 long numval;
1356 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001357 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001358 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001359
1360 c1 = *p;
1361 *p = NUL;
1362
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001363 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
1364 if ((opt_type == 1 || opt_type == -1)
1365 && (tv->v_type != VAR_STRING || !in_vim9script()))
1366 // number, possibly hidden
1367 n = (long)tv_get_number(tv);
1368
1369 // Avoid setting a string option to the text "v:false" or similar.
1370 // In Vim9 script also don't convert a number to string.
1371 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1372 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1373 s = tv_get_string_chk(tv);
1374
1375 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001376 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001377 if ((opt_type == 1 && *op == '.')
1378 || (opt_type == 0 && *op != '.'))
1379 {
1380 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001381 failed = TRUE; // don't set the value
1382
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001383 }
1384 else
1385 {
1386 if (opt_type == 1) // number
1387 {
1388 switch (*op)
1389 {
1390 case '+': n = numval + n; break;
1391 case '-': n = numval - n; break;
1392 case '*': n = numval * n; break;
1393 case '/': n = (long)num_divide(numval, n); break;
1394 case '%': n = (long)num_modulus(numval, n); break;
1395 }
1396 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001397 else if (opt_type == 0 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001398 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001399 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001400 s = concat_str(stringval, s);
1401 vim_free(stringval);
1402 stringval = s;
1403 }
1404 }
1405 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001406
1407 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001408 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001409 if (opt_type != 0 || s != NULL)
1410 {
1411 set_option_value(arg, n, s, opt_flags);
1412 arg_end = p;
1413 }
1414 else
1415 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001416 }
1417 *p = c1;
1418 vim_free(stringval);
1419 }
1420 }
1421
1422 // ":let @r = expr": Set register contents.
1423 else if (*arg == '@')
1424 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001425 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001426 {
1427 emsg(_("E996: Cannot lock a register"));
1428 return NULL;
1429 }
1430 ++arg;
1431 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1432 semsg(_(e_letwrong), op);
1433 else if (endchars != NULL
1434 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1435 emsg(_(e_letunexp));
1436 else
1437 {
1438 char_u *ptofree = NULL;
1439 char_u *s;
1440
1441 p = tv_get_string_chk(tv);
1442 if (p != NULL && op != NULL && *op == '.')
1443 {
1444 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1445 if (s != NULL)
1446 {
1447 p = ptofree = concat_str(s, p);
1448 vim_free(s);
1449 }
1450 }
1451 if (p != NULL)
1452 {
1453 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1454 arg_end = arg + 1;
1455 }
1456 vim_free(ptofree);
1457 }
1458 }
1459
1460 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001462 // ":let {expr} = expr": Idem, name made with curly braces
1463 else if (eval_isnamec1(*arg) || *arg == '{')
1464 {
1465 lval_T lv;
1466
1467 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001468 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001469 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 if (endchars != NULL && vim_strchr(endchars,
1471 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001472 emsg(_(e_letunexp));
1473 else
1474 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001475 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001476 arg_end = p;
1477 }
1478 }
1479 clear_lval(&lv);
1480 }
1481
1482 else
1483 semsg(_(e_invarg2), arg);
1484
1485 return arg_end;
1486}
1487
1488/*
1489 * ":unlet[!] var1 ... " command.
1490 */
1491 void
1492ex_unlet(exarg_T *eap)
1493{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001494 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001495}
1496
1497/*
1498 * ":lockvar" and ":unlockvar" commands
1499 */
1500 void
1501ex_lockvar(exarg_T *eap)
1502{
1503 char_u *arg = eap->arg;
1504 int deep = 2;
1505
1506 if (eap->forceit)
1507 deep = -1;
1508 else if (vim_isdigit(*arg))
1509 {
1510 deep = getdigits(&arg);
1511 arg = skipwhite(arg);
1512 }
1513
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001514 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001515}
1516
1517/*
1518 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001519 * Also used for Vim9 script. "callback" is invoked as:
1520 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001521 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001522 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001523ex_unletlock(
1524 exarg_T *eap,
1525 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001526 int deep,
1527 int glv_flags,
1528 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1529 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001530{
1531 char_u *arg = argstart;
1532 char_u *name_end;
1533 int error = FALSE;
1534 lval_T lv;
1535
1536 do
1537 {
1538 if (*arg == '$')
1539 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001540 lv.ll_name = arg;
1541 lv.ll_tv = NULL;
1542 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001543 if (get_env_len(&arg) == 0)
1544 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001545 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001546 return;
1547 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001548 if (!error && !eap->skip
1549 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1550 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001551 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001552 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001553 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001554 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001555 // Parse the name and find the end.
1556 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1557 glv_flags, FNE_CHECK_START);
1558 if (lv.ll_name == NULL)
1559 error = TRUE; // error but continue parsing
1560 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1561 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001562 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001563 if (name_end != NULL)
1564 {
1565 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001566 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001567 }
1568 if (!(eap->skip || error))
1569 clear_lval(&lv);
1570 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001571 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001572
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001573 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001574 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001575 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001576
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001577 if (!eap->skip)
1578 clear_lval(&lv);
1579 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001580
1581 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001582 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001583
1584 eap->nextcmd = check_nextcmd(arg);
1585}
1586
1587 static int
1588do_unlet_var(
1589 lval_T *lp,
1590 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001591 exarg_T *eap,
1592 int deep UNUSED,
1593 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001594{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001595 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001596 int ret = OK;
1597 int cc;
1598
1599 if (lp->ll_tv == NULL)
1600 {
1601 cc = *name_end;
1602 *name_end = NUL;
1603
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001604 // Environment variable, normal name or expanded name.
1605 if (*lp->ll_name == '$')
1606 vim_unsetenv(lp->ll_name + 1);
1607 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001608 ret = FAIL;
1609 *name_end = cc;
1610 }
1611 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001612 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001613 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001614 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001615 return FAIL;
1616 else if (lp->ll_range)
1617 {
1618 listitem_T *li;
1619 listitem_T *ll_li = lp->ll_li;
1620 int ll_n1 = lp->ll_n1;
1621
1622 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1623 {
1624 li = ll_li->li_next;
Bram Moolenaara187c432020-09-16 21:08:28 +02001625 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001626 return FAIL;
1627 ll_li = li;
1628 ++ll_n1;
1629 }
1630
1631 // Delete a range of List items.
1632 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1633 {
1634 li = lp->ll_li->li_next;
1635 listitem_remove(lp->ll_list, lp->ll_li);
1636 lp->ll_li = li;
1637 ++lp->ll_n1;
1638 }
1639 }
1640 else
1641 {
1642 if (lp->ll_list != NULL)
1643 // unlet a List item.
1644 listitem_remove(lp->ll_list, lp->ll_li);
1645 else
1646 // unlet a Dictionary item.
1647 dictitem_remove(lp->ll_dict, lp->ll_di);
1648 }
1649
1650 return ret;
1651}
1652
1653/*
1654 * "unlet" a variable. Return OK if it existed, FAIL if not.
1655 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1656 */
1657 int
1658do_unlet(char_u *name, int forceit)
1659{
1660 hashtab_T *ht;
1661 hashitem_T *hi;
1662 char_u *varname;
1663 dict_T *d;
1664 dictitem_T *di;
1665
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001666 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001667 return FAIL;
1668
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001669 ht = find_var_ht(name, &varname);
1670 if (ht != NULL && *varname != NUL)
1671 {
1672 d = get_current_funccal_dict(ht);
1673 if (d == NULL)
1674 {
1675 if (ht == &globvarht)
1676 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001677 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001678 d = &vimvardict;
1679 else
1680 {
1681 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1682 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1683 }
1684 if (d == NULL)
1685 {
1686 internal_error("do_unlet()");
1687 return FAIL;
1688 }
1689 }
1690 hi = hash_find(ht, varname);
1691 if (HASHITEM_EMPTY(hi))
1692 hi = find_hi_in_scoped_ht(name, &ht);
1693 if (hi != NULL && !HASHITEM_EMPTY(hi))
1694 {
1695 di = HI2DI(hi);
1696 if (var_check_fixed(di->di_flags, name, FALSE)
1697 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001698 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001699 return FAIL;
1700
1701 delete_var(ht, hi);
1702 return OK;
1703 }
1704 }
1705 if (forceit)
1706 return OK;
1707 semsg(_("E108: No such variable: \"%s\""), name);
1708 return FAIL;
1709}
1710
1711/*
1712 * Lock or unlock variable indicated by "lp".
1713 * "deep" is the levels to go (-1 for unlimited);
1714 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1715 */
1716 static int
1717do_lock_var(
1718 lval_T *lp,
1719 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001720 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001721 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001722 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001723{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001724 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001725 int ret = OK;
1726 int cc;
1727 dictitem_T *di;
1728
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001729 if (lp->ll_tv == NULL)
1730 {
1731 cc = *name_end;
1732 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001733 if (*lp->ll_name == '$')
1734 {
1735 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001736 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001737 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001738 else
1739 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001740 // Normal name or expanded name.
1741 di = find_var(lp->ll_name, NULL, TRUE);
1742 if (di == NULL)
1743 ret = FAIL;
1744 else if ((di->di_flags & DI_FLAGS_FIX)
1745 && di->di_tv.v_type != VAR_DICT
1746 && di->di_tv.v_type != VAR_LIST)
1747 {
1748 // For historic reasons this error is not given for a list or
1749 // dict. E.g., the b: dict could be locked/unlocked.
1750 semsg(_(e_lock_unlock), lp->ll_name);
1751 ret = FAIL;
1752 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001753 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001754 {
1755 if (lock)
1756 di->di_flags |= DI_FLAGS_LOCK;
1757 else
1758 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001759 if (deep != 0)
1760 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001761 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001762 }
1763 *name_end = cc;
1764 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001765 else if (deep == 0)
1766 {
1767 // nothing to do
1768 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001769 else if (lp->ll_range)
1770 {
1771 listitem_T *li = lp->ll_li;
1772
1773 // (un)lock a range of List items.
1774 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1775 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001776 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001777 li = li->li_next;
1778 ++lp->ll_n1;
1779 }
1780 }
1781 else if (lp->ll_list != NULL)
1782 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001783 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001784 else
1785 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001786 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001787
1788 return ret;
1789}
1790
1791/*
1792 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001793 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1794 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001795 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001796 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001797item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001798{
1799 static int recurse = 0;
1800 list_T *l;
1801 listitem_T *li;
1802 dict_T *d;
1803 blob_T *b;
1804 hashitem_T *hi;
1805 int todo;
1806
1807 if (recurse >= DICT_MAXNEST)
1808 {
1809 emsg(_("E743: variable nested too deep for (un)lock"));
1810 return;
1811 }
1812 if (deep == 0)
1813 return;
1814 ++recurse;
1815
1816 // lock/unlock the item itself
1817 if (lock)
1818 tv->v_lock |= VAR_LOCKED;
1819 else
1820 tv->v_lock &= ~VAR_LOCKED;
1821
1822 switch (tv->v_type)
1823 {
1824 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001825 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001827 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001829 case VAR_STRING:
1830 case VAR_FUNC:
1831 case VAR_PARTIAL:
1832 case VAR_FLOAT:
1833 case VAR_SPECIAL:
1834 case VAR_JOB:
1835 case VAR_CHANNEL:
1836 break;
1837
1838 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001839 if ((b = tv->vval.v_blob) != NULL
1840 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001841 {
1842 if (lock)
1843 b->bv_lock |= VAR_LOCKED;
1844 else
1845 b->bv_lock &= ~VAR_LOCKED;
1846 }
1847 break;
1848 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001849 if ((l = tv->vval.v_list) != NULL
1850 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001851 {
1852 if (lock)
1853 l->lv_lock |= VAR_LOCKED;
1854 else
1855 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001856 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001857 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001858 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001859 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001860 }
1861 break;
1862 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001863 if ((d = tv->vval.v_dict) != NULL
1864 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001865 {
1866 if (lock)
1867 d->dv_lock |= VAR_LOCKED;
1868 else
1869 d->dv_lock &= ~VAR_LOCKED;
1870 if (deep < 0 || deep > 1)
1871 {
1872 // recursive: lock/unlock the items the List contains
1873 todo = (int)d->dv_hashtab.ht_used;
1874 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1875 {
1876 if (!HASHITEM_EMPTY(hi))
1877 {
1878 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001879 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1880 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001881 }
1882 }
1883 }
1884 }
1885 }
1886 --recurse;
1887}
1888
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001889#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1890/*
1891 * Delete all "menutrans_" variables.
1892 */
1893 void
1894del_menutrans_vars(void)
1895{
1896 hashitem_T *hi;
1897 int todo;
1898
1899 hash_lock(&globvarht);
1900 todo = (int)globvarht.ht_used;
1901 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1902 {
1903 if (!HASHITEM_EMPTY(hi))
1904 {
1905 --todo;
1906 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1907 delete_var(&globvarht, hi);
1908 }
1909 }
1910 hash_unlock(&globvarht);
1911}
1912#endif
1913
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001914/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001915 * Local string buffer for the next two functions to store a variable name
1916 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1917 * get_user_var_name().
1918 */
1919
1920static char_u *varnamebuf = NULL;
1921static int varnamebuflen = 0;
1922
1923/*
1924 * Function to concatenate a prefix and a variable name.
1925 */
1926 static char_u *
1927cat_prefix_varname(int prefix, char_u *name)
1928{
1929 int len;
1930
1931 len = (int)STRLEN(name) + 3;
1932 if (len > varnamebuflen)
1933 {
1934 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001935 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001936 varnamebuf = alloc(len);
1937 if (varnamebuf == NULL)
1938 {
1939 varnamebuflen = 0;
1940 return NULL;
1941 }
1942 varnamebuflen = len;
1943 }
1944 *varnamebuf = prefix;
1945 varnamebuf[1] = ':';
1946 STRCPY(varnamebuf + 2, name);
1947 return varnamebuf;
1948}
1949
1950/*
1951 * Function given to ExpandGeneric() to obtain the list of user defined
1952 * (global/buffer/window/built-in) variable names.
1953 */
1954 char_u *
1955get_user_var_name(expand_T *xp, int idx)
1956{
1957 static long_u gdone;
1958 static long_u bdone;
1959 static long_u wdone;
1960 static long_u tdone;
1961 static int vidx;
1962 static hashitem_T *hi;
1963 hashtab_T *ht;
1964
1965 if (idx == 0)
1966 {
1967 gdone = bdone = wdone = vidx = 0;
1968 tdone = 0;
1969 }
1970
1971 // Global variables
1972 if (gdone < globvarht.ht_used)
1973 {
1974 if (gdone++ == 0)
1975 hi = globvarht.ht_array;
1976 else
1977 ++hi;
1978 while (HASHITEM_EMPTY(hi))
1979 ++hi;
1980 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1981 return cat_prefix_varname('g', hi->hi_key);
1982 return hi->hi_key;
1983 }
1984
1985 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01001986 ht =
1987#ifdef FEAT_CMDWIN
1988 // In cmdwin, the alternative buffer should be used.
1989 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
1990 &prevwin->w_buffer->b_vars->dv_hashtab :
1991#endif
1992 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001993 if (bdone < ht->ht_used)
1994 {
1995 if (bdone++ == 0)
1996 hi = ht->ht_array;
1997 else
1998 ++hi;
1999 while (HASHITEM_EMPTY(hi))
2000 ++hi;
2001 return cat_prefix_varname('b', hi->hi_key);
2002 }
2003
2004 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002005 ht =
2006#ifdef FEAT_CMDWIN
2007 // In cmdwin, the alternative window should be used.
2008 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2009 &prevwin->w_vars->dv_hashtab :
2010#endif
2011 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002012 if (wdone < ht->ht_used)
2013 {
2014 if (wdone++ == 0)
2015 hi = ht->ht_array;
2016 else
2017 ++hi;
2018 while (HASHITEM_EMPTY(hi))
2019 ++hi;
2020 return cat_prefix_varname('w', hi->hi_key);
2021 }
2022
2023 // t: variables
2024 ht = &curtab->tp_vars->dv_hashtab;
2025 if (tdone < ht->ht_used)
2026 {
2027 if (tdone++ == 0)
2028 hi = ht->ht_array;
2029 else
2030 ++hi;
2031 while (HASHITEM_EMPTY(hi))
2032 ++hi;
2033 return cat_prefix_varname('t', hi->hi_key);
2034 }
2035
2036 // v: variables
2037 if (vidx < VV_LEN)
2038 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2039
2040 VIM_CLEAR(varnamebuf);
2041 varnamebuflen = 0;
2042 return NULL;
2043}
2044
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002045 char *
2046get_var_special_name(int nr)
2047{
2048 switch (nr)
2049 {
2050 case VVAL_FALSE: return "v:false";
2051 case VVAL_TRUE: return "v:true";
2052 case VVAL_NONE: return "v:none";
2053 case VVAL_NULL: return "v:null";
2054 }
2055 internal_error("get_var_special_name()");
2056 return "42";
2057}
2058
2059/*
2060 * Returns the global variable dictionary
2061 */
2062 dict_T *
2063get_globvar_dict(void)
2064{
2065 return &globvardict;
2066}
2067
2068/*
2069 * Returns the global variable hash table
2070 */
2071 hashtab_T *
2072get_globvar_ht(void)
2073{
2074 return &globvarht;
2075}
2076
2077/*
2078 * Returns the v: variable dictionary
2079 */
2080 dict_T *
2081get_vimvar_dict(void)
2082{
2083 return &vimvardict;
2084}
2085
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002086/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002088 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089 */
2090 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002091find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002093 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2094 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002095
2096 if (di == NULL)
2097 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002098 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002099 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2100 return (int)(vv - vimvars);
2101}
2102
2103
2104/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002105 * Set type of v: variable to "type".
2106 */
2107 void
2108set_vim_var_type(int idx, vartype_T type)
2109{
2110 vimvars[idx].vv_type = type;
2111}
2112
2113/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002114 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002115 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002116 */
2117 void
2118set_vim_var_nr(int idx, varnumber_T val)
2119{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002120 vimvars[idx].vv_nr = val;
2121}
2122
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123 char *
2124get_vim_var_name(int idx)
2125{
2126 return vimvars[idx].vv_name;
2127}
2128
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002129/*
2130 * Get typval_T v: variable value.
2131 */
2132 typval_T *
2133get_vim_var_tv(int idx)
2134{
2135 return &vimvars[idx].vv_tv;
2136}
2137
2138/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002139 * Set v: variable to "tv". Only accepts the same type.
2140 * Takes over the value of "tv".
2141 */
2142 int
2143set_vim_var_tv(int idx, typval_T *tv)
2144{
2145 if (vimvars[idx].vv_type != tv->v_type)
2146 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002147 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002148 clear_tv(tv);
2149 return FAIL;
2150 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002151 // VV_RO is also checked when compiling, but let's check here as well.
2152 if (vimvars[idx].vv_flags & VV_RO)
2153 {
2154 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2155 return FAIL;
2156 }
2157 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2158 {
2159 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2160 return FAIL;
2161 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002162 clear_tv(&vimvars[idx].vv_di.di_tv);
2163 vimvars[idx].vv_di.di_tv = *tv;
2164 return OK;
2165}
2166
2167/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002168 * Get number v: variable value.
2169 */
2170 varnumber_T
2171get_vim_var_nr(int idx)
2172{
2173 return vimvars[idx].vv_nr;
2174}
2175
2176/*
2177 * Get string v: variable value. Uses a static buffer, can only be used once.
2178 * If the String variable has never been set, return an empty string.
2179 * Never returns NULL;
2180 */
2181 char_u *
2182get_vim_var_str(int idx)
2183{
2184 return tv_get_string(&vimvars[idx].vv_tv);
2185}
2186
2187/*
2188 * Get List v: variable value. Caller must take care of reference count when
2189 * needed.
2190 */
2191 list_T *
2192get_vim_var_list(int idx)
2193{
2194 return vimvars[idx].vv_list;
2195}
2196
2197/*
2198 * Get Dict v: variable value. Caller must take care of reference count when
2199 * needed.
2200 */
2201 dict_T *
2202get_vim_var_dict(int idx)
2203{
2204 return vimvars[idx].vv_dict;
2205}
2206
2207/*
2208 * Set v:char to character "c".
2209 */
2210 void
2211set_vim_var_char(int c)
2212{
2213 char_u buf[MB_MAXBYTES + 1];
2214
2215 if (has_mbyte)
2216 buf[(*mb_char2bytes)(c, buf)] = NUL;
2217 else
2218 {
2219 buf[0] = c;
2220 buf[1] = NUL;
2221 }
2222 set_vim_var_string(VV_CHAR, buf, -1);
2223}
2224
2225/*
2226 * Set v:count to "count" and v:count1 to "count1".
2227 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2228 */
2229 void
2230set_vcount(
2231 long count,
2232 long count1,
2233 int set_prevcount)
2234{
2235 if (set_prevcount)
2236 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2237 vimvars[VV_COUNT].vv_nr = count;
2238 vimvars[VV_COUNT1].vv_nr = count1;
2239}
2240
2241/*
2242 * Save variables that might be changed as a side effect. Used when executing
2243 * a timer callback.
2244 */
2245 void
2246save_vimvars(vimvars_save_T *vvsave)
2247{
2248 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2249 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2250 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2251}
2252
2253/*
2254 * Restore variables saved by save_vimvars().
2255 */
2256 void
2257restore_vimvars(vimvars_save_T *vvsave)
2258{
2259 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2260 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2261 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2262}
2263
2264/*
2265 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2266 * value.
2267 */
2268 void
2269set_vim_var_string(
2270 int idx,
2271 char_u *val,
2272 int len) // length of "val" to use or -1 (whole string)
2273{
2274 clear_tv(&vimvars[idx].vv_di.di_tv);
2275 vimvars[idx].vv_type = VAR_STRING;
2276 if (val == NULL)
2277 vimvars[idx].vv_str = NULL;
2278 else if (len == -1)
2279 vimvars[idx].vv_str = vim_strsave(val);
2280 else
2281 vimvars[idx].vv_str = vim_strnsave(val, len);
2282}
2283
2284/*
2285 * Set List v: variable to "val".
2286 */
2287 void
2288set_vim_var_list(int idx, list_T *val)
2289{
2290 clear_tv(&vimvars[idx].vv_di.di_tv);
2291 vimvars[idx].vv_type = VAR_LIST;
2292 vimvars[idx].vv_list = val;
2293 if (val != NULL)
2294 ++val->lv_refcount;
2295}
2296
2297/*
2298 * Set Dictionary v: variable to "val".
2299 */
2300 void
2301set_vim_var_dict(int idx, dict_T *val)
2302{
2303 clear_tv(&vimvars[idx].vv_di.di_tv);
2304 vimvars[idx].vv_type = VAR_DICT;
2305 vimvars[idx].vv_dict = val;
2306 if (val != NULL)
2307 {
2308 ++val->dv_refcount;
2309 dict_set_items_ro(val);
2310 }
2311}
2312
2313/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002314 * Set the v:argv list.
2315 */
2316 void
2317set_argv_var(char **argv, int argc)
2318{
2319 list_T *l = list_alloc();
2320 int i;
2321
2322 if (l == NULL)
2323 getout(1);
2324 l->lv_lock = VAR_FIXED;
2325 for (i = 0; i < argc; ++i)
2326 {
2327 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2328 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002329 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002330 }
2331 set_vim_var_list(VV_ARGV, l);
2332}
2333
2334/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002335 * Reset v:register, taking the 'clipboard' setting into account.
2336 */
2337 void
2338reset_reg_var(void)
2339{
2340 int regname = 0;
2341
2342 // Adjust the register according to 'clipboard', so that when
2343 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2344#ifdef FEAT_CLIPBOARD
2345 adjust_clip_reg(&regname);
2346#endif
2347 set_reg_var(regname);
2348}
2349
2350/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002351 * Set v:register if needed.
2352 */
2353 void
2354set_reg_var(int c)
2355{
2356 char_u regname;
2357
2358 if (c == 0 || c == ' ')
2359 regname = '"';
2360 else
2361 regname = c;
2362 // Avoid free/alloc when the value is already right.
2363 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2364 set_vim_var_string(VV_REG, &regname, 1);
2365}
2366
2367/*
2368 * Get or set v:exception. If "oldval" == NULL, return the current value.
2369 * Otherwise, restore the value to "oldval" and return NULL.
2370 * Must always be called in pairs to save and restore v:exception! Does not
2371 * take care of memory allocations.
2372 */
2373 char_u *
2374v_exception(char_u *oldval)
2375{
2376 if (oldval == NULL)
2377 return vimvars[VV_EXCEPTION].vv_str;
2378
2379 vimvars[VV_EXCEPTION].vv_str = oldval;
2380 return NULL;
2381}
2382
2383/*
2384 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2385 * Otherwise, restore the value to "oldval" and return NULL.
2386 * Must always be called in pairs to save and restore v:throwpoint! Does not
2387 * take care of memory allocations.
2388 */
2389 char_u *
2390v_throwpoint(char_u *oldval)
2391{
2392 if (oldval == NULL)
2393 return vimvars[VV_THROWPOINT].vv_str;
2394
2395 vimvars[VV_THROWPOINT].vv_str = oldval;
2396 return NULL;
2397}
2398
2399/*
2400 * Set v:cmdarg.
2401 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2402 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2403 * Must always be called in pairs!
2404 */
2405 char_u *
2406set_cmdarg(exarg_T *eap, char_u *oldarg)
2407{
2408 char_u *oldval;
2409 char_u *newval;
2410 unsigned len;
2411
2412 oldval = vimvars[VV_CMDARG].vv_str;
2413 if (eap == NULL)
2414 {
2415 vim_free(oldval);
2416 vimvars[VV_CMDARG].vv_str = oldarg;
2417 return NULL;
2418 }
2419
2420 if (eap->force_bin == FORCE_BIN)
2421 len = 6;
2422 else if (eap->force_bin == FORCE_NOBIN)
2423 len = 8;
2424 else
2425 len = 0;
2426
2427 if (eap->read_edit)
2428 len += 7;
2429
2430 if (eap->force_ff != 0)
2431 len += 10; // " ++ff=unix"
2432 if (eap->force_enc != 0)
2433 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2434 if (eap->bad_char != 0)
2435 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2436
2437 newval = alloc(len + 1);
2438 if (newval == NULL)
2439 return NULL;
2440
2441 if (eap->force_bin == FORCE_BIN)
2442 sprintf((char *)newval, " ++bin");
2443 else if (eap->force_bin == FORCE_NOBIN)
2444 sprintf((char *)newval, " ++nobin");
2445 else
2446 *newval = NUL;
2447
2448 if (eap->read_edit)
2449 STRCAT(newval, " ++edit");
2450
2451 if (eap->force_ff != 0)
2452 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2453 eap->force_ff == 'u' ? "unix"
2454 : eap->force_ff == 'd' ? "dos"
2455 : "mac");
2456 if (eap->force_enc != 0)
2457 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2458 eap->cmd + eap->force_enc);
2459 if (eap->bad_char == BAD_KEEP)
2460 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2461 else if (eap->bad_char == BAD_DROP)
2462 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2463 else if (eap->bad_char != 0)
2464 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2465 vimvars[VV_CMDARG].vv_str = newval;
2466 return oldval;
2467}
2468
2469/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002470 * Get the value of internal variable "name".
2471 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2472 */
2473 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002474eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002475 char_u *name,
2476 int len, // length of "name"
2477 typval_T *rettv, // NULL when only checking existence
2478 dictitem_T **dip, // non-NULL when typval's dict item is needed
2479 int verbose, // may give error message
2480 int no_autoload) // do not use script autoloading
2481{
2482 int ret = OK;
2483 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002484 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002485 dictitem_T *v;
2486 int cc;
2487
2488 // truncate the name, so that we can use strcmp()
2489 cc = name[len];
2490 name[len] = NUL;
2491
2492 // Check for user-defined variables.
2493 v = find_var(name, NULL, no_autoload);
2494 if (v != NULL)
2495 {
2496 tv = &v->di_tv;
2497 if (dip != NULL)
2498 *dip = v;
2499 }
2500
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002501 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002503 imported_T *import;
2504 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2505
2506 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507
2508 // imported variable from another script
2509 if (import != NULL)
2510 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002511 if (import->imp_funcname != NULL)
2512 {
2513 foundFunc = TRUE;
2514 if (rettv != NULL)
2515 {
2516 rettv->v_type = VAR_FUNC;
2517 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2518 }
2519 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002520 else if (import->imp_all)
2521 {
2522 emsg("Sorry, 'import * as X' not implemented yet");
2523 ret = FAIL;
2524 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002525 else
2526 {
2527 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002528 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002530 tv = sv->sv_tv;
2531 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002533 else if (in_vim9script())
2534 {
2535 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2536
2537 if (ufunc != NULL)
2538 {
2539 foundFunc = TRUE;
2540 if (rettv != NULL)
2541 {
2542 rettv->v_type = VAR_FUNC;
2543 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2544 }
2545 }
2546 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547 }
2548
Bram Moolenaarc620c052020-07-08 15:16:19 +02002549 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002550 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002551 if (tv == NULL)
2552 {
2553 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002554 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002555 ret = FAIL;
2556 }
2557 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002558 {
2559 // If a list or dict variable wasn't initialized, do it now.
2560 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2561 {
2562 tv->vval.v_dict = dict_alloc();
2563 if (tv->vval.v_dict != NULL)
2564 ++tv->vval.v_dict->dv_refcount;
2565 }
2566 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2567 {
2568 tv->vval.v_list = list_alloc();
2569 if (tv->vval.v_list != NULL)
2570 ++tv->vval.v_list->lv_refcount;
2571 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002572 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002573 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002574 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002575
2576 name[len] = cc;
2577
2578 return ret;
2579}
2580
2581/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002582 * Check if variable "name[len]" is a local variable or an argument.
2583 * If so, "*eval_lavars_used" is set to TRUE.
2584 */
2585 void
2586check_vars(char_u *name, int len)
2587{
2588 int cc;
2589 char_u *varname;
2590 hashtab_T *ht;
2591
2592 if (eval_lavars_used == NULL)
2593 return;
2594
2595 // truncate the name, so that we can use strcmp()
2596 cc = name[len];
2597 name[len] = NUL;
2598
2599 ht = find_var_ht(name, &varname);
2600 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2601 {
2602 if (find_var(name, NULL, TRUE) != NULL)
2603 *eval_lavars_used = TRUE;
2604 }
2605
2606 name[len] = cc;
2607}
2608
2609/*
2610 * Find variable "name" in the list of variables.
2611 * Return a pointer to it if found, NULL if not found.
2612 * Careful: "a:0" variables don't have a name.
2613 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2614 * hashtab_T used.
2615 */
2616 dictitem_T *
2617find_var(char_u *name, hashtab_T **htp, int no_autoload)
2618{
2619 char_u *varname;
2620 hashtab_T *ht;
2621 dictitem_T *ret = NULL;
2622
2623 ht = find_var_ht(name, &varname);
2624 if (htp != NULL)
2625 *htp = ht;
2626 if (ht == NULL)
2627 return NULL;
2628 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2629 if (ret != NULL)
2630 return ret;
2631
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002632 // Search in parent scope for lambda
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002633 ret = find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2634 if (ret != NULL)
2635 return ret;
2636
2637 // in Vim9 script items without a scope can be script-local
2638 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2639 {
2640 ht = get_script_local_ht();
2641 if (ht != NULL)
2642 {
2643 ret = find_var_in_ht(ht, *name, varname,
2644 no_autoload || htp != NULL);
2645 if (ret != NULL)
2646 {
2647 if (htp != NULL)
2648 *htp = ht;
2649 return ret;
2650 }
2651 }
2652 }
2653
2654 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002655}
2656
2657/*
2658 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002659 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002660 * Returns NULL if not found.
2661 */
2662 dictitem_T *
2663find_var_in_ht(
2664 hashtab_T *ht,
2665 int htname,
2666 char_u *varname,
2667 int no_autoload)
2668{
2669 hashitem_T *hi;
2670
2671 if (*varname == NUL)
2672 {
2673 // Must be something like "s:", otherwise "ht" would be NULL.
2674 switch (htname)
2675 {
2676 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2677 case 'g': return &globvars_var;
2678 case 'v': return &vimvars_var;
2679 case 'b': return &curbuf->b_bufvar;
2680 case 'w': return &curwin->w_winvar;
2681 case 't': return &curtab->tp_winvar;
2682 case 'l': return get_funccal_local_var();
2683 case 'a': return get_funccal_args_var();
2684 }
2685 return NULL;
2686 }
2687
2688 hi = hash_find(ht, varname);
2689 if (HASHITEM_EMPTY(hi))
2690 {
2691 // For global variables we may try auto-loading the script. If it
2692 // worked find the variable again. Don't auto-load a script if it was
2693 // loaded already, otherwise it would be loaded every time when
2694 // checking if a function name is a Funcref variable.
2695 if (ht == &globvarht && !no_autoload)
2696 {
2697 // Note: script_autoload() may make "hi" invalid. It must either
2698 // be obtained again or not used.
2699 if (!script_autoload(varname, FALSE) || aborting())
2700 return NULL;
2701 hi = hash_find(ht, varname);
2702 }
2703 if (HASHITEM_EMPTY(hi))
2704 return NULL;
2705 }
2706 return HI2DI(hi);
2707}
2708
2709/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 * Get the script-local hashtab. NULL if not in a script context.
2711 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002712 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713get_script_local_ht(void)
2714{
2715 scid_T sid = current_sctx.sc_sid;
2716
Bram Moolenaare3d46852020-08-29 13:39:17 +02002717 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718 return &SCRIPT_VARS(sid);
2719 return NULL;
2720}
2721
2722/*
2723 * Look for "name[len]" in script-local variables.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002724 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01002726 int
2727lookup_scriptvar(
2728 char_u *name,
2729 size_t len,
2730 void *lvar UNUSED,
2731 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002732{
2733 hashtab_T *ht = get_script_local_ht();
2734 char_u buffer[30];
2735 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01002736 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002737 hashitem_T *hi;
2738
2739 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002740 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 if (len < sizeof(buffer) - 1)
2742 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002743 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002744 vim_strncpy(buffer, name, len);
2745 p = buffer;
2746 }
2747 else
2748 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002749 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002750 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002751 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752 }
2753
2754 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002755 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756
2757 // if not script-local, then perhaps imported
Bram Moolenaar709664c2020-12-12 14:33:41 +01002758 if (res == FAIL && find_imported(p, 0, NULL) != NULL)
2759 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760
2761 if (p != buffer)
2762 vim_free(p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002763 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764}
2765
2766/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002767 * Find the hashtab used for a variable name.
2768 * Return NULL if the name is not valid.
2769 * Set "varname" to the start of name without ':'.
2770 */
2771 hashtab_T *
2772find_var_ht(char_u *name, char_u **varname)
2773{
2774 hashitem_T *hi;
2775 hashtab_T *ht;
2776
2777 if (name[0] == NUL)
2778 return NULL;
2779 if (name[1] != ':')
2780 {
2781 // The name must not start with a colon or #.
2782 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2783 return NULL;
2784 *varname = name;
2785
2786 // "version" is "v:version" in all scopes if scriptversion < 3.
2787 // Same for a few other variables marked with VV_COMPAT.
2788 if (current_sctx.sc_version < 3)
2789 {
2790 hi = hash_find(&compat_hashtab, name);
2791 if (!HASHITEM_EMPTY(hi))
2792 return &compat_hashtab;
2793 }
2794
2795 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 if (ht != NULL)
2797 return ht; // local variable
2798
2799 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002800 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801 {
2802 ht = get_script_local_ht();
2803 if (ht != NULL)
2804 return ht;
2805 }
2806
2807 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002808 }
2809 *varname = name + 2;
2810 if (*name == 'g') // global variable
2811 return &globvarht;
2812 // There must be no ':' or '#' in the rest of the name, unless g: is used
2813 if (vim_strchr(name + 2, ':') != NULL
2814 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2815 return NULL;
2816 if (*name == 'b') // buffer variable
2817 return &curbuf->b_vars->dv_hashtab;
2818 if (*name == 'w') // window variable
2819 return &curwin->w_vars->dv_hashtab;
2820 if (*name == 't') // tab page variable
2821 return &curtab->tp_vars->dv_hashtab;
2822 if (*name == 'v') // v: variable
2823 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002824 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002825 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002827 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 if (*name == 'a') // a: function argument
2829 return get_funccal_args_ht();
2830 if (*name == 'l') // l: local function variable
2831 return get_funccal_local_ht();
2832 }
2833 if (*name == 's') // script variable
2834 {
2835 ht = get_script_local_ht();
2836 if (ht != NULL)
2837 return ht;
2838 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002839 return NULL;
2840}
2841
2842/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002843 * Get the string value of a (global/local) variable.
2844 * Note: see tv_get_string() for how long the pointer remains valid.
2845 * Returns NULL when it doesn't exist.
2846 */
2847 char_u *
2848get_var_value(char_u *name)
2849{
2850 dictitem_T *v;
2851
2852 v = find_var(name, NULL, FALSE);
2853 if (v == NULL)
2854 return NULL;
2855 return tv_get_string(&v->di_tv);
2856}
2857
2858/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002859 * Allocate a new hashtab for a sourced script. It will be used while
2860 * sourcing this script and when executing functions defined in the script.
2861 */
2862 void
2863new_script_vars(scid_T id)
2864{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002865 scriptvar_T *sv;
2866
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002867 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2868 if (sv == NULL)
2869 return;
2870 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002871 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002872}
2873
2874/*
2875 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2876 * point to it.
2877 */
2878 void
2879init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2880{
2881 hash_init(&dict->dv_hashtab);
2882 dict->dv_lock = 0;
2883 dict->dv_scope = scope;
2884 dict->dv_refcount = DO_NOT_FREE_CNT;
2885 dict->dv_copyID = 0;
2886 dict_var->di_tv.vval.v_dict = dict;
2887 dict_var->di_tv.v_type = VAR_DICT;
2888 dict_var->di_tv.v_lock = VAR_FIXED;
2889 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2890 dict_var->di_key[0] = NUL;
2891}
2892
2893/*
2894 * Unreference a dictionary initialized by init_var_dict().
2895 */
2896 void
2897unref_var_dict(dict_T *dict)
2898{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002899 // Now the dict needs to be freed if no one else is using it, go back to
2900 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002901 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2902 dict_unref(dict);
2903}
2904
2905/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002906 * Clean up a list of internal variables.
2907 * Frees all allocated variables and the value they contain.
2908 * Clears hashtab "ht", does not free it.
2909 */
2910 void
2911vars_clear(hashtab_T *ht)
2912{
2913 vars_clear_ext(ht, TRUE);
2914}
2915
2916/*
2917 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2918 */
2919 void
2920vars_clear_ext(hashtab_T *ht, int free_val)
2921{
2922 int todo;
2923 hashitem_T *hi;
2924 dictitem_T *v;
2925
2926 hash_lock(ht);
2927 todo = (int)ht->ht_used;
2928 for (hi = ht->ht_array; todo > 0; ++hi)
2929 {
2930 if (!HASHITEM_EMPTY(hi))
2931 {
2932 --todo;
2933
2934 // Free the variable. Don't remove it from the hashtab,
2935 // ht_array might change then. hash_clear() takes care of it
2936 // later.
2937 v = HI2DI(hi);
2938 if (free_val)
2939 clear_tv(&v->di_tv);
2940 if (v->di_flags & DI_FLAGS_ALLOC)
2941 vim_free(v);
2942 }
2943 }
2944 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02002945 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002946}
2947
2948/*
2949 * Delete a variable from hashtab "ht" at item "hi".
2950 * Clear the variable value and free the dictitem.
2951 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02002952 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002953delete_var(hashtab_T *ht, hashitem_T *hi)
2954{
2955 dictitem_T *di = HI2DI(hi);
2956
2957 hash_remove(ht, hi);
2958 clear_tv(&di->di_tv);
2959 vim_free(di);
2960}
2961
2962/*
2963 * List the value of one internal variable.
2964 */
2965 static void
2966list_one_var(dictitem_T *v, char *prefix, int *first)
2967{
2968 char_u *tofree;
2969 char_u *s;
2970 char_u numbuf[NUMBUFLEN];
2971
2972 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2973 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2974 s == NULL ? (char_u *)"" : s, first);
2975 vim_free(tofree);
2976}
2977
2978 static void
2979list_one_var_a(
2980 char *prefix,
2981 char_u *name,
2982 int type,
2983 char_u *string,
2984 int *first) // when TRUE clear rest of screen and set to FALSE
2985{
2986 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2987 msg_start();
2988 msg_puts(prefix);
2989 if (name != NULL) // "a:" vars don't have a name stored
2990 msg_puts((char *)name);
2991 msg_putchar(' ');
2992 msg_advance(22);
2993 if (type == VAR_NUMBER)
2994 msg_putchar('#');
2995 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2996 msg_putchar('*');
2997 else if (type == VAR_LIST)
2998 {
2999 msg_putchar('[');
3000 if (*string == '[')
3001 ++string;
3002 }
3003 else if (type == VAR_DICT)
3004 {
3005 msg_putchar('{');
3006 if (*string == '{')
3007 ++string;
3008 }
3009 else
3010 msg_putchar(' ');
3011
3012 msg_outtrans(string);
3013
3014 if (type == VAR_FUNC || type == VAR_PARTIAL)
3015 msg_puts("()");
3016 if (*first)
3017 {
3018 msg_clr_eos();
3019 *first = FALSE;
3020 }
3021}
3022
3023/*
3024 * Set variable "name" to value in "tv".
3025 * If the variable already exists, the value is updated.
3026 * Otherwise the variable is created.
3027 */
3028 void
3029set_var(
3030 char_u *name,
3031 typval_T *tv,
3032 int copy) // make copy of value in "tv"
3033{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003034 set_var_const(name, NULL, tv, copy, ASSIGN_NO_DECL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003035}
3036
3037/*
3038 * Set variable "name" to value in "tv".
3039 * If the variable already exists and "is_const" is FALSE the value is updated.
3040 * Otherwise the variable is created.
3041 */
3042 void
3043set_var_const(
3044 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003046 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003047 int copy, // make copy of value in "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003048 int flags) // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003049{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003050 typval_T *tv = tv_arg;
3051 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003053 char_u *varname;
3054 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003056 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003057
3058 ht = find_var_ht(name, &varname);
3059 if (ht == NULL || *varname == NUL)
3060 {
3061 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003062 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003063 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003064 is_script_local = ht == get_script_local_ht();
3065
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003066 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003067 && !is_script_local
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003068 && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003069 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003070 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003071 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003072 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003073 }
3074
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003076
3077 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 if (di == NULL)
3079 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003080
3081 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003082 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003083 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003084
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003085 if (need_convert_to_bool(type, tv))
3086 {
3087 // Destination is a bool and the value is not, but it can be converted.
3088 CLEAR_FIELD(bool_tv);
3089 bool_tv.v_type = VAR_BOOL;
3090 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3091 tv = &bool_tv;
3092 }
3093
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003095 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003097 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003098 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 {
3100 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003101 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 }
3103
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003104 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003106 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003107 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003108 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003109 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003110 }
3111
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003112 // check the type and adjust to bool if needed
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003113 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003114 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003116
Bram Moolenaara187c432020-09-16 21:08:28 +02003117 // Check in this order for backwards compatibility:
3118 // - Whether the variable is read-only
3119 // - Whether the variable value is locked
3120 // - Whether the variable is locked
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003121 if (var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02003122 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3123 || var_check_lock(di->di_flags, name, FALSE))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003124 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003125 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 else
3127 // can only redefine once
3128 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003129
3130 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003133 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003134 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003135 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003137 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003139 if (copy || tv->v_type != VAR_STRING)
3140 {
3141 char_u *val = tv_get_string(tv);
3142
3143 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003144 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 if (di->di_tv.vval.v_string == NULL)
3146 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003147 }
3148 else
3149 {
3150 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003152 tv->vval.v_string = NULL;
3153 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003154 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003155 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003157 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003159 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003161#ifdef FEAT_SEARCH_EXTRA
3162 else if (STRCMP(varname, "hlsearch") == 0)
3163 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003165 redraw_all_later(SOME_VALID);
3166 }
3167#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003168 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003169 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003171 {
3172 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003173 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003174 }
3175 }
3176
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003178 }
3179 else // add a new variable
3180 {
3181 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003182 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003183 {
3184 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003185 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003186 }
3187
3188 // Make sure the variable name is valid.
3189 if (!valid_varname(varname))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003190 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003191
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3193 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003194 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 STRCPY(di->di_key, varname);
3196 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003197 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003199 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003200 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003202 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 di->di_flags |= DI_FLAGS_LOCK;
3204
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003205 // A Vim9 script-local variable is also added to sn_all_vars and
3206 // sn_var_vals.
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003207 if (is_script_local && vim9script)
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003208 add_vim9_script_var(di, tv, type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003209 }
3210
3211 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003213 else
3214 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 di->di_tv = *tv;
3216 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003217 init_tv(tv);
3218 }
3219
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003220 // ":const var = val" locks the value
3221 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003222 // Like :lockvar! name: lock the value and what it contains, but only
3223 // if the reference count is up to one. That locks only literal
3224 // values.
3225 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003226 return;
3227failed:
3228 if (!copy)
3229 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003230}
3231
3232/*
3233 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3234 * Also give an error message.
3235 */
3236 int
3237var_check_ro(int flags, char_u *name, int use_gettext)
3238{
3239 if (flags & DI_FLAGS_RO)
3240 {
3241 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3242 return TRUE;
3243 }
3244 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3245 {
3246 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3247 return TRUE;
3248 }
3249 return FALSE;
3250}
3251
3252/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003253 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3254 * Also give an error message.
3255 */
3256 int
3257var_check_lock(int flags, char_u *name, int use_gettext)
3258{
3259 if (flags & DI_FLAGS_LOCK)
3260 {
3261 semsg(_(e_variable_is_locked_str),
3262 use_gettext ? (char_u *)_(name) : name);
3263 return TRUE;
3264 }
3265 return FALSE;
3266}
3267
3268/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003269 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3270 * Also give an error message.
3271 */
3272 int
3273var_check_fixed(int flags, char_u *name, int use_gettext)
3274{
3275 if (flags & DI_FLAGS_FIX)
3276 {
3277 semsg(_("E795: Cannot delete variable %s"),
3278 use_gettext ? (char_u *)_(name) : name);
3279 return TRUE;
3280 }
3281 return FALSE;
3282}
3283
3284/*
3285 * Check if a funcref is assigned to a valid variable name.
3286 * Return TRUE and give an error if not.
3287 */
3288 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003289var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003290 char_u *name, // points to start of variable name
3291 int new_var) // TRUE when creating the variable
3292{
3293 // Allow for w: b: s: and t:.
3294 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3295 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3296 ? name[2] : name[0]))
3297 {
3298 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3299 name);
3300 return TRUE;
3301 }
3302 // Don't allow hiding a function. When "v" is not NULL we might be
3303 // assigning another function to the same var, the type is checked
3304 // below.
3305 if (new_var && function_exists(name, FALSE))
3306 {
3307 semsg(_("E705: Variable name conflicts with existing function: %s"),
3308 name);
3309 return TRUE;
3310 }
3311 return FALSE;
3312}
3313
3314/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003315 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3316 * value. Also give an error message, using "name" or _("name") when
3317 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003318 */
3319 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003320value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003321{
3322 if (lock & VAR_LOCKED)
3323 {
3324 semsg(_("E741: Value is locked: %s"),
3325 name == NULL ? (char_u *)_("Unknown")
3326 : use_gettext ? (char_u *)_(name)
3327 : name);
3328 return TRUE;
3329 }
3330 if (lock & VAR_FIXED)
3331 {
3332 semsg(_("E742: Cannot change value of %s"),
3333 name == NULL ? (char_u *)_("Unknown")
3334 : use_gettext ? (char_u *)_(name)
3335 : name);
3336 return TRUE;
3337 }
3338 return FALSE;
3339}
3340
3341/*
3342 * Check if a variable name is valid.
3343 * Return FALSE and give an error if not.
3344 */
3345 int
3346valid_varname(char_u *varname)
3347{
3348 char_u *p;
3349
3350 for (p = varname; *p != NUL; ++p)
3351 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3352 && *p != AUTOLOAD_CHAR)
3353 {
3354 semsg(_(e_illvar), varname);
3355 return FALSE;
3356 }
3357 return TRUE;
3358}
3359
3360/*
3361 * getwinvar() and gettabwinvar()
3362 */
3363 static void
3364getwinvar(
3365 typval_T *argvars,
3366 typval_T *rettv,
3367 int off) // 1 for gettabwinvar()
3368{
3369 win_T *win;
3370 char_u *varname;
3371 dictitem_T *v;
3372 tabpage_T *tp = NULL;
3373 int done = FALSE;
3374 win_T *oldcurwin;
3375 tabpage_T *oldtabpage;
3376 int need_switch_win;
3377
3378 if (off == 1)
3379 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3380 else
3381 tp = curtab;
3382 win = find_win_by_nr(&argvars[off], tp);
3383 varname = tv_get_string_chk(&argvars[off + 1]);
3384 ++emsg_off;
3385
3386 rettv->v_type = VAR_STRING;
3387 rettv->vval.v_string = NULL;
3388
3389 if (win != NULL && varname != NULL)
3390 {
3391 // Set curwin to be our win, temporarily. Also set the tabpage,
3392 // otherwise the window is not valid. Only do this when needed,
3393 // autocommands get blocked.
3394 need_switch_win = !(tp == curtab && win == curwin);
3395 if (!need_switch_win
3396 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3397 {
3398 if (*varname == '&')
3399 {
3400 if (varname[1] == NUL)
3401 {
3402 // get all window-local options in a dict
3403 dict_T *opts = get_winbuf_options(FALSE);
3404
3405 if (opts != NULL)
3406 {
3407 rettv_dict_set(rettv, opts);
3408 done = TRUE;
3409 }
3410 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003411 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003412 // window-local-option
3413 done = TRUE;
3414 }
3415 else
3416 {
3417 // Look up the variable.
3418 // Let getwinvar({nr}, "") return the "w:" dictionary.
3419 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3420 varname, FALSE);
3421 if (v != NULL)
3422 {
3423 copy_tv(&v->di_tv, rettv);
3424 done = TRUE;
3425 }
3426 }
3427 }
3428
3429 if (need_switch_win)
3430 // restore previous notion of curwin
3431 restore_win(oldcurwin, oldtabpage, TRUE);
3432 }
3433
3434 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3435 // use the default return value
3436 copy_tv(&argvars[off + 2], rettv);
3437
3438 --emsg_off;
3439}
3440
3441/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003442 * Set option "varname" to the value of "varp" for the current buffer/window.
3443 */
3444 static void
3445set_option_from_tv(char_u *varname, typval_T *varp)
3446{
3447 long numval = 0;
3448 char_u *strval;
3449 char_u nbuf[NUMBUFLEN];
3450 int error = FALSE;
3451
3452 if (!in_vim9script() || varp->v_type != VAR_STRING)
3453 numval = (long)tv_get_number_chk(varp, &error);
3454 strval = tv_get_string_buf_chk(varp, nbuf);
3455 if (!error && strval != NULL)
3456 set_option_value(varname, numval, strval, OPT_LOCAL);
3457}
3458
3459/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003460 * "setwinvar()" and "settabwinvar()" functions
3461 */
3462 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003463setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003464{
3465 win_T *win;
3466 win_T *save_curwin;
3467 tabpage_T *save_curtab;
3468 int need_switch_win;
3469 char_u *varname, *winvarname;
3470 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003471 tabpage_T *tp = NULL;
3472
3473 if (check_secure())
3474 return;
3475
3476 if (off == 1)
3477 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3478 else
3479 tp = curtab;
3480 win = find_win_by_nr(&argvars[off], tp);
3481 varname = tv_get_string_chk(&argvars[off + 1]);
3482 varp = &argvars[off + 2];
3483
3484 if (win != NULL && varname != NULL && varp != NULL)
3485 {
3486 need_switch_win = !(tp == curtab && win == curwin);
3487 if (!need_switch_win
3488 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3489 {
3490 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003491 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003492 else
3493 {
3494 winvarname = alloc(STRLEN(varname) + 3);
3495 if (winvarname != NULL)
3496 {
3497 STRCPY(winvarname, "w:");
3498 STRCPY(winvarname + 2, varname);
3499 set_var(winvarname, varp, TRUE);
3500 vim_free(winvarname);
3501 }
3502 }
3503 }
3504 if (need_switch_win)
3505 restore_win(save_curwin, save_curtab, TRUE);
3506 }
3507}
3508
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003509/*
3510 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3511 * v:option_type, and v:option_command.
3512 */
3513 void
3514reset_v_option_vars(void)
3515{
3516 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3517 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3518 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3519 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3520 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3521 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3522}
3523
3524/*
3525 * Add an assert error to v:errors.
3526 */
3527 void
3528assert_error(garray_T *gap)
3529{
3530 struct vimvar *vp = &vimvars[VV_ERRORS];
3531
3532 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003533 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003534 set_vim_var_list(VV_ERRORS, list_alloc());
3535 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3536}
3537
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003538 int
3539var_exists(char_u *var)
3540{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003541 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003542 char_u *name;
3543 char_u *tofree;
3544 typval_T tv;
3545 int len = 0;
3546 int n = FALSE;
3547
3548 // get_name_len() takes care of expanding curly braces
3549 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003550 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003551 if (len > 0)
3552 {
3553 if (tofree != NULL)
3554 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003555 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003556 if (n)
3557 {
3558 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003559 arg = skipwhite(arg);
3560 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003561 if (n)
3562 clear_tv(&tv);
3563 }
3564 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003565 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003566 n = FALSE;
3567
3568 vim_free(tofree);
3569 return n;
3570}
3571
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003572static lval_T *redir_lval = NULL;
3573#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3574static garray_T redir_ga; // only valid when redir_lval is not NULL
3575static char_u *redir_endp = NULL;
3576static char_u *redir_varname = NULL;
3577
3578/*
3579 * Start recording command output to a variable
3580 * When "append" is TRUE append to an existing variable.
3581 * Returns OK if successfully completed the setup. FAIL otherwise.
3582 */
3583 int
3584var_redir_start(char_u *name, int append)
3585{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003586 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003587 typval_T tv;
3588
3589 // Catch a bad name early.
3590 if (!eval_isnamec1(*name))
3591 {
3592 emsg(_(e_invarg));
3593 return FAIL;
3594 }
3595
3596 // Make a copy of the name, it is used in redir_lval until redir ends.
3597 redir_varname = vim_strsave(name);
3598 if (redir_varname == NULL)
3599 return FAIL;
3600
3601 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3602 if (redir_lval == NULL)
3603 {
3604 var_redir_stop();
3605 return FAIL;
3606 }
3607
3608 // The output is stored in growarray "redir_ga" until redirection ends.
3609 ga_init2(&redir_ga, (int)sizeof(char), 500);
3610
3611 // Parse the variable name (can be a dict or list entry).
3612 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3613 FNE_CHECK_START);
3614 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3615 {
3616 clear_lval(redir_lval);
3617 if (redir_endp != NULL && *redir_endp != NUL)
3618 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003619 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003620 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003621 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003622 redir_endp = NULL; // don't store a value, only cleanup
3623 var_redir_stop();
3624 return FAIL;
3625 }
3626
3627 // check if we can write to the variable: set it to or append an empty
3628 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003629 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003630 tv.v_type = VAR_STRING;
3631 tv.vval.v_string = (char_u *)"";
3632 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003633 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3634 ASSIGN_NO_DECL, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003635 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003636 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3637 ASSIGN_NO_DECL, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003638 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003639 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003640 {
3641 redir_endp = NULL; // don't store a value, only cleanup
3642 var_redir_stop();
3643 return FAIL;
3644 }
3645
3646 return OK;
3647}
3648
3649/*
3650 * Append "value[value_len]" to the variable set by var_redir_start().
3651 * The actual appending is postponed until redirection ends, because the value
3652 * appended may in fact be the string we write to, changing it may cause freed
3653 * memory to be used:
3654 * :redir => foo
3655 * :let foo
3656 * :redir END
3657 */
3658 void
3659var_redir_str(char_u *value, int value_len)
3660{
3661 int len;
3662
3663 if (redir_lval == NULL)
3664 return;
3665
3666 if (value_len == -1)
3667 len = (int)STRLEN(value); // Append the entire string
3668 else
3669 len = value_len; // Append only "value_len" characters
3670
3671 if (ga_grow(&redir_ga, len) == OK)
3672 {
3673 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3674 redir_ga.ga_len += len;
3675 }
3676 else
3677 var_redir_stop();
3678}
3679
3680/*
3681 * Stop redirecting command output to a variable.
3682 * Frees the allocated memory.
3683 */
3684 void
3685var_redir_stop(void)
3686{
3687 typval_T tv;
3688
3689 if (EVALCMD_BUSY)
3690 {
3691 redir_lval = NULL;
3692 return;
3693 }
3694
3695 if (redir_lval != NULL)
3696 {
3697 // If there was no error: assign the text to the variable.
3698 if (redir_endp != NULL)
3699 {
3700 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3701 tv.v_type = VAR_STRING;
3702 tv.vval.v_string = redir_ga.ga_data;
3703 // Call get_lval() again, if it's inside a Dict or List it may
3704 // have changed.
3705 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3706 FALSE, FALSE, 0, FNE_CHECK_START);
3707 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003709 (char_u *)".");
3710 clear_lval(redir_lval);
3711 }
3712
3713 // free the collected output
3714 VIM_CLEAR(redir_ga.ga_data);
3715
3716 VIM_CLEAR(redir_lval);
3717 }
3718 VIM_CLEAR(redir_varname);
3719}
3720
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003721/*
3722 * "gettabvar()" function
3723 */
3724 void
3725f_gettabvar(typval_T *argvars, typval_T *rettv)
3726{
3727 win_T *oldcurwin;
3728 tabpage_T *tp, *oldtabpage;
3729 dictitem_T *v;
3730 char_u *varname;
3731 int done = FALSE;
3732
3733 rettv->v_type = VAR_STRING;
3734 rettv->vval.v_string = NULL;
3735
3736 varname = tv_get_string_chk(&argvars[1]);
3737 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3738 if (tp != NULL && varname != NULL)
3739 {
3740 // Set tp to be our tabpage, temporarily. Also set the window to the
3741 // first window in the tabpage, otherwise the window is not valid.
3742 if (switch_win(&oldcurwin, &oldtabpage,
3743 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3744 : tp->tp_firstwin, tp, TRUE) == OK)
3745 {
3746 // look up the variable
3747 // Let gettabvar({nr}, "") return the "t:" dictionary.
3748 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3749 if (v != NULL)
3750 {
3751 copy_tv(&v->di_tv, rettv);
3752 done = TRUE;
3753 }
3754 }
3755
3756 // restore previous notion of curwin
3757 restore_win(oldcurwin, oldtabpage, TRUE);
3758 }
3759
3760 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3761 copy_tv(&argvars[2], rettv);
3762}
3763
3764/*
3765 * "gettabwinvar()" function
3766 */
3767 void
3768f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3769{
3770 getwinvar(argvars, rettv, 1);
3771}
3772
3773/*
3774 * "getwinvar()" function
3775 */
3776 void
3777f_getwinvar(typval_T *argvars, typval_T *rettv)
3778{
3779 getwinvar(argvars, rettv, 0);
3780}
3781
3782/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003783 * "getbufvar()" function
3784 */
3785 void
3786f_getbufvar(typval_T *argvars, typval_T *rettv)
3787{
3788 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003789 char_u *varname;
3790 dictitem_T *v;
3791 int done = FALSE;
3792
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003793 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003794 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003795
3796 rettv->v_type = VAR_STRING;
3797 rettv->vval.v_string = NULL;
3798
3799 if (buf != NULL && varname != NULL)
3800 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003801 if (*varname == '&')
3802 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003803 buf_T *save_curbuf = curbuf;
3804
3805 // set curbuf to be our buf, temporarily
3806 curbuf = buf;
3807
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003808 if (varname[1] == NUL)
3809 {
3810 // get all buffer-local options in a dict
3811 dict_T *opts = get_winbuf_options(TRUE);
3812
3813 if (opts != NULL)
3814 {
3815 rettv_dict_set(rettv, opts);
3816 done = TRUE;
3817 }
3818 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003819 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003820 // buffer-local-option
3821 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003822
3823 // restore previous notion of curbuf
3824 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003825 }
3826 else
3827 {
3828 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003829 if (*varname == NUL)
3830 // Let getbufvar({nr}, "") return the "b:" dictionary.
3831 v = &buf->b_bufvar;
3832 else
3833 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3834 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003835 if (v != NULL)
3836 {
3837 copy_tv(&v->di_tv, rettv);
3838 done = TRUE;
3839 }
3840 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003841 }
3842
3843 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3844 // use the default value
3845 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003846}
3847
3848/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003849 * "settabvar()" function
3850 */
3851 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003852f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003853{
3854 tabpage_T *save_curtab;
3855 tabpage_T *tp;
3856 char_u *varname, *tabvarname;
3857 typval_T *varp;
3858
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003859 if (check_secure())
3860 return;
3861
3862 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3863 varname = tv_get_string_chk(&argvars[1]);
3864 varp = &argvars[2];
3865
3866 if (varname != NULL && varp != NULL && tp != NULL)
3867 {
3868 save_curtab = curtab;
3869 goto_tabpage_tp(tp, FALSE, FALSE);
3870
3871 tabvarname = alloc(STRLEN(varname) + 3);
3872 if (tabvarname != NULL)
3873 {
3874 STRCPY(tabvarname, "t:");
3875 STRCPY(tabvarname + 2, varname);
3876 set_var(tabvarname, varp, TRUE);
3877 vim_free(tabvarname);
3878 }
3879
3880 // Restore current tabpage
3881 if (valid_tabpage(save_curtab))
3882 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3883 }
3884}
3885
3886/*
3887 * "settabwinvar()" function
3888 */
3889 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003890f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003891{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003892 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003893}
3894
3895/*
3896 * "setwinvar()" function
3897 */
3898 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003899f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003900{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003901 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003902}
3903
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003904/*
3905 * "setbufvar()" function
3906 */
3907 void
3908f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3909{
3910 buf_T *buf;
3911 char_u *varname, *bufvarname;
3912 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003913
3914 if (check_secure())
3915 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003916 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003917 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003918 varp = &argvars[2];
3919
3920 if (buf != NULL && varname != NULL && varp != NULL)
3921 {
3922 if (*varname == '&')
3923 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003924 aco_save_T aco;
3925
3926 // set curbuf to be our buf, temporarily
3927 aucmd_prepbuf(&aco, buf);
3928
Bram Moolenaar191929b2020-08-19 21:20:49 +02003929 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003930
3931 // reset notion of buffer
3932 aucmd_restbuf(&aco);
3933 }
3934 else
3935 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003936 bufvarname = alloc(STRLEN(varname) + 3);
3937 if (bufvarname != NULL)
3938 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003939 buf_T *save_curbuf = curbuf;
3940
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003941 curbuf = buf;
3942 STRCPY(bufvarname, "b:");
3943 STRCPY(bufvarname + 2, varname);
3944 set_var(bufvarname, varp, TRUE);
3945 vim_free(bufvarname);
3946 curbuf = save_curbuf;
3947 }
3948 }
3949 }
3950}
3951
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003952/*
3953 * Get a callback from "arg". It can be a Funcref or a function name.
3954 * When "arg" is zero return an empty string.
3955 * "cb_name" is not allocated.
3956 * "cb_name" is set to NULL for an invalid argument.
3957 */
3958 callback_T
3959get_callback(typval_T *arg)
3960{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003961 callback_T res;
3962 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003963
3964 res.cb_free_name = FALSE;
3965 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3966 {
3967 res.cb_partial = arg->vval.v_partial;
3968 ++res.cb_partial->pt_refcount;
3969 res.cb_name = partial_name(res.cb_partial);
3970 }
3971 else
3972 {
3973 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003974 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3975 && isdigit(*arg->vval.v_string))
3976 r = FAIL;
3977 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003978 {
3979 // Note that we don't make a copy of the string.
3980 res.cb_name = arg->vval.v_string;
3981 func_ref(res.cb_name);
3982 }
3983 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003984 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003985 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003986 r = FAIL;
3987
3988 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003989 {
3990 emsg(_("E921: Invalid callback argument"));
3991 res.cb_name = NULL;
3992 }
3993 }
3994 return res;
3995}
3996
3997/*
3998 * Copy a callback into a typval_T.
3999 */
4000 void
4001put_callback(callback_T *cb, typval_T *tv)
4002{
4003 if (cb->cb_partial != NULL)
4004 {
4005 tv->v_type = VAR_PARTIAL;
4006 tv->vval.v_partial = cb->cb_partial;
4007 ++tv->vval.v_partial->pt_refcount;
4008 }
4009 else
4010 {
4011 tv->v_type = VAR_FUNC;
4012 tv->vval.v_string = vim_strsave(cb->cb_name);
4013 func_ref(cb->cb_name);
4014 }
4015}
4016
4017/*
4018 * Make a copy of "src" into "dest", allocating the function name if needed,
4019 * without incrementing the refcount.
4020 */
4021 void
4022set_callback(callback_T *dest, callback_T *src)
4023{
4024 if (src->cb_partial == NULL)
4025 {
4026 // just a function name, make a copy
4027 dest->cb_name = vim_strsave(src->cb_name);
4028 dest->cb_free_name = TRUE;
4029 }
4030 else
4031 {
4032 // cb_name is a pointer into cb_partial
4033 dest->cb_name = src->cb_name;
4034 dest->cb_free_name = FALSE;
4035 }
4036 dest->cb_partial = src->cb_partial;
4037}
4038
4039/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004040 * Copy callback from "src" to "dest", incrementing the refcounts.
4041 */
4042 void
4043copy_callback(callback_T *dest, callback_T *src)
4044{
4045 dest->cb_partial = src->cb_partial;
4046 if (dest->cb_partial != NULL)
4047 {
4048 dest->cb_name = src->cb_name;
4049 dest->cb_free_name = FALSE;
4050 ++dest->cb_partial->pt_refcount;
4051 }
4052 else
4053 {
4054 dest->cb_name = vim_strsave(src->cb_name);
4055 dest->cb_free_name = TRUE;
4056 func_ref(src->cb_name);
4057 }
4058}
4059
4060/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004061 * Unref/free "callback" returned by get_callback() or set_callback().
4062 */
4063 void
4064free_callback(callback_T *callback)
4065{
4066 if (callback->cb_partial != NULL)
4067 {
4068 partial_unref(callback->cb_partial);
4069 callback->cb_partial = NULL;
4070 }
4071 else if (callback->cb_name != NULL)
4072 func_unref(callback->cb_name);
4073 if (callback->cb_free_name)
4074 {
4075 vim_free(callback->cb_name);
4076 callback->cb_free_name = FALSE;
4077 }
4078 callback->cb_name = NULL;
4079}
4080
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004081#endif // FEAT_EVAL