blob: 7578378b3bdc2788b21d54897d356147302ace03 [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
Bram Moolenaard787e402021-12-24 21:36:12 +000048 type_T *vv_type; // type or NULL
Bram Moolenaare5cdf152019-08-29 22:09:46 +020049 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
50} vimvars[VV_LEN] =
51{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020052 // The order here must match the VV_ defines in vim.h!
53 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaard787e402021-12-24 21:36:12 +000054 {VV_NAME("count", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
55 {VV_NAME("count1", VAR_NUMBER), NULL, VV_RO},
56 {VV_NAME("prevcount", VAR_NUMBER), NULL, VV_RO},
57 {VV_NAME("errmsg", VAR_STRING), NULL, VV_COMPAT},
58 {VV_NAME("warningmsg", VAR_STRING), NULL, 0},
59 {VV_NAME("statusmsg", VAR_STRING), NULL, 0},
60 {VV_NAME("shell_error", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
61 {VV_NAME("this_session", VAR_STRING), NULL, VV_COMPAT},
62 {VV_NAME("version", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
63 {VV_NAME("lnum", VAR_NUMBER), NULL, VV_RO_SBX},
64 {VV_NAME("termresponse", VAR_STRING), NULL, VV_RO},
65 {VV_NAME("fname", VAR_STRING), NULL, VV_RO},
66 {VV_NAME("lang", VAR_STRING), NULL, VV_RO},
67 {VV_NAME("lc_time", VAR_STRING), NULL, VV_RO},
68 {VV_NAME("ctype", VAR_STRING), NULL, VV_RO},
69 {VV_NAME("charconvert_from", VAR_STRING), NULL, VV_RO},
70 {VV_NAME("charconvert_to", VAR_STRING), NULL, VV_RO},
71 {VV_NAME("fname_in", VAR_STRING), NULL, VV_RO},
72 {VV_NAME("fname_out", VAR_STRING), NULL, VV_RO},
73 {VV_NAME("fname_new", VAR_STRING), NULL, VV_RO},
74 {VV_NAME("fname_diff", VAR_STRING), NULL, VV_RO},
75 {VV_NAME("cmdarg", VAR_STRING), NULL, VV_RO},
76 {VV_NAME("foldstart", VAR_NUMBER), NULL, VV_RO_SBX},
77 {VV_NAME("foldend", VAR_NUMBER), NULL, VV_RO_SBX},
78 {VV_NAME("folddashes", VAR_STRING), NULL, VV_RO_SBX},
79 {VV_NAME("foldlevel", VAR_NUMBER), NULL, VV_RO_SBX},
80 {VV_NAME("progname", VAR_STRING), NULL, VV_RO},
81 {VV_NAME("servername", VAR_STRING), NULL, VV_RO},
82 {VV_NAME("dying", VAR_NUMBER), NULL, VV_RO},
83 {VV_NAME("exception", VAR_STRING), NULL, VV_RO},
84 {VV_NAME("throwpoint", VAR_STRING), NULL, VV_RO},
85 {VV_NAME("register", VAR_STRING), NULL, VV_RO},
86 {VV_NAME("cmdbang", VAR_NUMBER), NULL, VV_RO},
87 {VV_NAME("insertmode", VAR_STRING), NULL, VV_RO},
88 {VV_NAME("val", VAR_UNKNOWN), NULL, VV_RO},
89 {VV_NAME("key", VAR_UNKNOWN), NULL, VV_RO},
90 {VV_NAME("profiling", VAR_NUMBER), NULL, VV_RO},
91 {VV_NAME("fcs_reason", VAR_STRING), NULL, VV_RO},
92 {VV_NAME("fcs_choice", VAR_STRING), NULL, 0},
93 {VV_NAME("beval_bufnr", VAR_NUMBER), NULL, VV_RO},
94 {VV_NAME("beval_winnr", VAR_NUMBER), NULL, VV_RO},
95 {VV_NAME("beval_winid", VAR_NUMBER), NULL, VV_RO},
96 {VV_NAME("beval_lnum", VAR_NUMBER), NULL, VV_RO},
97 {VV_NAME("beval_col", VAR_NUMBER), NULL, VV_RO},
98 {VV_NAME("beval_text", VAR_STRING), NULL, VV_RO},
99 {VV_NAME("scrollstart", VAR_STRING), NULL, 0},
100 {VV_NAME("swapname", VAR_STRING), NULL, VV_RO},
101 {VV_NAME("swapchoice", VAR_STRING), NULL, 0},
102 {VV_NAME("swapcommand", VAR_STRING), NULL, VV_RO},
103 {VV_NAME("char", VAR_STRING), NULL, 0},
104 {VV_NAME("mouse_win", VAR_NUMBER), NULL, 0},
105 {VV_NAME("mouse_winid", VAR_NUMBER), NULL, 0},
106 {VV_NAME("mouse_lnum", VAR_NUMBER), NULL, 0},
107 {VV_NAME("mouse_col", VAR_NUMBER), NULL, 0},
108 {VV_NAME("operator", VAR_STRING), NULL, VV_RO},
109 {VV_NAME("searchforward", VAR_NUMBER), NULL, 0},
110 {VV_NAME("hlsearch", VAR_NUMBER), NULL, 0},
111 {VV_NAME("oldfiles", VAR_LIST), &t_list_string, 0},
112 {VV_NAME("windowid", VAR_NUMBER), NULL, VV_RO},
113 {VV_NAME("progpath", VAR_STRING), NULL, VV_RO},
114 {VV_NAME("completed_item", VAR_DICT), &t_dict_string, VV_RO},
115 {VV_NAME("option_new", VAR_STRING), NULL, VV_RO},
116 {VV_NAME("option_old", VAR_STRING), NULL, VV_RO},
117 {VV_NAME("option_oldlocal", VAR_STRING), NULL, VV_RO},
118 {VV_NAME("option_oldglobal", VAR_STRING), NULL, VV_RO},
119 {VV_NAME("option_command", VAR_STRING), NULL, VV_RO},
120 {VV_NAME("option_type", VAR_STRING), NULL, VV_RO},
121 {VV_NAME("errors", VAR_LIST), &t_list_string, 0},
122 {VV_NAME("false", VAR_BOOL), NULL, VV_RO},
123 {VV_NAME("true", VAR_BOOL), NULL, VV_RO},
124 {VV_NAME("none", VAR_SPECIAL), NULL, VV_RO},
125 {VV_NAME("null", VAR_SPECIAL), NULL, VV_RO},
126 {VV_NAME("numbermax", VAR_NUMBER), NULL, VV_RO},
127 {VV_NAME("numbermin", VAR_NUMBER), NULL, VV_RO},
128 {VV_NAME("numbersize", VAR_NUMBER), NULL, VV_RO},
129 {VV_NAME("vim_did_enter", VAR_NUMBER), NULL, VV_RO},
130 {VV_NAME("testing", VAR_NUMBER), NULL, 0},
131 {VV_NAME("t_number", VAR_NUMBER), NULL, VV_RO},
132 {VV_NAME("t_string", VAR_NUMBER), NULL, VV_RO},
133 {VV_NAME("t_func", VAR_NUMBER), NULL, VV_RO},
134 {VV_NAME("t_list", VAR_NUMBER), NULL, VV_RO},
135 {VV_NAME("t_dict", VAR_NUMBER), NULL, VV_RO},
136 {VV_NAME("t_float", VAR_NUMBER), NULL, VV_RO},
137 {VV_NAME("t_bool", VAR_NUMBER), NULL, VV_RO},
138 {VV_NAME("t_none", VAR_NUMBER), NULL, VV_RO},
139 {VV_NAME("t_job", VAR_NUMBER), NULL, VV_RO},
140 {VV_NAME("t_channel", VAR_NUMBER), NULL, VV_RO},
141 {VV_NAME("t_blob", VAR_NUMBER), NULL, VV_RO},
142 {VV_NAME("termrfgresp", VAR_STRING), NULL, VV_RO},
143 {VV_NAME("termrbgresp", VAR_STRING), NULL, VV_RO},
144 {VV_NAME("termu7resp", VAR_STRING), NULL, VV_RO},
145 {VV_NAME("termstyleresp", VAR_STRING), NULL, VV_RO},
146 {VV_NAME("termblinkresp", VAR_STRING), NULL, VV_RO},
147 {VV_NAME("event", VAR_DICT), NULL, VV_RO},
148 {VV_NAME("versionlong", VAR_NUMBER), NULL, VV_RO},
149 {VV_NAME("echospace", VAR_NUMBER), NULL, VV_RO},
150 {VV_NAME("argv", VAR_LIST), &t_list_string, VV_RO},
151 {VV_NAME("collate", VAR_STRING), NULL, VV_RO},
152 {VV_NAME("exiting", VAR_SPECIAL), NULL, VV_RO},
153 {VV_NAME("colornames", VAR_DICT), &t_dict_string, VV_RO},
154 {VV_NAME("sizeofint", VAR_NUMBER), NULL, VV_RO},
155 {VV_NAME("sizeoflong", VAR_NUMBER), NULL, VV_RO},
156 {VV_NAME("sizeofpointer", VAR_NUMBER), NULL, VV_RO},
naohiro ono56200ee2022-01-01 14:59:44 +0000157 {VV_NAME("maxcol", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200158};
159
160// shorthand
Bram Moolenaard787e402021-12-24 21:36:12 +0000161#define vv_tv_type vv_di.di_tv.v_type
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200162#define vv_nr vv_di.di_tv.vval.v_number
163#define vv_float vv_di.di_tv.vval.v_float
164#define vv_str vv_di.di_tv.vval.v_string
165#define vv_list vv_di.di_tv.vval.v_list
166#define vv_dict vv_di.di_tv.vval.v_dict
167#define vv_blob vv_di.di_tv.vval.v_blob
168#define vv_tv vv_di.di_tv
169
170static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200171static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200172#define vimvarht vimvardict.dv_hashtab
173
174// for VIM_VERSION_ defines
175#include "version.h"
176
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200177static void list_glob_vars(int *first);
178static void list_buf_vars(int *first);
179static void list_win_vars(int *first);
180static void list_tab_vars(int *first);
181static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100182static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op, int var_idx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200183static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
184static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200185static void list_one_var(dictitem_T *v, char *prefix, int *first);
186static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
187
188/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200189 * Initialize global and vim special variables
190 */
191 void
192evalvars_init(void)
193{
194 int i;
195 struct vimvar *p;
196
197 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
198 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
199 vimvardict.dv_lock = VAR_FIXED;
200 hash_init(&compat_hashtab);
201
202 for (i = 0; i < VV_LEN; ++i)
203 {
204 p = &vimvars[i];
205 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
206 {
207 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
208 getout(1);
209 }
210 STRCPY(p->vv_di.di_key, p->vv_name);
211 if (p->vv_flags & VV_RO)
212 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
213 else if (p->vv_flags & VV_RO_SBX)
214 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
215 else
216 p->vv_di.di_flags = DI_FLAGS_FIX;
217
218 // add to v: scope dict, unless the value is not always available
Bram Moolenaard787e402021-12-24 21:36:12 +0000219 if (p->vv_tv_type != VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200220 hash_add(&vimvarht, p->vv_di.di_key);
221 if (p->vv_flags & VV_COMPAT)
222 // add to compat scope dict
223 hash_add(&compat_hashtab, p->vv_di.di_key);
224 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200225 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
226 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200227
228 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
229 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100230 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200231 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
232 set_vim_var_list(VV_ERRORS, list_alloc());
233 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
234
235 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
236 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
237 set_vim_var_nr(VV_NONE, VVAL_NONE);
238 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100239 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
240 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100241 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaar69b30722021-11-02 21:39:49 +0000242 set_vim_var_nr(VV_SIZEOFINT, sizeof(int));
243 set_vim_var_nr(VV_SIZEOFLONG, sizeof(long));
244 set_vim_var_nr(VV_SIZEOFPOINTER, sizeof(char *));
naohiro ono56200ee2022-01-01 14:59:44 +0000245 set_vim_var_nr(VV_MAXCOL, MAXCOL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200246
247 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
248 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
249 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
250 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
251 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
252 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
253 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
254 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
255 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
256 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
257 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
258
259 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
260
Drew Vogele30d1022021-10-24 20:35:07 +0100261 set_vim_var_dict(VV_COLORNAMES, dict_alloc());
262
Bram Moolenaar439c0362020-06-06 15:58:03 +0200263 // Default for v:register is not 0 but '"'. This is adjusted once the
264 // clipboard has been setup by calling reset_reg_var().
265 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200266}
267
268#if defined(EXITFREE) || defined(PROTO)
269/*
270 * Free all vim variables information on exit
271 */
272 void
273evalvars_clear(void)
274{
275 int i;
276 struct vimvar *p;
277
278 for (i = 0; i < VV_LEN; ++i)
279 {
280 p = &vimvars[i];
281 if (p->vv_di.di_tv.v_type == VAR_STRING)
282 VIM_CLEAR(p->vv_str);
283 else if (p->vv_di.di_tv.v_type == VAR_LIST)
284 {
285 list_unref(p->vv_list);
286 p->vv_list = NULL;
287 }
288 }
289 hash_clear(&vimvarht);
290 hash_init(&vimvarht); // garbage_collect() will access it
291 hash_clear(&compat_hashtab);
292
293 // global variables
294 vars_clear(&globvarht);
295
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100296 // Script-local variables. Clear all the variables here.
297 // The scriptvar_T is cleared later in free_scriptnames(), because a
298 // variable in one script might hold a reference to the whole scope of
299 // another script.
300 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200301 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200302}
303#endif
304
305 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200306garbage_collect_globvars(int copyID)
307{
308 return set_ref_in_ht(&globvarht, copyID, NULL);
309}
310
311 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200312garbage_collect_vimvars(int copyID)
313{
314 return set_ref_in_ht(&vimvarht, copyID, NULL);
315}
316
317 int
318garbage_collect_scriptvars(int copyID)
319{
Bram Moolenaared234f22020-10-15 20:42:20 +0200320 int i;
321 int idx;
322 int abort = FALSE;
323 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200324
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100325 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200326 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200327 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
328
Bram Moolenaared234f22020-10-15 20:42:20 +0200329 si = SCRIPT_ITEM(i);
330 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
331 {
332 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
333
Bram Moolenaard00a7fb2021-03-08 20:47:14 +0100334 if (sv->sv_name != NULL)
335 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
Bram Moolenaared234f22020-10-15 20:42:20 +0200336 }
337 }
338
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200339 return abort;
340}
341
342/*
343 * Set an internal variable to a string value. Creates the variable if it does
344 * not already exist.
345 */
346 void
347set_internal_string_var(char_u *name, char_u *value)
348{
349 char_u *val;
350 typval_T *tvp;
351
352 val = vim_strsave(value);
353 if (val != NULL)
354 {
355 tvp = alloc_string_tv(val);
356 if (tvp != NULL)
357 {
358 set_var(name, tvp, FALSE);
359 free_tv(tvp);
360 }
361 }
362}
363
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200364 int
365eval_charconvert(
366 char_u *enc_from,
367 char_u *enc_to,
368 char_u *fname_from,
369 char_u *fname_to)
370{
371 int err = FALSE;
372
373 set_vim_var_string(VV_CC_FROM, enc_from, -1);
374 set_vim_var_string(VV_CC_TO, enc_to, -1);
375 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
376 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
377 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
378 err = TRUE;
379 set_vim_var_string(VV_CC_FROM, NULL, -1);
380 set_vim_var_string(VV_CC_TO, NULL, -1);
381 set_vim_var_string(VV_FNAME_IN, NULL, -1);
382 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
383
384 if (err)
385 return FAIL;
386 return OK;
387}
388
389# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
390 int
391eval_printexpr(char_u *fname, char_u *args)
392{
393 int err = FALSE;
394
395 set_vim_var_string(VV_FNAME_IN, fname, -1);
396 set_vim_var_string(VV_CMDARG, args, -1);
397 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
398 err = TRUE;
399 set_vim_var_string(VV_FNAME_IN, NULL, -1);
400 set_vim_var_string(VV_CMDARG, NULL, -1);
401
402 if (err)
403 {
404 mch_remove(fname);
405 return FAIL;
406 }
407 return OK;
408}
409# endif
410
411# if defined(FEAT_DIFF) || defined(PROTO)
412 void
413eval_diff(
414 char_u *origfile,
415 char_u *newfile,
416 char_u *outfile)
417{
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000418 sctx_T saved_sctx = current_sctx;
419 sctx_T *ctx;
420 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200421
422 set_vim_var_string(VV_FNAME_IN, origfile, -1);
423 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
424 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000425
426 ctx = get_option_sctx("diffexpr");
427 if (ctx != NULL)
428 current_sctx = *ctx;
429
430 // errors are ignored
431 tv = eval_expr(p_dex, NULL);
Bram Moolenaar39b89442022-01-22 18:21:36 +0000432 free_tv(tv);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000433
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200434 set_vim_var_string(VV_FNAME_IN, NULL, -1);
435 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
436 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000437 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200438}
439
440 void
441eval_patch(
442 char_u *origfile,
443 char_u *difffile,
444 char_u *outfile)
445{
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000446 sctx_T saved_sctx = current_sctx;
447 sctx_T *ctx;
448 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200449
450 set_vim_var_string(VV_FNAME_IN, origfile, -1);
451 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
452 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000453
454 ctx = get_option_sctx("patchexpr");
455 if (ctx != NULL)
456 current_sctx = *ctx;
457
458 // errors are ignored
459 tv = eval_expr(p_pex, NULL);
460 free_tv(tv);
461
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200462 set_vim_var_string(VV_FNAME_IN, NULL, -1);
463 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
464 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000465 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200466}
467# endif
468
469#if defined(FEAT_SPELL) || defined(PROTO)
470/*
471 * Evaluate an expression to a list with suggestions.
472 * For the "expr:" part of 'spellsuggest'.
473 * Returns NULL when there is an error.
474 */
475 list_T *
476eval_spell_expr(char_u *badword, char_u *expr)
477{
478 typval_T save_val;
479 typval_T rettv;
480 list_T *list = NULL;
481 char_u *p = skipwhite(expr);
482
483 // Set "v:val" to the bad word.
484 prepare_vimvar(VV_VAL, &save_val);
485 set_vim_var_string(VV_VAL, badword, -1);
486 if (p_verbose == 0)
487 ++emsg_off;
488
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200489 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200490 {
491 if (rettv.v_type != VAR_LIST)
492 clear_tv(&rettv);
493 else
494 list = rettv.vval.v_list;
495 }
496
497 if (p_verbose == 0)
498 --emsg_off;
499 clear_tv(get_vim_var_tv(VV_VAL));
500 restore_vimvar(VV_VAL, &save_val);
501
502 return list;
503}
504
505/*
506 * "list" is supposed to contain two items: a word and a number. Return the
507 * word in "pp" and the number as the return value.
508 * Return -1 if anything isn't right.
509 * Used to get the good word and score from the eval_spell_expr() result.
510 */
511 int
512get_spellword(list_T *list, char_u **pp)
513{
514 listitem_T *li;
515
516 li = list->lv_first;
517 if (li == NULL)
518 return -1;
519 *pp = tv_get_string(&li->li_tv);
520
521 li = li->li_next;
522 if (li == NULL)
523 return -1;
524 return (int)tv_get_number(&li->li_tv);
525}
526#endif
527
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200528/*
529 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200530 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200531 * When not used yet add the variable to the v: hashtable.
532 */
533 void
534prepare_vimvar(int idx, typval_T *save_tv)
535{
536 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200537 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaard787e402021-12-24 21:36:12 +0000538 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200539 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
540}
541
542/*
543 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200544 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200545 * When no longer defined, remove the variable from the v: hashtable.
546 */
547 void
548restore_vimvar(int idx, typval_T *save_tv)
549{
550 hashitem_T *hi;
551
552 vimvars[idx].vv_tv = *save_tv;
Bram Moolenaard787e402021-12-24 21:36:12 +0000553 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200554 {
555 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
556 if (HASHITEM_EMPTY(hi))
557 internal_error("restore_vimvar()");
558 else
559 hash_remove(&vimvarht, hi);
560 }
561}
562
563/*
564 * List Vim variables.
565 */
566 static void
567list_vim_vars(int *first)
568{
569 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
570}
571
572/*
573 * List script-local variables, if there is a script.
574 */
575 static void
576list_script_vars(int *first)
577{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200578 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200579 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
580 "s:", FALSE, first);
581}
582
583/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200584 * Get a list of lines from a HERE document. The here document is a list of
585 * lines surrounded by a marker.
586 * cmd << {marker}
587 * {line1}
588 * {line2}
589 * ....
590 * {marker}
591 *
592 * The {marker} is a string. If the optional 'trim' word is supplied before the
593 * marker, then the leading indentation before the lines (matching the
594 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200595 *
596 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
597 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
598 * missing, then '.' is accepted as a marker.
599 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200600 * Returns a List with {lines} or NULL.
601 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100602 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200603heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200604{
605 char_u *theline;
606 char_u *marker;
607 list_T *l;
608 char_u *p;
609 int marker_indent_len = 0;
610 int text_indent_len = 0;
611 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200612 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200613 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200614
615 if (eap->getline == NULL)
616 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000617 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200618 return NULL;
619 }
620
621 // Check for the optional 'trim' word before the marker
622 cmd = skipwhite(cmd);
623 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
624 {
625 cmd = skipwhite(cmd + 4);
626
627 // Trim the indentation from all the lines in the here document.
628 // The amount of indentation trimmed is the same as the indentation of
629 // the first line after the :let command line. To find the end marker
630 // the indent of the :let command line is trimmed.
631 p = *eap->cmdlinep;
632 while (VIM_ISWHITE(*p))
633 {
634 p++;
635 marker_indent_len++;
636 }
637 text_indent_len = -1;
638 }
639
640 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200641 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200642 {
643 marker = skipwhite(cmd);
644 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200645 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200646 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000647 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200648 return NULL;
649 }
650 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200651 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200652 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000653 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200654 return NULL;
655 }
656 }
657 else
658 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200659 // When getting lines for an embedded script, if the marker is missing,
660 // accept '.' as the marker.
661 if (script_get)
662 marker = dot;
663 else
664 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000665 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200666 return NULL;
667 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200668 }
669
670 l = list_alloc();
671 if (l == NULL)
672 return NULL;
673
674 for (;;)
675 {
676 int mi = 0;
677 int ti = 0;
678
679 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
680 if (theline == NULL)
681 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000682 semsg(_(e_missing_end_marker_str), marker);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200683 break;
684 }
685
686 // with "trim": skip the indent matching the :let line to find the
687 // marker
688 if (marker_indent_len > 0
689 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
690 mi = marker_indent_len;
691 if (STRCMP(marker, theline + mi) == 0)
692 {
693 vim_free(theline);
694 break;
695 }
696
697 if (text_indent_len == -1 && *theline != NUL)
698 {
699 // set the text indent from the first line.
700 p = theline;
701 text_indent_len = 0;
702 while (VIM_ISWHITE(*p))
703 {
704 p++;
705 text_indent_len++;
706 }
707 text_indent = vim_strnsave(theline, text_indent_len);
708 }
709 // with "trim": skip the indent matching the first line
710 if (text_indent != NULL)
711 for (ti = 0; ti < text_indent_len; ++ti)
712 if (theline[ti] != text_indent[ti])
713 break;
714
715 if (list_append_string(l, theline + ti, -1) == FAIL)
716 break;
717 vim_free(theline);
718 }
719 vim_free(text_indent);
720
721 return l;
722}
723
724/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200725 * Vim9 variable declaration:
726 * ":var name"
727 * ":var name: type"
728 * ":var name = expr"
729 * ":var name: type = expr"
730 * etc.
731 */
732 void
733ex_var(exarg_T *eap)
734{
735 if (!in_vim9script())
736 {
737 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
738 return;
739 }
740 ex_let(eap);
741}
742
743/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200744 * ":let" list all variable values
745 * ":let var1 var2" list variable values
746 * ":let var = expr" assignment command.
747 * ":let var += expr" assignment command.
748 * ":let var -= expr" assignment command.
749 * ":let var *= expr" assignment command.
750 * ":let var /= expr" assignment command.
751 * ":let var %= expr" assignment command.
752 * ":let var .= expr" assignment command.
753 * ":let var ..= expr" assignment command.
754 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100755 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100756 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200757 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200758 * ":final var = expr" assignment command.
759 * ":final [var1, var2] = expr" unpack list.
760 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200761 * ":const" list all variable values
762 * ":const var1 var2" list variable values
763 * ":const var = expr" assignment command.
764 * ":const [var1, var2] = expr" unpack list.
765 */
766 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200767ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200768{
769 char_u *arg = eap->arg;
770 char_u *expr = NULL;
771 typval_T rettv;
772 int i;
773 int var_count = 0;
774 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200775 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200776 char_u *argend;
777 int first = TRUE;
778 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200779 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100780 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200781 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200782
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200783 if (eap->cmdidx == CMD_final && !vim9script)
784 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100785 // In legacy Vim script ":final" is short for ":finally".
786 ex_finally(eap);
787 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200788 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200789 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200790 {
791 emsg(_(e_cannot_use_let_in_vim9_script));
792 return;
793 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200794
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100795 if (eap->cmdidx == CMD_const)
796 flags |= ASSIGN_CONST;
797 else if (eap->cmdidx == CMD_final)
798 flags |= ASSIGN_FINAL;
799
800 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100801 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200802 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100803
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200804 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200805 if (argend == NULL)
806 return;
807 if (argend > arg && argend[-1] == '.') // for var.='str'
808 --argend;
809 expr = skipwhite(argend);
810 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +0200811 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200812 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200813 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
814 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200815 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200816 {
817 // ":let" without "=": list variables
818 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000819 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200820 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000821 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200822 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200823 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200824 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200825 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100826 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +0000827 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100828 else
829 // Vim9 declaration ":var name: type"
830 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200831 }
832 else
833 {
834 // ":let var1 var2" - list values
835 arg = list_arg_vars(eap, arg, &first);
836 }
837 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200838 else if (!eap->skip)
839 {
840 // ":let"
841 list_glob_vars(&first);
842 list_buf_vars(&first);
843 list_win_vars(&first);
844 list_tab_vars(&first);
845 list_script_vars(&first);
846 list_func_vars(&first);
847 list_vim_vars(&first);
848 }
Bram Moolenaar63b91732021-08-05 20:40:03 +0200849 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200850 }
851 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
852 {
853 list_T *l;
Bram Moolenaar81530e32021-07-28 21:25:49 +0200854 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200855
856 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200857 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200858 if (l != NULL)
859 {
860 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200861 if (!eap->skip)
862 {
Bram Moolenaar81530e32021-07-28 21:25:49 +0200863 // errors are for the assignment, not the end marker
864 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200865 op[0] = '=';
866 op[1] = NUL;
867 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200869 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200870 clear_tv(&rettv);
871 }
872 }
873 else
874 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200875 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200876 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200877
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200878 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200879 i = FAIL;
880 if (has_assign || concat)
881 {
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100882 int cur_lnum;
883
Bram Moolenaar32e35112020-05-14 22:41:15 +0200884 op[0] = '=';
885 op[1] = NUL;
886 if (*expr != '=')
887 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200888 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200889 {
890 // +=, /=, etc. require an existing variable
891 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
892 i = FAIL;
893 }
894 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200895 {
896 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200897 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200898 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200899 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200900 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200901 ++len;
902 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200903 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200904 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200905 }
906 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200907 ++expr;
908
Bram Moolenaar7f2c3412021-11-29 16:01:49 +0000909 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200910 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200911 {
912 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100913 semsg(_(e_white_space_required_before_and_after_str_at_str),
914 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200915 i = FAIL;
916 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200917
918 if (eap->skip)
919 ++emsg_skip;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200920 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200921 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100922 cur_lnum = SOURCING_LNUM;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200923 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200924 if (eap->skip)
925 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200926 clear_evalarg(&evalarg, eap);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100927
928 // Restore the line number so that any type error is given for the
929 // declaration, not the expression.
930 SOURCING_LNUM = cur_lnum;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200931 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200932 if (eap->skip)
933 {
934 if (i != FAIL)
935 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200936 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200937 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200938 {
939 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200940 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200941 clear_tv(&rettv);
942 }
943 }
944}
945
946/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +0100947 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200948 * Handles both "var" with any type and "[var, var; var]" with a list type.
949 * When "op" is not NULL it points to a string with characters that
950 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
951 * or concatenate.
952 * Returns OK or FAIL;
953 */
954 int
955ex_let_vars(
956 char_u *arg_start,
957 typval_T *tv,
958 int copy, // copy values from "tv", don't move
959 int semicolon, // from skip_var_list()
960 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100961 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200962 char_u *op)
963{
964 char_u *arg = arg_start;
965 list_T *l;
966 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100967 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200968 listitem_T *item;
969 typval_T ltv;
970
971 if (*arg != '[')
972 {
973 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100974 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200975 return FAIL;
976 return OK;
977 }
978
979 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
980 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
981 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000982 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200983 return FAIL;
984 }
985
986 i = list_len(l);
987 if (semicolon == 0 && var_count < i)
988 {
Bram Moolenaara6f79292022-01-04 21:30:47 +0000989 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200990 return FAIL;
991 }
992 if (var_count - semicolon > i)
993 {
Bram Moolenaara6f79292022-01-04 21:30:47 +0000994 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200995 return FAIL;
996 }
997
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200998 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200999 item = l->lv_first;
1000 while (*arg != ']')
1001 {
1002 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001003 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001004 arg = ex_let_one(arg, &item->li_tv, TRUE,
1005 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001006 item = item->li_next;
1007 if (arg == NULL)
1008 return FAIL;
1009
1010 arg = skipwhite(arg);
1011 if (*arg == ';')
1012 {
1013 // Put the rest of the list (may be empty) in the var after ';'.
1014 // Create a new list for this.
1015 l = list_alloc();
1016 if (l == NULL)
1017 return FAIL;
1018 while (item != NULL)
1019 {
1020 list_append_tv(l, &item->li_tv);
1021 item = item->li_next;
1022 }
1023
1024 ltv.v_type = VAR_LIST;
1025 ltv.v_lock = 0;
1026 ltv.vval.v_list = l;
1027 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001028 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001029
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001030 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1031 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001032 clear_tv(&ltv);
1033 if (arg == NULL)
1034 return FAIL;
1035 break;
1036 }
1037 else if (*arg != ',' && *arg != ']')
1038 {
1039 internal_error("ex_let_vars()");
1040 return FAIL;
1041 }
1042 }
1043
1044 return OK;
1045}
1046
1047/*
1048 * Skip over assignable variable "var" or list of variables "[var, var]".
1049 * Used for ":let varvar = expr" and ":for varvar in expr".
1050 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001051 * for "[var, var; var]" set "semicolon" to 1.
1052 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001053 * Return NULL for an error.
1054 */
1055 char_u *
1056skip_var_list(
1057 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001058 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001059 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001060 int *semicolon,
1061 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001062{
1063 char_u *p, *s;
1064
1065 if (*arg == '[')
1066 {
1067 // "[var, var]": find the matching ']'.
1068 p = arg;
1069 for (;;)
1070 {
1071 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001072 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001073 if (s == p)
1074 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001075 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001076 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001077 return NULL;
1078 }
1079 ++*var_count;
1080
1081 p = skipwhite(s);
1082 if (*p == ']')
1083 break;
1084 else if (*p == ';')
1085 {
1086 if (*semicolon == 1)
1087 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001088 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001089 return NULL;
1090 }
1091 *semicolon = 1;
1092 }
1093 else if (*p != ',')
1094 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001095 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001096 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001097 return NULL;
1098 }
1099 }
1100 return p + 1;
1101 }
1102 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001103 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001104}
1105
1106/*
1107 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1108 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001110 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001111 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001112skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001113{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001114 char_u *end;
1115 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001117 if (*arg == '@' && arg[1] != NUL)
1118 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001120 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001121
1122 // "a: type" is declaring variable "a" with a type, not "a:".
1123 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001124 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001125 --end;
1126
Bram Moolenaar585587d2021-01-17 20:52:13 +01001127 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001128 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001129 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001130 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001131 }
1132 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001133}
1134
1135/*
1136 * List variables for hashtab "ht" with prefix "prefix".
1137 * If "empty" is TRUE also list NULL strings as empty strings.
1138 */
1139 void
1140list_hashtable_vars(
1141 hashtab_T *ht,
1142 char *prefix,
1143 int empty,
1144 int *first)
1145{
1146 hashitem_T *hi;
1147 dictitem_T *di;
1148 int todo;
1149 char_u buf[IOSIZE];
1150
1151 todo = (int)ht->ht_used;
1152 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1153 {
1154 if (!HASHITEM_EMPTY(hi))
1155 {
1156 --todo;
1157 di = HI2DI(hi);
1158
1159 // apply :filter /pat/ to variable name
1160 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1161 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1162 if (message_filtered(buf))
1163 continue;
1164
1165 if (empty || di->di_tv.v_type != VAR_STRING
1166 || di->di_tv.vval.v_string != NULL)
1167 list_one_var(di, prefix, first);
1168 }
1169 }
1170}
1171
1172/*
1173 * List global variables.
1174 */
1175 static void
1176list_glob_vars(int *first)
1177{
1178 list_hashtable_vars(&globvarht, "", TRUE, first);
1179}
1180
1181/*
1182 * List buffer variables.
1183 */
1184 static void
1185list_buf_vars(int *first)
1186{
1187 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1188}
1189
1190/*
1191 * List window variables.
1192 */
1193 static void
1194list_win_vars(int *first)
1195{
1196 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1197}
1198
1199/*
1200 * List tab page variables.
1201 */
1202 static void
1203list_tab_vars(int *first)
1204{
1205 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1206}
1207
1208/*
1209 * List variables in "arg".
1210 */
1211 static char_u *
1212list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1213{
1214 int error = FALSE;
1215 int len;
1216 char_u *name;
1217 char_u *name_start;
1218 char_u *arg_subsc;
1219 char_u *tofree;
1220 typval_T tv;
1221
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001222 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001223 {
1224 if (error || eap->skip)
1225 {
1226 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1227 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1228 {
1229 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001230 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001231 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001232 break;
1233 }
1234 }
1235 else
1236 {
1237 // get_name_len() takes care of expanding curly braces
1238 name_start = name = arg;
1239 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1240 if (len <= 0)
1241 {
1242 // This is mainly to keep test 49 working: when expanding
1243 // curly braces fails overrule the exception error message.
1244 if (len < 0 && !aborting())
1245 {
1246 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001247 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001248 break;
1249 }
1250 error = TRUE;
1251 }
1252 else
1253 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001254 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001255 if (tofree != NULL)
1256 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001257 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001258 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001259 error = TRUE;
1260 else
1261 {
1262 // handle d.key, l[idx], f(expr)
1263 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001264 if (handle_subscript(&arg, name_start, &tv,
1265 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001266 error = TRUE;
1267 else
1268 {
1269 if (arg == arg_subsc && len == 2 && name[1] == ':')
1270 {
1271 switch (*name)
1272 {
1273 case 'g': list_glob_vars(first); break;
1274 case 'b': list_buf_vars(first); break;
1275 case 'w': list_win_vars(first); break;
1276 case 't': list_tab_vars(first); break;
1277 case 'v': list_vim_vars(first); break;
1278 case 's': list_script_vars(first); break;
1279 case 'l': list_func_vars(first); break;
1280 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001281 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001282 }
1283 }
1284 else
1285 {
1286 char_u numbuf[NUMBUFLEN];
1287 char_u *tf;
1288 int c;
1289 char_u *s;
1290
1291 s = echo_string(&tv, &tf, numbuf, 0);
1292 c = *arg;
1293 *arg = NUL;
1294 list_one_var_a("",
1295 arg == arg_subsc ? name : name_start,
1296 tv.v_type,
1297 s == NULL ? (char_u *)"" : s,
1298 first);
1299 *arg = c;
1300 vim_free(tf);
1301 }
1302 clear_tv(&tv);
1303 }
1304 }
1305 }
1306
1307 vim_free(tofree);
1308 }
1309
1310 arg = skipwhite(arg);
1311 }
1312
1313 return arg;
1314}
1315
1316/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001317 * Set an environment variable, part of ex_let_one().
1318 */
1319 static char_u *
1320ex_let_env(
1321 char_u *arg,
1322 typval_T *tv,
1323 int flags,
1324 char_u *endchars,
1325 char_u *op)
1326{
1327 char_u *arg_end = NULL;
1328 char_u *name;
1329 int len;
1330
1331 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1332 && (flags & ASSIGN_FOR_LOOP) == 0)
1333 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001334 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001335 return NULL;
1336 }
1337
1338 // Find the end of the name.
1339 ++arg;
1340 name = arg;
1341 len = get_env_len(&arg);
1342 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001343 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001344 else
1345 {
1346 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001347 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001348 else if (endchars != NULL
1349 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1350 emsg(_(e_unexpected_characters_in_let));
1351 else if (!check_secure())
1352 {
1353 char_u *tofree = NULL;
1354 int c1 = name[len];
1355 char_u *p;
1356
1357 name[len] = NUL;
1358 p = tv_get_string_chk(tv);
1359 if (p != NULL && op != NULL && *op == '.')
1360 {
1361 int mustfree = FALSE;
1362 char_u *s = vim_getenv(name, &mustfree);
1363
1364 if (s != NULL)
1365 {
1366 p = tofree = concat_str(s, p);
1367 if (mustfree)
1368 vim_free(s);
1369 }
1370 }
1371 if (p != NULL)
1372 {
1373 vim_setenv_ext(name, p);
1374 arg_end = arg;
1375 }
1376 name[len] = c1;
1377 vim_free(tofree);
1378 }
1379 }
1380 return arg_end;
1381}
1382
1383/*
1384 * Set an option, part of ex_let_one().
1385 */
1386 static char_u *
1387ex_let_option(
1388 char_u *arg,
1389 typval_T *tv,
1390 int flags,
1391 char_u *endchars,
1392 char_u *op)
1393{
1394 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001395 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001396 char_u *arg_end = NULL;
1397
1398 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1399 && (flags & ASSIGN_FOR_LOOP) == 0)
1400 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001401 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001402 return NULL;
1403 }
1404
1405 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001406 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001407 if (p == NULL || (endchars != NULL
1408 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1409 emsg(_(e_unexpected_characters_in_let));
1410 else
1411 {
1412 int c1;
1413 long n = 0;
1414 getoption_T opt_type;
1415 long numval;
1416 char_u *stringval = NULL;
1417 char_u *s = NULL;
1418 int failed = FALSE;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001419 int opt_p_flags;
1420 char_u *tofree = NULL;
Bram Moolenaar92c33eb2021-12-07 11:03:39 +00001421 char_u numbuf[NUMBUFLEN];
1422
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001423 c1 = *p;
1424 *p = NUL;
1425
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001426 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags,
1427 scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001428 if ((opt_type == gov_bool
1429 || opt_type == gov_number
1430 || opt_type == gov_hidden_bool
1431 || opt_type == gov_hidden_number)
1432 && (tv->v_type != VAR_STRING || !in_vim9script()))
1433 {
1434 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1435 // bool, possibly hidden
1436 n = (long)tv_get_bool(tv);
1437 else
1438 // number, possibly hidden
1439 n = (long)tv_get_number(tv);
1440 }
1441
Bram Moolenaaref082e12021-12-12 21:02:03 +00001442 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001443 || tv->v_type == VAR_FUNC))
1444 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001445 // If the option can be set to a function reference or a lambda
1446 // and the passed value is a function reference, then convert it to
1447 // the name (string) of the function reference.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001448 s = tv2string(tv, &tofree, numbuf, 0);
1449 }
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001450 // Avoid setting a string option to the text "v:false" or similar.
1451 // In Vim9 script also don't convert a number to string.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001452 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001453 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1454 s = tv_get_string_chk(tv);
1455
1456 if (op != NULL && *op != '=')
1457 {
1458 if (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1459 || (opt_type == gov_string && *op != '.'))
1460 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001461 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001462 failed = TRUE; // don't set the value
1463
1464 }
1465 else
1466 {
1467 // number, in legacy script also bool
1468 if (opt_type == gov_number
1469 || (opt_type == gov_bool && !in_vim9script()))
1470 {
1471 switch (*op)
1472 {
1473 case '+': n = numval + n; break;
1474 case '-': n = numval - n; break;
1475 case '*': n = numval * n; break;
1476 case '/': n = (long)num_divide(numval, n,
1477 &failed); break;
1478 case '%': n = (long)num_modulus(numval, n,
1479 &failed); break;
1480 }
1481 s = NULL;
1482 }
1483 else if (opt_type == gov_string
1484 && stringval != NULL && s != NULL)
1485 {
1486 // string
1487 s = concat_str(stringval, s);
1488 vim_free(stringval);
1489 stringval = s;
1490 }
1491 }
1492 }
1493
1494 if (!failed)
1495 {
1496 if (opt_type != gov_string || s != NULL)
1497 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001498 set_option_value(arg, n, s, scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001499 arg_end = p;
1500 }
1501 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001502 emsg(_(e_string_required));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001503 }
1504 *p = c1;
1505 vim_free(stringval);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001506 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001507 }
1508 return arg_end;
1509}
1510
1511/*
1512 * Set a register, part of ex_let_one().
1513 */
1514 static char_u *
1515ex_let_register(
1516 char_u *arg,
1517 typval_T *tv,
1518 int flags,
1519 char_u *endchars,
1520 char_u *op)
1521{
1522 char_u *arg_end = NULL;
1523
1524 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1525 && (flags & ASSIGN_FOR_LOOP) == 0)
1526 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001527 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001528 return NULL;
1529 }
1530 ++arg;
1531 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001532 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001533 else if (endchars != NULL
1534 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1535 emsg(_(e_unexpected_characters_in_let));
1536 else
1537 {
1538 char_u *ptofree = NULL;
1539 char_u *p;
1540
1541 p = tv_get_string_chk(tv);
1542 if (p != NULL && op != NULL && *op == '.')
1543 {
1544 char_u *s = get_reg_contents(*arg == '@'
1545 ? '"' : *arg, GREG_EXPR_SRC);
1546
1547 if (s != NULL)
1548 {
1549 p = ptofree = concat_str(s, p);
1550 vim_free(s);
1551 }
1552 }
1553 if (p != NULL)
1554 {
1555 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1556 arg_end = arg + 1;
1557 }
1558 vim_free(ptofree);
1559 }
1560 return arg_end;
1561}
1562
1563/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001564 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1565 * Returns a pointer to the char just after the var name.
1566 * Returns NULL if there is an error.
1567 */
1568 static char_u *
1569ex_let_one(
1570 char_u *arg, // points to variable name
1571 typval_T *tv, // value to assign to variable
1572 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001573 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001574 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001575 char_u *op, // "+", "-", "." or NULL
1576 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001577{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001578 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001579
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001580 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001581 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001582 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1583 {
1584 vim9_declare_error(arg);
1585 return NULL;
1586 }
1587
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001588 if (*arg == '$')
1589 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001590 // ":let $VAR = expr": Set environment variable.
1591 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001592 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001593 else if (*arg == '&')
1594 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001595 // ":let &option = expr": Set option value.
1596 // ":let &l:option = expr": Set local option value.
1597 // ":let &g:option = expr": Set global option value.
1598 // ":for &ts in range(8)": Set option value for for loop
1599 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001600 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001601 else if (*arg == '@')
1602 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001603 // ":let @r = expr": Set register contents.
1604 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001605 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001606 else if (eval_isnamec1(*arg) || *arg == '{')
1607 {
1608 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001609 char_u *p;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001610
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001611 // ":let var = expr": Set internal variable.
1612 // ":let var: type = expr": Set internal variable with type.
1613 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001614 p = get_lval(arg, tv, &lv, FALSE, FALSE,
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001615 (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1616 ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001617 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001618 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001619 if (endchars != NULL && vim_strchr(endchars,
1620 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001621 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001622 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001623 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001624 else
1625 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001626 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001627 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001628 }
1629 }
1630 clear_lval(&lv);
1631 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001632 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001633 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001634
1635 return arg_end;
1636}
1637
1638/*
1639 * ":unlet[!] var1 ... " command.
1640 */
1641 void
1642ex_unlet(exarg_T *eap)
1643{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001644 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001645}
1646
1647/*
1648 * ":lockvar" and ":unlockvar" commands
1649 */
1650 void
1651ex_lockvar(exarg_T *eap)
1652{
1653 char_u *arg = eap->arg;
1654 int deep = 2;
1655
1656 if (eap->forceit)
1657 deep = -1;
1658 else if (vim_isdigit(*arg))
1659 {
1660 deep = getdigits(&arg);
1661 arg = skipwhite(arg);
1662 }
1663
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001664 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001665}
1666
1667/*
1668 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001669 * Also used for Vim9 script. "callback" is invoked as:
1670 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001671 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001672 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001673ex_unletlock(
1674 exarg_T *eap,
1675 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001676 int deep,
1677 int glv_flags,
1678 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1679 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001680{
1681 char_u *arg = argstart;
1682 char_u *name_end;
1683 int error = FALSE;
1684 lval_T lv;
1685
1686 do
1687 {
1688 if (*arg == '$')
1689 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001690 lv.ll_name = arg;
1691 lv.ll_tv = NULL;
1692 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001693 if (get_env_len(&arg) == 0)
1694 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001695 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001696 return;
1697 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001698 if (!error && !eap->skip
1699 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1700 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001701 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001702 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001703 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001704 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001705 // Parse the name and find the end.
1706 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001707 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001708 if (lv.ll_name == NULL)
1709 error = TRUE; // error but continue parsing
1710 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1711 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001712 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001713 if (name_end != NULL)
1714 {
1715 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00001716 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001717 }
1718 if (!(eap->skip || error))
1719 clear_lval(&lv);
1720 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001721 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001722
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001723 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001724 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001725 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001726
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001727 if (!eap->skip)
1728 clear_lval(&lv);
1729 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001730
1731 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001732 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001733
Bram Moolenaar63b91732021-08-05 20:40:03 +02001734 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001735}
1736
1737 static int
1738do_unlet_var(
1739 lval_T *lp,
1740 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001741 exarg_T *eap,
1742 int deep UNUSED,
1743 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001744{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001745 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001746 int ret = OK;
1747 int cc;
1748
1749 if (lp->ll_tv == NULL)
1750 {
1751 cc = *name_end;
1752 *name_end = NUL;
1753
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001754 // Environment variable, normal name or expanded name.
1755 if (*lp->ll_name == '$')
1756 vim_unsetenv(lp->ll_name + 1);
1757 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001758 ret = FAIL;
1759 *name_end = cc;
1760 }
1761 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001762 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001763 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001764 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001765 return FAIL;
1766 else if (lp->ll_range)
1767 {
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001768 if (list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_name, lp->ll_n1,
1769 !lp->ll_empty2, lp->ll_n2) == FAIL)
1770 return FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001771 }
1772 else
1773 {
1774 if (lp->ll_list != NULL)
1775 // unlet a List item.
1776 listitem_remove(lp->ll_list, lp->ll_li);
1777 else
1778 // unlet a Dictionary item.
1779 dictitem_remove(lp->ll_dict, lp->ll_di);
1780 }
1781
1782 return ret;
1783}
1784
1785/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001786 * Unlet one item or a range of items from a list.
1787 * Return OK or FAIL.
1788 */
1789 int
1790list_unlet_range(
1791 list_T *l,
1792 listitem_T *li_first,
1793 char_u *name,
1794 long n1_arg,
1795 int has_n2,
1796 long n2)
1797{
1798 listitem_T *li = li_first;
1799 int n1 = n1_arg;
1800
1801 while (li != NULL && (!has_n2 || n2 >= n1))
1802 {
1803 if (value_check_lock(li->li_tv.v_lock, name, FALSE))
1804 return FAIL;
1805 li = li->li_next;
1806 ++n1;
1807 }
1808
1809 // Delete a range of List items.
1810 li = li_first;
1811 n1 = n1_arg;
1812 while (li != NULL && (!has_n2 || n2 >= n1))
1813 {
1814 listitem_T *next = li->li_next;
1815
1816 listitem_remove(l, li);
1817 li = next;
1818 ++n1;
1819 }
1820 return OK;
1821}
1822/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001823 * "unlet" a variable. Return OK if it existed, FAIL if not.
1824 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1825 */
1826 int
1827do_unlet(char_u *name, int forceit)
1828{
1829 hashtab_T *ht;
1830 hashitem_T *hi;
1831 char_u *varname;
1832 dict_T *d;
1833 dictitem_T *di;
1834
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001835 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001836 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001837 return FAIL;
1838
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001839 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001840
1841 // can't :unlet a script variable in Vim9 script from a function
1842 if (ht == get_script_local_ht()
1843 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1844 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1845 == SCRIPT_VERSION_VIM9
1846 && check_vim9_unlet(name) == FAIL)
1847 return FAIL;
1848
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001849 if (ht != NULL && *varname != NUL)
1850 {
1851 d = get_current_funccal_dict(ht);
1852 if (d == NULL)
1853 {
1854 if (ht == &globvarht)
1855 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001856 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001857 d = &vimvardict;
1858 else
1859 {
1860 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1861 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1862 }
1863 if (d == NULL)
1864 {
1865 internal_error("do_unlet()");
1866 return FAIL;
1867 }
1868 }
1869 hi = hash_find(ht, varname);
1870 if (HASHITEM_EMPTY(hi))
1871 hi = find_hi_in_scoped_ht(name, &ht);
1872 if (hi != NULL && !HASHITEM_EMPTY(hi))
1873 {
1874 di = HI2DI(hi);
1875 if (var_check_fixed(di->di_flags, name, FALSE)
1876 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001877 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001878 return FAIL;
1879
1880 delete_var(ht, hi);
1881 return OK;
1882 }
1883 }
1884 if (forceit)
1885 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00001886 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001887 return FAIL;
1888}
1889
1890/*
1891 * Lock or unlock variable indicated by "lp".
1892 * "deep" is the levels to go (-1 for unlimited);
1893 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1894 */
1895 static int
1896do_lock_var(
1897 lval_T *lp,
1898 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001899 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001900 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001901 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001902{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001903 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001904 int ret = OK;
1905 int cc;
1906 dictitem_T *di;
1907
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001908 if (lp->ll_tv == NULL)
1909 {
1910 cc = *name_end;
1911 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001912 if (*lp->ll_name == '$')
1913 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001914 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001915 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001916 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001917 else
1918 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001919 // Normal name or expanded name.
1920 di = find_var(lp->ll_name, NULL, TRUE);
1921 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001922 {
1923 if (in_vim9script())
1924 semsg(_(e_cannot_find_variable_to_unlock_str),
1925 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001926 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001927 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001928 else if ((di->di_flags & DI_FLAGS_FIX)
1929 && di->di_tv.v_type != VAR_DICT
1930 && di->di_tv.v_type != VAR_LIST)
1931 {
1932 // For historic reasons this error is not given for a list or
1933 // dict. E.g., the b: dict could be locked/unlocked.
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001934 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001935 ret = FAIL;
1936 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001937 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001938 {
1939 if (lock)
1940 di->di_flags |= DI_FLAGS_LOCK;
1941 else
1942 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001943 if (deep != 0)
1944 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001945 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001946 }
1947 *name_end = cc;
1948 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001949 else if (deep == 0)
1950 {
1951 // nothing to do
1952 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001953 else if (lp->ll_range)
1954 {
1955 listitem_T *li = lp->ll_li;
1956
1957 // (un)lock a range of List items.
1958 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1959 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001960 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001961 li = li->li_next;
1962 ++lp->ll_n1;
1963 }
1964 }
1965 else if (lp->ll_list != NULL)
1966 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001967 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001968 else
1969 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001970 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001971
1972 return ret;
1973}
1974
1975/*
1976 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001977 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1978 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001979 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001980 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001981item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001982{
1983 static int recurse = 0;
1984 list_T *l;
1985 listitem_T *li;
1986 dict_T *d;
1987 blob_T *b;
1988 hashitem_T *hi;
1989 int todo;
1990
1991 if (recurse >= DICT_MAXNEST)
1992 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00001993 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001994 return;
1995 }
1996 if (deep == 0)
1997 return;
1998 ++recurse;
1999
2000 // lock/unlock the item itself
2001 if (lock)
2002 tv->v_lock |= VAR_LOCKED;
2003 else
2004 tv->v_lock &= ~VAR_LOCKED;
2005
2006 switch (tv->v_type)
2007 {
2008 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002009 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002011 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002012 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002013 case VAR_STRING:
2014 case VAR_FUNC:
2015 case VAR_PARTIAL:
2016 case VAR_FLOAT:
2017 case VAR_SPECIAL:
2018 case VAR_JOB:
2019 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002020 case VAR_INSTR:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002021 break;
2022
2023 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002024 if ((b = tv->vval.v_blob) != NULL
2025 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002026 {
2027 if (lock)
2028 b->bv_lock |= VAR_LOCKED;
2029 else
2030 b->bv_lock &= ~VAR_LOCKED;
2031 }
2032 break;
2033 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002034 if ((l = tv->vval.v_list) != NULL
2035 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002036 {
2037 if (lock)
2038 l->lv_lock |= VAR_LOCKED;
2039 else
2040 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002041 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002042 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002043 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02002044 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002045 }
2046 break;
2047 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002048 if ((d = tv->vval.v_dict) != NULL
2049 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002050 {
2051 if (lock)
2052 d->dv_lock |= VAR_LOCKED;
2053 else
2054 d->dv_lock &= ~VAR_LOCKED;
2055 if (deep < 0 || deep > 1)
2056 {
2057 // recursive: lock/unlock the items the List contains
2058 todo = (int)d->dv_hashtab.ht_used;
2059 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2060 {
2061 if (!HASHITEM_EMPTY(hi))
2062 {
2063 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002064 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2065 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002066 }
2067 }
2068 }
2069 }
2070 }
2071 --recurse;
2072}
2073
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002074#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2075/*
2076 * Delete all "menutrans_" variables.
2077 */
2078 void
2079del_menutrans_vars(void)
2080{
2081 hashitem_T *hi;
2082 int todo;
2083
2084 hash_lock(&globvarht);
2085 todo = (int)globvarht.ht_used;
2086 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2087 {
2088 if (!HASHITEM_EMPTY(hi))
2089 {
2090 --todo;
2091 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2092 delete_var(&globvarht, hi);
2093 }
2094 }
2095 hash_unlock(&globvarht);
2096}
2097#endif
2098
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002099/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002100 * Local string buffer for the next two functions to store a variable name
2101 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2102 * get_user_var_name().
2103 */
2104
2105static char_u *varnamebuf = NULL;
2106static int varnamebuflen = 0;
2107
2108/*
2109 * Function to concatenate a prefix and a variable name.
2110 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002111 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002112cat_prefix_varname(int prefix, char_u *name)
2113{
2114 int len;
2115
2116 len = (int)STRLEN(name) + 3;
2117 if (len > varnamebuflen)
2118 {
2119 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002120 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002121 varnamebuf = alloc(len);
2122 if (varnamebuf == NULL)
2123 {
2124 varnamebuflen = 0;
2125 return NULL;
2126 }
2127 varnamebuflen = len;
2128 }
2129 *varnamebuf = prefix;
2130 varnamebuf[1] = ':';
2131 STRCPY(varnamebuf + 2, name);
2132 return varnamebuf;
2133}
2134
2135/*
2136 * Function given to ExpandGeneric() to obtain the list of user defined
2137 * (global/buffer/window/built-in) variable names.
2138 */
2139 char_u *
2140get_user_var_name(expand_T *xp, int idx)
2141{
2142 static long_u gdone;
2143 static long_u bdone;
2144 static long_u wdone;
2145 static long_u tdone;
2146 static int vidx;
2147 static hashitem_T *hi;
2148 hashtab_T *ht;
2149
2150 if (idx == 0)
2151 {
2152 gdone = bdone = wdone = vidx = 0;
2153 tdone = 0;
2154 }
2155
2156 // Global variables
2157 if (gdone < globvarht.ht_used)
2158 {
2159 if (gdone++ == 0)
2160 hi = globvarht.ht_array;
2161 else
2162 ++hi;
2163 while (HASHITEM_EMPTY(hi))
2164 ++hi;
2165 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2166 return cat_prefix_varname('g', hi->hi_key);
2167 return hi->hi_key;
2168 }
2169
2170 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002171 ht =
2172#ifdef FEAT_CMDWIN
2173 // In cmdwin, the alternative buffer should be used.
mityua1198122021-11-20 19:13:39 +00002174 is_in_cmdwin() ? &prevwin->w_buffer->b_vars->dv_hashtab :
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002175#endif
2176 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002177 if (bdone < ht->ht_used)
2178 {
2179 if (bdone++ == 0)
2180 hi = ht->ht_array;
2181 else
2182 ++hi;
2183 while (HASHITEM_EMPTY(hi))
2184 ++hi;
2185 return cat_prefix_varname('b', hi->hi_key);
2186 }
2187
2188 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002189 ht =
2190#ifdef FEAT_CMDWIN
2191 // In cmdwin, the alternative window should be used.
mityua1198122021-11-20 19:13:39 +00002192 is_in_cmdwin() ? &prevwin->w_vars->dv_hashtab :
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002193#endif
2194 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002195 if (wdone < ht->ht_used)
2196 {
2197 if (wdone++ == 0)
2198 hi = ht->ht_array;
2199 else
2200 ++hi;
2201 while (HASHITEM_EMPTY(hi))
2202 ++hi;
2203 return cat_prefix_varname('w', hi->hi_key);
2204 }
2205
2206 // t: variables
2207 ht = &curtab->tp_vars->dv_hashtab;
2208 if (tdone < ht->ht_used)
2209 {
2210 if (tdone++ == 0)
2211 hi = ht->ht_array;
2212 else
2213 ++hi;
2214 while (HASHITEM_EMPTY(hi))
2215 ++hi;
2216 return cat_prefix_varname('t', hi->hi_key);
2217 }
2218
2219 // v: variables
2220 if (vidx < VV_LEN)
2221 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2222
2223 VIM_CLEAR(varnamebuf);
2224 varnamebuflen = 0;
2225 return NULL;
2226}
2227
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002228 char *
2229get_var_special_name(int nr)
2230{
2231 switch (nr)
2232 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002233 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2234 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002235 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002236 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002237 }
2238 internal_error("get_var_special_name()");
2239 return "42";
2240}
2241
2242/*
2243 * Returns the global variable dictionary
2244 */
2245 dict_T *
2246get_globvar_dict(void)
2247{
2248 return &globvardict;
2249}
2250
2251/*
2252 * Returns the global variable hash table
2253 */
2254 hashtab_T *
2255get_globvar_ht(void)
2256{
2257 return &globvarht;
2258}
2259
2260/*
2261 * Returns the v: variable dictionary
2262 */
2263 dict_T *
2264get_vimvar_dict(void)
2265{
2266 return &vimvardict;
2267}
2268
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002269/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002270 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002271 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002272 */
2273 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002274find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002275{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002276 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2277 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002278
2279 if (di == NULL)
2280 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002281 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002282 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2283 return (int)(vv - vimvars);
2284}
2285
2286
2287/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002288 * Set type of v: variable to "type".
2289 */
2290 void
2291set_vim_var_type(int idx, vartype_T type)
2292{
Bram Moolenaard787e402021-12-24 21:36:12 +00002293 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002294}
2295
2296/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002297 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002298 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002299 */
2300 void
2301set_vim_var_nr(int idx, varnumber_T val)
2302{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002303 vimvars[idx].vv_nr = val;
2304}
2305
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002306 char *
2307get_vim_var_name(int idx)
2308{
2309 return vimvars[idx].vv_name;
2310}
2311
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002312/*
2313 * Get typval_T v: variable value.
2314 */
2315 typval_T *
2316get_vim_var_tv(int idx)
2317{
2318 return &vimvars[idx].vv_tv;
2319}
2320
Bram Moolenaard787e402021-12-24 21:36:12 +00002321 type_T *
2322get_vim_var_type(int idx, garray_T *type_list)
2323{
2324 if (vimvars[idx].vv_type != NULL)
2325 return vimvars[idx].vv_type;
2326 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2327}
2328
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002329/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002330 * Set v: variable to "tv". Only accepts the same type.
2331 * Takes over the value of "tv".
2332 */
2333 int
2334set_vim_var_tv(int idx, typval_T *tv)
2335{
Bram Moolenaard787e402021-12-24 21:36:12 +00002336 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002337 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002338 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002339 clear_tv(tv);
2340 return FAIL;
2341 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002342 // VV_RO is also checked when compiling, but let's check here as well.
2343 if (vimvars[idx].vv_flags & VV_RO)
2344 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002345 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002346 return FAIL;
2347 }
2348 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2349 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002350 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002351 return FAIL;
2352 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002353 clear_tv(&vimvars[idx].vv_di.di_tv);
2354 vimvars[idx].vv_di.di_tv = *tv;
2355 return OK;
2356}
2357
2358/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002359 * Get number v: variable value.
2360 */
2361 varnumber_T
2362get_vim_var_nr(int idx)
2363{
2364 return vimvars[idx].vv_nr;
2365}
2366
2367/*
2368 * Get string v: variable value. Uses a static buffer, can only be used once.
2369 * If the String variable has never been set, return an empty string.
2370 * Never returns NULL;
2371 */
2372 char_u *
2373get_vim_var_str(int idx)
2374{
2375 return tv_get_string(&vimvars[idx].vv_tv);
2376}
2377
2378/*
2379 * Get List v: variable value. Caller must take care of reference count when
2380 * needed.
2381 */
2382 list_T *
2383get_vim_var_list(int idx)
2384{
2385 return vimvars[idx].vv_list;
2386}
2387
2388/*
2389 * Get Dict v: variable value. Caller must take care of reference count when
2390 * needed.
2391 */
2392 dict_T *
2393get_vim_var_dict(int idx)
2394{
2395 return vimvars[idx].vv_dict;
2396}
2397
2398/*
2399 * Set v:char to character "c".
2400 */
2401 void
2402set_vim_var_char(int c)
2403{
2404 char_u buf[MB_MAXBYTES + 1];
2405
2406 if (has_mbyte)
2407 buf[(*mb_char2bytes)(c, buf)] = NUL;
2408 else
2409 {
2410 buf[0] = c;
2411 buf[1] = NUL;
2412 }
2413 set_vim_var_string(VV_CHAR, buf, -1);
2414}
2415
2416/*
2417 * Set v:count to "count" and v:count1 to "count1".
2418 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2419 */
2420 void
2421set_vcount(
2422 long count,
2423 long count1,
2424 int set_prevcount)
2425{
2426 if (set_prevcount)
2427 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2428 vimvars[VV_COUNT].vv_nr = count;
2429 vimvars[VV_COUNT1].vv_nr = count1;
2430}
2431
2432/*
2433 * Save variables that might be changed as a side effect. Used when executing
2434 * a timer callback.
2435 */
2436 void
2437save_vimvars(vimvars_save_T *vvsave)
2438{
2439 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2440 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2441 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2442}
2443
2444/*
2445 * Restore variables saved by save_vimvars().
2446 */
2447 void
2448restore_vimvars(vimvars_save_T *vvsave)
2449{
2450 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2451 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2452 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2453}
2454
2455/*
2456 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2457 * value.
2458 */
2459 void
2460set_vim_var_string(
2461 int idx,
2462 char_u *val,
2463 int len) // length of "val" to use or -1 (whole string)
2464{
2465 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002466 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002467 if (val == NULL)
2468 vimvars[idx].vv_str = NULL;
2469 else if (len == -1)
2470 vimvars[idx].vv_str = vim_strsave(val);
2471 else
2472 vimvars[idx].vv_str = vim_strnsave(val, len);
2473}
2474
2475/*
2476 * Set List v: variable to "val".
2477 */
2478 void
2479set_vim_var_list(int idx, list_T *val)
2480{
2481 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002482 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002483 vimvars[idx].vv_list = val;
2484 if (val != NULL)
2485 ++val->lv_refcount;
2486}
2487
2488/*
2489 * Set Dictionary v: variable to "val".
2490 */
2491 void
2492set_vim_var_dict(int idx, dict_T *val)
2493{
2494 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002495 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002496 vimvars[idx].vv_dict = val;
2497 if (val != NULL)
2498 {
2499 ++val->dv_refcount;
2500 dict_set_items_ro(val);
2501 }
2502}
2503
2504/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002505 * Set the v:argv list.
2506 */
2507 void
2508set_argv_var(char **argv, int argc)
2509{
2510 list_T *l = list_alloc();
2511 int i;
2512
2513 if (l == NULL)
2514 getout(1);
2515 l->lv_lock = VAR_FIXED;
2516 for (i = 0; i < argc; ++i)
2517 {
2518 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2519 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002520 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002521 }
2522 set_vim_var_list(VV_ARGV, l);
2523}
2524
2525/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002526 * Reset v:register, taking the 'clipboard' setting into account.
2527 */
2528 void
2529reset_reg_var(void)
2530{
2531 int regname = 0;
2532
2533 // Adjust the register according to 'clipboard', so that when
2534 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2535#ifdef FEAT_CLIPBOARD
2536 adjust_clip_reg(&regname);
2537#endif
2538 set_reg_var(regname);
2539}
2540
2541/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002542 * Set v:register if needed.
2543 */
2544 void
2545set_reg_var(int c)
2546{
2547 char_u regname;
2548
2549 if (c == 0 || c == ' ')
2550 regname = '"';
2551 else
2552 regname = c;
2553 // Avoid free/alloc when the value is already right.
2554 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2555 set_vim_var_string(VV_REG, &regname, 1);
2556}
2557
2558/*
2559 * Get or set v:exception. If "oldval" == NULL, return the current value.
2560 * Otherwise, restore the value to "oldval" and return NULL.
2561 * Must always be called in pairs to save and restore v:exception! Does not
2562 * take care of memory allocations.
2563 */
2564 char_u *
2565v_exception(char_u *oldval)
2566{
2567 if (oldval == NULL)
2568 return vimvars[VV_EXCEPTION].vv_str;
2569
2570 vimvars[VV_EXCEPTION].vv_str = oldval;
2571 return NULL;
2572}
2573
2574/*
2575 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2576 * Otherwise, restore the value to "oldval" and return NULL.
2577 * Must always be called in pairs to save and restore v:throwpoint! Does not
2578 * take care of memory allocations.
2579 */
2580 char_u *
2581v_throwpoint(char_u *oldval)
2582{
2583 if (oldval == NULL)
2584 return vimvars[VV_THROWPOINT].vv_str;
2585
2586 vimvars[VV_THROWPOINT].vv_str = oldval;
2587 return NULL;
2588}
2589
2590/*
2591 * Set v:cmdarg.
2592 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2593 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2594 * Must always be called in pairs!
2595 */
2596 char_u *
2597set_cmdarg(exarg_T *eap, char_u *oldarg)
2598{
2599 char_u *oldval;
2600 char_u *newval;
2601 unsigned len;
2602
2603 oldval = vimvars[VV_CMDARG].vv_str;
2604 if (eap == NULL)
2605 {
2606 vim_free(oldval);
2607 vimvars[VV_CMDARG].vv_str = oldarg;
2608 return NULL;
2609 }
2610
2611 if (eap->force_bin == FORCE_BIN)
2612 len = 6;
2613 else if (eap->force_bin == FORCE_NOBIN)
2614 len = 8;
2615 else
2616 len = 0;
2617
2618 if (eap->read_edit)
2619 len += 7;
2620
2621 if (eap->force_ff != 0)
2622 len += 10; // " ++ff=unix"
2623 if (eap->force_enc != 0)
2624 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2625 if (eap->bad_char != 0)
2626 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2627
2628 newval = alloc(len + 1);
2629 if (newval == NULL)
2630 return NULL;
2631
2632 if (eap->force_bin == FORCE_BIN)
2633 sprintf((char *)newval, " ++bin");
2634 else if (eap->force_bin == FORCE_NOBIN)
2635 sprintf((char *)newval, " ++nobin");
2636 else
2637 *newval = NUL;
2638
2639 if (eap->read_edit)
2640 STRCAT(newval, " ++edit");
2641
2642 if (eap->force_ff != 0)
2643 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2644 eap->force_ff == 'u' ? "unix"
2645 : eap->force_ff == 'd' ? "dos"
2646 : "mac");
2647 if (eap->force_enc != 0)
2648 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2649 eap->cmd + eap->force_enc);
2650 if (eap->bad_char == BAD_KEEP)
2651 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2652 else if (eap->bad_char == BAD_DROP)
2653 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2654 else if (eap->bad_char != 0)
2655 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2656 vimvars[VV_CMDARG].vv_str = newval;
2657 return oldval;
2658}
2659
2660/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002661 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002662 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2663 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002664 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2665 */
2666 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002667eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002668 char_u *name,
2669 int len, // length of "name"
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002670 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002671 typval_T *rettv, // NULL when only checking existence
2672 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002673 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002674{
2675 int ret = OK;
2676 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002677 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002678 hashtab_T *ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002679 int cc;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002680 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002681
2682 // truncate the name, so that we can use strcmp()
2683 cc = name[len];
2684 name[len] = NUL;
2685
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002686 // Check for local variable when debugging.
2687 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002688 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002689 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002690 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2691
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002692 if (v != NULL)
2693 {
2694 tv = &v->di_tv;
2695 if (dip != NULL)
2696 *dip = v;
2697 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002698 else
2699 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002700 }
2701
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002702 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002704 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002705 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2706
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002707 if (sid == 0)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002708 import = find_imported(p, 0, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709
2710 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002711 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002712 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002713 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002714 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00002715 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02002716 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002717 ht = &SCRIPT_VARS(sid);
2718 if (ht != NULL)
2719 {
2720 dictitem_T *v = find_var_in_ht(ht, 0, name,
2721 flags & EVAL_VAR_NOAUTOLOAD);
2722
2723 if (v != NULL)
2724 {
2725 tv = &v->di_tv;
2726 if (dip != NULL)
2727 *dip = v;
2728 }
2729 else
2730 ht = NULL;
2731 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002732 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002733 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002734 {
2735 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00002736 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002737 ret = FAIL;
2738 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002739 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002740 else
2741 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002742 if (rettv != NULL)
2743 {
2744 rettv->v_type = VAR_ANY;
2745 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
2746 }
2747 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002748 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00002750 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002751 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002752 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002753
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002754 // In Vim9 script we can get a function reference by using the
2755 // function name.
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002756 if (ufunc != NULL)
2757 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002758 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002759 if (rettv != NULL)
2760 {
2761 rettv->v_type = VAR_FUNC;
Bram Moolenaaref082e12021-12-12 21:02:03 +00002762 if (STRNCMP(name, "g:", 2) == 0)
2763 // Keep the "g:", otherwise script-local may be
2764 // assumed.
2765 rettv->vval.v_string = vim_strsave(name);
2766 else
2767 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002768 if (rettv->vval.v_string != NULL)
2769 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002770 }
2771 }
2772 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 }
2774
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002775 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002776 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002777 if (tv == NULL)
2778 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002779 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002780 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002781 ret = FAIL;
2782 }
2783 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002784 {
Bram Moolenaar11005b02021-07-11 20:59:00 +02002785 if (ht != NULL && ht == get_script_local_ht()
2786 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002787 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002788 svar_T *sv = find_typval_in_script(tv, 0);
Bram Moolenaarf055d452021-07-08 20:57:24 +02002789
Bram Moolenaarf055d452021-07-08 20:57:24 +02002790 if (sv != NULL)
2791 type = sv->sv_type;
2792 }
2793
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002794 // If a list or dict variable wasn't initialized, do it now.
2795 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2796 {
2797 tv->vval.v_dict = dict_alloc();
2798 if (tv->vval.v_dict != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002799 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002800 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002801 tv->vval.v_dict->dv_type = alloc_type(type);
2802 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002803 }
2804 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2805 {
2806 tv->vval.v_list = list_alloc();
2807 if (tv->vval.v_list != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002808 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002809 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002810 tv->vval.v_list->lv_type = alloc_type(type);
2811 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002812 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002813 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL)
2814 {
2815 tv->vval.v_blob = blob_alloc();
2816 if (tv->vval.v_blob != NULL)
2817 ++tv->vval.v_blob->bv_refcount;
2818 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002819 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002820 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002821 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002822
2823 name[len] = cc;
2824
2825 return ret;
2826}
2827
2828/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002829 * Check if variable "name[len]" is a local variable or an argument.
2830 * If so, "*eval_lavars_used" is set to TRUE.
2831 */
2832 void
2833check_vars(char_u *name, int len)
2834{
2835 int cc;
2836 char_u *varname;
2837 hashtab_T *ht;
2838
2839 if (eval_lavars_used == NULL)
2840 return;
2841
2842 // truncate the name, so that we can use strcmp()
2843 cc = name[len];
2844 name[len] = NUL;
2845
2846 ht = find_var_ht(name, &varname);
2847 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2848 {
2849 if (find_var(name, NULL, TRUE) != NULL)
2850 *eval_lavars_used = TRUE;
2851 }
2852
2853 name[len] = cc;
2854}
2855
2856/*
2857 * Find variable "name" in the list of variables.
2858 * Return a pointer to it if found, NULL if not found.
2859 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002860 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002861 */
2862 dictitem_T *
2863find_var(char_u *name, hashtab_T **htp, int no_autoload)
2864{
2865 char_u *varname;
2866 hashtab_T *ht;
2867 dictitem_T *ret = NULL;
2868
2869 ht = find_var_ht(name, &varname);
2870 if (htp != NULL)
2871 *htp = ht;
2872 if (ht == NULL)
2873 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002874 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002875 if (ret != NULL)
2876 return ret;
2877
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002878 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002879 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002880 if (ret != NULL)
2881 return ret;
2882
2883 // in Vim9 script items without a scope can be script-local
2884 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2885 {
2886 ht = get_script_local_ht();
2887 if (ht != NULL)
2888 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002889 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002890 if (ret != NULL)
2891 {
2892 if (htp != NULL)
2893 *htp = ht;
2894 return ret;
2895 }
2896 }
2897 }
2898
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002899 // When using "vim9script autoload" script-local items are prefixed but can
2900 // be used with s:name.
2901 if (SCRIPT_ID_VALID(current_sctx.sc_sid)
2902 && name[0] == 's' && name[1] == ':')
2903 {
2904 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2905
2906 if (si->sn_autoload_prefix != NULL)
2907 {
2908 char_u *auto_name = concat_str(si->sn_autoload_prefix, name + 2);
2909
2910 if (auto_name != NULL)
2911 {
2912 ht = &globvarht;
2913 ret = find_var_in_ht(ht, *name, auto_name, TRUE);
Bram Moolenaar130f65d2022-01-13 20:39:41 +00002914 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002915 if (ret != NULL)
2916 {
2917 if (htp != NULL)
2918 *htp = ht;
2919 return ret;
2920 }
2921 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002922 }
2923 }
2924
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002925 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002926}
2927
2928/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00002929 * Like find_var() but if the name starts with <SNR>99_ then look in the
2930 * referenced script (used for a funcref).
2931 */
2932 dictitem_T *
2933find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
2934{
2935 if (STRNCMP(name, "<SNR>", 5) == 0 && isdigit(name[5]))
2936 {
2937 char_u *p = name + 5;
2938 int sid = getdigits(&p);
2939
2940 if (SCRIPT_ID_VALID(sid) && *p == '_')
2941 {
2942 hashtab_T *ht = &SCRIPT_VARS(sid);
2943
2944 if (ht != NULL)
2945 {
2946 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
2947
2948 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00002949 {
2950 if (htp != NULL)
2951 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00002952 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00002953 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00002954 }
2955 }
2956 }
2957
2958 return find_var(name, htp, no_autoload);
2959}
2960
2961/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002962 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002963 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002964 * Returns NULL if not found.
2965 */
2966 dictitem_T *
2967find_var_in_ht(
2968 hashtab_T *ht,
2969 int htname,
2970 char_u *varname,
2971 int no_autoload)
2972{
2973 hashitem_T *hi;
2974
2975 if (*varname == NUL)
2976 {
2977 // Must be something like "s:", otherwise "ht" would be NULL.
2978 switch (htname)
2979 {
2980 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2981 case 'g': return &globvars_var;
2982 case 'v': return &vimvars_var;
2983 case 'b': return &curbuf->b_bufvar;
2984 case 'w': return &curwin->w_winvar;
2985 case 't': return &curtab->tp_winvar;
2986 case 'l': return get_funccal_local_var();
2987 case 'a': return get_funccal_args_var();
2988 }
2989 return NULL;
2990 }
2991
2992 hi = hash_find(ht, varname);
2993 if (HASHITEM_EMPTY(hi))
2994 {
2995 // For global variables we may try auto-loading the script. If it
2996 // worked find the variable again. Don't auto-load a script if it was
2997 // loaded already, otherwise it would be loaded every time when
2998 // checking if a function name is a Funcref variable.
2999 if (ht == &globvarht && !no_autoload)
3000 {
3001 // Note: script_autoload() may make "hi" invalid. It must either
3002 // be obtained again or not used.
3003 if (!script_autoload(varname, FALSE) || aborting())
3004 return NULL;
3005 hi = hash_find(ht, varname);
3006 }
3007 if (HASHITEM_EMPTY(hi))
3008 return NULL;
3009 }
3010 return HI2DI(hi);
3011}
3012
3013/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 * Get the script-local hashtab. NULL if not in a script context.
3015 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003016 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017get_script_local_ht(void)
3018{
3019 scid_T sid = current_sctx.sc_sid;
3020
Bram Moolenaare3d46852020-08-29 13:39:17 +02003021 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 return &SCRIPT_VARS(sid);
3023 return NULL;
3024}
3025
3026/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003027 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003028 * When "cmd" is TRUE it must look like a command, a function must be followed
3029 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003030 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003032 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003033lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003034 char_u *name,
3035 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003036 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003037 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038{
3039 hashtab_T *ht = get_script_local_ht();
3040 char_u buffer[30];
3041 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003042 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003044 int is_global = FALSE;
3045 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046
3047 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003048 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 if (len < sizeof(buffer) - 1)
3050 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003051 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 vim_strncpy(buffer, name, len);
3053 p = buffer;
3054 }
3055 else
3056 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003057 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003059 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 }
3061
3062 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003063 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064
3065 // if not script-local, then perhaps imported
Bram Moolenaardc4451d2022-01-09 21:36:37 +00003066 if (res == FAIL && find_imported(p, 0, FALSE, NULL) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003067 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 if (p != buffer)
3069 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003070
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003071 // Find a function, so that a following "->" works.
3072 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3073 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003074 if (res != OK)
3075 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003076 p = skipwhite(name + len);
3077
3078 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003079 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003080 // Do not check for an internal function, since it might also be a
3081 // valid command, such as ":split" versus "split()".
3082 // Skip "g:" before a function name.
3083 if (name[0] == 'g' && name[1] == ':')
3084 {
3085 is_global = TRUE;
3086 fname = name + 2;
3087 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003088 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003089 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003090 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003091 }
3092
Bram Moolenaar709664c2020-12-12 14:33:41 +01003093 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094}
3095
3096/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003097 * Find the hashtab used for a variable name.
3098 * Return NULL if the name is not valid.
3099 * Set "varname" to the start of name without ':'.
3100 */
3101 hashtab_T *
3102find_var_ht(char_u *name, char_u **varname)
3103{
3104 hashitem_T *hi;
3105 hashtab_T *ht;
3106
3107 if (name[0] == NUL)
3108 return NULL;
3109 if (name[1] != ':')
3110 {
3111 // The name must not start with a colon or #.
3112 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3113 return NULL;
3114 *varname = name;
3115
3116 // "version" is "v:version" in all scopes if scriptversion < 3.
3117 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003118 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003119 {
3120 hi = hash_find(&compat_hashtab, name);
3121 if (!HASHITEM_EMPTY(hi))
3122 return &compat_hashtab;
3123 }
3124
3125 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 if (ht != NULL)
3127 return ht; // local variable
3128
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003129 // In Vim9 script items at the script level are script-local, except
3130 // for autoload names.
3131 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 {
3133 ht = get_script_local_ht();
3134 if (ht != NULL)
3135 return ht;
3136 }
3137
3138 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003139 }
3140 *varname = name + 2;
3141 if (*name == 'g') // global variable
3142 return &globvarht;
3143 // There must be no ':' or '#' in the rest of the name, unless g: is used
3144 if (vim_strchr(name + 2, ':') != NULL
3145 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3146 return NULL;
3147 if (*name == 'b') // buffer variable
3148 return &curbuf->b_vars->dv_hashtab;
3149 if (*name == 'w') // window variable
3150 return &curwin->w_vars->dv_hashtab;
3151 if (*name == 't') // tab page variable
3152 return &curtab->tp_vars->dv_hashtab;
3153 if (*name == 'v') // v: variable
3154 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003155 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003156 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003158 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 if (*name == 'a') // a: function argument
3160 return get_funccal_args_ht();
3161 if (*name == 'l') // l: local function variable
3162 return get_funccal_local_ht();
3163 }
3164 if (*name == 's') // script variable
3165 {
3166 ht = get_script_local_ht();
3167 if (ht != NULL)
3168 return ht;
3169 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003170 return NULL;
3171}
3172
3173/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003174 * Get the string value of a (global/local) variable.
3175 * Note: see tv_get_string() for how long the pointer remains valid.
3176 * Returns NULL when it doesn't exist.
3177 */
3178 char_u *
3179get_var_value(char_u *name)
3180{
3181 dictitem_T *v;
3182
3183 v = find_var(name, NULL, FALSE);
3184 if (v == NULL)
3185 return NULL;
3186 return tv_get_string(&v->di_tv);
3187}
3188
3189/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003190 * Allocate a new hashtab for a sourced script. It will be used while
3191 * sourcing this script and when executing functions defined in the script.
3192 */
3193 void
3194new_script_vars(scid_T id)
3195{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003196 scriptvar_T *sv;
3197
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003198 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3199 if (sv == NULL)
3200 return;
3201 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003202 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003203}
3204
3205/*
3206 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3207 * point to it.
3208 */
3209 void
3210init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3211{
3212 hash_init(&dict->dv_hashtab);
3213 dict->dv_lock = 0;
3214 dict->dv_scope = scope;
3215 dict->dv_refcount = DO_NOT_FREE_CNT;
3216 dict->dv_copyID = 0;
3217 dict_var->di_tv.vval.v_dict = dict;
3218 dict_var->di_tv.v_type = VAR_DICT;
3219 dict_var->di_tv.v_lock = VAR_FIXED;
3220 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3221 dict_var->di_key[0] = NUL;
3222}
3223
3224/*
3225 * Unreference a dictionary initialized by init_var_dict().
3226 */
3227 void
3228unref_var_dict(dict_T *dict)
3229{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003230 // Now the dict needs to be freed if no one else is using it, go back to
3231 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003232 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3233 dict_unref(dict);
3234}
3235
3236/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003237 * Clean up a list of internal variables.
3238 * Frees all allocated variables and the value they contain.
3239 * Clears hashtab "ht", does not free it.
3240 */
3241 void
3242vars_clear(hashtab_T *ht)
3243{
3244 vars_clear_ext(ht, TRUE);
3245}
3246
3247/*
3248 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3249 */
3250 void
3251vars_clear_ext(hashtab_T *ht, int free_val)
3252{
3253 int todo;
3254 hashitem_T *hi;
3255 dictitem_T *v;
3256
3257 hash_lock(ht);
3258 todo = (int)ht->ht_used;
3259 for (hi = ht->ht_array; todo > 0; ++hi)
3260 {
3261 if (!HASHITEM_EMPTY(hi))
3262 {
3263 --todo;
3264
3265 // Free the variable. Don't remove it from the hashtab,
3266 // ht_array might change then. hash_clear() takes care of it
3267 // later.
3268 v = HI2DI(hi);
3269 if (free_val)
3270 clear_tv(&v->di_tv);
3271 if (v->di_flags & DI_FLAGS_ALLOC)
3272 vim_free(v);
3273 }
3274 }
3275 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003276 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003277}
3278
3279/*
3280 * Delete a variable from hashtab "ht" at item "hi".
3281 * Clear the variable value and free the dictitem.
3282 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003283 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003284delete_var(hashtab_T *ht, hashitem_T *hi)
3285{
3286 dictitem_T *di = HI2DI(hi);
3287
3288 hash_remove(ht, hi);
3289 clear_tv(&di->di_tv);
3290 vim_free(di);
3291}
3292
3293/*
3294 * List the value of one internal variable.
3295 */
3296 static void
3297list_one_var(dictitem_T *v, char *prefix, int *first)
3298{
3299 char_u *tofree;
3300 char_u *s;
3301 char_u numbuf[NUMBUFLEN];
3302
3303 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3304 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3305 s == NULL ? (char_u *)"" : s, first);
3306 vim_free(tofree);
3307}
3308
3309 static void
3310list_one_var_a(
3311 char *prefix,
3312 char_u *name,
3313 int type,
3314 char_u *string,
3315 int *first) // when TRUE clear rest of screen and set to FALSE
3316{
3317 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3318 msg_start();
3319 msg_puts(prefix);
3320 if (name != NULL) // "a:" vars don't have a name stored
3321 msg_puts((char *)name);
3322 msg_putchar(' ');
3323 msg_advance(22);
3324 if (type == VAR_NUMBER)
3325 msg_putchar('#');
3326 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3327 msg_putchar('*');
3328 else if (type == VAR_LIST)
3329 {
3330 msg_putchar('[');
3331 if (*string == '[')
3332 ++string;
3333 }
3334 else if (type == VAR_DICT)
3335 {
3336 msg_putchar('{');
3337 if (*string == '{')
3338 ++string;
3339 }
3340 else
3341 msg_putchar(' ');
3342
3343 msg_outtrans(string);
3344
3345 if (type == VAR_FUNC || type == VAR_PARTIAL)
3346 msg_puts("()");
3347 if (*first)
3348 {
3349 msg_clr_eos();
3350 *first = FALSE;
3351 }
3352}
3353
3354/*
3355 * Set variable "name" to value in "tv".
3356 * If the variable already exists, the value is updated.
3357 * Otherwise the variable is created.
3358 */
3359 void
3360set_var(
3361 char_u *name,
3362 typval_T *tv,
3363 int copy) // make copy of value in "tv"
3364{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003365 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003366}
3367
3368/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003369 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003370 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003371 * If the variable already exists and "is_const" is FALSE the value is updated.
3372 * Otherwise the variable is created.
3373 */
3374 void
3375set_var_const(
3376 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003377 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003378 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003379 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003380 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003381 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003382 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003383{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003384 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003385 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003386 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003387 dictitem_T *di;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003388 typval_T *dest_tv = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003389 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003390 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003391 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003393 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003394 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003395 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003396 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003397 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003398
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003399 if (sid != 0)
3400 {
3401 if (SCRIPT_ID_VALID(sid))
3402 ht = &SCRIPT_VARS(sid);
3403 varname = name;
3404 }
3405 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003406 {
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003407 scriptitem_T *si;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003408
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003409 if (in_vim9script() && is_export
3410 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3411 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
3412 ->sn_autoload_prefix != NULL)
3413 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003414 // In a vim9 autoload script an exported variable is put in the
3415 // global namespace with the autoload prefix.
3416 var_in_autoload = TRUE;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003417 varname = concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003418 if (varname == NULL)
3419 goto failed;
3420 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003421 ht = &globvarht;
3422 }
3423 else
3424 ht = find_var_ht(name, &varname);
3425 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003426 if (ht == NULL || *varname == NUL)
3427 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003428 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003429 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003430 }
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003431 is_script_local = ht == get_script_local_ht() || sid != 0 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003432
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003433 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003434 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003435 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003436 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003437 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003438 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003439 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003440 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003441 }
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003442 if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
3443 && vim_strchr((char_u *)"gwbt", name[0]) != NULL)
3444 // Do not make g:var, w:var, b:var or t:var final.
3445 flags &= ~ASSIGN_FINAL;
3446
Bram Moolenaare535db82021-03-31 21:07:24 +02003447 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003448 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3449 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003450 // For "[a, _] = list" the underscore is ignored.
3451 if ((flags & ASSIGN_UNPACK) == 0)
3452 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003453 goto failed;
3454 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003455
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003457
Bram Moolenaar24e93162021-07-18 20:40:33 +02003458 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003459 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +00003460 imported_T *import = find_imported(varname, 0, FALSE, NULL);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003461
Bram Moolenaar24e93162021-07-18 20:40:33 +02003462 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003463 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003464 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02003465 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003466 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003467 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003468 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003469 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003470 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3471 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003472 }
3473 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474
Bram Moolenaar24e93162021-07-18 20:40:33 +02003475 if (dest_tv == NULL)
3476 {
3477 // Search in parent scope which is possible to reference from lambda
3478 if (di == NULL)
3479 di = find_var_in_scoped_ht(name, TRUE);
3480
3481 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003482 && var_wrong_func_name(name, di == NULL))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003483 goto failed;
3484
3485 if (need_convert_to_bool(type, tv))
3486 {
Bram Moolenaarf6488542021-07-18 21:24:50 +02003487 // Destination is a bool and the value is not, but it can be
3488 // converted.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003489 CLEAR_FIELD(bool_tv);
3490 bool_tv.v_type = VAR_BOOL;
3491 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3492 tv = &bool_tv;
3493 }
3494
3495 if (di != NULL)
3496 {
3497 // Item already exists. Allowed to replace when reloading.
3498 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
3499 {
3500 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaarf6488542021-07-18 21:24:50 +02003501 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003502 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003503 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar24e93162021-07-18 20:40:33 +02003504 goto failed;
3505 }
3506
3507 if (is_script_local && vim9script
Bram Moolenaarf6488542021-07-18 21:24:50 +02003508 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003509 {
3510 semsg(_(e_redefining_script_item_str), name);
3511 goto failed;
3512 }
3513
Bram Moolenaara06758d2021-10-15 00:18:37 +01003514 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003515 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003516 where_T where = WHERE_INIT;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003517 svar_T *sv = find_typval_in_script(&di->di_tv, sid);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003518
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003519 if (sv != NULL)
3520 {
3521 // check the type and adjust to bool if needed
3522 where.wt_index = var_idx;
3523 where.wt_variable = TRUE;
3524 if (check_script_var_type(sv, tv, name, where) == FAIL)
3525 goto failed;
3526 if (type == NULL)
3527 type = sv->sv_type;
3528 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003529 }
3530
Bram Moolenaara06758d2021-10-15 00:18:37 +01003531 if ((flags & ASSIGN_FOR_LOOP) == 0
3532 && var_check_permission(di, name) == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003533 goto failed;
3534 }
3535 else
3536 {
3537 // can only redefine once
3538 di->di_flags &= ~DI_FLAGS_RELOAD;
3539
Bram Moolenaarf6488542021-07-18 21:24:50 +02003540 // A Vim9 script-local variable is also present in sn_all_vars
3541 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003542 if (var_in_vim9script || var_in_autoload)
3543 update_vim9_script_var(FALSE, di,
3544 var_in_autoload ? name : di->di_key, flags,
3545 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003546 }
3547
3548 // existing variable, need to clear the value
3549
3550 // Handle setting internal di: variables separately where needed to
3551 // prevent changing the type.
3552 if (ht == &vimvarht)
3553 {
3554 if (di->di_tv.v_type == VAR_STRING)
3555 {
3556 VIM_CLEAR(di->di_tv.vval.v_string);
3557 if (copy || tv->v_type != VAR_STRING)
3558 {
3559 char_u *val = tv_get_string(tv);
3560
Bram Moolenaarf6488542021-07-18 21:24:50 +02003561 // Careful: when assigning to v:errmsg and
3562 // tv_get_string() causes an error message the variable
3563 // will already be set.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003564 if (di->di_tv.vval.v_string == NULL)
3565 di->di_tv.vval.v_string = vim_strsave(val);
3566 }
3567 else
3568 {
3569 // Take over the string to avoid an extra alloc/free.
3570 di->di_tv.vval.v_string = tv->vval.v_string;
3571 tv->vval.v_string = NULL;
3572 }
3573 goto failed;
3574 }
3575 else if (di->di_tv.v_type == VAR_NUMBER)
3576 {
3577 di->di_tv.vval.v_number = tv_get_number(tv);
3578 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003579 set_search_direction(di->di_tv.vval.v_number
3580 ? '/' : '?');
Bram Moolenaar24e93162021-07-18 20:40:33 +02003581#ifdef FEAT_SEARCH_EXTRA
3582 else if (STRCMP(varname, "hlsearch") == 0)
3583 {
3584 no_hlsearch = !di->di_tv.vval.v_number;
3585 redraw_all_later(SOME_VALID);
3586 }
3587#endif
3588 goto failed;
3589 }
3590 else if (di->di_tv.v_type != tv->v_type)
3591 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003592 semsg(_(e_setting_str_to_value_with_wrong_type), name);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003593 goto failed;
3594 }
3595 }
3596
3597 clear_tv(&di->di_tv);
3598 }
3599 else
3600 {
3601 // Item not found, check if a function already exists.
3602 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaarf6488542021-07-18 21:24:50 +02003603 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003604 {
3605 semsg(_(e_redefining_script_item_str), name);
3606 goto failed;
3607 }
3608
Bram Moolenaar24e93162021-07-18 20:40:33 +02003609 // add a new variable
3610 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3611 {
3612 semsg(_(e_unknown_variable_str), name);
3613 goto failed;
3614 }
3615
3616 // Can't add "v:" or "a:" variable.
3617 if (ht == &vimvarht || ht == get_funccal_args_ht())
3618 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003619 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003620 goto failed;
3621 }
3622
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003623 // Make sure the variable name is valid. In Vim9 script an
3624 // autoload variable must be prefixed with "g:" unless in an
3625 // autoload script.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003626 if (!valid_varname(varname, -1, !vim9script
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003627 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003628 goto failed;
3629
3630 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3631 if (di == NULL)
3632 goto failed;
3633 STRCPY(di->di_key, varname);
3634 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
3635 {
3636 vim_free(di);
3637 goto failed;
3638 }
3639 di->di_flags = DI_FLAGS_ALLOC;
3640 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3641 di->di_flags |= DI_FLAGS_LOCK;
3642
3643 // A Vim9 script-local variable is also added to sn_all_vars and
3644 // sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003645 if (var_in_vim9script || var_in_autoload)
3646 update_vim9_script_var(TRUE, di,
3647 var_in_autoload ? name : di->di_key, flags,
3648 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003649 }
3650
Bram Moolenaar24e93162021-07-18 20:40:33 +02003651 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003652 }
3653
3654 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003655 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003656 else
3657 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003658 *dest_tv = *tv;
3659 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003660 init_tv(tv);
3661 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003662 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003663
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003664 if (vim9script && type != NULL)
3665 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003666 if (type->tt_type == VAR_DICT && dest_tv->vval.v_dict != NULL)
Bram Moolenaar464393a2021-09-11 23:07:44 +02003667 {
3668 if (dest_tv->vval.v_dict->dv_type != type)
3669 {
3670 free_type(dest_tv->vval.v_dict->dv_type);
3671 dest_tv->vval.v_dict->dv_type = alloc_type(type);
3672 }
3673 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003674 else if (type->tt_type == VAR_LIST && dest_tv->vval.v_list != NULL)
Bram Moolenaar464393a2021-09-11 23:07:44 +02003675 {
3676 if (dest_tv->vval.v_list->lv_type != type)
3677 {
3678 free_type(dest_tv->vval.v_list->lv_type);
3679 dest_tv->vval.v_list->lv_type = alloc_type(type);
3680 }
3681 }
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003682 }
3683
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003684 // ":const var = value" locks the value
3685 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003686 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003687 // Like :lockvar! name: lock the value and what it contains, but only
3688 // if the reference count is up to one. That locks only literal
3689 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003690 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003691
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003692failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003693 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003694 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003695 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003696}
3697
3698/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003699 * Check in this order for backwards compatibility:
3700 * - Whether the variable is read-only
3701 * - Whether the variable value is locked
3702 * - Whether the variable is locked
3703 */
3704 int
3705var_check_permission(dictitem_T *di, char_u *name)
3706{
3707 if (var_check_ro(di->di_flags, name, FALSE)
3708 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3709 || var_check_lock(di->di_flags, name, FALSE))
3710 return FAIL;
3711 return OK;
3712}
3713
3714/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003715 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3716 * Also give an error message.
3717 */
3718 int
3719var_check_ro(int flags, char_u *name, int use_gettext)
3720{
3721 if (flags & DI_FLAGS_RO)
3722 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003723 if (name == NULL)
3724 emsg(_(e_cannot_change_readonly_variable));
3725 else
3726 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02003727 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003728 return TRUE;
3729 }
3730 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3731 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003732 if (name == NULL)
3733 emsg(_(e_cannot_set_variable_in_sandbox));
3734 else
3735 semsg(_(e_cannot_set_variable_in_sandbox_str),
3736 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003737 return TRUE;
3738 }
3739 return FALSE;
3740}
3741
3742/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003743 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3744 * Also give an error message.
3745 */
3746 int
3747var_check_lock(int flags, char_u *name, int use_gettext)
3748{
3749 if (flags & DI_FLAGS_LOCK)
3750 {
3751 semsg(_(e_variable_is_locked_str),
3752 use_gettext ? (char_u *)_(name) : name);
3753 return TRUE;
3754 }
3755 return FALSE;
3756}
3757
3758/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003759 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3760 * Also give an error message.
3761 */
3762 int
3763var_check_fixed(int flags, char_u *name, int use_gettext)
3764{
3765 if (flags & DI_FLAGS_FIX)
3766 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003767 if (name == NULL)
3768 emsg(_(e_cannot_delete_variable));
3769 else
3770 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003771 use_gettext ? (char_u *)_(name) : name);
3772 return TRUE;
3773 }
3774 return FALSE;
3775}
3776
3777/*
3778 * Check if a funcref is assigned to a valid variable name.
3779 * Return TRUE and give an error if not.
3780 */
3781 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003782var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003783 char_u *name, // points to start of variable name
3784 int new_var) // TRUE when creating the variable
3785{
Bram Moolenaar32154662021-03-28 21:14:06 +02003786 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
3787 // the name can be used without the s: prefix.
3788 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
3789 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003790 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3791 ? name[2] : name[0]))
3792 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003793 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003794 return TRUE;
3795 }
3796 // Don't allow hiding a function. When "v" is not NULL we might be
3797 // assigning another function to the same var, the type is checked
3798 // below.
3799 if (new_var && function_exists(name, FALSE))
3800 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003801 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003802 name);
3803 return TRUE;
3804 }
3805 return FALSE;
3806}
3807
3808/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003809 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3810 * value. Also give an error message, using "name" or _("name") when
3811 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003812 */
3813 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003814value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003815{
3816 if (lock & VAR_LOCKED)
3817 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003818 if (name == NULL)
3819 emsg(_(e_value_is_locked));
3820 else
3821 semsg(_(e_value_is_locked_str),
3822 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003823 return TRUE;
3824 }
3825 if (lock & VAR_FIXED)
3826 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003827 if (name == NULL)
3828 emsg(_(e_cannot_change_value));
3829 else
3830 semsg(_(e_cannot_change_value_of_str),
3831 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003832 return TRUE;
3833 }
3834 return FALSE;
3835}
3836
3837/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003838 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003839 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003840 * Return FALSE and give an error if not.
3841 */
3842 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003843valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003844{
3845 char_u *p;
3846
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003847 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003848 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003849 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003850 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003851 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003852 return FALSE;
3853 }
3854 return TRUE;
3855}
3856
3857/*
3858 * getwinvar() and gettabwinvar()
3859 */
3860 static void
3861getwinvar(
3862 typval_T *argvars,
3863 typval_T *rettv,
3864 int off) // 1 for gettabwinvar()
3865{
3866 win_T *win;
3867 char_u *varname;
3868 dictitem_T *v;
3869 tabpage_T *tp = NULL;
3870 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00003871 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003872 int need_switch_win;
3873
3874 if (off == 1)
3875 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3876 else
3877 tp = curtab;
3878 win = find_win_by_nr(&argvars[off], tp);
3879 varname = tv_get_string_chk(&argvars[off + 1]);
3880 ++emsg_off;
3881
3882 rettv->v_type = VAR_STRING;
3883 rettv->vval.v_string = NULL;
3884
3885 if (win != NULL && varname != NULL)
3886 {
3887 // Set curwin to be our win, temporarily. Also set the tabpage,
3888 // otherwise the window is not valid. Only do this when needed,
3889 // autocommands get blocked.
3890 need_switch_win = !(tp == curtab && win == curwin);
3891 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00003892 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003893 {
3894 if (*varname == '&')
3895 {
3896 if (varname[1] == NUL)
3897 {
3898 // get all window-local options in a dict
3899 dict_T *opts = get_winbuf_options(FALSE);
3900
3901 if (opts != NULL)
3902 {
3903 rettv_dict_set(rettv, opts);
3904 done = TRUE;
3905 }
3906 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003907 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003908 // window-local-option
3909 done = TRUE;
3910 }
3911 else
3912 {
3913 // Look up the variable.
3914 // Let getwinvar({nr}, "") return the "w:" dictionary.
3915 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3916 varname, FALSE);
3917 if (v != NULL)
3918 {
3919 copy_tv(&v->di_tv, rettv);
3920 done = TRUE;
3921 }
3922 }
3923 }
3924
3925 if (need_switch_win)
3926 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00003927 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003928 }
3929
3930 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3931 // use the default return value
3932 copy_tv(&argvars[off + 2], rettv);
3933
3934 --emsg_off;
3935}
3936
3937/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003938 * Set option "varname" to the value of "varp" for the current buffer/window.
3939 */
3940 static void
3941set_option_from_tv(char_u *varname, typval_T *varp)
3942{
3943 long numval = 0;
3944 char_u *strval;
3945 char_u nbuf[NUMBUFLEN];
3946 int error = FALSE;
3947
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003948 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003949 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003950 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003951 strval = (char_u *)"0"; // avoid using "false"
3952 }
3953 else
3954 {
3955 if (!in_vim9script() || varp->v_type != VAR_STRING)
3956 numval = (long)tv_get_number_chk(varp, &error);
3957 strval = tv_get_string_buf_chk(varp, nbuf);
3958 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02003959 if (!error && strval != NULL)
3960 set_option_value(varname, numval, strval, OPT_LOCAL);
3961}
3962
3963/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003964 * "setwinvar()" and "settabwinvar()" functions
3965 */
3966 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003967setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003968{
3969 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00003970 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003971 int need_switch_win;
3972 char_u *varname, *winvarname;
3973 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003974 tabpage_T *tp = NULL;
3975
3976 if (check_secure())
3977 return;
3978
3979 if (off == 1)
3980 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3981 else
3982 tp = curtab;
3983 win = find_win_by_nr(&argvars[off], tp);
3984 varname = tv_get_string_chk(&argvars[off + 1]);
3985 varp = &argvars[off + 2];
3986
3987 if (win != NULL && varname != NULL && varp != NULL)
3988 {
3989 need_switch_win = !(tp == curtab && win == curwin);
3990 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00003991 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003992 {
3993 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003994 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003995 else
3996 {
3997 winvarname = alloc(STRLEN(varname) + 3);
3998 if (winvarname != NULL)
3999 {
4000 STRCPY(winvarname, "w:");
4001 STRCPY(winvarname + 2, varname);
4002 set_var(winvarname, varp, TRUE);
4003 vim_free(winvarname);
4004 }
4005 }
4006 }
4007 if (need_switch_win)
Bram Moolenaar18f47402022-01-06 13:24:51 +00004008 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004009 }
4010}
4011
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004012/*
4013 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4014 * v:option_type, and v:option_command.
4015 */
4016 void
4017reset_v_option_vars(void)
4018{
4019 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4020 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4021 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4022 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4023 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4024 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4025}
4026
4027/*
4028 * Add an assert error to v:errors.
4029 */
4030 void
4031assert_error(garray_T *gap)
4032{
4033 struct vimvar *vp = &vimvars[VV_ERRORS];
4034
Bram Moolenaard787e402021-12-24 21:36:12 +00004035 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004036 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004037 set_vim_var_list(VV_ERRORS, list_alloc());
4038 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4039}
4040
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004041 int
4042var_exists(char_u *var)
4043{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004044 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004045 char_u *name;
4046 char_u *tofree;
4047 typval_T tv;
4048 int len = 0;
4049 int n = FALSE;
4050
4051 // get_name_len() takes care of expanding curly braces
4052 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004053 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004054 if (len > 0)
4055 {
4056 if (tofree != NULL)
4057 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004058 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004059 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004060 if (n)
4061 {
4062 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004063 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004064 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4065 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004066 if (n)
4067 clear_tv(&tv);
4068 }
4069 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004070 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004071 n = FALSE;
4072
4073 vim_free(tofree);
4074 return n;
4075}
4076
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004077static lval_T *redir_lval = NULL;
4078#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4079static garray_T redir_ga; // only valid when redir_lval is not NULL
4080static char_u *redir_endp = NULL;
4081static char_u *redir_varname = NULL;
4082
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004083 int
4084alloc_redir_lval(void)
4085{
4086 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4087 if (redir_lval == NULL)
4088 return FAIL;
4089 return OK;
4090}
4091
4092 void
4093clear_redir_lval(void)
4094{
4095 VIM_CLEAR(redir_lval);
4096}
4097
4098 void
4099init_redir_ga(void)
4100{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004101 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004102}
4103
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004104/*
4105 * Start recording command output to a variable
4106 * When "append" is TRUE append to an existing variable.
4107 * Returns OK if successfully completed the setup. FAIL otherwise.
4108 */
4109 int
4110var_redir_start(char_u *name, int append)
4111{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004112 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004113 typval_T tv;
4114
4115 // Catch a bad name early.
4116 if (!eval_isnamec1(*name))
4117 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004118 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004119 return FAIL;
4120 }
4121
4122 // Make a copy of the name, it is used in redir_lval until redir ends.
4123 redir_varname = vim_strsave(name);
4124 if (redir_varname == NULL)
4125 return FAIL;
4126
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004127 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004128 {
4129 var_redir_stop();
4130 return FAIL;
4131 }
4132
4133 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004134 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004135
4136 // Parse the variable name (can be a dict or list entry).
4137 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4138 FNE_CHECK_START);
4139 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4140 {
4141 clear_lval(redir_lval);
4142 if (redir_endp != NULL && *redir_endp != NUL)
4143 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004144 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004145 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004146 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004147 redir_endp = NULL; // don't store a value, only cleanup
4148 var_redir_stop();
4149 return FAIL;
4150 }
4151
4152 // check if we can write to the variable: set it to or append an empty
4153 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004154 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004155 tv.v_type = VAR_STRING;
4156 tv.vval.v_string = (char_u *)"";
4157 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004158 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004159 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004160 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004161 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004162 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004163 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004164 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004165 {
4166 redir_endp = NULL; // don't store a value, only cleanup
4167 var_redir_stop();
4168 return FAIL;
4169 }
4170
4171 return OK;
4172}
4173
4174/*
4175 * Append "value[value_len]" to the variable set by var_redir_start().
4176 * The actual appending is postponed until redirection ends, because the value
4177 * appended may in fact be the string we write to, changing it may cause freed
4178 * memory to be used:
4179 * :redir => foo
4180 * :let foo
4181 * :redir END
4182 */
4183 void
4184var_redir_str(char_u *value, int value_len)
4185{
4186 int len;
4187
4188 if (redir_lval == NULL)
4189 return;
4190
4191 if (value_len == -1)
4192 len = (int)STRLEN(value); // Append the entire string
4193 else
4194 len = value_len; // Append only "value_len" characters
4195
4196 if (ga_grow(&redir_ga, len) == OK)
4197 {
4198 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4199 redir_ga.ga_len += len;
4200 }
4201 else
4202 var_redir_stop();
4203}
4204
4205/*
4206 * Stop redirecting command output to a variable.
4207 * Frees the allocated memory.
4208 */
4209 void
4210var_redir_stop(void)
4211{
4212 typval_T tv;
4213
4214 if (EVALCMD_BUSY)
4215 {
4216 redir_lval = NULL;
4217 return;
4218 }
4219
4220 if (redir_lval != NULL)
4221 {
4222 // If there was no error: assign the text to the variable.
4223 if (redir_endp != NULL)
4224 {
4225 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4226 tv.v_type = VAR_STRING;
4227 tv.vval.v_string = redir_ga.ga_data;
4228 // Call get_lval() again, if it's inside a Dict or List it may
4229 // have changed.
4230 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4231 FALSE, FALSE, 0, FNE_CHECK_START);
4232 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004234 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004235 clear_lval(redir_lval);
4236 }
4237
4238 // free the collected output
4239 VIM_CLEAR(redir_ga.ga_data);
4240
4241 VIM_CLEAR(redir_lval);
4242 }
4243 VIM_CLEAR(redir_varname);
4244}
4245
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004246/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004247 * Get the collected redirected text and clear redir_ga.
4248 */
4249 char_u *
4250get_clear_redir_ga(void)
4251{
4252 char_u *res;
4253
4254 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4255 res = redir_ga.ga_data;
4256 redir_ga.ga_data = NULL;
4257 return res;
4258}
4259
4260/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004261 * "gettabvar()" function
4262 */
4263 void
4264f_gettabvar(typval_T *argvars, typval_T *rettv)
4265{
Bram Moolenaar18f47402022-01-06 13:24:51 +00004266 switchwin_T switchwin;
4267 tabpage_T *tp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004268 dictitem_T *v;
4269 char_u *varname;
4270 int done = FALSE;
4271
4272 rettv->v_type = VAR_STRING;
4273 rettv->vval.v_string = NULL;
4274
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004275 if (in_vim9script()
4276 && (check_for_number_arg(argvars, 0) == FAIL
4277 || check_for_string_arg(argvars, 1) == FAIL))
4278 return;
4279
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004280 varname = tv_get_string_chk(&argvars[1]);
4281 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4282 if (tp != NULL && varname != NULL)
4283 {
4284 // Set tp to be our tabpage, temporarily. Also set the window to the
4285 // first window in the tabpage, otherwise the window is not valid.
Bram Moolenaar18f47402022-01-06 13:24:51 +00004286 if (switch_win(&switchwin,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004287 tp == curtab || tp->tp_firstwin == NULL ? firstwin
4288 : tp->tp_firstwin, tp, TRUE) == OK)
4289 {
4290 // look up the variable
4291 // Let gettabvar({nr}, "") return the "t:" dictionary.
4292 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
4293 if (v != NULL)
4294 {
4295 copy_tv(&v->di_tv, rettv);
4296 done = TRUE;
4297 }
4298 }
4299
4300 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004301 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004302 }
4303
4304 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4305 copy_tv(&argvars[2], rettv);
4306}
4307
4308/*
4309 * "gettabwinvar()" function
4310 */
4311 void
4312f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4313{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004314 if (in_vim9script()
4315 && (check_for_number_arg(argvars, 0) == FAIL
4316 || check_for_number_arg(argvars, 1) == FAIL
4317 || check_for_string_arg(argvars, 2) == FAIL))
4318 return;
4319
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004320 getwinvar(argvars, rettv, 1);
4321}
4322
4323/*
4324 * "getwinvar()" function
4325 */
4326 void
4327f_getwinvar(typval_T *argvars, typval_T *rettv)
4328{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004329 if (in_vim9script()
4330 && (check_for_number_arg(argvars, 0) == FAIL
4331 || check_for_string_arg(argvars, 1) == FAIL))
4332 return;
4333
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004334 getwinvar(argvars, rettv, 0);
4335}
4336
4337/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004338 * "getbufvar()" function
4339 */
4340 void
4341f_getbufvar(typval_T *argvars, typval_T *rettv)
4342{
4343 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004344 char_u *varname;
4345 dictitem_T *v;
4346 int done = FALSE;
4347
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004348 if (in_vim9script()
4349 && (check_for_buffer_arg(argvars, 0) == FAIL
4350 || check_for_string_arg(argvars, 1) == FAIL))
4351 return;
4352
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004353 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004354 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004355
4356 rettv->v_type = VAR_STRING;
4357 rettv->vval.v_string = NULL;
4358
4359 if (buf != NULL && varname != NULL)
4360 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004361 if (*varname == '&')
4362 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004363 buf_T *save_curbuf = curbuf;
4364
4365 // set curbuf to be our buf, temporarily
4366 curbuf = buf;
4367
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004368 if (varname[1] == NUL)
4369 {
4370 // get all buffer-local options in a dict
4371 dict_T *opts = get_winbuf_options(TRUE);
4372
4373 if (opts != NULL)
4374 {
4375 rettv_dict_set(rettv, opts);
4376 done = TRUE;
4377 }
4378 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004379 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004380 // buffer-local-option
4381 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02004382
4383 // restore previous notion of curbuf
4384 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004385 }
4386 else
4387 {
4388 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02004389 if (*varname == NUL)
4390 // Let getbufvar({nr}, "") return the "b:" dictionary.
4391 v = &buf->b_bufvar;
4392 else
4393 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
4394 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004395 if (v != NULL)
4396 {
4397 copy_tv(&v->di_tv, rettv);
4398 done = TRUE;
4399 }
4400 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004401 }
4402
4403 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4404 // use the default value
4405 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004406}
4407
4408/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004409 * "settabvar()" function
4410 */
4411 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004412f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004413{
4414 tabpage_T *save_curtab;
4415 tabpage_T *tp;
4416 char_u *varname, *tabvarname;
4417 typval_T *varp;
4418
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004419 if (check_secure())
4420 return;
4421
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004422 if (in_vim9script()
4423 && (check_for_number_arg(argvars, 0) == FAIL
4424 || check_for_string_arg(argvars, 1) == FAIL))
4425 return;
4426
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004427 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4428 varname = tv_get_string_chk(&argvars[1]);
4429 varp = &argvars[2];
4430
4431 if (varname != NULL && varp != NULL && tp != NULL)
4432 {
4433 save_curtab = curtab;
4434 goto_tabpage_tp(tp, FALSE, FALSE);
4435
4436 tabvarname = alloc(STRLEN(varname) + 3);
4437 if (tabvarname != NULL)
4438 {
4439 STRCPY(tabvarname, "t:");
4440 STRCPY(tabvarname + 2, varname);
4441 set_var(tabvarname, varp, TRUE);
4442 vim_free(tabvarname);
4443 }
4444
4445 // Restore current tabpage
4446 if (valid_tabpage(save_curtab))
4447 goto_tabpage_tp(save_curtab, FALSE, FALSE);
4448 }
4449}
4450
4451/*
4452 * "settabwinvar()" function
4453 */
4454 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004455f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004456{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004457 if (in_vim9script()
4458 && (check_for_number_arg(argvars, 0) == FAIL
4459 || check_for_number_arg(argvars, 1) == FAIL
4460 || check_for_string_arg(argvars, 2) == FAIL))
4461 return;
4462
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004463 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004464}
4465
4466/*
4467 * "setwinvar()" function
4468 */
4469 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004470f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004471{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004472 if (in_vim9script()
4473 && (check_for_number_arg(argvars, 0) == FAIL
4474 || check_for_string_arg(argvars, 1) == FAIL))
4475 return;
4476
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004477 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004478}
4479
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004480/*
4481 * "setbufvar()" function
4482 */
4483 void
4484f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4485{
4486 buf_T *buf;
4487 char_u *varname, *bufvarname;
4488 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004489
4490 if (check_secure())
4491 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004492
4493 if (in_vim9script()
4494 && (check_for_buffer_arg(argvars, 0) == FAIL
4495 || check_for_string_arg(argvars, 1) == FAIL))
4496 return;
4497
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004498 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004499 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004500 varp = &argvars[2];
4501
4502 if (buf != NULL && varname != NULL && varp != NULL)
4503 {
4504 if (*varname == '&')
4505 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004506 aco_save_T aco;
4507
4508 // set curbuf to be our buf, temporarily
4509 aucmd_prepbuf(&aco, buf);
4510
Bram Moolenaar191929b2020-08-19 21:20:49 +02004511 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004512
4513 // reset notion of buffer
4514 aucmd_restbuf(&aco);
4515 }
4516 else
4517 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004518 bufvarname = alloc(STRLEN(varname) + 3);
4519 if (bufvarname != NULL)
4520 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004521 buf_T *save_curbuf = curbuf;
4522
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004523 curbuf = buf;
4524 STRCPY(bufvarname, "b:");
4525 STRCPY(bufvarname + 2, varname);
4526 set_var(bufvarname, varp, TRUE);
4527 vim_free(bufvarname);
4528 curbuf = save_curbuf;
4529 }
4530 }
4531 }
4532}
4533
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004534/*
4535 * Get a callback from "arg". It can be a Funcref or a function name.
4536 * When "arg" is zero return an empty string.
4537 * "cb_name" is not allocated.
4538 * "cb_name" is set to NULL for an invalid argument.
4539 */
4540 callback_T
4541get_callback(typval_T *arg)
4542{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004543 callback_T res;
4544 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004545
4546 res.cb_free_name = FALSE;
4547 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4548 {
4549 res.cb_partial = arg->vval.v_partial;
4550 ++res.cb_partial->pt_refcount;
4551 res.cb_name = partial_name(res.cb_partial);
4552 }
4553 else
4554 {
4555 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004556 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4557 && isdigit(*arg->vval.v_string))
4558 r = FAIL;
4559 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004560 {
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004561 if (arg->v_type == VAR_STRING)
4562 {
4563 char_u *name;
4564
4565 name = get_scriptlocal_funcname(arg->vval.v_string);
4566 if (name != NULL)
4567 {
4568 vim_free(arg->vval.v_string);
4569 arg->vval.v_string = name;
4570 }
4571 }
4572
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004573 res.cb_name = arg->vval.v_string;
4574 func_ref(res.cb_name);
4575 }
4576 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004577 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004578 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004579 r = FAIL;
4580
4581 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004582 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004583 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004584 res.cb_name = NULL;
4585 }
4586 }
4587 return res;
4588}
4589
4590/*
4591 * Copy a callback into a typval_T.
4592 */
4593 void
4594put_callback(callback_T *cb, typval_T *tv)
4595{
4596 if (cb->cb_partial != NULL)
4597 {
4598 tv->v_type = VAR_PARTIAL;
4599 tv->vval.v_partial = cb->cb_partial;
4600 ++tv->vval.v_partial->pt_refcount;
4601 }
4602 else
4603 {
4604 tv->v_type = VAR_FUNC;
4605 tv->vval.v_string = vim_strsave(cb->cb_name);
4606 func_ref(cb->cb_name);
4607 }
4608}
4609
4610/*
4611 * Make a copy of "src" into "dest", allocating the function name if needed,
4612 * without incrementing the refcount.
4613 */
4614 void
4615set_callback(callback_T *dest, callback_T *src)
4616{
4617 if (src->cb_partial == NULL)
4618 {
4619 // just a function name, make a copy
4620 dest->cb_name = vim_strsave(src->cb_name);
4621 dest->cb_free_name = TRUE;
4622 }
4623 else
4624 {
4625 // cb_name is a pointer into cb_partial
4626 dest->cb_name = src->cb_name;
4627 dest->cb_free_name = FALSE;
4628 }
4629 dest->cb_partial = src->cb_partial;
4630}
4631
4632/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004633 * Copy callback from "src" to "dest", incrementing the refcounts.
4634 */
4635 void
4636copy_callback(callback_T *dest, callback_T *src)
4637{
4638 dest->cb_partial = src->cb_partial;
4639 if (dest->cb_partial != NULL)
4640 {
4641 dest->cb_name = src->cb_name;
4642 dest->cb_free_name = FALSE;
4643 ++dest->cb_partial->pt_refcount;
4644 }
4645 else
4646 {
4647 dest->cb_name = vim_strsave(src->cb_name);
4648 dest->cb_free_name = TRUE;
4649 func_ref(src->cb_name);
4650 }
4651}
4652
4653/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004654 * When a callback refers to an autoload import, change the function name to
4655 * the "path#name" form. Uses the current script context.
4656 * Only works when the name is allocated.
4657 */
4658 void
4659expand_autload_callback(callback_T *cb)
4660{
4661 char_u *p;
4662 imported_T *import;
4663
4664 if (!in_vim9script() || cb->cb_name == NULL || !cb->cb_free_name)
4665 return;
4666 p = vim_strchr(cb->cb_name, '.');
4667 if (p == NULL)
4668 return;
4669 import = find_imported(cb->cb_name, p - cb->cb_name, FALSE, NULL);
4670 if (import != NULL && SCRIPT_ID_VALID(import->imp_sid))
4671 {
4672 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
4673
4674 if (si->sn_autoload_prefix != NULL)
4675 {
4676 char_u *name = concat_str(si->sn_autoload_prefix, p + 1);
4677
4678 if (name != NULL)
4679 {
4680 vim_free(cb->cb_name);
4681 cb->cb_name = name;
4682 }
4683 }
4684 }
4685}
4686
4687/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004688 * Unref/free "callback" returned by get_callback() or set_callback().
4689 */
4690 void
4691free_callback(callback_T *callback)
4692{
4693 if (callback->cb_partial != NULL)
4694 {
4695 partial_unref(callback->cb_partial);
4696 callback->cb_partial = NULL;
4697 }
4698 else if (callback->cb_name != NULL)
4699 func_unref(callback->cb_name);
4700 if (callback->cb_free_name)
4701 {
4702 vim_free(callback->cb_name);
4703 callback->cb_free_name = FALSE;
4704 }
4705 callback->cb_name = NULL;
4706}
4707
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004708#endif // FEAT_EVAL