blob: 08736dd8c7a04fdb7516c750b4bbab8ca3d0d185 [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},
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100125 {VV_NAME("numbermax", VAR_NUMBER), VV_RO},
126 {VV_NAME("numbermin", VAR_NUMBER), VV_RO},
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100127 {VV_NAME("numbersize", VAR_NUMBER), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200128 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
129 {VV_NAME("testing", VAR_NUMBER), 0},
130 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
131 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
132 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
133 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
134 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
135 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
136 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
137 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
138 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
139 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
140 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
141 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
142 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
143 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
144 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
145 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
146 {VV_NAME("event", VAR_DICT), VV_RO},
147 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
148 {VV_NAME("echospace", VAR_NUMBER), VV_RO},
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100149 {VV_NAME("argv", VAR_LIST), VV_RO},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200150 {VV_NAME("collate", VAR_STRING), VV_RO},
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100151 {VV_NAME("exiting", VAR_SPECIAL), VV_RO},
Drew Vogele30d1022021-10-24 20:35:07 +0100152 {VV_NAME("colornames", VAR_DICT), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200153};
154
155// shorthand
156#define vv_type vv_di.di_tv.v_type
157#define vv_nr vv_di.di_tv.vval.v_number
158#define vv_float vv_di.di_tv.vval.v_float
159#define vv_str vv_di.di_tv.vval.v_string
160#define vv_list vv_di.di_tv.vval.v_list
161#define vv_dict vv_di.di_tv.vval.v_dict
162#define vv_blob vv_di.di_tv.vval.v_blob
163#define vv_tv vv_di.di_tv
164
165static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200166static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200167#define vimvarht vimvardict.dv_hashtab
168
169// for VIM_VERSION_ defines
170#include "version.h"
171
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200172static void list_glob_vars(int *first);
173static void list_buf_vars(int *first);
174static void list_win_vars(int *first);
175static void list_tab_vars(int *first);
176static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100177static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op, int var_idx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200178static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
179static 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 +0200180static void list_one_var(dictitem_T *v, char *prefix, int *first);
181static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
182
183/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200184 * Initialize global and vim special variables
185 */
186 void
187evalvars_init(void)
188{
189 int i;
190 struct vimvar *p;
191
192 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
193 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
194 vimvardict.dv_lock = VAR_FIXED;
195 hash_init(&compat_hashtab);
196
197 for (i = 0; i < VV_LEN; ++i)
198 {
199 p = &vimvars[i];
200 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
201 {
202 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
203 getout(1);
204 }
205 STRCPY(p->vv_di.di_key, p->vv_name);
206 if (p->vv_flags & VV_RO)
207 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
208 else if (p->vv_flags & VV_RO_SBX)
209 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
210 else
211 p->vv_di.di_flags = DI_FLAGS_FIX;
212
213 // add to v: scope dict, unless the value is not always available
214 if (p->vv_type != VAR_UNKNOWN)
215 hash_add(&vimvarht, p->vv_di.di_key);
216 if (p->vv_flags & VV_COMPAT)
217 // add to compat scope dict
218 hash_add(&compat_hashtab, p->vv_di.di_key);
219 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200220 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
221 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200222
223 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
224 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100225 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200226 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
227 set_vim_var_list(VV_ERRORS, list_alloc());
228 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
229
230 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
231 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
232 set_vim_var_nr(VV_NONE, VVAL_NONE);
233 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100234 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
235 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100236 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200237
238 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
239 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
240 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
241 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
242 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
243 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
244 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
245 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
246 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
247 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
248 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
249
250 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
251
Drew Vogele30d1022021-10-24 20:35:07 +0100252 set_vim_var_dict(VV_COLORNAMES, dict_alloc());
253
Bram Moolenaar439c0362020-06-06 15:58:03 +0200254 // Default for v:register is not 0 but '"'. This is adjusted once the
255 // clipboard has been setup by calling reset_reg_var().
256 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200257}
258
259#if defined(EXITFREE) || defined(PROTO)
260/*
261 * Free all vim variables information on exit
262 */
263 void
264evalvars_clear(void)
265{
266 int i;
267 struct vimvar *p;
268
269 for (i = 0; i < VV_LEN; ++i)
270 {
271 p = &vimvars[i];
272 if (p->vv_di.di_tv.v_type == VAR_STRING)
273 VIM_CLEAR(p->vv_str);
274 else if (p->vv_di.di_tv.v_type == VAR_LIST)
275 {
276 list_unref(p->vv_list);
277 p->vv_list = NULL;
278 }
279 }
280 hash_clear(&vimvarht);
281 hash_init(&vimvarht); // garbage_collect() will access it
282 hash_clear(&compat_hashtab);
283
284 // global variables
285 vars_clear(&globvarht);
286
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100287 // Script-local variables. Clear all the variables here.
288 // The scriptvar_T is cleared later in free_scriptnames(), because a
289 // variable in one script might hold a reference to the whole scope of
290 // another script.
291 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200292 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200293}
294#endif
295
296 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200297garbage_collect_globvars(int copyID)
298{
299 return set_ref_in_ht(&globvarht, copyID, NULL);
300}
301
302 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200303garbage_collect_vimvars(int copyID)
304{
305 return set_ref_in_ht(&vimvarht, copyID, NULL);
306}
307
308 int
309garbage_collect_scriptvars(int copyID)
310{
Bram Moolenaared234f22020-10-15 20:42:20 +0200311 int i;
312 int idx;
313 int abort = FALSE;
314 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200315
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100316 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200317 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200318 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
319
Bram Moolenaared234f22020-10-15 20:42:20 +0200320 si = SCRIPT_ITEM(i);
321 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
322 {
323 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
324
Bram Moolenaard00a7fb2021-03-08 20:47:14 +0100325 if (sv->sv_name != NULL)
326 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
Bram Moolenaared234f22020-10-15 20:42:20 +0200327 }
328 }
329
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200330 return abort;
331}
332
333/*
334 * Set an internal variable to a string value. Creates the variable if it does
335 * not already exist.
336 */
337 void
338set_internal_string_var(char_u *name, char_u *value)
339{
340 char_u *val;
341 typval_T *tvp;
342
343 val = vim_strsave(value);
344 if (val != NULL)
345 {
346 tvp = alloc_string_tv(val);
347 if (tvp != NULL)
348 {
349 set_var(name, tvp, FALSE);
350 free_tv(tvp);
351 }
352 }
353}
354
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200355 int
356eval_charconvert(
357 char_u *enc_from,
358 char_u *enc_to,
359 char_u *fname_from,
360 char_u *fname_to)
361{
362 int err = FALSE;
363
364 set_vim_var_string(VV_CC_FROM, enc_from, -1);
365 set_vim_var_string(VV_CC_TO, enc_to, -1);
366 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
367 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
368 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
369 err = TRUE;
370 set_vim_var_string(VV_CC_FROM, NULL, -1);
371 set_vim_var_string(VV_CC_TO, NULL, -1);
372 set_vim_var_string(VV_FNAME_IN, NULL, -1);
373 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
374
375 if (err)
376 return FAIL;
377 return OK;
378}
379
380# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
381 int
382eval_printexpr(char_u *fname, char_u *args)
383{
384 int err = FALSE;
385
386 set_vim_var_string(VV_FNAME_IN, fname, -1);
387 set_vim_var_string(VV_CMDARG, args, -1);
388 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
389 err = TRUE;
390 set_vim_var_string(VV_FNAME_IN, NULL, -1);
391 set_vim_var_string(VV_CMDARG, NULL, -1);
392
393 if (err)
394 {
395 mch_remove(fname);
396 return FAIL;
397 }
398 return OK;
399}
400# endif
401
402# if defined(FEAT_DIFF) || defined(PROTO)
403 void
404eval_diff(
405 char_u *origfile,
406 char_u *newfile,
407 char_u *outfile)
408{
409 int err = FALSE;
410
411 set_vim_var_string(VV_FNAME_IN, origfile, -1);
412 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
413 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
414 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
415 set_vim_var_string(VV_FNAME_IN, NULL, -1);
416 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
417 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
418}
419
420 void
421eval_patch(
422 char_u *origfile,
423 char_u *difffile,
424 char_u *outfile)
425{
426 int err;
427
428 set_vim_var_string(VV_FNAME_IN, origfile, -1);
429 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
430 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
431 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
432 set_vim_var_string(VV_FNAME_IN, NULL, -1);
433 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
434 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
435}
436# endif
437
438#if defined(FEAT_SPELL) || defined(PROTO)
439/*
440 * Evaluate an expression to a list with suggestions.
441 * For the "expr:" part of 'spellsuggest'.
442 * Returns NULL when there is an error.
443 */
444 list_T *
445eval_spell_expr(char_u *badword, char_u *expr)
446{
447 typval_T save_val;
448 typval_T rettv;
449 list_T *list = NULL;
450 char_u *p = skipwhite(expr);
451
452 // Set "v:val" to the bad word.
453 prepare_vimvar(VV_VAL, &save_val);
454 set_vim_var_string(VV_VAL, badword, -1);
455 if (p_verbose == 0)
456 ++emsg_off;
457
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200458 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200459 {
460 if (rettv.v_type != VAR_LIST)
461 clear_tv(&rettv);
462 else
463 list = rettv.vval.v_list;
464 }
465
466 if (p_verbose == 0)
467 --emsg_off;
468 clear_tv(get_vim_var_tv(VV_VAL));
469 restore_vimvar(VV_VAL, &save_val);
470
471 return list;
472}
473
474/*
475 * "list" is supposed to contain two items: a word and a number. Return the
476 * word in "pp" and the number as the return value.
477 * Return -1 if anything isn't right.
478 * Used to get the good word and score from the eval_spell_expr() result.
479 */
480 int
481get_spellword(list_T *list, char_u **pp)
482{
483 listitem_T *li;
484
485 li = list->lv_first;
486 if (li == NULL)
487 return -1;
488 *pp = tv_get_string(&li->li_tv);
489
490 li = li->li_next;
491 if (li == NULL)
492 return -1;
493 return (int)tv_get_number(&li->li_tv);
494}
495#endif
496
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200497/*
498 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200499 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200500 * When not used yet add the variable to the v: hashtable.
501 */
502 void
503prepare_vimvar(int idx, typval_T *save_tv)
504{
505 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200506 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200507 if (vimvars[idx].vv_type == VAR_UNKNOWN)
508 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
509}
510
511/*
512 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200513 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200514 * When no longer defined, remove the variable from the v: hashtable.
515 */
516 void
517restore_vimvar(int idx, typval_T *save_tv)
518{
519 hashitem_T *hi;
520
521 vimvars[idx].vv_tv = *save_tv;
522 if (vimvars[idx].vv_type == VAR_UNKNOWN)
523 {
524 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
525 if (HASHITEM_EMPTY(hi))
526 internal_error("restore_vimvar()");
527 else
528 hash_remove(&vimvarht, hi);
529 }
530}
531
532/*
533 * List Vim variables.
534 */
535 static void
536list_vim_vars(int *first)
537{
538 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
539}
540
541/*
542 * List script-local variables, if there is a script.
543 */
544 static void
545list_script_vars(int *first)
546{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200547 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200548 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
549 "s:", FALSE, first);
550}
551
552/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200553 * Get a list of lines from a HERE document. The here document is a list of
554 * lines surrounded by a marker.
555 * cmd << {marker}
556 * {line1}
557 * {line2}
558 * ....
559 * {marker}
560 *
561 * The {marker} is a string. If the optional 'trim' word is supplied before the
562 * marker, then the leading indentation before the lines (matching the
563 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200564 *
565 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
566 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
567 * missing, then '.' is accepted as a marker.
568 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200569 * Returns a List with {lines} or NULL.
570 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200572heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200573{
574 char_u *theline;
575 char_u *marker;
576 list_T *l;
577 char_u *p;
578 int marker_indent_len = 0;
579 int text_indent_len = 0;
580 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200581 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200582 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200583
584 if (eap->getline == NULL)
585 {
586 emsg(_("E991: cannot use =<< here"));
587 return NULL;
588 }
589
590 // Check for the optional 'trim' word before the marker
591 cmd = skipwhite(cmd);
592 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
593 {
594 cmd = skipwhite(cmd + 4);
595
596 // Trim the indentation from all the lines in the here document.
597 // The amount of indentation trimmed is the same as the indentation of
598 // the first line after the :let command line. To find the end marker
599 // the indent of the :let command line is trimmed.
600 p = *eap->cmdlinep;
601 while (VIM_ISWHITE(*p))
602 {
603 p++;
604 marker_indent_len++;
605 }
606 text_indent_len = -1;
607 }
608
609 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200610 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200611 {
612 marker = skipwhite(cmd);
613 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200614 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200615 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200616 semsg(_(e_trailing_arg), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200617 return NULL;
618 }
619 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200620 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200621 {
622 emsg(_("E221: Marker cannot start with lower case letter"));
623 return NULL;
624 }
625 }
626 else
627 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200628 // When getting lines for an embedded script, if the marker is missing,
629 // accept '.' as the marker.
630 if (script_get)
631 marker = dot;
632 else
633 {
634 emsg(_("E172: Missing marker"));
635 return NULL;
636 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200637 }
638
639 l = list_alloc();
640 if (l == NULL)
641 return NULL;
642
643 for (;;)
644 {
645 int mi = 0;
646 int ti = 0;
647
648 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
649 if (theline == NULL)
650 {
651 semsg(_("E990: Missing end marker '%s'"), marker);
652 break;
653 }
654
655 // with "trim": skip the indent matching the :let line to find the
656 // marker
657 if (marker_indent_len > 0
658 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
659 mi = marker_indent_len;
660 if (STRCMP(marker, theline + mi) == 0)
661 {
662 vim_free(theline);
663 break;
664 }
665
666 if (text_indent_len == -1 && *theline != NUL)
667 {
668 // set the text indent from the first line.
669 p = theline;
670 text_indent_len = 0;
671 while (VIM_ISWHITE(*p))
672 {
673 p++;
674 text_indent_len++;
675 }
676 text_indent = vim_strnsave(theline, text_indent_len);
677 }
678 // with "trim": skip the indent matching the first line
679 if (text_indent != NULL)
680 for (ti = 0; ti < text_indent_len; ++ti)
681 if (theline[ti] != text_indent[ti])
682 break;
683
684 if (list_append_string(l, theline + ti, -1) == FAIL)
685 break;
686 vim_free(theline);
687 }
688 vim_free(text_indent);
689
690 return l;
691}
692
693/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200694 * Vim9 variable declaration:
695 * ":var name"
696 * ":var name: type"
697 * ":var name = expr"
698 * ":var name: type = expr"
699 * etc.
700 */
701 void
702ex_var(exarg_T *eap)
703{
704 if (!in_vim9script())
705 {
706 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
707 return;
708 }
709 ex_let(eap);
710}
711
712/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200713 * ":let" list all variable values
714 * ":let var1 var2" list variable values
715 * ":let var = expr" assignment command.
716 * ":let var += expr" assignment command.
717 * ":let var -= expr" assignment command.
718 * ":let var *= expr" assignment command.
719 * ":let var /= expr" assignment command.
720 * ":let var %= expr" assignment command.
721 * ":let var .= expr" assignment command.
722 * ":let var ..= expr" assignment command.
723 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100725 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200726 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200727 * ":final var = expr" assignment command.
728 * ":final [var1, var2] = expr" unpack list.
729 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200730 * ":const" list all variable values
731 * ":const var1 var2" list variable values
732 * ":const var = expr" assignment command.
733 * ":const [var1, var2] = expr" unpack list.
734 */
735 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200736ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200737{
738 char_u *arg = eap->arg;
739 char_u *expr = NULL;
740 typval_T rettv;
741 int i;
742 int var_count = 0;
743 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200744 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200745 char_u *argend;
746 int first = TRUE;
747 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200748 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100749 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200750 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200751
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200752 if (eap->cmdidx == CMD_final && !vim9script)
753 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100754 // In legacy Vim script ":final" is short for ":finally".
755 ex_finally(eap);
756 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200757 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200758 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200759 {
760 emsg(_(e_cannot_use_let_in_vim9_script));
761 return;
762 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200763
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100764 if (eap->cmdidx == CMD_const)
765 flags |= ASSIGN_CONST;
766 else if (eap->cmdidx == CMD_final)
767 flags |= ASSIGN_FINAL;
768
769 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200771 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200773 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200774 if (argend == NULL)
775 return;
776 if (argend > arg && argend[-1] == '.') // for var.='str'
777 --argend;
778 expr = skipwhite(argend);
779 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +0200780 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200781 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200782 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
783 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200784 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200785 {
786 // ":let" without "=": list variables
787 if (*arg == '[')
788 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200789 else if (expr[0] == '.' && expr[1] == '=')
790 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200791 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200792 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200793 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200794 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100795 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
796 semsg(_(e_trailing_arg), argend);
797 else
798 // Vim9 declaration ":var name: type"
799 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200800 }
801 else
802 {
803 // ":let var1 var2" - list values
804 arg = list_arg_vars(eap, arg, &first);
805 }
806 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200807 else if (!eap->skip)
808 {
809 // ":let"
810 list_glob_vars(&first);
811 list_buf_vars(&first);
812 list_win_vars(&first);
813 list_tab_vars(&first);
814 list_script_vars(&first);
815 list_func_vars(&first);
816 list_vim_vars(&first);
817 }
Bram Moolenaar63b91732021-08-05 20:40:03 +0200818 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200819 }
820 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
821 {
822 list_T *l;
Bram Moolenaar81530e32021-07-28 21:25:49 +0200823 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200824
825 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200826 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200827 if (l != NULL)
828 {
829 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200830 if (!eap->skip)
831 {
Bram Moolenaar81530e32021-07-28 21:25:49 +0200832 // errors are for the assignment, not the end marker
833 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200834 op[0] = '=';
835 op[1] = NUL;
836 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200838 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200839 clear_tv(&rettv);
840 }
841 }
842 else
843 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200844 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200845 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200846
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200847 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200848 i = FAIL;
849 if (has_assign || concat)
850 {
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100851 int cur_lnum;
852
Bram Moolenaar32e35112020-05-14 22:41:15 +0200853 op[0] = '=';
854 op[1] = NUL;
855 if (*expr != '=')
856 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200857 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200858 {
859 // +=, /=, etc. require an existing variable
860 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
861 i = FAIL;
862 }
863 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200864 {
865 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200866 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200867 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200868 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200869 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200870 ++len;
871 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200872 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200873 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200874 }
875 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200876 ++expr;
877
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200878 if (vim9script && (!VIM_ISWHITE(*argend)
879 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200880 {
881 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100882 semsg(_(e_white_space_required_before_and_after_str_at_str),
883 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200884 i = FAIL;
885 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200886
887 if (eap->skip)
888 ++emsg_skip;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200889 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200890 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100891 cur_lnum = SOURCING_LNUM;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200892 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200893 if (eap->skip)
894 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200895 clear_evalarg(&evalarg, eap);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100896
897 // Restore the line number so that any type error is given for the
898 // declaration, not the expression.
899 SOURCING_LNUM = cur_lnum;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200900 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200901 if (eap->skip)
902 {
903 if (i != FAIL)
904 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200905 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200906 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200907 {
908 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200909 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200910 clear_tv(&rettv);
911 }
912 }
913}
914
915/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +0100916 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200917 * Handles both "var" with any type and "[var, var; var]" with a list type.
918 * When "op" is not NULL it points to a string with characters that
919 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
920 * or concatenate.
921 * Returns OK or FAIL;
922 */
923 int
924ex_let_vars(
925 char_u *arg_start,
926 typval_T *tv,
927 int copy, // copy values from "tv", don't move
928 int semicolon, // from skip_var_list()
929 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100930 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200931 char_u *op)
932{
933 char_u *arg = arg_start;
934 list_T *l;
935 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100936 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200937 listitem_T *item;
938 typval_T ltv;
939
940 if (*arg != '[')
941 {
942 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100943 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200944 return FAIL;
945 return OK;
946 }
947
948 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
949 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
950 {
951 emsg(_(e_listreq));
952 return FAIL;
953 }
954
955 i = list_len(l);
956 if (semicolon == 0 && var_count < i)
957 {
958 emsg(_("E687: Less targets than List items"));
959 return FAIL;
960 }
961 if (var_count - semicolon > i)
962 {
963 emsg(_("E688: More targets than List items"));
964 return FAIL;
965 }
966
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200967 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200968 item = l->lv_first;
969 while (*arg != ']')
970 {
971 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100972 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +0200973 arg = ex_let_one(arg, &item->li_tv, TRUE,
974 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200975 item = item->li_next;
976 if (arg == NULL)
977 return FAIL;
978
979 arg = skipwhite(arg);
980 if (*arg == ';')
981 {
982 // Put the rest of the list (may be empty) in the var after ';'.
983 // Create a new list for this.
984 l = list_alloc();
985 if (l == NULL)
986 return FAIL;
987 while (item != NULL)
988 {
989 list_append_tv(l, &item->li_tv);
990 item = item->li_next;
991 }
992
993 ltv.v_type = VAR_LIST;
994 ltv.v_lock = 0;
995 ltv.vval.v_list = l;
996 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100997 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200998
Bram Moolenaarf93bbd02021-04-10 22:35:43 +0200999 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1000 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001001 clear_tv(&ltv);
1002 if (arg == NULL)
1003 return FAIL;
1004 break;
1005 }
1006 else if (*arg != ',' && *arg != ']')
1007 {
1008 internal_error("ex_let_vars()");
1009 return FAIL;
1010 }
1011 }
1012
1013 return OK;
1014}
1015
1016/*
1017 * Skip over assignable variable "var" or list of variables "[var, var]".
1018 * Used for ":let varvar = expr" and ":for varvar in expr".
1019 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001020 * for "[var, var; var]" set "semicolon" to 1.
1021 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001022 * Return NULL for an error.
1023 */
1024 char_u *
1025skip_var_list(
1026 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001027 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001028 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001029 int *semicolon,
1030 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001031{
1032 char_u *p, *s;
1033
1034 if (*arg == '[')
1035 {
1036 // "[var, var]": find the matching ']'.
1037 p = arg;
1038 for (;;)
1039 {
1040 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001041 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001042 if (s == p)
1043 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001044 if (!silent)
1045 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001046 return NULL;
1047 }
1048 ++*var_count;
1049
1050 p = skipwhite(s);
1051 if (*p == ']')
1052 break;
1053 else if (*p == ';')
1054 {
1055 if (*semicolon == 1)
1056 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001057 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001058 return NULL;
1059 }
1060 *semicolon = 1;
1061 }
1062 else if (*p != ',')
1063 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001064 if (!silent)
1065 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001066 return NULL;
1067 }
1068 }
1069 return p + 1;
1070 }
1071 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001072 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001073}
1074
1075/*
1076 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1077 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001078 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001079 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001080 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001082{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001083 char_u *end;
1084 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001085
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001086 if (*arg == '@' && arg[1] != NUL)
1087 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001089 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001090
1091 // "a: type" is declaring variable "a" with a type, not "a:".
1092 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001093 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001094 --end;
1095
Bram Moolenaar585587d2021-01-17 20:52:13 +01001096 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001098 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001099 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001100 }
1101 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001102}
1103
1104/*
1105 * List variables for hashtab "ht" with prefix "prefix".
1106 * If "empty" is TRUE also list NULL strings as empty strings.
1107 */
1108 void
1109list_hashtable_vars(
1110 hashtab_T *ht,
1111 char *prefix,
1112 int empty,
1113 int *first)
1114{
1115 hashitem_T *hi;
1116 dictitem_T *di;
1117 int todo;
1118 char_u buf[IOSIZE];
1119
1120 todo = (int)ht->ht_used;
1121 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1122 {
1123 if (!HASHITEM_EMPTY(hi))
1124 {
1125 --todo;
1126 di = HI2DI(hi);
1127
1128 // apply :filter /pat/ to variable name
1129 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1130 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1131 if (message_filtered(buf))
1132 continue;
1133
1134 if (empty || di->di_tv.v_type != VAR_STRING
1135 || di->di_tv.vval.v_string != NULL)
1136 list_one_var(di, prefix, first);
1137 }
1138 }
1139}
1140
1141/*
1142 * List global variables.
1143 */
1144 static void
1145list_glob_vars(int *first)
1146{
1147 list_hashtable_vars(&globvarht, "", TRUE, first);
1148}
1149
1150/*
1151 * List buffer variables.
1152 */
1153 static void
1154list_buf_vars(int *first)
1155{
1156 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1157}
1158
1159/*
1160 * List window variables.
1161 */
1162 static void
1163list_win_vars(int *first)
1164{
1165 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1166}
1167
1168/*
1169 * List tab page variables.
1170 */
1171 static void
1172list_tab_vars(int *first)
1173{
1174 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1175}
1176
1177/*
1178 * List variables in "arg".
1179 */
1180 static char_u *
1181list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1182{
1183 int error = FALSE;
1184 int len;
1185 char_u *name;
1186 char_u *name_start;
1187 char_u *arg_subsc;
1188 char_u *tofree;
1189 typval_T tv;
1190
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001191 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001192 {
1193 if (error || eap->skip)
1194 {
1195 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1196 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1197 {
1198 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001199 if (!did_emsg)
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001200 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001201 break;
1202 }
1203 }
1204 else
1205 {
1206 // get_name_len() takes care of expanding curly braces
1207 name_start = name = arg;
1208 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1209 if (len <= 0)
1210 {
1211 // This is mainly to keep test 49 working: when expanding
1212 // curly braces fails overrule the exception error message.
1213 if (len < 0 && !aborting())
1214 {
1215 emsg_severe = TRUE;
1216 semsg(_(e_invarg2), arg);
1217 break;
1218 }
1219 error = TRUE;
1220 }
1221 else
1222 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001223 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001224 if (tofree != NULL)
1225 name = tofree;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001226 if (eval_variable(name, len, &tv, NULL,
1227 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001228 error = TRUE;
1229 else
1230 {
1231 // handle d.key, l[idx], f(expr)
1232 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001233 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001234 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001235 error = TRUE;
1236 else
1237 {
1238 if (arg == arg_subsc && len == 2 && name[1] == ':')
1239 {
1240 switch (*name)
1241 {
1242 case 'g': list_glob_vars(first); break;
1243 case 'b': list_buf_vars(first); break;
1244 case 'w': list_win_vars(first); break;
1245 case 't': list_tab_vars(first); break;
1246 case 'v': list_vim_vars(first); break;
1247 case 's': list_script_vars(first); break;
1248 case 'l': list_func_vars(first); break;
1249 default:
1250 semsg(_("E738: Can't list variables for %s"), name);
1251 }
1252 }
1253 else
1254 {
1255 char_u numbuf[NUMBUFLEN];
1256 char_u *tf;
1257 int c;
1258 char_u *s;
1259
1260 s = echo_string(&tv, &tf, numbuf, 0);
1261 c = *arg;
1262 *arg = NUL;
1263 list_one_var_a("",
1264 arg == arg_subsc ? name : name_start,
1265 tv.v_type,
1266 s == NULL ? (char_u *)"" : s,
1267 first);
1268 *arg = c;
1269 vim_free(tf);
1270 }
1271 clear_tv(&tv);
1272 }
1273 }
1274 }
1275
1276 vim_free(tofree);
1277 }
1278
1279 arg = skipwhite(arg);
1280 }
1281
1282 return arg;
1283}
1284
1285/*
1286 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1287 * Returns a pointer to the char just after the var name.
1288 * Returns NULL if there is an error.
1289 */
1290 static char_u *
1291ex_let_one(
1292 char_u *arg, // points to variable name
1293 typval_T *tv, // value to assign to variable
1294 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001295 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001296 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001297 char_u *op, // "+", "-", "." or NULL
1298 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001299{
1300 int c1;
1301 char_u *name;
1302 char_u *p;
1303 char_u *arg_end = NULL;
1304 int len;
1305 int opt_flags;
1306 char_u *tofree = NULL;
1307
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001308 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001309 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001310 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1311 {
1312 vim9_declare_error(arg);
1313 return NULL;
1314 }
1315
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001316 // ":let $VAR = expr": Set environment variable.
1317 if (*arg == '$')
1318 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001319 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1320 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001321 {
1322 emsg(_("E996: Cannot lock an environment variable"));
1323 return NULL;
1324 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001325
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001326 // Find the end of the name.
1327 ++arg;
1328 name = arg;
1329 len = get_env_len(&arg);
1330 if (len == 0)
1331 semsg(_(e_invarg2), name - 1);
1332 else
1333 {
1334 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1335 semsg(_(e_letwrong), op);
1336 else if (endchars != NULL
1337 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02001338 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001339 else if (!check_secure())
1340 {
1341 c1 = name[len];
1342 name[len] = NUL;
1343 p = tv_get_string_chk(tv);
1344 if (p != NULL && op != NULL && *op == '.')
1345 {
1346 int mustfree = FALSE;
1347 char_u *s = vim_getenv(name, &mustfree);
1348
1349 if (s != NULL)
1350 {
1351 p = tofree = concat_str(s, p);
1352 if (mustfree)
1353 vim_free(s);
1354 }
1355 }
1356 if (p != NULL)
1357 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001358 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001359 arg_end = arg;
1360 }
1361 name[len] = c1;
1362 vim_free(tofree);
1363 }
1364 }
1365 }
1366
1367 // ":let &option = expr": Set option value.
1368 // ":let &l:option = expr": Set local option value.
1369 // ":let &g:option = expr": Set global option value.
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001370 // ":for &ts in range(8)": Set option value for for loop
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001371 else if (*arg == '&')
1372 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001373 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1374 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001375 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001377 return NULL;
1378 }
1379 // Find the end of the name.
1380 p = find_option_end(&arg, &opt_flags);
1381 if (p == NULL || (endchars != NULL
1382 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaar108010a2021-06-27 22:03:33 +02001383 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001384 else
1385 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001386 long n = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001387 getoption_T opt_type;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001388 long numval;
1389 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001390 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001391 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001392
1393 c1 = *p;
1394 *p = NUL;
1395
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001396 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001397 if ((opt_type == gov_bool
1398 || opt_type == gov_number
1399 || opt_type == gov_hidden_bool
1400 || opt_type == gov_hidden_number)
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001401 && (tv->v_type != VAR_STRING || !in_vim9script()))
Bram Moolenaar0ea04402021-01-04 13:37:54 +01001402 {
1403 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1404 // bool, possibly hidden
1405 n = (long)tv_get_bool(tv);
1406 else
1407 // number, possibly hidden
1408 n = (long)tv_get_number(tv);
1409 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001410
1411 // Avoid setting a string option to the text "v:false" or similar.
1412 // In Vim9 script also don't convert a number to string.
1413 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1414 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1415 s = tv_get_string_chk(tv);
1416
1417 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001418 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001419 if (((opt_type == gov_bool || opt_type == gov_number)
1420 && *op == '.')
1421 || (opt_type == gov_string && *op != '.'))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001422 {
1423 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001424 failed = TRUE; // don't set the value
1425
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001426 }
1427 else
1428 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001429 // number, in legacy script also bool
1430 if (opt_type == gov_number
1431 || (opt_type == gov_bool && !in_vim9script()))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001432 {
1433 switch (*op)
1434 {
1435 case '+': n = numval + n; break;
1436 case '-': n = numval - n; break;
1437 case '*': n = numval * n; break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001438 case '/': n = (long)num_divide(numval, n,
1439 &failed); break;
1440 case '%': n = (long)num_modulus(numval, n,
1441 &failed); break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001442 }
Bram Moolenaara42e6e02021-06-10 18:43:25 +02001443 s = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001444 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001445 else if (opt_type == gov_string
1446 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001447 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001448 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001449 s = concat_str(stringval, s);
1450 vim_free(stringval);
1451 stringval = s;
1452 }
1453 }
1454 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001455
1456 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001457 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001458 if (opt_type != gov_string || s != NULL)
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001459 {
1460 set_option_value(arg, n, s, opt_flags);
1461 arg_end = p;
1462 }
1463 else
1464 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001465 }
1466 *p = c1;
1467 vim_free(stringval);
1468 }
1469 }
1470
1471 // ":let @r = expr": Set register contents.
1472 else if (*arg == '@')
1473 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001474 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1475 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001476 {
1477 emsg(_("E996: Cannot lock a register"));
1478 return NULL;
1479 }
1480 ++arg;
1481 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1482 semsg(_(e_letwrong), op);
1483 else if (endchars != NULL
1484 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02001485 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001486 else
1487 {
1488 char_u *ptofree = NULL;
1489 char_u *s;
1490
1491 p = tv_get_string_chk(tv);
1492 if (p != NULL && op != NULL && *op == '.')
1493 {
1494 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1495 if (s != NULL)
1496 {
1497 p = ptofree = concat_str(s, p);
1498 vim_free(s);
1499 }
1500 }
1501 if (p != NULL)
1502 {
1503 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1504 arg_end = arg + 1;
1505 }
1506 vim_free(ptofree);
1507 }
1508 }
1509
1510 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001512 // ":let {expr} = expr": Idem, name made with curly braces
1513 else if (eval_isnamec1(*arg) || *arg == '{')
1514 {
1515 lval_T lv;
1516
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001517 p = get_lval(arg, tv, &lv, FALSE, FALSE,
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001518 (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1519 ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001520 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001521 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522 if (endchars != NULL && vim_strchr(endchars,
1523 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02001524 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001525 else
1526 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001527 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001528 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001529 }
1530 }
1531 clear_lval(&lv);
1532 }
1533
1534 else
1535 semsg(_(e_invarg2), arg);
1536
1537 return arg_end;
1538}
1539
1540/*
1541 * ":unlet[!] var1 ... " command.
1542 */
1543 void
1544ex_unlet(exarg_T *eap)
1545{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001546 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001547}
1548
1549/*
1550 * ":lockvar" and ":unlockvar" commands
1551 */
1552 void
1553ex_lockvar(exarg_T *eap)
1554{
1555 char_u *arg = eap->arg;
1556 int deep = 2;
1557
1558 if (eap->forceit)
1559 deep = -1;
1560 else if (vim_isdigit(*arg))
1561 {
1562 deep = getdigits(&arg);
1563 arg = skipwhite(arg);
1564 }
1565
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001566 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001567}
1568
1569/*
1570 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001571 * Also used for Vim9 script. "callback" is invoked as:
1572 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001573 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001574 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001575ex_unletlock(
1576 exarg_T *eap,
1577 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001578 int deep,
1579 int glv_flags,
1580 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1581 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001582{
1583 char_u *arg = argstart;
1584 char_u *name_end;
1585 int error = FALSE;
1586 lval_T lv;
1587
1588 do
1589 {
1590 if (*arg == '$')
1591 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001592 lv.ll_name = arg;
1593 lv.ll_tv = NULL;
1594 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001595 if (get_env_len(&arg) == 0)
1596 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001597 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001598 return;
1599 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001600 if (!error && !eap->skip
1601 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1602 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001603 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001604 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001605 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001606 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001607 // Parse the name and find the end.
1608 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001609 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001610 if (lv.ll_name == NULL)
1611 error = TRUE; // error but continue parsing
1612 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1613 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001614 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001615 if (name_end != NULL)
1616 {
1617 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001618 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001619 }
1620 if (!(eap->skip || error))
1621 clear_lval(&lv);
1622 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001623 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001624
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001625 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001626 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001627 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001628
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001629 if (!eap->skip)
1630 clear_lval(&lv);
1631 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001632
1633 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001634 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001635
Bram Moolenaar63b91732021-08-05 20:40:03 +02001636 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001637}
1638
1639 static int
1640do_unlet_var(
1641 lval_T *lp,
1642 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001643 exarg_T *eap,
1644 int deep UNUSED,
1645 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001646{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001647 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001648 int ret = OK;
1649 int cc;
1650
1651 if (lp->ll_tv == NULL)
1652 {
1653 cc = *name_end;
1654 *name_end = NUL;
1655
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001656 // Environment variable, normal name or expanded name.
1657 if (*lp->ll_name == '$')
1658 vim_unsetenv(lp->ll_name + 1);
1659 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001660 ret = FAIL;
1661 *name_end = cc;
1662 }
1663 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001664 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001665 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001666 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001667 return FAIL;
1668 else if (lp->ll_range)
1669 {
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001670 if (list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_name, lp->ll_n1,
1671 !lp->ll_empty2, lp->ll_n2) == FAIL)
1672 return FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001673 }
1674 else
1675 {
1676 if (lp->ll_list != NULL)
1677 // unlet a List item.
1678 listitem_remove(lp->ll_list, lp->ll_li);
1679 else
1680 // unlet a Dictionary item.
1681 dictitem_remove(lp->ll_dict, lp->ll_di);
1682 }
1683
1684 return ret;
1685}
1686
1687/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001688 * Unlet one item or a range of items from a list.
1689 * Return OK or FAIL.
1690 */
1691 int
1692list_unlet_range(
1693 list_T *l,
1694 listitem_T *li_first,
1695 char_u *name,
1696 long n1_arg,
1697 int has_n2,
1698 long n2)
1699{
1700 listitem_T *li = li_first;
1701 int n1 = n1_arg;
1702
1703 while (li != NULL && (!has_n2 || n2 >= n1))
1704 {
1705 if (value_check_lock(li->li_tv.v_lock, name, FALSE))
1706 return FAIL;
1707 li = li->li_next;
1708 ++n1;
1709 }
1710
1711 // Delete a range of List items.
1712 li = li_first;
1713 n1 = n1_arg;
1714 while (li != NULL && (!has_n2 || n2 >= n1))
1715 {
1716 listitem_T *next = li->li_next;
1717
1718 listitem_remove(l, li);
1719 li = next;
1720 ++n1;
1721 }
1722 return OK;
1723}
1724/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001725 * "unlet" a variable. Return OK if it existed, FAIL if not.
1726 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1727 */
1728 int
1729do_unlet(char_u *name, int forceit)
1730{
1731 hashtab_T *ht;
1732 hashitem_T *hi;
1733 char_u *varname;
1734 dict_T *d;
1735 dictitem_T *di;
1736
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001737 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001738 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001739 return FAIL;
1740
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001741 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001742
1743 // can't :unlet a script variable in Vim9 script from a function
1744 if (ht == get_script_local_ht()
1745 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1746 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1747 == SCRIPT_VERSION_VIM9
1748 && check_vim9_unlet(name) == FAIL)
1749 return FAIL;
1750
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001751 if (ht != NULL && *varname != NUL)
1752 {
1753 d = get_current_funccal_dict(ht);
1754 if (d == NULL)
1755 {
1756 if (ht == &globvarht)
1757 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001758 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001759 d = &vimvardict;
1760 else
1761 {
1762 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1763 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1764 }
1765 if (d == NULL)
1766 {
1767 internal_error("do_unlet()");
1768 return FAIL;
1769 }
1770 }
1771 hi = hash_find(ht, varname);
1772 if (HASHITEM_EMPTY(hi))
1773 hi = find_hi_in_scoped_ht(name, &ht);
1774 if (hi != NULL && !HASHITEM_EMPTY(hi))
1775 {
1776 di = HI2DI(hi);
1777 if (var_check_fixed(di->di_flags, name, FALSE)
1778 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001779 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001780 return FAIL;
1781
1782 delete_var(ht, hi);
1783 return OK;
1784 }
1785 }
1786 if (forceit)
1787 return OK;
1788 semsg(_("E108: No such variable: \"%s\""), name);
1789 return FAIL;
1790}
1791
1792/*
1793 * Lock or unlock variable indicated by "lp".
1794 * "deep" is the levels to go (-1 for unlimited);
1795 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1796 */
1797 static int
1798do_lock_var(
1799 lval_T *lp,
1800 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001801 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001802 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001803 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001804{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001805 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001806 int ret = OK;
1807 int cc;
1808 dictitem_T *di;
1809
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001810 if (lp->ll_tv == NULL)
1811 {
1812 cc = *name_end;
1813 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001814 if (*lp->ll_name == '$')
1815 {
1816 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001817 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001818 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001819 else
1820 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001821 // Normal name or expanded name.
1822 di = find_var(lp->ll_name, NULL, TRUE);
1823 if (di == NULL)
1824 ret = FAIL;
1825 else if ((di->di_flags & DI_FLAGS_FIX)
1826 && di->di_tv.v_type != VAR_DICT
1827 && di->di_tv.v_type != VAR_LIST)
1828 {
1829 // For historic reasons this error is not given for a list or
1830 // dict. E.g., the b: dict could be locked/unlocked.
1831 semsg(_(e_lock_unlock), lp->ll_name);
1832 ret = FAIL;
1833 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001834 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001835 {
1836 if (lock)
1837 di->di_flags |= DI_FLAGS_LOCK;
1838 else
1839 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001840 if (deep != 0)
1841 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001842 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001843 }
1844 *name_end = cc;
1845 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001846 else if (deep == 0)
1847 {
1848 // nothing to do
1849 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001850 else if (lp->ll_range)
1851 {
1852 listitem_T *li = lp->ll_li;
1853
1854 // (un)lock a range of List items.
1855 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1856 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001857 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001858 li = li->li_next;
1859 ++lp->ll_n1;
1860 }
1861 }
1862 else if (lp->ll_list != NULL)
1863 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001864 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001865 else
1866 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001867 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001868
1869 return ret;
1870}
1871
1872/*
1873 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001874 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1875 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001876 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001877 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001878item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001879{
1880 static int recurse = 0;
1881 list_T *l;
1882 listitem_T *li;
1883 dict_T *d;
1884 blob_T *b;
1885 hashitem_T *hi;
1886 int todo;
1887
1888 if (recurse >= DICT_MAXNEST)
1889 {
1890 emsg(_("E743: variable nested too deep for (un)lock"));
1891 return;
1892 }
1893 if (deep == 0)
1894 return;
1895 ++recurse;
1896
1897 // lock/unlock the item itself
1898 if (lock)
1899 tv->v_lock |= VAR_LOCKED;
1900 else
1901 tv->v_lock &= ~VAR_LOCKED;
1902
1903 switch (tv->v_type)
1904 {
1905 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001906 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001908 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001909 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001910 case VAR_STRING:
1911 case VAR_FUNC:
1912 case VAR_PARTIAL:
1913 case VAR_FLOAT:
1914 case VAR_SPECIAL:
1915 case VAR_JOB:
1916 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001917 case VAR_INSTR:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001918 break;
1919
1920 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001921 if ((b = tv->vval.v_blob) != NULL
1922 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001923 {
1924 if (lock)
1925 b->bv_lock |= VAR_LOCKED;
1926 else
1927 b->bv_lock &= ~VAR_LOCKED;
1928 }
1929 break;
1930 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001931 if ((l = tv->vval.v_list) != NULL
1932 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001933 {
1934 if (lock)
1935 l->lv_lock |= VAR_LOCKED;
1936 else
1937 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001938 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001939 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001940 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001941 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001942 }
1943 break;
1944 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001945 if ((d = tv->vval.v_dict) != NULL
1946 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001947 {
1948 if (lock)
1949 d->dv_lock |= VAR_LOCKED;
1950 else
1951 d->dv_lock &= ~VAR_LOCKED;
1952 if (deep < 0 || deep > 1)
1953 {
1954 // recursive: lock/unlock the items the List contains
1955 todo = (int)d->dv_hashtab.ht_used;
1956 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1957 {
1958 if (!HASHITEM_EMPTY(hi))
1959 {
1960 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001961 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1962 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001963 }
1964 }
1965 }
1966 }
1967 }
1968 --recurse;
1969}
1970
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001971#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1972/*
1973 * Delete all "menutrans_" variables.
1974 */
1975 void
1976del_menutrans_vars(void)
1977{
1978 hashitem_T *hi;
1979 int todo;
1980
1981 hash_lock(&globvarht);
1982 todo = (int)globvarht.ht_used;
1983 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1984 {
1985 if (!HASHITEM_EMPTY(hi))
1986 {
1987 --todo;
1988 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1989 delete_var(&globvarht, hi);
1990 }
1991 }
1992 hash_unlock(&globvarht);
1993}
1994#endif
1995
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001996/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001997 * Local string buffer for the next two functions to store a variable name
1998 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1999 * get_user_var_name().
2000 */
2001
2002static char_u *varnamebuf = NULL;
2003static int varnamebuflen = 0;
2004
2005/*
2006 * Function to concatenate a prefix and a variable name.
2007 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002008 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002009cat_prefix_varname(int prefix, char_u *name)
2010{
2011 int len;
2012
2013 len = (int)STRLEN(name) + 3;
2014 if (len > varnamebuflen)
2015 {
2016 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002017 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002018 varnamebuf = alloc(len);
2019 if (varnamebuf == NULL)
2020 {
2021 varnamebuflen = 0;
2022 return NULL;
2023 }
2024 varnamebuflen = len;
2025 }
2026 *varnamebuf = prefix;
2027 varnamebuf[1] = ':';
2028 STRCPY(varnamebuf + 2, name);
2029 return varnamebuf;
2030}
2031
2032/*
2033 * Function given to ExpandGeneric() to obtain the list of user defined
2034 * (global/buffer/window/built-in) variable names.
2035 */
2036 char_u *
2037get_user_var_name(expand_T *xp, int idx)
2038{
2039 static long_u gdone;
2040 static long_u bdone;
2041 static long_u wdone;
2042 static long_u tdone;
2043 static int vidx;
2044 static hashitem_T *hi;
2045 hashtab_T *ht;
2046
2047 if (idx == 0)
2048 {
2049 gdone = bdone = wdone = vidx = 0;
2050 tdone = 0;
2051 }
2052
2053 // Global variables
2054 if (gdone < globvarht.ht_used)
2055 {
2056 if (gdone++ == 0)
2057 hi = globvarht.ht_array;
2058 else
2059 ++hi;
2060 while (HASHITEM_EMPTY(hi))
2061 ++hi;
2062 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2063 return cat_prefix_varname('g', hi->hi_key);
2064 return hi->hi_key;
2065 }
2066
2067 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002068 ht =
2069#ifdef FEAT_CMDWIN
2070 // In cmdwin, the alternative buffer should be used.
2071 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2072 &prevwin->w_buffer->b_vars->dv_hashtab :
2073#endif
2074 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002075 if (bdone < ht->ht_used)
2076 {
2077 if (bdone++ == 0)
2078 hi = ht->ht_array;
2079 else
2080 ++hi;
2081 while (HASHITEM_EMPTY(hi))
2082 ++hi;
2083 return cat_prefix_varname('b', hi->hi_key);
2084 }
2085
2086 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002087 ht =
2088#ifdef FEAT_CMDWIN
2089 // In cmdwin, the alternative window should be used.
2090 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2091 &prevwin->w_vars->dv_hashtab :
2092#endif
2093 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002094 if (wdone < ht->ht_used)
2095 {
2096 if (wdone++ == 0)
2097 hi = ht->ht_array;
2098 else
2099 ++hi;
2100 while (HASHITEM_EMPTY(hi))
2101 ++hi;
2102 return cat_prefix_varname('w', hi->hi_key);
2103 }
2104
2105 // t: variables
2106 ht = &curtab->tp_vars->dv_hashtab;
2107 if (tdone < ht->ht_used)
2108 {
2109 if (tdone++ == 0)
2110 hi = ht->ht_array;
2111 else
2112 ++hi;
2113 while (HASHITEM_EMPTY(hi))
2114 ++hi;
2115 return cat_prefix_varname('t', hi->hi_key);
2116 }
2117
2118 // v: variables
2119 if (vidx < VV_LEN)
2120 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2121
2122 VIM_CLEAR(varnamebuf);
2123 varnamebuflen = 0;
2124 return NULL;
2125}
2126
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002127 char *
2128get_var_special_name(int nr)
2129{
2130 switch (nr)
2131 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002132 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2133 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002134 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002135 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002136 }
2137 internal_error("get_var_special_name()");
2138 return "42";
2139}
2140
2141/*
2142 * Returns the global variable dictionary
2143 */
2144 dict_T *
2145get_globvar_dict(void)
2146{
2147 return &globvardict;
2148}
2149
2150/*
2151 * Returns the global variable hash table
2152 */
2153 hashtab_T *
2154get_globvar_ht(void)
2155{
2156 return &globvarht;
2157}
2158
2159/*
2160 * Returns the v: variable dictionary
2161 */
2162 dict_T *
2163get_vimvar_dict(void)
2164{
2165 return &vimvardict;
2166}
2167
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002168/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002170 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171 */
2172 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002173find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002174{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002175 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2176 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177
2178 if (di == NULL)
2179 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002180 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2182 return (int)(vv - vimvars);
2183}
2184
2185
2186/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002187 * Set type of v: variable to "type".
2188 */
2189 void
2190set_vim_var_type(int idx, vartype_T type)
2191{
2192 vimvars[idx].vv_type = type;
2193}
2194
2195/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002196 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002197 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002198 */
2199 void
2200set_vim_var_nr(int idx, varnumber_T val)
2201{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002202 vimvars[idx].vv_nr = val;
2203}
2204
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205 char *
2206get_vim_var_name(int idx)
2207{
2208 return vimvars[idx].vv_name;
2209}
2210
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002211/*
2212 * Get typval_T v: variable value.
2213 */
2214 typval_T *
2215get_vim_var_tv(int idx)
2216{
2217 return &vimvars[idx].vv_tv;
2218}
2219
2220/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002221 * Set v: variable to "tv". Only accepts the same type.
2222 * Takes over the value of "tv".
2223 */
2224 int
2225set_vim_var_tv(int idx, typval_T *tv)
2226{
2227 if (vimvars[idx].vv_type != tv->v_type)
2228 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002229 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002230 clear_tv(tv);
2231 return FAIL;
2232 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002233 // VV_RO is also checked when compiling, but let's check here as well.
2234 if (vimvars[idx].vv_flags & VV_RO)
2235 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002236 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002237 return FAIL;
2238 }
2239 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2240 {
2241 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2242 return FAIL;
2243 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002244 clear_tv(&vimvars[idx].vv_di.di_tv);
2245 vimvars[idx].vv_di.di_tv = *tv;
2246 return OK;
2247}
2248
2249/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002250 * Get number v: variable value.
2251 */
2252 varnumber_T
2253get_vim_var_nr(int idx)
2254{
2255 return vimvars[idx].vv_nr;
2256}
2257
2258/*
2259 * Get string v: variable value. Uses a static buffer, can only be used once.
2260 * If the String variable has never been set, return an empty string.
2261 * Never returns NULL;
2262 */
2263 char_u *
2264get_vim_var_str(int idx)
2265{
2266 return tv_get_string(&vimvars[idx].vv_tv);
2267}
2268
2269/*
2270 * Get List v: variable value. Caller must take care of reference count when
2271 * needed.
2272 */
2273 list_T *
2274get_vim_var_list(int idx)
2275{
2276 return vimvars[idx].vv_list;
2277}
2278
2279/*
2280 * Get Dict v: variable value. Caller must take care of reference count when
2281 * needed.
2282 */
2283 dict_T *
2284get_vim_var_dict(int idx)
2285{
2286 return vimvars[idx].vv_dict;
2287}
2288
2289/*
2290 * Set v:char to character "c".
2291 */
2292 void
2293set_vim_var_char(int c)
2294{
2295 char_u buf[MB_MAXBYTES + 1];
2296
2297 if (has_mbyte)
2298 buf[(*mb_char2bytes)(c, buf)] = NUL;
2299 else
2300 {
2301 buf[0] = c;
2302 buf[1] = NUL;
2303 }
2304 set_vim_var_string(VV_CHAR, buf, -1);
2305}
2306
2307/*
2308 * Set v:count to "count" and v:count1 to "count1".
2309 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2310 */
2311 void
2312set_vcount(
2313 long count,
2314 long count1,
2315 int set_prevcount)
2316{
2317 if (set_prevcount)
2318 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2319 vimvars[VV_COUNT].vv_nr = count;
2320 vimvars[VV_COUNT1].vv_nr = count1;
2321}
2322
2323/*
2324 * Save variables that might be changed as a side effect. Used when executing
2325 * a timer callback.
2326 */
2327 void
2328save_vimvars(vimvars_save_T *vvsave)
2329{
2330 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2331 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2332 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2333}
2334
2335/*
2336 * Restore variables saved by save_vimvars().
2337 */
2338 void
2339restore_vimvars(vimvars_save_T *vvsave)
2340{
2341 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2342 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2343 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2344}
2345
2346/*
2347 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2348 * value.
2349 */
2350 void
2351set_vim_var_string(
2352 int idx,
2353 char_u *val,
2354 int len) // length of "val" to use or -1 (whole string)
2355{
2356 clear_tv(&vimvars[idx].vv_di.di_tv);
2357 vimvars[idx].vv_type = VAR_STRING;
2358 if (val == NULL)
2359 vimvars[idx].vv_str = NULL;
2360 else if (len == -1)
2361 vimvars[idx].vv_str = vim_strsave(val);
2362 else
2363 vimvars[idx].vv_str = vim_strnsave(val, len);
2364}
2365
2366/*
2367 * Set List v: variable to "val".
2368 */
2369 void
2370set_vim_var_list(int idx, list_T *val)
2371{
2372 clear_tv(&vimvars[idx].vv_di.di_tv);
2373 vimvars[idx].vv_type = VAR_LIST;
2374 vimvars[idx].vv_list = val;
2375 if (val != NULL)
2376 ++val->lv_refcount;
2377}
2378
2379/*
2380 * Set Dictionary v: variable to "val".
2381 */
2382 void
2383set_vim_var_dict(int idx, dict_T *val)
2384{
2385 clear_tv(&vimvars[idx].vv_di.di_tv);
2386 vimvars[idx].vv_type = VAR_DICT;
2387 vimvars[idx].vv_dict = val;
2388 if (val != NULL)
2389 {
2390 ++val->dv_refcount;
2391 dict_set_items_ro(val);
2392 }
2393}
2394
2395/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002396 * Set the v:argv list.
2397 */
2398 void
2399set_argv_var(char **argv, int argc)
2400{
2401 list_T *l = list_alloc();
2402 int i;
2403
2404 if (l == NULL)
2405 getout(1);
2406 l->lv_lock = VAR_FIXED;
2407 for (i = 0; i < argc; ++i)
2408 {
2409 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2410 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002411 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002412 }
2413 set_vim_var_list(VV_ARGV, l);
2414}
2415
2416/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002417 * Reset v:register, taking the 'clipboard' setting into account.
2418 */
2419 void
2420reset_reg_var(void)
2421{
2422 int regname = 0;
2423
2424 // Adjust the register according to 'clipboard', so that when
2425 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2426#ifdef FEAT_CLIPBOARD
2427 adjust_clip_reg(&regname);
2428#endif
2429 set_reg_var(regname);
2430}
2431
2432/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002433 * Set v:register if needed.
2434 */
2435 void
2436set_reg_var(int c)
2437{
2438 char_u regname;
2439
2440 if (c == 0 || c == ' ')
2441 regname = '"';
2442 else
2443 regname = c;
2444 // Avoid free/alloc when the value is already right.
2445 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2446 set_vim_var_string(VV_REG, &regname, 1);
2447}
2448
2449/*
2450 * Get or set v:exception. If "oldval" == NULL, return the current value.
2451 * Otherwise, restore the value to "oldval" and return NULL.
2452 * Must always be called in pairs to save and restore v:exception! Does not
2453 * take care of memory allocations.
2454 */
2455 char_u *
2456v_exception(char_u *oldval)
2457{
2458 if (oldval == NULL)
2459 return vimvars[VV_EXCEPTION].vv_str;
2460
2461 vimvars[VV_EXCEPTION].vv_str = oldval;
2462 return NULL;
2463}
2464
2465/*
2466 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2467 * Otherwise, restore the value to "oldval" and return NULL.
2468 * Must always be called in pairs to save and restore v:throwpoint! Does not
2469 * take care of memory allocations.
2470 */
2471 char_u *
2472v_throwpoint(char_u *oldval)
2473{
2474 if (oldval == NULL)
2475 return vimvars[VV_THROWPOINT].vv_str;
2476
2477 vimvars[VV_THROWPOINT].vv_str = oldval;
2478 return NULL;
2479}
2480
2481/*
2482 * Set v:cmdarg.
2483 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2484 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2485 * Must always be called in pairs!
2486 */
2487 char_u *
2488set_cmdarg(exarg_T *eap, char_u *oldarg)
2489{
2490 char_u *oldval;
2491 char_u *newval;
2492 unsigned len;
2493
2494 oldval = vimvars[VV_CMDARG].vv_str;
2495 if (eap == NULL)
2496 {
2497 vim_free(oldval);
2498 vimvars[VV_CMDARG].vv_str = oldarg;
2499 return NULL;
2500 }
2501
2502 if (eap->force_bin == FORCE_BIN)
2503 len = 6;
2504 else if (eap->force_bin == FORCE_NOBIN)
2505 len = 8;
2506 else
2507 len = 0;
2508
2509 if (eap->read_edit)
2510 len += 7;
2511
2512 if (eap->force_ff != 0)
2513 len += 10; // " ++ff=unix"
2514 if (eap->force_enc != 0)
2515 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2516 if (eap->bad_char != 0)
2517 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2518
2519 newval = alloc(len + 1);
2520 if (newval == NULL)
2521 return NULL;
2522
2523 if (eap->force_bin == FORCE_BIN)
2524 sprintf((char *)newval, " ++bin");
2525 else if (eap->force_bin == FORCE_NOBIN)
2526 sprintf((char *)newval, " ++nobin");
2527 else
2528 *newval = NUL;
2529
2530 if (eap->read_edit)
2531 STRCAT(newval, " ++edit");
2532
2533 if (eap->force_ff != 0)
2534 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2535 eap->force_ff == 'u' ? "unix"
2536 : eap->force_ff == 'd' ? "dos"
2537 : "mac");
2538 if (eap->force_enc != 0)
2539 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2540 eap->cmd + eap->force_enc);
2541 if (eap->bad_char == BAD_KEEP)
2542 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2543 else if (eap->bad_char == BAD_DROP)
2544 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2545 else if (eap->bad_char != 0)
2546 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2547 vimvars[VV_CMDARG].vv_str = newval;
2548 return oldval;
2549}
2550
2551/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002552 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002553 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2554 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002555 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2556 */
2557 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002558eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002559 char_u *name,
2560 int len, // length of "name"
2561 typval_T *rettv, // NULL when only checking existence
2562 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002563 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002564{
2565 int ret = OK;
2566 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002567 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002568 hashtab_T *ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002569 int cc;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002570 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002571
2572 // truncate the name, so that we can use strcmp()
2573 cc = name[len];
2574 name[len] = NUL;
2575
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002576 // Check for local variable when debugging.
2577 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002578 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002579 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002580 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2581
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002582 if (v != NULL)
2583 {
2584 tv = &v->di_tv;
2585 if (dip != NULL)
2586 *dip = v;
2587 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002588 else
2589 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002590 }
2591
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002592 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002594 imported_T *import;
2595 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2596
2597 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598
2599 // imported variable from another script
2600 if (import != NULL)
2601 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002602 if (import->imp_funcname != NULL)
2603 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002604 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002605 if (rettv != NULL)
2606 {
2607 rettv->v_type = VAR_FUNC;
2608 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2609 }
2610 }
Bram Moolenaara6294952020-12-27 13:39:50 +01002611 else if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002612 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002613 if ((flags & EVAL_VAR_IMPORT) == 0)
2614 {
2615 if (flags & EVAL_VAR_VERBOSE)
2616 emsg(_(e_import_as_name_not_supported_here));
2617 ret = FAIL;
2618 }
2619 else
2620 {
2621 if (rettv != NULL)
2622 {
2623 rettv->v_type = VAR_ANY;
2624 rettv->vval.v_number = import->imp_sid;
2625 }
2626 found = TRUE;
2627 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002628 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002629 else
2630 {
2631 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002632 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002634 tv = sv->sv_tv;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002635 type = sv->sv_type;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002638 else if (in_vim9script())
2639 {
2640 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2641
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002642 // In Vim9 script we can get a function reference by using the
2643 // function name.
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002644 if (ufunc != NULL)
2645 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002646 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002647 if (rettv != NULL)
2648 {
2649 rettv->v_type = VAR_FUNC;
2650 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002651 if (rettv->vval.v_string != NULL)
2652 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002653 }
2654 }
2655 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002656 }
2657
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002658 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002659 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002660 if (tv == NULL)
2661 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002662 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002663 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002664 ret = FAIL;
2665 }
2666 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002667 {
Bram Moolenaar11005b02021-07-11 20:59:00 +02002668 if (ht != NULL && ht == get_script_local_ht()
2669 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002670 {
Bram Moolenaarc967d572021-07-08 21:38:50 +02002671 svar_T *sv = find_typval_in_script(tv);
Bram Moolenaarf055d452021-07-08 20:57:24 +02002672
Bram Moolenaarf055d452021-07-08 20:57:24 +02002673 if (sv != NULL)
2674 type = sv->sv_type;
2675 }
2676
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002677 // If a list or dict variable wasn't initialized, do it now.
2678 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2679 {
2680 tv->vval.v_dict = dict_alloc();
2681 if (tv->vval.v_dict != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002682 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002683 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002684 tv->vval.v_dict->dv_type = alloc_type(type);
2685 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002686 }
2687 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2688 {
2689 tv->vval.v_list = list_alloc();
2690 if (tv->vval.v_list != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002691 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002692 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002693 tv->vval.v_list->lv_type = alloc_type(type);
2694 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002695 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002696 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL)
2697 {
2698 tv->vval.v_blob = blob_alloc();
2699 if (tv->vval.v_blob != NULL)
2700 ++tv->vval.v_blob->bv_refcount;
2701 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002702 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002703 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002704 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002705
2706 name[len] = cc;
2707
2708 return ret;
2709}
2710
2711/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002712 * Check if variable "name[len]" is a local variable or an argument.
2713 * If so, "*eval_lavars_used" is set to TRUE.
2714 */
2715 void
2716check_vars(char_u *name, int len)
2717{
2718 int cc;
2719 char_u *varname;
2720 hashtab_T *ht;
2721
2722 if (eval_lavars_used == NULL)
2723 return;
2724
2725 // truncate the name, so that we can use strcmp()
2726 cc = name[len];
2727 name[len] = NUL;
2728
2729 ht = find_var_ht(name, &varname);
2730 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2731 {
2732 if (find_var(name, NULL, TRUE) != NULL)
2733 *eval_lavars_used = TRUE;
2734 }
2735
2736 name[len] = cc;
2737}
2738
2739/*
2740 * Find variable "name" in the list of variables.
2741 * Return a pointer to it if found, NULL if not found.
2742 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002743 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002744 */
2745 dictitem_T *
2746find_var(char_u *name, hashtab_T **htp, int no_autoload)
2747{
2748 char_u *varname;
2749 hashtab_T *ht;
2750 dictitem_T *ret = NULL;
2751
2752 ht = find_var_ht(name, &varname);
2753 if (htp != NULL)
2754 *htp = ht;
2755 if (ht == NULL)
2756 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002757 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002758 if (ret != NULL)
2759 return ret;
2760
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002761 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002762 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002763 if (ret != NULL)
2764 return ret;
2765
2766 // in Vim9 script items without a scope can be script-local
2767 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2768 {
2769 ht = get_script_local_ht();
2770 if (ht != NULL)
2771 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002772 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002773 if (ret != NULL)
2774 {
2775 if (htp != NULL)
2776 *htp = ht;
2777 return ret;
2778 }
2779 }
2780 }
2781
2782 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002783}
2784
2785/*
2786 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002787 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002788 * Returns NULL if not found.
2789 */
2790 dictitem_T *
2791find_var_in_ht(
2792 hashtab_T *ht,
2793 int htname,
2794 char_u *varname,
2795 int no_autoload)
2796{
2797 hashitem_T *hi;
2798
2799 if (*varname == NUL)
2800 {
2801 // Must be something like "s:", otherwise "ht" would be NULL.
2802 switch (htname)
2803 {
2804 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2805 case 'g': return &globvars_var;
2806 case 'v': return &vimvars_var;
2807 case 'b': return &curbuf->b_bufvar;
2808 case 'w': return &curwin->w_winvar;
2809 case 't': return &curtab->tp_winvar;
2810 case 'l': return get_funccal_local_var();
2811 case 'a': return get_funccal_args_var();
2812 }
2813 return NULL;
2814 }
2815
2816 hi = hash_find(ht, varname);
2817 if (HASHITEM_EMPTY(hi))
2818 {
2819 // For global variables we may try auto-loading the script. If it
2820 // worked find the variable again. Don't auto-load a script if it was
2821 // loaded already, otherwise it would be loaded every time when
2822 // checking if a function name is a Funcref variable.
2823 if (ht == &globvarht && !no_autoload)
2824 {
2825 // Note: script_autoload() may make "hi" invalid. It must either
2826 // be obtained again or not used.
2827 if (!script_autoload(varname, FALSE) || aborting())
2828 return NULL;
2829 hi = hash_find(ht, varname);
2830 }
2831 if (HASHITEM_EMPTY(hi))
2832 return NULL;
2833 }
2834 return HI2DI(hi);
2835}
2836
2837/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 * Get the script-local hashtab. NULL if not in a script context.
2839 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002840 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841get_script_local_ht(void)
2842{
2843 scid_T sid = current_sctx.sc_sid;
2844
Bram Moolenaare3d46852020-08-29 13:39:17 +02002845 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 return &SCRIPT_VARS(sid);
2847 return NULL;
2848}
2849
2850/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002851 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002852 * When "cmd" is TRUE it must look like a command, a function must be followed
2853 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01002854 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01002856 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002857lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01002858 char_u *name,
2859 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002860 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01002861 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862{
2863 hashtab_T *ht = get_script_local_ht();
2864 char_u buffer[30];
2865 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01002866 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002868 int is_global = FALSE;
2869 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870
2871 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002872 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873 if (len < sizeof(buffer) - 1)
2874 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002875 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876 vim_strncpy(buffer, name, len);
2877 p = buffer;
2878 }
2879 else
2880 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002881 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002883 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884 }
2885
2886 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002887 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888
2889 // if not script-local, then perhaps imported
Bram Moolenaar709664c2020-12-12 14:33:41 +01002890 if (res == FAIL && find_imported(p, 0, NULL) != NULL)
2891 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 if (p != buffer)
2893 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002894
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002895 // Find a function, so that a following "->" works.
2896 // When used as a command require "(" or "->" to follow, "Cmd" is a user
2897 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002898 if (res != OK)
2899 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002900 p = skipwhite(name + len);
2901
2902 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002903 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002904 // Do not check for an internal function, since it might also be a
2905 // valid command, such as ":split" versus "split()".
2906 // Skip "g:" before a function name.
2907 if (name[0] == 'g' && name[1] == ':')
2908 {
2909 is_global = TRUE;
2910 fname = name + 2;
2911 }
2912 if (find_func(fname, is_global, NULL) != NULL)
2913 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002914 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002915 }
2916
Bram Moolenaar709664c2020-12-12 14:33:41 +01002917 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918}
2919
2920/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002921 * Find the hashtab used for a variable name.
2922 * Return NULL if the name is not valid.
2923 * Set "varname" to the start of name without ':'.
2924 */
2925 hashtab_T *
2926find_var_ht(char_u *name, char_u **varname)
2927{
2928 hashitem_T *hi;
2929 hashtab_T *ht;
2930
2931 if (name[0] == NUL)
2932 return NULL;
2933 if (name[1] != ':')
2934 {
2935 // The name must not start with a colon or #.
2936 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2937 return NULL;
2938 *varname = name;
2939
2940 // "version" is "v:version" in all scopes if scriptversion < 3.
2941 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02002942 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002943 {
2944 hi = hash_find(&compat_hashtab, name);
2945 if (!HASHITEM_EMPTY(hi))
2946 return &compat_hashtab;
2947 }
2948
2949 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950 if (ht != NULL)
2951 return ht; // local variable
2952
Bram Moolenaarf0a40692021-06-11 22:05:47 +02002953 // In Vim9 script items at the script level are script-local, except
2954 // for autoload names.
2955 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 {
2957 ht = get_script_local_ht();
2958 if (ht != NULL)
2959 return ht;
2960 }
2961
2962 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002963 }
2964 *varname = name + 2;
2965 if (*name == 'g') // global variable
2966 return &globvarht;
2967 // There must be no ':' or '#' in the rest of the name, unless g: is used
2968 if (vim_strchr(name + 2, ':') != NULL
2969 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2970 return NULL;
2971 if (*name == 'b') // buffer variable
2972 return &curbuf->b_vars->dv_hashtab;
2973 if (*name == 'w') // window variable
2974 return &curwin->w_vars->dv_hashtab;
2975 if (*name == 't') // tab page variable
2976 return &curtab->tp_vars->dv_hashtab;
2977 if (*name == 'v') // v: variable
2978 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002979 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002980 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002982 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 if (*name == 'a') // a: function argument
2984 return get_funccal_args_ht();
2985 if (*name == 'l') // l: local function variable
2986 return get_funccal_local_ht();
2987 }
2988 if (*name == 's') // script variable
2989 {
2990 ht = get_script_local_ht();
2991 if (ht != NULL)
2992 return ht;
2993 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002994 return NULL;
2995}
2996
2997/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002998 * Get the string value of a (global/local) variable.
2999 * Note: see tv_get_string() for how long the pointer remains valid.
3000 * Returns NULL when it doesn't exist.
3001 */
3002 char_u *
3003get_var_value(char_u *name)
3004{
3005 dictitem_T *v;
3006
3007 v = find_var(name, NULL, FALSE);
3008 if (v == NULL)
3009 return NULL;
3010 return tv_get_string(&v->di_tv);
3011}
3012
3013/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003014 * Allocate a new hashtab for a sourced script. It will be used while
3015 * sourcing this script and when executing functions defined in the script.
3016 */
3017 void
3018new_script_vars(scid_T id)
3019{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003020 scriptvar_T *sv;
3021
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003022 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3023 if (sv == NULL)
3024 return;
3025 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003026 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003027}
3028
3029/*
3030 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3031 * point to it.
3032 */
3033 void
3034init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3035{
3036 hash_init(&dict->dv_hashtab);
3037 dict->dv_lock = 0;
3038 dict->dv_scope = scope;
3039 dict->dv_refcount = DO_NOT_FREE_CNT;
3040 dict->dv_copyID = 0;
3041 dict_var->di_tv.vval.v_dict = dict;
3042 dict_var->di_tv.v_type = VAR_DICT;
3043 dict_var->di_tv.v_lock = VAR_FIXED;
3044 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3045 dict_var->di_key[0] = NUL;
3046}
3047
3048/*
3049 * Unreference a dictionary initialized by init_var_dict().
3050 */
3051 void
3052unref_var_dict(dict_T *dict)
3053{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003054 // Now the dict needs to be freed if no one else is using it, go back to
3055 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003056 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3057 dict_unref(dict);
3058}
3059
3060/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003061 * Clean up a list of internal variables.
3062 * Frees all allocated variables and the value they contain.
3063 * Clears hashtab "ht", does not free it.
3064 */
3065 void
3066vars_clear(hashtab_T *ht)
3067{
3068 vars_clear_ext(ht, TRUE);
3069}
3070
3071/*
3072 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3073 */
3074 void
3075vars_clear_ext(hashtab_T *ht, int free_val)
3076{
3077 int todo;
3078 hashitem_T *hi;
3079 dictitem_T *v;
3080
3081 hash_lock(ht);
3082 todo = (int)ht->ht_used;
3083 for (hi = ht->ht_array; todo > 0; ++hi)
3084 {
3085 if (!HASHITEM_EMPTY(hi))
3086 {
3087 --todo;
3088
3089 // Free the variable. Don't remove it from the hashtab,
3090 // ht_array might change then. hash_clear() takes care of it
3091 // later.
3092 v = HI2DI(hi);
3093 if (free_val)
3094 clear_tv(&v->di_tv);
3095 if (v->di_flags & DI_FLAGS_ALLOC)
3096 vim_free(v);
3097 }
3098 }
3099 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003100 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003101}
3102
3103/*
3104 * Delete a variable from hashtab "ht" at item "hi".
3105 * Clear the variable value and free the dictitem.
3106 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003107 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003108delete_var(hashtab_T *ht, hashitem_T *hi)
3109{
3110 dictitem_T *di = HI2DI(hi);
3111
3112 hash_remove(ht, hi);
3113 clear_tv(&di->di_tv);
3114 vim_free(di);
3115}
3116
3117/*
3118 * List the value of one internal variable.
3119 */
3120 static void
3121list_one_var(dictitem_T *v, char *prefix, int *first)
3122{
3123 char_u *tofree;
3124 char_u *s;
3125 char_u numbuf[NUMBUFLEN];
3126
3127 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3128 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3129 s == NULL ? (char_u *)"" : s, first);
3130 vim_free(tofree);
3131}
3132
3133 static void
3134list_one_var_a(
3135 char *prefix,
3136 char_u *name,
3137 int type,
3138 char_u *string,
3139 int *first) // when TRUE clear rest of screen and set to FALSE
3140{
3141 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3142 msg_start();
3143 msg_puts(prefix);
3144 if (name != NULL) // "a:" vars don't have a name stored
3145 msg_puts((char *)name);
3146 msg_putchar(' ');
3147 msg_advance(22);
3148 if (type == VAR_NUMBER)
3149 msg_putchar('#');
3150 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3151 msg_putchar('*');
3152 else if (type == VAR_LIST)
3153 {
3154 msg_putchar('[');
3155 if (*string == '[')
3156 ++string;
3157 }
3158 else if (type == VAR_DICT)
3159 {
3160 msg_putchar('{');
3161 if (*string == '{')
3162 ++string;
3163 }
3164 else
3165 msg_putchar(' ');
3166
3167 msg_outtrans(string);
3168
3169 if (type == VAR_FUNC || type == VAR_PARTIAL)
3170 msg_puts("()");
3171 if (*first)
3172 {
3173 msg_clr_eos();
3174 *first = FALSE;
3175 }
3176}
3177
3178/*
3179 * Set variable "name" to value in "tv".
3180 * If the variable already exists, the value is updated.
3181 * Otherwise the variable is created.
3182 */
3183 void
3184set_var(
3185 char_u *name,
3186 typval_T *tv,
3187 int copy) // make copy of value in "tv"
3188{
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003189 set_var_const(name, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003190}
3191
3192/*
3193 * Set variable "name" to value in "tv".
3194 * If the variable already exists and "is_const" is FALSE the value is updated.
3195 * Otherwise the variable is created.
3196 */
3197 void
3198set_var_const(
3199 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003201 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003202 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003203 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003204 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003205{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003206 typval_T *tv = tv_arg;
3207 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 dictitem_T *di;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003209 typval_T *dest_tv = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003210 char_u *varname;
3211 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003213 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003214 int var_in_vim9script;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003215 int flags = flags_arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003216
3217 ht = find_var_ht(name, &varname);
3218 if (ht == NULL || *varname == NUL)
3219 {
3220 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003221 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003222 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003223 is_script_local = ht == get_script_local_ht();
3224
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003225 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003226 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003227 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003228 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003229 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003230 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003231 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003232 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003233 }
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003234 if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
3235 && vim_strchr((char_u *)"gwbt", name[0]) != NULL)
3236 // Do not make g:var, w:var, b:var or t:var final.
3237 flags &= ~ASSIGN_FINAL;
3238
Bram Moolenaare535db82021-03-31 21:07:24 +02003239 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003240 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3241 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003242 // For "[a, _] = list" the underscore is ignored.
3243 if ((flags & ASSIGN_UNPACK) == 0)
3244 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003245 goto failed;
3246 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003247
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003249
Bram Moolenaar24e93162021-07-18 20:40:33 +02003250 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003251 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003252 imported_T *import = find_imported(varname, 0, NULL);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003253
Bram Moolenaar24e93162021-07-18 20:40:33 +02003254 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003255 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003256 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
3257 svar_T *sv;
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003258 where_T where = WHERE_INIT;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003259
3260 // imported variable from another script
3261 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003263 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003264 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 }
Bram Moolenaar6853c382021-09-07 22:12:19 +02003266 if (import->imp_flags & IMP_FLAGS_STAR)
3267 {
3268 semsg(_(e_cannot_use_str_itself_it_is_imported_with_star),
3269 name);
3270 goto failed;
3271 }
Bram Moolenaar60579352021-07-19 21:45:07 +02003272 sv = ((svar_T *)si->sn_var_vals.ga_data) + import->imp_var_vals_idx;
3273
Bram Moolenaar60579352021-07-19 21:45:07 +02003274 where.wt_variable = TRUE;
3275 if (check_typval_type(sv->sv_type, tv, where) == FAIL
3276 || value_check_lock(sv->sv_tv->v_lock, name, FALSE))
3277 {
3278 goto failed;
3279 }
3280
Bram Moolenaar24e93162021-07-18 20:40:33 +02003281 dest_tv = sv->sv_tv;
Bram Moolenaarf6488542021-07-18 21:24:50 +02003282 clear_tv(dest_tv);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003283 }
3284 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285
Bram Moolenaar24e93162021-07-18 20:40:33 +02003286 if (dest_tv == NULL)
3287 {
3288 // Search in parent scope which is possible to reference from lambda
3289 if (di == NULL)
3290 di = find_var_in_scoped_ht(name, TRUE);
3291
3292 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003293 && var_wrong_func_name(name, di == NULL))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003294 goto failed;
3295
3296 if (need_convert_to_bool(type, tv))
3297 {
Bram Moolenaarf6488542021-07-18 21:24:50 +02003298 // Destination is a bool and the value is not, but it can be
3299 // converted.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003300 CLEAR_FIELD(bool_tv);
3301 bool_tv.v_type = VAR_BOOL;
3302 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3303 tv = &bool_tv;
3304 }
3305
3306 if (di != NULL)
3307 {
3308 // Item already exists. Allowed to replace when reloading.
3309 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
3310 {
3311 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaarf6488542021-07-18 21:24:50 +02003312 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003313 {
3314 emsg(_(e_cannot_mod));
3315 goto failed;
3316 }
3317
3318 if (is_script_local && vim9script
Bram Moolenaarf6488542021-07-18 21:24:50 +02003319 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003320 {
3321 semsg(_(e_redefining_script_item_str), name);
3322 goto failed;
3323 }
3324
Bram Moolenaara06758d2021-10-15 00:18:37 +01003325 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003326 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003327 where_T where = WHERE_INIT;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003328
3329 // check the type and adjust to bool if needed
3330 where.wt_index = var_idx;
3331 where.wt_variable = TRUE;
Bram Moolenaarf6488542021-07-18 21:24:50 +02003332 if (check_script_var_type(&di->di_tv, tv, name, where)
3333 == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003334 goto failed;
3335 }
3336
Bram Moolenaara06758d2021-10-15 00:18:37 +01003337 if ((flags & ASSIGN_FOR_LOOP) == 0
3338 && var_check_permission(di, name) == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003339 goto failed;
3340 }
3341 else
3342 {
3343 // can only redefine once
3344 di->di_flags &= ~DI_FLAGS_RELOAD;
3345
Bram Moolenaarf6488542021-07-18 21:24:50 +02003346 // A Vim9 script-local variable is also present in sn_all_vars
3347 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaar24e93162021-07-18 20:40:33 +02003348 if (var_in_vim9script)
3349 update_vim9_script_var(FALSE, di, flags, tv, &type,
Bram Moolenaarf6488542021-07-18 21:24:50 +02003350 (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003351 }
3352
3353 // existing variable, need to clear the value
3354
3355 // Handle setting internal di: variables separately where needed to
3356 // prevent changing the type.
3357 if (ht == &vimvarht)
3358 {
3359 if (di->di_tv.v_type == VAR_STRING)
3360 {
3361 VIM_CLEAR(di->di_tv.vval.v_string);
3362 if (copy || tv->v_type != VAR_STRING)
3363 {
3364 char_u *val = tv_get_string(tv);
3365
Bram Moolenaarf6488542021-07-18 21:24:50 +02003366 // Careful: when assigning to v:errmsg and
3367 // tv_get_string() causes an error message the variable
3368 // will already be set.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003369 if (di->di_tv.vval.v_string == NULL)
3370 di->di_tv.vval.v_string = vim_strsave(val);
3371 }
3372 else
3373 {
3374 // Take over the string to avoid an extra alloc/free.
3375 di->di_tv.vval.v_string = tv->vval.v_string;
3376 tv->vval.v_string = NULL;
3377 }
3378 goto failed;
3379 }
3380 else if (di->di_tv.v_type == VAR_NUMBER)
3381 {
3382 di->di_tv.vval.v_number = tv_get_number(tv);
3383 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003384 set_search_direction(di->di_tv.vval.v_number
3385 ? '/' : '?');
Bram Moolenaar24e93162021-07-18 20:40:33 +02003386#ifdef FEAT_SEARCH_EXTRA
3387 else if (STRCMP(varname, "hlsearch") == 0)
3388 {
3389 no_hlsearch = !di->di_tv.vval.v_number;
3390 redraw_all_later(SOME_VALID);
3391 }
3392#endif
3393 goto failed;
3394 }
3395 else if (di->di_tv.v_type != tv->v_type)
3396 {
3397 semsg(_("E963: setting %s to value with wrong type"), name);
3398 goto failed;
3399 }
3400 }
3401
3402 clear_tv(&di->di_tv);
3403 }
3404 else
3405 {
3406 // Item not found, check if a function already exists.
3407 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaarf6488542021-07-18 21:24:50 +02003408 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003409 {
3410 semsg(_(e_redefining_script_item_str), name);
3411 goto failed;
3412 }
3413
Bram Moolenaar24e93162021-07-18 20:40:33 +02003414 // add a new variable
3415 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3416 {
3417 semsg(_(e_unknown_variable_str), name);
3418 goto failed;
3419 }
3420
3421 // Can't add "v:" or "a:" variable.
3422 if (ht == &vimvarht || ht == get_funccal_args_ht())
3423 {
3424 semsg(_(e_illvar), name);
3425 goto failed;
3426 }
3427
3428 // Make sure the variable name is valid. In Vim9 script an autoload
3429 // variable must be prefixed with "g:".
3430 if (!valid_varname(varname, !vim9script
Bram Moolenaarf6488542021-07-18 21:24:50 +02003431 || STRNCMP(name, "g:", 2) == 0))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003432 goto failed;
3433
3434 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3435 if (di == NULL)
3436 goto failed;
3437 STRCPY(di->di_key, varname);
3438 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
3439 {
3440 vim_free(di);
3441 goto failed;
3442 }
3443 di->di_flags = DI_FLAGS_ALLOC;
3444 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3445 di->di_flags |= DI_FLAGS_LOCK;
3446
3447 // A Vim9 script-local variable is also added to sn_all_vars and
3448 // sn_var_vals. It may set "type" from "tv".
Bram Moolenaare535db82021-03-31 21:07:24 +02003449 if (var_in_vim9script)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003450 update_vim9_script_var(TRUE, di, flags, tv, &type,
Bram Moolenaarf6488542021-07-18 21:24:50 +02003451 (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003452 }
3453
Bram Moolenaar24e93162021-07-18 20:40:33 +02003454 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003455 }
3456
3457 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003458 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003459 else
3460 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003461 *dest_tv = *tv;
3462 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003463 init_tv(tv);
3464 }
3465
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003466 if (vim9script && type != NULL)
3467 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003468 if (type->tt_type == VAR_DICT && dest_tv->vval.v_dict != NULL)
Bram Moolenaar464393a2021-09-11 23:07:44 +02003469 {
3470 if (dest_tv->vval.v_dict->dv_type != type)
3471 {
3472 free_type(dest_tv->vval.v_dict->dv_type);
3473 dest_tv->vval.v_dict->dv_type = alloc_type(type);
3474 }
3475 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003476 else if (type->tt_type == VAR_LIST && dest_tv->vval.v_list != NULL)
Bram Moolenaar464393a2021-09-11 23:07:44 +02003477 {
3478 if (dest_tv->vval.v_list->lv_type != type)
3479 {
3480 free_type(dest_tv->vval.v_list->lv_type);
3481 dest_tv->vval.v_list->lv_type = alloc_type(type);
3482 }
3483 }
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003484 }
3485
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003486 // ":const var = value" locks the value
3487 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003488 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003489 // Like :lockvar! name: lock the value and what it contains, but only
3490 // if the reference count is up to one. That locks only literal
3491 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003492 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003493 return;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003494
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003495failed:
3496 if (!copy)
3497 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003498}
3499
3500/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003501 * Check in this order for backwards compatibility:
3502 * - Whether the variable is read-only
3503 * - Whether the variable value is locked
3504 * - Whether the variable is locked
3505 */
3506 int
3507var_check_permission(dictitem_T *di, char_u *name)
3508{
3509 if (var_check_ro(di->di_flags, name, FALSE)
3510 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3511 || var_check_lock(di->di_flags, name, FALSE))
3512 return FAIL;
3513 return OK;
3514}
3515
3516/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003517 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3518 * Also give an error message.
3519 */
3520 int
3521var_check_ro(int flags, char_u *name, int use_gettext)
3522{
3523 if (flags & DI_FLAGS_RO)
3524 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02003525 semsg(_(e_cannot_change_readonly_variable_str),
3526 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003527 return TRUE;
3528 }
3529 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3530 {
3531 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3532 return TRUE;
3533 }
3534 return FALSE;
3535}
3536
3537/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003538 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3539 * Also give an error message.
3540 */
3541 int
3542var_check_lock(int flags, char_u *name, int use_gettext)
3543{
3544 if (flags & DI_FLAGS_LOCK)
3545 {
3546 semsg(_(e_variable_is_locked_str),
3547 use_gettext ? (char_u *)_(name) : name);
3548 return TRUE;
3549 }
3550 return FALSE;
3551}
3552
3553/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003554 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3555 * Also give an error message.
3556 */
3557 int
3558var_check_fixed(int flags, char_u *name, int use_gettext)
3559{
3560 if (flags & DI_FLAGS_FIX)
3561 {
3562 semsg(_("E795: Cannot delete variable %s"),
3563 use_gettext ? (char_u *)_(name) : name);
3564 return TRUE;
3565 }
3566 return FALSE;
3567}
3568
3569/*
3570 * Check if a funcref is assigned to a valid variable name.
3571 * Return TRUE and give an error if not.
3572 */
3573 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003574var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003575 char_u *name, // points to start of variable name
3576 int new_var) // TRUE when creating the variable
3577{
Bram Moolenaar32154662021-03-28 21:14:06 +02003578 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
3579 // the name can be used without the s: prefix.
3580 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
3581 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003582 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3583 ? name[2] : name[0]))
3584 {
3585 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3586 name);
3587 return TRUE;
3588 }
3589 // Don't allow hiding a function. When "v" is not NULL we might be
3590 // assigning another function to the same var, the type is checked
3591 // below.
3592 if (new_var && function_exists(name, FALSE))
3593 {
3594 semsg(_("E705: Variable name conflicts with existing function: %s"),
3595 name);
3596 return TRUE;
3597 }
3598 return FALSE;
3599}
3600
3601/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003602 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3603 * value. Also give an error message, using "name" or _("name") when
3604 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003605 */
3606 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003607value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003608{
3609 if (lock & VAR_LOCKED)
3610 {
3611 semsg(_("E741: Value is locked: %s"),
3612 name == NULL ? (char_u *)_("Unknown")
3613 : use_gettext ? (char_u *)_(name)
3614 : name);
3615 return TRUE;
3616 }
3617 if (lock & VAR_FIXED)
3618 {
3619 semsg(_("E742: Cannot change value of %s"),
3620 name == NULL ? (char_u *)_("Unknown")
3621 : use_gettext ? (char_u *)_(name)
3622 : name);
3623 return TRUE;
3624 }
3625 return FALSE;
3626}
3627
3628/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003629 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003630 * Return FALSE and give an error if not.
3631 */
3632 int
Bram Moolenaar03290b82020-12-19 16:30:44 +01003633valid_varname(char_u *varname, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003634{
3635 char_u *p;
3636
3637 for (p = varname; *p != NUL; ++p)
3638 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003639 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003640 {
3641 semsg(_(e_illvar), varname);
3642 return FALSE;
3643 }
3644 return TRUE;
3645}
3646
3647/*
3648 * getwinvar() and gettabwinvar()
3649 */
3650 static void
3651getwinvar(
3652 typval_T *argvars,
3653 typval_T *rettv,
3654 int off) // 1 for gettabwinvar()
3655{
3656 win_T *win;
3657 char_u *varname;
3658 dictitem_T *v;
3659 tabpage_T *tp = NULL;
3660 int done = FALSE;
3661 win_T *oldcurwin;
3662 tabpage_T *oldtabpage;
3663 int need_switch_win;
3664
3665 if (off == 1)
3666 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3667 else
3668 tp = curtab;
3669 win = find_win_by_nr(&argvars[off], tp);
3670 varname = tv_get_string_chk(&argvars[off + 1]);
3671 ++emsg_off;
3672
3673 rettv->v_type = VAR_STRING;
3674 rettv->vval.v_string = NULL;
3675
3676 if (win != NULL && varname != NULL)
3677 {
3678 // Set curwin to be our win, temporarily. Also set the tabpage,
3679 // otherwise the window is not valid. Only do this when needed,
3680 // autocommands get blocked.
3681 need_switch_win = !(tp == curtab && win == curwin);
3682 if (!need_switch_win
3683 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3684 {
3685 if (*varname == '&')
3686 {
3687 if (varname[1] == NUL)
3688 {
3689 // get all window-local options in a dict
3690 dict_T *opts = get_winbuf_options(FALSE);
3691
3692 if (opts != NULL)
3693 {
3694 rettv_dict_set(rettv, opts);
3695 done = TRUE;
3696 }
3697 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003698 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003699 // window-local-option
3700 done = TRUE;
3701 }
3702 else
3703 {
3704 // Look up the variable.
3705 // Let getwinvar({nr}, "") return the "w:" dictionary.
3706 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3707 varname, FALSE);
3708 if (v != NULL)
3709 {
3710 copy_tv(&v->di_tv, rettv);
3711 done = TRUE;
3712 }
3713 }
3714 }
3715
3716 if (need_switch_win)
3717 // restore previous notion of curwin
3718 restore_win(oldcurwin, oldtabpage, TRUE);
3719 }
3720
3721 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3722 // use the default return value
3723 copy_tv(&argvars[off + 2], rettv);
3724
3725 --emsg_off;
3726}
3727
3728/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003729 * Set option "varname" to the value of "varp" for the current buffer/window.
3730 */
3731 static void
3732set_option_from_tv(char_u *varname, typval_T *varp)
3733{
3734 long numval = 0;
3735 char_u *strval;
3736 char_u nbuf[NUMBUFLEN];
3737 int error = FALSE;
3738
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003739 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003740 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003741 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003742 strval = (char_u *)"0"; // avoid using "false"
3743 }
3744 else
3745 {
3746 if (!in_vim9script() || varp->v_type != VAR_STRING)
3747 numval = (long)tv_get_number_chk(varp, &error);
3748 strval = tv_get_string_buf_chk(varp, nbuf);
3749 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02003750 if (!error && strval != NULL)
3751 set_option_value(varname, numval, strval, OPT_LOCAL);
3752}
3753
3754/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003755 * "setwinvar()" and "settabwinvar()" functions
3756 */
3757 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003758setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003759{
3760 win_T *win;
3761 win_T *save_curwin;
3762 tabpage_T *save_curtab;
3763 int need_switch_win;
3764 char_u *varname, *winvarname;
3765 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003766 tabpage_T *tp = NULL;
3767
3768 if (check_secure())
3769 return;
3770
3771 if (off == 1)
3772 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3773 else
3774 tp = curtab;
3775 win = find_win_by_nr(&argvars[off], tp);
3776 varname = tv_get_string_chk(&argvars[off + 1]);
3777 varp = &argvars[off + 2];
3778
3779 if (win != NULL && varname != NULL && varp != NULL)
3780 {
3781 need_switch_win = !(tp == curtab && win == curwin);
3782 if (!need_switch_win
3783 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3784 {
3785 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003786 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003787 else
3788 {
3789 winvarname = alloc(STRLEN(varname) + 3);
3790 if (winvarname != NULL)
3791 {
3792 STRCPY(winvarname, "w:");
3793 STRCPY(winvarname + 2, varname);
3794 set_var(winvarname, varp, TRUE);
3795 vim_free(winvarname);
3796 }
3797 }
3798 }
3799 if (need_switch_win)
3800 restore_win(save_curwin, save_curtab, TRUE);
3801 }
3802}
3803
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003804/*
3805 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3806 * v:option_type, and v:option_command.
3807 */
3808 void
3809reset_v_option_vars(void)
3810{
3811 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3812 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3813 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3814 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3815 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3816 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3817}
3818
3819/*
3820 * Add an assert error to v:errors.
3821 */
3822 void
3823assert_error(garray_T *gap)
3824{
3825 struct vimvar *vp = &vimvars[VV_ERRORS];
3826
3827 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003828 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003829 set_vim_var_list(VV_ERRORS, list_alloc());
3830 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3831}
3832
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003833 int
3834var_exists(char_u *var)
3835{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003836 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003837 char_u *name;
3838 char_u *tofree;
3839 typval_T tv;
3840 int len = 0;
3841 int n = FALSE;
3842
3843 // get_name_len() takes care of expanding curly braces
3844 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003845 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003846 if (len > 0)
3847 {
3848 if (tofree != NULL)
3849 name = tofree;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003850 n = (eval_variable(name, len, &tv, NULL,
3851 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003852 if (n)
3853 {
3854 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003855 arg = skipwhite(arg);
3856 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003857 if (n)
3858 clear_tv(&tv);
3859 }
3860 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003861 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003862 n = FALSE;
3863
3864 vim_free(tofree);
3865 return n;
3866}
3867
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003868static lval_T *redir_lval = NULL;
3869#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3870static garray_T redir_ga; // only valid when redir_lval is not NULL
3871static char_u *redir_endp = NULL;
3872static char_u *redir_varname = NULL;
3873
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003874 int
3875alloc_redir_lval(void)
3876{
3877 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3878 if (redir_lval == NULL)
3879 return FAIL;
3880 return OK;
3881}
3882
3883 void
3884clear_redir_lval(void)
3885{
3886 VIM_CLEAR(redir_lval);
3887}
3888
3889 void
3890init_redir_ga(void)
3891{
3892 ga_init2(&redir_ga, (int)sizeof(char), 500);
3893}
3894
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003895/*
3896 * Start recording command output to a variable
3897 * When "append" is TRUE append to an existing variable.
3898 * Returns OK if successfully completed the setup. FAIL otherwise.
3899 */
3900 int
3901var_redir_start(char_u *name, int append)
3902{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003903 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003904 typval_T tv;
3905
3906 // Catch a bad name early.
3907 if (!eval_isnamec1(*name))
3908 {
3909 emsg(_(e_invarg));
3910 return FAIL;
3911 }
3912
3913 // Make a copy of the name, it is used in redir_lval until redir ends.
3914 redir_varname = vim_strsave(name);
3915 if (redir_varname == NULL)
3916 return FAIL;
3917
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003918 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003919 {
3920 var_redir_stop();
3921 return FAIL;
3922 }
3923
3924 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003925 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003926
3927 // Parse the variable name (can be a dict or list entry).
3928 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3929 FNE_CHECK_START);
3930 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3931 {
3932 clear_lval(redir_lval);
3933 if (redir_endp != NULL && *redir_endp != NUL)
3934 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003935 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003936 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003937 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003938 redir_endp = NULL; // don't store a value, only cleanup
3939 var_redir_stop();
3940 return FAIL;
3941 }
3942
3943 // check if we can write to the variable: set it to or append an empty
3944 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003945 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003946 tv.v_type = VAR_STRING;
3947 tv.vval.v_string = (char_u *)"";
3948 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003949 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003950 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003951 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003952 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003953 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003954 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003955 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003956 {
3957 redir_endp = NULL; // don't store a value, only cleanup
3958 var_redir_stop();
3959 return FAIL;
3960 }
3961
3962 return OK;
3963}
3964
3965/*
3966 * Append "value[value_len]" to the variable set by var_redir_start().
3967 * The actual appending is postponed until redirection ends, because the value
3968 * appended may in fact be the string we write to, changing it may cause freed
3969 * memory to be used:
3970 * :redir => foo
3971 * :let foo
3972 * :redir END
3973 */
3974 void
3975var_redir_str(char_u *value, int value_len)
3976{
3977 int len;
3978
3979 if (redir_lval == NULL)
3980 return;
3981
3982 if (value_len == -1)
3983 len = (int)STRLEN(value); // Append the entire string
3984 else
3985 len = value_len; // Append only "value_len" characters
3986
3987 if (ga_grow(&redir_ga, len) == OK)
3988 {
3989 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3990 redir_ga.ga_len += len;
3991 }
3992 else
3993 var_redir_stop();
3994}
3995
3996/*
3997 * Stop redirecting command output to a variable.
3998 * Frees the allocated memory.
3999 */
4000 void
4001var_redir_stop(void)
4002{
4003 typval_T tv;
4004
4005 if (EVALCMD_BUSY)
4006 {
4007 redir_lval = NULL;
4008 return;
4009 }
4010
4011 if (redir_lval != NULL)
4012 {
4013 // If there was no error: assign the text to the variable.
4014 if (redir_endp != NULL)
4015 {
4016 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4017 tv.v_type = VAR_STRING;
4018 tv.vval.v_string = redir_ga.ga_data;
4019 // Call get_lval() again, if it's inside a Dict or List it may
4020 // have changed.
4021 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4022 FALSE, FALSE, 0, FNE_CHECK_START);
4023 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004025 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004026 clear_lval(redir_lval);
4027 }
4028
4029 // free the collected output
4030 VIM_CLEAR(redir_ga.ga_data);
4031
4032 VIM_CLEAR(redir_lval);
4033 }
4034 VIM_CLEAR(redir_varname);
4035}
4036
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004037/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004038 * Get the collected redirected text and clear redir_ga.
4039 */
4040 char_u *
4041get_clear_redir_ga(void)
4042{
4043 char_u *res;
4044
4045 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4046 res = redir_ga.ga_data;
4047 redir_ga.ga_data = NULL;
4048 return res;
4049}
4050
4051/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004052 * "gettabvar()" function
4053 */
4054 void
4055f_gettabvar(typval_T *argvars, typval_T *rettv)
4056{
4057 win_T *oldcurwin;
4058 tabpage_T *tp, *oldtabpage;
4059 dictitem_T *v;
4060 char_u *varname;
4061 int done = FALSE;
4062
4063 rettv->v_type = VAR_STRING;
4064 rettv->vval.v_string = NULL;
4065
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004066 if (in_vim9script()
4067 && (check_for_number_arg(argvars, 0) == FAIL
4068 || check_for_string_arg(argvars, 1) == FAIL))
4069 return;
4070
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004071 varname = tv_get_string_chk(&argvars[1]);
4072 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4073 if (tp != NULL && varname != NULL)
4074 {
4075 // Set tp to be our tabpage, temporarily. Also set the window to the
4076 // first window in the tabpage, otherwise the window is not valid.
4077 if (switch_win(&oldcurwin, &oldtabpage,
4078 tp == curtab || tp->tp_firstwin == NULL ? firstwin
4079 : tp->tp_firstwin, tp, TRUE) == OK)
4080 {
4081 // look up the variable
4082 // Let gettabvar({nr}, "") return the "t:" dictionary.
4083 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
4084 if (v != NULL)
4085 {
4086 copy_tv(&v->di_tv, rettv);
4087 done = TRUE;
4088 }
4089 }
4090
4091 // restore previous notion of curwin
4092 restore_win(oldcurwin, oldtabpage, TRUE);
4093 }
4094
4095 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4096 copy_tv(&argvars[2], rettv);
4097}
4098
4099/*
4100 * "gettabwinvar()" function
4101 */
4102 void
4103f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4104{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004105 if (in_vim9script()
4106 && (check_for_number_arg(argvars, 0) == FAIL
4107 || check_for_number_arg(argvars, 1) == FAIL
4108 || check_for_string_arg(argvars, 2) == FAIL))
4109 return;
4110
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004111 getwinvar(argvars, rettv, 1);
4112}
4113
4114/*
4115 * "getwinvar()" function
4116 */
4117 void
4118f_getwinvar(typval_T *argvars, typval_T *rettv)
4119{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004120 if (in_vim9script()
4121 && (check_for_number_arg(argvars, 0) == FAIL
4122 || check_for_string_arg(argvars, 1) == FAIL))
4123 return;
4124
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004125 getwinvar(argvars, rettv, 0);
4126}
4127
4128/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004129 * "getbufvar()" function
4130 */
4131 void
4132f_getbufvar(typval_T *argvars, typval_T *rettv)
4133{
4134 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004135 char_u *varname;
4136 dictitem_T *v;
4137 int done = FALSE;
4138
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004139 if (in_vim9script()
4140 && (check_for_buffer_arg(argvars, 0) == FAIL
4141 || check_for_string_arg(argvars, 1) == FAIL))
4142 return;
4143
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004144 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004145 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004146
4147 rettv->v_type = VAR_STRING;
4148 rettv->vval.v_string = NULL;
4149
4150 if (buf != NULL && varname != NULL)
4151 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004152 if (*varname == '&')
4153 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004154 buf_T *save_curbuf = curbuf;
4155
4156 // set curbuf to be our buf, temporarily
4157 curbuf = buf;
4158
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004159 if (varname[1] == NUL)
4160 {
4161 // get all buffer-local options in a dict
4162 dict_T *opts = get_winbuf_options(TRUE);
4163
4164 if (opts != NULL)
4165 {
4166 rettv_dict_set(rettv, opts);
4167 done = TRUE;
4168 }
4169 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004170 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004171 // buffer-local-option
4172 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02004173
4174 // restore previous notion of curbuf
4175 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004176 }
4177 else
4178 {
4179 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02004180 if (*varname == NUL)
4181 // Let getbufvar({nr}, "") return the "b:" dictionary.
4182 v = &buf->b_bufvar;
4183 else
4184 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
4185 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004186 if (v != NULL)
4187 {
4188 copy_tv(&v->di_tv, rettv);
4189 done = TRUE;
4190 }
4191 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004192 }
4193
4194 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4195 // use the default value
4196 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004197}
4198
4199/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004200 * "settabvar()" function
4201 */
4202 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004203f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004204{
4205 tabpage_T *save_curtab;
4206 tabpage_T *tp;
4207 char_u *varname, *tabvarname;
4208 typval_T *varp;
4209
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004210 if (check_secure())
4211 return;
4212
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004213 if (in_vim9script()
4214 && (check_for_number_arg(argvars, 0) == FAIL
4215 || check_for_string_arg(argvars, 1) == FAIL))
4216 return;
4217
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004218 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4219 varname = tv_get_string_chk(&argvars[1]);
4220 varp = &argvars[2];
4221
4222 if (varname != NULL && varp != NULL && tp != NULL)
4223 {
4224 save_curtab = curtab;
4225 goto_tabpage_tp(tp, FALSE, FALSE);
4226
4227 tabvarname = alloc(STRLEN(varname) + 3);
4228 if (tabvarname != NULL)
4229 {
4230 STRCPY(tabvarname, "t:");
4231 STRCPY(tabvarname + 2, varname);
4232 set_var(tabvarname, varp, TRUE);
4233 vim_free(tabvarname);
4234 }
4235
4236 // Restore current tabpage
4237 if (valid_tabpage(save_curtab))
4238 goto_tabpage_tp(save_curtab, FALSE, FALSE);
4239 }
4240}
4241
4242/*
4243 * "settabwinvar()" function
4244 */
4245 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004246f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004247{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004248 if (in_vim9script()
4249 && (check_for_number_arg(argvars, 0) == FAIL
4250 || check_for_number_arg(argvars, 1) == FAIL
4251 || check_for_string_arg(argvars, 2) == FAIL))
4252 return;
4253
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004254 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004255}
4256
4257/*
4258 * "setwinvar()" function
4259 */
4260 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004261f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004262{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004263 if (in_vim9script()
4264 && (check_for_number_arg(argvars, 0) == FAIL
4265 || check_for_string_arg(argvars, 1) == FAIL))
4266 return;
4267
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004268 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004269}
4270
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004271/*
4272 * "setbufvar()" function
4273 */
4274 void
4275f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4276{
4277 buf_T *buf;
4278 char_u *varname, *bufvarname;
4279 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004280
4281 if (check_secure())
4282 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004283
4284 if (in_vim9script()
4285 && (check_for_buffer_arg(argvars, 0) == FAIL
4286 || check_for_string_arg(argvars, 1) == FAIL))
4287 return;
4288
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004289 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004290 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004291 varp = &argvars[2];
4292
4293 if (buf != NULL && varname != NULL && varp != NULL)
4294 {
4295 if (*varname == '&')
4296 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004297 aco_save_T aco;
4298
4299 // set curbuf to be our buf, temporarily
4300 aucmd_prepbuf(&aco, buf);
4301
Bram Moolenaar191929b2020-08-19 21:20:49 +02004302 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004303
4304 // reset notion of buffer
4305 aucmd_restbuf(&aco);
4306 }
4307 else
4308 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004309 bufvarname = alloc(STRLEN(varname) + 3);
4310 if (bufvarname != NULL)
4311 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004312 buf_T *save_curbuf = curbuf;
4313
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004314 curbuf = buf;
4315 STRCPY(bufvarname, "b:");
4316 STRCPY(bufvarname + 2, varname);
4317 set_var(bufvarname, varp, TRUE);
4318 vim_free(bufvarname);
4319 curbuf = save_curbuf;
4320 }
4321 }
4322 }
4323}
4324
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004325/*
4326 * Get a callback from "arg". It can be a Funcref or a function name.
4327 * When "arg" is zero return an empty string.
4328 * "cb_name" is not allocated.
4329 * "cb_name" is set to NULL for an invalid argument.
4330 */
4331 callback_T
4332get_callback(typval_T *arg)
4333{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004334 callback_T res;
4335 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004336
4337 res.cb_free_name = FALSE;
4338 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4339 {
4340 res.cb_partial = arg->vval.v_partial;
4341 ++res.cb_partial->pt_refcount;
4342 res.cb_name = partial_name(res.cb_partial);
4343 }
4344 else
4345 {
4346 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004347 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4348 && isdigit(*arg->vval.v_string))
4349 r = FAIL;
4350 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004351 {
4352 // Note that we don't make a copy of the string.
4353 res.cb_name = arg->vval.v_string;
4354 func_ref(res.cb_name);
4355 }
4356 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004357 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004358 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004359 r = FAIL;
4360
4361 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004362 {
4363 emsg(_("E921: Invalid callback argument"));
4364 res.cb_name = NULL;
4365 }
4366 }
4367 return res;
4368}
4369
4370/*
4371 * Copy a callback into a typval_T.
4372 */
4373 void
4374put_callback(callback_T *cb, typval_T *tv)
4375{
4376 if (cb->cb_partial != NULL)
4377 {
4378 tv->v_type = VAR_PARTIAL;
4379 tv->vval.v_partial = cb->cb_partial;
4380 ++tv->vval.v_partial->pt_refcount;
4381 }
4382 else
4383 {
4384 tv->v_type = VAR_FUNC;
4385 tv->vval.v_string = vim_strsave(cb->cb_name);
4386 func_ref(cb->cb_name);
4387 }
4388}
4389
4390/*
4391 * Make a copy of "src" into "dest", allocating the function name if needed,
4392 * without incrementing the refcount.
4393 */
4394 void
4395set_callback(callback_T *dest, callback_T *src)
4396{
4397 if (src->cb_partial == NULL)
4398 {
4399 // just a function name, make a copy
4400 dest->cb_name = vim_strsave(src->cb_name);
4401 dest->cb_free_name = TRUE;
4402 }
4403 else
4404 {
4405 // cb_name is a pointer into cb_partial
4406 dest->cb_name = src->cb_name;
4407 dest->cb_free_name = FALSE;
4408 }
4409 dest->cb_partial = src->cb_partial;
4410}
4411
4412/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004413 * Copy callback from "src" to "dest", incrementing the refcounts.
4414 */
4415 void
4416copy_callback(callback_T *dest, callback_T *src)
4417{
4418 dest->cb_partial = src->cb_partial;
4419 if (dest->cb_partial != NULL)
4420 {
4421 dest->cb_name = src->cb_name;
4422 dest->cb_free_name = FALSE;
4423 ++dest->cb_partial->pt_refcount;
4424 }
4425 else
4426 {
4427 dest->cb_name = vim_strsave(src->cb_name);
4428 dest->cb_free_name = TRUE;
4429 func_ref(src->cb_name);
4430 }
4431}
4432
4433/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004434 * Unref/free "callback" returned by get_callback() or set_callback().
4435 */
4436 void
4437free_callback(callback_T *callback)
4438{
4439 if (callback->cb_partial != NULL)
4440 {
4441 partial_unref(callback->cb_partial);
4442 callback->cb_partial = NULL;
4443 }
4444 else if (callback->cb_name != NULL)
4445 func_unref(callback->cb_name);
4446 if (callback->cb_free_name)
4447 {
4448 vim_free(callback->cb_name);
4449 callback->cb_free_name = FALSE;
4450 }
4451 callback->cb_name = NULL;
4452}
4453
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004454#endif // FEAT_EVAL