blob: 71cab0a3085f1b9bc1cec48e7f3669869d73238a [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},
Shougo Matsushita61021aa2022-07-27 14:40:00 +0100114 {VV_NAME("completed_item", VAR_DICT), &t_dict_string, 0},
Bram Moolenaard787e402021-12-24 21:36:12 +0000115 {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 "{".
Bram Moolenaar70c41242022-05-10 18:11:43 +0100608 * When "evaluate" is FALSE only skip over the expression.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100609 * Return a pointer to the character after "}", NULL for an error.
610 */
611 char_u *
Bram Moolenaar70c41242022-05-10 18:11:43 +0100612eval_one_expr_in_str(char_u *p, garray_T *gap, int evaluate)
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100613{
614 char_u *block_start = skipwhite(p + 1); // skip the opening {
615 char_u *block_end = block_start;
616 char_u *expr_val;
617
618 if (*block_start == NUL)
619 {
620 semsg(_(e_missing_close_curly_str), p);
621 return NULL;
622 }
623 if (skip_expr(&block_end, NULL) == FAIL)
624 return NULL;
625 block_end = skipwhite(block_end);
626 if (*block_end != '}')
627 {
628 semsg(_(e_missing_close_curly_str), p);
629 return NULL;
630 }
Bram Moolenaar70c41242022-05-10 18:11:43 +0100631 if (evaluate)
632 {
633 *block_end = NUL;
634 expr_val = eval_to_string(block_start, TRUE);
635 *block_end = '}';
636 if (expr_val == NULL)
637 return NULL;
638 ga_concat(gap, expr_val);
639 vim_free(expr_val);
640 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100641
642 return block_end + 1;
643}
644
645/*
646 * Evaluate all the Vim expressions {expr} in "str" and return the resulting
647 * string in allocated memory. "{{" is reduced to "{" and "}}" to "}".
648 * Used for a heredoc assignment.
649 * Returns NULL for an error.
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100650 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +0100651 static char_u *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100652eval_all_expr_in_str(char_u *str)
653{
654 garray_T ga;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100655 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100656
657 ga_init2(&ga, 1, 80);
658 p = str;
659
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100660 while (*p != NUL)
661 {
LemonBoy2eaef102022-05-06 13:14:50 +0100662 char_u *lit_start;
LemonBoy2eaef102022-05-06 13:14:50 +0100663 int escaped_brace = FALSE;
664
665 // Look for a block start.
666 lit_start = p;
667 while (*p != '{' && *p != '}' && *p != NUL)
668 ++p;
669
670 if (*p != NUL && *p == p[1])
671 {
672 // Escaped brace, unescape and continue.
673 // Include the brace in the literal string.
674 ++p;
675 escaped_brace = TRUE;
676 }
677 else if (*p == '}')
678 {
679 semsg(_(e_stray_closing_curly_str), str);
680 ga_clear(&ga);
681 return NULL;
682 }
683
684 // Append the literal part.
685 ga_concat_len(&ga, lit_start, (size_t)(p - lit_start));
686
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100687 if (*p == NUL)
LemonBoy2eaef102022-05-06 13:14:50 +0100688 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100689
LemonBoy2eaef102022-05-06 13:14:50 +0100690 if (escaped_brace)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100691 {
LemonBoy2eaef102022-05-06 13:14:50 +0100692 // Skip the second brace.
693 ++p;
694 continue;
695 }
696
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100697 // Evaluate the expression and append the result.
Bram Moolenaar70c41242022-05-10 18:11:43 +0100698 p = eval_one_expr_in_str(p, &ga, TRUE);
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100699 if (p == NULL)
LemonBoy2eaef102022-05-06 13:14:50 +0100700 {
701 ga_clear(&ga);
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100702 return NULL;
703 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100704 }
705 ga_append(&ga, NUL);
706
707 return ga.ga_data;
708}
709
710/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200711 * Get a list of lines from a HERE document. The here document is a list of
712 * lines surrounded by a marker.
713 * cmd << {marker}
714 * {line1}
715 * {line2}
716 * ....
717 * {marker}
718 *
719 * The {marker} is a string. If the optional 'trim' word is supplied before the
720 * marker, then the leading indentation before the lines (matching the
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100721 * indentation in the "cmd" line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200722 *
723 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100724 * tcl, mzscheme), "script_get" is set to TRUE. In this case, if the marker is
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200725 * missing, then '.' is accepted as a marker.
726 *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100727 * When compiling a heredoc assignment to a variable in a Vim9 def function,
728 * "vim9compile" is set to TRUE. In this case, instead of generating a list of
729 * string values from the heredoc, vim9 instructions are generated. On success
730 * the returned list will be empty.
731 *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100732 * Returns a List with {lines} or NULL on failure.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200733 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100734 list_T *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100735heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200736{
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100737 char_u *theline = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200738 char_u *marker;
739 list_T *l;
740 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100741 char_u *str;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200742 int marker_indent_len = 0;
743 int text_indent_len = 0;
744 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200745 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200746 int comment_char = in_vim9script() ? '#' : '"';
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100747 int evalstr = FALSE;
748 int eval_failed = FALSE;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100749 cctx_T *cctx = vim9compile ? eap->cookie : NULL;
750 int count = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200751
752 if (eap->getline == NULL)
753 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000754 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200755 return NULL;
756 }
757
758 // Check for the optional 'trim' word before the marker
759 cmd = skipwhite(cmd);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200760
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100761 while (TRUE)
762 {
763 if (STRNCMP(cmd, "trim", 4) == 0
764 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200765 {
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100766 cmd = skipwhite(cmd + 4);
767
768 // Trim the indentation from all the lines in the here document.
769 // The amount of indentation trimmed is the same as the indentation
770 // of the first line after the :let command line. To find the end
771 // marker the indent of the :let command line is trimmed.
772 p = *eap->cmdlinep;
773 while (VIM_ISWHITE(*p))
774 {
775 p++;
776 marker_indent_len++;
777 }
778 text_indent_len = -1;
779
780 continue;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200781 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100782 if (STRNCMP(cmd, "eval", 4) == 0
783 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
784 {
785 cmd = skipwhite(cmd + 4);
786 evalstr = TRUE;
787 continue;
788 }
789 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200790 }
791
792 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200793 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200794 {
795 marker = skipwhite(cmd);
796 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200797 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200798 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000799 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200800 return NULL;
801 }
802 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200803 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200804 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000805 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200806 return NULL;
807 }
808 }
809 else
810 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200811 // When getting lines for an embedded script, if the marker is missing,
812 // accept '.' as the marker.
813 if (script_get)
814 marker = dot;
815 else
816 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000817 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200818 return NULL;
819 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200820 }
821
822 l = list_alloc();
823 if (l == NULL)
824 return NULL;
825
826 for (;;)
827 {
828 int mi = 0;
829 int ti = 0;
830
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100831 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200832 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
833 if (theline == NULL)
834 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000835 semsg(_(e_missing_end_marker_str), marker);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200836 break;
837 }
838
839 // with "trim": skip the indent matching the :let line to find the
840 // marker
841 if (marker_indent_len > 0
842 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
843 mi = marker_indent_len;
844 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200845 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200846
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100847 // If expression evaluation failed in the heredoc, then skip till the
848 // end marker.
849 if (eval_failed)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100850 continue;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100851
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200852 if (text_indent_len == -1 && *theline != NUL)
853 {
854 // set the text indent from the first line.
855 p = theline;
856 text_indent_len = 0;
857 while (VIM_ISWHITE(*p))
858 {
859 p++;
860 text_indent_len++;
861 }
862 text_indent = vim_strnsave(theline, text_indent_len);
863 }
864 // with "trim": skip the indent matching the first line
865 if (text_indent != NULL)
866 for (ti = 0; ti < text_indent_len; ++ti)
867 if (theline[ti] != text_indent[ti])
868 break;
869
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100870 str = theline + ti;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100871 if (vim9compile)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100872 {
LemonBoy2eaef102022-05-06 13:14:50 +0100873 if (compile_all_expr_in_str(str, evalstr, cctx) == FAIL)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100874 {
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100875 vim_free(theline);
876 vim_free(text_indent);
877 return FAIL;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100878 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100879 count++;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100880 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100881 else
882 {
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100883 if (evalstr && !eap->skip)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100884 {
885 str = eval_all_expr_in_str(str);
886 if (str == NULL)
887 {
888 // expression evaluation failed
889 eval_failed = TRUE;
890 continue;
891 }
892 vim_free(theline);
893 theline = str;
894 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100895
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100896 if (list_append_string(l, str, -1) == FAIL)
897 break;
898 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200899 }
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100900 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200901 vim_free(text_indent);
902
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100903 if (vim9compile && cctx->ctx_skip != SKIP_YES && !eval_failed)
904 generate_NEWLIST(cctx, count, FALSE);
905
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100906 if (eval_failed)
907 {
908 // expression evaluation in the heredoc failed
909 list_free(l);
910 return NULL;
911 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200912 return l;
913}
914
915/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200916 * Vim9 variable declaration:
917 * ":var name"
918 * ":var name: type"
919 * ":var name = expr"
920 * ":var name: type = expr"
921 * etc.
922 */
923 void
924ex_var(exarg_T *eap)
925{
Bram Moolenaar330a3882022-03-05 11:05:57 +0000926 char_u *p = eap->cmd;
Bram Moolenaare1d12112022-03-05 11:37:48 +0000927 int has_var;
Bram Moolenaar330a3882022-03-05 11:05:57 +0000928
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200929 if (!in_vim9script())
930 {
931 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
932 return;
933 }
Bram Moolenaare1d12112022-03-05 11:37:48 +0000934 has_var = checkforcmd_noparen(&p, "var", 3);
935 if (current_sctx.sc_sid == 0 && has_var)
Bram Moolenaar0e1574c2022-03-03 17:05:35 +0000936 {
937 emsg(_(e_cannot_declare_variable_on_command_line));
938 return;
939 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200940 ex_let(eap);
941}
942
943/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200944 * ":let" list all variable values
945 * ":let var1 var2" list variable values
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 var /= expr" assignment command.
951 * ":let var %= expr" assignment command.
952 * ":let var .= expr" assignment command.
953 * ":let var ..= expr" assignment command.
954 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100956 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200957 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200958 * ":final var = expr" assignment command.
959 * ":final [var1, var2] = expr" unpack list.
960 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200961 * ":const" list all variable values
962 * ":const var1 var2" list variable values
963 * ":const var = expr" assignment command.
964 * ":const [var1, var2] = expr" unpack list.
965 */
966 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200967ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200968{
969 char_u *arg = eap->arg;
970 char_u *expr = NULL;
971 typval_T rettv;
972 int i;
973 int var_count = 0;
974 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200975 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200976 char_u *argend;
977 int first = TRUE;
978 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200979 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100980 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200981 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200982
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200983 if (eap->cmdidx == CMD_final && !vim9script)
984 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100985 // In legacy Vim script ":final" is short for ":finally".
986 ex_finally(eap);
987 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200988 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200989 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200990 {
991 emsg(_(e_cannot_use_let_in_vim9_script));
992 return;
993 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200994
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100995 if (eap->cmdidx == CMD_const)
996 flags |= ASSIGN_CONST;
997 else if (eap->cmdidx == CMD_final)
998 flags |= ASSIGN_FINAL;
999
1000 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001001 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001002 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001003
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001004 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001005 if (argend == NULL)
1006 return;
1007 if (argend > arg && argend[-1] == '.') // for var.='str'
1008 --argend;
1009 expr = skipwhite(argend);
1010 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02001011 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001012 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +02001013 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
1014 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +02001015 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001016 {
1017 // ":let" without "=": list variables
1018 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001019 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001020 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001021 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001022 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001023 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001024 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001025 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001026 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001027 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001028 else
1029 // Vim9 declaration ":var name: type"
1030 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001031 }
1032 else
1033 {
1034 // ":let var1 var2" - list values
1035 arg = list_arg_vars(eap, arg, &first);
1036 }
1037 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001038 else if (!eap->skip)
1039 {
1040 // ":let"
1041 list_glob_vars(&first);
1042 list_buf_vars(&first);
1043 list_win_vars(&first);
1044 list_tab_vars(&first);
1045 list_script_vars(&first);
1046 list_func_vars(&first);
1047 list_vim_vars(&first);
1048 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02001049 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001050 }
1051 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
1052 {
1053 list_T *l;
Bram Moolenaar81530e32021-07-28 21:25:49 +02001054 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001055
1056 // HERE document
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001057 l = heredoc_get(eap, expr + 3, FALSE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001058 if (l != NULL)
1059 {
1060 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001061 if (!eap->skip)
1062 {
Bram Moolenaar81530e32021-07-28 21:25:49 +02001063 // errors are for the assignment, not the end marker
1064 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001065 op[0] = '=';
1066 op[1] = NUL;
1067 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001069 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001070 clear_tv(&rettv);
1071 }
1072 }
1073 else
1074 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001075 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001076 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001077
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02001078 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001079 i = FAIL;
1080 if (has_assign || concat)
1081 {
Bram Moolenaar9a562c12021-01-23 13:39:14 +01001082 int cur_lnum;
1083
Bram Moolenaar32e35112020-05-14 22:41:15 +02001084 op[0] = '=';
1085 op[1] = NUL;
1086 if (*expr != '=')
1087 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001088 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +02001089 {
1090 // +=, /=, etc. require an existing variable
1091 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
1092 i = FAIL;
1093 }
1094 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +02001095 {
1096 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001097 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001098 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001099 {
Bram Moolenaar32e35112020-05-14 22:41:15 +02001100 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001101 ++len;
1102 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02001103 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001104 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001105 }
1106 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001107 ++expr;
1108
Bram Moolenaar7f2c3412021-11-29 16:01:49 +00001109 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
Bram Moolenaarc7e44a72020-07-29 21:37:43 +02001110 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001111 {
1112 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01001113 semsg(_(e_white_space_required_before_and_after_str_at_str),
1114 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001115 i = FAIL;
1116 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02001117
1118 if (eap->skip)
1119 ++emsg_skip;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001120 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaarc7e44a72020-07-29 21:37:43 +02001121 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaar9a562c12021-01-23 13:39:14 +01001122 cur_lnum = SOURCING_LNUM;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001123 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001124 if (eap->skip)
1125 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001126 clear_evalarg(&evalarg, eap);
Bram Moolenaar9a562c12021-01-23 13:39:14 +01001127
1128 // Restore the line number so that any type error is given for the
1129 // declaration, not the expression.
1130 SOURCING_LNUM = cur_lnum;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001131 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001132 if (eap->skip)
1133 {
1134 if (i != FAIL)
1135 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001136 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001137 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001138 {
1139 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001140 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001141 clear_tv(&rettv);
1142 }
1143 }
1144}
1145
1146/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001147 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001148 * Handles both "var" with any type and "[var, var; var]" with a list type.
1149 * When "op" is not NULL it points to a string with characters that
1150 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1151 * or concatenate.
1152 * Returns OK or FAIL;
1153 */
1154 int
1155ex_let_vars(
1156 char_u *arg_start,
1157 typval_T *tv,
1158 int copy, // copy values from "tv", don't move
1159 int semicolon, // from skip_var_list()
1160 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001161 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001162 char_u *op)
1163{
1164 char_u *arg = arg_start;
1165 list_T *l;
1166 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001167 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001168 listitem_T *item;
1169 typval_T ltv;
1170
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00001171 if (tv->v_type == VAR_VOID)
1172 {
1173 emsg(_(e_cannot_use_void_value));
1174 return FAIL;
1175 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001176 if (*arg != '[')
1177 {
1178 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001179 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001180 return FAIL;
1181 return OK;
1182 }
1183
1184 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1185 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
1186 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001187 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001188 return FAIL;
1189 }
1190
1191 i = list_len(l);
1192 if (semicolon == 0 && var_count < i)
1193 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001194 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001195 return FAIL;
1196 }
1197 if (var_count - semicolon > i)
1198 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001199 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001200 return FAIL;
1201 }
1202
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001203 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001204 item = l->lv_first;
1205 while (*arg != ']')
1206 {
1207 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001208 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001209 arg = ex_let_one(arg, &item->li_tv, TRUE,
1210 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001211 item = item->li_next;
1212 if (arg == NULL)
1213 return FAIL;
1214
1215 arg = skipwhite(arg);
1216 if (*arg == ';')
1217 {
1218 // Put the rest of the list (may be empty) in the var after ';'.
1219 // Create a new list for this.
1220 l = list_alloc();
1221 if (l == NULL)
1222 return FAIL;
1223 while (item != NULL)
1224 {
1225 list_append_tv(l, &item->li_tv);
1226 item = item->li_next;
1227 }
1228
1229 ltv.v_type = VAR_LIST;
1230 ltv.v_lock = 0;
1231 ltv.vval.v_list = l;
1232 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001233 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001234
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001235 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1236 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001237 clear_tv(&ltv);
1238 if (arg == NULL)
1239 return FAIL;
1240 break;
1241 }
1242 else if (*arg != ',' && *arg != ']')
1243 {
1244 internal_error("ex_let_vars()");
1245 return FAIL;
1246 }
1247 }
1248
1249 return OK;
1250}
1251
1252/*
1253 * Skip over assignable variable "var" or list of variables "[var, var]".
1254 * Used for ":let varvar = expr" and ":for varvar in expr".
1255 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001256 * for "[var, var; var]" set "semicolon" to 1.
1257 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001258 * Return NULL for an error.
1259 */
1260 char_u *
1261skip_var_list(
1262 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001264 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001265 int *semicolon,
1266 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001267{
1268 char_u *p, *s;
1269
1270 if (*arg == '[')
1271 {
1272 // "[var, var]": find the matching ']'.
1273 p = arg;
1274 for (;;)
1275 {
1276 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001277 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001278 if (s == p)
1279 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001280 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001281 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001282 return NULL;
1283 }
1284 ++*var_count;
1285
1286 p = skipwhite(s);
1287 if (*p == ']')
1288 break;
1289 else if (*p == ';')
1290 {
1291 if (*semicolon == 1)
1292 {
Bram Moolenaar8b716f52022-02-15 21:17:56 +00001293 if (!silent)
1294 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001295 return NULL;
1296 }
1297 *semicolon = 1;
1298 }
1299 else if (*p != ',')
1300 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001301 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001302 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001303 return NULL;
1304 }
1305 }
1306 return p + 1;
1307 }
1308 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001310}
1311
1312/*
1313 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1314 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001316 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001317 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001319{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001320 char_u *end;
1321 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001323 if (*arg == '@' && arg[1] != NUL)
1324 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001325 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001326 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001327
1328 // "a: type" is declaring variable "a" with a type, not "a:".
1329 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001330 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001331 --end;
1332
Bram Moolenaar585587d2021-01-17 20:52:13 +01001333 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001334 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001335 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001336 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 }
1338 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001339}
1340
1341/*
1342 * List variables for hashtab "ht" with prefix "prefix".
1343 * If "empty" is TRUE also list NULL strings as empty strings.
1344 */
1345 void
1346list_hashtable_vars(
1347 hashtab_T *ht,
1348 char *prefix,
1349 int empty,
1350 int *first)
1351{
1352 hashitem_T *hi;
1353 dictitem_T *di;
1354 int todo;
1355 char_u buf[IOSIZE];
1356
1357 todo = (int)ht->ht_used;
1358 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1359 {
1360 if (!HASHITEM_EMPTY(hi))
1361 {
1362 --todo;
1363 di = HI2DI(hi);
1364
1365 // apply :filter /pat/ to variable name
1366 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1367 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1368 if (message_filtered(buf))
1369 continue;
1370
1371 if (empty || di->di_tv.v_type != VAR_STRING
1372 || di->di_tv.vval.v_string != NULL)
1373 list_one_var(di, prefix, first);
1374 }
1375 }
1376}
1377
1378/*
1379 * List global variables.
1380 */
1381 static void
1382list_glob_vars(int *first)
1383{
1384 list_hashtable_vars(&globvarht, "", TRUE, first);
1385}
1386
1387/*
1388 * List buffer variables.
1389 */
1390 static void
1391list_buf_vars(int *first)
1392{
1393 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1394}
1395
1396/*
1397 * List window variables.
1398 */
1399 static void
1400list_win_vars(int *first)
1401{
1402 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1403}
1404
1405/*
1406 * List tab page variables.
1407 */
1408 static void
1409list_tab_vars(int *first)
1410{
1411 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1412}
1413
1414/*
1415 * List variables in "arg".
1416 */
1417 static char_u *
1418list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1419{
1420 int error = FALSE;
1421 int len;
1422 char_u *name;
1423 char_u *name_start;
1424 char_u *arg_subsc;
1425 char_u *tofree;
1426 typval_T tv;
1427
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001428 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001429 {
1430 if (error || eap->skip)
1431 {
1432 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1433 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1434 {
1435 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001436 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001437 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001438 break;
1439 }
1440 }
1441 else
1442 {
1443 // get_name_len() takes care of expanding curly braces
1444 name_start = name = arg;
1445 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1446 if (len <= 0)
1447 {
1448 // This is mainly to keep test 49 working: when expanding
1449 // curly braces fails overrule the exception error message.
1450 if (len < 0 && !aborting())
1451 {
1452 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001453 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001454 break;
1455 }
1456 error = TRUE;
1457 }
1458 else
1459 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001460 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001461 if (tofree != NULL)
1462 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001463 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001464 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001465 error = TRUE;
1466 else
1467 {
1468 // handle d.key, l[idx], f(expr)
1469 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001470 if (handle_subscript(&arg, name_start, &tv,
1471 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001472 error = TRUE;
1473 else
1474 {
1475 if (arg == arg_subsc && len == 2 && name[1] == ':')
1476 {
1477 switch (*name)
1478 {
1479 case 'g': list_glob_vars(first); break;
1480 case 'b': list_buf_vars(first); break;
1481 case 'w': list_win_vars(first); break;
1482 case 't': list_tab_vars(first); break;
1483 case 'v': list_vim_vars(first); break;
1484 case 's': list_script_vars(first); break;
1485 case 'l': list_func_vars(first); break;
1486 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001487 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001488 }
1489 }
1490 else
1491 {
1492 char_u numbuf[NUMBUFLEN];
1493 char_u *tf;
1494 int c;
1495 char_u *s;
1496
1497 s = echo_string(&tv, &tf, numbuf, 0);
1498 c = *arg;
1499 *arg = NUL;
1500 list_one_var_a("",
1501 arg == arg_subsc ? name : name_start,
1502 tv.v_type,
1503 s == NULL ? (char_u *)"" : s,
1504 first);
1505 *arg = c;
1506 vim_free(tf);
1507 }
1508 clear_tv(&tv);
1509 }
1510 }
1511 }
1512
1513 vim_free(tofree);
1514 }
1515
1516 arg = skipwhite(arg);
1517 }
1518
1519 return arg;
1520}
1521
1522/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001523 * Set an environment variable, part of ex_let_one().
1524 */
1525 static char_u *
1526ex_let_env(
1527 char_u *arg,
1528 typval_T *tv,
1529 int flags,
1530 char_u *endchars,
1531 char_u *op)
1532{
1533 char_u *arg_end = NULL;
1534 char_u *name;
1535 int len;
1536
1537 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1538 && (flags & ASSIGN_FOR_LOOP) == 0)
1539 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001540 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001541 return NULL;
1542 }
1543
1544 // Find the end of the name.
1545 ++arg;
1546 name = arg;
1547 len = get_env_len(&arg);
1548 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001549 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001550 else
1551 {
1552 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001553 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001554 else if (endchars != NULL
1555 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1556 emsg(_(e_unexpected_characters_in_let));
1557 else if (!check_secure())
1558 {
1559 char_u *tofree = NULL;
1560 int c1 = name[len];
1561 char_u *p;
1562
1563 name[len] = NUL;
1564 p = tv_get_string_chk(tv);
1565 if (p != NULL && op != NULL && *op == '.')
1566 {
1567 int mustfree = FALSE;
1568 char_u *s = vim_getenv(name, &mustfree);
1569
1570 if (s != NULL)
1571 {
1572 p = tofree = concat_str(s, p);
1573 if (mustfree)
1574 vim_free(s);
1575 }
1576 }
1577 if (p != NULL)
1578 {
1579 vim_setenv_ext(name, p);
1580 arg_end = arg;
1581 }
1582 name[len] = c1;
1583 vim_free(tofree);
1584 }
1585 }
1586 return arg_end;
1587}
1588
1589/*
1590 * Set an option, part of ex_let_one().
1591 */
1592 static char_u *
1593ex_let_option(
1594 char_u *arg,
1595 typval_T *tv,
1596 int flags,
1597 char_u *endchars,
1598 char_u *op)
1599{
1600 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001601 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001602 char_u *arg_end = NULL;
1603
1604 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1605 && (flags & ASSIGN_FOR_LOOP) == 0)
1606 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001607 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001608 return NULL;
1609 }
1610
1611 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001612 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001613 if (p == NULL || (endchars != NULL
1614 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1615 emsg(_(e_unexpected_characters_in_let));
1616 else
1617 {
1618 int c1;
1619 long n = 0;
1620 getoption_T opt_type;
1621 long numval;
1622 char_u *stringval = NULL;
1623 char_u *s = NULL;
1624 int failed = FALSE;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001625 int opt_p_flags;
1626 char_u *tofree = NULL;
Bram Moolenaar92c33eb2021-12-07 11:03:39 +00001627 char_u numbuf[NUMBUFLEN];
1628
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001629 c1 = *p;
1630 *p = NUL;
1631
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001632 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags,
1633 scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001634 if ((opt_type == gov_bool
1635 || opt_type == gov_number
1636 || opt_type == gov_hidden_bool
1637 || opt_type == gov_hidden_number)
1638 && (tv->v_type != VAR_STRING || !in_vim9script()))
1639 {
1640 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1641 // bool, possibly hidden
1642 n = (long)tv_get_bool(tv);
1643 else
1644 // number, possibly hidden
1645 n = (long)tv_get_number(tv);
1646 }
1647
Bram Moolenaaref082e12021-12-12 21:02:03 +00001648 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001649 || tv->v_type == VAR_FUNC))
1650 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001651 // If the option can be set to a function reference or a lambda
1652 // and the passed value is a function reference, then convert it to
1653 // the name (string) of the function reference.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001654 s = tv2string(tv, &tofree, numbuf, 0);
1655 }
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001656 // Avoid setting a string option to the text "v:false" or similar.
1657 // In Vim9 script also don't convert a number to string.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001658 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001659 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1660 s = tv_get_string_chk(tv);
1661
1662 if (op != NULL && *op != '=')
1663 {
1664 if (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1665 || (opt_type == gov_string && *op != '.'))
1666 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001667 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001668 failed = TRUE; // don't set the value
1669
1670 }
1671 else
1672 {
1673 // number, in legacy script also bool
1674 if (opt_type == gov_number
1675 || (opt_type == gov_bool && !in_vim9script()))
1676 {
1677 switch (*op)
1678 {
1679 case '+': n = numval + n; break;
1680 case '-': n = numval - n; break;
1681 case '*': n = numval * n; break;
1682 case '/': n = (long)num_divide(numval, n,
1683 &failed); break;
1684 case '%': n = (long)num_modulus(numval, n,
1685 &failed); break;
1686 }
1687 s = NULL;
1688 }
1689 else if (opt_type == gov_string
1690 && stringval != NULL && s != NULL)
1691 {
1692 // string
1693 s = concat_str(stringval, s);
1694 vim_free(stringval);
1695 stringval = s;
1696 }
1697 }
1698 }
1699
1700 if (!failed)
1701 {
1702 if (opt_type != gov_string || s != NULL)
1703 {
Bram Moolenaar8ccbbeb2022-03-02 19:49:38 +00001704 char *err = set_option_value(arg, n, s, scope);
1705
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001706 arg_end = p;
Bram Moolenaar8ccbbeb2022-03-02 19:49:38 +00001707 if (err != NULL)
1708 emsg(_(err));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001709 }
1710 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001711 emsg(_(e_string_required));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001712 }
1713 *p = c1;
1714 vim_free(stringval);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001715 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001716 }
1717 return arg_end;
1718}
1719
1720/*
1721 * Set a register, part of ex_let_one().
1722 */
1723 static char_u *
1724ex_let_register(
1725 char_u *arg,
1726 typval_T *tv,
1727 int flags,
1728 char_u *endchars,
1729 char_u *op)
1730{
1731 char_u *arg_end = NULL;
1732
1733 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1734 && (flags & ASSIGN_FOR_LOOP) == 0)
1735 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001736 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001737 return NULL;
1738 }
1739 ++arg;
1740 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001741 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001742 else if (endchars != NULL
1743 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1744 emsg(_(e_unexpected_characters_in_let));
1745 else
1746 {
1747 char_u *ptofree = NULL;
1748 char_u *p;
1749
1750 p = tv_get_string_chk(tv);
1751 if (p != NULL && op != NULL && *op == '.')
1752 {
1753 char_u *s = get_reg_contents(*arg == '@'
1754 ? '"' : *arg, GREG_EXPR_SRC);
1755
1756 if (s != NULL)
1757 {
1758 p = ptofree = concat_str(s, p);
1759 vim_free(s);
1760 }
1761 }
1762 if (p != NULL)
1763 {
1764 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1765 arg_end = arg + 1;
1766 }
1767 vim_free(ptofree);
1768 }
1769 return arg_end;
1770}
1771
1772/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001773 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1774 * Returns a pointer to the char just after the var name.
1775 * Returns NULL if there is an error.
1776 */
1777 static char_u *
1778ex_let_one(
1779 char_u *arg, // points to variable name
1780 typval_T *tv, // value to assign to variable
1781 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001782 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001783 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001784 char_u *op, // "+", "-", "." or NULL
1785 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001786{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001787 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001788
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001789 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001790 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001791 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1792 {
1793 vim9_declare_error(arg);
1794 return NULL;
1795 }
1796
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001797 if (*arg == '$')
1798 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001799 // ":let $VAR = expr": Set environment variable.
1800 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001801 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001802 else if (*arg == '&')
1803 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001804 // ":let &option = expr": Set option value.
1805 // ":let &l:option = expr": Set local option value.
1806 // ":let &g:option = expr": Set global option value.
1807 // ":for &ts in range(8)": Set option value for for loop
1808 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001809 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001810 else if (*arg == '@')
1811 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001812 // ":let @r = expr": Set register contents.
1813 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001814 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001815 else if (eval_isnamec1(*arg) || *arg == '{')
1816 {
1817 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001818 char_u *p;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001819 int lval_flags = (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1820 ? GLV_NO_DECL : 0;
1821 if (op != NULL && *op != '=')
1822 lval_flags |= GLV_ASSIGN_WITH_OP;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001823
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001824 // ":let var = expr": Set internal variable.
1825 // ":let var: type = expr": Set internal variable with type.
1826 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001827 p = get_lval(arg, tv, &lv, FALSE, FALSE, lval_flags, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001828 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001829 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001830 if (endchars != NULL && vim_strchr(endchars,
1831 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001832 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001833 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001834 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001835 else
1836 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001837 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001838 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001839 }
1840 }
1841 clear_lval(&lv);
1842 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001843 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001844 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001845
1846 return arg_end;
1847}
1848
1849/*
1850 * ":unlet[!] var1 ... " command.
1851 */
1852 void
1853ex_unlet(exarg_T *eap)
1854{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001855 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001856}
1857
1858/*
1859 * ":lockvar" and ":unlockvar" commands
1860 */
1861 void
1862ex_lockvar(exarg_T *eap)
1863{
1864 char_u *arg = eap->arg;
1865 int deep = 2;
1866
1867 if (eap->forceit)
1868 deep = -1;
1869 else if (vim_isdigit(*arg))
1870 {
1871 deep = getdigits(&arg);
1872 arg = skipwhite(arg);
1873 }
1874
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001875 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001876}
1877
1878/*
1879 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001880 * Also used for Vim9 script. "callback" is invoked as:
1881 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001882 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001883 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001884ex_unletlock(
1885 exarg_T *eap,
1886 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001887 int deep,
1888 int glv_flags,
1889 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1890 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001891{
1892 char_u *arg = argstart;
1893 char_u *name_end;
1894 int error = FALSE;
1895 lval_T lv;
1896
1897 do
1898 {
1899 if (*arg == '$')
1900 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001901 lv.ll_name = arg;
1902 lv.ll_tv = NULL;
1903 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001904 if (get_env_len(&arg) == 0)
1905 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001906 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001907 return;
1908 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001909 if (!error && !eap->skip
1910 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1911 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001912 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001913 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001914 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001915 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001916 // Parse the name and find the end.
1917 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001918 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001919 if (lv.ll_name == NULL)
1920 error = TRUE; // error but continue parsing
1921 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1922 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001923 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001924 if (name_end != NULL)
1925 {
1926 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00001927 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001928 }
1929 if (!(eap->skip || error))
1930 clear_lval(&lv);
1931 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001932 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001933
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001934 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001935 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001936 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001937
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001938 if (!eap->skip)
1939 clear_lval(&lv);
1940 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001941
1942 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001943 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001944
Bram Moolenaar63b91732021-08-05 20:40:03 +02001945 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001946}
1947
1948 static int
1949do_unlet_var(
1950 lval_T *lp,
1951 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001952 exarg_T *eap,
1953 int deep UNUSED,
1954 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001955{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001956 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001957 int ret = OK;
1958 int cc;
1959
1960 if (lp->ll_tv == NULL)
1961 {
1962 cc = *name_end;
1963 *name_end = NUL;
1964
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001965 // Environment variable, normal name or expanded name.
1966 if (*lp->ll_name == '$')
LemonBoy77142312022-04-15 20:50:46 +01001967 vim_unsetenv_ext(lp->ll_name + 1);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001968 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001969 ret = FAIL;
1970 *name_end = cc;
1971 }
1972 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001973 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001974 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001975 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001976 return FAIL;
1977 else if (lp->ll_range)
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00001978 list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_n1,
1979 !lp->ll_empty2, lp->ll_n2);
1980 else if (lp->ll_list != NULL)
1981 // unlet a List item.
1982 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001983 else
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00001984 // unlet a Dictionary item.
1985 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001986
1987 return ret;
1988}
1989
1990/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001991 * Unlet one item or a range of items from a list.
1992 * Return OK or FAIL.
1993 */
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00001994 void
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001995list_unlet_range(
1996 list_T *l,
1997 listitem_T *li_first,
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001998 long n1_arg,
1999 int has_n2,
2000 long n2)
2001{
2002 listitem_T *li = li_first;
2003 int n1 = n1_arg;
2004
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002005 // Delete a range of List items.
2006 li = li_first;
2007 n1 = n1_arg;
2008 while (li != NULL && (!has_n2 || n2 >= n1))
2009 {
2010 listitem_T *next = li->li_next;
2011
2012 listitem_remove(l, li);
2013 li = next;
2014 ++n1;
2015 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002016}
2017/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002018 * "unlet" a variable. Return OK if it existed, FAIL if not.
2019 * When "forceit" is TRUE don't complain if the variable doesn't exist.
2020 */
2021 int
2022do_unlet(char_u *name, int forceit)
2023{
2024 hashtab_T *ht;
2025 hashitem_T *hi;
mityu8be36ee2022-05-25 17:29:46 +01002026 char_u *varname = NULL; // init to shut up gcc
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002027 dict_T *d;
2028 dictitem_T *di;
2029
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002030 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002031 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002032 return FAIL;
2033
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002034 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002035
2036 // can't :unlet a script variable in Vim9 script from a function
2037 if (ht == get_script_local_ht()
2038 && SCRIPT_ID_VALID(current_sctx.sc_sid)
2039 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2040 == SCRIPT_VERSION_VIM9
2041 && check_vim9_unlet(name) == FAIL)
2042 return FAIL;
2043
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002044 if (ht != NULL && *varname != NUL)
2045 {
2046 d = get_current_funccal_dict(ht);
2047 if (d == NULL)
2048 {
2049 if (ht == &globvarht)
2050 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002051 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002052 d = &vimvardict;
2053 else
2054 {
2055 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2056 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2057 }
2058 if (d == NULL)
2059 {
2060 internal_error("do_unlet()");
2061 return FAIL;
2062 }
2063 }
2064 hi = hash_find(ht, varname);
2065 if (HASHITEM_EMPTY(hi))
2066 hi = find_hi_in_scoped_ht(name, &ht);
2067 if (hi != NULL && !HASHITEM_EMPTY(hi))
2068 {
2069 di = HI2DI(hi);
2070 if (var_check_fixed(di->di_flags, name, FALSE)
2071 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02002072 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002073 return FAIL;
2074
2075 delete_var(ht, hi);
2076 return OK;
2077 }
2078 }
2079 if (forceit)
2080 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00002081 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002082 return FAIL;
2083}
2084
2085/*
2086 * Lock or unlock variable indicated by "lp".
2087 * "deep" is the levels to go (-1 for unlimited);
2088 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2089 */
2090 static int
2091do_lock_var(
2092 lval_T *lp,
2093 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002094 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002095 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002096 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002097{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002098 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002099 int ret = OK;
2100 int cc;
2101 dictitem_T *di;
2102
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002103 if (lp->ll_tv == NULL)
2104 {
2105 cc = *name_end;
2106 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002107 if (*lp->ll_name == '$')
2108 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002109 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002110 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002111 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002112 else
2113 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002114 // Normal name or expanded name.
2115 di = find_var(lp->ll_name, NULL, TRUE);
2116 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002117 {
2118 if (in_vim9script())
2119 semsg(_(e_cannot_find_variable_to_unlock_str),
2120 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002121 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002122 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002123 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002124 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002125 if ((di->di_flags & DI_FLAGS_FIX)
2126 && di->di_tv.v_type != VAR_DICT
2127 && di->di_tv.v_type != VAR_LIST)
2128 {
2129 // For historic reasons this error is not given for a list
2130 // or dict. E.g., the b: dict could be locked/unlocked.
2131 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
2132 ret = FAIL;
2133 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002134 else
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002135 {
2136 if (in_vim9script())
2137 {
2138 svar_T *sv = find_typval_in_script(&di->di_tv,
2139 0, FALSE);
2140
2141 if (sv != NULL && sv->sv_const != 0)
2142 {
2143 semsg(_(e_cannot_change_readonly_variable_str),
2144 lp->ll_name);
2145 ret = FAIL;
2146 }
2147 }
2148
2149 if (ret == OK)
2150 {
2151 if (lock)
2152 di->di_flags |= DI_FLAGS_LOCK;
2153 else
2154 di->di_flags &= ~DI_FLAGS_LOCK;
2155 if (deep != 0)
2156 item_lock(&di->di_tv, deep, lock, FALSE);
2157 }
2158 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002159 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002160 }
2161 *name_end = cc;
2162 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002163 else if (deep == 0)
2164 {
2165 // nothing to do
2166 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002167 else if (lp->ll_range)
2168 {
2169 listitem_T *li = lp->ll_li;
2170
2171 // (un)lock a range of List items.
2172 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2173 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02002174 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002175 li = li->li_next;
2176 ++lp->ll_n1;
2177 }
2178 }
2179 else if (lp->ll_list != NULL)
2180 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002181 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002182 else
2183 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002184 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002185
2186 return ret;
2187}
2188
2189/*
2190 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002191 * When "check_refcount" is TRUE do not lock a list or dict with a reference
2192 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002193 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002194 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02002195item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002196{
2197 static int recurse = 0;
2198 list_T *l;
2199 listitem_T *li;
2200 dict_T *d;
2201 blob_T *b;
2202 hashitem_T *hi;
2203 int todo;
2204
2205 if (recurse >= DICT_MAXNEST)
2206 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002207 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002208 return;
2209 }
2210 if (deep == 0)
2211 return;
2212 ++recurse;
2213
2214 // lock/unlock the item itself
2215 if (lock)
2216 tv->v_lock |= VAR_LOCKED;
2217 else
2218 tv->v_lock &= ~VAR_LOCKED;
2219
2220 switch (tv->v_type)
2221 {
2222 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002223 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002225 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002226 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002227 case VAR_STRING:
2228 case VAR_FUNC:
2229 case VAR_PARTIAL:
2230 case VAR_FLOAT:
2231 case VAR_SPECIAL:
2232 case VAR_JOB:
2233 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002234 case VAR_INSTR:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002235 break;
2236
2237 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002238 if ((b = tv->vval.v_blob) != NULL
2239 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002240 {
2241 if (lock)
2242 b->bv_lock |= VAR_LOCKED;
2243 else
2244 b->bv_lock &= ~VAR_LOCKED;
2245 }
2246 break;
2247 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002248 if ((l = tv->vval.v_list) != NULL
2249 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002250 {
2251 if (lock)
2252 l->lv_lock |= VAR_LOCKED;
2253 else
2254 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002255 if (deep < 0 || deep > 1)
2256 {
2257 if (l->lv_first == &range_list_item)
2258 l->lv_lock |= VAR_ITEMS_LOCKED;
2259 else
2260 {
2261 // recursive: lock/unlock the items the List contains
2262 CHECK_LIST_MATERIALIZE(l);
2263 FOR_ALL_LIST_ITEMS(l, li) item_lock(&li->li_tv,
2264 deep - 1, lock, check_refcount);
2265 }
2266 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002267 }
2268 break;
2269 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002270 if ((d = tv->vval.v_dict) != NULL
2271 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002272 {
2273 if (lock)
2274 d->dv_lock |= VAR_LOCKED;
2275 else
2276 d->dv_lock &= ~VAR_LOCKED;
2277 if (deep < 0 || deep > 1)
2278 {
2279 // recursive: lock/unlock the items the List contains
2280 todo = (int)d->dv_hashtab.ht_used;
2281 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2282 {
2283 if (!HASHITEM_EMPTY(hi))
2284 {
2285 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002286 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2287 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002288 }
2289 }
2290 }
2291 }
2292 }
2293 --recurse;
2294}
2295
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002296#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2297/*
2298 * Delete all "menutrans_" variables.
2299 */
2300 void
2301del_menutrans_vars(void)
2302{
2303 hashitem_T *hi;
2304 int todo;
2305
2306 hash_lock(&globvarht);
2307 todo = (int)globvarht.ht_used;
2308 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2309 {
2310 if (!HASHITEM_EMPTY(hi))
2311 {
2312 --todo;
2313 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2314 delete_var(&globvarht, hi);
2315 }
2316 }
2317 hash_unlock(&globvarht);
2318}
2319#endif
2320
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002321/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002322 * Local string buffer for the next two functions to store a variable name
2323 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2324 * get_user_var_name().
2325 */
2326
2327static char_u *varnamebuf = NULL;
2328static int varnamebuflen = 0;
2329
2330/*
2331 * Function to concatenate a prefix and a variable name.
2332 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002333 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002334cat_prefix_varname(int prefix, char_u *name)
2335{
2336 int len;
2337
2338 len = (int)STRLEN(name) + 3;
2339 if (len > varnamebuflen)
2340 {
2341 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002342 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002343 varnamebuf = alloc(len);
2344 if (varnamebuf == NULL)
2345 {
2346 varnamebuflen = 0;
2347 return NULL;
2348 }
2349 varnamebuflen = len;
2350 }
2351 *varnamebuf = prefix;
2352 varnamebuf[1] = ':';
2353 STRCPY(varnamebuf + 2, name);
2354 return varnamebuf;
2355}
2356
2357/*
2358 * Function given to ExpandGeneric() to obtain the list of user defined
2359 * (global/buffer/window/built-in) variable names.
2360 */
2361 char_u *
2362get_user_var_name(expand_T *xp, int idx)
2363{
2364 static long_u gdone;
2365 static long_u bdone;
2366 static long_u wdone;
2367 static long_u tdone;
2368 static int vidx;
2369 static hashitem_T *hi;
2370 hashtab_T *ht;
2371
2372 if (idx == 0)
2373 {
2374 gdone = bdone = wdone = vidx = 0;
2375 tdone = 0;
2376 }
2377
2378 // Global variables
2379 if (gdone < globvarht.ht_used)
2380 {
2381 if (gdone++ == 0)
2382 hi = globvarht.ht_array;
2383 else
2384 ++hi;
2385 while (HASHITEM_EMPTY(hi))
2386 ++hi;
2387 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2388 return cat_prefix_varname('g', hi->hi_key);
2389 return hi->hi_key;
2390 }
2391
2392 // b: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002393 ht = &prevwin_curwin()->w_buffer->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002394 if (bdone < ht->ht_used)
2395 {
2396 if (bdone++ == 0)
2397 hi = ht->ht_array;
2398 else
2399 ++hi;
2400 while (HASHITEM_EMPTY(hi))
2401 ++hi;
2402 return cat_prefix_varname('b', hi->hi_key);
2403 }
2404
2405 // w: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002406 ht = &prevwin_curwin()->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002407 if (wdone < ht->ht_used)
2408 {
2409 if (wdone++ == 0)
2410 hi = ht->ht_array;
2411 else
2412 ++hi;
2413 while (HASHITEM_EMPTY(hi))
2414 ++hi;
2415 return cat_prefix_varname('w', hi->hi_key);
2416 }
2417
2418 // t: variables
2419 ht = &curtab->tp_vars->dv_hashtab;
2420 if (tdone < ht->ht_used)
2421 {
2422 if (tdone++ == 0)
2423 hi = ht->ht_array;
2424 else
2425 ++hi;
2426 while (HASHITEM_EMPTY(hi))
2427 ++hi;
2428 return cat_prefix_varname('t', hi->hi_key);
2429 }
2430
2431 // v: variables
2432 if (vidx < VV_LEN)
2433 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2434
2435 VIM_CLEAR(varnamebuf);
2436 varnamebuflen = 0;
2437 return NULL;
2438}
2439
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002440 char *
2441get_var_special_name(int nr)
2442{
2443 switch (nr)
2444 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002445 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2446 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002447 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002448 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002449 }
2450 internal_error("get_var_special_name()");
2451 return "42";
2452}
2453
2454/*
2455 * Returns the global variable dictionary
2456 */
2457 dict_T *
2458get_globvar_dict(void)
2459{
2460 return &globvardict;
2461}
2462
2463/*
2464 * Returns the global variable hash table
2465 */
2466 hashtab_T *
2467get_globvar_ht(void)
2468{
2469 return &globvarht;
2470}
2471
2472/*
2473 * Returns the v: variable dictionary
2474 */
2475 dict_T *
2476get_vimvar_dict(void)
2477{
2478 return &vimvardict;
2479}
2480
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002481/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002482 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002483 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484 */
2485 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002486find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002488 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2489 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490
2491 if (di == NULL)
2492 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002493 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2495 return (int)(vv - vimvars);
2496}
2497
2498
2499/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002500 * Set type of v: variable to "type".
2501 */
2502 void
2503set_vim_var_type(int idx, vartype_T type)
2504{
Bram Moolenaard787e402021-12-24 21:36:12 +00002505 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002506}
2507
2508/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002509 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002510 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002511 */
2512 void
2513set_vim_var_nr(int idx, varnumber_T val)
2514{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002515 vimvars[idx].vv_nr = val;
2516}
2517
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 char *
2519get_vim_var_name(int idx)
2520{
2521 return vimvars[idx].vv_name;
2522}
2523
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002524/*
2525 * Get typval_T v: variable value.
2526 */
2527 typval_T *
2528get_vim_var_tv(int idx)
2529{
2530 return &vimvars[idx].vv_tv;
2531}
2532
Bram Moolenaard787e402021-12-24 21:36:12 +00002533 type_T *
2534get_vim_var_type(int idx, garray_T *type_list)
2535{
2536 if (vimvars[idx].vv_type != NULL)
2537 return vimvars[idx].vv_type;
2538 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2539}
2540
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002541/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002542 * Set v: variable to "tv". Only accepts the same type.
2543 * Takes over the value of "tv".
2544 */
2545 int
2546set_vim_var_tv(int idx, typval_T *tv)
2547{
Bram Moolenaard787e402021-12-24 21:36:12 +00002548 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002549 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002550 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002551 clear_tv(tv);
2552 return FAIL;
2553 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002554 // VV_RO is also checked when compiling, but let's check here as well.
2555 if (vimvars[idx].vv_flags & VV_RO)
2556 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002557 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002558 return FAIL;
2559 }
2560 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2561 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002562 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002563 return FAIL;
2564 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002565 clear_tv(&vimvars[idx].vv_di.di_tv);
2566 vimvars[idx].vv_di.di_tv = *tv;
2567 return OK;
2568}
2569
2570/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002571 * Get number v: variable value.
2572 */
2573 varnumber_T
2574get_vim_var_nr(int idx)
2575{
2576 return vimvars[idx].vv_nr;
2577}
2578
2579/*
2580 * Get string v: variable value. Uses a static buffer, can only be used once.
2581 * If the String variable has never been set, return an empty string.
2582 * Never returns NULL;
2583 */
2584 char_u *
2585get_vim_var_str(int idx)
2586{
2587 return tv_get_string(&vimvars[idx].vv_tv);
2588}
2589
2590/*
2591 * Get List v: variable value. Caller must take care of reference count when
2592 * needed.
2593 */
2594 list_T *
2595get_vim_var_list(int idx)
2596{
2597 return vimvars[idx].vv_list;
2598}
2599
2600/*
2601 * Get Dict v: variable value. Caller must take care of reference count when
2602 * needed.
2603 */
2604 dict_T *
2605get_vim_var_dict(int idx)
2606{
2607 return vimvars[idx].vv_dict;
2608}
2609
2610/*
2611 * Set v:char to character "c".
2612 */
2613 void
2614set_vim_var_char(int c)
2615{
2616 char_u buf[MB_MAXBYTES + 1];
2617
2618 if (has_mbyte)
2619 buf[(*mb_char2bytes)(c, buf)] = NUL;
2620 else
2621 {
2622 buf[0] = c;
2623 buf[1] = NUL;
2624 }
2625 set_vim_var_string(VV_CHAR, buf, -1);
2626}
2627
2628/*
2629 * Set v:count to "count" and v:count1 to "count1".
2630 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2631 */
2632 void
2633set_vcount(
2634 long count,
2635 long count1,
2636 int set_prevcount)
2637{
2638 if (set_prevcount)
2639 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2640 vimvars[VV_COUNT].vv_nr = count;
2641 vimvars[VV_COUNT1].vv_nr = count1;
2642}
2643
2644/*
2645 * Save variables that might be changed as a side effect. Used when executing
2646 * a timer callback.
2647 */
2648 void
2649save_vimvars(vimvars_save_T *vvsave)
2650{
2651 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2652 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2653 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2654}
2655
2656/*
2657 * Restore variables saved by save_vimvars().
2658 */
2659 void
2660restore_vimvars(vimvars_save_T *vvsave)
2661{
2662 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2663 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2664 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2665}
2666
2667/*
2668 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2669 * value.
2670 */
2671 void
2672set_vim_var_string(
2673 int idx,
2674 char_u *val,
2675 int len) // length of "val" to use or -1 (whole string)
2676{
2677 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002678 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002679 if (val == NULL)
2680 vimvars[idx].vv_str = NULL;
2681 else if (len == -1)
2682 vimvars[idx].vv_str = vim_strsave(val);
2683 else
2684 vimvars[idx].vv_str = vim_strnsave(val, len);
2685}
2686
2687/*
2688 * Set List v: variable to "val".
2689 */
2690 void
2691set_vim_var_list(int idx, list_T *val)
2692{
2693 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002694 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002695 vimvars[idx].vv_list = val;
2696 if (val != NULL)
2697 ++val->lv_refcount;
2698}
2699
2700/*
2701 * Set Dictionary v: variable to "val".
2702 */
2703 void
2704set_vim_var_dict(int idx, dict_T *val)
2705{
2706 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002707 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002708 vimvars[idx].vv_dict = val;
2709 if (val != NULL)
2710 {
2711 ++val->dv_refcount;
2712 dict_set_items_ro(val);
2713 }
2714}
2715
2716/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002717 * Set the v:argv list.
2718 */
2719 void
2720set_argv_var(char **argv, int argc)
2721{
2722 list_T *l = list_alloc();
2723 int i;
2724
2725 if (l == NULL)
2726 getout(1);
2727 l->lv_lock = VAR_FIXED;
2728 for (i = 0; i < argc; ++i)
2729 {
2730 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2731 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002732 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002733 }
2734 set_vim_var_list(VV_ARGV, l);
2735}
2736
2737/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002738 * Reset v:register, taking the 'clipboard' setting into account.
2739 */
2740 void
2741reset_reg_var(void)
2742{
2743 int regname = 0;
2744
2745 // Adjust the register according to 'clipboard', so that when
2746 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2747#ifdef FEAT_CLIPBOARD
2748 adjust_clip_reg(&regname);
2749#endif
2750 set_reg_var(regname);
2751}
2752
2753/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002754 * Set v:register if needed.
2755 */
2756 void
2757set_reg_var(int c)
2758{
2759 char_u regname;
2760
2761 if (c == 0 || c == ' ')
2762 regname = '"';
2763 else
2764 regname = c;
2765 // Avoid free/alloc when the value is already right.
2766 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2767 set_vim_var_string(VV_REG, &regname, 1);
2768}
2769
2770/*
2771 * Get or set v:exception. If "oldval" == NULL, return the current value.
2772 * Otherwise, restore the value to "oldval" and return NULL.
2773 * Must always be called in pairs to save and restore v:exception! Does not
2774 * take care of memory allocations.
2775 */
2776 char_u *
2777v_exception(char_u *oldval)
2778{
2779 if (oldval == NULL)
2780 return vimvars[VV_EXCEPTION].vv_str;
2781
2782 vimvars[VV_EXCEPTION].vv_str = oldval;
2783 return NULL;
2784}
2785
2786/*
2787 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2788 * Otherwise, restore the value to "oldval" and return NULL.
2789 * Must always be called in pairs to save and restore v:throwpoint! Does not
2790 * take care of memory allocations.
2791 */
2792 char_u *
2793v_throwpoint(char_u *oldval)
2794{
2795 if (oldval == NULL)
2796 return vimvars[VV_THROWPOINT].vv_str;
2797
2798 vimvars[VV_THROWPOINT].vv_str = oldval;
2799 return NULL;
2800}
2801
2802/*
2803 * Set v:cmdarg.
2804 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2805 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2806 * Must always be called in pairs!
2807 */
2808 char_u *
2809set_cmdarg(exarg_T *eap, char_u *oldarg)
2810{
2811 char_u *oldval;
2812 char_u *newval;
2813 unsigned len;
2814
2815 oldval = vimvars[VV_CMDARG].vv_str;
2816 if (eap == NULL)
2817 {
2818 vim_free(oldval);
2819 vimvars[VV_CMDARG].vv_str = oldarg;
2820 return NULL;
2821 }
2822
2823 if (eap->force_bin == FORCE_BIN)
2824 len = 6;
2825 else if (eap->force_bin == FORCE_NOBIN)
2826 len = 8;
2827 else
2828 len = 0;
2829
2830 if (eap->read_edit)
2831 len += 7;
2832
2833 if (eap->force_ff != 0)
2834 len += 10; // " ++ff=unix"
2835 if (eap->force_enc != 0)
2836 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2837 if (eap->bad_char != 0)
2838 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2839
2840 newval = alloc(len + 1);
2841 if (newval == NULL)
2842 return NULL;
2843
2844 if (eap->force_bin == FORCE_BIN)
2845 sprintf((char *)newval, " ++bin");
2846 else if (eap->force_bin == FORCE_NOBIN)
2847 sprintf((char *)newval, " ++nobin");
2848 else
2849 *newval = NUL;
2850
2851 if (eap->read_edit)
2852 STRCAT(newval, " ++edit");
2853
2854 if (eap->force_ff != 0)
2855 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2856 eap->force_ff == 'u' ? "unix"
2857 : eap->force_ff == 'd' ? "dos"
2858 : "mac");
2859 if (eap->force_enc != 0)
2860 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2861 eap->cmd + eap->force_enc);
2862 if (eap->bad_char == BAD_KEEP)
2863 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2864 else if (eap->bad_char == BAD_DROP)
2865 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2866 else if (eap->bad_char != 0)
2867 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2868 vimvars[VV_CMDARG].vv_str = newval;
2869 return oldval;
2870}
2871
2872/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002873 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002874 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2875 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002876 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2877 */
2878 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002879eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002880 char_u *name,
2881 int len, // length of "name"
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002882 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002883 typval_T *rettv, // NULL when only checking existence
2884 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002885 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002886{
2887 int ret = OK;
2888 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002889 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002890 hashtab_T *ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002891 int cc;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002892 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002893
2894 // truncate the name, so that we can use strcmp()
2895 cc = name[len];
2896 name[len] = NUL;
2897
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002898 // Check for local variable when debugging.
2899 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002900 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002901 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002902 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2903
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002904 if (v != NULL)
2905 {
2906 tv = &v->di_tv;
2907 if (dip != NULL)
2908 *dip = v;
2909 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002910 else
2911 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002912 }
2913
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002914 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002916 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002917 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2918
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002919 if (sid == 0)
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00002920 import = find_imported(p, 0, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921
2922 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002923 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002925 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002926 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00002927 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02002928 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002929 ht = &SCRIPT_VARS(sid);
2930 if (ht != NULL)
2931 {
2932 dictitem_T *v = find_var_in_ht(ht, 0, name,
2933 flags & EVAL_VAR_NOAUTOLOAD);
2934
2935 if (v != NULL)
2936 {
2937 tv = &v->di_tv;
2938 if (dip != NULL)
2939 *dip = v;
2940 }
2941 else
2942 ht = NULL;
2943 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002944 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002945 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002946 {
2947 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00002948 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002949 ret = FAIL;
2950 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002951 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002952 else
2953 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002954 if (rettv != NULL)
2955 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01002956 // special value that is used in handle_subscript()
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002957 rettv->v_type = VAR_ANY;
2958 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
2959 }
2960 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002961 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00002963 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002964 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002965 int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002966 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002967
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002968 // In Vim9 script we can get a function reference by using the
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002969 // function name. For a global non-autoload function "g:" is
2970 // required.
2971 if (ufunc != NULL && (has_g_prefix
2972 || !func_requires_g_prefix(ufunc)))
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002973 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002974 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002975 if (rettv != NULL)
2976 {
2977 rettv->v_type = VAR_FUNC;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002978 if (has_g_prefix)
Bram Moolenaaref082e12021-12-12 21:02:03 +00002979 // Keep the "g:", otherwise script-local may be
2980 // assumed.
2981 rettv->vval.v_string = vim_strsave(name);
2982 else
2983 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002984 if (rettv->vval.v_string != NULL)
2985 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002986 }
2987 }
2988 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 }
2990
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002991 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002992 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002993 if (tv == NULL)
2994 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002995 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002996 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002997 ret = FAIL;
2998 }
2999 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003000 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003001 svar_T *sv = NULL;
3002 int was_assigned = FALSE;
3003
Bram Moolenaar11005b02021-07-11 20:59:00 +02003004 if (ht != NULL && ht == get_script_local_ht()
3005 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02003006 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003007 sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaarf055d452021-07-08 20:57:24 +02003008 if (sv != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003009 {
Bram Moolenaarf055d452021-07-08 20:57:24 +02003010 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003011 was_assigned = sv->sv_flags & SVFLAG_ASSIGNED;
3012 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003013 }
3014
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003015 // If a list or dict variable wasn't initialized and has meaningful
3016 // type, do it now. Not for global variables, they are not
3017 // declared.
Bram Moolenaar7a222242022-03-01 19:23:24 +00003018 if (ht != &globvarht)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003019 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003020 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003021 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003022 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003023 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003024 tv->vval.v_dict = dict_alloc();
3025 if (tv->vval.v_dict != NULL)
3026 {
3027 ++tv->vval.v_dict->dv_refcount;
3028 tv->vval.v_dict->dv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003029 if (sv != NULL)
3030 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003031 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003032 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003033 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003034 && ((type != NULL && !was_assigned)
Bram Moolenaar501f9782022-03-27 16:51:04 +01003035 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003036 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003037 tv->vval.v_list = list_alloc();
3038 if (tv->vval.v_list != NULL)
3039 {
3040 ++tv->vval.v_list->lv_refcount;
3041 tv->vval.v_list->lv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003042 if (sv != NULL)
3043 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003044 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003045 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003046 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003047 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003048 || !in_vim9script()))
3049 {
3050 tv->vval.v_blob = blob_alloc();
3051 if (tv->vval.v_blob != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003052 {
Bram Moolenaar859cc212022-03-28 15:22:35 +01003053 ++tv->vval.v_blob->bv_refcount;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003054 if (sv != NULL)
3055 sv->sv_flags |= SVFLAG_ASSIGNED;
3056 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003057 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02003058 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003059 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003060 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003061 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003062
3063 name[len] = cc;
3064
3065 return ret;
3066}
3067
3068/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003069 * Check if variable "name[len]" is a local variable or an argument.
3070 * If so, "*eval_lavars_used" is set to TRUE.
3071 */
3072 void
3073check_vars(char_u *name, int len)
3074{
3075 int cc;
3076 char_u *varname;
3077 hashtab_T *ht;
3078
3079 if (eval_lavars_used == NULL)
3080 return;
3081
3082 // truncate the name, so that we can use strcmp()
3083 cc = name[len];
3084 name[len] = NUL;
3085
3086 ht = find_var_ht(name, &varname);
3087 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
3088 {
3089 if (find_var(name, NULL, TRUE) != NULL)
3090 *eval_lavars_used = TRUE;
3091 }
3092
3093 name[len] = cc;
3094}
3095
3096/*
3097 * Find variable "name" in the list of variables.
3098 * Return a pointer to it if found, NULL if not found.
3099 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003100 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003101 */
3102 dictitem_T *
3103find_var(char_u *name, hashtab_T **htp, int no_autoload)
3104{
3105 char_u *varname;
3106 hashtab_T *ht;
3107 dictitem_T *ret = NULL;
3108
3109 ht = find_var_ht(name, &varname);
3110 if (htp != NULL)
3111 *htp = ht;
3112 if (ht == NULL)
3113 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003114 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003115 if (ret != NULL)
3116 return ret;
3117
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003118 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003119 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003120 if (ret != NULL)
3121 return ret;
3122
3123 // in Vim9 script items without a scope can be script-local
3124 if (in_vim9script() && name[0] != NUL && name[1] != ':')
3125 {
3126 ht = get_script_local_ht();
3127 if (ht != NULL)
3128 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003129 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003130 if (ret != NULL)
3131 {
3132 if (htp != NULL)
3133 *htp = ht;
3134 return ret;
3135 }
3136 }
3137 }
3138
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003139 // When using "vim9script autoload" script-local items are prefixed but can
3140 // be used with s:name.
3141 if (SCRIPT_ID_VALID(current_sctx.sc_sid)
3142 && name[0] == 's' && name[1] == ':')
3143 {
3144 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
3145
3146 if (si->sn_autoload_prefix != NULL)
3147 {
3148 char_u *auto_name = concat_str(si->sn_autoload_prefix, name + 2);
3149
3150 if (auto_name != NULL)
3151 {
3152 ht = &globvarht;
3153 ret = find_var_in_ht(ht, *name, auto_name, TRUE);
Bram Moolenaar130f65d2022-01-13 20:39:41 +00003154 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003155 if (ret != NULL)
3156 {
3157 if (htp != NULL)
3158 *htp = ht;
3159 return ret;
3160 }
3161 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003162 }
3163 }
3164
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003165 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003166}
3167
3168/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00003169 * Like find_var() but if the name starts with <SNR>99_ then look in the
3170 * referenced script (used for a funcref).
3171 */
3172 dictitem_T *
3173find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
3174{
3175 if (STRNCMP(name, "<SNR>", 5) == 0 && isdigit(name[5]))
3176 {
3177 char_u *p = name + 5;
3178 int sid = getdigits(&p);
3179
3180 if (SCRIPT_ID_VALID(sid) && *p == '_')
3181 {
3182 hashtab_T *ht = &SCRIPT_VARS(sid);
3183
3184 if (ht != NULL)
3185 {
3186 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
3187
3188 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003189 {
3190 if (htp != NULL)
3191 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00003192 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003193 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00003194 }
3195 }
3196 }
3197
3198 return find_var(name, htp, no_autoload);
3199}
3200
3201/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003202 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02003203 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003204 * Returns NULL if not found.
3205 */
3206 dictitem_T *
3207find_var_in_ht(
3208 hashtab_T *ht,
3209 int htname,
3210 char_u *varname,
3211 int no_autoload)
3212{
3213 hashitem_T *hi;
3214
3215 if (*varname == NUL)
3216 {
3217 // Must be something like "s:", otherwise "ht" would be NULL.
3218 switch (htname)
3219 {
3220 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
3221 case 'g': return &globvars_var;
3222 case 'v': return &vimvars_var;
3223 case 'b': return &curbuf->b_bufvar;
3224 case 'w': return &curwin->w_winvar;
3225 case 't': return &curtab->tp_winvar;
3226 case 'l': return get_funccal_local_var();
3227 case 'a': return get_funccal_args_var();
3228 }
3229 return NULL;
3230 }
3231
3232 hi = hash_find(ht, varname);
3233 if (HASHITEM_EMPTY(hi))
3234 {
3235 // For global variables we may try auto-loading the script. If it
3236 // worked find the variable again. Don't auto-load a script if it was
3237 // loaded already, otherwise it would be loaded every time when
3238 // checking if a function name is a Funcref variable.
3239 if (ht == &globvarht && !no_autoload)
3240 {
3241 // Note: script_autoload() may make "hi" invalid. It must either
3242 // be obtained again or not used.
3243 if (!script_autoload(varname, FALSE) || aborting())
3244 return NULL;
3245 hi = hash_find(ht, varname);
3246 }
3247 if (HASHITEM_EMPTY(hi))
3248 return NULL;
3249 }
3250 return HI2DI(hi);
3251}
3252
3253/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 * Get the script-local hashtab. NULL if not in a script context.
3255 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003256 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257get_script_local_ht(void)
3258{
3259 scid_T sid = current_sctx.sc_sid;
3260
Bram Moolenaare3d46852020-08-29 13:39:17 +02003261 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 return &SCRIPT_VARS(sid);
3263 return NULL;
3264}
3265
3266/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003267 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003268 * When "cmd" is TRUE it must look like a command, a function must be followed
3269 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003270 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003272 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003273lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003274 char_u *name,
3275 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003276 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003277 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278{
3279 hashtab_T *ht = get_script_local_ht();
3280 char_u buffer[30];
3281 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003282 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003284 int is_global = FALSE;
3285 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286
3287 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003288 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 if (len < sizeof(buffer) - 1)
3290 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003291 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 vim_strncpy(buffer, name, len);
3293 p = buffer;
3294 }
3295 else
3296 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003297 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003299 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 }
3301
3302 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003303 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304
3305 // if not script-local, then perhaps imported
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003306 if (res == FAIL && find_imported(p, 0, FALSE) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003307 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 if (p != buffer)
3309 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003310
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003311 // Find a function, so that a following "->" works.
3312 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3313 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003314 if (res != OK)
3315 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003316 p = skipwhite(name + len);
3317
3318 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003319 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003320 // Do not check for an internal function, since it might also be a
3321 // valid command, such as ":split" versus "split()".
3322 // Skip "g:" before a function name.
3323 if (name[0] == 'g' && name[1] == ':')
3324 {
3325 is_global = TRUE;
3326 fname = name + 2;
3327 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003328 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003329 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003330 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003331 }
3332
Bram Moolenaar709664c2020-12-12 14:33:41 +01003333 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334}
3335
3336/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003337 * Find the hashtab used for a variable name.
3338 * Return NULL if the name is not valid.
3339 * Set "varname" to the start of name without ':'.
3340 */
3341 hashtab_T *
3342find_var_ht(char_u *name, char_u **varname)
3343{
3344 hashitem_T *hi;
3345 hashtab_T *ht;
3346
3347 if (name[0] == NUL)
3348 return NULL;
3349 if (name[1] != ':')
3350 {
3351 // The name must not start with a colon or #.
3352 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3353 return NULL;
3354 *varname = name;
3355
3356 // "version" is "v:version" in all scopes if scriptversion < 3.
3357 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003358 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003359 {
3360 hi = hash_find(&compat_hashtab, name);
3361 if (!HASHITEM_EMPTY(hi))
3362 return &compat_hashtab;
3363 }
3364
3365 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003366 if (ht != NULL)
3367 return ht; // local variable
3368
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003369 // In Vim9 script items at the script level are script-local, except
3370 // for autoload names.
3371 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003372 {
3373 ht = get_script_local_ht();
3374 if (ht != NULL)
3375 return ht;
3376 }
3377
3378 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003379 }
3380 *varname = name + 2;
3381 if (*name == 'g') // global variable
3382 return &globvarht;
3383 // There must be no ':' or '#' in the rest of the name, unless g: is used
3384 if (vim_strchr(name + 2, ':') != NULL
3385 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3386 return NULL;
3387 if (*name == 'b') // buffer variable
3388 return &curbuf->b_vars->dv_hashtab;
3389 if (*name == 'w') // window variable
3390 return &curwin->w_vars->dv_hashtab;
3391 if (*name == 't') // tab page variable
3392 return &curtab->tp_vars->dv_hashtab;
3393 if (*name == 'v') // v: variable
3394 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003395 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003396 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003398 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 if (*name == 'a') // a: function argument
3400 return get_funccal_args_ht();
3401 if (*name == 'l') // l: local function variable
3402 return get_funccal_local_ht();
3403 }
3404 if (*name == 's') // script variable
3405 {
3406 ht = get_script_local_ht();
3407 if (ht != NULL)
3408 return ht;
3409 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003410 return NULL;
3411}
3412
3413/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003414 * Get the string value of a (global/local) variable.
3415 * Note: see tv_get_string() for how long the pointer remains valid.
3416 * Returns NULL when it doesn't exist.
3417 */
3418 char_u *
3419get_var_value(char_u *name)
3420{
3421 dictitem_T *v;
3422
3423 v = find_var(name, NULL, FALSE);
3424 if (v == NULL)
3425 return NULL;
3426 return tv_get_string(&v->di_tv);
3427}
3428
3429/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003430 * Allocate a new hashtab for a sourced script. It will be used while
3431 * sourcing this script and when executing functions defined in the script.
3432 */
3433 void
3434new_script_vars(scid_T id)
3435{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003436 scriptvar_T *sv;
3437
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003438 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3439 if (sv == NULL)
3440 return;
3441 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003442 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003443}
3444
3445/*
3446 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3447 * point to it.
3448 */
3449 void
3450init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3451{
3452 hash_init(&dict->dv_hashtab);
3453 dict->dv_lock = 0;
3454 dict->dv_scope = scope;
3455 dict->dv_refcount = DO_NOT_FREE_CNT;
3456 dict->dv_copyID = 0;
3457 dict_var->di_tv.vval.v_dict = dict;
3458 dict_var->di_tv.v_type = VAR_DICT;
3459 dict_var->di_tv.v_lock = VAR_FIXED;
3460 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3461 dict_var->di_key[0] = NUL;
3462}
3463
3464/*
3465 * Unreference a dictionary initialized by init_var_dict().
3466 */
3467 void
3468unref_var_dict(dict_T *dict)
3469{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003470 // Now the dict needs to be freed if no one else is using it, go back to
3471 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003472 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3473 dict_unref(dict);
3474}
3475
3476/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003477 * Clean up a list of internal variables.
3478 * Frees all allocated variables and the value they contain.
3479 * Clears hashtab "ht", does not free it.
3480 */
3481 void
3482vars_clear(hashtab_T *ht)
3483{
3484 vars_clear_ext(ht, TRUE);
3485}
3486
3487/*
3488 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3489 */
3490 void
3491vars_clear_ext(hashtab_T *ht, int free_val)
3492{
3493 int todo;
3494 hashitem_T *hi;
3495 dictitem_T *v;
3496
3497 hash_lock(ht);
3498 todo = (int)ht->ht_used;
3499 for (hi = ht->ht_array; todo > 0; ++hi)
3500 {
3501 if (!HASHITEM_EMPTY(hi))
3502 {
3503 --todo;
3504
3505 // Free the variable. Don't remove it from the hashtab,
3506 // ht_array might change then. hash_clear() takes care of it
3507 // later.
3508 v = HI2DI(hi);
3509 if (free_val)
3510 clear_tv(&v->di_tv);
3511 if (v->di_flags & DI_FLAGS_ALLOC)
3512 vim_free(v);
3513 }
3514 }
3515 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003516 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003517}
3518
3519/*
3520 * Delete a variable from hashtab "ht" at item "hi".
3521 * Clear the variable value and free the dictitem.
3522 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003523 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003524delete_var(hashtab_T *ht, hashitem_T *hi)
3525{
3526 dictitem_T *di = HI2DI(hi);
3527
3528 hash_remove(ht, hi);
3529 clear_tv(&di->di_tv);
3530 vim_free(di);
3531}
3532
3533/*
3534 * List the value of one internal variable.
3535 */
3536 static void
3537list_one_var(dictitem_T *v, char *prefix, int *first)
3538{
3539 char_u *tofree;
3540 char_u *s;
3541 char_u numbuf[NUMBUFLEN];
3542
3543 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3544 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3545 s == NULL ? (char_u *)"" : s, first);
3546 vim_free(tofree);
3547}
3548
3549 static void
3550list_one_var_a(
3551 char *prefix,
3552 char_u *name,
3553 int type,
3554 char_u *string,
3555 int *first) // when TRUE clear rest of screen and set to FALSE
3556{
3557 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3558 msg_start();
3559 msg_puts(prefix);
3560 if (name != NULL) // "a:" vars don't have a name stored
3561 msg_puts((char *)name);
3562 msg_putchar(' ');
3563 msg_advance(22);
3564 if (type == VAR_NUMBER)
3565 msg_putchar('#');
3566 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3567 msg_putchar('*');
3568 else if (type == VAR_LIST)
3569 {
3570 msg_putchar('[');
3571 if (*string == '[')
3572 ++string;
3573 }
3574 else if (type == VAR_DICT)
3575 {
3576 msg_putchar('{');
3577 if (*string == '{')
3578 ++string;
3579 }
3580 else
3581 msg_putchar(' ');
3582
3583 msg_outtrans(string);
3584
3585 if (type == VAR_FUNC || type == VAR_PARTIAL)
3586 msg_puts("()");
3587 if (*first)
3588 {
3589 msg_clr_eos();
3590 *first = FALSE;
3591 }
3592}
3593
3594/*
3595 * Set variable "name" to value in "tv".
3596 * If the variable already exists, the value is updated.
3597 * Otherwise the variable is created.
3598 */
3599 void
3600set_var(
3601 char_u *name,
3602 typval_T *tv,
3603 int copy) // make copy of value in "tv"
3604{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003605 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003606}
3607
3608/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003609 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003610 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003611 * If the variable already exists and "is_const" is FALSE the value is updated.
3612 * Otherwise the variable is created.
3613 */
3614 void
3615set_var_const(
3616 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003617 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003618 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003619 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003620 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003621 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003622 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003623{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003624 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003625 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003626 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 dictitem_T *di;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003628 typval_T *dest_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003629 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003630 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003631 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003633 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003634 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003635 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003636 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003637 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003638
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003639 if (sid != 0)
3640 {
3641 if (SCRIPT_ID_VALID(sid))
3642 ht = &SCRIPT_VARS(sid);
3643 varname = name;
3644 }
3645 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003646 {
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003647 scriptitem_T *si;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003648
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003649 if (in_vim9script() && is_export
3650 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3651 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
Bram Moolenaar0e1574c2022-03-03 17:05:35 +00003652 ->sn_autoload_prefix != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003653 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003654 // In a vim9 autoload script an exported variable is put in the
3655 // global namespace with the autoload prefix.
3656 var_in_autoload = TRUE;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003657 varname = concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003658 if (varname == NULL)
3659 goto failed;
3660 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003661 ht = &globvarht;
3662 }
3663 else
3664 ht = find_var_ht(name, &varname);
3665 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003666 if (ht == NULL || *varname == NUL)
3667 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003668 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003669 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003670 }
Bram Moolenaar54969f42022-02-07 13:56:44 +00003671 is_script_local = ht == get_script_local_ht() || sid != 0
3672 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003673
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003674 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003675 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003676 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003677 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003678 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003679 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003680 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003681 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003682 }
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003683 if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
3684 && vim_strchr((char_u *)"gwbt", name[0]) != NULL)
3685 // Do not make g:var, w:var, b:var or t:var final.
3686 flags &= ~ASSIGN_FINAL;
3687
Bram Moolenaare535db82021-03-31 21:07:24 +02003688 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003689 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3690 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003691 // For "[a, _] = list" the underscore is ignored.
3692 if ((flags & ASSIGN_UNPACK) == 0)
3693 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003694 goto failed;
3695 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003696
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003698
Bram Moolenaar24e93162021-07-18 20:40:33 +02003699 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003700 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003701 imported_T *import = find_imported(varname, 0, FALSE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003702
Bram Moolenaar24e93162021-07-18 20:40:33 +02003703 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003704 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003705 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02003706 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003708 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003709 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003711 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3712 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003713 }
Bram Moolenaar75e27d72022-02-13 13:56:29 +00003714 if (!in_vim9script())
3715 {
3716 semsg(_(e_cannot_create_vim9_script_variable_in_function_str),
3717 name);
3718 goto failed;
3719 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003720 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721
Bram Moolenaar993faa32022-02-21 15:59:11 +00003722 // Search in parent scope which is possible to reference from lambda
3723 if (di == NULL)
3724 di = find_var_in_scoped_ht(name, TRUE);
3725
3726 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
3727 && var_wrong_func_name(name, di == NULL))
3728 goto failed;
3729
3730 if (need_convert_to_bool(type, tv))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003731 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003732 // Destination is a bool and the value is not, but it can be
3733 // converted.
3734 CLEAR_FIELD(bool_tv);
3735 bool_tv.v_type = VAR_BOOL;
3736 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3737 tv = &bool_tv;
3738 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003739
Bram Moolenaar993faa32022-02-21 15:59:11 +00003740 if (di != NULL)
3741 {
3742 // Item already exists. Allowed to replace when reloading.
3743 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003744 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003745 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
3746 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003747 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003748 emsg(_(e_cannot_modify_existing_variable));
3749 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003750 }
3751
Bram Moolenaar993faa32022-02-21 15:59:11 +00003752 if (is_script_local && vim9script
3753 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003754 {
3755 semsg(_(e_redefining_script_item_str), name);
3756 goto failed;
3757 }
3758
Bram Moolenaar993faa32022-02-21 15:59:11 +00003759 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003760 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003761 where_T where = WHERE_INIT;
Bram Moolenaar7a411a32022-04-04 14:58:06 +01003762 svar_T *sv = find_typval_in_script(&di->di_tv, sid, TRUE);
Bram Moolenaar993faa32022-02-21 15:59:11 +00003763
3764 if (sv != NULL)
3765 {
3766 // check the type and adjust to bool if needed
3767 where.wt_index = var_idx;
3768 where.wt_variable = TRUE;
3769 if (check_script_var_type(sv, tv, name, where) == FAIL)
3770 goto failed;
3771 if (type == NULL)
3772 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003773 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003774 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003775 }
3776
Bram Moolenaar993faa32022-02-21 15:59:11 +00003777 if ((flags & ASSIGN_FOR_LOOP) == 0
3778 && var_check_permission(di, name) == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003779 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003780 }
3781 else
3782 {
3783 // can only redefine once
3784 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003785
Bram Moolenaar993faa32022-02-21 15:59:11 +00003786 // A Vim9 script-local variable is also present in sn_all_vars
3787 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003788 if (var_in_vim9script || var_in_autoload)
Bram Moolenaar993faa32022-02-21 15:59:11 +00003789 update_vim9_script_var(FALSE, di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003790 var_in_autoload ? name : di->di_key, flags,
Bram Moolenaar993faa32022-02-21 15:59:11 +00003791 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003792 }
3793
Bram Moolenaar993faa32022-02-21 15:59:11 +00003794 // existing variable, need to clear the value
3795
3796 // Handle setting internal di: variables separately where needed to
3797 // prevent changing the type.
3798 if (ht == &vimvarht)
3799 {
3800 if (di->di_tv.v_type == VAR_STRING)
3801 {
3802 VIM_CLEAR(di->di_tv.vval.v_string);
3803 if (copy || tv->v_type != VAR_STRING)
3804 {
3805 char_u *val = tv_get_string(tv);
3806
3807 // Careful: when assigning to v:errmsg and
3808 // tv_get_string() causes an error message the variable
3809 // will already be set.
3810 if (di->di_tv.vval.v_string == NULL)
3811 di->di_tv.vval.v_string = vim_strsave(val);
3812 }
3813 else
3814 {
3815 // Take over the string to avoid an extra alloc/free.
3816 di->di_tv.vval.v_string = tv->vval.v_string;
3817 tv->vval.v_string = NULL;
3818 }
3819 goto failed;
3820 }
3821 else if (di->di_tv.v_type == VAR_NUMBER)
3822 {
3823 di->di_tv.vval.v_number = tv_get_number(tv);
3824 if (STRCMP(varname, "searchforward") == 0)
3825 set_search_direction(di->di_tv.vval.v_number
3826 ? '/' : '?');
3827#ifdef FEAT_SEARCH_EXTRA
3828 else if (STRCMP(varname, "hlsearch") == 0)
3829 {
3830 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003831 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar993faa32022-02-21 15:59:11 +00003832 }
3833#endif
3834 goto failed;
3835 }
3836 else if (di->di_tv.v_type != tv->v_type)
3837 {
3838 semsg(_(e_setting_str_to_value_with_wrong_type), name);
3839 goto failed;
3840 }
3841 }
3842
3843 clear_tv(&di->di_tv);
3844 }
3845 else
3846 {
3847 // Item not found, check if a function already exists.
3848 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
3849 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
3850 {
3851 semsg(_(e_redefining_script_item_str), name);
3852 goto failed;
3853 }
3854
3855 // add a new variable
3856 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3857 {
3858 semsg(_(e_unknown_variable_str), name);
3859 goto failed;
3860 }
3861
3862 // Can't add "v:" or "a:" variable.
3863 if (ht == &vimvarht || ht == get_funccal_args_ht())
3864 {
3865 semsg(_(e_illegal_variable_name_str), name);
3866 goto failed;
3867 }
3868
3869 // Make sure the variable name is valid. In Vim9 script an
3870 // autoload variable must be prefixed with "g:" unless in an
3871 // autoload script.
3872 if (!valid_varname(varname, -1, !vim9script
3873 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
3874 goto failed;
3875
3876 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3877 if (di == NULL)
3878 goto failed;
3879 STRCPY(di->di_key, varname);
3880 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
3881 {
3882 vim_free(di);
3883 goto failed;
3884 }
3885 di->di_flags = DI_FLAGS_ALLOC;
3886 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3887 di->di_flags |= DI_FLAGS_LOCK;
3888
3889 // A Vim9 script-local variable is also added to sn_all_vars and
3890 // sn_var_vals. It may set "type" from "tv".
3891 if (var_in_vim9script || var_in_autoload)
3892 update_vim9_script_var(TRUE, di,
3893 var_in_autoload ? name : di->di_key, flags,
3894 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003895 }
3896
Bram Moolenaar993faa32022-02-21 15:59:11 +00003897 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003898 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003899 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003900 else
3901 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003902 *dest_tv = *tv;
3903 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003904 init_tv(tv);
3905 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003906 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003907
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003908 if (vim9script && type != NULL)
Bram Moolenaar381692b2022-02-02 20:01:27 +00003909 set_tv_type(dest_tv, type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003910
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003911 // ":const var = value" locks the value
3912 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003913 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003914 // Like :lockvar! name: lock the value and what it contains, but only
3915 // if the reference count is up to one. That locks only literal
3916 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003917 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003918
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003919failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003920 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003921 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003922 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003923}
3924
3925/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003926 * Check in this order for backwards compatibility:
3927 * - Whether the variable is read-only
3928 * - Whether the variable value is locked
3929 * - Whether the variable is locked
3930 */
3931 int
3932var_check_permission(dictitem_T *di, char_u *name)
3933{
3934 if (var_check_ro(di->di_flags, name, FALSE)
3935 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3936 || var_check_lock(di->di_flags, name, FALSE))
3937 return FAIL;
3938 return OK;
3939}
3940
3941/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003942 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3943 * Also give an error message.
3944 */
3945 int
3946var_check_ro(int flags, char_u *name, int use_gettext)
3947{
3948 if (flags & DI_FLAGS_RO)
3949 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003950 if (name == NULL)
3951 emsg(_(e_cannot_change_readonly_variable));
3952 else
3953 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02003954 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003955 return TRUE;
3956 }
3957 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3958 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003959 if (name == NULL)
3960 emsg(_(e_cannot_set_variable_in_sandbox));
3961 else
3962 semsg(_(e_cannot_set_variable_in_sandbox_str),
3963 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003964 return TRUE;
3965 }
3966 return FALSE;
3967}
3968
3969/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003970 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3971 * Also give an error message.
3972 */
3973 int
3974var_check_lock(int flags, char_u *name, int use_gettext)
3975{
3976 if (flags & DI_FLAGS_LOCK)
3977 {
3978 semsg(_(e_variable_is_locked_str),
3979 use_gettext ? (char_u *)_(name) : name);
3980 return TRUE;
3981 }
3982 return FALSE;
3983}
3984
3985/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003986 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3987 * Also give an error message.
3988 */
3989 int
3990var_check_fixed(int flags, char_u *name, int use_gettext)
3991{
3992 if (flags & DI_FLAGS_FIX)
3993 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003994 if (name == NULL)
3995 emsg(_(e_cannot_delete_variable));
3996 else
3997 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003998 use_gettext ? (char_u *)_(name) : name);
3999 return TRUE;
4000 }
4001 return FALSE;
4002}
4003
4004/*
4005 * Check if a funcref is assigned to a valid variable name.
4006 * Return TRUE and give an error if not.
4007 */
4008 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004009var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004010 char_u *name, // points to start of variable name
4011 int new_var) // TRUE when creating the variable
4012{
Bram Moolenaar32154662021-03-28 21:14:06 +02004013 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
4014 // the name can be used without the s: prefix.
4015 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
4016 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004017 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
4018 ? name[2] : name[0]))
4019 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004020 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004021 return TRUE;
4022 }
4023 // Don't allow hiding a function. When "v" is not NULL we might be
4024 // assigning another function to the same var, the type is checked
4025 // below.
4026 if (new_var && function_exists(name, FALSE))
4027 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004028 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004029 name);
4030 return TRUE;
4031 }
4032 return FALSE;
4033}
4034
4035/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004036 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
4037 * value. Also give an error message, using "name" or _("name") when
4038 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004039 */
4040 int
Bram Moolenaara187c432020-09-16 21:08:28 +02004041value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004042{
4043 if (lock & VAR_LOCKED)
4044 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004045 if (name == NULL)
4046 emsg(_(e_value_is_locked));
4047 else
4048 semsg(_(e_value_is_locked_str),
4049 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004050 return TRUE;
4051 }
4052 if (lock & VAR_FIXED)
4053 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004054 if (name == NULL)
4055 emsg(_(e_cannot_change_value));
4056 else
4057 semsg(_(e_cannot_change_value_of_str),
4058 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004059 return TRUE;
4060 }
4061 return FALSE;
4062}
4063
4064/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01004065 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004066 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004067 * Return FALSE and give an error if not.
4068 */
4069 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004070valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004071{
4072 char_u *p;
4073
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004074 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004075 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01004076 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004077 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004078 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004079 return FALSE;
4080 }
4081 return TRUE;
4082}
4083
4084/*
LemonBoy47d4e312022-05-04 18:12:55 +01004085 * Implements the logic to retrieve local variable and option values.
4086 * Used by "getwinvar()" "gettabvar()" "gettabwinvar()" "getbufvar()".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004087 */
4088 static void
LemonBoy47d4e312022-05-04 18:12:55 +01004089get_var_from(
4090 char_u *varname,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004091 typval_T *rettv,
LemonBoy47d4e312022-05-04 18:12:55 +01004092 typval_T *deftv, // Default value if not found.
4093 int htname, // 't'ab, 'w'indow or 'b'uffer local.
4094 tabpage_T *tp, // can be NULL
4095 win_T *win,
4096 buf_T *buf) // Ignored if htname is not 'b'.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004097{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004098 dictitem_T *v;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004099 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004100 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004101 int need_switch_win;
zeertzjqcd6ad642022-07-25 12:28:09 +01004102 int do_change_curbuf = buf != NULL && htname == 'b';
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004103
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004104 ++emsg_off;
4105
4106 rettv->v_type = VAR_STRING;
4107 rettv->vval.v_string = NULL;
4108
LemonBoy47d4e312022-05-04 18:12:55 +01004109 if (varname != NULL && tp != NULL && win != NULL
4110 && (htname != 'b' || buf != NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004111 {
4112 // Set curwin to be our win, temporarily. Also set the tabpage,
4113 // otherwise the window is not valid. Only do this when needed,
4114 // autocommands get blocked.
LemonBoy47d4e312022-05-04 18:12:55 +01004115 // If we have a buffer reference avoid the switching, we're saving and
4116 // restoring curbuf directly.
zeertzjqcd6ad642022-07-25 12:28:09 +01004117 need_switch_win = !(tp == curtab && win == curwin) && !do_change_curbuf;
LemonBoy47d4e312022-05-04 18:12:55 +01004118 if (!need_switch_win || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004119 {
LemonBoy47d4e312022-05-04 18:12:55 +01004120 // Handle options. There are no tab-local options.
4121 if (*varname == '&' && htname != 't')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004122 {
LemonBoy47d4e312022-05-04 18:12:55 +01004123 buf_T *save_curbuf = curbuf;
4124
4125 // Change curbuf so the option is read from the correct buffer.
zeertzjqcd6ad642022-07-25 12:28:09 +01004126 if (do_change_curbuf)
LemonBoy47d4e312022-05-04 18:12:55 +01004127 curbuf = buf;
4128
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004129 if (varname[1] == NUL)
4130 {
zeertzjqcd6ad642022-07-25 12:28:09 +01004131 // get all window-local or buffer-local options in a dict
LemonBoy47d4e312022-05-04 18:12:55 +01004132 dict_T *opts = get_winbuf_options(htname == 'b');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004133
4134 if (opts != NULL)
4135 {
4136 rettv_dict_set(rettv, opts);
4137 done = TRUE;
4138 }
4139 }
LemonBoy47d4e312022-05-04 18:12:55 +01004140 else if (eval_option(&varname, rettv, TRUE) == OK)
4141 // Local option
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004142 done = TRUE;
LemonBoy47d4e312022-05-04 18:12:55 +01004143
4144 curbuf = save_curbuf;
4145 }
4146 else if (*varname == NUL)
4147 {
4148 // Empty string: return a dict with all the local variables.
4149 if (htname == 'b')
4150 v = &buf->b_bufvar;
4151 else if (htname == 'w')
4152 v = &win->w_winvar;
4153 else
4154 v = &tp->tp_winvar;
4155 copy_tv(&v->di_tv, rettv);
4156 done = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004157 }
4158 else
4159 {
LemonBoy47d4e312022-05-04 18:12:55 +01004160 hashtab_T *ht;
4161
4162 if (htname == 'b')
4163 ht = &buf->b_vars->dv_hashtab;
4164 else if (htname == 'w')
4165 ht = &win->w_vars->dv_hashtab;
4166 else
4167 ht = &tp->tp_vars->dv_hashtab;
4168
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004169 // Look up the variable.
LemonBoy47d4e312022-05-04 18:12:55 +01004170 v = find_var_in_ht(ht, htname, varname, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004171 if (v != NULL)
4172 {
4173 copy_tv(&v->di_tv, rettv);
4174 done = TRUE;
4175 }
4176 }
4177 }
4178
4179 if (need_switch_win)
4180 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004181 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004182 }
4183
LemonBoy47d4e312022-05-04 18:12:55 +01004184 if (!done && deftv->v_type != VAR_UNKNOWN)
4185 // use the default value
4186 copy_tv(deftv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004187
4188 --emsg_off;
4189}
4190
4191/*
LemonBoy47d4e312022-05-04 18:12:55 +01004192 * getwinvar() and gettabwinvar()
4193 */
4194 static void
4195getwinvar(
4196 typval_T *argvars,
4197 typval_T *rettv,
4198 int off) // 1 for gettabwinvar()
4199{
4200 char_u *varname;
4201 tabpage_T *tp;
4202 win_T *win;
4203
4204 if (off == 1)
4205 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4206 else
4207 tp = curtab;
4208 win = find_win_by_nr(&argvars[off], tp);
4209 varname = tv_get_string_chk(&argvars[off + 1]);
4210
4211 get_var_from(varname, rettv, &argvars[off + 2], 'w', tp, win, NULL);
4212}
4213
4214/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02004215 * Set option "varname" to the value of "varp" for the current buffer/window.
4216 */
4217 static void
4218set_option_from_tv(char_u *varname, typval_T *varp)
4219{
4220 long numval = 0;
4221 char_u *strval;
4222 char_u nbuf[NUMBUFLEN];
4223 int error = FALSE;
4224
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004225 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004226 {
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004227 if (is_string_option(varname))
4228 {
4229 emsg(_(e_string_required));
4230 return;
4231 }
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004232 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004233 strval = (char_u *)"0"; // avoid using "false"
4234 }
4235 else
4236 {
4237 if (!in_vim9script() || varp->v_type != VAR_STRING)
4238 numval = (long)tv_get_number_chk(varp, &error);
4239 strval = tv_get_string_buf_chk(varp, nbuf);
4240 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02004241 if (!error && strval != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004242 set_option_value_give_err(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar191929b2020-08-19 21:20:49 +02004243}
4244
4245/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004246 * "setwinvar()" and "settabwinvar()" functions
4247 */
4248 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004249setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004250{
4251 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004252 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004253 int need_switch_win;
4254 char_u *varname, *winvarname;
4255 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004256 tabpage_T *tp = NULL;
4257
4258 if (check_secure())
4259 return;
4260
4261 if (off == 1)
4262 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4263 else
4264 tp = curtab;
4265 win = find_win_by_nr(&argvars[off], tp);
4266 varname = tv_get_string_chk(&argvars[off + 1]);
4267 varp = &argvars[off + 2];
4268
4269 if (win != NULL && varname != NULL && varp != NULL)
4270 {
4271 need_switch_win = !(tp == curtab && win == curwin);
4272 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00004273 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004274 {
4275 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02004276 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004277 else
4278 {
4279 winvarname = alloc(STRLEN(varname) + 3);
4280 if (winvarname != NULL)
4281 {
4282 STRCPY(winvarname, "w:");
4283 STRCPY(winvarname + 2, varname);
4284 set_var(winvarname, varp, TRUE);
4285 vim_free(winvarname);
4286 }
4287 }
4288 }
4289 if (need_switch_win)
Bram Moolenaar18f47402022-01-06 13:24:51 +00004290 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004291 }
4292}
4293
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004294/*
4295 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4296 * v:option_type, and v:option_command.
4297 */
4298 void
4299reset_v_option_vars(void)
4300{
4301 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4302 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4303 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4304 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4305 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4306 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4307}
4308
4309/*
4310 * Add an assert error to v:errors.
4311 */
4312 void
4313assert_error(garray_T *gap)
4314{
4315 struct vimvar *vp = &vimvars[VV_ERRORS];
4316
Bram Moolenaard787e402021-12-24 21:36:12 +00004317 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004318 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004319 set_vim_var_list(VV_ERRORS, list_alloc());
4320 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4321}
4322
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004323 int
4324var_exists(char_u *var)
4325{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004326 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004327 char_u *name;
4328 char_u *tofree;
4329 typval_T tv;
4330 int len = 0;
4331 int n = FALSE;
4332
4333 // get_name_len() takes care of expanding curly braces
4334 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004335 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004336 if (len > 0)
4337 {
4338 if (tofree != NULL)
4339 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004340 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004341 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004342 if (n)
4343 {
4344 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004345 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004346 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4347 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004348 if (n)
4349 clear_tv(&tv);
4350 }
4351 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004352 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004353 n = FALSE;
4354
4355 vim_free(tofree);
4356 return n;
4357}
4358
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004359static lval_T *redir_lval = NULL;
4360#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4361static garray_T redir_ga; // only valid when redir_lval is not NULL
4362static char_u *redir_endp = NULL;
4363static char_u *redir_varname = NULL;
4364
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004365 int
4366alloc_redir_lval(void)
4367{
4368 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4369 if (redir_lval == NULL)
4370 return FAIL;
4371 return OK;
4372}
4373
4374 void
4375clear_redir_lval(void)
4376{
4377 VIM_CLEAR(redir_lval);
4378}
4379
4380 void
4381init_redir_ga(void)
4382{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004383 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004384}
4385
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004386/*
4387 * Start recording command output to a variable
4388 * When "append" is TRUE append to an existing variable.
4389 * Returns OK if successfully completed the setup. FAIL otherwise.
4390 */
4391 int
4392var_redir_start(char_u *name, int append)
4393{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004394 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004395 typval_T tv;
4396
4397 // Catch a bad name early.
4398 if (!eval_isnamec1(*name))
4399 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004400 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004401 return FAIL;
4402 }
4403
4404 // Make a copy of the name, it is used in redir_lval until redir ends.
4405 redir_varname = vim_strsave(name);
4406 if (redir_varname == NULL)
4407 return FAIL;
4408
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004409 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004410 {
4411 var_redir_stop();
4412 return FAIL;
4413 }
4414
4415 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004416 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004417
4418 // Parse the variable name (can be a dict or list entry).
4419 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4420 FNE_CHECK_START);
4421 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4422 {
4423 clear_lval(redir_lval);
4424 if (redir_endp != NULL && *redir_endp != NUL)
4425 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004426 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004427 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004428 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004429 redir_endp = NULL; // don't store a value, only cleanup
4430 var_redir_stop();
4431 return FAIL;
4432 }
4433
4434 // check if we can write to the variable: set it to or append an empty
4435 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004436 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004437 tv.v_type = VAR_STRING;
4438 tv.vval.v_string = (char_u *)"";
4439 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004440 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004441 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004442 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004443 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004444 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004445 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004446 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004447 {
4448 redir_endp = NULL; // don't store a value, only cleanup
4449 var_redir_stop();
4450 return FAIL;
4451 }
4452
4453 return OK;
4454}
4455
4456/*
4457 * Append "value[value_len]" to the variable set by var_redir_start().
4458 * The actual appending is postponed until redirection ends, because the value
4459 * appended may in fact be the string we write to, changing it may cause freed
4460 * memory to be used:
4461 * :redir => foo
4462 * :let foo
4463 * :redir END
4464 */
4465 void
4466var_redir_str(char_u *value, int value_len)
4467{
4468 int len;
4469
4470 if (redir_lval == NULL)
4471 return;
4472
4473 if (value_len == -1)
4474 len = (int)STRLEN(value); // Append the entire string
4475 else
4476 len = value_len; // Append only "value_len" characters
4477
4478 if (ga_grow(&redir_ga, len) == OK)
4479 {
4480 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4481 redir_ga.ga_len += len;
4482 }
4483 else
4484 var_redir_stop();
4485}
4486
4487/*
4488 * Stop redirecting command output to a variable.
4489 * Frees the allocated memory.
4490 */
4491 void
4492var_redir_stop(void)
4493{
4494 typval_T tv;
4495
4496 if (EVALCMD_BUSY)
4497 {
4498 redir_lval = NULL;
4499 return;
4500 }
4501
4502 if (redir_lval != NULL)
4503 {
4504 // If there was no error: assign the text to the variable.
4505 if (redir_endp != NULL)
4506 {
4507 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4508 tv.v_type = VAR_STRING;
4509 tv.vval.v_string = redir_ga.ga_data;
4510 // Call get_lval() again, if it's inside a Dict or List it may
4511 // have changed.
4512 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4513 FALSE, FALSE, 0, FNE_CHECK_START);
4514 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004515 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004516 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004517 clear_lval(redir_lval);
4518 }
4519
4520 // free the collected output
4521 VIM_CLEAR(redir_ga.ga_data);
4522
4523 VIM_CLEAR(redir_lval);
4524 }
4525 VIM_CLEAR(redir_varname);
4526}
4527
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004528/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004529 * Get the collected redirected text and clear redir_ga.
4530 */
4531 char_u *
4532get_clear_redir_ga(void)
4533{
4534 char_u *res;
4535
4536 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4537 res = redir_ga.ga_data;
4538 redir_ga.ga_data = NULL;
4539 return res;
4540}
4541
4542/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004543 * "gettabvar()" function
4544 */
4545 void
4546f_gettabvar(typval_T *argvars, typval_T *rettv)
4547{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004548 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004549 tabpage_T *tp;
4550 win_T *win = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004551
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004552 if (in_vim9script()
4553 && (check_for_number_arg(argvars, 0) == FAIL
4554 || check_for_string_arg(argvars, 1) == FAIL))
4555 return;
4556
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004557 varname = tv_get_string_chk(&argvars[1]);
4558 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
LemonBoy47d4e312022-05-04 18:12:55 +01004559 if (tp != NULL)
4560 win = tp == curtab || tp->tp_firstwin == NULL ? firstwin
4561 : tp->tp_firstwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004562
LemonBoy47d4e312022-05-04 18:12:55 +01004563 get_var_from(varname, rettv, &argvars[2], 't', tp, win, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004564}
4565
4566/*
4567 * "gettabwinvar()" function
4568 */
4569 void
4570f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4571{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004572 if (in_vim9script()
4573 && (check_for_number_arg(argvars, 0) == FAIL
4574 || check_for_number_arg(argvars, 1) == FAIL
4575 || check_for_string_arg(argvars, 2) == FAIL))
4576 return;
4577
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004578 getwinvar(argvars, rettv, 1);
4579}
4580
4581/*
4582 * "getwinvar()" function
4583 */
4584 void
4585f_getwinvar(typval_T *argvars, typval_T *rettv)
4586{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004587 if (in_vim9script()
4588 && (check_for_number_arg(argvars, 0) == FAIL
4589 || check_for_string_arg(argvars, 1) == FAIL))
4590 return;
4591
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004592 getwinvar(argvars, rettv, 0);
4593}
4594
4595/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004596 * "getbufvar()" function
4597 */
4598 void
4599f_getbufvar(typval_T *argvars, typval_T *rettv)
4600{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004601 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004602 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004603
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004604 if (in_vim9script()
4605 && (check_for_buffer_arg(argvars, 0) == FAIL
4606 || check_for_string_arg(argvars, 1) == FAIL))
4607 return;
4608
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004609 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004610 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004611
LemonBoy47d4e312022-05-04 18:12:55 +01004612 get_var_from(varname, rettv, &argvars[2], 'b', curtab, curwin, buf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004613}
4614
4615/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004616 * "settabvar()" function
4617 */
4618 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004619f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004620{
4621 tabpage_T *save_curtab;
4622 tabpage_T *tp;
4623 char_u *varname, *tabvarname;
4624 typval_T *varp;
4625
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004626 if (check_secure())
4627 return;
4628
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004629 if (in_vim9script()
4630 && (check_for_number_arg(argvars, 0) == FAIL
4631 || check_for_string_arg(argvars, 1) == FAIL))
4632 return;
4633
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004634 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4635 varname = tv_get_string_chk(&argvars[1]);
4636 varp = &argvars[2];
4637
4638 if (varname != NULL && varp != NULL && tp != NULL)
4639 {
4640 save_curtab = curtab;
4641 goto_tabpage_tp(tp, FALSE, FALSE);
4642
4643 tabvarname = alloc(STRLEN(varname) + 3);
4644 if (tabvarname != NULL)
4645 {
4646 STRCPY(tabvarname, "t:");
4647 STRCPY(tabvarname + 2, varname);
4648 set_var(tabvarname, varp, TRUE);
4649 vim_free(tabvarname);
4650 }
4651
4652 // Restore current tabpage
4653 if (valid_tabpage(save_curtab))
4654 goto_tabpage_tp(save_curtab, FALSE, FALSE);
4655 }
4656}
4657
4658/*
4659 * "settabwinvar()" function
4660 */
4661 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004662f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004663{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004664 if (in_vim9script()
4665 && (check_for_number_arg(argvars, 0) == FAIL
4666 || check_for_number_arg(argvars, 1) == FAIL
4667 || check_for_string_arg(argvars, 2) == FAIL))
4668 return;
4669
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004670 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004671}
4672
4673/*
4674 * "setwinvar()" function
4675 */
4676 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004677f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004678{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004679 if (in_vim9script()
4680 && (check_for_number_arg(argvars, 0) == FAIL
4681 || check_for_string_arg(argvars, 1) == FAIL))
4682 return;
4683
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004684 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004685}
4686
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004687/*
4688 * "setbufvar()" function
4689 */
4690 void
4691f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4692{
4693 buf_T *buf;
4694 char_u *varname, *bufvarname;
4695 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004696
4697 if (check_secure())
4698 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004699
4700 if (in_vim9script()
4701 && (check_for_buffer_arg(argvars, 0) == FAIL
4702 || check_for_string_arg(argvars, 1) == FAIL))
4703 return;
4704
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004705 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004706 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004707 varp = &argvars[2];
4708
4709 if (buf != NULL && varname != NULL && varp != NULL)
4710 {
4711 if (*varname == '&')
4712 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004713 aco_save_T aco;
4714
4715 // set curbuf to be our buf, temporarily
4716 aucmd_prepbuf(&aco, buf);
4717
Bram Moolenaar191929b2020-08-19 21:20:49 +02004718 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004719
4720 // reset notion of buffer
4721 aucmd_restbuf(&aco);
4722 }
4723 else
4724 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004725 bufvarname = alloc(STRLEN(varname) + 3);
4726 if (bufvarname != NULL)
4727 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004728 buf_T *save_curbuf = curbuf;
4729
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004730 curbuf = buf;
4731 STRCPY(bufvarname, "b:");
4732 STRCPY(bufvarname + 2, varname);
4733 set_var(bufvarname, varp, TRUE);
4734 vim_free(bufvarname);
4735 curbuf = save_curbuf;
4736 }
4737 }
4738 }
4739}
4740
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004741/*
4742 * Get a callback from "arg". It can be a Funcref or a function name.
4743 * When "arg" is zero return an empty string.
4744 * "cb_name" is not allocated.
4745 * "cb_name" is set to NULL for an invalid argument.
4746 */
4747 callback_T
4748get_callback(typval_T *arg)
4749{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004750 callback_T res;
4751 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004752
4753 res.cb_free_name = FALSE;
4754 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4755 {
4756 res.cb_partial = arg->vval.v_partial;
4757 ++res.cb_partial->pt_refcount;
4758 res.cb_name = partial_name(res.cb_partial);
4759 }
4760 else
4761 {
4762 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004763 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4764 && isdigit(*arg->vval.v_string))
4765 r = FAIL;
4766 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004767 {
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004768 if (arg->v_type == VAR_STRING)
4769 {
4770 char_u *name;
4771
4772 name = get_scriptlocal_funcname(arg->vval.v_string);
4773 if (name != NULL)
4774 {
4775 vim_free(arg->vval.v_string);
4776 arg->vval.v_string = name;
4777 }
4778 }
4779
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004780 res.cb_name = arg->vval.v_string;
4781 func_ref(res.cb_name);
4782 }
4783 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004784 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004785 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004786 r = FAIL;
4787
4788 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004789 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004790 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004791 res.cb_name = NULL;
4792 }
4793 }
4794 return res;
4795}
4796
4797/*
4798 * Copy a callback into a typval_T.
4799 */
4800 void
4801put_callback(callback_T *cb, typval_T *tv)
4802{
4803 if (cb->cb_partial != NULL)
4804 {
4805 tv->v_type = VAR_PARTIAL;
4806 tv->vval.v_partial = cb->cb_partial;
4807 ++tv->vval.v_partial->pt_refcount;
4808 }
4809 else
4810 {
4811 tv->v_type = VAR_FUNC;
4812 tv->vval.v_string = vim_strsave(cb->cb_name);
4813 func_ref(cb->cb_name);
4814 }
4815}
4816
4817/*
4818 * Make a copy of "src" into "dest", allocating the function name if needed,
4819 * without incrementing the refcount.
4820 */
4821 void
4822set_callback(callback_T *dest, callback_T *src)
4823{
4824 if (src->cb_partial == NULL)
4825 {
4826 // just a function name, make a copy
4827 dest->cb_name = vim_strsave(src->cb_name);
4828 dest->cb_free_name = TRUE;
4829 }
4830 else
4831 {
4832 // cb_name is a pointer into cb_partial
4833 dest->cb_name = src->cb_name;
4834 dest->cb_free_name = FALSE;
4835 }
4836 dest->cb_partial = src->cb_partial;
4837}
4838
4839/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004840 * Copy callback from "src" to "dest", incrementing the refcounts.
4841 */
4842 void
4843copy_callback(callback_T *dest, callback_T *src)
4844{
4845 dest->cb_partial = src->cb_partial;
4846 if (dest->cb_partial != NULL)
4847 {
4848 dest->cb_name = src->cb_name;
4849 dest->cb_free_name = FALSE;
4850 ++dest->cb_partial->pt_refcount;
4851 }
4852 else
4853 {
4854 dest->cb_name = vim_strsave(src->cb_name);
4855 dest->cb_free_name = TRUE;
4856 func_ref(src->cb_name);
4857 }
4858}
4859
4860/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004861 * When a callback refers to an autoload import, change the function name to
4862 * the "path#name" form. Uses the current script context.
4863 * Only works when the name is allocated.
4864 */
4865 void
4866expand_autload_callback(callback_T *cb)
4867{
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004868 char_u *name;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004869 char_u *p;
4870 imported_T *import;
4871
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004872 if (!in_vim9script() || cb->cb_name == NULL
4873 || (!cb->cb_free_name
4874 && (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004875 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004876 if (cb->cb_partial != NULL)
4877 name = cb->cb_partial->pt_name;
4878 else
4879 name = cb->cb_name;
4880 p = vim_strchr(name, '.');
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004881 if (p == NULL)
4882 return;
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004883 import = find_imported(name, p - name, FALSE);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004884 if (import != NULL && SCRIPT_ID_VALID(import->imp_sid))
4885 {
4886 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
4887
4888 if (si->sn_autoload_prefix != NULL)
4889 {
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004890 char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004891
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004892 if (newname != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004893 {
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004894 if (cb->cb_partial != NULL)
4895 {
4896 if (cb->cb_name == cb->cb_partial->pt_name)
4897 cb->cb_name = newname;
4898 vim_free(cb->cb_partial->pt_name);
4899 cb->cb_partial->pt_name = newname;
4900 }
4901 else
4902 {
4903 vim_free(cb->cb_name);
4904 cb->cb_name = newname;
4905 }
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004906 }
4907 }
4908 }
4909}
4910
4911/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004912 * Unref/free "callback" returned by get_callback() or set_callback().
4913 */
4914 void
4915free_callback(callback_T *callback)
4916{
4917 if (callback->cb_partial != NULL)
4918 {
4919 partial_unref(callback->cb_partial);
4920 callback->cb_partial = NULL;
4921 }
4922 else if (callback->cb_name != NULL)
4923 func_unref(callback->cb_name);
4924 if (callback->cb_free_name)
4925 {
4926 vim_free(callback->cb_name);
4927 callback->cb_free_name = FALSE;
4928 }
4929 callback->cb_name = NULL;
4930}
4931
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004932#endif // FEAT_EVAL