blob: 534163c543286dc34bda113eec1d0ee323477070 [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 }
815 eap->nextcmd = check_nextcmd(arg);
816 }
817 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
818 {
819 list_T *l;
820
821 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200822 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200823 if (l != NULL)
824 {
825 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200826 if (!eap->skip)
827 {
828 op[0] = '=';
829 op[1] = NUL;
830 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100831 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200832 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200833 clear_tv(&rettv);
834 }
835 }
836 else
837 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200838 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200839 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200840
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200841 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200842 i = FAIL;
843 if (has_assign || concat)
844 {
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100845 int cur_lnum;
846
Bram Moolenaar32e35112020-05-14 22:41:15 +0200847 op[0] = '=';
848 op[1] = NUL;
849 if (*expr != '=')
850 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200851 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200852 {
853 // +=, /=, etc. require an existing variable
854 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
855 i = FAIL;
856 }
857 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200858 {
859 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200860 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200861 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200862 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200863 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200864 ++len;
865 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200866 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200867 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200868 }
869 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200870 ++expr;
871
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200872 if (vim9script && (!VIM_ISWHITE(*argend)
873 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200874 {
875 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100876 semsg(_(e_white_space_required_before_and_after_str_at_str),
877 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200878 i = FAIL;
879 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200880
881 if (eap->skip)
882 ++emsg_skip;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200883 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200884 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100885 cur_lnum = SOURCING_LNUM;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200886 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200887 if (eap->skip)
888 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200889 clear_evalarg(&evalarg, eap);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100890
891 // Restore the line number so that any type error is given for the
892 // declaration, not the expression.
893 SOURCING_LNUM = cur_lnum;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200894 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200895 if (eap->skip)
896 {
897 if (i != FAIL)
898 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200899 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200900 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200901 {
902 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200903 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200904 clear_tv(&rettv);
905 }
906 }
907}
908
909/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +0100910 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200911 * Handles both "var" with any type and "[var, var; var]" with a list type.
912 * When "op" is not NULL it points to a string with characters that
913 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
914 * or concatenate.
915 * Returns OK or FAIL;
916 */
917 int
918ex_let_vars(
919 char_u *arg_start,
920 typval_T *tv,
921 int copy, // copy values from "tv", don't move
922 int semicolon, // from skip_var_list()
923 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100924 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200925 char_u *op)
926{
927 char_u *arg = arg_start;
928 list_T *l;
929 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100930 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200931 listitem_T *item;
932 typval_T ltv;
933
934 if (*arg != '[')
935 {
936 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100937 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200938 return FAIL;
939 return OK;
940 }
941
942 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
943 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
944 {
945 emsg(_(e_listreq));
946 return FAIL;
947 }
948
949 i = list_len(l);
950 if (semicolon == 0 && var_count < i)
951 {
952 emsg(_("E687: Less targets than List items"));
953 return FAIL;
954 }
955 if (var_count - semicolon > i)
956 {
957 emsg(_("E688: More targets than List items"));
958 return FAIL;
959 }
960
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200961 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200962 item = l->lv_first;
963 while (*arg != ']')
964 {
965 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100966 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +0200967 arg = ex_let_one(arg, &item->li_tv, TRUE,
968 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200969 item = item->li_next;
970 if (arg == NULL)
971 return FAIL;
972
973 arg = skipwhite(arg);
974 if (*arg == ';')
975 {
976 // Put the rest of the list (may be empty) in the var after ';'.
977 // Create a new list for this.
978 l = list_alloc();
979 if (l == NULL)
980 return FAIL;
981 while (item != NULL)
982 {
983 list_append_tv(l, &item->li_tv);
984 item = item->li_next;
985 }
986
987 ltv.v_type = VAR_LIST;
988 ltv.v_lock = 0;
989 ltv.vval.v_list = l;
990 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100991 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200992
Bram Moolenaarf93bbd02021-04-10 22:35:43 +0200993 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
994 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200995 clear_tv(&ltv);
996 if (arg == NULL)
997 return FAIL;
998 break;
999 }
1000 else if (*arg != ',' && *arg != ']')
1001 {
1002 internal_error("ex_let_vars()");
1003 return FAIL;
1004 }
1005 }
1006
1007 return OK;
1008}
1009
1010/*
1011 * Skip over assignable variable "var" or list of variables "[var, var]".
1012 * Used for ":let varvar = expr" and ":for varvar in expr".
1013 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001014 * for "[var, var; var]" set "semicolon" to 1.
1015 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001016 * Return NULL for an error.
1017 */
1018 char_u *
1019skip_var_list(
1020 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001021 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001022 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001023 int *semicolon,
1024 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001025{
1026 char_u *p, *s;
1027
1028 if (*arg == '[')
1029 {
1030 // "[var, var]": find the matching ']'.
1031 p = arg;
1032 for (;;)
1033 {
1034 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001035 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001036 if (s == p)
1037 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001038 if (!silent)
1039 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001040 return NULL;
1041 }
1042 ++*var_count;
1043
1044 p = skipwhite(s);
1045 if (*p == ']')
1046 break;
1047 else if (*p == ';')
1048 {
1049 if (*semicolon == 1)
1050 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001051 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001052 return NULL;
1053 }
1054 *semicolon = 1;
1055 }
1056 else if (*p != ',')
1057 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001058 if (!silent)
1059 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001060 return NULL;
1061 }
1062 }
1063 return p + 1;
1064 }
1065 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001067}
1068
1069/*
1070 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1071 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001072 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001073 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001074 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001076{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001077 char_u *end;
1078 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001079
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001080 if (*arg == '@' && arg[1] != NUL)
1081 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001082 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001083 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001084
1085 // "a: type" is declaring variable "a" with a type, not "a:".
1086 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001087 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001088 --end;
1089
Bram Moolenaar585587d2021-01-17 20:52:13 +01001090 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001091 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001092 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001093 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094 }
1095 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001096}
1097
1098/*
1099 * List variables for hashtab "ht" with prefix "prefix".
1100 * If "empty" is TRUE also list NULL strings as empty strings.
1101 */
1102 void
1103list_hashtable_vars(
1104 hashtab_T *ht,
1105 char *prefix,
1106 int empty,
1107 int *first)
1108{
1109 hashitem_T *hi;
1110 dictitem_T *di;
1111 int todo;
1112 char_u buf[IOSIZE];
1113
1114 todo = (int)ht->ht_used;
1115 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1116 {
1117 if (!HASHITEM_EMPTY(hi))
1118 {
1119 --todo;
1120 di = HI2DI(hi);
1121
1122 // apply :filter /pat/ to variable name
1123 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1124 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1125 if (message_filtered(buf))
1126 continue;
1127
1128 if (empty || di->di_tv.v_type != VAR_STRING
1129 || di->di_tv.vval.v_string != NULL)
1130 list_one_var(di, prefix, first);
1131 }
1132 }
1133}
1134
1135/*
1136 * List global variables.
1137 */
1138 static void
1139list_glob_vars(int *first)
1140{
1141 list_hashtable_vars(&globvarht, "", TRUE, first);
1142}
1143
1144/*
1145 * List buffer variables.
1146 */
1147 static void
1148list_buf_vars(int *first)
1149{
1150 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1151}
1152
1153/*
1154 * List window variables.
1155 */
1156 static void
1157list_win_vars(int *first)
1158{
1159 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1160}
1161
1162/*
1163 * List tab page variables.
1164 */
1165 static void
1166list_tab_vars(int *first)
1167{
1168 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1169}
1170
1171/*
1172 * List variables in "arg".
1173 */
1174 static char_u *
1175list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1176{
1177 int error = FALSE;
1178 int len;
1179 char_u *name;
1180 char_u *name_start;
1181 char_u *arg_subsc;
1182 char_u *tofree;
1183 typval_T tv;
1184
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001185 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001186 {
1187 if (error || eap->skip)
1188 {
1189 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1190 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1191 {
1192 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001193 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001194 break;
1195 }
1196 }
1197 else
1198 {
1199 // get_name_len() takes care of expanding curly braces
1200 name_start = name = arg;
1201 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1202 if (len <= 0)
1203 {
1204 // This is mainly to keep test 49 working: when expanding
1205 // curly braces fails overrule the exception error message.
1206 if (len < 0 && !aborting())
1207 {
1208 emsg_severe = TRUE;
1209 semsg(_(e_invarg2), arg);
1210 break;
1211 }
1212 error = TRUE;
1213 }
1214 else
1215 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001216 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001217 if (tofree != NULL)
1218 name = tofree;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001219 if (eval_variable(name, len, &tv, NULL,
1220 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001221 error = TRUE;
1222 else
1223 {
1224 // handle d.key, l[idx], f(expr)
1225 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001226 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001227 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001228 error = TRUE;
1229 else
1230 {
1231 if (arg == arg_subsc && len == 2 && name[1] == ':')
1232 {
1233 switch (*name)
1234 {
1235 case 'g': list_glob_vars(first); break;
1236 case 'b': list_buf_vars(first); break;
1237 case 'w': list_win_vars(first); break;
1238 case 't': list_tab_vars(first); break;
1239 case 'v': list_vim_vars(first); break;
1240 case 's': list_script_vars(first); break;
1241 case 'l': list_func_vars(first); break;
1242 default:
1243 semsg(_("E738: Can't list variables for %s"), name);
1244 }
1245 }
1246 else
1247 {
1248 char_u numbuf[NUMBUFLEN];
1249 char_u *tf;
1250 int c;
1251 char_u *s;
1252
1253 s = echo_string(&tv, &tf, numbuf, 0);
1254 c = *arg;
1255 *arg = NUL;
1256 list_one_var_a("",
1257 arg == arg_subsc ? name : name_start,
1258 tv.v_type,
1259 s == NULL ? (char_u *)"" : s,
1260 first);
1261 *arg = c;
1262 vim_free(tf);
1263 }
1264 clear_tv(&tv);
1265 }
1266 }
1267 }
1268
1269 vim_free(tofree);
1270 }
1271
1272 arg = skipwhite(arg);
1273 }
1274
1275 return arg;
1276}
1277
1278/*
1279 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1280 * Returns a pointer to the char just after the var name.
1281 * Returns NULL if there is an error.
1282 */
1283 static char_u *
1284ex_let_one(
1285 char_u *arg, // points to variable name
1286 typval_T *tv, // value to assign to variable
1287 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001288 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001289 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001290 char_u *op, // "+", "-", "." or NULL
1291 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001292{
1293 int c1;
1294 char_u *name;
1295 char_u *p;
1296 char_u *arg_end = NULL;
1297 int len;
1298 int opt_flags;
1299 char_u *tofree = NULL;
1300
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001301 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001302 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001303 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1304 {
1305 vim9_declare_error(arg);
1306 return NULL;
1307 }
1308
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001309 // ":let $VAR = expr": Set environment variable.
1310 if (*arg == '$')
1311 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001312 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1313 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001314 {
1315 emsg(_("E996: Cannot lock an environment variable"));
1316 return NULL;
1317 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001318
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001319 // Find the end of the name.
1320 ++arg;
1321 name = arg;
1322 len = get_env_len(&arg);
1323 if (len == 0)
1324 semsg(_(e_invarg2), name - 1);
1325 else
1326 {
1327 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1328 semsg(_(e_letwrong), op);
1329 else if (endchars != NULL
1330 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02001331 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001332 else if (!check_secure())
1333 {
1334 c1 = name[len];
1335 name[len] = NUL;
1336 p = tv_get_string_chk(tv);
1337 if (p != NULL && op != NULL && *op == '.')
1338 {
1339 int mustfree = FALSE;
1340 char_u *s = vim_getenv(name, &mustfree);
1341
1342 if (s != NULL)
1343 {
1344 p = tofree = concat_str(s, p);
1345 if (mustfree)
1346 vim_free(s);
1347 }
1348 }
1349 if (p != NULL)
1350 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001351 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001352 arg_end = arg;
1353 }
1354 name[len] = c1;
1355 vim_free(tofree);
1356 }
1357 }
1358 }
1359
1360 // ":let &option = expr": Set option value.
1361 // ":let &l:option = expr": Set local option value.
1362 // ":let &g:option = expr": Set global option value.
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001363 // ":for &ts in range(8)": Set option value for for loop
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001364 else if (*arg == '&')
1365 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001366 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1367 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001368 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001369 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001370 return NULL;
1371 }
1372 // Find the end of the name.
1373 p = find_option_end(&arg, &opt_flags);
1374 if (p == NULL || (endchars != NULL
1375 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaar108010a2021-06-27 22:03:33 +02001376 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001377 else
1378 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001379 long n = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001380 getoption_T opt_type;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001381 long numval;
1382 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001383 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001384 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001385
1386 c1 = *p;
1387 *p = NUL;
1388
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001389 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001390 if ((opt_type == gov_bool
1391 || opt_type == gov_number
1392 || opt_type == gov_hidden_bool
1393 || opt_type == gov_hidden_number)
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001394 && (tv->v_type != VAR_STRING || !in_vim9script()))
Bram Moolenaar0ea04402021-01-04 13:37:54 +01001395 {
1396 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1397 // bool, possibly hidden
1398 n = (long)tv_get_bool(tv);
1399 else
1400 // number, possibly hidden
1401 n = (long)tv_get_number(tv);
1402 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001403
1404 // Avoid setting a string option to the text "v:false" or similar.
1405 // In Vim9 script also don't convert a number to string.
1406 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1407 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1408 s = tv_get_string_chk(tv);
1409
1410 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001411 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001412 if (((opt_type == gov_bool || opt_type == gov_number)
1413 && *op == '.')
1414 || (opt_type == gov_string && *op != '.'))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001415 {
1416 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001417 failed = TRUE; // don't set the value
1418
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001419 }
1420 else
1421 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001422 // number, in legacy script also bool
1423 if (opt_type == gov_number
1424 || (opt_type == gov_bool && !in_vim9script()))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001425 {
1426 switch (*op)
1427 {
1428 case '+': n = numval + n; break;
1429 case '-': n = numval - n; break;
1430 case '*': n = numval * n; break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001431 case '/': n = (long)num_divide(numval, n,
1432 &failed); break;
1433 case '%': n = (long)num_modulus(numval, n,
1434 &failed); break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001435 }
Bram Moolenaara42e6e02021-06-10 18:43:25 +02001436 s = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001437 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001438 else if (opt_type == gov_string
1439 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001440 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001441 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001442 s = concat_str(stringval, s);
1443 vim_free(stringval);
1444 stringval = s;
1445 }
1446 }
1447 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001448
1449 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001450 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001451 if (opt_type != gov_string || s != NULL)
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001452 {
1453 set_option_value(arg, n, s, opt_flags);
1454 arg_end = p;
1455 }
1456 else
1457 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001458 }
1459 *p = c1;
1460 vim_free(stringval);
1461 }
1462 }
1463
1464 // ":let @r = expr": Set register contents.
1465 else if (*arg == '@')
1466 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001467 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1468 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001469 {
1470 emsg(_("E996: Cannot lock a register"));
1471 return NULL;
1472 }
1473 ++arg;
1474 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1475 semsg(_(e_letwrong), op);
1476 else if (endchars != NULL
1477 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02001478 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001479 else
1480 {
1481 char_u *ptofree = NULL;
1482 char_u *s;
1483
1484 p = tv_get_string_chk(tv);
1485 if (p != NULL && op != NULL && *op == '.')
1486 {
1487 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1488 if (s != NULL)
1489 {
1490 p = ptofree = concat_str(s, p);
1491 vim_free(s);
1492 }
1493 }
1494 if (p != NULL)
1495 {
1496 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1497 arg_end = arg + 1;
1498 }
1499 vim_free(ptofree);
1500 }
1501 }
1502
1503 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001505 // ":let {expr} = expr": Idem, name made with curly braces
1506 else if (eval_isnamec1(*arg) || *arg == '{')
1507 {
1508 lval_T lv;
1509
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001510 p = get_lval(arg, tv, &lv, FALSE, FALSE,
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001511 (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1512 ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001513 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001514 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515 if (endchars != NULL && vim_strchr(endchars,
1516 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02001517 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001518 else
1519 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001520 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001521 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001522 }
1523 }
1524 clear_lval(&lv);
1525 }
1526
1527 else
1528 semsg(_(e_invarg2), arg);
1529
1530 return arg_end;
1531}
1532
1533/*
1534 * ":unlet[!] var1 ... " command.
1535 */
1536 void
1537ex_unlet(exarg_T *eap)
1538{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001539 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001540}
1541
1542/*
1543 * ":lockvar" and ":unlockvar" commands
1544 */
1545 void
1546ex_lockvar(exarg_T *eap)
1547{
1548 char_u *arg = eap->arg;
1549 int deep = 2;
1550
1551 if (eap->forceit)
1552 deep = -1;
1553 else if (vim_isdigit(*arg))
1554 {
1555 deep = getdigits(&arg);
1556 arg = skipwhite(arg);
1557 }
1558
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001559 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001560}
1561
1562/*
1563 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001564 * Also used for Vim9 script. "callback" is invoked as:
1565 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001566 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001567 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001568ex_unletlock(
1569 exarg_T *eap,
1570 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001571 int deep,
1572 int glv_flags,
1573 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1574 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001575{
1576 char_u *arg = argstart;
1577 char_u *name_end;
1578 int error = FALSE;
1579 lval_T lv;
1580
1581 do
1582 {
1583 if (*arg == '$')
1584 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001585 lv.ll_name = arg;
1586 lv.ll_tv = NULL;
1587 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001588 if (get_env_len(&arg) == 0)
1589 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001590 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001591 return;
1592 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001593 if (!error && !eap->skip
1594 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1595 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001596 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001597 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001598 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001599 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001600 // Parse the name and find the end.
1601 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001602 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001603 if (lv.ll_name == NULL)
1604 error = TRUE; // error but continue parsing
1605 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1606 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001607 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001608 if (name_end != NULL)
1609 {
1610 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001611 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001612 }
1613 if (!(eap->skip || error))
1614 clear_lval(&lv);
1615 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001616 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001617
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001618 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001619 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001620 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001621
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001622 if (!eap->skip)
1623 clear_lval(&lv);
1624 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001625
1626 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001627 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001628
1629 eap->nextcmd = check_nextcmd(arg);
1630}
1631
1632 static int
1633do_unlet_var(
1634 lval_T *lp,
1635 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001636 exarg_T *eap,
1637 int deep UNUSED,
1638 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001639{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001640 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001641 int ret = OK;
1642 int cc;
1643
1644 if (lp->ll_tv == NULL)
1645 {
1646 cc = *name_end;
1647 *name_end = NUL;
1648
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001649 // Environment variable, normal name or expanded name.
1650 if (*lp->ll_name == '$')
1651 vim_unsetenv(lp->ll_name + 1);
1652 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001653 ret = FAIL;
1654 *name_end = cc;
1655 }
1656 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001657 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001658 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001659 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001660 return FAIL;
1661 else if (lp->ll_range)
1662 {
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001663 if (list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_name, lp->ll_n1,
1664 !lp->ll_empty2, lp->ll_n2) == FAIL)
1665 return FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001666 }
1667 else
1668 {
1669 if (lp->ll_list != NULL)
1670 // unlet a List item.
1671 listitem_remove(lp->ll_list, lp->ll_li);
1672 else
1673 // unlet a Dictionary item.
1674 dictitem_remove(lp->ll_dict, lp->ll_di);
1675 }
1676
1677 return ret;
1678}
1679
1680/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001681 * Unlet one item or a range of items from a list.
1682 * Return OK or FAIL.
1683 */
1684 int
1685list_unlet_range(
1686 list_T *l,
1687 listitem_T *li_first,
1688 char_u *name,
1689 long n1_arg,
1690 int has_n2,
1691 long n2)
1692{
1693 listitem_T *li = li_first;
1694 int n1 = n1_arg;
1695
1696 while (li != NULL && (!has_n2 || n2 >= n1))
1697 {
1698 if (value_check_lock(li->li_tv.v_lock, name, FALSE))
1699 return FAIL;
1700 li = li->li_next;
1701 ++n1;
1702 }
1703
1704 // Delete a range of List items.
1705 li = li_first;
1706 n1 = n1_arg;
1707 while (li != NULL && (!has_n2 || n2 >= n1))
1708 {
1709 listitem_T *next = li->li_next;
1710
1711 listitem_remove(l, li);
1712 li = next;
1713 ++n1;
1714 }
1715 return OK;
1716}
1717/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001718 * "unlet" a variable. Return OK if it existed, FAIL if not.
1719 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1720 */
1721 int
1722do_unlet(char_u *name, int forceit)
1723{
1724 hashtab_T *ht;
1725 hashitem_T *hi;
1726 char_u *varname;
1727 dict_T *d;
1728 dictitem_T *di;
1729
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001730 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001731 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001732 return FAIL;
1733
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001734 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001735
1736 // can't :unlet a script variable in Vim9 script from a function
1737 if (ht == get_script_local_ht()
1738 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1739 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1740 == SCRIPT_VERSION_VIM9
1741 && check_vim9_unlet(name) == FAIL)
1742 return FAIL;
1743
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001744 if (ht != NULL && *varname != NUL)
1745 {
1746 d = get_current_funccal_dict(ht);
1747 if (d == NULL)
1748 {
1749 if (ht == &globvarht)
1750 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001751 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001752 d = &vimvardict;
1753 else
1754 {
1755 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1756 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1757 }
1758 if (d == NULL)
1759 {
1760 internal_error("do_unlet()");
1761 return FAIL;
1762 }
1763 }
1764 hi = hash_find(ht, varname);
1765 if (HASHITEM_EMPTY(hi))
1766 hi = find_hi_in_scoped_ht(name, &ht);
1767 if (hi != NULL && !HASHITEM_EMPTY(hi))
1768 {
1769 di = HI2DI(hi);
1770 if (var_check_fixed(di->di_flags, name, FALSE)
1771 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001772 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001773 return FAIL;
1774
1775 delete_var(ht, hi);
1776 return OK;
1777 }
1778 }
1779 if (forceit)
1780 return OK;
1781 semsg(_("E108: No such variable: \"%s\""), name);
1782 return FAIL;
1783}
1784
1785/*
1786 * Lock or unlock variable indicated by "lp".
1787 * "deep" is the levels to go (-1 for unlimited);
1788 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1789 */
1790 static int
1791do_lock_var(
1792 lval_T *lp,
1793 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001794 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001795 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001796 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001797{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001798 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001799 int ret = OK;
1800 int cc;
1801 dictitem_T *di;
1802
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001803 if (lp->ll_tv == NULL)
1804 {
1805 cc = *name_end;
1806 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001807 if (*lp->ll_name == '$')
1808 {
1809 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001810 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001811 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001812 else
1813 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001814 // Normal name or expanded name.
1815 di = find_var(lp->ll_name, NULL, TRUE);
1816 if (di == NULL)
1817 ret = FAIL;
1818 else if ((di->di_flags & DI_FLAGS_FIX)
1819 && di->di_tv.v_type != VAR_DICT
1820 && di->di_tv.v_type != VAR_LIST)
1821 {
1822 // For historic reasons this error is not given for a list or
1823 // dict. E.g., the b: dict could be locked/unlocked.
1824 semsg(_(e_lock_unlock), lp->ll_name);
1825 ret = FAIL;
1826 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001827 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001828 {
1829 if (lock)
1830 di->di_flags |= DI_FLAGS_LOCK;
1831 else
1832 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001833 if (deep != 0)
1834 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001835 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001836 }
1837 *name_end = cc;
1838 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001839 else if (deep == 0)
1840 {
1841 // nothing to do
1842 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001843 else if (lp->ll_range)
1844 {
1845 listitem_T *li = lp->ll_li;
1846
1847 // (un)lock a range of List items.
1848 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1849 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001850 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001851 li = li->li_next;
1852 ++lp->ll_n1;
1853 }
1854 }
1855 else if (lp->ll_list != NULL)
1856 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001857 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001858 else
1859 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001860 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001861
1862 return ret;
1863}
1864
1865/*
1866 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001867 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1868 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001869 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001870 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001871item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001872{
1873 static int recurse = 0;
1874 list_T *l;
1875 listitem_T *li;
1876 dict_T *d;
1877 blob_T *b;
1878 hashitem_T *hi;
1879 int todo;
1880
1881 if (recurse >= DICT_MAXNEST)
1882 {
1883 emsg(_("E743: variable nested too deep for (un)lock"));
1884 return;
1885 }
1886 if (deep == 0)
1887 return;
1888 ++recurse;
1889
1890 // lock/unlock the item itself
1891 if (lock)
1892 tv->v_lock |= VAR_LOCKED;
1893 else
1894 tv->v_lock &= ~VAR_LOCKED;
1895
1896 switch (tv->v_type)
1897 {
1898 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001899 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001900 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001901 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001903 case VAR_STRING:
1904 case VAR_FUNC:
1905 case VAR_PARTIAL:
1906 case VAR_FLOAT:
1907 case VAR_SPECIAL:
1908 case VAR_JOB:
1909 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001910 case VAR_INSTR:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001911 break;
1912
1913 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001914 if ((b = tv->vval.v_blob) != NULL
1915 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001916 {
1917 if (lock)
1918 b->bv_lock |= VAR_LOCKED;
1919 else
1920 b->bv_lock &= ~VAR_LOCKED;
1921 }
1922 break;
1923 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001924 if ((l = tv->vval.v_list) != NULL
1925 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001926 {
1927 if (lock)
1928 l->lv_lock |= VAR_LOCKED;
1929 else
1930 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001931 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001932 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001933 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001934 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001935 }
1936 break;
1937 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001938 if ((d = tv->vval.v_dict) != NULL
1939 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001940 {
1941 if (lock)
1942 d->dv_lock |= VAR_LOCKED;
1943 else
1944 d->dv_lock &= ~VAR_LOCKED;
1945 if (deep < 0 || deep > 1)
1946 {
1947 // recursive: lock/unlock the items the List contains
1948 todo = (int)d->dv_hashtab.ht_used;
1949 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1950 {
1951 if (!HASHITEM_EMPTY(hi))
1952 {
1953 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001954 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1955 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001956 }
1957 }
1958 }
1959 }
1960 }
1961 --recurse;
1962}
1963
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001964#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1965/*
1966 * Delete all "menutrans_" variables.
1967 */
1968 void
1969del_menutrans_vars(void)
1970{
1971 hashitem_T *hi;
1972 int todo;
1973
1974 hash_lock(&globvarht);
1975 todo = (int)globvarht.ht_used;
1976 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1977 {
1978 if (!HASHITEM_EMPTY(hi))
1979 {
1980 --todo;
1981 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1982 delete_var(&globvarht, hi);
1983 }
1984 }
1985 hash_unlock(&globvarht);
1986}
1987#endif
1988
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001989/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001990 * Local string buffer for the next two functions to store a variable name
1991 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1992 * get_user_var_name().
1993 */
1994
1995static char_u *varnamebuf = NULL;
1996static int varnamebuflen = 0;
1997
1998/*
1999 * Function to concatenate a prefix and a variable name.
2000 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002001 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002002cat_prefix_varname(int prefix, char_u *name)
2003{
2004 int len;
2005
2006 len = (int)STRLEN(name) + 3;
2007 if (len > varnamebuflen)
2008 {
2009 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002010 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002011 varnamebuf = alloc(len);
2012 if (varnamebuf == NULL)
2013 {
2014 varnamebuflen = 0;
2015 return NULL;
2016 }
2017 varnamebuflen = len;
2018 }
2019 *varnamebuf = prefix;
2020 varnamebuf[1] = ':';
2021 STRCPY(varnamebuf + 2, name);
2022 return varnamebuf;
2023}
2024
2025/*
2026 * Function given to ExpandGeneric() to obtain the list of user defined
2027 * (global/buffer/window/built-in) variable names.
2028 */
2029 char_u *
2030get_user_var_name(expand_T *xp, int idx)
2031{
2032 static long_u gdone;
2033 static long_u bdone;
2034 static long_u wdone;
2035 static long_u tdone;
2036 static int vidx;
2037 static hashitem_T *hi;
2038 hashtab_T *ht;
2039
2040 if (idx == 0)
2041 {
2042 gdone = bdone = wdone = vidx = 0;
2043 tdone = 0;
2044 }
2045
2046 // Global variables
2047 if (gdone < globvarht.ht_used)
2048 {
2049 if (gdone++ == 0)
2050 hi = globvarht.ht_array;
2051 else
2052 ++hi;
2053 while (HASHITEM_EMPTY(hi))
2054 ++hi;
2055 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2056 return cat_prefix_varname('g', hi->hi_key);
2057 return hi->hi_key;
2058 }
2059
2060 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002061 ht =
2062#ifdef FEAT_CMDWIN
2063 // In cmdwin, the alternative buffer should be used.
2064 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2065 &prevwin->w_buffer->b_vars->dv_hashtab :
2066#endif
2067 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002068 if (bdone < ht->ht_used)
2069 {
2070 if (bdone++ == 0)
2071 hi = ht->ht_array;
2072 else
2073 ++hi;
2074 while (HASHITEM_EMPTY(hi))
2075 ++hi;
2076 return cat_prefix_varname('b', hi->hi_key);
2077 }
2078
2079 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002080 ht =
2081#ifdef FEAT_CMDWIN
2082 // In cmdwin, the alternative window should be used.
2083 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2084 &prevwin->w_vars->dv_hashtab :
2085#endif
2086 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002087 if (wdone < ht->ht_used)
2088 {
2089 if (wdone++ == 0)
2090 hi = ht->ht_array;
2091 else
2092 ++hi;
2093 while (HASHITEM_EMPTY(hi))
2094 ++hi;
2095 return cat_prefix_varname('w', hi->hi_key);
2096 }
2097
2098 // t: variables
2099 ht = &curtab->tp_vars->dv_hashtab;
2100 if (tdone < ht->ht_used)
2101 {
2102 if (tdone++ == 0)
2103 hi = ht->ht_array;
2104 else
2105 ++hi;
2106 while (HASHITEM_EMPTY(hi))
2107 ++hi;
2108 return cat_prefix_varname('t', hi->hi_key);
2109 }
2110
2111 // v: variables
2112 if (vidx < VV_LEN)
2113 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2114
2115 VIM_CLEAR(varnamebuf);
2116 varnamebuflen = 0;
2117 return NULL;
2118}
2119
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002120 char *
2121get_var_special_name(int nr)
2122{
2123 switch (nr)
2124 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002125 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2126 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002127 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002128 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002129 }
2130 internal_error("get_var_special_name()");
2131 return "42";
2132}
2133
2134/*
2135 * Returns the global variable dictionary
2136 */
2137 dict_T *
2138get_globvar_dict(void)
2139{
2140 return &globvardict;
2141}
2142
2143/*
2144 * Returns the global variable hash table
2145 */
2146 hashtab_T *
2147get_globvar_ht(void)
2148{
2149 return &globvarht;
2150}
2151
2152/*
2153 * Returns the v: variable dictionary
2154 */
2155 dict_T *
2156get_vimvar_dict(void)
2157{
2158 return &vimvardict;
2159}
2160
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002161/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002163 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 */
2165 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002166find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002167{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002168 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2169 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002170
2171 if (di == NULL)
2172 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002173 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002174 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2175 return (int)(vv - vimvars);
2176}
2177
2178
2179/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002180 * Set type of v: variable to "type".
2181 */
2182 void
2183set_vim_var_type(int idx, vartype_T type)
2184{
2185 vimvars[idx].vv_type = type;
2186}
2187
2188/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002189 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002190 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002191 */
2192 void
2193set_vim_var_nr(int idx, varnumber_T val)
2194{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002195 vimvars[idx].vv_nr = val;
2196}
2197
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002198 char *
2199get_vim_var_name(int idx)
2200{
2201 return vimvars[idx].vv_name;
2202}
2203
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002204/*
2205 * Get typval_T v: variable value.
2206 */
2207 typval_T *
2208get_vim_var_tv(int idx)
2209{
2210 return &vimvars[idx].vv_tv;
2211}
2212
2213/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002214 * Set v: variable to "tv". Only accepts the same type.
2215 * Takes over the value of "tv".
2216 */
2217 int
2218set_vim_var_tv(int idx, typval_T *tv)
2219{
2220 if (vimvars[idx].vv_type != tv->v_type)
2221 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002222 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002223 clear_tv(tv);
2224 return FAIL;
2225 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002226 // VV_RO is also checked when compiling, but let's check here as well.
2227 if (vimvars[idx].vv_flags & VV_RO)
2228 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002229 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002230 return FAIL;
2231 }
2232 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2233 {
2234 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2235 return FAIL;
2236 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002237 clear_tv(&vimvars[idx].vv_di.di_tv);
2238 vimvars[idx].vv_di.di_tv = *tv;
2239 return OK;
2240}
2241
2242/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002243 * Get number v: variable value.
2244 */
2245 varnumber_T
2246get_vim_var_nr(int idx)
2247{
2248 return vimvars[idx].vv_nr;
2249}
2250
2251/*
2252 * Get string v: variable value. Uses a static buffer, can only be used once.
2253 * If the String variable has never been set, return an empty string.
2254 * Never returns NULL;
2255 */
2256 char_u *
2257get_vim_var_str(int idx)
2258{
2259 return tv_get_string(&vimvars[idx].vv_tv);
2260}
2261
2262/*
2263 * Get List v: variable value. Caller must take care of reference count when
2264 * needed.
2265 */
2266 list_T *
2267get_vim_var_list(int idx)
2268{
2269 return vimvars[idx].vv_list;
2270}
2271
2272/*
2273 * Get Dict v: variable value. Caller must take care of reference count when
2274 * needed.
2275 */
2276 dict_T *
2277get_vim_var_dict(int idx)
2278{
2279 return vimvars[idx].vv_dict;
2280}
2281
2282/*
2283 * Set v:char to character "c".
2284 */
2285 void
2286set_vim_var_char(int c)
2287{
2288 char_u buf[MB_MAXBYTES + 1];
2289
2290 if (has_mbyte)
2291 buf[(*mb_char2bytes)(c, buf)] = NUL;
2292 else
2293 {
2294 buf[0] = c;
2295 buf[1] = NUL;
2296 }
2297 set_vim_var_string(VV_CHAR, buf, -1);
2298}
2299
2300/*
2301 * Set v:count to "count" and v:count1 to "count1".
2302 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2303 */
2304 void
2305set_vcount(
2306 long count,
2307 long count1,
2308 int set_prevcount)
2309{
2310 if (set_prevcount)
2311 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2312 vimvars[VV_COUNT].vv_nr = count;
2313 vimvars[VV_COUNT1].vv_nr = count1;
2314}
2315
2316/*
2317 * Save variables that might be changed as a side effect. Used when executing
2318 * a timer callback.
2319 */
2320 void
2321save_vimvars(vimvars_save_T *vvsave)
2322{
2323 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2324 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2325 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2326}
2327
2328/*
2329 * Restore variables saved by save_vimvars().
2330 */
2331 void
2332restore_vimvars(vimvars_save_T *vvsave)
2333{
2334 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2335 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2336 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2337}
2338
2339/*
2340 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2341 * value.
2342 */
2343 void
2344set_vim_var_string(
2345 int idx,
2346 char_u *val,
2347 int len) // length of "val" to use or -1 (whole string)
2348{
2349 clear_tv(&vimvars[idx].vv_di.di_tv);
2350 vimvars[idx].vv_type = VAR_STRING;
2351 if (val == NULL)
2352 vimvars[idx].vv_str = NULL;
2353 else if (len == -1)
2354 vimvars[idx].vv_str = vim_strsave(val);
2355 else
2356 vimvars[idx].vv_str = vim_strnsave(val, len);
2357}
2358
2359/*
2360 * Set List v: variable to "val".
2361 */
2362 void
2363set_vim_var_list(int idx, list_T *val)
2364{
2365 clear_tv(&vimvars[idx].vv_di.di_tv);
2366 vimvars[idx].vv_type = VAR_LIST;
2367 vimvars[idx].vv_list = val;
2368 if (val != NULL)
2369 ++val->lv_refcount;
2370}
2371
2372/*
2373 * Set Dictionary v: variable to "val".
2374 */
2375 void
2376set_vim_var_dict(int idx, dict_T *val)
2377{
2378 clear_tv(&vimvars[idx].vv_di.di_tv);
2379 vimvars[idx].vv_type = VAR_DICT;
2380 vimvars[idx].vv_dict = val;
2381 if (val != NULL)
2382 {
2383 ++val->dv_refcount;
2384 dict_set_items_ro(val);
2385 }
2386}
2387
2388/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002389 * Set the v:argv list.
2390 */
2391 void
2392set_argv_var(char **argv, int argc)
2393{
2394 list_T *l = list_alloc();
2395 int i;
2396
2397 if (l == NULL)
2398 getout(1);
2399 l->lv_lock = VAR_FIXED;
2400 for (i = 0; i < argc; ++i)
2401 {
2402 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2403 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002404 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002405 }
2406 set_vim_var_list(VV_ARGV, l);
2407}
2408
2409/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002410 * Reset v:register, taking the 'clipboard' setting into account.
2411 */
2412 void
2413reset_reg_var(void)
2414{
2415 int regname = 0;
2416
2417 // Adjust the register according to 'clipboard', so that when
2418 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2419#ifdef FEAT_CLIPBOARD
2420 adjust_clip_reg(&regname);
2421#endif
2422 set_reg_var(regname);
2423}
2424
2425/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002426 * Set v:register if needed.
2427 */
2428 void
2429set_reg_var(int c)
2430{
2431 char_u regname;
2432
2433 if (c == 0 || c == ' ')
2434 regname = '"';
2435 else
2436 regname = c;
2437 // Avoid free/alloc when the value is already right.
2438 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2439 set_vim_var_string(VV_REG, &regname, 1);
2440}
2441
2442/*
2443 * Get or set v:exception. If "oldval" == NULL, return the current value.
2444 * Otherwise, restore the value to "oldval" and return NULL.
2445 * Must always be called in pairs to save and restore v:exception! Does not
2446 * take care of memory allocations.
2447 */
2448 char_u *
2449v_exception(char_u *oldval)
2450{
2451 if (oldval == NULL)
2452 return vimvars[VV_EXCEPTION].vv_str;
2453
2454 vimvars[VV_EXCEPTION].vv_str = oldval;
2455 return NULL;
2456}
2457
2458/*
2459 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2460 * Otherwise, restore the value to "oldval" and return NULL.
2461 * Must always be called in pairs to save and restore v:throwpoint! Does not
2462 * take care of memory allocations.
2463 */
2464 char_u *
2465v_throwpoint(char_u *oldval)
2466{
2467 if (oldval == NULL)
2468 return vimvars[VV_THROWPOINT].vv_str;
2469
2470 vimvars[VV_THROWPOINT].vv_str = oldval;
2471 return NULL;
2472}
2473
2474/*
2475 * Set v:cmdarg.
2476 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2477 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2478 * Must always be called in pairs!
2479 */
2480 char_u *
2481set_cmdarg(exarg_T *eap, char_u *oldarg)
2482{
2483 char_u *oldval;
2484 char_u *newval;
2485 unsigned len;
2486
2487 oldval = vimvars[VV_CMDARG].vv_str;
2488 if (eap == NULL)
2489 {
2490 vim_free(oldval);
2491 vimvars[VV_CMDARG].vv_str = oldarg;
2492 return NULL;
2493 }
2494
2495 if (eap->force_bin == FORCE_BIN)
2496 len = 6;
2497 else if (eap->force_bin == FORCE_NOBIN)
2498 len = 8;
2499 else
2500 len = 0;
2501
2502 if (eap->read_edit)
2503 len += 7;
2504
2505 if (eap->force_ff != 0)
2506 len += 10; // " ++ff=unix"
2507 if (eap->force_enc != 0)
2508 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2509 if (eap->bad_char != 0)
2510 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2511
2512 newval = alloc(len + 1);
2513 if (newval == NULL)
2514 return NULL;
2515
2516 if (eap->force_bin == FORCE_BIN)
2517 sprintf((char *)newval, " ++bin");
2518 else if (eap->force_bin == FORCE_NOBIN)
2519 sprintf((char *)newval, " ++nobin");
2520 else
2521 *newval = NUL;
2522
2523 if (eap->read_edit)
2524 STRCAT(newval, " ++edit");
2525
2526 if (eap->force_ff != 0)
2527 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2528 eap->force_ff == 'u' ? "unix"
2529 : eap->force_ff == 'd' ? "dos"
2530 : "mac");
2531 if (eap->force_enc != 0)
2532 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2533 eap->cmd + eap->force_enc);
2534 if (eap->bad_char == BAD_KEEP)
2535 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2536 else if (eap->bad_char == BAD_DROP)
2537 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2538 else if (eap->bad_char != 0)
2539 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2540 vimvars[VV_CMDARG].vv_str = newval;
2541 return oldval;
2542}
2543
2544/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002545 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002546 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2547 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002548 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2549 */
2550 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002551eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002552 char_u *name,
2553 int len, // length of "name"
2554 typval_T *rettv, // NULL when only checking existence
2555 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002556 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002557{
2558 int ret = OK;
2559 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002560 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002561 hashtab_T *ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002562 int cc;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002563 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002564
2565 // truncate the name, so that we can use strcmp()
2566 cc = name[len];
2567 name[len] = NUL;
2568
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002569 // Check for local variable when debugging.
2570 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002571 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002572 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002573 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2574
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002575 if (v != NULL)
2576 {
2577 tv = &v->di_tv;
2578 if (dip != NULL)
2579 *dip = v;
2580 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002581 else
2582 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002583 }
2584
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002585 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002587 imported_T *import;
2588 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2589
2590 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591
2592 // imported variable from another script
2593 if (import != NULL)
2594 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002595 if (import->imp_funcname != NULL)
2596 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002597 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002598 if (rettv != NULL)
2599 {
2600 rettv->v_type = VAR_FUNC;
2601 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2602 }
2603 }
Bram Moolenaara6294952020-12-27 13:39:50 +01002604 else if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002605 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002606 if ((flags & EVAL_VAR_IMPORT) == 0)
2607 {
2608 if (flags & EVAL_VAR_VERBOSE)
2609 emsg(_(e_import_as_name_not_supported_here));
2610 ret = FAIL;
2611 }
2612 else
2613 {
2614 if (rettv != NULL)
2615 {
2616 rettv->v_type = VAR_ANY;
2617 rettv->vval.v_number = import->imp_sid;
2618 }
2619 found = TRUE;
2620 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002621 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002622 else
2623 {
2624 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002625 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002627 tv = sv->sv_tv;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002628 type = sv->sv_type;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002629 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002631 else if (in_vim9script())
2632 {
2633 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2634
2635 if (ufunc != NULL)
2636 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002637 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002638 if (rettv != NULL)
2639 {
2640 rettv->v_type = VAR_FUNC;
2641 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2642 }
2643 }
2644 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 }
2646
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002647 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002648 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002649 if (tv == NULL)
2650 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002651 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002652 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002653 ret = FAIL;
2654 }
2655 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002656 {
Bram Moolenaar11005b02021-07-11 20:59:00 +02002657 if (ht != NULL && ht == get_script_local_ht()
2658 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002659 {
Bram Moolenaarc967d572021-07-08 21:38:50 +02002660 svar_T *sv = find_typval_in_script(tv);
Bram Moolenaarf055d452021-07-08 20:57:24 +02002661
Bram Moolenaarf055d452021-07-08 20:57:24 +02002662 if (sv != NULL)
2663 type = sv->sv_type;
2664 }
2665
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002666 // If a list or dict variable wasn't initialized, do it now.
2667 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2668 {
2669 tv->vval.v_dict = dict_alloc();
2670 if (tv->vval.v_dict != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002671 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002672 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002673 tv->vval.v_dict->dv_type = alloc_type(type);
2674 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002675 }
2676 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2677 {
2678 tv->vval.v_list = list_alloc();
2679 if (tv->vval.v_list != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002680 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002681 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002682 tv->vval.v_list->lv_type = alloc_type(type);
2683 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002684 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002685 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL)
2686 {
2687 tv->vval.v_blob = blob_alloc();
2688 if (tv->vval.v_blob != NULL)
2689 ++tv->vval.v_blob->bv_refcount;
2690 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002691 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002692 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002693 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002694
2695 name[len] = cc;
2696
2697 return ret;
2698}
2699
2700/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002701 * Check if variable "name[len]" is a local variable or an argument.
2702 * If so, "*eval_lavars_used" is set to TRUE.
2703 */
2704 void
2705check_vars(char_u *name, int len)
2706{
2707 int cc;
2708 char_u *varname;
2709 hashtab_T *ht;
2710
2711 if (eval_lavars_used == NULL)
2712 return;
2713
2714 // truncate the name, so that we can use strcmp()
2715 cc = name[len];
2716 name[len] = NUL;
2717
2718 ht = find_var_ht(name, &varname);
2719 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2720 {
2721 if (find_var(name, NULL, TRUE) != NULL)
2722 *eval_lavars_used = TRUE;
2723 }
2724
2725 name[len] = cc;
2726}
2727
2728/*
2729 * Find variable "name" in the list of variables.
2730 * Return a pointer to it if found, NULL if not found.
2731 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002732 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002733 */
2734 dictitem_T *
2735find_var(char_u *name, hashtab_T **htp, int no_autoload)
2736{
2737 char_u *varname;
2738 hashtab_T *ht;
2739 dictitem_T *ret = NULL;
2740
2741 ht = find_var_ht(name, &varname);
2742 if (htp != NULL)
2743 *htp = ht;
2744 if (ht == NULL)
2745 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002746 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002747 if (ret != NULL)
2748 return ret;
2749
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002750 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002751 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002752 if (ret != NULL)
2753 return ret;
2754
2755 // in Vim9 script items without a scope can be script-local
2756 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2757 {
2758 ht = get_script_local_ht();
2759 if (ht != NULL)
2760 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002761 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002762 if (ret != NULL)
2763 {
2764 if (htp != NULL)
2765 *htp = ht;
2766 return ret;
2767 }
2768 }
2769 }
2770
2771 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002772}
2773
2774/*
2775 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002776 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002777 * Returns NULL if not found.
2778 */
2779 dictitem_T *
2780find_var_in_ht(
2781 hashtab_T *ht,
2782 int htname,
2783 char_u *varname,
2784 int no_autoload)
2785{
2786 hashitem_T *hi;
2787
2788 if (*varname == NUL)
2789 {
2790 // Must be something like "s:", otherwise "ht" would be NULL.
2791 switch (htname)
2792 {
2793 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2794 case 'g': return &globvars_var;
2795 case 'v': return &vimvars_var;
2796 case 'b': return &curbuf->b_bufvar;
2797 case 'w': return &curwin->w_winvar;
2798 case 't': return &curtab->tp_winvar;
2799 case 'l': return get_funccal_local_var();
2800 case 'a': return get_funccal_args_var();
2801 }
2802 return NULL;
2803 }
2804
2805 hi = hash_find(ht, varname);
2806 if (HASHITEM_EMPTY(hi))
2807 {
2808 // For global variables we may try auto-loading the script. If it
2809 // worked find the variable again. Don't auto-load a script if it was
2810 // loaded already, otherwise it would be loaded every time when
2811 // checking if a function name is a Funcref variable.
2812 if (ht == &globvarht && !no_autoload)
2813 {
2814 // Note: script_autoload() may make "hi" invalid. It must either
2815 // be obtained again or not used.
2816 if (!script_autoload(varname, FALSE) || aborting())
2817 return NULL;
2818 hi = hash_find(ht, varname);
2819 }
2820 if (HASHITEM_EMPTY(hi))
2821 return NULL;
2822 }
2823 return HI2DI(hi);
2824}
2825
2826/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827 * Get the script-local hashtab. NULL if not in a script context.
2828 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002829 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830get_script_local_ht(void)
2831{
2832 scid_T sid = current_sctx.sc_sid;
2833
Bram Moolenaare3d46852020-08-29 13:39:17 +02002834 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 return &SCRIPT_VARS(sid);
2836 return NULL;
2837}
2838
2839/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002840 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002841 * When "cmd" is TRUE it must look like a command, a function must be followed
2842 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01002843 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002844 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01002845 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002846lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01002847 char_u *name,
2848 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002849 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01002850 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851{
2852 hashtab_T *ht = get_script_local_ht();
2853 char_u buffer[30];
2854 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01002855 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002857 int is_global = FALSE;
2858 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859
2860 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002861 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 if (len < sizeof(buffer) - 1)
2863 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002864 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865 vim_strncpy(buffer, name, len);
2866 p = buffer;
2867 }
2868 else
2869 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002870 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002872 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873 }
2874
2875 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002876 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877
2878 // if not script-local, then perhaps imported
Bram Moolenaar709664c2020-12-12 14:33:41 +01002879 if (res == FAIL && find_imported(p, 0, NULL) != NULL)
2880 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881 if (p != buffer)
2882 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002883
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002884 // Find a function, so that a following "->" works.
2885 // When used as a command require "(" or "->" to follow, "Cmd" is a user
2886 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002887 if (res != OK)
2888 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002889 p = skipwhite(name + len);
2890
2891 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002892 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002893 // Do not check for an internal function, since it might also be a
2894 // valid command, such as ":split" versus "split()".
2895 // Skip "g:" before a function name.
2896 if (name[0] == 'g' && name[1] == ':')
2897 {
2898 is_global = TRUE;
2899 fname = name + 2;
2900 }
2901 if (find_func(fname, is_global, NULL) != NULL)
2902 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002903 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002904 }
2905
Bram Moolenaar709664c2020-12-12 14:33:41 +01002906 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907}
2908
2909/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002910 * Find the hashtab used for a variable name.
2911 * Return NULL if the name is not valid.
2912 * Set "varname" to the start of name without ':'.
2913 */
2914 hashtab_T *
2915find_var_ht(char_u *name, char_u **varname)
2916{
2917 hashitem_T *hi;
2918 hashtab_T *ht;
2919
2920 if (name[0] == NUL)
2921 return NULL;
2922 if (name[1] != ':')
2923 {
2924 // The name must not start with a colon or #.
2925 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2926 return NULL;
2927 *varname = name;
2928
2929 // "version" is "v:version" in all scopes if scriptversion < 3.
2930 // Same for a few other variables marked with VV_COMPAT.
2931 if (current_sctx.sc_version < 3)
2932 {
2933 hi = hash_find(&compat_hashtab, name);
2934 if (!HASHITEM_EMPTY(hi))
2935 return &compat_hashtab;
2936 }
2937
2938 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 if (ht != NULL)
2940 return ht; // local variable
2941
Bram Moolenaarf0a40692021-06-11 22:05:47 +02002942 // In Vim9 script items at the script level are script-local, except
2943 // for autoload names.
2944 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 {
2946 ht = get_script_local_ht();
2947 if (ht != NULL)
2948 return ht;
2949 }
2950
2951 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002952 }
2953 *varname = name + 2;
2954 if (*name == 'g') // global variable
2955 return &globvarht;
2956 // There must be no ':' or '#' in the rest of the name, unless g: is used
2957 if (vim_strchr(name + 2, ':') != NULL
2958 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2959 return NULL;
2960 if (*name == 'b') // buffer variable
2961 return &curbuf->b_vars->dv_hashtab;
2962 if (*name == 'w') // window variable
2963 return &curwin->w_vars->dv_hashtab;
2964 if (*name == 't') // tab page variable
2965 return &curtab->tp_vars->dv_hashtab;
2966 if (*name == 'v') // v: variable
2967 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002968 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002969 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002971 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 if (*name == 'a') // a: function argument
2973 return get_funccal_args_ht();
2974 if (*name == 'l') // l: local function variable
2975 return get_funccal_local_ht();
2976 }
2977 if (*name == 's') // script variable
2978 {
2979 ht = get_script_local_ht();
2980 if (ht != NULL)
2981 return ht;
2982 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002983 return NULL;
2984}
2985
2986/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002987 * Get the string value of a (global/local) variable.
2988 * Note: see tv_get_string() for how long the pointer remains valid.
2989 * Returns NULL when it doesn't exist.
2990 */
2991 char_u *
2992get_var_value(char_u *name)
2993{
2994 dictitem_T *v;
2995
2996 v = find_var(name, NULL, FALSE);
2997 if (v == NULL)
2998 return NULL;
2999 return tv_get_string(&v->di_tv);
3000}
3001
3002/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003003 * Allocate a new hashtab for a sourced script. It will be used while
3004 * sourcing this script and when executing functions defined in the script.
3005 */
3006 void
3007new_script_vars(scid_T id)
3008{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003009 scriptvar_T *sv;
3010
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003011 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3012 if (sv == NULL)
3013 return;
3014 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003015 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003016}
3017
3018/*
3019 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3020 * point to it.
3021 */
3022 void
3023init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3024{
3025 hash_init(&dict->dv_hashtab);
3026 dict->dv_lock = 0;
3027 dict->dv_scope = scope;
3028 dict->dv_refcount = DO_NOT_FREE_CNT;
3029 dict->dv_copyID = 0;
3030 dict_var->di_tv.vval.v_dict = dict;
3031 dict_var->di_tv.v_type = VAR_DICT;
3032 dict_var->di_tv.v_lock = VAR_FIXED;
3033 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3034 dict_var->di_key[0] = NUL;
3035}
3036
3037/*
3038 * Unreference a dictionary initialized by init_var_dict().
3039 */
3040 void
3041unref_var_dict(dict_T *dict)
3042{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003043 // Now the dict needs to be freed if no one else is using it, go back to
3044 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003045 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3046 dict_unref(dict);
3047}
3048
3049/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003050 * Clean up a list of internal variables.
3051 * Frees all allocated variables and the value they contain.
3052 * Clears hashtab "ht", does not free it.
3053 */
3054 void
3055vars_clear(hashtab_T *ht)
3056{
3057 vars_clear_ext(ht, TRUE);
3058}
3059
3060/*
3061 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3062 */
3063 void
3064vars_clear_ext(hashtab_T *ht, int free_val)
3065{
3066 int todo;
3067 hashitem_T *hi;
3068 dictitem_T *v;
3069
3070 hash_lock(ht);
3071 todo = (int)ht->ht_used;
3072 for (hi = ht->ht_array; todo > 0; ++hi)
3073 {
3074 if (!HASHITEM_EMPTY(hi))
3075 {
3076 --todo;
3077
3078 // Free the variable. Don't remove it from the hashtab,
3079 // ht_array might change then. hash_clear() takes care of it
3080 // later.
3081 v = HI2DI(hi);
3082 if (free_val)
3083 clear_tv(&v->di_tv);
3084 if (v->di_flags & DI_FLAGS_ALLOC)
3085 vim_free(v);
3086 }
3087 }
3088 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003089 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003090}
3091
3092/*
3093 * Delete a variable from hashtab "ht" at item "hi".
3094 * Clear the variable value and free the dictitem.
3095 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003096 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003097delete_var(hashtab_T *ht, hashitem_T *hi)
3098{
3099 dictitem_T *di = HI2DI(hi);
3100
3101 hash_remove(ht, hi);
3102 clear_tv(&di->di_tv);
3103 vim_free(di);
3104}
3105
3106/*
3107 * List the value of one internal variable.
3108 */
3109 static void
3110list_one_var(dictitem_T *v, char *prefix, int *first)
3111{
3112 char_u *tofree;
3113 char_u *s;
3114 char_u numbuf[NUMBUFLEN];
3115
3116 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3117 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3118 s == NULL ? (char_u *)"" : s, first);
3119 vim_free(tofree);
3120}
3121
3122 static void
3123list_one_var_a(
3124 char *prefix,
3125 char_u *name,
3126 int type,
3127 char_u *string,
3128 int *first) // when TRUE clear rest of screen and set to FALSE
3129{
3130 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3131 msg_start();
3132 msg_puts(prefix);
3133 if (name != NULL) // "a:" vars don't have a name stored
3134 msg_puts((char *)name);
3135 msg_putchar(' ');
3136 msg_advance(22);
3137 if (type == VAR_NUMBER)
3138 msg_putchar('#');
3139 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3140 msg_putchar('*');
3141 else if (type == VAR_LIST)
3142 {
3143 msg_putchar('[');
3144 if (*string == '[')
3145 ++string;
3146 }
3147 else if (type == VAR_DICT)
3148 {
3149 msg_putchar('{');
3150 if (*string == '{')
3151 ++string;
3152 }
3153 else
3154 msg_putchar(' ');
3155
3156 msg_outtrans(string);
3157
3158 if (type == VAR_FUNC || type == VAR_PARTIAL)
3159 msg_puts("()");
3160 if (*first)
3161 {
3162 msg_clr_eos();
3163 *first = FALSE;
3164 }
3165}
3166
3167/*
3168 * Set variable "name" to value in "tv".
3169 * If the variable already exists, the value is updated.
3170 * Otherwise the variable is created.
3171 */
3172 void
3173set_var(
3174 char_u *name,
3175 typval_T *tv,
3176 int copy) // make copy of value in "tv"
3177{
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003178 set_var_const(name, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003179}
3180
3181/*
3182 * Set variable "name" to value in "tv".
3183 * If the variable already exists and "is_const" is FALSE the value is updated.
3184 * Otherwise the variable is created.
3185 */
3186 void
3187set_var_const(
3188 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003190 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003191 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003192 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003193 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003194{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003195 typval_T *tv = tv_arg;
3196 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 dictitem_T *di;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003198 typval_T *dest_tv = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003199 char_u *varname;
3200 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003202 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003203 int var_in_vim9script;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003204 int flags = flags_arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003205
3206 ht = find_var_ht(name, &varname);
3207 if (ht == NULL || *varname == NUL)
3208 {
3209 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003210 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003211 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003212 is_script_local = ht == get_script_local_ht();
3213
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003214 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003215 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003216 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003217 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003218 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003219 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003220 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003221 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003222 }
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003223 if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
3224 && vim_strchr((char_u *)"gwbt", name[0]) != NULL)
3225 // Do not make g:var, w:var, b:var or t:var final.
3226 flags &= ~ASSIGN_FINAL;
3227
Bram Moolenaare535db82021-03-31 21:07:24 +02003228 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003229 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3230 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003231 // For "[a, _] = list" the underscore is ignored.
3232 if ((flags & ASSIGN_UNPACK) == 0)
3233 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003234 goto failed;
3235 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003236
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003238
Bram Moolenaar24e93162021-07-18 20:40:33 +02003239 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003240 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003241 imported_T *import = find_imported(varname, 0, NULL);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003242
Bram Moolenaar24e93162021-07-18 20:40:33 +02003243 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003244 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003245 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
3246 svar_T *sv;
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003247 where_T where = WHERE_INIT;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003248
3249 // imported variable from another script
3250 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003252 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003253 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 }
Bram Moolenaar60579352021-07-19 21:45:07 +02003255 sv = ((svar_T *)si->sn_var_vals.ga_data) + import->imp_var_vals_idx;
3256
Bram Moolenaar60579352021-07-19 21:45:07 +02003257 where.wt_variable = TRUE;
3258 if (check_typval_type(sv->sv_type, tv, where) == FAIL
3259 || value_check_lock(sv->sv_tv->v_lock, name, FALSE))
3260 {
3261 goto failed;
3262 }
3263
Bram Moolenaar24e93162021-07-18 20:40:33 +02003264 dest_tv = sv->sv_tv;
Bram Moolenaarf6488542021-07-18 21:24:50 +02003265 clear_tv(dest_tv);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003266 }
3267 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268
Bram Moolenaar24e93162021-07-18 20:40:33 +02003269 if (dest_tv == NULL)
3270 {
3271 // Search in parent scope which is possible to reference from lambda
3272 if (di == NULL)
3273 di = find_var_in_scoped_ht(name, TRUE);
3274
3275 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003276 && var_wrong_func_name(name, di == NULL))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003277 goto failed;
3278
3279 if (need_convert_to_bool(type, tv))
3280 {
Bram Moolenaarf6488542021-07-18 21:24:50 +02003281 // Destination is a bool and the value is not, but it can be
3282 // converted.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003283 CLEAR_FIELD(bool_tv);
3284 bool_tv.v_type = VAR_BOOL;
3285 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3286 tv = &bool_tv;
3287 }
3288
3289 if (di != NULL)
3290 {
3291 // Item already exists. Allowed to replace when reloading.
3292 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
3293 {
3294 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaarf6488542021-07-18 21:24:50 +02003295 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003296 {
3297 emsg(_(e_cannot_mod));
3298 goto failed;
3299 }
3300
3301 if (is_script_local && vim9script
Bram Moolenaarf6488542021-07-18 21:24:50 +02003302 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003303 {
3304 semsg(_(e_redefining_script_item_str), name);
3305 goto failed;
3306 }
3307
3308 if (var_in_vim9script)
3309 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003310 where_T where = WHERE_INIT;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003311
3312 // check the type and adjust to bool if needed
3313 where.wt_index = var_idx;
3314 where.wt_variable = TRUE;
Bram Moolenaarf6488542021-07-18 21:24:50 +02003315 if (check_script_var_type(&di->di_tv, tv, name, where)
3316 == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003317 goto failed;
3318 }
3319
3320 if (var_check_permission(di, name) == FAIL)
3321 goto failed;
3322 }
3323 else
3324 {
3325 // can only redefine once
3326 di->di_flags &= ~DI_FLAGS_RELOAD;
3327
Bram Moolenaarf6488542021-07-18 21:24:50 +02003328 // A Vim9 script-local variable is also present in sn_all_vars
3329 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaar24e93162021-07-18 20:40:33 +02003330 if (var_in_vim9script)
3331 update_vim9_script_var(FALSE, di, flags, tv, &type,
Bram Moolenaarf6488542021-07-18 21:24:50 +02003332 (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003333 }
3334
3335 // existing variable, need to clear the value
3336
3337 // Handle setting internal di: variables separately where needed to
3338 // prevent changing the type.
3339 if (ht == &vimvarht)
3340 {
3341 if (di->di_tv.v_type == VAR_STRING)
3342 {
3343 VIM_CLEAR(di->di_tv.vval.v_string);
3344 if (copy || tv->v_type != VAR_STRING)
3345 {
3346 char_u *val = tv_get_string(tv);
3347
Bram Moolenaarf6488542021-07-18 21:24:50 +02003348 // Careful: when assigning to v:errmsg and
3349 // tv_get_string() causes an error message the variable
3350 // will already be set.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003351 if (di->di_tv.vval.v_string == NULL)
3352 di->di_tv.vval.v_string = vim_strsave(val);
3353 }
3354 else
3355 {
3356 // Take over the string to avoid an extra alloc/free.
3357 di->di_tv.vval.v_string = tv->vval.v_string;
3358 tv->vval.v_string = NULL;
3359 }
3360 goto failed;
3361 }
3362 else if (di->di_tv.v_type == VAR_NUMBER)
3363 {
3364 di->di_tv.vval.v_number = tv_get_number(tv);
3365 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003366 set_search_direction(di->di_tv.vval.v_number
3367 ? '/' : '?');
Bram Moolenaar24e93162021-07-18 20:40:33 +02003368#ifdef FEAT_SEARCH_EXTRA
3369 else if (STRCMP(varname, "hlsearch") == 0)
3370 {
3371 no_hlsearch = !di->di_tv.vval.v_number;
3372 redraw_all_later(SOME_VALID);
3373 }
3374#endif
3375 goto failed;
3376 }
3377 else if (di->di_tv.v_type != tv->v_type)
3378 {
3379 semsg(_("E963: setting %s to value with wrong type"), name);
3380 goto failed;
3381 }
3382 }
3383
3384 clear_tv(&di->di_tv);
3385 }
3386 else
3387 {
3388 // Item not found, check if a function already exists.
3389 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaarf6488542021-07-18 21:24:50 +02003390 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003391 {
3392 semsg(_(e_redefining_script_item_str), name);
3393 goto failed;
3394 }
3395
Bram Moolenaar24e93162021-07-18 20:40:33 +02003396 // add a new variable
3397 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3398 {
3399 semsg(_(e_unknown_variable_str), name);
3400 goto failed;
3401 }
3402
3403 // Can't add "v:" or "a:" variable.
3404 if (ht == &vimvarht || ht == get_funccal_args_ht())
3405 {
3406 semsg(_(e_illvar), name);
3407 goto failed;
3408 }
3409
3410 // Make sure the variable name is valid. In Vim9 script an autoload
3411 // variable must be prefixed with "g:".
3412 if (!valid_varname(varname, !vim9script
Bram Moolenaarf6488542021-07-18 21:24:50 +02003413 || STRNCMP(name, "g:", 2) == 0))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003414 goto failed;
3415
3416 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3417 if (di == NULL)
3418 goto failed;
3419 STRCPY(di->di_key, varname);
3420 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
3421 {
3422 vim_free(di);
3423 goto failed;
3424 }
3425 di->di_flags = DI_FLAGS_ALLOC;
3426 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3427 di->di_flags |= DI_FLAGS_LOCK;
3428
3429 // A Vim9 script-local variable is also added to sn_all_vars and
3430 // sn_var_vals. It may set "type" from "tv".
Bram Moolenaare535db82021-03-31 21:07:24 +02003431 if (var_in_vim9script)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003432 update_vim9_script_var(TRUE, di, flags, tv, &type,
Bram Moolenaarf6488542021-07-18 21:24:50 +02003433 (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003434 }
3435
Bram Moolenaar24e93162021-07-18 20:40:33 +02003436 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003437 }
3438
3439 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003440 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003441 else
3442 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003443 *dest_tv = *tv;
3444 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003445 init_tv(tv);
3446 }
3447
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003448 if (vim9script && type != NULL)
3449 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003450 if (type->tt_type == VAR_DICT && dest_tv->vval.v_dict != NULL)
3451 dest_tv->vval.v_dict->dv_type = alloc_type(type);
3452 else if (type->tt_type == VAR_LIST && dest_tv->vval.v_list != NULL)
3453 dest_tv->vval.v_list->lv_type = alloc_type(type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003454 }
3455
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003456 // ":const var = value" locks the value
3457 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003458 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003459 // Like :lockvar! name: lock the value and what it contains, but only
3460 // if the reference count is up to one. That locks only literal
3461 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003462 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003463 return;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003464
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003465failed:
3466 if (!copy)
3467 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003468}
3469
3470/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003471 * Check in this order for backwards compatibility:
3472 * - Whether the variable is read-only
3473 * - Whether the variable value is locked
3474 * - Whether the variable is locked
3475 */
3476 int
3477var_check_permission(dictitem_T *di, char_u *name)
3478{
3479 if (var_check_ro(di->di_flags, name, FALSE)
3480 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3481 || var_check_lock(di->di_flags, name, FALSE))
3482 return FAIL;
3483 return OK;
3484}
3485
3486/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003487 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3488 * Also give an error message.
3489 */
3490 int
3491var_check_ro(int flags, char_u *name, int use_gettext)
3492{
3493 if (flags & DI_FLAGS_RO)
3494 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02003495 semsg(_(e_cannot_change_readonly_variable_str),
3496 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003497 return TRUE;
3498 }
3499 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3500 {
3501 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3502 return TRUE;
3503 }
3504 return FALSE;
3505}
3506
3507/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003508 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3509 * Also give an error message.
3510 */
3511 int
3512var_check_lock(int flags, char_u *name, int use_gettext)
3513{
3514 if (flags & DI_FLAGS_LOCK)
3515 {
3516 semsg(_(e_variable_is_locked_str),
3517 use_gettext ? (char_u *)_(name) : name);
3518 return TRUE;
3519 }
3520 return FALSE;
3521}
3522
3523/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003524 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3525 * Also give an error message.
3526 */
3527 int
3528var_check_fixed(int flags, char_u *name, int use_gettext)
3529{
3530 if (flags & DI_FLAGS_FIX)
3531 {
3532 semsg(_("E795: Cannot delete variable %s"),
3533 use_gettext ? (char_u *)_(name) : name);
3534 return TRUE;
3535 }
3536 return FALSE;
3537}
3538
3539/*
3540 * Check if a funcref is assigned to a valid variable name.
3541 * Return TRUE and give an error if not.
3542 */
3543 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003544var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003545 char_u *name, // points to start of variable name
3546 int new_var) // TRUE when creating the variable
3547{
Bram Moolenaar32154662021-03-28 21:14:06 +02003548 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
3549 // the name can be used without the s: prefix.
3550 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
3551 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003552 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3553 ? name[2] : name[0]))
3554 {
3555 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3556 name);
3557 return TRUE;
3558 }
3559 // Don't allow hiding a function. When "v" is not NULL we might be
3560 // assigning another function to the same var, the type is checked
3561 // below.
3562 if (new_var && function_exists(name, FALSE))
3563 {
3564 semsg(_("E705: Variable name conflicts with existing function: %s"),
3565 name);
3566 return TRUE;
3567 }
3568 return FALSE;
3569}
3570
3571/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003572 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3573 * value. Also give an error message, using "name" or _("name") when
3574 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003575 */
3576 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003577value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003578{
3579 if (lock & VAR_LOCKED)
3580 {
3581 semsg(_("E741: Value is locked: %s"),
3582 name == NULL ? (char_u *)_("Unknown")
3583 : use_gettext ? (char_u *)_(name)
3584 : name);
3585 return TRUE;
3586 }
3587 if (lock & VAR_FIXED)
3588 {
3589 semsg(_("E742: Cannot change value of %s"),
3590 name == NULL ? (char_u *)_("Unknown")
3591 : use_gettext ? (char_u *)_(name)
3592 : name);
3593 return TRUE;
3594 }
3595 return FALSE;
3596}
3597
3598/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003599 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003600 * Return FALSE and give an error if not.
3601 */
3602 int
Bram Moolenaar03290b82020-12-19 16:30:44 +01003603valid_varname(char_u *varname, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003604{
3605 char_u *p;
3606
3607 for (p = varname; *p != NUL; ++p)
3608 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003609 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003610 {
3611 semsg(_(e_illvar), varname);
3612 return FALSE;
3613 }
3614 return TRUE;
3615}
3616
3617/*
3618 * getwinvar() and gettabwinvar()
3619 */
3620 static void
3621getwinvar(
3622 typval_T *argvars,
3623 typval_T *rettv,
3624 int off) // 1 for gettabwinvar()
3625{
3626 win_T *win;
3627 char_u *varname;
3628 dictitem_T *v;
3629 tabpage_T *tp = NULL;
3630 int done = FALSE;
3631 win_T *oldcurwin;
3632 tabpage_T *oldtabpage;
3633 int need_switch_win;
3634
3635 if (off == 1)
3636 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3637 else
3638 tp = curtab;
3639 win = find_win_by_nr(&argvars[off], tp);
3640 varname = tv_get_string_chk(&argvars[off + 1]);
3641 ++emsg_off;
3642
3643 rettv->v_type = VAR_STRING;
3644 rettv->vval.v_string = NULL;
3645
3646 if (win != NULL && varname != NULL)
3647 {
3648 // Set curwin to be our win, temporarily. Also set the tabpage,
3649 // otherwise the window is not valid. Only do this when needed,
3650 // autocommands get blocked.
3651 need_switch_win = !(tp == curtab && win == curwin);
3652 if (!need_switch_win
3653 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3654 {
3655 if (*varname == '&')
3656 {
3657 if (varname[1] == NUL)
3658 {
3659 // get all window-local options in a dict
3660 dict_T *opts = get_winbuf_options(FALSE);
3661
3662 if (opts != NULL)
3663 {
3664 rettv_dict_set(rettv, opts);
3665 done = TRUE;
3666 }
3667 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003668 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003669 // window-local-option
3670 done = TRUE;
3671 }
3672 else
3673 {
3674 // Look up the variable.
3675 // Let getwinvar({nr}, "") return the "w:" dictionary.
3676 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3677 varname, FALSE);
3678 if (v != NULL)
3679 {
3680 copy_tv(&v->di_tv, rettv);
3681 done = TRUE;
3682 }
3683 }
3684 }
3685
3686 if (need_switch_win)
3687 // restore previous notion of curwin
3688 restore_win(oldcurwin, oldtabpage, TRUE);
3689 }
3690
3691 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3692 // use the default return value
3693 copy_tv(&argvars[off + 2], rettv);
3694
3695 --emsg_off;
3696}
3697
3698/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003699 * Set option "varname" to the value of "varp" for the current buffer/window.
3700 */
3701 static void
3702set_option_from_tv(char_u *varname, typval_T *varp)
3703{
3704 long numval = 0;
3705 char_u *strval;
3706 char_u nbuf[NUMBUFLEN];
3707 int error = FALSE;
3708
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003709 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003710 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003711 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003712 strval = (char_u *)"0"; // avoid using "false"
3713 }
3714 else
3715 {
3716 if (!in_vim9script() || varp->v_type != VAR_STRING)
3717 numval = (long)tv_get_number_chk(varp, &error);
3718 strval = tv_get_string_buf_chk(varp, nbuf);
3719 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02003720 if (!error && strval != NULL)
3721 set_option_value(varname, numval, strval, OPT_LOCAL);
3722}
3723
3724/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003725 * "setwinvar()" and "settabwinvar()" functions
3726 */
3727 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003728setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003729{
3730 win_T *win;
3731 win_T *save_curwin;
3732 tabpage_T *save_curtab;
3733 int need_switch_win;
3734 char_u *varname, *winvarname;
3735 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003736 tabpage_T *tp = NULL;
3737
3738 if (check_secure())
3739 return;
3740
3741 if (off == 1)
3742 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3743 else
3744 tp = curtab;
3745 win = find_win_by_nr(&argvars[off], tp);
3746 varname = tv_get_string_chk(&argvars[off + 1]);
3747 varp = &argvars[off + 2];
3748
3749 if (win != NULL && varname != NULL && varp != NULL)
3750 {
3751 need_switch_win = !(tp == curtab && win == curwin);
3752 if (!need_switch_win
3753 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3754 {
3755 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003756 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003757 else
3758 {
3759 winvarname = alloc(STRLEN(varname) + 3);
3760 if (winvarname != NULL)
3761 {
3762 STRCPY(winvarname, "w:");
3763 STRCPY(winvarname + 2, varname);
3764 set_var(winvarname, varp, TRUE);
3765 vim_free(winvarname);
3766 }
3767 }
3768 }
3769 if (need_switch_win)
3770 restore_win(save_curwin, save_curtab, TRUE);
3771 }
3772}
3773
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003774/*
3775 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3776 * v:option_type, and v:option_command.
3777 */
3778 void
3779reset_v_option_vars(void)
3780{
3781 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3782 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3783 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3784 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3785 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3786 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3787}
3788
3789/*
3790 * Add an assert error to v:errors.
3791 */
3792 void
3793assert_error(garray_T *gap)
3794{
3795 struct vimvar *vp = &vimvars[VV_ERRORS];
3796
3797 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003798 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003799 set_vim_var_list(VV_ERRORS, list_alloc());
3800 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3801}
3802
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003803 int
3804var_exists(char_u *var)
3805{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003806 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003807 char_u *name;
3808 char_u *tofree;
3809 typval_T tv;
3810 int len = 0;
3811 int n = FALSE;
3812
3813 // get_name_len() takes care of expanding curly braces
3814 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003815 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003816 if (len > 0)
3817 {
3818 if (tofree != NULL)
3819 name = tofree;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003820 n = (eval_variable(name, len, &tv, NULL,
3821 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003822 if (n)
3823 {
3824 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003825 arg = skipwhite(arg);
3826 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003827 if (n)
3828 clear_tv(&tv);
3829 }
3830 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003831 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003832 n = FALSE;
3833
3834 vim_free(tofree);
3835 return n;
3836}
3837
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003838static lval_T *redir_lval = NULL;
3839#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3840static garray_T redir_ga; // only valid when redir_lval is not NULL
3841static char_u *redir_endp = NULL;
3842static char_u *redir_varname = NULL;
3843
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003844 int
3845alloc_redir_lval(void)
3846{
3847 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3848 if (redir_lval == NULL)
3849 return FAIL;
3850 return OK;
3851}
3852
3853 void
3854clear_redir_lval(void)
3855{
3856 VIM_CLEAR(redir_lval);
3857}
3858
3859 void
3860init_redir_ga(void)
3861{
3862 ga_init2(&redir_ga, (int)sizeof(char), 500);
3863}
3864
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003865/*
3866 * Start recording command output to a variable
3867 * When "append" is TRUE append to an existing variable.
3868 * Returns OK if successfully completed the setup. FAIL otherwise.
3869 */
3870 int
3871var_redir_start(char_u *name, int append)
3872{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003873 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003874 typval_T tv;
3875
3876 // Catch a bad name early.
3877 if (!eval_isnamec1(*name))
3878 {
3879 emsg(_(e_invarg));
3880 return FAIL;
3881 }
3882
3883 // Make a copy of the name, it is used in redir_lval until redir ends.
3884 redir_varname = vim_strsave(name);
3885 if (redir_varname == NULL)
3886 return FAIL;
3887
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003888 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003889 {
3890 var_redir_stop();
3891 return FAIL;
3892 }
3893
3894 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003895 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003896
3897 // Parse the variable name (can be a dict or list entry).
3898 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3899 FNE_CHECK_START);
3900 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3901 {
3902 clear_lval(redir_lval);
3903 if (redir_endp != NULL && *redir_endp != NUL)
3904 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003905 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003906 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003907 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003908 redir_endp = NULL; // don't store a value, only cleanup
3909 var_redir_stop();
3910 return FAIL;
3911 }
3912
3913 // check if we can write to the variable: set it to or append an empty
3914 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003915 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003916 tv.v_type = VAR_STRING;
3917 tv.vval.v_string = (char_u *)"";
3918 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003919 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003920 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003921 else
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 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003925 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003926 {
3927 redir_endp = NULL; // don't store a value, only cleanup
3928 var_redir_stop();
3929 return FAIL;
3930 }
3931
3932 return OK;
3933}
3934
3935/*
3936 * Append "value[value_len]" to the variable set by var_redir_start().
3937 * The actual appending is postponed until redirection ends, because the value
3938 * appended may in fact be the string we write to, changing it may cause freed
3939 * memory to be used:
3940 * :redir => foo
3941 * :let foo
3942 * :redir END
3943 */
3944 void
3945var_redir_str(char_u *value, int value_len)
3946{
3947 int len;
3948
3949 if (redir_lval == NULL)
3950 return;
3951
3952 if (value_len == -1)
3953 len = (int)STRLEN(value); // Append the entire string
3954 else
3955 len = value_len; // Append only "value_len" characters
3956
3957 if (ga_grow(&redir_ga, len) == OK)
3958 {
3959 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3960 redir_ga.ga_len += len;
3961 }
3962 else
3963 var_redir_stop();
3964}
3965
3966/*
3967 * Stop redirecting command output to a variable.
3968 * Frees the allocated memory.
3969 */
3970 void
3971var_redir_stop(void)
3972{
3973 typval_T tv;
3974
3975 if (EVALCMD_BUSY)
3976 {
3977 redir_lval = NULL;
3978 return;
3979 }
3980
3981 if (redir_lval != NULL)
3982 {
3983 // If there was no error: assign the text to the variable.
3984 if (redir_endp != NULL)
3985 {
3986 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3987 tv.v_type = VAR_STRING;
3988 tv.vval.v_string = redir_ga.ga_data;
3989 // Call get_lval() again, if it's inside a Dict or List it may
3990 // have changed.
3991 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3992 FALSE, FALSE, 0, FNE_CHECK_START);
3993 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003995 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003996 clear_lval(redir_lval);
3997 }
3998
3999 // free the collected output
4000 VIM_CLEAR(redir_ga.ga_data);
4001
4002 VIM_CLEAR(redir_lval);
4003 }
4004 VIM_CLEAR(redir_varname);
4005}
4006
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004007/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004008 * Get the collected redirected text and clear redir_ga.
4009 */
4010 char_u *
4011get_clear_redir_ga(void)
4012{
4013 char_u *res;
4014
4015 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4016 res = redir_ga.ga_data;
4017 redir_ga.ga_data = NULL;
4018 return res;
4019}
4020
4021/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004022 * "gettabvar()" function
4023 */
4024 void
4025f_gettabvar(typval_T *argvars, typval_T *rettv)
4026{
4027 win_T *oldcurwin;
4028 tabpage_T *tp, *oldtabpage;
4029 dictitem_T *v;
4030 char_u *varname;
4031 int done = FALSE;
4032
4033 rettv->v_type = VAR_STRING;
4034 rettv->vval.v_string = NULL;
4035
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004036 if (in_vim9script()
4037 && (check_for_number_arg(argvars, 0) == FAIL
4038 || check_for_string_arg(argvars, 1) == FAIL))
4039 return;
4040
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004041 varname = tv_get_string_chk(&argvars[1]);
4042 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4043 if (tp != NULL && varname != NULL)
4044 {
4045 // Set tp to be our tabpage, temporarily. Also set the window to the
4046 // first window in the tabpage, otherwise the window is not valid.
4047 if (switch_win(&oldcurwin, &oldtabpage,
4048 tp == curtab || tp->tp_firstwin == NULL ? firstwin
4049 : tp->tp_firstwin, tp, TRUE) == OK)
4050 {
4051 // look up the variable
4052 // Let gettabvar({nr}, "") return the "t:" dictionary.
4053 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
4054 if (v != NULL)
4055 {
4056 copy_tv(&v->di_tv, rettv);
4057 done = TRUE;
4058 }
4059 }
4060
4061 // restore previous notion of curwin
4062 restore_win(oldcurwin, oldtabpage, TRUE);
4063 }
4064
4065 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4066 copy_tv(&argvars[2], rettv);
4067}
4068
4069/*
4070 * "gettabwinvar()" function
4071 */
4072 void
4073f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4074{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004075 if (in_vim9script()
4076 && (check_for_number_arg(argvars, 0) == FAIL
4077 || check_for_number_arg(argvars, 1) == FAIL
4078 || check_for_string_arg(argvars, 2) == FAIL))
4079 return;
4080
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004081 getwinvar(argvars, rettv, 1);
4082}
4083
4084/*
4085 * "getwinvar()" function
4086 */
4087 void
4088f_getwinvar(typval_T *argvars, typval_T *rettv)
4089{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004090 if (in_vim9script()
4091 && (check_for_number_arg(argvars, 0) == FAIL
4092 || check_for_string_arg(argvars, 1) == FAIL))
4093 return;
4094
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004095 getwinvar(argvars, rettv, 0);
4096}
4097
4098/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004099 * "getbufvar()" function
4100 */
4101 void
4102f_getbufvar(typval_T *argvars, typval_T *rettv)
4103{
4104 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004105 char_u *varname;
4106 dictitem_T *v;
4107 int done = FALSE;
4108
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004109 if (in_vim9script()
4110 && (check_for_buffer_arg(argvars, 0) == FAIL
4111 || check_for_string_arg(argvars, 1) == FAIL))
4112 return;
4113
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004114 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004115 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004116
4117 rettv->v_type = VAR_STRING;
4118 rettv->vval.v_string = NULL;
4119
4120 if (buf != NULL && varname != NULL)
4121 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004122 if (*varname == '&')
4123 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004124 buf_T *save_curbuf = curbuf;
4125
4126 // set curbuf to be our buf, temporarily
4127 curbuf = buf;
4128
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004129 if (varname[1] == NUL)
4130 {
4131 // get all buffer-local options in a dict
4132 dict_T *opts = get_winbuf_options(TRUE);
4133
4134 if (opts != NULL)
4135 {
4136 rettv_dict_set(rettv, opts);
4137 done = TRUE;
4138 }
4139 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004140 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004141 // buffer-local-option
4142 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02004143
4144 // restore previous notion of curbuf
4145 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004146 }
4147 else
4148 {
4149 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02004150 if (*varname == NUL)
4151 // Let getbufvar({nr}, "") return the "b:" dictionary.
4152 v = &buf->b_bufvar;
4153 else
4154 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
4155 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004156 if (v != NULL)
4157 {
4158 copy_tv(&v->di_tv, rettv);
4159 done = TRUE;
4160 }
4161 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004162 }
4163
4164 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4165 // use the default value
4166 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004167}
4168
4169/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004170 * "settabvar()" function
4171 */
4172 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004173f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004174{
4175 tabpage_T *save_curtab;
4176 tabpage_T *tp;
4177 char_u *varname, *tabvarname;
4178 typval_T *varp;
4179
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004180 if (check_secure())
4181 return;
4182
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004183 if (in_vim9script()
4184 && (check_for_number_arg(argvars, 0) == FAIL
4185 || check_for_string_arg(argvars, 1) == FAIL))
4186 return;
4187
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004188 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4189 varname = tv_get_string_chk(&argvars[1]);
4190 varp = &argvars[2];
4191
4192 if (varname != NULL && varp != NULL && tp != NULL)
4193 {
4194 save_curtab = curtab;
4195 goto_tabpage_tp(tp, FALSE, FALSE);
4196
4197 tabvarname = alloc(STRLEN(varname) + 3);
4198 if (tabvarname != NULL)
4199 {
4200 STRCPY(tabvarname, "t:");
4201 STRCPY(tabvarname + 2, varname);
4202 set_var(tabvarname, varp, TRUE);
4203 vim_free(tabvarname);
4204 }
4205
4206 // Restore current tabpage
4207 if (valid_tabpage(save_curtab))
4208 goto_tabpage_tp(save_curtab, FALSE, FALSE);
4209 }
4210}
4211
4212/*
4213 * "settabwinvar()" function
4214 */
4215 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004216f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004217{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004218 if (in_vim9script()
4219 && (check_for_number_arg(argvars, 0) == FAIL
4220 || check_for_number_arg(argvars, 1) == FAIL
4221 || check_for_string_arg(argvars, 2) == FAIL))
4222 return;
4223
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004224 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004225}
4226
4227/*
4228 * "setwinvar()" function
4229 */
4230 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004231f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004232{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004233 if (in_vim9script()
4234 && (check_for_number_arg(argvars, 0) == FAIL
4235 || check_for_string_arg(argvars, 1) == FAIL))
4236 return;
4237
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004238 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004239}
4240
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004241/*
4242 * "setbufvar()" function
4243 */
4244 void
4245f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4246{
4247 buf_T *buf;
4248 char_u *varname, *bufvarname;
4249 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004250
4251 if (check_secure())
4252 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004253
4254 if (in_vim9script()
4255 && (check_for_buffer_arg(argvars, 0) == FAIL
4256 || check_for_string_arg(argvars, 1) == FAIL))
4257 return;
4258
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004259 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004260 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004261 varp = &argvars[2];
4262
4263 if (buf != NULL && varname != NULL && varp != NULL)
4264 {
4265 if (*varname == '&')
4266 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004267 aco_save_T aco;
4268
4269 // set curbuf to be our buf, temporarily
4270 aucmd_prepbuf(&aco, buf);
4271
Bram Moolenaar191929b2020-08-19 21:20:49 +02004272 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004273
4274 // reset notion of buffer
4275 aucmd_restbuf(&aco);
4276 }
4277 else
4278 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004279 bufvarname = alloc(STRLEN(varname) + 3);
4280 if (bufvarname != NULL)
4281 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004282 buf_T *save_curbuf = curbuf;
4283
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004284 curbuf = buf;
4285 STRCPY(bufvarname, "b:");
4286 STRCPY(bufvarname + 2, varname);
4287 set_var(bufvarname, varp, TRUE);
4288 vim_free(bufvarname);
4289 curbuf = save_curbuf;
4290 }
4291 }
4292 }
4293}
4294
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004295/*
4296 * Get a callback from "arg". It can be a Funcref or a function name.
4297 * When "arg" is zero return an empty string.
4298 * "cb_name" is not allocated.
4299 * "cb_name" is set to NULL for an invalid argument.
4300 */
4301 callback_T
4302get_callback(typval_T *arg)
4303{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004304 callback_T res;
4305 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004306
4307 res.cb_free_name = FALSE;
4308 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4309 {
4310 res.cb_partial = arg->vval.v_partial;
4311 ++res.cb_partial->pt_refcount;
4312 res.cb_name = partial_name(res.cb_partial);
4313 }
4314 else
4315 {
4316 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004317 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4318 && isdigit(*arg->vval.v_string))
4319 r = FAIL;
4320 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004321 {
4322 // Note that we don't make a copy of the string.
4323 res.cb_name = arg->vval.v_string;
4324 func_ref(res.cb_name);
4325 }
4326 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004327 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004328 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004329 r = FAIL;
4330
4331 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004332 {
4333 emsg(_("E921: Invalid callback argument"));
4334 res.cb_name = NULL;
4335 }
4336 }
4337 return res;
4338}
4339
4340/*
4341 * Copy a callback into a typval_T.
4342 */
4343 void
4344put_callback(callback_T *cb, typval_T *tv)
4345{
4346 if (cb->cb_partial != NULL)
4347 {
4348 tv->v_type = VAR_PARTIAL;
4349 tv->vval.v_partial = cb->cb_partial;
4350 ++tv->vval.v_partial->pt_refcount;
4351 }
4352 else
4353 {
4354 tv->v_type = VAR_FUNC;
4355 tv->vval.v_string = vim_strsave(cb->cb_name);
4356 func_ref(cb->cb_name);
4357 }
4358}
4359
4360/*
4361 * Make a copy of "src" into "dest", allocating the function name if needed,
4362 * without incrementing the refcount.
4363 */
4364 void
4365set_callback(callback_T *dest, callback_T *src)
4366{
4367 if (src->cb_partial == NULL)
4368 {
4369 // just a function name, make a copy
4370 dest->cb_name = vim_strsave(src->cb_name);
4371 dest->cb_free_name = TRUE;
4372 }
4373 else
4374 {
4375 // cb_name is a pointer into cb_partial
4376 dest->cb_name = src->cb_name;
4377 dest->cb_free_name = FALSE;
4378 }
4379 dest->cb_partial = src->cb_partial;
4380}
4381
4382/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004383 * Copy callback from "src" to "dest", incrementing the refcounts.
4384 */
4385 void
4386copy_callback(callback_T *dest, callback_T *src)
4387{
4388 dest->cb_partial = src->cb_partial;
4389 if (dest->cb_partial != NULL)
4390 {
4391 dest->cb_name = src->cb_name;
4392 dest->cb_free_name = FALSE;
4393 ++dest->cb_partial->pt_refcount;
4394 }
4395 else
4396 {
4397 dest->cb_name = vim_strsave(src->cb_name);
4398 dest->cb_free_name = TRUE;
4399 func_ref(src->cb_name);
4400 }
4401}
4402
4403/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004404 * Unref/free "callback" returned by get_callback() or set_callback().
4405 */
4406 void
4407free_callback(callback_T *callback)
4408{
4409 if (callback->cb_partial != NULL)
4410 {
4411 partial_unref(callback->cb_partial);
4412 callback->cb_partial = NULL;
4413 }
4414 else if (callback->cb_name != NULL)
4415 func_unref(callback->cb_name);
4416 if (callback->cb_free_name)
4417 {
4418 vim_free(callback->cb_name);
4419 callback->cb_free_name = FALSE;
4420 }
4421 callback->cb_name = NULL;
4422}
4423
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004424#endif // FEAT_EVAL