blob: d8ee2160842f71950d05282b1cfb5b70df0bced4 [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/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100606 * Evaluate one Vim expression {expr} in string "p" and append the
607 * resulting string to "gap". "p" points to the opening "{".
608 * Return a pointer to the character after "}", NULL for an error.
609 */
610 char_u *
611eval_one_expr_in_str(char_u *p, garray_T *gap)
612{
613 char_u *block_start = skipwhite(p + 1); // skip the opening {
614 char_u *block_end = block_start;
615 char_u *expr_val;
616
617 if (*block_start == NUL)
618 {
619 semsg(_(e_missing_close_curly_str), p);
620 return NULL;
621 }
622 if (skip_expr(&block_end, NULL) == FAIL)
623 return NULL;
624 block_end = skipwhite(block_end);
625 if (*block_end != '}')
626 {
627 semsg(_(e_missing_close_curly_str), p);
628 return NULL;
629 }
630 *block_end = NUL;
631 expr_val = eval_to_string(block_start, TRUE);
632 *block_end = '}';
633 if (expr_val == NULL)
634 return NULL;
635 ga_concat(gap, expr_val);
636 vim_free(expr_val);
637
638 return block_end + 1;
639}
640
641/*
642 * Evaluate all the Vim expressions {expr} in "str" and return the resulting
643 * string in allocated memory. "{{" is reduced to "{" and "}}" to "}".
644 * Used for a heredoc assignment.
645 * Returns NULL for an error.
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100646 */
LemonBoy2eaef102022-05-06 13:14:50 +0100647 char_u *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100648eval_all_expr_in_str(char_u *str)
649{
650 garray_T ga;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100651 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100652
653 ga_init2(&ga, 1, 80);
654 p = str;
655
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100656 while (*p != NUL)
657 {
LemonBoy2eaef102022-05-06 13:14:50 +0100658 char_u *lit_start;
LemonBoy2eaef102022-05-06 13:14:50 +0100659 int escaped_brace = FALSE;
660
661 // Look for a block start.
662 lit_start = p;
663 while (*p != '{' && *p != '}' && *p != NUL)
664 ++p;
665
666 if (*p != NUL && *p == p[1])
667 {
668 // Escaped brace, unescape and continue.
669 // Include the brace in the literal string.
670 ++p;
671 escaped_brace = TRUE;
672 }
673 else if (*p == '}')
674 {
675 semsg(_(e_stray_closing_curly_str), str);
676 ga_clear(&ga);
677 return NULL;
678 }
679
680 // Append the literal part.
681 ga_concat_len(&ga, lit_start, (size_t)(p - lit_start));
682
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100683 if (*p == NUL)
LemonBoy2eaef102022-05-06 13:14:50 +0100684 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100685
LemonBoy2eaef102022-05-06 13:14:50 +0100686 if (escaped_brace)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100687 {
LemonBoy2eaef102022-05-06 13:14:50 +0100688 // Skip the second brace.
689 ++p;
690 continue;
691 }
692
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100693 // Evaluate the expression and append the result.
694 p = eval_one_expr_in_str(p, &ga);
695 if (p == NULL)
LemonBoy2eaef102022-05-06 13:14:50 +0100696 {
697 ga_clear(&ga);
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100698 return NULL;
699 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100700 }
701 ga_append(&ga, NUL);
702
703 return ga.ga_data;
704}
705
706/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200707 * Get a list of lines from a HERE document. The here document is a list of
708 * lines surrounded by a marker.
709 * cmd << {marker}
710 * {line1}
711 * {line2}
712 * ....
713 * {marker}
714 *
715 * The {marker} is a string. If the optional 'trim' word is supplied before the
716 * marker, then the leading indentation before the lines (matching the
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100717 * indentation in the "cmd" line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200718 *
719 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100720 * tcl, mzscheme), "script_get" is set to TRUE. In this case, if the marker is
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200721 * missing, then '.' is accepted as a marker.
722 *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100723 * When compiling a heredoc assignment to a variable in a Vim9 def function,
724 * "vim9compile" is set to TRUE. In this case, instead of generating a list of
725 * string values from the heredoc, vim9 instructions are generated. On success
726 * the returned list will be empty.
727 *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100728 * Returns a List with {lines} or NULL on failure.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200729 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100730 list_T *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100731heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200732{
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100733 char_u *theline = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200734 char_u *marker;
735 list_T *l;
736 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100737 char_u *str;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200738 int marker_indent_len = 0;
739 int text_indent_len = 0;
740 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200741 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200742 int comment_char = in_vim9script() ? '#' : '"';
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100743 int evalstr = FALSE;
744 int eval_failed = FALSE;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100745 cctx_T *cctx = vim9compile ? eap->cookie : NULL;
746 int count = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200747
748 if (eap->getline == NULL)
749 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000750 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200751 return NULL;
752 }
753
754 // Check for the optional 'trim' word before the marker
755 cmd = skipwhite(cmd);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200756
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100757 while (TRUE)
758 {
759 if (STRNCMP(cmd, "trim", 4) == 0
760 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200761 {
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100762 cmd = skipwhite(cmd + 4);
763
764 // Trim the indentation from all the lines in the here document.
765 // The amount of indentation trimmed is the same as the indentation
766 // of the first line after the :let command line. To find the end
767 // marker the indent of the :let command line is trimmed.
768 p = *eap->cmdlinep;
769 while (VIM_ISWHITE(*p))
770 {
771 p++;
772 marker_indent_len++;
773 }
774 text_indent_len = -1;
775
776 continue;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200777 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100778 if (STRNCMP(cmd, "eval", 4) == 0
779 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
780 {
781 cmd = skipwhite(cmd + 4);
782 evalstr = TRUE;
783 continue;
784 }
785 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200786 }
787
788 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200789 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200790 {
791 marker = skipwhite(cmd);
792 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200793 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200794 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000795 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200796 return NULL;
797 }
798 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200799 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200800 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000801 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200802 return NULL;
803 }
804 }
805 else
806 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200807 // When getting lines for an embedded script, if the marker is missing,
808 // accept '.' as the marker.
809 if (script_get)
810 marker = dot;
811 else
812 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000813 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200814 return NULL;
815 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200816 }
817
818 l = list_alloc();
819 if (l == NULL)
820 return NULL;
821
822 for (;;)
823 {
824 int mi = 0;
825 int ti = 0;
826
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100827 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200828 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
829 if (theline == NULL)
830 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000831 semsg(_(e_missing_end_marker_str), marker);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200832 break;
833 }
834
835 // with "trim": skip the indent matching the :let line to find the
836 // marker
837 if (marker_indent_len > 0
838 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
839 mi = marker_indent_len;
840 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200841 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200842
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100843 // If expression evaluation failed in the heredoc, then skip till the
844 // end marker.
845 if (eval_failed)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100846 continue;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100847
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200848 if (text_indent_len == -1 && *theline != NUL)
849 {
850 // set the text indent from the first line.
851 p = theline;
852 text_indent_len = 0;
853 while (VIM_ISWHITE(*p))
854 {
855 p++;
856 text_indent_len++;
857 }
858 text_indent = vim_strnsave(theline, text_indent_len);
859 }
860 // with "trim": skip the indent matching the first line
861 if (text_indent != NULL)
862 for (ti = 0; ti < text_indent_len; ++ti)
863 if (theline[ti] != text_indent[ti])
864 break;
865
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100866 str = theline + ti;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100867 if (vim9compile)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100868 {
LemonBoy2eaef102022-05-06 13:14:50 +0100869 if (compile_all_expr_in_str(str, evalstr, cctx) == FAIL)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100870 {
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100871 vim_free(theline);
872 vim_free(text_indent);
873 return FAIL;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100874 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100875 count++;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100876 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100877 else
878 {
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100879 if (evalstr && !eap->skip)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100880 {
881 str = eval_all_expr_in_str(str);
882 if (str == NULL)
883 {
884 // expression evaluation failed
885 eval_failed = TRUE;
886 continue;
887 }
888 vim_free(theline);
889 theline = str;
890 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100891
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100892 if (list_append_string(l, str, -1) == FAIL)
893 break;
894 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200895 }
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100896 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200897 vim_free(text_indent);
898
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100899 if (vim9compile && cctx->ctx_skip != SKIP_YES && !eval_failed)
900 generate_NEWLIST(cctx, count, FALSE);
901
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100902 if (eval_failed)
903 {
904 // expression evaluation in the heredoc failed
905 list_free(l);
906 return NULL;
907 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200908 return l;
909}
910
911/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200912 * Vim9 variable declaration:
913 * ":var name"
914 * ":var name: type"
915 * ":var name = expr"
916 * ":var name: type = expr"
917 * etc.
918 */
919 void
920ex_var(exarg_T *eap)
921{
Bram Moolenaar330a3882022-03-05 11:05:57 +0000922 char_u *p = eap->cmd;
Bram Moolenaare1d12112022-03-05 11:37:48 +0000923 int has_var;
Bram Moolenaar330a3882022-03-05 11:05:57 +0000924
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200925 if (!in_vim9script())
926 {
927 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
928 return;
929 }
Bram Moolenaare1d12112022-03-05 11:37:48 +0000930 has_var = checkforcmd_noparen(&p, "var", 3);
931 if (current_sctx.sc_sid == 0 && has_var)
Bram Moolenaar0e1574c2022-03-03 17:05:35 +0000932 {
933 emsg(_(e_cannot_declare_variable_on_command_line));
934 return;
935 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200936 ex_let(eap);
937}
938
939/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200940 * ":let" list all variable values
941 * ":let var1 var2" list variable values
942 * ":let var = expr" assignment command.
943 * ":let var += expr" assignment command.
944 * ":let var -= expr" assignment command.
945 * ":let var *= expr" assignment command.
946 * ":let var /= expr" assignment command.
947 * ":let var %= expr" assignment command.
948 * ":let var .= expr" assignment command.
949 * ":let var ..= expr" assignment command.
950 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100952 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200953 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200954 * ":final var = expr" assignment command.
955 * ":final [var1, var2] = expr" unpack list.
956 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200957 * ":const" list all variable values
958 * ":const var1 var2" list variable values
959 * ":const var = expr" assignment command.
960 * ":const [var1, var2] = expr" unpack list.
961 */
962 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200963ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200964{
965 char_u *arg = eap->arg;
966 char_u *expr = NULL;
967 typval_T rettv;
968 int i;
969 int var_count = 0;
970 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200971 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200972 char_u *argend;
973 int first = TRUE;
974 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200975 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100976 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200977 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200978
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200979 if (eap->cmdidx == CMD_final && !vim9script)
980 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100981 // In legacy Vim script ":final" is short for ":finally".
982 ex_finally(eap);
983 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200984 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200985 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200986 {
987 emsg(_(e_cannot_use_let_in_vim9_script));
988 return;
989 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200990
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100991 if (eap->cmdidx == CMD_const)
992 flags |= ASSIGN_CONST;
993 else if (eap->cmdidx == CMD_final)
994 flags |= ASSIGN_FINAL;
995
996 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100997 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200998 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100999
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001000 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001001 if (argend == NULL)
1002 return;
1003 if (argend > arg && argend[-1] == '.') // for var.='str'
1004 --argend;
1005 expr = skipwhite(argend);
1006 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02001007 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001008 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +02001009 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
1010 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +02001011 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001012 {
1013 // ":let" without "=": list variables
1014 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001015 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001016 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001017 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001018 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001019 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001020 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001021 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001022 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001023 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001024 else
1025 // Vim9 declaration ":var name: type"
1026 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001027 }
1028 else
1029 {
1030 // ":let var1 var2" - list values
1031 arg = list_arg_vars(eap, arg, &first);
1032 }
1033 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001034 else if (!eap->skip)
1035 {
1036 // ":let"
1037 list_glob_vars(&first);
1038 list_buf_vars(&first);
1039 list_win_vars(&first);
1040 list_tab_vars(&first);
1041 list_script_vars(&first);
1042 list_func_vars(&first);
1043 list_vim_vars(&first);
1044 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02001045 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001046 }
1047 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
1048 {
1049 list_T *l;
Bram Moolenaar81530e32021-07-28 21:25:49 +02001050 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001051
1052 // HERE document
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001053 l = heredoc_get(eap, expr + 3, FALSE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001054 if (l != NULL)
1055 {
1056 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001057 if (!eap->skip)
1058 {
Bram Moolenaar81530e32021-07-28 21:25:49 +02001059 // errors are for the assignment, not the end marker
1060 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001061 op[0] = '=';
1062 op[1] = NUL;
1063 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001064 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001065 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001066 clear_tv(&rettv);
1067 }
1068 }
1069 else
1070 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001071 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001072 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001073
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02001074 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001075 i = FAIL;
1076 if (has_assign || concat)
1077 {
Bram Moolenaar9a562c12021-01-23 13:39:14 +01001078 int cur_lnum;
1079
Bram Moolenaar32e35112020-05-14 22:41:15 +02001080 op[0] = '=';
1081 op[1] = NUL;
1082 if (*expr != '=')
1083 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001084 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +02001085 {
1086 // +=, /=, etc. require an existing variable
1087 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
1088 i = FAIL;
1089 }
1090 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +02001091 {
1092 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001093 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001094 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001095 {
Bram Moolenaar32e35112020-05-14 22:41:15 +02001096 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001097 ++len;
1098 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02001099 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001100 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001101 }
1102 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001103 ++expr;
1104
Bram Moolenaar7f2c3412021-11-29 16:01:49 +00001105 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
Bram Moolenaarc7e44a72020-07-29 21:37:43 +02001106 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001107 {
1108 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01001109 semsg(_(e_white_space_required_before_and_after_str_at_str),
1110 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001111 i = FAIL;
1112 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02001113
1114 if (eap->skip)
1115 ++emsg_skip;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001116 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaarc7e44a72020-07-29 21:37:43 +02001117 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaar9a562c12021-01-23 13:39:14 +01001118 cur_lnum = SOURCING_LNUM;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001119 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001120 if (eap->skip)
1121 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001122 clear_evalarg(&evalarg, eap);
Bram Moolenaar9a562c12021-01-23 13:39:14 +01001123
1124 // Restore the line number so that any type error is given for the
1125 // declaration, not the expression.
1126 SOURCING_LNUM = cur_lnum;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001127 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001128 if (eap->skip)
1129 {
1130 if (i != FAIL)
1131 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001132 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001133 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001134 {
1135 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001136 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001137 clear_tv(&rettv);
1138 }
1139 }
1140}
1141
1142/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001143 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001144 * Handles both "var" with any type and "[var, var; var]" with a list type.
1145 * When "op" is not NULL it points to a string with characters that
1146 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1147 * or concatenate.
1148 * Returns OK or FAIL;
1149 */
1150 int
1151ex_let_vars(
1152 char_u *arg_start,
1153 typval_T *tv,
1154 int copy, // copy values from "tv", don't move
1155 int semicolon, // from skip_var_list()
1156 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001157 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001158 char_u *op)
1159{
1160 char_u *arg = arg_start;
1161 list_T *l;
1162 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001163 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001164 listitem_T *item;
1165 typval_T ltv;
1166
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00001167 if (tv->v_type == VAR_VOID)
1168 {
1169 emsg(_(e_cannot_use_void_value));
1170 return FAIL;
1171 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001172 if (*arg != '[')
1173 {
1174 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001175 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001176 return FAIL;
1177 return OK;
1178 }
1179
1180 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1181 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
1182 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001183 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001184 return FAIL;
1185 }
1186
1187 i = list_len(l);
1188 if (semicolon == 0 && var_count < i)
1189 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001190 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001191 return FAIL;
1192 }
1193 if (var_count - semicolon > i)
1194 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001195 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001196 return FAIL;
1197 }
1198
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001199 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001200 item = l->lv_first;
1201 while (*arg != ']')
1202 {
1203 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001204 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001205 arg = ex_let_one(arg, &item->li_tv, TRUE,
1206 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001207 item = item->li_next;
1208 if (arg == NULL)
1209 return FAIL;
1210
1211 arg = skipwhite(arg);
1212 if (*arg == ';')
1213 {
1214 // Put the rest of the list (may be empty) in the var after ';'.
1215 // Create a new list for this.
1216 l = list_alloc();
1217 if (l == NULL)
1218 return FAIL;
1219 while (item != NULL)
1220 {
1221 list_append_tv(l, &item->li_tv);
1222 item = item->li_next;
1223 }
1224
1225 ltv.v_type = VAR_LIST;
1226 ltv.v_lock = 0;
1227 ltv.vval.v_list = l;
1228 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001229 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001230
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001231 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1232 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001233 clear_tv(&ltv);
1234 if (arg == NULL)
1235 return FAIL;
1236 break;
1237 }
1238 else if (*arg != ',' && *arg != ']')
1239 {
1240 internal_error("ex_let_vars()");
1241 return FAIL;
1242 }
1243 }
1244
1245 return OK;
1246}
1247
1248/*
1249 * Skip over assignable variable "var" or list of variables "[var, var]".
1250 * Used for ":let varvar = expr" and ":for varvar in expr".
1251 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001252 * for "[var, var; var]" set "semicolon" to 1.
1253 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001254 * Return NULL for an error.
1255 */
1256 char_u *
1257skip_var_list(
1258 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001259 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001260 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001261 int *semicolon,
1262 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001263{
1264 char_u *p, *s;
1265
1266 if (*arg == '[')
1267 {
1268 // "[var, var]": find the matching ']'.
1269 p = arg;
1270 for (;;)
1271 {
1272 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001273 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001274 if (s == p)
1275 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001276 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001277 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001278 return NULL;
1279 }
1280 ++*var_count;
1281
1282 p = skipwhite(s);
1283 if (*p == ']')
1284 break;
1285 else if (*p == ';')
1286 {
1287 if (*semicolon == 1)
1288 {
Bram Moolenaar8b716f52022-02-15 21:17:56 +00001289 if (!silent)
1290 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001291 return NULL;
1292 }
1293 *semicolon = 1;
1294 }
1295 else if (*p != ',')
1296 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001297 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001298 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001299 return NULL;
1300 }
1301 }
1302 return p + 1;
1303 }
1304 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001306}
1307
1308/*
1309 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1310 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001311 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001312 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001313 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001314skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001315{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001316 char_u *end;
1317 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001319 if (*arg == '@' && arg[1] != NUL)
1320 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001322 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001323
1324 // "a: type" is declaring variable "a" with a type, not "a:".
1325 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001326 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001327 --end;
1328
Bram Moolenaar585587d2021-01-17 20:52:13 +01001329 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001331 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001332 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001333 }
1334 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001335}
1336
1337/*
1338 * List variables for hashtab "ht" with prefix "prefix".
1339 * If "empty" is TRUE also list NULL strings as empty strings.
1340 */
1341 void
1342list_hashtable_vars(
1343 hashtab_T *ht,
1344 char *prefix,
1345 int empty,
1346 int *first)
1347{
1348 hashitem_T *hi;
1349 dictitem_T *di;
1350 int todo;
1351 char_u buf[IOSIZE];
1352
1353 todo = (int)ht->ht_used;
1354 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1355 {
1356 if (!HASHITEM_EMPTY(hi))
1357 {
1358 --todo;
1359 di = HI2DI(hi);
1360
1361 // apply :filter /pat/ to variable name
1362 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1363 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1364 if (message_filtered(buf))
1365 continue;
1366
1367 if (empty || di->di_tv.v_type != VAR_STRING
1368 || di->di_tv.vval.v_string != NULL)
1369 list_one_var(di, prefix, first);
1370 }
1371 }
1372}
1373
1374/*
1375 * List global variables.
1376 */
1377 static void
1378list_glob_vars(int *first)
1379{
1380 list_hashtable_vars(&globvarht, "", TRUE, first);
1381}
1382
1383/*
1384 * List buffer variables.
1385 */
1386 static void
1387list_buf_vars(int *first)
1388{
1389 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1390}
1391
1392/*
1393 * List window variables.
1394 */
1395 static void
1396list_win_vars(int *first)
1397{
1398 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1399}
1400
1401/*
1402 * List tab page variables.
1403 */
1404 static void
1405list_tab_vars(int *first)
1406{
1407 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1408}
1409
1410/*
1411 * List variables in "arg".
1412 */
1413 static char_u *
1414list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1415{
1416 int error = FALSE;
1417 int len;
1418 char_u *name;
1419 char_u *name_start;
1420 char_u *arg_subsc;
1421 char_u *tofree;
1422 typval_T tv;
1423
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001424 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001425 {
1426 if (error || eap->skip)
1427 {
1428 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1429 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1430 {
1431 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001432 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001433 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001434 break;
1435 }
1436 }
1437 else
1438 {
1439 // get_name_len() takes care of expanding curly braces
1440 name_start = name = arg;
1441 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1442 if (len <= 0)
1443 {
1444 // This is mainly to keep test 49 working: when expanding
1445 // curly braces fails overrule the exception error message.
1446 if (len < 0 && !aborting())
1447 {
1448 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001449 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001450 break;
1451 }
1452 error = TRUE;
1453 }
1454 else
1455 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001456 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001457 if (tofree != NULL)
1458 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001459 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001460 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001461 error = TRUE;
1462 else
1463 {
1464 // handle d.key, l[idx], f(expr)
1465 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001466 if (handle_subscript(&arg, name_start, &tv,
1467 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001468 error = TRUE;
1469 else
1470 {
1471 if (arg == arg_subsc && len == 2 && name[1] == ':')
1472 {
1473 switch (*name)
1474 {
1475 case 'g': list_glob_vars(first); break;
1476 case 'b': list_buf_vars(first); break;
1477 case 'w': list_win_vars(first); break;
1478 case 't': list_tab_vars(first); break;
1479 case 'v': list_vim_vars(first); break;
1480 case 's': list_script_vars(first); break;
1481 case 'l': list_func_vars(first); break;
1482 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001483 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001484 }
1485 }
1486 else
1487 {
1488 char_u numbuf[NUMBUFLEN];
1489 char_u *tf;
1490 int c;
1491 char_u *s;
1492
1493 s = echo_string(&tv, &tf, numbuf, 0);
1494 c = *arg;
1495 *arg = NUL;
1496 list_one_var_a("",
1497 arg == arg_subsc ? name : name_start,
1498 tv.v_type,
1499 s == NULL ? (char_u *)"" : s,
1500 first);
1501 *arg = c;
1502 vim_free(tf);
1503 }
1504 clear_tv(&tv);
1505 }
1506 }
1507 }
1508
1509 vim_free(tofree);
1510 }
1511
1512 arg = skipwhite(arg);
1513 }
1514
1515 return arg;
1516}
1517
1518/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001519 * Set an environment variable, part of ex_let_one().
1520 */
1521 static char_u *
1522ex_let_env(
1523 char_u *arg,
1524 typval_T *tv,
1525 int flags,
1526 char_u *endchars,
1527 char_u *op)
1528{
1529 char_u *arg_end = NULL;
1530 char_u *name;
1531 int len;
1532
1533 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1534 && (flags & ASSIGN_FOR_LOOP) == 0)
1535 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001536 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001537 return NULL;
1538 }
1539
1540 // Find the end of the name.
1541 ++arg;
1542 name = arg;
1543 len = get_env_len(&arg);
1544 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001545 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001546 else
1547 {
1548 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001549 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001550 else if (endchars != NULL
1551 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1552 emsg(_(e_unexpected_characters_in_let));
1553 else if (!check_secure())
1554 {
1555 char_u *tofree = NULL;
1556 int c1 = name[len];
1557 char_u *p;
1558
1559 name[len] = NUL;
1560 p = tv_get_string_chk(tv);
1561 if (p != NULL && op != NULL && *op == '.')
1562 {
1563 int mustfree = FALSE;
1564 char_u *s = vim_getenv(name, &mustfree);
1565
1566 if (s != NULL)
1567 {
1568 p = tofree = concat_str(s, p);
1569 if (mustfree)
1570 vim_free(s);
1571 }
1572 }
1573 if (p != NULL)
1574 {
1575 vim_setenv_ext(name, p);
1576 arg_end = arg;
1577 }
1578 name[len] = c1;
1579 vim_free(tofree);
1580 }
1581 }
1582 return arg_end;
1583}
1584
1585/*
1586 * Set an option, part of ex_let_one().
1587 */
1588 static char_u *
1589ex_let_option(
1590 char_u *arg,
1591 typval_T *tv,
1592 int flags,
1593 char_u *endchars,
1594 char_u *op)
1595{
1596 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001597 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001598 char_u *arg_end = NULL;
1599
1600 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1601 && (flags & ASSIGN_FOR_LOOP) == 0)
1602 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001603 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001604 return NULL;
1605 }
1606
1607 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001608 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001609 if (p == NULL || (endchars != NULL
1610 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1611 emsg(_(e_unexpected_characters_in_let));
1612 else
1613 {
1614 int c1;
1615 long n = 0;
1616 getoption_T opt_type;
1617 long numval;
1618 char_u *stringval = NULL;
1619 char_u *s = NULL;
1620 int failed = FALSE;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001621 int opt_p_flags;
1622 char_u *tofree = NULL;
Bram Moolenaar92c33eb2021-12-07 11:03:39 +00001623 char_u numbuf[NUMBUFLEN];
1624
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001625 c1 = *p;
1626 *p = NUL;
1627
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001628 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags,
1629 scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001630 if ((opt_type == gov_bool
1631 || opt_type == gov_number
1632 || opt_type == gov_hidden_bool
1633 || opt_type == gov_hidden_number)
1634 && (tv->v_type != VAR_STRING || !in_vim9script()))
1635 {
1636 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1637 // bool, possibly hidden
1638 n = (long)tv_get_bool(tv);
1639 else
1640 // number, possibly hidden
1641 n = (long)tv_get_number(tv);
1642 }
1643
Bram Moolenaaref082e12021-12-12 21:02:03 +00001644 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001645 || tv->v_type == VAR_FUNC))
1646 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001647 // If the option can be set to a function reference or a lambda
1648 // and the passed value is a function reference, then convert it to
1649 // the name (string) of the function reference.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001650 s = tv2string(tv, &tofree, numbuf, 0);
1651 }
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001652 // Avoid setting a string option to the text "v:false" or similar.
1653 // In Vim9 script also don't convert a number to string.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001654 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001655 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1656 s = tv_get_string_chk(tv);
1657
1658 if (op != NULL && *op != '=')
1659 {
1660 if (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1661 || (opt_type == gov_string && *op != '.'))
1662 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001663 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001664 failed = TRUE; // don't set the value
1665
1666 }
1667 else
1668 {
1669 // number, in legacy script also bool
1670 if (opt_type == gov_number
1671 || (opt_type == gov_bool && !in_vim9script()))
1672 {
1673 switch (*op)
1674 {
1675 case '+': n = numval + n; break;
1676 case '-': n = numval - n; break;
1677 case '*': n = numval * n; break;
1678 case '/': n = (long)num_divide(numval, n,
1679 &failed); break;
1680 case '%': n = (long)num_modulus(numval, n,
1681 &failed); break;
1682 }
1683 s = NULL;
1684 }
1685 else if (opt_type == gov_string
1686 && stringval != NULL && s != NULL)
1687 {
1688 // string
1689 s = concat_str(stringval, s);
1690 vim_free(stringval);
1691 stringval = s;
1692 }
1693 }
1694 }
1695
1696 if (!failed)
1697 {
1698 if (opt_type != gov_string || s != NULL)
1699 {
Bram Moolenaar8ccbbeb2022-03-02 19:49:38 +00001700 char *err = set_option_value(arg, n, s, scope);
1701
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001702 arg_end = p;
Bram Moolenaar8ccbbeb2022-03-02 19:49:38 +00001703 if (err != NULL)
1704 emsg(_(err));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001705 }
1706 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001707 emsg(_(e_string_required));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001708 }
1709 *p = c1;
1710 vim_free(stringval);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001711 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001712 }
1713 return arg_end;
1714}
1715
1716/*
1717 * Set a register, part of ex_let_one().
1718 */
1719 static char_u *
1720ex_let_register(
1721 char_u *arg,
1722 typval_T *tv,
1723 int flags,
1724 char_u *endchars,
1725 char_u *op)
1726{
1727 char_u *arg_end = NULL;
1728
1729 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1730 && (flags & ASSIGN_FOR_LOOP) == 0)
1731 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001732 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001733 return NULL;
1734 }
1735 ++arg;
1736 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001737 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001738 else if (endchars != NULL
1739 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1740 emsg(_(e_unexpected_characters_in_let));
1741 else
1742 {
1743 char_u *ptofree = NULL;
1744 char_u *p;
1745
1746 p = tv_get_string_chk(tv);
1747 if (p != NULL && op != NULL && *op == '.')
1748 {
1749 char_u *s = get_reg_contents(*arg == '@'
1750 ? '"' : *arg, GREG_EXPR_SRC);
1751
1752 if (s != NULL)
1753 {
1754 p = ptofree = concat_str(s, p);
1755 vim_free(s);
1756 }
1757 }
1758 if (p != NULL)
1759 {
1760 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1761 arg_end = arg + 1;
1762 }
1763 vim_free(ptofree);
1764 }
1765 return arg_end;
1766}
1767
1768/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001769 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1770 * Returns a pointer to the char just after the var name.
1771 * Returns NULL if there is an error.
1772 */
1773 static char_u *
1774ex_let_one(
1775 char_u *arg, // points to variable name
1776 typval_T *tv, // value to assign to variable
1777 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001778 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001779 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001780 char_u *op, // "+", "-", "." or NULL
1781 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001782{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001783 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001784
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001785 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001786 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001787 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1788 {
1789 vim9_declare_error(arg);
1790 return NULL;
1791 }
1792
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001793 if (*arg == '$')
1794 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001795 // ":let $VAR = expr": Set environment variable.
1796 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001797 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001798 else if (*arg == '&')
1799 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001800 // ":let &option = expr": Set option value.
1801 // ":let &l:option = expr": Set local option value.
1802 // ":let &g:option = expr": Set global option value.
1803 // ":for &ts in range(8)": Set option value for for loop
1804 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001805 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001806 else if (*arg == '@')
1807 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001808 // ":let @r = expr": Set register contents.
1809 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001810 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001811 else if (eval_isnamec1(*arg) || *arg == '{')
1812 {
1813 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001814 char_u *p;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001815 int lval_flags = (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1816 ? GLV_NO_DECL : 0;
1817 if (op != NULL && *op != '=')
1818 lval_flags |= GLV_ASSIGN_WITH_OP;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001819
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001820 // ":let var = expr": Set internal variable.
1821 // ":let var: type = expr": Set internal variable with type.
1822 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001823 p = get_lval(arg, tv, &lv, FALSE, FALSE, lval_flags, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001824 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001825 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 if (endchars != NULL && vim_strchr(endchars,
1827 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001828 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001829 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001830 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001831 else
1832 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001833 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001834 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001835 }
1836 }
1837 clear_lval(&lv);
1838 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001839 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001840 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001841
1842 return arg_end;
1843}
1844
1845/*
1846 * ":unlet[!] var1 ... " command.
1847 */
1848 void
1849ex_unlet(exarg_T *eap)
1850{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001851 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001852}
1853
1854/*
1855 * ":lockvar" and ":unlockvar" commands
1856 */
1857 void
1858ex_lockvar(exarg_T *eap)
1859{
1860 char_u *arg = eap->arg;
1861 int deep = 2;
1862
1863 if (eap->forceit)
1864 deep = -1;
1865 else if (vim_isdigit(*arg))
1866 {
1867 deep = getdigits(&arg);
1868 arg = skipwhite(arg);
1869 }
1870
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001871 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001872}
1873
1874/*
1875 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001876 * Also used for Vim9 script. "callback" is invoked as:
1877 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001878 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001879 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001880ex_unletlock(
1881 exarg_T *eap,
1882 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001883 int deep,
1884 int glv_flags,
1885 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1886 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001887{
1888 char_u *arg = argstart;
1889 char_u *name_end;
1890 int error = FALSE;
1891 lval_T lv;
1892
1893 do
1894 {
1895 if (*arg == '$')
1896 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001897 lv.ll_name = arg;
1898 lv.ll_tv = NULL;
1899 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001900 if (get_env_len(&arg) == 0)
1901 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001902 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001903 return;
1904 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001905 if (!error && !eap->skip
1906 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1907 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001908 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001909 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001910 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001911 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001912 // Parse the name and find the end.
1913 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001914 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001915 if (lv.ll_name == NULL)
1916 error = TRUE; // error but continue parsing
1917 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1918 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001919 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001920 if (name_end != NULL)
1921 {
1922 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00001923 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001924 }
1925 if (!(eap->skip || error))
1926 clear_lval(&lv);
1927 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001928 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001929
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001930 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001931 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001932 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001933
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001934 if (!eap->skip)
1935 clear_lval(&lv);
1936 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001937
1938 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001939 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001940
Bram Moolenaar63b91732021-08-05 20:40:03 +02001941 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001942}
1943
1944 static int
1945do_unlet_var(
1946 lval_T *lp,
1947 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001948 exarg_T *eap,
1949 int deep UNUSED,
1950 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001951{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001952 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001953 int ret = OK;
1954 int cc;
1955
1956 if (lp->ll_tv == NULL)
1957 {
1958 cc = *name_end;
1959 *name_end = NUL;
1960
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001961 // Environment variable, normal name or expanded name.
1962 if (*lp->ll_name == '$')
LemonBoy77142312022-04-15 20:50:46 +01001963 vim_unsetenv_ext(lp->ll_name + 1);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001964 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001965 ret = FAIL;
1966 *name_end = cc;
1967 }
1968 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001969 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001970 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001971 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001972 return FAIL;
1973 else if (lp->ll_range)
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00001974 list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_n1,
1975 !lp->ll_empty2, lp->ll_n2);
1976 else if (lp->ll_list != NULL)
1977 // unlet a List item.
1978 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001979 else
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00001980 // unlet a Dictionary item.
1981 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001982
1983 return ret;
1984}
1985
1986/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001987 * Unlet one item or a range of items from a list.
1988 * Return OK or FAIL.
1989 */
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00001990 void
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001991list_unlet_range(
1992 list_T *l,
1993 listitem_T *li_first,
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001994 long n1_arg,
1995 int has_n2,
1996 long n2)
1997{
1998 listitem_T *li = li_first;
1999 int n1 = n1_arg;
2000
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002001 // Delete a range of List items.
2002 li = li_first;
2003 n1 = n1_arg;
2004 while (li != NULL && (!has_n2 || n2 >= n1))
2005 {
2006 listitem_T *next = li->li_next;
2007
2008 listitem_remove(l, li);
2009 li = next;
2010 ++n1;
2011 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002012}
2013/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002014 * "unlet" a variable. Return OK if it existed, FAIL if not.
2015 * When "forceit" is TRUE don't complain if the variable doesn't exist.
2016 */
2017 int
2018do_unlet(char_u *name, int forceit)
2019{
2020 hashtab_T *ht;
2021 hashitem_T *hi;
2022 char_u *varname;
2023 dict_T *d;
2024 dictitem_T *di;
2025
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002026 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002027 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002028 return FAIL;
2029
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002030 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002031
2032 // can't :unlet a script variable in Vim9 script from a function
2033 if (ht == get_script_local_ht()
2034 && SCRIPT_ID_VALID(current_sctx.sc_sid)
2035 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2036 == SCRIPT_VERSION_VIM9
2037 && check_vim9_unlet(name) == FAIL)
2038 return FAIL;
2039
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002040 if (ht != NULL && *varname != NUL)
2041 {
2042 d = get_current_funccal_dict(ht);
2043 if (d == NULL)
2044 {
2045 if (ht == &globvarht)
2046 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002047 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002048 d = &vimvardict;
2049 else
2050 {
2051 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2052 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2053 }
2054 if (d == NULL)
2055 {
2056 internal_error("do_unlet()");
2057 return FAIL;
2058 }
2059 }
2060 hi = hash_find(ht, varname);
2061 if (HASHITEM_EMPTY(hi))
2062 hi = find_hi_in_scoped_ht(name, &ht);
2063 if (hi != NULL && !HASHITEM_EMPTY(hi))
2064 {
2065 di = HI2DI(hi);
2066 if (var_check_fixed(di->di_flags, name, FALSE)
2067 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02002068 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002069 return FAIL;
2070
2071 delete_var(ht, hi);
2072 return OK;
2073 }
2074 }
2075 if (forceit)
2076 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00002077 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002078 return FAIL;
2079}
2080
2081/*
2082 * Lock or unlock variable indicated by "lp".
2083 * "deep" is the levels to go (-1 for unlimited);
2084 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2085 */
2086 static int
2087do_lock_var(
2088 lval_T *lp,
2089 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002090 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002091 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002092 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002093{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002094 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002095 int ret = OK;
2096 int cc;
2097 dictitem_T *di;
2098
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002099 if (lp->ll_tv == NULL)
2100 {
2101 cc = *name_end;
2102 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002103 if (*lp->ll_name == '$')
2104 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002105 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002106 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002107 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002108 else
2109 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002110 // Normal name or expanded name.
2111 di = find_var(lp->ll_name, NULL, TRUE);
2112 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002113 {
2114 if (in_vim9script())
2115 semsg(_(e_cannot_find_variable_to_unlock_str),
2116 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002117 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002118 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002119 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002120 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002121 if ((di->di_flags & DI_FLAGS_FIX)
2122 && di->di_tv.v_type != VAR_DICT
2123 && di->di_tv.v_type != VAR_LIST)
2124 {
2125 // For historic reasons this error is not given for a list
2126 // or dict. E.g., the b: dict could be locked/unlocked.
2127 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
2128 ret = FAIL;
2129 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002130 else
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002131 {
2132 if (in_vim9script())
2133 {
2134 svar_T *sv = find_typval_in_script(&di->di_tv,
2135 0, FALSE);
2136
2137 if (sv != NULL && sv->sv_const != 0)
2138 {
2139 semsg(_(e_cannot_change_readonly_variable_str),
2140 lp->ll_name);
2141 ret = FAIL;
2142 }
2143 }
2144
2145 if (ret == OK)
2146 {
2147 if (lock)
2148 di->di_flags |= DI_FLAGS_LOCK;
2149 else
2150 di->di_flags &= ~DI_FLAGS_LOCK;
2151 if (deep != 0)
2152 item_lock(&di->di_tv, deep, lock, FALSE);
2153 }
2154 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002155 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002156 }
2157 *name_end = cc;
2158 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002159 else if (deep == 0)
2160 {
2161 // nothing to do
2162 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002163 else if (lp->ll_range)
2164 {
2165 listitem_T *li = lp->ll_li;
2166
2167 // (un)lock a range of List items.
2168 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2169 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02002170 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002171 li = li->li_next;
2172 ++lp->ll_n1;
2173 }
2174 }
2175 else if (lp->ll_list != NULL)
2176 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002177 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002178 else
2179 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002180 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002181
2182 return ret;
2183}
2184
2185/*
2186 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002187 * When "check_refcount" is TRUE do not lock a list or dict with a reference
2188 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002189 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002190 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02002191item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002192{
2193 static int recurse = 0;
2194 list_T *l;
2195 listitem_T *li;
2196 dict_T *d;
2197 blob_T *b;
2198 hashitem_T *hi;
2199 int todo;
2200
2201 if (recurse >= DICT_MAXNEST)
2202 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002203 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002204 return;
2205 }
2206 if (deep == 0)
2207 return;
2208 ++recurse;
2209
2210 // lock/unlock the item itself
2211 if (lock)
2212 tv->v_lock |= VAR_LOCKED;
2213 else
2214 tv->v_lock &= ~VAR_LOCKED;
2215
2216 switch (tv->v_type)
2217 {
2218 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002219 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002220 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002221 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002223 case VAR_STRING:
2224 case VAR_FUNC:
2225 case VAR_PARTIAL:
2226 case VAR_FLOAT:
2227 case VAR_SPECIAL:
2228 case VAR_JOB:
2229 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002230 case VAR_INSTR:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002231 break;
2232
2233 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002234 if ((b = tv->vval.v_blob) != NULL
2235 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002236 {
2237 if (lock)
2238 b->bv_lock |= VAR_LOCKED;
2239 else
2240 b->bv_lock &= ~VAR_LOCKED;
2241 }
2242 break;
2243 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002244 if ((l = tv->vval.v_list) != NULL
2245 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002246 {
2247 if (lock)
2248 l->lv_lock |= VAR_LOCKED;
2249 else
2250 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002251 if (deep < 0 || deep > 1)
2252 {
2253 if (l->lv_first == &range_list_item)
2254 l->lv_lock |= VAR_ITEMS_LOCKED;
2255 else
2256 {
2257 // recursive: lock/unlock the items the List contains
2258 CHECK_LIST_MATERIALIZE(l);
2259 FOR_ALL_LIST_ITEMS(l, li) item_lock(&li->li_tv,
2260 deep - 1, lock, check_refcount);
2261 }
2262 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002263 }
2264 break;
2265 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002266 if ((d = tv->vval.v_dict) != NULL
2267 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002268 {
2269 if (lock)
2270 d->dv_lock |= VAR_LOCKED;
2271 else
2272 d->dv_lock &= ~VAR_LOCKED;
2273 if (deep < 0 || deep > 1)
2274 {
2275 // recursive: lock/unlock the items the List contains
2276 todo = (int)d->dv_hashtab.ht_used;
2277 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2278 {
2279 if (!HASHITEM_EMPTY(hi))
2280 {
2281 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002282 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2283 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002284 }
2285 }
2286 }
2287 }
2288 }
2289 --recurse;
2290}
2291
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002292#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2293/*
2294 * Delete all "menutrans_" variables.
2295 */
2296 void
2297del_menutrans_vars(void)
2298{
2299 hashitem_T *hi;
2300 int todo;
2301
2302 hash_lock(&globvarht);
2303 todo = (int)globvarht.ht_used;
2304 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2305 {
2306 if (!HASHITEM_EMPTY(hi))
2307 {
2308 --todo;
2309 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2310 delete_var(&globvarht, hi);
2311 }
2312 }
2313 hash_unlock(&globvarht);
2314}
2315#endif
2316
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002317/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002318 * Local string buffer for the next two functions to store a variable name
2319 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2320 * get_user_var_name().
2321 */
2322
2323static char_u *varnamebuf = NULL;
2324static int varnamebuflen = 0;
2325
2326/*
2327 * Function to concatenate a prefix and a variable name.
2328 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002329 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002330cat_prefix_varname(int prefix, char_u *name)
2331{
2332 int len;
2333
2334 len = (int)STRLEN(name) + 3;
2335 if (len > varnamebuflen)
2336 {
2337 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002338 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002339 varnamebuf = alloc(len);
2340 if (varnamebuf == NULL)
2341 {
2342 varnamebuflen = 0;
2343 return NULL;
2344 }
2345 varnamebuflen = len;
2346 }
2347 *varnamebuf = prefix;
2348 varnamebuf[1] = ':';
2349 STRCPY(varnamebuf + 2, name);
2350 return varnamebuf;
2351}
2352
2353/*
2354 * Function given to ExpandGeneric() to obtain the list of user defined
2355 * (global/buffer/window/built-in) variable names.
2356 */
2357 char_u *
2358get_user_var_name(expand_T *xp, int idx)
2359{
2360 static long_u gdone;
2361 static long_u bdone;
2362 static long_u wdone;
2363 static long_u tdone;
2364 static int vidx;
2365 static hashitem_T *hi;
2366 hashtab_T *ht;
2367
2368 if (idx == 0)
2369 {
2370 gdone = bdone = wdone = vidx = 0;
2371 tdone = 0;
2372 }
2373
2374 // Global variables
2375 if (gdone < globvarht.ht_used)
2376 {
2377 if (gdone++ == 0)
2378 hi = globvarht.ht_array;
2379 else
2380 ++hi;
2381 while (HASHITEM_EMPTY(hi))
2382 ++hi;
2383 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2384 return cat_prefix_varname('g', hi->hi_key);
2385 return hi->hi_key;
2386 }
2387
2388 // b: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002389 ht = &prevwin_curwin()->w_buffer->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002390 if (bdone < ht->ht_used)
2391 {
2392 if (bdone++ == 0)
2393 hi = ht->ht_array;
2394 else
2395 ++hi;
2396 while (HASHITEM_EMPTY(hi))
2397 ++hi;
2398 return cat_prefix_varname('b', hi->hi_key);
2399 }
2400
2401 // w: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002402 ht = &prevwin_curwin()->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002403 if (wdone < ht->ht_used)
2404 {
2405 if (wdone++ == 0)
2406 hi = ht->ht_array;
2407 else
2408 ++hi;
2409 while (HASHITEM_EMPTY(hi))
2410 ++hi;
2411 return cat_prefix_varname('w', hi->hi_key);
2412 }
2413
2414 // t: variables
2415 ht = &curtab->tp_vars->dv_hashtab;
2416 if (tdone < ht->ht_used)
2417 {
2418 if (tdone++ == 0)
2419 hi = ht->ht_array;
2420 else
2421 ++hi;
2422 while (HASHITEM_EMPTY(hi))
2423 ++hi;
2424 return cat_prefix_varname('t', hi->hi_key);
2425 }
2426
2427 // v: variables
2428 if (vidx < VV_LEN)
2429 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2430
2431 VIM_CLEAR(varnamebuf);
2432 varnamebuflen = 0;
2433 return NULL;
2434}
2435
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002436 char *
2437get_var_special_name(int nr)
2438{
2439 switch (nr)
2440 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002441 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2442 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002443 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002444 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002445 }
2446 internal_error("get_var_special_name()");
2447 return "42";
2448}
2449
2450/*
2451 * Returns the global variable dictionary
2452 */
2453 dict_T *
2454get_globvar_dict(void)
2455{
2456 return &globvardict;
2457}
2458
2459/*
2460 * Returns the global variable hash table
2461 */
2462 hashtab_T *
2463get_globvar_ht(void)
2464{
2465 return &globvarht;
2466}
2467
2468/*
2469 * Returns the v: variable dictionary
2470 */
2471 dict_T *
2472get_vimvar_dict(void)
2473{
2474 return &vimvardict;
2475}
2476
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002477/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002479 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480 */
2481 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002482find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002484 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2485 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486
2487 if (di == NULL)
2488 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002489 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2491 return (int)(vv - vimvars);
2492}
2493
2494
2495/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002496 * Set type of v: variable to "type".
2497 */
2498 void
2499set_vim_var_type(int idx, vartype_T type)
2500{
Bram Moolenaard787e402021-12-24 21:36:12 +00002501 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002502}
2503
2504/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002505 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002506 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002507 */
2508 void
2509set_vim_var_nr(int idx, varnumber_T val)
2510{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002511 vimvars[idx].vv_nr = val;
2512}
2513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 char *
2515get_vim_var_name(int idx)
2516{
2517 return vimvars[idx].vv_name;
2518}
2519
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002520/*
2521 * Get typval_T v: variable value.
2522 */
2523 typval_T *
2524get_vim_var_tv(int idx)
2525{
2526 return &vimvars[idx].vv_tv;
2527}
2528
Bram Moolenaard787e402021-12-24 21:36:12 +00002529 type_T *
2530get_vim_var_type(int idx, garray_T *type_list)
2531{
2532 if (vimvars[idx].vv_type != NULL)
2533 return vimvars[idx].vv_type;
2534 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2535}
2536
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002537/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002538 * Set v: variable to "tv". Only accepts the same type.
2539 * Takes over the value of "tv".
2540 */
2541 int
2542set_vim_var_tv(int idx, typval_T *tv)
2543{
Bram Moolenaard787e402021-12-24 21:36:12 +00002544 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002545 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002546 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002547 clear_tv(tv);
2548 return FAIL;
2549 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002550 // VV_RO is also checked when compiling, but let's check here as well.
2551 if (vimvars[idx].vv_flags & VV_RO)
2552 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002553 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002554 return FAIL;
2555 }
2556 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2557 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002558 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002559 return FAIL;
2560 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002561 clear_tv(&vimvars[idx].vv_di.di_tv);
2562 vimvars[idx].vv_di.di_tv = *tv;
2563 return OK;
2564}
2565
2566/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002567 * Get number v: variable value.
2568 */
2569 varnumber_T
2570get_vim_var_nr(int idx)
2571{
2572 return vimvars[idx].vv_nr;
2573}
2574
2575/*
2576 * Get string v: variable value. Uses a static buffer, can only be used once.
2577 * If the String variable has never been set, return an empty string.
2578 * Never returns NULL;
2579 */
2580 char_u *
2581get_vim_var_str(int idx)
2582{
2583 return tv_get_string(&vimvars[idx].vv_tv);
2584}
2585
2586/*
2587 * Get List v: variable value. Caller must take care of reference count when
2588 * needed.
2589 */
2590 list_T *
2591get_vim_var_list(int idx)
2592{
2593 return vimvars[idx].vv_list;
2594}
2595
2596/*
2597 * Get Dict v: variable value. Caller must take care of reference count when
2598 * needed.
2599 */
2600 dict_T *
2601get_vim_var_dict(int idx)
2602{
2603 return vimvars[idx].vv_dict;
2604}
2605
2606/*
2607 * Set v:char to character "c".
2608 */
2609 void
2610set_vim_var_char(int c)
2611{
2612 char_u buf[MB_MAXBYTES + 1];
2613
2614 if (has_mbyte)
2615 buf[(*mb_char2bytes)(c, buf)] = NUL;
2616 else
2617 {
2618 buf[0] = c;
2619 buf[1] = NUL;
2620 }
2621 set_vim_var_string(VV_CHAR, buf, -1);
2622}
2623
2624/*
2625 * Set v:count to "count" and v:count1 to "count1".
2626 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2627 */
2628 void
2629set_vcount(
2630 long count,
2631 long count1,
2632 int set_prevcount)
2633{
2634 if (set_prevcount)
2635 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2636 vimvars[VV_COUNT].vv_nr = count;
2637 vimvars[VV_COUNT1].vv_nr = count1;
2638}
2639
2640/*
2641 * Save variables that might be changed as a side effect. Used when executing
2642 * a timer callback.
2643 */
2644 void
2645save_vimvars(vimvars_save_T *vvsave)
2646{
2647 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2648 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2649 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2650}
2651
2652/*
2653 * Restore variables saved by save_vimvars().
2654 */
2655 void
2656restore_vimvars(vimvars_save_T *vvsave)
2657{
2658 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2659 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2660 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2661}
2662
2663/*
2664 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2665 * value.
2666 */
2667 void
2668set_vim_var_string(
2669 int idx,
2670 char_u *val,
2671 int len) // length of "val" to use or -1 (whole string)
2672{
2673 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002674 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002675 if (val == NULL)
2676 vimvars[idx].vv_str = NULL;
2677 else if (len == -1)
2678 vimvars[idx].vv_str = vim_strsave(val);
2679 else
2680 vimvars[idx].vv_str = vim_strnsave(val, len);
2681}
2682
2683/*
2684 * Set List v: variable to "val".
2685 */
2686 void
2687set_vim_var_list(int idx, list_T *val)
2688{
2689 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002690 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002691 vimvars[idx].vv_list = val;
2692 if (val != NULL)
2693 ++val->lv_refcount;
2694}
2695
2696/*
2697 * Set Dictionary v: variable to "val".
2698 */
2699 void
2700set_vim_var_dict(int idx, dict_T *val)
2701{
2702 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002703 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002704 vimvars[idx].vv_dict = val;
2705 if (val != NULL)
2706 {
2707 ++val->dv_refcount;
2708 dict_set_items_ro(val);
2709 }
2710}
2711
2712/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002713 * Set the v:argv list.
2714 */
2715 void
2716set_argv_var(char **argv, int argc)
2717{
2718 list_T *l = list_alloc();
2719 int i;
2720
2721 if (l == NULL)
2722 getout(1);
2723 l->lv_lock = VAR_FIXED;
2724 for (i = 0; i < argc; ++i)
2725 {
2726 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2727 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002728 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002729 }
2730 set_vim_var_list(VV_ARGV, l);
2731}
2732
2733/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002734 * Reset v:register, taking the 'clipboard' setting into account.
2735 */
2736 void
2737reset_reg_var(void)
2738{
2739 int regname = 0;
2740
2741 // Adjust the register according to 'clipboard', so that when
2742 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2743#ifdef FEAT_CLIPBOARD
2744 adjust_clip_reg(&regname);
2745#endif
2746 set_reg_var(regname);
2747}
2748
2749/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002750 * Set v:register if needed.
2751 */
2752 void
2753set_reg_var(int c)
2754{
2755 char_u regname;
2756
2757 if (c == 0 || c == ' ')
2758 regname = '"';
2759 else
2760 regname = c;
2761 // Avoid free/alloc when the value is already right.
2762 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2763 set_vim_var_string(VV_REG, &regname, 1);
2764}
2765
2766/*
2767 * Get or set v:exception. If "oldval" == NULL, return the current value.
2768 * Otherwise, restore the value to "oldval" and return NULL.
2769 * Must always be called in pairs to save and restore v:exception! Does not
2770 * take care of memory allocations.
2771 */
2772 char_u *
2773v_exception(char_u *oldval)
2774{
2775 if (oldval == NULL)
2776 return vimvars[VV_EXCEPTION].vv_str;
2777
2778 vimvars[VV_EXCEPTION].vv_str = oldval;
2779 return NULL;
2780}
2781
2782/*
2783 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2784 * Otherwise, restore the value to "oldval" and return NULL.
2785 * Must always be called in pairs to save and restore v:throwpoint! Does not
2786 * take care of memory allocations.
2787 */
2788 char_u *
2789v_throwpoint(char_u *oldval)
2790{
2791 if (oldval == NULL)
2792 return vimvars[VV_THROWPOINT].vv_str;
2793
2794 vimvars[VV_THROWPOINT].vv_str = oldval;
2795 return NULL;
2796}
2797
2798/*
2799 * Set v:cmdarg.
2800 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2801 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2802 * Must always be called in pairs!
2803 */
2804 char_u *
2805set_cmdarg(exarg_T *eap, char_u *oldarg)
2806{
2807 char_u *oldval;
2808 char_u *newval;
2809 unsigned len;
2810
2811 oldval = vimvars[VV_CMDARG].vv_str;
2812 if (eap == NULL)
2813 {
2814 vim_free(oldval);
2815 vimvars[VV_CMDARG].vv_str = oldarg;
2816 return NULL;
2817 }
2818
2819 if (eap->force_bin == FORCE_BIN)
2820 len = 6;
2821 else if (eap->force_bin == FORCE_NOBIN)
2822 len = 8;
2823 else
2824 len = 0;
2825
2826 if (eap->read_edit)
2827 len += 7;
2828
2829 if (eap->force_ff != 0)
2830 len += 10; // " ++ff=unix"
2831 if (eap->force_enc != 0)
2832 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2833 if (eap->bad_char != 0)
2834 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2835
2836 newval = alloc(len + 1);
2837 if (newval == NULL)
2838 return NULL;
2839
2840 if (eap->force_bin == FORCE_BIN)
2841 sprintf((char *)newval, " ++bin");
2842 else if (eap->force_bin == FORCE_NOBIN)
2843 sprintf((char *)newval, " ++nobin");
2844 else
2845 *newval = NUL;
2846
2847 if (eap->read_edit)
2848 STRCAT(newval, " ++edit");
2849
2850 if (eap->force_ff != 0)
2851 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2852 eap->force_ff == 'u' ? "unix"
2853 : eap->force_ff == 'd' ? "dos"
2854 : "mac");
2855 if (eap->force_enc != 0)
2856 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2857 eap->cmd + eap->force_enc);
2858 if (eap->bad_char == BAD_KEEP)
2859 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2860 else if (eap->bad_char == BAD_DROP)
2861 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2862 else if (eap->bad_char != 0)
2863 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2864 vimvars[VV_CMDARG].vv_str = newval;
2865 return oldval;
2866}
2867
2868/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002869 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002870 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2871 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002872 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2873 */
2874 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002875eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002876 char_u *name,
2877 int len, // length of "name"
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002878 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002879 typval_T *rettv, // NULL when only checking existence
2880 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002881 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002882{
2883 int ret = OK;
2884 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002885 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002886 hashtab_T *ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002887 int cc;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002888 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002889
2890 // truncate the name, so that we can use strcmp()
2891 cc = name[len];
2892 name[len] = NUL;
2893
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002894 // Check for local variable when debugging.
2895 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002896 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002897 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002898 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2899
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002900 if (v != NULL)
2901 {
2902 tv = &v->di_tv;
2903 if (dip != NULL)
2904 *dip = v;
2905 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002906 else
2907 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002908 }
2909
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002910 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002912 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002913 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2914
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002915 if (sid == 0)
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00002916 import = find_imported(p, 0, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917
2918 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002919 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002921 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002922 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00002923 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02002924 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002925 ht = &SCRIPT_VARS(sid);
2926 if (ht != NULL)
2927 {
2928 dictitem_T *v = find_var_in_ht(ht, 0, name,
2929 flags & EVAL_VAR_NOAUTOLOAD);
2930
2931 if (v != NULL)
2932 {
2933 tv = &v->di_tv;
2934 if (dip != NULL)
2935 *dip = v;
2936 }
2937 else
2938 ht = NULL;
2939 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002940 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002941 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002942 {
2943 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00002944 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002945 ret = FAIL;
2946 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002947 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002948 else
2949 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002950 if (rettv != NULL)
2951 {
2952 rettv->v_type = VAR_ANY;
2953 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
2954 }
2955 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002956 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00002958 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002959 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002960 int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002961 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002962
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002963 // In Vim9 script we can get a function reference by using the
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002964 // function name. For a global non-autoload function "g:" is
2965 // required.
2966 if (ufunc != NULL && (has_g_prefix
2967 || !func_requires_g_prefix(ufunc)))
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002968 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002969 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002970 if (rettv != NULL)
2971 {
2972 rettv->v_type = VAR_FUNC;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002973 if (has_g_prefix)
Bram Moolenaaref082e12021-12-12 21:02:03 +00002974 // Keep the "g:", otherwise script-local may be
2975 // assumed.
2976 rettv->vval.v_string = vim_strsave(name);
2977 else
2978 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002979 if (rettv->vval.v_string != NULL)
2980 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002981 }
2982 }
2983 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 }
2985
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002986 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002987 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002988 if (tv == NULL)
2989 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002990 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002991 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002992 ret = FAIL;
2993 }
2994 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002995 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01002996 svar_T *sv = NULL;
2997 int was_assigned = FALSE;
2998
Bram Moolenaar11005b02021-07-11 20:59:00 +02002999 if (ht != NULL && ht == get_script_local_ht()
3000 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02003001 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003002 sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaarf055d452021-07-08 20:57:24 +02003003 if (sv != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003004 {
Bram Moolenaarf055d452021-07-08 20:57:24 +02003005 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003006 was_assigned = sv->sv_flags & SVFLAG_ASSIGNED;
3007 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003008 }
3009
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003010 // If a list or dict variable wasn't initialized and has meaningful
3011 // type, do it now. Not for global variables, they are not
3012 // declared.
Bram Moolenaar7a222242022-03-01 19:23:24 +00003013 if (ht != &globvarht)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003014 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003015 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003016 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003017 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003018 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003019 tv->vval.v_dict = dict_alloc();
3020 if (tv->vval.v_dict != NULL)
3021 {
3022 ++tv->vval.v_dict->dv_refcount;
3023 tv->vval.v_dict->dv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003024 if (sv != NULL)
3025 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003026 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003027 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003028 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003029 && ((type != NULL && !was_assigned)
Bram Moolenaar501f9782022-03-27 16:51:04 +01003030 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003031 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003032 tv->vval.v_list = list_alloc();
3033 if (tv->vval.v_list != NULL)
3034 {
3035 ++tv->vval.v_list->lv_refcount;
3036 tv->vval.v_list->lv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003037 if (sv != NULL)
3038 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003039 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003040 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003041 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003042 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003043 || !in_vim9script()))
3044 {
3045 tv->vval.v_blob = blob_alloc();
3046 if (tv->vval.v_blob != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003047 {
Bram Moolenaar859cc212022-03-28 15:22:35 +01003048 ++tv->vval.v_blob->bv_refcount;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003049 if (sv != NULL)
3050 sv->sv_flags |= SVFLAG_ASSIGNED;
3051 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003052 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02003053 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003054 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003055 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003056 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003057
3058 name[len] = cc;
3059
3060 return ret;
3061}
3062
3063/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003064 * Check if variable "name[len]" is a local variable or an argument.
3065 * If so, "*eval_lavars_used" is set to TRUE.
3066 */
3067 void
3068check_vars(char_u *name, int len)
3069{
3070 int cc;
3071 char_u *varname;
3072 hashtab_T *ht;
3073
3074 if (eval_lavars_used == NULL)
3075 return;
3076
3077 // truncate the name, so that we can use strcmp()
3078 cc = name[len];
3079 name[len] = NUL;
3080
3081 ht = find_var_ht(name, &varname);
3082 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
3083 {
3084 if (find_var(name, NULL, TRUE) != NULL)
3085 *eval_lavars_used = TRUE;
3086 }
3087
3088 name[len] = cc;
3089}
3090
3091/*
3092 * Find variable "name" in the list of variables.
3093 * Return a pointer to it if found, NULL if not found.
3094 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003095 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003096 */
3097 dictitem_T *
3098find_var(char_u *name, hashtab_T **htp, int no_autoload)
3099{
3100 char_u *varname;
3101 hashtab_T *ht;
3102 dictitem_T *ret = NULL;
3103
3104 ht = find_var_ht(name, &varname);
3105 if (htp != NULL)
3106 *htp = ht;
3107 if (ht == NULL)
3108 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003109 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003110 if (ret != NULL)
3111 return ret;
3112
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003113 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003114 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003115 if (ret != NULL)
3116 return ret;
3117
3118 // in Vim9 script items without a scope can be script-local
3119 if (in_vim9script() && name[0] != NUL && name[1] != ':')
3120 {
3121 ht = get_script_local_ht();
3122 if (ht != NULL)
3123 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003124 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003125 if (ret != NULL)
3126 {
3127 if (htp != NULL)
3128 *htp = ht;
3129 return ret;
3130 }
3131 }
3132 }
3133
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003134 // When using "vim9script autoload" script-local items are prefixed but can
3135 // be used with s:name.
3136 if (SCRIPT_ID_VALID(current_sctx.sc_sid)
3137 && name[0] == 's' && name[1] == ':')
3138 {
3139 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
3140
3141 if (si->sn_autoload_prefix != NULL)
3142 {
3143 char_u *auto_name = concat_str(si->sn_autoload_prefix, name + 2);
3144
3145 if (auto_name != NULL)
3146 {
3147 ht = &globvarht;
3148 ret = find_var_in_ht(ht, *name, auto_name, TRUE);
Bram Moolenaar130f65d2022-01-13 20:39:41 +00003149 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003150 if (ret != NULL)
3151 {
3152 if (htp != NULL)
3153 *htp = ht;
3154 return ret;
3155 }
3156 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003157 }
3158 }
3159
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003160 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003161}
3162
3163/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00003164 * Like find_var() but if the name starts with <SNR>99_ then look in the
3165 * referenced script (used for a funcref).
3166 */
3167 dictitem_T *
3168find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
3169{
3170 if (STRNCMP(name, "<SNR>", 5) == 0 && isdigit(name[5]))
3171 {
3172 char_u *p = name + 5;
3173 int sid = getdigits(&p);
3174
3175 if (SCRIPT_ID_VALID(sid) && *p == '_')
3176 {
3177 hashtab_T *ht = &SCRIPT_VARS(sid);
3178
3179 if (ht != NULL)
3180 {
3181 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
3182
3183 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003184 {
3185 if (htp != NULL)
3186 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00003187 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003188 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00003189 }
3190 }
3191 }
3192
3193 return find_var(name, htp, no_autoload);
3194}
3195
3196/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003197 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02003198 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003199 * Returns NULL if not found.
3200 */
3201 dictitem_T *
3202find_var_in_ht(
3203 hashtab_T *ht,
3204 int htname,
3205 char_u *varname,
3206 int no_autoload)
3207{
3208 hashitem_T *hi;
3209
3210 if (*varname == NUL)
3211 {
3212 // Must be something like "s:", otherwise "ht" would be NULL.
3213 switch (htname)
3214 {
3215 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
3216 case 'g': return &globvars_var;
3217 case 'v': return &vimvars_var;
3218 case 'b': return &curbuf->b_bufvar;
3219 case 'w': return &curwin->w_winvar;
3220 case 't': return &curtab->tp_winvar;
3221 case 'l': return get_funccal_local_var();
3222 case 'a': return get_funccal_args_var();
3223 }
3224 return NULL;
3225 }
3226
3227 hi = hash_find(ht, varname);
3228 if (HASHITEM_EMPTY(hi))
3229 {
3230 // For global variables we may try auto-loading the script. If it
3231 // worked find the variable again. Don't auto-load a script if it was
3232 // loaded already, otherwise it would be loaded every time when
3233 // checking if a function name is a Funcref variable.
3234 if (ht == &globvarht && !no_autoload)
3235 {
3236 // Note: script_autoload() may make "hi" invalid. It must either
3237 // be obtained again or not used.
3238 if (!script_autoload(varname, FALSE) || aborting())
3239 return NULL;
3240 hi = hash_find(ht, varname);
3241 }
3242 if (HASHITEM_EMPTY(hi))
3243 return NULL;
3244 }
3245 return HI2DI(hi);
3246}
3247
3248/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 * Get the script-local hashtab. NULL if not in a script context.
3250 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003251 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252get_script_local_ht(void)
3253{
3254 scid_T sid = current_sctx.sc_sid;
3255
Bram Moolenaare3d46852020-08-29 13:39:17 +02003256 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 return &SCRIPT_VARS(sid);
3258 return NULL;
3259}
3260
3261/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003262 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003263 * When "cmd" is TRUE it must look like a command, a function must be followed
3264 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003265 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003267 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003268lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003269 char_u *name,
3270 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003271 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003272 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273{
3274 hashtab_T *ht = get_script_local_ht();
3275 char_u buffer[30];
3276 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003277 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003279 int is_global = FALSE;
3280 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281
3282 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003283 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 if (len < sizeof(buffer) - 1)
3285 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003286 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287 vim_strncpy(buffer, name, len);
3288 p = buffer;
3289 }
3290 else
3291 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003292 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003294 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 }
3296
3297 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003298 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299
3300 // if not script-local, then perhaps imported
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003301 if (res == FAIL && find_imported(p, 0, FALSE) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003302 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 if (p != buffer)
3304 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003305
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003306 // Find a function, so that a following "->" works.
3307 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3308 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003309 if (res != OK)
3310 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003311 p = skipwhite(name + len);
3312
3313 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003314 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003315 // Do not check for an internal function, since it might also be a
3316 // valid command, such as ":split" versus "split()".
3317 // Skip "g:" before a function name.
3318 if (name[0] == 'g' && name[1] == ':')
3319 {
3320 is_global = TRUE;
3321 fname = name + 2;
3322 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003323 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003324 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003325 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003326 }
3327
Bram Moolenaar709664c2020-12-12 14:33:41 +01003328 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329}
3330
3331/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003332 * Find the hashtab used for a variable name.
3333 * Return NULL if the name is not valid.
3334 * Set "varname" to the start of name without ':'.
3335 */
3336 hashtab_T *
3337find_var_ht(char_u *name, char_u **varname)
3338{
3339 hashitem_T *hi;
3340 hashtab_T *ht;
3341
3342 if (name[0] == NUL)
3343 return NULL;
3344 if (name[1] != ':')
3345 {
3346 // The name must not start with a colon or #.
3347 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3348 return NULL;
3349 *varname = name;
3350
3351 // "version" is "v:version" in all scopes if scriptversion < 3.
3352 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003353 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003354 {
3355 hi = hash_find(&compat_hashtab, name);
3356 if (!HASHITEM_EMPTY(hi))
3357 return &compat_hashtab;
3358 }
3359
3360 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361 if (ht != NULL)
3362 return ht; // local variable
3363
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003364 // In Vim9 script items at the script level are script-local, except
3365 // for autoload names.
3366 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 {
3368 ht = get_script_local_ht();
3369 if (ht != NULL)
3370 return ht;
3371 }
3372
3373 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003374 }
3375 *varname = name + 2;
3376 if (*name == 'g') // global variable
3377 return &globvarht;
3378 // There must be no ':' or '#' in the rest of the name, unless g: is used
3379 if (vim_strchr(name + 2, ':') != NULL
3380 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3381 return NULL;
3382 if (*name == 'b') // buffer variable
3383 return &curbuf->b_vars->dv_hashtab;
3384 if (*name == 'w') // window variable
3385 return &curwin->w_vars->dv_hashtab;
3386 if (*name == 't') // tab page variable
3387 return &curtab->tp_vars->dv_hashtab;
3388 if (*name == 'v') // v: variable
3389 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003390 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003391 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003393 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 if (*name == 'a') // a: function argument
3395 return get_funccal_args_ht();
3396 if (*name == 'l') // l: local function variable
3397 return get_funccal_local_ht();
3398 }
3399 if (*name == 's') // script variable
3400 {
3401 ht = get_script_local_ht();
3402 if (ht != NULL)
3403 return ht;
3404 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003405 return NULL;
3406}
3407
3408/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003409 * Get the string value of a (global/local) variable.
3410 * Note: see tv_get_string() for how long the pointer remains valid.
3411 * Returns NULL when it doesn't exist.
3412 */
3413 char_u *
3414get_var_value(char_u *name)
3415{
3416 dictitem_T *v;
3417
3418 v = find_var(name, NULL, FALSE);
3419 if (v == NULL)
3420 return NULL;
3421 return tv_get_string(&v->di_tv);
3422}
3423
3424/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003425 * Allocate a new hashtab for a sourced script. It will be used while
3426 * sourcing this script and when executing functions defined in the script.
3427 */
3428 void
3429new_script_vars(scid_T id)
3430{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003431 scriptvar_T *sv;
3432
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003433 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3434 if (sv == NULL)
3435 return;
3436 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003437 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003438}
3439
3440/*
3441 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3442 * point to it.
3443 */
3444 void
3445init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3446{
3447 hash_init(&dict->dv_hashtab);
3448 dict->dv_lock = 0;
3449 dict->dv_scope = scope;
3450 dict->dv_refcount = DO_NOT_FREE_CNT;
3451 dict->dv_copyID = 0;
3452 dict_var->di_tv.vval.v_dict = dict;
3453 dict_var->di_tv.v_type = VAR_DICT;
3454 dict_var->di_tv.v_lock = VAR_FIXED;
3455 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3456 dict_var->di_key[0] = NUL;
3457}
3458
3459/*
3460 * Unreference a dictionary initialized by init_var_dict().
3461 */
3462 void
3463unref_var_dict(dict_T *dict)
3464{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003465 // Now the dict needs to be freed if no one else is using it, go back to
3466 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003467 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3468 dict_unref(dict);
3469}
3470
3471/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003472 * Clean up a list of internal variables.
3473 * Frees all allocated variables and the value they contain.
3474 * Clears hashtab "ht", does not free it.
3475 */
3476 void
3477vars_clear(hashtab_T *ht)
3478{
3479 vars_clear_ext(ht, TRUE);
3480}
3481
3482/*
3483 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3484 */
3485 void
3486vars_clear_ext(hashtab_T *ht, int free_val)
3487{
3488 int todo;
3489 hashitem_T *hi;
3490 dictitem_T *v;
3491
3492 hash_lock(ht);
3493 todo = (int)ht->ht_used;
3494 for (hi = ht->ht_array; todo > 0; ++hi)
3495 {
3496 if (!HASHITEM_EMPTY(hi))
3497 {
3498 --todo;
3499
3500 // Free the variable. Don't remove it from the hashtab,
3501 // ht_array might change then. hash_clear() takes care of it
3502 // later.
3503 v = HI2DI(hi);
3504 if (free_val)
3505 clear_tv(&v->di_tv);
3506 if (v->di_flags & DI_FLAGS_ALLOC)
3507 vim_free(v);
3508 }
3509 }
3510 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003511 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003512}
3513
3514/*
3515 * Delete a variable from hashtab "ht" at item "hi".
3516 * Clear the variable value and free the dictitem.
3517 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003518 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003519delete_var(hashtab_T *ht, hashitem_T *hi)
3520{
3521 dictitem_T *di = HI2DI(hi);
3522
3523 hash_remove(ht, hi);
3524 clear_tv(&di->di_tv);
3525 vim_free(di);
3526}
3527
3528/*
3529 * List the value of one internal variable.
3530 */
3531 static void
3532list_one_var(dictitem_T *v, char *prefix, int *first)
3533{
3534 char_u *tofree;
3535 char_u *s;
3536 char_u numbuf[NUMBUFLEN];
3537
3538 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3539 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3540 s == NULL ? (char_u *)"" : s, first);
3541 vim_free(tofree);
3542}
3543
3544 static void
3545list_one_var_a(
3546 char *prefix,
3547 char_u *name,
3548 int type,
3549 char_u *string,
3550 int *first) // when TRUE clear rest of screen and set to FALSE
3551{
3552 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3553 msg_start();
3554 msg_puts(prefix);
3555 if (name != NULL) // "a:" vars don't have a name stored
3556 msg_puts((char *)name);
3557 msg_putchar(' ');
3558 msg_advance(22);
3559 if (type == VAR_NUMBER)
3560 msg_putchar('#');
3561 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3562 msg_putchar('*');
3563 else if (type == VAR_LIST)
3564 {
3565 msg_putchar('[');
3566 if (*string == '[')
3567 ++string;
3568 }
3569 else if (type == VAR_DICT)
3570 {
3571 msg_putchar('{');
3572 if (*string == '{')
3573 ++string;
3574 }
3575 else
3576 msg_putchar(' ');
3577
3578 msg_outtrans(string);
3579
3580 if (type == VAR_FUNC || type == VAR_PARTIAL)
3581 msg_puts("()");
3582 if (*first)
3583 {
3584 msg_clr_eos();
3585 *first = FALSE;
3586 }
3587}
3588
3589/*
3590 * Set variable "name" to value in "tv".
3591 * If the variable already exists, the value is updated.
3592 * Otherwise the variable is created.
3593 */
3594 void
3595set_var(
3596 char_u *name,
3597 typval_T *tv,
3598 int copy) // make copy of value in "tv"
3599{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003600 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003601}
3602
3603/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003604 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003605 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003606 * If the variable already exists and "is_const" is FALSE the value is updated.
3607 * Otherwise the variable is created.
3608 */
3609 void
3610set_var_const(
3611 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003612 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003613 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003614 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003615 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003616 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003617 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003618{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003619 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003620 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003621 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622 dictitem_T *di;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003623 typval_T *dest_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003624 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003625 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003626 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003628 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003629 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003630 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003631 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003632 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003633
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003634 if (sid != 0)
3635 {
3636 if (SCRIPT_ID_VALID(sid))
3637 ht = &SCRIPT_VARS(sid);
3638 varname = name;
3639 }
3640 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003641 {
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003642 scriptitem_T *si;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003643
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003644 if (in_vim9script() && is_export
3645 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3646 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
Bram Moolenaar0e1574c2022-03-03 17:05:35 +00003647 ->sn_autoload_prefix != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003648 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003649 // In a vim9 autoload script an exported variable is put in the
3650 // global namespace with the autoload prefix.
3651 var_in_autoload = TRUE;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003652 varname = concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003653 if (varname == NULL)
3654 goto failed;
3655 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003656 ht = &globvarht;
3657 }
3658 else
3659 ht = find_var_ht(name, &varname);
3660 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003661 if (ht == NULL || *varname == NUL)
3662 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003663 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003664 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003665 }
Bram Moolenaar54969f42022-02-07 13:56:44 +00003666 is_script_local = ht == get_script_local_ht() || sid != 0
3667 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003668
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003669 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003670 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003671 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003672 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003673 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003674 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003675 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003676 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003677 }
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003678 if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
3679 && vim_strchr((char_u *)"gwbt", name[0]) != NULL)
3680 // Do not make g:var, w:var, b:var or t:var final.
3681 flags &= ~ASSIGN_FINAL;
3682
Bram Moolenaare535db82021-03-31 21:07:24 +02003683 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003684 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3685 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003686 // For "[a, _] = list" the underscore is ignored.
3687 if ((flags & ASSIGN_UNPACK) == 0)
3688 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003689 goto failed;
3690 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003693
Bram Moolenaar24e93162021-07-18 20:40:33 +02003694 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003695 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003696 imported_T *import = find_imported(varname, 0, FALSE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003697
Bram Moolenaar24e93162021-07-18 20:40:33 +02003698 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003699 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003700 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02003701 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003703 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003704 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003706 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3707 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003708 }
Bram Moolenaar75e27d72022-02-13 13:56:29 +00003709 if (!in_vim9script())
3710 {
3711 semsg(_(e_cannot_create_vim9_script_variable_in_function_str),
3712 name);
3713 goto failed;
3714 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003715 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003716
Bram Moolenaar993faa32022-02-21 15:59:11 +00003717 // Search in parent scope which is possible to reference from lambda
3718 if (di == NULL)
3719 di = find_var_in_scoped_ht(name, TRUE);
3720
3721 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
3722 && var_wrong_func_name(name, di == NULL))
3723 goto failed;
3724
3725 if (need_convert_to_bool(type, tv))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003726 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003727 // Destination is a bool and the value is not, but it can be
3728 // converted.
3729 CLEAR_FIELD(bool_tv);
3730 bool_tv.v_type = VAR_BOOL;
3731 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3732 tv = &bool_tv;
3733 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003734
Bram Moolenaar993faa32022-02-21 15:59:11 +00003735 if (di != NULL)
3736 {
3737 // Item already exists. Allowed to replace when reloading.
3738 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003739 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003740 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
3741 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003742 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003743 emsg(_(e_cannot_modify_existing_variable));
3744 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003745 }
3746
Bram Moolenaar993faa32022-02-21 15:59:11 +00003747 if (is_script_local && vim9script
3748 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003749 {
3750 semsg(_(e_redefining_script_item_str), name);
3751 goto failed;
3752 }
3753
Bram Moolenaar993faa32022-02-21 15:59:11 +00003754 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003755 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003756 where_T where = WHERE_INIT;
Bram Moolenaar7a411a32022-04-04 14:58:06 +01003757 svar_T *sv = find_typval_in_script(&di->di_tv, sid, TRUE);
Bram Moolenaar993faa32022-02-21 15:59:11 +00003758
3759 if (sv != NULL)
3760 {
3761 // check the type and adjust to bool if needed
3762 where.wt_index = var_idx;
3763 where.wt_variable = TRUE;
3764 if (check_script_var_type(sv, tv, name, where) == FAIL)
3765 goto failed;
3766 if (type == NULL)
3767 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003768 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003769 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003770 }
3771
Bram Moolenaar993faa32022-02-21 15:59:11 +00003772 if ((flags & ASSIGN_FOR_LOOP) == 0
3773 && var_check_permission(di, name) == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003774 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003775 }
3776 else
3777 {
3778 // can only redefine once
3779 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003780
Bram Moolenaar993faa32022-02-21 15:59:11 +00003781 // A Vim9 script-local variable is also present in sn_all_vars
3782 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003783 if (var_in_vim9script || var_in_autoload)
Bram Moolenaar993faa32022-02-21 15:59:11 +00003784 update_vim9_script_var(FALSE, di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003785 var_in_autoload ? name : di->di_key, flags,
Bram Moolenaar993faa32022-02-21 15:59:11 +00003786 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003787 }
3788
Bram Moolenaar993faa32022-02-21 15:59:11 +00003789 // existing variable, need to clear the value
3790
3791 // Handle setting internal di: variables separately where needed to
3792 // prevent changing the type.
3793 if (ht == &vimvarht)
3794 {
3795 if (di->di_tv.v_type == VAR_STRING)
3796 {
3797 VIM_CLEAR(di->di_tv.vval.v_string);
3798 if (copy || tv->v_type != VAR_STRING)
3799 {
3800 char_u *val = tv_get_string(tv);
3801
3802 // Careful: when assigning to v:errmsg and
3803 // tv_get_string() causes an error message the variable
3804 // will already be set.
3805 if (di->di_tv.vval.v_string == NULL)
3806 di->di_tv.vval.v_string = vim_strsave(val);
3807 }
3808 else
3809 {
3810 // Take over the string to avoid an extra alloc/free.
3811 di->di_tv.vval.v_string = tv->vval.v_string;
3812 tv->vval.v_string = NULL;
3813 }
3814 goto failed;
3815 }
3816 else if (di->di_tv.v_type == VAR_NUMBER)
3817 {
3818 di->di_tv.vval.v_number = tv_get_number(tv);
3819 if (STRCMP(varname, "searchforward") == 0)
3820 set_search_direction(di->di_tv.vval.v_number
3821 ? '/' : '?');
3822#ifdef FEAT_SEARCH_EXTRA
3823 else if (STRCMP(varname, "hlsearch") == 0)
3824 {
3825 no_hlsearch = !di->di_tv.vval.v_number;
3826 redraw_all_later(SOME_VALID);
3827 }
3828#endif
3829 goto failed;
3830 }
3831 else if (di->di_tv.v_type != tv->v_type)
3832 {
3833 semsg(_(e_setting_str_to_value_with_wrong_type), name);
3834 goto failed;
3835 }
3836 }
3837
3838 clear_tv(&di->di_tv);
3839 }
3840 else
3841 {
3842 // Item not found, check if a function already exists.
3843 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
3844 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
3845 {
3846 semsg(_(e_redefining_script_item_str), name);
3847 goto failed;
3848 }
3849
3850 // add a new variable
3851 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3852 {
3853 semsg(_(e_unknown_variable_str), name);
3854 goto failed;
3855 }
3856
3857 // Can't add "v:" or "a:" variable.
3858 if (ht == &vimvarht || ht == get_funccal_args_ht())
3859 {
3860 semsg(_(e_illegal_variable_name_str), name);
3861 goto failed;
3862 }
3863
3864 // Make sure the variable name is valid. In Vim9 script an
3865 // autoload variable must be prefixed with "g:" unless in an
3866 // autoload script.
3867 if (!valid_varname(varname, -1, !vim9script
3868 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
3869 goto failed;
3870
3871 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3872 if (di == NULL)
3873 goto failed;
3874 STRCPY(di->di_key, varname);
3875 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
3876 {
3877 vim_free(di);
3878 goto failed;
3879 }
3880 di->di_flags = DI_FLAGS_ALLOC;
3881 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3882 di->di_flags |= DI_FLAGS_LOCK;
3883
3884 // A Vim9 script-local variable is also added to sn_all_vars and
3885 // sn_var_vals. It may set "type" from "tv".
3886 if (var_in_vim9script || var_in_autoload)
3887 update_vim9_script_var(TRUE, di,
3888 var_in_autoload ? name : di->di_key, flags,
3889 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003890 }
3891
Bram Moolenaar993faa32022-02-21 15:59:11 +00003892 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003893 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003894 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003895 else
3896 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003897 *dest_tv = *tv;
3898 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003899 init_tv(tv);
3900 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003901 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003902
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003903 if (vim9script && type != NULL)
Bram Moolenaar381692b2022-02-02 20:01:27 +00003904 set_tv_type(dest_tv, type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003905
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003906 // ":const var = value" locks the value
3907 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003908 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003909 // Like :lockvar! name: lock the value and what it contains, but only
3910 // if the reference count is up to one. That locks only literal
3911 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003912 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003913
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003914failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003915 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003916 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003917 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003918}
3919
3920/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003921 * Check in this order for backwards compatibility:
3922 * - Whether the variable is read-only
3923 * - Whether the variable value is locked
3924 * - Whether the variable is locked
3925 */
3926 int
3927var_check_permission(dictitem_T *di, char_u *name)
3928{
3929 if (var_check_ro(di->di_flags, name, FALSE)
3930 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3931 || var_check_lock(di->di_flags, name, FALSE))
3932 return FAIL;
3933 return OK;
3934}
3935
3936/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003937 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3938 * Also give an error message.
3939 */
3940 int
3941var_check_ro(int flags, char_u *name, int use_gettext)
3942{
3943 if (flags & DI_FLAGS_RO)
3944 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003945 if (name == NULL)
3946 emsg(_(e_cannot_change_readonly_variable));
3947 else
3948 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02003949 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003950 return TRUE;
3951 }
3952 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3953 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003954 if (name == NULL)
3955 emsg(_(e_cannot_set_variable_in_sandbox));
3956 else
3957 semsg(_(e_cannot_set_variable_in_sandbox_str),
3958 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003959 return TRUE;
3960 }
3961 return FALSE;
3962}
3963
3964/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003965 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3966 * Also give an error message.
3967 */
3968 int
3969var_check_lock(int flags, char_u *name, int use_gettext)
3970{
3971 if (flags & DI_FLAGS_LOCK)
3972 {
3973 semsg(_(e_variable_is_locked_str),
3974 use_gettext ? (char_u *)_(name) : name);
3975 return TRUE;
3976 }
3977 return FALSE;
3978}
3979
3980/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003981 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3982 * Also give an error message.
3983 */
3984 int
3985var_check_fixed(int flags, char_u *name, int use_gettext)
3986{
3987 if (flags & DI_FLAGS_FIX)
3988 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003989 if (name == NULL)
3990 emsg(_(e_cannot_delete_variable));
3991 else
3992 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003993 use_gettext ? (char_u *)_(name) : name);
3994 return TRUE;
3995 }
3996 return FALSE;
3997}
3998
3999/*
4000 * Check if a funcref is assigned to a valid variable name.
4001 * Return TRUE and give an error if not.
4002 */
4003 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004004var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004005 char_u *name, // points to start of variable name
4006 int new_var) // TRUE when creating the variable
4007{
Bram Moolenaar32154662021-03-28 21:14:06 +02004008 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
4009 // the name can be used without the s: prefix.
4010 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
4011 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004012 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
4013 ? name[2] : name[0]))
4014 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004015 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004016 return TRUE;
4017 }
4018 // Don't allow hiding a function. When "v" is not NULL we might be
4019 // assigning another function to the same var, the type is checked
4020 // below.
4021 if (new_var && function_exists(name, FALSE))
4022 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004023 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004024 name);
4025 return TRUE;
4026 }
4027 return FALSE;
4028}
4029
4030/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004031 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
4032 * value. Also give an error message, using "name" or _("name") when
4033 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004034 */
4035 int
Bram Moolenaara187c432020-09-16 21:08:28 +02004036value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004037{
4038 if (lock & VAR_LOCKED)
4039 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004040 if (name == NULL)
4041 emsg(_(e_value_is_locked));
4042 else
4043 semsg(_(e_value_is_locked_str),
4044 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004045 return TRUE;
4046 }
4047 if (lock & VAR_FIXED)
4048 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004049 if (name == NULL)
4050 emsg(_(e_cannot_change_value));
4051 else
4052 semsg(_(e_cannot_change_value_of_str),
4053 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004054 return TRUE;
4055 }
4056 return FALSE;
4057}
4058
4059/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01004060 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004061 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004062 * Return FALSE and give an error if not.
4063 */
4064 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004065valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004066{
4067 char_u *p;
4068
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004069 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004070 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01004071 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004072 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004073 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004074 return FALSE;
4075 }
4076 return TRUE;
4077}
4078
4079/*
LemonBoy47d4e312022-05-04 18:12:55 +01004080 * Implements the logic to retrieve local variable and option values.
4081 * Used by "getwinvar()" "gettabvar()" "gettabwinvar()" "getbufvar()".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004082 */
4083 static void
LemonBoy47d4e312022-05-04 18:12:55 +01004084get_var_from(
4085 char_u *varname,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004086 typval_T *rettv,
LemonBoy47d4e312022-05-04 18:12:55 +01004087 typval_T *deftv, // Default value if not found.
4088 int htname, // 't'ab, 'w'indow or 'b'uffer local.
4089 tabpage_T *tp, // can be NULL
4090 win_T *win,
4091 buf_T *buf) // Ignored if htname is not 'b'.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004092{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004093 dictitem_T *v;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004094 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004095 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004096 int need_switch_win;
4097
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004098 ++emsg_off;
4099
4100 rettv->v_type = VAR_STRING;
4101 rettv->vval.v_string = NULL;
4102
LemonBoy47d4e312022-05-04 18:12:55 +01004103 if (varname != NULL && tp != NULL && win != NULL
4104 && (htname != 'b' || buf != NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004105 {
4106 // Set curwin to be our win, temporarily. Also set the tabpage,
4107 // otherwise the window is not valid. Only do this when needed,
4108 // autocommands get blocked.
LemonBoy47d4e312022-05-04 18:12:55 +01004109 // If we have a buffer reference avoid the switching, we're saving and
4110 // restoring curbuf directly.
4111 need_switch_win = !(tp == curtab && win == curwin) || (buf != NULL);
4112 if (!need_switch_win || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004113 {
LemonBoy47d4e312022-05-04 18:12:55 +01004114 // Handle options. There are no tab-local options.
4115 if (*varname == '&' && htname != 't')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004116 {
LemonBoy47d4e312022-05-04 18:12:55 +01004117 buf_T *save_curbuf = curbuf;
4118
4119 // Change curbuf so the option is read from the correct buffer.
4120 if (buf != NULL && htname == 'b')
4121 curbuf = buf;
4122
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004123 if (varname[1] == NUL)
4124 {
4125 // get all window-local options in a dict
LemonBoy47d4e312022-05-04 18:12:55 +01004126 dict_T *opts = get_winbuf_options(htname == 'b');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004127
4128 if (opts != NULL)
4129 {
4130 rettv_dict_set(rettv, opts);
4131 done = TRUE;
4132 }
4133 }
LemonBoy47d4e312022-05-04 18:12:55 +01004134 else if (eval_option(&varname, rettv, TRUE) == OK)
4135 // Local option
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004136 done = TRUE;
LemonBoy47d4e312022-05-04 18:12:55 +01004137
4138 curbuf = save_curbuf;
4139 }
4140 else if (*varname == NUL)
4141 {
4142 // Empty string: return a dict with all the local variables.
4143 if (htname == 'b')
4144 v = &buf->b_bufvar;
4145 else if (htname == 'w')
4146 v = &win->w_winvar;
4147 else
4148 v = &tp->tp_winvar;
4149 copy_tv(&v->di_tv, rettv);
4150 done = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004151 }
4152 else
4153 {
LemonBoy47d4e312022-05-04 18:12:55 +01004154 hashtab_T *ht;
4155
4156 if (htname == 'b')
4157 ht = &buf->b_vars->dv_hashtab;
4158 else if (htname == 'w')
4159 ht = &win->w_vars->dv_hashtab;
4160 else
4161 ht = &tp->tp_vars->dv_hashtab;
4162
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004163 // Look up the variable.
LemonBoy47d4e312022-05-04 18:12:55 +01004164 v = find_var_in_ht(ht, htname, varname, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004165 if (v != NULL)
4166 {
4167 copy_tv(&v->di_tv, rettv);
4168 done = TRUE;
4169 }
4170 }
4171 }
4172
4173 if (need_switch_win)
4174 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004175 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004176 }
4177
LemonBoy47d4e312022-05-04 18:12:55 +01004178 if (!done && deftv->v_type != VAR_UNKNOWN)
4179 // use the default value
4180 copy_tv(deftv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004181
4182 --emsg_off;
4183}
4184
4185/*
LemonBoy47d4e312022-05-04 18:12:55 +01004186 * getwinvar() and gettabwinvar()
4187 */
4188 static void
4189getwinvar(
4190 typval_T *argvars,
4191 typval_T *rettv,
4192 int off) // 1 for gettabwinvar()
4193{
4194 char_u *varname;
4195 tabpage_T *tp;
4196 win_T *win;
4197
4198 if (off == 1)
4199 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4200 else
4201 tp = curtab;
4202 win = find_win_by_nr(&argvars[off], tp);
4203 varname = tv_get_string_chk(&argvars[off + 1]);
4204
4205 get_var_from(varname, rettv, &argvars[off + 2], 'w', tp, win, NULL);
4206}
4207
4208/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02004209 * Set option "varname" to the value of "varp" for the current buffer/window.
4210 */
4211 static void
4212set_option_from_tv(char_u *varname, typval_T *varp)
4213{
4214 long numval = 0;
4215 char_u *strval;
4216 char_u nbuf[NUMBUFLEN];
4217 int error = FALSE;
4218
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004219 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004220 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004221 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004222 strval = (char_u *)"0"; // avoid using "false"
4223 }
4224 else
4225 {
4226 if (!in_vim9script() || varp->v_type != VAR_STRING)
4227 numval = (long)tv_get_number_chk(varp, &error);
4228 strval = tv_get_string_buf_chk(varp, nbuf);
4229 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02004230 if (!error && strval != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004231 set_option_value_give_err(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar191929b2020-08-19 21:20:49 +02004232}
4233
4234/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004235 * "setwinvar()" and "settabwinvar()" functions
4236 */
4237 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004238setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004239{
4240 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004241 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004242 int need_switch_win;
4243 char_u *varname, *winvarname;
4244 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004245 tabpage_T *tp = NULL;
4246
4247 if (check_secure())
4248 return;
4249
4250 if (off == 1)
4251 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4252 else
4253 tp = curtab;
4254 win = find_win_by_nr(&argvars[off], tp);
4255 varname = tv_get_string_chk(&argvars[off + 1]);
4256 varp = &argvars[off + 2];
4257
4258 if (win != NULL && varname != NULL && varp != NULL)
4259 {
4260 need_switch_win = !(tp == curtab && win == curwin);
4261 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00004262 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004263 {
4264 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02004265 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004266 else
4267 {
4268 winvarname = alloc(STRLEN(varname) + 3);
4269 if (winvarname != NULL)
4270 {
4271 STRCPY(winvarname, "w:");
4272 STRCPY(winvarname + 2, varname);
4273 set_var(winvarname, varp, TRUE);
4274 vim_free(winvarname);
4275 }
4276 }
4277 }
4278 if (need_switch_win)
Bram Moolenaar18f47402022-01-06 13:24:51 +00004279 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004280 }
4281}
4282
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004283/*
4284 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4285 * v:option_type, and v:option_command.
4286 */
4287 void
4288reset_v_option_vars(void)
4289{
4290 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4291 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4292 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4293 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4294 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4295 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4296}
4297
4298/*
4299 * Add an assert error to v:errors.
4300 */
4301 void
4302assert_error(garray_T *gap)
4303{
4304 struct vimvar *vp = &vimvars[VV_ERRORS];
4305
Bram Moolenaard787e402021-12-24 21:36:12 +00004306 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004307 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004308 set_vim_var_list(VV_ERRORS, list_alloc());
4309 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4310}
4311
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004312 int
4313var_exists(char_u *var)
4314{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004315 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004316 char_u *name;
4317 char_u *tofree;
4318 typval_T tv;
4319 int len = 0;
4320 int n = FALSE;
4321
4322 // get_name_len() takes care of expanding curly braces
4323 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004324 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004325 if (len > 0)
4326 {
4327 if (tofree != NULL)
4328 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004329 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004330 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004331 if (n)
4332 {
4333 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004334 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004335 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4336 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004337 if (n)
4338 clear_tv(&tv);
4339 }
4340 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004341 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004342 n = FALSE;
4343
4344 vim_free(tofree);
4345 return n;
4346}
4347
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004348static lval_T *redir_lval = NULL;
4349#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4350static garray_T redir_ga; // only valid when redir_lval is not NULL
4351static char_u *redir_endp = NULL;
4352static char_u *redir_varname = NULL;
4353
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004354 int
4355alloc_redir_lval(void)
4356{
4357 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4358 if (redir_lval == NULL)
4359 return FAIL;
4360 return OK;
4361}
4362
4363 void
4364clear_redir_lval(void)
4365{
4366 VIM_CLEAR(redir_lval);
4367}
4368
4369 void
4370init_redir_ga(void)
4371{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004372 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004373}
4374
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004375/*
4376 * Start recording command output to a variable
4377 * When "append" is TRUE append to an existing variable.
4378 * Returns OK if successfully completed the setup. FAIL otherwise.
4379 */
4380 int
4381var_redir_start(char_u *name, int append)
4382{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004383 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004384 typval_T tv;
4385
4386 // Catch a bad name early.
4387 if (!eval_isnamec1(*name))
4388 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004389 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004390 return FAIL;
4391 }
4392
4393 // Make a copy of the name, it is used in redir_lval until redir ends.
4394 redir_varname = vim_strsave(name);
4395 if (redir_varname == NULL)
4396 return FAIL;
4397
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004398 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004399 {
4400 var_redir_stop();
4401 return FAIL;
4402 }
4403
4404 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004405 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004406
4407 // Parse the variable name (can be a dict or list entry).
4408 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4409 FNE_CHECK_START);
4410 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4411 {
4412 clear_lval(redir_lval);
4413 if (redir_endp != NULL && *redir_endp != NUL)
4414 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004415 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004416 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004417 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004418 redir_endp = NULL; // don't store a value, only cleanup
4419 var_redir_stop();
4420 return FAIL;
4421 }
4422
4423 // check if we can write to the variable: set it to or append an empty
4424 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004425 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004426 tv.v_type = VAR_STRING;
4427 tv.vval.v_string = (char_u *)"";
4428 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004429 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004430 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004431 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004432 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004433 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004434 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004435 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004436 {
4437 redir_endp = NULL; // don't store a value, only cleanup
4438 var_redir_stop();
4439 return FAIL;
4440 }
4441
4442 return OK;
4443}
4444
4445/*
4446 * Append "value[value_len]" to the variable set by var_redir_start().
4447 * The actual appending is postponed until redirection ends, because the value
4448 * appended may in fact be the string we write to, changing it may cause freed
4449 * memory to be used:
4450 * :redir => foo
4451 * :let foo
4452 * :redir END
4453 */
4454 void
4455var_redir_str(char_u *value, int value_len)
4456{
4457 int len;
4458
4459 if (redir_lval == NULL)
4460 return;
4461
4462 if (value_len == -1)
4463 len = (int)STRLEN(value); // Append the entire string
4464 else
4465 len = value_len; // Append only "value_len" characters
4466
4467 if (ga_grow(&redir_ga, len) == OK)
4468 {
4469 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4470 redir_ga.ga_len += len;
4471 }
4472 else
4473 var_redir_stop();
4474}
4475
4476/*
4477 * Stop redirecting command output to a variable.
4478 * Frees the allocated memory.
4479 */
4480 void
4481var_redir_stop(void)
4482{
4483 typval_T tv;
4484
4485 if (EVALCMD_BUSY)
4486 {
4487 redir_lval = NULL;
4488 return;
4489 }
4490
4491 if (redir_lval != NULL)
4492 {
4493 // If there was no error: assign the text to the variable.
4494 if (redir_endp != NULL)
4495 {
4496 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4497 tv.v_type = VAR_STRING;
4498 tv.vval.v_string = redir_ga.ga_data;
4499 // Call get_lval() again, if it's inside a Dict or List it may
4500 // have changed.
4501 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4502 FALSE, FALSE, 0, FNE_CHECK_START);
4503 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004504 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004505 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004506 clear_lval(redir_lval);
4507 }
4508
4509 // free the collected output
4510 VIM_CLEAR(redir_ga.ga_data);
4511
4512 VIM_CLEAR(redir_lval);
4513 }
4514 VIM_CLEAR(redir_varname);
4515}
4516
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004517/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004518 * Get the collected redirected text and clear redir_ga.
4519 */
4520 char_u *
4521get_clear_redir_ga(void)
4522{
4523 char_u *res;
4524
4525 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4526 res = redir_ga.ga_data;
4527 redir_ga.ga_data = NULL;
4528 return res;
4529}
4530
4531/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004532 * "gettabvar()" function
4533 */
4534 void
4535f_gettabvar(typval_T *argvars, typval_T *rettv)
4536{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004537 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004538 tabpage_T *tp;
4539 win_T *win = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004540
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004541 if (in_vim9script()
4542 && (check_for_number_arg(argvars, 0) == FAIL
4543 || check_for_string_arg(argvars, 1) == FAIL))
4544 return;
4545
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004546 varname = tv_get_string_chk(&argvars[1]);
4547 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
LemonBoy47d4e312022-05-04 18:12:55 +01004548 if (tp != NULL)
4549 win = tp == curtab || tp->tp_firstwin == NULL ? firstwin
4550 : tp->tp_firstwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004551
LemonBoy47d4e312022-05-04 18:12:55 +01004552 get_var_from(varname, rettv, &argvars[2], 't', tp, win, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004553}
4554
4555/*
4556 * "gettabwinvar()" function
4557 */
4558 void
4559f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4560{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004561 if (in_vim9script()
4562 && (check_for_number_arg(argvars, 0) == FAIL
4563 || check_for_number_arg(argvars, 1) == FAIL
4564 || check_for_string_arg(argvars, 2) == FAIL))
4565 return;
4566
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004567 getwinvar(argvars, rettv, 1);
4568}
4569
4570/*
4571 * "getwinvar()" function
4572 */
4573 void
4574f_getwinvar(typval_T *argvars, typval_T *rettv)
4575{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004576 if (in_vim9script()
4577 && (check_for_number_arg(argvars, 0) == FAIL
4578 || check_for_string_arg(argvars, 1) == FAIL))
4579 return;
4580
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004581 getwinvar(argvars, rettv, 0);
4582}
4583
4584/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004585 * "getbufvar()" function
4586 */
4587 void
4588f_getbufvar(typval_T *argvars, typval_T *rettv)
4589{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004590 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004591 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004592
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004593 if (in_vim9script()
4594 && (check_for_buffer_arg(argvars, 0) == FAIL
4595 || check_for_string_arg(argvars, 1) == FAIL))
4596 return;
4597
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004598 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004599 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004600
LemonBoy47d4e312022-05-04 18:12:55 +01004601 get_var_from(varname, rettv, &argvars[2], 'b', curtab, curwin, buf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004602}
4603
4604/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004605 * "settabvar()" function
4606 */
4607 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004608f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004609{
4610 tabpage_T *save_curtab;
4611 tabpage_T *tp;
4612 char_u *varname, *tabvarname;
4613 typval_T *varp;
4614
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004615 if (check_secure())
4616 return;
4617
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004618 if (in_vim9script()
4619 && (check_for_number_arg(argvars, 0) == FAIL
4620 || check_for_string_arg(argvars, 1) == FAIL))
4621 return;
4622
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004623 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4624 varname = tv_get_string_chk(&argvars[1]);
4625 varp = &argvars[2];
4626
4627 if (varname != NULL && varp != NULL && tp != NULL)
4628 {
4629 save_curtab = curtab;
4630 goto_tabpage_tp(tp, FALSE, FALSE);
4631
4632 tabvarname = alloc(STRLEN(varname) + 3);
4633 if (tabvarname != NULL)
4634 {
4635 STRCPY(tabvarname, "t:");
4636 STRCPY(tabvarname + 2, varname);
4637 set_var(tabvarname, varp, TRUE);
4638 vim_free(tabvarname);
4639 }
4640
4641 // Restore current tabpage
4642 if (valid_tabpage(save_curtab))
4643 goto_tabpage_tp(save_curtab, FALSE, FALSE);
4644 }
4645}
4646
4647/*
4648 * "settabwinvar()" function
4649 */
4650 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004651f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004652{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004653 if (in_vim9script()
4654 && (check_for_number_arg(argvars, 0) == FAIL
4655 || check_for_number_arg(argvars, 1) == FAIL
4656 || check_for_string_arg(argvars, 2) == FAIL))
4657 return;
4658
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004659 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004660}
4661
4662/*
4663 * "setwinvar()" function
4664 */
4665 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004666f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004667{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004668 if (in_vim9script()
4669 && (check_for_number_arg(argvars, 0) == FAIL
4670 || check_for_string_arg(argvars, 1) == FAIL))
4671 return;
4672
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004673 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004674}
4675
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004676/*
4677 * "setbufvar()" function
4678 */
4679 void
4680f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4681{
4682 buf_T *buf;
4683 char_u *varname, *bufvarname;
4684 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004685
4686 if (check_secure())
4687 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004688
4689 if (in_vim9script()
4690 && (check_for_buffer_arg(argvars, 0) == FAIL
4691 || check_for_string_arg(argvars, 1) == FAIL))
4692 return;
4693
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004694 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004695 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004696 varp = &argvars[2];
4697
4698 if (buf != NULL && varname != NULL && varp != NULL)
4699 {
4700 if (*varname == '&')
4701 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004702 aco_save_T aco;
4703
4704 // set curbuf to be our buf, temporarily
4705 aucmd_prepbuf(&aco, buf);
4706
Bram Moolenaar191929b2020-08-19 21:20:49 +02004707 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004708
4709 // reset notion of buffer
4710 aucmd_restbuf(&aco);
4711 }
4712 else
4713 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004714 bufvarname = alloc(STRLEN(varname) + 3);
4715 if (bufvarname != NULL)
4716 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004717 buf_T *save_curbuf = curbuf;
4718
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004719 curbuf = buf;
4720 STRCPY(bufvarname, "b:");
4721 STRCPY(bufvarname + 2, varname);
4722 set_var(bufvarname, varp, TRUE);
4723 vim_free(bufvarname);
4724 curbuf = save_curbuf;
4725 }
4726 }
4727 }
4728}
4729
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004730/*
4731 * Get a callback from "arg". It can be a Funcref or a function name.
4732 * When "arg" is zero return an empty string.
4733 * "cb_name" is not allocated.
4734 * "cb_name" is set to NULL for an invalid argument.
4735 */
4736 callback_T
4737get_callback(typval_T *arg)
4738{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004739 callback_T res;
4740 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004741
4742 res.cb_free_name = FALSE;
4743 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4744 {
4745 res.cb_partial = arg->vval.v_partial;
4746 ++res.cb_partial->pt_refcount;
4747 res.cb_name = partial_name(res.cb_partial);
4748 }
4749 else
4750 {
4751 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004752 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4753 && isdigit(*arg->vval.v_string))
4754 r = FAIL;
4755 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004756 {
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004757 if (arg->v_type == VAR_STRING)
4758 {
4759 char_u *name;
4760
4761 name = get_scriptlocal_funcname(arg->vval.v_string);
4762 if (name != NULL)
4763 {
4764 vim_free(arg->vval.v_string);
4765 arg->vval.v_string = name;
4766 }
4767 }
4768
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004769 res.cb_name = arg->vval.v_string;
4770 func_ref(res.cb_name);
4771 }
4772 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004773 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004774 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004775 r = FAIL;
4776
4777 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004778 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004779 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004780 res.cb_name = NULL;
4781 }
4782 }
4783 return res;
4784}
4785
4786/*
4787 * Copy a callback into a typval_T.
4788 */
4789 void
4790put_callback(callback_T *cb, typval_T *tv)
4791{
4792 if (cb->cb_partial != NULL)
4793 {
4794 tv->v_type = VAR_PARTIAL;
4795 tv->vval.v_partial = cb->cb_partial;
4796 ++tv->vval.v_partial->pt_refcount;
4797 }
4798 else
4799 {
4800 tv->v_type = VAR_FUNC;
4801 tv->vval.v_string = vim_strsave(cb->cb_name);
4802 func_ref(cb->cb_name);
4803 }
4804}
4805
4806/*
4807 * Make a copy of "src" into "dest", allocating the function name if needed,
4808 * without incrementing the refcount.
4809 */
4810 void
4811set_callback(callback_T *dest, callback_T *src)
4812{
4813 if (src->cb_partial == NULL)
4814 {
4815 // just a function name, make a copy
4816 dest->cb_name = vim_strsave(src->cb_name);
4817 dest->cb_free_name = TRUE;
4818 }
4819 else
4820 {
4821 // cb_name is a pointer into cb_partial
4822 dest->cb_name = src->cb_name;
4823 dest->cb_free_name = FALSE;
4824 }
4825 dest->cb_partial = src->cb_partial;
4826}
4827
4828/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004829 * Copy callback from "src" to "dest", incrementing the refcounts.
4830 */
4831 void
4832copy_callback(callback_T *dest, callback_T *src)
4833{
4834 dest->cb_partial = src->cb_partial;
4835 if (dest->cb_partial != NULL)
4836 {
4837 dest->cb_name = src->cb_name;
4838 dest->cb_free_name = FALSE;
4839 ++dest->cb_partial->pt_refcount;
4840 }
4841 else
4842 {
4843 dest->cb_name = vim_strsave(src->cb_name);
4844 dest->cb_free_name = TRUE;
4845 func_ref(src->cb_name);
4846 }
4847}
4848
4849/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004850 * When a callback refers to an autoload import, change the function name to
4851 * the "path#name" form. Uses the current script context.
4852 * Only works when the name is allocated.
4853 */
4854 void
4855expand_autload_callback(callback_T *cb)
4856{
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004857 char_u *name;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004858 char_u *p;
4859 imported_T *import;
4860
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004861 if (!in_vim9script() || cb->cb_name == NULL
4862 || (!cb->cb_free_name
4863 && (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004864 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004865 if (cb->cb_partial != NULL)
4866 name = cb->cb_partial->pt_name;
4867 else
4868 name = cb->cb_name;
4869 p = vim_strchr(name, '.');
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004870 if (p == NULL)
4871 return;
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004872 import = find_imported(name, p - name, FALSE);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004873 if (import != NULL && SCRIPT_ID_VALID(import->imp_sid))
4874 {
4875 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
4876
4877 if (si->sn_autoload_prefix != NULL)
4878 {
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004879 char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004880
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004881 if (newname != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004882 {
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004883 if (cb->cb_partial != NULL)
4884 {
4885 if (cb->cb_name == cb->cb_partial->pt_name)
4886 cb->cb_name = newname;
4887 vim_free(cb->cb_partial->pt_name);
4888 cb->cb_partial->pt_name = newname;
4889 }
4890 else
4891 {
4892 vim_free(cb->cb_name);
4893 cb->cb_name = newname;
4894 }
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004895 }
4896 }
4897 }
4898}
4899
4900/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004901 * Unref/free "callback" returned by get_callback() or set_callback().
4902 */
4903 void
4904free_callback(callback_T *callback)
4905{
4906 if (callback->cb_partial != NULL)
4907 {
4908 partial_unref(callback->cb_partial);
4909 callback->cb_partial = NULL;
4910 }
4911 else if (callback->cb_name != NULL)
4912 func_unref(callback->cb_name);
4913 if (callback->cb_free_name)
4914 {
4915 vim_free(callback->cb_name);
4916 callback->cb_free_name = FALSE;
4917 }
4918 callback->cb_name = NULL;
4919}
4920
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004921#endif // FEAT_EVAL