blob: fde80d750d169ea364c352ad3e5c040f185e4d92 [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 Moolenaar9aed7292020-12-18 15:38:00 +01001666 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001667 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001668 return FAIL;
1669
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001670 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001671
1672 // can't :unlet a script variable in Vim9 script from a function
1673 if (ht == get_script_local_ht()
1674 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1675 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1676 == SCRIPT_VERSION_VIM9
1677 && check_vim9_unlet(name) == FAIL)
1678 return FAIL;
1679
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001680 if (ht != NULL && *varname != NUL)
1681 {
1682 d = get_current_funccal_dict(ht);
1683 if (d == NULL)
1684 {
1685 if (ht == &globvarht)
1686 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001687 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001688 d = &vimvardict;
1689 else
1690 {
1691 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1692 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1693 }
1694 if (d == NULL)
1695 {
1696 internal_error("do_unlet()");
1697 return FAIL;
1698 }
1699 }
1700 hi = hash_find(ht, varname);
1701 if (HASHITEM_EMPTY(hi))
1702 hi = find_hi_in_scoped_ht(name, &ht);
1703 if (hi != NULL && !HASHITEM_EMPTY(hi))
1704 {
1705 di = HI2DI(hi);
1706 if (var_check_fixed(di->di_flags, name, FALSE)
1707 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001708 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001709 return FAIL;
1710
1711 delete_var(ht, hi);
1712 return OK;
1713 }
1714 }
1715 if (forceit)
1716 return OK;
1717 semsg(_("E108: No such variable: \"%s\""), name);
1718 return FAIL;
1719}
1720
1721/*
1722 * Lock or unlock variable indicated by "lp".
1723 * "deep" is the levels to go (-1 for unlimited);
1724 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1725 */
1726 static int
1727do_lock_var(
1728 lval_T *lp,
1729 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001730 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001731 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001732 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001733{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001734 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001735 int ret = OK;
1736 int cc;
1737 dictitem_T *di;
1738
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001739 if (lp->ll_tv == NULL)
1740 {
1741 cc = *name_end;
1742 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001743 if (*lp->ll_name == '$')
1744 {
1745 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001746 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001747 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001748 else
1749 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001750 // Normal name or expanded name.
1751 di = find_var(lp->ll_name, NULL, TRUE);
1752 if (di == NULL)
1753 ret = FAIL;
1754 else if ((di->di_flags & DI_FLAGS_FIX)
1755 && di->di_tv.v_type != VAR_DICT
1756 && di->di_tv.v_type != VAR_LIST)
1757 {
1758 // For historic reasons this error is not given for a list or
1759 // dict. E.g., the b: dict could be locked/unlocked.
1760 semsg(_(e_lock_unlock), lp->ll_name);
1761 ret = FAIL;
1762 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001763 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001764 {
1765 if (lock)
1766 di->di_flags |= DI_FLAGS_LOCK;
1767 else
1768 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001769 if (deep != 0)
1770 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001771 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001772 }
1773 *name_end = cc;
1774 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001775 else if (deep == 0)
1776 {
1777 // nothing to do
1778 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001779 else if (lp->ll_range)
1780 {
1781 listitem_T *li = lp->ll_li;
1782
1783 // (un)lock a range of List items.
1784 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1785 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001786 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001787 li = li->li_next;
1788 ++lp->ll_n1;
1789 }
1790 }
1791 else if (lp->ll_list != NULL)
1792 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001793 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001794 else
1795 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001796 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001797
1798 return ret;
1799}
1800
1801/*
1802 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001803 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1804 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001805 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001806 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001807item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001808{
1809 static int recurse = 0;
1810 list_T *l;
1811 listitem_T *li;
1812 dict_T *d;
1813 blob_T *b;
1814 hashitem_T *hi;
1815 int todo;
1816
1817 if (recurse >= DICT_MAXNEST)
1818 {
1819 emsg(_("E743: variable nested too deep for (un)lock"));
1820 return;
1821 }
1822 if (deep == 0)
1823 return;
1824 ++recurse;
1825
1826 // lock/unlock the item itself
1827 if (lock)
1828 tv->v_lock |= VAR_LOCKED;
1829 else
1830 tv->v_lock &= ~VAR_LOCKED;
1831
1832 switch (tv->v_type)
1833 {
1834 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001835 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001836 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001837 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001839 case VAR_STRING:
1840 case VAR_FUNC:
1841 case VAR_PARTIAL:
1842 case VAR_FLOAT:
1843 case VAR_SPECIAL:
1844 case VAR_JOB:
1845 case VAR_CHANNEL:
1846 break;
1847
1848 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001849 if ((b = tv->vval.v_blob) != NULL
1850 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001851 {
1852 if (lock)
1853 b->bv_lock |= VAR_LOCKED;
1854 else
1855 b->bv_lock &= ~VAR_LOCKED;
1856 }
1857 break;
1858 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001859 if ((l = tv->vval.v_list) != NULL
1860 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001861 {
1862 if (lock)
1863 l->lv_lock |= VAR_LOCKED;
1864 else
1865 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001866 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001867 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001868 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001869 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001870 }
1871 break;
1872 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001873 if ((d = tv->vval.v_dict) != NULL
1874 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001875 {
1876 if (lock)
1877 d->dv_lock |= VAR_LOCKED;
1878 else
1879 d->dv_lock &= ~VAR_LOCKED;
1880 if (deep < 0 || deep > 1)
1881 {
1882 // recursive: lock/unlock the items the List contains
1883 todo = (int)d->dv_hashtab.ht_used;
1884 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1885 {
1886 if (!HASHITEM_EMPTY(hi))
1887 {
1888 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001889 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1890 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001891 }
1892 }
1893 }
1894 }
1895 }
1896 --recurse;
1897}
1898
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001899#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1900/*
1901 * Delete all "menutrans_" variables.
1902 */
1903 void
1904del_menutrans_vars(void)
1905{
1906 hashitem_T *hi;
1907 int todo;
1908
1909 hash_lock(&globvarht);
1910 todo = (int)globvarht.ht_used;
1911 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1912 {
1913 if (!HASHITEM_EMPTY(hi))
1914 {
1915 --todo;
1916 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1917 delete_var(&globvarht, hi);
1918 }
1919 }
1920 hash_unlock(&globvarht);
1921}
1922#endif
1923
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001924/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001925 * Local string buffer for the next two functions to store a variable name
1926 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1927 * get_user_var_name().
1928 */
1929
1930static char_u *varnamebuf = NULL;
1931static int varnamebuflen = 0;
1932
1933/*
1934 * Function to concatenate a prefix and a variable name.
1935 */
1936 static char_u *
1937cat_prefix_varname(int prefix, char_u *name)
1938{
1939 int len;
1940
1941 len = (int)STRLEN(name) + 3;
1942 if (len > varnamebuflen)
1943 {
1944 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001945 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001946 varnamebuf = alloc(len);
1947 if (varnamebuf == NULL)
1948 {
1949 varnamebuflen = 0;
1950 return NULL;
1951 }
1952 varnamebuflen = len;
1953 }
1954 *varnamebuf = prefix;
1955 varnamebuf[1] = ':';
1956 STRCPY(varnamebuf + 2, name);
1957 return varnamebuf;
1958}
1959
1960/*
1961 * Function given to ExpandGeneric() to obtain the list of user defined
1962 * (global/buffer/window/built-in) variable names.
1963 */
1964 char_u *
1965get_user_var_name(expand_T *xp, int idx)
1966{
1967 static long_u gdone;
1968 static long_u bdone;
1969 static long_u wdone;
1970 static long_u tdone;
1971 static int vidx;
1972 static hashitem_T *hi;
1973 hashtab_T *ht;
1974
1975 if (idx == 0)
1976 {
1977 gdone = bdone = wdone = vidx = 0;
1978 tdone = 0;
1979 }
1980
1981 // Global variables
1982 if (gdone < globvarht.ht_used)
1983 {
1984 if (gdone++ == 0)
1985 hi = globvarht.ht_array;
1986 else
1987 ++hi;
1988 while (HASHITEM_EMPTY(hi))
1989 ++hi;
1990 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1991 return cat_prefix_varname('g', hi->hi_key);
1992 return hi->hi_key;
1993 }
1994
1995 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01001996 ht =
1997#ifdef FEAT_CMDWIN
1998 // In cmdwin, the alternative buffer should be used.
1999 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2000 &prevwin->w_buffer->b_vars->dv_hashtab :
2001#endif
2002 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002003 if (bdone < ht->ht_used)
2004 {
2005 if (bdone++ == 0)
2006 hi = ht->ht_array;
2007 else
2008 ++hi;
2009 while (HASHITEM_EMPTY(hi))
2010 ++hi;
2011 return cat_prefix_varname('b', hi->hi_key);
2012 }
2013
2014 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002015 ht =
2016#ifdef FEAT_CMDWIN
2017 // In cmdwin, the alternative window should be used.
2018 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2019 &prevwin->w_vars->dv_hashtab :
2020#endif
2021 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002022 if (wdone < ht->ht_used)
2023 {
2024 if (wdone++ == 0)
2025 hi = ht->ht_array;
2026 else
2027 ++hi;
2028 while (HASHITEM_EMPTY(hi))
2029 ++hi;
2030 return cat_prefix_varname('w', hi->hi_key);
2031 }
2032
2033 // t: variables
2034 ht = &curtab->tp_vars->dv_hashtab;
2035 if (tdone < ht->ht_used)
2036 {
2037 if (tdone++ == 0)
2038 hi = ht->ht_array;
2039 else
2040 ++hi;
2041 while (HASHITEM_EMPTY(hi))
2042 ++hi;
2043 return cat_prefix_varname('t', hi->hi_key);
2044 }
2045
2046 // v: variables
2047 if (vidx < VV_LEN)
2048 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2049
2050 VIM_CLEAR(varnamebuf);
2051 varnamebuflen = 0;
2052 return NULL;
2053}
2054
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002055 char *
2056get_var_special_name(int nr)
2057{
2058 switch (nr)
2059 {
2060 case VVAL_FALSE: return "v:false";
2061 case VVAL_TRUE: return "v:true";
2062 case VVAL_NONE: return "v:none";
2063 case VVAL_NULL: return "v:null";
2064 }
2065 internal_error("get_var_special_name()");
2066 return "42";
2067}
2068
2069/*
2070 * Returns the global variable dictionary
2071 */
2072 dict_T *
2073get_globvar_dict(void)
2074{
2075 return &globvardict;
2076}
2077
2078/*
2079 * Returns the global variable hash table
2080 */
2081 hashtab_T *
2082get_globvar_ht(void)
2083{
2084 return &globvarht;
2085}
2086
2087/*
2088 * Returns the v: variable dictionary
2089 */
2090 dict_T *
2091get_vimvar_dict(void)
2092{
2093 return &vimvardict;
2094}
2095
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002096/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002097 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002098 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002099 */
2100 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002101find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002102{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002103 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2104 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002105
2106 if (di == NULL)
2107 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002108 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2110 return (int)(vv - vimvars);
2111}
2112
2113
2114/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002115 * Set type of v: variable to "type".
2116 */
2117 void
2118set_vim_var_type(int idx, vartype_T type)
2119{
2120 vimvars[idx].vv_type = type;
2121}
2122
2123/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002124 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002125 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002126 */
2127 void
2128set_vim_var_nr(int idx, varnumber_T val)
2129{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002130 vimvars[idx].vv_nr = val;
2131}
2132
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002133 char *
2134get_vim_var_name(int idx)
2135{
2136 return vimvars[idx].vv_name;
2137}
2138
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002139/*
2140 * Get typval_T v: variable value.
2141 */
2142 typval_T *
2143get_vim_var_tv(int idx)
2144{
2145 return &vimvars[idx].vv_tv;
2146}
2147
2148/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002149 * Set v: variable to "tv". Only accepts the same type.
2150 * Takes over the value of "tv".
2151 */
2152 int
2153set_vim_var_tv(int idx, typval_T *tv)
2154{
2155 if (vimvars[idx].vv_type != tv->v_type)
2156 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002157 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002158 clear_tv(tv);
2159 return FAIL;
2160 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002161 // VV_RO is also checked when compiling, but let's check here as well.
2162 if (vimvars[idx].vv_flags & VV_RO)
2163 {
2164 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2165 return FAIL;
2166 }
2167 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2168 {
2169 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2170 return FAIL;
2171 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002172 clear_tv(&vimvars[idx].vv_di.di_tv);
2173 vimvars[idx].vv_di.di_tv = *tv;
2174 return OK;
2175}
2176
2177/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002178 * Get number v: variable value.
2179 */
2180 varnumber_T
2181get_vim_var_nr(int idx)
2182{
2183 return vimvars[idx].vv_nr;
2184}
2185
2186/*
2187 * Get string v: variable value. Uses a static buffer, can only be used once.
2188 * If the String variable has never been set, return an empty string.
2189 * Never returns NULL;
2190 */
2191 char_u *
2192get_vim_var_str(int idx)
2193{
2194 return tv_get_string(&vimvars[idx].vv_tv);
2195}
2196
2197/*
2198 * Get List v: variable value. Caller must take care of reference count when
2199 * needed.
2200 */
2201 list_T *
2202get_vim_var_list(int idx)
2203{
2204 return vimvars[idx].vv_list;
2205}
2206
2207/*
2208 * Get Dict v: variable value. Caller must take care of reference count when
2209 * needed.
2210 */
2211 dict_T *
2212get_vim_var_dict(int idx)
2213{
2214 return vimvars[idx].vv_dict;
2215}
2216
2217/*
2218 * Set v:char to character "c".
2219 */
2220 void
2221set_vim_var_char(int c)
2222{
2223 char_u buf[MB_MAXBYTES + 1];
2224
2225 if (has_mbyte)
2226 buf[(*mb_char2bytes)(c, buf)] = NUL;
2227 else
2228 {
2229 buf[0] = c;
2230 buf[1] = NUL;
2231 }
2232 set_vim_var_string(VV_CHAR, buf, -1);
2233}
2234
2235/*
2236 * Set v:count to "count" and v:count1 to "count1".
2237 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2238 */
2239 void
2240set_vcount(
2241 long count,
2242 long count1,
2243 int set_prevcount)
2244{
2245 if (set_prevcount)
2246 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2247 vimvars[VV_COUNT].vv_nr = count;
2248 vimvars[VV_COUNT1].vv_nr = count1;
2249}
2250
2251/*
2252 * Save variables that might be changed as a side effect. Used when executing
2253 * a timer callback.
2254 */
2255 void
2256save_vimvars(vimvars_save_T *vvsave)
2257{
2258 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2259 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2260 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2261}
2262
2263/*
2264 * Restore variables saved by save_vimvars().
2265 */
2266 void
2267restore_vimvars(vimvars_save_T *vvsave)
2268{
2269 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2270 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2271 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2272}
2273
2274/*
2275 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2276 * value.
2277 */
2278 void
2279set_vim_var_string(
2280 int idx,
2281 char_u *val,
2282 int len) // length of "val" to use or -1 (whole string)
2283{
2284 clear_tv(&vimvars[idx].vv_di.di_tv);
2285 vimvars[idx].vv_type = VAR_STRING;
2286 if (val == NULL)
2287 vimvars[idx].vv_str = NULL;
2288 else if (len == -1)
2289 vimvars[idx].vv_str = vim_strsave(val);
2290 else
2291 vimvars[idx].vv_str = vim_strnsave(val, len);
2292}
2293
2294/*
2295 * Set List v: variable to "val".
2296 */
2297 void
2298set_vim_var_list(int idx, list_T *val)
2299{
2300 clear_tv(&vimvars[idx].vv_di.di_tv);
2301 vimvars[idx].vv_type = VAR_LIST;
2302 vimvars[idx].vv_list = val;
2303 if (val != NULL)
2304 ++val->lv_refcount;
2305}
2306
2307/*
2308 * Set Dictionary v: variable to "val".
2309 */
2310 void
2311set_vim_var_dict(int idx, dict_T *val)
2312{
2313 clear_tv(&vimvars[idx].vv_di.di_tv);
2314 vimvars[idx].vv_type = VAR_DICT;
2315 vimvars[idx].vv_dict = val;
2316 if (val != NULL)
2317 {
2318 ++val->dv_refcount;
2319 dict_set_items_ro(val);
2320 }
2321}
2322
2323/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002324 * Set the v:argv list.
2325 */
2326 void
2327set_argv_var(char **argv, int argc)
2328{
2329 list_T *l = list_alloc();
2330 int i;
2331
2332 if (l == NULL)
2333 getout(1);
2334 l->lv_lock = VAR_FIXED;
2335 for (i = 0; i < argc; ++i)
2336 {
2337 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2338 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002339 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002340 }
2341 set_vim_var_list(VV_ARGV, l);
2342}
2343
2344/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002345 * Reset v:register, taking the 'clipboard' setting into account.
2346 */
2347 void
2348reset_reg_var(void)
2349{
2350 int regname = 0;
2351
2352 // Adjust the register according to 'clipboard', so that when
2353 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2354#ifdef FEAT_CLIPBOARD
2355 adjust_clip_reg(&regname);
2356#endif
2357 set_reg_var(regname);
2358}
2359
2360/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002361 * Set v:register if needed.
2362 */
2363 void
2364set_reg_var(int c)
2365{
2366 char_u regname;
2367
2368 if (c == 0 || c == ' ')
2369 regname = '"';
2370 else
2371 regname = c;
2372 // Avoid free/alloc when the value is already right.
2373 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2374 set_vim_var_string(VV_REG, &regname, 1);
2375}
2376
2377/*
2378 * Get or set v:exception. If "oldval" == NULL, return the current value.
2379 * Otherwise, restore the value to "oldval" and return NULL.
2380 * Must always be called in pairs to save and restore v:exception! Does not
2381 * take care of memory allocations.
2382 */
2383 char_u *
2384v_exception(char_u *oldval)
2385{
2386 if (oldval == NULL)
2387 return vimvars[VV_EXCEPTION].vv_str;
2388
2389 vimvars[VV_EXCEPTION].vv_str = oldval;
2390 return NULL;
2391}
2392
2393/*
2394 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2395 * Otherwise, restore the value to "oldval" and return NULL.
2396 * Must always be called in pairs to save and restore v:throwpoint! Does not
2397 * take care of memory allocations.
2398 */
2399 char_u *
2400v_throwpoint(char_u *oldval)
2401{
2402 if (oldval == NULL)
2403 return vimvars[VV_THROWPOINT].vv_str;
2404
2405 vimvars[VV_THROWPOINT].vv_str = oldval;
2406 return NULL;
2407}
2408
2409/*
2410 * Set v:cmdarg.
2411 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2412 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2413 * Must always be called in pairs!
2414 */
2415 char_u *
2416set_cmdarg(exarg_T *eap, char_u *oldarg)
2417{
2418 char_u *oldval;
2419 char_u *newval;
2420 unsigned len;
2421
2422 oldval = vimvars[VV_CMDARG].vv_str;
2423 if (eap == NULL)
2424 {
2425 vim_free(oldval);
2426 vimvars[VV_CMDARG].vv_str = oldarg;
2427 return NULL;
2428 }
2429
2430 if (eap->force_bin == FORCE_BIN)
2431 len = 6;
2432 else if (eap->force_bin == FORCE_NOBIN)
2433 len = 8;
2434 else
2435 len = 0;
2436
2437 if (eap->read_edit)
2438 len += 7;
2439
2440 if (eap->force_ff != 0)
2441 len += 10; // " ++ff=unix"
2442 if (eap->force_enc != 0)
2443 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2444 if (eap->bad_char != 0)
2445 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2446
2447 newval = alloc(len + 1);
2448 if (newval == NULL)
2449 return NULL;
2450
2451 if (eap->force_bin == FORCE_BIN)
2452 sprintf((char *)newval, " ++bin");
2453 else if (eap->force_bin == FORCE_NOBIN)
2454 sprintf((char *)newval, " ++nobin");
2455 else
2456 *newval = NUL;
2457
2458 if (eap->read_edit)
2459 STRCAT(newval, " ++edit");
2460
2461 if (eap->force_ff != 0)
2462 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2463 eap->force_ff == 'u' ? "unix"
2464 : eap->force_ff == 'd' ? "dos"
2465 : "mac");
2466 if (eap->force_enc != 0)
2467 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2468 eap->cmd + eap->force_enc);
2469 if (eap->bad_char == BAD_KEEP)
2470 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2471 else if (eap->bad_char == BAD_DROP)
2472 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2473 else if (eap->bad_char != 0)
2474 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2475 vimvars[VV_CMDARG].vv_str = newval;
2476 return oldval;
2477}
2478
2479/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002480 * Get the value of internal variable "name".
2481 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2482 */
2483 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002484eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002485 char_u *name,
2486 int len, // length of "name"
2487 typval_T *rettv, // NULL when only checking existence
2488 dictitem_T **dip, // non-NULL when typval's dict item is needed
2489 int verbose, // may give error message
2490 int no_autoload) // do not use script autoloading
2491{
2492 int ret = OK;
2493 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002494 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002495 dictitem_T *v;
2496 int cc;
2497
2498 // truncate the name, so that we can use strcmp()
2499 cc = name[len];
2500 name[len] = NUL;
2501
2502 // Check for user-defined variables.
2503 v = find_var(name, NULL, no_autoload);
2504 if (v != NULL)
2505 {
2506 tv = &v->di_tv;
2507 if (dip != NULL)
2508 *dip = v;
2509 }
2510
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002511 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002513 imported_T *import;
2514 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2515
2516 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517
2518 // imported variable from another script
2519 if (import != NULL)
2520 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002521 if (import->imp_funcname != NULL)
2522 {
2523 foundFunc = TRUE;
2524 if (rettv != NULL)
2525 {
2526 rettv->v_type = VAR_FUNC;
2527 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2528 }
2529 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002530 else if (import->imp_all)
2531 {
2532 emsg("Sorry, 'import * as X' not implemented yet");
2533 ret = FAIL;
2534 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002535 else
2536 {
2537 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002538 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002540 tv = sv->sv_tv;
2541 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002543 else if (in_vim9script())
2544 {
2545 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2546
2547 if (ufunc != NULL)
2548 {
2549 foundFunc = TRUE;
2550 if (rettv != NULL)
2551 {
2552 rettv->v_type = VAR_FUNC;
2553 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2554 }
2555 }
2556 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557 }
2558
Bram Moolenaarc620c052020-07-08 15:16:19 +02002559 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002560 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002561 if (tv == NULL)
2562 {
2563 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002564 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002565 ret = FAIL;
2566 }
2567 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002568 {
2569 // If a list or dict variable wasn't initialized, do it now.
2570 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2571 {
2572 tv->vval.v_dict = dict_alloc();
2573 if (tv->vval.v_dict != NULL)
2574 ++tv->vval.v_dict->dv_refcount;
2575 }
2576 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2577 {
2578 tv->vval.v_list = list_alloc();
2579 if (tv->vval.v_list != NULL)
2580 ++tv->vval.v_list->lv_refcount;
2581 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002582 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002583 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002584 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002585
2586 name[len] = cc;
2587
2588 return ret;
2589}
2590
2591/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002592 * Check if variable "name[len]" is a local variable or an argument.
2593 * If so, "*eval_lavars_used" is set to TRUE.
2594 */
2595 void
2596check_vars(char_u *name, int len)
2597{
2598 int cc;
2599 char_u *varname;
2600 hashtab_T *ht;
2601
2602 if (eval_lavars_used == NULL)
2603 return;
2604
2605 // truncate the name, so that we can use strcmp()
2606 cc = name[len];
2607 name[len] = NUL;
2608
2609 ht = find_var_ht(name, &varname);
2610 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2611 {
2612 if (find_var(name, NULL, TRUE) != NULL)
2613 *eval_lavars_used = TRUE;
2614 }
2615
2616 name[len] = cc;
2617}
2618
2619/*
2620 * Find variable "name" in the list of variables.
2621 * Return a pointer to it if found, NULL if not found.
2622 * Careful: "a:0" variables don't have a name.
2623 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2624 * hashtab_T used.
2625 */
2626 dictitem_T *
2627find_var(char_u *name, hashtab_T **htp, int no_autoload)
2628{
2629 char_u *varname;
2630 hashtab_T *ht;
2631 dictitem_T *ret = NULL;
2632
2633 ht = find_var_ht(name, &varname);
2634 if (htp != NULL)
2635 *htp = ht;
2636 if (ht == NULL)
2637 return NULL;
2638 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2639 if (ret != NULL)
2640 return ret;
2641
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002642 // Search in parent scope for lambda
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002643 ret = find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2644 if (ret != NULL)
2645 return ret;
2646
2647 // in Vim9 script items without a scope can be script-local
2648 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2649 {
2650 ht = get_script_local_ht();
2651 if (ht != NULL)
2652 {
2653 ret = find_var_in_ht(ht, *name, varname,
2654 no_autoload || htp != NULL);
2655 if (ret != NULL)
2656 {
2657 if (htp != NULL)
2658 *htp = ht;
2659 return ret;
2660 }
2661 }
2662 }
2663
2664 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002665}
2666
2667/*
2668 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002669 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002670 * Returns NULL if not found.
2671 */
2672 dictitem_T *
2673find_var_in_ht(
2674 hashtab_T *ht,
2675 int htname,
2676 char_u *varname,
2677 int no_autoload)
2678{
2679 hashitem_T *hi;
2680
2681 if (*varname == NUL)
2682 {
2683 // Must be something like "s:", otherwise "ht" would be NULL.
2684 switch (htname)
2685 {
2686 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2687 case 'g': return &globvars_var;
2688 case 'v': return &vimvars_var;
2689 case 'b': return &curbuf->b_bufvar;
2690 case 'w': return &curwin->w_winvar;
2691 case 't': return &curtab->tp_winvar;
2692 case 'l': return get_funccal_local_var();
2693 case 'a': return get_funccal_args_var();
2694 }
2695 return NULL;
2696 }
2697
2698 hi = hash_find(ht, varname);
2699 if (HASHITEM_EMPTY(hi))
2700 {
2701 // For global variables we may try auto-loading the script. If it
2702 // worked find the variable again. Don't auto-load a script if it was
2703 // loaded already, otherwise it would be loaded every time when
2704 // checking if a function name is a Funcref variable.
2705 if (ht == &globvarht && !no_autoload)
2706 {
2707 // Note: script_autoload() may make "hi" invalid. It must either
2708 // be obtained again or not used.
2709 if (!script_autoload(varname, FALSE) || aborting())
2710 return NULL;
2711 hi = hash_find(ht, varname);
2712 }
2713 if (HASHITEM_EMPTY(hi))
2714 return NULL;
2715 }
2716 return HI2DI(hi);
2717}
2718
2719/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720 * Get the script-local hashtab. NULL if not in a script context.
2721 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002722 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002723get_script_local_ht(void)
2724{
2725 scid_T sid = current_sctx.sc_sid;
2726
Bram Moolenaare3d46852020-08-29 13:39:17 +02002727 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 return &SCRIPT_VARS(sid);
2729 return NULL;
2730}
2731
2732/*
2733 * Look for "name[len]" in script-local variables.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002734 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002735 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01002736 int
2737lookup_scriptvar(
2738 char_u *name,
2739 size_t len,
2740 void *lvar UNUSED,
2741 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742{
2743 hashtab_T *ht = get_script_local_ht();
2744 char_u buffer[30];
2745 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01002746 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002747 hashitem_T *hi;
2748
2749 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002750 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 if (len < sizeof(buffer) - 1)
2752 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002753 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002754 vim_strncpy(buffer, name, len);
2755 p = buffer;
2756 }
2757 else
2758 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002759 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002761 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 }
2763
2764 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002765 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766
2767 // if not script-local, then perhaps imported
Bram Moolenaar709664c2020-12-12 14:33:41 +01002768 if (res == FAIL && find_imported(p, 0, NULL) != NULL)
2769 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770
2771 if (p != buffer)
2772 vim_free(p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002773 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774}
2775
2776/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002777 * Find the hashtab used for a variable name.
2778 * Return NULL if the name is not valid.
2779 * Set "varname" to the start of name without ':'.
2780 */
2781 hashtab_T *
2782find_var_ht(char_u *name, char_u **varname)
2783{
2784 hashitem_T *hi;
2785 hashtab_T *ht;
2786
2787 if (name[0] == NUL)
2788 return NULL;
2789 if (name[1] != ':')
2790 {
2791 // The name must not start with a colon or #.
2792 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2793 return NULL;
2794 *varname = name;
2795
2796 // "version" is "v:version" in all scopes if scriptversion < 3.
2797 // Same for a few other variables marked with VV_COMPAT.
2798 if (current_sctx.sc_version < 3)
2799 {
2800 hi = hash_find(&compat_hashtab, name);
2801 if (!HASHITEM_EMPTY(hi))
2802 return &compat_hashtab;
2803 }
2804
2805 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 if (ht != NULL)
2807 return ht; // local variable
2808
2809 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002810 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 {
2812 ht = get_script_local_ht();
2813 if (ht != NULL)
2814 return ht;
2815 }
2816
2817 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002818 }
2819 *varname = name + 2;
2820 if (*name == 'g') // global variable
2821 return &globvarht;
2822 // There must be no ':' or '#' in the rest of the name, unless g: is used
2823 if (vim_strchr(name + 2, ':') != NULL
2824 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2825 return NULL;
2826 if (*name == 'b') // buffer variable
2827 return &curbuf->b_vars->dv_hashtab;
2828 if (*name == 'w') // window variable
2829 return &curwin->w_vars->dv_hashtab;
2830 if (*name == 't') // tab page variable
2831 return &curtab->tp_vars->dv_hashtab;
2832 if (*name == 'v') // v: variable
2833 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002834 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002835 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002837 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 if (*name == 'a') // a: function argument
2839 return get_funccal_args_ht();
2840 if (*name == 'l') // l: local function variable
2841 return get_funccal_local_ht();
2842 }
2843 if (*name == 's') // script variable
2844 {
2845 ht = get_script_local_ht();
2846 if (ht != NULL)
2847 return ht;
2848 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002849 return NULL;
2850}
2851
2852/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002853 * Get the string value of a (global/local) variable.
2854 * Note: see tv_get_string() for how long the pointer remains valid.
2855 * Returns NULL when it doesn't exist.
2856 */
2857 char_u *
2858get_var_value(char_u *name)
2859{
2860 dictitem_T *v;
2861
2862 v = find_var(name, NULL, FALSE);
2863 if (v == NULL)
2864 return NULL;
2865 return tv_get_string(&v->di_tv);
2866}
2867
2868/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002869 * Allocate a new hashtab for a sourced script. It will be used while
2870 * sourcing this script and when executing functions defined in the script.
2871 */
2872 void
2873new_script_vars(scid_T id)
2874{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002875 scriptvar_T *sv;
2876
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002877 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2878 if (sv == NULL)
2879 return;
2880 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002881 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002882}
2883
2884/*
2885 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2886 * point to it.
2887 */
2888 void
2889init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2890{
2891 hash_init(&dict->dv_hashtab);
2892 dict->dv_lock = 0;
2893 dict->dv_scope = scope;
2894 dict->dv_refcount = DO_NOT_FREE_CNT;
2895 dict->dv_copyID = 0;
2896 dict_var->di_tv.vval.v_dict = dict;
2897 dict_var->di_tv.v_type = VAR_DICT;
2898 dict_var->di_tv.v_lock = VAR_FIXED;
2899 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2900 dict_var->di_key[0] = NUL;
2901}
2902
2903/*
2904 * Unreference a dictionary initialized by init_var_dict().
2905 */
2906 void
2907unref_var_dict(dict_T *dict)
2908{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002909 // Now the dict needs to be freed if no one else is using it, go back to
2910 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002911 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2912 dict_unref(dict);
2913}
2914
2915/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002916 * Clean up a list of internal variables.
2917 * Frees all allocated variables and the value they contain.
2918 * Clears hashtab "ht", does not free it.
2919 */
2920 void
2921vars_clear(hashtab_T *ht)
2922{
2923 vars_clear_ext(ht, TRUE);
2924}
2925
2926/*
2927 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2928 */
2929 void
2930vars_clear_ext(hashtab_T *ht, int free_val)
2931{
2932 int todo;
2933 hashitem_T *hi;
2934 dictitem_T *v;
2935
2936 hash_lock(ht);
2937 todo = (int)ht->ht_used;
2938 for (hi = ht->ht_array; todo > 0; ++hi)
2939 {
2940 if (!HASHITEM_EMPTY(hi))
2941 {
2942 --todo;
2943
2944 // Free the variable. Don't remove it from the hashtab,
2945 // ht_array might change then. hash_clear() takes care of it
2946 // later.
2947 v = HI2DI(hi);
2948 if (free_val)
2949 clear_tv(&v->di_tv);
2950 if (v->di_flags & DI_FLAGS_ALLOC)
2951 vim_free(v);
2952 }
2953 }
2954 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02002955 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002956}
2957
2958/*
2959 * Delete a variable from hashtab "ht" at item "hi".
2960 * Clear the variable value and free the dictitem.
2961 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02002962 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002963delete_var(hashtab_T *ht, hashitem_T *hi)
2964{
2965 dictitem_T *di = HI2DI(hi);
2966
2967 hash_remove(ht, hi);
2968 clear_tv(&di->di_tv);
2969 vim_free(di);
2970}
2971
2972/*
2973 * List the value of one internal variable.
2974 */
2975 static void
2976list_one_var(dictitem_T *v, char *prefix, int *first)
2977{
2978 char_u *tofree;
2979 char_u *s;
2980 char_u numbuf[NUMBUFLEN];
2981
2982 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2983 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2984 s == NULL ? (char_u *)"" : s, first);
2985 vim_free(tofree);
2986}
2987
2988 static void
2989list_one_var_a(
2990 char *prefix,
2991 char_u *name,
2992 int type,
2993 char_u *string,
2994 int *first) // when TRUE clear rest of screen and set to FALSE
2995{
2996 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2997 msg_start();
2998 msg_puts(prefix);
2999 if (name != NULL) // "a:" vars don't have a name stored
3000 msg_puts((char *)name);
3001 msg_putchar(' ');
3002 msg_advance(22);
3003 if (type == VAR_NUMBER)
3004 msg_putchar('#');
3005 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3006 msg_putchar('*');
3007 else if (type == VAR_LIST)
3008 {
3009 msg_putchar('[');
3010 if (*string == '[')
3011 ++string;
3012 }
3013 else if (type == VAR_DICT)
3014 {
3015 msg_putchar('{');
3016 if (*string == '{')
3017 ++string;
3018 }
3019 else
3020 msg_putchar(' ');
3021
3022 msg_outtrans(string);
3023
3024 if (type == VAR_FUNC || type == VAR_PARTIAL)
3025 msg_puts("()");
3026 if (*first)
3027 {
3028 msg_clr_eos();
3029 *first = FALSE;
3030 }
3031}
3032
3033/*
3034 * Set variable "name" to value in "tv".
3035 * If the variable already exists, the value is updated.
3036 * Otherwise the variable is created.
3037 */
3038 void
3039set_var(
3040 char_u *name,
3041 typval_T *tv,
3042 int copy) // make copy of value in "tv"
3043{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003044 set_var_const(name, NULL, tv, copy, ASSIGN_NO_DECL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003045}
3046
3047/*
3048 * Set variable "name" to value in "tv".
3049 * If the variable already exists and "is_const" is FALSE the value is updated.
3050 * Otherwise the variable is created.
3051 */
3052 void
3053set_var_const(
3054 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003056 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003057 int copy, // make copy of value in "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003058 int flags) // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003059{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003060 typval_T *tv = tv_arg;
3061 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003063 char_u *varname;
3064 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003066 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003067
3068 ht = find_var_ht(name, &varname);
3069 if (ht == NULL || *varname == NUL)
3070 {
3071 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003072 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003073 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003074 is_script_local = ht == get_script_local_ht();
3075
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003076 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003077 && !is_script_local
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003078 && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003079 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003080 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003081 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003082 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003083 }
3084
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003086
3087 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 if (di == NULL)
3089 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003090
3091 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003092 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003093 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003094
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003095 if (need_convert_to_bool(type, tv))
3096 {
3097 // Destination is a bool and the value is not, but it can be converted.
3098 CLEAR_FIELD(bool_tv);
3099 bool_tv.v_type = VAR_BOOL;
3100 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3101 tv = &bool_tv;
3102 }
3103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003105 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003107 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003108 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 {
3110 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003111 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 }
3113
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003114 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003116 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003117 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003118 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003119 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003120 }
3121
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003122 // check the type and adjust to bool if needed
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003123 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003124 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003126
Bram Moolenaara187c432020-09-16 21:08:28 +02003127 // Check in this order for backwards compatibility:
3128 // - Whether the variable is read-only
3129 // - Whether the variable value is locked
3130 // - Whether the variable is locked
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003131 if (var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02003132 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3133 || var_check_lock(di->di_flags, name, FALSE))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003134 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003135 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 else
3137 // can only redefine once
3138 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003139
3140 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003143 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003144 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003145 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003147 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003149 if (copy || tv->v_type != VAR_STRING)
3150 {
3151 char_u *val = tv_get_string(tv);
3152
3153 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003154 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 if (di->di_tv.vval.v_string == NULL)
3156 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003157 }
3158 else
3159 {
3160 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003162 tv->vval.v_string = NULL;
3163 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003164 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003165 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003167 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003169 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003171#ifdef FEAT_SEARCH_EXTRA
3172 else if (STRCMP(varname, "hlsearch") == 0)
3173 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003175 redraw_all_later(SOME_VALID);
3176 }
3177#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003178 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003179 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003181 {
3182 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003183 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003184 }
3185 }
3186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003188 }
3189 else // add a new variable
3190 {
3191 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003192 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003193 {
3194 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003195 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003196 }
3197
3198 // Make sure the variable name is valid.
3199 if (!valid_varname(varname))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003200 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003201
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3203 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003204 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 STRCPY(di->di_key, varname);
3206 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003207 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003209 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003210 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003212 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 di->di_flags |= DI_FLAGS_LOCK;
3214
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003215 // A Vim9 script-local variable is also added to sn_all_vars and
3216 // sn_var_vals.
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003217 if (is_script_local && vim9script)
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003218 add_vim9_script_var(di, tv, type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003219 }
3220
3221 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003223 else
3224 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 di->di_tv = *tv;
3226 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003227 init_tv(tv);
3228 }
3229
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003230 // ":const var = val" locks the value
3231 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003232 // Like :lockvar! name: lock the value and what it contains, but only
3233 // if the reference count is up to one. That locks only literal
3234 // values.
3235 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003236 return;
3237failed:
3238 if (!copy)
3239 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003240}
3241
3242/*
3243 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3244 * Also give an error message.
3245 */
3246 int
3247var_check_ro(int flags, char_u *name, int use_gettext)
3248{
3249 if (flags & DI_FLAGS_RO)
3250 {
3251 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3252 return TRUE;
3253 }
3254 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3255 {
3256 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3257 return TRUE;
3258 }
3259 return FALSE;
3260}
3261
3262/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003263 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3264 * Also give an error message.
3265 */
3266 int
3267var_check_lock(int flags, char_u *name, int use_gettext)
3268{
3269 if (flags & DI_FLAGS_LOCK)
3270 {
3271 semsg(_(e_variable_is_locked_str),
3272 use_gettext ? (char_u *)_(name) : name);
3273 return TRUE;
3274 }
3275 return FALSE;
3276}
3277
3278/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003279 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3280 * Also give an error message.
3281 */
3282 int
3283var_check_fixed(int flags, char_u *name, int use_gettext)
3284{
3285 if (flags & DI_FLAGS_FIX)
3286 {
3287 semsg(_("E795: Cannot delete variable %s"),
3288 use_gettext ? (char_u *)_(name) : name);
3289 return TRUE;
3290 }
3291 return FALSE;
3292}
3293
3294/*
3295 * Check if a funcref is assigned to a valid variable name.
3296 * Return TRUE and give an error if not.
3297 */
3298 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003299var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003300 char_u *name, // points to start of variable name
3301 int new_var) // TRUE when creating the variable
3302{
3303 // Allow for w: b: s: and t:.
3304 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3305 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3306 ? name[2] : name[0]))
3307 {
3308 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3309 name);
3310 return TRUE;
3311 }
3312 // Don't allow hiding a function. When "v" is not NULL we might be
3313 // assigning another function to the same var, the type is checked
3314 // below.
3315 if (new_var && function_exists(name, FALSE))
3316 {
3317 semsg(_("E705: Variable name conflicts with existing function: %s"),
3318 name);
3319 return TRUE;
3320 }
3321 return FALSE;
3322}
3323
3324/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003325 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3326 * value. Also give an error message, using "name" or _("name") when
3327 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003328 */
3329 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003330value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003331{
3332 if (lock & VAR_LOCKED)
3333 {
3334 semsg(_("E741: Value is locked: %s"),
3335 name == NULL ? (char_u *)_("Unknown")
3336 : use_gettext ? (char_u *)_(name)
3337 : name);
3338 return TRUE;
3339 }
3340 if (lock & VAR_FIXED)
3341 {
3342 semsg(_("E742: Cannot change value of %s"),
3343 name == NULL ? (char_u *)_("Unknown")
3344 : use_gettext ? (char_u *)_(name)
3345 : name);
3346 return TRUE;
3347 }
3348 return FALSE;
3349}
3350
3351/*
3352 * Check if a variable name is valid.
3353 * Return FALSE and give an error if not.
3354 */
3355 int
3356valid_varname(char_u *varname)
3357{
3358 char_u *p;
3359
3360 for (p = varname; *p != NUL; ++p)
3361 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3362 && *p != AUTOLOAD_CHAR)
3363 {
3364 semsg(_(e_illvar), varname);
3365 return FALSE;
3366 }
3367 return TRUE;
3368}
3369
3370/*
3371 * getwinvar() and gettabwinvar()
3372 */
3373 static void
3374getwinvar(
3375 typval_T *argvars,
3376 typval_T *rettv,
3377 int off) // 1 for gettabwinvar()
3378{
3379 win_T *win;
3380 char_u *varname;
3381 dictitem_T *v;
3382 tabpage_T *tp = NULL;
3383 int done = FALSE;
3384 win_T *oldcurwin;
3385 tabpage_T *oldtabpage;
3386 int need_switch_win;
3387
3388 if (off == 1)
3389 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3390 else
3391 tp = curtab;
3392 win = find_win_by_nr(&argvars[off], tp);
3393 varname = tv_get_string_chk(&argvars[off + 1]);
3394 ++emsg_off;
3395
3396 rettv->v_type = VAR_STRING;
3397 rettv->vval.v_string = NULL;
3398
3399 if (win != NULL && varname != NULL)
3400 {
3401 // Set curwin to be our win, temporarily. Also set the tabpage,
3402 // otherwise the window is not valid. Only do this when needed,
3403 // autocommands get blocked.
3404 need_switch_win = !(tp == curtab && win == curwin);
3405 if (!need_switch_win
3406 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3407 {
3408 if (*varname == '&')
3409 {
3410 if (varname[1] == NUL)
3411 {
3412 // get all window-local options in a dict
3413 dict_T *opts = get_winbuf_options(FALSE);
3414
3415 if (opts != NULL)
3416 {
3417 rettv_dict_set(rettv, opts);
3418 done = TRUE;
3419 }
3420 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003421 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003422 // window-local-option
3423 done = TRUE;
3424 }
3425 else
3426 {
3427 // Look up the variable.
3428 // Let getwinvar({nr}, "") return the "w:" dictionary.
3429 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3430 varname, FALSE);
3431 if (v != NULL)
3432 {
3433 copy_tv(&v->di_tv, rettv);
3434 done = TRUE;
3435 }
3436 }
3437 }
3438
3439 if (need_switch_win)
3440 // restore previous notion of curwin
3441 restore_win(oldcurwin, oldtabpage, TRUE);
3442 }
3443
3444 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3445 // use the default return value
3446 copy_tv(&argvars[off + 2], rettv);
3447
3448 --emsg_off;
3449}
3450
3451/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003452 * Set option "varname" to the value of "varp" for the current buffer/window.
3453 */
3454 static void
3455set_option_from_tv(char_u *varname, typval_T *varp)
3456{
3457 long numval = 0;
3458 char_u *strval;
3459 char_u nbuf[NUMBUFLEN];
3460 int error = FALSE;
3461
3462 if (!in_vim9script() || varp->v_type != VAR_STRING)
3463 numval = (long)tv_get_number_chk(varp, &error);
3464 strval = tv_get_string_buf_chk(varp, nbuf);
3465 if (!error && strval != NULL)
3466 set_option_value(varname, numval, strval, OPT_LOCAL);
3467}
3468
3469/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003470 * "setwinvar()" and "settabwinvar()" functions
3471 */
3472 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003473setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003474{
3475 win_T *win;
3476 win_T *save_curwin;
3477 tabpage_T *save_curtab;
3478 int need_switch_win;
3479 char_u *varname, *winvarname;
3480 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003481 tabpage_T *tp = NULL;
3482
3483 if (check_secure())
3484 return;
3485
3486 if (off == 1)
3487 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3488 else
3489 tp = curtab;
3490 win = find_win_by_nr(&argvars[off], tp);
3491 varname = tv_get_string_chk(&argvars[off + 1]);
3492 varp = &argvars[off + 2];
3493
3494 if (win != NULL && varname != NULL && varp != NULL)
3495 {
3496 need_switch_win = !(tp == curtab && win == curwin);
3497 if (!need_switch_win
3498 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3499 {
3500 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003501 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003502 else
3503 {
3504 winvarname = alloc(STRLEN(varname) + 3);
3505 if (winvarname != NULL)
3506 {
3507 STRCPY(winvarname, "w:");
3508 STRCPY(winvarname + 2, varname);
3509 set_var(winvarname, varp, TRUE);
3510 vim_free(winvarname);
3511 }
3512 }
3513 }
3514 if (need_switch_win)
3515 restore_win(save_curwin, save_curtab, TRUE);
3516 }
3517}
3518
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003519/*
3520 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3521 * v:option_type, and v:option_command.
3522 */
3523 void
3524reset_v_option_vars(void)
3525{
3526 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3527 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3528 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3529 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3530 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3531 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3532}
3533
3534/*
3535 * Add an assert error to v:errors.
3536 */
3537 void
3538assert_error(garray_T *gap)
3539{
3540 struct vimvar *vp = &vimvars[VV_ERRORS];
3541
3542 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003543 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003544 set_vim_var_list(VV_ERRORS, list_alloc());
3545 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3546}
3547
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003548 int
3549var_exists(char_u *var)
3550{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003551 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003552 char_u *name;
3553 char_u *tofree;
3554 typval_T tv;
3555 int len = 0;
3556 int n = FALSE;
3557
3558 // get_name_len() takes care of expanding curly braces
3559 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003560 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003561 if (len > 0)
3562 {
3563 if (tofree != NULL)
3564 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003565 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003566 if (n)
3567 {
3568 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003569 arg = skipwhite(arg);
3570 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003571 if (n)
3572 clear_tv(&tv);
3573 }
3574 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003575 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003576 n = FALSE;
3577
3578 vim_free(tofree);
3579 return n;
3580}
3581
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003582static lval_T *redir_lval = NULL;
3583#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3584static garray_T redir_ga; // only valid when redir_lval is not NULL
3585static char_u *redir_endp = NULL;
3586static char_u *redir_varname = NULL;
3587
3588/*
3589 * Start recording command output to a variable
3590 * When "append" is TRUE append to an existing variable.
3591 * Returns OK if successfully completed the setup. FAIL otherwise.
3592 */
3593 int
3594var_redir_start(char_u *name, int append)
3595{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003596 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003597 typval_T tv;
3598
3599 // Catch a bad name early.
3600 if (!eval_isnamec1(*name))
3601 {
3602 emsg(_(e_invarg));
3603 return FAIL;
3604 }
3605
3606 // Make a copy of the name, it is used in redir_lval until redir ends.
3607 redir_varname = vim_strsave(name);
3608 if (redir_varname == NULL)
3609 return FAIL;
3610
3611 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3612 if (redir_lval == NULL)
3613 {
3614 var_redir_stop();
3615 return FAIL;
3616 }
3617
3618 // The output is stored in growarray "redir_ga" until redirection ends.
3619 ga_init2(&redir_ga, (int)sizeof(char), 500);
3620
3621 // Parse the variable name (can be a dict or list entry).
3622 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3623 FNE_CHECK_START);
3624 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3625 {
3626 clear_lval(redir_lval);
3627 if (redir_endp != NULL && *redir_endp != NUL)
3628 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003629 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003630 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003631 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003632 redir_endp = NULL; // don't store a value, only cleanup
3633 var_redir_stop();
3634 return FAIL;
3635 }
3636
3637 // check if we can write to the variable: set it to or append an empty
3638 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003639 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003640 tv.v_type = VAR_STRING;
3641 tv.vval.v_string = (char_u *)"";
3642 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003643 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3644 ASSIGN_NO_DECL, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003645 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003646 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3647 ASSIGN_NO_DECL, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003648 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003649 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003650 {
3651 redir_endp = NULL; // don't store a value, only cleanup
3652 var_redir_stop();
3653 return FAIL;
3654 }
3655
3656 return OK;
3657}
3658
3659/*
3660 * Append "value[value_len]" to the variable set by var_redir_start().
3661 * The actual appending is postponed until redirection ends, because the value
3662 * appended may in fact be the string we write to, changing it may cause freed
3663 * memory to be used:
3664 * :redir => foo
3665 * :let foo
3666 * :redir END
3667 */
3668 void
3669var_redir_str(char_u *value, int value_len)
3670{
3671 int len;
3672
3673 if (redir_lval == NULL)
3674 return;
3675
3676 if (value_len == -1)
3677 len = (int)STRLEN(value); // Append the entire string
3678 else
3679 len = value_len; // Append only "value_len" characters
3680
3681 if (ga_grow(&redir_ga, len) == OK)
3682 {
3683 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3684 redir_ga.ga_len += len;
3685 }
3686 else
3687 var_redir_stop();
3688}
3689
3690/*
3691 * Stop redirecting command output to a variable.
3692 * Frees the allocated memory.
3693 */
3694 void
3695var_redir_stop(void)
3696{
3697 typval_T tv;
3698
3699 if (EVALCMD_BUSY)
3700 {
3701 redir_lval = NULL;
3702 return;
3703 }
3704
3705 if (redir_lval != NULL)
3706 {
3707 // If there was no error: assign the text to the variable.
3708 if (redir_endp != NULL)
3709 {
3710 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3711 tv.v_type = VAR_STRING;
3712 tv.vval.v_string = redir_ga.ga_data;
3713 // Call get_lval() again, if it's inside a Dict or List it may
3714 // have changed.
3715 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3716 FALSE, FALSE, 0, FNE_CHECK_START);
3717 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003719 (char_u *)".");
3720 clear_lval(redir_lval);
3721 }
3722
3723 // free the collected output
3724 VIM_CLEAR(redir_ga.ga_data);
3725
3726 VIM_CLEAR(redir_lval);
3727 }
3728 VIM_CLEAR(redir_varname);
3729}
3730
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003731/*
3732 * "gettabvar()" function
3733 */
3734 void
3735f_gettabvar(typval_T *argvars, typval_T *rettv)
3736{
3737 win_T *oldcurwin;
3738 tabpage_T *tp, *oldtabpage;
3739 dictitem_T *v;
3740 char_u *varname;
3741 int done = FALSE;
3742
3743 rettv->v_type = VAR_STRING;
3744 rettv->vval.v_string = NULL;
3745
3746 varname = tv_get_string_chk(&argvars[1]);
3747 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3748 if (tp != NULL && varname != NULL)
3749 {
3750 // Set tp to be our tabpage, temporarily. Also set the window to the
3751 // first window in the tabpage, otherwise the window is not valid.
3752 if (switch_win(&oldcurwin, &oldtabpage,
3753 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3754 : tp->tp_firstwin, tp, TRUE) == OK)
3755 {
3756 // look up the variable
3757 // Let gettabvar({nr}, "") return the "t:" dictionary.
3758 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3759 if (v != NULL)
3760 {
3761 copy_tv(&v->di_tv, rettv);
3762 done = TRUE;
3763 }
3764 }
3765
3766 // restore previous notion of curwin
3767 restore_win(oldcurwin, oldtabpage, TRUE);
3768 }
3769
3770 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3771 copy_tv(&argvars[2], rettv);
3772}
3773
3774/*
3775 * "gettabwinvar()" function
3776 */
3777 void
3778f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3779{
3780 getwinvar(argvars, rettv, 1);
3781}
3782
3783/*
3784 * "getwinvar()" function
3785 */
3786 void
3787f_getwinvar(typval_T *argvars, typval_T *rettv)
3788{
3789 getwinvar(argvars, rettv, 0);
3790}
3791
3792/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003793 * "getbufvar()" function
3794 */
3795 void
3796f_getbufvar(typval_T *argvars, typval_T *rettv)
3797{
3798 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003799 char_u *varname;
3800 dictitem_T *v;
3801 int done = FALSE;
3802
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003803 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003804 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003805
3806 rettv->v_type = VAR_STRING;
3807 rettv->vval.v_string = NULL;
3808
3809 if (buf != NULL && varname != NULL)
3810 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003811 if (*varname == '&')
3812 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003813 buf_T *save_curbuf = curbuf;
3814
3815 // set curbuf to be our buf, temporarily
3816 curbuf = buf;
3817
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003818 if (varname[1] == NUL)
3819 {
3820 // get all buffer-local options in a dict
3821 dict_T *opts = get_winbuf_options(TRUE);
3822
3823 if (opts != NULL)
3824 {
3825 rettv_dict_set(rettv, opts);
3826 done = TRUE;
3827 }
3828 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003829 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003830 // buffer-local-option
3831 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003832
3833 // restore previous notion of curbuf
3834 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003835 }
3836 else
3837 {
3838 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003839 if (*varname == NUL)
3840 // Let getbufvar({nr}, "") return the "b:" dictionary.
3841 v = &buf->b_bufvar;
3842 else
3843 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3844 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003845 if (v != NULL)
3846 {
3847 copy_tv(&v->di_tv, rettv);
3848 done = TRUE;
3849 }
3850 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003851 }
3852
3853 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3854 // use the default value
3855 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003856}
3857
3858/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003859 * "settabvar()" function
3860 */
3861 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003862f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003863{
3864 tabpage_T *save_curtab;
3865 tabpage_T *tp;
3866 char_u *varname, *tabvarname;
3867 typval_T *varp;
3868
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003869 if (check_secure())
3870 return;
3871
3872 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3873 varname = tv_get_string_chk(&argvars[1]);
3874 varp = &argvars[2];
3875
3876 if (varname != NULL && varp != NULL && tp != NULL)
3877 {
3878 save_curtab = curtab;
3879 goto_tabpage_tp(tp, FALSE, FALSE);
3880
3881 tabvarname = alloc(STRLEN(varname) + 3);
3882 if (tabvarname != NULL)
3883 {
3884 STRCPY(tabvarname, "t:");
3885 STRCPY(tabvarname + 2, varname);
3886 set_var(tabvarname, varp, TRUE);
3887 vim_free(tabvarname);
3888 }
3889
3890 // Restore current tabpage
3891 if (valid_tabpage(save_curtab))
3892 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3893 }
3894}
3895
3896/*
3897 * "settabwinvar()" function
3898 */
3899 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003900f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003901{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003902 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003903}
3904
3905/*
3906 * "setwinvar()" function
3907 */
3908 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003909f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003910{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003911 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003912}
3913
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003914/*
3915 * "setbufvar()" function
3916 */
3917 void
3918f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3919{
3920 buf_T *buf;
3921 char_u *varname, *bufvarname;
3922 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003923
3924 if (check_secure())
3925 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003926 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003927 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003928 varp = &argvars[2];
3929
3930 if (buf != NULL && varname != NULL && varp != NULL)
3931 {
3932 if (*varname == '&')
3933 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003934 aco_save_T aco;
3935
3936 // set curbuf to be our buf, temporarily
3937 aucmd_prepbuf(&aco, buf);
3938
Bram Moolenaar191929b2020-08-19 21:20:49 +02003939 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003940
3941 // reset notion of buffer
3942 aucmd_restbuf(&aco);
3943 }
3944 else
3945 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003946 bufvarname = alloc(STRLEN(varname) + 3);
3947 if (bufvarname != NULL)
3948 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003949 buf_T *save_curbuf = curbuf;
3950
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003951 curbuf = buf;
3952 STRCPY(bufvarname, "b:");
3953 STRCPY(bufvarname + 2, varname);
3954 set_var(bufvarname, varp, TRUE);
3955 vim_free(bufvarname);
3956 curbuf = save_curbuf;
3957 }
3958 }
3959 }
3960}
3961
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003962/*
3963 * Get a callback from "arg". It can be a Funcref or a function name.
3964 * When "arg" is zero return an empty string.
3965 * "cb_name" is not allocated.
3966 * "cb_name" is set to NULL for an invalid argument.
3967 */
3968 callback_T
3969get_callback(typval_T *arg)
3970{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003971 callback_T res;
3972 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003973
3974 res.cb_free_name = FALSE;
3975 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3976 {
3977 res.cb_partial = arg->vval.v_partial;
3978 ++res.cb_partial->pt_refcount;
3979 res.cb_name = partial_name(res.cb_partial);
3980 }
3981 else
3982 {
3983 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003984 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3985 && isdigit(*arg->vval.v_string))
3986 r = FAIL;
3987 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003988 {
3989 // Note that we don't make a copy of the string.
3990 res.cb_name = arg->vval.v_string;
3991 func_ref(res.cb_name);
3992 }
3993 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003994 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003995 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003996 r = FAIL;
3997
3998 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003999 {
4000 emsg(_("E921: Invalid callback argument"));
4001 res.cb_name = NULL;
4002 }
4003 }
4004 return res;
4005}
4006
4007/*
4008 * Copy a callback into a typval_T.
4009 */
4010 void
4011put_callback(callback_T *cb, typval_T *tv)
4012{
4013 if (cb->cb_partial != NULL)
4014 {
4015 tv->v_type = VAR_PARTIAL;
4016 tv->vval.v_partial = cb->cb_partial;
4017 ++tv->vval.v_partial->pt_refcount;
4018 }
4019 else
4020 {
4021 tv->v_type = VAR_FUNC;
4022 tv->vval.v_string = vim_strsave(cb->cb_name);
4023 func_ref(cb->cb_name);
4024 }
4025}
4026
4027/*
4028 * Make a copy of "src" into "dest", allocating the function name if needed,
4029 * without incrementing the refcount.
4030 */
4031 void
4032set_callback(callback_T *dest, callback_T *src)
4033{
4034 if (src->cb_partial == NULL)
4035 {
4036 // just a function name, make a copy
4037 dest->cb_name = vim_strsave(src->cb_name);
4038 dest->cb_free_name = TRUE;
4039 }
4040 else
4041 {
4042 // cb_name is a pointer into cb_partial
4043 dest->cb_name = src->cb_name;
4044 dest->cb_free_name = FALSE;
4045 }
4046 dest->cb_partial = src->cb_partial;
4047}
4048
4049/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004050 * Copy callback from "src" to "dest", incrementing the refcounts.
4051 */
4052 void
4053copy_callback(callback_T *dest, callback_T *src)
4054{
4055 dest->cb_partial = src->cb_partial;
4056 if (dest->cb_partial != NULL)
4057 {
4058 dest->cb_name = src->cb_name;
4059 dest->cb_free_name = FALSE;
4060 ++dest->cb_partial->pt_refcount;
4061 }
4062 else
4063 {
4064 dest->cb_name = vim_strsave(src->cb_name);
4065 dest->cb_free_name = TRUE;
4066 func_ref(src->cb_name);
4067 }
4068}
4069
4070/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004071 * Unref/free "callback" returned by get_callback() or set_callback().
4072 */
4073 void
4074free_callback(callback_T *callback)
4075{
4076 if (callback->cb_partial != NULL)
4077 {
4078 partial_unref(callback->cb_partial);
4079 callback->cb_partial = NULL;
4080 }
4081 else if (callback->cb_name != NULL)
4082 func_unref(callback->cb_name);
4083 if (callback->cb_free_name)
4084 {
4085 vim_free(callback->cb_name);
4086 callback->cb_free_name = FALSE;
4087 }
4088 callback->cb_name = NULL;
4089}
4090
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004091#endif // FEAT_EVAL