blob: a12b9bd23ba9379e66300368c93be6b73da1f7f2 [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
48 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
49} vimvars[VV_LEN] =
50{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020051 // The order here must match the VV_ defines in vim.h!
52 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaare5cdf152019-08-29 22:09:46 +020053 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
54 {VV_NAME("count1", VAR_NUMBER), VV_RO},
55 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
56 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
57 {VV_NAME("warningmsg", VAR_STRING), 0},
58 {VV_NAME("statusmsg", VAR_STRING), 0},
59 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
60 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
61 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
62 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
63 {VV_NAME("termresponse", VAR_STRING), VV_RO},
64 {VV_NAME("fname", VAR_STRING), VV_RO},
65 {VV_NAME("lang", VAR_STRING), VV_RO},
66 {VV_NAME("lc_time", VAR_STRING), VV_RO},
67 {VV_NAME("ctype", VAR_STRING), VV_RO},
68 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
69 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
70 {VV_NAME("fname_in", VAR_STRING), VV_RO},
71 {VV_NAME("fname_out", VAR_STRING), VV_RO},
72 {VV_NAME("fname_new", VAR_STRING), VV_RO},
73 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
74 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
75 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
76 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
77 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
78 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
79 {VV_NAME("progname", VAR_STRING), VV_RO},
80 {VV_NAME("servername", VAR_STRING), VV_RO},
81 {VV_NAME("dying", VAR_NUMBER), VV_RO},
82 {VV_NAME("exception", VAR_STRING), VV_RO},
83 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
84 {VV_NAME("register", VAR_STRING), VV_RO},
85 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
86 {VV_NAME("insertmode", VAR_STRING), VV_RO},
87 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
88 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
89 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
90 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
91 {VV_NAME("fcs_choice", VAR_STRING), 0},
92 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
93 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
94 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
95 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
96 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
97 {VV_NAME("beval_text", VAR_STRING), VV_RO},
98 {VV_NAME("scrollstart", VAR_STRING), 0},
99 {VV_NAME("swapname", VAR_STRING), VV_RO},
100 {VV_NAME("swapchoice", VAR_STRING), 0},
101 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
102 {VV_NAME("char", VAR_STRING), 0},
103 {VV_NAME("mouse_win", VAR_NUMBER), 0},
104 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
105 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
106 {VV_NAME("mouse_col", VAR_NUMBER), 0},
107 {VV_NAME("operator", VAR_STRING), VV_RO},
108 {VV_NAME("searchforward", VAR_NUMBER), 0},
109 {VV_NAME("hlsearch", VAR_NUMBER), 0},
110 {VV_NAME("oldfiles", VAR_LIST), 0},
111 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
112 {VV_NAME("progpath", VAR_STRING), VV_RO},
113 {VV_NAME("completed_item", VAR_DICT), VV_RO},
114 {VV_NAME("option_new", VAR_STRING), VV_RO},
115 {VV_NAME("option_old", VAR_STRING), VV_RO},
116 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
117 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
118 {VV_NAME("option_command", VAR_STRING), VV_RO},
119 {VV_NAME("option_type", VAR_STRING), VV_RO},
120 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100121 {VV_NAME("false", VAR_BOOL), VV_RO},
122 {VV_NAME("true", VAR_BOOL), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200123 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100124 {VV_NAME("null", VAR_SPECIAL), VV_RO},
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100125 {VV_NAME("numbermax", VAR_NUMBER), VV_RO},
126 {VV_NAME("numbermin", VAR_NUMBER), VV_RO},
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100127 {VV_NAME("numbersize", VAR_NUMBER), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200128 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
129 {VV_NAME("testing", VAR_NUMBER), 0},
130 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
131 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
132 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
133 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
134 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
135 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
136 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
137 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
138 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
139 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
140 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
141 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
142 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
143 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
144 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
145 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
146 {VV_NAME("event", VAR_DICT), VV_RO},
147 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
148 {VV_NAME("echospace", VAR_NUMBER), VV_RO},
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100149 {VV_NAME("argv", VAR_LIST), VV_RO},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200150 {VV_NAME("collate", VAR_STRING), VV_RO},
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100151 {VV_NAME("exiting", VAR_SPECIAL), VV_RO},
Drew Vogele30d1022021-10-24 20:35:07 +0100152 {VV_NAME("colornames", VAR_DICT), VV_RO},
Bram Moolenaar69b30722021-11-02 21:39:49 +0000153 {VV_NAME("sizeofint", VAR_NUMBER), VV_RO},
154 {VV_NAME("sizeoflong", VAR_NUMBER), VV_RO},
155 {VV_NAME("sizeofpointer", VAR_NUMBER), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200156};
157
158// shorthand
159#define vv_type vv_di.di_tv.v_type
160#define vv_nr vv_di.di_tv.vval.v_number
161#define vv_float vv_di.di_tv.vval.v_float
162#define vv_str vv_di.di_tv.vval.v_string
163#define vv_list vv_di.di_tv.vval.v_list
164#define vv_dict vv_di.di_tv.vval.v_dict
165#define vv_blob vv_di.di_tv.vval.v_blob
166#define vv_tv vv_di.di_tv
167
168static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200169static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200170#define vimvarht vimvardict.dv_hashtab
171
172// for VIM_VERSION_ defines
173#include "version.h"
174
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200175static void list_glob_vars(int *first);
176static void list_buf_vars(int *first);
177static void list_win_vars(int *first);
178static void list_tab_vars(int *first);
179static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100180static 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 +0200181static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
182static 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 +0200183static void list_one_var(dictitem_T *v, char *prefix, int *first);
184static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
185
186/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200187 * Initialize global and vim special variables
188 */
189 void
190evalvars_init(void)
191{
192 int i;
193 struct vimvar *p;
194
195 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
196 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
197 vimvardict.dv_lock = VAR_FIXED;
198 hash_init(&compat_hashtab);
199
200 for (i = 0; i < VV_LEN; ++i)
201 {
202 p = &vimvars[i];
203 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
204 {
205 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
206 getout(1);
207 }
208 STRCPY(p->vv_di.di_key, p->vv_name);
209 if (p->vv_flags & VV_RO)
210 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
211 else if (p->vv_flags & VV_RO_SBX)
212 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
213 else
214 p->vv_di.di_flags = DI_FLAGS_FIX;
215
216 // add to v: scope dict, unless the value is not always available
217 if (p->vv_type != VAR_UNKNOWN)
218 hash_add(&vimvarht, p->vv_di.di_key);
219 if (p->vv_flags & VV_COMPAT)
220 // add to compat scope dict
221 hash_add(&compat_hashtab, p->vv_di.di_key);
222 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200223 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
224 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200225
226 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
227 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100228 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200229 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
230 set_vim_var_list(VV_ERRORS, list_alloc());
231 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
232
233 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
234 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
235 set_vim_var_nr(VV_NONE, VVAL_NONE);
236 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100237 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
238 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100239 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaar69b30722021-11-02 21:39:49 +0000240 set_vim_var_nr(VV_SIZEOFINT, sizeof(int));
241 set_vim_var_nr(VV_SIZEOFLONG, sizeof(long));
242 set_vim_var_nr(VV_SIZEOFPOINTER, sizeof(char *));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200243
244 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
245 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
246 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
247 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
248 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
249 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
250 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
251 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
252 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
253 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
254 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
255
256 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
257
Drew Vogele30d1022021-10-24 20:35:07 +0100258 set_vim_var_dict(VV_COLORNAMES, dict_alloc());
259
Bram Moolenaar439c0362020-06-06 15:58:03 +0200260 // Default for v:register is not 0 but '"'. This is adjusted once the
261 // clipboard has been setup by calling reset_reg_var().
262 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200263}
264
265#if defined(EXITFREE) || defined(PROTO)
266/*
267 * Free all vim variables information on exit
268 */
269 void
270evalvars_clear(void)
271{
272 int i;
273 struct vimvar *p;
274
275 for (i = 0; i < VV_LEN; ++i)
276 {
277 p = &vimvars[i];
278 if (p->vv_di.di_tv.v_type == VAR_STRING)
279 VIM_CLEAR(p->vv_str);
280 else if (p->vv_di.di_tv.v_type == VAR_LIST)
281 {
282 list_unref(p->vv_list);
283 p->vv_list = NULL;
284 }
285 }
286 hash_clear(&vimvarht);
287 hash_init(&vimvarht); // garbage_collect() will access it
288 hash_clear(&compat_hashtab);
289
290 // global variables
291 vars_clear(&globvarht);
292
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100293 // Script-local variables. Clear all the variables here.
294 // The scriptvar_T is cleared later in free_scriptnames(), because a
295 // variable in one script might hold a reference to the whole scope of
296 // another script.
297 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200298 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200299}
300#endif
301
302 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200303garbage_collect_globvars(int copyID)
304{
305 return set_ref_in_ht(&globvarht, copyID, NULL);
306}
307
308 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200309garbage_collect_vimvars(int copyID)
310{
311 return set_ref_in_ht(&vimvarht, copyID, NULL);
312}
313
314 int
315garbage_collect_scriptvars(int copyID)
316{
Bram Moolenaared234f22020-10-15 20:42:20 +0200317 int i;
318 int idx;
319 int abort = FALSE;
320 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200321
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100322 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200323 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200324 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
325
Bram Moolenaared234f22020-10-15 20:42:20 +0200326 si = SCRIPT_ITEM(i);
327 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
328 {
329 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
330
Bram Moolenaard00a7fb2021-03-08 20:47:14 +0100331 if (sv->sv_name != NULL)
332 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
Bram Moolenaared234f22020-10-15 20:42:20 +0200333 }
334 }
335
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200336 return abort;
337}
338
339/*
340 * Set an internal variable to a string value. Creates the variable if it does
341 * not already exist.
342 */
343 void
344set_internal_string_var(char_u *name, char_u *value)
345{
346 char_u *val;
347 typval_T *tvp;
348
349 val = vim_strsave(value);
350 if (val != NULL)
351 {
352 tvp = alloc_string_tv(val);
353 if (tvp != NULL)
354 {
355 set_var(name, tvp, FALSE);
356 free_tv(tvp);
357 }
358 }
359}
360
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200361 int
362eval_charconvert(
363 char_u *enc_from,
364 char_u *enc_to,
365 char_u *fname_from,
366 char_u *fname_to)
367{
368 int err = FALSE;
369
370 set_vim_var_string(VV_CC_FROM, enc_from, -1);
371 set_vim_var_string(VV_CC_TO, enc_to, -1);
372 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
373 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
374 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
375 err = TRUE;
376 set_vim_var_string(VV_CC_FROM, NULL, -1);
377 set_vim_var_string(VV_CC_TO, NULL, -1);
378 set_vim_var_string(VV_FNAME_IN, NULL, -1);
379 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
380
381 if (err)
382 return FAIL;
383 return OK;
384}
385
386# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
387 int
388eval_printexpr(char_u *fname, char_u *args)
389{
390 int err = FALSE;
391
392 set_vim_var_string(VV_FNAME_IN, fname, -1);
393 set_vim_var_string(VV_CMDARG, args, -1);
394 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
395 err = TRUE;
396 set_vim_var_string(VV_FNAME_IN, NULL, -1);
397 set_vim_var_string(VV_CMDARG, NULL, -1);
398
399 if (err)
400 {
401 mch_remove(fname);
402 return FAIL;
403 }
404 return OK;
405}
406# endif
407
408# if defined(FEAT_DIFF) || defined(PROTO)
409 void
410eval_diff(
411 char_u *origfile,
412 char_u *newfile,
413 char_u *outfile)
414{
415 int err = FALSE;
416
417 set_vim_var_string(VV_FNAME_IN, origfile, -1);
418 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
419 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
420 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
421 set_vim_var_string(VV_FNAME_IN, NULL, -1);
422 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
423 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
424}
425
426 void
427eval_patch(
428 char_u *origfile,
429 char_u *difffile,
430 char_u *outfile)
431{
432 int err;
433
434 set_vim_var_string(VV_FNAME_IN, origfile, -1);
435 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
436 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
437 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
438 set_vim_var_string(VV_FNAME_IN, NULL, -1);
439 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
440 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
441}
442# endif
443
444#if defined(FEAT_SPELL) || defined(PROTO)
445/*
446 * Evaluate an expression to a list with suggestions.
447 * For the "expr:" part of 'spellsuggest'.
448 * Returns NULL when there is an error.
449 */
450 list_T *
451eval_spell_expr(char_u *badword, char_u *expr)
452{
453 typval_T save_val;
454 typval_T rettv;
455 list_T *list = NULL;
456 char_u *p = skipwhite(expr);
457
458 // Set "v:val" to the bad word.
459 prepare_vimvar(VV_VAL, &save_val);
460 set_vim_var_string(VV_VAL, badword, -1);
461 if (p_verbose == 0)
462 ++emsg_off;
463
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200464 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200465 {
466 if (rettv.v_type != VAR_LIST)
467 clear_tv(&rettv);
468 else
469 list = rettv.vval.v_list;
470 }
471
472 if (p_verbose == 0)
473 --emsg_off;
474 clear_tv(get_vim_var_tv(VV_VAL));
475 restore_vimvar(VV_VAL, &save_val);
476
477 return list;
478}
479
480/*
481 * "list" is supposed to contain two items: a word and a number. Return the
482 * word in "pp" and the number as the return value.
483 * Return -1 if anything isn't right.
484 * Used to get the good word and score from the eval_spell_expr() result.
485 */
486 int
487get_spellword(list_T *list, char_u **pp)
488{
489 listitem_T *li;
490
491 li = list->lv_first;
492 if (li == NULL)
493 return -1;
494 *pp = tv_get_string(&li->li_tv);
495
496 li = li->li_next;
497 if (li == NULL)
498 return -1;
499 return (int)tv_get_number(&li->li_tv);
500}
501#endif
502
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200503/*
504 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200505 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200506 * When not used yet add the variable to the v: hashtable.
507 */
508 void
509prepare_vimvar(int idx, typval_T *save_tv)
510{
511 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200512 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200513 if (vimvars[idx].vv_type == VAR_UNKNOWN)
514 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
515}
516
517/*
518 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200519 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200520 * When no longer defined, remove the variable from the v: hashtable.
521 */
522 void
523restore_vimvar(int idx, typval_T *save_tv)
524{
525 hashitem_T *hi;
526
527 vimvars[idx].vv_tv = *save_tv;
528 if (vimvars[idx].vv_type == VAR_UNKNOWN)
529 {
530 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
531 if (HASHITEM_EMPTY(hi))
532 internal_error("restore_vimvar()");
533 else
534 hash_remove(&vimvarht, hi);
535 }
536}
537
538/*
539 * List Vim variables.
540 */
541 static void
542list_vim_vars(int *first)
543{
544 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
545}
546
547/*
548 * List script-local variables, if there is a script.
549 */
550 static void
551list_script_vars(int *first)
552{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200553 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200554 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
555 "s:", FALSE, first);
556}
557
558/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200559 * Get a list of lines from a HERE document. The here document is a list of
560 * lines surrounded by a marker.
561 * cmd << {marker}
562 * {line1}
563 * {line2}
564 * ....
565 * {marker}
566 *
567 * The {marker} is a string. If the optional 'trim' word is supplied before the
568 * marker, then the leading indentation before the lines (matching the
569 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200570 *
571 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
572 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
573 * missing, then '.' is accepted as a marker.
574 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200575 * Returns a List with {lines} or NULL.
576 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100577 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200578heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200579{
580 char_u *theline;
581 char_u *marker;
582 list_T *l;
583 char_u *p;
584 int marker_indent_len = 0;
585 int text_indent_len = 0;
586 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200587 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200588 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200589
590 if (eap->getline == NULL)
591 {
592 emsg(_("E991: cannot use =<< here"));
593 return NULL;
594 }
595
596 // Check for the optional 'trim' word before the marker
597 cmd = skipwhite(cmd);
598 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
599 {
600 cmd = skipwhite(cmd + 4);
601
602 // Trim the indentation from all the lines in the here document.
603 // The amount of indentation trimmed is the same as the indentation of
604 // the first line after the :let command line. To find the end marker
605 // the indent of the :let command line is trimmed.
606 p = *eap->cmdlinep;
607 while (VIM_ISWHITE(*p))
608 {
609 p++;
610 marker_indent_len++;
611 }
612 text_indent_len = -1;
613 }
614
615 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200616 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200617 {
618 marker = skipwhite(cmd);
619 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200620 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200621 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200622 semsg(_(e_trailing_arg), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200623 return NULL;
624 }
625 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200626 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200627 {
628 emsg(_("E221: Marker cannot start with lower case letter"));
629 return NULL;
630 }
631 }
632 else
633 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200634 // When getting lines for an embedded script, if the marker is missing,
635 // accept '.' as the marker.
636 if (script_get)
637 marker = dot;
638 else
639 {
640 emsg(_("E172: Missing marker"));
641 return NULL;
642 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200643 }
644
645 l = list_alloc();
646 if (l == NULL)
647 return NULL;
648
649 for (;;)
650 {
651 int mi = 0;
652 int ti = 0;
653
654 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
655 if (theline == NULL)
656 {
657 semsg(_("E990: Missing end marker '%s'"), marker);
658 break;
659 }
660
661 // with "trim": skip the indent matching the :let line to find the
662 // marker
663 if (marker_indent_len > 0
664 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
665 mi = marker_indent_len;
666 if (STRCMP(marker, theline + mi) == 0)
667 {
668 vim_free(theline);
669 break;
670 }
671
672 if (text_indent_len == -1 && *theline != NUL)
673 {
674 // set the text indent from the first line.
675 p = theline;
676 text_indent_len = 0;
677 while (VIM_ISWHITE(*p))
678 {
679 p++;
680 text_indent_len++;
681 }
682 text_indent = vim_strnsave(theline, text_indent_len);
683 }
684 // with "trim": skip the indent matching the first line
685 if (text_indent != NULL)
686 for (ti = 0; ti < text_indent_len; ++ti)
687 if (theline[ti] != text_indent[ti])
688 break;
689
690 if (list_append_string(l, theline + ti, -1) == FAIL)
691 break;
692 vim_free(theline);
693 }
694 vim_free(text_indent);
695
696 return l;
697}
698
699/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200700 * Vim9 variable declaration:
701 * ":var name"
702 * ":var name: type"
703 * ":var name = expr"
704 * ":var name: type = expr"
705 * etc.
706 */
707 void
708ex_var(exarg_T *eap)
709{
710 if (!in_vim9script())
711 {
712 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
713 return;
714 }
715 ex_let(eap);
716}
717
718/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200719 * ":let" list all variable values
720 * ":let var1 var2" list variable values
721 * ":let var = expr" assignment command.
722 * ":let var += expr" assignment command.
723 * ":let var -= expr" assignment command.
724 * ":let var *= expr" assignment command.
725 * ":let var /= expr" assignment command.
726 * ":let var %= expr" assignment command.
727 * ":let var .= expr" assignment command.
728 * ":let var ..= expr" assignment command.
729 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100730 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100731 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200732 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200733 * ":final var = expr" assignment command.
734 * ":final [var1, var2] = expr" unpack list.
735 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200736 * ":const" list all variable values
737 * ":const var1 var2" list variable values
738 * ":const var = expr" assignment command.
739 * ":const [var1, var2] = expr" unpack list.
740 */
741 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200742ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200743{
744 char_u *arg = eap->arg;
745 char_u *expr = NULL;
746 typval_T rettv;
747 int i;
748 int var_count = 0;
749 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200750 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200751 char_u *argend;
752 int first = TRUE;
753 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200754 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100755 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200756 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200757
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200758 if (eap->cmdidx == CMD_final && !vim9script)
759 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100760 // In legacy Vim script ":final" is short for ":finally".
761 ex_finally(eap);
762 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200763 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200764 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200765 {
766 emsg(_(e_cannot_use_let_in_vim9_script));
767 return;
768 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200769
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100770 if (eap->cmdidx == CMD_const)
771 flags |= ASSIGN_CONST;
772 else if (eap->cmdidx == CMD_final)
773 flags |= ASSIGN_FINAL;
774
775 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200777 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100778
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200779 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200780 if (argend == NULL)
781 return;
782 if (argend > arg && argend[-1] == '.') // for var.='str'
783 --argend;
784 expr = skipwhite(argend);
785 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +0200786 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200787 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200788 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
789 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200790 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200791 {
792 // ":let" without "=": list variables
793 if (*arg == '[')
794 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200795 else if (expr[0] == '.' && expr[1] == '=')
796 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200797 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200798 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200799 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200800 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100801 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
802 semsg(_(e_trailing_arg), argend);
803 else
804 // Vim9 declaration ":var name: type"
805 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200806 }
807 else
808 {
809 // ":let var1 var2" - list values
810 arg = list_arg_vars(eap, arg, &first);
811 }
812 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200813 else if (!eap->skip)
814 {
815 // ":let"
816 list_glob_vars(&first);
817 list_buf_vars(&first);
818 list_win_vars(&first);
819 list_tab_vars(&first);
820 list_script_vars(&first);
821 list_func_vars(&first);
822 list_vim_vars(&first);
823 }
Bram Moolenaar63b91732021-08-05 20:40:03 +0200824 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200825 }
826 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
827 {
828 list_T *l;
Bram Moolenaar81530e32021-07-28 21:25:49 +0200829 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200830
831 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200832 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200833 if (l != NULL)
834 {
835 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200836 if (!eap->skip)
837 {
Bram Moolenaar81530e32021-07-28 21:25:49 +0200838 // errors are for the assignment, not the end marker
839 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200840 op[0] = '=';
841 op[1] = NUL;
842 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200844 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200845 clear_tv(&rettv);
846 }
847 }
848 else
849 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200850 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200851 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200852
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200853 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200854 i = FAIL;
855 if (has_assign || concat)
856 {
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100857 int cur_lnum;
858
Bram Moolenaar32e35112020-05-14 22:41:15 +0200859 op[0] = '=';
860 op[1] = NUL;
861 if (*expr != '=')
862 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200863 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200864 {
865 // +=, /=, etc. require an existing variable
866 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
867 i = FAIL;
868 }
869 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200870 {
871 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200872 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200873 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200874 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200875 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200876 ++len;
877 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200878 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200879 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200880 }
881 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200882 ++expr;
883
Bram Moolenaar7f2c3412021-11-29 16:01:49 +0000884 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200885 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200886 {
887 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100888 semsg(_(e_white_space_required_before_and_after_str_at_str),
889 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200890 i = FAIL;
891 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200892
893 if (eap->skip)
894 ++emsg_skip;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200895 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200896 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100897 cur_lnum = SOURCING_LNUM;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200898 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200899 if (eap->skip)
900 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200901 clear_evalarg(&evalarg, eap);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100902
903 // Restore the line number so that any type error is given for the
904 // declaration, not the expression.
905 SOURCING_LNUM = cur_lnum;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200906 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200907 if (eap->skip)
908 {
909 if (i != FAIL)
910 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200911 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200912 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200913 {
914 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200915 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200916 clear_tv(&rettv);
917 }
918 }
919}
920
921/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +0100922 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200923 * Handles both "var" with any type and "[var, var; var]" with a list type.
924 * When "op" is not NULL it points to a string with characters that
925 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
926 * or concatenate.
927 * Returns OK or FAIL;
928 */
929 int
930ex_let_vars(
931 char_u *arg_start,
932 typval_T *tv,
933 int copy, // copy values from "tv", don't move
934 int semicolon, // from skip_var_list()
935 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100936 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200937 char_u *op)
938{
939 char_u *arg = arg_start;
940 list_T *l;
941 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100942 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200943 listitem_T *item;
944 typval_T ltv;
945
946 if (*arg != '[')
947 {
948 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100949 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200950 return FAIL;
951 return OK;
952 }
953
954 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
955 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
956 {
957 emsg(_(e_listreq));
958 return FAIL;
959 }
960
961 i = list_len(l);
962 if (semicolon == 0 && var_count < i)
963 {
964 emsg(_("E687: Less targets than List items"));
965 return FAIL;
966 }
967 if (var_count - semicolon > i)
968 {
969 emsg(_("E688: More targets than List items"));
970 return FAIL;
971 }
972
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200973 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200974 item = l->lv_first;
975 while (*arg != ']')
976 {
977 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100978 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +0200979 arg = ex_let_one(arg, &item->li_tv, TRUE,
980 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200981 item = item->li_next;
982 if (arg == NULL)
983 return FAIL;
984
985 arg = skipwhite(arg);
986 if (*arg == ';')
987 {
988 // Put the rest of the list (may be empty) in the var after ';'.
989 // Create a new list for this.
990 l = list_alloc();
991 if (l == NULL)
992 return FAIL;
993 while (item != NULL)
994 {
995 list_append_tv(l, &item->li_tv);
996 item = item->li_next;
997 }
998
999 ltv.v_type = VAR_LIST;
1000 ltv.v_lock = 0;
1001 ltv.vval.v_list = l;
1002 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001003 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001004
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001005 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1006 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001007 clear_tv(&ltv);
1008 if (arg == NULL)
1009 return FAIL;
1010 break;
1011 }
1012 else if (*arg != ',' && *arg != ']')
1013 {
1014 internal_error("ex_let_vars()");
1015 return FAIL;
1016 }
1017 }
1018
1019 return OK;
1020}
1021
1022/*
1023 * Skip over assignable variable "var" or list of variables "[var, var]".
1024 * Used for ":let varvar = expr" and ":for varvar in expr".
1025 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001026 * for "[var, var; var]" set "semicolon" to 1.
1027 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001028 * Return NULL for an error.
1029 */
1030 char_u *
1031skip_var_list(
1032 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001034 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001035 int *semicolon,
1036 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001037{
1038 char_u *p, *s;
1039
1040 if (*arg == '[')
1041 {
1042 // "[var, var]": find the matching ']'.
1043 p = arg;
1044 for (;;)
1045 {
1046 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001047 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001048 if (s == p)
1049 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001050 if (!silent)
1051 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001052 return NULL;
1053 }
1054 ++*var_count;
1055
1056 p = skipwhite(s);
1057 if (*p == ']')
1058 break;
1059 else if (*p == ';')
1060 {
1061 if (*semicolon == 1)
1062 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001063 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001064 return NULL;
1065 }
1066 *semicolon = 1;
1067 }
1068 else if (*p != ',')
1069 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001070 if (!silent)
1071 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001072 return NULL;
1073 }
1074 }
1075 return p + 1;
1076 }
1077 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001078 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001079}
1080
1081/*
1082 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1083 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001085 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001086 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001088{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001089 char_u *end;
1090 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001091
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001092 if (*arg == '@' && arg[1] != NUL)
1093 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001095 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001096
1097 // "a: type" is declaring variable "a" with a type, not "a:".
1098 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001099 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001100 --end;
1101
Bram Moolenaar585587d2021-01-17 20:52:13 +01001102 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001103 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001104 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001105 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001106 }
1107 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001108}
1109
1110/*
1111 * List variables for hashtab "ht" with prefix "prefix".
1112 * If "empty" is TRUE also list NULL strings as empty strings.
1113 */
1114 void
1115list_hashtable_vars(
1116 hashtab_T *ht,
1117 char *prefix,
1118 int empty,
1119 int *first)
1120{
1121 hashitem_T *hi;
1122 dictitem_T *di;
1123 int todo;
1124 char_u buf[IOSIZE];
1125
1126 todo = (int)ht->ht_used;
1127 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1128 {
1129 if (!HASHITEM_EMPTY(hi))
1130 {
1131 --todo;
1132 di = HI2DI(hi);
1133
1134 // apply :filter /pat/ to variable name
1135 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1136 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1137 if (message_filtered(buf))
1138 continue;
1139
1140 if (empty || di->di_tv.v_type != VAR_STRING
1141 || di->di_tv.vval.v_string != NULL)
1142 list_one_var(di, prefix, first);
1143 }
1144 }
1145}
1146
1147/*
1148 * List global variables.
1149 */
1150 static void
1151list_glob_vars(int *first)
1152{
1153 list_hashtable_vars(&globvarht, "", TRUE, first);
1154}
1155
1156/*
1157 * List buffer variables.
1158 */
1159 static void
1160list_buf_vars(int *first)
1161{
1162 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1163}
1164
1165/*
1166 * List window variables.
1167 */
1168 static void
1169list_win_vars(int *first)
1170{
1171 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1172}
1173
1174/*
1175 * List tab page variables.
1176 */
1177 static void
1178list_tab_vars(int *first)
1179{
1180 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1181}
1182
1183/*
1184 * List variables in "arg".
1185 */
1186 static char_u *
1187list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1188{
1189 int error = FALSE;
1190 int len;
1191 char_u *name;
1192 char_u *name_start;
1193 char_u *arg_subsc;
1194 char_u *tofree;
1195 typval_T tv;
1196
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001197 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001198 {
1199 if (error || eap->skip)
1200 {
1201 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1202 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1203 {
1204 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001205 if (!did_emsg)
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001206 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001207 break;
1208 }
1209 }
1210 else
1211 {
1212 // get_name_len() takes care of expanding curly braces
1213 name_start = name = arg;
1214 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1215 if (len <= 0)
1216 {
1217 // This is mainly to keep test 49 working: when expanding
1218 // curly braces fails overrule the exception error message.
1219 if (len < 0 && !aborting())
1220 {
1221 emsg_severe = TRUE;
1222 semsg(_(e_invarg2), arg);
1223 break;
1224 }
1225 error = TRUE;
1226 }
1227 else
1228 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001229 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001230 if (tofree != NULL)
1231 name = tofree;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001232 if (eval_variable(name, len, &tv, NULL,
1233 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001234 error = TRUE;
1235 else
1236 {
1237 // handle d.key, l[idx], f(expr)
1238 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001239 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001240 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001241 error = TRUE;
1242 else
1243 {
1244 if (arg == arg_subsc && len == 2 && name[1] == ':')
1245 {
1246 switch (*name)
1247 {
1248 case 'g': list_glob_vars(first); break;
1249 case 'b': list_buf_vars(first); break;
1250 case 'w': list_win_vars(first); break;
1251 case 't': list_tab_vars(first); break;
1252 case 'v': list_vim_vars(first); break;
1253 case 's': list_script_vars(first); break;
1254 case 'l': list_func_vars(first); break;
1255 default:
1256 semsg(_("E738: Can't list variables for %s"), name);
1257 }
1258 }
1259 else
1260 {
1261 char_u numbuf[NUMBUFLEN];
1262 char_u *tf;
1263 int c;
1264 char_u *s;
1265
1266 s = echo_string(&tv, &tf, numbuf, 0);
1267 c = *arg;
1268 *arg = NUL;
1269 list_one_var_a("",
1270 arg == arg_subsc ? name : name_start,
1271 tv.v_type,
1272 s == NULL ? (char_u *)"" : s,
1273 first);
1274 *arg = c;
1275 vim_free(tf);
1276 }
1277 clear_tv(&tv);
1278 }
1279 }
1280 }
1281
1282 vim_free(tofree);
1283 }
1284
1285 arg = skipwhite(arg);
1286 }
1287
1288 return arg;
1289}
1290
1291/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001292 * Set an environment variable, part of ex_let_one().
1293 */
1294 static char_u *
1295ex_let_env(
1296 char_u *arg,
1297 typval_T *tv,
1298 int flags,
1299 char_u *endchars,
1300 char_u *op)
1301{
1302 char_u *arg_end = NULL;
1303 char_u *name;
1304 int len;
1305
1306 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1307 && (flags & ASSIGN_FOR_LOOP) == 0)
1308 {
1309 emsg(_("E996: Cannot lock an environment variable"));
1310 return NULL;
1311 }
1312
1313 // Find the end of the name.
1314 ++arg;
1315 name = arg;
1316 len = get_env_len(&arg);
1317 if (len == 0)
1318 semsg(_(e_invarg2), name - 1);
1319 else
1320 {
1321 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1322 semsg(_(e_letwrong), op);
1323 else if (endchars != NULL
1324 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1325 emsg(_(e_unexpected_characters_in_let));
1326 else if (!check_secure())
1327 {
1328 char_u *tofree = NULL;
1329 int c1 = name[len];
1330 char_u *p;
1331
1332 name[len] = NUL;
1333 p = tv_get_string_chk(tv);
1334 if (p != NULL && op != NULL && *op == '.')
1335 {
1336 int mustfree = FALSE;
1337 char_u *s = vim_getenv(name, &mustfree);
1338
1339 if (s != NULL)
1340 {
1341 p = tofree = concat_str(s, p);
1342 if (mustfree)
1343 vim_free(s);
1344 }
1345 }
1346 if (p != NULL)
1347 {
1348 vim_setenv_ext(name, p);
1349 arg_end = arg;
1350 }
1351 name[len] = c1;
1352 vim_free(tofree);
1353 }
1354 }
1355 return arg_end;
1356}
1357
1358/*
1359 * Set an option, part of ex_let_one().
1360 */
1361 static char_u *
1362ex_let_option(
1363 char_u *arg,
1364 typval_T *tv,
1365 int flags,
1366 char_u *endchars,
1367 char_u *op)
1368{
1369 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001370 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001371 char_u *arg_end = NULL;
1372
1373 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1374 && (flags & ASSIGN_FOR_LOOP) == 0)
1375 {
1376 emsg(_(e_const_option));
1377 return NULL;
1378 }
1379
1380 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001381 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001382 if (p == NULL || (endchars != NULL
1383 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1384 emsg(_(e_unexpected_characters_in_let));
1385 else
1386 {
1387 int c1;
1388 long n = 0;
1389 getoption_T opt_type;
1390 long numval;
1391 char_u *stringval = NULL;
1392 char_u *s = NULL;
1393 int failed = FALSE;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001394 int opt_p_flags;
1395 char_u *tofree = NULL;
Bram Moolenaar92c33eb2021-12-07 11:03:39 +00001396 char_u numbuf[NUMBUFLEN];
1397
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001398 c1 = *p;
1399 *p = NUL;
1400
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001401 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags,
1402 scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001403 if ((opt_type == gov_bool
1404 || opt_type == gov_number
1405 || opt_type == gov_hidden_bool
1406 || opt_type == gov_hidden_number)
1407 && (tv->v_type != VAR_STRING || !in_vim9script()))
1408 {
1409 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1410 // bool, possibly hidden
1411 n = (long)tv_get_bool(tv);
1412 else
1413 // number, possibly hidden
1414 n = (long)tv_get_number(tv);
1415 }
1416
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001417 if (opt_p_flags & P_FUNC && (tv->v_type == VAR_PARTIAL
1418 || tv->v_type == VAR_FUNC))
1419 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001420 // If the option can be set to a function reference or a lambda
1421 // and the passed value is a function reference, then convert it to
1422 // the name (string) of the function reference.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001423 s = tv2string(tv, &tofree, numbuf, 0);
1424 }
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001425 // Avoid setting a string option to the text "v:false" or similar.
1426 // In Vim9 script also don't convert a number to string.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001427 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001428 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1429 s = tv_get_string_chk(tv);
1430
1431 if (op != NULL && *op != '=')
1432 {
1433 if (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1434 || (opt_type == gov_string && *op != '.'))
1435 {
1436 semsg(_(e_letwrong), op);
1437 failed = TRUE; // don't set the value
1438
1439 }
1440 else
1441 {
1442 // number, in legacy script also bool
1443 if (opt_type == gov_number
1444 || (opt_type == gov_bool && !in_vim9script()))
1445 {
1446 switch (*op)
1447 {
1448 case '+': n = numval + n; break;
1449 case '-': n = numval - n; break;
1450 case '*': n = numval * n; break;
1451 case '/': n = (long)num_divide(numval, n,
1452 &failed); break;
1453 case '%': n = (long)num_modulus(numval, n,
1454 &failed); break;
1455 }
1456 s = NULL;
1457 }
1458 else if (opt_type == gov_string
1459 && stringval != NULL && s != NULL)
1460 {
1461 // string
1462 s = concat_str(stringval, s);
1463 vim_free(stringval);
1464 stringval = s;
1465 }
1466 }
1467 }
1468
1469 if (!failed)
1470 {
1471 if (opt_type != gov_string || s != NULL)
1472 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001473 set_option_value(arg, n, s, scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001474 arg_end = p;
1475 }
1476 else
1477 emsg(_(e_stringreq));
1478 }
1479 *p = c1;
1480 vim_free(stringval);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001481 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001482 }
1483 return arg_end;
1484}
1485
1486/*
1487 * Set a register, part of ex_let_one().
1488 */
1489 static char_u *
1490ex_let_register(
1491 char_u *arg,
1492 typval_T *tv,
1493 int flags,
1494 char_u *endchars,
1495 char_u *op)
1496{
1497 char_u *arg_end = NULL;
1498
1499 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1500 && (flags & ASSIGN_FOR_LOOP) == 0)
1501 {
1502 emsg(_("E996: Cannot lock a register"));
1503 return NULL;
1504 }
1505 ++arg;
1506 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1507 semsg(_(e_letwrong), op);
1508 else if (endchars != NULL
1509 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1510 emsg(_(e_unexpected_characters_in_let));
1511 else
1512 {
1513 char_u *ptofree = NULL;
1514 char_u *p;
1515
1516 p = tv_get_string_chk(tv);
1517 if (p != NULL && op != NULL && *op == '.')
1518 {
1519 char_u *s = get_reg_contents(*arg == '@'
1520 ? '"' : *arg, GREG_EXPR_SRC);
1521
1522 if (s != NULL)
1523 {
1524 p = ptofree = concat_str(s, p);
1525 vim_free(s);
1526 }
1527 }
1528 if (p != NULL)
1529 {
1530 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1531 arg_end = arg + 1;
1532 }
1533 vim_free(ptofree);
1534 }
1535 return arg_end;
1536}
1537
1538/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001539 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1540 * Returns a pointer to the char just after the var name.
1541 * Returns NULL if there is an error.
1542 */
1543 static char_u *
1544ex_let_one(
1545 char_u *arg, // points to variable name
1546 typval_T *tv, // value to assign to variable
1547 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001548 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001549 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001550 char_u *op, // "+", "-", "." or NULL
1551 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001552{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001553 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001554
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001555 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001556 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001557 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1558 {
1559 vim9_declare_error(arg);
1560 return NULL;
1561 }
1562
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001563 if (*arg == '$')
1564 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001565 // ":let $VAR = expr": Set environment variable.
1566 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001567 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001568 else if (*arg == '&')
1569 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001570 // ":let &option = expr": Set option value.
1571 // ":let &l:option = expr": Set local option value.
1572 // ":let &g:option = expr": Set global option value.
1573 // ":for &ts in range(8)": Set option value for for loop
1574 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001575 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001576 else if (*arg == '@')
1577 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001578 // ":let @r = expr": Set register contents.
1579 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001580 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001581 else if (eval_isnamec1(*arg) || *arg == '{')
1582 {
1583 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001584 char_u *p;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001585
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001586 // ":let var = expr": Set internal variable.
1587 // ":let var: type = expr": Set internal variable with type.
1588 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001589 p = get_lval(arg, tv, &lv, FALSE, FALSE,
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001590 (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1591 ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001592 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001593 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 if (endchars != NULL && vim_strchr(endchars,
1595 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001596 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001597 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001598 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001599 else
1600 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001601 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001602 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001603 }
1604 }
1605 clear_lval(&lv);
1606 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001607 else
1608 semsg(_(e_invarg2), arg);
1609
1610 return arg_end;
1611}
1612
1613/*
1614 * ":unlet[!] var1 ... " command.
1615 */
1616 void
1617ex_unlet(exarg_T *eap)
1618{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001619 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001620}
1621
1622/*
1623 * ":lockvar" and ":unlockvar" commands
1624 */
1625 void
1626ex_lockvar(exarg_T *eap)
1627{
1628 char_u *arg = eap->arg;
1629 int deep = 2;
1630
1631 if (eap->forceit)
1632 deep = -1;
1633 else if (vim_isdigit(*arg))
1634 {
1635 deep = getdigits(&arg);
1636 arg = skipwhite(arg);
1637 }
1638
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001639 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001640}
1641
1642/*
1643 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001644 * Also used for Vim9 script. "callback" is invoked as:
1645 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001646 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001647 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001648ex_unletlock(
1649 exarg_T *eap,
1650 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001651 int deep,
1652 int glv_flags,
1653 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1654 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001655{
1656 char_u *arg = argstart;
1657 char_u *name_end;
1658 int error = FALSE;
1659 lval_T lv;
1660
1661 do
1662 {
1663 if (*arg == '$')
1664 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001665 lv.ll_name = arg;
1666 lv.ll_tv = NULL;
1667 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001668 if (get_env_len(&arg) == 0)
1669 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001670 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001671 return;
1672 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001673 if (!error && !eap->skip
1674 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1675 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001676 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001677 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001678 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001679 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001680 // Parse the name and find the end.
1681 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001682 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001683 if (lv.ll_name == NULL)
1684 error = TRUE; // error but continue parsing
1685 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1686 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001687 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001688 if (name_end != NULL)
1689 {
1690 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001691 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001692 }
1693 if (!(eap->skip || error))
1694 clear_lval(&lv);
1695 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001696 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001697
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001698 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001699 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001700 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001701
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001702 if (!eap->skip)
1703 clear_lval(&lv);
1704 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001705
1706 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001707 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001708
Bram Moolenaar63b91732021-08-05 20:40:03 +02001709 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001710}
1711
1712 static int
1713do_unlet_var(
1714 lval_T *lp,
1715 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001716 exarg_T *eap,
1717 int deep UNUSED,
1718 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001719{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001720 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001721 int ret = OK;
1722 int cc;
1723
1724 if (lp->ll_tv == NULL)
1725 {
1726 cc = *name_end;
1727 *name_end = NUL;
1728
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001729 // Environment variable, normal name or expanded name.
1730 if (*lp->ll_name == '$')
1731 vim_unsetenv(lp->ll_name + 1);
1732 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001733 ret = FAIL;
1734 *name_end = cc;
1735 }
1736 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001737 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001738 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001739 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001740 return FAIL;
1741 else if (lp->ll_range)
1742 {
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001743 if (list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_name, lp->ll_n1,
1744 !lp->ll_empty2, lp->ll_n2) == FAIL)
1745 return FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001746 }
1747 else
1748 {
1749 if (lp->ll_list != NULL)
1750 // unlet a List item.
1751 listitem_remove(lp->ll_list, lp->ll_li);
1752 else
1753 // unlet a Dictionary item.
1754 dictitem_remove(lp->ll_dict, lp->ll_di);
1755 }
1756
1757 return ret;
1758}
1759
1760/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001761 * Unlet one item or a range of items from a list.
1762 * Return OK or FAIL.
1763 */
1764 int
1765list_unlet_range(
1766 list_T *l,
1767 listitem_T *li_first,
1768 char_u *name,
1769 long n1_arg,
1770 int has_n2,
1771 long n2)
1772{
1773 listitem_T *li = li_first;
1774 int n1 = n1_arg;
1775
1776 while (li != NULL && (!has_n2 || n2 >= n1))
1777 {
1778 if (value_check_lock(li->li_tv.v_lock, name, FALSE))
1779 return FAIL;
1780 li = li->li_next;
1781 ++n1;
1782 }
1783
1784 // Delete a range of List items.
1785 li = li_first;
1786 n1 = n1_arg;
1787 while (li != NULL && (!has_n2 || n2 >= n1))
1788 {
1789 listitem_T *next = li->li_next;
1790
1791 listitem_remove(l, li);
1792 li = next;
1793 ++n1;
1794 }
1795 return OK;
1796}
1797/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001798 * "unlet" a variable. Return OK if it existed, FAIL if not.
1799 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1800 */
1801 int
1802do_unlet(char_u *name, int forceit)
1803{
1804 hashtab_T *ht;
1805 hashitem_T *hi;
1806 char_u *varname;
1807 dict_T *d;
1808 dictitem_T *di;
1809
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001810 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001811 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001812 return FAIL;
1813
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001814 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001815
1816 // can't :unlet a script variable in Vim9 script from a function
1817 if (ht == get_script_local_ht()
1818 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1819 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1820 == SCRIPT_VERSION_VIM9
1821 && check_vim9_unlet(name) == FAIL)
1822 return FAIL;
1823
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001824 if (ht != NULL && *varname != NUL)
1825 {
1826 d = get_current_funccal_dict(ht);
1827 if (d == NULL)
1828 {
1829 if (ht == &globvarht)
1830 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001831 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001832 d = &vimvardict;
1833 else
1834 {
1835 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1836 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1837 }
1838 if (d == NULL)
1839 {
1840 internal_error("do_unlet()");
1841 return FAIL;
1842 }
1843 }
1844 hi = hash_find(ht, varname);
1845 if (HASHITEM_EMPTY(hi))
1846 hi = find_hi_in_scoped_ht(name, &ht);
1847 if (hi != NULL && !HASHITEM_EMPTY(hi))
1848 {
1849 di = HI2DI(hi);
1850 if (var_check_fixed(di->di_flags, name, FALSE)
1851 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001852 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001853 return FAIL;
1854
1855 delete_var(ht, hi);
1856 return OK;
1857 }
1858 }
1859 if (forceit)
1860 return OK;
1861 semsg(_("E108: No such variable: \"%s\""), name);
1862 return FAIL;
1863}
1864
1865/*
1866 * Lock or unlock variable indicated by "lp".
1867 * "deep" is the levels to go (-1 for unlimited);
1868 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1869 */
1870 static int
1871do_lock_var(
1872 lval_T *lp,
1873 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001874 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001875 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001876 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001877{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001878 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001879 int ret = OK;
1880 int cc;
1881 dictitem_T *di;
1882
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001883 if (lp->ll_tv == NULL)
1884 {
1885 cc = *name_end;
1886 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001887 if (*lp->ll_name == '$')
1888 {
1889 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001890 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001891 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001892 else
1893 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001894 // Normal name or expanded name.
1895 di = find_var(lp->ll_name, NULL, TRUE);
1896 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001897 {
1898 if (in_vim9script())
1899 semsg(_(e_cannot_find_variable_to_unlock_str),
1900 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001901 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001902 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001903 else if ((di->di_flags & DI_FLAGS_FIX)
1904 && di->di_tv.v_type != VAR_DICT
1905 && di->di_tv.v_type != VAR_LIST)
1906 {
1907 // For historic reasons this error is not given for a list or
1908 // dict. E.g., the b: dict could be locked/unlocked.
1909 semsg(_(e_lock_unlock), lp->ll_name);
1910 ret = FAIL;
1911 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001912 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001913 {
1914 if (lock)
1915 di->di_flags |= DI_FLAGS_LOCK;
1916 else
1917 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001918 if (deep != 0)
1919 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001920 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001921 }
1922 *name_end = cc;
1923 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001924 else if (deep == 0)
1925 {
1926 // nothing to do
1927 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001928 else if (lp->ll_range)
1929 {
1930 listitem_T *li = lp->ll_li;
1931
1932 // (un)lock a range of List items.
1933 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1934 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001935 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001936 li = li->li_next;
1937 ++lp->ll_n1;
1938 }
1939 }
1940 else if (lp->ll_list != NULL)
1941 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001942 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001943 else
1944 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001945 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001946
1947 return ret;
1948}
1949
1950/*
1951 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001952 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1953 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001954 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001955 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001956item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001957{
1958 static int recurse = 0;
1959 list_T *l;
1960 listitem_T *li;
1961 dict_T *d;
1962 blob_T *b;
1963 hashitem_T *hi;
1964 int todo;
1965
1966 if (recurse >= DICT_MAXNEST)
1967 {
1968 emsg(_("E743: variable nested too deep for (un)lock"));
1969 return;
1970 }
1971 if (deep == 0)
1972 return;
1973 ++recurse;
1974
1975 // lock/unlock the item itself
1976 if (lock)
1977 tv->v_lock |= VAR_LOCKED;
1978 else
1979 tv->v_lock &= ~VAR_LOCKED;
1980
1981 switch (tv->v_type)
1982 {
1983 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001984 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001986 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001988 case VAR_STRING:
1989 case VAR_FUNC:
1990 case VAR_PARTIAL:
1991 case VAR_FLOAT:
1992 case VAR_SPECIAL:
1993 case VAR_JOB:
1994 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001995 case VAR_INSTR:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001996 break;
1997
1998 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001999 if ((b = tv->vval.v_blob) != NULL
2000 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002001 {
2002 if (lock)
2003 b->bv_lock |= VAR_LOCKED;
2004 else
2005 b->bv_lock &= ~VAR_LOCKED;
2006 }
2007 break;
2008 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002009 if ((l = tv->vval.v_list) != NULL
2010 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002011 {
2012 if (lock)
2013 l->lv_lock |= VAR_LOCKED;
2014 else
2015 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002016 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002017 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002018 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02002019 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002020 }
2021 break;
2022 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002023 if ((d = tv->vval.v_dict) != NULL
2024 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002025 {
2026 if (lock)
2027 d->dv_lock |= VAR_LOCKED;
2028 else
2029 d->dv_lock &= ~VAR_LOCKED;
2030 if (deep < 0 || deep > 1)
2031 {
2032 // recursive: lock/unlock the items the List contains
2033 todo = (int)d->dv_hashtab.ht_used;
2034 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2035 {
2036 if (!HASHITEM_EMPTY(hi))
2037 {
2038 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002039 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2040 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002041 }
2042 }
2043 }
2044 }
2045 }
2046 --recurse;
2047}
2048
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002049#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2050/*
2051 * Delete all "menutrans_" variables.
2052 */
2053 void
2054del_menutrans_vars(void)
2055{
2056 hashitem_T *hi;
2057 int todo;
2058
2059 hash_lock(&globvarht);
2060 todo = (int)globvarht.ht_used;
2061 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2062 {
2063 if (!HASHITEM_EMPTY(hi))
2064 {
2065 --todo;
2066 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2067 delete_var(&globvarht, hi);
2068 }
2069 }
2070 hash_unlock(&globvarht);
2071}
2072#endif
2073
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002074/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002075 * Local string buffer for the next two functions to store a variable name
2076 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2077 * get_user_var_name().
2078 */
2079
2080static char_u *varnamebuf = NULL;
2081static int varnamebuflen = 0;
2082
2083/*
2084 * Function to concatenate a prefix and a variable name.
2085 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002086 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002087cat_prefix_varname(int prefix, char_u *name)
2088{
2089 int len;
2090
2091 len = (int)STRLEN(name) + 3;
2092 if (len > varnamebuflen)
2093 {
2094 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002095 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002096 varnamebuf = alloc(len);
2097 if (varnamebuf == NULL)
2098 {
2099 varnamebuflen = 0;
2100 return NULL;
2101 }
2102 varnamebuflen = len;
2103 }
2104 *varnamebuf = prefix;
2105 varnamebuf[1] = ':';
2106 STRCPY(varnamebuf + 2, name);
2107 return varnamebuf;
2108}
2109
2110/*
2111 * Function given to ExpandGeneric() to obtain the list of user defined
2112 * (global/buffer/window/built-in) variable names.
2113 */
2114 char_u *
2115get_user_var_name(expand_T *xp, int idx)
2116{
2117 static long_u gdone;
2118 static long_u bdone;
2119 static long_u wdone;
2120 static long_u tdone;
2121 static int vidx;
2122 static hashitem_T *hi;
2123 hashtab_T *ht;
2124
2125 if (idx == 0)
2126 {
2127 gdone = bdone = wdone = vidx = 0;
2128 tdone = 0;
2129 }
2130
2131 // Global variables
2132 if (gdone < globvarht.ht_used)
2133 {
2134 if (gdone++ == 0)
2135 hi = globvarht.ht_array;
2136 else
2137 ++hi;
2138 while (HASHITEM_EMPTY(hi))
2139 ++hi;
2140 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2141 return cat_prefix_varname('g', hi->hi_key);
2142 return hi->hi_key;
2143 }
2144
2145 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002146 ht =
2147#ifdef FEAT_CMDWIN
2148 // In cmdwin, the alternative buffer should be used.
mityua1198122021-11-20 19:13:39 +00002149 is_in_cmdwin() ? &prevwin->w_buffer->b_vars->dv_hashtab :
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002150#endif
2151 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002152 if (bdone < ht->ht_used)
2153 {
2154 if (bdone++ == 0)
2155 hi = ht->ht_array;
2156 else
2157 ++hi;
2158 while (HASHITEM_EMPTY(hi))
2159 ++hi;
2160 return cat_prefix_varname('b', hi->hi_key);
2161 }
2162
2163 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002164 ht =
2165#ifdef FEAT_CMDWIN
2166 // In cmdwin, the alternative window should be used.
mityua1198122021-11-20 19:13:39 +00002167 is_in_cmdwin() ? &prevwin->w_vars->dv_hashtab :
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002168#endif
2169 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002170 if (wdone < ht->ht_used)
2171 {
2172 if (wdone++ == 0)
2173 hi = ht->ht_array;
2174 else
2175 ++hi;
2176 while (HASHITEM_EMPTY(hi))
2177 ++hi;
2178 return cat_prefix_varname('w', hi->hi_key);
2179 }
2180
2181 // t: variables
2182 ht = &curtab->tp_vars->dv_hashtab;
2183 if (tdone < ht->ht_used)
2184 {
2185 if (tdone++ == 0)
2186 hi = ht->ht_array;
2187 else
2188 ++hi;
2189 while (HASHITEM_EMPTY(hi))
2190 ++hi;
2191 return cat_prefix_varname('t', hi->hi_key);
2192 }
2193
2194 // v: variables
2195 if (vidx < VV_LEN)
2196 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2197
2198 VIM_CLEAR(varnamebuf);
2199 varnamebuflen = 0;
2200 return NULL;
2201}
2202
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002203 char *
2204get_var_special_name(int nr)
2205{
2206 switch (nr)
2207 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002208 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2209 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002210 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002211 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002212 }
2213 internal_error("get_var_special_name()");
2214 return "42";
2215}
2216
2217/*
2218 * Returns the global variable dictionary
2219 */
2220 dict_T *
2221get_globvar_dict(void)
2222{
2223 return &globvardict;
2224}
2225
2226/*
2227 * Returns the global variable hash table
2228 */
2229 hashtab_T *
2230get_globvar_ht(void)
2231{
2232 return &globvarht;
2233}
2234
2235/*
2236 * Returns the v: variable dictionary
2237 */
2238 dict_T *
2239get_vimvar_dict(void)
2240{
2241 return &vimvardict;
2242}
2243
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002244/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002246 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002247 */
2248 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002249find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002251 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2252 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253
2254 if (di == NULL)
2255 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002256 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2258 return (int)(vv - vimvars);
2259}
2260
2261
2262/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002263 * Set type of v: variable to "type".
2264 */
2265 void
2266set_vim_var_type(int idx, vartype_T type)
2267{
2268 vimvars[idx].vv_type = type;
2269}
2270
2271/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002272 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002273 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002274 */
2275 void
2276set_vim_var_nr(int idx, varnumber_T val)
2277{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002278 vimvars[idx].vv_nr = val;
2279}
2280
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002281 char *
2282get_vim_var_name(int idx)
2283{
2284 return vimvars[idx].vv_name;
2285}
2286
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002287/*
2288 * Get typval_T v: variable value.
2289 */
2290 typval_T *
2291get_vim_var_tv(int idx)
2292{
2293 return &vimvars[idx].vv_tv;
2294}
2295
2296/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002297 * Set v: variable to "tv". Only accepts the same type.
2298 * Takes over the value of "tv".
2299 */
2300 int
2301set_vim_var_tv(int idx, typval_T *tv)
2302{
2303 if (vimvars[idx].vv_type != tv->v_type)
2304 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002305 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002306 clear_tv(tv);
2307 return FAIL;
2308 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002309 // VV_RO is also checked when compiling, but let's check here as well.
2310 if (vimvars[idx].vv_flags & VV_RO)
2311 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002312 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002313 return FAIL;
2314 }
2315 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2316 {
2317 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2318 return FAIL;
2319 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002320 clear_tv(&vimvars[idx].vv_di.di_tv);
2321 vimvars[idx].vv_di.di_tv = *tv;
2322 return OK;
2323}
2324
2325/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002326 * Get number v: variable value.
2327 */
2328 varnumber_T
2329get_vim_var_nr(int idx)
2330{
2331 return vimvars[idx].vv_nr;
2332}
2333
2334/*
2335 * Get string v: variable value. Uses a static buffer, can only be used once.
2336 * If the String variable has never been set, return an empty string.
2337 * Never returns NULL;
2338 */
2339 char_u *
2340get_vim_var_str(int idx)
2341{
2342 return tv_get_string(&vimvars[idx].vv_tv);
2343}
2344
2345/*
2346 * Get List v: variable value. Caller must take care of reference count when
2347 * needed.
2348 */
2349 list_T *
2350get_vim_var_list(int idx)
2351{
2352 return vimvars[idx].vv_list;
2353}
2354
2355/*
2356 * Get Dict v: variable value. Caller must take care of reference count when
2357 * needed.
2358 */
2359 dict_T *
2360get_vim_var_dict(int idx)
2361{
2362 return vimvars[idx].vv_dict;
2363}
2364
2365/*
2366 * Set v:char to character "c".
2367 */
2368 void
2369set_vim_var_char(int c)
2370{
2371 char_u buf[MB_MAXBYTES + 1];
2372
2373 if (has_mbyte)
2374 buf[(*mb_char2bytes)(c, buf)] = NUL;
2375 else
2376 {
2377 buf[0] = c;
2378 buf[1] = NUL;
2379 }
2380 set_vim_var_string(VV_CHAR, buf, -1);
2381}
2382
2383/*
2384 * Set v:count to "count" and v:count1 to "count1".
2385 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2386 */
2387 void
2388set_vcount(
2389 long count,
2390 long count1,
2391 int set_prevcount)
2392{
2393 if (set_prevcount)
2394 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2395 vimvars[VV_COUNT].vv_nr = count;
2396 vimvars[VV_COUNT1].vv_nr = count1;
2397}
2398
2399/*
2400 * Save variables that might be changed as a side effect. Used when executing
2401 * a timer callback.
2402 */
2403 void
2404save_vimvars(vimvars_save_T *vvsave)
2405{
2406 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2407 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2408 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2409}
2410
2411/*
2412 * Restore variables saved by save_vimvars().
2413 */
2414 void
2415restore_vimvars(vimvars_save_T *vvsave)
2416{
2417 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2418 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2419 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2420}
2421
2422/*
2423 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2424 * value.
2425 */
2426 void
2427set_vim_var_string(
2428 int idx,
2429 char_u *val,
2430 int len) // length of "val" to use or -1 (whole string)
2431{
2432 clear_tv(&vimvars[idx].vv_di.di_tv);
2433 vimvars[idx].vv_type = VAR_STRING;
2434 if (val == NULL)
2435 vimvars[idx].vv_str = NULL;
2436 else if (len == -1)
2437 vimvars[idx].vv_str = vim_strsave(val);
2438 else
2439 vimvars[idx].vv_str = vim_strnsave(val, len);
2440}
2441
2442/*
2443 * Set List v: variable to "val".
2444 */
2445 void
2446set_vim_var_list(int idx, list_T *val)
2447{
2448 clear_tv(&vimvars[idx].vv_di.di_tv);
2449 vimvars[idx].vv_type = VAR_LIST;
2450 vimvars[idx].vv_list = val;
2451 if (val != NULL)
2452 ++val->lv_refcount;
2453}
2454
2455/*
2456 * Set Dictionary v: variable to "val".
2457 */
2458 void
2459set_vim_var_dict(int idx, dict_T *val)
2460{
2461 clear_tv(&vimvars[idx].vv_di.di_tv);
2462 vimvars[idx].vv_type = VAR_DICT;
2463 vimvars[idx].vv_dict = val;
2464 if (val != NULL)
2465 {
2466 ++val->dv_refcount;
2467 dict_set_items_ro(val);
2468 }
2469}
2470
2471/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002472 * Set the v:argv list.
2473 */
2474 void
2475set_argv_var(char **argv, int argc)
2476{
2477 list_T *l = list_alloc();
2478 int i;
2479
2480 if (l == NULL)
2481 getout(1);
2482 l->lv_lock = VAR_FIXED;
2483 for (i = 0; i < argc; ++i)
2484 {
2485 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2486 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002487 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002488 }
2489 set_vim_var_list(VV_ARGV, l);
2490}
2491
2492/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002493 * Reset v:register, taking the 'clipboard' setting into account.
2494 */
2495 void
2496reset_reg_var(void)
2497{
2498 int regname = 0;
2499
2500 // Adjust the register according to 'clipboard', so that when
2501 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2502#ifdef FEAT_CLIPBOARD
2503 adjust_clip_reg(&regname);
2504#endif
2505 set_reg_var(regname);
2506}
2507
2508/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002509 * Set v:register if needed.
2510 */
2511 void
2512set_reg_var(int c)
2513{
2514 char_u regname;
2515
2516 if (c == 0 || c == ' ')
2517 regname = '"';
2518 else
2519 regname = c;
2520 // Avoid free/alloc when the value is already right.
2521 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2522 set_vim_var_string(VV_REG, &regname, 1);
2523}
2524
2525/*
2526 * Get or set v:exception. If "oldval" == NULL, return the current value.
2527 * Otherwise, restore the value to "oldval" and return NULL.
2528 * Must always be called in pairs to save and restore v:exception! Does not
2529 * take care of memory allocations.
2530 */
2531 char_u *
2532v_exception(char_u *oldval)
2533{
2534 if (oldval == NULL)
2535 return vimvars[VV_EXCEPTION].vv_str;
2536
2537 vimvars[VV_EXCEPTION].vv_str = oldval;
2538 return NULL;
2539}
2540
2541/*
2542 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2543 * Otherwise, restore the value to "oldval" and return NULL.
2544 * Must always be called in pairs to save and restore v:throwpoint! Does not
2545 * take care of memory allocations.
2546 */
2547 char_u *
2548v_throwpoint(char_u *oldval)
2549{
2550 if (oldval == NULL)
2551 return vimvars[VV_THROWPOINT].vv_str;
2552
2553 vimvars[VV_THROWPOINT].vv_str = oldval;
2554 return NULL;
2555}
2556
2557/*
2558 * Set v:cmdarg.
2559 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2560 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2561 * Must always be called in pairs!
2562 */
2563 char_u *
2564set_cmdarg(exarg_T *eap, char_u *oldarg)
2565{
2566 char_u *oldval;
2567 char_u *newval;
2568 unsigned len;
2569
2570 oldval = vimvars[VV_CMDARG].vv_str;
2571 if (eap == NULL)
2572 {
2573 vim_free(oldval);
2574 vimvars[VV_CMDARG].vv_str = oldarg;
2575 return NULL;
2576 }
2577
2578 if (eap->force_bin == FORCE_BIN)
2579 len = 6;
2580 else if (eap->force_bin == FORCE_NOBIN)
2581 len = 8;
2582 else
2583 len = 0;
2584
2585 if (eap->read_edit)
2586 len += 7;
2587
2588 if (eap->force_ff != 0)
2589 len += 10; // " ++ff=unix"
2590 if (eap->force_enc != 0)
2591 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2592 if (eap->bad_char != 0)
2593 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2594
2595 newval = alloc(len + 1);
2596 if (newval == NULL)
2597 return NULL;
2598
2599 if (eap->force_bin == FORCE_BIN)
2600 sprintf((char *)newval, " ++bin");
2601 else if (eap->force_bin == FORCE_NOBIN)
2602 sprintf((char *)newval, " ++nobin");
2603 else
2604 *newval = NUL;
2605
2606 if (eap->read_edit)
2607 STRCAT(newval, " ++edit");
2608
2609 if (eap->force_ff != 0)
2610 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2611 eap->force_ff == 'u' ? "unix"
2612 : eap->force_ff == 'd' ? "dos"
2613 : "mac");
2614 if (eap->force_enc != 0)
2615 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2616 eap->cmd + eap->force_enc);
2617 if (eap->bad_char == BAD_KEEP)
2618 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2619 else if (eap->bad_char == BAD_DROP)
2620 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2621 else if (eap->bad_char != 0)
2622 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2623 vimvars[VV_CMDARG].vv_str = newval;
2624 return oldval;
2625}
2626
2627/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002628 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002629 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2630 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002631 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2632 */
2633 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002634eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002635 char_u *name,
2636 int len, // length of "name"
2637 typval_T *rettv, // NULL when only checking existence
2638 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002639 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002640{
2641 int ret = OK;
2642 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002643 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002644 hashtab_T *ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002645 int cc;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002646 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002647
2648 // truncate the name, so that we can use strcmp()
2649 cc = name[len];
2650 name[len] = NUL;
2651
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002652 // Check for local variable when debugging.
2653 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002654 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002655 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002656 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2657
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002658 if (v != NULL)
2659 {
2660 tv = &v->di_tv;
2661 if (dip != NULL)
2662 *dip = v;
2663 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002664 else
2665 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002666 }
2667
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002668 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002669 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002670 imported_T *import;
2671 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2672
2673 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002674
2675 // imported variable from another script
2676 if (import != NULL)
2677 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002678 if (import->imp_funcname != NULL)
2679 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002680 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002681 if (rettv != NULL)
2682 {
2683 rettv->v_type = VAR_FUNC;
2684 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2685 }
2686 }
Bram Moolenaara6294952020-12-27 13:39:50 +01002687 else if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002688 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002689 if ((flags & EVAL_VAR_IMPORT) == 0)
2690 {
2691 if (flags & EVAL_VAR_VERBOSE)
2692 emsg(_(e_import_as_name_not_supported_here));
2693 ret = FAIL;
2694 }
2695 else
2696 {
2697 if (rettv != NULL)
2698 {
2699 rettv->v_type = VAR_ANY;
2700 rettv->vval.v_number = import->imp_sid;
2701 }
2702 found = TRUE;
2703 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002704 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002705 else
2706 {
2707 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002708 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002710 tv = sv->sv_tv;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002711 type = sv->sv_type;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002712 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00002714 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002715 {
2716 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2717
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002718 // In Vim9 script we can get a function reference by using the
2719 // function name.
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002720 if (ufunc != NULL)
2721 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002722 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002723 if (rettv != NULL)
2724 {
2725 rettv->v_type = VAR_FUNC;
2726 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002727 if (rettv->vval.v_string != NULL)
2728 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002729 }
2730 }
2731 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002732 }
2733
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002734 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002735 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002736 if (tv == NULL)
2737 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002738 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002739 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002740 ret = FAIL;
2741 }
2742 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002743 {
Bram Moolenaar11005b02021-07-11 20:59:00 +02002744 if (ht != NULL && ht == get_script_local_ht()
2745 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002746 {
Bram Moolenaarc967d572021-07-08 21:38:50 +02002747 svar_T *sv = find_typval_in_script(tv);
Bram Moolenaarf055d452021-07-08 20:57:24 +02002748
Bram Moolenaarf055d452021-07-08 20:57:24 +02002749 if (sv != NULL)
2750 type = sv->sv_type;
2751 }
2752
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002753 // If a list or dict variable wasn't initialized, do it now.
2754 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2755 {
2756 tv->vval.v_dict = dict_alloc();
2757 if (tv->vval.v_dict != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002758 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002759 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002760 tv->vval.v_dict->dv_type = alloc_type(type);
2761 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002762 }
2763 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2764 {
2765 tv->vval.v_list = list_alloc();
2766 if (tv->vval.v_list != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002767 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002768 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002769 tv->vval.v_list->lv_type = alloc_type(type);
2770 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002771 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002772 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL)
2773 {
2774 tv->vval.v_blob = blob_alloc();
2775 if (tv->vval.v_blob != NULL)
2776 ++tv->vval.v_blob->bv_refcount;
2777 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002778 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002779 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002780 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002781
2782 name[len] = cc;
2783
2784 return ret;
2785}
2786
2787/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002788 * Check if variable "name[len]" is a local variable or an argument.
2789 * If so, "*eval_lavars_used" is set to TRUE.
2790 */
2791 void
2792check_vars(char_u *name, int len)
2793{
2794 int cc;
2795 char_u *varname;
2796 hashtab_T *ht;
2797
2798 if (eval_lavars_used == NULL)
2799 return;
2800
2801 // truncate the name, so that we can use strcmp()
2802 cc = name[len];
2803 name[len] = NUL;
2804
2805 ht = find_var_ht(name, &varname);
2806 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2807 {
2808 if (find_var(name, NULL, TRUE) != NULL)
2809 *eval_lavars_used = TRUE;
2810 }
2811
2812 name[len] = cc;
2813}
2814
2815/*
2816 * Find variable "name" in the list of variables.
2817 * Return a pointer to it if found, NULL if not found.
2818 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002819 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002820 */
2821 dictitem_T *
2822find_var(char_u *name, hashtab_T **htp, int no_autoload)
2823{
2824 char_u *varname;
2825 hashtab_T *ht;
2826 dictitem_T *ret = NULL;
2827
2828 ht = find_var_ht(name, &varname);
2829 if (htp != NULL)
2830 *htp = ht;
2831 if (ht == NULL)
2832 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002833 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002834 if (ret != NULL)
2835 return ret;
2836
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002837 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002838 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002839 if (ret != NULL)
2840 return ret;
2841
2842 // in Vim9 script items without a scope can be script-local
2843 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2844 {
2845 ht = get_script_local_ht();
2846 if (ht != NULL)
2847 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002848 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002849 if (ret != NULL)
2850 {
2851 if (htp != NULL)
2852 *htp = ht;
2853 return ret;
2854 }
2855 }
2856 }
2857
2858 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002859}
2860
2861/*
2862 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002863 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002864 * Returns NULL if not found.
2865 */
2866 dictitem_T *
2867find_var_in_ht(
2868 hashtab_T *ht,
2869 int htname,
2870 char_u *varname,
2871 int no_autoload)
2872{
2873 hashitem_T *hi;
2874
2875 if (*varname == NUL)
2876 {
2877 // Must be something like "s:", otherwise "ht" would be NULL.
2878 switch (htname)
2879 {
2880 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2881 case 'g': return &globvars_var;
2882 case 'v': return &vimvars_var;
2883 case 'b': return &curbuf->b_bufvar;
2884 case 'w': return &curwin->w_winvar;
2885 case 't': return &curtab->tp_winvar;
2886 case 'l': return get_funccal_local_var();
2887 case 'a': return get_funccal_args_var();
2888 }
2889 return NULL;
2890 }
2891
2892 hi = hash_find(ht, varname);
2893 if (HASHITEM_EMPTY(hi))
2894 {
2895 // For global variables we may try auto-loading the script. If it
2896 // worked find the variable again. Don't auto-load a script if it was
2897 // loaded already, otherwise it would be loaded every time when
2898 // checking if a function name is a Funcref variable.
2899 if (ht == &globvarht && !no_autoload)
2900 {
2901 // Note: script_autoload() may make "hi" invalid. It must either
2902 // be obtained again or not used.
2903 if (!script_autoload(varname, FALSE) || aborting())
2904 return NULL;
2905 hi = hash_find(ht, varname);
2906 }
2907 if (HASHITEM_EMPTY(hi))
2908 return NULL;
2909 }
2910 return HI2DI(hi);
2911}
2912
2913/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 * Get the script-local hashtab. NULL if not in a script context.
2915 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002916 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917get_script_local_ht(void)
2918{
2919 scid_T sid = current_sctx.sc_sid;
2920
Bram Moolenaare3d46852020-08-29 13:39:17 +02002921 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922 return &SCRIPT_VARS(sid);
2923 return NULL;
2924}
2925
2926/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002927 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002928 * When "cmd" is TRUE it must look like a command, a function must be followed
2929 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01002930 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01002932 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002933lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01002934 char_u *name,
2935 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002936 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01002937 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938{
2939 hashtab_T *ht = get_script_local_ht();
2940 char_u buffer[30];
2941 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01002942 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002944 int is_global = FALSE;
2945 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946
2947 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002948 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 if (len < sizeof(buffer) - 1)
2950 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002951 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952 vim_strncpy(buffer, name, len);
2953 p = buffer;
2954 }
2955 else
2956 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002957 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002959 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 }
2961
2962 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002963 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964
2965 // if not script-local, then perhaps imported
Bram Moolenaar709664c2020-12-12 14:33:41 +01002966 if (res == FAIL && find_imported(p, 0, NULL) != NULL)
2967 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 if (p != buffer)
2969 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002970
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002971 // Find a function, so that a following "->" works.
2972 // When used as a command require "(" or "->" to follow, "Cmd" is a user
2973 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002974 if (res != OK)
2975 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002976 p = skipwhite(name + len);
2977
2978 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002979 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002980 // Do not check for an internal function, since it might also be a
2981 // valid command, such as ":split" versus "split()".
2982 // Skip "g:" before a function name.
2983 if (name[0] == 'g' && name[1] == ':')
2984 {
2985 is_global = TRUE;
2986 fname = name + 2;
2987 }
2988 if (find_func(fname, is_global, NULL) != NULL)
2989 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002990 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002991 }
2992
Bram Moolenaar709664c2020-12-12 14:33:41 +01002993 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002994}
2995
2996/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002997 * Find the hashtab used for a variable name.
2998 * Return NULL if the name is not valid.
2999 * Set "varname" to the start of name without ':'.
3000 */
3001 hashtab_T *
3002find_var_ht(char_u *name, char_u **varname)
3003{
3004 hashitem_T *hi;
3005 hashtab_T *ht;
3006
3007 if (name[0] == NUL)
3008 return NULL;
3009 if (name[1] != ':')
3010 {
3011 // The name must not start with a colon or #.
3012 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3013 return NULL;
3014 *varname = name;
3015
3016 // "version" is "v:version" in all scopes if scriptversion < 3.
3017 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003018 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003019 {
3020 hi = hash_find(&compat_hashtab, name);
3021 if (!HASHITEM_EMPTY(hi))
3022 return &compat_hashtab;
3023 }
3024
3025 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 if (ht != NULL)
3027 return ht; // local variable
3028
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003029 // In Vim9 script items at the script level are script-local, except
3030 // for autoload names.
3031 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 {
3033 ht = get_script_local_ht();
3034 if (ht != NULL)
3035 return ht;
3036 }
3037
3038 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003039 }
3040 *varname = name + 2;
3041 if (*name == 'g') // global variable
3042 return &globvarht;
3043 // There must be no ':' or '#' in the rest of the name, unless g: is used
3044 if (vim_strchr(name + 2, ':') != NULL
3045 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3046 return NULL;
3047 if (*name == 'b') // buffer variable
3048 return &curbuf->b_vars->dv_hashtab;
3049 if (*name == 'w') // window variable
3050 return &curwin->w_vars->dv_hashtab;
3051 if (*name == 't') // tab page variable
3052 return &curtab->tp_vars->dv_hashtab;
3053 if (*name == 'v') // v: variable
3054 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003055 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003056 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003058 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 if (*name == 'a') // a: function argument
3060 return get_funccal_args_ht();
3061 if (*name == 'l') // l: local function variable
3062 return get_funccal_local_ht();
3063 }
3064 if (*name == 's') // script variable
3065 {
3066 ht = get_script_local_ht();
3067 if (ht != NULL)
3068 return ht;
3069 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003070 return NULL;
3071}
3072
3073/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003074 * Get the string value of a (global/local) variable.
3075 * Note: see tv_get_string() for how long the pointer remains valid.
3076 * Returns NULL when it doesn't exist.
3077 */
3078 char_u *
3079get_var_value(char_u *name)
3080{
3081 dictitem_T *v;
3082
3083 v = find_var(name, NULL, FALSE);
3084 if (v == NULL)
3085 return NULL;
3086 return tv_get_string(&v->di_tv);
3087}
3088
3089/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003090 * Allocate a new hashtab for a sourced script. It will be used while
3091 * sourcing this script and when executing functions defined in the script.
3092 */
3093 void
3094new_script_vars(scid_T id)
3095{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003096 scriptvar_T *sv;
3097
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003098 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3099 if (sv == NULL)
3100 return;
3101 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003102 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003103}
3104
3105/*
3106 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3107 * point to it.
3108 */
3109 void
3110init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3111{
3112 hash_init(&dict->dv_hashtab);
3113 dict->dv_lock = 0;
3114 dict->dv_scope = scope;
3115 dict->dv_refcount = DO_NOT_FREE_CNT;
3116 dict->dv_copyID = 0;
3117 dict_var->di_tv.vval.v_dict = dict;
3118 dict_var->di_tv.v_type = VAR_DICT;
3119 dict_var->di_tv.v_lock = VAR_FIXED;
3120 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3121 dict_var->di_key[0] = NUL;
3122}
3123
3124/*
3125 * Unreference a dictionary initialized by init_var_dict().
3126 */
3127 void
3128unref_var_dict(dict_T *dict)
3129{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003130 // Now the dict needs to be freed if no one else is using it, go back to
3131 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003132 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3133 dict_unref(dict);
3134}
3135
3136/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003137 * Clean up a list of internal variables.
3138 * Frees all allocated variables and the value they contain.
3139 * Clears hashtab "ht", does not free it.
3140 */
3141 void
3142vars_clear(hashtab_T *ht)
3143{
3144 vars_clear_ext(ht, TRUE);
3145}
3146
3147/*
3148 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3149 */
3150 void
3151vars_clear_ext(hashtab_T *ht, int free_val)
3152{
3153 int todo;
3154 hashitem_T *hi;
3155 dictitem_T *v;
3156
3157 hash_lock(ht);
3158 todo = (int)ht->ht_used;
3159 for (hi = ht->ht_array; todo > 0; ++hi)
3160 {
3161 if (!HASHITEM_EMPTY(hi))
3162 {
3163 --todo;
3164
3165 // Free the variable. Don't remove it from the hashtab,
3166 // ht_array might change then. hash_clear() takes care of it
3167 // later.
3168 v = HI2DI(hi);
3169 if (free_val)
3170 clear_tv(&v->di_tv);
3171 if (v->di_flags & DI_FLAGS_ALLOC)
3172 vim_free(v);
3173 }
3174 }
3175 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003176 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003177}
3178
3179/*
3180 * Delete a variable from hashtab "ht" at item "hi".
3181 * Clear the variable value and free the dictitem.
3182 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003183 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003184delete_var(hashtab_T *ht, hashitem_T *hi)
3185{
3186 dictitem_T *di = HI2DI(hi);
3187
3188 hash_remove(ht, hi);
3189 clear_tv(&di->di_tv);
3190 vim_free(di);
3191}
3192
3193/*
3194 * List the value of one internal variable.
3195 */
3196 static void
3197list_one_var(dictitem_T *v, char *prefix, int *first)
3198{
3199 char_u *tofree;
3200 char_u *s;
3201 char_u numbuf[NUMBUFLEN];
3202
3203 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3204 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3205 s == NULL ? (char_u *)"" : s, first);
3206 vim_free(tofree);
3207}
3208
3209 static void
3210list_one_var_a(
3211 char *prefix,
3212 char_u *name,
3213 int type,
3214 char_u *string,
3215 int *first) // when TRUE clear rest of screen and set to FALSE
3216{
3217 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3218 msg_start();
3219 msg_puts(prefix);
3220 if (name != NULL) // "a:" vars don't have a name stored
3221 msg_puts((char *)name);
3222 msg_putchar(' ');
3223 msg_advance(22);
3224 if (type == VAR_NUMBER)
3225 msg_putchar('#');
3226 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3227 msg_putchar('*');
3228 else if (type == VAR_LIST)
3229 {
3230 msg_putchar('[');
3231 if (*string == '[')
3232 ++string;
3233 }
3234 else if (type == VAR_DICT)
3235 {
3236 msg_putchar('{');
3237 if (*string == '{')
3238 ++string;
3239 }
3240 else
3241 msg_putchar(' ');
3242
3243 msg_outtrans(string);
3244
3245 if (type == VAR_FUNC || type == VAR_PARTIAL)
3246 msg_puts("()");
3247 if (*first)
3248 {
3249 msg_clr_eos();
3250 *first = FALSE;
3251 }
3252}
3253
3254/*
3255 * Set variable "name" to value in "tv".
3256 * If the variable already exists, the value is updated.
3257 * Otherwise the variable is created.
3258 */
3259 void
3260set_var(
3261 char_u *name,
3262 typval_T *tv,
3263 int copy) // make copy of value in "tv"
3264{
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003265 set_var_const(name, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003266}
3267
3268/*
3269 * Set variable "name" to value in "tv".
3270 * If the variable already exists and "is_const" is FALSE the value is updated.
3271 * Otherwise the variable is created.
3272 */
3273 void
3274set_var_const(
3275 char_u *name,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003276 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003277 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003278 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003279 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003280 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003281{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003282 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003283 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003284 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 dictitem_T *di;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003286 typval_T *dest_tv = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003287 char_u *varname;
3288 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003290 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003291 int var_in_vim9script;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003292 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003293 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003294
3295 ht = find_var_ht(name, &varname);
3296 if (ht == NULL || *varname == NUL)
3297 {
3298 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003299 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003300 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003301 is_script_local = ht == get_script_local_ht();
3302
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003303 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003304 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003305 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003306 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003307 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003308 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003309 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003310 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003311 }
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003312 if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
3313 && vim_strchr((char_u *)"gwbt", name[0]) != NULL)
3314 // Do not make g:var, w:var, b:var or t:var final.
3315 flags &= ~ASSIGN_FINAL;
3316
Bram Moolenaare535db82021-03-31 21:07:24 +02003317 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003318 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3319 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003320 // For "[a, _] = list" the underscore is ignored.
3321 if ((flags & ASSIGN_UNPACK) == 0)
3322 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003323 goto failed;
3324 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003325
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003327
Bram Moolenaar24e93162021-07-18 20:40:33 +02003328 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003329 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003330 imported_T *import = find_imported(varname, 0, NULL);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003331
Bram Moolenaar24e93162021-07-18 20:40:33 +02003332 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003333 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003334 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
3335 svar_T *sv;
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003336 where_T where = WHERE_INIT;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003337
3338 // imported variable from another script
3339 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003341 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003342 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 }
Bram Moolenaar6853c382021-09-07 22:12:19 +02003344 if (import->imp_flags & IMP_FLAGS_STAR)
3345 {
3346 semsg(_(e_cannot_use_str_itself_it_is_imported_with_star),
3347 name);
3348 goto failed;
3349 }
Bram Moolenaar60579352021-07-19 21:45:07 +02003350 sv = ((svar_T *)si->sn_var_vals.ga_data) + import->imp_var_vals_idx;
3351
Bram Moolenaar60579352021-07-19 21:45:07 +02003352 where.wt_variable = TRUE;
3353 if (check_typval_type(sv->sv_type, tv, where) == FAIL
3354 || value_check_lock(sv->sv_tv->v_lock, name, FALSE))
3355 {
3356 goto failed;
3357 }
3358
Bram Moolenaar24e93162021-07-18 20:40:33 +02003359 dest_tv = sv->sv_tv;
Bram Moolenaarf6488542021-07-18 21:24:50 +02003360 clear_tv(dest_tv);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003361 }
3362 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363
Bram Moolenaar24e93162021-07-18 20:40:33 +02003364 if (dest_tv == NULL)
3365 {
3366 // Search in parent scope which is possible to reference from lambda
3367 if (di == NULL)
3368 di = find_var_in_scoped_ht(name, TRUE);
3369
3370 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003371 && var_wrong_func_name(name, di == NULL))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003372 goto failed;
3373
3374 if (need_convert_to_bool(type, tv))
3375 {
Bram Moolenaarf6488542021-07-18 21:24:50 +02003376 // Destination is a bool and the value is not, but it can be
3377 // converted.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003378 CLEAR_FIELD(bool_tv);
3379 bool_tv.v_type = VAR_BOOL;
3380 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3381 tv = &bool_tv;
3382 }
3383
3384 if (di != NULL)
3385 {
3386 // Item already exists. Allowed to replace when reloading.
3387 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
3388 {
3389 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaarf6488542021-07-18 21:24:50 +02003390 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003391 {
3392 emsg(_(e_cannot_mod));
3393 goto failed;
3394 }
3395
3396 if (is_script_local && vim9script
Bram Moolenaarf6488542021-07-18 21:24:50 +02003397 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003398 {
3399 semsg(_(e_redefining_script_item_str), name);
3400 goto failed;
3401 }
3402
Bram Moolenaara06758d2021-10-15 00:18:37 +01003403 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003404 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003405 where_T where = WHERE_INIT;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003406 svar_T *sv = find_typval_in_script(&di->di_tv);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003407
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003408 if (sv != NULL)
3409 {
3410 // check the type and adjust to bool if needed
3411 where.wt_index = var_idx;
3412 where.wt_variable = TRUE;
3413 if (check_script_var_type(sv, tv, name, where) == FAIL)
3414 goto failed;
3415 if (type == NULL)
3416 type = sv->sv_type;
3417 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003418 }
3419
Bram Moolenaara06758d2021-10-15 00:18:37 +01003420 if ((flags & ASSIGN_FOR_LOOP) == 0
3421 && var_check_permission(di, name) == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003422 goto failed;
3423 }
3424 else
3425 {
3426 // can only redefine once
3427 di->di_flags &= ~DI_FLAGS_RELOAD;
3428
Bram Moolenaarf6488542021-07-18 21:24:50 +02003429 // A Vim9 script-local variable is also present in sn_all_vars
3430 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaar24e93162021-07-18 20:40:33 +02003431 if (var_in_vim9script)
3432 update_vim9_script_var(FALSE, di, flags, tv, &type,
Bram Moolenaarf6488542021-07-18 21:24:50 +02003433 (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003434 }
3435
3436 // existing variable, need to clear the value
3437
3438 // Handle setting internal di: variables separately where needed to
3439 // prevent changing the type.
3440 if (ht == &vimvarht)
3441 {
3442 if (di->di_tv.v_type == VAR_STRING)
3443 {
3444 VIM_CLEAR(di->di_tv.vval.v_string);
3445 if (copy || tv->v_type != VAR_STRING)
3446 {
3447 char_u *val = tv_get_string(tv);
3448
Bram Moolenaarf6488542021-07-18 21:24:50 +02003449 // Careful: when assigning to v:errmsg and
3450 // tv_get_string() causes an error message the variable
3451 // will already be set.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003452 if (di->di_tv.vval.v_string == NULL)
3453 di->di_tv.vval.v_string = vim_strsave(val);
3454 }
3455 else
3456 {
3457 // Take over the string to avoid an extra alloc/free.
3458 di->di_tv.vval.v_string = tv->vval.v_string;
3459 tv->vval.v_string = NULL;
3460 }
3461 goto failed;
3462 }
3463 else if (di->di_tv.v_type == VAR_NUMBER)
3464 {
3465 di->di_tv.vval.v_number = tv_get_number(tv);
3466 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003467 set_search_direction(di->di_tv.vval.v_number
3468 ? '/' : '?');
Bram Moolenaar24e93162021-07-18 20:40:33 +02003469#ifdef FEAT_SEARCH_EXTRA
3470 else if (STRCMP(varname, "hlsearch") == 0)
3471 {
3472 no_hlsearch = !di->di_tv.vval.v_number;
3473 redraw_all_later(SOME_VALID);
3474 }
3475#endif
3476 goto failed;
3477 }
3478 else if (di->di_tv.v_type != tv->v_type)
3479 {
3480 semsg(_("E963: setting %s to value with wrong type"), name);
3481 goto failed;
3482 }
3483 }
3484
3485 clear_tv(&di->di_tv);
3486 }
3487 else
3488 {
3489 // Item not found, check if a function already exists.
3490 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaarf6488542021-07-18 21:24:50 +02003491 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003492 {
3493 semsg(_(e_redefining_script_item_str), name);
3494 goto failed;
3495 }
3496
Bram Moolenaar24e93162021-07-18 20:40:33 +02003497 // add a new variable
3498 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3499 {
3500 semsg(_(e_unknown_variable_str), name);
3501 goto failed;
3502 }
3503
3504 // Can't add "v:" or "a:" variable.
3505 if (ht == &vimvarht || ht == get_funccal_args_ht())
3506 {
3507 semsg(_(e_illvar), name);
3508 goto failed;
3509 }
3510
3511 // Make sure the variable name is valid. In Vim9 script an autoload
3512 // variable must be prefixed with "g:".
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003513 if (!valid_varname(varname, -1, !vim9script
Bram Moolenaarf6488542021-07-18 21:24:50 +02003514 || STRNCMP(name, "g:", 2) == 0))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003515 goto failed;
3516
3517 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3518 if (di == NULL)
3519 goto failed;
3520 STRCPY(di->di_key, varname);
3521 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
3522 {
3523 vim_free(di);
3524 goto failed;
3525 }
3526 di->di_flags = DI_FLAGS_ALLOC;
3527 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3528 di->di_flags |= DI_FLAGS_LOCK;
3529
3530 // A Vim9 script-local variable is also added to sn_all_vars and
3531 // sn_var_vals. It may set "type" from "tv".
Bram Moolenaare535db82021-03-31 21:07:24 +02003532 if (var_in_vim9script)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003533 update_vim9_script_var(TRUE, di, flags, tv, &type,
Bram Moolenaarf6488542021-07-18 21:24:50 +02003534 (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003535 }
3536
Bram Moolenaar24e93162021-07-18 20:40:33 +02003537 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003538 }
3539
3540 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003541 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003542 else
3543 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003544 *dest_tv = *tv;
3545 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003546 init_tv(tv);
3547 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003548 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003549
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003550 if (vim9script && type != NULL)
3551 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003552 if (type->tt_type == VAR_DICT && dest_tv->vval.v_dict != NULL)
Bram Moolenaar464393a2021-09-11 23:07:44 +02003553 {
3554 if (dest_tv->vval.v_dict->dv_type != type)
3555 {
3556 free_type(dest_tv->vval.v_dict->dv_type);
3557 dest_tv->vval.v_dict->dv_type = alloc_type(type);
3558 }
3559 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003560 else if (type->tt_type == VAR_LIST && dest_tv->vval.v_list != NULL)
Bram Moolenaar464393a2021-09-11 23:07:44 +02003561 {
3562 if (dest_tv->vval.v_list->lv_type != type)
3563 {
3564 free_type(dest_tv->vval.v_list->lv_type);
3565 dest_tv->vval.v_list->lv_type = alloc_type(type);
3566 }
3567 }
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003568 }
3569
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003570 // ":const var = value" locks the value
3571 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003572 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003573 // Like :lockvar! name: lock the value and what it contains, but only
3574 // if the reference count is up to one. That locks only literal
3575 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003576 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003577
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003578failed:
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003579 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003580 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003581}
3582
3583/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003584 * Check in this order for backwards compatibility:
3585 * - Whether the variable is read-only
3586 * - Whether the variable value is locked
3587 * - Whether the variable is locked
3588 */
3589 int
3590var_check_permission(dictitem_T *di, char_u *name)
3591{
3592 if (var_check_ro(di->di_flags, name, FALSE)
3593 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3594 || var_check_lock(di->di_flags, name, FALSE))
3595 return FAIL;
3596 return OK;
3597}
3598
3599/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003600 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3601 * Also give an error message.
3602 */
3603 int
3604var_check_ro(int flags, char_u *name, int use_gettext)
3605{
3606 if (flags & DI_FLAGS_RO)
3607 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02003608 semsg(_(e_cannot_change_readonly_variable_str),
3609 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003610 return TRUE;
3611 }
3612 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3613 {
3614 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3615 return TRUE;
3616 }
3617 return FALSE;
3618}
3619
3620/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003621 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3622 * Also give an error message.
3623 */
3624 int
3625var_check_lock(int flags, char_u *name, int use_gettext)
3626{
3627 if (flags & DI_FLAGS_LOCK)
3628 {
3629 semsg(_(e_variable_is_locked_str),
3630 use_gettext ? (char_u *)_(name) : name);
3631 return TRUE;
3632 }
3633 return FALSE;
3634}
3635
3636/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003637 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3638 * Also give an error message.
3639 */
3640 int
3641var_check_fixed(int flags, char_u *name, int use_gettext)
3642{
3643 if (flags & DI_FLAGS_FIX)
3644 {
3645 semsg(_("E795: Cannot delete variable %s"),
3646 use_gettext ? (char_u *)_(name) : name);
3647 return TRUE;
3648 }
3649 return FALSE;
3650}
3651
3652/*
3653 * Check if a funcref is assigned to a valid variable name.
3654 * Return TRUE and give an error if not.
3655 */
3656 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003657var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003658 char_u *name, // points to start of variable name
3659 int new_var) // TRUE when creating the variable
3660{
Bram Moolenaar32154662021-03-28 21:14:06 +02003661 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
3662 // the name can be used without the s: prefix.
3663 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
3664 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003665 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3666 ? name[2] : name[0]))
3667 {
3668 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3669 name);
3670 return TRUE;
3671 }
3672 // Don't allow hiding a function. When "v" is not NULL we might be
3673 // assigning another function to the same var, the type is checked
3674 // below.
3675 if (new_var && function_exists(name, FALSE))
3676 {
3677 semsg(_("E705: Variable name conflicts with existing function: %s"),
3678 name);
3679 return TRUE;
3680 }
3681 return FALSE;
3682}
3683
3684/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003685 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3686 * value. Also give an error message, using "name" or _("name") when
3687 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003688 */
3689 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003690value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003691{
3692 if (lock & VAR_LOCKED)
3693 {
3694 semsg(_("E741: Value is locked: %s"),
3695 name == NULL ? (char_u *)_("Unknown")
3696 : use_gettext ? (char_u *)_(name)
3697 : name);
3698 return TRUE;
3699 }
3700 if (lock & VAR_FIXED)
3701 {
3702 semsg(_("E742: Cannot change value of %s"),
3703 name == NULL ? (char_u *)_("Unknown")
3704 : use_gettext ? (char_u *)_(name)
3705 : name);
3706 return TRUE;
3707 }
3708 return FALSE;
3709}
3710
3711/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003712 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003713 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003714 * Return FALSE and give an error if not.
3715 */
3716 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003717valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003718{
3719 char_u *p;
3720
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003721 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003722 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003723 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003724 {
3725 semsg(_(e_illvar), varname);
3726 return FALSE;
3727 }
3728 return TRUE;
3729}
3730
3731/*
3732 * getwinvar() and gettabwinvar()
3733 */
3734 static void
3735getwinvar(
3736 typval_T *argvars,
3737 typval_T *rettv,
3738 int off) // 1 for gettabwinvar()
3739{
3740 win_T *win;
3741 char_u *varname;
3742 dictitem_T *v;
3743 tabpage_T *tp = NULL;
3744 int done = FALSE;
3745 win_T *oldcurwin;
3746 tabpage_T *oldtabpage;
3747 int need_switch_win;
3748
3749 if (off == 1)
3750 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3751 else
3752 tp = curtab;
3753 win = find_win_by_nr(&argvars[off], tp);
3754 varname = tv_get_string_chk(&argvars[off + 1]);
3755 ++emsg_off;
3756
3757 rettv->v_type = VAR_STRING;
3758 rettv->vval.v_string = NULL;
3759
3760 if (win != NULL && varname != NULL)
3761 {
3762 // Set curwin to be our win, temporarily. Also set the tabpage,
3763 // otherwise the window is not valid. Only do this when needed,
3764 // autocommands get blocked.
3765 need_switch_win = !(tp == curtab && win == curwin);
3766 if (!need_switch_win
3767 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3768 {
3769 if (*varname == '&')
3770 {
3771 if (varname[1] == NUL)
3772 {
3773 // get all window-local options in a dict
3774 dict_T *opts = get_winbuf_options(FALSE);
3775
3776 if (opts != NULL)
3777 {
3778 rettv_dict_set(rettv, opts);
3779 done = TRUE;
3780 }
3781 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003782 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003783 // window-local-option
3784 done = TRUE;
3785 }
3786 else
3787 {
3788 // Look up the variable.
3789 // Let getwinvar({nr}, "") return the "w:" dictionary.
3790 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3791 varname, FALSE);
3792 if (v != NULL)
3793 {
3794 copy_tv(&v->di_tv, rettv);
3795 done = TRUE;
3796 }
3797 }
3798 }
3799
3800 if (need_switch_win)
3801 // restore previous notion of curwin
3802 restore_win(oldcurwin, oldtabpage, TRUE);
3803 }
3804
3805 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3806 // use the default return value
3807 copy_tv(&argvars[off + 2], rettv);
3808
3809 --emsg_off;
3810}
3811
3812/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003813 * Set option "varname" to the value of "varp" for the current buffer/window.
3814 */
3815 static void
3816set_option_from_tv(char_u *varname, typval_T *varp)
3817{
3818 long numval = 0;
3819 char_u *strval;
3820 char_u nbuf[NUMBUFLEN];
3821 int error = FALSE;
3822
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003823 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003824 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003825 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003826 strval = (char_u *)"0"; // avoid using "false"
3827 }
3828 else
3829 {
3830 if (!in_vim9script() || varp->v_type != VAR_STRING)
3831 numval = (long)tv_get_number_chk(varp, &error);
3832 strval = tv_get_string_buf_chk(varp, nbuf);
3833 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02003834 if (!error && strval != NULL)
3835 set_option_value(varname, numval, strval, OPT_LOCAL);
3836}
3837
3838/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003839 * "setwinvar()" and "settabwinvar()" functions
3840 */
3841 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003842setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003843{
3844 win_T *win;
3845 win_T *save_curwin;
3846 tabpage_T *save_curtab;
3847 int need_switch_win;
3848 char_u *varname, *winvarname;
3849 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003850 tabpage_T *tp = NULL;
3851
3852 if (check_secure())
3853 return;
3854
3855 if (off == 1)
3856 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3857 else
3858 tp = curtab;
3859 win = find_win_by_nr(&argvars[off], tp);
3860 varname = tv_get_string_chk(&argvars[off + 1]);
3861 varp = &argvars[off + 2];
3862
3863 if (win != NULL && varname != NULL && varp != NULL)
3864 {
3865 need_switch_win = !(tp == curtab && win == curwin);
3866 if (!need_switch_win
3867 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3868 {
3869 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003870 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003871 else
3872 {
3873 winvarname = alloc(STRLEN(varname) + 3);
3874 if (winvarname != NULL)
3875 {
3876 STRCPY(winvarname, "w:");
3877 STRCPY(winvarname + 2, varname);
3878 set_var(winvarname, varp, TRUE);
3879 vim_free(winvarname);
3880 }
3881 }
3882 }
3883 if (need_switch_win)
3884 restore_win(save_curwin, save_curtab, TRUE);
3885 }
3886}
3887
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003888/*
3889 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3890 * v:option_type, and v:option_command.
3891 */
3892 void
3893reset_v_option_vars(void)
3894{
3895 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3896 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3897 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3898 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3899 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3900 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3901}
3902
3903/*
3904 * Add an assert error to v:errors.
3905 */
3906 void
3907assert_error(garray_T *gap)
3908{
3909 struct vimvar *vp = &vimvars[VV_ERRORS];
3910
3911 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003912 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003913 set_vim_var_list(VV_ERRORS, list_alloc());
3914 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3915}
3916
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003917 int
3918var_exists(char_u *var)
3919{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003920 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003921 char_u *name;
3922 char_u *tofree;
3923 typval_T tv;
3924 int len = 0;
3925 int n = FALSE;
3926
3927 // get_name_len() takes care of expanding curly braces
3928 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003929 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003930 if (len > 0)
3931 {
3932 if (tofree != NULL)
3933 name = tofree;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003934 n = (eval_variable(name, len, &tv, NULL,
3935 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003936 if (n)
3937 {
3938 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003939 arg = skipwhite(arg);
3940 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003941 if (n)
3942 clear_tv(&tv);
3943 }
3944 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003945 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003946 n = FALSE;
3947
3948 vim_free(tofree);
3949 return n;
3950}
3951
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003952static lval_T *redir_lval = NULL;
3953#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3954static garray_T redir_ga; // only valid when redir_lval is not NULL
3955static char_u *redir_endp = NULL;
3956static char_u *redir_varname = NULL;
3957
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003958 int
3959alloc_redir_lval(void)
3960{
3961 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3962 if (redir_lval == NULL)
3963 return FAIL;
3964 return OK;
3965}
3966
3967 void
3968clear_redir_lval(void)
3969{
3970 VIM_CLEAR(redir_lval);
3971}
3972
3973 void
3974init_redir_ga(void)
3975{
3976 ga_init2(&redir_ga, (int)sizeof(char), 500);
3977}
3978
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003979/*
3980 * Start recording command output to a variable
3981 * When "append" is TRUE append to an existing variable.
3982 * Returns OK if successfully completed the setup. FAIL otherwise.
3983 */
3984 int
3985var_redir_start(char_u *name, int append)
3986{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003987 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003988 typval_T tv;
3989
3990 // Catch a bad name early.
3991 if (!eval_isnamec1(*name))
3992 {
3993 emsg(_(e_invarg));
3994 return FAIL;
3995 }
3996
3997 // Make a copy of the name, it is used in redir_lval until redir ends.
3998 redir_varname = vim_strsave(name);
3999 if (redir_varname == NULL)
4000 return FAIL;
4001
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004002 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004003 {
4004 var_redir_stop();
4005 return FAIL;
4006 }
4007
4008 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004009 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004010
4011 // Parse the variable name (can be a dict or list entry).
4012 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4013 FNE_CHECK_START);
4014 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4015 {
4016 clear_lval(redir_lval);
4017 if (redir_endp != NULL && *redir_endp != NUL)
4018 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004019 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004020 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004021 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004022 redir_endp = NULL; // don't store a value, only cleanup
4023 var_redir_stop();
4024 return FAIL;
4025 }
4026
4027 // check if we can write to the variable: set it to or append an empty
4028 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004029 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004030 tv.v_type = VAR_STRING;
4031 tv.vval.v_string = (char_u *)"";
4032 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004033 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004034 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004035 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004036 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004037 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004038 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004039 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004040 {
4041 redir_endp = NULL; // don't store a value, only cleanup
4042 var_redir_stop();
4043 return FAIL;
4044 }
4045
4046 return OK;
4047}
4048
4049/*
4050 * Append "value[value_len]" to the variable set by var_redir_start().
4051 * The actual appending is postponed until redirection ends, because the value
4052 * appended may in fact be the string we write to, changing it may cause freed
4053 * memory to be used:
4054 * :redir => foo
4055 * :let foo
4056 * :redir END
4057 */
4058 void
4059var_redir_str(char_u *value, int value_len)
4060{
4061 int len;
4062
4063 if (redir_lval == NULL)
4064 return;
4065
4066 if (value_len == -1)
4067 len = (int)STRLEN(value); // Append the entire string
4068 else
4069 len = value_len; // Append only "value_len" characters
4070
4071 if (ga_grow(&redir_ga, len) == OK)
4072 {
4073 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4074 redir_ga.ga_len += len;
4075 }
4076 else
4077 var_redir_stop();
4078}
4079
4080/*
4081 * Stop redirecting command output to a variable.
4082 * Frees the allocated memory.
4083 */
4084 void
4085var_redir_stop(void)
4086{
4087 typval_T tv;
4088
4089 if (EVALCMD_BUSY)
4090 {
4091 redir_lval = NULL;
4092 return;
4093 }
4094
4095 if (redir_lval != NULL)
4096 {
4097 // If there was no error: assign the text to the variable.
4098 if (redir_endp != NULL)
4099 {
4100 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4101 tv.v_type = VAR_STRING;
4102 tv.vval.v_string = redir_ga.ga_data;
4103 // Call get_lval() again, if it's inside a Dict or List it may
4104 // have changed.
4105 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4106 FALSE, FALSE, 0, FNE_CHECK_START);
4107 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004109 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004110 clear_lval(redir_lval);
4111 }
4112
4113 // free the collected output
4114 VIM_CLEAR(redir_ga.ga_data);
4115
4116 VIM_CLEAR(redir_lval);
4117 }
4118 VIM_CLEAR(redir_varname);
4119}
4120
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004121/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004122 * Get the collected redirected text and clear redir_ga.
4123 */
4124 char_u *
4125get_clear_redir_ga(void)
4126{
4127 char_u *res;
4128
4129 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4130 res = redir_ga.ga_data;
4131 redir_ga.ga_data = NULL;
4132 return res;
4133}
4134
4135/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004136 * "gettabvar()" function
4137 */
4138 void
4139f_gettabvar(typval_T *argvars, typval_T *rettv)
4140{
4141 win_T *oldcurwin;
4142 tabpage_T *tp, *oldtabpage;
4143 dictitem_T *v;
4144 char_u *varname;
4145 int done = FALSE;
4146
4147 rettv->v_type = VAR_STRING;
4148 rettv->vval.v_string = NULL;
4149
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004150 if (in_vim9script()
4151 && (check_for_number_arg(argvars, 0) == FAIL
4152 || check_for_string_arg(argvars, 1) == FAIL))
4153 return;
4154
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004155 varname = tv_get_string_chk(&argvars[1]);
4156 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4157 if (tp != NULL && varname != NULL)
4158 {
4159 // Set tp to be our tabpage, temporarily. Also set the window to the
4160 // first window in the tabpage, otherwise the window is not valid.
4161 if (switch_win(&oldcurwin, &oldtabpage,
4162 tp == curtab || tp->tp_firstwin == NULL ? firstwin
4163 : tp->tp_firstwin, tp, TRUE) == OK)
4164 {
4165 // look up the variable
4166 // Let gettabvar({nr}, "") return the "t:" dictionary.
4167 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
4168 if (v != NULL)
4169 {
4170 copy_tv(&v->di_tv, rettv);
4171 done = TRUE;
4172 }
4173 }
4174
4175 // restore previous notion of curwin
4176 restore_win(oldcurwin, oldtabpage, TRUE);
4177 }
4178
4179 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4180 copy_tv(&argvars[2], rettv);
4181}
4182
4183/*
4184 * "gettabwinvar()" function
4185 */
4186 void
4187f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4188{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004189 if (in_vim9script()
4190 && (check_for_number_arg(argvars, 0) == FAIL
4191 || check_for_number_arg(argvars, 1) == FAIL
4192 || check_for_string_arg(argvars, 2) == FAIL))
4193 return;
4194
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004195 getwinvar(argvars, rettv, 1);
4196}
4197
4198/*
4199 * "getwinvar()" function
4200 */
4201 void
4202f_getwinvar(typval_T *argvars, typval_T *rettv)
4203{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004204 if (in_vim9script()
4205 && (check_for_number_arg(argvars, 0) == FAIL
4206 || check_for_string_arg(argvars, 1) == FAIL))
4207 return;
4208
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004209 getwinvar(argvars, rettv, 0);
4210}
4211
4212/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004213 * "getbufvar()" function
4214 */
4215 void
4216f_getbufvar(typval_T *argvars, typval_T *rettv)
4217{
4218 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004219 char_u *varname;
4220 dictitem_T *v;
4221 int done = FALSE;
4222
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004223 if (in_vim9script()
4224 && (check_for_buffer_arg(argvars, 0) == FAIL
4225 || check_for_string_arg(argvars, 1) == FAIL))
4226 return;
4227
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004228 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004229 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004230
4231 rettv->v_type = VAR_STRING;
4232 rettv->vval.v_string = NULL;
4233
4234 if (buf != NULL && varname != NULL)
4235 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004236 if (*varname == '&')
4237 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004238 buf_T *save_curbuf = curbuf;
4239
4240 // set curbuf to be our buf, temporarily
4241 curbuf = buf;
4242
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004243 if (varname[1] == NUL)
4244 {
4245 // get all buffer-local options in a dict
4246 dict_T *opts = get_winbuf_options(TRUE);
4247
4248 if (opts != NULL)
4249 {
4250 rettv_dict_set(rettv, opts);
4251 done = TRUE;
4252 }
4253 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004254 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004255 // buffer-local-option
4256 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02004257
4258 // restore previous notion of curbuf
4259 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004260 }
4261 else
4262 {
4263 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02004264 if (*varname == NUL)
4265 // Let getbufvar({nr}, "") return the "b:" dictionary.
4266 v = &buf->b_bufvar;
4267 else
4268 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
4269 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004270 if (v != NULL)
4271 {
4272 copy_tv(&v->di_tv, rettv);
4273 done = TRUE;
4274 }
4275 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004276 }
4277
4278 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4279 // use the default value
4280 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004281}
4282
4283/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004284 * "settabvar()" function
4285 */
4286 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004287f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004288{
4289 tabpage_T *save_curtab;
4290 tabpage_T *tp;
4291 char_u *varname, *tabvarname;
4292 typval_T *varp;
4293
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004294 if (check_secure())
4295 return;
4296
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004297 if (in_vim9script()
4298 && (check_for_number_arg(argvars, 0) == FAIL
4299 || check_for_string_arg(argvars, 1) == FAIL))
4300 return;
4301
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004302 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4303 varname = tv_get_string_chk(&argvars[1]);
4304 varp = &argvars[2];
4305
4306 if (varname != NULL && varp != NULL && tp != NULL)
4307 {
4308 save_curtab = curtab;
4309 goto_tabpage_tp(tp, FALSE, FALSE);
4310
4311 tabvarname = alloc(STRLEN(varname) + 3);
4312 if (tabvarname != NULL)
4313 {
4314 STRCPY(tabvarname, "t:");
4315 STRCPY(tabvarname + 2, varname);
4316 set_var(tabvarname, varp, TRUE);
4317 vim_free(tabvarname);
4318 }
4319
4320 // Restore current tabpage
4321 if (valid_tabpage(save_curtab))
4322 goto_tabpage_tp(save_curtab, FALSE, FALSE);
4323 }
4324}
4325
4326/*
4327 * "settabwinvar()" function
4328 */
4329 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004330f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004331{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004332 if (in_vim9script()
4333 && (check_for_number_arg(argvars, 0) == FAIL
4334 || check_for_number_arg(argvars, 1) == FAIL
4335 || check_for_string_arg(argvars, 2) == FAIL))
4336 return;
4337
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004338 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004339}
4340
4341/*
4342 * "setwinvar()" function
4343 */
4344 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004345f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004346{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004347 if (in_vim9script()
4348 && (check_for_number_arg(argvars, 0) == FAIL
4349 || check_for_string_arg(argvars, 1) == FAIL))
4350 return;
4351
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004352 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004353}
4354
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004355/*
4356 * "setbufvar()" function
4357 */
4358 void
4359f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4360{
4361 buf_T *buf;
4362 char_u *varname, *bufvarname;
4363 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004364
4365 if (check_secure())
4366 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004367
4368 if (in_vim9script()
4369 && (check_for_buffer_arg(argvars, 0) == FAIL
4370 || check_for_string_arg(argvars, 1) == FAIL))
4371 return;
4372
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004373 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004374 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004375 varp = &argvars[2];
4376
4377 if (buf != NULL && varname != NULL && varp != NULL)
4378 {
4379 if (*varname == '&')
4380 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004381 aco_save_T aco;
4382
4383 // set curbuf to be our buf, temporarily
4384 aucmd_prepbuf(&aco, buf);
4385
Bram Moolenaar191929b2020-08-19 21:20:49 +02004386 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004387
4388 // reset notion of buffer
4389 aucmd_restbuf(&aco);
4390 }
4391 else
4392 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004393 bufvarname = alloc(STRLEN(varname) + 3);
4394 if (bufvarname != NULL)
4395 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004396 buf_T *save_curbuf = curbuf;
4397
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004398 curbuf = buf;
4399 STRCPY(bufvarname, "b:");
4400 STRCPY(bufvarname + 2, varname);
4401 set_var(bufvarname, varp, TRUE);
4402 vim_free(bufvarname);
4403 curbuf = save_curbuf;
4404 }
4405 }
4406 }
4407}
4408
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004409/*
4410 * Get a callback from "arg". It can be a Funcref or a function name.
4411 * When "arg" is zero return an empty string.
4412 * "cb_name" is not allocated.
4413 * "cb_name" is set to NULL for an invalid argument.
4414 */
4415 callback_T
4416get_callback(typval_T *arg)
4417{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004418 callback_T res;
4419 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004420
4421 res.cb_free_name = FALSE;
4422 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4423 {
4424 res.cb_partial = arg->vval.v_partial;
4425 ++res.cb_partial->pt_refcount;
4426 res.cb_name = partial_name(res.cb_partial);
4427 }
4428 else
4429 {
4430 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004431 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4432 && isdigit(*arg->vval.v_string))
4433 r = FAIL;
4434 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004435 {
4436 // Note that we don't make a copy of the string.
4437 res.cb_name = arg->vval.v_string;
4438 func_ref(res.cb_name);
4439 }
4440 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004441 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004442 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004443 r = FAIL;
4444
4445 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004446 {
4447 emsg(_("E921: Invalid callback argument"));
4448 res.cb_name = NULL;
4449 }
4450 }
4451 return res;
4452}
4453
4454/*
4455 * Copy a callback into a typval_T.
4456 */
4457 void
4458put_callback(callback_T *cb, typval_T *tv)
4459{
4460 if (cb->cb_partial != NULL)
4461 {
4462 tv->v_type = VAR_PARTIAL;
4463 tv->vval.v_partial = cb->cb_partial;
4464 ++tv->vval.v_partial->pt_refcount;
4465 }
4466 else
4467 {
4468 tv->v_type = VAR_FUNC;
4469 tv->vval.v_string = vim_strsave(cb->cb_name);
4470 func_ref(cb->cb_name);
4471 }
4472}
4473
4474/*
4475 * Make a copy of "src" into "dest", allocating the function name if needed,
4476 * without incrementing the refcount.
4477 */
4478 void
4479set_callback(callback_T *dest, callback_T *src)
4480{
4481 if (src->cb_partial == NULL)
4482 {
4483 // just a function name, make a copy
4484 dest->cb_name = vim_strsave(src->cb_name);
4485 dest->cb_free_name = TRUE;
4486 }
4487 else
4488 {
4489 // cb_name is a pointer into cb_partial
4490 dest->cb_name = src->cb_name;
4491 dest->cb_free_name = FALSE;
4492 }
4493 dest->cb_partial = src->cb_partial;
4494}
4495
4496/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004497 * Copy callback from "src" to "dest", incrementing the refcounts.
4498 */
4499 void
4500copy_callback(callback_T *dest, callback_T *src)
4501{
4502 dest->cb_partial = src->cb_partial;
4503 if (dest->cb_partial != NULL)
4504 {
4505 dest->cb_name = src->cb_name;
4506 dest->cb_free_name = FALSE;
4507 ++dest->cb_partial->pt_refcount;
4508 }
4509 else
4510 {
4511 dest->cb_name = vim_strsave(src->cb_name);
4512 dest->cb_free_name = TRUE;
4513 func_ref(src->cb_name);
4514 }
4515}
4516
4517/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004518 * Unref/free "callback" returned by get_callback() or set_callback().
4519 */
4520 void
4521free_callback(callback_T *callback)
4522{
4523 if (callback->cb_partial != NULL)
4524 {
4525 partial_unref(callback->cb_partial);
4526 callback->cb_partial = NULL;
4527 }
4528 else if (callback->cb_name != NULL)
4529 func_unref(callback->cb_name);
4530 if (callback->cb_free_name)
4531 {
4532 vim_free(callback->cb_name);
4533 callback->cb_free_name = FALSE;
4534 }
4535 callback->cb_name = NULL;
4536}
4537
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004538#endif // FEAT_EVAL