blob: 721b688d5835f6ba680122829739609a798202e1 [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},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200152};
153
154// shorthand
155#define vv_type vv_di.di_tv.v_type
156#define vv_nr vv_di.di_tv.vval.v_number
157#define vv_float vv_di.di_tv.vval.v_float
158#define vv_str vv_di.di_tv.vval.v_string
159#define vv_list vv_di.di_tv.vval.v_list
160#define vv_dict vv_di.di_tv.vval.v_dict
161#define vv_blob vv_di.di_tv.vval.v_blob
162#define vv_tv vv_di.di_tv
163
164static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200165static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200166#define vimvarht vimvardict.dv_hashtab
167
168// for VIM_VERSION_ defines
169#include "version.h"
170
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200171static void list_glob_vars(int *first);
172static void list_buf_vars(int *first);
173static void list_win_vars(int *first);
174static void list_tab_vars(int *first);
175static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100176static 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 +0200177static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
178static 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 +0200179static void list_one_var(dictitem_T *v, char *prefix, int *first);
180static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
181
182/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200183 * Initialize global and vim special variables
184 */
185 void
186evalvars_init(void)
187{
188 int i;
189 struct vimvar *p;
190
191 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
192 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
193 vimvardict.dv_lock = VAR_FIXED;
194 hash_init(&compat_hashtab);
195
196 for (i = 0; i < VV_LEN; ++i)
197 {
198 p = &vimvars[i];
199 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
200 {
201 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
202 getout(1);
203 }
204 STRCPY(p->vv_di.di_key, p->vv_name);
205 if (p->vv_flags & VV_RO)
206 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
207 else if (p->vv_flags & VV_RO_SBX)
208 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
209 else
210 p->vv_di.di_flags = DI_FLAGS_FIX;
211
212 // add to v: scope dict, unless the value is not always available
213 if (p->vv_type != VAR_UNKNOWN)
214 hash_add(&vimvarht, p->vv_di.di_key);
215 if (p->vv_flags & VV_COMPAT)
216 // add to compat scope dict
217 hash_add(&compat_hashtab, p->vv_di.di_key);
218 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200219 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
220 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200221
222 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
223 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100224 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200225 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
226 set_vim_var_list(VV_ERRORS, list_alloc());
227 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
228
229 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
230 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
231 set_vim_var_nr(VV_NONE, VVAL_NONE);
232 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100233 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
234 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100235 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200236
237 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
238 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
239 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
240 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
241 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
242 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
243 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
244 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
245 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
246 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
247 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
248
249 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
250
Bram Moolenaar439c0362020-06-06 15:58:03 +0200251 // Default for v:register is not 0 but '"'. This is adjusted once the
252 // clipboard has been setup by calling reset_reg_var().
253 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200254}
255
256#if defined(EXITFREE) || defined(PROTO)
257/*
258 * Free all vim variables information on exit
259 */
260 void
261evalvars_clear(void)
262{
263 int i;
264 struct vimvar *p;
265
266 for (i = 0; i < VV_LEN; ++i)
267 {
268 p = &vimvars[i];
269 if (p->vv_di.di_tv.v_type == VAR_STRING)
270 VIM_CLEAR(p->vv_str);
271 else if (p->vv_di.di_tv.v_type == VAR_LIST)
272 {
273 list_unref(p->vv_list);
274 p->vv_list = NULL;
275 }
276 }
277 hash_clear(&vimvarht);
278 hash_init(&vimvarht); // garbage_collect() will access it
279 hash_clear(&compat_hashtab);
280
281 // global variables
282 vars_clear(&globvarht);
283
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100284 // Script-local variables. Clear all the variables here.
285 // The scriptvar_T is cleared later in free_scriptnames(), because a
286 // variable in one script might hold a reference to the whole scope of
287 // another script.
288 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200289 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200290}
291#endif
292
293 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200294garbage_collect_globvars(int copyID)
295{
296 return set_ref_in_ht(&globvarht, copyID, NULL);
297}
298
299 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200300garbage_collect_vimvars(int copyID)
301{
302 return set_ref_in_ht(&vimvarht, copyID, NULL);
303}
304
305 int
306garbage_collect_scriptvars(int copyID)
307{
Bram Moolenaared234f22020-10-15 20:42:20 +0200308 int i;
309 int idx;
310 int abort = FALSE;
311 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200312
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100313 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200314 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200315 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
316
Bram Moolenaared234f22020-10-15 20:42:20 +0200317 si = SCRIPT_ITEM(i);
318 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
319 {
320 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
321
Bram Moolenaard00a7fb2021-03-08 20:47:14 +0100322 if (sv->sv_name != NULL)
323 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
Bram Moolenaared234f22020-10-15 20:42:20 +0200324 }
325 }
326
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200327 return abort;
328}
329
330/*
331 * Set an internal variable to a string value. Creates the variable if it does
332 * not already exist.
333 */
334 void
335set_internal_string_var(char_u *name, char_u *value)
336{
337 char_u *val;
338 typval_T *tvp;
339
340 val = vim_strsave(value);
341 if (val != NULL)
342 {
343 tvp = alloc_string_tv(val);
344 if (tvp != NULL)
345 {
346 set_var(name, tvp, FALSE);
347 free_tv(tvp);
348 }
349 }
350}
351
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200352 int
353eval_charconvert(
354 char_u *enc_from,
355 char_u *enc_to,
356 char_u *fname_from,
357 char_u *fname_to)
358{
359 int err = FALSE;
360
361 set_vim_var_string(VV_CC_FROM, enc_from, -1);
362 set_vim_var_string(VV_CC_TO, enc_to, -1);
363 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
364 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
365 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
366 err = TRUE;
367 set_vim_var_string(VV_CC_FROM, NULL, -1);
368 set_vim_var_string(VV_CC_TO, NULL, -1);
369 set_vim_var_string(VV_FNAME_IN, NULL, -1);
370 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
371
372 if (err)
373 return FAIL;
374 return OK;
375}
376
377# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
378 int
379eval_printexpr(char_u *fname, char_u *args)
380{
381 int err = FALSE;
382
383 set_vim_var_string(VV_FNAME_IN, fname, -1);
384 set_vim_var_string(VV_CMDARG, args, -1);
385 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
386 err = TRUE;
387 set_vim_var_string(VV_FNAME_IN, NULL, -1);
388 set_vim_var_string(VV_CMDARG, NULL, -1);
389
390 if (err)
391 {
392 mch_remove(fname);
393 return FAIL;
394 }
395 return OK;
396}
397# endif
398
399# if defined(FEAT_DIFF) || defined(PROTO)
400 void
401eval_diff(
402 char_u *origfile,
403 char_u *newfile,
404 char_u *outfile)
405{
406 int err = FALSE;
407
408 set_vim_var_string(VV_FNAME_IN, origfile, -1);
409 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
410 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
411 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
412 set_vim_var_string(VV_FNAME_IN, NULL, -1);
413 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
414 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
415}
416
417 void
418eval_patch(
419 char_u *origfile,
420 char_u *difffile,
421 char_u *outfile)
422{
423 int err;
424
425 set_vim_var_string(VV_FNAME_IN, origfile, -1);
426 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
427 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
428 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
429 set_vim_var_string(VV_FNAME_IN, NULL, -1);
430 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
431 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
432}
433# endif
434
435#if defined(FEAT_SPELL) || defined(PROTO)
436/*
437 * Evaluate an expression to a list with suggestions.
438 * For the "expr:" part of 'spellsuggest'.
439 * Returns NULL when there is an error.
440 */
441 list_T *
442eval_spell_expr(char_u *badword, char_u *expr)
443{
444 typval_T save_val;
445 typval_T rettv;
446 list_T *list = NULL;
447 char_u *p = skipwhite(expr);
448
449 // Set "v:val" to the bad word.
450 prepare_vimvar(VV_VAL, &save_val);
451 set_vim_var_string(VV_VAL, badword, -1);
452 if (p_verbose == 0)
453 ++emsg_off;
454
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200455 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200456 {
457 if (rettv.v_type != VAR_LIST)
458 clear_tv(&rettv);
459 else
460 list = rettv.vval.v_list;
461 }
462
463 if (p_verbose == 0)
464 --emsg_off;
465 clear_tv(get_vim_var_tv(VV_VAL));
466 restore_vimvar(VV_VAL, &save_val);
467
468 return list;
469}
470
471/*
472 * "list" is supposed to contain two items: a word and a number. Return the
473 * word in "pp" and the number as the return value.
474 * Return -1 if anything isn't right.
475 * Used to get the good word and score from the eval_spell_expr() result.
476 */
477 int
478get_spellword(list_T *list, char_u **pp)
479{
480 listitem_T *li;
481
482 li = list->lv_first;
483 if (li == NULL)
484 return -1;
485 *pp = tv_get_string(&li->li_tv);
486
487 li = li->li_next;
488 if (li == NULL)
489 return -1;
490 return (int)tv_get_number(&li->li_tv);
491}
492#endif
493
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200494/*
495 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200496 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200497 * When not used yet add the variable to the v: hashtable.
498 */
499 void
500prepare_vimvar(int idx, typval_T *save_tv)
501{
502 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200503 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200504 if (vimvars[idx].vv_type == VAR_UNKNOWN)
505 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
506}
507
508/*
509 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200510 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200511 * When no longer defined, remove the variable from the v: hashtable.
512 */
513 void
514restore_vimvar(int idx, typval_T *save_tv)
515{
516 hashitem_T *hi;
517
518 vimvars[idx].vv_tv = *save_tv;
519 if (vimvars[idx].vv_type == VAR_UNKNOWN)
520 {
521 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
522 if (HASHITEM_EMPTY(hi))
523 internal_error("restore_vimvar()");
524 else
525 hash_remove(&vimvarht, hi);
526 }
527}
528
529/*
530 * List Vim variables.
531 */
532 static void
533list_vim_vars(int *first)
534{
535 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
536}
537
538/*
539 * List script-local variables, if there is a script.
540 */
541 static void
542list_script_vars(int *first)
543{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200544 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200545 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
546 "s:", FALSE, first);
547}
548
549/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200550 * Get a list of lines from a HERE document. The here document is a list of
551 * lines surrounded by a marker.
552 * cmd << {marker}
553 * {line1}
554 * {line2}
555 * ....
556 * {marker}
557 *
558 * The {marker} is a string. If the optional 'trim' word is supplied before the
559 * marker, then the leading indentation before the lines (matching the
560 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200561 *
562 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
563 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
564 * missing, then '.' is accepted as a marker.
565 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200566 * Returns a List with {lines} or NULL.
567 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100568 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200569heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200570{
571 char_u *theline;
572 char_u *marker;
573 list_T *l;
574 char_u *p;
575 int marker_indent_len = 0;
576 int text_indent_len = 0;
577 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200578 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200579 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200580
581 if (eap->getline == NULL)
582 {
583 emsg(_("E991: cannot use =<< here"));
584 return NULL;
585 }
586
587 // Check for the optional 'trim' word before the marker
588 cmd = skipwhite(cmd);
589 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
590 {
591 cmd = skipwhite(cmd + 4);
592
593 // Trim the indentation from all the lines in the here document.
594 // The amount of indentation trimmed is the same as the indentation of
595 // the first line after the :let command line. To find the end marker
596 // the indent of the :let command line is trimmed.
597 p = *eap->cmdlinep;
598 while (VIM_ISWHITE(*p))
599 {
600 p++;
601 marker_indent_len++;
602 }
603 text_indent_len = -1;
604 }
605
606 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200607 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200608 {
609 marker = skipwhite(cmd);
610 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200611 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200612 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200613 semsg(_(e_trailing_arg), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200614 return NULL;
615 }
616 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200617 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200618 {
619 emsg(_("E221: Marker cannot start with lower case letter"));
620 return NULL;
621 }
622 }
623 else
624 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200625 // When getting lines for an embedded script, if the marker is missing,
626 // accept '.' as the marker.
627 if (script_get)
628 marker = dot;
629 else
630 {
631 emsg(_("E172: Missing marker"));
632 return NULL;
633 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200634 }
635
636 l = list_alloc();
637 if (l == NULL)
638 return NULL;
639
640 for (;;)
641 {
642 int mi = 0;
643 int ti = 0;
644
645 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
646 if (theline == NULL)
647 {
648 semsg(_("E990: Missing end marker '%s'"), marker);
649 break;
650 }
651
652 // with "trim": skip the indent matching the :let line to find the
653 // marker
654 if (marker_indent_len > 0
655 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
656 mi = marker_indent_len;
657 if (STRCMP(marker, theline + mi) == 0)
658 {
659 vim_free(theline);
660 break;
661 }
662
663 if (text_indent_len == -1 && *theline != NUL)
664 {
665 // set the text indent from the first line.
666 p = theline;
667 text_indent_len = 0;
668 while (VIM_ISWHITE(*p))
669 {
670 p++;
671 text_indent_len++;
672 }
673 text_indent = vim_strnsave(theline, text_indent_len);
674 }
675 // with "trim": skip the indent matching the first line
676 if (text_indent != NULL)
677 for (ti = 0; ti < text_indent_len; ++ti)
678 if (theline[ti] != text_indent[ti])
679 break;
680
681 if (list_append_string(l, theline + ti, -1) == FAIL)
682 break;
683 vim_free(theline);
684 }
685 vim_free(text_indent);
686
687 return l;
688}
689
690/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200691 * Vim9 variable declaration:
692 * ":var name"
693 * ":var name: type"
694 * ":var name = expr"
695 * ":var name: type = expr"
696 * etc.
697 */
698 void
699ex_var(exarg_T *eap)
700{
701 if (!in_vim9script())
702 {
703 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
704 return;
705 }
706 ex_let(eap);
707}
708
709/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200710 * ":let" list all variable values
711 * ":let var1 var2" list variable values
712 * ":let var = expr" assignment command.
713 * ":let var += expr" assignment command.
714 * ":let var -= expr" assignment command.
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 [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100721 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100722 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200723 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200724 * ":final var = expr" assignment command.
725 * ":final [var1, var2] = expr" unpack list.
726 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200727 * ":const" list all variable values
728 * ":const var1 var2" list variable values
729 * ":const var = expr" assignment command.
730 * ":const [var1, var2] = expr" unpack list.
731 */
732 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200733ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200734{
735 char_u *arg = eap->arg;
736 char_u *expr = NULL;
737 typval_T rettv;
738 int i;
739 int var_count = 0;
740 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200741 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200742 char_u *argend;
743 int first = TRUE;
744 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200745 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100746 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200747 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200748
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200749 if (eap->cmdidx == CMD_final && !vim9script)
750 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100751 // In legacy Vim script ":final" is short for ":finally".
752 ex_finally(eap);
753 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200754 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200755 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200756 {
757 emsg(_(e_cannot_use_let_in_vim9_script));
758 return;
759 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200760
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100761 if (eap->cmdidx == CMD_const)
762 flags |= ASSIGN_CONST;
763 else if (eap->cmdidx == CMD_final)
764 flags |= ASSIGN_FINAL;
765
766 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100767 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200768 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200770 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200771 if (argend == NULL)
772 return;
773 if (argend > arg && argend[-1] == '.') // for var.='str'
774 --argend;
775 expr = skipwhite(argend);
776 concat = expr[0] == '.'
777 && ((expr[1] == '=' && current_sctx.sc_version < 2)
778 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200779 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
780 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200781 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200782 {
783 // ":let" without "=": list variables
784 if (*arg == '[')
785 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200786 else if (expr[0] == '.' && expr[1] == '=')
787 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200788 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200789 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200790 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200791 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100792 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
793 semsg(_(e_trailing_arg), argend);
794 else
795 // Vim9 declaration ":var name: type"
796 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200797 }
798 else
799 {
800 // ":let var1 var2" - list values
801 arg = list_arg_vars(eap, arg, &first);
802 }
803 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200804 else if (!eap->skip)
805 {
806 // ":let"
807 list_glob_vars(&first);
808 list_buf_vars(&first);
809 list_win_vars(&first);
810 list_tab_vars(&first);
811 list_script_vars(&first);
812 list_func_vars(&first);
813 list_vim_vars(&first);
814 }
Bram Moolenaar63b91732021-08-05 20:40:03 +0200815 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200816 }
817 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
818 {
819 list_T *l;
Bram Moolenaar81530e32021-07-28 21:25:49 +0200820 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200821
822 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200823 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200824 if (l != NULL)
825 {
826 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200827 if (!eap->skip)
828 {
Bram Moolenaar81530e32021-07-28 21:25:49 +0200829 // errors are for the assignment, not the end marker
830 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200831 op[0] = '=';
832 op[1] = NUL;
833 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200835 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200836 clear_tv(&rettv);
837 }
838 }
839 else
840 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200841 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200842 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200843
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200844 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200845 i = FAIL;
846 if (has_assign || concat)
847 {
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100848 int cur_lnum;
849
Bram Moolenaar32e35112020-05-14 22:41:15 +0200850 op[0] = '=';
851 op[1] = NUL;
852 if (*expr != '=')
853 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200854 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200855 {
856 // +=, /=, etc. require an existing variable
857 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
858 i = FAIL;
859 }
860 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200861 {
862 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200863 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200864 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200865 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200866 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200867 ++len;
868 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200869 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200870 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200871 }
872 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200873 ++expr;
874
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200875 if (vim9script && (!VIM_ISWHITE(*argend)
876 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200877 {
878 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100879 semsg(_(e_white_space_required_before_and_after_str_at_str),
880 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200881 i = FAIL;
882 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200883
884 if (eap->skip)
885 ++emsg_skip;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200886 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200887 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100888 cur_lnum = SOURCING_LNUM;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200889 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200890 if (eap->skip)
891 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200892 clear_evalarg(&evalarg, eap);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100893
894 // Restore the line number so that any type error is given for the
895 // declaration, not the expression.
896 SOURCING_LNUM = cur_lnum;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200897 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200898 if (eap->skip)
899 {
900 if (i != FAIL)
901 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200902 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200903 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200904 {
905 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200906 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200907 clear_tv(&rettv);
908 }
909 }
910}
911
912/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +0100913 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200914 * Handles both "var" with any type and "[var, var; var]" with a list type.
915 * When "op" is not NULL it points to a string with characters that
916 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
917 * or concatenate.
918 * Returns OK or FAIL;
919 */
920 int
921ex_let_vars(
922 char_u *arg_start,
923 typval_T *tv,
924 int copy, // copy values from "tv", don't move
925 int semicolon, // from skip_var_list()
926 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100927 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200928 char_u *op)
929{
930 char_u *arg = arg_start;
931 list_T *l;
932 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100933 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200934 listitem_T *item;
935 typval_T ltv;
936
937 if (*arg != '[')
938 {
939 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100940 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200941 return FAIL;
942 return OK;
943 }
944
945 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
946 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
947 {
948 emsg(_(e_listreq));
949 return FAIL;
950 }
951
952 i = list_len(l);
953 if (semicolon == 0 && var_count < i)
954 {
955 emsg(_("E687: Less targets than List items"));
956 return FAIL;
957 }
958 if (var_count - semicolon > i)
959 {
960 emsg(_("E688: More targets than List items"));
961 return FAIL;
962 }
963
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200964 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200965 item = l->lv_first;
966 while (*arg != ']')
967 {
968 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100969 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +0200970 arg = ex_let_one(arg, &item->li_tv, TRUE,
971 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200972 item = item->li_next;
973 if (arg == NULL)
974 return FAIL;
975
976 arg = skipwhite(arg);
977 if (*arg == ';')
978 {
979 // Put the rest of the list (may be empty) in the var after ';'.
980 // Create a new list for this.
981 l = list_alloc();
982 if (l == NULL)
983 return FAIL;
984 while (item != NULL)
985 {
986 list_append_tv(l, &item->li_tv);
987 item = item->li_next;
988 }
989
990 ltv.v_type = VAR_LIST;
991 ltv.v_lock = 0;
992 ltv.vval.v_list = l;
993 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100994 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200995
Bram Moolenaarf93bbd02021-04-10 22:35:43 +0200996 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
997 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200998 clear_tv(&ltv);
999 if (arg == NULL)
1000 return FAIL;
1001 break;
1002 }
1003 else if (*arg != ',' && *arg != ']')
1004 {
1005 internal_error("ex_let_vars()");
1006 return FAIL;
1007 }
1008 }
1009
1010 return OK;
1011}
1012
1013/*
1014 * Skip over assignable variable "var" or list of variables "[var, var]".
1015 * Used for ":let varvar = expr" and ":for varvar in expr".
1016 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001017 * for "[var, var; var]" set "semicolon" to 1.
1018 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001019 * Return NULL for an error.
1020 */
1021 char_u *
1022skip_var_list(
1023 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001024 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001025 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001026 int *semicolon,
1027 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001028{
1029 char_u *p, *s;
1030
1031 if (*arg == '[')
1032 {
1033 // "[var, var]": find the matching ']'.
1034 p = arg;
1035 for (;;)
1036 {
1037 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001038 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001039 if (s == p)
1040 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001041 if (!silent)
1042 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001043 return NULL;
1044 }
1045 ++*var_count;
1046
1047 p = skipwhite(s);
1048 if (*p == ']')
1049 break;
1050 else if (*p == ';')
1051 {
1052 if (*semicolon == 1)
1053 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001054 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001055 return NULL;
1056 }
1057 *semicolon = 1;
1058 }
1059 else if (*p != ',')
1060 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001061 if (!silent)
1062 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001063 return NULL;
1064 }
1065 }
1066 return p + 1;
1067 }
1068 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001069 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001070}
1071
1072/*
1073 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1074 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001076 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001077 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001078skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001079{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001080 char_u *end;
1081 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001082
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001083 if (*arg == '@' && arg[1] != NUL)
1084 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001085 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001086 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001087
1088 // "a: type" is declaring variable "a" with a type, not "a:".
1089 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001090 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001091 --end;
1092
Bram Moolenaar585587d2021-01-17 20:52:13 +01001093 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001095 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001096 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 }
1098 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001099}
1100
1101/*
1102 * List variables for hashtab "ht" with prefix "prefix".
1103 * If "empty" is TRUE also list NULL strings as empty strings.
1104 */
1105 void
1106list_hashtable_vars(
1107 hashtab_T *ht,
1108 char *prefix,
1109 int empty,
1110 int *first)
1111{
1112 hashitem_T *hi;
1113 dictitem_T *di;
1114 int todo;
1115 char_u buf[IOSIZE];
1116
1117 todo = (int)ht->ht_used;
1118 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1119 {
1120 if (!HASHITEM_EMPTY(hi))
1121 {
1122 --todo;
1123 di = HI2DI(hi);
1124
1125 // apply :filter /pat/ to variable name
1126 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1127 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1128 if (message_filtered(buf))
1129 continue;
1130
1131 if (empty || di->di_tv.v_type != VAR_STRING
1132 || di->di_tv.vval.v_string != NULL)
1133 list_one_var(di, prefix, first);
1134 }
1135 }
1136}
1137
1138/*
1139 * List global variables.
1140 */
1141 static void
1142list_glob_vars(int *first)
1143{
1144 list_hashtable_vars(&globvarht, "", TRUE, first);
1145}
1146
1147/*
1148 * List buffer variables.
1149 */
1150 static void
1151list_buf_vars(int *first)
1152{
1153 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1154}
1155
1156/*
1157 * List window variables.
1158 */
1159 static void
1160list_win_vars(int *first)
1161{
1162 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1163}
1164
1165/*
1166 * List tab page variables.
1167 */
1168 static void
1169list_tab_vars(int *first)
1170{
1171 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1172}
1173
1174/*
1175 * List variables in "arg".
1176 */
1177 static char_u *
1178list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1179{
1180 int error = FALSE;
1181 int len;
1182 char_u *name;
1183 char_u *name_start;
1184 char_u *arg_subsc;
1185 char_u *tofree;
1186 typval_T tv;
1187
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001188 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001189 {
1190 if (error || eap->skip)
1191 {
1192 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1193 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1194 {
1195 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001196 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001197 break;
1198 }
1199 }
1200 else
1201 {
1202 // get_name_len() takes care of expanding curly braces
1203 name_start = name = arg;
1204 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1205 if (len <= 0)
1206 {
1207 // This is mainly to keep test 49 working: when expanding
1208 // curly braces fails overrule the exception error message.
1209 if (len < 0 && !aborting())
1210 {
1211 emsg_severe = TRUE;
1212 semsg(_(e_invarg2), arg);
1213 break;
1214 }
1215 error = TRUE;
1216 }
1217 else
1218 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001219 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001220 if (tofree != NULL)
1221 name = tofree;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001222 if (eval_variable(name, len, &tv, NULL,
1223 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001224 error = TRUE;
1225 else
1226 {
1227 // handle d.key, l[idx], f(expr)
1228 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001229 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001230 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001231 error = TRUE;
1232 else
1233 {
1234 if (arg == arg_subsc && len == 2 && name[1] == ':')
1235 {
1236 switch (*name)
1237 {
1238 case 'g': list_glob_vars(first); break;
1239 case 'b': list_buf_vars(first); break;
1240 case 'w': list_win_vars(first); break;
1241 case 't': list_tab_vars(first); break;
1242 case 'v': list_vim_vars(first); break;
1243 case 's': list_script_vars(first); break;
1244 case 'l': list_func_vars(first); break;
1245 default:
1246 semsg(_("E738: Can't list variables for %s"), name);
1247 }
1248 }
1249 else
1250 {
1251 char_u numbuf[NUMBUFLEN];
1252 char_u *tf;
1253 int c;
1254 char_u *s;
1255
1256 s = echo_string(&tv, &tf, numbuf, 0);
1257 c = *arg;
1258 *arg = NUL;
1259 list_one_var_a("",
1260 arg == arg_subsc ? name : name_start,
1261 tv.v_type,
1262 s == NULL ? (char_u *)"" : s,
1263 first);
1264 *arg = c;
1265 vim_free(tf);
1266 }
1267 clear_tv(&tv);
1268 }
1269 }
1270 }
1271
1272 vim_free(tofree);
1273 }
1274
1275 arg = skipwhite(arg);
1276 }
1277
1278 return arg;
1279}
1280
1281/*
1282 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1283 * Returns a pointer to the char just after the var name.
1284 * Returns NULL if there is an error.
1285 */
1286 static char_u *
1287ex_let_one(
1288 char_u *arg, // points to variable name
1289 typval_T *tv, // value to assign to variable
1290 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001291 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001292 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001293 char_u *op, // "+", "-", "." or NULL
1294 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001295{
1296 int c1;
1297 char_u *name;
1298 char_u *p;
1299 char_u *arg_end = NULL;
1300 int len;
1301 int opt_flags;
1302 char_u *tofree = NULL;
1303
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001304 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001305 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001306 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1307 {
1308 vim9_declare_error(arg);
1309 return NULL;
1310 }
1311
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001312 // ":let $VAR = expr": Set environment variable.
1313 if (*arg == '$')
1314 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001315 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1316 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001317 {
1318 emsg(_("E996: Cannot lock an environment variable"));
1319 return NULL;
1320 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001321
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001322 // Find the end of the name.
1323 ++arg;
1324 name = arg;
1325 len = get_env_len(&arg);
1326 if (len == 0)
1327 semsg(_(e_invarg2), name - 1);
1328 else
1329 {
1330 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1331 semsg(_(e_letwrong), op);
1332 else if (endchars != NULL
1333 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02001334 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001335 else if (!check_secure())
1336 {
1337 c1 = name[len];
1338 name[len] = NUL;
1339 p = tv_get_string_chk(tv);
1340 if (p != NULL && op != NULL && *op == '.')
1341 {
1342 int mustfree = FALSE;
1343 char_u *s = vim_getenv(name, &mustfree);
1344
1345 if (s != NULL)
1346 {
1347 p = tofree = concat_str(s, p);
1348 if (mustfree)
1349 vim_free(s);
1350 }
1351 }
1352 if (p != NULL)
1353 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001354 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001355 arg_end = arg;
1356 }
1357 name[len] = c1;
1358 vim_free(tofree);
1359 }
1360 }
1361 }
1362
1363 // ":let &option = expr": Set option value.
1364 // ":let &l:option = expr": Set local option value.
1365 // ":let &g:option = expr": Set global option value.
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001366 // ":for &ts in range(8)": Set option value for for loop
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001367 else if (*arg == '&')
1368 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001369 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1370 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001371 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001372 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001373 return NULL;
1374 }
1375 // Find the end of the name.
1376 p = find_option_end(&arg, &opt_flags);
1377 if (p == NULL || (endchars != NULL
1378 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaar108010a2021-06-27 22:03:33 +02001379 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001380 else
1381 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001382 long n = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001383 getoption_T opt_type;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001384 long numval;
1385 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001386 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001387 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001388
1389 c1 = *p;
1390 *p = NUL;
1391
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001392 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001393 if ((opt_type == gov_bool
1394 || opt_type == gov_number
1395 || opt_type == gov_hidden_bool
1396 || opt_type == gov_hidden_number)
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001397 && (tv->v_type != VAR_STRING || !in_vim9script()))
Bram Moolenaar0ea04402021-01-04 13:37:54 +01001398 {
1399 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1400 // bool, possibly hidden
1401 n = (long)tv_get_bool(tv);
1402 else
1403 // number, possibly hidden
1404 n = (long)tv_get_number(tv);
1405 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001406
1407 // Avoid setting a string option to the text "v:false" or similar.
1408 // In Vim9 script also don't convert a number to string.
1409 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1410 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1411 s = tv_get_string_chk(tv);
1412
1413 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001414 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001415 if (((opt_type == gov_bool || opt_type == gov_number)
1416 && *op == '.')
1417 || (opt_type == gov_string && *op != '.'))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001418 {
1419 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001420 failed = TRUE; // don't set the value
1421
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001422 }
1423 else
1424 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001425 // number, in legacy script also bool
1426 if (opt_type == gov_number
1427 || (opt_type == gov_bool && !in_vim9script()))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001428 {
1429 switch (*op)
1430 {
1431 case '+': n = numval + n; break;
1432 case '-': n = numval - n; break;
1433 case '*': n = numval * n; break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001434 case '/': n = (long)num_divide(numval, n,
1435 &failed); break;
1436 case '%': n = (long)num_modulus(numval, n,
1437 &failed); break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001438 }
Bram Moolenaara42e6e02021-06-10 18:43:25 +02001439 s = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001440 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001441 else if (opt_type == gov_string
1442 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001443 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001444 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001445 s = concat_str(stringval, s);
1446 vim_free(stringval);
1447 stringval = s;
1448 }
1449 }
1450 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001451
1452 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001453 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001454 if (opt_type != gov_string || s != NULL)
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001455 {
1456 set_option_value(arg, n, s, opt_flags);
1457 arg_end = p;
1458 }
1459 else
1460 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001461 }
1462 *p = c1;
1463 vim_free(stringval);
1464 }
1465 }
1466
1467 // ":let @r = expr": Set register contents.
1468 else if (*arg == '@')
1469 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001470 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1471 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001472 {
1473 emsg(_("E996: Cannot lock a register"));
1474 return NULL;
1475 }
1476 ++arg;
1477 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1478 semsg(_(e_letwrong), op);
1479 else if (endchars != NULL
1480 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02001481 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001482 else
1483 {
1484 char_u *ptofree = NULL;
1485 char_u *s;
1486
1487 p = tv_get_string_chk(tv);
1488 if (p != NULL && op != NULL && *op == '.')
1489 {
1490 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1491 if (s != NULL)
1492 {
1493 p = ptofree = concat_str(s, p);
1494 vim_free(s);
1495 }
1496 }
1497 if (p != NULL)
1498 {
1499 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1500 arg_end = arg + 1;
1501 }
1502 vim_free(ptofree);
1503 }
1504 }
1505
1506 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001508 // ":let {expr} = expr": Idem, name made with curly braces
1509 else if (eval_isnamec1(*arg) || *arg == '{')
1510 {
1511 lval_T lv;
1512
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001513 p = get_lval(arg, tv, &lv, FALSE, FALSE,
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001514 (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1515 ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001516 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001517 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 if (endchars != NULL && vim_strchr(endchars,
1519 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02001520 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001521 else
1522 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001523 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001524 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001525 }
1526 }
1527 clear_lval(&lv);
1528 }
1529
1530 else
1531 semsg(_(e_invarg2), arg);
1532
1533 return arg_end;
1534}
1535
1536/*
1537 * ":unlet[!] var1 ... " command.
1538 */
1539 void
1540ex_unlet(exarg_T *eap)
1541{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001542 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001543}
1544
1545/*
1546 * ":lockvar" and ":unlockvar" commands
1547 */
1548 void
1549ex_lockvar(exarg_T *eap)
1550{
1551 char_u *arg = eap->arg;
1552 int deep = 2;
1553
1554 if (eap->forceit)
1555 deep = -1;
1556 else if (vim_isdigit(*arg))
1557 {
1558 deep = getdigits(&arg);
1559 arg = skipwhite(arg);
1560 }
1561
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001562 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001563}
1564
1565/*
1566 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001567 * Also used for Vim9 script. "callback" is invoked as:
1568 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001569 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001570 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001571ex_unletlock(
1572 exarg_T *eap,
1573 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001574 int deep,
1575 int glv_flags,
1576 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1577 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001578{
1579 char_u *arg = argstart;
1580 char_u *name_end;
1581 int error = FALSE;
1582 lval_T lv;
1583
1584 do
1585 {
1586 if (*arg == '$')
1587 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001588 lv.ll_name = arg;
1589 lv.ll_tv = NULL;
1590 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001591 if (get_env_len(&arg) == 0)
1592 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001593 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001594 return;
1595 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001596 if (!error && !eap->skip
1597 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1598 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001599 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001600 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001601 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001602 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001603 // Parse the name and find the end.
1604 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001605 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001606 if (lv.ll_name == NULL)
1607 error = TRUE; // error but continue parsing
1608 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1609 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001610 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001611 if (name_end != NULL)
1612 {
1613 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001614 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001615 }
1616 if (!(eap->skip || error))
1617 clear_lval(&lv);
1618 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001619 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001620
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001621 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001622 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001623 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001624
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001625 if (!eap->skip)
1626 clear_lval(&lv);
1627 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001628
1629 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001630 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001631
Bram Moolenaar63b91732021-08-05 20:40:03 +02001632 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001633}
1634
1635 static int
1636do_unlet_var(
1637 lval_T *lp,
1638 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001639 exarg_T *eap,
1640 int deep UNUSED,
1641 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001642{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001643 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001644 int ret = OK;
1645 int cc;
1646
1647 if (lp->ll_tv == NULL)
1648 {
1649 cc = *name_end;
1650 *name_end = NUL;
1651
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001652 // Environment variable, normal name or expanded name.
1653 if (*lp->ll_name == '$')
1654 vim_unsetenv(lp->ll_name + 1);
1655 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001656 ret = FAIL;
1657 *name_end = cc;
1658 }
1659 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001660 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001661 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001662 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001663 return FAIL;
1664 else if (lp->ll_range)
1665 {
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001666 if (list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_name, lp->ll_n1,
1667 !lp->ll_empty2, lp->ll_n2) == FAIL)
1668 return FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001669 }
1670 else
1671 {
1672 if (lp->ll_list != NULL)
1673 // unlet a List item.
1674 listitem_remove(lp->ll_list, lp->ll_li);
1675 else
1676 // unlet a Dictionary item.
1677 dictitem_remove(lp->ll_dict, lp->ll_di);
1678 }
1679
1680 return ret;
1681}
1682
1683/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001684 * Unlet one item or a range of items from a list.
1685 * Return OK or FAIL.
1686 */
1687 int
1688list_unlet_range(
1689 list_T *l,
1690 listitem_T *li_first,
1691 char_u *name,
1692 long n1_arg,
1693 int has_n2,
1694 long n2)
1695{
1696 listitem_T *li = li_first;
1697 int n1 = n1_arg;
1698
1699 while (li != NULL && (!has_n2 || n2 >= n1))
1700 {
1701 if (value_check_lock(li->li_tv.v_lock, name, FALSE))
1702 return FAIL;
1703 li = li->li_next;
1704 ++n1;
1705 }
1706
1707 // Delete a range of List items.
1708 li = li_first;
1709 n1 = n1_arg;
1710 while (li != NULL && (!has_n2 || n2 >= n1))
1711 {
1712 listitem_T *next = li->li_next;
1713
1714 listitem_remove(l, li);
1715 li = next;
1716 ++n1;
1717 }
1718 return OK;
1719}
1720/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001721 * "unlet" a variable. Return OK if it existed, FAIL if not.
1722 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1723 */
1724 int
1725do_unlet(char_u *name, int forceit)
1726{
1727 hashtab_T *ht;
1728 hashitem_T *hi;
1729 char_u *varname;
1730 dict_T *d;
1731 dictitem_T *di;
1732
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001733 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001734 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001735 return FAIL;
1736
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001737 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001738
1739 // can't :unlet a script variable in Vim9 script from a function
1740 if (ht == get_script_local_ht()
1741 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1742 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1743 == SCRIPT_VERSION_VIM9
1744 && check_vim9_unlet(name) == FAIL)
1745 return FAIL;
1746
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001747 if (ht != NULL && *varname != NUL)
1748 {
1749 d = get_current_funccal_dict(ht);
1750 if (d == NULL)
1751 {
1752 if (ht == &globvarht)
1753 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001754 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001755 d = &vimvardict;
1756 else
1757 {
1758 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1759 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1760 }
1761 if (d == NULL)
1762 {
1763 internal_error("do_unlet()");
1764 return FAIL;
1765 }
1766 }
1767 hi = hash_find(ht, varname);
1768 if (HASHITEM_EMPTY(hi))
1769 hi = find_hi_in_scoped_ht(name, &ht);
1770 if (hi != NULL && !HASHITEM_EMPTY(hi))
1771 {
1772 di = HI2DI(hi);
1773 if (var_check_fixed(di->di_flags, name, FALSE)
1774 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001775 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001776 return FAIL;
1777
1778 delete_var(ht, hi);
1779 return OK;
1780 }
1781 }
1782 if (forceit)
1783 return OK;
1784 semsg(_("E108: No such variable: \"%s\""), name);
1785 return FAIL;
1786}
1787
1788/*
1789 * Lock or unlock variable indicated by "lp".
1790 * "deep" is the levels to go (-1 for unlimited);
1791 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1792 */
1793 static int
1794do_lock_var(
1795 lval_T *lp,
1796 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001797 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001798 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001799 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001800{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001801 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001802 int ret = OK;
1803 int cc;
1804 dictitem_T *di;
1805
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001806 if (lp->ll_tv == NULL)
1807 {
1808 cc = *name_end;
1809 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001810 if (*lp->ll_name == '$')
1811 {
1812 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001813 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001814 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001815 else
1816 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001817 // Normal name or expanded name.
1818 di = find_var(lp->ll_name, NULL, TRUE);
1819 if (di == NULL)
1820 ret = FAIL;
1821 else if ((di->di_flags & DI_FLAGS_FIX)
1822 && di->di_tv.v_type != VAR_DICT
1823 && di->di_tv.v_type != VAR_LIST)
1824 {
1825 // For historic reasons this error is not given for a list or
1826 // dict. E.g., the b: dict could be locked/unlocked.
1827 semsg(_(e_lock_unlock), lp->ll_name);
1828 ret = FAIL;
1829 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001830 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001831 {
1832 if (lock)
1833 di->di_flags |= DI_FLAGS_LOCK;
1834 else
1835 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001836 if (deep != 0)
1837 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001838 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001839 }
1840 *name_end = cc;
1841 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001842 else if (deep == 0)
1843 {
1844 // nothing to do
1845 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001846 else if (lp->ll_range)
1847 {
1848 listitem_T *li = lp->ll_li;
1849
1850 // (un)lock a range of List items.
1851 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1852 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001853 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001854 li = li->li_next;
1855 ++lp->ll_n1;
1856 }
1857 }
1858 else if (lp->ll_list != NULL)
1859 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001860 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001861 else
1862 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001863 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001864
1865 return ret;
1866}
1867
1868/*
1869 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001870 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1871 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001872 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001873 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001874item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001875{
1876 static int recurse = 0;
1877 list_T *l;
1878 listitem_T *li;
1879 dict_T *d;
1880 blob_T *b;
1881 hashitem_T *hi;
1882 int todo;
1883
1884 if (recurse >= DICT_MAXNEST)
1885 {
1886 emsg(_("E743: variable nested too deep for (un)lock"));
1887 return;
1888 }
1889 if (deep == 0)
1890 return;
1891 ++recurse;
1892
1893 // lock/unlock the item itself
1894 if (lock)
1895 tv->v_lock |= VAR_LOCKED;
1896 else
1897 tv->v_lock &= ~VAR_LOCKED;
1898
1899 switch (tv->v_type)
1900 {
1901 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001902 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001903 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001904 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001906 case VAR_STRING:
1907 case VAR_FUNC:
1908 case VAR_PARTIAL:
1909 case VAR_FLOAT:
1910 case VAR_SPECIAL:
1911 case VAR_JOB:
1912 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001913 case VAR_INSTR:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001914 break;
1915
1916 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001917 if ((b = tv->vval.v_blob) != NULL
1918 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001919 {
1920 if (lock)
1921 b->bv_lock |= VAR_LOCKED;
1922 else
1923 b->bv_lock &= ~VAR_LOCKED;
1924 }
1925 break;
1926 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001927 if ((l = tv->vval.v_list) != NULL
1928 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001929 {
1930 if (lock)
1931 l->lv_lock |= VAR_LOCKED;
1932 else
1933 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001934 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001935 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001936 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001937 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001938 }
1939 break;
1940 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001941 if ((d = tv->vval.v_dict) != NULL
1942 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001943 {
1944 if (lock)
1945 d->dv_lock |= VAR_LOCKED;
1946 else
1947 d->dv_lock &= ~VAR_LOCKED;
1948 if (deep < 0 || deep > 1)
1949 {
1950 // recursive: lock/unlock the items the List contains
1951 todo = (int)d->dv_hashtab.ht_used;
1952 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1953 {
1954 if (!HASHITEM_EMPTY(hi))
1955 {
1956 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001957 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1958 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001959 }
1960 }
1961 }
1962 }
1963 }
1964 --recurse;
1965}
1966
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001967#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1968/*
1969 * Delete all "menutrans_" variables.
1970 */
1971 void
1972del_menutrans_vars(void)
1973{
1974 hashitem_T *hi;
1975 int todo;
1976
1977 hash_lock(&globvarht);
1978 todo = (int)globvarht.ht_used;
1979 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1980 {
1981 if (!HASHITEM_EMPTY(hi))
1982 {
1983 --todo;
1984 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1985 delete_var(&globvarht, hi);
1986 }
1987 }
1988 hash_unlock(&globvarht);
1989}
1990#endif
1991
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001992/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001993 * Local string buffer for the next two functions to store a variable name
1994 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1995 * get_user_var_name().
1996 */
1997
1998static char_u *varnamebuf = NULL;
1999static int varnamebuflen = 0;
2000
2001/*
2002 * Function to concatenate a prefix and a variable name.
2003 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002004 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002005cat_prefix_varname(int prefix, char_u *name)
2006{
2007 int len;
2008
2009 len = (int)STRLEN(name) + 3;
2010 if (len > varnamebuflen)
2011 {
2012 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002013 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002014 varnamebuf = alloc(len);
2015 if (varnamebuf == NULL)
2016 {
2017 varnamebuflen = 0;
2018 return NULL;
2019 }
2020 varnamebuflen = len;
2021 }
2022 *varnamebuf = prefix;
2023 varnamebuf[1] = ':';
2024 STRCPY(varnamebuf + 2, name);
2025 return varnamebuf;
2026}
2027
2028/*
2029 * Function given to ExpandGeneric() to obtain the list of user defined
2030 * (global/buffer/window/built-in) variable names.
2031 */
2032 char_u *
2033get_user_var_name(expand_T *xp, int idx)
2034{
2035 static long_u gdone;
2036 static long_u bdone;
2037 static long_u wdone;
2038 static long_u tdone;
2039 static int vidx;
2040 static hashitem_T *hi;
2041 hashtab_T *ht;
2042
2043 if (idx == 0)
2044 {
2045 gdone = bdone = wdone = vidx = 0;
2046 tdone = 0;
2047 }
2048
2049 // Global variables
2050 if (gdone < globvarht.ht_used)
2051 {
2052 if (gdone++ == 0)
2053 hi = globvarht.ht_array;
2054 else
2055 ++hi;
2056 while (HASHITEM_EMPTY(hi))
2057 ++hi;
2058 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2059 return cat_prefix_varname('g', hi->hi_key);
2060 return hi->hi_key;
2061 }
2062
2063 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002064 ht =
2065#ifdef FEAT_CMDWIN
2066 // In cmdwin, the alternative buffer should be used.
2067 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2068 &prevwin->w_buffer->b_vars->dv_hashtab :
2069#endif
2070 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002071 if (bdone < ht->ht_used)
2072 {
2073 if (bdone++ == 0)
2074 hi = ht->ht_array;
2075 else
2076 ++hi;
2077 while (HASHITEM_EMPTY(hi))
2078 ++hi;
2079 return cat_prefix_varname('b', hi->hi_key);
2080 }
2081
2082 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002083 ht =
2084#ifdef FEAT_CMDWIN
2085 // In cmdwin, the alternative window should be used.
2086 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2087 &prevwin->w_vars->dv_hashtab :
2088#endif
2089 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002090 if (wdone < ht->ht_used)
2091 {
2092 if (wdone++ == 0)
2093 hi = ht->ht_array;
2094 else
2095 ++hi;
2096 while (HASHITEM_EMPTY(hi))
2097 ++hi;
2098 return cat_prefix_varname('w', hi->hi_key);
2099 }
2100
2101 // t: variables
2102 ht = &curtab->tp_vars->dv_hashtab;
2103 if (tdone < ht->ht_used)
2104 {
2105 if (tdone++ == 0)
2106 hi = ht->ht_array;
2107 else
2108 ++hi;
2109 while (HASHITEM_EMPTY(hi))
2110 ++hi;
2111 return cat_prefix_varname('t', hi->hi_key);
2112 }
2113
2114 // v: variables
2115 if (vidx < VV_LEN)
2116 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2117
2118 VIM_CLEAR(varnamebuf);
2119 varnamebuflen = 0;
2120 return NULL;
2121}
2122
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002123 char *
2124get_var_special_name(int nr)
2125{
2126 switch (nr)
2127 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002128 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2129 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002130 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002131 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002132 }
2133 internal_error("get_var_special_name()");
2134 return "42";
2135}
2136
2137/*
2138 * Returns the global variable dictionary
2139 */
2140 dict_T *
2141get_globvar_dict(void)
2142{
2143 return &globvardict;
2144}
2145
2146/*
2147 * Returns the global variable hash table
2148 */
2149 hashtab_T *
2150get_globvar_ht(void)
2151{
2152 return &globvarht;
2153}
2154
2155/*
2156 * Returns the v: variable dictionary
2157 */
2158 dict_T *
2159get_vimvar_dict(void)
2160{
2161 return &vimvardict;
2162}
2163
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002164/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002165 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002166 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002167 */
2168 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002169find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002170{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002171 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2172 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002173
2174 if (di == NULL)
2175 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002176 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2178 return (int)(vv - vimvars);
2179}
2180
2181
2182/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002183 * Set type of v: variable to "type".
2184 */
2185 void
2186set_vim_var_type(int idx, vartype_T type)
2187{
2188 vimvars[idx].vv_type = type;
2189}
2190
2191/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002192 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002193 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002194 */
2195 void
2196set_vim_var_nr(int idx, varnumber_T val)
2197{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002198 vimvars[idx].vv_nr = val;
2199}
2200
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002201 char *
2202get_vim_var_name(int idx)
2203{
2204 return vimvars[idx].vv_name;
2205}
2206
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002207/*
2208 * Get typval_T v: variable value.
2209 */
2210 typval_T *
2211get_vim_var_tv(int idx)
2212{
2213 return &vimvars[idx].vv_tv;
2214}
2215
2216/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002217 * Set v: variable to "tv". Only accepts the same type.
2218 * Takes over the value of "tv".
2219 */
2220 int
2221set_vim_var_tv(int idx, typval_T *tv)
2222{
2223 if (vimvars[idx].vv_type != tv->v_type)
2224 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002225 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002226 clear_tv(tv);
2227 return FAIL;
2228 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002229 // VV_RO is also checked when compiling, but let's check here as well.
2230 if (vimvars[idx].vv_flags & VV_RO)
2231 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002232 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002233 return FAIL;
2234 }
2235 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2236 {
2237 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2238 return FAIL;
2239 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002240 clear_tv(&vimvars[idx].vv_di.di_tv);
2241 vimvars[idx].vv_di.di_tv = *tv;
2242 return OK;
2243}
2244
2245/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002246 * Get number v: variable value.
2247 */
2248 varnumber_T
2249get_vim_var_nr(int idx)
2250{
2251 return vimvars[idx].vv_nr;
2252}
2253
2254/*
2255 * Get string v: variable value. Uses a static buffer, can only be used once.
2256 * If the String variable has never been set, return an empty string.
2257 * Never returns NULL;
2258 */
2259 char_u *
2260get_vim_var_str(int idx)
2261{
2262 return tv_get_string(&vimvars[idx].vv_tv);
2263}
2264
2265/*
2266 * Get List v: variable value. Caller must take care of reference count when
2267 * needed.
2268 */
2269 list_T *
2270get_vim_var_list(int idx)
2271{
2272 return vimvars[idx].vv_list;
2273}
2274
2275/*
2276 * Get Dict v: variable value. Caller must take care of reference count when
2277 * needed.
2278 */
2279 dict_T *
2280get_vim_var_dict(int idx)
2281{
2282 return vimvars[idx].vv_dict;
2283}
2284
2285/*
2286 * Set v:char to character "c".
2287 */
2288 void
2289set_vim_var_char(int c)
2290{
2291 char_u buf[MB_MAXBYTES + 1];
2292
2293 if (has_mbyte)
2294 buf[(*mb_char2bytes)(c, buf)] = NUL;
2295 else
2296 {
2297 buf[0] = c;
2298 buf[1] = NUL;
2299 }
2300 set_vim_var_string(VV_CHAR, buf, -1);
2301}
2302
2303/*
2304 * Set v:count to "count" and v:count1 to "count1".
2305 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2306 */
2307 void
2308set_vcount(
2309 long count,
2310 long count1,
2311 int set_prevcount)
2312{
2313 if (set_prevcount)
2314 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2315 vimvars[VV_COUNT].vv_nr = count;
2316 vimvars[VV_COUNT1].vv_nr = count1;
2317}
2318
2319/*
2320 * Save variables that might be changed as a side effect. Used when executing
2321 * a timer callback.
2322 */
2323 void
2324save_vimvars(vimvars_save_T *vvsave)
2325{
2326 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2327 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2328 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2329}
2330
2331/*
2332 * Restore variables saved by save_vimvars().
2333 */
2334 void
2335restore_vimvars(vimvars_save_T *vvsave)
2336{
2337 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2338 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2339 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2340}
2341
2342/*
2343 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2344 * value.
2345 */
2346 void
2347set_vim_var_string(
2348 int idx,
2349 char_u *val,
2350 int len) // length of "val" to use or -1 (whole string)
2351{
2352 clear_tv(&vimvars[idx].vv_di.di_tv);
2353 vimvars[idx].vv_type = VAR_STRING;
2354 if (val == NULL)
2355 vimvars[idx].vv_str = NULL;
2356 else if (len == -1)
2357 vimvars[idx].vv_str = vim_strsave(val);
2358 else
2359 vimvars[idx].vv_str = vim_strnsave(val, len);
2360}
2361
2362/*
2363 * Set List v: variable to "val".
2364 */
2365 void
2366set_vim_var_list(int idx, list_T *val)
2367{
2368 clear_tv(&vimvars[idx].vv_di.di_tv);
2369 vimvars[idx].vv_type = VAR_LIST;
2370 vimvars[idx].vv_list = val;
2371 if (val != NULL)
2372 ++val->lv_refcount;
2373}
2374
2375/*
2376 * Set Dictionary v: variable to "val".
2377 */
2378 void
2379set_vim_var_dict(int idx, dict_T *val)
2380{
2381 clear_tv(&vimvars[idx].vv_di.di_tv);
2382 vimvars[idx].vv_type = VAR_DICT;
2383 vimvars[idx].vv_dict = val;
2384 if (val != NULL)
2385 {
2386 ++val->dv_refcount;
2387 dict_set_items_ro(val);
2388 }
2389}
2390
2391/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002392 * Set the v:argv list.
2393 */
2394 void
2395set_argv_var(char **argv, int argc)
2396{
2397 list_T *l = list_alloc();
2398 int i;
2399
2400 if (l == NULL)
2401 getout(1);
2402 l->lv_lock = VAR_FIXED;
2403 for (i = 0; i < argc; ++i)
2404 {
2405 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2406 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002407 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002408 }
2409 set_vim_var_list(VV_ARGV, l);
2410}
2411
2412/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002413 * Reset v:register, taking the 'clipboard' setting into account.
2414 */
2415 void
2416reset_reg_var(void)
2417{
2418 int regname = 0;
2419
2420 // Adjust the register according to 'clipboard', so that when
2421 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2422#ifdef FEAT_CLIPBOARD
2423 adjust_clip_reg(&regname);
2424#endif
2425 set_reg_var(regname);
2426}
2427
2428/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002429 * Set v:register if needed.
2430 */
2431 void
2432set_reg_var(int c)
2433{
2434 char_u regname;
2435
2436 if (c == 0 || c == ' ')
2437 regname = '"';
2438 else
2439 regname = c;
2440 // Avoid free/alloc when the value is already right.
2441 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2442 set_vim_var_string(VV_REG, &regname, 1);
2443}
2444
2445/*
2446 * Get or set v:exception. If "oldval" == NULL, return the current value.
2447 * Otherwise, restore the value to "oldval" and return NULL.
2448 * Must always be called in pairs to save and restore v:exception! Does not
2449 * take care of memory allocations.
2450 */
2451 char_u *
2452v_exception(char_u *oldval)
2453{
2454 if (oldval == NULL)
2455 return vimvars[VV_EXCEPTION].vv_str;
2456
2457 vimvars[VV_EXCEPTION].vv_str = oldval;
2458 return NULL;
2459}
2460
2461/*
2462 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2463 * Otherwise, restore the value to "oldval" and return NULL.
2464 * Must always be called in pairs to save and restore v:throwpoint! Does not
2465 * take care of memory allocations.
2466 */
2467 char_u *
2468v_throwpoint(char_u *oldval)
2469{
2470 if (oldval == NULL)
2471 return vimvars[VV_THROWPOINT].vv_str;
2472
2473 vimvars[VV_THROWPOINT].vv_str = oldval;
2474 return NULL;
2475}
2476
2477/*
2478 * Set v:cmdarg.
2479 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2480 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2481 * Must always be called in pairs!
2482 */
2483 char_u *
2484set_cmdarg(exarg_T *eap, char_u *oldarg)
2485{
2486 char_u *oldval;
2487 char_u *newval;
2488 unsigned len;
2489
2490 oldval = vimvars[VV_CMDARG].vv_str;
2491 if (eap == NULL)
2492 {
2493 vim_free(oldval);
2494 vimvars[VV_CMDARG].vv_str = oldarg;
2495 return NULL;
2496 }
2497
2498 if (eap->force_bin == FORCE_BIN)
2499 len = 6;
2500 else if (eap->force_bin == FORCE_NOBIN)
2501 len = 8;
2502 else
2503 len = 0;
2504
2505 if (eap->read_edit)
2506 len += 7;
2507
2508 if (eap->force_ff != 0)
2509 len += 10; // " ++ff=unix"
2510 if (eap->force_enc != 0)
2511 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2512 if (eap->bad_char != 0)
2513 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2514
2515 newval = alloc(len + 1);
2516 if (newval == NULL)
2517 return NULL;
2518
2519 if (eap->force_bin == FORCE_BIN)
2520 sprintf((char *)newval, " ++bin");
2521 else if (eap->force_bin == FORCE_NOBIN)
2522 sprintf((char *)newval, " ++nobin");
2523 else
2524 *newval = NUL;
2525
2526 if (eap->read_edit)
2527 STRCAT(newval, " ++edit");
2528
2529 if (eap->force_ff != 0)
2530 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2531 eap->force_ff == 'u' ? "unix"
2532 : eap->force_ff == 'd' ? "dos"
2533 : "mac");
2534 if (eap->force_enc != 0)
2535 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2536 eap->cmd + eap->force_enc);
2537 if (eap->bad_char == BAD_KEEP)
2538 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2539 else if (eap->bad_char == BAD_DROP)
2540 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2541 else if (eap->bad_char != 0)
2542 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2543 vimvars[VV_CMDARG].vv_str = newval;
2544 return oldval;
2545}
2546
2547/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002548 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002549 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2550 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002551 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2552 */
2553 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002554eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002555 char_u *name,
2556 int len, // length of "name"
2557 typval_T *rettv, // NULL when only checking existence
2558 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002559 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002560{
2561 int ret = OK;
2562 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002563 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002564 hashtab_T *ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002565 int cc;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002566 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002567
2568 // truncate the name, so that we can use strcmp()
2569 cc = name[len];
2570 name[len] = NUL;
2571
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002572 // Check for local variable when debugging.
2573 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002574 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002575 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002576 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2577
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002578 if (v != NULL)
2579 {
2580 tv = &v->di_tv;
2581 if (dip != NULL)
2582 *dip = v;
2583 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002584 else
2585 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002586 }
2587
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002588 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002590 imported_T *import;
2591 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2592
2593 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594
2595 // imported variable from another script
2596 if (import != NULL)
2597 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002598 if (import->imp_funcname != NULL)
2599 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002600 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002601 if (rettv != NULL)
2602 {
2603 rettv->v_type = VAR_FUNC;
2604 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2605 }
2606 }
Bram Moolenaara6294952020-12-27 13:39:50 +01002607 else if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002608 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002609 if ((flags & EVAL_VAR_IMPORT) == 0)
2610 {
2611 if (flags & EVAL_VAR_VERBOSE)
2612 emsg(_(e_import_as_name_not_supported_here));
2613 ret = FAIL;
2614 }
2615 else
2616 {
2617 if (rettv != NULL)
2618 {
2619 rettv->v_type = VAR_ANY;
2620 rettv->vval.v_number = import->imp_sid;
2621 }
2622 found = TRUE;
2623 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002624 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002625 else
2626 {
2627 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002628 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002630 tv = sv->sv_tv;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002631 type = sv->sv_type;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002632 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002634 else if (in_vim9script())
2635 {
2636 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2637
2638 if (ufunc != NULL)
2639 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002640 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002641 if (rettv != NULL)
2642 {
2643 rettv->v_type = VAR_FUNC;
2644 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2645 }
2646 }
2647 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648 }
2649
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002650 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002651 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002652 if (tv == NULL)
2653 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002654 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002655 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002656 ret = FAIL;
2657 }
2658 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002659 {
Bram Moolenaar11005b02021-07-11 20:59:00 +02002660 if (ht != NULL && ht == get_script_local_ht()
2661 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002662 {
Bram Moolenaarc967d572021-07-08 21:38:50 +02002663 svar_T *sv = find_typval_in_script(tv);
Bram Moolenaarf055d452021-07-08 20:57:24 +02002664
Bram Moolenaarf055d452021-07-08 20:57:24 +02002665 if (sv != NULL)
2666 type = sv->sv_type;
2667 }
2668
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002669 // If a list or dict variable wasn't initialized, do it now.
2670 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2671 {
2672 tv->vval.v_dict = dict_alloc();
2673 if (tv->vval.v_dict != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002674 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002675 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002676 tv->vval.v_dict->dv_type = alloc_type(type);
2677 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002678 }
2679 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2680 {
2681 tv->vval.v_list = list_alloc();
2682 if (tv->vval.v_list != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002683 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002684 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002685 tv->vval.v_list->lv_type = alloc_type(type);
2686 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002687 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002688 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL)
2689 {
2690 tv->vval.v_blob = blob_alloc();
2691 if (tv->vval.v_blob != NULL)
2692 ++tv->vval.v_blob->bv_refcount;
2693 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002694 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002695 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002696 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002697
2698 name[len] = cc;
2699
2700 return ret;
2701}
2702
2703/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002704 * Check if variable "name[len]" is a local variable or an argument.
2705 * If so, "*eval_lavars_used" is set to TRUE.
2706 */
2707 void
2708check_vars(char_u *name, int len)
2709{
2710 int cc;
2711 char_u *varname;
2712 hashtab_T *ht;
2713
2714 if (eval_lavars_used == NULL)
2715 return;
2716
2717 // truncate the name, so that we can use strcmp()
2718 cc = name[len];
2719 name[len] = NUL;
2720
2721 ht = find_var_ht(name, &varname);
2722 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2723 {
2724 if (find_var(name, NULL, TRUE) != NULL)
2725 *eval_lavars_used = TRUE;
2726 }
2727
2728 name[len] = cc;
2729}
2730
2731/*
2732 * Find variable "name" in the list of variables.
2733 * Return a pointer to it if found, NULL if not found.
2734 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002735 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002736 */
2737 dictitem_T *
2738find_var(char_u *name, hashtab_T **htp, int no_autoload)
2739{
2740 char_u *varname;
2741 hashtab_T *ht;
2742 dictitem_T *ret = NULL;
2743
2744 ht = find_var_ht(name, &varname);
2745 if (htp != NULL)
2746 *htp = ht;
2747 if (ht == NULL)
2748 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002749 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002750 if (ret != NULL)
2751 return ret;
2752
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002753 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002754 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002755 if (ret != NULL)
2756 return ret;
2757
2758 // in Vim9 script items without a scope can be script-local
2759 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2760 {
2761 ht = get_script_local_ht();
2762 if (ht != NULL)
2763 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002764 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002765 if (ret != NULL)
2766 {
2767 if (htp != NULL)
2768 *htp = ht;
2769 return ret;
2770 }
2771 }
2772 }
2773
2774 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002775}
2776
2777/*
2778 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002779 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002780 * Returns NULL if not found.
2781 */
2782 dictitem_T *
2783find_var_in_ht(
2784 hashtab_T *ht,
2785 int htname,
2786 char_u *varname,
2787 int no_autoload)
2788{
2789 hashitem_T *hi;
2790
2791 if (*varname == NUL)
2792 {
2793 // Must be something like "s:", otherwise "ht" would be NULL.
2794 switch (htname)
2795 {
2796 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2797 case 'g': return &globvars_var;
2798 case 'v': return &vimvars_var;
2799 case 'b': return &curbuf->b_bufvar;
2800 case 'w': return &curwin->w_winvar;
2801 case 't': return &curtab->tp_winvar;
2802 case 'l': return get_funccal_local_var();
2803 case 'a': return get_funccal_args_var();
2804 }
2805 return NULL;
2806 }
2807
2808 hi = hash_find(ht, varname);
2809 if (HASHITEM_EMPTY(hi))
2810 {
2811 // For global variables we may try auto-loading the script. If it
2812 // worked find the variable again. Don't auto-load a script if it was
2813 // loaded already, otherwise it would be loaded every time when
2814 // checking if a function name is a Funcref variable.
2815 if (ht == &globvarht && !no_autoload)
2816 {
2817 // Note: script_autoload() may make "hi" invalid. It must either
2818 // be obtained again or not used.
2819 if (!script_autoload(varname, FALSE) || aborting())
2820 return NULL;
2821 hi = hash_find(ht, varname);
2822 }
2823 if (HASHITEM_EMPTY(hi))
2824 return NULL;
2825 }
2826 return HI2DI(hi);
2827}
2828
2829/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 * Get the script-local hashtab. NULL if not in a script context.
2831 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002832 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833get_script_local_ht(void)
2834{
2835 scid_T sid = current_sctx.sc_sid;
2836
Bram Moolenaare3d46852020-08-29 13:39:17 +02002837 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 return &SCRIPT_VARS(sid);
2839 return NULL;
2840}
2841
2842/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002843 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002844 * When "cmd" is TRUE it must look like a command, a function must be followed
2845 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01002846 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01002848 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002849lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01002850 char_u *name,
2851 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002852 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01002853 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854{
2855 hashtab_T *ht = get_script_local_ht();
2856 char_u buffer[30];
2857 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01002858 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002860 int is_global = FALSE;
2861 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862
2863 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002864 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865 if (len < sizeof(buffer) - 1)
2866 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002867 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002868 vim_strncpy(buffer, name, len);
2869 p = buffer;
2870 }
2871 else
2872 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002873 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002875 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876 }
2877
2878 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002879 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002880
2881 // if not script-local, then perhaps imported
Bram Moolenaar709664c2020-12-12 14:33:41 +01002882 if (res == FAIL && find_imported(p, 0, NULL) != NULL)
2883 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884 if (p != buffer)
2885 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002886
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002887 // Find a function, so that a following "->" works.
2888 // When used as a command require "(" or "->" to follow, "Cmd" is a user
2889 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002890 if (res != OK)
2891 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002892 p = skipwhite(name + len);
2893
2894 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002895 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002896 // Do not check for an internal function, since it might also be a
2897 // valid command, such as ":split" versus "split()".
2898 // Skip "g:" before a function name.
2899 if (name[0] == 'g' && name[1] == ':')
2900 {
2901 is_global = TRUE;
2902 fname = name + 2;
2903 }
2904 if (find_func(fname, is_global, NULL) != NULL)
2905 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002906 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002907 }
2908
Bram Moolenaar709664c2020-12-12 14:33:41 +01002909 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910}
2911
2912/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002913 * Find the hashtab used for a variable name.
2914 * Return NULL if the name is not valid.
2915 * Set "varname" to the start of name without ':'.
2916 */
2917 hashtab_T *
2918find_var_ht(char_u *name, char_u **varname)
2919{
2920 hashitem_T *hi;
2921 hashtab_T *ht;
2922
2923 if (name[0] == NUL)
2924 return NULL;
2925 if (name[1] != ':')
2926 {
2927 // The name must not start with a colon or #.
2928 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2929 return NULL;
2930 *varname = name;
2931
2932 // "version" is "v:version" in all scopes if scriptversion < 3.
2933 // Same for a few other variables marked with VV_COMPAT.
2934 if (current_sctx.sc_version < 3)
2935 {
2936 hi = hash_find(&compat_hashtab, name);
2937 if (!HASHITEM_EMPTY(hi))
2938 return &compat_hashtab;
2939 }
2940
2941 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 if (ht != NULL)
2943 return ht; // local variable
2944
Bram Moolenaarf0a40692021-06-11 22:05:47 +02002945 // In Vim9 script items at the script level are script-local, except
2946 // for autoload names.
2947 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 {
2949 ht = get_script_local_ht();
2950 if (ht != NULL)
2951 return ht;
2952 }
2953
2954 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002955 }
2956 *varname = name + 2;
2957 if (*name == 'g') // global variable
2958 return &globvarht;
2959 // There must be no ':' or '#' in the rest of the name, unless g: is used
2960 if (vim_strchr(name + 2, ':') != NULL
2961 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2962 return NULL;
2963 if (*name == 'b') // buffer variable
2964 return &curbuf->b_vars->dv_hashtab;
2965 if (*name == 'w') // window variable
2966 return &curwin->w_vars->dv_hashtab;
2967 if (*name == 't') // tab page variable
2968 return &curtab->tp_vars->dv_hashtab;
2969 if (*name == 'v') // v: variable
2970 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002971 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002972 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002974 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 if (*name == 'a') // a: function argument
2976 return get_funccal_args_ht();
2977 if (*name == 'l') // l: local function variable
2978 return get_funccal_local_ht();
2979 }
2980 if (*name == 's') // script variable
2981 {
2982 ht = get_script_local_ht();
2983 if (ht != NULL)
2984 return ht;
2985 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002986 return NULL;
2987}
2988
2989/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002990 * Get the string value of a (global/local) variable.
2991 * Note: see tv_get_string() for how long the pointer remains valid.
2992 * Returns NULL when it doesn't exist.
2993 */
2994 char_u *
2995get_var_value(char_u *name)
2996{
2997 dictitem_T *v;
2998
2999 v = find_var(name, NULL, FALSE);
3000 if (v == NULL)
3001 return NULL;
3002 return tv_get_string(&v->di_tv);
3003}
3004
3005/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003006 * Allocate a new hashtab for a sourced script. It will be used while
3007 * sourcing this script and when executing functions defined in the script.
3008 */
3009 void
3010new_script_vars(scid_T id)
3011{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003012 scriptvar_T *sv;
3013
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003014 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3015 if (sv == NULL)
3016 return;
3017 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003018 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003019}
3020
3021/*
3022 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3023 * point to it.
3024 */
3025 void
3026init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3027{
3028 hash_init(&dict->dv_hashtab);
3029 dict->dv_lock = 0;
3030 dict->dv_scope = scope;
3031 dict->dv_refcount = DO_NOT_FREE_CNT;
3032 dict->dv_copyID = 0;
3033 dict_var->di_tv.vval.v_dict = dict;
3034 dict_var->di_tv.v_type = VAR_DICT;
3035 dict_var->di_tv.v_lock = VAR_FIXED;
3036 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3037 dict_var->di_key[0] = NUL;
3038}
3039
3040/*
3041 * Unreference a dictionary initialized by init_var_dict().
3042 */
3043 void
3044unref_var_dict(dict_T *dict)
3045{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003046 // Now the dict needs to be freed if no one else is using it, go back to
3047 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003048 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3049 dict_unref(dict);
3050}
3051
3052/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003053 * Clean up a list of internal variables.
3054 * Frees all allocated variables and the value they contain.
3055 * Clears hashtab "ht", does not free it.
3056 */
3057 void
3058vars_clear(hashtab_T *ht)
3059{
3060 vars_clear_ext(ht, TRUE);
3061}
3062
3063/*
3064 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3065 */
3066 void
3067vars_clear_ext(hashtab_T *ht, int free_val)
3068{
3069 int todo;
3070 hashitem_T *hi;
3071 dictitem_T *v;
3072
3073 hash_lock(ht);
3074 todo = (int)ht->ht_used;
3075 for (hi = ht->ht_array; todo > 0; ++hi)
3076 {
3077 if (!HASHITEM_EMPTY(hi))
3078 {
3079 --todo;
3080
3081 // Free the variable. Don't remove it from the hashtab,
3082 // ht_array might change then. hash_clear() takes care of it
3083 // later.
3084 v = HI2DI(hi);
3085 if (free_val)
3086 clear_tv(&v->di_tv);
3087 if (v->di_flags & DI_FLAGS_ALLOC)
3088 vim_free(v);
3089 }
3090 }
3091 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003092 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003093}
3094
3095/*
3096 * Delete a variable from hashtab "ht" at item "hi".
3097 * Clear the variable value and free the dictitem.
3098 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003099 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003100delete_var(hashtab_T *ht, hashitem_T *hi)
3101{
3102 dictitem_T *di = HI2DI(hi);
3103
3104 hash_remove(ht, hi);
3105 clear_tv(&di->di_tv);
3106 vim_free(di);
3107}
3108
3109/*
3110 * List the value of one internal variable.
3111 */
3112 static void
3113list_one_var(dictitem_T *v, char *prefix, int *first)
3114{
3115 char_u *tofree;
3116 char_u *s;
3117 char_u numbuf[NUMBUFLEN];
3118
3119 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3120 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3121 s == NULL ? (char_u *)"" : s, first);
3122 vim_free(tofree);
3123}
3124
3125 static void
3126list_one_var_a(
3127 char *prefix,
3128 char_u *name,
3129 int type,
3130 char_u *string,
3131 int *first) // when TRUE clear rest of screen and set to FALSE
3132{
3133 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3134 msg_start();
3135 msg_puts(prefix);
3136 if (name != NULL) // "a:" vars don't have a name stored
3137 msg_puts((char *)name);
3138 msg_putchar(' ');
3139 msg_advance(22);
3140 if (type == VAR_NUMBER)
3141 msg_putchar('#');
3142 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3143 msg_putchar('*');
3144 else if (type == VAR_LIST)
3145 {
3146 msg_putchar('[');
3147 if (*string == '[')
3148 ++string;
3149 }
3150 else if (type == VAR_DICT)
3151 {
3152 msg_putchar('{');
3153 if (*string == '{')
3154 ++string;
3155 }
3156 else
3157 msg_putchar(' ');
3158
3159 msg_outtrans(string);
3160
3161 if (type == VAR_FUNC || type == VAR_PARTIAL)
3162 msg_puts("()");
3163 if (*first)
3164 {
3165 msg_clr_eos();
3166 *first = FALSE;
3167 }
3168}
3169
3170/*
3171 * Set variable "name" to value in "tv".
3172 * If the variable already exists, the value is updated.
3173 * Otherwise the variable is created.
3174 */
3175 void
3176set_var(
3177 char_u *name,
3178 typval_T *tv,
3179 int copy) // make copy of value in "tv"
3180{
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003181 set_var_const(name, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003182}
3183
3184/*
3185 * Set variable "name" to value in "tv".
3186 * If the variable already exists and "is_const" is FALSE the value is updated.
3187 * Otherwise the variable is created.
3188 */
3189 void
3190set_var_const(
3191 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003193 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003194 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003195 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003196 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003197{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003198 typval_T *tv = tv_arg;
3199 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200 dictitem_T *di;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003201 typval_T *dest_tv = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003202 char_u *varname;
3203 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003205 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003206 int var_in_vim9script;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003207 int flags = flags_arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003208
3209 ht = find_var_ht(name, &varname);
3210 if (ht == NULL || *varname == NUL)
3211 {
3212 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003213 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003214 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003215 is_script_local = ht == get_script_local_ht();
3216
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003217 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003218 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003219 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003220 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003221 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003222 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003223 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003224 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003225 }
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003226 if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
3227 && vim_strchr((char_u *)"gwbt", name[0]) != NULL)
3228 // Do not make g:var, w:var, b:var or t:var final.
3229 flags &= ~ASSIGN_FINAL;
3230
Bram Moolenaare535db82021-03-31 21:07:24 +02003231 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003232 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3233 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003234 // For "[a, _] = list" the underscore is ignored.
3235 if ((flags & ASSIGN_UNPACK) == 0)
3236 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003237 goto failed;
3238 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003239
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003241
Bram Moolenaar24e93162021-07-18 20:40:33 +02003242 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003243 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003244 imported_T *import = find_imported(varname, 0, NULL);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003245
Bram Moolenaar24e93162021-07-18 20:40:33 +02003246 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003247 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003248 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
3249 svar_T *sv;
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003250 where_T where = WHERE_INIT;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003251
3252 // imported variable from another script
3253 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003255 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003256 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 }
Bram Moolenaar60579352021-07-19 21:45:07 +02003258 sv = ((svar_T *)si->sn_var_vals.ga_data) + import->imp_var_vals_idx;
3259
Bram Moolenaar60579352021-07-19 21:45:07 +02003260 where.wt_variable = TRUE;
3261 if (check_typval_type(sv->sv_type, tv, where) == FAIL
3262 || value_check_lock(sv->sv_tv->v_lock, name, FALSE))
3263 {
3264 goto failed;
3265 }
3266
Bram Moolenaar24e93162021-07-18 20:40:33 +02003267 dest_tv = sv->sv_tv;
Bram Moolenaarf6488542021-07-18 21:24:50 +02003268 clear_tv(dest_tv);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003269 }
3270 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271
Bram Moolenaar24e93162021-07-18 20:40:33 +02003272 if (dest_tv == NULL)
3273 {
3274 // Search in parent scope which is possible to reference from lambda
3275 if (di == NULL)
3276 di = find_var_in_scoped_ht(name, TRUE);
3277
3278 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003279 && var_wrong_func_name(name, di == NULL))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003280 goto failed;
3281
3282 if (need_convert_to_bool(type, tv))
3283 {
Bram Moolenaarf6488542021-07-18 21:24:50 +02003284 // Destination is a bool and the value is not, but it can be
3285 // converted.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003286 CLEAR_FIELD(bool_tv);
3287 bool_tv.v_type = VAR_BOOL;
3288 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3289 tv = &bool_tv;
3290 }
3291
3292 if (di != NULL)
3293 {
3294 // Item already exists. Allowed to replace when reloading.
3295 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
3296 {
3297 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaarf6488542021-07-18 21:24:50 +02003298 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003299 {
3300 emsg(_(e_cannot_mod));
3301 goto failed;
3302 }
3303
3304 if (is_script_local && vim9script
Bram Moolenaarf6488542021-07-18 21:24:50 +02003305 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003306 {
3307 semsg(_(e_redefining_script_item_str), name);
3308 goto failed;
3309 }
3310
3311 if (var_in_vim9script)
3312 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003313 where_T where = WHERE_INIT;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003314
3315 // check the type and adjust to bool if needed
3316 where.wt_index = var_idx;
3317 where.wt_variable = TRUE;
Bram Moolenaarf6488542021-07-18 21:24:50 +02003318 if (check_script_var_type(&di->di_tv, tv, name, where)
3319 == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003320 goto failed;
3321 }
3322
3323 if (var_check_permission(di, name) == FAIL)
3324 goto failed;
3325 }
3326 else
3327 {
3328 // can only redefine once
3329 di->di_flags &= ~DI_FLAGS_RELOAD;
3330
Bram Moolenaarf6488542021-07-18 21:24:50 +02003331 // A Vim9 script-local variable is also present in sn_all_vars
3332 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaar24e93162021-07-18 20:40:33 +02003333 if (var_in_vim9script)
3334 update_vim9_script_var(FALSE, di, flags, tv, &type,
Bram Moolenaarf6488542021-07-18 21:24:50 +02003335 (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003336 }
3337
3338 // existing variable, need to clear the value
3339
3340 // Handle setting internal di: variables separately where needed to
3341 // prevent changing the type.
3342 if (ht == &vimvarht)
3343 {
3344 if (di->di_tv.v_type == VAR_STRING)
3345 {
3346 VIM_CLEAR(di->di_tv.vval.v_string);
3347 if (copy || tv->v_type != VAR_STRING)
3348 {
3349 char_u *val = tv_get_string(tv);
3350
Bram Moolenaarf6488542021-07-18 21:24:50 +02003351 // Careful: when assigning to v:errmsg and
3352 // tv_get_string() causes an error message the variable
3353 // will already be set.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003354 if (di->di_tv.vval.v_string == NULL)
3355 di->di_tv.vval.v_string = vim_strsave(val);
3356 }
3357 else
3358 {
3359 // Take over the string to avoid an extra alloc/free.
3360 di->di_tv.vval.v_string = tv->vval.v_string;
3361 tv->vval.v_string = NULL;
3362 }
3363 goto failed;
3364 }
3365 else if (di->di_tv.v_type == VAR_NUMBER)
3366 {
3367 di->di_tv.vval.v_number = tv_get_number(tv);
3368 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003369 set_search_direction(di->di_tv.vval.v_number
3370 ? '/' : '?');
Bram Moolenaar24e93162021-07-18 20:40:33 +02003371#ifdef FEAT_SEARCH_EXTRA
3372 else if (STRCMP(varname, "hlsearch") == 0)
3373 {
3374 no_hlsearch = !di->di_tv.vval.v_number;
3375 redraw_all_later(SOME_VALID);
3376 }
3377#endif
3378 goto failed;
3379 }
3380 else if (di->di_tv.v_type != tv->v_type)
3381 {
3382 semsg(_("E963: setting %s to value with wrong type"), name);
3383 goto failed;
3384 }
3385 }
3386
3387 clear_tv(&di->di_tv);
3388 }
3389 else
3390 {
3391 // Item not found, check if a function already exists.
3392 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaarf6488542021-07-18 21:24:50 +02003393 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003394 {
3395 semsg(_(e_redefining_script_item_str), name);
3396 goto failed;
3397 }
3398
Bram Moolenaar24e93162021-07-18 20:40:33 +02003399 // add a new variable
3400 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3401 {
3402 semsg(_(e_unknown_variable_str), name);
3403 goto failed;
3404 }
3405
3406 // Can't add "v:" or "a:" variable.
3407 if (ht == &vimvarht || ht == get_funccal_args_ht())
3408 {
3409 semsg(_(e_illvar), name);
3410 goto failed;
3411 }
3412
3413 // Make sure the variable name is valid. In Vim9 script an autoload
3414 // variable must be prefixed with "g:".
3415 if (!valid_varname(varname, !vim9script
Bram Moolenaarf6488542021-07-18 21:24:50 +02003416 || STRNCMP(name, "g:", 2) == 0))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003417 goto failed;
3418
3419 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3420 if (di == NULL)
3421 goto failed;
3422 STRCPY(di->di_key, varname);
3423 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
3424 {
3425 vim_free(di);
3426 goto failed;
3427 }
3428 di->di_flags = DI_FLAGS_ALLOC;
3429 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3430 di->di_flags |= DI_FLAGS_LOCK;
3431
3432 // A Vim9 script-local variable is also added to sn_all_vars and
3433 // sn_var_vals. It may set "type" from "tv".
Bram Moolenaare535db82021-03-31 21:07:24 +02003434 if (var_in_vim9script)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003435 update_vim9_script_var(TRUE, di, flags, tv, &type,
Bram Moolenaarf6488542021-07-18 21:24:50 +02003436 (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003437 }
3438
Bram Moolenaar24e93162021-07-18 20:40:33 +02003439 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003440 }
3441
3442 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003443 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003444 else
3445 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003446 *dest_tv = *tv;
3447 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003448 init_tv(tv);
3449 }
3450
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003451 if (vim9script && type != NULL)
3452 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003453 if (type->tt_type == VAR_DICT && dest_tv->vval.v_dict != NULL)
3454 dest_tv->vval.v_dict->dv_type = alloc_type(type);
3455 else if (type->tt_type == VAR_LIST && dest_tv->vval.v_list != NULL)
3456 dest_tv->vval.v_list->lv_type = alloc_type(type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003457 }
3458
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003459 // ":const var = value" locks the value
3460 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003461 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003462 // Like :lockvar! name: lock the value and what it contains, but only
3463 // if the reference count is up to one. That locks only literal
3464 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003465 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003466 return;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003467
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003468failed:
3469 if (!copy)
3470 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003471}
3472
3473/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003474 * Check in this order for backwards compatibility:
3475 * - Whether the variable is read-only
3476 * - Whether the variable value is locked
3477 * - Whether the variable is locked
3478 */
3479 int
3480var_check_permission(dictitem_T *di, char_u *name)
3481{
3482 if (var_check_ro(di->di_flags, name, FALSE)
3483 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3484 || var_check_lock(di->di_flags, name, FALSE))
3485 return FAIL;
3486 return OK;
3487}
3488
3489/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003490 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3491 * Also give an error message.
3492 */
3493 int
3494var_check_ro(int flags, char_u *name, int use_gettext)
3495{
3496 if (flags & DI_FLAGS_RO)
3497 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02003498 semsg(_(e_cannot_change_readonly_variable_str),
3499 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003500 return TRUE;
3501 }
3502 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3503 {
3504 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3505 return TRUE;
3506 }
3507 return FALSE;
3508}
3509
3510/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003511 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3512 * Also give an error message.
3513 */
3514 int
3515var_check_lock(int flags, char_u *name, int use_gettext)
3516{
3517 if (flags & DI_FLAGS_LOCK)
3518 {
3519 semsg(_(e_variable_is_locked_str),
3520 use_gettext ? (char_u *)_(name) : name);
3521 return TRUE;
3522 }
3523 return FALSE;
3524}
3525
3526/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003527 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3528 * Also give an error message.
3529 */
3530 int
3531var_check_fixed(int flags, char_u *name, int use_gettext)
3532{
3533 if (flags & DI_FLAGS_FIX)
3534 {
3535 semsg(_("E795: Cannot delete variable %s"),
3536 use_gettext ? (char_u *)_(name) : name);
3537 return TRUE;
3538 }
3539 return FALSE;
3540}
3541
3542/*
3543 * Check if a funcref is assigned to a valid variable name.
3544 * Return TRUE and give an error if not.
3545 */
3546 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003547var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003548 char_u *name, // points to start of variable name
3549 int new_var) // TRUE when creating the variable
3550{
Bram Moolenaar32154662021-03-28 21:14:06 +02003551 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
3552 // the name can be used without the s: prefix.
3553 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
3554 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003555 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3556 ? name[2] : name[0]))
3557 {
3558 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3559 name);
3560 return TRUE;
3561 }
3562 // Don't allow hiding a function. When "v" is not NULL we might be
3563 // assigning another function to the same var, the type is checked
3564 // below.
3565 if (new_var && function_exists(name, FALSE))
3566 {
3567 semsg(_("E705: Variable name conflicts with existing function: %s"),
3568 name);
3569 return TRUE;
3570 }
3571 return FALSE;
3572}
3573
3574/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003575 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3576 * value. Also give an error message, using "name" or _("name") when
3577 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003578 */
3579 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003580value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003581{
3582 if (lock & VAR_LOCKED)
3583 {
3584 semsg(_("E741: Value is locked: %s"),
3585 name == NULL ? (char_u *)_("Unknown")
3586 : use_gettext ? (char_u *)_(name)
3587 : name);
3588 return TRUE;
3589 }
3590 if (lock & VAR_FIXED)
3591 {
3592 semsg(_("E742: Cannot change value of %s"),
3593 name == NULL ? (char_u *)_("Unknown")
3594 : use_gettext ? (char_u *)_(name)
3595 : name);
3596 return TRUE;
3597 }
3598 return FALSE;
3599}
3600
3601/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003602 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003603 * Return FALSE and give an error if not.
3604 */
3605 int
Bram Moolenaar03290b82020-12-19 16:30:44 +01003606valid_varname(char_u *varname, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003607{
3608 char_u *p;
3609
3610 for (p = varname; *p != NUL; ++p)
3611 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003612 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003613 {
3614 semsg(_(e_illvar), varname);
3615 return FALSE;
3616 }
3617 return TRUE;
3618}
3619
3620/*
3621 * getwinvar() and gettabwinvar()
3622 */
3623 static void
3624getwinvar(
3625 typval_T *argvars,
3626 typval_T *rettv,
3627 int off) // 1 for gettabwinvar()
3628{
3629 win_T *win;
3630 char_u *varname;
3631 dictitem_T *v;
3632 tabpage_T *tp = NULL;
3633 int done = FALSE;
3634 win_T *oldcurwin;
3635 tabpage_T *oldtabpage;
3636 int need_switch_win;
3637
3638 if (off == 1)
3639 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3640 else
3641 tp = curtab;
3642 win = find_win_by_nr(&argvars[off], tp);
3643 varname = tv_get_string_chk(&argvars[off + 1]);
3644 ++emsg_off;
3645
3646 rettv->v_type = VAR_STRING;
3647 rettv->vval.v_string = NULL;
3648
3649 if (win != NULL && varname != NULL)
3650 {
3651 // Set curwin to be our win, temporarily. Also set the tabpage,
3652 // otherwise the window is not valid. Only do this when needed,
3653 // autocommands get blocked.
3654 need_switch_win = !(tp == curtab && win == curwin);
3655 if (!need_switch_win
3656 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3657 {
3658 if (*varname == '&')
3659 {
3660 if (varname[1] == NUL)
3661 {
3662 // get all window-local options in a dict
3663 dict_T *opts = get_winbuf_options(FALSE);
3664
3665 if (opts != NULL)
3666 {
3667 rettv_dict_set(rettv, opts);
3668 done = TRUE;
3669 }
3670 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003671 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003672 // window-local-option
3673 done = TRUE;
3674 }
3675 else
3676 {
3677 // Look up the variable.
3678 // Let getwinvar({nr}, "") return the "w:" dictionary.
3679 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3680 varname, FALSE);
3681 if (v != NULL)
3682 {
3683 copy_tv(&v->di_tv, rettv);
3684 done = TRUE;
3685 }
3686 }
3687 }
3688
3689 if (need_switch_win)
3690 // restore previous notion of curwin
3691 restore_win(oldcurwin, oldtabpage, TRUE);
3692 }
3693
3694 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3695 // use the default return value
3696 copy_tv(&argvars[off + 2], rettv);
3697
3698 --emsg_off;
3699}
3700
3701/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003702 * Set option "varname" to the value of "varp" for the current buffer/window.
3703 */
3704 static void
3705set_option_from_tv(char_u *varname, typval_T *varp)
3706{
3707 long numval = 0;
3708 char_u *strval;
3709 char_u nbuf[NUMBUFLEN];
3710 int error = FALSE;
3711
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003712 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003713 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003714 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003715 strval = (char_u *)"0"; // avoid using "false"
3716 }
3717 else
3718 {
3719 if (!in_vim9script() || varp->v_type != VAR_STRING)
3720 numval = (long)tv_get_number_chk(varp, &error);
3721 strval = tv_get_string_buf_chk(varp, nbuf);
3722 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02003723 if (!error && strval != NULL)
3724 set_option_value(varname, numval, strval, OPT_LOCAL);
3725}
3726
3727/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003728 * "setwinvar()" and "settabwinvar()" functions
3729 */
3730 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003731setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003732{
3733 win_T *win;
3734 win_T *save_curwin;
3735 tabpage_T *save_curtab;
3736 int need_switch_win;
3737 char_u *varname, *winvarname;
3738 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003739 tabpage_T *tp = NULL;
3740
3741 if (check_secure())
3742 return;
3743
3744 if (off == 1)
3745 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3746 else
3747 tp = curtab;
3748 win = find_win_by_nr(&argvars[off], tp);
3749 varname = tv_get_string_chk(&argvars[off + 1]);
3750 varp = &argvars[off + 2];
3751
3752 if (win != NULL && varname != NULL && varp != NULL)
3753 {
3754 need_switch_win = !(tp == curtab && win == curwin);
3755 if (!need_switch_win
3756 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3757 {
3758 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003759 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003760 else
3761 {
3762 winvarname = alloc(STRLEN(varname) + 3);
3763 if (winvarname != NULL)
3764 {
3765 STRCPY(winvarname, "w:");
3766 STRCPY(winvarname + 2, varname);
3767 set_var(winvarname, varp, TRUE);
3768 vim_free(winvarname);
3769 }
3770 }
3771 }
3772 if (need_switch_win)
3773 restore_win(save_curwin, save_curtab, TRUE);
3774 }
3775}
3776
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003777/*
3778 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3779 * v:option_type, and v:option_command.
3780 */
3781 void
3782reset_v_option_vars(void)
3783{
3784 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3785 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3786 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3787 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3788 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3789 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3790}
3791
3792/*
3793 * Add an assert error to v:errors.
3794 */
3795 void
3796assert_error(garray_T *gap)
3797{
3798 struct vimvar *vp = &vimvars[VV_ERRORS];
3799
3800 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003801 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003802 set_vim_var_list(VV_ERRORS, list_alloc());
3803 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3804}
3805
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003806 int
3807var_exists(char_u *var)
3808{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003809 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003810 char_u *name;
3811 char_u *tofree;
3812 typval_T tv;
3813 int len = 0;
3814 int n = FALSE;
3815
3816 // get_name_len() takes care of expanding curly braces
3817 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003818 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003819 if (len > 0)
3820 {
3821 if (tofree != NULL)
3822 name = tofree;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003823 n = (eval_variable(name, len, &tv, NULL,
3824 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003825 if (n)
3826 {
3827 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003828 arg = skipwhite(arg);
3829 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003830 if (n)
3831 clear_tv(&tv);
3832 }
3833 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003834 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003835 n = FALSE;
3836
3837 vim_free(tofree);
3838 return n;
3839}
3840
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003841static lval_T *redir_lval = NULL;
3842#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3843static garray_T redir_ga; // only valid when redir_lval is not NULL
3844static char_u *redir_endp = NULL;
3845static char_u *redir_varname = NULL;
3846
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003847 int
3848alloc_redir_lval(void)
3849{
3850 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3851 if (redir_lval == NULL)
3852 return FAIL;
3853 return OK;
3854}
3855
3856 void
3857clear_redir_lval(void)
3858{
3859 VIM_CLEAR(redir_lval);
3860}
3861
3862 void
3863init_redir_ga(void)
3864{
3865 ga_init2(&redir_ga, (int)sizeof(char), 500);
3866}
3867
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003868/*
3869 * Start recording command output to a variable
3870 * When "append" is TRUE append to an existing variable.
3871 * Returns OK if successfully completed the setup. FAIL otherwise.
3872 */
3873 int
3874var_redir_start(char_u *name, int append)
3875{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003876 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003877 typval_T tv;
3878
3879 // Catch a bad name early.
3880 if (!eval_isnamec1(*name))
3881 {
3882 emsg(_(e_invarg));
3883 return FAIL;
3884 }
3885
3886 // Make a copy of the name, it is used in redir_lval until redir ends.
3887 redir_varname = vim_strsave(name);
3888 if (redir_varname == NULL)
3889 return FAIL;
3890
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003891 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003892 {
3893 var_redir_stop();
3894 return FAIL;
3895 }
3896
3897 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003898 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003899
3900 // Parse the variable name (can be a dict or list entry).
3901 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3902 FNE_CHECK_START);
3903 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3904 {
3905 clear_lval(redir_lval);
3906 if (redir_endp != NULL && *redir_endp != NUL)
3907 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003908 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003909 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003910 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003911 redir_endp = NULL; // don't store a value, only cleanup
3912 var_redir_stop();
3913 return FAIL;
3914 }
3915
3916 // check if we can write to the variable: set it to or append an empty
3917 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003918 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003919 tv.v_type = VAR_STRING;
3920 tv.vval.v_string = (char_u *)"";
3921 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003922 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003923 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003924 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003925 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003926 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003927 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003928 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003929 {
3930 redir_endp = NULL; // don't store a value, only cleanup
3931 var_redir_stop();
3932 return FAIL;
3933 }
3934
3935 return OK;
3936}
3937
3938/*
3939 * Append "value[value_len]" to the variable set by var_redir_start().
3940 * The actual appending is postponed until redirection ends, because the value
3941 * appended may in fact be the string we write to, changing it may cause freed
3942 * memory to be used:
3943 * :redir => foo
3944 * :let foo
3945 * :redir END
3946 */
3947 void
3948var_redir_str(char_u *value, int value_len)
3949{
3950 int len;
3951
3952 if (redir_lval == NULL)
3953 return;
3954
3955 if (value_len == -1)
3956 len = (int)STRLEN(value); // Append the entire string
3957 else
3958 len = value_len; // Append only "value_len" characters
3959
3960 if (ga_grow(&redir_ga, len) == OK)
3961 {
3962 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3963 redir_ga.ga_len += len;
3964 }
3965 else
3966 var_redir_stop();
3967}
3968
3969/*
3970 * Stop redirecting command output to a variable.
3971 * Frees the allocated memory.
3972 */
3973 void
3974var_redir_stop(void)
3975{
3976 typval_T tv;
3977
3978 if (EVALCMD_BUSY)
3979 {
3980 redir_lval = NULL;
3981 return;
3982 }
3983
3984 if (redir_lval != NULL)
3985 {
3986 // If there was no error: assign the text to the variable.
3987 if (redir_endp != NULL)
3988 {
3989 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3990 tv.v_type = VAR_STRING;
3991 tv.vval.v_string = redir_ga.ga_data;
3992 // Call get_lval() again, if it's inside a Dict or List it may
3993 // have changed.
3994 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3995 FALSE, FALSE, 0, FNE_CHECK_START);
3996 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003997 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003998 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003999 clear_lval(redir_lval);
4000 }
4001
4002 // free the collected output
4003 VIM_CLEAR(redir_ga.ga_data);
4004
4005 VIM_CLEAR(redir_lval);
4006 }
4007 VIM_CLEAR(redir_varname);
4008}
4009
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004010/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004011 * Get the collected redirected text and clear redir_ga.
4012 */
4013 char_u *
4014get_clear_redir_ga(void)
4015{
4016 char_u *res;
4017
4018 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4019 res = redir_ga.ga_data;
4020 redir_ga.ga_data = NULL;
4021 return res;
4022}
4023
4024/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004025 * "gettabvar()" function
4026 */
4027 void
4028f_gettabvar(typval_T *argvars, typval_T *rettv)
4029{
4030 win_T *oldcurwin;
4031 tabpage_T *tp, *oldtabpage;
4032 dictitem_T *v;
4033 char_u *varname;
4034 int done = FALSE;
4035
4036 rettv->v_type = VAR_STRING;
4037 rettv->vval.v_string = NULL;
4038
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004039 if (in_vim9script()
4040 && (check_for_number_arg(argvars, 0) == FAIL
4041 || check_for_string_arg(argvars, 1) == FAIL))
4042 return;
4043
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004044 varname = tv_get_string_chk(&argvars[1]);
4045 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4046 if (tp != NULL && varname != NULL)
4047 {
4048 // Set tp to be our tabpage, temporarily. Also set the window to the
4049 // first window in the tabpage, otherwise the window is not valid.
4050 if (switch_win(&oldcurwin, &oldtabpage,
4051 tp == curtab || tp->tp_firstwin == NULL ? firstwin
4052 : tp->tp_firstwin, tp, TRUE) == OK)
4053 {
4054 // look up the variable
4055 // Let gettabvar({nr}, "") return the "t:" dictionary.
4056 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
4057 if (v != NULL)
4058 {
4059 copy_tv(&v->di_tv, rettv);
4060 done = TRUE;
4061 }
4062 }
4063
4064 // restore previous notion of curwin
4065 restore_win(oldcurwin, oldtabpage, TRUE);
4066 }
4067
4068 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4069 copy_tv(&argvars[2], rettv);
4070}
4071
4072/*
4073 * "gettabwinvar()" function
4074 */
4075 void
4076f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4077{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004078 if (in_vim9script()
4079 && (check_for_number_arg(argvars, 0) == FAIL
4080 || check_for_number_arg(argvars, 1) == FAIL
4081 || check_for_string_arg(argvars, 2) == FAIL))
4082 return;
4083
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004084 getwinvar(argvars, rettv, 1);
4085}
4086
4087/*
4088 * "getwinvar()" function
4089 */
4090 void
4091f_getwinvar(typval_T *argvars, typval_T *rettv)
4092{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004093 if (in_vim9script()
4094 && (check_for_number_arg(argvars, 0) == FAIL
4095 || check_for_string_arg(argvars, 1) == FAIL))
4096 return;
4097
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004098 getwinvar(argvars, rettv, 0);
4099}
4100
4101/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004102 * "getbufvar()" function
4103 */
4104 void
4105f_getbufvar(typval_T *argvars, typval_T *rettv)
4106{
4107 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004108 char_u *varname;
4109 dictitem_T *v;
4110 int done = FALSE;
4111
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004112 if (in_vim9script()
4113 && (check_for_buffer_arg(argvars, 0) == FAIL
4114 || check_for_string_arg(argvars, 1) == FAIL))
4115 return;
4116
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004117 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004118 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004119
4120 rettv->v_type = VAR_STRING;
4121 rettv->vval.v_string = NULL;
4122
4123 if (buf != NULL && varname != NULL)
4124 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004125 if (*varname == '&')
4126 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004127 buf_T *save_curbuf = curbuf;
4128
4129 // set curbuf to be our buf, temporarily
4130 curbuf = buf;
4131
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004132 if (varname[1] == NUL)
4133 {
4134 // get all buffer-local options in a dict
4135 dict_T *opts = get_winbuf_options(TRUE);
4136
4137 if (opts != NULL)
4138 {
4139 rettv_dict_set(rettv, opts);
4140 done = TRUE;
4141 }
4142 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004143 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004144 // buffer-local-option
4145 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02004146
4147 // restore previous notion of curbuf
4148 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004149 }
4150 else
4151 {
4152 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02004153 if (*varname == NUL)
4154 // Let getbufvar({nr}, "") return the "b:" dictionary.
4155 v = &buf->b_bufvar;
4156 else
4157 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
4158 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004159 if (v != NULL)
4160 {
4161 copy_tv(&v->di_tv, rettv);
4162 done = TRUE;
4163 }
4164 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004165 }
4166
4167 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4168 // use the default value
4169 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004170}
4171
4172/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004173 * "settabvar()" function
4174 */
4175 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004176f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004177{
4178 tabpage_T *save_curtab;
4179 tabpage_T *tp;
4180 char_u *varname, *tabvarname;
4181 typval_T *varp;
4182
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004183 if (check_secure())
4184 return;
4185
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004186 if (in_vim9script()
4187 && (check_for_number_arg(argvars, 0) == FAIL
4188 || check_for_string_arg(argvars, 1) == FAIL))
4189 return;
4190
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004191 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4192 varname = tv_get_string_chk(&argvars[1]);
4193 varp = &argvars[2];
4194
4195 if (varname != NULL && varp != NULL && tp != NULL)
4196 {
4197 save_curtab = curtab;
4198 goto_tabpage_tp(tp, FALSE, FALSE);
4199
4200 tabvarname = alloc(STRLEN(varname) + 3);
4201 if (tabvarname != NULL)
4202 {
4203 STRCPY(tabvarname, "t:");
4204 STRCPY(tabvarname + 2, varname);
4205 set_var(tabvarname, varp, TRUE);
4206 vim_free(tabvarname);
4207 }
4208
4209 // Restore current tabpage
4210 if (valid_tabpage(save_curtab))
4211 goto_tabpage_tp(save_curtab, FALSE, FALSE);
4212 }
4213}
4214
4215/*
4216 * "settabwinvar()" function
4217 */
4218 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004219f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004220{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004221 if (in_vim9script()
4222 && (check_for_number_arg(argvars, 0) == FAIL
4223 || check_for_number_arg(argvars, 1) == FAIL
4224 || check_for_string_arg(argvars, 2) == FAIL))
4225 return;
4226
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004227 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004228}
4229
4230/*
4231 * "setwinvar()" function
4232 */
4233 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004234f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004235{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004236 if (in_vim9script()
4237 && (check_for_number_arg(argvars, 0) == FAIL
4238 || check_for_string_arg(argvars, 1) == FAIL))
4239 return;
4240
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004241 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004242}
4243
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004244/*
4245 * "setbufvar()" function
4246 */
4247 void
4248f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4249{
4250 buf_T *buf;
4251 char_u *varname, *bufvarname;
4252 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004253
4254 if (check_secure())
4255 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004256
4257 if (in_vim9script()
4258 && (check_for_buffer_arg(argvars, 0) == FAIL
4259 || check_for_string_arg(argvars, 1) == FAIL))
4260 return;
4261
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004262 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004263 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004264 varp = &argvars[2];
4265
4266 if (buf != NULL && varname != NULL && varp != NULL)
4267 {
4268 if (*varname == '&')
4269 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004270 aco_save_T aco;
4271
4272 // set curbuf to be our buf, temporarily
4273 aucmd_prepbuf(&aco, buf);
4274
Bram Moolenaar191929b2020-08-19 21:20:49 +02004275 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004276
4277 // reset notion of buffer
4278 aucmd_restbuf(&aco);
4279 }
4280 else
4281 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004282 bufvarname = alloc(STRLEN(varname) + 3);
4283 if (bufvarname != NULL)
4284 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004285 buf_T *save_curbuf = curbuf;
4286
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004287 curbuf = buf;
4288 STRCPY(bufvarname, "b:");
4289 STRCPY(bufvarname + 2, varname);
4290 set_var(bufvarname, varp, TRUE);
4291 vim_free(bufvarname);
4292 curbuf = save_curbuf;
4293 }
4294 }
4295 }
4296}
4297
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004298/*
4299 * Get a callback from "arg". It can be a Funcref or a function name.
4300 * When "arg" is zero return an empty string.
4301 * "cb_name" is not allocated.
4302 * "cb_name" is set to NULL for an invalid argument.
4303 */
4304 callback_T
4305get_callback(typval_T *arg)
4306{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004307 callback_T res;
4308 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004309
4310 res.cb_free_name = FALSE;
4311 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4312 {
4313 res.cb_partial = arg->vval.v_partial;
4314 ++res.cb_partial->pt_refcount;
4315 res.cb_name = partial_name(res.cb_partial);
4316 }
4317 else
4318 {
4319 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004320 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4321 && isdigit(*arg->vval.v_string))
4322 r = FAIL;
4323 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004324 {
4325 // Note that we don't make a copy of the string.
4326 res.cb_name = arg->vval.v_string;
4327 func_ref(res.cb_name);
4328 }
4329 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004330 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004331 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004332 r = FAIL;
4333
4334 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004335 {
4336 emsg(_("E921: Invalid callback argument"));
4337 res.cb_name = NULL;
4338 }
4339 }
4340 return res;
4341}
4342
4343/*
4344 * Copy a callback into a typval_T.
4345 */
4346 void
4347put_callback(callback_T *cb, typval_T *tv)
4348{
4349 if (cb->cb_partial != NULL)
4350 {
4351 tv->v_type = VAR_PARTIAL;
4352 tv->vval.v_partial = cb->cb_partial;
4353 ++tv->vval.v_partial->pt_refcount;
4354 }
4355 else
4356 {
4357 tv->v_type = VAR_FUNC;
4358 tv->vval.v_string = vim_strsave(cb->cb_name);
4359 func_ref(cb->cb_name);
4360 }
4361}
4362
4363/*
4364 * Make a copy of "src" into "dest", allocating the function name if needed,
4365 * without incrementing the refcount.
4366 */
4367 void
4368set_callback(callback_T *dest, callback_T *src)
4369{
4370 if (src->cb_partial == NULL)
4371 {
4372 // just a function name, make a copy
4373 dest->cb_name = vim_strsave(src->cb_name);
4374 dest->cb_free_name = TRUE;
4375 }
4376 else
4377 {
4378 // cb_name is a pointer into cb_partial
4379 dest->cb_name = src->cb_name;
4380 dest->cb_free_name = FALSE;
4381 }
4382 dest->cb_partial = src->cb_partial;
4383}
4384
4385/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004386 * Copy callback from "src" to "dest", incrementing the refcounts.
4387 */
4388 void
4389copy_callback(callback_T *dest, callback_T *src)
4390{
4391 dest->cb_partial = src->cb_partial;
4392 if (dest->cb_partial != NULL)
4393 {
4394 dest->cb_name = src->cb_name;
4395 dest->cb_free_name = FALSE;
4396 ++dest->cb_partial->pt_refcount;
4397 }
4398 else
4399 {
4400 dest->cb_name = vim_strsave(src->cb_name);
4401 dest->cb_free_name = TRUE;
4402 func_ref(src->cb_name);
4403 }
4404}
4405
4406/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004407 * Unref/free "callback" returned by get_callback() or set_callback().
4408 */
4409 void
4410free_callback(callback_T *callback)
4411{
4412 if (callback->cb_partial != NULL)
4413 {
4414 partial_unref(callback->cb_partial);
4415 callback->cb_partial = NULL;
4416 }
4417 else if (callback->cb_name != NULL)
4418 func_unref(callback->cb_name);
4419 if (callback->cb_free_name)
4420 {
4421 vim_free(callback->cb_name);
4422 callback->cb_free_name = FALSE;
4423 }
4424 callback->cb_name = NULL;
4425}
4426
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004427#endif // FEAT_EVAL