blob: ca7e7e8cf5f94b9b6967367154096b5669057aeb [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{
418 int err = FALSE;
419
420 set_vim_var_string(VV_FNAME_IN, origfile, -1);
421 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
422 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
423 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
424 set_vim_var_string(VV_FNAME_IN, NULL, -1);
425 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
426 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
427}
428
429 void
430eval_patch(
431 char_u *origfile,
432 char_u *difffile,
433 char_u *outfile)
434{
435 int err;
436
437 set_vim_var_string(VV_FNAME_IN, origfile, -1);
438 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
439 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
440 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
441 set_vim_var_string(VV_FNAME_IN, NULL, -1);
442 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
443 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
444}
445# endif
446
447#if defined(FEAT_SPELL) || defined(PROTO)
448/*
449 * Evaluate an expression to a list with suggestions.
450 * For the "expr:" part of 'spellsuggest'.
451 * Returns NULL when there is an error.
452 */
453 list_T *
454eval_spell_expr(char_u *badword, char_u *expr)
455{
456 typval_T save_val;
457 typval_T rettv;
458 list_T *list = NULL;
459 char_u *p = skipwhite(expr);
460
461 // Set "v:val" to the bad word.
462 prepare_vimvar(VV_VAL, &save_val);
463 set_vim_var_string(VV_VAL, badword, -1);
464 if (p_verbose == 0)
465 ++emsg_off;
466
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200467 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200468 {
469 if (rettv.v_type != VAR_LIST)
470 clear_tv(&rettv);
471 else
472 list = rettv.vval.v_list;
473 }
474
475 if (p_verbose == 0)
476 --emsg_off;
477 clear_tv(get_vim_var_tv(VV_VAL));
478 restore_vimvar(VV_VAL, &save_val);
479
480 return list;
481}
482
483/*
484 * "list" is supposed to contain two items: a word and a number. Return the
485 * word in "pp" and the number as the return value.
486 * Return -1 if anything isn't right.
487 * Used to get the good word and score from the eval_spell_expr() result.
488 */
489 int
490get_spellword(list_T *list, char_u **pp)
491{
492 listitem_T *li;
493
494 li = list->lv_first;
495 if (li == NULL)
496 return -1;
497 *pp = tv_get_string(&li->li_tv);
498
499 li = li->li_next;
500 if (li == NULL)
501 return -1;
502 return (int)tv_get_number(&li->li_tv);
503}
504#endif
505
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200506/*
507 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200508 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200509 * When not used yet add the variable to the v: hashtable.
510 */
511 void
512prepare_vimvar(int idx, typval_T *save_tv)
513{
514 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200515 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaard787e402021-12-24 21:36:12 +0000516 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200517 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
518}
519
520/*
521 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200522 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200523 * When no longer defined, remove the variable from the v: hashtable.
524 */
525 void
526restore_vimvar(int idx, typval_T *save_tv)
527{
528 hashitem_T *hi;
529
530 vimvars[idx].vv_tv = *save_tv;
Bram Moolenaard787e402021-12-24 21:36:12 +0000531 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200532 {
533 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
534 if (HASHITEM_EMPTY(hi))
535 internal_error("restore_vimvar()");
536 else
537 hash_remove(&vimvarht, hi);
538 }
539}
540
541/*
542 * List Vim variables.
543 */
544 static void
545list_vim_vars(int *first)
546{
547 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
548}
549
550/*
551 * List script-local variables, if there is a script.
552 */
553 static void
554list_script_vars(int *first)
555{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200556 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200557 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
558 "s:", FALSE, first);
559}
560
561/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200562 * Get a list of lines from a HERE document. The here document is a list of
563 * lines surrounded by a marker.
564 * cmd << {marker}
565 * {line1}
566 * {line2}
567 * ....
568 * {marker}
569 *
570 * The {marker} is a string. If the optional 'trim' word is supplied before the
571 * marker, then the leading indentation before the lines (matching the
572 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200573 *
574 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
575 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
576 * missing, then '.' is accepted as a marker.
577 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200578 * Returns a List with {lines} or NULL.
579 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200581heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200582{
583 char_u *theline;
584 char_u *marker;
585 list_T *l;
586 char_u *p;
587 int marker_indent_len = 0;
588 int text_indent_len = 0;
589 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200590 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200591 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200592
593 if (eap->getline == NULL)
594 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000595 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200596 return NULL;
597 }
598
599 // Check for the optional 'trim' word before the marker
600 cmd = skipwhite(cmd);
601 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
602 {
603 cmd = skipwhite(cmd + 4);
604
605 // Trim the indentation from all the lines in the here document.
606 // The amount of indentation trimmed is the same as the indentation of
607 // the first line after the :let command line. To find the end marker
608 // the indent of the :let command line is trimmed.
609 p = *eap->cmdlinep;
610 while (VIM_ISWHITE(*p))
611 {
612 p++;
613 marker_indent_len++;
614 }
615 text_indent_len = -1;
616 }
617
618 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200619 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200620 {
621 marker = skipwhite(cmd);
622 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200623 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200624 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000625 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200626 return NULL;
627 }
628 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200629 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200630 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000631 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200632 return NULL;
633 }
634 }
635 else
636 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200637 // When getting lines for an embedded script, if the marker is missing,
638 // accept '.' as the marker.
639 if (script_get)
640 marker = dot;
641 else
642 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000643 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200644 return NULL;
645 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200646 }
647
648 l = list_alloc();
649 if (l == NULL)
650 return NULL;
651
652 for (;;)
653 {
654 int mi = 0;
655 int ti = 0;
656
657 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
658 if (theline == NULL)
659 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000660 semsg(_(e_missing_end_marker_str), marker);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200661 break;
662 }
663
664 // with "trim": skip the indent matching the :let line to find the
665 // marker
666 if (marker_indent_len > 0
667 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
668 mi = marker_indent_len;
669 if (STRCMP(marker, theline + mi) == 0)
670 {
671 vim_free(theline);
672 break;
673 }
674
675 if (text_indent_len == -1 && *theline != NUL)
676 {
677 // set the text indent from the first line.
678 p = theline;
679 text_indent_len = 0;
680 while (VIM_ISWHITE(*p))
681 {
682 p++;
683 text_indent_len++;
684 }
685 text_indent = vim_strnsave(theline, text_indent_len);
686 }
687 // with "trim": skip the indent matching the first line
688 if (text_indent != NULL)
689 for (ti = 0; ti < text_indent_len; ++ti)
690 if (theline[ti] != text_indent[ti])
691 break;
692
693 if (list_append_string(l, theline + ti, -1) == FAIL)
694 break;
695 vim_free(theline);
696 }
697 vim_free(text_indent);
698
699 return l;
700}
701
702/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200703 * Vim9 variable declaration:
704 * ":var name"
705 * ":var name: type"
706 * ":var name = expr"
707 * ":var name: type = expr"
708 * etc.
709 */
710 void
711ex_var(exarg_T *eap)
712{
713 if (!in_vim9script())
714 {
715 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
716 return;
717 }
718 ex_let(eap);
719}
720
721/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200722 * ":let" list all variable values
723 * ":let var1 var2" list variable values
724 * ":let var = expr" assignment command.
725 * ":let var += expr" assignment command.
726 * ":let var -= expr" assignment command.
727 * ":let var *= expr" assignment command.
728 * ":let var /= expr" assignment command.
729 * ":let var %= expr" assignment command.
730 * ":let var .= expr" assignment command.
731 * ":let var ..= expr" assignment command.
732 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100734 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200735 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200736 * ":final var = expr" assignment command.
737 * ":final [var1, var2] = expr" unpack list.
738 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200739 * ":const" list all variable values
740 * ":const var1 var2" list variable values
741 * ":const var = expr" assignment command.
742 * ":const [var1, var2] = expr" unpack list.
743 */
744 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200745ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200746{
747 char_u *arg = eap->arg;
748 char_u *expr = NULL;
749 typval_T rettv;
750 int i;
751 int var_count = 0;
752 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200753 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200754 char_u *argend;
755 int first = TRUE;
756 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200757 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100758 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200759 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200760
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200761 if (eap->cmdidx == CMD_final && !vim9script)
762 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100763 // In legacy Vim script ":final" is short for ":finally".
764 ex_finally(eap);
765 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200766 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200767 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200768 {
769 emsg(_(e_cannot_use_let_in_vim9_script));
770 return;
771 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200772
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100773 if (eap->cmdidx == CMD_const)
774 flags |= ASSIGN_CONST;
775 else if (eap->cmdidx == CMD_final)
776 flags |= ASSIGN_FINAL;
777
778 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200780 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200782 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200783 if (argend == NULL)
784 return;
785 if (argend > arg && argend[-1] == '.') // for var.='str'
786 --argend;
787 expr = skipwhite(argend);
788 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +0200789 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200790 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200791 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
792 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200793 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200794 {
795 // ":let" without "=": list variables
796 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000797 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200798 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000799 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200800 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200801 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200802 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200803 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100804 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +0000805 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100806 else
807 // Vim9 declaration ":var name: type"
808 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200809 }
810 else
811 {
812 // ":let var1 var2" - list values
813 arg = list_arg_vars(eap, arg, &first);
814 }
815 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200816 else if (!eap->skip)
817 {
818 // ":let"
819 list_glob_vars(&first);
820 list_buf_vars(&first);
821 list_win_vars(&first);
822 list_tab_vars(&first);
823 list_script_vars(&first);
824 list_func_vars(&first);
825 list_vim_vars(&first);
826 }
Bram Moolenaar63b91732021-08-05 20:40:03 +0200827 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200828 }
829 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
830 {
831 list_T *l;
Bram Moolenaar81530e32021-07-28 21:25:49 +0200832 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200833
834 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200835 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200836 if (l != NULL)
837 {
838 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200839 if (!eap->skip)
840 {
Bram Moolenaar81530e32021-07-28 21:25:49 +0200841 // errors are for the assignment, not the end marker
842 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200843 op[0] = '=';
844 op[1] = NUL;
845 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100846 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200847 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200848 clear_tv(&rettv);
849 }
850 }
851 else
852 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200853 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200854 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200855
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200856 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200857 i = FAIL;
858 if (has_assign || concat)
859 {
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100860 int cur_lnum;
861
Bram Moolenaar32e35112020-05-14 22:41:15 +0200862 op[0] = '=';
863 op[1] = NUL;
864 if (*expr != '=')
865 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200866 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200867 {
868 // +=, /=, etc. require an existing variable
869 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
870 i = FAIL;
871 }
872 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200873 {
874 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200875 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200876 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200877 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200878 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200879 ++len;
880 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200881 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200882 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200883 }
884 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200885 ++expr;
886
Bram Moolenaar7f2c3412021-11-29 16:01:49 +0000887 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200888 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200889 {
890 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100891 semsg(_(e_white_space_required_before_and_after_str_at_str),
892 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200893 i = FAIL;
894 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200895
896 if (eap->skip)
897 ++emsg_skip;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200898 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200899 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100900 cur_lnum = SOURCING_LNUM;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200901 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200902 if (eap->skip)
903 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200904 clear_evalarg(&evalarg, eap);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100905
906 // Restore the line number so that any type error is given for the
907 // declaration, not the expression.
908 SOURCING_LNUM = cur_lnum;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200909 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200910 if (eap->skip)
911 {
912 if (i != FAIL)
913 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200914 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200915 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200916 {
917 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200918 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200919 clear_tv(&rettv);
920 }
921 }
922}
923
924/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +0100925 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200926 * Handles both "var" with any type and "[var, var; var]" with a list type.
927 * When "op" is not NULL it points to a string with characters that
928 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
929 * or concatenate.
930 * Returns OK or FAIL;
931 */
932 int
933ex_let_vars(
934 char_u *arg_start,
935 typval_T *tv,
936 int copy, // copy values from "tv", don't move
937 int semicolon, // from skip_var_list()
938 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100939 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200940 char_u *op)
941{
942 char_u *arg = arg_start;
943 list_T *l;
944 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100945 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200946 listitem_T *item;
947 typval_T ltv;
948
949 if (*arg != '[')
950 {
951 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100952 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200953 return FAIL;
954 return OK;
955 }
956
957 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
958 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
959 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000960 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200961 return FAIL;
962 }
963
964 i = list_len(l);
965 if (semicolon == 0 && var_count < i)
966 {
Bram Moolenaara6f79292022-01-04 21:30:47 +0000967 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200968 return FAIL;
969 }
970 if (var_count - semicolon > i)
971 {
Bram Moolenaara6f79292022-01-04 21:30:47 +0000972 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200973 return FAIL;
974 }
975
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200976 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200977 item = l->lv_first;
978 while (*arg != ']')
979 {
980 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100981 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +0200982 arg = ex_let_one(arg, &item->li_tv, TRUE,
983 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200984 item = item->li_next;
985 if (arg == NULL)
986 return FAIL;
987
988 arg = skipwhite(arg);
989 if (*arg == ';')
990 {
991 // Put the rest of the list (may be empty) in the var after ';'.
992 // Create a new list for this.
993 l = list_alloc();
994 if (l == NULL)
995 return FAIL;
996 while (item != NULL)
997 {
998 list_append_tv(l, &item->li_tv);
999 item = item->li_next;
1000 }
1001
1002 ltv.v_type = VAR_LIST;
1003 ltv.v_lock = 0;
1004 ltv.vval.v_list = l;
1005 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001006 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001007
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001008 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1009 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001010 clear_tv(&ltv);
1011 if (arg == NULL)
1012 return FAIL;
1013 break;
1014 }
1015 else if (*arg != ',' && *arg != ']')
1016 {
1017 internal_error("ex_let_vars()");
1018 return FAIL;
1019 }
1020 }
1021
1022 return OK;
1023}
1024
1025/*
1026 * Skip over assignable variable "var" or list of variables "[var, var]".
1027 * Used for ":let varvar = expr" and ":for varvar in expr".
1028 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001029 * for "[var, var; var]" set "semicolon" to 1.
1030 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001031 * Return NULL for an error.
1032 */
1033 char_u *
1034skip_var_list(
1035 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001036 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001037 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001038 int *semicolon,
1039 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001040{
1041 char_u *p, *s;
1042
1043 if (*arg == '[')
1044 {
1045 // "[var, var]": find the matching ']'.
1046 p = arg;
1047 for (;;)
1048 {
1049 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001050 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001051 if (s == p)
1052 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001053 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001054 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001055 return NULL;
1056 }
1057 ++*var_count;
1058
1059 p = skipwhite(s);
1060 if (*p == ']')
1061 break;
1062 else if (*p == ';')
1063 {
1064 if (*semicolon == 1)
1065 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001066 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001067 return NULL;
1068 }
1069 *semicolon = 1;
1070 }
1071 else if (*p != ',')
1072 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001073 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001074 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001075 return NULL;
1076 }
1077 }
1078 return p + 1;
1079 }
1080 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001082}
1083
1084/*
1085 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1086 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001088 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001089 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001090skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001091{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001092 char_u *end;
1093 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001095 if (*arg == '@' && arg[1] != NUL)
1096 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001098 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001099
1100 // "a: type" is declaring variable "a" with a type, not "a:".
1101 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001102 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001103 --end;
1104
Bram Moolenaar585587d2021-01-17 20:52:13 +01001105 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001106 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001107 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001108 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109 }
1110 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001111}
1112
1113/*
1114 * List variables for hashtab "ht" with prefix "prefix".
1115 * If "empty" is TRUE also list NULL strings as empty strings.
1116 */
1117 void
1118list_hashtable_vars(
1119 hashtab_T *ht,
1120 char *prefix,
1121 int empty,
1122 int *first)
1123{
1124 hashitem_T *hi;
1125 dictitem_T *di;
1126 int todo;
1127 char_u buf[IOSIZE];
1128
1129 todo = (int)ht->ht_used;
1130 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1131 {
1132 if (!HASHITEM_EMPTY(hi))
1133 {
1134 --todo;
1135 di = HI2DI(hi);
1136
1137 // apply :filter /pat/ to variable name
1138 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1139 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1140 if (message_filtered(buf))
1141 continue;
1142
1143 if (empty || di->di_tv.v_type != VAR_STRING
1144 || di->di_tv.vval.v_string != NULL)
1145 list_one_var(di, prefix, first);
1146 }
1147 }
1148}
1149
1150/*
1151 * List global variables.
1152 */
1153 static void
1154list_glob_vars(int *first)
1155{
1156 list_hashtable_vars(&globvarht, "", TRUE, first);
1157}
1158
1159/*
1160 * List buffer variables.
1161 */
1162 static void
1163list_buf_vars(int *first)
1164{
1165 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1166}
1167
1168/*
1169 * List window variables.
1170 */
1171 static void
1172list_win_vars(int *first)
1173{
1174 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1175}
1176
1177/*
1178 * List tab page variables.
1179 */
1180 static void
1181list_tab_vars(int *first)
1182{
1183 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1184}
1185
1186/*
1187 * List variables in "arg".
1188 */
1189 static char_u *
1190list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1191{
1192 int error = FALSE;
1193 int len;
1194 char_u *name;
1195 char_u *name_start;
1196 char_u *arg_subsc;
1197 char_u *tofree;
1198 typval_T tv;
1199
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001200 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001201 {
1202 if (error || eap->skip)
1203 {
1204 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1205 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1206 {
1207 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001208 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001209 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001210 break;
1211 }
1212 }
1213 else
1214 {
1215 // get_name_len() takes care of expanding curly braces
1216 name_start = name = arg;
1217 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1218 if (len <= 0)
1219 {
1220 // This is mainly to keep test 49 working: when expanding
1221 // curly braces fails overrule the exception error message.
1222 if (len < 0 && !aborting())
1223 {
1224 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001225 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001226 break;
1227 }
1228 error = TRUE;
1229 }
1230 else
1231 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001232 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001233 if (tofree != NULL)
1234 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001235 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001236 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001237 error = TRUE;
1238 else
1239 {
1240 // handle d.key, l[idx], f(expr)
1241 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001242 if (handle_subscript(&arg, name_start, &tv,
1243 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001244 error = TRUE;
1245 else
1246 {
1247 if (arg == arg_subsc && len == 2 && name[1] == ':')
1248 {
1249 switch (*name)
1250 {
1251 case 'g': list_glob_vars(first); break;
1252 case 'b': list_buf_vars(first); break;
1253 case 'w': list_win_vars(first); break;
1254 case 't': list_tab_vars(first); break;
1255 case 'v': list_vim_vars(first); break;
1256 case 's': list_script_vars(first); break;
1257 case 'l': list_func_vars(first); break;
1258 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001259 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001260 }
1261 }
1262 else
1263 {
1264 char_u numbuf[NUMBUFLEN];
1265 char_u *tf;
1266 int c;
1267 char_u *s;
1268
1269 s = echo_string(&tv, &tf, numbuf, 0);
1270 c = *arg;
1271 *arg = NUL;
1272 list_one_var_a("",
1273 arg == arg_subsc ? name : name_start,
1274 tv.v_type,
1275 s == NULL ? (char_u *)"" : s,
1276 first);
1277 *arg = c;
1278 vim_free(tf);
1279 }
1280 clear_tv(&tv);
1281 }
1282 }
1283 }
1284
1285 vim_free(tofree);
1286 }
1287
1288 arg = skipwhite(arg);
1289 }
1290
1291 return arg;
1292}
1293
1294/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001295 * Set an environment variable, part of ex_let_one().
1296 */
1297 static char_u *
1298ex_let_env(
1299 char_u *arg,
1300 typval_T *tv,
1301 int flags,
1302 char_u *endchars,
1303 char_u *op)
1304{
1305 char_u *arg_end = NULL;
1306 char_u *name;
1307 int len;
1308
1309 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1310 && (flags & ASSIGN_FOR_LOOP) == 0)
1311 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001312 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001313 return NULL;
1314 }
1315
1316 // Find the end of the name.
1317 ++arg;
1318 name = arg;
1319 len = get_env_len(&arg);
1320 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001321 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001322 else
1323 {
1324 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001325 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001326 else if (endchars != NULL
1327 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1328 emsg(_(e_unexpected_characters_in_let));
1329 else if (!check_secure())
1330 {
1331 char_u *tofree = NULL;
1332 int c1 = name[len];
1333 char_u *p;
1334
1335 name[len] = NUL;
1336 p = tv_get_string_chk(tv);
1337 if (p != NULL && op != NULL && *op == '.')
1338 {
1339 int mustfree = FALSE;
1340 char_u *s = vim_getenv(name, &mustfree);
1341
1342 if (s != NULL)
1343 {
1344 p = tofree = concat_str(s, p);
1345 if (mustfree)
1346 vim_free(s);
1347 }
1348 }
1349 if (p != NULL)
1350 {
1351 vim_setenv_ext(name, p);
1352 arg_end = arg;
1353 }
1354 name[len] = c1;
1355 vim_free(tofree);
1356 }
1357 }
1358 return arg_end;
1359}
1360
1361/*
1362 * Set an option, part of ex_let_one().
1363 */
1364 static char_u *
1365ex_let_option(
1366 char_u *arg,
1367 typval_T *tv,
1368 int flags,
1369 char_u *endchars,
1370 char_u *op)
1371{
1372 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001373 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001374 char_u *arg_end = NULL;
1375
1376 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1377 && (flags & ASSIGN_FOR_LOOP) == 0)
1378 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001379 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001380 return NULL;
1381 }
1382
1383 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001384 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001385 if (p == NULL || (endchars != NULL
1386 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1387 emsg(_(e_unexpected_characters_in_let));
1388 else
1389 {
1390 int c1;
1391 long n = 0;
1392 getoption_T opt_type;
1393 long numval;
1394 char_u *stringval = NULL;
1395 char_u *s = NULL;
1396 int failed = FALSE;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001397 int opt_p_flags;
1398 char_u *tofree = NULL;
Bram Moolenaar92c33eb2021-12-07 11:03:39 +00001399 char_u numbuf[NUMBUFLEN];
1400
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001401 c1 = *p;
1402 *p = NUL;
1403
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001404 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags,
1405 scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001406 if ((opt_type == gov_bool
1407 || opt_type == gov_number
1408 || opt_type == gov_hidden_bool
1409 || opt_type == gov_hidden_number)
1410 && (tv->v_type != VAR_STRING || !in_vim9script()))
1411 {
1412 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1413 // bool, possibly hidden
1414 n = (long)tv_get_bool(tv);
1415 else
1416 // number, possibly hidden
1417 n = (long)tv_get_number(tv);
1418 }
1419
Bram Moolenaaref082e12021-12-12 21:02:03 +00001420 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001421 || tv->v_type == VAR_FUNC))
1422 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001423 // If the option can be set to a function reference or a lambda
1424 // and the passed value is a function reference, then convert it to
1425 // the name (string) of the function reference.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001426 s = tv2string(tv, &tofree, numbuf, 0);
1427 }
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001428 // Avoid setting a string option to the text "v:false" or similar.
1429 // In Vim9 script also don't convert a number to string.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001430 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001431 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1432 s = tv_get_string_chk(tv);
1433
1434 if (op != NULL && *op != '=')
1435 {
1436 if (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1437 || (opt_type == gov_string && *op != '.'))
1438 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001439 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001440 failed = TRUE; // don't set the value
1441
1442 }
1443 else
1444 {
1445 // number, in legacy script also bool
1446 if (opt_type == gov_number
1447 || (opt_type == gov_bool && !in_vim9script()))
1448 {
1449 switch (*op)
1450 {
1451 case '+': n = numval + n; break;
1452 case '-': n = numval - n; break;
1453 case '*': n = numval * n; break;
1454 case '/': n = (long)num_divide(numval, n,
1455 &failed); break;
1456 case '%': n = (long)num_modulus(numval, n,
1457 &failed); break;
1458 }
1459 s = NULL;
1460 }
1461 else if (opt_type == gov_string
1462 && stringval != NULL && s != NULL)
1463 {
1464 // string
1465 s = concat_str(stringval, s);
1466 vim_free(stringval);
1467 stringval = s;
1468 }
1469 }
1470 }
1471
1472 if (!failed)
1473 {
1474 if (opt_type != gov_string || s != NULL)
1475 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001476 set_option_value(arg, n, s, scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001477 arg_end = p;
1478 }
1479 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001480 emsg(_(e_string_required));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001481 }
1482 *p = c1;
1483 vim_free(stringval);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001484 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001485 }
1486 return arg_end;
1487}
1488
1489/*
1490 * Set a register, part of ex_let_one().
1491 */
1492 static char_u *
1493ex_let_register(
1494 char_u *arg,
1495 typval_T *tv,
1496 int flags,
1497 char_u *endchars,
1498 char_u *op)
1499{
1500 char_u *arg_end = NULL;
1501
1502 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1503 && (flags & ASSIGN_FOR_LOOP) == 0)
1504 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001505 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001506 return NULL;
1507 }
1508 ++arg;
1509 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001510 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001511 else if (endchars != NULL
1512 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1513 emsg(_(e_unexpected_characters_in_let));
1514 else
1515 {
1516 char_u *ptofree = NULL;
1517 char_u *p;
1518
1519 p = tv_get_string_chk(tv);
1520 if (p != NULL && op != NULL && *op == '.')
1521 {
1522 char_u *s = get_reg_contents(*arg == '@'
1523 ? '"' : *arg, GREG_EXPR_SRC);
1524
1525 if (s != NULL)
1526 {
1527 p = ptofree = concat_str(s, p);
1528 vim_free(s);
1529 }
1530 }
1531 if (p != NULL)
1532 {
1533 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1534 arg_end = arg + 1;
1535 }
1536 vim_free(ptofree);
1537 }
1538 return arg_end;
1539}
1540
1541/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001542 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1543 * Returns a pointer to the char just after the var name.
1544 * Returns NULL if there is an error.
1545 */
1546 static char_u *
1547ex_let_one(
1548 char_u *arg, // points to variable name
1549 typval_T *tv, // value to assign to variable
1550 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001551 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001552 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001553 char_u *op, // "+", "-", "." or NULL
1554 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001555{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001556 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001557
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001558 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001559 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001560 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1561 {
1562 vim9_declare_error(arg);
1563 return NULL;
1564 }
1565
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001566 if (*arg == '$')
1567 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001568 // ":let $VAR = expr": Set environment variable.
1569 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001570 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001571 else if (*arg == '&')
1572 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001573 // ":let &option = expr": Set option value.
1574 // ":let &l:option = expr": Set local option value.
1575 // ":let &g:option = expr": Set global option value.
1576 // ":for &ts in range(8)": Set option value for for loop
1577 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001578 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001579 else if (*arg == '@')
1580 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001581 // ":let @r = expr": Set register contents.
1582 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001583 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001584 else if (eval_isnamec1(*arg) || *arg == '{')
1585 {
1586 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001587 char_u *p;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001588
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001589 // ":let var = expr": Set internal variable.
1590 // ":let var: type = expr": Set internal variable with type.
1591 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001592 p = get_lval(arg, tv, &lv, FALSE, FALSE,
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001593 (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1594 ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001595 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001596 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597 if (endchars != NULL && vim_strchr(endchars,
1598 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001599 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001600 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001601 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001602 else
1603 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001604 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001605 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001606 }
1607 }
1608 clear_lval(&lv);
1609 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001610 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001611 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001612
1613 return arg_end;
1614}
1615
1616/*
1617 * ":unlet[!] var1 ... " command.
1618 */
1619 void
1620ex_unlet(exarg_T *eap)
1621{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001622 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001623}
1624
1625/*
1626 * ":lockvar" and ":unlockvar" commands
1627 */
1628 void
1629ex_lockvar(exarg_T *eap)
1630{
1631 char_u *arg = eap->arg;
1632 int deep = 2;
1633
1634 if (eap->forceit)
1635 deep = -1;
1636 else if (vim_isdigit(*arg))
1637 {
1638 deep = getdigits(&arg);
1639 arg = skipwhite(arg);
1640 }
1641
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001642 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001643}
1644
1645/*
1646 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001647 * Also used for Vim9 script. "callback" is invoked as:
1648 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001649 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001650 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001651ex_unletlock(
1652 exarg_T *eap,
1653 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001654 int deep,
1655 int glv_flags,
1656 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1657 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001658{
1659 char_u *arg = argstart;
1660 char_u *name_end;
1661 int error = FALSE;
1662 lval_T lv;
1663
1664 do
1665 {
1666 if (*arg == '$')
1667 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001668 lv.ll_name = arg;
1669 lv.ll_tv = NULL;
1670 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001671 if (get_env_len(&arg) == 0)
1672 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001673 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001674 return;
1675 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001676 if (!error && !eap->skip
1677 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1678 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001679 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001680 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001681 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001682 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001683 // Parse the name and find the end.
1684 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001685 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001686 if (lv.ll_name == NULL)
1687 error = TRUE; // error but continue parsing
1688 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1689 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001690 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001691 if (name_end != NULL)
1692 {
1693 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00001694 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001695 }
1696 if (!(eap->skip || error))
1697 clear_lval(&lv);
1698 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001699 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001700
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001701 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001702 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001703 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001704
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001705 if (!eap->skip)
1706 clear_lval(&lv);
1707 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001708
1709 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001710 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001711
Bram Moolenaar63b91732021-08-05 20:40:03 +02001712 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001713}
1714
1715 static int
1716do_unlet_var(
1717 lval_T *lp,
1718 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001719 exarg_T *eap,
1720 int deep UNUSED,
1721 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001722{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001723 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001724 int ret = OK;
1725 int cc;
1726
1727 if (lp->ll_tv == NULL)
1728 {
1729 cc = *name_end;
1730 *name_end = NUL;
1731
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001732 // Environment variable, normal name or expanded name.
1733 if (*lp->ll_name == '$')
1734 vim_unsetenv(lp->ll_name + 1);
1735 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001736 ret = FAIL;
1737 *name_end = cc;
1738 }
1739 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001740 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001741 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001742 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001743 return FAIL;
1744 else if (lp->ll_range)
1745 {
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001746 if (list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_name, lp->ll_n1,
1747 !lp->ll_empty2, lp->ll_n2) == FAIL)
1748 return FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001749 }
1750 else
1751 {
1752 if (lp->ll_list != NULL)
1753 // unlet a List item.
1754 listitem_remove(lp->ll_list, lp->ll_li);
1755 else
1756 // unlet a Dictionary item.
1757 dictitem_remove(lp->ll_dict, lp->ll_di);
1758 }
1759
1760 return ret;
1761}
1762
1763/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001764 * Unlet one item or a range of items from a list.
1765 * Return OK or FAIL.
1766 */
1767 int
1768list_unlet_range(
1769 list_T *l,
1770 listitem_T *li_first,
1771 char_u *name,
1772 long n1_arg,
1773 int has_n2,
1774 long n2)
1775{
1776 listitem_T *li = li_first;
1777 int n1 = n1_arg;
1778
1779 while (li != NULL && (!has_n2 || n2 >= n1))
1780 {
1781 if (value_check_lock(li->li_tv.v_lock, name, FALSE))
1782 return FAIL;
1783 li = li->li_next;
1784 ++n1;
1785 }
1786
1787 // Delete a range of List items.
1788 li = li_first;
1789 n1 = n1_arg;
1790 while (li != NULL && (!has_n2 || n2 >= n1))
1791 {
1792 listitem_T *next = li->li_next;
1793
1794 listitem_remove(l, li);
1795 li = next;
1796 ++n1;
1797 }
1798 return OK;
1799}
1800/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001801 * "unlet" a variable. Return OK if it existed, FAIL if not.
1802 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1803 */
1804 int
1805do_unlet(char_u *name, int forceit)
1806{
1807 hashtab_T *ht;
1808 hashitem_T *hi;
1809 char_u *varname;
1810 dict_T *d;
1811 dictitem_T *di;
1812
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001813 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001814 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001815 return FAIL;
1816
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001817 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001818
1819 // can't :unlet a script variable in Vim9 script from a function
1820 if (ht == get_script_local_ht()
1821 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1822 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1823 == SCRIPT_VERSION_VIM9
1824 && check_vim9_unlet(name) == FAIL)
1825 return FAIL;
1826
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001827 if (ht != NULL && *varname != NUL)
1828 {
1829 d = get_current_funccal_dict(ht);
1830 if (d == NULL)
1831 {
1832 if (ht == &globvarht)
1833 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001834 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001835 d = &vimvardict;
1836 else
1837 {
1838 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1839 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1840 }
1841 if (d == NULL)
1842 {
1843 internal_error("do_unlet()");
1844 return FAIL;
1845 }
1846 }
1847 hi = hash_find(ht, varname);
1848 if (HASHITEM_EMPTY(hi))
1849 hi = find_hi_in_scoped_ht(name, &ht);
1850 if (hi != NULL && !HASHITEM_EMPTY(hi))
1851 {
1852 di = HI2DI(hi);
1853 if (var_check_fixed(di->di_flags, name, FALSE)
1854 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001855 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001856 return FAIL;
1857
1858 delete_var(ht, hi);
1859 return OK;
1860 }
1861 }
1862 if (forceit)
1863 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00001864 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001865 return FAIL;
1866}
1867
1868/*
1869 * Lock or unlock variable indicated by "lp".
1870 * "deep" is the levels to go (-1 for unlimited);
1871 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1872 */
1873 static int
1874do_lock_var(
1875 lval_T *lp,
1876 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001877 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001878 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001879 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001880{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001881 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001882 int ret = OK;
1883 int cc;
1884 dictitem_T *di;
1885
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001886 if (lp->ll_tv == NULL)
1887 {
1888 cc = *name_end;
1889 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001890 if (*lp->ll_name == '$')
1891 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001892 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001893 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001894 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001895 else
1896 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001897 // Normal name or expanded name.
1898 di = find_var(lp->ll_name, NULL, TRUE);
1899 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001900 {
1901 if (in_vim9script())
1902 semsg(_(e_cannot_find_variable_to_unlock_str),
1903 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001904 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001905 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001906 else if ((di->di_flags & DI_FLAGS_FIX)
1907 && di->di_tv.v_type != VAR_DICT
1908 && di->di_tv.v_type != VAR_LIST)
1909 {
1910 // For historic reasons this error is not given for a list or
1911 // dict. E.g., the b: dict could be locked/unlocked.
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001912 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001913 ret = FAIL;
1914 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001915 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001916 {
1917 if (lock)
1918 di->di_flags |= DI_FLAGS_LOCK;
1919 else
1920 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001921 if (deep != 0)
1922 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001923 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001924 }
1925 *name_end = cc;
1926 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001927 else if (deep == 0)
1928 {
1929 // nothing to do
1930 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001931 else if (lp->ll_range)
1932 {
1933 listitem_T *li = lp->ll_li;
1934
1935 // (un)lock a range of List items.
1936 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1937 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001938 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001939 li = li->li_next;
1940 ++lp->ll_n1;
1941 }
1942 }
1943 else if (lp->ll_list != NULL)
1944 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001945 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001946 else
1947 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001948 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001949
1950 return ret;
1951}
1952
1953/*
1954 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001955 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1956 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001957 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001958 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001959item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001960{
1961 static int recurse = 0;
1962 list_T *l;
1963 listitem_T *li;
1964 dict_T *d;
1965 blob_T *b;
1966 hashitem_T *hi;
1967 int todo;
1968
1969 if (recurse >= DICT_MAXNEST)
1970 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00001971 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001972 return;
1973 }
1974 if (deep == 0)
1975 return;
1976 ++recurse;
1977
1978 // lock/unlock the item itself
1979 if (lock)
1980 tv->v_lock |= VAR_LOCKED;
1981 else
1982 tv->v_lock &= ~VAR_LOCKED;
1983
1984 switch (tv->v_type)
1985 {
1986 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001987 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001989 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001991 case VAR_STRING:
1992 case VAR_FUNC:
1993 case VAR_PARTIAL:
1994 case VAR_FLOAT:
1995 case VAR_SPECIAL:
1996 case VAR_JOB:
1997 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001998 case VAR_INSTR:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001999 break;
2000
2001 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002002 if ((b = tv->vval.v_blob) != NULL
2003 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002004 {
2005 if (lock)
2006 b->bv_lock |= VAR_LOCKED;
2007 else
2008 b->bv_lock &= ~VAR_LOCKED;
2009 }
2010 break;
2011 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002012 if ((l = tv->vval.v_list) != NULL
2013 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002014 {
2015 if (lock)
2016 l->lv_lock |= VAR_LOCKED;
2017 else
2018 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002019 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002020 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002021 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02002022 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002023 }
2024 break;
2025 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002026 if ((d = tv->vval.v_dict) != NULL
2027 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002028 {
2029 if (lock)
2030 d->dv_lock |= VAR_LOCKED;
2031 else
2032 d->dv_lock &= ~VAR_LOCKED;
2033 if (deep < 0 || deep > 1)
2034 {
2035 // recursive: lock/unlock the items the List contains
2036 todo = (int)d->dv_hashtab.ht_used;
2037 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2038 {
2039 if (!HASHITEM_EMPTY(hi))
2040 {
2041 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002042 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2043 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002044 }
2045 }
2046 }
2047 }
2048 }
2049 --recurse;
2050}
2051
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002052#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2053/*
2054 * Delete all "menutrans_" variables.
2055 */
2056 void
2057del_menutrans_vars(void)
2058{
2059 hashitem_T *hi;
2060 int todo;
2061
2062 hash_lock(&globvarht);
2063 todo = (int)globvarht.ht_used;
2064 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2065 {
2066 if (!HASHITEM_EMPTY(hi))
2067 {
2068 --todo;
2069 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2070 delete_var(&globvarht, hi);
2071 }
2072 }
2073 hash_unlock(&globvarht);
2074}
2075#endif
2076
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002077/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002078 * Local string buffer for the next two functions to store a variable name
2079 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2080 * get_user_var_name().
2081 */
2082
2083static char_u *varnamebuf = NULL;
2084static int varnamebuflen = 0;
2085
2086/*
2087 * Function to concatenate a prefix and a variable name.
2088 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002089 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002090cat_prefix_varname(int prefix, char_u *name)
2091{
2092 int len;
2093
2094 len = (int)STRLEN(name) + 3;
2095 if (len > varnamebuflen)
2096 {
2097 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002098 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002099 varnamebuf = alloc(len);
2100 if (varnamebuf == NULL)
2101 {
2102 varnamebuflen = 0;
2103 return NULL;
2104 }
2105 varnamebuflen = len;
2106 }
2107 *varnamebuf = prefix;
2108 varnamebuf[1] = ':';
2109 STRCPY(varnamebuf + 2, name);
2110 return varnamebuf;
2111}
2112
2113/*
2114 * Function given to ExpandGeneric() to obtain the list of user defined
2115 * (global/buffer/window/built-in) variable names.
2116 */
2117 char_u *
2118get_user_var_name(expand_T *xp, int idx)
2119{
2120 static long_u gdone;
2121 static long_u bdone;
2122 static long_u wdone;
2123 static long_u tdone;
2124 static int vidx;
2125 static hashitem_T *hi;
2126 hashtab_T *ht;
2127
2128 if (idx == 0)
2129 {
2130 gdone = bdone = wdone = vidx = 0;
2131 tdone = 0;
2132 }
2133
2134 // Global variables
2135 if (gdone < globvarht.ht_used)
2136 {
2137 if (gdone++ == 0)
2138 hi = globvarht.ht_array;
2139 else
2140 ++hi;
2141 while (HASHITEM_EMPTY(hi))
2142 ++hi;
2143 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2144 return cat_prefix_varname('g', hi->hi_key);
2145 return hi->hi_key;
2146 }
2147
2148 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002149 ht =
2150#ifdef FEAT_CMDWIN
2151 // In cmdwin, the alternative buffer should be used.
mityua1198122021-11-20 19:13:39 +00002152 is_in_cmdwin() ? &prevwin->w_buffer->b_vars->dv_hashtab :
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002153#endif
2154 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002155 if (bdone < ht->ht_used)
2156 {
2157 if (bdone++ == 0)
2158 hi = ht->ht_array;
2159 else
2160 ++hi;
2161 while (HASHITEM_EMPTY(hi))
2162 ++hi;
2163 return cat_prefix_varname('b', hi->hi_key);
2164 }
2165
2166 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002167 ht =
2168#ifdef FEAT_CMDWIN
2169 // In cmdwin, the alternative window should be used.
mityua1198122021-11-20 19:13:39 +00002170 is_in_cmdwin() ? &prevwin->w_vars->dv_hashtab :
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002171#endif
2172 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002173 if (wdone < ht->ht_used)
2174 {
2175 if (wdone++ == 0)
2176 hi = ht->ht_array;
2177 else
2178 ++hi;
2179 while (HASHITEM_EMPTY(hi))
2180 ++hi;
2181 return cat_prefix_varname('w', hi->hi_key);
2182 }
2183
2184 // t: variables
2185 ht = &curtab->tp_vars->dv_hashtab;
2186 if (tdone < ht->ht_used)
2187 {
2188 if (tdone++ == 0)
2189 hi = ht->ht_array;
2190 else
2191 ++hi;
2192 while (HASHITEM_EMPTY(hi))
2193 ++hi;
2194 return cat_prefix_varname('t', hi->hi_key);
2195 }
2196
2197 // v: variables
2198 if (vidx < VV_LEN)
2199 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2200
2201 VIM_CLEAR(varnamebuf);
2202 varnamebuflen = 0;
2203 return NULL;
2204}
2205
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002206 char *
2207get_var_special_name(int nr)
2208{
2209 switch (nr)
2210 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002211 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2212 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002213 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002214 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002215 }
2216 internal_error("get_var_special_name()");
2217 return "42";
2218}
2219
2220/*
2221 * Returns the global variable dictionary
2222 */
2223 dict_T *
2224get_globvar_dict(void)
2225{
2226 return &globvardict;
2227}
2228
2229/*
2230 * Returns the global variable hash table
2231 */
2232 hashtab_T *
2233get_globvar_ht(void)
2234{
2235 return &globvarht;
2236}
2237
2238/*
2239 * Returns the v: variable dictionary
2240 */
2241 dict_T *
2242get_vimvar_dict(void)
2243{
2244 return &vimvardict;
2245}
2246
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002247/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002248 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002249 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250 */
2251 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002252find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002254 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2255 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256
2257 if (di == NULL)
2258 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002259 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2261 return (int)(vv - vimvars);
2262}
2263
2264
2265/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002266 * Set type of v: variable to "type".
2267 */
2268 void
2269set_vim_var_type(int idx, vartype_T type)
2270{
Bram Moolenaard787e402021-12-24 21:36:12 +00002271 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002272}
2273
2274/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002275 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002276 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002277 */
2278 void
2279set_vim_var_nr(int idx, varnumber_T val)
2280{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002281 vimvars[idx].vv_nr = val;
2282}
2283
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 char *
2285get_vim_var_name(int idx)
2286{
2287 return vimvars[idx].vv_name;
2288}
2289
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002290/*
2291 * Get typval_T v: variable value.
2292 */
2293 typval_T *
2294get_vim_var_tv(int idx)
2295{
2296 return &vimvars[idx].vv_tv;
2297}
2298
Bram Moolenaard787e402021-12-24 21:36:12 +00002299 type_T *
2300get_vim_var_type(int idx, garray_T *type_list)
2301{
2302 if (vimvars[idx].vv_type != NULL)
2303 return vimvars[idx].vv_type;
2304 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2305}
2306
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002307/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002308 * Set v: variable to "tv". Only accepts the same type.
2309 * Takes over the value of "tv".
2310 */
2311 int
2312set_vim_var_tv(int idx, typval_T *tv)
2313{
Bram Moolenaard787e402021-12-24 21:36:12 +00002314 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002315 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002316 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002317 clear_tv(tv);
2318 return FAIL;
2319 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002320 // VV_RO is also checked when compiling, but let's check here as well.
2321 if (vimvars[idx].vv_flags & VV_RO)
2322 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002323 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002324 return FAIL;
2325 }
2326 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2327 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002328 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002329 return FAIL;
2330 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002331 clear_tv(&vimvars[idx].vv_di.di_tv);
2332 vimvars[idx].vv_di.di_tv = *tv;
2333 return OK;
2334}
2335
2336/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002337 * Get number v: variable value.
2338 */
2339 varnumber_T
2340get_vim_var_nr(int idx)
2341{
2342 return vimvars[idx].vv_nr;
2343}
2344
2345/*
2346 * Get string v: variable value. Uses a static buffer, can only be used once.
2347 * If the String variable has never been set, return an empty string.
2348 * Never returns NULL;
2349 */
2350 char_u *
2351get_vim_var_str(int idx)
2352{
2353 return tv_get_string(&vimvars[idx].vv_tv);
2354}
2355
2356/*
2357 * Get List v: variable value. Caller must take care of reference count when
2358 * needed.
2359 */
2360 list_T *
2361get_vim_var_list(int idx)
2362{
2363 return vimvars[idx].vv_list;
2364}
2365
2366/*
2367 * Get Dict v: variable value. Caller must take care of reference count when
2368 * needed.
2369 */
2370 dict_T *
2371get_vim_var_dict(int idx)
2372{
2373 return vimvars[idx].vv_dict;
2374}
2375
2376/*
2377 * Set v:char to character "c".
2378 */
2379 void
2380set_vim_var_char(int c)
2381{
2382 char_u buf[MB_MAXBYTES + 1];
2383
2384 if (has_mbyte)
2385 buf[(*mb_char2bytes)(c, buf)] = NUL;
2386 else
2387 {
2388 buf[0] = c;
2389 buf[1] = NUL;
2390 }
2391 set_vim_var_string(VV_CHAR, buf, -1);
2392}
2393
2394/*
2395 * Set v:count to "count" and v:count1 to "count1".
2396 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2397 */
2398 void
2399set_vcount(
2400 long count,
2401 long count1,
2402 int set_prevcount)
2403{
2404 if (set_prevcount)
2405 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2406 vimvars[VV_COUNT].vv_nr = count;
2407 vimvars[VV_COUNT1].vv_nr = count1;
2408}
2409
2410/*
2411 * Save variables that might be changed as a side effect. Used when executing
2412 * a timer callback.
2413 */
2414 void
2415save_vimvars(vimvars_save_T *vvsave)
2416{
2417 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2418 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2419 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2420}
2421
2422/*
2423 * Restore variables saved by save_vimvars().
2424 */
2425 void
2426restore_vimvars(vimvars_save_T *vvsave)
2427{
2428 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2429 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2430 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2431}
2432
2433/*
2434 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2435 * value.
2436 */
2437 void
2438set_vim_var_string(
2439 int idx,
2440 char_u *val,
2441 int len) // length of "val" to use or -1 (whole string)
2442{
2443 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002444 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002445 if (val == NULL)
2446 vimvars[idx].vv_str = NULL;
2447 else if (len == -1)
2448 vimvars[idx].vv_str = vim_strsave(val);
2449 else
2450 vimvars[idx].vv_str = vim_strnsave(val, len);
2451}
2452
2453/*
2454 * Set List v: variable to "val".
2455 */
2456 void
2457set_vim_var_list(int idx, list_T *val)
2458{
2459 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002460 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002461 vimvars[idx].vv_list = val;
2462 if (val != NULL)
2463 ++val->lv_refcount;
2464}
2465
2466/*
2467 * Set Dictionary v: variable to "val".
2468 */
2469 void
2470set_vim_var_dict(int idx, dict_T *val)
2471{
2472 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002473 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002474 vimvars[idx].vv_dict = val;
2475 if (val != NULL)
2476 {
2477 ++val->dv_refcount;
2478 dict_set_items_ro(val);
2479 }
2480}
2481
2482/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002483 * Set the v:argv list.
2484 */
2485 void
2486set_argv_var(char **argv, int argc)
2487{
2488 list_T *l = list_alloc();
2489 int i;
2490
2491 if (l == NULL)
2492 getout(1);
2493 l->lv_lock = VAR_FIXED;
2494 for (i = 0; i < argc; ++i)
2495 {
2496 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2497 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002498 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002499 }
2500 set_vim_var_list(VV_ARGV, l);
2501}
2502
2503/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002504 * Reset v:register, taking the 'clipboard' setting into account.
2505 */
2506 void
2507reset_reg_var(void)
2508{
2509 int regname = 0;
2510
2511 // Adjust the register according to 'clipboard', so that when
2512 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2513#ifdef FEAT_CLIPBOARD
2514 adjust_clip_reg(&regname);
2515#endif
2516 set_reg_var(regname);
2517}
2518
2519/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002520 * Set v:register if needed.
2521 */
2522 void
2523set_reg_var(int c)
2524{
2525 char_u regname;
2526
2527 if (c == 0 || c == ' ')
2528 regname = '"';
2529 else
2530 regname = c;
2531 // Avoid free/alloc when the value is already right.
2532 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2533 set_vim_var_string(VV_REG, &regname, 1);
2534}
2535
2536/*
2537 * Get or set v:exception. If "oldval" == NULL, return the current value.
2538 * Otherwise, restore the value to "oldval" and return NULL.
2539 * Must always be called in pairs to save and restore v:exception! Does not
2540 * take care of memory allocations.
2541 */
2542 char_u *
2543v_exception(char_u *oldval)
2544{
2545 if (oldval == NULL)
2546 return vimvars[VV_EXCEPTION].vv_str;
2547
2548 vimvars[VV_EXCEPTION].vv_str = oldval;
2549 return NULL;
2550}
2551
2552/*
2553 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2554 * Otherwise, restore the value to "oldval" and return NULL.
2555 * Must always be called in pairs to save and restore v:throwpoint! Does not
2556 * take care of memory allocations.
2557 */
2558 char_u *
2559v_throwpoint(char_u *oldval)
2560{
2561 if (oldval == NULL)
2562 return vimvars[VV_THROWPOINT].vv_str;
2563
2564 vimvars[VV_THROWPOINT].vv_str = oldval;
2565 return NULL;
2566}
2567
2568/*
2569 * Set v:cmdarg.
2570 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2571 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2572 * Must always be called in pairs!
2573 */
2574 char_u *
2575set_cmdarg(exarg_T *eap, char_u *oldarg)
2576{
2577 char_u *oldval;
2578 char_u *newval;
2579 unsigned len;
2580
2581 oldval = vimvars[VV_CMDARG].vv_str;
2582 if (eap == NULL)
2583 {
2584 vim_free(oldval);
2585 vimvars[VV_CMDARG].vv_str = oldarg;
2586 return NULL;
2587 }
2588
2589 if (eap->force_bin == FORCE_BIN)
2590 len = 6;
2591 else if (eap->force_bin == FORCE_NOBIN)
2592 len = 8;
2593 else
2594 len = 0;
2595
2596 if (eap->read_edit)
2597 len += 7;
2598
2599 if (eap->force_ff != 0)
2600 len += 10; // " ++ff=unix"
2601 if (eap->force_enc != 0)
2602 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2603 if (eap->bad_char != 0)
2604 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2605
2606 newval = alloc(len + 1);
2607 if (newval == NULL)
2608 return NULL;
2609
2610 if (eap->force_bin == FORCE_BIN)
2611 sprintf((char *)newval, " ++bin");
2612 else if (eap->force_bin == FORCE_NOBIN)
2613 sprintf((char *)newval, " ++nobin");
2614 else
2615 *newval = NUL;
2616
2617 if (eap->read_edit)
2618 STRCAT(newval, " ++edit");
2619
2620 if (eap->force_ff != 0)
2621 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2622 eap->force_ff == 'u' ? "unix"
2623 : eap->force_ff == 'd' ? "dos"
2624 : "mac");
2625 if (eap->force_enc != 0)
2626 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2627 eap->cmd + eap->force_enc);
2628 if (eap->bad_char == BAD_KEEP)
2629 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2630 else if (eap->bad_char == BAD_DROP)
2631 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2632 else if (eap->bad_char != 0)
2633 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2634 vimvars[VV_CMDARG].vv_str = newval;
2635 return oldval;
2636}
2637
2638/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002639 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002640 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2641 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002642 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2643 */
2644 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002645eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002646 char_u *name,
2647 int len, // length of "name"
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002648 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002649 typval_T *rettv, // NULL when only checking existence
2650 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002651 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002652{
2653 int ret = OK;
2654 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002655 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002656 hashtab_T *ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002657 int cc;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002658 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002659
2660 // truncate the name, so that we can use strcmp()
2661 cc = name[len];
2662 name[len] = NUL;
2663
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002664 // Check for local variable when debugging.
2665 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002666 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002667 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002668 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2669
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002670 if (v != NULL)
2671 {
2672 tv = &v->di_tv;
2673 if (dip != NULL)
2674 *dip = v;
2675 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002676 else
2677 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002678 }
2679
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002680 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002682 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002683 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2684
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002685 if (sid == 0)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002686 import = find_imported(p, 0, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002687
2688 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002689 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002691 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002692 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00002693 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02002694 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002695 ht = &SCRIPT_VARS(sid);
2696 if (ht != NULL)
2697 {
2698 dictitem_T *v = find_var_in_ht(ht, 0, name,
2699 flags & EVAL_VAR_NOAUTOLOAD);
2700
2701 if (v != NULL)
2702 {
2703 tv = &v->di_tv;
2704 if (dip != NULL)
2705 *dip = v;
2706 }
2707 else
2708 ht = NULL;
2709 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002710 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002711 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002712 {
2713 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00002714 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002715 ret = FAIL;
2716 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002717 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002718 else
2719 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002720 if (rettv != NULL)
2721 {
2722 rettv->v_type = VAR_ANY;
2723 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
2724 }
2725 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002726 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00002728 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002729 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002730 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002731
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002732 // In Vim9 script we can get a function reference by using the
2733 // function name.
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002734 if (ufunc != NULL)
2735 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002736 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002737 if (rettv != NULL)
2738 {
2739 rettv->v_type = VAR_FUNC;
Bram Moolenaaref082e12021-12-12 21:02:03 +00002740 if (STRNCMP(name, "g:", 2) == 0)
2741 // Keep the "g:", otherwise script-local may be
2742 // assumed.
2743 rettv->vval.v_string = vim_strsave(name);
2744 else
2745 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002746 if (rettv->vval.v_string != NULL)
2747 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002748 }
2749 }
2750 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 }
2752
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002753 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002754 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002755 if (tv == NULL)
2756 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002757 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002758 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002759 ret = FAIL;
2760 }
2761 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002762 {
Bram Moolenaar11005b02021-07-11 20:59:00 +02002763 if (ht != NULL && ht == get_script_local_ht()
2764 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002765 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002766 svar_T *sv = find_typval_in_script(tv, 0);
Bram Moolenaarf055d452021-07-08 20:57:24 +02002767
Bram Moolenaarf055d452021-07-08 20:57:24 +02002768 if (sv != NULL)
2769 type = sv->sv_type;
2770 }
2771
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002772 // If a list or dict variable wasn't initialized, do it now.
2773 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2774 {
2775 tv->vval.v_dict = dict_alloc();
2776 if (tv->vval.v_dict != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002777 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002778 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002779 tv->vval.v_dict->dv_type = alloc_type(type);
2780 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002781 }
2782 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2783 {
2784 tv->vval.v_list = list_alloc();
2785 if (tv->vval.v_list != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002786 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002787 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002788 tv->vval.v_list->lv_type = alloc_type(type);
2789 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002790 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002791 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL)
2792 {
2793 tv->vval.v_blob = blob_alloc();
2794 if (tv->vval.v_blob != NULL)
2795 ++tv->vval.v_blob->bv_refcount;
2796 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002797 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002798 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002799 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002800
2801 name[len] = cc;
2802
2803 return ret;
2804}
2805
2806/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002807 * Check if variable "name[len]" is a local variable or an argument.
2808 * If so, "*eval_lavars_used" is set to TRUE.
2809 */
2810 void
2811check_vars(char_u *name, int len)
2812{
2813 int cc;
2814 char_u *varname;
2815 hashtab_T *ht;
2816
2817 if (eval_lavars_used == NULL)
2818 return;
2819
2820 // truncate the name, so that we can use strcmp()
2821 cc = name[len];
2822 name[len] = NUL;
2823
2824 ht = find_var_ht(name, &varname);
2825 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2826 {
2827 if (find_var(name, NULL, TRUE) != NULL)
2828 *eval_lavars_used = TRUE;
2829 }
2830
2831 name[len] = cc;
2832}
2833
2834/*
2835 * Find variable "name" in the list of variables.
2836 * Return a pointer to it if found, NULL if not found.
2837 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002838 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002839 */
2840 dictitem_T *
2841find_var(char_u *name, hashtab_T **htp, int no_autoload)
2842{
2843 char_u *varname;
2844 hashtab_T *ht;
2845 dictitem_T *ret = NULL;
2846
2847 ht = find_var_ht(name, &varname);
2848 if (htp != NULL)
2849 *htp = ht;
2850 if (ht == NULL)
2851 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002852 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002853 if (ret != NULL)
2854 return ret;
2855
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002856 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002857 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002858 if (ret != NULL)
2859 return ret;
2860
2861 // in Vim9 script items without a scope can be script-local
2862 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2863 {
2864 ht = get_script_local_ht();
2865 if (ht != NULL)
2866 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002867 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002868 if (ret != NULL)
2869 {
2870 if (htp != NULL)
2871 *htp = ht;
2872 return ret;
2873 }
2874 }
2875 }
2876
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002877 // When using "vim9script autoload" script-local items are prefixed but can
2878 // be used with s:name.
2879 if (SCRIPT_ID_VALID(current_sctx.sc_sid)
2880 && name[0] == 's' && name[1] == ':')
2881 {
2882 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2883
2884 if (si->sn_autoload_prefix != NULL)
2885 {
2886 char_u *auto_name = concat_str(si->sn_autoload_prefix, name + 2);
2887
2888 if (auto_name != NULL)
2889 {
2890 ht = &globvarht;
2891 ret = find_var_in_ht(ht, *name, auto_name, TRUE);
Bram Moolenaar130f65d2022-01-13 20:39:41 +00002892 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002893 if (ret != NULL)
2894 {
2895 if (htp != NULL)
2896 *htp = ht;
2897 return ret;
2898 }
2899 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002900 }
2901 }
2902
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002903 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002904}
2905
2906/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00002907 * Like find_var() but if the name starts with <SNR>99_ then look in the
2908 * referenced script (used for a funcref).
2909 */
2910 dictitem_T *
2911find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
2912{
2913 if (STRNCMP(name, "<SNR>", 5) == 0 && isdigit(name[5]))
2914 {
2915 char_u *p = name + 5;
2916 int sid = getdigits(&p);
2917
2918 if (SCRIPT_ID_VALID(sid) && *p == '_')
2919 {
2920 hashtab_T *ht = &SCRIPT_VARS(sid);
2921
2922 if (ht != NULL)
2923 {
2924 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
2925
2926 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00002927 {
2928 if (htp != NULL)
2929 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00002930 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00002931 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00002932 }
2933 }
2934 }
2935
2936 return find_var(name, htp, no_autoload);
2937}
2938
2939/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002940 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002941 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002942 * Returns NULL if not found.
2943 */
2944 dictitem_T *
2945find_var_in_ht(
2946 hashtab_T *ht,
2947 int htname,
2948 char_u *varname,
2949 int no_autoload)
2950{
2951 hashitem_T *hi;
2952
2953 if (*varname == NUL)
2954 {
2955 // Must be something like "s:", otherwise "ht" would be NULL.
2956 switch (htname)
2957 {
2958 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2959 case 'g': return &globvars_var;
2960 case 'v': return &vimvars_var;
2961 case 'b': return &curbuf->b_bufvar;
2962 case 'w': return &curwin->w_winvar;
2963 case 't': return &curtab->tp_winvar;
2964 case 'l': return get_funccal_local_var();
2965 case 'a': return get_funccal_args_var();
2966 }
2967 return NULL;
2968 }
2969
2970 hi = hash_find(ht, varname);
2971 if (HASHITEM_EMPTY(hi))
2972 {
2973 // For global variables we may try auto-loading the script. If it
2974 // worked find the variable again. Don't auto-load a script if it was
2975 // loaded already, otherwise it would be loaded every time when
2976 // checking if a function name is a Funcref variable.
2977 if (ht == &globvarht && !no_autoload)
2978 {
2979 // Note: script_autoload() may make "hi" invalid. It must either
2980 // be obtained again or not used.
2981 if (!script_autoload(varname, FALSE) || aborting())
2982 return NULL;
2983 hi = hash_find(ht, varname);
2984 }
2985 if (HASHITEM_EMPTY(hi))
2986 return NULL;
2987 }
2988 return HI2DI(hi);
2989}
2990
2991/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 * Get the script-local hashtab. NULL if not in a script context.
2993 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002994 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995get_script_local_ht(void)
2996{
2997 scid_T sid = current_sctx.sc_sid;
2998
Bram Moolenaare3d46852020-08-29 13:39:17 +02002999 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 return &SCRIPT_VARS(sid);
3001 return NULL;
3002}
3003
3004/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003005 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003006 * When "cmd" is TRUE it must look like a command, a function must be followed
3007 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003008 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003010 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003011lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003012 char_u *name,
3013 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003014 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003015 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016{
3017 hashtab_T *ht = get_script_local_ht();
3018 char_u buffer[30];
3019 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003020 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003022 int is_global = FALSE;
3023 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024
3025 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003026 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 if (len < sizeof(buffer) - 1)
3028 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003029 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 vim_strncpy(buffer, name, len);
3031 p = buffer;
3032 }
3033 else
3034 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003035 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003037 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 }
3039
3040 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003041 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042
3043 // if not script-local, then perhaps imported
Bram Moolenaardc4451d2022-01-09 21:36:37 +00003044 if (res == FAIL && find_imported(p, 0, FALSE, NULL) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003045 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 if (p != buffer)
3047 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003048
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003049 // Find a function, so that a following "->" works.
3050 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3051 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003052 if (res != OK)
3053 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003054 p = skipwhite(name + len);
3055
3056 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003057 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003058 // Do not check for an internal function, since it might also be a
3059 // valid command, such as ":split" versus "split()".
3060 // Skip "g:" before a function name.
3061 if (name[0] == 'g' && name[1] == ':')
3062 {
3063 is_global = TRUE;
3064 fname = name + 2;
3065 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003066 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003067 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003068 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003069 }
3070
Bram Moolenaar709664c2020-12-12 14:33:41 +01003071 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072}
3073
3074/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003075 * Find the hashtab used for a variable name.
3076 * Return NULL if the name is not valid.
3077 * Set "varname" to the start of name without ':'.
3078 */
3079 hashtab_T *
3080find_var_ht(char_u *name, char_u **varname)
3081{
3082 hashitem_T *hi;
3083 hashtab_T *ht;
3084
3085 if (name[0] == NUL)
3086 return NULL;
3087 if (name[1] != ':')
3088 {
3089 // The name must not start with a colon or #.
3090 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3091 return NULL;
3092 *varname = name;
3093
3094 // "version" is "v:version" in all scopes if scriptversion < 3.
3095 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003096 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003097 {
3098 hi = hash_find(&compat_hashtab, name);
3099 if (!HASHITEM_EMPTY(hi))
3100 return &compat_hashtab;
3101 }
3102
3103 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104 if (ht != NULL)
3105 return ht; // local variable
3106
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003107 // In Vim9 script items at the script level are script-local, except
3108 // for autoload names.
3109 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 {
3111 ht = get_script_local_ht();
3112 if (ht != NULL)
3113 return ht;
3114 }
3115
3116 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003117 }
3118 *varname = name + 2;
3119 if (*name == 'g') // global variable
3120 return &globvarht;
3121 // There must be no ':' or '#' in the rest of the name, unless g: is used
3122 if (vim_strchr(name + 2, ':') != NULL
3123 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3124 return NULL;
3125 if (*name == 'b') // buffer variable
3126 return &curbuf->b_vars->dv_hashtab;
3127 if (*name == 'w') // window variable
3128 return &curwin->w_vars->dv_hashtab;
3129 if (*name == 't') // tab page variable
3130 return &curtab->tp_vars->dv_hashtab;
3131 if (*name == 'v') // v: variable
3132 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003133 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003134 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003136 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 if (*name == 'a') // a: function argument
3138 return get_funccal_args_ht();
3139 if (*name == 'l') // l: local function variable
3140 return get_funccal_local_ht();
3141 }
3142 if (*name == 's') // script variable
3143 {
3144 ht = get_script_local_ht();
3145 if (ht != NULL)
3146 return ht;
3147 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003148 return NULL;
3149}
3150
3151/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003152 * Get the string value of a (global/local) variable.
3153 * Note: see tv_get_string() for how long the pointer remains valid.
3154 * Returns NULL when it doesn't exist.
3155 */
3156 char_u *
3157get_var_value(char_u *name)
3158{
3159 dictitem_T *v;
3160
3161 v = find_var(name, NULL, FALSE);
3162 if (v == NULL)
3163 return NULL;
3164 return tv_get_string(&v->di_tv);
3165}
3166
3167/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003168 * Allocate a new hashtab for a sourced script. It will be used while
3169 * sourcing this script and when executing functions defined in the script.
3170 */
3171 void
3172new_script_vars(scid_T id)
3173{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003174 scriptvar_T *sv;
3175
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003176 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3177 if (sv == NULL)
3178 return;
3179 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003180 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003181}
3182
3183/*
3184 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3185 * point to it.
3186 */
3187 void
3188init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3189{
3190 hash_init(&dict->dv_hashtab);
3191 dict->dv_lock = 0;
3192 dict->dv_scope = scope;
3193 dict->dv_refcount = DO_NOT_FREE_CNT;
3194 dict->dv_copyID = 0;
3195 dict_var->di_tv.vval.v_dict = dict;
3196 dict_var->di_tv.v_type = VAR_DICT;
3197 dict_var->di_tv.v_lock = VAR_FIXED;
3198 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3199 dict_var->di_key[0] = NUL;
3200}
3201
3202/*
3203 * Unreference a dictionary initialized by init_var_dict().
3204 */
3205 void
3206unref_var_dict(dict_T *dict)
3207{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003208 // Now the dict needs to be freed if no one else is using it, go back to
3209 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003210 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3211 dict_unref(dict);
3212}
3213
3214/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003215 * Clean up a list of internal variables.
3216 * Frees all allocated variables and the value they contain.
3217 * Clears hashtab "ht", does not free it.
3218 */
3219 void
3220vars_clear(hashtab_T *ht)
3221{
3222 vars_clear_ext(ht, TRUE);
3223}
3224
3225/*
3226 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3227 */
3228 void
3229vars_clear_ext(hashtab_T *ht, int free_val)
3230{
3231 int todo;
3232 hashitem_T *hi;
3233 dictitem_T *v;
3234
3235 hash_lock(ht);
3236 todo = (int)ht->ht_used;
3237 for (hi = ht->ht_array; todo > 0; ++hi)
3238 {
3239 if (!HASHITEM_EMPTY(hi))
3240 {
3241 --todo;
3242
3243 // Free the variable. Don't remove it from the hashtab,
3244 // ht_array might change then. hash_clear() takes care of it
3245 // later.
3246 v = HI2DI(hi);
3247 if (free_val)
3248 clear_tv(&v->di_tv);
3249 if (v->di_flags & DI_FLAGS_ALLOC)
3250 vim_free(v);
3251 }
3252 }
3253 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003254 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003255}
3256
3257/*
3258 * Delete a variable from hashtab "ht" at item "hi".
3259 * Clear the variable value and free the dictitem.
3260 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003261 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003262delete_var(hashtab_T *ht, hashitem_T *hi)
3263{
3264 dictitem_T *di = HI2DI(hi);
3265
3266 hash_remove(ht, hi);
3267 clear_tv(&di->di_tv);
3268 vim_free(di);
3269}
3270
3271/*
3272 * List the value of one internal variable.
3273 */
3274 static void
3275list_one_var(dictitem_T *v, char *prefix, int *first)
3276{
3277 char_u *tofree;
3278 char_u *s;
3279 char_u numbuf[NUMBUFLEN];
3280
3281 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3282 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3283 s == NULL ? (char_u *)"" : s, first);
3284 vim_free(tofree);
3285}
3286
3287 static void
3288list_one_var_a(
3289 char *prefix,
3290 char_u *name,
3291 int type,
3292 char_u *string,
3293 int *first) // when TRUE clear rest of screen and set to FALSE
3294{
3295 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3296 msg_start();
3297 msg_puts(prefix);
3298 if (name != NULL) // "a:" vars don't have a name stored
3299 msg_puts((char *)name);
3300 msg_putchar(' ');
3301 msg_advance(22);
3302 if (type == VAR_NUMBER)
3303 msg_putchar('#');
3304 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3305 msg_putchar('*');
3306 else if (type == VAR_LIST)
3307 {
3308 msg_putchar('[');
3309 if (*string == '[')
3310 ++string;
3311 }
3312 else if (type == VAR_DICT)
3313 {
3314 msg_putchar('{');
3315 if (*string == '{')
3316 ++string;
3317 }
3318 else
3319 msg_putchar(' ');
3320
3321 msg_outtrans(string);
3322
3323 if (type == VAR_FUNC || type == VAR_PARTIAL)
3324 msg_puts("()");
3325 if (*first)
3326 {
3327 msg_clr_eos();
3328 *first = FALSE;
3329 }
3330}
3331
3332/*
3333 * Set variable "name" to value in "tv".
3334 * If the variable already exists, the value is updated.
3335 * Otherwise the variable is created.
3336 */
3337 void
3338set_var(
3339 char_u *name,
3340 typval_T *tv,
3341 int copy) // make copy of value in "tv"
3342{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003343 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003344}
3345
3346/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003347 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003348 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003349 * If the variable already exists and "is_const" is FALSE the value is updated.
3350 * Otherwise the variable is created.
3351 */
3352 void
3353set_var_const(
3354 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003355 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003356 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003357 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003358 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003359 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003360 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003361{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003362 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003363 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003364 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365 dictitem_T *di;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003366 typval_T *dest_tv = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003367 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003368 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003369 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003371 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003372 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003373 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003374 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003375 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003376
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003377 if (sid != 0)
3378 {
3379 if (SCRIPT_ID_VALID(sid))
3380 ht = &SCRIPT_VARS(sid);
3381 varname = name;
3382 }
3383 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003384 {
3385 if (in_vim9script() && SCRIPT_ID_VALID(current_sctx.sc_sid)
3386 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_autoload_prefix != NULL
3387 && is_export)
3388 {
3389 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
3390 size_t len = STRLEN(name) + STRLEN(si->sn_autoload_prefix) + 1;
3391
3392 // In a vim9 autoload script an exported variable is put in the
3393 // global namespace with the autoload prefix.
3394 var_in_autoload = TRUE;
3395 varname = alloc(len);
3396 if (varname == NULL)
3397 goto failed;
3398 name_tofree = varname;
3399 vim_snprintf((char *)varname, len, "%s%s",
3400 si->sn_autoload_prefix, name);
3401 ht = &globvarht;
3402 }
3403 else
3404 ht = find_var_ht(name, &varname);
3405 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003406 if (ht == NULL || *varname == NUL)
3407 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003408 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003409 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003410 }
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003411 is_script_local = ht == get_script_local_ht() || sid != 0 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003412
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003413 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003414 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003415 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003416 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003417 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003418 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003419 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003420 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003421 }
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003422 if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
3423 && vim_strchr((char_u *)"gwbt", name[0]) != NULL)
3424 // Do not make g:var, w:var, b:var or t:var final.
3425 flags &= ~ASSIGN_FINAL;
3426
Bram Moolenaare535db82021-03-31 21:07:24 +02003427 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003428 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3429 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003430 // For "[a, _] = list" the underscore is ignored.
3431 if ((flags & ASSIGN_UNPACK) == 0)
3432 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003433 goto failed;
3434 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003435
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003437
Bram Moolenaar24e93162021-07-18 20:40:33 +02003438 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003439 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +00003440 imported_T *import = find_imported(varname, 0, FALSE, NULL);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003441
Bram Moolenaar24e93162021-07-18 20:40:33 +02003442 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003443 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003444 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02003445 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003447 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003448 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003450 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3451 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003452 }
3453 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454
Bram Moolenaar24e93162021-07-18 20:40:33 +02003455 if (dest_tv == NULL)
3456 {
3457 // Search in parent scope which is possible to reference from lambda
3458 if (di == NULL)
3459 di = find_var_in_scoped_ht(name, TRUE);
3460
3461 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003462 && var_wrong_func_name(name, di == NULL))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003463 goto failed;
3464
3465 if (need_convert_to_bool(type, tv))
3466 {
Bram Moolenaarf6488542021-07-18 21:24:50 +02003467 // Destination is a bool and the value is not, but it can be
3468 // converted.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003469 CLEAR_FIELD(bool_tv);
3470 bool_tv.v_type = VAR_BOOL;
3471 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3472 tv = &bool_tv;
3473 }
3474
3475 if (di != NULL)
3476 {
3477 // Item already exists. Allowed to replace when reloading.
3478 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
3479 {
3480 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaarf6488542021-07-18 21:24:50 +02003481 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003482 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003483 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar24e93162021-07-18 20:40:33 +02003484 goto failed;
3485 }
3486
3487 if (is_script_local && vim9script
Bram Moolenaarf6488542021-07-18 21:24:50 +02003488 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003489 {
3490 semsg(_(e_redefining_script_item_str), name);
3491 goto failed;
3492 }
3493
Bram Moolenaara06758d2021-10-15 00:18:37 +01003494 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003495 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003496 where_T where = WHERE_INIT;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003497 svar_T *sv = find_typval_in_script(&di->di_tv, sid);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003498
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003499 if (sv != NULL)
3500 {
3501 // check the type and adjust to bool if needed
3502 where.wt_index = var_idx;
3503 where.wt_variable = TRUE;
3504 if (check_script_var_type(sv, tv, name, where) == FAIL)
3505 goto failed;
3506 if (type == NULL)
3507 type = sv->sv_type;
3508 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003509 }
3510
Bram Moolenaara06758d2021-10-15 00:18:37 +01003511 if ((flags & ASSIGN_FOR_LOOP) == 0
3512 && var_check_permission(di, name) == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003513 goto failed;
3514 }
3515 else
3516 {
3517 // can only redefine once
3518 di->di_flags &= ~DI_FLAGS_RELOAD;
3519
Bram Moolenaarf6488542021-07-18 21:24:50 +02003520 // A Vim9 script-local variable is also present in sn_all_vars
3521 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003522 if (var_in_vim9script || var_in_autoload)
3523 update_vim9_script_var(FALSE, di,
3524 var_in_autoload ? name : di->di_key, flags,
3525 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003526 }
3527
3528 // existing variable, need to clear the value
3529
3530 // Handle setting internal di: variables separately where needed to
3531 // prevent changing the type.
3532 if (ht == &vimvarht)
3533 {
3534 if (di->di_tv.v_type == VAR_STRING)
3535 {
3536 VIM_CLEAR(di->di_tv.vval.v_string);
3537 if (copy || tv->v_type != VAR_STRING)
3538 {
3539 char_u *val = tv_get_string(tv);
3540
Bram Moolenaarf6488542021-07-18 21:24:50 +02003541 // Careful: when assigning to v:errmsg and
3542 // tv_get_string() causes an error message the variable
3543 // will already be set.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003544 if (di->di_tv.vval.v_string == NULL)
3545 di->di_tv.vval.v_string = vim_strsave(val);
3546 }
3547 else
3548 {
3549 // Take over the string to avoid an extra alloc/free.
3550 di->di_tv.vval.v_string = tv->vval.v_string;
3551 tv->vval.v_string = NULL;
3552 }
3553 goto failed;
3554 }
3555 else if (di->di_tv.v_type == VAR_NUMBER)
3556 {
3557 di->di_tv.vval.v_number = tv_get_number(tv);
3558 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003559 set_search_direction(di->di_tv.vval.v_number
3560 ? '/' : '?');
Bram Moolenaar24e93162021-07-18 20:40:33 +02003561#ifdef FEAT_SEARCH_EXTRA
3562 else if (STRCMP(varname, "hlsearch") == 0)
3563 {
3564 no_hlsearch = !di->di_tv.vval.v_number;
3565 redraw_all_later(SOME_VALID);
3566 }
3567#endif
3568 goto failed;
3569 }
3570 else if (di->di_tv.v_type != tv->v_type)
3571 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003572 semsg(_(e_setting_str_to_value_with_wrong_type), name);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003573 goto failed;
3574 }
3575 }
3576
3577 clear_tv(&di->di_tv);
3578 }
3579 else
3580 {
3581 // Item not found, check if a function already exists.
3582 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaarf6488542021-07-18 21:24:50 +02003583 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003584 {
3585 semsg(_(e_redefining_script_item_str), name);
3586 goto failed;
3587 }
3588
Bram Moolenaar24e93162021-07-18 20:40:33 +02003589 // add a new variable
3590 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3591 {
3592 semsg(_(e_unknown_variable_str), name);
3593 goto failed;
3594 }
3595
3596 // Can't add "v:" or "a:" variable.
3597 if (ht == &vimvarht || ht == get_funccal_args_ht())
3598 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003599 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003600 goto failed;
3601 }
3602
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003603 // Make sure the variable name is valid. In Vim9 script an
3604 // autoload variable must be prefixed with "g:" unless in an
3605 // autoload script.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003606 if (!valid_varname(varname, -1, !vim9script
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003607 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003608 goto failed;
3609
3610 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3611 if (di == NULL)
3612 goto failed;
3613 STRCPY(di->di_key, varname);
3614 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
3615 {
3616 vim_free(di);
3617 goto failed;
3618 }
3619 di->di_flags = DI_FLAGS_ALLOC;
3620 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3621 di->di_flags |= DI_FLAGS_LOCK;
3622
3623 // A Vim9 script-local variable is also added to sn_all_vars and
3624 // sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003625 if (var_in_vim9script || var_in_autoload)
3626 update_vim9_script_var(TRUE, di,
3627 var_in_autoload ? name : di->di_key, flags,
3628 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003629 }
3630
Bram Moolenaar24e93162021-07-18 20:40:33 +02003631 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003632 }
3633
3634 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003635 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003636 else
3637 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003638 *dest_tv = *tv;
3639 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003640 init_tv(tv);
3641 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003642 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003643
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003644 if (vim9script && type != NULL)
3645 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003646 if (type->tt_type == VAR_DICT && dest_tv->vval.v_dict != NULL)
Bram Moolenaar464393a2021-09-11 23:07:44 +02003647 {
3648 if (dest_tv->vval.v_dict->dv_type != type)
3649 {
3650 free_type(dest_tv->vval.v_dict->dv_type);
3651 dest_tv->vval.v_dict->dv_type = alloc_type(type);
3652 }
3653 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003654 else if (type->tt_type == VAR_LIST && dest_tv->vval.v_list != NULL)
Bram Moolenaar464393a2021-09-11 23:07:44 +02003655 {
3656 if (dest_tv->vval.v_list->lv_type != type)
3657 {
3658 free_type(dest_tv->vval.v_list->lv_type);
3659 dest_tv->vval.v_list->lv_type = alloc_type(type);
3660 }
3661 }
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003662 }
3663
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003664 // ":const var = value" locks the value
3665 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003666 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003667 // Like :lockvar! name: lock the value and what it contains, but only
3668 // if the reference count is up to one. That locks only literal
3669 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003670 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003671
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003672failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003673 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003674 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003675 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003676}
3677
3678/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003679 * Check in this order for backwards compatibility:
3680 * - Whether the variable is read-only
3681 * - Whether the variable value is locked
3682 * - Whether the variable is locked
3683 */
3684 int
3685var_check_permission(dictitem_T *di, char_u *name)
3686{
3687 if (var_check_ro(di->di_flags, name, FALSE)
3688 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3689 || var_check_lock(di->di_flags, name, FALSE))
3690 return FAIL;
3691 return OK;
3692}
3693
3694/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003695 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3696 * Also give an error message.
3697 */
3698 int
3699var_check_ro(int flags, char_u *name, int use_gettext)
3700{
3701 if (flags & DI_FLAGS_RO)
3702 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003703 if (name == NULL)
3704 emsg(_(e_cannot_change_readonly_variable));
3705 else
3706 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02003707 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003708 return TRUE;
3709 }
3710 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3711 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003712 if (name == NULL)
3713 emsg(_(e_cannot_set_variable_in_sandbox));
3714 else
3715 semsg(_(e_cannot_set_variable_in_sandbox_str),
3716 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003717 return TRUE;
3718 }
3719 return FALSE;
3720}
3721
3722/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003723 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3724 * Also give an error message.
3725 */
3726 int
3727var_check_lock(int flags, char_u *name, int use_gettext)
3728{
3729 if (flags & DI_FLAGS_LOCK)
3730 {
3731 semsg(_(e_variable_is_locked_str),
3732 use_gettext ? (char_u *)_(name) : name);
3733 return TRUE;
3734 }
3735 return FALSE;
3736}
3737
3738/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003739 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3740 * Also give an error message.
3741 */
3742 int
3743var_check_fixed(int flags, char_u *name, int use_gettext)
3744{
3745 if (flags & DI_FLAGS_FIX)
3746 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003747 if (name == NULL)
3748 emsg(_(e_cannot_delete_variable));
3749 else
3750 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003751 use_gettext ? (char_u *)_(name) : name);
3752 return TRUE;
3753 }
3754 return FALSE;
3755}
3756
3757/*
3758 * Check if a funcref is assigned to a valid variable name.
3759 * Return TRUE and give an error if not.
3760 */
3761 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003762var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003763 char_u *name, // points to start of variable name
3764 int new_var) // TRUE when creating the variable
3765{
Bram Moolenaar32154662021-03-28 21:14:06 +02003766 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
3767 // the name can be used without the s: prefix.
3768 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
3769 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003770 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3771 ? name[2] : name[0]))
3772 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003773 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003774 return TRUE;
3775 }
3776 // Don't allow hiding a function. When "v" is not NULL we might be
3777 // assigning another function to the same var, the type is checked
3778 // below.
3779 if (new_var && function_exists(name, FALSE))
3780 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003781 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003782 name);
3783 return TRUE;
3784 }
3785 return FALSE;
3786}
3787
3788/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003789 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3790 * value. Also give an error message, using "name" or _("name") when
3791 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003792 */
3793 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003794value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003795{
3796 if (lock & VAR_LOCKED)
3797 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003798 if (name == NULL)
3799 emsg(_(e_value_is_locked));
3800 else
3801 semsg(_(e_value_is_locked_str),
3802 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003803 return TRUE;
3804 }
3805 if (lock & VAR_FIXED)
3806 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003807 if (name == NULL)
3808 emsg(_(e_cannot_change_value));
3809 else
3810 semsg(_(e_cannot_change_value_of_str),
3811 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003812 return TRUE;
3813 }
3814 return FALSE;
3815}
3816
3817/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003818 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003819 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003820 * Return FALSE and give an error if not.
3821 */
3822 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003823valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003824{
3825 char_u *p;
3826
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003827 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003828 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003829 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003830 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003831 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003832 return FALSE;
3833 }
3834 return TRUE;
3835}
3836
3837/*
3838 * getwinvar() and gettabwinvar()
3839 */
3840 static void
3841getwinvar(
3842 typval_T *argvars,
3843 typval_T *rettv,
3844 int off) // 1 for gettabwinvar()
3845{
3846 win_T *win;
3847 char_u *varname;
3848 dictitem_T *v;
3849 tabpage_T *tp = NULL;
3850 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00003851 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003852 int need_switch_win;
3853
3854 if (off == 1)
3855 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3856 else
3857 tp = curtab;
3858 win = find_win_by_nr(&argvars[off], tp);
3859 varname = tv_get_string_chk(&argvars[off + 1]);
3860 ++emsg_off;
3861
3862 rettv->v_type = VAR_STRING;
3863 rettv->vval.v_string = NULL;
3864
3865 if (win != NULL && varname != NULL)
3866 {
3867 // Set curwin to be our win, temporarily. Also set the tabpage,
3868 // otherwise the window is not valid. Only do this when needed,
3869 // autocommands get blocked.
3870 need_switch_win = !(tp == curtab && win == curwin);
3871 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00003872 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003873 {
3874 if (*varname == '&')
3875 {
3876 if (varname[1] == NUL)
3877 {
3878 // get all window-local options in a dict
3879 dict_T *opts = get_winbuf_options(FALSE);
3880
3881 if (opts != NULL)
3882 {
3883 rettv_dict_set(rettv, opts);
3884 done = TRUE;
3885 }
3886 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003887 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003888 // window-local-option
3889 done = TRUE;
3890 }
3891 else
3892 {
3893 // Look up the variable.
3894 // Let getwinvar({nr}, "") return the "w:" dictionary.
3895 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3896 varname, FALSE);
3897 if (v != NULL)
3898 {
3899 copy_tv(&v->di_tv, rettv);
3900 done = TRUE;
3901 }
3902 }
3903 }
3904
3905 if (need_switch_win)
3906 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00003907 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003908 }
3909
3910 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3911 // use the default return value
3912 copy_tv(&argvars[off + 2], rettv);
3913
3914 --emsg_off;
3915}
3916
3917/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003918 * Set option "varname" to the value of "varp" for the current buffer/window.
3919 */
3920 static void
3921set_option_from_tv(char_u *varname, typval_T *varp)
3922{
3923 long numval = 0;
3924 char_u *strval;
3925 char_u nbuf[NUMBUFLEN];
3926 int error = FALSE;
3927
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003928 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003929 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003930 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003931 strval = (char_u *)"0"; // avoid using "false"
3932 }
3933 else
3934 {
3935 if (!in_vim9script() || varp->v_type != VAR_STRING)
3936 numval = (long)tv_get_number_chk(varp, &error);
3937 strval = tv_get_string_buf_chk(varp, nbuf);
3938 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02003939 if (!error && strval != NULL)
3940 set_option_value(varname, numval, strval, OPT_LOCAL);
3941}
3942
3943/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003944 * "setwinvar()" and "settabwinvar()" functions
3945 */
3946 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003947setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003948{
3949 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00003950 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003951 int need_switch_win;
3952 char_u *varname, *winvarname;
3953 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003954 tabpage_T *tp = NULL;
3955
3956 if (check_secure())
3957 return;
3958
3959 if (off == 1)
3960 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3961 else
3962 tp = curtab;
3963 win = find_win_by_nr(&argvars[off], tp);
3964 varname = tv_get_string_chk(&argvars[off + 1]);
3965 varp = &argvars[off + 2];
3966
3967 if (win != NULL && varname != NULL && varp != NULL)
3968 {
3969 need_switch_win = !(tp == curtab && win == curwin);
3970 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00003971 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003972 {
3973 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003974 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003975 else
3976 {
3977 winvarname = alloc(STRLEN(varname) + 3);
3978 if (winvarname != NULL)
3979 {
3980 STRCPY(winvarname, "w:");
3981 STRCPY(winvarname + 2, varname);
3982 set_var(winvarname, varp, TRUE);
3983 vim_free(winvarname);
3984 }
3985 }
3986 }
3987 if (need_switch_win)
Bram Moolenaar18f47402022-01-06 13:24:51 +00003988 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003989 }
3990}
3991
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003992/*
3993 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3994 * v:option_type, and v:option_command.
3995 */
3996 void
3997reset_v_option_vars(void)
3998{
3999 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4000 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4001 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4002 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4003 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4004 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4005}
4006
4007/*
4008 * Add an assert error to v:errors.
4009 */
4010 void
4011assert_error(garray_T *gap)
4012{
4013 struct vimvar *vp = &vimvars[VV_ERRORS];
4014
Bram Moolenaard787e402021-12-24 21:36:12 +00004015 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004016 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004017 set_vim_var_list(VV_ERRORS, list_alloc());
4018 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4019}
4020
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004021 int
4022var_exists(char_u *var)
4023{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004024 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004025 char_u *name;
4026 char_u *tofree;
4027 typval_T tv;
4028 int len = 0;
4029 int n = FALSE;
4030
4031 // get_name_len() takes care of expanding curly braces
4032 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004033 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004034 if (len > 0)
4035 {
4036 if (tofree != NULL)
4037 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004038 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004039 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004040 if (n)
4041 {
4042 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004043 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004044 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4045 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004046 if (n)
4047 clear_tv(&tv);
4048 }
4049 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004050 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004051 n = FALSE;
4052
4053 vim_free(tofree);
4054 return n;
4055}
4056
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004057static lval_T *redir_lval = NULL;
4058#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4059static garray_T redir_ga; // only valid when redir_lval is not NULL
4060static char_u *redir_endp = NULL;
4061static char_u *redir_varname = NULL;
4062
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004063 int
4064alloc_redir_lval(void)
4065{
4066 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4067 if (redir_lval == NULL)
4068 return FAIL;
4069 return OK;
4070}
4071
4072 void
4073clear_redir_lval(void)
4074{
4075 VIM_CLEAR(redir_lval);
4076}
4077
4078 void
4079init_redir_ga(void)
4080{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004081 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004082}
4083
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004084/*
4085 * Start recording command output to a variable
4086 * When "append" is TRUE append to an existing variable.
4087 * Returns OK if successfully completed the setup. FAIL otherwise.
4088 */
4089 int
4090var_redir_start(char_u *name, int append)
4091{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004092 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004093 typval_T tv;
4094
4095 // Catch a bad name early.
4096 if (!eval_isnamec1(*name))
4097 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004098 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004099 return FAIL;
4100 }
4101
4102 // Make a copy of the name, it is used in redir_lval until redir ends.
4103 redir_varname = vim_strsave(name);
4104 if (redir_varname == NULL)
4105 return FAIL;
4106
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004107 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004108 {
4109 var_redir_stop();
4110 return FAIL;
4111 }
4112
4113 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004114 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004115
4116 // Parse the variable name (can be a dict or list entry).
4117 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4118 FNE_CHECK_START);
4119 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4120 {
4121 clear_lval(redir_lval);
4122 if (redir_endp != NULL && *redir_endp != NUL)
4123 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004124 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004125 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004126 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004127 redir_endp = NULL; // don't store a value, only cleanup
4128 var_redir_stop();
4129 return FAIL;
4130 }
4131
4132 // check if we can write to the variable: set it to or append an empty
4133 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004134 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004135 tv.v_type = VAR_STRING;
4136 tv.vval.v_string = (char_u *)"";
4137 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004138 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004139 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004140 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004141 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004142 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004143 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004144 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004145 {
4146 redir_endp = NULL; // don't store a value, only cleanup
4147 var_redir_stop();
4148 return FAIL;
4149 }
4150
4151 return OK;
4152}
4153
4154/*
4155 * Append "value[value_len]" to the variable set by var_redir_start().
4156 * The actual appending is postponed until redirection ends, because the value
4157 * appended may in fact be the string we write to, changing it may cause freed
4158 * memory to be used:
4159 * :redir => foo
4160 * :let foo
4161 * :redir END
4162 */
4163 void
4164var_redir_str(char_u *value, int value_len)
4165{
4166 int len;
4167
4168 if (redir_lval == NULL)
4169 return;
4170
4171 if (value_len == -1)
4172 len = (int)STRLEN(value); // Append the entire string
4173 else
4174 len = value_len; // Append only "value_len" characters
4175
4176 if (ga_grow(&redir_ga, len) == OK)
4177 {
4178 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4179 redir_ga.ga_len += len;
4180 }
4181 else
4182 var_redir_stop();
4183}
4184
4185/*
4186 * Stop redirecting command output to a variable.
4187 * Frees the allocated memory.
4188 */
4189 void
4190var_redir_stop(void)
4191{
4192 typval_T tv;
4193
4194 if (EVALCMD_BUSY)
4195 {
4196 redir_lval = NULL;
4197 return;
4198 }
4199
4200 if (redir_lval != NULL)
4201 {
4202 // If there was no error: assign the text to the variable.
4203 if (redir_endp != NULL)
4204 {
4205 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4206 tv.v_type = VAR_STRING;
4207 tv.vval.v_string = redir_ga.ga_data;
4208 // Call get_lval() again, if it's inside a Dict or List it may
4209 // have changed.
4210 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4211 FALSE, FALSE, 0, FNE_CHECK_START);
4212 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004214 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004215 clear_lval(redir_lval);
4216 }
4217
4218 // free the collected output
4219 VIM_CLEAR(redir_ga.ga_data);
4220
4221 VIM_CLEAR(redir_lval);
4222 }
4223 VIM_CLEAR(redir_varname);
4224}
4225
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004226/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004227 * Get the collected redirected text and clear redir_ga.
4228 */
4229 char_u *
4230get_clear_redir_ga(void)
4231{
4232 char_u *res;
4233
4234 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4235 res = redir_ga.ga_data;
4236 redir_ga.ga_data = NULL;
4237 return res;
4238}
4239
4240/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004241 * "gettabvar()" function
4242 */
4243 void
4244f_gettabvar(typval_T *argvars, typval_T *rettv)
4245{
Bram Moolenaar18f47402022-01-06 13:24:51 +00004246 switchwin_T switchwin;
4247 tabpage_T *tp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004248 dictitem_T *v;
4249 char_u *varname;
4250 int done = FALSE;
4251
4252 rettv->v_type = VAR_STRING;
4253 rettv->vval.v_string = NULL;
4254
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004255 if (in_vim9script()
4256 && (check_for_number_arg(argvars, 0) == FAIL
4257 || check_for_string_arg(argvars, 1) == FAIL))
4258 return;
4259
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004260 varname = tv_get_string_chk(&argvars[1]);
4261 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4262 if (tp != NULL && varname != NULL)
4263 {
4264 // Set tp to be our tabpage, temporarily. Also set the window to the
4265 // first window in the tabpage, otherwise the window is not valid.
Bram Moolenaar18f47402022-01-06 13:24:51 +00004266 if (switch_win(&switchwin,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004267 tp == curtab || tp->tp_firstwin == NULL ? firstwin
4268 : tp->tp_firstwin, tp, TRUE) == OK)
4269 {
4270 // look up the variable
4271 // Let gettabvar({nr}, "") return the "t:" dictionary.
4272 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
4273 if (v != NULL)
4274 {
4275 copy_tv(&v->di_tv, rettv);
4276 done = TRUE;
4277 }
4278 }
4279
4280 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004281 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004282 }
4283
4284 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4285 copy_tv(&argvars[2], rettv);
4286}
4287
4288/*
4289 * "gettabwinvar()" function
4290 */
4291 void
4292f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4293{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004294 if (in_vim9script()
4295 && (check_for_number_arg(argvars, 0) == FAIL
4296 || check_for_number_arg(argvars, 1) == FAIL
4297 || check_for_string_arg(argvars, 2) == FAIL))
4298 return;
4299
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004300 getwinvar(argvars, rettv, 1);
4301}
4302
4303/*
4304 * "getwinvar()" function
4305 */
4306 void
4307f_getwinvar(typval_T *argvars, typval_T *rettv)
4308{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004309 if (in_vim9script()
4310 && (check_for_number_arg(argvars, 0) == FAIL
4311 || check_for_string_arg(argvars, 1) == FAIL))
4312 return;
4313
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004314 getwinvar(argvars, rettv, 0);
4315}
4316
4317/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004318 * "getbufvar()" function
4319 */
4320 void
4321f_getbufvar(typval_T *argvars, typval_T *rettv)
4322{
4323 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004324 char_u *varname;
4325 dictitem_T *v;
4326 int done = FALSE;
4327
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004328 if (in_vim9script()
4329 && (check_for_buffer_arg(argvars, 0) == FAIL
4330 || check_for_string_arg(argvars, 1) == FAIL))
4331 return;
4332
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004333 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004334 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004335
4336 rettv->v_type = VAR_STRING;
4337 rettv->vval.v_string = NULL;
4338
4339 if (buf != NULL && varname != NULL)
4340 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004341 if (*varname == '&')
4342 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004343 buf_T *save_curbuf = curbuf;
4344
4345 // set curbuf to be our buf, temporarily
4346 curbuf = buf;
4347
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004348 if (varname[1] == NUL)
4349 {
4350 // get all buffer-local options in a dict
4351 dict_T *opts = get_winbuf_options(TRUE);
4352
4353 if (opts != NULL)
4354 {
4355 rettv_dict_set(rettv, opts);
4356 done = TRUE;
4357 }
4358 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004359 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004360 // buffer-local-option
4361 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02004362
4363 // restore previous notion of curbuf
4364 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004365 }
4366 else
4367 {
4368 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02004369 if (*varname == NUL)
4370 // Let getbufvar({nr}, "") return the "b:" dictionary.
4371 v = &buf->b_bufvar;
4372 else
4373 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
4374 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004375 if (v != NULL)
4376 {
4377 copy_tv(&v->di_tv, rettv);
4378 done = TRUE;
4379 }
4380 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004381 }
4382
4383 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4384 // use the default value
4385 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004386}
4387
4388/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004389 * "settabvar()" function
4390 */
4391 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004392f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004393{
4394 tabpage_T *save_curtab;
4395 tabpage_T *tp;
4396 char_u *varname, *tabvarname;
4397 typval_T *varp;
4398
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004399 if (check_secure())
4400 return;
4401
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004402 if (in_vim9script()
4403 && (check_for_number_arg(argvars, 0) == FAIL
4404 || check_for_string_arg(argvars, 1) == FAIL))
4405 return;
4406
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004407 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4408 varname = tv_get_string_chk(&argvars[1]);
4409 varp = &argvars[2];
4410
4411 if (varname != NULL && varp != NULL && tp != NULL)
4412 {
4413 save_curtab = curtab;
4414 goto_tabpage_tp(tp, FALSE, FALSE);
4415
4416 tabvarname = alloc(STRLEN(varname) + 3);
4417 if (tabvarname != NULL)
4418 {
4419 STRCPY(tabvarname, "t:");
4420 STRCPY(tabvarname + 2, varname);
4421 set_var(tabvarname, varp, TRUE);
4422 vim_free(tabvarname);
4423 }
4424
4425 // Restore current tabpage
4426 if (valid_tabpage(save_curtab))
4427 goto_tabpage_tp(save_curtab, FALSE, FALSE);
4428 }
4429}
4430
4431/*
4432 * "settabwinvar()" function
4433 */
4434 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004435f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004436{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004437 if (in_vim9script()
4438 && (check_for_number_arg(argvars, 0) == FAIL
4439 || check_for_number_arg(argvars, 1) == FAIL
4440 || check_for_string_arg(argvars, 2) == FAIL))
4441 return;
4442
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004443 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004444}
4445
4446/*
4447 * "setwinvar()" function
4448 */
4449 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004450f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004451{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004452 if (in_vim9script()
4453 && (check_for_number_arg(argvars, 0) == FAIL
4454 || check_for_string_arg(argvars, 1) == FAIL))
4455 return;
4456
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004457 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004458}
4459
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004460/*
4461 * "setbufvar()" function
4462 */
4463 void
4464f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4465{
4466 buf_T *buf;
4467 char_u *varname, *bufvarname;
4468 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004469
4470 if (check_secure())
4471 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004472
4473 if (in_vim9script()
4474 && (check_for_buffer_arg(argvars, 0) == FAIL
4475 || check_for_string_arg(argvars, 1) == FAIL))
4476 return;
4477
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004478 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004479 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004480 varp = &argvars[2];
4481
4482 if (buf != NULL && varname != NULL && varp != NULL)
4483 {
4484 if (*varname == '&')
4485 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004486 aco_save_T aco;
4487
4488 // set curbuf to be our buf, temporarily
4489 aucmd_prepbuf(&aco, buf);
4490
Bram Moolenaar191929b2020-08-19 21:20:49 +02004491 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004492
4493 // reset notion of buffer
4494 aucmd_restbuf(&aco);
4495 }
4496 else
4497 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004498 bufvarname = alloc(STRLEN(varname) + 3);
4499 if (bufvarname != NULL)
4500 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004501 buf_T *save_curbuf = curbuf;
4502
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004503 curbuf = buf;
4504 STRCPY(bufvarname, "b:");
4505 STRCPY(bufvarname + 2, varname);
4506 set_var(bufvarname, varp, TRUE);
4507 vim_free(bufvarname);
4508 curbuf = save_curbuf;
4509 }
4510 }
4511 }
4512}
4513
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004514/*
4515 * Get a callback from "arg". It can be a Funcref or a function name.
4516 * When "arg" is zero return an empty string.
4517 * "cb_name" is not allocated.
4518 * "cb_name" is set to NULL for an invalid argument.
4519 */
4520 callback_T
4521get_callback(typval_T *arg)
4522{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004523 callback_T res;
4524 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004525
4526 res.cb_free_name = FALSE;
4527 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4528 {
4529 res.cb_partial = arg->vval.v_partial;
4530 ++res.cb_partial->pt_refcount;
4531 res.cb_name = partial_name(res.cb_partial);
4532 }
4533 else
4534 {
4535 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004536 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4537 && isdigit(*arg->vval.v_string))
4538 r = FAIL;
4539 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004540 {
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004541 if (arg->v_type == VAR_STRING)
4542 {
4543 char_u *name;
4544
4545 name = get_scriptlocal_funcname(arg->vval.v_string);
4546 if (name != NULL)
4547 {
4548 vim_free(arg->vval.v_string);
4549 arg->vval.v_string = name;
4550 }
4551 }
4552
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004553 res.cb_name = arg->vval.v_string;
4554 func_ref(res.cb_name);
4555 }
4556 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004557 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004558 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004559 r = FAIL;
4560
4561 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004562 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004563 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004564 res.cb_name = NULL;
4565 }
4566 }
4567 return res;
4568}
4569
4570/*
4571 * Copy a callback into a typval_T.
4572 */
4573 void
4574put_callback(callback_T *cb, typval_T *tv)
4575{
4576 if (cb->cb_partial != NULL)
4577 {
4578 tv->v_type = VAR_PARTIAL;
4579 tv->vval.v_partial = cb->cb_partial;
4580 ++tv->vval.v_partial->pt_refcount;
4581 }
4582 else
4583 {
4584 tv->v_type = VAR_FUNC;
4585 tv->vval.v_string = vim_strsave(cb->cb_name);
4586 func_ref(cb->cb_name);
4587 }
4588}
4589
4590/*
4591 * Make a copy of "src" into "dest", allocating the function name if needed,
4592 * without incrementing the refcount.
4593 */
4594 void
4595set_callback(callback_T *dest, callback_T *src)
4596{
4597 if (src->cb_partial == NULL)
4598 {
4599 // just a function name, make a copy
4600 dest->cb_name = vim_strsave(src->cb_name);
4601 dest->cb_free_name = TRUE;
4602 }
4603 else
4604 {
4605 // cb_name is a pointer into cb_partial
4606 dest->cb_name = src->cb_name;
4607 dest->cb_free_name = FALSE;
4608 }
4609 dest->cb_partial = src->cb_partial;
4610}
4611
4612/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004613 * Copy callback from "src" to "dest", incrementing the refcounts.
4614 */
4615 void
4616copy_callback(callback_T *dest, callback_T *src)
4617{
4618 dest->cb_partial = src->cb_partial;
4619 if (dest->cb_partial != NULL)
4620 {
4621 dest->cb_name = src->cb_name;
4622 dest->cb_free_name = FALSE;
4623 ++dest->cb_partial->pt_refcount;
4624 }
4625 else
4626 {
4627 dest->cb_name = vim_strsave(src->cb_name);
4628 dest->cb_free_name = TRUE;
4629 func_ref(src->cb_name);
4630 }
4631}
4632
4633/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004634 * Unref/free "callback" returned by get_callback() or set_callback().
4635 */
4636 void
4637free_callback(callback_T *callback)
4638{
4639 if (callback->cb_partial != NULL)
4640 {
4641 partial_unref(callback->cb_partial);
4642 callback->cb_partial = NULL;
4643 }
4644 else if (callback->cb_name != NULL)
4645 func_unref(callback->cb_name);
4646 if (callback->cb_free_name)
4647 {
4648 vim_free(callback->cb_name);
4649 callback->cb_free_name = FALSE;
4650 }
4651 callback->cb_name = NULL;
4652}
4653
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004654#endif // FEAT_EVAL