blob: 11770aa532ebb2eba82aa5c2d68c3d607118c857 [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 Moolenaarcb4e80f2021-03-13 20:57:19 +01001235 if (eval_variable(name, len, &tv, NULL,
1236 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 Moolenaare40fbc22020-06-27 18:06:45 +02001242 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001243 == 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"
2648 typval_T *rettv, // NULL when only checking existence
2649 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002650 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002651{
2652 int ret = OK;
2653 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002654 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002655 hashtab_T *ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002656 int cc;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002657 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002658
2659 // truncate the name, so that we can use strcmp()
2660 cc = name[len];
2661 name[len] = NUL;
2662
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002663 // Check for local variable when debugging.
2664 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002665 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002666 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002667 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2668
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002669 if (v != NULL)
2670 {
2671 tv = &v->di_tv;
2672 if (dip != NULL)
2673 *dip = v;
2674 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002675 else
2676 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002677 }
2678
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002679 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002681 imported_T *import;
2682 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2683
2684 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685
2686 // imported variable from another script
2687 if (import != NULL)
2688 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002689 if (import->imp_funcname != NULL)
2690 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002691 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002692 if (rettv != NULL)
2693 {
2694 rettv->v_type = VAR_FUNC;
2695 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2696 }
2697 }
Bram Moolenaara6294952020-12-27 13:39:50 +01002698 else if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002699 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002700 if ((flags & EVAL_VAR_IMPORT) == 0)
2701 {
2702 if (flags & EVAL_VAR_VERBOSE)
2703 emsg(_(e_import_as_name_not_supported_here));
2704 ret = FAIL;
2705 }
2706 else
2707 {
2708 if (rettv != NULL)
2709 {
2710 rettv->v_type = VAR_ANY;
2711 rettv->vval.v_number = import->imp_sid;
2712 }
2713 found = TRUE;
2714 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002715 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002716 else
2717 {
2718 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002719 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002721 tv = sv->sv_tv;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002722 type = sv->sv_type;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002723 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00002725 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002726 {
2727 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2728
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002729 // In Vim9 script we can get a function reference by using the
2730 // function name.
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002731 if (ufunc != NULL)
2732 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002733 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002734 if (rettv != NULL)
2735 {
2736 rettv->v_type = VAR_FUNC;
Bram Moolenaaref082e12021-12-12 21:02:03 +00002737 if (STRNCMP(name, "g:", 2) == 0)
2738 // Keep the "g:", otherwise script-local may be
2739 // assumed.
2740 rettv->vval.v_string = vim_strsave(name);
2741 else
2742 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002743 if (rettv->vval.v_string != NULL)
2744 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002745 }
2746 }
2747 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748 }
2749
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002750 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002751 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002752 if (tv == NULL)
2753 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002754 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002755 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002756 ret = FAIL;
2757 }
2758 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002759 {
Bram Moolenaar11005b02021-07-11 20:59:00 +02002760 if (ht != NULL && ht == get_script_local_ht()
2761 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002762 {
Bram Moolenaarc967d572021-07-08 21:38:50 +02002763 svar_T *sv = find_typval_in_script(tv);
Bram Moolenaarf055d452021-07-08 20:57:24 +02002764
Bram Moolenaarf055d452021-07-08 20:57:24 +02002765 if (sv != NULL)
2766 type = sv->sv_type;
2767 }
2768
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002769 // If a list or dict variable wasn't initialized, do it now.
2770 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2771 {
2772 tv->vval.v_dict = dict_alloc();
2773 if (tv->vval.v_dict != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002774 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002775 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002776 tv->vval.v_dict->dv_type = alloc_type(type);
2777 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002778 }
2779 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2780 {
2781 tv->vval.v_list = list_alloc();
2782 if (tv->vval.v_list != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002783 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002784 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002785 tv->vval.v_list->lv_type = alloc_type(type);
2786 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002787 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002788 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL)
2789 {
2790 tv->vval.v_blob = blob_alloc();
2791 if (tv->vval.v_blob != NULL)
2792 ++tv->vval.v_blob->bv_refcount;
2793 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002794 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002795 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002796 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002797
2798 name[len] = cc;
2799
2800 return ret;
2801}
2802
2803/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002804 * Check if variable "name[len]" is a local variable or an argument.
2805 * If so, "*eval_lavars_used" is set to TRUE.
2806 */
2807 void
2808check_vars(char_u *name, int len)
2809{
2810 int cc;
2811 char_u *varname;
2812 hashtab_T *ht;
2813
2814 if (eval_lavars_used == NULL)
2815 return;
2816
2817 // truncate the name, so that we can use strcmp()
2818 cc = name[len];
2819 name[len] = NUL;
2820
2821 ht = find_var_ht(name, &varname);
2822 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2823 {
2824 if (find_var(name, NULL, TRUE) != NULL)
2825 *eval_lavars_used = TRUE;
2826 }
2827
2828 name[len] = cc;
2829}
2830
2831/*
2832 * Find variable "name" in the list of variables.
2833 * Return a pointer to it if found, NULL if not found.
2834 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002835 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002836 */
2837 dictitem_T *
2838find_var(char_u *name, hashtab_T **htp, int no_autoload)
2839{
2840 char_u *varname;
2841 hashtab_T *ht;
2842 dictitem_T *ret = NULL;
2843
2844 ht = find_var_ht(name, &varname);
2845 if (htp != NULL)
2846 *htp = ht;
2847 if (ht == NULL)
2848 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002849 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002850 if (ret != NULL)
2851 return ret;
2852
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002853 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002854 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002855 if (ret != NULL)
2856 return ret;
2857
2858 // in Vim9 script items without a scope can be script-local
2859 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2860 {
2861 ht = get_script_local_ht();
2862 if (ht != NULL)
2863 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002864 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002865 if (ret != NULL)
2866 {
2867 if (htp != NULL)
2868 *htp = ht;
2869 return ret;
2870 }
2871 }
2872 }
2873
2874 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002875}
2876
2877/*
2878 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002879 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002880 * Returns NULL if not found.
2881 */
2882 dictitem_T *
2883find_var_in_ht(
2884 hashtab_T *ht,
2885 int htname,
2886 char_u *varname,
2887 int no_autoload)
2888{
2889 hashitem_T *hi;
2890
2891 if (*varname == NUL)
2892 {
2893 // Must be something like "s:", otherwise "ht" would be NULL.
2894 switch (htname)
2895 {
2896 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2897 case 'g': return &globvars_var;
2898 case 'v': return &vimvars_var;
2899 case 'b': return &curbuf->b_bufvar;
2900 case 'w': return &curwin->w_winvar;
2901 case 't': return &curtab->tp_winvar;
2902 case 'l': return get_funccal_local_var();
2903 case 'a': return get_funccal_args_var();
2904 }
2905 return NULL;
2906 }
2907
2908 hi = hash_find(ht, varname);
2909 if (HASHITEM_EMPTY(hi))
2910 {
2911 // For global variables we may try auto-loading the script. If it
2912 // worked find the variable again. Don't auto-load a script if it was
2913 // loaded already, otherwise it would be loaded every time when
2914 // checking if a function name is a Funcref variable.
2915 if (ht == &globvarht && !no_autoload)
2916 {
2917 // Note: script_autoload() may make "hi" invalid. It must either
2918 // be obtained again or not used.
2919 if (!script_autoload(varname, FALSE) || aborting())
2920 return NULL;
2921 hi = hash_find(ht, varname);
2922 }
2923 if (HASHITEM_EMPTY(hi))
2924 return NULL;
2925 }
2926 return HI2DI(hi);
2927}
2928
2929/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 * Get the script-local hashtab. NULL if not in a script context.
2931 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002932 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933get_script_local_ht(void)
2934{
2935 scid_T sid = current_sctx.sc_sid;
2936
Bram Moolenaare3d46852020-08-29 13:39:17 +02002937 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 return &SCRIPT_VARS(sid);
2939 return NULL;
2940}
2941
2942/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002943 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002944 * When "cmd" is TRUE it must look like a command, a function must be followed
2945 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01002946 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01002948 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002949lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01002950 char_u *name,
2951 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002952 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01002953 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954{
2955 hashtab_T *ht = get_script_local_ht();
2956 char_u buffer[30];
2957 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01002958 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002960 int is_global = FALSE;
2961 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962
2963 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002964 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 if (len < sizeof(buffer) - 1)
2966 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002967 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 vim_strncpy(buffer, name, len);
2969 p = buffer;
2970 }
2971 else
2972 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002973 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002975 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976 }
2977
2978 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002979 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980
2981 // if not script-local, then perhaps imported
Bram Moolenaar709664c2020-12-12 14:33:41 +01002982 if (res == FAIL && find_imported(p, 0, NULL) != NULL)
2983 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 if (p != buffer)
2985 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002986
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002987 // Find a function, so that a following "->" works.
2988 // When used as a command require "(" or "->" to follow, "Cmd" is a user
2989 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002990 if (res != OK)
2991 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002992 p = skipwhite(name + len);
2993
2994 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002995 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002996 // Do not check for an internal function, since it might also be a
2997 // valid command, such as ":split" versus "split()".
2998 // Skip "g:" before a function name.
2999 if (name[0] == 'g' && name[1] == ':')
3000 {
3001 is_global = TRUE;
3002 fname = name + 2;
3003 }
3004 if (find_func(fname, is_global, NULL) != NULL)
3005 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003006 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003007 }
3008
Bram Moolenaar709664c2020-12-12 14:33:41 +01003009 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010}
3011
3012/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003013 * Find the hashtab used for a variable name.
3014 * Return NULL if the name is not valid.
3015 * Set "varname" to the start of name without ':'.
3016 */
3017 hashtab_T *
3018find_var_ht(char_u *name, char_u **varname)
3019{
3020 hashitem_T *hi;
3021 hashtab_T *ht;
3022
3023 if (name[0] == NUL)
3024 return NULL;
3025 if (name[1] != ':')
3026 {
3027 // The name must not start with a colon or #.
3028 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3029 return NULL;
3030 *varname = name;
3031
3032 // "version" is "v:version" in all scopes if scriptversion < 3.
3033 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003034 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003035 {
3036 hi = hash_find(&compat_hashtab, name);
3037 if (!HASHITEM_EMPTY(hi))
3038 return &compat_hashtab;
3039 }
3040
3041 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 if (ht != NULL)
3043 return ht; // local variable
3044
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003045 // In Vim9 script items at the script level are script-local, except
3046 // for autoload names.
3047 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 {
3049 ht = get_script_local_ht();
3050 if (ht != NULL)
3051 return ht;
3052 }
3053
3054 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003055 }
3056 *varname = name + 2;
3057 if (*name == 'g') // global variable
3058 return &globvarht;
3059 // There must be no ':' or '#' in the rest of the name, unless g: is used
3060 if (vim_strchr(name + 2, ':') != NULL
3061 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3062 return NULL;
3063 if (*name == 'b') // buffer variable
3064 return &curbuf->b_vars->dv_hashtab;
3065 if (*name == 'w') // window variable
3066 return &curwin->w_vars->dv_hashtab;
3067 if (*name == 't') // tab page variable
3068 return &curtab->tp_vars->dv_hashtab;
3069 if (*name == 'v') // v: variable
3070 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003071 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003072 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003074 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 if (*name == 'a') // a: function argument
3076 return get_funccal_args_ht();
3077 if (*name == 'l') // l: local function variable
3078 return get_funccal_local_ht();
3079 }
3080 if (*name == 's') // script variable
3081 {
3082 ht = get_script_local_ht();
3083 if (ht != NULL)
3084 return ht;
3085 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003086 return NULL;
3087}
3088
3089/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003090 * Get the string value of a (global/local) variable.
3091 * Note: see tv_get_string() for how long the pointer remains valid.
3092 * Returns NULL when it doesn't exist.
3093 */
3094 char_u *
3095get_var_value(char_u *name)
3096{
3097 dictitem_T *v;
3098
3099 v = find_var(name, NULL, FALSE);
3100 if (v == NULL)
3101 return NULL;
3102 return tv_get_string(&v->di_tv);
3103}
3104
3105/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003106 * Allocate a new hashtab for a sourced script. It will be used while
3107 * sourcing this script and when executing functions defined in the script.
3108 */
3109 void
3110new_script_vars(scid_T id)
3111{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003112 scriptvar_T *sv;
3113
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003114 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3115 if (sv == NULL)
3116 return;
3117 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003118 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003119}
3120
3121/*
3122 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3123 * point to it.
3124 */
3125 void
3126init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3127{
3128 hash_init(&dict->dv_hashtab);
3129 dict->dv_lock = 0;
3130 dict->dv_scope = scope;
3131 dict->dv_refcount = DO_NOT_FREE_CNT;
3132 dict->dv_copyID = 0;
3133 dict_var->di_tv.vval.v_dict = dict;
3134 dict_var->di_tv.v_type = VAR_DICT;
3135 dict_var->di_tv.v_lock = VAR_FIXED;
3136 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3137 dict_var->di_key[0] = NUL;
3138}
3139
3140/*
3141 * Unreference a dictionary initialized by init_var_dict().
3142 */
3143 void
3144unref_var_dict(dict_T *dict)
3145{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003146 // Now the dict needs to be freed if no one else is using it, go back to
3147 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003148 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3149 dict_unref(dict);
3150}
3151
3152/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003153 * Clean up a list of internal variables.
3154 * Frees all allocated variables and the value they contain.
3155 * Clears hashtab "ht", does not free it.
3156 */
3157 void
3158vars_clear(hashtab_T *ht)
3159{
3160 vars_clear_ext(ht, TRUE);
3161}
3162
3163/*
3164 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3165 */
3166 void
3167vars_clear_ext(hashtab_T *ht, int free_val)
3168{
3169 int todo;
3170 hashitem_T *hi;
3171 dictitem_T *v;
3172
3173 hash_lock(ht);
3174 todo = (int)ht->ht_used;
3175 for (hi = ht->ht_array; todo > 0; ++hi)
3176 {
3177 if (!HASHITEM_EMPTY(hi))
3178 {
3179 --todo;
3180
3181 // Free the variable. Don't remove it from the hashtab,
3182 // ht_array might change then. hash_clear() takes care of it
3183 // later.
3184 v = HI2DI(hi);
3185 if (free_val)
3186 clear_tv(&v->di_tv);
3187 if (v->di_flags & DI_FLAGS_ALLOC)
3188 vim_free(v);
3189 }
3190 }
3191 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003192 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003193}
3194
3195/*
3196 * Delete a variable from hashtab "ht" at item "hi".
3197 * Clear the variable value and free the dictitem.
3198 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003199 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003200delete_var(hashtab_T *ht, hashitem_T *hi)
3201{
3202 dictitem_T *di = HI2DI(hi);
3203
3204 hash_remove(ht, hi);
3205 clear_tv(&di->di_tv);
3206 vim_free(di);
3207}
3208
3209/*
3210 * List the value of one internal variable.
3211 */
3212 static void
3213list_one_var(dictitem_T *v, char *prefix, int *first)
3214{
3215 char_u *tofree;
3216 char_u *s;
3217 char_u numbuf[NUMBUFLEN];
3218
3219 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3220 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3221 s == NULL ? (char_u *)"" : s, first);
3222 vim_free(tofree);
3223}
3224
3225 static void
3226list_one_var_a(
3227 char *prefix,
3228 char_u *name,
3229 int type,
3230 char_u *string,
3231 int *first) // when TRUE clear rest of screen and set to FALSE
3232{
3233 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3234 msg_start();
3235 msg_puts(prefix);
3236 if (name != NULL) // "a:" vars don't have a name stored
3237 msg_puts((char *)name);
3238 msg_putchar(' ');
3239 msg_advance(22);
3240 if (type == VAR_NUMBER)
3241 msg_putchar('#');
3242 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3243 msg_putchar('*');
3244 else if (type == VAR_LIST)
3245 {
3246 msg_putchar('[');
3247 if (*string == '[')
3248 ++string;
3249 }
3250 else if (type == VAR_DICT)
3251 {
3252 msg_putchar('{');
3253 if (*string == '{')
3254 ++string;
3255 }
3256 else
3257 msg_putchar(' ');
3258
3259 msg_outtrans(string);
3260
3261 if (type == VAR_FUNC || type == VAR_PARTIAL)
3262 msg_puts("()");
3263 if (*first)
3264 {
3265 msg_clr_eos();
3266 *first = FALSE;
3267 }
3268}
3269
3270/*
3271 * Set variable "name" to value in "tv".
3272 * If the variable already exists, the value is updated.
3273 * Otherwise the variable is created.
3274 */
3275 void
3276set_var(
3277 char_u *name,
3278 typval_T *tv,
3279 int copy) // make copy of value in "tv"
3280{
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003281 set_var_const(name, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003282}
3283
3284/*
3285 * Set variable "name" to value in "tv".
3286 * If the variable already exists and "is_const" is FALSE the value is updated.
3287 * Otherwise the variable is created.
3288 */
3289 void
3290set_var_const(
3291 char_u *name,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003292 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003293 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003294 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003295 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003296 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003297{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003298 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003299 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003300 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 dictitem_T *di;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003302 typval_T *dest_tv = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003303 char_u *varname;
3304 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003306 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003307 int var_in_vim9script;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003308 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003309 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003310
3311 ht = find_var_ht(name, &varname);
3312 if (ht == NULL || *varname == NUL)
3313 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003314 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003315 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003316 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003317 is_script_local = ht == get_script_local_ht();
3318
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003319 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003320 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003321 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003322 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003323 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003324 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003325 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003326 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003327 }
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003328 if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
3329 && vim_strchr((char_u *)"gwbt", name[0]) != NULL)
3330 // Do not make g:var, w:var, b:var or t:var final.
3331 flags &= ~ASSIGN_FINAL;
3332
Bram Moolenaare535db82021-03-31 21:07:24 +02003333 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003334 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3335 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003336 // For "[a, _] = list" the underscore is ignored.
3337 if ((flags & ASSIGN_UNPACK) == 0)
3338 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003339 goto failed;
3340 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003341
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003342 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003343
Bram Moolenaar24e93162021-07-18 20:40:33 +02003344 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003345 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003346 imported_T *import = find_imported(varname, 0, NULL);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003347
Bram Moolenaar24e93162021-07-18 20:40:33 +02003348 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003349 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003350 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
3351 svar_T *sv;
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003352 where_T where = WHERE_INIT;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003353
3354 // imported variable from another script
3355 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003357 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003358 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 }
Bram Moolenaar6853c382021-09-07 22:12:19 +02003360 if (import->imp_flags & IMP_FLAGS_STAR)
3361 {
3362 semsg(_(e_cannot_use_str_itself_it_is_imported_with_star),
3363 name);
3364 goto failed;
3365 }
Bram Moolenaar60579352021-07-19 21:45:07 +02003366 sv = ((svar_T *)si->sn_var_vals.ga_data) + import->imp_var_vals_idx;
3367
Bram Moolenaar60579352021-07-19 21:45:07 +02003368 where.wt_variable = TRUE;
3369 if (check_typval_type(sv->sv_type, tv, where) == FAIL
3370 || value_check_lock(sv->sv_tv->v_lock, name, FALSE))
3371 {
3372 goto failed;
3373 }
3374
Bram Moolenaar24e93162021-07-18 20:40:33 +02003375 dest_tv = sv->sv_tv;
Bram Moolenaarf6488542021-07-18 21:24:50 +02003376 clear_tv(dest_tv);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003377 }
3378 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379
Bram Moolenaar24e93162021-07-18 20:40:33 +02003380 if (dest_tv == NULL)
3381 {
3382 // Search in parent scope which is possible to reference from lambda
3383 if (di == NULL)
3384 di = find_var_in_scoped_ht(name, TRUE);
3385
3386 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003387 && var_wrong_func_name(name, di == NULL))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003388 goto failed;
3389
3390 if (need_convert_to_bool(type, tv))
3391 {
Bram Moolenaarf6488542021-07-18 21:24:50 +02003392 // Destination is a bool and the value is not, but it can be
3393 // converted.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003394 CLEAR_FIELD(bool_tv);
3395 bool_tv.v_type = VAR_BOOL;
3396 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3397 tv = &bool_tv;
3398 }
3399
3400 if (di != NULL)
3401 {
3402 // Item already exists. Allowed to replace when reloading.
3403 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
3404 {
3405 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaarf6488542021-07-18 21:24:50 +02003406 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003407 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003408 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar24e93162021-07-18 20:40:33 +02003409 goto failed;
3410 }
3411
3412 if (is_script_local && vim9script
Bram Moolenaarf6488542021-07-18 21:24:50 +02003413 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003414 {
3415 semsg(_(e_redefining_script_item_str), name);
3416 goto failed;
3417 }
3418
Bram Moolenaara06758d2021-10-15 00:18:37 +01003419 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003420 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003421 where_T where = WHERE_INIT;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003422 svar_T *sv = find_typval_in_script(&di->di_tv);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003423
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003424 if (sv != NULL)
3425 {
3426 // check the type and adjust to bool if needed
3427 where.wt_index = var_idx;
3428 where.wt_variable = TRUE;
3429 if (check_script_var_type(sv, tv, name, where) == FAIL)
3430 goto failed;
3431 if (type == NULL)
3432 type = sv->sv_type;
3433 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003434 }
3435
Bram Moolenaara06758d2021-10-15 00:18:37 +01003436 if ((flags & ASSIGN_FOR_LOOP) == 0
3437 && var_check_permission(di, name) == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003438 goto failed;
3439 }
3440 else
3441 {
3442 // can only redefine once
3443 di->di_flags &= ~DI_FLAGS_RELOAD;
3444
Bram Moolenaarf6488542021-07-18 21:24:50 +02003445 // A Vim9 script-local variable is also present in sn_all_vars
3446 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaar24e93162021-07-18 20:40:33 +02003447 if (var_in_vim9script)
3448 update_vim9_script_var(FALSE, di, flags, tv, &type,
Bram Moolenaarf6488542021-07-18 21:24:50 +02003449 (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003450 }
3451
3452 // existing variable, need to clear the value
3453
3454 // Handle setting internal di: variables separately where needed to
3455 // prevent changing the type.
3456 if (ht == &vimvarht)
3457 {
3458 if (di->di_tv.v_type == VAR_STRING)
3459 {
3460 VIM_CLEAR(di->di_tv.vval.v_string);
3461 if (copy || tv->v_type != VAR_STRING)
3462 {
3463 char_u *val = tv_get_string(tv);
3464
Bram Moolenaarf6488542021-07-18 21:24:50 +02003465 // Careful: when assigning to v:errmsg and
3466 // tv_get_string() causes an error message the variable
3467 // will already be set.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003468 if (di->di_tv.vval.v_string == NULL)
3469 di->di_tv.vval.v_string = vim_strsave(val);
3470 }
3471 else
3472 {
3473 // Take over the string to avoid an extra alloc/free.
3474 di->di_tv.vval.v_string = tv->vval.v_string;
3475 tv->vval.v_string = NULL;
3476 }
3477 goto failed;
3478 }
3479 else if (di->di_tv.v_type == VAR_NUMBER)
3480 {
3481 di->di_tv.vval.v_number = tv_get_number(tv);
3482 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003483 set_search_direction(di->di_tv.vval.v_number
3484 ? '/' : '?');
Bram Moolenaar24e93162021-07-18 20:40:33 +02003485#ifdef FEAT_SEARCH_EXTRA
3486 else if (STRCMP(varname, "hlsearch") == 0)
3487 {
3488 no_hlsearch = !di->di_tv.vval.v_number;
3489 redraw_all_later(SOME_VALID);
3490 }
3491#endif
3492 goto failed;
3493 }
3494 else if (di->di_tv.v_type != tv->v_type)
3495 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003496 semsg(_(e_setting_str_to_value_with_wrong_type), name);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003497 goto failed;
3498 }
3499 }
3500
3501 clear_tv(&di->di_tv);
3502 }
3503 else
3504 {
3505 // Item not found, check if a function already exists.
3506 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaarf6488542021-07-18 21:24:50 +02003507 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003508 {
3509 semsg(_(e_redefining_script_item_str), name);
3510 goto failed;
3511 }
3512
Bram Moolenaar24e93162021-07-18 20:40:33 +02003513 // add a new variable
3514 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3515 {
3516 semsg(_(e_unknown_variable_str), name);
3517 goto failed;
3518 }
3519
3520 // Can't add "v:" or "a:" variable.
3521 if (ht == &vimvarht || ht == get_funccal_args_ht())
3522 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003523 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003524 goto failed;
3525 }
3526
3527 // Make sure the variable name is valid. In Vim9 script an autoload
3528 // variable must be prefixed with "g:".
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003529 if (!valid_varname(varname, -1, !vim9script
Bram Moolenaarf6488542021-07-18 21:24:50 +02003530 || STRNCMP(name, "g:", 2) == 0))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003531 goto failed;
3532
3533 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3534 if (di == NULL)
3535 goto failed;
3536 STRCPY(di->di_key, varname);
3537 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
3538 {
3539 vim_free(di);
3540 goto failed;
3541 }
3542 di->di_flags = DI_FLAGS_ALLOC;
3543 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3544 di->di_flags |= DI_FLAGS_LOCK;
3545
3546 // A Vim9 script-local variable is also added to sn_all_vars and
3547 // sn_var_vals. It may set "type" from "tv".
Bram Moolenaare535db82021-03-31 21:07:24 +02003548 if (var_in_vim9script)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003549 update_vim9_script_var(TRUE, di, flags, tv, &type,
Bram Moolenaarf6488542021-07-18 21:24:50 +02003550 (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003551 }
3552
Bram Moolenaar24e93162021-07-18 20:40:33 +02003553 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003554 }
3555
3556 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003557 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003558 else
3559 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003560 *dest_tv = *tv;
3561 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003562 init_tv(tv);
3563 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003564 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003565
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003566 if (vim9script && type != NULL)
3567 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003568 if (type->tt_type == VAR_DICT && dest_tv->vval.v_dict != NULL)
Bram Moolenaar464393a2021-09-11 23:07:44 +02003569 {
3570 if (dest_tv->vval.v_dict->dv_type != type)
3571 {
3572 free_type(dest_tv->vval.v_dict->dv_type);
3573 dest_tv->vval.v_dict->dv_type = alloc_type(type);
3574 }
3575 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003576 else if (type->tt_type == VAR_LIST && dest_tv->vval.v_list != NULL)
Bram Moolenaar464393a2021-09-11 23:07:44 +02003577 {
3578 if (dest_tv->vval.v_list->lv_type != type)
3579 {
3580 free_type(dest_tv->vval.v_list->lv_type);
3581 dest_tv->vval.v_list->lv_type = alloc_type(type);
3582 }
3583 }
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003584 }
3585
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003586 // ":const var = value" locks the value
3587 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003588 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003589 // Like :lockvar! name: lock the value and what it contains, but only
3590 // if the reference count is up to one. That locks only literal
3591 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003592 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003593
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003594failed:
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003595 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003596 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003597}
3598
3599/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003600 * Check in this order for backwards compatibility:
3601 * - Whether the variable is read-only
3602 * - Whether the variable value is locked
3603 * - Whether the variable is locked
3604 */
3605 int
3606var_check_permission(dictitem_T *di, char_u *name)
3607{
3608 if (var_check_ro(di->di_flags, name, FALSE)
3609 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3610 || var_check_lock(di->di_flags, name, FALSE))
3611 return FAIL;
3612 return OK;
3613}
3614
3615/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003616 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3617 * Also give an error message.
3618 */
3619 int
3620var_check_ro(int flags, char_u *name, int use_gettext)
3621{
3622 if (flags & DI_FLAGS_RO)
3623 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003624 if (name == NULL)
3625 emsg(_(e_cannot_change_readonly_variable));
3626 else
3627 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02003628 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003629 return TRUE;
3630 }
3631 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3632 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003633 if (name == NULL)
3634 emsg(_(e_cannot_set_variable_in_sandbox));
3635 else
3636 semsg(_(e_cannot_set_variable_in_sandbox_str),
3637 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003638 return TRUE;
3639 }
3640 return FALSE;
3641}
3642
3643/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003644 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3645 * Also give an error message.
3646 */
3647 int
3648var_check_lock(int flags, char_u *name, int use_gettext)
3649{
3650 if (flags & DI_FLAGS_LOCK)
3651 {
3652 semsg(_(e_variable_is_locked_str),
3653 use_gettext ? (char_u *)_(name) : name);
3654 return TRUE;
3655 }
3656 return FALSE;
3657}
3658
3659/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003660 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3661 * Also give an error message.
3662 */
3663 int
3664var_check_fixed(int flags, char_u *name, int use_gettext)
3665{
3666 if (flags & DI_FLAGS_FIX)
3667 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003668 if (name == NULL)
3669 emsg(_(e_cannot_delete_variable));
3670 else
3671 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003672 use_gettext ? (char_u *)_(name) : name);
3673 return TRUE;
3674 }
3675 return FALSE;
3676}
3677
3678/*
3679 * Check if a funcref is assigned to a valid variable name.
3680 * Return TRUE and give an error if not.
3681 */
3682 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003683var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003684 char_u *name, // points to start of variable name
3685 int new_var) // TRUE when creating the variable
3686{
Bram Moolenaar32154662021-03-28 21:14:06 +02003687 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
3688 // the name can be used without the s: prefix.
3689 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
3690 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003691 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3692 ? name[2] : name[0]))
3693 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003694 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003695 return TRUE;
3696 }
3697 // Don't allow hiding a function. When "v" is not NULL we might be
3698 // assigning another function to the same var, the type is checked
3699 // below.
3700 if (new_var && function_exists(name, FALSE))
3701 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003702 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003703 name);
3704 return TRUE;
3705 }
3706 return FALSE;
3707}
3708
3709/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003710 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3711 * value. Also give an error message, using "name" or _("name") when
3712 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003713 */
3714 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003715value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003716{
3717 if (lock & VAR_LOCKED)
3718 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003719 if (name == NULL)
3720 emsg(_(e_value_is_locked));
3721 else
3722 semsg(_(e_value_is_locked_str),
3723 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003724 return TRUE;
3725 }
3726 if (lock & VAR_FIXED)
3727 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003728 if (name == NULL)
3729 emsg(_(e_cannot_change_value));
3730 else
3731 semsg(_(e_cannot_change_value_of_str),
3732 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003733 return TRUE;
3734 }
3735 return FALSE;
3736}
3737
3738/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003739 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003740 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003741 * Return FALSE and give an error if not.
3742 */
3743 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003744valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003745{
3746 char_u *p;
3747
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003748 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003749 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003750 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003751 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003752 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003753 return FALSE;
3754 }
3755 return TRUE;
3756}
3757
3758/*
3759 * getwinvar() and gettabwinvar()
3760 */
3761 static void
3762getwinvar(
3763 typval_T *argvars,
3764 typval_T *rettv,
3765 int off) // 1 for gettabwinvar()
3766{
3767 win_T *win;
3768 char_u *varname;
3769 dictitem_T *v;
3770 tabpage_T *tp = NULL;
3771 int done = FALSE;
3772 win_T *oldcurwin;
3773 tabpage_T *oldtabpage;
3774 int need_switch_win;
3775
3776 if (off == 1)
3777 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3778 else
3779 tp = curtab;
3780 win = find_win_by_nr(&argvars[off], tp);
3781 varname = tv_get_string_chk(&argvars[off + 1]);
3782 ++emsg_off;
3783
3784 rettv->v_type = VAR_STRING;
3785 rettv->vval.v_string = NULL;
3786
3787 if (win != NULL && varname != NULL)
3788 {
3789 // Set curwin to be our win, temporarily. Also set the tabpage,
3790 // otherwise the window is not valid. Only do this when needed,
3791 // autocommands get blocked.
3792 need_switch_win = !(tp == curtab && win == curwin);
3793 if (!need_switch_win
3794 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3795 {
3796 if (*varname == '&')
3797 {
3798 if (varname[1] == NUL)
3799 {
3800 // get all window-local options in a dict
3801 dict_T *opts = get_winbuf_options(FALSE);
3802
3803 if (opts != NULL)
3804 {
3805 rettv_dict_set(rettv, opts);
3806 done = TRUE;
3807 }
3808 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003809 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003810 // window-local-option
3811 done = TRUE;
3812 }
3813 else
3814 {
3815 // Look up the variable.
3816 // Let getwinvar({nr}, "") return the "w:" dictionary.
3817 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3818 varname, FALSE);
3819 if (v != NULL)
3820 {
3821 copy_tv(&v->di_tv, rettv);
3822 done = TRUE;
3823 }
3824 }
3825 }
3826
3827 if (need_switch_win)
3828 // restore previous notion of curwin
3829 restore_win(oldcurwin, oldtabpage, TRUE);
3830 }
3831
3832 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3833 // use the default return value
3834 copy_tv(&argvars[off + 2], rettv);
3835
3836 --emsg_off;
3837}
3838
3839/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003840 * Set option "varname" to the value of "varp" for the current buffer/window.
3841 */
3842 static void
3843set_option_from_tv(char_u *varname, typval_T *varp)
3844{
3845 long numval = 0;
3846 char_u *strval;
3847 char_u nbuf[NUMBUFLEN];
3848 int error = FALSE;
3849
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003850 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003851 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003852 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003853 strval = (char_u *)"0"; // avoid using "false"
3854 }
3855 else
3856 {
3857 if (!in_vim9script() || varp->v_type != VAR_STRING)
3858 numval = (long)tv_get_number_chk(varp, &error);
3859 strval = tv_get_string_buf_chk(varp, nbuf);
3860 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02003861 if (!error && strval != NULL)
3862 set_option_value(varname, numval, strval, OPT_LOCAL);
3863}
3864
3865/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003866 * "setwinvar()" and "settabwinvar()" functions
3867 */
3868 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003869setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003870{
3871 win_T *win;
3872 win_T *save_curwin;
3873 tabpage_T *save_curtab;
3874 int need_switch_win;
3875 char_u *varname, *winvarname;
3876 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003877 tabpage_T *tp = NULL;
3878
3879 if (check_secure())
3880 return;
3881
3882 if (off == 1)
3883 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3884 else
3885 tp = curtab;
3886 win = find_win_by_nr(&argvars[off], tp);
3887 varname = tv_get_string_chk(&argvars[off + 1]);
3888 varp = &argvars[off + 2];
3889
3890 if (win != NULL && varname != NULL && varp != NULL)
3891 {
3892 need_switch_win = !(tp == curtab && win == curwin);
3893 if (!need_switch_win
3894 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3895 {
3896 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003897 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003898 else
3899 {
3900 winvarname = alloc(STRLEN(varname) + 3);
3901 if (winvarname != NULL)
3902 {
3903 STRCPY(winvarname, "w:");
3904 STRCPY(winvarname + 2, varname);
3905 set_var(winvarname, varp, TRUE);
3906 vim_free(winvarname);
3907 }
3908 }
3909 }
3910 if (need_switch_win)
3911 restore_win(save_curwin, save_curtab, TRUE);
3912 }
3913}
3914
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003915/*
3916 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3917 * v:option_type, and v:option_command.
3918 */
3919 void
3920reset_v_option_vars(void)
3921{
3922 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3923 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3924 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3925 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3926 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3927 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3928}
3929
3930/*
3931 * Add an assert error to v:errors.
3932 */
3933 void
3934assert_error(garray_T *gap)
3935{
3936 struct vimvar *vp = &vimvars[VV_ERRORS];
3937
Bram Moolenaard787e402021-12-24 21:36:12 +00003938 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003939 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003940 set_vim_var_list(VV_ERRORS, list_alloc());
3941 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3942}
3943
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003944 int
3945var_exists(char_u *var)
3946{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003947 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003948 char_u *name;
3949 char_u *tofree;
3950 typval_T tv;
3951 int len = 0;
3952 int n = FALSE;
3953
3954 // get_name_len() takes care of expanding curly braces
3955 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003956 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003957 if (len > 0)
3958 {
3959 if (tofree != NULL)
3960 name = tofree;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003961 n = (eval_variable(name, len, &tv, NULL,
3962 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003963 if (n)
3964 {
3965 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003966 arg = skipwhite(arg);
3967 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003968 if (n)
3969 clear_tv(&tv);
3970 }
3971 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003972 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003973 n = FALSE;
3974
3975 vim_free(tofree);
3976 return n;
3977}
3978
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003979static lval_T *redir_lval = NULL;
3980#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3981static garray_T redir_ga; // only valid when redir_lval is not NULL
3982static char_u *redir_endp = NULL;
3983static char_u *redir_varname = NULL;
3984
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003985 int
3986alloc_redir_lval(void)
3987{
3988 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3989 if (redir_lval == NULL)
3990 return FAIL;
3991 return OK;
3992}
3993
3994 void
3995clear_redir_lval(void)
3996{
3997 VIM_CLEAR(redir_lval);
3998}
3999
4000 void
4001init_redir_ga(void)
4002{
4003 ga_init2(&redir_ga, (int)sizeof(char), 500);
4004}
4005
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004006/*
4007 * Start recording command output to a variable
4008 * When "append" is TRUE append to an existing variable.
4009 * Returns OK if successfully completed the setup. FAIL otherwise.
4010 */
4011 int
4012var_redir_start(char_u *name, int append)
4013{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004014 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004015 typval_T tv;
4016
4017 // Catch a bad name early.
4018 if (!eval_isnamec1(*name))
4019 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004020 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004021 return FAIL;
4022 }
4023
4024 // Make a copy of the name, it is used in redir_lval until redir ends.
4025 redir_varname = vim_strsave(name);
4026 if (redir_varname == NULL)
4027 return FAIL;
4028
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004029 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004030 {
4031 var_redir_stop();
4032 return FAIL;
4033 }
4034
4035 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004036 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004037
4038 // Parse the variable name (can be a dict or list entry).
4039 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4040 FNE_CHECK_START);
4041 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4042 {
4043 clear_lval(redir_lval);
4044 if (redir_endp != NULL && *redir_endp != NUL)
4045 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004046 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004047 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004048 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004049 redir_endp = NULL; // don't store a value, only cleanup
4050 var_redir_stop();
4051 return FAIL;
4052 }
4053
4054 // check if we can write to the variable: set it to or append an empty
4055 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004056 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004057 tv.v_type = VAR_STRING;
4058 tv.vval.v_string = (char_u *)"";
4059 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004060 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004061 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004062 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004063 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004064 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004065 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004066 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004067 {
4068 redir_endp = NULL; // don't store a value, only cleanup
4069 var_redir_stop();
4070 return FAIL;
4071 }
4072
4073 return OK;
4074}
4075
4076/*
4077 * Append "value[value_len]" to the variable set by var_redir_start().
4078 * The actual appending is postponed until redirection ends, because the value
4079 * appended may in fact be the string we write to, changing it may cause freed
4080 * memory to be used:
4081 * :redir => foo
4082 * :let foo
4083 * :redir END
4084 */
4085 void
4086var_redir_str(char_u *value, int value_len)
4087{
4088 int len;
4089
4090 if (redir_lval == NULL)
4091 return;
4092
4093 if (value_len == -1)
4094 len = (int)STRLEN(value); // Append the entire string
4095 else
4096 len = value_len; // Append only "value_len" characters
4097
4098 if (ga_grow(&redir_ga, len) == OK)
4099 {
4100 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4101 redir_ga.ga_len += len;
4102 }
4103 else
4104 var_redir_stop();
4105}
4106
4107/*
4108 * Stop redirecting command output to a variable.
4109 * Frees the allocated memory.
4110 */
4111 void
4112var_redir_stop(void)
4113{
4114 typval_T tv;
4115
4116 if (EVALCMD_BUSY)
4117 {
4118 redir_lval = NULL;
4119 return;
4120 }
4121
4122 if (redir_lval != NULL)
4123 {
4124 // If there was no error: assign the text to the variable.
4125 if (redir_endp != NULL)
4126 {
4127 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4128 tv.v_type = VAR_STRING;
4129 tv.vval.v_string = redir_ga.ga_data;
4130 // Call get_lval() again, if it's inside a Dict or List it may
4131 // have changed.
4132 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4133 FALSE, FALSE, 0, FNE_CHECK_START);
4134 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004136 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004137 clear_lval(redir_lval);
4138 }
4139
4140 // free the collected output
4141 VIM_CLEAR(redir_ga.ga_data);
4142
4143 VIM_CLEAR(redir_lval);
4144 }
4145 VIM_CLEAR(redir_varname);
4146}
4147
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004148/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004149 * Get the collected redirected text and clear redir_ga.
4150 */
4151 char_u *
4152get_clear_redir_ga(void)
4153{
4154 char_u *res;
4155
4156 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4157 res = redir_ga.ga_data;
4158 redir_ga.ga_data = NULL;
4159 return res;
4160}
4161
4162/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004163 * "gettabvar()" function
4164 */
4165 void
4166f_gettabvar(typval_T *argvars, typval_T *rettv)
4167{
4168 win_T *oldcurwin;
4169 tabpage_T *tp, *oldtabpage;
4170 dictitem_T *v;
4171 char_u *varname;
4172 int done = FALSE;
4173
4174 rettv->v_type = VAR_STRING;
4175 rettv->vval.v_string = NULL;
4176
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004177 if (in_vim9script()
4178 && (check_for_number_arg(argvars, 0) == FAIL
4179 || check_for_string_arg(argvars, 1) == FAIL))
4180 return;
4181
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004182 varname = tv_get_string_chk(&argvars[1]);
4183 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4184 if (tp != NULL && varname != NULL)
4185 {
4186 // Set tp to be our tabpage, temporarily. Also set the window to the
4187 // first window in the tabpage, otherwise the window is not valid.
4188 if (switch_win(&oldcurwin, &oldtabpage,
4189 tp == curtab || tp->tp_firstwin == NULL ? firstwin
4190 : tp->tp_firstwin, tp, TRUE) == OK)
4191 {
4192 // look up the variable
4193 // Let gettabvar({nr}, "") return the "t:" dictionary.
4194 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
4195 if (v != NULL)
4196 {
4197 copy_tv(&v->di_tv, rettv);
4198 done = TRUE;
4199 }
4200 }
4201
4202 // restore previous notion of curwin
4203 restore_win(oldcurwin, oldtabpage, TRUE);
4204 }
4205
4206 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4207 copy_tv(&argvars[2], rettv);
4208}
4209
4210/*
4211 * "gettabwinvar()" function
4212 */
4213 void
4214f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4215{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004216 if (in_vim9script()
4217 && (check_for_number_arg(argvars, 0) == FAIL
4218 || check_for_number_arg(argvars, 1) == FAIL
4219 || check_for_string_arg(argvars, 2) == FAIL))
4220 return;
4221
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004222 getwinvar(argvars, rettv, 1);
4223}
4224
4225/*
4226 * "getwinvar()" function
4227 */
4228 void
4229f_getwinvar(typval_T *argvars, typval_T *rettv)
4230{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004231 if (in_vim9script()
4232 && (check_for_number_arg(argvars, 0) == FAIL
4233 || check_for_string_arg(argvars, 1) == FAIL))
4234 return;
4235
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004236 getwinvar(argvars, rettv, 0);
4237}
4238
4239/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004240 * "getbufvar()" function
4241 */
4242 void
4243f_getbufvar(typval_T *argvars, typval_T *rettv)
4244{
4245 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004246 char_u *varname;
4247 dictitem_T *v;
4248 int done = FALSE;
4249
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004250 if (in_vim9script()
4251 && (check_for_buffer_arg(argvars, 0) == FAIL
4252 || check_for_string_arg(argvars, 1) == FAIL))
4253 return;
4254
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004255 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004256 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004257
4258 rettv->v_type = VAR_STRING;
4259 rettv->vval.v_string = NULL;
4260
4261 if (buf != NULL && varname != NULL)
4262 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004263 if (*varname == '&')
4264 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004265 buf_T *save_curbuf = curbuf;
4266
4267 // set curbuf to be our buf, temporarily
4268 curbuf = buf;
4269
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004270 if (varname[1] == NUL)
4271 {
4272 // get all buffer-local options in a dict
4273 dict_T *opts = get_winbuf_options(TRUE);
4274
4275 if (opts != NULL)
4276 {
4277 rettv_dict_set(rettv, opts);
4278 done = TRUE;
4279 }
4280 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004281 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004282 // buffer-local-option
4283 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02004284
4285 // restore previous notion of curbuf
4286 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004287 }
4288 else
4289 {
4290 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02004291 if (*varname == NUL)
4292 // Let getbufvar({nr}, "") return the "b:" dictionary.
4293 v = &buf->b_bufvar;
4294 else
4295 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
4296 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004297 if (v != NULL)
4298 {
4299 copy_tv(&v->di_tv, rettv);
4300 done = TRUE;
4301 }
4302 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004303 }
4304
4305 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4306 // use the default value
4307 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004308}
4309
4310/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004311 * "settabvar()" function
4312 */
4313 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004314f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004315{
4316 tabpage_T *save_curtab;
4317 tabpage_T *tp;
4318 char_u *varname, *tabvarname;
4319 typval_T *varp;
4320
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004321 if (check_secure())
4322 return;
4323
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004324 if (in_vim9script()
4325 && (check_for_number_arg(argvars, 0) == FAIL
4326 || check_for_string_arg(argvars, 1) == FAIL))
4327 return;
4328
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004329 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4330 varname = tv_get_string_chk(&argvars[1]);
4331 varp = &argvars[2];
4332
4333 if (varname != NULL && varp != NULL && tp != NULL)
4334 {
4335 save_curtab = curtab;
4336 goto_tabpage_tp(tp, FALSE, FALSE);
4337
4338 tabvarname = alloc(STRLEN(varname) + 3);
4339 if (tabvarname != NULL)
4340 {
4341 STRCPY(tabvarname, "t:");
4342 STRCPY(tabvarname + 2, varname);
4343 set_var(tabvarname, varp, TRUE);
4344 vim_free(tabvarname);
4345 }
4346
4347 // Restore current tabpage
4348 if (valid_tabpage(save_curtab))
4349 goto_tabpage_tp(save_curtab, FALSE, FALSE);
4350 }
4351}
4352
4353/*
4354 * "settabwinvar()" function
4355 */
4356 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004357f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004358{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004359 if (in_vim9script()
4360 && (check_for_number_arg(argvars, 0) == FAIL
4361 || check_for_number_arg(argvars, 1) == FAIL
4362 || check_for_string_arg(argvars, 2) == FAIL))
4363 return;
4364
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004365 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004366}
4367
4368/*
4369 * "setwinvar()" function
4370 */
4371 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004372f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004373{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004374 if (in_vim9script()
4375 && (check_for_number_arg(argvars, 0) == FAIL
4376 || check_for_string_arg(argvars, 1) == FAIL))
4377 return;
4378
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004379 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004380}
4381
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004382/*
4383 * "setbufvar()" function
4384 */
4385 void
4386f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4387{
4388 buf_T *buf;
4389 char_u *varname, *bufvarname;
4390 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004391
4392 if (check_secure())
4393 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004394
4395 if (in_vim9script()
4396 && (check_for_buffer_arg(argvars, 0) == FAIL
4397 || check_for_string_arg(argvars, 1) == FAIL))
4398 return;
4399
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004400 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004401 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004402 varp = &argvars[2];
4403
4404 if (buf != NULL && varname != NULL && varp != NULL)
4405 {
4406 if (*varname == '&')
4407 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004408 aco_save_T aco;
4409
4410 // set curbuf to be our buf, temporarily
4411 aucmd_prepbuf(&aco, buf);
4412
Bram Moolenaar191929b2020-08-19 21:20:49 +02004413 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004414
4415 // reset notion of buffer
4416 aucmd_restbuf(&aco);
4417 }
4418 else
4419 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004420 bufvarname = alloc(STRLEN(varname) + 3);
4421 if (bufvarname != NULL)
4422 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004423 buf_T *save_curbuf = curbuf;
4424
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004425 curbuf = buf;
4426 STRCPY(bufvarname, "b:");
4427 STRCPY(bufvarname + 2, varname);
4428 set_var(bufvarname, varp, TRUE);
4429 vim_free(bufvarname);
4430 curbuf = save_curbuf;
4431 }
4432 }
4433 }
4434}
4435
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004436/*
4437 * Get a callback from "arg". It can be a Funcref or a function name.
4438 * When "arg" is zero return an empty string.
4439 * "cb_name" is not allocated.
4440 * "cb_name" is set to NULL for an invalid argument.
4441 */
4442 callback_T
4443get_callback(typval_T *arg)
4444{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004445 callback_T res;
4446 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004447
4448 res.cb_free_name = FALSE;
4449 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4450 {
4451 res.cb_partial = arg->vval.v_partial;
4452 ++res.cb_partial->pt_refcount;
4453 res.cb_name = partial_name(res.cb_partial);
4454 }
4455 else
4456 {
4457 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004458 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4459 && isdigit(*arg->vval.v_string))
4460 r = FAIL;
4461 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004462 {
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004463 if (arg->v_type == VAR_STRING)
4464 {
4465 char_u *name;
4466
4467 name = get_scriptlocal_funcname(arg->vval.v_string);
4468 if (name != NULL)
4469 {
4470 vim_free(arg->vval.v_string);
4471 arg->vval.v_string = name;
4472 }
4473 }
4474
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004475 res.cb_name = arg->vval.v_string;
4476 func_ref(res.cb_name);
4477 }
4478 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004479 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004480 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004481 r = FAIL;
4482
4483 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004484 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004485 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004486 res.cb_name = NULL;
4487 }
4488 }
4489 return res;
4490}
4491
4492/*
4493 * Copy a callback into a typval_T.
4494 */
4495 void
4496put_callback(callback_T *cb, typval_T *tv)
4497{
4498 if (cb->cb_partial != NULL)
4499 {
4500 tv->v_type = VAR_PARTIAL;
4501 tv->vval.v_partial = cb->cb_partial;
4502 ++tv->vval.v_partial->pt_refcount;
4503 }
4504 else
4505 {
4506 tv->v_type = VAR_FUNC;
4507 tv->vval.v_string = vim_strsave(cb->cb_name);
4508 func_ref(cb->cb_name);
4509 }
4510}
4511
4512/*
4513 * Make a copy of "src" into "dest", allocating the function name if needed,
4514 * without incrementing the refcount.
4515 */
4516 void
4517set_callback(callback_T *dest, callback_T *src)
4518{
4519 if (src->cb_partial == NULL)
4520 {
4521 // just a function name, make a copy
4522 dest->cb_name = vim_strsave(src->cb_name);
4523 dest->cb_free_name = TRUE;
4524 }
4525 else
4526 {
4527 // cb_name is a pointer into cb_partial
4528 dest->cb_name = src->cb_name;
4529 dest->cb_free_name = FALSE;
4530 }
4531 dest->cb_partial = src->cb_partial;
4532}
4533
4534/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004535 * Copy callback from "src" to "dest", incrementing the refcounts.
4536 */
4537 void
4538copy_callback(callback_T *dest, callback_T *src)
4539{
4540 dest->cb_partial = src->cb_partial;
4541 if (dest->cb_partial != NULL)
4542 {
4543 dest->cb_name = src->cb_name;
4544 dest->cb_free_name = FALSE;
4545 ++dest->cb_partial->pt_refcount;
4546 }
4547 else
4548 {
4549 dest->cb_name = vim_strsave(src->cb_name);
4550 dest->cb_free_name = TRUE;
4551 func_ref(src->cb_name);
4552 }
4553}
4554
4555/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004556 * Unref/free "callback" returned by get_callback() or set_callback().
4557 */
4558 void
4559free_callback(callback_T *callback)
4560{
4561 if (callback->cb_partial != NULL)
4562 {
4563 partial_unref(callback->cb_partial);
4564 callback->cb_partial = NULL;
4565 }
4566 else if (callback->cb_name != NULL)
4567 func_unref(callback->cb_name);
4568 if (callback->cb_free_name)
4569 {
4570 vim_free(callback->cb_name);
4571 callback->cb_free_name = FALSE;
4572 }
4573 callback->cb_name = NULL;
4574}
4575
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004576#endif // FEAT_EVAL