blob: 2f285189b6df7f169cd3e82c34475e26e0ff3c67 [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!)
Bram Moolenaard787e402021-12-24 21:36:12 +000048 type_T *vv_type; // type or NULL
Bram Moolenaare5cdf152019-08-29 22:09:46 +020049 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
50} vimvars[VV_LEN] =
51{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020052 // The order here must match the VV_ defines in vim.h!
53 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaard787e402021-12-24 21:36:12 +000054 {VV_NAME("count", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
55 {VV_NAME("count1", VAR_NUMBER), NULL, VV_RO},
56 {VV_NAME("prevcount", VAR_NUMBER), NULL, VV_RO},
57 {VV_NAME("errmsg", VAR_STRING), NULL, VV_COMPAT},
58 {VV_NAME("warningmsg", VAR_STRING), NULL, 0},
59 {VV_NAME("statusmsg", VAR_STRING), NULL, 0},
60 {VV_NAME("shell_error", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
61 {VV_NAME("this_session", VAR_STRING), NULL, VV_COMPAT},
62 {VV_NAME("version", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
63 {VV_NAME("lnum", VAR_NUMBER), NULL, VV_RO_SBX},
64 {VV_NAME("termresponse", VAR_STRING), NULL, VV_RO},
65 {VV_NAME("fname", VAR_STRING), NULL, VV_RO},
66 {VV_NAME("lang", VAR_STRING), NULL, VV_RO},
67 {VV_NAME("lc_time", VAR_STRING), NULL, VV_RO},
68 {VV_NAME("ctype", VAR_STRING), NULL, VV_RO},
69 {VV_NAME("charconvert_from", VAR_STRING), NULL, VV_RO},
70 {VV_NAME("charconvert_to", VAR_STRING), NULL, VV_RO},
71 {VV_NAME("fname_in", VAR_STRING), NULL, VV_RO},
72 {VV_NAME("fname_out", VAR_STRING), NULL, VV_RO},
73 {VV_NAME("fname_new", VAR_STRING), NULL, VV_RO},
74 {VV_NAME("fname_diff", VAR_STRING), NULL, VV_RO},
75 {VV_NAME("cmdarg", VAR_STRING), NULL, VV_RO},
76 {VV_NAME("foldstart", VAR_NUMBER), NULL, VV_RO_SBX},
77 {VV_NAME("foldend", VAR_NUMBER), NULL, VV_RO_SBX},
78 {VV_NAME("folddashes", VAR_STRING), NULL, VV_RO_SBX},
79 {VV_NAME("foldlevel", VAR_NUMBER), NULL, VV_RO_SBX},
80 {VV_NAME("progname", VAR_STRING), NULL, VV_RO},
81 {VV_NAME("servername", VAR_STRING), NULL, VV_RO},
82 {VV_NAME("dying", VAR_NUMBER), NULL, VV_RO},
83 {VV_NAME("exception", VAR_STRING), NULL, VV_RO},
84 {VV_NAME("throwpoint", VAR_STRING), NULL, VV_RO},
85 {VV_NAME("register", VAR_STRING), NULL, VV_RO},
86 {VV_NAME("cmdbang", VAR_NUMBER), NULL, VV_RO},
87 {VV_NAME("insertmode", VAR_STRING), NULL, VV_RO},
88 {VV_NAME("val", VAR_UNKNOWN), NULL, VV_RO},
89 {VV_NAME("key", VAR_UNKNOWN), NULL, VV_RO},
90 {VV_NAME("profiling", VAR_NUMBER), NULL, VV_RO},
91 {VV_NAME("fcs_reason", VAR_STRING), NULL, VV_RO},
92 {VV_NAME("fcs_choice", VAR_STRING), NULL, 0},
93 {VV_NAME("beval_bufnr", VAR_NUMBER), NULL, VV_RO},
94 {VV_NAME("beval_winnr", VAR_NUMBER), NULL, VV_RO},
95 {VV_NAME("beval_winid", VAR_NUMBER), NULL, VV_RO},
96 {VV_NAME("beval_lnum", VAR_NUMBER), NULL, VV_RO},
97 {VV_NAME("beval_col", VAR_NUMBER), NULL, VV_RO},
98 {VV_NAME("beval_text", VAR_STRING), NULL, VV_RO},
99 {VV_NAME("scrollstart", VAR_STRING), NULL, 0},
100 {VV_NAME("swapname", VAR_STRING), NULL, VV_RO},
101 {VV_NAME("swapchoice", VAR_STRING), NULL, 0},
102 {VV_NAME("swapcommand", VAR_STRING), NULL, VV_RO},
103 {VV_NAME("char", VAR_STRING), NULL, 0},
104 {VV_NAME("mouse_win", VAR_NUMBER), NULL, 0},
105 {VV_NAME("mouse_winid", VAR_NUMBER), NULL, 0},
106 {VV_NAME("mouse_lnum", VAR_NUMBER), NULL, 0},
107 {VV_NAME("mouse_col", VAR_NUMBER), NULL, 0},
108 {VV_NAME("operator", VAR_STRING), NULL, VV_RO},
109 {VV_NAME("searchforward", VAR_NUMBER), NULL, 0},
110 {VV_NAME("hlsearch", VAR_NUMBER), NULL, 0},
111 {VV_NAME("oldfiles", VAR_LIST), &t_list_string, 0},
112 {VV_NAME("windowid", VAR_NUMBER), NULL, VV_RO},
113 {VV_NAME("progpath", VAR_STRING), NULL, VV_RO},
114 {VV_NAME("completed_item", VAR_DICT), &t_dict_string, VV_RO},
115 {VV_NAME("option_new", VAR_STRING), NULL, VV_RO},
116 {VV_NAME("option_old", VAR_STRING), NULL, VV_RO},
117 {VV_NAME("option_oldlocal", VAR_STRING), NULL, VV_RO},
118 {VV_NAME("option_oldglobal", VAR_STRING), NULL, VV_RO},
119 {VV_NAME("option_command", VAR_STRING), NULL, VV_RO},
120 {VV_NAME("option_type", VAR_STRING), NULL, VV_RO},
121 {VV_NAME("errors", VAR_LIST), &t_list_string, 0},
122 {VV_NAME("false", VAR_BOOL), NULL, VV_RO},
123 {VV_NAME("true", VAR_BOOL), NULL, VV_RO},
124 {VV_NAME("none", VAR_SPECIAL), NULL, VV_RO},
125 {VV_NAME("null", VAR_SPECIAL), NULL, VV_RO},
126 {VV_NAME("numbermax", VAR_NUMBER), NULL, VV_RO},
127 {VV_NAME("numbermin", VAR_NUMBER), NULL, VV_RO},
128 {VV_NAME("numbersize", VAR_NUMBER), NULL, VV_RO},
129 {VV_NAME("vim_did_enter", VAR_NUMBER), NULL, VV_RO},
130 {VV_NAME("testing", VAR_NUMBER), NULL, 0},
131 {VV_NAME("t_number", VAR_NUMBER), NULL, VV_RO},
132 {VV_NAME("t_string", VAR_NUMBER), NULL, VV_RO},
133 {VV_NAME("t_func", VAR_NUMBER), NULL, VV_RO},
134 {VV_NAME("t_list", VAR_NUMBER), NULL, VV_RO},
135 {VV_NAME("t_dict", VAR_NUMBER), NULL, VV_RO},
136 {VV_NAME("t_float", VAR_NUMBER), NULL, VV_RO},
137 {VV_NAME("t_bool", VAR_NUMBER), NULL, VV_RO},
138 {VV_NAME("t_none", VAR_NUMBER), NULL, VV_RO},
139 {VV_NAME("t_job", VAR_NUMBER), NULL, VV_RO},
140 {VV_NAME("t_channel", VAR_NUMBER), NULL, VV_RO},
141 {VV_NAME("t_blob", VAR_NUMBER), NULL, VV_RO},
142 {VV_NAME("termrfgresp", VAR_STRING), NULL, VV_RO},
143 {VV_NAME("termrbgresp", VAR_STRING), NULL, VV_RO},
144 {VV_NAME("termu7resp", VAR_STRING), NULL, VV_RO},
145 {VV_NAME("termstyleresp", VAR_STRING), NULL, VV_RO},
146 {VV_NAME("termblinkresp", VAR_STRING), NULL, VV_RO},
147 {VV_NAME("event", VAR_DICT), NULL, VV_RO},
148 {VV_NAME("versionlong", VAR_NUMBER), NULL, VV_RO},
149 {VV_NAME("echospace", VAR_NUMBER), NULL, VV_RO},
150 {VV_NAME("argv", VAR_LIST), &t_list_string, VV_RO},
151 {VV_NAME("collate", VAR_STRING), NULL, VV_RO},
152 {VV_NAME("exiting", VAR_SPECIAL), NULL, VV_RO},
153 {VV_NAME("colornames", VAR_DICT), &t_dict_string, VV_RO},
154 {VV_NAME("sizeofint", VAR_NUMBER), NULL, VV_RO},
155 {VV_NAME("sizeoflong", VAR_NUMBER), NULL, VV_RO},
156 {VV_NAME("sizeofpointer", VAR_NUMBER), NULL, VV_RO},
naohiro ono56200ee2022-01-01 14:59:44 +0000157 {VV_NAME("maxcol", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200158};
159
160// shorthand
Bram Moolenaard787e402021-12-24 21:36:12 +0000161#define vv_tv_type vv_di.di_tv.v_type
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200162#define vv_nr vv_di.di_tv.vval.v_number
163#define vv_float vv_di.di_tv.vval.v_float
164#define vv_str vv_di.di_tv.vval.v_string
165#define vv_list vv_di.di_tv.vval.v_list
166#define vv_dict vv_di.di_tv.vval.v_dict
167#define vv_blob vv_di.di_tv.vval.v_blob
168#define vv_tv vv_di.di_tv
169
170static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200171static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200172#define vimvarht vimvardict.dv_hashtab
173
174// for VIM_VERSION_ defines
175#include "version.h"
176
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200177static void list_glob_vars(int *first);
178static void list_buf_vars(int *first);
179static void list_win_vars(int *first);
180static void list_tab_vars(int *first);
181static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100182static 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 +0200183static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
184static 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 +0200185static void list_one_var(dictitem_T *v, char *prefix, int *first);
186static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
187
188/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200189 * Initialize global and vim special variables
190 */
191 void
192evalvars_init(void)
193{
194 int i;
195 struct vimvar *p;
196
197 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
198 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
199 vimvardict.dv_lock = VAR_FIXED;
200 hash_init(&compat_hashtab);
201
202 for (i = 0; i < VV_LEN; ++i)
203 {
204 p = &vimvars[i];
205 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
206 {
207 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
208 getout(1);
209 }
210 STRCPY(p->vv_di.di_key, p->vv_name);
211 if (p->vv_flags & VV_RO)
212 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
213 else if (p->vv_flags & VV_RO_SBX)
214 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
215 else
216 p->vv_di.di_flags = DI_FLAGS_FIX;
217
218 // add to v: scope dict, unless the value is not always available
Bram Moolenaard787e402021-12-24 21:36:12 +0000219 if (p->vv_tv_type != VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200220 hash_add(&vimvarht, p->vv_di.di_key);
221 if (p->vv_flags & VV_COMPAT)
222 // add to compat scope dict
223 hash_add(&compat_hashtab, p->vv_di.di_key);
224 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200225 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
226 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200227
228 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
229 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100230 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200231 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
232 set_vim_var_list(VV_ERRORS, list_alloc());
233 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
234
235 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
236 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
237 set_vim_var_nr(VV_NONE, VVAL_NONE);
238 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100239 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
240 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100241 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaar69b30722021-11-02 21:39:49 +0000242 set_vim_var_nr(VV_SIZEOFINT, sizeof(int));
243 set_vim_var_nr(VV_SIZEOFLONG, sizeof(long));
244 set_vim_var_nr(VV_SIZEOFPOINTER, sizeof(char *));
naohiro ono56200ee2022-01-01 14:59:44 +0000245 set_vim_var_nr(VV_MAXCOL, MAXCOL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200246
247 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
248 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
249 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
250 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
251 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
252 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
253 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
254 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
255 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
256 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
257 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
258
259 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
260
Drew Vogele30d1022021-10-24 20:35:07 +0100261 set_vim_var_dict(VV_COLORNAMES, dict_alloc());
262
Bram Moolenaar439c0362020-06-06 15:58:03 +0200263 // Default for v:register is not 0 but '"'. This is adjusted once the
264 // clipboard has been setup by calling reset_reg_var().
265 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200266}
267
268#if defined(EXITFREE) || defined(PROTO)
269/*
270 * Free all vim variables information on exit
271 */
272 void
273evalvars_clear(void)
274{
275 int i;
276 struct vimvar *p;
277
278 for (i = 0; i < VV_LEN; ++i)
279 {
280 p = &vimvars[i];
281 if (p->vv_di.di_tv.v_type == VAR_STRING)
282 VIM_CLEAR(p->vv_str);
283 else if (p->vv_di.di_tv.v_type == VAR_LIST)
284 {
285 list_unref(p->vv_list);
286 p->vv_list = NULL;
287 }
288 }
289 hash_clear(&vimvarht);
290 hash_init(&vimvarht); // garbage_collect() will access it
291 hash_clear(&compat_hashtab);
292
293 // global variables
294 vars_clear(&globvarht);
295
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100296 // Script-local variables. Clear all the variables here.
297 // The scriptvar_T is cleared later in free_scriptnames(), because a
298 // variable in one script might hold a reference to the whole scope of
299 // another script.
300 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200301 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200302}
303#endif
304
305 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200306garbage_collect_globvars(int copyID)
307{
308 return set_ref_in_ht(&globvarht, copyID, NULL);
309}
310
311 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200312garbage_collect_vimvars(int copyID)
313{
314 return set_ref_in_ht(&vimvarht, copyID, NULL);
315}
316
317 int
318garbage_collect_scriptvars(int copyID)
319{
Bram Moolenaared234f22020-10-15 20:42:20 +0200320 int i;
321 int idx;
322 int abort = FALSE;
323 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200324
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100325 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200326 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200327 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
328
Bram Moolenaared234f22020-10-15 20:42:20 +0200329 si = SCRIPT_ITEM(i);
330 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
331 {
332 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
333
Bram Moolenaard00a7fb2021-03-08 20:47:14 +0100334 if (sv->sv_name != NULL)
335 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
Bram Moolenaared234f22020-10-15 20:42:20 +0200336 }
337 }
338
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200339 return abort;
340}
341
342/*
343 * Set an internal variable to a string value. Creates the variable if it does
344 * not already exist.
345 */
346 void
347set_internal_string_var(char_u *name, char_u *value)
348{
349 char_u *val;
350 typval_T *tvp;
351
352 val = vim_strsave(value);
353 if (val != NULL)
354 {
355 tvp = alloc_string_tv(val);
356 if (tvp != NULL)
357 {
358 set_var(name, tvp, FALSE);
359 free_tv(tvp);
360 }
361 }
362}
363
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200364 int
365eval_charconvert(
366 char_u *enc_from,
367 char_u *enc_to,
368 char_u *fname_from,
369 char_u *fname_to)
370{
371 int err = FALSE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000372 sctx_T saved_sctx = current_sctx;
373 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200374
375 set_vim_var_string(VV_CC_FROM, enc_from, -1);
376 set_vim_var_string(VV_CC_TO, enc_to, -1);
377 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
378 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000379 ctx = get_option_sctx("charconvert");
380 if (ctx != NULL)
381 current_sctx = *ctx;
382
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200383 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
384 err = TRUE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000385
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200386 set_vim_var_string(VV_CC_FROM, NULL, -1);
387 set_vim_var_string(VV_CC_TO, NULL, -1);
388 set_vim_var_string(VV_FNAME_IN, NULL, -1);
389 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000390 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200391
392 if (err)
393 return FAIL;
394 return OK;
395}
396
397# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
398 int
399eval_printexpr(char_u *fname, char_u *args)
400{
401 int err = FALSE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000402 sctx_T saved_sctx = current_sctx;
403 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200404
405 set_vim_var_string(VV_FNAME_IN, fname, -1);
406 set_vim_var_string(VV_CMDARG, args, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000407 ctx = get_option_sctx("printexpr");
408 if (ctx != NULL)
409 current_sctx = *ctx;
410
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200411 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
412 err = TRUE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000413
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200414 set_vim_var_string(VV_FNAME_IN, NULL, -1);
415 set_vim_var_string(VV_CMDARG, NULL, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000416 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200417
418 if (err)
419 {
420 mch_remove(fname);
421 return FAIL;
422 }
423 return OK;
424}
425# endif
426
427# if defined(FEAT_DIFF) || defined(PROTO)
428 void
429eval_diff(
430 char_u *origfile,
431 char_u *newfile,
432 char_u *outfile)
433{
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000434 sctx_T saved_sctx = current_sctx;
435 sctx_T *ctx;
436 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200437
438 set_vim_var_string(VV_FNAME_IN, origfile, -1);
439 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
440 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000441
442 ctx = get_option_sctx("diffexpr");
443 if (ctx != NULL)
444 current_sctx = *ctx;
445
446 // errors are ignored
447 tv = eval_expr(p_dex, NULL);
Bram Moolenaar39b89442022-01-22 18:21:36 +0000448 free_tv(tv);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000449
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200450 set_vim_var_string(VV_FNAME_IN, NULL, -1);
451 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
452 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000453 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200454}
455
456 void
457eval_patch(
458 char_u *origfile,
459 char_u *difffile,
460 char_u *outfile)
461{
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000462 sctx_T saved_sctx = current_sctx;
463 sctx_T *ctx;
464 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200465
466 set_vim_var_string(VV_FNAME_IN, origfile, -1);
467 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
468 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000469
470 ctx = get_option_sctx("patchexpr");
471 if (ctx != NULL)
472 current_sctx = *ctx;
473
474 // errors are ignored
475 tv = eval_expr(p_pex, NULL);
476 free_tv(tv);
477
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200478 set_vim_var_string(VV_FNAME_IN, NULL, -1);
479 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
480 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000481 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200482}
483# endif
484
485#if defined(FEAT_SPELL) || defined(PROTO)
486/*
487 * Evaluate an expression to a list with suggestions.
488 * For the "expr:" part of 'spellsuggest'.
489 * Returns NULL when there is an error.
490 */
491 list_T *
492eval_spell_expr(char_u *badword, char_u *expr)
493{
494 typval_T save_val;
495 typval_T rettv;
496 list_T *list = NULL;
497 char_u *p = skipwhite(expr);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000498 sctx_T saved_sctx = current_sctx;
499 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200500
501 // Set "v:val" to the bad word.
502 prepare_vimvar(VV_VAL, &save_val);
503 set_vim_var_string(VV_VAL, badword, -1);
504 if (p_verbose == 0)
505 ++emsg_off;
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000506 ctx = get_option_sctx("spellsuggest");
507 if (ctx != NULL)
508 current_sctx = *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200509
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200510 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200511 {
512 if (rettv.v_type != VAR_LIST)
513 clear_tv(&rettv);
514 else
515 list = rettv.vval.v_list;
516 }
517
518 if (p_verbose == 0)
519 --emsg_off;
520 clear_tv(get_vim_var_tv(VV_VAL));
521 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000522 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200523
524 return list;
525}
526
527/*
528 * "list" is supposed to contain two items: a word and a number. Return the
529 * word in "pp" and the number as the return value.
530 * Return -1 if anything isn't right.
531 * Used to get the good word and score from the eval_spell_expr() result.
532 */
533 int
534get_spellword(list_T *list, char_u **pp)
535{
536 listitem_T *li;
537
538 li = list->lv_first;
539 if (li == NULL)
540 return -1;
541 *pp = tv_get_string(&li->li_tv);
542
543 li = li->li_next;
544 if (li == NULL)
545 return -1;
546 return (int)tv_get_number(&li->li_tv);
547}
548#endif
549
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200550/*
551 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200552 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200553 * When not used yet add the variable to the v: hashtable.
554 */
555 void
556prepare_vimvar(int idx, typval_T *save_tv)
557{
558 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200559 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaard787e402021-12-24 21:36:12 +0000560 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200561 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
562}
563
564/*
565 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200566 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200567 * When no longer defined, remove the variable from the v: hashtable.
568 */
569 void
570restore_vimvar(int idx, typval_T *save_tv)
571{
572 hashitem_T *hi;
573
574 vimvars[idx].vv_tv = *save_tv;
Bram Moolenaard787e402021-12-24 21:36:12 +0000575 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200576 {
577 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
578 if (HASHITEM_EMPTY(hi))
579 internal_error("restore_vimvar()");
580 else
581 hash_remove(&vimvarht, hi);
582 }
583}
584
585/*
586 * List Vim variables.
587 */
588 static void
589list_vim_vars(int *first)
590{
591 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
592}
593
594/*
595 * List script-local variables, if there is a script.
596 */
597 static void
598list_script_vars(int *first)
599{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200600 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200601 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
602 "s:", FALSE, first);
603}
604
605/*
LemonBoy2eaef102022-05-06 13:14:50 +0100606 * Evaluate all the Vim expressions ({expr}) in string "str" and return the
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100607 * resulting string. The caller must free the returned string.
608 */
LemonBoy2eaef102022-05-06 13:14:50 +0100609 char_u *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100610eval_all_expr_in_str(char_u *str)
611{
612 garray_T ga;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100613 char_u *p;
614 char_u save_c;
LemonBoy2eaef102022-05-06 13:14:50 +0100615 char_u *expr_val;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100616
617 ga_init2(&ga, 1, 80);
618 p = str;
619
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100620 while (*p != NUL)
621 {
LemonBoy2eaef102022-05-06 13:14:50 +0100622 char_u *lit_start;
623 char_u *block_start;
624 char_u *block_end;
625 int escaped_brace = FALSE;
626
627 // Look for a block start.
628 lit_start = p;
629 while (*p != '{' && *p != '}' && *p != NUL)
630 ++p;
631
632 if (*p != NUL && *p == p[1])
633 {
634 // Escaped brace, unescape and continue.
635 // Include the brace in the literal string.
636 ++p;
637 escaped_brace = TRUE;
638 }
639 else if (*p == '}')
640 {
641 semsg(_(e_stray_closing_curly_str), str);
642 ga_clear(&ga);
643 return NULL;
644 }
645
646 // Append the literal part.
647 ga_concat_len(&ga, lit_start, (size_t)(p - lit_start));
648
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100649 if (*p == NUL)
LemonBoy2eaef102022-05-06 13:14:50 +0100650 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100651
LemonBoy2eaef102022-05-06 13:14:50 +0100652 if (escaped_brace)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100653 {
LemonBoy2eaef102022-05-06 13:14:50 +0100654 // Skip the second brace.
655 ++p;
656 continue;
657 }
658
659 // Skip the opening {.
660 block_start = ++p;
661 block_end = block_start;
662 if (*block_start != NUL && skip_expr(&block_end, NULL) == FAIL)
663 {
664 ga_clear(&ga);
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100665 return NULL;
666 }
LemonBoy2eaef102022-05-06 13:14:50 +0100667 block_end = skipwhite(block_end);
668 // The block must be closed by a }.
669 if (*block_end != '}')
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100670 {
LemonBoy2eaef102022-05-06 13:14:50 +0100671 semsg(_(e_missing_close_curly_str), str);
672 ga_clear(&ga);
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100673 return NULL;
674 }
LemonBoy2eaef102022-05-06 13:14:50 +0100675 save_c = *block_end;
676 *block_end = NUL;
677 expr_val = eval_to_string(block_start, TRUE);
678 *block_end = save_c;
679 if (expr_val == NULL)
680 {
681 ga_clear(&ga);
682 return NULL;
683 }
684 ga_concat(&ga, expr_val);
685 vim_free(expr_val);
686
687 p = block_end + 1;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100688 }
689 ga_append(&ga, NUL);
690
691 return ga.ga_data;
692}
693
694/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200695 * Get a list of lines from a HERE document. The here document is a list of
696 * lines surrounded by a marker.
697 * cmd << {marker}
698 * {line1}
699 * {line2}
700 * ....
701 * {marker}
702 *
703 * The {marker} is a string. If the optional 'trim' word is supplied before the
704 * marker, then the leading indentation before the lines (matching the
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100705 * indentation in the "cmd" line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200706 *
707 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100708 * tcl, mzscheme), "script_get" is set to TRUE. In this case, if the marker is
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200709 * missing, then '.' is accepted as a marker.
710 *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100711 * When compiling a heredoc assignment to a variable in a Vim9 def function,
712 * "vim9compile" is set to TRUE. In this case, instead of generating a list of
713 * string values from the heredoc, vim9 instructions are generated. On success
714 * the returned list will be empty.
715 *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100716 * Returns a List with {lines} or NULL on failure.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200717 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718 list_T *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100719heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200720{
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100721 char_u *theline = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200722 char_u *marker;
723 list_T *l;
724 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100725 char_u *str;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200726 int marker_indent_len = 0;
727 int text_indent_len = 0;
728 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200729 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200730 int comment_char = in_vim9script() ? '#' : '"';
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100731 int evalstr = FALSE;
732 int eval_failed = FALSE;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100733 cctx_T *cctx = vim9compile ? eap->cookie : NULL;
734 int count = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200735
736 if (eap->getline == NULL)
737 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000738 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200739 return NULL;
740 }
741
742 // Check for the optional 'trim' word before the marker
743 cmd = skipwhite(cmd);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200744
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100745 while (TRUE)
746 {
747 if (STRNCMP(cmd, "trim", 4) == 0
748 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200749 {
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100750 cmd = skipwhite(cmd + 4);
751
752 // Trim the indentation from all the lines in the here document.
753 // The amount of indentation trimmed is the same as the indentation
754 // of the first line after the :let command line. To find the end
755 // marker the indent of the :let command line is trimmed.
756 p = *eap->cmdlinep;
757 while (VIM_ISWHITE(*p))
758 {
759 p++;
760 marker_indent_len++;
761 }
762 text_indent_len = -1;
763
764 continue;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200765 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100766 if (STRNCMP(cmd, "eval", 4) == 0
767 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
768 {
769 cmd = skipwhite(cmd + 4);
770 evalstr = TRUE;
771 continue;
772 }
773 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200774 }
775
776 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200777 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200778 {
779 marker = skipwhite(cmd);
780 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200781 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200782 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000783 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200784 return NULL;
785 }
786 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200787 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200788 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000789 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200790 return NULL;
791 }
792 }
793 else
794 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200795 // When getting lines for an embedded script, if the marker is missing,
796 // accept '.' as the marker.
797 if (script_get)
798 marker = dot;
799 else
800 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000801 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200802 return NULL;
803 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200804 }
805
806 l = list_alloc();
807 if (l == NULL)
808 return NULL;
809
810 for (;;)
811 {
812 int mi = 0;
813 int ti = 0;
814
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100815 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200816 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
817 if (theline == NULL)
818 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000819 semsg(_(e_missing_end_marker_str), marker);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200820 break;
821 }
822
823 // with "trim": skip the indent matching the :let line to find the
824 // marker
825 if (marker_indent_len > 0
826 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
827 mi = marker_indent_len;
828 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200829 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200830
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100831 // If expression evaluation failed in the heredoc, then skip till the
832 // end marker.
833 if (eval_failed)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100834 continue;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100835
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200836 if (text_indent_len == -1 && *theline != NUL)
837 {
838 // set the text indent from the first line.
839 p = theline;
840 text_indent_len = 0;
841 while (VIM_ISWHITE(*p))
842 {
843 p++;
844 text_indent_len++;
845 }
846 text_indent = vim_strnsave(theline, text_indent_len);
847 }
848 // with "trim": skip the indent matching the first line
849 if (text_indent != NULL)
850 for (ti = 0; ti < text_indent_len; ++ti)
851 if (theline[ti] != text_indent[ti])
852 break;
853
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100854 str = theline + ti;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100855 if (vim9compile)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100856 {
LemonBoy2eaef102022-05-06 13:14:50 +0100857 if (compile_all_expr_in_str(str, evalstr, cctx) == FAIL)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100858 {
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100859 vim_free(theline);
860 vim_free(text_indent);
861 return FAIL;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100862 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100863 count++;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100864 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100865 else
866 {
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100867 if (evalstr && !eap->skip)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100868 {
869 str = eval_all_expr_in_str(str);
870 if (str == NULL)
871 {
872 // expression evaluation failed
873 eval_failed = TRUE;
874 continue;
875 }
876 vim_free(theline);
877 theline = str;
878 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100879
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100880 if (list_append_string(l, str, -1) == FAIL)
881 break;
882 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200883 }
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100884 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200885 vim_free(text_indent);
886
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100887 if (vim9compile && cctx->ctx_skip != SKIP_YES && !eval_failed)
888 generate_NEWLIST(cctx, count, FALSE);
889
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100890 if (eval_failed)
891 {
892 // expression evaluation in the heredoc failed
893 list_free(l);
894 return NULL;
895 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200896 return l;
897}
898
899/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200900 * Vim9 variable declaration:
901 * ":var name"
902 * ":var name: type"
903 * ":var name = expr"
904 * ":var name: type = expr"
905 * etc.
906 */
907 void
908ex_var(exarg_T *eap)
909{
Bram Moolenaar330a3882022-03-05 11:05:57 +0000910 char_u *p = eap->cmd;
Bram Moolenaare1d12112022-03-05 11:37:48 +0000911 int has_var;
Bram Moolenaar330a3882022-03-05 11:05:57 +0000912
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200913 if (!in_vim9script())
914 {
915 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
916 return;
917 }
Bram Moolenaare1d12112022-03-05 11:37:48 +0000918 has_var = checkforcmd_noparen(&p, "var", 3);
919 if (current_sctx.sc_sid == 0 && has_var)
Bram Moolenaar0e1574c2022-03-03 17:05:35 +0000920 {
921 emsg(_(e_cannot_declare_variable_on_command_line));
922 return;
923 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200924 ex_let(eap);
925}
926
927/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200928 * ":let" list all variable values
929 * ":let var1 var2" list variable values
930 * ":let var = expr" assignment command.
931 * ":let var += expr" assignment command.
932 * ":let var -= expr" assignment command.
933 * ":let var *= expr" assignment command.
934 * ":let var /= expr" assignment command.
935 * ":let var %= expr" assignment command.
936 * ":let var .= expr" assignment command.
937 * ":let var ..= expr" assignment command.
938 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100939 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100940 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200941 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200942 * ":final var = expr" assignment command.
943 * ":final [var1, var2] = expr" unpack list.
944 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200945 * ":const" list all variable values
946 * ":const var1 var2" list variable values
947 * ":const var = expr" assignment command.
948 * ":const [var1, var2] = expr" unpack list.
949 */
950 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200951ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200952{
953 char_u *arg = eap->arg;
954 char_u *expr = NULL;
955 typval_T rettv;
956 int i;
957 int var_count = 0;
958 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200959 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200960 char_u *argend;
961 int first = TRUE;
962 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200963 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100964 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200965 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200966
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200967 if (eap->cmdidx == CMD_final && !vim9script)
968 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100969 // In legacy Vim script ":final" is short for ":finally".
970 ex_finally(eap);
971 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200972 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200973 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200974 {
975 emsg(_(e_cannot_use_let_in_vim9_script));
976 return;
977 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200978
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100979 if (eap->cmdidx == CMD_const)
980 flags |= ASSIGN_CONST;
981 else if (eap->cmdidx == CMD_final)
982 flags |= ASSIGN_FINAL;
983
984 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200986 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100987
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200988 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200989 if (argend == NULL)
990 return;
991 if (argend > arg && argend[-1] == '.') // for var.='str'
992 --argend;
993 expr = skipwhite(argend);
994 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +0200995 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200996 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200997 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
998 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200999 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001000 {
1001 // ":let" without "=": list variables
1002 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001003 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001004 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001005 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001006 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001007 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001008 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001009 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001010 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001011 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001012 else
1013 // Vim9 declaration ":var name: type"
1014 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001015 }
1016 else
1017 {
1018 // ":let var1 var2" - list values
1019 arg = list_arg_vars(eap, arg, &first);
1020 }
1021 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001022 else if (!eap->skip)
1023 {
1024 // ":let"
1025 list_glob_vars(&first);
1026 list_buf_vars(&first);
1027 list_win_vars(&first);
1028 list_tab_vars(&first);
1029 list_script_vars(&first);
1030 list_func_vars(&first);
1031 list_vim_vars(&first);
1032 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02001033 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001034 }
1035 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
1036 {
1037 list_T *l;
Bram Moolenaar81530e32021-07-28 21:25:49 +02001038 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001039
1040 // HERE document
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001041 l = heredoc_get(eap, expr + 3, FALSE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001042 if (l != NULL)
1043 {
1044 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001045 if (!eap->skip)
1046 {
Bram Moolenaar81530e32021-07-28 21:25:49 +02001047 // errors are for the assignment, not the end marker
1048 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001049 op[0] = '=';
1050 op[1] = NUL;
1051 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001052 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001053 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001054 clear_tv(&rettv);
1055 }
1056 }
1057 else
1058 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001059 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001060 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001061
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02001062 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001063 i = FAIL;
1064 if (has_assign || concat)
1065 {
Bram Moolenaar9a562c12021-01-23 13:39:14 +01001066 int cur_lnum;
1067
Bram Moolenaar32e35112020-05-14 22:41:15 +02001068 op[0] = '=';
1069 op[1] = NUL;
1070 if (*expr != '=')
1071 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001072 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +02001073 {
1074 // +=, /=, etc. require an existing variable
1075 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
1076 i = FAIL;
1077 }
1078 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +02001079 {
1080 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001081 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001082 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001083 {
Bram Moolenaar32e35112020-05-14 22:41:15 +02001084 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001085 ++len;
1086 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02001087 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001088 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001089 }
1090 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001091 ++expr;
1092
Bram Moolenaar7f2c3412021-11-29 16:01:49 +00001093 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
Bram Moolenaarc7e44a72020-07-29 21:37:43 +02001094 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001095 {
1096 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01001097 semsg(_(e_white_space_required_before_and_after_str_at_str),
1098 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001099 i = FAIL;
1100 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02001101
1102 if (eap->skip)
1103 ++emsg_skip;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001104 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaarc7e44a72020-07-29 21:37:43 +02001105 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaar9a562c12021-01-23 13:39:14 +01001106 cur_lnum = SOURCING_LNUM;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001107 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001108 if (eap->skip)
1109 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001110 clear_evalarg(&evalarg, eap);
Bram Moolenaar9a562c12021-01-23 13:39:14 +01001111
1112 // Restore the line number so that any type error is given for the
1113 // declaration, not the expression.
1114 SOURCING_LNUM = cur_lnum;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001115 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001116 if (eap->skip)
1117 {
1118 if (i != FAIL)
1119 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001120 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001121 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001122 {
1123 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001124 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001125 clear_tv(&rettv);
1126 }
1127 }
1128}
1129
1130/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001131 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001132 * Handles both "var" with any type and "[var, var; var]" with a list type.
1133 * When "op" is not NULL it points to a string with characters that
1134 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1135 * or concatenate.
1136 * Returns OK or FAIL;
1137 */
1138 int
1139ex_let_vars(
1140 char_u *arg_start,
1141 typval_T *tv,
1142 int copy, // copy values from "tv", don't move
1143 int semicolon, // from skip_var_list()
1144 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001145 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001146 char_u *op)
1147{
1148 char_u *arg = arg_start;
1149 list_T *l;
1150 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001151 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001152 listitem_T *item;
1153 typval_T ltv;
1154
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00001155 if (tv->v_type == VAR_VOID)
1156 {
1157 emsg(_(e_cannot_use_void_value));
1158 return FAIL;
1159 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001160 if (*arg != '[')
1161 {
1162 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001163 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001164 return FAIL;
1165 return OK;
1166 }
1167
1168 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1169 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
1170 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001171 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001172 return FAIL;
1173 }
1174
1175 i = list_len(l);
1176 if (semicolon == 0 && var_count < i)
1177 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001178 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001179 return FAIL;
1180 }
1181 if (var_count - semicolon > i)
1182 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001183 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001184 return FAIL;
1185 }
1186
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001187 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001188 item = l->lv_first;
1189 while (*arg != ']')
1190 {
1191 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001192 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001193 arg = ex_let_one(arg, &item->li_tv, TRUE,
1194 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001195 item = item->li_next;
1196 if (arg == NULL)
1197 return FAIL;
1198
1199 arg = skipwhite(arg);
1200 if (*arg == ';')
1201 {
1202 // Put the rest of the list (may be empty) in the var after ';'.
1203 // Create a new list for this.
1204 l = list_alloc();
1205 if (l == NULL)
1206 return FAIL;
1207 while (item != NULL)
1208 {
1209 list_append_tv(l, &item->li_tv);
1210 item = item->li_next;
1211 }
1212
1213 ltv.v_type = VAR_LIST;
1214 ltv.v_lock = 0;
1215 ltv.vval.v_list = l;
1216 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001217 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001218
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001219 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1220 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001221 clear_tv(&ltv);
1222 if (arg == NULL)
1223 return FAIL;
1224 break;
1225 }
1226 else if (*arg != ',' && *arg != ']')
1227 {
1228 internal_error("ex_let_vars()");
1229 return FAIL;
1230 }
1231 }
1232
1233 return OK;
1234}
1235
1236/*
1237 * Skip over assignable variable "var" or list of variables "[var, var]".
1238 * Used for ":let varvar = expr" and ":for varvar in expr".
1239 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001240 * for "[var, var; var]" set "semicolon" to 1.
1241 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001242 * Return NULL for an error.
1243 */
1244 char_u *
1245skip_var_list(
1246 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001247 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001248 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001249 int *semicolon,
1250 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001251{
1252 char_u *p, *s;
1253
1254 if (*arg == '[')
1255 {
1256 // "[var, var]": find the matching ']'.
1257 p = arg;
1258 for (;;)
1259 {
1260 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001261 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001262 if (s == p)
1263 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001264 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001265 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001266 return NULL;
1267 }
1268 ++*var_count;
1269
1270 p = skipwhite(s);
1271 if (*p == ']')
1272 break;
1273 else if (*p == ';')
1274 {
1275 if (*semicolon == 1)
1276 {
Bram Moolenaar8b716f52022-02-15 21:17:56 +00001277 if (!silent)
1278 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001279 return NULL;
1280 }
1281 *semicolon = 1;
1282 }
1283 else if (*p != ',')
1284 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001285 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001286 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001287 return NULL;
1288 }
1289 }
1290 return p + 1;
1291 }
1292 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001293 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001294}
1295
1296/*
1297 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1298 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001299 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001300 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001301 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001302skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001303{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001304 char_u *end;
1305 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001307 if (*arg == '@' && arg[1] != NUL)
1308 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001310 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001311
1312 // "a: type" is declaring variable "a" with a type, not "a:".
1313 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001314 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001315 --end;
1316
Bram Moolenaar585587d2021-01-17 20:52:13 +01001317 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001319 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001320 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 }
1322 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001323}
1324
1325/*
1326 * List variables for hashtab "ht" with prefix "prefix".
1327 * If "empty" is TRUE also list NULL strings as empty strings.
1328 */
1329 void
1330list_hashtable_vars(
1331 hashtab_T *ht,
1332 char *prefix,
1333 int empty,
1334 int *first)
1335{
1336 hashitem_T *hi;
1337 dictitem_T *di;
1338 int todo;
1339 char_u buf[IOSIZE];
1340
1341 todo = (int)ht->ht_used;
1342 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1343 {
1344 if (!HASHITEM_EMPTY(hi))
1345 {
1346 --todo;
1347 di = HI2DI(hi);
1348
1349 // apply :filter /pat/ to variable name
1350 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1351 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1352 if (message_filtered(buf))
1353 continue;
1354
1355 if (empty || di->di_tv.v_type != VAR_STRING
1356 || di->di_tv.vval.v_string != NULL)
1357 list_one_var(di, prefix, first);
1358 }
1359 }
1360}
1361
1362/*
1363 * List global variables.
1364 */
1365 static void
1366list_glob_vars(int *first)
1367{
1368 list_hashtable_vars(&globvarht, "", TRUE, first);
1369}
1370
1371/*
1372 * List buffer variables.
1373 */
1374 static void
1375list_buf_vars(int *first)
1376{
1377 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1378}
1379
1380/*
1381 * List window variables.
1382 */
1383 static void
1384list_win_vars(int *first)
1385{
1386 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1387}
1388
1389/*
1390 * List tab page variables.
1391 */
1392 static void
1393list_tab_vars(int *first)
1394{
1395 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1396}
1397
1398/*
1399 * List variables in "arg".
1400 */
1401 static char_u *
1402list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1403{
1404 int error = FALSE;
1405 int len;
1406 char_u *name;
1407 char_u *name_start;
1408 char_u *arg_subsc;
1409 char_u *tofree;
1410 typval_T tv;
1411
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001412 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001413 {
1414 if (error || eap->skip)
1415 {
1416 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1417 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1418 {
1419 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001420 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001421 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001422 break;
1423 }
1424 }
1425 else
1426 {
1427 // get_name_len() takes care of expanding curly braces
1428 name_start = name = arg;
1429 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1430 if (len <= 0)
1431 {
1432 // This is mainly to keep test 49 working: when expanding
1433 // curly braces fails overrule the exception error message.
1434 if (len < 0 && !aborting())
1435 {
1436 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001437 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001438 break;
1439 }
1440 error = TRUE;
1441 }
1442 else
1443 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001444 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001445 if (tofree != NULL)
1446 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001447 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001448 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001449 error = TRUE;
1450 else
1451 {
1452 // handle d.key, l[idx], f(expr)
1453 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001454 if (handle_subscript(&arg, name_start, &tv,
1455 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001456 error = TRUE;
1457 else
1458 {
1459 if (arg == arg_subsc && len == 2 && name[1] == ':')
1460 {
1461 switch (*name)
1462 {
1463 case 'g': list_glob_vars(first); break;
1464 case 'b': list_buf_vars(first); break;
1465 case 'w': list_win_vars(first); break;
1466 case 't': list_tab_vars(first); break;
1467 case 'v': list_vim_vars(first); break;
1468 case 's': list_script_vars(first); break;
1469 case 'l': list_func_vars(first); break;
1470 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001471 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001472 }
1473 }
1474 else
1475 {
1476 char_u numbuf[NUMBUFLEN];
1477 char_u *tf;
1478 int c;
1479 char_u *s;
1480
1481 s = echo_string(&tv, &tf, numbuf, 0);
1482 c = *arg;
1483 *arg = NUL;
1484 list_one_var_a("",
1485 arg == arg_subsc ? name : name_start,
1486 tv.v_type,
1487 s == NULL ? (char_u *)"" : s,
1488 first);
1489 *arg = c;
1490 vim_free(tf);
1491 }
1492 clear_tv(&tv);
1493 }
1494 }
1495 }
1496
1497 vim_free(tofree);
1498 }
1499
1500 arg = skipwhite(arg);
1501 }
1502
1503 return arg;
1504}
1505
1506/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001507 * Set an environment variable, part of ex_let_one().
1508 */
1509 static char_u *
1510ex_let_env(
1511 char_u *arg,
1512 typval_T *tv,
1513 int flags,
1514 char_u *endchars,
1515 char_u *op)
1516{
1517 char_u *arg_end = NULL;
1518 char_u *name;
1519 int len;
1520
1521 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1522 && (flags & ASSIGN_FOR_LOOP) == 0)
1523 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001524 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001525 return NULL;
1526 }
1527
1528 // Find the end of the name.
1529 ++arg;
1530 name = arg;
1531 len = get_env_len(&arg);
1532 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001533 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001534 else
1535 {
1536 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001537 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001538 else if (endchars != NULL
1539 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1540 emsg(_(e_unexpected_characters_in_let));
1541 else if (!check_secure())
1542 {
1543 char_u *tofree = NULL;
1544 int c1 = name[len];
1545 char_u *p;
1546
1547 name[len] = NUL;
1548 p = tv_get_string_chk(tv);
1549 if (p != NULL && op != NULL && *op == '.')
1550 {
1551 int mustfree = FALSE;
1552 char_u *s = vim_getenv(name, &mustfree);
1553
1554 if (s != NULL)
1555 {
1556 p = tofree = concat_str(s, p);
1557 if (mustfree)
1558 vim_free(s);
1559 }
1560 }
1561 if (p != NULL)
1562 {
1563 vim_setenv_ext(name, p);
1564 arg_end = arg;
1565 }
1566 name[len] = c1;
1567 vim_free(tofree);
1568 }
1569 }
1570 return arg_end;
1571}
1572
1573/*
1574 * Set an option, part of ex_let_one().
1575 */
1576 static char_u *
1577ex_let_option(
1578 char_u *arg,
1579 typval_T *tv,
1580 int flags,
1581 char_u *endchars,
1582 char_u *op)
1583{
1584 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001585 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001586 char_u *arg_end = NULL;
1587
1588 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1589 && (flags & ASSIGN_FOR_LOOP) == 0)
1590 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001591 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001592 return NULL;
1593 }
1594
1595 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001596 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001597 if (p == NULL || (endchars != NULL
1598 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1599 emsg(_(e_unexpected_characters_in_let));
1600 else
1601 {
1602 int c1;
1603 long n = 0;
1604 getoption_T opt_type;
1605 long numval;
1606 char_u *stringval = NULL;
1607 char_u *s = NULL;
1608 int failed = FALSE;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001609 int opt_p_flags;
1610 char_u *tofree = NULL;
Bram Moolenaar92c33eb2021-12-07 11:03:39 +00001611 char_u numbuf[NUMBUFLEN];
1612
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001613 c1 = *p;
1614 *p = NUL;
1615
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001616 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags,
1617 scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001618 if ((opt_type == gov_bool
1619 || opt_type == gov_number
1620 || opt_type == gov_hidden_bool
1621 || opt_type == gov_hidden_number)
1622 && (tv->v_type != VAR_STRING || !in_vim9script()))
1623 {
1624 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1625 // bool, possibly hidden
1626 n = (long)tv_get_bool(tv);
1627 else
1628 // number, possibly hidden
1629 n = (long)tv_get_number(tv);
1630 }
1631
Bram Moolenaaref082e12021-12-12 21:02:03 +00001632 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001633 || tv->v_type == VAR_FUNC))
1634 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001635 // If the option can be set to a function reference or a lambda
1636 // and the passed value is a function reference, then convert it to
1637 // the name (string) of the function reference.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001638 s = tv2string(tv, &tofree, numbuf, 0);
1639 }
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001640 // Avoid setting a string option to the text "v:false" or similar.
1641 // In Vim9 script also don't convert a number to string.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001642 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001643 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1644 s = tv_get_string_chk(tv);
1645
1646 if (op != NULL && *op != '=')
1647 {
1648 if (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1649 || (opt_type == gov_string && *op != '.'))
1650 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001651 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001652 failed = TRUE; // don't set the value
1653
1654 }
1655 else
1656 {
1657 // number, in legacy script also bool
1658 if (opt_type == gov_number
1659 || (opt_type == gov_bool && !in_vim9script()))
1660 {
1661 switch (*op)
1662 {
1663 case '+': n = numval + n; break;
1664 case '-': n = numval - n; break;
1665 case '*': n = numval * n; break;
1666 case '/': n = (long)num_divide(numval, n,
1667 &failed); break;
1668 case '%': n = (long)num_modulus(numval, n,
1669 &failed); break;
1670 }
1671 s = NULL;
1672 }
1673 else if (opt_type == gov_string
1674 && stringval != NULL && s != NULL)
1675 {
1676 // string
1677 s = concat_str(stringval, s);
1678 vim_free(stringval);
1679 stringval = s;
1680 }
1681 }
1682 }
1683
1684 if (!failed)
1685 {
1686 if (opt_type != gov_string || s != NULL)
1687 {
Bram Moolenaar8ccbbeb2022-03-02 19:49:38 +00001688 char *err = set_option_value(arg, n, s, scope);
1689
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001690 arg_end = p;
Bram Moolenaar8ccbbeb2022-03-02 19:49:38 +00001691 if (err != NULL)
1692 emsg(_(err));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001693 }
1694 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001695 emsg(_(e_string_required));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001696 }
1697 *p = c1;
1698 vim_free(stringval);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001699 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001700 }
1701 return arg_end;
1702}
1703
1704/*
1705 * Set a register, part of ex_let_one().
1706 */
1707 static char_u *
1708ex_let_register(
1709 char_u *arg,
1710 typval_T *tv,
1711 int flags,
1712 char_u *endchars,
1713 char_u *op)
1714{
1715 char_u *arg_end = NULL;
1716
1717 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1718 && (flags & ASSIGN_FOR_LOOP) == 0)
1719 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001720 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001721 return NULL;
1722 }
1723 ++arg;
1724 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001725 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001726 else if (endchars != NULL
1727 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1728 emsg(_(e_unexpected_characters_in_let));
1729 else
1730 {
1731 char_u *ptofree = NULL;
1732 char_u *p;
1733
1734 p = tv_get_string_chk(tv);
1735 if (p != NULL && op != NULL && *op == '.')
1736 {
1737 char_u *s = get_reg_contents(*arg == '@'
1738 ? '"' : *arg, GREG_EXPR_SRC);
1739
1740 if (s != NULL)
1741 {
1742 p = ptofree = concat_str(s, p);
1743 vim_free(s);
1744 }
1745 }
1746 if (p != NULL)
1747 {
1748 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1749 arg_end = arg + 1;
1750 }
1751 vim_free(ptofree);
1752 }
1753 return arg_end;
1754}
1755
1756/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001757 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1758 * Returns a pointer to the char just after the var name.
1759 * Returns NULL if there is an error.
1760 */
1761 static char_u *
1762ex_let_one(
1763 char_u *arg, // points to variable name
1764 typval_T *tv, // value to assign to variable
1765 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001766 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001767 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001768 char_u *op, // "+", "-", "." or NULL
1769 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001770{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001771 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001772
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001773 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001774 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001775 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1776 {
1777 vim9_declare_error(arg);
1778 return NULL;
1779 }
1780
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001781 if (*arg == '$')
1782 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001783 // ":let $VAR = expr": Set environment variable.
1784 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001785 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001786 else if (*arg == '&')
1787 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001788 // ":let &option = expr": Set option value.
1789 // ":let &l:option = expr": Set local option value.
1790 // ":let &g:option = expr": Set global option value.
1791 // ":for &ts in range(8)": Set option value for for loop
1792 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001793 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001794 else if (*arg == '@')
1795 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001796 // ":let @r = expr": Set register contents.
1797 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001798 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001799 else if (eval_isnamec1(*arg) || *arg == '{')
1800 {
1801 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001802 char_u *p;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001803 int lval_flags = (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1804 ? GLV_NO_DECL : 0;
1805 if (op != NULL && *op != '=')
1806 lval_flags |= GLV_ASSIGN_WITH_OP;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001807
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001808 // ":let var = expr": Set internal variable.
1809 // ":let var: type = expr": Set internal variable with type.
1810 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001811 p = get_lval(arg, tv, &lv, FALSE, FALSE, lval_flags, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001812 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001813 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001814 if (endchars != NULL && vim_strchr(endchars,
1815 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001816 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001817 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001818 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001819 else
1820 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001821 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001822 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001823 }
1824 }
1825 clear_lval(&lv);
1826 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001827 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001828 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001829
1830 return arg_end;
1831}
1832
1833/*
1834 * ":unlet[!] var1 ... " command.
1835 */
1836 void
1837ex_unlet(exarg_T *eap)
1838{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001839 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001840}
1841
1842/*
1843 * ":lockvar" and ":unlockvar" commands
1844 */
1845 void
1846ex_lockvar(exarg_T *eap)
1847{
1848 char_u *arg = eap->arg;
1849 int deep = 2;
1850
1851 if (eap->forceit)
1852 deep = -1;
1853 else if (vim_isdigit(*arg))
1854 {
1855 deep = getdigits(&arg);
1856 arg = skipwhite(arg);
1857 }
1858
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001859 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001860}
1861
1862/*
1863 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001864 * Also used for Vim9 script. "callback" is invoked as:
1865 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001866 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001867 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001868ex_unletlock(
1869 exarg_T *eap,
1870 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001871 int deep,
1872 int glv_flags,
1873 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1874 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001875{
1876 char_u *arg = argstart;
1877 char_u *name_end;
1878 int error = FALSE;
1879 lval_T lv;
1880
1881 do
1882 {
1883 if (*arg == '$')
1884 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001885 lv.ll_name = arg;
1886 lv.ll_tv = NULL;
1887 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001888 if (get_env_len(&arg) == 0)
1889 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001890 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001891 return;
1892 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001893 if (!error && !eap->skip
1894 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1895 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001896 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001897 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001898 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001899 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001900 // Parse the name and find the end.
1901 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001902 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001903 if (lv.ll_name == NULL)
1904 error = TRUE; // error but continue parsing
1905 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1906 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001907 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001908 if (name_end != NULL)
1909 {
1910 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00001911 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001912 }
1913 if (!(eap->skip || error))
1914 clear_lval(&lv);
1915 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001916 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001917
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001918 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001919 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001920 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001921
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001922 if (!eap->skip)
1923 clear_lval(&lv);
1924 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001925
1926 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001927 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001928
Bram Moolenaar63b91732021-08-05 20:40:03 +02001929 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001930}
1931
1932 static int
1933do_unlet_var(
1934 lval_T *lp,
1935 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001936 exarg_T *eap,
1937 int deep UNUSED,
1938 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001939{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001940 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001941 int ret = OK;
1942 int cc;
1943
1944 if (lp->ll_tv == NULL)
1945 {
1946 cc = *name_end;
1947 *name_end = NUL;
1948
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001949 // Environment variable, normal name or expanded name.
1950 if (*lp->ll_name == '$')
LemonBoy77142312022-04-15 20:50:46 +01001951 vim_unsetenv_ext(lp->ll_name + 1);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001952 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001953 ret = FAIL;
1954 *name_end = cc;
1955 }
1956 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001957 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001958 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001959 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001960 return FAIL;
1961 else if (lp->ll_range)
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00001962 list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_n1,
1963 !lp->ll_empty2, lp->ll_n2);
1964 else if (lp->ll_list != NULL)
1965 // unlet a List item.
1966 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001967 else
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00001968 // unlet a Dictionary item.
1969 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001970
1971 return ret;
1972}
1973
1974/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001975 * Unlet one item or a range of items from a list.
1976 * Return OK or FAIL.
1977 */
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00001978 void
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001979list_unlet_range(
1980 list_T *l,
1981 listitem_T *li_first,
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001982 long n1_arg,
1983 int has_n2,
1984 long n2)
1985{
1986 listitem_T *li = li_first;
1987 int n1 = n1_arg;
1988
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001989 // Delete a range of List items.
1990 li = li_first;
1991 n1 = n1_arg;
1992 while (li != NULL && (!has_n2 || n2 >= n1))
1993 {
1994 listitem_T *next = li->li_next;
1995
1996 listitem_remove(l, li);
1997 li = next;
1998 ++n1;
1999 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002000}
2001/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002002 * "unlet" a variable. Return OK if it existed, FAIL if not.
2003 * When "forceit" is TRUE don't complain if the variable doesn't exist.
2004 */
2005 int
2006do_unlet(char_u *name, int forceit)
2007{
2008 hashtab_T *ht;
2009 hashitem_T *hi;
2010 char_u *varname;
2011 dict_T *d;
2012 dictitem_T *di;
2013
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002014 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002015 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002016 return FAIL;
2017
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002018 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002019
2020 // can't :unlet a script variable in Vim9 script from a function
2021 if (ht == get_script_local_ht()
2022 && SCRIPT_ID_VALID(current_sctx.sc_sid)
2023 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2024 == SCRIPT_VERSION_VIM9
2025 && check_vim9_unlet(name) == FAIL)
2026 return FAIL;
2027
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002028 if (ht != NULL && *varname != NUL)
2029 {
2030 d = get_current_funccal_dict(ht);
2031 if (d == NULL)
2032 {
2033 if (ht == &globvarht)
2034 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002035 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002036 d = &vimvardict;
2037 else
2038 {
2039 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2040 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2041 }
2042 if (d == NULL)
2043 {
2044 internal_error("do_unlet()");
2045 return FAIL;
2046 }
2047 }
2048 hi = hash_find(ht, varname);
2049 if (HASHITEM_EMPTY(hi))
2050 hi = find_hi_in_scoped_ht(name, &ht);
2051 if (hi != NULL && !HASHITEM_EMPTY(hi))
2052 {
2053 di = HI2DI(hi);
2054 if (var_check_fixed(di->di_flags, name, FALSE)
2055 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02002056 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002057 return FAIL;
2058
2059 delete_var(ht, hi);
2060 return OK;
2061 }
2062 }
2063 if (forceit)
2064 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00002065 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002066 return FAIL;
2067}
2068
2069/*
2070 * Lock or unlock variable indicated by "lp".
2071 * "deep" is the levels to go (-1 for unlimited);
2072 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2073 */
2074 static int
2075do_lock_var(
2076 lval_T *lp,
2077 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002078 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002079 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002080 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002081{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002082 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002083 int ret = OK;
2084 int cc;
2085 dictitem_T *di;
2086
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002087 if (lp->ll_tv == NULL)
2088 {
2089 cc = *name_end;
2090 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002091 if (*lp->ll_name == '$')
2092 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002093 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002094 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002095 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002096 else
2097 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002098 // Normal name or expanded name.
2099 di = find_var(lp->ll_name, NULL, TRUE);
2100 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002101 {
2102 if (in_vim9script())
2103 semsg(_(e_cannot_find_variable_to_unlock_str),
2104 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002105 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002106 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002107 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002108 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002109 if ((di->di_flags & DI_FLAGS_FIX)
2110 && di->di_tv.v_type != VAR_DICT
2111 && di->di_tv.v_type != VAR_LIST)
2112 {
2113 // For historic reasons this error is not given for a list
2114 // or dict. E.g., the b: dict could be locked/unlocked.
2115 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
2116 ret = FAIL;
2117 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002118 else
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002119 {
2120 if (in_vim9script())
2121 {
2122 svar_T *sv = find_typval_in_script(&di->di_tv,
2123 0, FALSE);
2124
2125 if (sv != NULL && sv->sv_const != 0)
2126 {
2127 semsg(_(e_cannot_change_readonly_variable_str),
2128 lp->ll_name);
2129 ret = FAIL;
2130 }
2131 }
2132
2133 if (ret == OK)
2134 {
2135 if (lock)
2136 di->di_flags |= DI_FLAGS_LOCK;
2137 else
2138 di->di_flags &= ~DI_FLAGS_LOCK;
2139 if (deep != 0)
2140 item_lock(&di->di_tv, deep, lock, FALSE);
2141 }
2142 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002143 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002144 }
2145 *name_end = cc;
2146 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002147 else if (deep == 0)
2148 {
2149 // nothing to do
2150 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002151 else if (lp->ll_range)
2152 {
2153 listitem_T *li = lp->ll_li;
2154
2155 // (un)lock a range of List items.
2156 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2157 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02002158 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002159 li = li->li_next;
2160 ++lp->ll_n1;
2161 }
2162 }
2163 else if (lp->ll_list != NULL)
2164 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002165 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002166 else
2167 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002168 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002169
2170 return ret;
2171}
2172
2173/*
2174 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002175 * When "check_refcount" is TRUE do not lock a list or dict with a reference
2176 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002177 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002178 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02002179item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002180{
2181 static int recurse = 0;
2182 list_T *l;
2183 listitem_T *li;
2184 dict_T *d;
2185 blob_T *b;
2186 hashitem_T *hi;
2187 int todo;
2188
2189 if (recurse >= DICT_MAXNEST)
2190 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002191 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002192 return;
2193 }
2194 if (deep == 0)
2195 return;
2196 ++recurse;
2197
2198 // lock/unlock the item itself
2199 if (lock)
2200 tv->v_lock |= VAR_LOCKED;
2201 else
2202 tv->v_lock &= ~VAR_LOCKED;
2203
2204 switch (tv->v_type)
2205 {
2206 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002207 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002208 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002209 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002211 case VAR_STRING:
2212 case VAR_FUNC:
2213 case VAR_PARTIAL:
2214 case VAR_FLOAT:
2215 case VAR_SPECIAL:
2216 case VAR_JOB:
2217 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002218 case VAR_INSTR:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002219 break;
2220
2221 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002222 if ((b = tv->vval.v_blob) != NULL
2223 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002224 {
2225 if (lock)
2226 b->bv_lock |= VAR_LOCKED;
2227 else
2228 b->bv_lock &= ~VAR_LOCKED;
2229 }
2230 break;
2231 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002232 if ((l = tv->vval.v_list) != NULL
2233 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002234 {
2235 if (lock)
2236 l->lv_lock |= VAR_LOCKED;
2237 else
2238 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002239 if (deep < 0 || deep > 1)
2240 {
2241 if (l->lv_first == &range_list_item)
2242 l->lv_lock |= VAR_ITEMS_LOCKED;
2243 else
2244 {
2245 // recursive: lock/unlock the items the List contains
2246 CHECK_LIST_MATERIALIZE(l);
2247 FOR_ALL_LIST_ITEMS(l, li) item_lock(&li->li_tv,
2248 deep - 1, lock, check_refcount);
2249 }
2250 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002251 }
2252 break;
2253 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002254 if ((d = tv->vval.v_dict) != NULL
2255 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002256 {
2257 if (lock)
2258 d->dv_lock |= VAR_LOCKED;
2259 else
2260 d->dv_lock &= ~VAR_LOCKED;
2261 if (deep < 0 || deep > 1)
2262 {
2263 // recursive: lock/unlock the items the List contains
2264 todo = (int)d->dv_hashtab.ht_used;
2265 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2266 {
2267 if (!HASHITEM_EMPTY(hi))
2268 {
2269 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002270 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2271 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002272 }
2273 }
2274 }
2275 }
2276 }
2277 --recurse;
2278}
2279
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002280#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2281/*
2282 * Delete all "menutrans_" variables.
2283 */
2284 void
2285del_menutrans_vars(void)
2286{
2287 hashitem_T *hi;
2288 int todo;
2289
2290 hash_lock(&globvarht);
2291 todo = (int)globvarht.ht_used;
2292 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2293 {
2294 if (!HASHITEM_EMPTY(hi))
2295 {
2296 --todo;
2297 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2298 delete_var(&globvarht, hi);
2299 }
2300 }
2301 hash_unlock(&globvarht);
2302}
2303#endif
2304
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002305/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002306 * Local string buffer for the next two functions to store a variable name
2307 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2308 * get_user_var_name().
2309 */
2310
2311static char_u *varnamebuf = NULL;
2312static int varnamebuflen = 0;
2313
2314/*
2315 * Function to concatenate a prefix and a variable name.
2316 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002317 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002318cat_prefix_varname(int prefix, char_u *name)
2319{
2320 int len;
2321
2322 len = (int)STRLEN(name) + 3;
2323 if (len > varnamebuflen)
2324 {
2325 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002326 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002327 varnamebuf = alloc(len);
2328 if (varnamebuf == NULL)
2329 {
2330 varnamebuflen = 0;
2331 return NULL;
2332 }
2333 varnamebuflen = len;
2334 }
2335 *varnamebuf = prefix;
2336 varnamebuf[1] = ':';
2337 STRCPY(varnamebuf + 2, name);
2338 return varnamebuf;
2339}
2340
2341/*
2342 * Function given to ExpandGeneric() to obtain the list of user defined
2343 * (global/buffer/window/built-in) variable names.
2344 */
2345 char_u *
2346get_user_var_name(expand_T *xp, int idx)
2347{
2348 static long_u gdone;
2349 static long_u bdone;
2350 static long_u wdone;
2351 static long_u tdone;
2352 static int vidx;
2353 static hashitem_T *hi;
2354 hashtab_T *ht;
2355
2356 if (idx == 0)
2357 {
2358 gdone = bdone = wdone = vidx = 0;
2359 tdone = 0;
2360 }
2361
2362 // Global variables
2363 if (gdone < globvarht.ht_used)
2364 {
2365 if (gdone++ == 0)
2366 hi = globvarht.ht_array;
2367 else
2368 ++hi;
2369 while (HASHITEM_EMPTY(hi))
2370 ++hi;
2371 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2372 return cat_prefix_varname('g', hi->hi_key);
2373 return hi->hi_key;
2374 }
2375
2376 // b: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002377 ht = &prevwin_curwin()->w_buffer->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002378 if (bdone < ht->ht_used)
2379 {
2380 if (bdone++ == 0)
2381 hi = ht->ht_array;
2382 else
2383 ++hi;
2384 while (HASHITEM_EMPTY(hi))
2385 ++hi;
2386 return cat_prefix_varname('b', hi->hi_key);
2387 }
2388
2389 // w: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002390 ht = &prevwin_curwin()->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002391 if (wdone < ht->ht_used)
2392 {
2393 if (wdone++ == 0)
2394 hi = ht->ht_array;
2395 else
2396 ++hi;
2397 while (HASHITEM_EMPTY(hi))
2398 ++hi;
2399 return cat_prefix_varname('w', hi->hi_key);
2400 }
2401
2402 // t: variables
2403 ht = &curtab->tp_vars->dv_hashtab;
2404 if (tdone < ht->ht_used)
2405 {
2406 if (tdone++ == 0)
2407 hi = ht->ht_array;
2408 else
2409 ++hi;
2410 while (HASHITEM_EMPTY(hi))
2411 ++hi;
2412 return cat_prefix_varname('t', hi->hi_key);
2413 }
2414
2415 // v: variables
2416 if (vidx < VV_LEN)
2417 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2418
2419 VIM_CLEAR(varnamebuf);
2420 varnamebuflen = 0;
2421 return NULL;
2422}
2423
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002424 char *
2425get_var_special_name(int nr)
2426{
2427 switch (nr)
2428 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002429 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2430 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002431 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002432 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002433 }
2434 internal_error("get_var_special_name()");
2435 return "42";
2436}
2437
2438/*
2439 * Returns the global variable dictionary
2440 */
2441 dict_T *
2442get_globvar_dict(void)
2443{
2444 return &globvardict;
2445}
2446
2447/*
2448 * Returns the global variable hash table
2449 */
2450 hashtab_T *
2451get_globvar_ht(void)
2452{
2453 return &globvarht;
2454}
2455
2456/*
2457 * Returns the v: variable dictionary
2458 */
2459 dict_T *
2460get_vimvar_dict(void)
2461{
2462 return &vimvardict;
2463}
2464
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002465/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002467 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002468 */
2469 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002470find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002472 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2473 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474
2475 if (di == NULL)
2476 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002477 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2479 return (int)(vv - vimvars);
2480}
2481
2482
2483/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002484 * Set type of v: variable to "type".
2485 */
2486 void
2487set_vim_var_type(int idx, vartype_T type)
2488{
Bram Moolenaard787e402021-12-24 21:36:12 +00002489 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002490}
2491
2492/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002493 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002494 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002495 */
2496 void
2497set_vim_var_nr(int idx, varnumber_T val)
2498{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002499 vimvars[idx].vv_nr = val;
2500}
2501
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502 char *
2503get_vim_var_name(int idx)
2504{
2505 return vimvars[idx].vv_name;
2506}
2507
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002508/*
2509 * Get typval_T v: variable value.
2510 */
2511 typval_T *
2512get_vim_var_tv(int idx)
2513{
2514 return &vimvars[idx].vv_tv;
2515}
2516
Bram Moolenaard787e402021-12-24 21:36:12 +00002517 type_T *
2518get_vim_var_type(int idx, garray_T *type_list)
2519{
2520 if (vimvars[idx].vv_type != NULL)
2521 return vimvars[idx].vv_type;
2522 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2523}
2524
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002525/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002526 * Set v: variable to "tv". Only accepts the same type.
2527 * Takes over the value of "tv".
2528 */
2529 int
2530set_vim_var_tv(int idx, typval_T *tv)
2531{
Bram Moolenaard787e402021-12-24 21:36:12 +00002532 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002533 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002534 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002535 clear_tv(tv);
2536 return FAIL;
2537 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002538 // VV_RO is also checked when compiling, but let's check here as well.
2539 if (vimvars[idx].vv_flags & VV_RO)
2540 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002541 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002542 return FAIL;
2543 }
2544 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2545 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002546 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002547 return FAIL;
2548 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002549 clear_tv(&vimvars[idx].vv_di.di_tv);
2550 vimvars[idx].vv_di.di_tv = *tv;
2551 return OK;
2552}
2553
2554/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002555 * Get number v: variable value.
2556 */
2557 varnumber_T
2558get_vim_var_nr(int idx)
2559{
2560 return vimvars[idx].vv_nr;
2561}
2562
2563/*
2564 * Get string v: variable value. Uses a static buffer, can only be used once.
2565 * If the String variable has never been set, return an empty string.
2566 * Never returns NULL;
2567 */
2568 char_u *
2569get_vim_var_str(int idx)
2570{
2571 return tv_get_string(&vimvars[idx].vv_tv);
2572}
2573
2574/*
2575 * Get List v: variable value. Caller must take care of reference count when
2576 * needed.
2577 */
2578 list_T *
2579get_vim_var_list(int idx)
2580{
2581 return vimvars[idx].vv_list;
2582}
2583
2584/*
2585 * Get Dict v: variable value. Caller must take care of reference count when
2586 * needed.
2587 */
2588 dict_T *
2589get_vim_var_dict(int idx)
2590{
2591 return vimvars[idx].vv_dict;
2592}
2593
2594/*
2595 * Set v:char to character "c".
2596 */
2597 void
2598set_vim_var_char(int c)
2599{
2600 char_u buf[MB_MAXBYTES + 1];
2601
2602 if (has_mbyte)
2603 buf[(*mb_char2bytes)(c, buf)] = NUL;
2604 else
2605 {
2606 buf[0] = c;
2607 buf[1] = NUL;
2608 }
2609 set_vim_var_string(VV_CHAR, buf, -1);
2610}
2611
2612/*
2613 * Set v:count to "count" and v:count1 to "count1".
2614 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2615 */
2616 void
2617set_vcount(
2618 long count,
2619 long count1,
2620 int set_prevcount)
2621{
2622 if (set_prevcount)
2623 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2624 vimvars[VV_COUNT].vv_nr = count;
2625 vimvars[VV_COUNT1].vv_nr = count1;
2626}
2627
2628/*
2629 * Save variables that might be changed as a side effect. Used when executing
2630 * a timer callback.
2631 */
2632 void
2633save_vimvars(vimvars_save_T *vvsave)
2634{
2635 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2636 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2637 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2638}
2639
2640/*
2641 * Restore variables saved by save_vimvars().
2642 */
2643 void
2644restore_vimvars(vimvars_save_T *vvsave)
2645{
2646 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2647 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2648 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2649}
2650
2651/*
2652 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2653 * value.
2654 */
2655 void
2656set_vim_var_string(
2657 int idx,
2658 char_u *val,
2659 int len) // length of "val" to use or -1 (whole string)
2660{
2661 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002662 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002663 if (val == NULL)
2664 vimvars[idx].vv_str = NULL;
2665 else if (len == -1)
2666 vimvars[idx].vv_str = vim_strsave(val);
2667 else
2668 vimvars[idx].vv_str = vim_strnsave(val, len);
2669}
2670
2671/*
2672 * Set List v: variable to "val".
2673 */
2674 void
2675set_vim_var_list(int idx, list_T *val)
2676{
2677 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002678 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002679 vimvars[idx].vv_list = val;
2680 if (val != NULL)
2681 ++val->lv_refcount;
2682}
2683
2684/*
2685 * Set Dictionary v: variable to "val".
2686 */
2687 void
2688set_vim_var_dict(int idx, dict_T *val)
2689{
2690 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002691 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002692 vimvars[idx].vv_dict = val;
2693 if (val != NULL)
2694 {
2695 ++val->dv_refcount;
2696 dict_set_items_ro(val);
2697 }
2698}
2699
2700/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002701 * Set the v:argv list.
2702 */
2703 void
2704set_argv_var(char **argv, int argc)
2705{
2706 list_T *l = list_alloc();
2707 int i;
2708
2709 if (l == NULL)
2710 getout(1);
2711 l->lv_lock = VAR_FIXED;
2712 for (i = 0; i < argc; ++i)
2713 {
2714 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2715 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002716 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002717 }
2718 set_vim_var_list(VV_ARGV, l);
2719}
2720
2721/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002722 * Reset v:register, taking the 'clipboard' setting into account.
2723 */
2724 void
2725reset_reg_var(void)
2726{
2727 int regname = 0;
2728
2729 // Adjust the register according to 'clipboard', so that when
2730 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2731#ifdef FEAT_CLIPBOARD
2732 adjust_clip_reg(&regname);
2733#endif
2734 set_reg_var(regname);
2735}
2736
2737/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002738 * Set v:register if needed.
2739 */
2740 void
2741set_reg_var(int c)
2742{
2743 char_u regname;
2744
2745 if (c == 0 || c == ' ')
2746 regname = '"';
2747 else
2748 regname = c;
2749 // Avoid free/alloc when the value is already right.
2750 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2751 set_vim_var_string(VV_REG, &regname, 1);
2752}
2753
2754/*
2755 * Get or set v:exception. If "oldval" == NULL, return the current value.
2756 * Otherwise, restore the value to "oldval" and return NULL.
2757 * Must always be called in pairs to save and restore v:exception! Does not
2758 * take care of memory allocations.
2759 */
2760 char_u *
2761v_exception(char_u *oldval)
2762{
2763 if (oldval == NULL)
2764 return vimvars[VV_EXCEPTION].vv_str;
2765
2766 vimvars[VV_EXCEPTION].vv_str = oldval;
2767 return NULL;
2768}
2769
2770/*
2771 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2772 * Otherwise, restore the value to "oldval" and return NULL.
2773 * Must always be called in pairs to save and restore v:throwpoint! Does not
2774 * take care of memory allocations.
2775 */
2776 char_u *
2777v_throwpoint(char_u *oldval)
2778{
2779 if (oldval == NULL)
2780 return vimvars[VV_THROWPOINT].vv_str;
2781
2782 vimvars[VV_THROWPOINT].vv_str = oldval;
2783 return NULL;
2784}
2785
2786/*
2787 * Set v:cmdarg.
2788 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2789 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2790 * Must always be called in pairs!
2791 */
2792 char_u *
2793set_cmdarg(exarg_T *eap, char_u *oldarg)
2794{
2795 char_u *oldval;
2796 char_u *newval;
2797 unsigned len;
2798
2799 oldval = vimvars[VV_CMDARG].vv_str;
2800 if (eap == NULL)
2801 {
2802 vim_free(oldval);
2803 vimvars[VV_CMDARG].vv_str = oldarg;
2804 return NULL;
2805 }
2806
2807 if (eap->force_bin == FORCE_BIN)
2808 len = 6;
2809 else if (eap->force_bin == FORCE_NOBIN)
2810 len = 8;
2811 else
2812 len = 0;
2813
2814 if (eap->read_edit)
2815 len += 7;
2816
2817 if (eap->force_ff != 0)
2818 len += 10; // " ++ff=unix"
2819 if (eap->force_enc != 0)
2820 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2821 if (eap->bad_char != 0)
2822 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2823
2824 newval = alloc(len + 1);
2825 if (newval == NULL)
2826 return NULL;
2827
2828 if (eap->force_bin == FORCE_BIN)
2829 sprintf((char *)newval, " ++bin");
2830 else if (eap->force_bin == FORCE_NOBIN)
2831 sprintf((char *)newval, " ++nobin");
2832 else
2833 *newval = NUL;
2834
2835 if (eap->read_edit)
2836 STRCAT(newval, " ++edit");
2837
2838 if (eap->force_ff != 0)
2839 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2840 eap->force_ff == 'u' ? "unix"
2841 : eap->force_ff == 'd' ? "dos"
2842 : "mac");
2843 if (eap->force_enc != 0)
2844 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2845 eap->cmd + eap->force_enc);
2846 if (eap->bad_char == BAD_KEEP)
2847 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2848 else if (eap->bad_char == BAD_DROP)
2849 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2850 else if (eap->bad_char != 0)
2851 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2852 vimvars[VV_CMDARG].vv_str = newval;
2853 return oldval;
2854}
2855
2856/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002857 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002858 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2859 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002860 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2861 */
2862 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002863eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002864 char_u *name,
2865 int len, // length of "name"
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002866 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002867 typval_T *rettv, // NULL when only checking existence
2868 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002869 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002870{
2871 int ret = OK;
2872 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002873 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002874 hashtab_T *ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002875 int cc;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002876 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002877
2878 // truncate the name, so that we can use strcmp()
2879 cc = name[len];
2880 name[len] = NUL;
2881
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002882 // Check for local variable when debugging.
2883 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002884 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002885 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002886 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2887
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002888 if (v != NULL)
2889 {
2890 tv = &v->di_tv;
2891 if (dip != NULL)
2892 *dip = v;
2893 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002894 else
2895 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002896 }
2897
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002898 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002900 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002901 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2902
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002903 if (sid == 0)
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00002904 import = find_imported(p, 0, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905
2906 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002907 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002909 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002910 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00002911 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02002912 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002913 ht = &SCRIPT_VARS(sid);
2914 if (ht != NULL)
2915 {
2916 dictitem_T *v = find_var_in_ht(ht, 0, name,
2917 flags & EVAL_VAR_NOAUTOLOAD);
2918
2919 if (v != NULL)
2920 {
2921 tv = &v->di_tv;
2922 if (dip != NULL)
2923 *dip = v;
2924 }
2925 else
2926 ht = NULL;
2927 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002928 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002929 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002930 {
2931 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00002932 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002933 ret = FAIL;
2934 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002935 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002936 else
2937 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002938 if (rettv != NULL)
2939 {
2940 rettv->v_type = VAR_ANY;
2941 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
2942 }
2943 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002944 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00002946 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002947 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002948 int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002949 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002950
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002951 // In Vim9 script we can get a function reference by using the
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002952 // function name. For a global non-autoload function "g:" is
2953 // required.
2954 if (ufunc != NULL && (has_g_prefix
2955 || !func_requires_g_prefix(ufunc)))
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002956 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002957 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002958 if (rettv != NULL)
2959 {
2960 rettv->v_type = VAR_FUNC;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002961 if (has_g_prefix)
Bram Moolenaaref082e12021-12-12 21:02:03 +00002962 // Keep the "g:", otherwise script-local may be
2963 // assumed.
2964 rettv->vval.v_string = vim_strsave(name);
2965 else
2966 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002967 if (rettv->vval.v_string != NULL)
2968 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002969 }
2970 }
2971 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 }
2973
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002974 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002975 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002976 if (tv == NULL)
2977 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002978 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002979 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002980 ret = FAIL;
2981 }
2982 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002983 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01002984 svar_T *sv = NULL;
2985 int was_assigned = FALSE;
2986
Bram Moolenaar11005b02021-07-11 20:59:00 +02002987 if (ht != NULL && ht == get_script_local_ht()
2988 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002989 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01002990 sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaarf055d452021-07-08 20:57:24 +02002991 if (sv != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01002992 {
Bram Moolenaarf055d452021-07-08 20:57:24 +02002993 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01002994 was_assigned = sv->sv_flags & SVFLAG_ASSIGNED;
2995 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02002996 }
2997
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01002998 // If a list or dict variable wasn't initialized and has meaningful
2999 // type, do it now. Not for global variables, they are not
3000 // declared.
Bram Moolenaar7a222242022-03-01 19:23:24 +00003001 if (ht != &globvarht)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003002 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003003 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003004 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003005 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003006 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003007 tv->vval.v_dict = dict_alloc();
3008 if (tv->vval.v_dict != NULL)
3009 {
3010 ++tv->vval.v_dict->dv_refcount;
3011 tv->vval.v_dict->dv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003012 if (sv != NULL)
3013 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003014 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003015 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003016 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003017 && ((type != NULL && !was_assigned)
Bram Moolenaar501f9782022-03-27 16:51:04 +01003018 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003019 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003020 tv->vval.v_list = list_alloc();
3021 if (tv->vval.v_list != NULL)
3022 {
3023 ++tv->vval.v_list->lv_refcount;
3024 tv->vval.v_list->lv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003025 if (sv != NULL)
3026 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003027 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003028 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003029 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003030 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003031 || !in_vim9script()))
3032 {
3033 tv->vval.v_blob = blob_alloc();
3034 if (tv->vval.v_blob != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003035 {
Bram Moolenaar859cc212022-03-28 15:22:35 +01003036 ++tv->vval.v_blob->bv_refcount;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003037 if (sv != NULL)
3038 sv->sv_flags |= SVFLAG_ASSIGNED;
3039 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003040 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02003041 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003042 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003043 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003044 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003045
3046 name[len] = cc;
3047
3048 return ret;
3049}
3050
3051/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003052 * Check if variable "name[len]" is a local variable or an argument.
3053 * If so, "*eval_lavars_used" is set to TRUE.
3054 */
3055 void
3056check_vars(char_u *name, int len)
3057{
3058 int cc;
3059 char_u *varname;
3060 hashtab_T *ht;
3061
3062 if (eval_lavars_used == NULL)
3063 return;
3064
3065 // truncate the name, so that we can use strcmp()
3066 cc = name[len];
3067 name[len] = NUL;
3068
3069 ht = find_var_ht(name, &varname);
3070 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
3071 {
3072 if (find_var(name, NULL, TRUE) != NULL)
3073 *eval_lavars_used = TRUE;
3074 }
3075
3076 name[len] = cc;
3077}
3078
3079/*
3080 * Find variable "name" in the list of variables.
3081 * Return a pointer to it if found, NULL if not found.
3082 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003083 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003084 */
3085 dictitem_T *
3086find_var(char_u *name, hashtab_T **htp, int no_autoload)
3087{
3088 char_u *varname;
3089 hashtab_T *ht;
3090 dictitem_T *ret = NULL;
3091
3092 ht = find_var_ht(name, &varname);
3093 if (htp != NULL)
3094 *htp = ht;
3095 if (ht == NULL)
3096 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003097 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003098 if (ret != NULL)
3099 return ret;
3100
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003101 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003102 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003103 if (ret != NULL)
3104 return ret;
3105
3106 // in Vim9 script items without a scope can be script-local
3107 if (in_vim9script() && name[0] != NUL && name[1] != ':')
3108 {
3109 ht = get_script_local_ht();
3110 if (ht != NULL)
3111 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003112 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003113 if (ret != NULL)
3114 {
3115 if (htp != NULL)
3116 *htp = ht;
3117 return ret;
3118 }
3119 }
3120 }
3121
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003122 // When using "vim9script autoload" script-local items are prefixed but can
3123 // be used with s:name.
3124 if (SCRIPT_ID_VALID(current_sctx.sc_sid)
3125 && name[0] == 's' && name[1] == ':')
3126 {
3127 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
3128
3129 if (si->sn_autoload_prefix != NULL)
3130 {
3131 char_u *auto_name = concat_str(si->sn_autoload_prefix, name + 2);
3132
3133 if (auto_name != NULL)
3134 {
3135 ht = &globvarht;
3136 ret = find_var_in_ht(ht, *name, auto_name, TRUE);
Bram Moolenaar130f65d2022-01-13 20:39:41 +00003137 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003138 if (ret != NULL)
3139 {
3140 if (htp != NULL)
3141 *htp = ht;
3142 return ret;
3143 }
3144 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003145 }
3146 }
3147
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003148 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003149}
3150
3151/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00003152 * Like find_var() but if the name starts with <SNR>99_ then look in the
3153 * referenced script (used for a funcref).
3154 */
3155 dictitem_T *
3156find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
3157{
3158 if (STRNCMP(name, "<SNR>", 5) == 0 && isdigit(name[5]))
3159 {
3160 char_u *p = name + 5;
3161 int sid = getdigits(&p);
3162
3163 if (SCRIPT_ID_VALID(sid) && *p == '_')
3164 {
3165 hashtab_T *ht = &SCRIPT_VARS(sid);
3166
3167 if (ht != NULL)
3168 {
3169 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
3170
3171 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003172 {
3173 if (htp != NULL)
3174 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00003175 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003176 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00003177 }
3178 }
3179 }
3180
3181 return find_var(name, htp, no_autoload);
3182}
3183
3184/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003185 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02003186 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003187 * Returns NULL if not found.
3188 */
3189 dictitem_T *
3190find_var_in_ht(
3191 hashtab_T *ht,
3192 int htname,
3193 char_u *varname,
3194 int no_autoload)
3195{
3196 hashitem_T *hi;
3197
3198 if (*varname == NUL)
3199 {
3200 // Must be something like "s:", otherwise "ht" would be NULL.
3201 switch (htname)
3202 {
3203 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
3204 case 'g': return &globvars_var;
3205 case 'v': return &vimvars_var;
3206 case 'b': return &curbuf->b_bufvar;
3207 case 'w': return &curwin->w_winvar;
3208 case 't': return &curtab->tp_winvar;
3209 case 'l': return get_funccal_local_var();
3210 case 'a': return get_funccal_args_var();
3211 }
3212 return NULL;
3213 }
3214
3215 hi = hash_find(ht, varname);
3216 if (HASHITEM_EMPTY(hi))
3217 {
3218 // For global variables we may try auto-loading the script. If it
3219 // worked find the variable again. Don't auto-load a script if it was
3220 // loaded already, otherwise it would be loaded every time when
3221 // checking if a function name is a Funcref variable.
3222 if (ht == &globvarht && !no_autoload)
3223 {
3224 // Note: script_autoload() may make "hi" invalid. It must either
3225 // be obtained again or not used.
3226 if (!script_autoload(varname, FALSE) || aborting())
3227 return NULL;
3228 hi = hash_find(ht, varname);
3229 }
3230 if (HASHITEM_EMPTY(hi))
3231 return NULL;
3232 }
3233 return HI2DI(hi);
3234}
3235
3236/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 * Get the script-local hashtab. NULL if not in a script context.
3238 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003239 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240get_script_local_ht(void)
3241{
3242 scid_T sid = current_sctx.sc_sid;
3243
Bram Moolenaare3d46852020-08-29 13:39:17 +02003244 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 return &SCRIPT_VARS(sid);
3246 return NULL;
3247}
3248
3249/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003250 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003251 * When "cmd" is TRUE it must look like a command, a function must be followed
3252 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003253 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003255 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003256lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003257 char_u *name,
3258 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003259 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003260 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261{
3262 hashtab_T *ht = get_script_local_ht();
3263 char_u buffer[30];
3264 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003265 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003267 int is_global = FALSE;
3268 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269
3270 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003271 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 if (len < sizeof(buffer) - 1)
3273 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003274 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 vim_strncpy(buffer, name, len);
3276 p = buffer;
3277 }
3278 else
3279 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003280 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003282 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283 }
3284
3285 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003286 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287
3288 // if not script-local, then perhaps imported
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003289 if (res == FAIL && find_imported(p, 0, FALSE) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003290 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 if (p != buffer)
3292 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003293
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003294 // Find a function, so that a following "->" works.
3295 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3296 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003297 if (res != OK)
3298 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003299 p = skipwhite(name + len);
3300
3301 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003302 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003303 // Do not check for an internal function, since it might also be a
3304 // valid command, such as ":split" versus "split()".
3305 // Skip "g:" before a function name.
3306 if (name[0] == 'g' && name[1] == ':')
3307 {
3308 is_global = TRUE;
3309 fname = name + 2;
3310 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003311 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003312 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003313 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003314 }
3315
Bram Moolenaar709664c2020-12-12 14:33:41 +01003316 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317}
3318
3319/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003320 * Find the hashtab used for a variable name.
3321 * Return NULL if the name is not valid.
3322 * Set "varname" to the start of name without ':'.
3323 */
3324 hashtab_T *
3325find_var_ht(char_u *name, char_u **varname)
3326{
3327 hashitem_T *hi;
3328 hashtab_T *ht;
3329
3330 if (name[0] == NUL)
3331 return NULL;
3332 if (name[1] != ':')
3333 {
3334 // The name must not start with a colon or #.
3335 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3336 return NULL;
3337 *varname = name;
3338
3339 // "version" is "v:version" in all scopes if scriptversion < 3.
3340 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003341 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003342 {
3343 hi = hash_find(&compat_hashtab, name);
3344 if (!HASHITEM_EMPTY(hi))
3345 return &compat_hashtab;
3346 }
3347
3348 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349 if (ht != NULL)
3350 return ht; // local variable
3351
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003352 // In Vim9 script items at the script level are script-local, except
3353 // for autoload names.
3354 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 {
3356 ht = get_script_local_ht();
3357 if (ht != NULL)
3358 return ht;
3359 }
3360
3361 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003362 }
3363 *varname = name + 2;
3364 if (*name == 'g') // global variable
3365 return &globvarht;
3366 // There must be no ':' or '#' in the rest of the name, unless g: is used
3367 if (vim_strchr(name + 2, ':') != NULL
3368 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3369 return NULL;
3370 if (*name == 'b') // buffer variable
3371 return &curbuf->b_vars->dv_hashtab;
3372 if (*name == 'w') // window variable
3373 return &curwin->w_vars->dv_hashtab;
3374 if (*name == 't') // tab page variable
3375 return &curtab->tp_vars->dv_hashtab;
3376 if (*name == 'v') // v: variable
3377 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003378 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003379 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003381 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 if (*name == 'a') // a: function argument
3383 return get_funccal_args_ht();
3384 if (*name == 'l') // l: local function variable
3385 return get_funccal_local_ht();
3386 }
3387 if (*name == 's') // script variable
3388 {
3389 ht = get_script_local_ht();
3390 if (ht != NULL)
3391 return ht;
3392 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003393 return NULL;
3394}
3395
3396/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003397 * Get the string value of a (global/local) variable.
3398 * Note: see tv_get_string() for how long the pointer remains valid.
3399 * Returns NULL when it doesn't exist.
3400 */
3401 char_u *
3402get_var_value(char_u *name)
3403{
3404 dictitem_T *v;
3405
3406 v = find_var(name, NULL, FALSE);
3407 if (v == NULL)
3408 return NULL;
3409 return tv_get_string(&v->di_tv);
3410}
3411
3412/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003413 * Allocate a new hashtab for a sourced script. It will be used while
3414 * sourcing this script and when executing functions defined in the script.
3415 */
3416 void
3417new_script_vars(scid_T id)
3418{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003419 scriptvar_T *sv;
3420
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003421 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3422 if (sv == NULL)
3423 return;
3424 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003425 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003426}
3427
3428/*
3429 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3430 * point to it.
3431 */
3432 void
3433init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3434{
3435 hash_init(&dict->dv_hashtab);
3436 dict->dv_lock = 0;
3437 dict->dv_scope = scope;
3438 dict->dv_refcount = DO_NOT_FREE_CNT;
3439 dict->dv_copyID = 0;
3440 dict_var->di_tv.vval.v_dict = dict;
3441 dict_var->di_tv.v_type = VAR_DICT;
3442 dict_var->di_tv.v_lock = VAR_FIXED;
3443 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3444 dict_var->di_key[0] = NUL;
3445}
3446
3447/*
3448 * Unreference a dictionary initialized by init_var_dict().
3449 */
3450 void
3451unref_var_dict(dict_T *dict)
3452{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003453 // Now the dict needs to be freed if no one else is using it, go back to
3454 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003455 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3456 dict_unref(dict);
3457}
3458
3459/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003460 * Clean up a list of internal variables.
3461 * Frees all allocated variables and the value they contain.
3462 * Clears hashtab "ht", does not free it.
3463 */
3464 void
3465vars_clear(hashtab_T *ht)
3466{
3467 vars_clear_ext(ht, TRUE);
3468}
3469
3470/*
3471 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3472 */
3473 void
3474vars_clear_ext(hashtab_T *ht, int free_val)
3475{
3476 int todo;
3477 hashitem_T *hi;
3478 dictitem_T *v;
3479
3480 hash_lock(ht);
3481 todo = (int)ht->ht_used;
3482 for (hi = ht->ht_array; todo > 0; ++hi)
3483 {
3484 if (!HASHITEM_EMPTY(hi))
3485 {
3486 --todo;
3487
3488 // Free the variable. Don't remove it from the hashtab,
3489 // ht_array might change then. hash_clear() takes care of it
3490 // later.
3491 v = HI2DI(hi);
3492 if (free_val)
3493 clear_tv(&v->di_tv);
3494 if (v->di_flags & DI_FLAGS_ALLOC)
3495 vim_free(v);
3496 }
3497 }
3498 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003499 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003500}
3501
3502/*
3503 * Delete a variable from hashtab "ht" at item "hi".
3504 * Clear the variable value and free the dictitem.
3505 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003506 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003507delete_var(hashtab_T *ht, hashitem_T *hi)
3508{
3509 dictitem_T *di = HI2DI(hi);
3510
3511 hash_remove(ht, hi);
3512 clear_tv(&di->di_tv);
3513 vim_free(di);
3514}
3515
3516/*
3517 * List the value of one internal variable.
3518 */
3519 static void
3520list_one_var(dictitem_T *v, char *prefix, int *first)
3521{
3522 char_u *tofree;
3523 char_u *s;
3524 char_u numbuf[NUMBUFLEN];
3525
3526 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3527 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3528 s == NULL ? (char_u *)"" : s, first);
3529 vim_free(tofree);
3530}
3531
3532 static void
3533list_one_var_a(
3534 char *prefix,
3535 char_u *name,
3536 int type,
3537 char_u *string,
3538 int *first) // when TRUE clear rest of screen and set to FALSE
3539{
3540 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3541 msg_start();
3542 msg_puts(prefix);
3543 if (name != NULL) // "a:" vars don't have a name stored
3544 msg_puts((char *)name);
3545 msg_putchar(' ');
3546 msg_advance(22);
3547 if (type == VAR_NUMBER)
3548 msg_putchar('#');
3549 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3550 msg_putchar('*');
3551 else if (type == VAR_LIST)
3552 {
3553 msg_putchar('[');
3554 if (*string == '[')
3555 ++string;
3556 }
3557 else if (type == VAR_DICT)
3558 {
3559 msg_putchar('{');
3560 if (*string == '{')
3561 ++string;
3562 }
3563 else
3564 msg_putchar(' ');
3565
3566 msg_outtrans(string);
3567
3568 if (type == VAR_FUNC || type == VAR_PARTIAL)
3569 msg_puts("()");
3570 if (*first)
3571 {
3572 msg_clr_eos();
3573 *first = FALSE;
3574 }
3575}
3576
3577/*
3578 * Set variable "name" to value in "tv".
3579 * If the variable already exists, the value is updated.
3580 * Otherwise the variable is created.
3581 */
3582 void
3583set_var(
3584 char_u *name,
3585 typval_T *tv,
3586 int copy) // make copy of value in "tv"
3587{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003588 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003589}
3590
3591/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003592 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003593 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003594 * If the variable already exists and "is_const" is FALSE the value is updated.
3595 * Otherwise the variable is created.
3596 */
3597 void
3598set_var_const(
3599 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003600 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003601 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003602 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003603 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003604 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003605 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003606{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003607 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003608 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003609 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 dictitem_T *di;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003611 typval_T *dest_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003612 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003613 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003614 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003616 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003617 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003618 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003619 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003620 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003621
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003622 if (sid != 0)
3623 {
3624 if (SCRIPT_ID_VALID(sid))
3625 ht = &SCRIPT_VARS(sid);
3626 varname = name;
3627 }
3628 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003629 {
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003630 scriptitem_T *si;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003631
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003632 if (in_vim9script() && is_export
3633 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3634 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
Bram Moolenaar0e1574c2022-03-03 17:05:35 +00003635 ->sn_autoload_prefix != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003636 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003637 // In a vim9 autoload script an exported variable is put in the
3638 // global namespace with the autoload prefix.
3639 var_in_autoload = TRUE;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003640 varname = concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003641 if (varname == NULL)
3642 goto failed;
3643 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003644 ht = &globvarht;
3645 }
3646 else
3647 ht = find_var_ht(name, &varname);
3648 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003649 if (ht == NULL || *varname == NUL)
3650 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003651 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003652 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003653 }
Bram Moolenaar54969f42022-02-07 13:56:44 +00003654 is_script_local = ht == get_script_local_ht() || sid != 0
3655 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003656
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003657 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003658 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003659 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003660 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003661 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003662 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003663 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003664 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003665 }
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003666 if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
3667 && vim_strchr((char_u *)"gwbt", name[0]) != NULL)
3668 // Do not make g:var, w:var, b:var or t:var final.
3669 flags &= ~ASSIGN_FINAL;
3670
Bram Moolenaare535db82021-03-31 21:07:24 +02003671 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003672 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3673 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003674 // For "[a, _] = list" the underscore is ignored.
3675 if ((flags & ASSIGN_UNPACK) == 0)
3676 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003677 goto failed;
3678 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003679
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003681
Bram Moolenaar24e93162021-07-18 20:40:33 +02003682 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003683 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003684 imported_T *import = find_imported(varname, 0, FALSE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003685
Bram Moolenaar24e93162021-07-18 20:40:33 +02003686 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003687 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003688 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02003689 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003691 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003692 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003694 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3695 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003696 }
Bram Moolenaar75e27d72022-02-13 13:56:29 +00003697 if (!in_vim9script())
3698 {
3699 semsg(_(e_cannot_create_vim9_script_variable_in_function_str),
3700 name);
3701 goto failed;
3702 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003703 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704
Bram Moolenaar993faa32022-02-21 15:59:11 +00003705 // Search in parent scope which is possible to reference from lambda
3706 if (di == NULL)
3707 di = find_var_in_scoped_ht(name, TRUE);
3708
3709 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
3710 && var_wrong_func_name(name, di == NULL))
3711 goto failed;
3712
3713 if (need_convert_to_bool(type, tv))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003714 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003715 // Destination is a bool and the value is not, but it can be
3716 // converted.
3717 CLEAR_FIELD(bool_tv);
3718 bool_tv.v_type = VAR_BOOL;
3719 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3720 tv = &bool_tv;
3721 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003722
Bram Moolenaar993faa32022-02-21 15:59:11 +00003723 if (di != NULL)
3724 {
3725 // Item already exists. Allowed to replace when reloading.
3726 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003727 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003728 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
3729 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003730 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003731 emsg(_(e_cannot_modify_existing_variable));
3732 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003733 }
3734
Bram Moolenaar993faa32022-02-21 15:59:11 +00003735 if (is_script_local && vim9script
3736 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003737 {
3738 semsg(_(e_redefining_script_item_str), name);
3739 goto failed;
3740 }
3741
Bram Moolenaar993faa32022-02-21 15:59:11 +00003742 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003743 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003744 where_T where = WHERE_INIT;
Bram Moolenaar7a411a32022-04-04 14:58:06 +01003745 svar_T *sv = find_typval_in_script(&di->di_tv, sid, TRUE);
Bram Moolenaar993faa32022-02-21 15:59:11 +00003746
3747 if (sv != NULL)
3748 {
3749 // check the type and adjust to bool if needed
3750 where.wt_index = var_idx;
3751 where.wt_variable = TRUE;
3752 if (check_script_var_type(sv, tv, name, where) == FAIL)
3753 goto failed;
3754 if (type == NULL)
3755 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003756 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003757 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003758 }
3759
Bram Moolenaar993faa32022-02-21 15:59:11 +00003760 if ((flags & ASSIGN_FOR_LOOP) == 0
3761 && var_check_permission(di, name) == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003762 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003763 }
3764 else
3765 {
3766 // can only redefine once
3767 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003768
Bram Moolenaar993faa32022-02-21 15:59:11 +00003769 // A Vim9 script-local variable is also present in sn_all_vars
3770 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003771 if (var_in_vim9script || var_in_autoload)
Bram Moolenaar993faa32022-02-21 15:59:11 +00003772 update_vim9_script_var(FALSE, di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003773 var_in_autoload ? name : di->di_key, flags,
Bram Moolenaar993faa32022-02-21 15:59:11 +00003774 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003775 }
3776
Bram Moolenaar993faa32022-02-21 15:59:11 +00003777 // existing variable, need to clear the value
3778
3779 // Handle setting internal di: variables separately where needed to
3780 // prevent changing the type.
3781 if (ht == &vimvarht)
3782 {
3783 if (di->di_tv.v_type == VAR_STRING)
3784 {
3785 VIM_CLEAR(di->di_tv.vval.v_string);
3786 if (copy || tv->v_type != VAR_STRING)
3787 {
3788 char_u *val = tv_get_string(tv);
3789
3790 // Careful: when assigning to v:errmsg and
3791 // tv_get_string() causes an error message the variable
3792 // will already be set.
3793 if (di->di_tv.vval.v_string == NULL)
3794 di->di_tv.vval.v_string = vim_strsave(val);
3795 }
3796 else
3797 {
3798 // Take over the string to avoid an extra alloc/free.
3799 di->di_tv.vval.v_string = tv->vval.v_string;
3800 tv->vval.v_string = NULL;
3801 }
3802 goto failed;
3803 }
3804 else if (di->di_tv.v_type == VAR_NUMBER)
3805 {
3806 di->di_tv.vval.v_number = tv_get_number(tv);
3807 if (STRCMP(varname, "searchforward") == 0)
3808 set_search_direction(di->di_tv.vval.v_number
3809 ? '/' : '?');
3810#ifdef FEAT_SEARCH_EXTRA
3811 else if (STRCMP(varname, "hlsearch") == 0)
3812 {
3813 no_hlsearch = !di->di_tv.vval.v_number;
3814 redraw_all_later(SOME_VALID);
3815 }
3816#endif
3817 goto failed;
3818 }
3819 else if (di->di_tv.v_type != tv->v_type)
3820 {
3821 semsg(_(e_setting_str_to_value_with_wrong_type), name);
3822 goto failed;
3823 }
3824 }
3825
3826 clear_tv(&di->di_tv);
3827 }
3828 else
3829 {
3830 // Item not found, check if a function already exists.
3831 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
3832 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
3833 {
3834 semsg(_(e_redefining_script_item_str), name);
3835 goto failed;
3836 }
3837
3838 // add a new variable
3839 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3840 {
3841 semsg(_(e_unknown_variable_str), name);
3842 goto failed;
3843 }
3844
3845 // Can't add "v:" or "a:" variable.
3846 if (ht == &vimvarht || ht == get_funccal_args_ht())
3847 {
3848 semsg(_(e_illegal_variable_name_str), name);
3849 goto failed;
3850 }
3851
3852 // Make sure the variable name is valid. In Vim9 script an
3853 // autoload variable must be prefixed with "g:" unless in an
3854 // autoload script.
3855 if (!valid_varname(varname, -1, !vim9script
3856 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
3857 goto failed;
3858
3859 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3860 if (di == NULL)
3861 goto failed;
3862 STRCPY(di->di_key, varname);
3863 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
3864 {
3865 vim_free(di);
3866 goto failed;
3867 }
3868 di->di_flags = DI_FLAGS_ALLOC;
3869 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3870 di->di_flags |= DI_FLAGS_LOCK;
3871
3872 // A Vim9 script-local variable is also added to sn_all_vars and
3873 // sn_var_vals. It may set "type" from "tv".
3874 if (var_in_vim9script || var_in_autoload)
3875 update_vim9_script_var(TRUE, di,
3876 var_in_autoload ? name : di->di_key, flags,
3877 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003878 }
3879
Bram Moolenaar993faa32022-02-21 15:59:11 +00003880 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003881 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003882 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003883 else
3884 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003885 *dest_tv = *tv;
3886 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003887 init_tv(tv);
3888 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003889 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003890
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003891 if (vim9script && type != NULL)
Bram Moolenaar381692b2022-02-02 20:01:27 +00003892 set_tv_type(dest_tv, type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003893
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003894 // ":const var = value" locks the value
3895 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003896 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003897 // Like :lockvar! name: lock the value and what it contains, but only
3898 // if the reference count is up to one. That locks only literal
3899 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003900 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003901
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003902failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003903 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003904 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003905 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003906}
3907
3908/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003909 * Check in this order for backwards compatibility:
3910 * - Whether the variable is read-only
3911 * - Whether the variable value is locked
3912 * - Whether the variable is locked
3913 */
3914 int
3915var_check_permission(dictitem_T *di, char_u *name)
3916{
3917 if (var_check_ro(di->di_flags, name, FALSE)
3918 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3919 || var_check_lock(di->di_flags, name, FALSE))
3920 return FAIL;
3921 return OK;
3922}
3923
3924/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003925 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3926 * Also give an error message.
3927 */
3928 int
3929var_check_ro(int flags, char_u *name, int use_gettext)
3930{
3931 if (flags & DI_FLAGS_RO)
3932 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003933 if (name == NULL)
3934 emsg(_(e_cannot_change_readonly_variable));
3935 else
3936 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02003937 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003938 return TRUE;
3939 }
3940 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3941 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003942 if (name == NULL)
3943 emsg(_(e_cannot_set_variable_in_sandbox));
3944 else
3945 semsg(_(e_cannot_set_variable_in_sandbox_str),
3946 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003947 return TRUE;
3948 }
3949 return FALSE;
3950}
3951
3952/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003953 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3954 * Also give an error message.
3955 */
3956 int
3957var_check_lock(int flags, char_u *name, int use_gettext)
3958{
3959 if (flags & DI_FLAGS_LOCK)
3960 {
3961 semsg(_(e_variable_is_locked_str),
3962 use_gettext ? (char_u *)_(name) : name);
3963 return TRUE;
3964 }
3965 return FALSE;
3966}
3967
3968/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003969 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3970 * Also give an error message.
3971 */
3972 int
3973var_check_fixed(int flags, char_u *name, int use_gettext)
3974{
3975 if (flags & DI_FLAGS_FIX)
3976 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003977 if (name == NULL)
3978 emsg(_(e_cannot_delete_variable));
3979 else
3980 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003981 use_gettext ? (char_u *)_(name) : name);
3982 return TRUE;
3983 }
3984 return FALSE;
3985}
3986
3987/*
3988 * Check if a funcref is assigned to a valid variable name.
3989 * Return TRUE and give an error if not.
3990 */
3991 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003992var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003993 char_u *name, // points to start of variable name
3994 int new_var) // TRUE when creating the variable
3995{
Bram Moolenaar32154662021-03-28 21:14:06 +02003996 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
3997 // the name can be used without the s: prefix.
3998 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
3999 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004000 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
4001 ? name[2] : name[0]))
4002 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004003 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004004 return TRUE;
4005 }
4006 // Don't allow hiding a function. When "v" is not NULL we might be
4007 // assigning another function to the same var, the type is checked
4008 // below.
4009 if (new_var && function_exists(name, FALSE))
4010 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004011 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004012 name);
4013 return TRUE;
4014 }
4015 return FALSE;
4016}
4017
4018/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004019 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
4020 * value. Also give an error message, using "name" or _("name") when
4021 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004022 */
4023 int
Bram Moolenaara187c432020-09-16 21:08:28 +02004024value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004025{
4026 if (lock & VAR_LOCKED)
4027 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004028 if (name == NULL)
4029 emsg(_(e_value_is_locked));
4030 else
4031 semsg(_(e_value_is_locked_str),
4032 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004033 return TRUE;
4034 }
4035 if (lock & VAR_FIXED)
4036 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004037 if (name == NULL)
4038 emsg(_(e_cannot_change_value));
4039 else
4040 semsg(_(e_cannot_change_value_of_str),
4041 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004042 return TRUE;
4043 }
4044 return FALSE;
4045}
4046
4047/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01004048 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004049 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004050 * Return FALSE and give an error if not.
4051 */
4052 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004053valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004054{
4055 char_u *p;
4056
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004057 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004058 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01004059 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004060 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004061 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004062 return FALSE;
4063 }
4064 return TRUE;
4065}
4066
4067/*
LemonBoy47d4e312022-05-04 18:12:55 +01004068 * Implements the logic to retrieve local variable and option values.
4069 * Used by "getwinvar()" "gettabvar()" "gettabwinvar()" "getbufvar()".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004070 */
4071 static void
LemonBoy47d4e312022-05-04 18:12:55 +01004072get_var_from(
4073 char_u *varname,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004074 typval_T *rettv,
LemonBoy47d4e312022-05-04 18:12:55 +01004075 typval_T *deftv, // Default value if not found.
4076 int htname, // 't'ab, 'w'indow or 'b'uffer local.
4077 tabpage_T *tp, // can be NULL
4078 win_T *win,
4079 buf_T *buf) // Ignored if htname is not 'b'.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004080{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004081 dictitem_T *v;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004082 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004083 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004084 int need_switch_win;
4085
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004086 ++emsg_off;
4087
4088 rettv->v_type = VAR_STRING;
4089 rettv->vval.v_string = NULL;
4090
LemonBoy47d4e312022-05-04 18:12:55 +01004091 if (varname != NULL && tp != NULL && win != NULL
4092 && (htname != 'b' || buf != NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004093 {
4094 // Set curwin to be our win, temporarily. Also set the tabpage,
4095 // otherwise the window is not valid. Only do this when needed,
4096 // autocommands get blocked.
LemonBoy47d4e312022-05-04 18:12:55 +01004097 // If we have a buffer reference avoid the switching, we're saving and
4098 // restoring curbuf directly.
4099 need_switch_win = !(tp == curtab && win == curwin) || (buf != NULL);
4100 if (!need_switch_win || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004101 {
LemonBoy47d4e312022-05-04 18:12:55 +01004102 // Handle options. There are no tab-local options.
4103 if (*varname == '&' && htname != 't')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004104 {
LemonBoy47d4e312022-05-04 18:12:55 +01004105 buf_T *save_curbuf = curbuf;
4106
4107 // Change curbuf so the option is read from the correct buffer.
4108 if (buf != NULL && htname == 'b')
4109 curbuf = buf;
4110
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004111 if (varname[1] == NUL)
4112 {
4113 // get all window-local options in a dict
LemonBoy47d4e312022-05-04 18:12:55 +01004114 dict_T *opts = get_winbuf_options(htname == 'b');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004115
4116 if (opts != NULL)
4117 {
4118 rettv_dict_set(rettv, opts);
4119 done = TRUE;
4120 }
4121 }
LemonBoy47d4e312022-05-04 18:12:55 +01004122 else if (eval_option(&varname, rettv, TRUE) == OK)
4123 // Local option
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004124 done = TRUE;
LemonBoy47d4e312022-05-04 18:12:55 +01004125
4126 curbuf = save_curbuf;
4127 }
4128 else if (*varname == NUL)
4129 {
4130 // Empty string: return a dict with all the local variables.
4131 if (htname == 'b')
4132 v = &buf->b_bufvar;
4133 else if (htname == 'w')
4134 v = &win->w_winvar;
4135 else
4136 v = &tp->tp_winvar;
4137 copy_tv(&v->di_tv, rettv);
4138 done = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004139 }
4140 else
4141 {
LemonBoy47d4e312022-05-04 18:12:55 +01004142 hashtab_T *ht;
4143
4144 if (htname == 'b')
4145 ht = &buf->b_vars->dv_hashtab;
4146 else if (htname == 'w')
4147 ht = &win->w_vars->dv_hashtab;
4148 else
4149 ht = &tp->tp_vars->dv_hashtab;
4150
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004151 // Look up the variable.
LemonBoy47d4e312022-05-04 18:12:55 +01004152 v = find_var_in_ht(ht, htname, varname, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004153 if (v != NULL)
4154 {
4155 copy_tv(&v->di_tv, rettv);
4156 done = TRUE;
4157 }
4158 }
4159 }
4160
4161 if (need_switch_win)
4162 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004163 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004164 }
4165
LemonBoy47d4e312022-05-04 18:12:55 +01004166 if (!done && deftv->v_type != VAR_UNKNOWN)
4167 // use the default value
4168 copy_tv(deftv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004169
4170 --emsg_off;
4171}
4172
4173/*
LemonBoy47d4e312022-05-04 18:12:55 +01004174 * getwinvar() and gettabwinvar()
4175 */
4176 static void
4177getwinvar(
4178 typval_T *argvars,
4179 typval_T *rettv,
4180 int off) // 1 for gettabwinvar()
4181{
4182 char_u *varname;
4183 tabpage_T *tp;
4184 win_T *win;
4185
4186 if (off == 1)
4187 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4188 else
4189 tp = curtab;
4190 win = find_win_by_nr(&argvars[off], tp);
4191 varname = tv_get_string_chk(&argvars[off + 1]);
4192
4193 get_var_from(varname, rettv, &argvars[off + 2], 'w', tp, win, NULL);
4194}
4195
4196/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02004197 * Set option "varname" to the value of "varp" for the current buffer/window.
4198 */
4199 static void
4200set_option_from_tv(char_u *varname, typval_T *varp)
4201{
4202 long numval = 0;
4203 char_u *strval;
4204 char_u nbuf[NUMBUFLEN];
4205 int error = FALSE;
4206
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004207 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004208 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004209 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004210 strval = (char_u *)"0"; // avoid using "false"
4211 }
4212 else
4213 {
4214 if (!in_vim9script() || varp->v_type != VAR_STRING)
4215 numval = (long)tv_get_number_chk(varp, &error);
4216 strval = tv_get_string_buf_chk(varp, nbuf);
4217 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02004218 if (!error && strval != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004219 set_option_value_give_err(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar191929b2020-08-19 21:20:49 +02004220}
4221
4222/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004223 * "setwinvar()" and "settabwinvar()" functions
4224 */
4225 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004226setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004227{
4228 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004229 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004230 int need_switch_win;
4231 char_u *varname, *winvarname;
4232 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004233 tabpage_T *tp = NULL;
4234
4235 if (check_secure())
4236 return;
4237
4238 if (off == 1)
4239 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4240 else
4241 tp = curtab;
4242 win = find_win_by_nr(&argvars[off], tp);
4243 varname = tv_get_string_chk(&argvars[off + 1]);
4244 varp = &argvars[off + 2];
4245
4246 if (win != NULL && varname != NULL && varp != NULL)
4247 {
4248 need_switch_win = !(tp == curtab && win == curwin);
4249 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00004250 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004251 {
4252 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02004253 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004254 else
4255 {
4256 winvarname = alloc(STRLEN(varname) + 3);
4257 if (winvarname != NULL)
4258 {
4259 STRCPY(winvarname, "w:");
4260 STRCPY(winvarname + 2, varname);
4261 set_var(winvarname, varp, TRUE);
4262 vim_free(winvarname);
4263 }
4264 }
4265 }
4266 if (need_switch_win)
Bram Moolenaar18f47402022-01-06 13:24:51 +00004267 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004268 }
4269}
4270
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004271/*
4272 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4273 * v:option_type, and v:option_command.
4274 */
4275 void
4276reset_v_option_vars(void)
4277{
4278 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4279 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4280 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4281 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4282 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4283 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4284}
4285
4286/*
4287 * Add an assert error to v:errors.
4288 */
4289 void
4290assert_error(garray_T *gap)
4291{
4292 struct vimvar *vp = &vimvars[VV_ERRORS];
4293
Bram Moolenaard787e402021-12-24 21:36:12 +00004294 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004295 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004296 set_vim_var_list(VV_ERRORS, list_alloc());
4297 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4298}
4299
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004300 int
4301var_exists(char_u *var)
4302{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004303 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004304 char_u *name;
4305 char_u *tofree;
4306 typval_T tv;
4307 int len = 0;
4308 int n = FALSE;
4309
4310 // get_name_len() takes care of expanding curly braces
4311 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004312 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004313 if (len > 0)
4314 {
4315 if (tofree != NULL)
4316 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004317 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004318 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004319 if (n)
4320 {
4321 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004322 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004323 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4324 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004325 if (n)
4326 clear_tv(&tv);
4327 }
4328 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004329 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004330 n = FALSE;
4331
4332 vim_free(tofree);
4333 return n;
4334}
4335
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004336static lval_T *redir_lval = NULL;
4337#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4338static garray_T redir_ga; // only valid when redir_lval is not NULL
4339static char_u *redir_endp = NULL;
4340static char_u *redir_varname = NULL;
4341
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004342 int
4343alloc_redir_lval(void)
4344{
4345 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4346 if (redir_lval == NULL)
4347 return FAIL;
4348 return OK;
4349}
4350
4351 void
4352clear_redir_lval(void)
4353{
4354 VIM_CLEAR(redir_lval);
4355}
4356
4357 void
4358init_redir_ga(void)
4359{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004360 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004361}
4362
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004363/*
4364 * Start recording command output to a variable
4365 * When "append" is TRUE append to an existing variable.
4366 * Returns OK if successfully completed the setup. FAIL otherwise.
4367 */
4368 int
4369var_redir_start(char_u *name, int append)
4370{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004371 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004372 typval_T tv;
4373
4374 // Catch a bad name early.
4375 if (!eval_isnamec1(*name))
4376 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004377 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004378 return FAIL;
4379 }
4380
4381 // Make a copy of the name, it is used in redir_lval until redir ends.
4382 redir_varname = vim_strsave(name);
4383 if (redir_varname == NULL)
4384 return FAIL;
4385
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004386 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004387 {
4388 var_redir_stop();
4389 return FAIL;
4390 }
4391
4392 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004393 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004394
4395 // Parse the variable name (can be a dict or list entry).
4396 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4397 FNE_CHECK_START);
4398 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4399 {
4400 clear_lval(redir_lval);
4401 if (redir_endp != NULL && *redir_endp != NUL)
4402 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004403 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004404 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004405 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004406 redir_endp = NULL; // don't store a value, only cleanup
4407 var_redir_stop();
4408 return FAIL;
4409 }
4410
4411 // check if we can write to the variable: set it to or append an empty
4412 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004413 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004414 tv.v_type = VAR_STRING;
4415 tv.vval.v_string = (char_u *)"";
4416 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004417 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004418 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004419 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004420 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004421 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004422 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004423 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004424 {
4425 redir_endp = NULL; // don't store a value, only cleanup
4426 var_redir_stop();
4427 return FAIL;
4428 }
4429
4430 return OK;
4431}
4432
4433/*
4434 * Append "value[value_len]" to the variable set by var_redir_start().
4435 * The actual appending is postponed until redirection ends, because the value
4436 * appended may in fact be the string we write to, changing it may cause freed
4437 * memory to be used:
4438 * :redir => foo
4439 * :let foo
4440 * :redir END
4441 */
4442 void
4443var_redir_str(char_u *value, int value_len)
4444{
4445 int len;
4446
4447 if (redir_lval == NULL)
4448 return;
4449
4450 if (value_len == -1)
4451 len = (int)STRLEN(value); // Append the entire string
4452 else
4453 len = value_len; // Append only "value_len" characters
4454
4455 if (ga_grow(&redir_ga, len) == OK)
4456 {
4457 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4458 redir_ga.ga_len += len;
4459 }
4460 else
4461 var_redir_stop();
4462}
4463
4464/*
4465 * Stop redirecting command output to a variable.
4466 * Frees the allocated memory.
4467 */
4468 void
4469var_redir_stop(void)
4470{
4471 typval_T tv;
4472
4473 if (EVALCMD_BUSY)
4474 {
4475 redir_lval = NULL;
4476 return;
4477 }
4478
4479 if (redir_lval != NULL)
4480 {
4481 // If there was no error: assign the text to the variable.
4482 if (redir_endp != NULL)
4483 {
4484 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4485 tv.v_type = VAR_STRING;
4486 tv.vval.v_string = redir_ga.ga_data;
4487 // Call get_lval() again, if it's inside a Dict or List it may
4488 // have changed.
4489 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4490 FALSE, FALSE, 0, FNE_CHECK_START);
4491 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004493 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004494 clear_lval(redir_lval);
4495 }
4496
4497 // free the collected output
4498 VIM_CLEAR(redir_ga.ga_data);
4499
4500 VIM_CLEAR(redir_lval);
4501 }
4502 VIM_CLEAR(redir_varname);
4503}
4504
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004505/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004506 * Get the collected redirected text and clear redir_ga.
4507 */
4508 char_u *
4509get_clear_redir_ga(void)
4510{
4511 char_u *res;
4512
4513 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4514 res = redir_ga.ga_data;
4515 redir_ga.ga_data = NULL;
4516 return res;
4517}
4518
4519/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004520 * "gettabvar()" function
4521 */
4522 void
4523f_gettabvar(typval_T *argvars, typval_T *rettv)
4524{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004525 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004526 tabpage_T *tp;
4527 win_T *win = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004528
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004529 if (in_vim9script()
4530 && (check_for_number_arg(argvars, 0) == FAIL
4531 || check_for_string_arg(argvars, 1) == FAIL))
4532 return;
4533
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004534 varname = tv_get_string_chk(&argvars[1]);
4535 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
LemonBoy47d4e312022-05-04 18:12:55 +01004536 if (tp != NULL)
4537 win = tp == curtab || tp->tp_firstwin == NULL ? firstwin
4538 : tp->tp_firstwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004539
LemonBoy47d4e312022-05-04 18:12:55 +01004540 get_var_from(varname, rettv, &argvars[2], 't', tp, win, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004541}
4542
4543/*
4544 * "gettabwinvar()" function
4545 */
4546 void
4547f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4548{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004549 if (in_vim9script()
4550 && (check_for_number_arg(argvars, 0) == FAIL
4551 || check_for_number_arg(argvars, 1) == FAIL
4552 || check_for_string_arg(argvars, 2) == FAIL))
4553 return;
4554
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004555 getwinvar(argvars, rettv, 1);
4556}
4557
4558/*
4559 * "getwinvar()" function
4560 */
4561 void
4562f_getwinvar(typval_T *argvars, typval_T *rettv)
4563{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004564 if (in_vim9script()
4565 && (check_for_number_arg(argvars, 0) == FAIL
4566 || check_for_string_arg(argvars, 1) == FAIL))
4567 return;
4568
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004569 getwinvar(argvars, rettv, 0);
4570}
4571
4572/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004573 * "getbufvar()" function
4574 */
4575 void
4576f_getbufvar(typval_T *argvars, typval_T *rettv)
4577{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004578 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004579 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004580
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004581 if (in_vim9script()
4582 && (check_for_buffer_arg(argvars, 0) == FAIL
4583 || check_for_string_arg(argvars, 1) == FAIL))
4584 return;
4585
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004586 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004587 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004588
LemonBoy47d4e312022-05-04 18:12:55 +01004589 get_var_from(varname, rettv, &argvars[2], 'b', curtab, curwin, buf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004590}
4591
4592/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004593 * "settabvar()" function
4594 */
4595 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004596f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004597{
4598 tabpage_T *save_curtab;
4599 tabpage_T *tp;
4600 char_u *varname, *tabvarname;
4601 typval_T *varp;
4602
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004603 if (check_secure())
4604 return;
4605
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004606 if (in_vim9script()
4607 && (check_for_number_arg(argvars, 0) == FAIL
4608 || check_for_string_arg(argvars, 1) == FAIL))
4609 return;
4610
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004611 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4612 varname = tv_get_string_chk(&argvars[1]);
4613 varp = &argvars[2];
4614
4615 if (varname != NULL && varp != NULL && tp != NULL)
4616 {
4617 save_curtab = curtab;
4618 goto_tabpage_tp(tp, FALSE, FALSE);
4619
4620 tabvarname = alloc(STRLEN(varname) + 3);
4621 if (tabvarname != NULL)
4622 {
4623 STRCPY(tabvarname, "t:");
4624 STRCPY(tabvarname + 2, varname);
4625 set_var(tabvarname, varp, TRUE);
4626 vim_free(tabvarname);
4627 }
4628
4629 // Restore current tabpage
4630 if (valid_tabpage(save_curtab))
4631 goto_tabpage_tp(save_curtab, FALSE, FALSE);
4632 }
4633}
4634
4635/*
4636 * "settabwinvar()" function
4637 */
4638 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004639f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004640{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004641 if (in_vim9script()
4642 && (check_for_number_arg(argvars, 0) == FAIL
4643 || check_for_number_arg(argvars, 1) == FAIL
4644 || check_for_string_arg(argvars, 2) == FAIL))
4645 return;
4646
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004647 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004648}
4649
4650/*
4651 * "setwinvar()" function
4652 */
4653 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004654f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004655{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004656 if (in_vim9script()
4657 && (check_for_number_arg(argvars, 0) == FAIL
4658 || check_for_string_arg(argvars, 1) == FAIL))
4659 return;
4660
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004661 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004662}
4663
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004664/*
4665 * "setbufvar()" function
4666 */
4667 void
4668f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4669{
4670 buf_T *buf;
4671 char_u *varname, *bufvarname;
4672 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004673
4674 if (check_secure())
4675 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004676
4677 if (in_vim9script()
4678 && (check_for_buffer_arg(argvars, 0) == FAIL
4679 || check_for_string_arg(argvars, 1) == FAIL))
4680 return;
4681
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004682 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004683 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004684 varp = &argvars[2];
4685
4686 if (buf != NULL && varname != NULL && varp != NULL)
4687 {
4688 if (*varname == '&')
4689 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004690 aco_save_T aco;
4691
4692 // set curbuf to be our buf, temporarily
4693 aucmd_prepbuf(&aco, buf);
4694
Bram Moolenaar191929b2020-08-19 21:20:49 +02004695 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004696
4697 // reset notion of buffer
4698 aucmd_restbuf(&aco);
4699 }
4700 else
4701 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004702 bufvarname = alloc(STRLEN(varname) + 3);
4703 if (bufvarname != NULL)
4704 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004705 buf_T *save_curbuf = curbuf;
4706
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004707 curbuf = buf;
4708 STRCPY(bufvarname, "b:");
4709 STRCPY(bufvarname + 2, varname);
4710 set_var(bufvarname, varp, TRUE);
4711 vim_free(bufvarname);
4712 curbuf = save_curbuf;
4713 }
4714 }
4715 }
4716}
4717
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004718/*
4719 * Get a callback from "arg". It can be a Funcref or a function name.
4720 * When "arg" is zero return an empty string.
4721 * "cb_name" is not allocated.
4722 * "cb_name" is set to NULL for an invalid argument.
4723 */
4724 callback_T
4725get_callback(typval_T *arg)
4726{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004727 callback_T res;
4728 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004729
4730 res.cb_free_name = FALSE;
4731 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4732 {
4733 res.cb_partial = arg->vval.v_partial;
4734 ++res.cb_partial->pt_refcount;
4735 res.cb_name = partial_name(res.cb_partial);
4736 }
4737 else
4738 {
4739 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004740 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4741 && isdigit(*arg->vval.v_string))
4742 r = FAIL;
4743 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004744 {
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004745 if (arg->v_type == VAR_STRING)
4746 {
4747 char_u *name;
4748
4749 name = get_scriptlocal_funcname(arg->vval.v_string);
4750 if (name != NULL)
4751 {
4752 vim_free(arg->vval.v_string);
4753 arg->vval.v_string = name;
4754 }
4755 }
4756
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004757 res.cb_name = arg->vval.v_string;
4758 func_ref(res.cb_name);
4759 }
4760 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004761 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004762 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004763 r = FAIL;
4764
4765 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004766 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004767 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004768 res.cb_name = NULL;
4769 }
4770 }
4771 return res;
4772}
4773
4774/*
4775 * Copy a callback into a typval_T.
4776 */
4777 void
4778put_callback(callback_T *cb, typval_T *tv)
4779{
4780 if (cb->cb_partial != NULL)
4781 {
4782 tv->v_type = VAR_PARTIAL;
4783 tv->vval.v_partial = cb->cb_partial;
4784 ++tv->vval.v_partial->pt_refcount;
4785 }
4786 else
4787 {
4788 tv->v_type = VAR_FUNC;
4789 tv->vval.v_string = vim_strsave(cb->cb_name);
4790 func_ref(cb->cb_name);
4791 }
4792}
4793
4794/*
4795 * Make a copy of "src" into "dest", allocating the function name if needed,
4796 * without incrementing the refcount.
4797 */
4798 void
4799set_callback(callback_T *dest, callback_T *src)
4800{
4801 if (src->cb_partial == NULL)
4802 {
4803 // just a function name, make a copy
4804 dest->cb_name = vim_strsave(src->cb_name);
4805 dest->cb_free_name = TRUE;
4806 }
4807 else
4808 {
4809 // cb_name is a pointer into cb_partial
4810 dest->cb_name = src->cb_name;
4811 dest->cb_free_name = FALSE;
4812 }
4813 dest->cb_partial = src->cb_partial;
4814}
4815
4816/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004817 * Copy callback from "src" to "dest", incrementing the refcounts.
4818 */
4819 void
4820copy_callback(callback_T *dest, callback_T *src)
4821{
4822 dest->cb_partial = src->cb_partial;
4823 if (dest->cb_partial != NULL)
4824 {
4825 dest->cb_name = src->cb_name;
4826 dest->cb_free_name = FALSE;
4827 ++dest->cb_partial->pt_refcount;
4828 }
4829 else
4830 {
4831 dest->cb_name = vim_strsave(src->cb_name);
4832 dest->cb_free_name = TRUE;
4833 func_ref(src->cb_name);
4834 }
4835}
4836
4837/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004838 * When a callback refers to an autoload import, change the function name to
4839 * the "path#name" form. Uses the current script context.
4840 * Only works when the name is allocated.
4841 */
4842 void
4843expand_autload_callback(callback_T *cb)
4844{
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004845 char_u *name;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004846 char_u *p;
4847 imported_T *import;
4848
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004849 if (!in_vim9script() || cb->cb_name == NULL
4850 || (!cb->cb_free_name
4851 && (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004852 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004853 if (cb->cb_partial != NULL)
4854 name = cb->cb_partial->pt_name;
4855 else
4856 name = cb->cb_name;
4857 p = vim_strchr(name, '.');
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004858 if (p == NULL)
4859 return;
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004860 import = find_imported(name, p - name, FALSE);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004861 if (import != NULL && SCRIPT_ID_VALID(import->imp_sid))
4862 {
4863 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
4864
4865 if (si->sn_autoload_prefix != NULL)
4866 {
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004867 char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004868
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004869 if (newname != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004870 {
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004871 if (cb->cb_partial != NULL)
4872 {
4873 if (cb->cb_name == cb->cb_partial->pt_name)
4874 cb->cb_name = newname;
4875 vim_free(cb->cb_partial->pt_name);
4876 cb->cb_partial->pt_name = newname;
4877 }
4878 else
4879 {
4880 vim_free(cb->cb_name);
4881 cb->cb_name = newname;
4882 }
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004883 }
4884 }
4885 }
4886}
4887
4888/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004889 * Unref/free "callback" returned by get_callback() or set_callback().
4890 */
4891 void
4892free_callback(callback_T *callback)
4893{
4894 if (callback->cb_partial != NULL)
4895 {
4896 partial_unref(callback->cb_partial);
4897 callback->cb_partial = NULL;
4898 }
4899 else if (callback->cb_name != NULL)
4900 func_unref(callback->cb_name);
4901 if (callback->cb_free_name)
4902 {
4903 vim_free(callback->cb_name);
4904 callback->cb_free_name = FALSE;
4905 }
4906 callback->cb_name = NULL;
4907}
4908
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004909#endif // FEAT_EVAL