blob: 3c5b28a982c9db7ac256589da8b4fd5cd06d961d [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 Moolenaar9510d222022-09-11 15:14:05 +0100606 * Return TRUE if "name" starts with "g:", "w:", "t:" or "b:".
607 * But only when an identifier character follows.
608 */
609 int
610is_scoped_variable(char_u *name)
611{
612 return vim_strchr((char_u *)"gwbt", name[0]) != NULL
613 && name[1] == ':'
614 && eval_isnamec(name[2]);
615}
616
617/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100618 * Evaluate one Vim expression {expr} in string "p" and append the
619 * resulting string to "gap". "p" points to the opening "{".
Bram Moolenaar70c41242022-05-10 18:11:43 +0100620 * When "evaluate" is FALSE only skip over the expression.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100621 * Return a pointer to the character after "}", NULL for an error.
622 */
623 char_u *
Bram Moolenaar70c41242022-05-10 18:11:43 +0100624eval_one_expr_in_str(char_u *p, garray_T *gap, int evaluate)
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100625{
626 char_u *block_start = skipwhite(p + 1); // skip the opening {
627 char_u *block_end = block_start;
628 char_u *expr_val;
629
630 if (*block_start == NUL)
631 {
632 semsg(_(e_missing_close_curly_str), p);
633 return NULL;
634 }
635 if (skip_expr(&block_end, NULL) == FAIL)
636 return NULL;
637 block_end = skipwhite(block_end);
638 if (*block_end != '}')
639 {
640 semsg(_(e_missing_close_curly_str), p);
641 return NULL;
642 }
Bram Moolenaar70c41242022-05-10 18:11:43 +0100643 if (evaluate)
644 {
645 *block_end = NUL;
646 expr_val = eval_to_string(block_start, TRUE);
647 *block_end = '}';
648 if (expr_val == NULL)
649 return NULL;
650 ga_concat(gap, expr_val);
651 vim_free(expr_val);
652 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100653
654 return block_end + 1;
655}
656
657/*
658 * Evaluate all the Vim expressions {expr} in "str" and return the resulting
659 * string in allocated memory. "{{" is reduced to "{" and "}}" to "}".
660 * Used for a heredoc assignment.
661 * Returns NULL for an error.
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100662 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +0100663 static char_u *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100664eval_all_expr_in_str(char_u *str)
665{
666 garray_T ga;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100667 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100668
669 ga_init2(&ga, 1, 80);
670 p = str;
671
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100672 while (*p != NUL)
673 {
LemonBoy2eaef102022-05-06 13:14:50 +0100674 char_u *lit_start;
LemonBoy2eaef102022-05-06 13:14:50 +0100675 int escaped_brace = FALSE;
676
677 // Look for a block start.
678 lit_start = p;
679 while (*p != '{' && *p != '}' && *p != NUL)
680 ++p;
681
682 if (*p != NUL && *p == p[1])
683 {
684 // Escaped brace, unescape and continue.
685 // Include the brace in the literal string.
686 ++p;
687 escaped_brace = TRUE;
688 }
689 else if (*p == '}')
690 {
691 semsg(_(e_stray_closing_curly_str), str);
692 ga_clear(&ga);
693 return NULL;
694 }
695
696 // Append the literal part.
697 ga_concat_len(&ga, lit_start, (size_t)(p - lit_start));
698
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100699 if (*p == NUL)
LemonBoy2eaef102022-05-06 13:14:50 +0100700 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100701
LemonBoy2eaef102022-05-06 13:14:50 +0100702 if (escaped_brace)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100703 {
LemonBoy2eaef102022-05-06 13:14:50 +0100704 // Skip the second brace.
705 ++p;
706 continue;
707 }
708
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100709 // Evaluate the expression and append the result.
Bram Moolenaar70c41242022-05-10 18:11:43 +0100710 p = eval_one_expr_in_str(p, &ga, TRUE);
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100711 if (p == NULL)
LemonBoy2eaef102022-05-06 13:14:50 +0100712 {
713 ga_clear(&ga);
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100714 return NULL;
715 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100716 }
717 ga_append(&ga, NUL);
718
719 return ga.ga_data;
720}
721
722/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200723 * Get a list of lines from a HERE document. The here document is a list of
724 * lines surrounded by a marker.
725 * cmd << {marker}
726 * {line1}
727 * {line2}
728 * ....
729 * {marker}
730 *
731 * The {marker} is a string. If the optional 'trim' word is supplied before the
732 * marker, then the leading indentation before the lines (matching the
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100733 * indentation in the "cmd" line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200734 *
735 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100736 * tcl, mzscheme), "script_get" is set to TRUE. In this case, if the marker is
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200737 * missing, then '.' is accepted as a marker.
738 *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100739 * When compiling a heredoc assignment to a variable in a Vim9 def function,
740 * "vim9compile" is set to TRUE. In this case, instead of generating a list of
741 * string values from the heredoc, vim9 instructions are generated. On success
742 * the returned list will be empty.
743 *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100744 * Returns a List with {lines} or NULL on failure.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200745 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100746 list_T *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100747heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200748{
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100749 char_u *theline = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200750 char_u *marker;
751 list_T *l;
752 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100753 char_u *str;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200754 int marker_indent_len = 0;
755 int text_indent_len = 0;
756 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200757 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200758 int comment_char = in_vim9script() ? '#' : '"';
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100759 int evalstr = FALSE;
760 int eval_failed = FALSE;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100761 cctx_T *cctx = vim9compile ? eap->cookie : NULL;
762 int count = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200763
764 if (eap->getline == NULL)
765 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000766 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200767 return NULL;
768 }
769
770 // Check for the optional 'trim' word before the marker
771 cmd = skipwhite(cmd);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200772
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100773 while (TRUE)
774 {
775 if (STRNCMP(cmd, "trim", 4) == 0
776 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200777 {
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100778 cmd = skipwhite(cmd + 4);
779
780 // Trim the indentation from all the lines in the here document.
781 // The amount of indentation trimmed is the same as the indentation
782 // of the first line after the :let command line. To find the end
783 // marker the indent of the :let command line is trimmed.
784 p = *eap->cmdlinep;
785 while (VIM_ISWHITE(*p))
786 {
787 p++;
788 marker_indent_len++;
789 }
790 text_indent_len = -1;
791
792 continue;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200793 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100794 if (STRNCMP(cmd, "eval", 4) == 0
795 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
796 {
797 cmd = skipwhite(cmd + 4);
798 evalstr = TRUE;
799 continue;
800 }
801 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200802 }
803
804 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200805 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200806 {
807 marker = skipwhite(cmd);
808 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200809 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200810 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000811 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200812 return NULL;
813 }
814 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200815 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200816 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000817 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200818 return NULL;
819 }
820 }
821 else
822 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200823 // When getting lines for an embedded script, if the marker is missing,
824 // accept '.' as the marker.
825 if (script_get)
826 marker = dot;
827 else
828 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000829 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200830 return NULL;
831 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200832 }
833
834 l = list_alloc();
835 if (l == NULL)
836 return NULL;
837
838 for (;;)
839 {
840 int mi = 0;
841 int ti = 0;
842
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100843 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200844 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
845 if (theline == NULL)
846 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000847 semsg(_(e_missing_end_marker_str), marker);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200848 break;
849 }
850
851 // with "trim": skip the indent matching the :let line to find the
852 // marker
853 if (marker_indent_len > 0
854 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
855 mi = marker_indent_len;
856 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200857 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200858
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100859 // If expression evaluation failed in the heredoc, then skip till the
860 // end marker.
861 if (eval_failed)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100862 continue;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100863
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200864 if (text_indent_len == -1 && *theline != NUL)
865 {
866 // set the text indent from the first line.
867 p = theline;
868 text_indent_len = 0;
869 while (VIM_ISWHITE(*p))
870 {
871 p++;
872 text_indent_len++;
873 }
874 text_indent = vim_strnsave(theline, text_indent_len);
875 }
876 // with "trim": skip the indent matching the first line
877 if (text_indent != NULL)
878 for (ti = 0; ti < text_indent_len; ++ti)
879 if (theline[ti] != text_indent[ti])
880 break;
881
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100882 str = theline + ti;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100883 if (vim9compile)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100884 {
LemonBoy2eaef102022-05-06 13:14:50 +0100885 if (compile_all_expr_in_str(str, evalstr, cctx) == FAIL)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100886 {
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100887 vim_free(theline);
888 vim_free(text_indent);
889 return FAIL;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100890 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100891 count++;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100892 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100893 else
894 {
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100895 if (evalstr && !eap->skip)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100896 {
897 str = eval_all_expr_in_str(str);
898 if (str == NULL)
899 {
900 // expression evaluation failed
901 eval_failed = TRUE;
902 continue;
903 }
904 vim_free(theline);
905 theline = str;
906 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100907
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100908 if (list_append_string(l, str, -1) == FAIL)
909 break;
910 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200911 }
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100912 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200913 vim_free(text_indent);
914
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100915 if (vim9compile && cctx->ctx_skip != SKIP_YES && !eval_failed)
916 generate_NEWLIST(cctx, count, FALSE);
917
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100918 if (eval_failed)
919 {
920 // expression evaluation in the heredoc failed
921 list_free(l);
922 return NULL;
923 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200924 return l;
925}
926
927/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200928 * Vim9 variable declaration:
929 * ":var name"
930 * ":var name: type"
931 * ":var name = expr"
932 * ":var name: type = expr"
933 * etc.
934 */
935 void
936ex_var(exarg_T *eap)
937{
Bram Moolenaar330a3882022-03-05 11:05:57 +0000938 char_u *p = eap->cmd;
Bram Moolenaare1d12112022-03-05 11:37:48 +0000939 int has_var;
Bram Moolenaar330a3882022-03-05 11:05:57 +0000940
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200941 if (!in_vim9script())
942 {
943 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
944 return;
945 }
Bram Moolenaare1d12112022-03-05 11:37:48 +0000946 has_var = checkforcmd_noparen(&p, "var", 3);
947 if (current_sctx.sc_sid == 0 && has_var)
Bram Moolenaar0e1574c2022-03-03 17:05:35 +0000948 {
949 emsg(_(e_cannot_declare_variable_on_command_line));
950 return;
951 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200952 ex_let(eap);
953}
954
955/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200956 * ":let" list all variable values
957 * ":let var1 var2" list variable values
958 * ":let var = expr" assignment command.
959 * ":let var += expr" assignment command.
960 * ":let var -= expr" assignment command.
961 * ":let var *= expr" assignment command.
962 * ":let var /= expr" assignment command.
963 * ":let var %= expr" assignment command.
964 * ":let var .= expr" assignment command.
965 * ":let var ..= expr" assignment command.
966 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100967 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100968 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200969 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200970 * ":final var = expr" assignment command.
971 * ":final [var1, var2] = expr" unpack list.
972 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200973 * ":const" list all variable values
974 * ":const var1 var2" list variable values
975 * ":const var = expr" assignment command.
976 * ":const [var1, var2] = expr" unpack list.
977 */
978 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200979ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200980{
981 char_u *arg = eap->arg;
982 char_u *expr = NULL;
983 typval_T rettv;
984 int i;
985 int var_count = 0;
986 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200987 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200988 char_u *argend;
989 int first = TRUE;
990 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200991 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100992 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200993 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200994
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200995 if (eap->cmdidx == CMD_final && !vim9script)
996 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100997 // In legacy Vim script ":final" is short for ":finally".
998 ex_finally(eap);
999 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001000 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +02001001 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001002 {
1003 emsg(_(e_cannot_use_let_in_vim9_script));
1004 return;
1005 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001006
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001007 if (eap->cmdidx == CMD_const)
1008 flags |= ASSIGN_CONST;
1009 else if (eap->cmdidx == CMD_final)
1010 flags |= ASSIGN_FINAL;
1011
1012 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001014 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001015
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001016 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001017 if (argend == NULL)
1018 return;
1019 if (argend > arg && argend[-1] == '.') // for var.='str'
1020 --argend;
1021 expr = skipwhite(argend);
1022 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02001023 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001024 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +02001025 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
1026 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +02001027 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001028 {
1029 // ":let" without "=": list variables
1030 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001031 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001032 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001033 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001034 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001035 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001036 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001037 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001038 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001039 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001040 else
1041 // Vim9 declaration ":var name: type"
1042 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001043 }
1044 else
1045 {
1046 // ":let var1 var2" - list values
1047 arg = list_arg_vars(eap, arg, &first);
1048 }
1049 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001050 else if (!eap->skip)
1051 {
1052 // ":let"
1053 list_glob_vars(&first);
1054 list_buf_vars(&first);
1055 list_win_vars(&first);
1056 list_tab_vars(&first);
1057 list_script_vars(&first);
1058 list_func_vars(&first);
1059 list_vim_vars(&first);
1060 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02001061 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001062 }
1063 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
1064 {
1065 list_T *l;
Bram Moolenaar81530e32021-07-28 21:25:49 +02001066 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001067
1068 // HERE document
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001069 l = heredoc_get(eap, expr + 3, FALSE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001070 if (l != NULL)
1071 {
1072 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001073 if (!eap->skip)
1074 {
Bram Moolenaar81530e32021-07-28 21:25:49 +02001075 // errors are for the assignment, not the end marker
1076 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001077 op[0] = '=';
1078 op[1] = NUL;
1079 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001080 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001081 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001082 clear_tv(&rettv);
1083 }
1084 }
1085 else
1086 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001087 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001088 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001089
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02001090 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001091 i = FAIL;
1092 if (has_assign || concat)
1093 {
Bram Moolenaar9a562c12021-01-23 13:39:14 +01001094 int cur_lnum;
1095
Bram Moolenaar32e35112020-05-14 22:41:15 +02001096 op[0] = '=';
1097 op[1] = NUL;
1098 if (*expr != '=')
1099 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001100 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +02001101 {
1102 // +=, /=, etc. require an existing variable
1103 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
Bram Moolenaar122616d2020-08-21 21:32:50 +02001104 }
1105 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +02001106 {
1107 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001108 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001109 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001110 {
Bram Moolenaar32e35112020-05-14 22:41:15 +02001111 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001112 ++len;
1113 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02001114 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001115 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001116 }
1117 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001118 ++expr;
1119
Bram Moolenaar7f2c3412021-11-29 16:01:49 +00001120 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
Bram Moolenaarc7e44a72020-07-29 21:37:43 +02001121 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001122 {
1123 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01001124 semsg(_(e_white_space_required_before_and_after_str_at_str),
1125 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001126 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02001127
1128 if (eap->skip)
1129 ++emsg_skip;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001130 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaarc7e44a72020-07-29 21:37:43 +02001131 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaar9a562c12021-01-23 13:39:14 +01001132 cur_lnum = SOURCING_LNUM;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001133 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001134 if (eap->skip)
1135 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001136 clear_evalarg(&evalarg, eap);
Bram Moolenaar9a562c12021-01-23 13:39:14 +01001137
1138 // Restore the line number so that any type error is given for the
1139 // declaration, not the expression.
1140 SOURCING_LNUM = cur_lnum;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001141 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001142 if (eap->skip)
1143 {
1144 if (i != FAIL)
1145 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001146 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001147 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001148 {
1149 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001150 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001151 clear_tv(&rettv);
1152 }
1153 }
1154}
1155
1156/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001157 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001158 * Handles both "var" with any type and "[var, var; var]" with a list type.
1159 * When "op" is not NULL it points to a string with characters that
1160 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1161 * or concatenate.
1162 * Returns OK or FAIL;
1163 */
1164 int
1165ex_let_vars(
1166 char_u *arg_start,
1167 typval_T *tv,
1168 int copy, // copy values from "tv", don't move
1169 int semicolon, // from skip_var_list()
1170 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001171 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001172 char_u *op)
1173{
1174 char_u *arg = arg_start;
1175 list_T *l;
1176 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001177 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001178 listitem_T *item;
1179 typval_T ltv;
1180
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00001181 if (tv->v_type == VAR_VOID)
1182 {
1183 emsg(_(e_cannot_use_void_value));
1184 return FAIL;
1185 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001186 if (*arg != '[')
1187 {
1188 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001189 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001190 return FAIL;
1191 return OK;
1192 }
1193
1194 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1195 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
1196 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001197 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001198 return FAIL;
1199 }
1200
1201 i = list_len(l);
1202 if (semicolon == 0 && var_count < i)
1203 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001204 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001205 return FAIL;
1206 }
1207 if (var_count - semicolon > i)
1208 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001209 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001210 return FAIL;
1211 }
1212
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001213 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001214 item = l->lv_first;
1215 while (*arg != ']')
1216 {
1217 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001218 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001219 arg = ex_let_one(arg, &item->li_tv, TRUE,
1220 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001221 item = item->li_next;
1222 if (arg == NULL)
1223 return FAIL;
1224
1225 arg = skipwhite(arg);
1226 if (*arg == ';')
1227 {
1228 // Put the rest of the list (may be empty) in the var after ';'.
1229 // Create a new list for this.
1230 l = list_alloc();
1231 if (l == NULL)
1232 return FAIL;
1233 while (item != NULL)
1234 {
1235 list_append_tv(l, &item->li_tv);
1236 item = item->li_next;
1237 }
1238
1239 ltv.v_type = VAR_LIST;
1240 ltv.v_lock = 0;
1241 ltv.vval.v_list = l;
1242 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001243 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001244
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001245 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1246 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001247 clear_tv(&ltv);
1248 if (arg == NULL)
1249 return FAIL;
1250 break;
1251 }
1252 else if (*arg != ',' && *arg != ']')
1253 {
1254 internal_error("ex_let_vars()");
1255 return FAIL;
1256 }
1257 }
1258
1259 return OK;
1260}
1261
1262/*
1263 * Skip over assignable variable "var" or list of variables "[var, var]".
1264 * Used for ":let varvar = expr" and ":for varvar in expr".
1265 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001266 * for "[var, var; var]" set "semicolon" to 1.
1267 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001268 * Return NULL for an error.
1269 */
1270 char_u *
1271skip_var_list(
1272 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001274 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001275 int *semicolon,
1276 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001277{
1278 char_u *p, *s;
1279
1280 if (*arg == '[')
1281 {
1282 // "[var, var]": find the matching ']'.
1283 p = arg;
1284 for (;;)
1285 {
1286 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001287 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001288 if (s == p)
1289 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001290 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001291 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001292 return NULL;
1293 }
1294 ++*var_count;
1295
1296 p = skipwhite(s);
1297 if (*p == ']')
1298 break;
1299 else if (*p == ';')
1300 {
1301 if (*semicolon == 1)
1302 {
Bram Moolenaar8b716f52022-02-15 21:17:56 +00001303 if (!silent)
1304 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001305 return NULL;
1306 }
1307 *semicolon = 1;
1308 }
1309 else if (*p != ',')
1310 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001311 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001312 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001313 return NULL;
1314 }
1315 }
1316 return p + 1;
1317 }
Bram Moolenaare8e369a2022-09-21 18:59:14 +01001318
1319 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001320}
1321
1322/*
1323 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1324 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001325 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001326 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001327 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001329{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001330 char_u *end;
1331 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001333 if (*arg == '@' && arg[1] != NUL)
1334 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001336 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001337
1338 // "a: type" is declaring variable "a" with a type, not "a:".
1339 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001340 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001341 --end;
1342
Bram Moolenaar585587d2021-01-17 20:52:13 +01001343 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001345 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001346 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347 }
1348 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001349}
1350
1351/*
1352 * List variables for hashtab "ht" with prefix "prefix".
1353 * If "empty" is TRUE also list NULL strings as empty strings.
1354 */
1355 void
1356list_hashtable_vars(
1357 hashtab_T *ht,
1358 char *prefix,
1359 int empty,
1360 int *first)
1361{
1362 hashitem_T *hi;
1363 dictitem_T *di;
1364 int todo;
1365 char_u buf[IOSIZE];
1366
1367 todo = (int)ht->ht_used;
1368 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1369 {
1370 if (!HASHITEM_EMPTY(hi))
1371 {
1372 --todo;
1373 di = HI2DI(hi);
1374
1375 // apply :filter /pat/ to variable name
1376 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1377 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1378 if (message_filtered(buf))
1379 continue;
1380
1381 if (empty || di->di_tv.v_type != VAR_STRING
1382 || di->di_tv.vval.v_string != NULL)
1383 list_one_var(di, prefix, first);
1384 }
1385 }
1386}
1387
1388/*
1389 * List global variables.
1390 */
1391 static void
1392list_glob_vars(int *first)
1393{
1394 list_hashtable_vars(&globvarht, "", TRUE, first);
1395}
1396
1397/*
1398 * List buffer variables.
1399 */
1400 static void
1401list_buf_vars(int *first)
1402{
1403 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1404}
1405
1406/*
1407 * List window variables.
1408 */
1409 static void
1410list_win_vars(int *first)
1411{
1412 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1413}
1414
1415/*
1416 * List tab page variables.
1417 */
1418 static void
1419list_tab_vars(int *first)
1420{
1421 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1422}
1423
1424/*
1425 * List variables in "arg".
1426 */
1427 static char_u *
1428list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1429{
1430 int error = FALSE;
1431 int len;
1432 char_u *name;
1433 char_u *name_start;
1434 char_u *arg_subsc;
1435 char_u *tofree;
1436 typval_T tv;
1437
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001438 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001439 {
1440 if (error || eap->skip)
1441 {
1442 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1443 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1444 {
1445 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001446 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001447 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001448 break;
1449 }
1450 }
1451 else
1452 {
1453 // get_name_len() takes care of expanding curly braces
1454 name_start = name = arg;
1455 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1456 if (len <= 0)
1457 {
1458 // This is mainly to keep test 49 working: when expanding
1459 // curly braces fails overrule the exception error message.
1460 if (len < 0 && !aborting())
1461 {
1462 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001463 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001464 break;
1465 }
1466 error = TRUE;
1467 }
1468 else
1469 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001470 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001471 if (tofree != NULL)
1472 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001473 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001474 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001475 error = TRUE;
1476 else
1477 {
1478 // handle d.key, l[idx], f(expr)
1479 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001480 if (handle_subscript(&arg, name_start, &tv,
1481 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001482 error = TRUE;
1483 else
1484 {
1485 if (arg == arg_subsc && len == 2 && name[1] == ':')
1486 {
1487 switch (*name)
1488 {
1489 case 'g': list_glob_vars(first); break;
1490 case 'b': list_buf_vars(first); break;
1491 case 'w': list_win_vars(first); break;
1492 case 't': list_tab_vars(first); break;
1493 case 'v': list_vim_vars(first); break;
1494 case 's': list_script_vars(first); break;
1495 case 'l': list_func_vars(first); break;
1496 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001497 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001498 }
1499 }
1500 else
1501 {
1502 char_u numbuf[NUMBUFLEN];
1503 char_u *tf;
1504 int c;
1505 char_u *s;
1506
1507 s = echo_string(&tv, &tf, numbuf, 0);
1508 c = *arg;
1509 *arg = NUL;
1510 list_one_var_a("",
1511 arg == arg_subsc ? name : name_start,
1512 tv.v_type,
1513 s == NULL ? (char_u *)"" : s,
1514 first);
1515 *arg = c;
1516 vim_free(tf);
1517 }
1518 clear_tv(&tv);
1519 }
1520 }
1521 }
1522
1523 vim_free(tofree);
1524 }
1525
1526 arg = skipwhite(arg);
1527 }
1528
1529 return arg;
1530}
1531
1532/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001533 * Set an environment variable, part of ex_let_one().
1534 */
1535 static char_u *
1536ex_let_env(
1537 char_u *arg,
1538 typval_T *tv,
1539 int flags,
1540 char_u *endchars,
1541 char_u *op)
1542{
1543 char_u *arg_end = NULL;
1544 char_u *name;
1545 int len;
1546
1547 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1548 && (flags & ASSIGN_FOR_LOOP) == 0)
1549 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001550 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001551 return NULL;
1552 }
1553
1554 // Find the end of the name.
1555 ++arg;
1556 name = arg;
1557 len = get_env_len(&arg);
1558 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001559 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001560 else
1561 {
1562 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001563 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001564 else if (endchars != NULL
1565 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1566 emsg(_(e_unexpected_characters_in_let));
1567 else if (!check_secure())
1568 {
1569 char_u *tofree = NULL;
1570 int c1 = name[len];
1571 char_u *p;
1572
1573 name[len] = NUL;
1574 p = tv_get_string_chk(tv);
1575 if (p != NULL && op != NULL && *op == '.')
1576 {
1577 int mustfree = FALSE;
1578 char_u *s = vim_getenv(name, &mustfree);
1579
1580 if (s != NULL)
1581 {
1582 p = tofree = concat_str(s, p);
1583 if (mustfree)
1584 vim_free(s);
1585 }
1586 }
1587 if (p != NULL)
1588 {
1589 vim_setenv_ext(name, p);
1590 arg_end = arg;
1591 }
1592 name[len] = c1;
1593 vim_free(tofree);
1594 }
1595 }
1596 return arg_end;
1597}
1598
1599/*
1600 * Set an option, part of ex_let_one().
1601 */
1602 static char_u *
1603ex_let_option(
1604 char_u *arg,
1605 typval_T *tv,
1606 int flags,
1607 char_u *endchars,
1608 char_u *op)
1609{
1610 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001611 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001612 char_u *arg_end = NULL;
1613
1614 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1615 && (flags & ASSIGN_FOR_LOOP) == 0)
1616 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001617 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001618 return NULL;
1619 }
1620
1621 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001622 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001623 if (p == NULL || (endchars != NULL
1624 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1625 emsg(_(e_unexpected_characters_in_let));
1626 else
1627 {
1628 int c1;
1629 long n = 0;
1630 getoption_T opt_type;
1631 long numval;
1632 char_u *stringval = NULL;
1633 char_u *s = NULL;
1634 int failed = FALSE;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001635 int opt_p_flags;
1636 char_u *tofree = NULL;
Bram Moolenaar92c33eb2021-12-07 11:03:39 +00001637 char_u numbuf[NUMBUFLEN];
1638
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001639 c1 = *p;
1640 *p = NUL;
1641
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001642 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags,
1643 scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001644 if ((opt_type == gov_bool
1645 || opt_type == gov_number
1646 || opt_type == gov_hidden_bool
1647 || opt_type == gov_hidden_number)
1648 && (tv->v_type != VAR_STRING || !in_vim9script()))
1649 {
1650 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1651 // bool, possibly hidden
1652 n = (long)tv_get_bool(tv);
1653 else
1654 // number, possibly hidden
1655 n = (long)tv_get_number(tv);
1656 }
1657
Bram Moolenaaref082e12021-12-12 21:02:03 +00001658 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001659 || tv->v_type == VAR_FUNC))
1660 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001661 // If the option can be set to a function reference or a lambda
1662 // and the passed value is a function reference, then convert it to
1663 // the name (string) of the function reference.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001664 s = tv2string(tv, &tofree, numbuf, 0);
1665 }
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001666 // Avoid setting a string option to the text "v:false" or similar.
1667 // In Vim9 script also don't convert a number to string.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001668 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001669 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1670 s = tv_get_string_chk(tv);
1671
1672 if (op != NULL && *op != '=')
1673 {
1674 if (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1675 || (opt_type == gov_string && *op != '.'))
1676 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001677 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001678 failed = TRUE; // don't set the value
1679
1680 }
1681 else
1682 {
1683 // number, in legacy script also bool
1684 if (opt_type == gov_number
1685 || (opt_type == gov_bool && !in_vim9script()))
1686 {
1687 switch (*op)
1688 {
1689 case '+': n = numval + n; break;
1690 case '-': n = numval - n; break;
1691 case '*': n = numval * n; break;
1692 case '/': n = (long)num_divide(numval, n,
1693 &failed); break;
1694 case '%': n = (long)num_modulus(numval, n,
1695 &failed); break;
1696 }
1697 s = NULL;
1698 }
1699 else if (opt_type == gov_string
1700 && stringval != NULL && s != NULL)
1701 {
1702 // string
1703 s = concat_str(stringval, s);
1704 vim_free(stringval);
1705 stringval = s;
1706 }
1707 }
1708 }
1709
1710 if (!failed)
1711 {
1712 if (opt_type != gov_string || s != NULL)
1713 {
Bram Moolenaar8ccbbeb2022-03-02 19:49:38 +00001714 char *err = set_option_value(arg, n, s, scope);
1715
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001716 arg_end = p;
Bram Moolenaar8ccbbeb2022-03-02 19:49:38 +00001717 if (err != NULL)
1718 emsg(_(err));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001719 }
1720 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001721 emsg(_(e_string_required));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001722 }
1723 *p = c1;
1724 vim_free(stringval);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001725 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001726 }
1727 return arg_end;
1728}
1729
1730/*
1731 * Set a register, part of ex_let_one().
1732 */
1733 static char_u *
1734ex_let_register(
1735 char_u *arg,
1736 typval_T *tv,
1737 int flags,
1738 char_u *endchars,
1739 char_u *op)
1740{
1741 char_u *arg_end = NULL;
1742
1743 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1744 && (flags & ASSIGN_FOR_LOOP) == 0)
1745 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001746 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001747 return NULL;
1748 }
1749 ++arg;
1750 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001751 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001752 else if (endchars != NULL
1753 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1754 emsg(_(e_unexpected_characters_in_let));
1755 else
1756 {
1757 char_u *ptofree = NULL;
1758 char_u *p;
1759
1760 p = tv_get_string_chk(tv);
1761 if (p != NULL && op != NULL && *op == '.')
1762 {
1763 char_u *s = get_reg_contents(*arg == '@'
1764 ? '"' : *arg, GREG_EXPR_SRC);
1765
1766 if (s != NULL)
1767 {
1768 p = ptofree = concat_str(s, p);
1769 vim_free(s);
1770 }
1771 }
1772 if (p != NULL)
1773 {
1774 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1775 arg_end = arg + 1;
1776 }
1777 vim_free(ptofree);
1778 }
1779 return arg_end;
1780}
1781
1782/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001783 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1784 * Returns a pointer to the char just after the var name.
1785 * Returns NULL if there is an error.
1786 */
1787 static char_u *
1788ex_let_one(
1789 char_u *arg, // points to variable name
1790 typval_T *tv, // value to assign to variable
1791 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001792 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001793 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001794 char_u *op, // "+", "-", "." or NULL
1795 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001796{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001797 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001798
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001799 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001800 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001801 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1802 {
1803 vim9_declare_error(arg);
1804 return NULL;
1805 }
1806
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001807 if (*arg == '$')
1808 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001809 // ":let $VAR = expr": Set environment variable.
1810 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001811 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001812 else if (*arg == '&')
1813 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001814 // ":let &option = expr": Set option value.
1815 // ":let &l:option = expr": Set local option value.
1816 // ":let &g:option = expr": Set global option value.
1817 // ":for &ts in range(8)": Set option value for for loop
1818 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001819 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001820 else if (*arg == '@')
1821 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001822 // ":let @r = expr": Set register contents.
1823 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001824 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001825 else if (eval_isnamec1(*arg) || *arg == '{')
1826 {
1827 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001828 char_u *p;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001829 int lval_flags = (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1830 ? GLV_NO_DECL : 0;
1831 if (op != NULL && *op != '=')
1832 lval_flags |= GLV_ASSIGN_WITH_OP;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001833
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001834 // ":let var = expr": Set internal variable.
1835 // ":let var: type = expr": Set internal variable with type.
1836 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001837 p = get_lval(arg, tv, &lv, FALSE, FALSE, lval_flags, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001838 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001839 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840 if (endchars != NULL && vim_strchr(endchars,
1841 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001842 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001843 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001844 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001845 else
1846 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001847 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001848 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001849 }
1850 }
1851 clear_lval(&lv);
1852 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001853 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001854 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001855
1856 return arg_end;
1857}
1858
1859/*
1860 * ":unlet[!] var1 ... " command.
1861 */
1862 void
1863ex_unlet(exarg_T *eap)
1864{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001865 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001866}
1867
1868/*
1869 * ":lockvar" and ":unlockvar" commands
1870 */
1871 void
1872ex_lockvar(exarg_T *eap)
1873{
1874 char_u *arg = eap->arg;
1875 int deep = 2;
1876
1877 if (eap->forceit)
1878 deep = -1;
1879 else if (vim_isdigit(*arg))
1880 {
1881 deep = getdigits(&arg);
1882 arg = skipwhite(arg);
1883 }
1884
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001885 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001886}
1887
1888/*
1889 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001890 * Also used for Vim9 script. "callback" is invoked as:
1891 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001892 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001893 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001894ex_unletlock(
1895 exarg_T *eap,
1896 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001897 int deep,
1898 int glv_flags,
1899 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1900 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001901{
1902 char_u *arg = argstart;
1903 char_u *name_end;
1904 int error = FALSE;
1905 lval_T lv;
1906
1907 do
1908 {
1909 if (*arg == '$')
1910 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001911 lv.ll_name = arg;
1912 lv.ll_tv = NULL;
1913 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001914 if (get_env_len(&arg) == 0)
1915 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001916 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001917 return;
1918 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001919 if (!error && !eap->skip
1920 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1921 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001922 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001923 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001924 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001925 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001926 // Parse the name and find the end.
1927 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001928 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001929 if (lv.ll_name == NULL)
1930 error = TRUE; // error but continue parsing
1931 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1932 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001933 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001934 if (name_end != NULL)
1935 {
1936 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00001937 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001938 }
1939 if (!(eap->skip || error))
1940 clear_lval(&lv);
1941 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001942 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001943
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001944 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001945 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001946 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001947
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001948 if (!eap->skip)
1949 clear_lval(&lv);
1950 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001951
1952 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001953 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001954
Bram Moolenaar63b91732021-08-05 20:40:03 +02001955 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001956}
1957
1958 static int
1959do_unlet_var(
1960 lval_T *lp,
1961 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001962 exarg_T *eap,
1963 int deep UNUSED,
1964 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001965{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001966 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001967 int ret = OK;
1968 int cc;
1969
1970 if (lp->ll_tv == NULL)
1971 {
1972 cc = *name_end;
1973 *name_end = NUL;
1974
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001975 // Environment variable, normal name or expanded name.
1976 if (*lp->ll_name == '$')
LemonBoy77142312022-04-15 20:50:46 +01001977 vim_unsetenv_ext(lp->ll_name + 1);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001978 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001979 ret = FAIL;
1980 *name_end = cc;
1981 }
1982 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001983 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001984 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001985 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001986 return FAIL;
1987 else if (lp->ll_range)
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00001988 list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_n1,
1989 !lp->ll_empty2, lp->ll_n2);
1990 else if (lp->ll_list != NULL)
1991 // unlet a List item.
1992 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001993 else
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00001994 // unlet a Dictionary item.
1995 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001996
1997 return ret;
1998}
1999
2000/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002001 * Unlet one item or a range of items from a list.
2002 * Return OK or FAIL.
2003 */
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002004 void
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002005list_unlet_range(
2006 list_T *l,
2007 listitem_T *li_first,
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002008 long n1_arg,
2009 int has_n2,
2010 long n2)
2011{
2012 listitem_T *li = li_first;
2013 int n1 = n1_arg;
2014
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002015 // Delete a range of List items.
2016 li = li_first;
2017 n1 = n1_arg;
2018 while (li != NULL && (!has_n2 || n2 >= n1))
2019 {
2020 listitem_T *next = li->li_next;
2021
2022 listitem_remove(l, li);
2023 li = next;
2024 ++n1;
2025 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002026}
2027/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002028 * "unlet" a variable. Return OK if it existed, FAIL if not.
2029 * When "forceit" is TRUE don't complain if the variable doesn't exist.
2030 */
2031 int
2032do_unlet(char_u *name, int forceit)
2033{
2034 hashtab_T *ht;
2035 hashitem_T *hi;
mityu8be36ee2022-05-25 17:29:46 +01002036 char_u *varname = NULL; // init to shut up gcc
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002037 dict_T *d;
2038 dictitem_T *di;
2039
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002040 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002041 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002042 return FAIL;
2043
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002044 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002045
2046 // can't :unlet a script variable in Vim9 script from a function
2047 if (ht == get_script_local_ht()
2048 && SCRIPT_ID_VALID(current_sctx.sc_sid)
2049 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2050 == SCRIPT_VERSION_VIM9
2051 && check_vim9_unlet(name) == FAIL)
2052 return FAIL;
2053
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002054 if (ht != NULL && *varname != NUL)
2055 {
2056 d = get_current_funccal_dict(ht);
2057 if (d == NULL)
2058 {
2059 if (ht == &globvarht)
2060 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002061 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002062 d = &vimvardict;
2063 else
2064 {
2065 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2066 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2067 }
2068 if (d == NULL)
2069 {
2070 internal_error("do_unlet()");
2071 return FAIL;
2072 }
2073 }
2074 hi = hash_find(ht, varname);
2075 if (HASHITEM_EMPTY(hi))
2076 hi = find_hi_in_scoped_ht(name, &ht);
2077 if (hi != NULL && !HASHITEM_EMPTY(hi))
2078 {
2079 di = HI2DI(hi);
2080 if (var_check_fixed(di->di_flags, name, FALSE)
2081 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02002082 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002083 return FAIL;
2084
2085 delete_var(ht, hi);
2086 return OK;
2087 }
2088 }
2089 if (forceit)
2090 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00002091 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002092 return FAIL;
2093}
2094
2095/*
2096 * Lock or unlock variable indicated by "lp".
2097 * "deep" is the levels to go (-1 for unlimited);
2098 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2099 */
2100 static int
2101do_lock_var(
2102 lval_T *lp,
2103 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002104 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002105 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002106 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002107{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002108 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002109 int ret = OK;
2110 int cc;
2111 dictitem_T *di;
2112
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002113 if (lp->ll_tv == NULL)
2114 {
2115 cc = *name_end;
2116 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002117 if (*lp->ll_name == '$')
2118 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002119 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002120 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002121 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002122 else
2123 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002124 // Normal name or expanded name.
2125 di = find_var(lp->ll_name, NULL, TRUE);
2126 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002127 {
2128 if (in_vim9script())
2129 semsg(_(e_cannot_find_variable_to_unlock_str),
2130 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002131 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002132 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002133 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002134 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002135 if ((di->di_flags & DI_FLAGS_FIX)
2136 && di->di_tv.v_type != VAR_DICT
2137 && di->di_tv.v_type != VAR_LIST)
2138 {
2139 // For historic reasons this error is not given for a list
2140 // or dict. E.g., the b: dict could be locked/unlocked.
2141 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
2142 ret = FAIL;
2143 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002144 else
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002145 {
2146 if (in_vim9script())
2147 {
2148 svar_T *sv = find_typval_in_script(&di->di_tv,
2149 0, FALSE);
2150
2151 if (sv != NULL && sv->sv_const != 0)
2152 {
2153 semsg(_(e_cannot_change_readonly_variable_str),
2154 lp->ll_name);
2155 ret = FAIL;
2156 }
2157 }
2158
2159 if (ret == OK)
2160 {
2161 if (lock)
2162 di->di_flags |= DI_FLAGS_LOCK;
2163 else
2164 di->di_flags &= ~DI_FLAGS_LOCK;
2165 if (deep != 0)
2166 item_lock(&di->di_tv, deep, lock, FALSE);
2167 }
2168 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002169 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002170 }
2171 *name_end = cc;
2172 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002173 else if (deep == 0)
2174 {
2175 // nothing to do
2176 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002177 else if (lp->ll_range)
2178 {
2179 listitem_T *li = lp->ll_li;
2180
2181 // (un)lock a range of List items.
2182 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2183 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02002184 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002185 li = li->li_next;
2186 ++lp->ll_n1;
2187 }
2188 }
2189 else if (lp->ll_list != NULL)
2190 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002191 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002192 else
2193 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002194 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002195
2196 return ret;
2197}
2198
2199/*
2200 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002201 * When "check_refcount" is TRUE do not lock a list or dict with a reference
2202 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002203 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002204 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02002205item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002206{
2207 static int recurse = 0;
2208 list_T *l;
2209 listitem_T *li;
2210 dict_T *d;
2211 blob_T *b;
2212 hashitem_T *hi;
2213 int todo;
2214
2215 if (recurse >= DICT_MAXNEST)
2216 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002217 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002218 return;
2219 }
2220 if (deep == 0)
2221 return;
2222 ++recurse;
2223
2224 // lock/unlock the item itself
2225 if (lock)
2226 tv->v_lock |= VAR_LOCKED;
2227 else
2228 tv->v_lock &= ~VAR_LOCKED;
2229
2230 switch (tv->v_type)
2231 {
2232 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002233 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002234 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002235 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002236 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002237 case VAR_STRING:
2238 case VAR_FUNC:
2239 case VAR_PARTIAL:
2240 case VAR_FLOAT:
2241 case VAR_SPECIAL:
2242 case VAR_JOB:
2243 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002244 case VAR_INSTR:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002245 break;
2246
2247 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002248 if ((b = tv->vval.v_blob) != NULL
2249 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002250 {
2251 if (lock)
2252 b->bv_lock |= VAR_LOCKED;
2253 else
2254 b->bv_lock &= ~VAR_LOCKED;
2255 }
2256 break;
2257 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002258 if ((l = tv->vval.v_list) != NULL
2259 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002260 {
2261 if (lock)
2262 l->lv_lock |= VAR_LOCKED;
2263 else
2264 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002265 if (deep < 0 || deep > 1)
2266 {
2267 if (l->lv_first == &range_list_item)
2268 l->lv_lock |= VAR_ITEMS_LOCKED;
2269 else
2270 {
2271 // recursive: lock/unlock the items the List contains
2272 CHECK_LIST_MATERIALIZE(l);
2273 FOR_ALL_LIST_ITEMS(l, li) item_lock(&li->li_tv,
2274 deep - 1, lock, check_refcount);
2275 }
2276 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002277 }
2278 break;
2279 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002280 if ((d = tv->vval.v_dict) != NULL
2281 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002282 {
2283 if (lock)
2284 d->dv_lock |= VAR_LOCKED;
2285 else
2286 d->dv_lock &= ~VAR_LOCKED;
2287 if (deep < 0 || deep > 1)
2288 {
2289 // recursive: lock/unlock the items the List contains
2290 todo = (int)d->dv_hashtab.ht_used;
2291 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2292 {
2293 if (!HASHITEM_EMPTY(hi))
2294 {
2295 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002296 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2297 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002298 }
2299 }
2300 }
2301 }
2302 }
2303 --recurse;
2304}
2305
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002306#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2307/*
2308 * Delete all "menutrans_" variables.
2309 */
2310 void
2311del_menutrans_vars(void)
2312{
2313 hashitem_T *hi;
2314 int todo;
2315
2316 hash_lock(&globvarht);
2317 todo = (int)globvarht.ht_used;
2318 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2319 {
2320 if (!HASHITEM_EMPTY(hi))
2321 {
2322 --todo;
2323 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2324 delete_var(&globvarht, hi);
2325 }
2326 }
2327 hash_unlock(&globvarht);
2328}
2329#endif
2330
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002331/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002332 * Local string buffer for the next two functions to store a variable name
2333 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2334 * get_user_var_name().
2335 */
2336
2337static char_u *varnamebuf = NULL;
2338static int varnamebuflen = 0;
2339
2340/*
2341 * Function to concatenate a prefix and a variable name.
2342 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002343 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002344cat_prefix_varname(int prefix, char_u *name)
2345{
2346 int len;
2347
2348 len = (int)STRLEN(name) + 3;
2349 if (len > varnamebuflen)
2350 {
2351 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002352 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002353 varnamebuf = alloc(len);
2354 if (varnamebuf == NULL)
2355 {
2356 varnamebuflen = 0;
2357 return NULL;
2358 }
2359 varnamebuflen = len;
2360 }
2361 *varnamebuf = prefix;
2362 varnamebuf[1] = ':';
2363 STRCPY(varnamebuf + 2, name);
2364 return varnamebuf;
2365}
2366
2367/*
2368 * Function given to ExpandGeneric() to obtain the list of user defined
2369 * (global/buffer/window/built-in) variable names.
2370 */
2371 char_u *
2372get_user_var_name(expand_T *xp, int idx)
2373{
2374 static long_u gdone;
2375 static long_u bdone;
2376 static long_u wdone;
2377 static long_u tdone;
2378 static int vidx;
2379 static hashitem_T *hi;
2380 hashtab_T *ht;
2381
2382 if (idx == 0)
2383 {
2384 gdone = bdone = wdone = vidx = 0;
2385 tdone = 0;
2386 }
2387
2388 // Global variables
2389 if (gdone < globvarht.ht_used)
2390 {
2391 if (gdone++ == 0)
2392 hi = globvarht.ht_array;
2393 else
2394 ++hi;
2395 while (HASHITEM_EMPTY(hi))
2396 ++hi;
2397 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2398 return cat_prefix_varname('g', hi->hi_key);
2399 return hi->hi_key;
2400 }
2401
2402 // b: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002403 ht = &prevwin_curwin()->w_buffer->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002404 if (bdone < ht->ht_used)
2405 {
2406 if (bdone++ == 0)
2407 hi = ht->ht_array;
2408 else
2409 ++hi;
2410 while (HASHITEM_EMPTY(hi))
2411 ++hi;
2412 return cat_prefix_varname('b', hi->hi_key);
2413 }
2414
2415 // w: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002416 ht = &prevwin_curwin()->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002417 if (wdone < ht->ht_used)
2418 {
2419 if (wdone++ == 0)
2420 hi = ht->ht_array;
2421 else
2422 ++hi;
2423 while (HASHITEM_EMPTY(hi))
2424 ++hi;
2425 return cat_prefix_varname('w', hi->hi_key);
2426 }
2427
2428 // t: variables
2429 ht = &curtab->tp_vars->dv_hashtab;
2430 if (tdone < ht->ht_used)
2431 {
2432 if (tdone++ == 0)
2433 hi = ht->ht_array;
2434 else
2435 ++hi;
2436 while (HASHITEM_EMPTY(hi))
2437 ++hi;
2438 return cat_prefix_varname('t', hi->hi_key);
2439 }
2440
2441 // v: variables
2442 if (vidx < VV_LEN)
2443 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2444
2445 VIM_CLEAR(varnamebuf);
2446 varnamebuflen = 0;
2447 return NULL;
2448}
2449
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002450 char *
2451get_var_special_name(int nr)
2452{
2453 switch (nr)
2454 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002455 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2456 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002457 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002458 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002459 }
2460 internal_error("get_var_special_name()");
2461 return "42";
2462}
2463
2464/*
2465 * Returns the global variable dictionary
2466 */
2467 dict_T *
2468get_globvar_dict(void)
2469{
2470 return &globvardict;
2471}
2472
2473/*
2474 * Returns the global variable hash table
2475 */
2476 hashtab_T *
2477get_globvar_ht(void)
2478{
2479 return &globvarht;
2480}
2481
2482/*
2483 * Returns the v: variable dictionary
2484 */
2485 dict_T *
2486get_vimvar_dict(void)
2487{
2488 return &vimvardict;
2489}
2490
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002491/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002493 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494 */
2495 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002496find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002498 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2499 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500
2501 if (di == NULL)
2502 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002503 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002504 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2505 return (int)(vv - vimvars);
2506}
2507
2508
2509/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002510 * Set type of v: variable to "type".
2511 */
2512 void
2513set_vim_var_type(int idx, vartype_T type)
2514{
Bram Moolenaard787e402021-12-24 21:36:12 +00002515 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002516}
2517
2518/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002519 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002520 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002521 */
2522 void
2523set_vim_var_nr(int idx, varnumber_T val)
2524{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002525 vimvars[idx].vv_nr = val;
2526}
2527
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 char *
2529get_vim_var_name(int idx)
2530{
2531 return vimvars[idx].vv_name;
2532}
2533
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002534/*
2535 * Get typval_T v: variable value.
2536 */
2537 typval_T *
2538get_vim_var_tv(int idx)
2539{
2540 return &vimvars[idx].vv_tv;
2541}
2542
Bram Moolenaard787e402021-12-24 21:36:12 +00002543 type_T *
2544get_vim_var_type(int idx, garray_T *type_list)
2545{
2546 if (vimvars[idx].vv_type != NULL)
2547 return vimvars[idx].vv_type;
2548 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2549}
2550
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002551/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002552 * Set v: variable to "tv". Only accepts the same type.
2553 * Takes over the value of "tv".
2554 */
2555 int
2556set_vim_var_tv(int idx, typval_T *tv)
2557{
Bram Moolenaard787e402021-12-24 21:36:12 +00002558 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002559 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002560 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002561 clear_tv(tv);
2562 return FAIL;
2563 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002564 // VV_RO is also checked when compiling, but let's check here as well.
2565 if (vimvars[idx].vv_flags & VV_RO)
2566 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002567 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002568 return FAIL;
2569 }
2570 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2571 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002572 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002573 return FAIL;
2574 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002575 clear_tv(&vimvars[idx].vv_di.di_tv);
2576 vimvars[idx].vv_di.di_tv = *tv;
2577 return OK;
2578}
2579
2580/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002581 * Get number v: variable value.
2582 */
2583 varnumber_T
2584get_vim_var_nr(int idx)
2585{
2586 return vimvars[idx].vv_nr;
2587}
2588
2589/*
2590 * Get string v: variable value. Uses a static buffer, can only be used once.
2591 * If the String variable has never been set, return an empty string.
2592 * Never returns NULL;
2593 */
2594 char_u *
2595get_vim_var_str(int idx)
2596{
2597 return tv_get_string(&vimvars[idx].vv_tv);
2598}
2599
2600/*
2601 * Get List v: variable value. Caller must take care of reference count when
2602 * needed.
2603 */
2604 list_T *
2605get_vim_var_list(int idx)
2606{
2607 return vimvars[idx].vv_list;
2608}
2609
2610/*
2611 * Get Dict v: variable value. Caller must take care of reference count when
2612 * needed.
2613 */
2614 dict_T *
2615get_vim_var_dict(int idx)
2616{
2617 return vimvars[idx].vv_dict;
2618}
2619
2620/*
2621 * Set v:char to character "c".
2622 */
2623 void
2624set_vim_var_char(int c)
2625{
2626 char_u buf[MB_MAXBYTES + 1];
2627
2628 if (has_mbyte)
2629 buf[(*mb_char2bytes)(c, buf)] = NUL;
2630 else
2631 {
2632 buf[0] = c;
2633 buf[1] = NUL;
2634 }
2635 set_vim_var_string(VV_CHAR, buf, -1);
2636}
2637
2638/*
2639 * Set v:count to "count" and v:count1 to "count1".
2640 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2641 */
2642 void
2643set_vcount(
2644 long count,
2645 long count1,
2646 int set_prevcount)
2647{
2648 if (set_prevcount)
2649 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2650 vimvars[VV_COUNT].vv_nr = count;
2651 vimvars[VV_COUNT1].vv_nr = count1;
2652}
2653
2654/*
2655 * Save variables that might be changed as a side effect. Used when executing
2656 * a timer callback.
2657 */
2658 void
2659save_vimvars(vimvars_save_T *vvsave)
2660{
2661 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2662 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2663 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2664}
2665
2666/*
2667 * Restore variables saved by save_vimvars().
2668 */
2669 void
2670restore_vimvars(vimvars_save_T *vvsave)
2671{
2672 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2673 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2674 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2675}
2676
2677/*
2678 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2679 * value.
2680 */
2681 void
2682set_vim_var_string(
2683 int idx,
2684 char_u *val,
2685 int len) // length of "val" to use or -1 (whole string)
2686{
2687 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002688 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002689 if (val == NULL)
2690 vimvars[idx].vv_str = NULL;
2691 else if (len == -1)
2692 vimvars[idx].vv_str = vim_strsave(val);
2693 else
2694 vimvars[idx].vv_str = vim_strnsave(val, len);
2695}
2696
2697/*
2698 * Set List v: variable to "val".
2699 */
2700 void
2701set_vim_var_list(int idx, list_T *val)
2702{
2703 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002704 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002705 vimvars[idx].vv_list = val;
2706 if (val != NULL)
2707 ++val->lv_refcount;
2708}
2709
2710/*
2711 * Set Dictionary v: variable to "val".
2712 */
2713 void
2714set_vim_var_dict(int idx, dict_T *val)
2715{
2716 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002717 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002718 vimvars[idx].vv_dict = val;
2719 if (val != NULL)
2720 {
2721 ++val->dv_refcount;
2722 dict_set_items_ro(val);
2723 }
2724}
2725
2726/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002727 * Set the v:argv list.
2728 */
2729 void
2730set_argv_var(char **argv, int argc)
2731{
2732 list_T *l = list_alloc();
2733 int i;
2734
2735 if (l == NULL)
2736 getout(1);
2737 l->lv_lock = VAR_FIXED;
2738 for (i = 0; i < argc; ++i)
2739 {
2740 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2741 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002742 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002743 }
2744 set_vim_var_list(VV_ARGV, l);
2745}
2746
2747/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002748 * Reset v:register, taking the 'clipboard' setting into account.
2749 */
2750 void
2751reset_reg_var(void)
2752{
2753 int regname = 0;
2754
2755 // Adjust the register according to 'clipboard', so that when
2756 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2757#ifdef FEAT_CLIPBOARD
2758 adjust_clip_reg(&regname);
2759#endif
2760 set_reg_var(regname);
2761}
2762
2763/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002764 * Set v:register if needed.
2765 */
2766 void
2767set_reg_var(int c)
2768{
2769 char_u regname;
2770
2771 if (c == 0 || c == ' ')
2772 regname = '"';
2773 else
2774 regname = c;
2775 // Avoid free/alloc when the value is already right.
2776 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2777 set_vim_var_string(VV_REG, &regname, 1);
2778}
2779
2780/*
2781 * Get or set v:exception. If "oldval" == NULL, return the current value.
2782 * Otherwise, restore the value to "oldval" and return NULL.
2783 * Must always be called in pairs to save and restore v:exception! Does not
2784 * take care of memory allocations.
2785 */
2786 char_u *
2787v_exception(char_u *oldval)
2788{
2789 if (oldval == NULL)
2790 return vimvars[VV_EXCEPTION].vv_str;
2791
2792 vimvars[VV_EXCEPTION].vv_str = oldval;
2793 return NULL;
2794}
2795
2796/*
2797 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2798 * Otherwise, restore the value to "oldval" and return NULL.
2799 * Must always be called in pairs to save and restore v:throwpoint! Does not
2800 * take care of memory allocations.
2801 */
2802 char_u *
2803v_throwpoint(char_u *oldval)
2804{
2805 if (oldval == NULL)
2806 return vimvars[VV_THROWPOINT].vv_str;
2807
2808 vimvars[VV_THROWPOINT].vv_str = oldval;
2809 return NULL;
2810}
2811
2812/*
2813 * Set v:cmdarg.
2814 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2815 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2816 * Must always be called in pairs!
2817 */
2818 char_u *
2819set_cmdarg(exarg_T *eap, char_u *oldarg)
2820{
2821 char_u *oldval;
2822 char_u *newval;
2823 unsigned len;
2824
2825 oldval = vimvars[VV_CMDARG].vv_str;
2826 if (eap == NULL)
2827 {
2828 vim_free(oldval);
2829 vimvars[VV_CMDARG].vv_str = oldarg;
2830 return NULL;
2831 }
2832
2833 if (eap->force_bin == FORCE_BIN)
2834 len = 6;
2835 else if (eap->force_bin == FORCE_NOBIN)
2836 len = 8;
2837 else
2838 len = 0;
2839
2840 if (eap->read_edit)
2841 len += 7;
2842
2843 if (eap->force_ff != 0)
2844 len += 10; // " ++ff=unix"
2845 if (eap->force_enc != 0)
2846 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2847 if (eap->bad_char != 0)
2848 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2849
2850 newval = alloc(len + 1);
2851 if (newval == NULL)
2852 return NULL;
2853
2854 if (eap->force_bin == FORCE_BIN)
2855 sprintf((char *)newval, " ++bin");
2856 else if (eap->force_bin == FORCE_NOBIN)
2857 sprintf((char *)newval, " ++nobin");
2858 else
2859 *newval = NUL;
2860
2861 if (eap->read_edit)
2862 STRCAT(newval, " ++edit");
2863
2864 if (eap->force_ff != 0)
2865 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2866 eap->force_ff == 'u' ? "unix"
2867 : eap->force_ff == 'd' ? "dos"
2868 : "mac");
2869 if (eap->force_enc != 0)
2870 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2871 eap->cmd + eap->force_enc);
2872 if (eap->bad_char == BAD_KEEP)
2873 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2874 else if (eap->bad_char == BAD_DROP)
2875 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2876 else if (eap->bad_char != 0)
2877 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2878 vimvars[VV_CMDARG].vv_str = newval;
2879 return oldval;
2880}
2881
2882/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002883 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002884 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2885 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002886 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2887 */
2888 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002889eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002890 char_u *name,
2891 int len, // length of "name"
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002892 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002893 typval_T *rettv, // NULL when only checking existence
2894 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002895 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002896{
2897 int ret = OK;
2898 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002899 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002900 hashtab_T *ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002901 int cc;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002902 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002903
2904 // truncate the name, so that we can use strcmp()
2905 cc = name[len];
2906 name[len] = NUL;
2907
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002908 // Check for local variable when debugging.
2909 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002910 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002911 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002912 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2913
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002914 if (v != NULL)
2915 {
2916 tv = &v->di_tv;
2917 if (dip != NULL)
2918 *dip = v;
2919 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002920 else
2921 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002922 }
2923
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002924 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002926 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002927 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2928
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002929 if (sid == 0)
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00002930 import = find_imported(p, 0, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931
2932 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002933 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002934 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002935 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002936 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00002937 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02002938 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002939 ht = &SCRIPT_VARS(sid);
2940 if (ht != NULL)
2941 {
2942 dictitem_T *v = find_var_in_ht(ht, 0, name,
2943 flags & EVAL_VAR_NOAUTOLOAD);
2944
2945 if (v != NULL)
2946 {
2947 tv = &v->di_tv;
2948 if (dip != NULL)
2949 *dip = v;
2950 }
2951 else
2952 ht = NULL;
2953 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002954 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002955 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002956 {
2957 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00002958 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002959 ret = FAIL;
2960 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002961 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002962 else
2963 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002964 if (rettv != NULL)
2965 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01002966 // special value that is used in handle_subscript()
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002967 rettv->v_type = VAR_ANY;
2968 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
2969 }
2970 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002971 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00002973 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002974 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002975 int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002976 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002977
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002978 // In Vim9 script we can get a function reference by using the
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002979 // function name. For a global non-autoload function "g:" is
2980 // required.
2981 if (ufunc != NULL && (has_g_prefix
2982 || !func_requires_g_prefix(ufunc)))
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002983 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002984 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002985 if (rettv != NULL)
2986 {
2987 rettv->v_type = VAR_FUNC;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002988 if (has_g_prefix)
Bram Moolenaaref082e12021-12-12 21:02:03 +00002989 // Keep the "g:", otherwise script-local may be
2990 // assumed.
2991 rettv->vval.v_string = vim_strsave(name);
2992 else
2993 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002994 if (rettv->vval.v_string != NULL)
2995 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002996 }
2997 }
2998 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 }
3000
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003001 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003002 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02003003 if (tv == NULL)
3004 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003005 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003006 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02003007 ret = FAIL;
3008 }
3009 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003010 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003011 svar_T *sv = NULL;
3012 int was_assigned = FALSE;
3013
Bram Moolenaar11005b02021-07-11 20:59:00 +02003014 if (ht != NULL && ht == get_script_local_ht()
3015 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02003016 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003017 sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaarf055d452021-07-08 20:57:24 +02003018 if (sv != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003019 {
Bram Moolenaarf055d452021-07-08 20:57:24 +02003020 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003021 was_assigned = sv->sv_flags & SVFLAG_ASSIGNED;
3022 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003023 }
3024
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003025 // If a list or dict variable wasn't initialized and has meaningful
3026 // type, do it now. Not for global variables, they are not
3027 // declared.
Bram Moolenaar7a222242022-03-01 19:23:24 +00003028 if (ht != &globvarht)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003029 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003030 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003031 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003032 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003033 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003034 tv->vval.v_dict = dict_alloc();
3035 if (tv->vval.v_dict != NULL)
3036 {
3037 ++tv->vval.v_dict->dv_refcount;
3038 tv->vval.v_dict->dv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003039 if (sv != NULL)
3040 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003041 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003042 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003043 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003044 && ((type != NULL && !was_assigned)
Bram Moolenaar501f9782022-03-27 16:51:04 +01003045 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003046 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003047 tv->vval.v_list = list_alloc();
3048 if (tv->vval.v_list != NULL)
3049 {
3050 ++tv->vval.v_list->lv_refcount;
3051 tv->vval.v_list->lv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003052 if (sv != NULL)
3053 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003054 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003055 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003056 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003057 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003058 || !in_vim9script()))
3059 {
3060 tv->vval.v_blob = blob_alloc();
3061 if (tv->vval.v_blob != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003062 {
Bram Moolenaar859cc212022-03-28 15:22:35 +01003063 ++tv->vval.v_blob->bv_refcount;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003064 if (sv != NULL)
3065 sv->sv_flags |= SVFLAG_ASSIGNED;
3066 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003067 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02003068 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003069 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003070 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003071 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003072
3073 name[len] = cc;
3074
3075 return ret;
3076}
3077
3078/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003079 * Check if variable "name[len]" is a local variable or an argument.
3080 * If so, "*eval_lavars_used" is set to TRUE.
3081 */
3082 void
3083check_vars(char_u *name, int len)
3084{
3085 int cc;
3086 char_u *varname;
3087 hashtab_T *ht;
3088
3089 if (eval_lavars_used == NULL)
3090 return;
3091
3092 // truncate the name, so that we can use strcmp()
3093 cc = name[len];
3094 name[len] = NUL;
3095
3096 ht = find_var_ht(name, &varname);
3097 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
3098 {
3099 if (find_var(name, NULL, TRUE) != NULL)
3100 *eval_lavars_used = TRUE;
3101 }
3102
3103 name[len] = cc;
3104}
3105
3106/*
3107 * Find variable "name" in the list of variables.
3108 * Return a pointer to it if found, NULL if not found.
3109 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003110 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003111 */
3112 dictitem_T *
3113find_var(char_u *name, hashtab_T **htp, int no_autoload)
3114{
3115 char_u *varname;
3116 hashtab_T *ht;
3117 dictitem_T *ret = NULL;
3118
3119 ht = find_var_ht(name, &varname);
3120 if (htp != NULL)
3121 *htp = ht;
3122 if (ht == NULL)
3123 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003124 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003125 if (ret != NULL)
3126 return ret;
3127
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003128 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003129 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003130 if (ret != NULL)
3131 return ret;
3132
3133 // in Vim9 script items without a scope can be script-local
3134 if (in_vim9script() && name[0] != NUL && name[1] != ':')
3135 {
3136 ht = get_script_local_ht();
3137 if (ht != NULL)
3138 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003139 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003140 if (ret != NULL)
3141 {
3142 if (htp != NULL)
3143 *htp = ht;
3144 return ret;
3145 }
3146 }
3147 }
3148
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003149 // When using "vim9script autoload" script-local items are prefixed but can
3150 // be used with s:name.
3151 if (SCRIPT_ID_VALID(current_sctx.sc_sid)
3152 && name[0] == 's' && name[1] == ':')
3153 {
3154 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
3155
3156 if (si->sn_autoload_prefix != NULL)
3157 {
3158 char_u *auto_name = concat_str(si->sn_autoload_prefix, name + 2);
3159
3160 if (auto_name != NULL)
3161 {
3162 ht = &globvarht;
3163 ret = find_var_in_ht(ht, *name, auto_name, TRUE);
Bram Moolenaar130f65d2022-01-13 20:39:41 +00003164 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003165 if (ret != NULL)
3166 {
3167 if (htp != NULL)
3168 *htp = ht;
3169 return ret;
3170 }
3171 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003172 }
3173 }
3174
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003175 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003176}
3177
3178/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00003179 * Like find_var() but if the name starts with <SNR>99_ then look in the
3180 * referenced script (used for a funcref).
3181 */
3182 dictitem_T *
3183find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
3184{
3185 if (STRNCMP(name, "<SNR>", 5) == 0 && isdigit(name[5]))
3186 {
3187 char_u *p = name + 5;
3188 int sid = getdigits(&p);
3189
3190 if (SCRIPT_ID_VALID(sid) && *p == '_')
3191 {
3192 hashtab_T *ht = &SCRIPT_VARS(sid);
3193
3194 if (ht != NULL)
3195 {
3196 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
3197
3198 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003199 {
3200 if (htp != NULL)
3201 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00003202 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003203 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00003204 }
3205 }
3206 }
3207
3208 return find_var(name, htp, no_autoload);
3209}
3210
3211/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003212 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02003213 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003214 * Returns NULL if not found.
3215 */
3216 dictitem_T *
3217find_var_in_ht(
3218 hashtab_T *ht,
3219 int htname,
3220 char_u *varname,
3221 int no_autoload)
3222{
3223 hashitem_T *hi;
3224
3225 if (*varname == NUL)
3226 {
3227 // Must be something like "s:", otherwise "ht" would be NULL.
3228 switch (htname)
3229 {
3230 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
3231 case 'g': return &globvars_var;
3232 case 'v': return &vimvars_var;
3233 case 'b': return &curbuf->b_bufvar;
3234 case 'w': return &curwin->w_winvar;
3235 case 't': return &curtab->tp_winvar;
3236 case 'l': return get_funccal_local_var();
3237 case 'a': return get_funccal_args_var();
3238 }
3239 return NULL;
3240 }
3241
3242 hi = hash_find(ht, varname);
3243 if (HASHITEM_EMPTY(hi))
3244 {
3245 // For global variables we may try auto-loading the script. If it
3246 // worked find the variable again. Don't auto-load a script if it was
3247 // loaded already, otherwise it would be loaded every time when
3248 // checking if a function name is a Funcref variable.
3249 if (ht == &globvarht && !no_autoload)
3250 {
3251 // Note: script_autoload() may make "hi" invalid. It must either
3252 // be obtained again or not used.
3253 if (!script_autoload(varname, FALSE) || aborting())
3254 return NULL;
3255 hi = hash_find(ht, varname);
3256 }
3257 if (HASHITEM_EMPTY(hi))
3258 return NULL;
3259 }
3260 return HI2DI(hi);
3261}
3262
3263/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 * Get the script-local hashtab. NULL if not in a script context.
3265 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003266 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267get_script_local_ht(void)
3268{
3269 scid_T sid = current_sctx.sc_sid;
3270
Bram Moolenaare3d46852020-08-29 13:39:17 +02003271 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 return &SCRIPT_VARS(sid);
3273 return NULL;
3274}
3275
3276/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003277 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003278 * When "cmd" is TRUE it must look like a command, a function must be followed
3279 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003280 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003282 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003283lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003284 char_u *name,
3285 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003286 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003287 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288{
3289 hashtab_T *ht = get_script_local_ht();
3290 char_u buffer[30];
3291 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003292 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003294 int is_global = FALSE;
3295 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296
3297 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003298 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 if (len < sizeof(buffer) - 1)
3300 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003301 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 vim_strncpy(buffer, name, len);
3303 p = buffer;
3304 }
3305 else
3306 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003307 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003309 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 }
3311
3312 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003313 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314
3315 // if not script-local, then perhaps imported
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003316 if (res == FAIL && find_imported(p, 0, FALSE) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003317 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 if (p != buffer)
3319 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003320
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003321 // Find a function, so that a following "->" works.
3322 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3323 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003324 if (res != OK)
3325 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003326 p = skipwhite(name + len);
3327
3328 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003329 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003330 // Do not check for an internal function, since it might also be a
3331 // valid command, such as ":split" versus "split()".
3332 // Skip "g:" before a function name.
3333 if (name[0] == 'g' && name[1] == ':')
3334 {
3335 is_global = TRUE;
3336 fname = name + 2;
3337 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003338 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003339 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003340 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003341 }
3342
Bram Moolenaar709664c2020-12-12 14:33:41 +01003343 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344}
3345
3346/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003347 * Find the hashtab used for a variable name.
3348 * Return NULL if the name is not valid.
3349 * Set "varname" to the start of name without ':'.
3350 */
3351 hashtab_T *
3352find_var_ht(char_u *name, char_u **varname)
3353{
3354 hashitem_T *hi;
3355 hashtab_T *ht;
3356
3357 if (name[0] == NUL)
3358 return NULL;
3359 if (name[1] != ':')
3360 {
3361 // The name must not start with a colon or #.
3362 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3363 return NULL;
3364 *varname = name;
3365
3366 // "version" is "v:version" in all scopes if scriptversion < 3.
3367 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003368 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003369 {
3370 hi = hash_find(&compat_hashtab, name);
3371 if (!HASHITEM_EMPTY(hi))
3372 return &compat_hashtab;
3373 }
3374
3375 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 if (ht != NULL)
3377 return ht; // local variable
3378
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003379 // In Vim9 script items at the script level are script-local, except
3380 // for autoload names.
3381 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 {
3383 ht = get_script_local_ht();
3384 if (ht != NULL)
3385 return ht;
3386 }
3387
3388 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003389 }
3390 *varname = name + 2;
3391 if (*name == 'g') // global variable
3392 return &globvarht;
3393 // There must be no ':' or '#' in the rest of the name, unless g: is used
3394 if (vim_strchr(name + 2, ':') != NULL
3395 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3396 return NULL;
3397 if (*name == 'b') // buffer variable
3398 return &curbuf->b_vars->dv_hashtab;
3399 if (*name == 'w') // window variable
3400 return &curwin->w_vars->dv_hashtab;
3401 if (*name == 't') // tab page variable
3402 return &curtab->tp_vars->dv_hashtab;
3403 if (*name == 'v') // v: variable
3404 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003405 if (get_current_funccal() != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01003406 && get_current_funccal()->fc_func->uf_def_status
3407 == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003409 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 if (*name == 'a') // a: function argument
3411 return get_funccal_args_ht();
3412 if (*name == 'l') // l: local function variable
3413 return get_funccal_local_ht();
3414 }
3415 if (*name == 's') // script variable
3416 {
3417 ht = get_script_local_ht();
3418 if (ht != NULL)
3419 return ht;
3420 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003421 return NULL;
3422}
3423
3424/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003425 * Get the string value of a (global/local) variable.
3426 * Note: see tv_get_string() for how long the pointer remains valid.
3427 * Returns NULL when it doesn't exist.
3428 */
3429 char_u *
3430get_var_value(char_u *name)
3431{
3432 dictitem_T *v;
3433
3434 v = find_var(name, NULL, FALSE);
3435 if (v == NULL)
3436 return NULL;
3437 return tv_get_string(&v->di_tv);
3438}
3439
3440/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003441 * Allocate a new hashtab for a sourced script. It will be used while
3442 * sourcing this script and when executing functions defined in the script.
3443 */
3444 void
3445new_script_vars(scid_T id)
3446{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003447 scriptvar_T *sv;
3448
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003449 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3450 if (sv == NULL)
3451 return;
3452 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003453 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003454}
3455
3456/*
3457 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3458 * point to it.
3459 */
3460 void
3461init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3462{
3463 hash_init(&dict->dv_hashtab);
3464 dict->dv_lock = 0;
3465 dict->dv_scope = scope;
3466 dict->dv_refcount = DO_NOT_FREE_CNT;
3467 dict->dv_copyID = 0;
3468 dict_var->di_tv.vval.v_dict = dict;
3469 dict_var->di_tv.v_type = VAR_DICT;
3470 dict_var->di_tv.v_lock = VAR_FIXED;
3471 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3472 dict_var->di_key[0] = NUL;
3473}
3474
3475/*
3476 * Unreference a dictionary initialized by init_var_dict().
3477 */
3478 void
3479unref_var_dict(dict_T *dict)
3480{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003481 // Now the dict needs to be freed if no one else is using it, go back to
3482 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003483 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3484 dict_unref(dict);
3485}
3486
3487/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003488 * Clean up a list of internal variables.
3489 * Frees all allocated variables and the value they contain.
3490 * Clears hashtab "ht", does not free it.
3491 */
3492 void
3493vars_clear(hashtab_T *ht)
3494{
3495 vars_clear_ext(ht, TRUE);
3496}
3497
3498/*
3499 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3500 */
3501 void
3502vars_clear_ext(hashtab_T *ht, int free_val)
3503{
3504 int todo;
3505 hashitem_T *hi;
3506 dictitem_T *v;
3507
3508 hash_lock(ht);
3509 todo = (int)ht->ht_used;
3510 for (hi = ht->ht_array; todo > 0; ++hi)
3511 {
3512 if (!HASHITEM_EMPTY(hi))
3513 {
3514 --todo;
3515
3516 // Free the variable. Don't remove it from the hashtab,
3517 // ht_array might change then. hash_clear() takes care of it
3518 // later.
3519 v = HI2DI(hi);
3520 if (free_val)
3521 clear_tv(&v->di_tv);
3522 if (v->di_flags & DI_FLAGS_ALLOC)
3523 vim_free(v);
3524 }
3525 }
3526 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003527 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003528}
3529
3530/*
3531 * Delete a variable from hashtab "ht" at item "hi".
3532 * Clear the variable value and free the dictitem.
3533 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003534 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003535delete_var(hashtab_T *ht, hashitem_T *hi)
3536{
3537 dictitem_T *di = HI2DI(hi);
3538
3539 hash_remove(ht, hi);
3540 clear_tv(&di->di_tv);
3541 vim_free(di);
3542}
3543
3544/*
3545 * List the value of one internal variable.
3546 */
3547 static void
3548list_one_var(dictitem_T *v, char *prefix, int *first)
3549{
3550 char_u *tofree;
3551 char_u *s;
3552 char_u numbuf[NUMBUFLEN];
3553
3554 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3555 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3556 s == NULL ? (char_u *)"" : s, first);
3557 vim_free(tofree);
3558}
3559
3560 static void
3561list_one_var_a(
3562 char *prefix,
3563 char_u *name,
3564 int type,
3565 char_u *string,
3566 int *first) // when TRUE clear rest of screen and set to FALSE
3567{
3568 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3569 msg_start();
3570 msg_puts(prefix);
3571 if (name != NULL) // "a:" vars don't have a name stored
3572 msg_puts((char *)name);
3573 msg_putchar(' ');
3574 msg_advance(22);
3575 if (type == VAR_NUMBER)
3576 msg_putchar('#');
3577 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3578 msg_putchar('*');
3579 else if (type == VAR_LIST)
3580 {
3581 msg_putchar('[');
3582 if (*string == '[')
3583 ++string;
3584 }
3585 else if (type == VAR_DICT)
3586 {
3587 msg_putchar('{');
3588 if (*string == '{')
3589 ++string;
3590 }
3591 else
3592 msg_putchar(' ');
3593
3594 msg_outtrans(string);
3595
3596 if (type == VAR_FUNC || type == VAR_PARTIAL)
3597 msg_puts("()");
3598 if (*first)
3599 {
3600 msg_clr_eos();
3601 *first = FALSE;
3602 }
3603}
3604
3605/*
3606 * Set variable "name" to value in "tv".
3607 * If the variable already exists, the value is updated.
3608 * Otherwise the variable is created.
3609 */
3610 void
3611set_var(
3612 char_u *name,
3613 typval_T *tv,
3614 int copy) // make copy of value in "tv"
3615{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003616 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003617}
3618
3619/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003620 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003621 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003622 * If the variable already exists and "is_const" is FALSE the value is updated.
3623 * Otherwise the variable is created.
3624 */
3625 void
3626set_var_const(
3627 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003628 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003629 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003630 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003631 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003632 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003633 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003634{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003635 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003636 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003637 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 dictitem_T *di;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003639 typval_T *dest_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003640 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003641 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003642 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003644 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003645 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003646 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003647 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003648 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003649
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003650 if (sid != 0)
3651 {
3652 if (SCRIPT_ID_VALID(sid))
3653 ht = &SCRIPT_VARS(sid);
3654 varname = name;
3655 }
3656 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003657 {
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003658 scriptitem_T *si;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003659
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003660 if (in_vim9script() && is_export
3661 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3662 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
Bram Moolenaar0e1574c2022-03-03 17:05:35 +00003663 ->sn_autoload_prefix != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003664 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003665 // In a vim9 autoload script an exported variable is put in the
3666 // global namespace with the autoload prefix.
3667 var_in_autoload = TRUE;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003668 varname = concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003669 if (varname == NULL)
3670 goto failed;
3671 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003672 ht = &globvarht;
3673 }
3674 else
3675 ht = find_var_ht(name, &varname);
3676 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003677 if (ht == NULL || *varname == NUL)
3678 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003679 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003680 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003681 }
Bram Moolenaar54969f42022-02-07 13:56:44 +00003682 is_script_local = ht == get_script_local_ht() || sid != 0
3683 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003684
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003685 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003686 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003687 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003688 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003689 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003690 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003691 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003692 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003693 }
Bram Moolenaar9510d222022-09-11 15:14:05 +01003694 if ((flags & ASSIGN_FOR_LOOP) && is_scoped_variable(name))
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003695 // Do not make g:var, w:var, b:var or t:var final.
3696 flags &= ~ASSIGN_FINAL;
3697
Bram Moolenaare535db82021-03-31 21:07:24 +02003698 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003699 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3700 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003701 // For "[a, _] = list" the underscore is ignored.
3702 if ((flags & ASSIGN_UNPACK) == 0)
3703 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003704 goto failed;
3705 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003708
Bram Moolenaar24e93162021-07-18 20:40:33 +02003709 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003710 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003711 imported_T *import = find_imported(varname, 0, FALSE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003712
Bram Moolenaar24e93162021-07-18 20:40:33 +02003713 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003714 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003715 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02003716 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003718 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003719 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003721 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3722 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003723 }
Bram Moolenaar75e27d72022-02-13 13:56:29 +00003724 if (!in_vim9script())
3725 {
3726 semsg(_(e_cannot_create_vim9_script_variable_in_function_str),
3727 name);
3728 goto failed;
3729 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003730 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731
Bram Moolenaar993faa32022-02-21 15:59:11 +00003732 // Search in parent scope which is possible to reference from lambda
3733 if (di == NULL)
3734 di = find_var_in_scoped_ht(name, TRUE);
3735
3736 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
3737 && var_wrong_func_name(name, di == NULL))
3738 goto failed;
3739
3740 if (need_convert_to_bool(type, tv))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003741 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003742 // Destination is a bool and the value is not, but it can be
3743 // converted.
3744 CLEAR_FIELD(bool_tv);
3745 bool_tv.v_type = VAR_BOOL;
3746 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3747 tv = &bool_tv;
3748 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003749
Bram Moolenaar993faa32022-02-21 15:59:11 +00003750 if (di != NULL)
3751 {
3752 // Item already exists. Allowed to replace when reloading.
3753 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003754 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003755 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
3756 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003757 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003758 emsg(_(e_cannot_modify_existing_variable));
3759 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003760 }
3761
Bram Moolenaar993faa32022-02-21 15:59:11 +00003762 if (is_script_local && vim9script
3763 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003764 {
3765 semsg(_(e_redefining_script_item_str), name);
3766 goto failed;
3767 }
3768
Bram Moolenaar993faa32022-02-21 15:59:11 +00003769 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003770 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003771 where_T where = WHERE_INIT;
Bram Moolenaar7a411a32022-04-04 14:58:06 +01003772 svar_T *sv = find_typval_in_script(&di->di_tv, sid, TRUE);
Bram Moolenaar993faa32022-02-21 15:59:11 +00003773
3774 if (sv != NULL)
3775 {
3776 // check the type and adjust to bool if needed
3777 where.wt_index = var_idx;
3778 where.wt_variable = TRUE;
3779 if (check_script_var_type(sv, tv, name, where) == FAIL)
3780 goto failed;
3781 if (type == NULL)
3782 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003783 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003784 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003785 }
3786
Bram Moolenaar993faa32022-02-21 15:59:11 +00003787 if ((flags & ASSIGN_FOR_LOOP) == 0
3788 && var_check_permission(di, name) == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003789 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003790 }
3791 else
3792 {
3793 // can only redefine once
3794 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003795
Bram Moolenaar993faa32022-02-21 15:59:11 +00003796 // A Vim9 script-local variable is also present in sn_all_vars
3797 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003798 if (var_in_vim9script || var_in_autoload)
Bram Moolenaar993faa32022-02-21 15:59:11 +00003799 update_vim9_script_var(FALSE, di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003800 var_in_autoload ? name : di->di_key, flags,
Bram Moolenaar993faa32022-02-21 15:59:11 +00003801 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003802 }
3803
Bram Moolenaar993faa32022-02-21 15:59:11 +00003804 // existing variable, need to clear the value
3805
3806 // Handle setting internal di: variables separately where needed to
3807 // prevent changing the type.
3808 if (ht == &vimvarht)
3809 {
3810 if (di->di_tv.v_type == VAR_STRING)
3811 {
3812 VIM_CLEAR(di->di_tv.vval.v_string);
3813 if (copy || tv->v_type != VAR_STRING)
3814 {
3815 char_u *val = tv_get_string(tv);
3816
3817 // Careful: when assigning to v:errmsg and
3818 // tv_get_string() causes an error message the variable
3819 // will already be set.
3820 if (di->di_tv.vval.v_string == NULL)
3821 di->di_tv.vval.v_string = vim_strsave(val);
3822 }
3823 else
3824 {
3825 // Take over the string to avoid an extra alloc/free.
3826 di->di_tv.vval.v_string = tv->vval.v_string;
3827 tv->vval.v_string = NULL;
3828 }
3829 goto failed;
3830 }
3831 else if (di->di_tv.v_type == VAR_NUMBER)
3832 {
3833 di->di_tv.vval.v_number = tv_get_number(tv);
3834 if (STRCMP(varname, "searchforward") == 0)
3835 set_search_direction(di->di_tv.vval.v_number
3836 ? '/' : '?');
3837#ifdef FEAT_SEARCH_EXTRA
3838 else if (STRCMP(varname, "hlsearch") == 0)
3839 {
3840 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003841 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar993faa32022-02-21 15:59:11 +00003842 }
3843#endif
3844 goto failed;
3845 }
3846 else if (di->di_tv.v_type != tv->v_type)
3847 {
3848 semsg(_(e_setting_str_to_value_with_wrong_type), name);
3849 goto failed;
3850 }
3851 }
3852
3853 clear_tv(&di->di_tv);
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01003854
3855 if ((flags & ASSIGN_UPDATE_BLOCK_ID)
3856 && SCRIPT_ID_VALID(current_sctx.sc_sid))
3857 {
3858 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
3859
3860 update_script_var_block_id(name, si->sn_current_block_id);
3861 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00003862 }
3863 else
3864 {
3865 // Item not found, check if a function already exists.
3866 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
3867 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
3868 {
3869 semsg(_(e_redefining_script_item_str), name);
3870 goto failed;
3871 }
3872
3873 // add a new variable
3874 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3875 {
3876 semsg(_(e_unknown_variable_str), name);
3877 goto failed;
3878 }
3879
3880 // Can't add "v:" or "a:" variable.
3881 if (ht == &vimvarht || ht == get_funccal_args_ht())
3882 {
3883 semsg(_(e_illegal_variable_name_str), name);
3884 goto failed;
3885 }
3886
3887 // Make sure the variable name is valid. In Vim9 script an
3888 // autoload variable must be prefixed with "g:" unless in an
3889 // autoload script.
3890 if (!valid_varname(varname, -1, !vim9script
3891 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
3892 goto failed;
3893
3894 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3895 if (di == NULL)
3896 goto failed;
3897 STRCPY(di->di_key, varname);
3898 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
3899 {
3900 vim_free(di);
3901 goto failed;
3902 }
3903 di->di_flags = DI_FLAGS_ALLOC;
3904 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3905 di->di_flags |= DI_FLAGS_LOCK;
3906
3907 // A Vim9 script-local variable is also added to sn_all_vars and
3908 // sn_var_vals. It may set "type" from "tv".
3909 if (var_in_vim9script || var_in_autoload)
3910 update_vim9_script_var(TRUE, di,
3911 var_in_autoload ? name : di->di_key, flags,
3912 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003913 }
3914
Bram Moolenaar993faa32022-02-21 15:59:11 +00003915 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003916 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003917 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003918 else
3919 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003920 *dest_tv = *tv;
3921 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003922 init_tv(tv);
3923 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003924 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003925
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003926 if (vim9script && type != NULL)
Bram Moolenaar381692b2022-02-02 20:01:27 +00003927 set_tv_type(dest_tv, type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003928
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003929 // ":const var = value" locks the value
3930 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003931 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003932 // Like :lockvar! name: lock the value and what it contains, but only
3933 // if the reference count is up to one. That locks only literal
3934 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003935 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003936
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003937failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003938 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003939 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003940 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003941}
3942
3943/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003944 * Check in this order for backwards compatibility:
3945 * - Whether the variable is read-only
3946 * - Whether the variable value is locked
3947 * - Whether the variable is locked
3948 */
3949 int
3950var_check_permission(dictitem_T *di, char_u *name)
3951{
3952 if (var_check_ro(di->di_flags, name, FALSE)
3953 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3954 || var_check_lock(di->di_flags, name, FALSE))
3955 return FAIL;
3956 return OK;
3957}
3958
3959/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003960 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3961 * Also give an error message.
3962 */
3963 int
3964var_check_ro(int flags, char_u *name, int use_gettext)
3965{
3966 if (flags & DI_FLAGS_RO)
3967 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003968 if (name == NULL)
3969 emsg(_(e_cannot_change_readonly_variable));
3970 else
3971 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02003972 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003973 return TRUE;
3974 }
3975 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3976 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003977 if (name == NULL)
3978 emsg(_(e_cannot_set_variable_in_sandbox));
3979 else
3980 semsg(_(e_cannot_set_variable_in_sandbox_str),
3981 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003982 return TRUE;
3983 }
3984 return FALSE;
3985}
3986
3987/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003988 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3989 * Also give an error message.
3990 */
3991 int
3992var_check_lock(int flags, char_u *name, int use_gettext)
3993{
3994 if (flags & DI_FLAGS_LOCK)
3995 {
3996 semsg(_(e_variable_is_locked_str),
3997 use_gettext ? (char_u *)_(name) : name);
3998 return TRUE;
3999 }
4000 return FALSE;
4001}
4002
4003/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004004 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
4005 * Also give an error message.
4006 */
4007 int
4008var_check_fixed(int flags, char_u *name, int use_gettext)
4009{
4010 if (flags & DI_FLAGS_FIX)
4011 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004012 if (name == NULL)
4013 emsg(_(e_cannot_delete_variable));
4014 else
4015 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004016 use_gettext ? (char_u *)_(name) : name);
4017 return TRUE;
4018 }
4019 return FALSE;
4020}
4021
4022/*
4023 * Check if a funcref is assigned to a valid variable name.
4024 * Return TRUE and give an error if not.
4025 */
4026 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004027var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004028 char_u *name, // points to start of variable name
4029 int new_var) // TRUE when creating the variable
4030{
Bram Moolenaar32154662021-03-28 21:14:06 +02004031 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
4032 // the name can be used without the s: prefix.
thinca6c667bd2022-09-02 11:25:37 +01004033 // Allow autoload variable.
Bram Moolenaar32154662021-03-28 21:14:06 +02004034 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
4035 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004036 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
thinca6c667bd2022-09-02 11:25:37 +01004037 ? name[2] : name[0])
4038 && vim_strchr(name, '#') == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004039 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004040 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004041 return TRUE;
4042 }
4043 // Don't allow hiding a function. When "v" is not NULL we might be
4044 // assigning another function to the same var, the type is checked
4045 // below.
4046 if (new_var && function_exists(name, FALSE))
4047 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004048 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004049 name);
4050 return TRUE;
4051 }
4052 return FALSE;
4053}
4054
4055/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004056 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
4057 * value. Also give an error message, using "name" or _("name") when
4058 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004059 */
4060 int
Bram Moolenaara187c432020-09-16 21:08:28 +02004061value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004062{
4063 if (lock & VAR_LOCKED)
4064 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004065 if (name == NULL)
4066 emsg(_(e_value_is_locked));
4067 else
4068 semsg(_(e_value_is_locked_str),
4069 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004070 return TRUE;
4071 }
4072 if (lock & VAR_FIXED)
4073 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004074 if (name == NULL)
4075 emsg(_(e_cannot_change_value));
4076 else
4077 semsg(_(e_cannot_change_value_of_str),
4078 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004079 return TRUE;
4080 }
4081 return FALSE;
4082}
4083
4084/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01004085 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004086 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004087 * Return FALSE and give an error if not.
4088 */
4089 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004090valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004091{
4092 char_u *p;
4093
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004094 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004095 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01004096 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004097 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004098 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004099 return FALSE;
4100 }
4101 return TRUE;
4102}
4103
4104/*
LemonBoy47d4e312022-05-04 18:12:55 +01004105 * Implements the logic to retrieve local variable and option values.
4106 * Used by "getwinvar()" "gettabvar()" "gettabwinvar()" "getbufvar()".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004107 */
4108 static void
LemonBoy47d4e312022-05-04 18:12:55 +01004109get_var_from(
4110 char_u *varname,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004111 typval_T *rettv,
LemonBoy47d4e312022-05-04 18:12:55 +01004112 typval_T *deftv, // Default value if not found.
4113 int htname, // 't'ab, 'w'indow or 'b'uffer local.
4114 tabpage_T *tp, // can be NULL
4115 win_T *win,
4116 buf_T *buf) // Ignored if htname is not 'b'.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004117{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004118 dictitem_T *v;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004119 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004120 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004121 int need_switch_win;
zeertzjqcd6ad642022-07-25 12:28:09 +01004122 int do_change_curbuf = buf != NULL && htname == 'b';
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004123
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004124 ++emsg_off;
4125
4126 rettv->v_type = VAR_STRING;
4127 rettv->vval.v_string = NULL;
4128
LemonBoy47d4e312022-05-04 18:12:55 +01004129 if (varname != NULL && tp != NULL && win != NULL
4130 && (htname != 'b' || buf != NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004131 {
4132 // Set curwin to be our win, temporarily. Also set the tabpage,
4133 // otherwise the window is not valid. Only do this when needed,
4134 // autocommands get blocked.
LemonBoy47d4e312022-05-04 18:12:55 +01004135 // If we have a buffer reference avoid the switching, we're saving and
4136 // restoring curbuf directly.
zeertzjqcd6ad642022-07-25 12:28:09 +01004137 need_switch_win = !(tp == curtab && win == curwin) && !do_change_curbuf;
LemonBoy47d4e312022-05-04 18:12:55 +01004138 if (!need_switch_win || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004139 {
LemonBoy47d4e312022-05-04 18:12:55 +01004140 // Handle options. There are no tab-local options.
4141 if (*varname == '&' && htname != 't')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004142 {
LemonBoy47d4e312022-05-04 18:12:55 +01004143 buf_T *save_curbuf = curbuf;
4144
4145 // Change curbuf so the option is read from the correct buffer.
zeertzjqcd6ad642022-07-25 12:28:09 +01004146 if (do_change_curbuf)
LemonBoy47d4e312022-05-04 18:12:55 +01004147 curbuf = buf;
4148
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004149 if (varname[1] == NUL)
4150 {
zeertzjqcd6ad642022-07-25 12:28:09 +01004151 // get all window-local or buffer-local options in a dict
LemonBoy47d4e312022-05-04 18:12:55 +01004152 dict_T *opts = get_winbuf_options(htname == 'b');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004153
4154 if (opts != NULL)
4155 {
4156 rettv_dict_set(rettv, opts);
4157 done = TRUE;
4158 }
4159 }
LemonBoy47d4e312022-05-04 18:12:55 +01004160 else if (eval_option(&varname, rettv, TRUE) == OK)
4161 // Local option
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004162 done = TRUE;
LemonBoy47d4e312022-05-04 18:12:55 +01004163
4164 curbuf = save_curbuf;
4165 }
4166 else if (*varname == NUL)
4167 {
4168 // Empty string: return a dict with all the local variables.
4169 if (htname == 'b')
4170 v = &buf->b_bufvar;
4171 else if (htname == 'w')
4172 v = &win->w_winvar;
4173 else
4174 v = &tp->tp_winvar;
4175 copy_tv(&v->di_tv, rettv);
4176 done = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004177 }
4178 else
4179 {
LemonBoy47d4e312022-05-04 18:12:55 +01004180 hashtab_T *ht;
4181
4182 if (htname == 'b')
4183 ht = &buf->b_vars->dv_hashtab;
4184 else if (htname == 'w')
4185 ht = &win->w_vars->dv_hashtab;
4186 else
4187 ht = &tp->tp_vars->dv_hashtab;
4188
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004189 // Look up the variable.
LemonBoy47d4e312022-05-04 18:12:55 +01004190 v = find_var_in_ht(ht, htname, varname, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004191 if (v != NULL)
4192 {
4193 copy_tv(&v->di_tv, rettv);
4194 done = TRUE;
4195 }
4196 }
4197 }
4198
4199 if (need_switch_win)
4200 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004201 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004202 }
4203
LemonBoy47d4e312022-05-04 18:12:55 +01004204 if (!done && deftv->v_type != VAR_UNKNOWN)
4205 // use the default value
4206 copy_tv(deftv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004207
4208 --emsg_off;
4209}
4210
4211/*
LemonBoy47d4e312022-05-04 18:12:55 +01004212 * getwinvar() and gettabwinvar()
4213 */
4214 static void
4215getwinvar(
4216 typval_T *argvars,
4217 typval_T *rettv,
4218 int off) // 1 for gettabwinvar()
4219{
4220 char_u *varname;
4221 tabpage_T *tp;
4222 win_T *win;
4223
4224 if (off == 1)
4225 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4226 else
4227 tp = curtab;
4228 win = find_win_by_nr(&argvars[off], tp);
4229 varname = tv_get_string_chk(&argvars[off + 1]);
4230
4231 get_var_from(varname, rettv, &argvars[off + 2], 'w', tp, win, NULL);
4232}
4233
4234/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02004235 * Set option "varname" to the value of "varp" for the current buffer/window.
4236 */
4237 static void
4238set_option_from_tv(char_u *varname, typval_T *varp)
4239{
4240 long numval = 0;
4241 char_u *strval;
4242 char_u nbuf[NUMBUFLEN];
4243 int error = FALSE;
4244
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004245 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004246 {
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004247 if (is_string_option(varname))
4248 {
4249 emsg(_(e_string_required));
4250 return;
4251 }
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004252 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004253 strval = (char_u *)"0"; // avoid using "false"
4254 }
4255 else
4256 {
4257 if (!in_vim9script() || varp->v_type != VAR_STRING)
4258 numval = (long)tv_get_number_chk(varp, &error);
4259 strval = tv_get_string_buf_chk(varp, nbuf);
4260 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02004261 if (!error && strval != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004262 set_option_value_give_err(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar191929b2020-08-19 21:20:49 +02004263}
4264
4265/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004266 * "setwinvar()" and "settabwinvar()" functions
4267 */
4268 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004269setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004270{
4271 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004272 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004273 int need_switch_win;
4274 char_u *varname, *winvarname;
4275 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004276 tabpage_T *tp = NULL;
4277
4278 if (check_secure())
4279 return;
4280
4281 if (off == 1)
4282 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4283 else
4284 tp = curtab;
4285 win = find_win_by_nr(&argvars[off], tp);
4286 varname = tv_get_string_chk(&argvars[off + 1]);
4287 varp = &argvars[off + 2];
4288
4289 if (win != NULL && varname != NULL && varp != NULL)
4290 {
4291 need_switch_win = !(tp == curtab && win == curwin);
4292 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00004293 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004294 {
4295 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02004296 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004297 else
4298 {
4299 winvarname = alloc(STRLEN(varname) + 3);
4300 if (winvarname != NULL)
4301 {
4302 STRCPY(winvarname, "w:");
4303 STRCPY(winvarname + 2, varname);
4304 set_var(winvarname, varp, TRUE);
4305 vim_free(winvarname);
4306 }
4307 }
4308 }
4309 if (need_switch_win)
Bram Moolenaar18f47402022-01-06 13:24:51 +00004310 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004311 }
4312}
4313
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004314/*
4315 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4316 * v:option_type, and v:option_command.
4317 */
4318 void
4319reset_v_option_vars(void)
4320{
4321 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4322 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4323 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4324 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4325 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4326 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4327}
4328
4329/*
4330 * Add an assert error to v:errors.
4331 */
4332 void
4333assert_error(garray_T *gap)
4334{
4335 struct vimvar *vp = &vimvars[VV_ERRORS];
4336
Bram Moolenaard787e402021-12-24 21:36:12 +00004337 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004338 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004339 set_vim_var_list(VV_ERRORS, list_alloc());
4340 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4341}
4342
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004343 int
4344var_exists(char_u *var)
4345{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004346 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004347 char_u *name;
4348 char_u *tofree;
4349 typval_T tv;
4350 int len = 0;
4351 int n = FALSE;
4352
4353 // get_name_len() takes care of expanding curly braces
4354 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004355 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004356 if (len > 0)
4357 {
4358 if (tofree != NULL)
4359 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004360 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004361 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004362 if (n)
4363 {
4364 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004365 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004366 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4367 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004368 if (n)
4369 clear_tv(&tv);
4370 }
4371 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004372 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004373 n = FALSE;
4374
4375 vim_free(tofree);
4376 return n;
4377}
4378
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004379static lval_T *redir_lval = NULL;
4380#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4381static garray_T redir_ga; // only valid when redir_lval is not NULL
4382static char_u *redir_endp = NULL;
4383static char_u *redir_varname = NULL;
4384
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004385 int
4386alloc_redir_lval(void)
4387{
4388 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4389 if (redir_lval == NULL)
4390 return FAIL;
4391 return OK;
4392}
4393
4394 void
4395clear_redir_lval(void)
4396{
4397 VIM_CLEAR(redir_lval);
4398}
4399
4400 void
4401init_redir_ga(void)
4402{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004403 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004404}
4405
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004406/*
4407 * Start recording command output to a variable
4408 * When "append" is TRUE append to an existing variable.
4409 * Returns OK if successfully completed the setup. FAIL otherwise.
4410 */
4411 int
4412var_redir_start(char_u *name, int append)
4413{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004414 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004415 typval_T tv;
4416
4417 // Catch a bad name early.
4418 if (!eval_isnamec1(*name))
4419 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004420 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004421 return FAIL;
4422 }
4423
4424 // Make a copy of the name, it is used in redir_lval until redir ends.
4425 redir_varname = vim_strsave(name);
4426 if (redir_varname == NULL)
4427 return FAIL;
4428
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004429 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004430 {
4431 var_redir_stop();
4432 return FAIL;
4433 }
4434
4435 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004436 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004437
4438 // Parse the variable name (can be a dict or list entry).
4439 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4440 FNE_CHECK_START);
4441 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4442 {
4443 clear_lval(redir_lval);
4444 if (redir_endp != NULL && *redir_endp != NUL)
4445 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004446 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004447 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004448 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004449 redir_endp = NULL; // don't store a value, only cleanup
4450 var_redir_stop();
4451 return FAIL;
4452 }
4453
4454 // check if we can write to the variable: set it to or append an empty
4455 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004456 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004457 tv.v_type = VAR_STRING;
4458 tv.vval.v_string = (char_u *)"";
4459 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004460 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004461 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004462 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004463 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004464 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004465 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004466 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004467 {
4468 redir_endp = NULL; // don't store a value, only cleanup
4469 var_redir_stop();
4470 return FAIL;
4471 }
4472
4473 return OK;
4474}
4475
4476/*
4477 * Append "value[value_len]" to the variable set by var_redir_start().
4478 * The actual appending is postponed until redirection ends, because the value
4479 * appended may in fact be the string we write to, changing it may cause freed
4480 * memory to be used:
4481 * :redir => foo
4482 * :let foo
4483 * :redir END
4484 */
4485 void
4486var_redir_str(char_u *value, int value_len)
4487{
4488 int len;
4489
4490 if (redir_lval == NULL)
4491 return;
4492
4493 if (value_len == -1)
4494 len = (int)STRLEN(value); // Append the entire string
4495 else
4496 len = value_len; // Append only "value_len" characters
4497
4498 if (ga_grow(&redir_ga, len) == OK)
4499 {
4500 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4501 redir_ga.ga_len += len;
4502 }
4503 else
4504 var_redir_stop();
4505}
4506
4507/*
4508 * Stop redirecting command output to a variable.
4509 * Frees the allocated memory.
4510 */
4511 void
4512var_redir_stop(void)
4513{
4514 typval_T tv;
4515
4516 if (EVALCMD_BUSY)
4517 {
4518 redir_lval = NULL;
4519 return;
4520 }
4521
4522 if (redir_lval != NULL)
4523 {
4524 // If there was no error: assign the text to the variable.
4525 if (redir_endp != NULL)
4526 {
4527 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4528 tv.v_type = VAR_STRING;
4529 tv.vval.v_string = redir_ga.ga_data;
4530 // Call get_lval() again, if it's inside a Dict or List it may
4531 // have changed.
4532 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4533 FALSE, FALSE, 0, FNE_CHECK_START);
4534 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004535 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004536 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004537 clear_lval(redir_lval);
4538 }
4539
4540 // free the collected output
4541 VIM_CLEAR(redir_ga.ga_data);
4542
4543 VIM_CLEAR(redir_lval);
4544 }
4545 VIM_CLEAR(redir_varname);
4546}
4547
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004548/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004549 * Get the collected redirected text and clear redir_ga.
4550 */
4551 char_u *
4552get_clear_redir_ga(void)
4553{
4554 char_u *res;
4555
4556 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4557 res = redir_ga.ga_data;
4558 redir_ga.ga_data = NULL;
4559 return res;
4560}
4561
4562/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004563 * "gettabvar()" function
4564 */
4565 void
4566f_gettabvar(typval_T *argvars, typval_T *rettv)
4567{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004568 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004569 tabpage_T *tp;
4570 win_T *win = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004571
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004572 if (in_vim9script()
4573 && (check_for_number_arg(argvars, 0) == FAIL
4574 || check_for_string_arg(argvars, 1) == FAIL))
4575 return;
4576
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004577 varname = tv_get_string_chk(&argvars[1]);
4578 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
LemonBoy47d4e312022-05-04 18:12:55 +01004579 if (tp != NULL)
4580 win = tp == curtab || tp->tp_firstwin == NULL ? firstwin
4581 : tp->tp_firstwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004582
LemonBoy47d4e312022-05-04 18:12:55 +01004583 get_var_from(varname, rettv, &argvars[2], 't', tp, win, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004584}
4585
4586/*
4587 * "gettabwinvar()" function
4588 */
4589 void
4590f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4591{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004592 if (in_vim9script()
4593 && (check_for_number_arg(argvars, 0) == FAIL
4594 || check_for_number_arg(argvars, 1) == FAIL
4595 || check_for_string_arg(argvars, 2) == FAIL))
4596 return;
4597
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004598 getwinvar(argvars, rettv, 1);
4599}
4600
4601/*
4602 * "getwinvar()" function
4603 */
4604 void
4605f_getwinvar(typval_T *argvars, typval_T *rettv)
4606{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004607 if (in_vim9script()
4608 && (check_for_number_arg(argvars, 0) == FAIL
4609 || check_for_string_arg(argvars, 1) == FAIL))
4610 return;
4611
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004612 getwinvar(argvars, rettv, 0);
4613}
4614
4615/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004616 * "getbufvar()" function
4617 */
4618 void
4619f_getbufvar(typval_T *argvars, typval_T *rettv)
4620{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004621 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004622 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004623
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004624 if (in_vim9script()
4625 && (check_for_buffer_arg(argvars, 0) == FAIL
4626 || check_for_string_arg(argvars, 1) == FAIL))
4627 return;
4628
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004629 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004630 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004631
LemonBoy47d4e312022-05-04 18:12:55 +01004632 get_var_from(varname, rettv, &argvars[2], 'b', curtab, curwin, buf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004633}
4634
4635/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004636 * "settabvar()" function
4637 */
4638 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004639f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004640{
4641 tabpage_T *save_curtab;
4642 tabpage_T *tp;
4643 char_u *varname, *tabvarname;
4644 typval_T *varp;
4645
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004646 if (check_secure())
4647 return;
4648
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004649 if (in_vim9script()
4650 && (check_for_number_arg(argvars, 0) == FAIL
4651 || check_for_string_arg(argvars, 1) == FAIL))
4652 return;
4653
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004654 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4655 varname = tv_get_string_chk(&argvars[1]);
4656 varp = &argvars[2];
4657
4658 if (varname != NULL && varp != NULL && tp != NULL)
4659 {
4660 save_curtab = curtab;
4661 goto_tabpage_tp(tp, FALSE, FALSE);
4662
4663 tabvarname = alloc(STRLEN(varname) + 3);
4664 if (tabvarname != NULL)
4665 {
4666 STRCPY(tabvarname, "t:");
4667 STRCPY(tabvarname + 2, varname);
4668 set_var(tabvarname, varp, TRUE);
4669 vim_free(tabvarname);
4670 }
4671
4672 // Restore current tabpage
4673 if (valid_tabpage(save_curtab))
4674 goto_tabpage_tp(save_curtab, FALSE, FALSE);
4675 }
4676}
4677
4678/*
4679 * "settabwinvar()" function
4680 */
4681 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004682f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004683{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004684 if (in_vim9script()
4685 && (check_for_number_arg(argvars, 0) == FAIL
4686 || check_for_number_arg(argvars, 1) == FAIL
4687 || check_for_string_arg(argvars, 2) == FAIL))
4688 return;
4689
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004690 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004691}
4692
4693/*
4694 * "setwinvar()" function
4695 */
4696 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004697f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004698{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004699 if (in_vim9script()
4700 && (check_for_number_arg(argvars, 0) == FAIL
4701 || check_for_string_arg(argvars, 1) == FAIL))
4702 return;
4703
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004704 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004705}
4706
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004707/*
4708 * "setbufvar()" function
4709 */
4710 void
4711f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4712{
4713 buf_T *buf;
4714 char_u *varname, *bufvarname;
4715 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004716
4717 if (check_secure())
4718 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004719
4720 if (in_vim9script()
4721 && (check_for_buffer_arg(argvars, 0) == FAIL
4722 || check_for_string_arg(argvars, 1) == FAIL))
4723 return;
4724
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004725 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004726 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004727 varp = &argvars[2];
4728
4729 if (buf != NULL && varname != NULL && varp != NULL)
4730 {
4731 if (*varname == '&')
4732 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004733 aco_save_T aco;
4734
4735 // set curbuf to be our buf, temporarily
4736 aucmd_prepbuf(&aco, buf);
4737
Bram Moolenaar191929b2020-08-19 21:20:49 +02004738 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004739
4740 // reset notion of buffer
4741 aucmd_restbuf(&aco);
4742 }
4743 else
4744 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004745 bufvarname = alloc(STRLEN(varname) + 3);
4746 if (bufvarname != NULL)
4747 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004748 buf_T *save_curbuf = curbuf;
4749
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004750 curbuf = buf;
4751 STRCPY(bufvarname, "b:");
4752 STRCPY(bufvarname + 2, varname);
4753 set_var(bufvarname, varp, TRUE);
4754 vim_free(bufvarname);
4755 curbuf = save_curbuf;
4756 }
4757 }
4758 }
4759}
4760
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004761/*
4762 * Get a callback from "arg". It can be a Funcref or a function name.
4763 * When "arg" is zero return an empty string.
4764 * "cb_name" is not allocated.
4765 * "cb_name" is set to NULL for an invalid argument.
4766 */
4767 callback_T
4768get_callback(typval_T *arg)
4769{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004770 callback_T res;
4771 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004772
4773 res.cb_free_name = FALSE;
4774 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4775 {
4776 res.cb_partial = arg->vval.v_partial;
4777 ++res.cb_partial->pt_refcount;
4778 res.cb_name = partial_name(res.cb_partial);
4779 }
4780 else
4781 {
4782 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004783 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4784 && isdigit(*arg->vval.v_string))
4785 r = FAIL;
4786 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004787 {
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004788 if (arg->v_type == VAR_STRING)
4789 {
4790 char_u *name;
4791
4792 name = get_scriptlocal_funcname(arg->vval.v_string);
4793 if (name != NULL)
4794 {
4795 vim_free(arg->vval.v_string);
4796 arg->vval.v_string = name;
4797 }
4798 }
4799
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004800 res.cb_name = arg->vval.v_string;
4801 func_ref(res.cb_name);
4802 }
4803 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004804 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004805 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004806 r = FAIL;
4807
4808 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004809 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004810 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004811 res.cb_name = NULL;
4812 }
4813 }
4814 return res;
4815}
4816
4817/*
4818 * Copy a callback into a typval_T.
4819 */
4820 void
4821put_callback(callback_T *cb, typval_T *tv)
4822{
4823 if (cb->cb_partial != NULL)
4824 {
4825 tv->v_type = VAR_PARTIAL;
4826 tv->vval.v_partial = cb->cb_partial;
4827 ++tv->vval.v_partial->pt_refcount;
4828 }
4829 else
4830 {
4831 tv->v_type = VAR_FUNC;
4832 tv->vval.v_string = vim_strsave(cb->cb_name);
4833 func_ref(cb->cb_name);
4834 }
4835}
4836
4837/*
4838 * Make a copy of "src" into "dest", allocating the function name if needed,
4839 * without incrementing the refcount.
4840 */
4841 void
4842set_callback(callback_T *dest, callback_T *src)
4843{
4844 if (src->cb_partial == NULL)
4845 {
4846 // just a function name, make a copy
4847 dest->cb_name = vim_strsave(src->cb_name);
4848 dest->cb_free_name = TRUE;
4849 }
4850 else
4851 {
4852 // cb_name is a pointer into cb_partial
4853 dest->cb_name = src->cb_name;
4854 dest->cb_free_name = FALSE;
4855 }
4856 dest->cb_partial = src->cb_partial;
4857}
4858
4859/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004860 * Copy callback from "src" to "dest", incrementing the refcounts.
4861 */
4862 void
4863copy_callback(callback_T *dest, callback_T *src)
4864{
4865 dest->cb_partial = src->cb_partial;
4866 if (dest->cb_partial != NULL)
4867 {
4868 dest->cb_name = src->cb_name;
4869 dest->cb_free_name = FALSE;
4870 ++dest->cb_partial->pt_refcount;
4871 }
4872 else
4873 {
4874 dest->cb_name = vim_strsave(src->cb_name);
4875 dest->cb_free_name = TRUE;
4876 func_ref(src->cb_name);
4877 }
4878}
4879
4880/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004881 * When a callback refers to an autoload import, change the function name to
4882 * the "path#name" form. Uses the current script context.
4883 * Only works when the name is allocated.
4884 */
4885 void
4886expand_autload_callback(callback_T *cb)
4887{
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004888 char_u *name;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004889 char_u *p;
4890 imported_T *import;
4891
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004892 if (!in_vim9script() || cb->cb_name == NULL
4893 || (!cb->cb_free_name
4894 && (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004895 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004896 if (cb->cb_partial != NULL)
4897 name = cb->cb_partial->pt_name;
4898 else
4899 name = cb->cb_name;
4900 p = vim_strchr(name, '.');
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004901 if (p == NULL)
4902 return;
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004903 import = find_imported(name, p - name, FALSE);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004904 if (import != NULL && SCRIPT_ID_VALID(import->imp_sid))
4905 {
4906 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
4907
4908 if (si->sn_autoload_prefix != NULL)
4909 {
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004910 char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004911
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004912 if (newname != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004913 {
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004914 if (cb->cb_partial != NULL)
4915 {
4916 if (cb->cb_name == cb->cb_partial->pt_name)
4917 cb->cb_name = newname;
4918 vim_free(cb->cb_partial->pt_name);
4919 cb->cb_partial->pt_name = newname;
4920 }
4921 else
4922 {
4923 vim_free(cb->cb_name);
4924 cb->cb_name = newname;
4925 }
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004926 }
4927 }
4928 }
4929}
4930
4931/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004932 * Unref/free "callback" returned by get_callback() or set_callback().
4933 */
4934 void
4935free_callback(callback_T *callback)
4936{
4937 if (callback->cb_partial != NULL)
4938 {
4939 partial_unref(callback->cb_partial);
4940 callback->cb_partial = NULL;
4941 }
4942 else if (callback->cb_name != NULL)
4943 func_unref(callback->cb_name);
4944 if (callback->cb_free_name)
4945 {
4946 vim_free(callback->cb_name);
4947 callback->cb_free_name = FALSE;
4948 }
4949 callback->cb_name = NULL;
4950}
4951
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004952#endif // FEAT_EVAL