blob: b1d7b78c8130b2bcb13b7f4da513166cbfbf43bf [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 Moolenaarc7e44a72020-07-29 21:37:43 +0200884 if (vim9script && (!VIM_ISWHITE(*argend)
885 || !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/*
1292 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1293 * Returns a pointer to the char just after the var name.
1294 * Returns NULL if there is an error.
1295 */
1296 static char_u *
1297ex_let_one(
1298 char_u *arg, // points to variable name
1299 typval_T *tv, // value to assign to variable
1300 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001301 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001302 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001303 char_u *op, // "+", "-", "." or NULL
1304 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001305{
1306 int c1;
1307 char_u *name;
1308 char_u *p;
1309 char_u *arg_end = NULL;
1310 int len;
1311 int opt_flags;
1312 char_u *tofree = NULL;
1313
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001314 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001315 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001316 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1317 {
1318 vim9_declare_error(arg);
1319 return NULL;
1320 }
1321
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001322 // ":let $VAR = expr": Set environment variable.
1323 if (*arg == '$')
1324 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001325 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1326 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001327 {
1328 emsg(_("E996: Cannot lock an environment variable"));
1329 return NULL;
1330 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001331
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001332 // Find the end of the name.
1333 ++arg;
1334 name = arg;
1335 len = get_env_len(&arg);
1336 if (len == 0)
1337 semsg(_(e_invarg2), name - 1);
1338 else
1339 {
1340 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1341 semsg(_(e_letwrong), op);
1342 else if (endchars != NULL
1343 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02001344 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001345 else if (!check_secure())
1346 {
1347 c1 = name[len];
1348 name[len] = NUL;
1349 p = tv_get_string_chk(tv);
1350 if (p != NULL && op != NULL && *op == '.')
1351 {
1352 int mustfree = FALSE;
1353 char_u *s = vim_getenv(name, &mustfree);
1354
1355 if (s != NULL)
1356 {
1357 p = tofree = concat_str(s, p);
1358 if (mustfree)
1359 vim_free(s);
1360 }
1361 }
1362 if (p != NULL)
1363 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001364 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001365 arg_end = arg;
1366 }
1367 name[len] = c1;
1368 vim_free(tofree);
1369 }
1370 }
1371 }
1372
1373 // ":let &option = expr": Set option value.
1374 // ":let &l:option = expr": Set local option value.
1375 // ":let &g:option = expr": Set global option value.
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001376 // ":for &ts in range(8)": Set option value for for loop
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001377 else if (*arg == '&')
1378 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001379 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1380 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001381 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001382 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001383 return NULL;
1384 }
1385 // Find the end of the name.
1386 p = find_option_end(&arg, &opt_flags);
1387 if (p == NULL || (endchars != NULL
1388 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaar108010a2021-06-27 22:03:33 +02001389 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001390 else
1391 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001392 long n = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001393 getoption_T opt_type;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001394 long numval;
1395 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001396 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001397 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001398
1399 c1 = *p;
1400 *p = NUL;
1401
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001402 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001403 if ((opt_type == gov_bool
1404 || opt_type == gov_number
1405 || opt_type == gov_hidden_bool
1406 || opt_type == gov_hidden_number)
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001407 && (tv->v_type != VAR_STRING || !in_vim9script()))
Bram Moolenaar0ea04402021-01-04 13:37:54 +01001408 {
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 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001416
1417 // Avoid setting a string option to the text "v:false" or similar.
1418 // In Vim9 script also don't convert a number to string.
1419 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1420 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1421 s = tv_get_string_chk(tv);
1422
1423 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001424 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001425 if (((opt_type == gov_bool || opt_type == gov_number)
1426 && *op == '.')
1427 || (opt_type == gov_string && *op != '.'))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001428 {
1429 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001430 failed = TRUE; // don't set the value
1431
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001432 }
1433 else
1434 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001435 // number, in legacy script also bool
1436 if (opt_type == gov_number
1437 || (opt_type == gov_bool && !in_vim9script()))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001438 {
1439 switch (*op)
1440 {
1441 case '+': n = numval + n; break;
1442 case '-': n = numval - n; break;
1443 case '*': n = numval * n; break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001444 case '/': n = (long)num_divide(numval, n,
1445 &failed); break;
1446 case '%': n = (long)num_modulus(numval, n,
1447 &failed); break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001448 }
Bram Moolenaara42e6e02021-06-10 18:43:25 +02001449 s = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001450 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001451 else if (opt_type == gov_string
1452 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001453 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001454 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001455 s = concat_str(stringval, s);
1456 vim_free(stringval);
1457 stringval = s;
1458 }
1459 }
1460 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001461
1462 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001463 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001464 if (opt_type != gov_string || s != NULL)
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001465 {
1466 set_option_value(arg, n, s, opt_flags);
1467 arg_end = p;
1468 }
1469 else
1470 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001471 }
1472 *p = c1;
1473 vim_free(stringval);
1474 }
1475 }
1476
1477 // ":let @r = expr": Set register contents.
1478 else if (*arg == '@')
1479 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001480 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1481 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001482 {
1483 emsg(_("E996: Cannot lock a register"));
1484 return NULL;
1485 }
1486 ++arg;
1487 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1488 semsg(_(e_letwrong), op);
1489 else if (endchars != NULL
1490 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02001491 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001492 else
1493 {
1494 char_u *ptofree = NULL;
1495 char_u *s;
1496
1497 p = tv_get_string_chk(tv);
1498 if (p != NULL && op != NULL && *op == '.')
1499 {
1500 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1501 if (s != NULL)
1502 {
1503 p = ptofree = concat_str(s, p);
1504 vim_free(s);
1505 }
1506 }
1507 if (p != NULL)
1508 {
1509 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1510 arg_end = arg + 1;
1511 }
1512 vim_free(ptofree);
1513 }
1514 }
1515
1516 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001517 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001518 // ":let {expr} = expr": Idem, name made with curly braces
1519 else if (eval_isnamec1(*arg) || *arg == '{')
1520 {
1521 lval_T lv;
1522
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001523 p = get_lval(arg, tv, &lv, FALSE, FALSE,
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001524 (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1525 ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001526 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001527 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 if (endchars != NULL && vim_strchr(endchars,
1529 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02001530 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001531 else
1532 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001533 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001534 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001535 }
1536 }
1537 clear_lval(&lv);
1538 }
1539
1540 else
1541 semsg(_(e_invarg2), arg);
1542
1543 return arg_end;
1544}
1545
1546/*
1547 * ":unlet[!] var1 ... " command.
1548 */
1549 void
1550ex_unlet(exarg_T *eap)
1551{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001552 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001553}
1554
1555/*
1556 * ":lockvar" and ":unlockvar" commands
1557 */
1558 void
1559ex_lockvar(exarg_T *eap)
1560{
1561 char_u *arg = eap->arg;
1562 int deep = 2;
1563
1564 if (eap->forceit)
1565 deep = -1;
1566 else if (vim_isdigit(*arg))
1567 {
1568 deep = getdigits(&arg);
1569 arg = skipwhite(arg);
1570 }
1571
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001572 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001573}
1574
1575/*
1576 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001577 * Also used for Vim9 script. "callback" is invoked as:
1578 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001579 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001580 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001581ex_unletlock(
1582 exarg_T *eap,
1583 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001584 int deep,
1585 int glv_flags,
1586 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1587 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001588{
1589 char_u *arg = argstart;
1590 char_u *name_end;
1591 int error = FALSE;
1592 lval_T lv;
1593
1594 do
1595 {
1596 if (*arg == '$')
1597 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001598 lv.ll_name = arg;
1599 lv.ll_tv = NULL;
1600 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001601 if (get_env_len(&arg) == 0)
1602 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001603 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001604 return;
1605 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001606 if (!error && !eap->skip
1607 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1608 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001609 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001610 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001611 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001612 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001613 // Parse the name and find the end.
1614 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001615 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001616 if (lv.ll_name == NULL)
1617 error = TRUE; // error but continue parsing
1618 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1619 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001620 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001621 if (name_end != NULL)
1622 {
1623 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001624 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001625 }
1626 if (!(eap->skip || error))
1627 clear_lval(&lv);
1628 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001629 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001630
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001631 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001632 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001633 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001634
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001635 if (!eap->skip)
1636 clear_lval(&lv);
1637 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001638
1639 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001640 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001641
Bram Moolenaar63b91732021-08-05 20:40:03 +02001642 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001643}
1644
1645 static int
1646do_unlet_var(
1647 lval_T *lp,
1648 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001649 exarg_T *eap,
1650 int deep UNUSED,
1651 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001652{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001653 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001654 int ret = OK;
1655 int cc;
1656
1657 if (lp->ll_tv == NULL)
1658 {
1659 cc = *name_end;
1660 *name_end = NUL;
1661
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001662 // Environment variable, normal name or expanded name.
1663 if (*lp->ll_name == '$')
1664 vim_unsetenv(lp->ll_name + 1);
1665 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001666 ret = FAIL;
1667 *name_end = cc;
1668 }
1669 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001670 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001671 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001672 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001673 return FAIL;
1674 else if (lp->ll_range)
1675 {
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001676 if (list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_name, lp->ll_n1,
1677 !lp->ll_empty2, lp->ll_n2) == FAIL)
1678 return FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001679 }
1680 else
1681 {
1682 if (lp->ll_list != NULL)
1683 // unlet a List item.
1684 listitem_remove(lp->ll_list, lp->ll_li);
1685 else
1686 // unlet a Dictionary item.
1687 dictitem_remove(lp->ll_dict, lp->ll_di);
1688 }
1689
1690 return ret;
1691}
1692
1693/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001694 * Unlet one item or a range of items from a list.
1695 * Return OK or FAIL.
1696 */
1697 int
1698list_unlet_range(
1699 list_T *l,
1700 listitem_T *li_first,
1701 char_u *name,
1702 long n1_arg,
1703 int has_n2,
1704 long n2)
1705{
1706 listitem_T *li = li_first;
1707 int n1 = n1_arg;
1708
1709 while (li != NULL && (!has_n2 || n2 >= n1))
1710 {
1711 if (value_check_lock(li->li_tv.v_lock, name, FALSE))
1712 return FAIL;
1713 li = li->li_next;
1714 ++n1;
1715 }
1716
1717 // Delete a range of List items.
1718 li = li_first;
1719 n1 = n1_arg;
1720 while (li != NULL && (!has_n2 || n2 >= n1))
1721 {
1722 listitem_T *next = li->li_next;
1723
1724 listitem_remove(l, li);
1725 li = next;
1726 ++n1;
1727 }
1728 return OK;
1729}
1730/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001731 * "unlet" a variable. Return OK if it existed, FAIL if not.
1732 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1733 */
1734 int
1735do_unlet(char_u *name, int forceit)
1736{
1737 hashtab_T *ht;
1738 hashitem_T *hi;
1739 char_u *varname;
1740 dict_T *d;
1741 dictitem_T *di;
1742
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001743 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001744 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001745 return FAIL;
1746
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001747 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001748
1749 // can't :unlet a script variable in Vim9 script from a function
1750 if (ht == get_script_local_ht()
1751 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1752 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1753 == SCRIPT_VERSION_VIM9
1754 && check_vim9_unlet(name) == FAIL)
1755 return FAIL;
1756
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001757 if (ht != NULL && *varname != NUL)
1758 {
1759 d = get_current_funccal_dict(ht);
1760 if (d == NULL)
1761 {
1762 if (ht == &globvarht)
1763 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001764 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001765 d = &vimvardict;
1766 else
1767 {
1768 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1769 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1770 }
1771 if (d == NULL)
1772 {
1773 internal_error("do_unlet()");
1774 return FAIL;
1775 }
1776 }
1777 hi = hash_find(ht, varname);
1778 if (HASHITEM_EMPTY(hi))
1779 hi = find_hi_in_scoped_ht(name, &ht);
1780 if (hi != NULL && !HASHITEM_EMPTY(hi))
1781 {
1782 di = HI2DI(hi);
1783 if (var_check_fixed(di->di_flags, name, FALSE)
1784 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001785 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001786 return FAIL;
1787
1788 delete_var(ht, hi);
1789 return OK;
1790 }
1791 }
1792 if (forceit)
1793 return OK;
1794 semsg(_("E108: No such variable: \"%s\""), name);
1795 return FAIL;
1796}
1797
1798/*
1799 * Lock or unlock variable indicated by "lp".
1800 * "deep" is the levels to go (-1 for unlimited);
1801 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1802 */
1803 static int
1804do_lock_var(
1805 lval_T *lp,
1806 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001807 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001808 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001809 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001810{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001811 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001812 int ret = OK;
1813 int cc;
1814 dictitem_T *di;
1815
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001816 if (lp->ll_tv == NULL)
1817 {
1818 cc = *name_end;
1819 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001820 if (*lp->ll_name == '$')
1821 {
1822 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001823 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001824 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001825 else
1826 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001827 // Normal name or expanded name.
1828 di = find_var(lp->ll_name, NULL, TRUE);
1829 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001830 {
1831 if (in_vim9script())
1832 semsg(_(e_cannot_find_variable_to_unlock_str),
1833 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001834 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001835 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001836 else if ((di->di_flags & DI_FLAGS_FIX)
1837 && di->di_tv.v_type != VAR_DICT
1838 && di->di_tv.v_type != VAR_LIST)
1839 {
1840 // For historic reasons this error is not given for a list or
1841 // dict. E.g., the b: dict could be locked/unlocked.
1842 semsg(_(e_lock_unlock), lp->ll_name);
1843 ret = FAIL;
1844 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001845 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001846 {
1847 if (lock)
1848 di->di_flags |= DI_FLAGS_LOCK;
1849 else
1850 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001851 if (deep != 0)
1852 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001853 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001854 }
1855 *name_end = cc;
1856 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001857 else if (deep == 0)
1858 {
1859 // nothing to do
1860 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001861 else if (lp->ll_range)
1862 {
1863 listitem_T *li = lp->ll_li;
1864
1865 // (un)lock a range of List items.
1866 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1867 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001868 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001869 li = li->li_next;
1870 ++lp->ll_n1;
1871 }
1872 }
1873 else if (lp->ll_list != NULL)
1874 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001875 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001876 else
1877 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001878 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001879
1880 return ret;
1881}
1882
1883/*
1884 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001885 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1886 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001887 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001888 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001889item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001890{
1891 static int recurse = 0;
1892 list_T *l;
1893 listitem_T *li;
1894 dict_T *d;
1895 blob_T *b;
1896 hashitem_T *hi;
1897 int todo;
1898
1899 if (recurse >= DICT_MAXNEST)
1900 {
1901 emsg(_("E743: variable nested too deep for (un)lock"));
1902 return;
1903 }
1904 if (deep == 0)
1905 return;
1906 ++recurse;
1907
1908 // lock/unlock the item itself
1909 if (lock)
1910 tv->v_lock |= VAR_LOCKED;
1911 else
1912 tv->v_lock &= ~VAR_LOCKED;
1913
1914 switch (tv->v_type)
1915 {
1916 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001917 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001918 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001919 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001920 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001921 case VAR_STRING:
1922 case VAR_FUNC:
1923 case VAR_PARTIAL:
1924 case VAR_FLOAT:
1925 case VAR_SPECIAL:
1926 case VAR_JOB:
1927 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001928 case VAR_INSTR:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001929 break;
1930
1931 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001932 if ((b = tv->vval.v_blob) != NULL
1933 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001934 {
1935 if (lock)
1936 b->bv_lock |= VAR_LOCKED;
1937 else
1938 b->bv_lock &= ~VAR_LOCKED;
1939 }
1940 break;
1941 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001942 if ((l = tv->vval.v_list) != NULL
1943 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001944 {
1945 if (lock)
1946 l->lv_lock |= VAR_LOCKED;
1947 else
1948 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001949 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001950 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001951 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001952 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001953 }
1954 break;
1955 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001956 if ((d = tv->vval.v_dict) != NULL
1957 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001958 {
1959 if (lock)
1960 d->dv_lock |= VAR_LOCKED;
1961 else
1962 d->dv_lock &= ~VAR_LOCKED;
1963 if (deep < 0 || deep > 1)
1964 {
1965 // recursive: lock/unlock the items the List contains
1966 todo = (int)d->dv_hashtab.ht_used;
1967 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1968 {
1969 if (!HASHITEM_EMPTY(hi))
1970 {
1971 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001972 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1973 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001974 }
1975 }
1976 }
1977 }
1978 }
1979 --recurse;
1980}
1981
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001982#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1983/*
1984 * Delete all "menutrans_" variables.
1985 */
1986 void
1987del_menutrans_vars(void)
1988{
1989 hashitem_T *hi;
1990 int todo;
1991
1992 hash_lock(&globvarht);
1993 todo = (int)globvarht.ht_used;
1994 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1995 {
1996 if (!HASHITEM_EMPTY(hi))
1997 {
1998 --todo;
1999 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2000 delete_var(&globvarht, hi);
2001 }
2002 }
2003 hash_unlock(&globvarht);
2004}
2005#endif
2006
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002007/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002008 * Local string buffer for the next two functions to store a variable name
2009 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2010 * get_user_var_name().
2011 */
2012
2013static char_u *varnamebuf = NULL;
2014static int varnamebuflen = 0;
2015
2016/*
2017 * Function to concatenate a prefix and a variable name.
2018 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002019 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002020cat_prefix_varname(int prefix, char_u *name)
2021{
2022 int len;
2023
2024 len = (int)STRLEN(name) + 3;
2025 if (len > varnamebuflen)
2026 {
2027 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002028 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002029 varnamebuf = alloc(len);
2030 if (varnamebuf == NULL)
2031 {
2032 varnamebuflen = 0;
2033 return NULL;
2034 }
2035 varnamebuflen = len;
2036 }
2037 *varnamebuf = prefix;
2038 varnamebuf[1] = ':';
2039 STRCPY(varnamebuf + 2, name);
2040 return varnamebuf;
2041}
2042
2043/*
2044 * Function given to ExpandGeneric() to obtain the list of user defined
2045 * (global/buffer/window/built-in) variable names.
2046 */
2047 char_u *
2048get_user_var_name(expand_T *xp, int idx)
2049{
2050 static long_u gdone;
2051 static long_u bdone;
2052 static long_u wdone;
2053 static long_u tdone;
2054 static int vidx;
2055 static hashitem_T *hi;
2056 hashtab_T *ht;
2057
2058 if (idx == 0)
2059 {
2060 gdone = bdone = wdone = vidx = 0;
2061 tdone = 0;
2062 }
2063
2064 // Global variables
2065 if (gdone < globvarht.ht_used)
2066 {
2067 if (gdone++ == 0)
2068 hi = globvarht.ht_array;
2069 else
2070 ++hi;
2071 while (HASHITEM_EMPTY(hi))
2072 ++hi;
2073 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2074 return cat_prefix_varname('g', hi->hi_key);
2075 return hi->hi_key;
2076 }
2077
2078 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002079 ht =
2080#ifdef FEAT_CMDWIN
2081 // In cmdwin, the alternative buffer should be used.
mityua1198122021-11-20 19:13:39 +00002082 is_in_cmdwin() ? &prevwin->w_buffer->b_vars->dv_hashtab :
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002083#endif
2084 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002085 if (bdone < ht->ht_used)
2086 {
2087 if (bdone++ == 0)
2088 hi = ht->ht_array;
2089 else
2090 ++hi;
2091 while (HASHITEM_EMPTY(hi))
2092 ++hi;
2093 return cat_prefix_varname('b', hi->hi_key);
2094 }
2095
2096 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002097 ht =
2098#ifdef FEAT_CMDWIN
2099 // In cmdwin, the alternative window should be used.
mityua1198122021-11-20 19:13:39 +00002100 is_in_cmdwin() ? &prevwin->w_vars->dv_hashtab :
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002101#endif
2102 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002103 if (wdone < ht->ht_used)
2104 {
2105 if (wdone++ == 0)
2106 hi = ht->ht_array;
2107 else
2108 ++hi;
2109 while (HASHITEM_EMPTY(hi))
2110 ++hi;
2111 return cat_prefix_varname('w', hi->hi_key);
2112 }
2113
2114 // t: variables
2115 ht = &curtab->tp_vars->dv_hashtab;
2116 if (tdone < ht->ht_used)
2117 {
2118 if (tdone++ == 0)
2119 hi = ht->ht_array;
2120 else
2121 ++hi;
2122 while (HASHITEM_EMPTY(hi))
2123 ++hi;
2124 return cat_prefix_varname('t', hi->hi_key);
2125 }
2126
2127 // v: variables
2128 if (vidx < VV_LEN)
2129 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2130
2131 VIM_CLEAR(varnamebuf);
2132 varnamebuflen = 0;
2133 return NULL;
2134}
2135
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002136 char *
2137get_var_special_name(int nr)
2138{
2139 switch (nr)
2140 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002141 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2142 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002143 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002144 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002145 }
2146 internal_error("get_var_special_name()");
2147 return "42";
2148}
2149
2150/*
2151 * Returns the global variable dictionary
2152 */
2153 dict_T *
2154get_globvar_dict(void)
2155{
2156 return &globvardict;
2157}
2158
2159/*
2160 * Returns the global variable hash table
2161 */
2162 hashtab_T *
2163get_globvar_ht(void)
2164{
2165 return &globvarht;
2166}
2167
2168/*
2169 * Returns the v: variable dictionary
2170 */
2171 dict_T *
2172get_vimvar_dict(void)
2173{
2174 return &vimvardict;
2175}
2176
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002177/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002178 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002179 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002180 */
2181 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002182find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002183{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002184 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2185 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186
2187 if (di == NULL)
2188 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002189 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002190 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2191 return (int)(vv - vimvars);
2192}
2193
2194
2195/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002196 * Set type of v: variable to "type".
2197 */
2198 void
2199set_vim_var_type(int idx, vartype_T type)
2200{
2201 vimvars[idx].vv_type = type;
2202}
2203
2204/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002205 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002206 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002207 */
2208 void
2209set_vim_var_nr(int idx, varnumber_T val)
2210{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002211 vimvars[idx].vv_nr = val;
2212}
2213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002214 char *
2215get_vim_var_name(int idx)
2216{
2217 return vimvars[idx].vv_name;
2218}
2219
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002220/*
2221 * Get typval_T v: variable value.
2222 */
2223 typval_T *
2224get_vim_var_tv(int idx)
2225{
2226 return &vimvars[idx].vv_tv;
2227}
2228
2229/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002230 * Set v: variable to "tv". Only accepts the same type.
2231 * Takes over the value of "tv".
2232 */
2233 int
2234set_vim_var_tv(int idx, typval_T *tv)
2235{
2236 if (vimvars[idx].vv_type != tv->v_type)
2237 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002238 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002239 clear_tv(tv);
2240 return FAIL;
2241 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002242 // VV_RO is also checked when compiling, but let's check here as well.
2243 if (vimvars[idx].vv_flags & VV_RO)
2244 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002245 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002246 return FAIL;
2247 }
2248 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2249 {
2250 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2251 return FAIL;
2252 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002253 clear_tv(&vimvars[idx].vv_di.di_tv);
2254 vimvars[idx].vv_di.di_tv = *tv;
2255 return OK;
2256}
2257
2258/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002259 * Get number v: variable value.
2260 */
2261 varnumber_T
2262get_vim_var_nr(int idx)
2263{
2264 return vimvars[idx].vv_nr;
2265}
2266
2267/*
2268 * Get string v: variable value. Uses a static buffer, can only be used once.
2269 * If the String variable has never been set, return an empty string.
2270 * Never returns NULL;
2271 */
2272 char_u *
2273get_vim_var_str(int idx)
2274{
2275 return tv_get_string(&vimvars[idx].vv_tv);
2276}
2277
2278/*
2279 * Get List v: variable value. Caller must take care of reference count when
2280 * needed.
2281 */
2282 list_T *
2283get_vim_var_list(int idx)
2284{
2285 return vimvars[idx].vv_list;
2286}
2287
2288/*
2289 * Get Dict v: variable value. Caller must take care of reference count when
2290 * needed.
2291 */
2292 dict_T *
2293get_vim_var_dict(int idx)
2294{
2295 return vimvars[idx].vv_dict;
2296}
2297
2298/*
2299 * Set v:char to character "c".
2300 */
2301 void
2302set_vim_var_char(int c)
2303{
2304 char_u buf[MB_MAXBYTES + 1];
2305
2306 if (has_mbyte)
2307 buf[(*mb_char2bytes)(c, buf)] = NUL;
2308 else
2309 {
2310 buf[0] = c;
2311 buf[1] = NUL;
2312 }
2313 set_vim_var_string(VV_CHAR, buf, -1);
2314}
2315
2316/*
2317 * Set v:count to "count" and v:count1 to "count1".
2318 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2319 */
2320 void
2321set_vcount(
2322 long count,
2323 long count1,
2324 int set_prevcount)
2325{
2326 if (set_prevcount)
2327 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2328 vimvars[VV_COUNT].vv_nr = count;
2329 vimvars[VV_COUNT1].vv_nr = count1;
2330}
2331
2332/*
2333 * Save variables that might be changed as a side effect. Used when executing
2334 * a timer callback.
2335 */
2336 void
2337save_vimvars(vimvars_save_T *vvsave)
2338{
2339 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2340 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2341 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2342}
2343
2344/*
2345 * Restore variables saved by save_vimvars().
2346 */
2347 void
2348restore_vimvars(vimvars_save_T *vvsave)
2349{
2350 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2351 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2352 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2353}
2354
2355/*
2356 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2357 * value.
2358 */
2359 void
2360set_vim_var_string(
2361 int idx,
2362 char_u *val,
2363 int len) // length of "val" to use or -1 (whole string)
2364{
2365 clear_tv(&vimvars[idx].vv_di.di_tv);
2366 vimvars[idx].vv_type = VAR_STRING;
2367 if (val == NULL)
2368 vimvars[idx].vv_str = NULL;
2369 else if (len == -1)
2370 vimvars[idx].vv_str = vim_strsave(val);
2371 else
2372 vimvars[idx].vv_str = vim_strnsave(val, len);
2373}
2374
2375/*
2376 * Set List v: variable to "val".
2377 */
2378 void
2379set_vim_var_list(int idx, list_T *val)
2380{
2381 clear_tv(&vimvars[idx].vv_di.di_tv);
2382 vimvars[idx].vv_type = VAR_LIST;
2383 vimvars[idx].vv_list = val;
2384 if (val != NULL)
2385 ++val->lv_refcount;
2386}
2387
2388/*
2389 * Set Dictionary v: variable to "val".
2390 */
2391 void
2392set_vim_var_dict(int idx, dict_T *val)
2393{
2394 clear_tv(&vimvars[idx].vv_di.di_tv);
2395 vimvars[idx].vv_type = VAR_DICT;
2396 vimvars[idx].vv_dict = val;
2397 if (val != NULL)
2398 {
2399 ++val->dv_refcount;
2400 dict_set_items_ro(val);
2401 }
2402}
2403
2404/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002405 * Set the v:argv list.
2406 */
2407 void
2408set_argv_var(char **argv, int argc)
2409{
2410 list_T *l = list_alloc();
2411 int i;
2412
2413 if (l == NULL)
2414 getout(1);
2415 l->lv_lock = VAR_FIXED;
2416 for (i = 0; i < argc; ++i)
2417 {
2418 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2419 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002420 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002421 }
2422 set_vim_var_list(VV_ARGV, l);
2423}
2424
2425/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002426 * Reset v:register, taking the 'clipboard' setting into account.
2427 */
2428 void
2429reset_reg_var(void)
2430{
2431 int regname = 0;
2432
2433 // Adjust the register according to 'clipboard', so that when
2434 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2435#ifdef FEAT_CLIPBOARD
2436 adjust_clip_reg(&regname);
2437#endif
2438 set_reg_var(regname);
2439}
2440
2441/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002442 * Set v:register if needed.
2443 */
2444 void
2445set_reg_var(int c)
2446{
2447 char_u regname;
2448
2449 if (c == 0 || c == ' ')
2450 regname = '"';
2451 else
2452 regname = c;
2453 // Avoid free/alloc when the value is already right.
2454 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2455 set_vim_var_string(VV_REG, &regname, 1);
2456}
2457
2458/*
2459 * Get or set v:exception. If "oldval" == NULL, return the current value.
2460 * Otherwise, restore the value to "oldval" and return NULL.
2461 * Must always be called in pairs to save and restore v:exception! Does not
2462 * take care of memory allocations.
2463 */
2464 char_u *
2465v_exception(char_u *oldval)
2466{
2467 if (oldval == NULL)
2468 return vimvars[VV_EXCEPTION].vv_str;
2469
2470 vimvars[VV_EXCEPTION].vv_str = oldval;
2471 return NULL;
2472}
2473
2474/*
2475 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2476 * Otherwise, restore the value to "oldval" and return NULL.
2477 * Must always be called in pairs to save and restore v:throwpoint! Does not
2478 * take care of memory allocations.
2479 */
2480 char_u *
2481v_throwpoint(char_u *oldval)
2482{
2483 if (oldval == NULL)
2484 return vimvars[VV_THROWPOINT].vv_str;
2485
2486 vimvars[VV_THROWPOINT].vv_str = oldval;
2487 return NULL;
2488}
2489
2490/*
2491 * Set v:cmdarg.
2492 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2493 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2494 * Must always be called in pairs!
2495 */
2496 char_u *
2497set_cmdarg(exarg_T *eap, char_u *oldarg)
2498{
2499 char_u *oldval;
2500 char_u *newval;
2501 unsigned len;
2502
2503 oldval = vimvars[VV_CMDARG].vv_str;
2504 if (eap == NULL)
2505 {
2506 vim_free(oldval);
2507 vimvars[VV_CMDARG].vv_str = oldarg;
2508 return NULL;
2509 }
2510
2511 if (eap->force_bin == FORCE_BIN)
2512 len = 6;
2513 else if (eap->force_bin == FORCE_NOBIN)
2514 len = 8;
2515 else
2516 len = 0;
2517
2518 if (eap->read_edit)
2519 len += 7;
2520
2521 if (eap->force_ff != 0)
2522 len += 10; // " ++ff=unix"
2523 if (eap->force_enc != 0)
2524 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2525 if (eap->bad_char != 0)
2526 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2527
2528 newval = alloc(len + 1);
2529 if (newval == NULL)
2530 return NULL;
2531
2532 if (eap->force_bin == FORCE_BIN)
2533 sprintf((char *)newval, " ++bin");
2534 else if (eap->force_bin == FORCE_NOBIN)
2535 sprintf((char *)newval, " ++nobin");
2536 else
2537 *newval = NUL;
2538
2539 if (eap->read_edit)
2540 STRCAT(newval, " ++edit");
2541
2542 if (eap->force_ff != 0)
2543 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2544 eap->force_ff == 'u' ? "unix"
2545 : eap->force_ff == 'd' ? "dos"
2546 : "mac");
2547 if (eap->force_enc != 0)
2548 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2549 eap->cmd + eap->force_enc);
2550 if (eap->bad_char == BAD_KEEP)
2551 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2552 else if (eap->bad_char == BAD_DROP)
2553 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2554 else if (eap->bad_char != 0)
2555 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2556 vimvars[VV_CMDARG].vv_str = newval;
2557 return oldval;
2558}
2559
2560/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002561 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002562 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2563 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002564 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2565 */
2566 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002567eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002568 char_u *name,
2569 int len, // length of "name"
2570 typval_T *rettv, // NULL when only checking existence
2571 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002572 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002573{
2574 int ret = OK;
2575 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002576 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002577 hashtab_T *ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002578 int cc;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002579 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002580
2581 // truncate the name, so that we can use strcmp()
2582 cc = name[len];
2583 name[len] = NUL;
2584
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002585 // Check for local variable when debugging.
2586 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002587 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002588 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002589 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2590
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002591 if (v != NULL)
2592 {
2593 tv = &v->di_tv;
2594 if (dip != NULL)
2595 *dip = v;
2596 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002597 else
2598 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002599 }
2600
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002601 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002603 imported_T *import;
2604 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2605
2606 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002607
2608 // imported variable from another script
2609 if (import != NULL)
2610 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002611 if (import->imp_funcname != NULL)
2612 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002613 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002614 if (rettv != NULL)
2615 {
2616 rettv->v_type = VAR_FUNC;
2617 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2618 }
2619 }
Bram Moolenaara6294952020-12-27 13:39:50 +01002620 else if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002621 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002622 if ((flags & EVAL_VAR_IMPORT) == 0)
2623 {
2624 if (flags & EVAL_VAR_VERBOSE)
2625 emsg(_(e_import_as_name_not_supported_here));
2626 ret = FAIL;
2627 }
2628 else
2629 {
2630 if (rettv != NULL)
2631 {
2632 rettv->v_type = VAR_ANY;
2633 rettv->vval.v_number = import->imp_sid;
2634 }
2635 found = TRUE;
2636 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002637 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002638 else
2639 {
2640 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002641 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002643 tv = sv->sv_tv;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002644 type = sv->sv_type;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002645 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002647 else if (in_vim9script())
2648 {
2649 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2650
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002651 // In Vim9 script we can get a function reference by using the
2652 // function name.
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002653 if (ufunc != NULL)
2654 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002655 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002656 if (rettv != NULL)
2657 {
2658 rettv->v_type = VAR_FUNC;
2659 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002660 if (rettv->vval.v_string != NULL)
2661 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002662 }
2663 }
2664 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 }
2666
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002667 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002668 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002669 if (tv == NULL)
2670 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002671 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002672 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002673 ret = FAIL;
2674 }
2675 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002676 {
Bram Moolenaar11005b02021-07-11 20:59:00 +02002677 if (ht != NULL && ht == get_script_local_ht()
2678 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002679 {
Bram Moolenaarc967d572021-07-08 21:38:50 +02002680 svar_T *sv = find_typval_in_script(tv);
Bram Moolenaarf055d452021-07-08 20:57:24 +02002681
Bram Moolenaarf055d452021-07-08 20:57:24 +02002682 if (sv != NULL)
2683 type = sv->sv_type;
2684 }
2685
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002686 // If a list or dict variable wasn't initialized, do it now.
2687 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2688 {
2689 tv->vval.v_dict = dict_alloc();
2690 if (tv->vval.v_dict != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002691 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002692 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002693 tv->vval.v_dict->dv_type = alloc_type(type);
2694 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002695 }
2696 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2697 {
2698 tv->vval.v_list = list_alloc();
2699 if (tv->vval.v_list != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002700 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002701 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002702 tv->vval.v_list->lv_type = alloc_type(type);
2703 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002704 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002705 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL)
2706 {
2707 tv->vval.v_blob = blob_alloc();
2708 if (tv->vval.v_blob != NULL)
2709 ++tv->vval.v_blob->bv_refcount;
2710 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002711 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002712 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002713 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002714
2715 name[len] = cc;
2716
2717 return ret;
2718}
2719
2720/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002721 * Check if variable "name[len]" is a local variable or an argument.
2722 * If so, "*eval_lavars_used" is set to TRUE.
2723 */
2724 void
2725check_vars(char_u *name, int len)
2726{
2727 int cc;
2728 char_u *varname;
2729 hashtab_T *ht;
2730
2731 if (eval_lavars_used == NULL)
2732 return;
2733
2734 // truncate the name, so that we can use strcmp()
2735 cc = name[len];
2736 name[len] = NUL;
2737
2738 ht = find_var_ht(name, &varname);
2739 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2740 {
2741 if (find_var(name, NULL, TRUE) != NULL)
2742 *eval_lavars_used = TRUE;
2743 }
2744
2745 name[len] = cc;
2746}
2747
2748/*
2749 * Find variable "name" in the list of variables.
2750 * Return a pointer to it if found, NULL if not found.
2751 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002752 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002753 */
2754 dictitem_T *
2755find_var(char_u *name, hashtab_T **htp, int no_autoload)
2756{
2757 char_u *varname;
2758 hashtab_T *ht;
2759 dictitem_T *ret = NULL;
2760
2761 ht = find_var_ht(name, &varname);
2762 if (htp != NULL)
2763 *htp = ht;
2764 if (ht == NULL)
2765 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002766 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002767 if (ret != NULL)
2768 return ret;
2769
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002770 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002771 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002772 if (ret != NULL)
2773 return ret;
2774
2775 // in Vim9 script items without a scope can be script-local
2776 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2777 {
2778 ht = get_script_local_ht();
2779 if (ht != NULL)
2780 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002781 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002782 if (ret != NULL)
2783 {
2784 if (htp != NULL)
2785 *htp = ht;
2786 return ret;
2787 }
2788 }
2789 }
2790
2791 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002792}
2793
2794/*
2795 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002796 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002797 * Returns NULL if not found.
2798 */
2799 dictitem_T *
2800find_var_in_ht(
2801 hashtab_T *ht,
2802 int htname,
2803 char_u *varname,
2804 int no_autoload)
2805{
2806 hashitem_T *hi;
2807
2808 if (*varname == NUL)
2809 {
2810 // Must be something like "s:", otherwise "ht" would be NULL.
2811 switch (htname)
2812 {
2813 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2814 case 'g': return &globvars_var;
2815 case 'v': return &vimvars_var;
2816 case 'b': return &curbuf->b_bufvar;
2817 case 'w': return &curwin->w_winvar;
2818 case 't': return &curtab->tp_winvar;
2819 case 'l': return get_funccal_local_var();
2820 case 'a': return get_funccal_args_var();
2821 }
2822 return NULL;
2823 }
2824
2825 hi = hash_find(ht, varname);
2826 if (HASHITEM_EMPTY(hi))
2827 {
2828 // For global variables we may try auto-loading the script. If it
2829 // worked find the variable again. Don't auto-load a script if it was
2830 // loaded already, otherwise it would be loaded every time when
2831 // checking if a function name is a Funcref variable.
2832 if (ht == &globvarht && !no_autoload)
2833 {
2834 // Note: script_autoload() may make "hi" invalid. It must either
2835 // be obtained again or not used.
2836 if (!script_autoload(varname, FALSE) || aborting())
2837 return NULL;
2838 hi = hash_find(ht, varname);
2839 }
2840 if (HASHITEM_EMPTY(hi))
2841 return NULL;
2842 }
2843 return HI2DI(hi);
2844}
2845
2846/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847 * Get the script-local hashtab. NULL if not in a script context.
2848 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002849 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850get_script_local_ht(void)
2851{
2852 scid_T sid = current_sctx.sc_sid;
2853
Bram Moolenaare3d46852020-08-29 13:39:17 +02002854 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 return &SCRIPT_VARS(sid);
2856 return NULL;
2857}
2858
2859/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002860 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002861 * When "cmd" is TRUE it must look like a command, a function must be followed
2862 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01002863 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01002865 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002866lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01002867 char_u *name,
2868 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002869 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01002870 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871{
2872 hashtab_T *ht = get_script_local_ht();
2873 char_u buffer[30];
2874 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01002875 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002877 int is_global = FALSE;
2878 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002879
2880 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002881 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 if (len < sizeof(buffer) - 1)
2883 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002884 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 vim_strncpy(buffer, name, len);
2886 p = buffer;
2887 }
2888 else
2889 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002890 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002892 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893 }
2894
2895 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002896 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897
2898 // if not script-local, then perhaps imported
Bram Moolenaar709664c2020-12-12 14:33:41 +01002899 if (res == FAIL && find_imported(p, 0, NULL) != NULL)
2900 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 if (p != buffer)
2902 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002903
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002904 // Find a function, so that a following "->" works.
2905 // When used as a command require "(" or "->" to follow, "Cmd" is a user
2906 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002907 if (res != OK)
2908 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002909 p = skipwhite(name + len);
2910
2911 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002912 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002913 // Do not check for an internal function, since it might also be a
2914 // valid command, such as ":split" versus "split()".
2915 // Skip "g:" before a function name.
2916 if (name[0] == 'g' && name[1] == ':')
2917 {
2918 is_global = TRUE;
2919 fname = name + 2;
2920 }
2921 if (find_func(fname, is_global, NULL) != NULL)
2922 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002923 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002924 }
2925
Bram Moolenaar709664c2020-12-12 14:33:41 +01002926 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927}
2928
2929/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002930 * Find the hashtab used for a variable name.
2931 * Return NULL if the name is not valid.
2932 * Set "varname" to the start of name without ':'.
2933 */
2934 hashtab_T *
2935find_var_ht(char_u *name, char_u **varname)
2936{
2937 hashitem_T *hi;
2938 hashtab_T *ht;
2939
2940 if (name[0] == NUL)
2941 return NULL;
2942 if (name[1] != ':')
2943 {
2944 // The name must not start with a colon or #.
2945 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2946 return NULL;
2947 *varname = name;
2948
2949 // "version" is "v:version" in all scopes if scriptversion < 3.
2950 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02002951 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002952 {
2953 hi = hash_find(&compat_hashtab, name);
2954 if (!HASHITEM_EMPTY(hi))
2955 return &compat_hashtab;
2956 }
2957
2958 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 if (ht != NULL)
2960 return ht; // local variable
2961
Bram Moolenaarf0a40692021-06-11 22:05:47 +02002962 // In Vim9 script items at the script level are script-local, except
2963 // for autoload names.
2964 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 {
2966 ht = get_script_local_ht();
2967 if (ht != NULL)
2968 return ht;
2969 }
2970
2971 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002972 }
2973 *varname = name + 2;
2974 if (*name == 'g') // global variable
2975 return &globvarht;
2976 // There must be no ':' or '#' in the rest of the name, unless g: is used
2977 if (vim_strchr(name + 2, ':') != NULL
2978 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2979 return NULL;
2980 if (*name == 'b') // buffer variable
2981 return &curbuf->b_vars->dv_hashtab;
2982 if (*name == 'w') // window variable
2983 return &curwin->w_vars->dv_hashtab;
2984 if (*name == 't') // tab page variable
2985 return &curtab->tp_vars->dv_hashtab;
2986 if (*name == 'v') // v: variable
2987 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002988 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002989 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002991 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 if (*name == 'a') // a: function argument
2993 return get_funccal_args_ht();
2994 if (*name == 'l') // l: local function variable
2995 return get_funccal_local_ht();
2996 }
2997 if (*name == 's') // script variable
2998 {
2999 ht = get_script_local_ht();
3000 if (ht != NULL)
3001 return ht;
3002 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003003 return NULL;
3004}
3005
3006/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003007 * Get the string value of a (global/local) variable.
3008 * Note: see tv_get_string() for how long the pointer remains valid.
3009 * Returns NULL when it doesn't exist.
3010 */
3011 char_u *
3012get_var_value(char_u *name)
3013{
3014 dictitem_T *v;
3015
3016 v = find_var(name, NULL, FALSE);
3017 if (v == NULL)
3018 return NULL;
3019 return tv_get_string(&v->di_tv);
3020}
3021
3022/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003023 * Allocate a new hashtab for a sourced script. It will be used while
3024 * sourcing this script and when executing functions defined in the script.
3025 */
3026 void
3027new_script_vars(scid_T id)
3028{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003029 scriptvar_T *sv;
3030
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003031 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3032 if (sv == NULL)
3033 return;
3034 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003035 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003036}
3037
3038/*
3039 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3040 * point to it.
3041 */
3042 void
3043init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3044{
3045 hash_init(&dict->dv_hashtab);
3046 dict->dv_lock = 0;
3047 dict->dv_scope = scope;
3048 dict->dv_refcount = DO_NOT_FREE_CNT;
3049 dict->dv_copyID = 0;
3050 dict_var->di_tv.vval.v_dict = dict;
3051 dict_var->di_tv.v_type = VAR_DICT;
3052 dict_var->di_tv.v_lock = VAR_FIXED;
3053 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3054 dict_var->di_key[0] = NUL;
3055}
3056
3057/*
3058 * Unreference a dictionary initialized by init_var_dict().
3059 */
3060 void
3061unref_var_dict(dict_T *dict)
3062{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003063 // Now the dict needs to be freed if no one else is using it, go back to
3064 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003065 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3066 dict_unref(dict);
3067}
3068
3069/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003070 * Clean up a list of internal variables.
3071 * Frees all allocated variables and the value they contain.
3072 * Clears hashtab "ht", does not free it.
3073 */
3074 void
3075vars_clear(hashtab_T *ht)
3076{
3077 vars_clear_ext(ht, TRUE);
3078}
3079
3080/*
3081 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3082 */
3083 void
3084vars_clear_ext(hashtab_T *ht, int free_val)
3085{
3086 int todo;
3087 hashitem_T *hi;
3088 dictitem_T *v;
3089
3090 hash_lock(ht);
3091 todo = (int)ht->ht_used;
3092 for (hi = ht->ht_array; todo > 0; ++hi)
3093 {
3094 if (!HASHITEM_EMPTY(hi))
3095 {
3096 --todo;
3097
3098 // Free the variable. Don't remove it from the hashtab,
3099 // ht_array might change then. hash_clear() takes care of it
3100 // later.
3101 v = HI2DI(hi);
3102 if (free_val)
3103 clear_tv(&v->di_tv);
3104 if (v->di_flags & DI_FLAGS_ALLOC)
3105 vim_free(v);
3106 }
3107 }
3108 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003109 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003110}
3111
3112/*
3113 * Delete a variable from hashtab "ht" at item "hi".
3114 * Clear the variable value and free the dictitem.
3115 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003116 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003117delete_var(hashtab_T *ht, hashitem_T *hi)
3118{
3119 dictitem_T *di = HI2DI(hi);
3120
3121 hash_remove(ht, hi);
3122 clear_tv(&di->di_tv);
3123 vim_free(di);
3124}
3125
3126/*
3127 * List the value of one internal variable.
3128 */
3129 static void
3130list_one_var(dictitem_T *v, char *prefix, int *first)
3131{
3132 char_u *tofree;
3133 char_u *s;
3134 char_u numbuf[NUMBUFLEN];
3135
3136 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3137 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3138 s == NULL ? (char_u *)"" : s, first);
3139 vim_free(tofree);
3140}
3141
3142 static void
3143list_one_var_a(
3144 char *prefix,
3145 char_u *name,
3146 int type,
3147 char_u *string,
3148 int *first) // when TRUE clear rest of screen and set to FALSE
3149{
3150 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3151 msg_start();
3152 msg_puts(prefix);
3153 if (name != NULL) // "a:" vars don't have a name stored
3154 msg_puts((char *)name);
3155 msg_putchar(' ');
3156 msg_advance(22);
3157 if (type == VAR_NUMBER)
3158 msg_putchar('#');
3159 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3160 msg_putchar('*');
3161 else if (type == VAR_LIST)
3162 {
3163 msg_putchar('[');
3164 if (*string == '[')
3165 ++string;
3166 }
3167 else if (type == VAR_DICT)
3168 {
3169 msg_putchar('{');
3170 if (*string == '{')
3171 ++string;
3172 }
3173 else
3174 msg_putchar(' ');
3175
3176 msg_outtrans(string);
3177
3178 if (type == VAR_FUNC || type == VAR_PARTIAL)
3179 msg_puts("()");
3180 if (*first)
3181 {
3182 msg_clr_eos();
3183 *first = FALSE;
3184 }
3185}
3186
3187/*
3188 * Set variable "name" to value in "tv".
3189 * If the variable already exists, the value is updated.
3190 * Otherwise the variable is created.
3191 */
3192 void
3193set_var(
3194 char_u *name,
3195 typval_T *tv,
3196 int copy) // make copy of value in "tv"
3197{
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003198 set_var_const(name, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003199}
3200
3201/*
3202 * Set variable "name" to value in "tv".
3203 * If the variable already exists and "is_const" is FALSE the value is updated.
3204 * Otherwise the variable is created.
3205 */
3206 void
3207set_var_const(
3208 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003210 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003211 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003212 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003213 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003214{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003215 typval_T *tv = tv_arg;
3216 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 dictitem_T *di;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003218 typval_T *dest_tv = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003219 char_u *varname;
3220 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003222 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003223 int var_in_vim9script;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003224 int flags = flags_arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003225
3226 ht = find_var_ht(name, &varname);
3227 if (ht == NULL || *varname == NUL)
3228 {
3229 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003230 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003231 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003232 is_script_local = ht == get_script_local_ht();
3233
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003234 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003235 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003236 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003237 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003238 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003239 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003240 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003241 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003242 }
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003243 if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
3244 && vim_strchr((char_u *)"gwbt", name[0]) != NULL)
3245 // Do not make g:var, w:var, b:var or t:var final.
3246 flags &= ~ASSIGN_FINAL;
3247
Bram Moolenaare535db82021-03-31 21:07:24 +02003248 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003249 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3250 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003251 // For "[a, _] = list" the underscore is ignored.
3252 if ((flags & ASSIGN_UNPACK) == 0)
3253 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003254 goto failed;
3255 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003256
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003258
Bram Moolenaar24e93162021-07-18 20:40:33 +02003259 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003260 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003261 imported_T *import = find_imported(varname, 0, NULL);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003262
Bram Moolenaar24e93162021-07-18 20:40:33 +02003263 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003264 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003265 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
3266 svar_T *sv;
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003267 where_T where = WHERE_INIT;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003268
3269 // imported variable from another script
3270 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003272 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003273 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 }
Bram Moolenaar6853c382021-09-07 22:12:19 +02003275 if (import->imp_flags & IMP_FLAGS_STAR)
3276 {
3277 semsg(_(e_cannot_use_str_itself_it_is_imported_with_star),
3278 name);
3279 goto failed;
3280 }
Bram Moolenaar60579352021-07-19 21:45:07 +02003281 sv = ((svar_T *)si->sn_var_vals.ga_data) + import->imp_var_vals_idx;
3282
Bram Moolenaar60579352021-07-19 21:45:07 +02003283 where.wt_variable = TRUE;
3284 if (check_typval_type(sv->sv_type, tv, where) == FAIL
3285 || value_check_lock(sv->sv_tv->v_lock, name, FALSE))
3286 {
3287 goto failed;
3288 }
3289
Bram Moolenaar24e93162021-07-18 20:40:33 +02003290 dest_tv = sv->sv_tv;
Bram Moolenaarf6488542021-07-18 21:24:50 +02003291 clear_tv(dest_tv);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003292 }
3293 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294
Bram Moolenaar24e93162021-07-18 20:40:33 +02003295 if (dest_tv == NULL)
3296 {
3297 // Search in parent scope which is possible to reference from lambda
3298 if (di == NULL)
3299 di = find_var_in_scoped_ht(name, TRUE);
3300
3301 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003302 && var_wrong_func_name(name, di == NULL))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003303 goto failed;
3304
3305 if (need_convert_to_bool(type, tv))
3306 {
Bram Moolenaarf6488542021-07-18 21:24:50 +02003307 // Destination is a bool and the value is not, but it can be
3308 // converted.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003309 CLEAR_FIELD(bool_tv);
3310 bool_tv.v_type = VAR_BOOL;
3311 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3312 tv = &bool_tv;
3313 }
3314
3315 if (di != NULL)
3316 {
3317 // Item already exists. Allowed to replace when reloading.
3318 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
3319 {
3320 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaarf6488542021-07-18 21:24:50 +02003321 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003322 {
3323 emsg(_(e_cannot_mod));
3324 goto failed;
3325 }
3326
3327 if (is_script_local && vim9script
Bram Moolenaarf6488542021-07-18 21:24:50 +02003328 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003329 {
3330 semsg(_(e_redefining_script_item_str), name);
3331 goto failed;
3332 }
3333
Bram Moolenaara06758d2021-10-15 00:18:37 +01003334 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003335 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003336 where_T where = WHERE_INIT;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003337
3338 // check the type and adjust to bool if needed
3339 where.wt_index = var_idx;
3340 where.wt_variable = TRUE;
Bram Moolenaarf6488542021-07-18 21:24:50 +02003341 if (check_script_var_type(&di->di_tv, tv, name, where)
3342 == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003343 goto failed;
3344 }
3345
Bram Moolenaara06758d2021-10-15 00:18:37 +01003346 if ((flags & ASSIGN_FOR_LOOP) == 0
3347 && var_check_permission(di, name) == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003348 goto failed;
3349 }
3350 else
3351 {
3352 // can only redefine once
3353 di->di_flags &= ~DI_FLAGS_RELOAD;
3354
Bram Moolenaarf6488542021-07-18 21:24:50 +02003355 // A Vim9 script-local variable is also present in sn_all_vars
3356 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaar24e93162021-07-18 20:40:33 +02003357 if (var_in_vim9script)
3358 update_vim9_script_var(FALSE, di, flags, tv, &type,
Bram Moolenaarf6488542021-07-18 21:24:50 +02003359 (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003360 }
3361
3362 // existing variable, need to clear the value
3363
3364 // Handle setting internal di: variables separately where needed to
3365 // prevent changing the type.
3366 if (ht == &vimvarht)
3367 {
3368 if (di->di_tv.v_type == VAR_STRING)
3369 {
3370 VIM_CLEAR(di->di_tv.vval.v_string);
3371 if (copy || tv->v_type != VAR_STRING)
3372 {
3373 char_u *val = tv_get_string(tv);
3374
Bram Moolenaarf6488542021-07-18 21:24:50 +02003375 // Careful: when assigning to v:errmsg and
3376 // tv_get_string() causes an error message the variable
3377 // will already be set.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003378 if (di->di_tv.vval.v_string == NULL)
3379 di->di_tv.vval.v_string = vim_strsave(val);
3380 }
3381 else
3382 {
3383 // Take over the string to avoid an extra alloc/free.
3384 di->di_tv.vval.v_string = tv->vval.v_string;
3385 tv->vval.v_string = NULL;
3386 }
3387 goto failed;
3388 }
3389 else if (di->di_tv.v_type == VAR_NUMBER)
3390 {
3391 di->di_tv.vval.v_number = tv_get_number(tv);
3392 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003393 set_search_direction(di->di_tv.vval.v_number
3394 ? '/' : '?');
Bram Moolenaar24e93162021-07-18 20:40:33 +02003395#ifdef FEAT_SEARCH_EXTRA
3396 else if (STRCMP(varname, "hlsearch") == 0)
3397 {
3398 no_hlsearch = !di->di_tv.vval.v_number;
3399 redraw_all_later(SOME_VALID);
3400 }
3401#endif
3402 goto failed;
3403 }
3404 else if (di->di_tv.v_type != tv->v_type)
3405 {
3406 semsg(_("E963: setting %s to value with wrong type"), name);
3407 goto failed;
3408 }
3409 }
3410
3411 clear_tv(&di->di_tv);
3412 }
3413 else
3414 {
3415 // Item not found, check if a function already exists.
3416 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaarf6488542021-07-18 21:24:50 +02003417 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003418 {
3419 semsg(_(e_redefining_script_item_str), name);
3420 goto failed;
3421 }
3422
Bram Moolenaar24e93162021-07-18 20:40:33 +02003423 // add a new variable
3424 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3425 {
3426 semsg(_(e_unknown_variable_str), name);
3427 goto failed;
3428 }
3429
3430 // Can't add "v:" or "a:" variable.
3431 if (ht == &vimvarht || ht == get_funccal_args_ht())
3432 {
3433 semsg(_(e_illvar), name);
3434 goto failed;
3435 }
3436
3437 // Make sure the variable name is valid. In Vim9 script an autoload
3438 // variable must be prefixed with "g:".
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003439 if (!valid_varname(varname, -1, !vim9script
Bram Moolenaarf6488542021-07-18 21:24:50 +02003440 || STRNCMP(name, "g:", 2) == 0))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003441 goto failed;
3442
3443 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3444 if (di == NULL)
3445 goto failed;
3446 STRCPY(di->di_key, varname);
3447 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
3448 {
3449 vim_free(di);
3450 goto failed;
3451 }
3452 di->di_flags = DI_FLAGS_ALLOC;
3453 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3454 di->di_flags |= DI_FLAGS_LOCK;
3455
3456 // A Vim9 script-local variable is also added to sn_all_vars and
3457 // sn_var_vals. It may set "type" from "tv".
Bram Moolenaare535db82021-03-31 21:07:24 +02003458 if (var_in_vim9script)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003459 update_vim9_script_var(TRUE, di, flags, tv, &type,
Bram Moolenaarf6488542021-07-18 21:24:50 +02003460 (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003461 }
3462
Bram Moolenaar24e93162021-07-18 20:40:33 +02003463 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003464 }
3465
3466 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003467 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003468 else
3469 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003470 *dest_tv = *tv;
3471 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003472 init_tv(tv);
3473 }
3474
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003475 if (vim9script && type != NULL)
3476 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003477 if (type->tt_type == VAR_DICT && dest_tv->vval.v_dict != NULL)
Bram Moolenaar464393a2021-09-11 23:07:44 +02003478 {
3479 if (dest_tv->vval.v_dict->dv_type != type)
3480 {
3481 free_type(dest_tv->vval.v_dict->dv_type);
3482 dest_tv->vval.v_dict->dv_type = alloc_type(type);
3483 }
3484 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003485 else if (type->tt_type == VAR_LIST && dest_tv->vval.v_list != NULL)
Bram Moolenaar464393a2021-09-11 23:07:44 +02003486 {
3487 if (dest_tv->vval.v_list->lv_type != type)
3488 {
3489 free_type(dest_tv->vval.v_list->lv_type);
3490 dest_tv->vval.v_list->lv_type = alloc_type(type);
3491 }
3492 }
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003493 }
3494
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003495 // ":const var = value" locks the value
3496 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003497 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003498 // Like :lockvar! name: lock the value and what it contains, but only
3499 // if the reference count is up to one. That locks only literal
3500 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003501 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003502 return;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003503
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003504failed:
3505 if (!copy)
3506 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003507}
3508
3509/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003510 * Check in this order for backwards compatibility:
3511 * - Whether the variable is read-only
3512 * - Whether the variable value is locked
3513 * - Whether the variable is locked
3514 */
3515 int
3516var_check_permission(dictitem_T *di, char_u *name)
3517{
3518 if (var_check_ro(di->di_flags, name, FALSE)
3519 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3520 || var_check_lock(di->di_flags, name, FALSE))
3521 return FAIL;
3522 return OK;
3523}
3524
3525/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003526 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3527 * Also give an error message.
3528 */
3529 int
3530var_check_ro(int flags, char_u *name, int use_gettext)
3531{
3532 if (flags & DI_FLAGS_RO)
3533 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02003534 semsg(_(e_cannot_change_readonly_variable_str),
3535 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003536 return TRUE;
3537 }
3538 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3539 {
3540 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3541 return TRUE;
3542 }
3543 return FALSE;
3544}
3545
3546/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003547 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3548 * Also give an error message.
3549 */
3550 int
3551var_check_lock(int flags, char_u *name, int use_gettext)
3552{
3553 if (flags & DI_FLAGS_LOCK)
3554 {
3555 semsg(_(e_variable_is_locked_str),
3556 use_gettext ? (char_u *)_(name) : name);
3557 return TRUE;
3558 }
3559 return FALSE;
3560}
3561
3562/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003563 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3564 * Also give an error message.
3565 */
3566 int
3567var_check_fixed(int flags, char_u *name, int use_gettext)
3568{
3569 if (flags & DI_FLAGS_FIX)
3570 {
3571 semsg(_("E795: Cannot delete variable %s"),
3572 use_gettext ? (char_u *)_(name) : name);
3573 return TRUE;
3574 }
3575 return FALSE;
3576}
3577
3578/*
3579 * Check if a funcref is assigned to a valid variable name.
3580 * Return TRUE and give an error if not.
3581 */
3582 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003583var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003584 char_u *name, // points to start of variable name
3585 int new_var) // TRUE when creating the variable
3586{
Bram Moolenaar32154662021-03-28 21:14:06 +02003587 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
3588 // the name can be used without the s: prefix.
3589 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
3590 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003591 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3592 ? name[2] : name[0]))
3593 {
3594 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3595 name);
3596 return TRUE;
3597 }
3598 // Don't allow hiding a function. When "v" is not NULL we might be
3599 // assigning another function to the same var, the type is checked
3600 // below.
3601 if (new_var && function_exists(name, FALSE))
3602 {
3603 semsg(_("E705: Variable name conflicts with existing function: %s"),
3604 name);
3605 return TRUE;
3606 }
3607 return FALSE;
3608}
3609
3610/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003611 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3612 * value. Also give an error message, using "name" or _("name") when
3613 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003614 */
3615 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003616value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003617{
3618 if (lock & VAR_LOCKED)
3619 {
3620 semsg(_("E741: Value is locked: %s"),
3621 name == NULL ? (char_u *)_("Unknown")
3622 : use_gettext ? (char_u *)_(name)
3623 : name);
3624 return TRUE;
3625 }
3626 if (lock & VAR_FIXED)
3627 {
3628 semsg(_("E742: Cannot change value of %s"),
3629 name == NULL ? (char_u *)_("Unknown")
3630 : use_gettext ? (char_u *)_(name)
3631 : name);
3632 return TRUE;
3633 }
3634 return FALSE;
3635}
3636
3637/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003638 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003639 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003640 * Return FALSE and give an error if not.
3641 */
3642 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003643valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003644{
3645 char_u *p;
3646
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003647 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003648 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003649 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003650 {
3651 semsg(_(e_illvar), varname);
3652 return FALSE;
3653 }
3654 return TRUE;
3655}
3656
3657/*
3658 * getwinvar() and gettabwinvar()
3659 */
3660 static void
3661getwinvar(
3662 typval_T *argvars,
3663 typval_T *rettv,
3664 int off) // 1 for gettabwinvar()
3665{
3666 win_T *win;
3667 char_u *varname;
3668 dictitem_T *v;
3669 tabpage_T *tp = NULL;
3670 int done = FALSE;
3671 win_T *oldcurwin;
3672 tabpage_T *oldtabpage;
3673 int need_switch_win;
3674
3675 if (off == 1)
3676 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3677 else
3678 tp = curtab;
3679 win = find_win_by_nr(&argvars[off], tp);
3680 varname = tv_get_string_chk(&argvars[off + 1]);
3681 ++emsg_off;
3682
3683 rettv->v_type = VAR_STRING;
3684 rettv->vval.v_string = NULL;
3685
3686 if (win != NULL && varname != NULL)
3687 {
3688 // Set curwin to be our win, temporarily. Also set the tabpage,
3689 // otherwise the window is not valid. Only do this when needed,
3690 // autocommands get blocked.
3691 need_switch_win = !(tp == curtab && win == curwin);
3692 if (!need_switch_win
3693 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3694 {
3695 if (*varname == '&')
3696 {
3697 if (varname[1] == NUL)
3698 {
3699 // get all window-local options in a dict
3700 dict_T *opts = get_winbuf_options(FALSE);
3701
3702 if (opts != NULL)
3703 {
3704 rettv_dict_set(rettv, opts);
3705 done = TRUE;
3706 }
3707 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003708 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003709 // window-local-option
3710 done = TRUE;
3711 }
3712 else
3713 {
3714 // Look up the variable.
3715 // Let getwinvar({nr}, "") return the "w:" dictionary.
3716 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3717 varname, FALSE);
3718 if (v != NULL)
3719 {
3720 copy_tv(&v->di_tv, rettv);
3721 done = TRUE;
3722 }
3723 }
3724 }
3725
3726 if (need_switch_win)
3727 // restore previous notion of curwin
3728 restore_win(oldcurwin, oldtabpage, TRUE);
3729 }
3730
3731 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3732 // use the default return value
3733 copy_tv(&argvars[off + 2], rettv);
3734
3735 --emsg_off;
3736}
3737
3738/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003739 * Set option "varname" to the value of "varp" for the current buffer/window.
3740 */
3741 static void
3742set_option_from_tv(char_u *varname, typval_T *varp)
3743{
3744 long numval = 0;
3745 char_u *strval;
3746 char_u nbuf[NUMBUFLEN];
3747 int error = FALSE;
3748
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003749 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003750 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003751 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003752 strval = (char_u *)"0"; // avoid using "false"
3753 }
3754 else
3755 {
3756 if (!in_vim9script() || varp->v_type != VAR_STRING)
3757 numval = (long)tv_get_number_chk(varp, &error);
3758 strval = tv_get_string_buf_chk(varp, nbuf);
3759 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02003760 if (!error && strval != NULL)
3761 set_option_value(varname, numval, strval, OPT_LOCAL);
3762}
3763
3764/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003765 * "setwinvar()" and "settabwinvar()" functions
3766 */
3767 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003768setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003769{
3770 win_T *win;
3771 win_T *save_curwin;
3772 tabpage_T *save_curtab;
3773 int need_switch_win;
3774 char_u *varname, *winvarname;
3775 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003776 tabpage_T *tp = NULL;
3777
3778 if (check_secure())
3779 return;
3780
3781 if (off == 1)
3782 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3783 else
3784 tp = curtab;
3785 win = find_win_by_nr(&argvars[off], tp);
3786 varname = tv_get_string_chk(&argvars[off + 1]);
3787 varp = &argvars[off + 2];
3788
3789 if (win != NULL && varname != NULL && varp != NULL)
3790 {
3791 need_switch_win = !(tp == curtab && win == curwin);
3792 if (!need_switch_win
3793 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3794 {
3795 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003796 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003797 else
3798 {
3799 winvarname = alloc(STRLEN(varname) + 3);
3800 if (winvarname != NULL)
3801 {
3802 STRCPY(winvarname, "w:");
3803 STRCPY(winvarname + 2, varname);
3804 set_var(winvarname, varp, TRUE);
3805 vim_free(winvarname);
3806 }
3807 }
3808 }
3809 if (need_switch_win)
3810 restore_win(save_curwin, save_curtab, TRUE);
3811 }
3812}
3813
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003814/*
3815 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3816 * v:option_type, and v:option_command.
3817 */
3818 void
3819reset_v_option_vars(void)
3820{
3821 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3822 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3823 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3824 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3825 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3826 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3827}
3828
3829/*
3830 * Add an assert error to v:errors.
3831 */
3832 void
3833assert_error(garray_T *gap)
3834{
3835 struct vimvar *vp = &vimvars[VV_ERRORS];
3836
3837 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003838 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003839 set_vim_var_list(VV_ERRORS, list_alloc());
3840 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3841}
3842
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003843 int
3844var_exists(char_u *var)
3845{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003846 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003847 char_u *name;
3848 char_u *tofree;
3849 typval_T tv;
3850 int len = 0;
3851 int n = FALSE;
3852
3853 // get_name_len() takes care of expanding curly braces
3854 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003855 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003856 if (len > 0)
3857 {
3858 if (tofree != NULL)
3859 name = tofree;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003860 n = (eval_variable(name, len, &tv, NULL,
3861 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003862 if (n)
3863 {
3864 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003865 arg = skipwhite(arg);
3866 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003867 if (n)
3868 clear_tv(&tv);
3869 }
3870 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003871 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003872 n = FALSE;
3873
3874 vim_free(tofree);
3875 return n;
3876}
3877
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003878static lval_T *redir_lval = NULL;
3879#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3880static garray_T redir_ga; // only valid when redir_lval is not NULL
3881static char_u *redir_endp = NULL;
3882static char_u *redir_varname = NULL;
3883
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003884 int
3885alloc_redir_lval(void)
3886{
3887 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3888 if (redir_lval == NULL)
3889 return FAIL;
3890 return OK;
3891}
3892
3893 void
3894clear_redir_lval(void)
3895{
3896 VIM_CLEAR(redir_lval);
3897}
3898
3899 void
3900init_redir_ga(void)
3901{
3902 ga_init2(&redir_ga, (int)sizeof(char), 500);
3903}
3904
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003905/*
3906 * Start recording command output to a variable
3907 * When "append" is TRUE append to an existing variable.
3908 * Returns OK if successfully completed the setup. FAIL otherwise.
3909 */
3910 int
3911var_redir_start(char_u *name, int append)
3912{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003913 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003914 typval_T tv;
3915
3916 // Catch a bad name early.
3917 if (!eval_isnamec1(*name))
3918 {
3919 emsg(_(e_invarg));
3920 return FAIL;
3921 }
3922
3923 // Make a copy of the name, it is used in redir_lval until redir ends.
3924 redir_varname = vim_strsave(name);
3925 if (redir_varname == NULL)
3926 return FAIL;
3927
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003928 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003929 {
3930 var_redir_stop();
3931 return FAIL;
3932 }
3933
3934 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003935 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003936
3937 // Parse the variable name (can be a dict or list entry).
3938 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3939 FNE_CHECK_START);
3940 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3941 {
3942 clear_lval(redir_lval);
3943 if (redir_endp != NULL && *redir_endp != NUL)
3944 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003945 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003946 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003947 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003948 redir_endp = NULL; // don't store a value, only cleanup
3949 var_redir_stop();
3950 return FAIL;
3951 }
3952
3953 // check if we can write to the variable: set it to or append an empty
3954 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003955 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003956 tv.v_type = VAR_STRING;
3957 tv.vval.v_string = (char_u *)"";
3958 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003959 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003960 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003961 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003962 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003963 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003964 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003965 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003966 {
3967 redir_endp = NULL; // don't store a value, only cleanup
3968 var_redir_stop();
3969 return FAIL;
3970 }
3971
3972 return OK;
3973}
3974
3975/*
3976 * Append "value[value_len]" to the variable set by var_redir_start().
3977 * The actual appending is postponed until redirection ends, because the value
3978 * appended may in fact be the string we write to, changing it may cause freed
3979 * memory to be used:
3980 * :redir => foo
3981 * :let foo
3982 * :redir END
3983 */
3984 void
3985var_redir_str(char_u *value, int value_len)
3986{
3987 int len;
3988
3989 if (redir_lval == NULL)
3990 return;
3991
3992 if (value_len == -1)
3993 len = (int)STRLEN(value); // Append the entire string
3994 else
3995 len = value_len; // Append only "value_len" characters
3996
3997 if (ga_grow(&redir_ga, len) == OK)
3998 {
3999 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4000 redir_ga.ga_len += len;
4001 }
4002 else
4003 var_redir_stop();
4004}
4005
4006/*
4007 * Stop redirecting command output to a variable.
4008 * Frees the allocated memory.
4009 */
4010 void
4011var_redir_stop(void)
4012{
4013 typval_T tv;
4014
4015 if (EVALCMD_BUSY)
4016 {
4017 redir_lval = NULL;
4018 return;
4019 }
4020
4021 if (redir_lval != NULL)
4022 {
4023 // If there was no error: assign the text to the variable.
4024 if (redir_endp != NULL)
4025 {
4026 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4027 tv.v_type = VAR_STRING;
4028 tv.vval.v_string = redir_ga.ga_data;
4029 // Call get_lval() again, if it's inside a Dict or List it may
4030 // have changed.
4031 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4032 FALSE, FALSE, 0, FNE_CHECK_START);
4033 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004035 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004036 clear_lval(redir_lval);
4037 }
4038
4039 // free the collected output
4040 VIM_CLEAR(redir_ga.ga_data);
4041
4042 VIM_CLEAR(redir_lval);
4043 }
4044 VIM_CLEAR(redir_varname);
4045}
4046
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004047/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004048 * Get the collected redirected text and clear redir_ga.
4049 */
4050 char_u *
4051get_clear_redir_ga(void)
4052{
4053 char_u *res;
4054
4055 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4056 res = redir_ga.ga_data;
4057 redir_ga.ga_data = NULL;
4058 return res;
4059}
4060
4061/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004062 * "gettabvar()" function
4063 */
4064 void
4065f_gettabvar(typval_T *argvars, typval_T *rettv)
4066{
4067 win_T *oldcurwin;
4068 tabpage_T *tp, *oldtabpage;
4069 dictitem_T *v;
4070 char_u *varname;
4071 int done = FALSE;
4072
4073 rettv->v_type = VAR_STRING;
4074 rettv->vval.v_string = NULL;
4075
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004076 if (in_vim9script()
4077 && (check_for_number_arg(argvars, 0) == FAIL
4078 || check_for_string_arg(argvars, 1) == FAIL))
4079 return;
4080
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004081 varname = tv_get_string_chk(&argvars[1]);
4082 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4083 if (tp != NULL && varname != NULL)
4084 {
4085 // Set tp to be our tabpage, temporarily. Also set the window to the
4086 // first window in the tabpage, otherwise the window is not valid.
4087 if (switch_win(&oldcurwin, &oldtabpage,
4088 tp == curtab || tp->tp_firstwin == NULL ? firstwin
4089 : tp->tp_firstwin, tp, TRUE) == OK)
4090 {
4091 // look up the variable
4092 // Let gettabvar({nr}, "") return the "t:" dictionary.
4093 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
4094 if (v != NULL)
4095 {
4096 copy_tv(&v->di_tv, rettv);
4097 done = TRUE;
4098 }
4099 }
4100
4101 // restore previous notion of curwin
4102 restore_win(oldcurwin, oldtabpage, TRUE);
4103 }
4104
4105 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4106 copy_tv(&argvars[2], rettv);
4107}
4108
4109/*
4110 * "gettabwinvar()" function
4111 */
4112 void
4113f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4114{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004115 if (in_vim9script()
4116 && (check_for_number_arg(argvars, 0) == FAIL
4117 || check_for_number_arg(argvars, 1) == FAIL
4118 || check_for_string_arg(argvars, 2) == FAIL))
4119 return;
4120
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004121 getwinvar(argvars, rettv, 1);
4122}
4123
4124/*
4125 * "getwinvar()" function
4126 */
4127 void
4128f_getwinvar(typval_T *argvars, typval_T *rettv)
4129{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004130 if (in_vim9script()
4131 && (check_for_number_arg(argvars, 0) == FAIL
4132 || check_for_string_arg(argvars, 1) == FAIL))
4133 return;
4134
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004135 getwinvar(argvars, rettv, 0);
4136}
4137
4138/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004139 * "getbufvar()" function
4140 */
4141 void
4142f_getbufvar(typval_T *argvars, typval_T *rettv)
4143{
4144 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004145 char_u *varname;
4146 dictitem_T *v;
4147 int done = FALSE;
4148
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004149 if (in_vim9script()
4150 && (check_for_buffer_arg(argvars, 0) == FAIL
4151 || check_for_string_arg(argvars, 1) == FAIL))
4152 return;
4153
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004154 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004155 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004156
4157 rettv->v_type = VAR_STRING;
4158 rettv->vval.v_string = NULL;
4159
4160 if (buf != NULL && varname != NULL)
4161 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004162 if (*varname == '&')
4163 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004164 buf_T *save_curbuf = curbuf;
4165
4166 // set curbuf to be our buf, temporarily
4167 curbuf = buf;
4168
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004169 if (varname[1] == NUL)
4170 {
4171 // get all buffer-local options in a dict
4172 dict_T *opts = get_winbuf_options(TRUE);
4173
4174 if (opts != NULL)
4175 {
4176 rettv_dict_set(rettv, opts);
4177 done = TRUE;
4178 }
4179 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004180 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004181 // buffer-local-option
4182 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02004183
4184 // restore previous notion of curbuf
4185 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004186 }
4187 else
4188 {
4189 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02004190 if (*varname == NUL)
4191 // Let getbufvar({nr}, "") return the "b:" dictionary.
4192 v = &buf->b_bufvar;
4193 else
4194 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
4195 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004196 if (v != NULL)
4197 {
4198 copy_tv(&v->di_tv, rettv);
4199 done = TRUE;
4200 }
4201 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004202 }
4203
4204 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4205 // use the default value
4206 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004207}
4208
4209/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004210 * "settabvar()" function
4211 */
4212 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004213f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004214{
4215 tabpage_T *save_curtab;
4216 tabpage_T *tp;
4217 char_u *varname, *tabvarname;
4218 typval_T *varp;
4219
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004220 if (check_secure())
4221 return;
4222
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004223 if (in_vim9script()
4224 && (check_for_number_arg(argvars, 0) == FAIL
4225 || check_for_string_arg(argvars, 1) == FAIL))
4226 return;
4227
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004228 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4229 varname = tv_get_string_chk(&argvars[1]);
4230 varp = &argvars[2];
4231
4232 if (varname != NULL && varp != NULL && tp != NULL)
4233 {
4234 save_curtab = curtab;
4235 goto_tabpage_tp(tp, FALSE, FALSE);
4236
4237 tabvarname = alloc(STRLEN(varname) + 3);
4238 if (tabvarname != NULL)
4239 {
4240 STRCPY(tabvarname, "t:");
4241 STRCPY(tabvarname + 2, varname);
4242 set_var(tabvarname, varp, TRUE);
4243 vim_free(tabvarname);
4244 }
4245
4246 // Restore current tabpage
4247 if (valid_tabpage(save_curtab))
4248 goto_tabpage_tp(save_curtab, FALSE, FALSE);
4249 }
4250}
4251
4252/*
4253 * "settabwinvar()" function
4254 */
4255 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004256f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004257{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004258 if (in_vim9script()
4259 && (check_for_number_arg(argvars, 0) == FAIL
4260 || check_for_number_arg(argvars, 1) == FAIL
4261 || check_for_string_arg(argvars, 2) == FAIL))
4262 return;
4263
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004264 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004265}
4266
4267/*
4268 * "setwinvar()" function
4269 */
4270 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004271f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004272{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004273 if (in_vim9script()
4274 && (check_for_number_arg(argvars, 0) == FAIL
4275 || check_for_string_arg(argvars, 1) == FAIL))
4276 return;
4277
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004278 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004279}
4280
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004281/*
4282 * "setbufvar()" function
4283 */
4284 void
4285f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4286{
4287 buf_T *buf;
4288 char_u *varname, *bufvarname;
4289 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004290
4291 if (check_secure())
4292 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004293
4294 if (in_vim9script()
4295 && (check_for_buffer_arg(argvars, 0) == FAIL
4296 || check_for_string_arg(argvars, 1) == FAIL))
4297 return;
4298
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004299 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004300 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004301 varp = &argvars[2];
4302
4303 if (buf != NULL && varname != NULL && varp != NULL)
4304 {
4305 if (*varname == '&')
4306 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004307 aco_save_T aco;
4308
4309 // set curbuf to be our buf, temporarily
4310 aucmd_prepbuf(&aco, buf);
4311
Bram Moolenaar191929b2020-08-19 21:20:49 +02004312 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004313
4314 // reset notion of buffer
4315 aucmd_restbuf(&aco);
4316 }
4317 else
4318 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004319 bufvarname = alloc(STRLEN(varname) + 3);
4320 if (bufvarname != NULL)
4321 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004322 buf_T *save_curbuf = curbuf;
4323
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004324 curbuf = buf;
4325 STRCPY(bufvarname, "b:");
4326 STRCPY(bufvarname + 2, varname);
4327 set_var(bufvarname, varp, TRUE);
4328 vim_free(bufvarname);
4329 curbuf = save_curbuf;
4330 }
4331 }
4332 }
4333}
4334
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004335/*
4336 * Get a callback from "arg". It can be a Funcref or a function name.
4337 * When "arg" is zero return an empty string.
4338 * "cb_name" is not allocated.
4339 * "cb_name" is set to NULL for an invalid argument.
4340 */
4341 callback_T
4342get_callback(typval_T *arg)
4343{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004344 callback_T res;
4345 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004346
4347 res.cb_free_name = FALSE;
4348 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4349 {
4350 res.cb_partial = arg->vval.v_partial;
4351 ++res.cb_partial->pt_refcount;
4352 res.cb_name = partial_name(res.cb_partial);
4353 }
4354 else
4355 {
4356 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004357 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4358 && isdigit(*arg->vval.v_string))
4359 r = FAIL;
4360 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004361 {
4362 // Note that we don't make a copy of the string.
4363 res.cb_name = arg->vval.v_string;
4364 func_ref(res.cb_name);
4365 }
4366 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004367 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004368 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004369 r = FAIL;
4370
4371 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004372 {
4373 emsg(_("E921: Invalid callback argument"));
4374 res.cb_name = NULL;
4375 }
4376 }
4377 return res;
4378}
4379
4380/*
4381 * Copy a callback into a typval_T.
4382 */
4383 void
4384put_callback(callback_T *cb, typval_T *tv)
4385{
4386 if (cb->cb_partial != NULL)
4387 {
4388 tv->v_type = VAR_PARTIAL;
4389 tv->vval.v_partial = cb->cb_partial;
4390 ++tv->vval.v_partial->pt_refcount;
4391 }
4392 else
4393 {
4394 tv->v_type = VAR_FUNC;
4395 tv->vval.v_string = vim_strsave(cb->cb_name);
4396 func_ref(cb->cb_name);
4397 }
4398}
4399
4400/*
4401 * Make a copy of "src" into "dest", allocating the function name if needed,
4402 * without incrementing the refcount.
4403 */
4404 void
4405set_callback(callback_T *dest, callback_T *src)
4406{
4407 if (src->cb_partial == NULL)
4408 {
4409 // just a function name, make a copy
4410 dest->cb_name = vim_strsave(src->cb_name);
4411 dest->cb_free_name = TRUE;
4412 }
4413 else
4414 {
4415 // cb_name is a pointer into cb_partial
4416 dest->cb_name = src->cb_name;
4417 dest->cb_free_name = FALSE;
4418 }
4419 dest->cb_partial = src->cb_partial;
4420}
4421
4422/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004423 * Copy callback from "src" to "dest", incrementing the refcounts.
4424 */
4425 void
4426copy_callback(callback_T *dest, callback_T *src)
4427{
4428 dest->cb_partial = src->cb_partial;
4429 if (dest->cb_partial != NULL)
4430 {
4431 dest->cb_name = src->cb_name;
4432 dest->cb_free_name = FALSE;
4433 ++dest->cb_partial->pt_refcount;
4434 }
4435 else
4436 {
4437 dest->cb_name = vim_strsave(src->cb_name);
4438 dest->cb_free_name = TRUE;
4439 func_ref(src->cb_name);
4440 }
4441}
4442
4443/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004444 * Unref/free "callback" returned by get_callback() or set_callback().
4445 */
4446 void
4447free_callback(callback_T *callback)
4448{
4449 if (callback->cb_partial != NULL)
4450 {
4451 partial_unref(callback->cb_partial);
4452 callback->cb_partial = NULL;
4453 }
4454 else if (callback->cb_name != NULL)
4455 func_unref(callback->cb_name);
4456 if (callback->cb_free_name)
4457 {
4458 vim_free(callback->cb_name);
4459 callback->cb_free_name = FALSE;
4460 }
4461 callback->cb_name = NULL;
4462}
4463
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004464#endif // FEAT_EVAL