blob: 4f7252c5c68fdc0e36b61253e2471843ad594142 [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;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000372 sctx_T saved_sctx = current_sctx;
373 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200374
375 set_vim_var_string(VV_CC_FROM, enc_from, -1);
376 set_vim_var_string(VV_CC_TO, enc_to, -1);
377 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
378 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000379 ctx = get_option_sctx("charconvert");
380 if (ctx != NULL)
381 current_sctx = *ctx;
382
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200383 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
384 err = TRUE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000385
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200386 set_vim_var_string(VV_CC_FROM, NULL, -1);
387 set_vim_var_string(VV_CC_TO, NULL, -1);
388 set_vim_var_string(VV_FNAME_IN, NULL, -1);
389 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000390 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200391
392 if (err)
393 return FAIL;
394 return OK;
395}
396
397# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
398 int
399eval_printexpr(char_u *fname, char_u *args)
400{
401 int err = FALSE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000402 sctx_T saved_sctx = current_sctx;
403 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200404
405 set_vim_var_string(VV_FNAME_IN, fname, -1);
406 set_vim_var_string(VV_CMDARG, args, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000407 ctx = get_option_sctx("printexpr");
408 if (ctx != NULL)
409 current_sctx = *ctx;
410
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200411 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
412 err = TRUE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000413
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200414 set_vim_var_string(VV_FNAME_IN, NULL, -1);
415 set_vim_var_string(VV_CMDARG, NULL, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000416 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200417
418 if (err)
419 {
420 mch_remove(fname);
421 return FAIL;
422 }
423 return OK;
424}
425# endif
426
427# if defined(FEAT_DIFF) || defined(PROTO)
428 void
429eval_diff(
430 char_u *origfile,
431 char_u *newfile,
432 char_u *outfile)
433{
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000434 sctx_T saved_sctx = current_sctx;
435 sctx_T *ctx;
436 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200437
438 set_vim_var_string(VV_FNAME_IN, origfile, -1);
439 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
440 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000441
442 ctx = get_option_sctx("diffexpr");
443 if (ctx != NULL)
444 current_sctx = *ctx;
445
446 // errors are ignored
447 tv = eval_expr(p_dex, NULL);
Bram Moolenaar39b89442022-01-22 18:21:36 +0000448 free_tv(tv);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000449
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200450 set_vim_var_string(VV_FNAME_IN, NULL, -1);
451 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
452 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000453 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200454}
455
456 void
457eval_patch(
458 char_u *origfile,
459 char_u *difffile,
460 char_u *outfile)
461{
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000462 sctx_T saved_sctx = current_sctx;
463 sctx_T *ctx;
464 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200465
466 set_vim_var_string(VV_FNAME_IN, origfile, -1);
467 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
468 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000469
470 ctx = get_option_sctx("patchexpr");
471 if (ctx != NULL)
472 current_sctx = *ctx;
473
474 // errors are ignored
475 tv = eval_expr(p_pex, NULL);
476 free_tv(tv);
477
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200478 set_vim_var_string(VV_FNAME_IN, NULL, -1);
479 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
480 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000481 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200482}
483# endif
484
485#if defined(FEAT_SPELL) || defined(PROTO)
486/*
487 * Evaluate an expression to a list with suggestions.
488 * For the "expr:" part of 'spellsuggest'.
489 * Returns NULL when there is an error.
490 */
491 list_T *
492eval_spell_expr(char_u *badword, char_u *expr)
493{
494 typval_T save_val;
495 typval_T rettv;
496 list_T *list = NULL;
497 char_u *p = skipwhite(expr);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000498 sctx_T saved_sctx = current_sctx;
499 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200500
501 // Set "v:val" to the bad word.
502 prepare_vimvar(VV_VAL, &save_val);
503 set_vim_var_string(VV_VAL, badword, -1);
504 if (p_verbose == 0)
505 ++emsg_off;
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000506 ctx = get_option_sctx("spellsuggest");
507 if (ctx != NULL)
508 current_sctx = *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200509
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200510 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200511 {
512 if (rettv.v_type != VAR_LIST)
513 clear_tv(&rettv);
514 else
515 list = rettv.vval.v_list;
516 }
517
518 if (p_verbose == 0)
519 --emsg_off;
520 clear_tv(get_vim_var_tv(VV_VAL));
521 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000522 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200523
524 return list;
525}
526
527/*
528 * "list" is supposed to contain two items: a word and a number. Return the
529 * word in "pp" and the number as the return value.
530 * Return -1 if anything isn't right.
531 * Used to get the good word and score from the eval_spell_expr() result.
532 */
533 int
534get_spellword(list_T *list, char_u **pp)
535{
536 listitem_T *li;
537
538 li = list->lv_first;
539 if (li == NULL)
540 return -1;
541 *pp = tv_get_string(&li->li_tv);
542
543 li = li->li_next;
544 if (li == NULL)
545 return -1;
546 return (int)tv_get_number(&li->li_tv);
547}
548#endif
549
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200550/*
551 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200552 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200553 * When not used yet add the variable to the v: hashtable.
554 */
555 void
556prepare_vimvar(int idx, typval_T *save_tv)
557{
558 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200559 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaard787e402021-12-24 21:36:12 +0000560 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200561 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
562}
563
564/*
565 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200566 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200567 * When no longer defined, remove the variable from the v: hashtable.
568 */
569 void
570restore_vimvar(int idx, typval_T *save_tv)
571{
572 hashitem_T *hi;
573
574 vimvars[idx].vv_tv = *save_tv;
Bram Moolenaard787e402021-12-24 21:36:12 +0000575 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200576 {
577 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
578 if (HASHITEM_EMPTY(hi))
579 internal_error("restore_vimvar()");
580 else
581 hash_remove(&vimvarht, hi);
582 }
583}
584
585/*
586 * List Vim variables.
587 */
588 static void
589list_vim_vars(int *first)
590{
591 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
592}
593
594/*
595 * List script-local variables, if there is a script.
596 */
597 static void
598list_script_vars(int *first)
599{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200600 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200601 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
602 "s:", FALSE, first);
603}
604
605/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200606 * Get a list of lines from a HERE document. The here document is a list of
607 * lines surrounded by a marker.
608 * cmd << {marker}
609 * {line1}
610 * {line2}
611 * ....
612 * {marker}
613 *
614 * The {marker} is a string. If the optional 'trim' word is supplied before the
615 * marker, then the leading indentation before the lines (matching the
616 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200617 *
618 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
619 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
620 * missing, then '.' is accepted as a marker.
621 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200622 * Returns a List with {lines} or NULL.
623 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200625heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200626{
627 char_u *theline;
628 char_u *marker;
629 list_T *l;
630 char_u *p;
631 int marker_indent_len = 0;
632 int text_indent_len = 0;
633 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200634 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200635 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200636
637 if (eap->getline == NULL)
638 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000639 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200640 return NULL;
641 }
642
643 // Check for the optional 'trim' word before the marker
644 cmd = skipwhite(cmd);
645 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
646 {
647 cmd = skipwhite(cmd + 4);
648
649 // Trim the indentation from all the lines in the here document.
650 // The amount of indentation trimmed is the same as the indentation of
651 // the first line after the :let command line. To find the end marker
652 // the indent of the :let command line is trimmed.
653 p = *eap->cmdlinep;
654 while (VIM_ISWHITE(*p))
655 {
656 p++;
657 marker_indent_len++;
658 }
659 text_indent_len = -1;
660 }
661
662 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200663 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200664 {
665 marker = skipwhite(cmd);
666 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200667 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200668 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000669 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200670 return NULL;
671 }
672 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200673 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200674 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000675 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200676 return NULL;
677 }
678 }
679 else
680 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200681 // When getting lines for an embedded script, if the marker is missing,
682 // accept '.' as the marker.
683 if (script_get)
684 marker = dot;
685 else
686 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000687 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200688 return NULL;
689 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200690 }
691
692 l = list_alloc();
693 if (l == NULL)
694 return NULL;
695
696 for (;;)
697 {
698 int mi = 0;
699 int ti = 0;
700
701 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
702 if (theline == NULL)
703 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000704 semsg(_(e_missing_end_marker_str), marker);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200705 break;
706 }
707
708 // with "trim": skip the indent matching the :let line to find the
709 // marker
710 if (marker_indent_len > 0
711 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
712 mi = marker_indent_len;
713 if (STRCMP(marker, theline + mi) == 0)
714 {
715 vim_free(theline);
716 break;
717 }
718
719 if (text_indent_len == -1 && *theline != NUL)
720 {
721 // set the text indent from the first line.
722 p = theline;
723 text_indent_len = 0;
724 while (VIM_ISWHITE(*p))
725 {
726 p++;
727 text_indent_len++;
728 }
729 text_indent = vim_strnsave(theline, text_indent_len);
730 }
731 // with "trim": skip the indent matching the first line
732 if (text_indent != NULL)
733 for (ti = 0; ti < text_indent_len; ++ti)
734 if (theline[ti] != text_indent[ti])
735 break;
736
737 if (list_append_string(l, theline + ti, -1) == FAIL)
738 break;
739 vim_free(theline);
740 }
741 vim_free(text_indent);
742
743 return l;
744}
745
746/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200747 * Vim9 variable declaration:
748 * ":var name"
749 * ":var name: type"
750 * ":var name = expr"
751 * ":var name: type = expr"
752 * etc.
753 */
754 void
755ex_var(exarg_T *eap)
756{
Bram Moolenaar330a3882022-03-05 11:05:57 +0000757 char_u *p = eap->cmd;
Bram Moolenaare1d12112022-03-05 11:37:48 +0000758 int has_var;
Bram Moolenaar330a3882022-03-05 11:05:57 +0000759
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200760 if (!in_vim9script())
761 {
762 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
763 return;
764 }
Bram Moolenaare1d12112022-03-05 11:37:48 +0000765 has_var = checkforcmd_noparen(&p, "var", 3);
766 if (current_sctx.sc_sid == 0 && has_var)
Bram Moolenaar0e1574c2022-03-03 17:05:35 +0000767 {
768 emsg(_(e_cannot_declare_variable_on_command_line));
769 return;
770 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200771 ex_let(eap);
772}
773
774/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200775 * ":let" list all variable values
776 * ":let var1 var2" list variable values
777 * ":let var = expr" assignment command.
778 * ":let var += expr" assignment command.
779 * ":let var -= expr" assignment command.
780 * ":let var *= expr" assignment command.
781 * ":let var /= expr" assignment command.
782 * ":let var %= expr" assignment command.
783 * ":let var .= expr" assignment command.
784 * ":let var ..= expr" assignment command.
785 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100786 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100787 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200788 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200789 * ":final var = expr" assignment command.
790 * ":final [var1, var2] = expr" unpack list.
791 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200792 * ":const" list all variable values
793 * ":const var1 var2" list variable values
794 * ":const var = expr" assignment command.
795 * ":const [var1, var2] = expr" unpack list.
796 */
797 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200798ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200799{
800 char_u *arg = eap->arg;
801 char_u *expr = NULL;
802 typval_T rettv;
803 int i;
804 int var_count = 0;
805 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200806 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200807 char_u *argend;
808 int first = TRUE;
809 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200810 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100811 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200812 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200813
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200814 if (eap->cmdidx == CMD_final && !vim9script)
815 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100816 // In legacy Vim script ":final" is short for ":finally".
817 ex_finally(eap);
818 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200819 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200820 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200821 {
822 emsg(_(e_cannot_use_let_in_vim9_script));
823 return;
824 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200825
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100826 if (eap->cmdidx == CMD_const)
827 flags |= ASSIGN_CONST;
828 else if (eap->cmdidx == CMD_final)
829 flags |= ASSIGN_FINAL;
830
831 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100832 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200833 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200835 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200836 if (argend == NULL)
837 return;
838 if (argend > arg && argend[-1] == '.') // for var.='str'
839 --argend;
840 expr = skipwhite(argend);
841 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +0200842 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200843 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200844 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
845 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200846 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200847 {
848 // ":let" without "=": list variables
849 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000850 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200851 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000852 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200853 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200854 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200855 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200856 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100857 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +0000858 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100859 else
860 // Vim9 declaration ":var name: type"
861 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200862 }
863 else
864 {
865 // ":let var1 var2" - list values
866 arg = list_arg_vars(eap, arg, &first);
867 }
868 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200869 else if (!eap->skip)
870 {
871 // ":let"
872 list_glob_vars(&first);
873 list_buf_vars(&first);
874 list_win_vars(&first);
875 list_tab_vars(&first);
876 list_script_vars(&first);
877 list_func_vars(&first);
878 list_vim_vars(&first);
879 }
Bram Moolenaar63b91732021-08-05 20:40:03 +0200880 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200881 }
882 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
883 {
884 list_T *l;
Bram Moolenaar81530e32021-07-28 21:25:49 +0200885 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200886
887 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200888 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200889 if (l != NULL)
890 {
891 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200892 if (!eap->skip)
893 {
Bram Moolenaar81530e32021-07-28 21:25:49 +0200894 // errors are for the assignment, not the end marker
895 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200896 op[0] = '=';
897 op[1] = NUL;
898 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100899 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200900 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200901 clear_tv(&rettv);
902 }
903 }
904 else
905 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200906 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200907 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200908
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200909 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200910 i = FAIL;
911 if (has_assign || concat)
912 {
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100913 int cur_lnum;
914
Bram Moolenaar32e35112020-05-14 22:41:15 +0200915 op[0] = '=';
916 op[1] = NUL;
917 if (*expr != '=')
918 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200919 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200920 {
921 // +=, /=, etc. require an existing variable
922 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
923 i = FAIL;
924 }
925 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200926 {
927 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200928 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200929 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200930 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200931 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200932 ++len;
933 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200934 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200935 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200936 }
937 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200938 ++expr;
939
Bram Moolenaar7f2c3412021-11-29 16:01:49 +0000940 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200941 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200942 {
943 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100944 semsg(_(e_white_space_required_before_and_after_str_at_str),
945 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200946 i = FAIL;
947 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200948
949 if (eap->skip)
950 ++emsg_skip;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200951 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200952 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100953 cur_lnum = SOURCING_LNUM;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200954 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200955 if (eap->skip)
956 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200957 clear_evalarg(&evalarg, eap);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100958
959 // Restore the line number so that any type error is given for the
960 // declaration, not the expression.
961 SOURCING_LNUM = cur_lnum;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200962 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200963 if (eap->skip)
964 {
965 if (i != FAIL)
966 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200967 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200968 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200969 {
970 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200971 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200972 clear_tv(&rettv);
973 }
974 }
975}
976
977/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +0100978 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200979 * Handles both "var" with any type and "[var, var; var]" with a list type.
980 * When "op" is not NULL it points to a string with characters that
981 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
982 * or concatenate.
983 * Returns OK or FAIL;
984 */
985 int
986ex_let_vars(
987 char_u *arg_start,
988 typval_T *tv,
989 int copy, // copy values from "tv", don't move
990 int semicolon, // from skip_var_list()
991 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100992 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200993 char_u *op)
994{
995 char_u *arg = arg_start;
996 list_T *l;
997 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100998 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200999 listitem_T *item;
1000 typval_T ltv;
1001
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00001002 if (tv->v_type == VAR_VOID)
1003 {
1004 emsg(_(e_cannot_use_void_value));
1005 return FAIL;
1006 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001007 if (*arg != '[')
1008 {
1009 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001010 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001011 return FAIL;
1012 return OK;
1013 }
1014
1015 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1016 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
1017 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001018 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001019 return FAIL;
1020 }
1021
1022 i = list_len(l);
1023 if (semicolon == 0 && var_count < i)
1024 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001025 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001026 return FAIL;
1027 }
1028 if (var_count - semicolon > i)
1029 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001030 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001031 return FAIL;
1032 }
1033
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001034 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001035 item = l->lv_first;
1036 while (*arg != ']')
1037 {
1038 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001039 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001040 arg = ex_let_one(arg, &item->li_tv, TRUE,
1041 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001042 item = item->li_next;
1043 if (arg == NULL)
1044 return FAIL;
1045
1046 arg = skipwhite(arg);
1047 if (*arg == ';')
1048 {
1049 // Put the rest of the list (may be empty) in the var after ';'.
1050 // Create a new list for this.
1051 l = list_alloc();
1052 if (l == NULL)
1053 return FAIL;
1054 while (item != NULL)
1055 {
1056 list_append_tv(l, &item->li_tv);
1057 item = item->li_next;
1058 }
1059
1060 ltv.v_type = VAR_LIST;
1061 ltv.v_lock = 0;
1062 ltv.vval.v_list = l;
1063 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001064 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001065
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001066 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1067 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001068 clear_tv(&ltv);
1069 if (arg == NULL)
1070 return FAIL;
1071 break;
1072 }
1073 else if (*arg != ',' && *arg != ']')
1074 {
1075 internal_error("ex_let_vars()");
1076 return FAIL;
1077 }
1078 }
1079
1080 return OK;
1081}
1082
1083/*
1084 * Skip over assignable variable "var" or list of variables "[var, var]".
1085 * Used for ":let varvar = expr" and ":for varvar in expr".
1086 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001087 * for "[var, var; var]" set "semicolon" to 1.
1088 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001089 * Return NULL for an error.
1090 */
1091 char_u *
1092skip_var_list(
1093 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001095 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001096 int *semicolon,
1097 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001098{
1099 char_u *p, *s;
1100
1101 if (*arg == '[')
1102 {
1103 // "[var, var]": find the matching ']'.
1104 p = arg;
1105 for (;;)
1106 {
1107 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001108 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001109 if (s == p)
1110 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001111 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001112 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001113 return NULL;
1114 }
1115 ++*var_count;
1116
1117 p = skipwhite(s);
1118 if (*p == ']')
1119 break;
1120 else if (*p == ';')
1121 {
1122 if (*semicolon == 1)
1123 {
Bram Moolenaar8b716f52022-02-15 21:17:56 +00001124 if (!silent)
1125 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001126 return NULL;
1127 }
1128 *semicolon = 1;
1129 }
1130 else if (*p != ',')
1131 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001132 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001133 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001134 return NULL;
1135 }
1136 }
1137 return p + 1;
1138 }
1139 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001140 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001141}
1142
1143/*
1144 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1145 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001146 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001147 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001148 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001150{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001151 char_u *end;
1152 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001153
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001154 if (*arg == '@' && arg[1] != NUL)
1155 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001156 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001157 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001158
1159 // "a: type" is declaring variable "a" with a type, not "a:".
1160 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001161 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001162 --end;
1163
Bram Moolenaar585587d2021-01-17 20:52:13 +01001164 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001165 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001166 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001167 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001168 }
1169 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001170}
1171
1172/*
1173 * List variables for hashtab "ht" with prefix "prefix".
1174 * If "empty" is TRUE also list NULL strings as empty strings.
1175 */
1176 void
1177list_hashtable_vars(
1178 hashtab_T *ht,
1179 char *prefix,
1180 int empty,
1181 int *first)
1182{
1183 hashitem_T *hi;
1184 dictitem_T *di;
1185 int todo;
1186 char_u buf[IOSIZE];
1187
1188 todo = (int)ht->ht_used;
1189 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1190 {
1191 if (!HASHITEM_EMPTY(hi))
1192 {
1193 --todo;
1194 di = HI2DI(hi);
1195
1196 // apply :filter /pat/ to variable name
1197 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1198 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1199 if (message_filtered(buf))
1200 continue;
1201
1202 if (empty || di->di_tv.v_type != VAR_STRING
1203 || di->di_tv.vval.v_string != NULL)
1204 list_one_var(di, prefix, first);
1205 }
1206 }
1207}
1208
1209/*
1210 * List global variables.
1211 */
1212 static void
1213list_glob_vars(int *first)
1214{
1215 list_hashtable_vars(&globvarht, "", TRUE, first);
1216}
1217
1218/*
1219 * List buffer variables.
1220 */
1221 static void
1222list_buf_vars(int *first)
1223{
1224 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1225}
1226
1227/*
1228 * List window variables.
1229 */
1230 static void
1231list_win_vars(int *first)
1232{
1233 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1234}
1235
1236/*
1237 * List tab page variables.
1238 */
1239 static void
1240list_tab_vars(int *first)
1241{
1242 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1243}
1244
1245/*
1246 * List variables in "arg".
1247 */
1248 static char_u *
1249list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1250{
1251 int error = FALSE;
1252 int len;
1253 char_u *name;
1254 char_u *name_start;
1255 char_u *arg_subsc;
1256 char_u *tofree;
1257 typval_T tv;
1258
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001259 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001260 {
1261 if (error || eap->skip)
1262 {
1263 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1264 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1265 {
1266 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001267 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001268 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001269 break;
1270 }
1271 }
1272 else
1273 {
1274 // get_name_len() takes care of expanding curly braces
1275 name_start = name = arg;
1276 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1277 if (len <= 0)
1278 {
1279 // This is mainly to keep test 49 working: when expanding
1280 // curly braces fails overrule the exception error message.
1281 if (len < 0 && !aborting())
1282 {
1283 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001284 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001285 break;
1286 }
1287 error = TRUE;
1288 }
1289 else
1290 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001291 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001292 if (tofree != NULL)
1293 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001294 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001295 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001296 error = TRUE;
1297 else
1298 {
1299 // handle d.key, l[idx], f(expr)
1300 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001301 if (handle_subscript(&arg, name_start, &tv,
1302 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001303 error = TRUE;
1304 else
1305 {
1306 if (arg == arg_subsc && len == 2 && name[1] == ':')
1307 {
1308 switch (*name)
1309 {
1310 case 'g': list_glob_vars(first); break;
1311 case 'b': list_buf_vars(first); break;
1312 case 'w': list_win_vars(first); break;
1313 case 't': list_tab_vars(first); break;
1314 case 'v': list_vim_vars(first); break;
1315 case 's': list_script_vars(first); break;
1316 case 'l': list_func_vars(first); break;
1317 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001318 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001319 }
1320 }
1321 else
1322 {
1323 char_u numbuf[NUMBUFLEN];
1324 char_u *tf;
1325 int c;
1326 char_u *s;
1327
1328 s = echo_string(&tv, &tf, numbuf, 0);
1329 c = *arg;
1330 *arg = NUL;
1331 list_one_var_a("",
1332 arg == arg_subsc ? name : name_start,
1333 tv.v_type,
1334 s == NULL ? (char_u *)"" : s,
1335 first);
1336 *arg = c;
1337 vim_free(tf);
1338 }
1339 clear_tv(&tv);
1340 }
1341 }
1342 }
1343
1344 vim_free(tofree);
1345 }
1346
1347 arg = skipwhite(arg);
1348 }
1349
1350 return arg;
1351}
1352
1353/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001354 * Set an environment variable, part of ex_let_one().
1355 */
1356 static char_u *
1357ex_let_env(
1358 char_u *arg,
1359 typval_T *tv,
1360 int flags,
1361 char_u *endchars,
1362 char_u *op)
1363{
1364 char_u *arg_end = NULL;
1365 char_u *name;
1366 int len;
1367
1368 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1369 && (flags & ASSIGN_FOR_LOOP) == 0)
1370 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001371 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001372 return NULL;
1373 }
1374
1375 // Find the end of the name.
1376 ++arg;
1377 name = arg;
1378 len = get_env_len(&arg);
1379 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001380 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001381 else
1382 {
1383 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001384 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001385 else if (endchars != NULL
1386 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1387 emsg(_(e_unexpected_characters_in_let));
1388 else if (!check_secure())
1389 {
1390 char_u *tofree = NULL;
1391 int c1 = name[len];
1392 char_u *p;
1393
1394 name[len] = NUL;
1395 p = tv_get_string_chk(tv);
1396 if (p != NULL && op != NULL && *op == '.')
1397 {
1398 int mustfree = FALSE;
1399 char_u *s = vim_getenv(name, &mustfree);
1400
1401 if (s != NULL)
1402 {
1403 p = tofree = concat_str(s, p);
1404 if (mustfree)
1405 vim_free(s);
1406 }
1407 }
1408 if (p != NULL)
1409 {
1410 vim_setenv_ext(name, p);
1411 arg_end = arg;
1412 }
1413 name[len] = c1;
1414 vim_free(tofree);
1415 }
1416 }
1417 return arg_end;
1418}
1419
1420/*
1421 * Set an option, part of ex_let_one().
1422 */
1423 static char_u *
1424ex_let_option(
1425 char_u *arg,
1426 typval_T *tv,
1427 int flags,
1428 char_u *endchars,
1429 char_u *op)
1430{
1431 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001432 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001433 char_u *arg_end = NULL;
1434
1435 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1436 && (flags & ASSIGN_FOR_LOOP) == 0)
1437 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001438 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001439 return NULL;
1440 }
1441
1442 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001443 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001444 if (p == NULL || (endchars != NULL
1445 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1446 emsg(_(e_unexpected_characters_in_let));
1447 else
1448 {
1449 int c1;
1450 long n = 0;
1451 getoption_T opt_type;
1452 long numval;
1453 char_u *stringval = NULL;
1454 char_u *s = NULL;
1455 int failed = FALSE;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001456 int opt_p_flags;
1457 char_u *tofree = NULL;
Bram Moolenaar92c33eb2021-12-07 11:03:39 +00001458 char_u numbuf[NUMBUFLEN];
1459
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001460 c1 = *p;
1461 *p = NUL;
1462
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001463 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags,
1464 scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001465 if ((opt_type == gov_bool
1466 || opt_type == gov_number
1467 || opt_type == gov_hidden_bool
1468 || opt_type == gov_hidden_number)
1469 && (tv->v_type != VAR_STRING || !in_vim9script()))
1470 {
1471 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1472 // bool, possibly hidden
1473 n = (long)tv_get_bool(tv);
1474 else
1475 // number, possibly hidden
1476 n = (long)tv_get_number(tv);
1477 }
1478
Bram Moolenaaref082e12021-12-12 21:02:03 +00001479 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001480 || tv->v_type == VAR_FUNC))
1481 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001482 // If the option can be set to a function reference or a lambda
1483 // and the passed value is a function reference, then convert it to
1484 // the name (string) of the function reference.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001485 s = tv2string(tv, &tofree, numbuf, 0);
1486 }
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001487 // Avoid setting a string option to the text "v:false" or similar.
1488 // In Vim9 script also don't convert a number to string.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001489 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001490 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1491 s = tv_get_string_chk(tv);
1492
1493 if (op != NULL && *op != '=')
1494 {
1495 if (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1496 || (opt_type == gov_string && *op != '.'))
1497 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001498 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001499 failed = TRUE; // don't set the value
1500
1501 }
1502 else
1503 {
1504 // number, in legacy script also bool
1505 if (opt_type == gov_number
1506 || (opt_type == gov_bool && !in_vim9script()))
1507 {
1508 switch (*op)
1509 {
1510 case '+': n = numval + n; break;
1511 case '-': n = numval - n; break;
1512 case '*': n = numval * n; break;
1513 case '/': n = (long)num_divide(numval, n,
1514 &failed); break;
1515 case '%': n = (long)num_modulus(numval, n,
1516 &failed); break;
1517 }
1518 s = NULL;
1519 }
1520 else if (opt_type == gov_string
1521 && stringval != NULL && s != NULL)
1522 {
1523 // string
1524 s = concat_str(stringval, s);
1525 vim_free(stringval);
1526 stringval = s;
1527 }
1528 }
1529 }
1530
1531 if (!failed)
1532 {
1533 if (opt_type != gov_string || s != NULL)
1534 {
Bram Moolenaar8ccbbeb2022-03-02 19:49:38 +00001535 char *err = set_option_value(arg, n, s, scope);
1536
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001537 arg_end = p;
Bram Moolenaar8ccbbeb2022-03-02 19:49:38 +00001538 if (err != NULL)
1539 emsg(_(err));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001540 }
1541 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001542 emsg(_(e_string_required));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001543 }
1544 *p = c1;
1545 vim_free(stringval);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001546 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001547 }
1548 return arg_end;
1549}
1550
1551/*
1552 * Set a register, part of ex_let_one().
1553 */
1554 static char_u *
1555ex_let_register(
1556 char_u *arg,
1557 typval_T *tv,
1558 int flags,
1559 char_u *endchars,
1560 char_u *op)
1561{
1562 char_u *arg_end = NULL;
1563
1564 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1565 && (flags & ASSIGN_FOR_LOOP) == 0)
1566 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001567 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001568 return NULL;
1569 }
1570 ++arg;
1571 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001572 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001573 else if (endchars != NULL
1574 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1575 emsg(_(e_unexpected_characters_in_let));
1576 else
1577 {
1578 char_u *ptofree = NULL;
1579 char_u *p;
1580
1581 p = tv_get_string_chk(tv);
1582 if (p != NULL && op != NULL && *op == '.')
1583 {
1584 char_u *s = get_reg_contents(*arg == '@'
1585 ? '"' : *arg, GREG_EXPR_SRC);
1586
1587 if (s != NULL)
1588 {
1589 p = ptofree = concat_str(s, p);
1590 vim_free(s);
1591 }
1592 }
1593 if (p != NULL)
1594 {
1595 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1596 arg_end = arg + 1;
1597 }
1598 vim_free(ptofree);
1599 }
1600 return arg_end;
1601}
1602
1603/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001604 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1605 * Returns a pointer to the char just after the var name.
1606 * Returns NULL if there is an error.
1607 */
1608 static char_u *
1609ex_let_one(
1610 char_u *arg, // points to variable name
1611 typval_T *tv, // value to assign to variable
1612 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001613 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001614 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001615 char_u *op, // "+", "-", "." or NULL
1616 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001617{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001618 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001619
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001620 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001621 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001622 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1623 {
1624 vim9_declare_error(arg);
1625 return NULL;
1626 }
1627
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001628 if (*arg == '$')
1629 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001630 // ":let $VAR = expr": Set environment variable.
1631 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001632 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001633 else if (*arg == '&')
1634 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001635 // ":let &option = expr": Set option value.
1636 // ":let &l:option = expr": Set local option value.
1637 // ":let &g:option = expr": Set global option value.
1638 // ":for &ts in range(8)": Set option value for for loop
1639 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001640 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001641 else if (*arg == '@')
1642 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001643 // ":let @r = expr": Set register contents.
1644 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001645 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001646 else if (eval_isnamec1(*arg) || *arg == '{')
1647 {
1648 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001649 char_u *p;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001650
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001651 // ":let var = expr": Set internal variable.
1652 // ":let var: type = expr": Set internal variable with type.
1653 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001654 p = get_lval(arg, tv, &lv, FALSE, FALSE,
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001655 (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1656 ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001657 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001658 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001659 if (endchars != NULL && vim_strchr(endchars,
1660 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001661 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001662 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001663 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001664 else
1665 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001666 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001667 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001668 }
1669 }
1670 clear_lval(&lv);
1671 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001672 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001673 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001674
1675 return arg_end;
1676}
1677
1678/*
1679 * ":unlet[!] var1 ... " command.
1680 */
1681 void
1682ex_unlet(exarg_T *eap)
1683{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001684 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001685}
1686
1687/*
1688 * ":lockvar" and ":unlockvar" commands
1689 */
1690 void
1691ex_lockvar(exarg_T *eap)
1692{
1693 char_u *arg = eap->arg;
1694 int deep = 2;
1695
1696 if (eap->forceit)
1697 deep = -1;
1698 else if (vim_isdigit(*arg))
1699 {
1700 deep = getdigits(&arg);
1701 arg = skipwhite(arg);
1702 }
1703
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001704 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001705}
1706
1707/*
1708 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001709 * Also used for Vim9 script. "callback" is invoked as:
1710 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001711 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001712 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001713ex_unletlock(
1714 exarg_T *eap,
1715 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001716 int deep,
1717 int glv_flags,
1718 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1719 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001720{
1721 char_u *arg = argstart;
1722 char_u *name_end;
1723 int error = FALSE;
1724 lval_T lv;
1725
1726 do
1727 {
1728 if (*arg == '$')
1729 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001730 lv.ll_name = arg;
1731 lv.ll_tv = NULL;
1732 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001733 if (get_env_len(&arg) == 0)
1734 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001735 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001736 return;
1737 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001738 if (!error && !eap->skip
1739 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1740 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001741 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001742 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001743 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001744 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001745 // Parse the name and find the end.
1746 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001747 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001748 if (lv.ll_name == NULL)
1749 error = TRUE; // error but continue parsing
1750 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1751 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001752 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001753 if (name_end != NULL)
1754 {
1755 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00001756 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001757 }
1758 if (!(eap->skip || error))
1759 clear_lval(&lv);
1760 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001761 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001762
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001763 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001764 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001765 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001766
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001767 if (!eap->skip)
1768 clear_lval(&lv);
1769 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001770
1771 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001772 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001773
Bram Moolenaar63b91732021-08-05 20:40:03 +02001774 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001775}
1776
1777 static int
1778do_unlet_var(
1779 lval_T *lp,
1780 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001781 exarg_T *eap,
1782 int deep UNUSED,
1783 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001784{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001785 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001786 int ret = OK;
1787 int cc;
1788
1789 if (lp->ll_tv == NULL)
1790 {
1791 cc = *name_end;
1792 *name_end = NUL;
1793
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001794 // Environment variable, normal name or expanded name.
1795 if (*lp->ll_name == '$')
1796 vim_unsetenv(lp->ll_name + 1);
1797 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001798 ret = FAIL;
1799 *name_end = cc;
1800 }
1801 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001802 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001803 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001804 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001805 return FAIL;
1806 else if (lp->ll_range)
1807 {
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001808 if (list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_name, lp->ll_n1,
1809 !lp->ll_empty2, lp->ll_n2) == FAIL)
1810 return FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001811 }
1812 else
1813 {
1814 if (lp->ll_list != NULL)
1815 // unlet a List item.
1816 listitem_remove(lp->ll_list, lp->ll_li);
1817 else
1818 // unlet a Dictionary item.
1819 dictitem_remove(lp->ll_dict, lp->ll_di);
1820 }
1821
1822 return ret;
1823}
1824
1825/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001826 * Unlet one item or a range of items from a list.
1827 * Return OK or FAIL.
1828 */
1829 int
1830list_unlet_range(
1831 list_T *l,
1832 listitem_T *li_first,
1833 char_u *name,
1834 long n1_arg,
1835 int has_n2,
1836 long n2)
1837{
1838 listitem_T *li = li_first;
1839 int n1 = n1_arg;
1840
1841 while (li != NULL && (!has_n2 || n2 >= n1))
1842 {
1843 if (value_check_lock(li->li_tv.v_lock, name, FALSE))
1844 return FAIL;
1845 li = li->li_next;
1846 ++n1;
1847 }
1848
1849 // Delete a range of List items.
1850 li = li_first;
1851 n1 = n1_arg;
1852 while (li != NULL && (!has_n2 || n2 >= n1))
1853 {
1854 listitem_T *next = li->li_next;
1855
1856 listitem_remove(l, li);
1857 li = next;
1858 ++n1;
1859 }
1860 return OK;
1861}
1862/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001863 * "unlet" a variable. Return OK if it existed, FAIL if not.
1864 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1865 */
1866 int
1867do_unlet(char_u *name, int forceit)
1868{
1869 hashtab_T *ht;
1870 hashitem_T *hi;
1871 char_u *varname;
1872 dict_T *d;
1873 dictitem_T *di;
1874
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001875 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001876 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001877 return FAIL;
1878
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001879 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001880
1881 // can't :unlet a script variable in Vim9 script from a function
1882 if (ht == get_script_local_ht()
1883 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1884 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1885 == SCRIPT_VERSION_VIM9
1886 && check_vim9_unlet(name) == FAIL)
1887 return FAIL;
1888
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001889 if (ht != NULL && *varname != NUL)
1890 {
1891 d = get_current_funccal_dict(ht);
1892 if (d == NULL)
1893 {
1894 if (ht == &globvarht)
1895 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001896 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001897 d = &vimvardict;
1898 else
1899 {
1900 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1901 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1902 }
1903 if (d == NULL)
1904 {
1905 internal_error("do_unlet()");
1906 return FAIL;
1907 }
1908 }
1909 hi = hash_find(ht, varname);
1910 if (HASHITEM_EMPTY(hi))
1911 hi = find_hi_in_scoped_ht(name, &ht);
1912 if (hi != NULL && !HASHITEM_EMPTY(hi))
1913 {
1914 di = HI2DI(hi);
1915 if (var_check_fixed(di->di_flags, name, FALSE)
1916 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001917 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001918 return FAIL;
1919
1920 delete_var(ht, hi);
1921 return OK;
1922 }
1923 }
1924 if (forceit)
1925 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00001926 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001927 return FAIL;
1928}
1929
1930/*
1931 * Lock or unlock variable indicated by "lp".
1932 * "deep" is the levels to go (-1 for unlimited);
1933 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1934 */
1935 static int
1936do_lock_var(
1937 lval_T *lp,
1938 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001939 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001940 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001941 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001942{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001943 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001944 int ret = OK;
1945 int cc;
1946 dictitem_T *di;
1947
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001948 if (lp->ll_tv == NULL)
1949 {
1950 cc = *name_end;
1951 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001952 if (*lp->ll_name == '$')
1953 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001954 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001955 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001956 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001957 else
1958 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001959 // Normal name or expanded name.
1960 di = find_var(lp->ll_name, NULL, TRUE);
1961 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001962 {
1963 if (in_vim9script())
1964 semsg(_(e_cannot_find_variable_to_unlock_str),
1965 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001966 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001967 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001968 else if ((di->di_flags & DI_FLAGS_FIX)
1969 && di->di_tv.v_type != VAR_DICT
1970 && di->di_tv.v_type != VAR_LIST)
1971 {
1972 // For historic reasons this error is not given for a list or
1973 // dict. E.g., the b: dict could be locked/unlocked.
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001974 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001975 ret = FAIL;
1976 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001977 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001978 {
1979 if (lock)
1980 di->di_flags |= DI_FLAGS_LOCK;
1981 else
1982 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001983 if (deep != 0)
1984 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001985 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001986 }
1987 *name_end = cc;
1988 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001989 else if (deep == 0)
1990 {
1991 // nothing to do
1992 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001993 else if (lp->ll_range)
1994 {
1995 listitem_T *li = lp->ll_li;
1996
1997 // (un)lock a range of List items.
1998 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1999 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02002000 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002001 li = li->li_next;
2002 ++lp->ll_n1;
2003 }
2004 }
2005 else if (lp->ll_list != NULL)
2006 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002007 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002008 else
2009 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002010 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002011
2012 return ret;
2013}
2014
2015/*
2016 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002017 * When "check_refcount" is TRUE do not lock a list or dict with a reference
2018 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002019 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002020 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02002021item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002022{
2023 static int recurse = 0;
2024 list_T *l;
2025 listitem_T *li;
2026 dict_T *d;
2027 blob_T *b;
2028 hashitem_T *hi;
2029 int todo;
2030
2031 if (recurse >= DICT_MAXNEST)
2032 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002033 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002034 return;
2035 }
2036 if (deep == 0)
2037 return;
2038 ++recurse;
2039
2040 // lock/unlock the item itself
2041 if (lock)
2042 tv->v_lock |= VAR_LOCKED;
2043 else
2044 tv->v_lock &= ~VAR_LOCKED;
2045
2046 switch (tv->v_type)
2047 {
2048 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002049 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002051 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002052 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002053 case VAR_STRING:
2054 case VAR_FUNC:
2055 case VAR_PARTIAL:
2056 case VAR_FLOAT:
2057 case VAR_SPECIAL:
2058 case VAR_JOB:
2059 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002060 case VAR_INSTR:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002061 break;
2062
2063 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002064 if ((b = tv->vval.v_blob) != NULL
2065 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002066 {
2067 if (lock)
2068 b->bv_lock |= VAR_LOCKED;
2069 else
2070 b->bv_lock &= ~VAR_LOCKED;
2071 }
2072 break;
2073 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002074 if ((l = tv->vval.v_list) != NULL
2075 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002076 {
2077 if (lock)
2078 l->lv_lock |= VAR_LOCKED;
2079 else
2080 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002081 if (deep < 0 || deep > 1)
2082 {
2083 if (l->lv_first == &range_list_item)
2084 l->lv_lock |= VAR_ITEMS_LOCKED;
2085 else
2086 {
2087 // recursive: lock/unlock the items the List contains
2088 CHECK_LIST_MATERIALIZE(l);
2089 FOR_ALL_LIST_ITEMS(l, li) item_lock(&li->li_tv,
2090 deep - 1, lock, check_refcount);
2091 }
2092 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002093 }
2094 break;
2095 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002096 if ((d = tv->vval.v_dict) != NULL
2097 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002098 {
2099 if (lock)
2100 d->dv_lock |= VAR_LOCKED;
2101 else
2102 d->dv_lock &= ~VAR_LOCKED;
2103 if (deep < 0 || deep > 1)
2104 {
2105 // recursive: lock/unlock the items the List contains
2106 todo = (int)d->dv_hashtab.ht_used;
2107 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2108 {
2109 if (!HASHITEM_EMPTY(hi))
2110 {
2111 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002112 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2113 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002114 }
2115 }
2116 }
2117 }
2118 }
2119 --recurse;
2120}
2121
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002122#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2123/*
2124 * Delete all "menutrans_" variables.
2125 */
2126 void
2127del_menutrans_vars(void)
2128{
2129 hashitem_T *hi;
2130 int todo;
2131
2132 hash_lock(&globvarht);
2133 todo = (int)globvarht.ht_used;
2134 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2135 {
2136 if (!HASHITEM_EMPTY(hi))
2137 {
2138 --todo;
2139 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2140 delete_var(&globvarht, hi);
2141 }
2142 }
2143 hash_unlock(&globvarht);
2144}
2145#endif
2146
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002147/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002148 * Local string buffer for the next two functions to store a variable name
2149 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2150 * get_user_var_name().
2151 */
2152
2153static char_u *varnamebuf = NULL;
2154static int varnamebuflen = 0;
2155
2156/*
2157 * Function to concatenate a prefix and a variable name.
2158 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002159 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002160cat_prefix_varname(int prefix, char_u *name)
2161{
2162 int len;
2163
2164 len = (int)STRLEN(name) + 3;
2165 if (len > varnamebuflen)
2166 {
2167 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002168 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002169 varnamebuf = alloc(len);
2170 if (varnamebuf == NULL)
2171 {
2172 varnamebuflen = 0;
2173 return NULL;
2174 }
2175 varnamebuflen = len;
2176 }
2177 *varnamebuf = prefix;
2178 varnamebuf[1] = ':';
2179 STRCPY(varnamebuf + 2, name);
2180 return varnamebuf;
2181}
2182
2183/*
2184 * Function given to ExpandGeneric() to obtain the list of user defined
2185 * (global/buffer/window/built-in) variable names.
2186 */
2187 char_u *
2188get_user_var_name(expand_T *xp, int idx)
2189{
2190 static long_u gdone;
2191 static long_u bdone;
2192 static long_u wdone;
2193 static long_u tdone;
2194 static int vidx;
2195 static hashitem_T *hi;
2196 hashtab_T *ht;
2197
2198 if (idx == 0)
2199 {
2200 gdone = bdone = wdone = vidx = 0;
2201 tdone = 0;
2202 }
2203
2204 // Global variables
2205 if (gdone < globvarht.ht_used)
2206 {
2207 if (gdone++ == 0)
2208 hi = globvarht.ht_array;
2209 else
2210 ++hi;
2211 while (HASHITEM_EMPTY(hi))
2212 ++hi;
2213 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2214 return cat_prefix_varname('g', hi->hi_key);
2215 return hi->hi_key;
2216 }
2217
2218 // b: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002219 ht = &prevwin_curwin()->w_buffer->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002220 if (bdone < ht->ht_used)
2221 {
2222 if (bdone++ == 0)
2223 hi = ht->ht_array;
2224 else
2225 ++hi;
2226 while (HASHITEM_EMPTY(hi))
2227 ++hi;
2228 return cat_prefix_varname('b', hi->hi_key);
2229 }
2230
2231 // w: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002232 ht = &prevwin_curwin()->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002233 if (wdone < ht->ht_used)
2234 {
2235 if (wdone++ == 0)
2236 hi = ht->ht_array;
2237 else
2238 ++hi;
2239 while (HASHITEM_EMPTY(hi))
2240 ++hi;
2241 return cat_prefix_varname('w', hi->hi_key);
2242 }
2243
2244 // t: variables
2245 ht = &curtab->tp_vars->dv_hashtab;
2246 if (tdone < ht->ht_used)
2247 {
2248 if (tdone++ == 0)
2249 hi = ht->ht_array;
2250 else
2251 ++hi;
2252 while (HASHITEM_EMPTY(hi))
2253 ++hi;
2254 return cat_prefix_varname('t', hi->hi_key);
2255 }
2256
2257 // v: variables
2258 if (vidx < VV_LEN)
2259 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2260
2261 VIM_CLEAR(varnamebuf);
2262 varnamebuflen = 0;
2263 return NULL;
2264}
2265
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002266 char *
2267get_var_special_name(int nr)
2268{
2269 switch (nr)
2270 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002271 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2272 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002273 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002274 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002275 }
2276 internal_error("get_var_special_name()");
2277 return "42";
2278}
2279
2280/*
2281 * Returns the global variable dictionary
2282 */
2283 dict_T *
2284get_globvar_dict(void)
2285{
2286 return &globvardict;
2287}
2288
2289/*
2290 * Returns the global variable hash table
2291 */
2292 hashtab_T *
2293get_globvar_ht(void)
2294{
2295 return &globvarht;
2296}
2297
2298/*
2299 * Returns the v: variable dictionary
2300 */
2301 dict_T *
2302get_vimvar_dict(void)
2303{
2304 return &vimvardict;
2305}
2306
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002307/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002309 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002310 */
2311 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002312find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002314 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2315 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316
2317 if (di == NULL)
2318 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002319 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2321 return (int)(vv - vimvars);
2322}
2323
2324
2325/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002326 * Set type of v: variable to "type".
2327 */
2328 void
2329set_vim_var_type(int idx, vartype_T type)
2330{
Bram Moolenaard787e402021-12-24 21:36:12 +00002331 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002332}
2333
2334/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002335 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002336 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002337 */
2338 void
2339set_vim_var_nr(int idx, varnumber_T val)
2340{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002341 vimvars[idx].vv_nr = val;
2342}
2343
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344 char *
2345get_vim_var_name(int idx)
2346{
2347 return vimvars[idx].vv_name;
2348}
2349
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002350/*
2351 * Get typval_T v: variable value.
2352 */
2353 typval_T *
2354get_vim_var_tv(int idx)
2355{
2356 return &vimvars[idx].vv_tv;
2357}
2358
Bram Moolenaard787e402021-12-24 21:36:12 +00002359 type_T *
2360get_vim_var_type(int idx, garray_T *type_list)
2361{
2362 if (vimvars[idx].vv_type != NULL)
2363 return vimvars[idx].vv_type;
2364 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2365}
2366
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002367/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002368 * Set v: variable to "tv". Only accepts the same type.
2369 * Takes over the value of "tv".
2370 */
2371 int
2372set_vim_var_tv(int idx, typval_T *tv)
2373{
Bram Moolenaard787e402021-12-24 21:36:12 +00002374 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002375 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002376 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002377 clear_tv(tv);
2378 return FAIL;
2379 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002380 // VV_RO is also checked when compiling, but let's check here as well.
2381 if (vimvars[idx].vv_flags & VV_RO)
2382 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002383 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002384 return FAIL;
2385 }
2386 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2387 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002388 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002389 return FAIL;
2390 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002391 clear_tv(&vimvars[idx].vv_di.di_tv);
2392 vimvars[idx].vv_di.di_tv = *tv;
2393 return OK;
2394}
2395
2396/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002397 * Get number v: variable value.
2398 */
2399 varnumber_T
2400get_vim_var_nr(int idx)
2401{
2402 return vimvars[idx].vv_nr;
2403}
2404
2405/*
2406 * Get string v: variable value. Uses a static buffer, can only be used once.
2407 * If the String variable has never been set, return an empty string.
2408 * Never returns NULL;
2409 */
2410 char_u *
2411get_vim_var_str(int idx)
2412{
2413 return tv_get_string(&vimvars[idx].vv_tv);
2414}
2415
2416/*
2417 * Get List v: variable value. Caller must take care of reference count when
2418 * needed.
2419 */
2420 list_T *
2421get_vim_var_list(int idx)
2422{
2423 return vimvars[idx].vv_list;
2424}
2425
2426/*
2427 * Get Dict v: variable value. Caller must take care of reference count when
2428 * needed.
2429 */
2430 dict_T *
2431get_vim_var_dict(int idx)
2432{
2433 return vimvars[idx].vv_dict;
2434}
2435
2436/*
2437 * Set v:char to character "c".
2438 */
2439 void
2440set_vim_var_char(int c)
2441{
2442 char_u buf[MB_MAXBYTES + 1];
2443
2444 if (has_mbyte)
2445 buf[(*mb_char2bytes)(c, buf)] = NUL;
2446 else
2447 {
2448 buf[0] = c;
2449 buf[1] = NUL;
2450 }
2451 set_vim_var_string(VV_CHAR, buf, -1);
2452}
2453
2454/*
2455 * Set v:count to "count" and v:count1 to "count1".
2456 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2457 */
2458 void
2459set_vcount(
2460 long count,
2461 long count1,
2462 int set_prevcount)
2463{
2464 if (set_prevcount)
2465 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2466 vimvars[VV_COUNT].vv_nr = count;
2467 vimvars[VV_COUNT1].vv_nr = count1;
2468}
2469
2470/*
2471 * Save variables that might be changed as a side effect. Used when executing
2472 * a timer callback.
2473 */
2474 void
2475save_vimvars(vimvars_save_T *vvsave)
2476{
2477 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2478 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2479 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2480}
2481
2482/*
2483 * Restore variables saved by save_vimvars().
2484 */
2485 void
2486restore_vimvars(vimvars_save_T *vvsave)
2487{
2488 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2489 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2490 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2491}
2492
2493/*
2494 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2495 * value.
2496 */
2497 void
2498set_vim_var_string(
2499 int idx,
2500 char_u *val,
2501 int len) // length of "val" to use or -1 (whole string)
2502{
2503 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002504 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002505 if (val == NULL)
2506 vimvars[idx].vv_str = NULL;
2507 else if (len == -1)
2508 vimvars[idx].vv_str = vim_strsave(val);
2509 else
2510 vimvars[idx].vv_str = vim_strnsave(val, len);
2511}
2512
2513/*
2514 * Set List v: variable to "val".
2515 */
2516 void
2517set_vim_var_list(int idx, list_T *val)
2518{
2519 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002520 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002521 vimvars[idx].vv_list = val;
2522 if (val != NULL)
2523 ++val->lv_refcount;
2524}
2525
2526/*
2527 * Set Dictionary v: variable to "val".
2528 */
2529 void
2530set_vim_var_dict(int idx, dict_T *val)
2531{
2532 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002533 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002534 vimvars[idx].vv_dict = val;
2535 if (val != NULL)
2536 {
2537 ++val->dv_refcount;
2538 dict_set_items_ro(val);
2539 }
2540}
2541
2542/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002543 * Set the v:argv list.
2544 */
2545 void
2546set_argv_var(char **argv, int argc)
2547{
2548 list_T *l = list_alloc();
2549 int i;
2550
2551 if (l == NULL)
2552 getout(1);
2553 l->lv_lock = VAR_FIXED;
2554 for (i = 0; i < argc; ++i)
2555 {
2556 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2557 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002558 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002559 }
2560 set_vim_var_list(VV_ARGV, l);
2561}
2562
2563/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002564 * Reset v:register, taking the 'clipboard' setting into account.
2565 */
2566 void
2567reset_reg_var(void)
2568{
2569 int regname = 0;
2570
2571 // Adjust the register according to 'clipboard', so that when
2572 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2573#ifdef FEAT_CLIPBOARD
2574 adjust_clip_reg(&regname);
2575#endif
2576 set_reg_var(regname);
2577}
2578
2579/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002580 * Set v:register if needed.
2581 */
2582 void
2583set_reg_var(int c)
2584{
2585 char_u regname;
2586
2587 if (c == 0 || c == ' ')
2588 regname = '"';
2589 else
2590 regname = c;
2591 // Avoid free/alloc when the value is already right.
2592 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2593 set_vim_var_string(VV_REG, &regname, 1);
2594}
2595
2596/*
2597 * Get or set v:exception. If "oldval" == NULL, return the current value.
2598 * Otherwise, restore the value to "oldval" and return NULL.
2599 * Must always be called in pairs to save and restore v:exception! Does not
2600 * take care of memory allocations.
2601 */
2602 char_u *
2603v_exception(char_u *oldval)
2604{
2605 if (oldval == NULL)
2606 return vimvars[VV_EXCEPTION].vv_str;
2607
2608 vimvars[VV_EXCEPTION].vv_str = oldval;
2609 return NULL;
2610}
2611
2612/*
2613 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2614 * Otherwise, restore the value to "oldval" and return NULL.
2615 * Must always be called in pairs to save and restore v:throwpoint! Does not
2616 * take care of memory allocations.
2617 */
2618 char_u *
2619v_throwpoint(char_u *oldval)
2620{
2621 if (oldval == NULL)
2622 return vimvars[VV_THROWPOINT].vv_str;
2623
2624 vimvars[VV_THROWPOINT].vv_str = oldval;
2625 return NULL;
2626}
2627
2628/*
2629 * Set v:cmdarg.
2630 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2631 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2632 * Must always be called in pairs!
2633 */
2634 char_u *
2635set_cmdarg(exarg_T *eap, char_u *oldarg)
2636{
2637 char_u *oldval;
2638 char_u *newval;
2639 unsigned len;
2640
2641 oldval = vimvars[VV_CMDARG].vv_str;
2642 if (eap == NULL)
2643 {
2644 vim_free(oldval);
2645 vimvars[VV_CMDARG].vv_str = oldarg;
2646 return NULL;
2647 }
2648
2649 if (eap->force_bin == FORCE_BIN)
2650 len = 6;
2651 else if (eap->force_bin == FORCE_NOBIN)
2652 len = 8;
2653 else
2654 len = 0;
2655
2656 if (eap->read_edit)
2657 len += 7;
2658
2659 if (eap->force_ff != 0)
2660 len += 10; // " ++ff=unix"
2661 if (eap->force_enc != 0)
2662 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2663 if (eap->bad_char != 0)
2664 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2665
2666 newval = alloc(len + 1);
2667 if (newval == NULL)
2668 return NULL;
2669
2670 if (eap->force_bin == FORCE_BIN)
2671 sprintf((char *)newval, " ++bin");
2672 else if (eap->force_bin == FORCE_NOBIN)
2673 sprintf((char *)newval, " ++nobin");
2674 else
2675 *newval = NUL;
2676
2677 if (eap->read_edit)
2678 STRCAT(newval, " ++edit");
2679
2680 if (eap->force_ff != 0)
2681 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2682 eap->force_ff == 'u' ? "unix"
2683 : eap->force_ff == 'd' ? "dos"
2684 : "mac");
2685 if (eap->force_enc != 0)
2686 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2687 eap->cmd + eap->force_enc);
2688 if (eap->bad_char == BAD_KEEP)
2689 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2690 else if (eap->bad_char == BAD_DROP)
2691 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2692 else if (eap->bad_char != 0)
2693 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2694 vimvars[VV_CMDARG].vv_str = newval;
2695 return oldval;
2696}
2697
2698/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002699 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002700 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2701 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002702 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2703 */
2704 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002705eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002706 char_u *name,
2707 int len, // length of "name"
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002708 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002709 typval_T *rettv, // NULL when only checking existence
2710 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002711 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002712{
2713 int ret = OK;
2714 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002715 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002716 hashtab_T *ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002717 int cc;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002718 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002719
2720 // truncate the name, so that we can use strcmp()
2721 cc = name[len];
2722 name[len] = NUL;
2723
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002724 // Check for local variable when debugging.
2725 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002726 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002727 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002728 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2729
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002730 if (v != NULL)
2731 {
2732 tv = &v->di_tv;
2733 if (dip != NULL)
2734 *dip = v;
2735 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002736 else
2737 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002738 }
2739
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002740 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002742 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002743 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2744
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002745 if (sid == 0)
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00002746 import = find_imported(p, 0, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002747
2748 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002749 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002750 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002751 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002752 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00002753 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02002754 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002755 ht = &SCRIPT_VARS(sid);
2756 if (ht != NULL)
2757 {
2758 dictitem_T *v = find_var_in_ht(ht, 0, name,
2759 flags & EVAL_VAR_NOAUTOLOAD);
2760
2761 if (v != NULL)
2762 {
2763 tv = &v->di_tv;
2764 if (dip != NULL)
2765 *dip = v;
2766 }
2767 else
2768 ht = NULL;
2769 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002770 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002771 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002772 {
2773 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00002774 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002775 ret = FAIL;
2776 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002777 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002778 else
2779 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002780 if (rettv != NULL)
2781 {
2782 rettv->v_type = VAR_ANY;
2783 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
2784 }
2785 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002786 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00002788 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002789 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002790 int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002791 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002792
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002793 // In Vim9 script we can get a function reference by using the
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002794 // function name. For a global non-autoload function "g:" is
2795 // required.
2796 if (ufunc != NULL && (has_g_prefix
2797 || !func_requires_g_prefix(ufunc)))
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002798 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002799 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002800 if (rettv != NULL)
2801 {
2802 rettv->v_type = VAR_FUNC;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002803 if (has_g_prefix)
Bram Moolenaaref082e12021-12-12 21:02:03 +00002804 // Keep the "g:", otherwise script-local may be
2805 // assumed.
2806 rettv->vval.v_string = vim_strsave(name);
2807 else
2808 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002809 if (rettv->vval.v_string != NULL)
2810 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002811 }
2812 }
2813 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002814 }
2815
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002816 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002817 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002818 if (tv == NULL)
2819 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002820 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002821 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002822 ret = FAIL;
2823 }
2824 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002825 {
Bram Moolenaar11005b02021-07-11 20:59:00 +02002826 if (ht != NULL && ht == get_script_local_ht()
2827 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002828 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002829 svar_T *sv = find_typval_in_script(tv, 0);
Bram Moolenaarf055d452021-07-08 20:57:24 +02002830
Bram Moolenaarf055d452021-07-08 20:57:24 +02002831 if (sv != NULL)
2832 type = sv->sv_type;
2833 }
2834
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002835 // If a list or dict variable wasn't initialized, do it now.
Bram Moolenaar7a222242022-03-01 19:23:24 +00002836 // Not for global variables, they are not declared.
2837 if (ht != &globvarht)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002838 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00002839 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002840 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00002841 tv->vval.v_dict = dict_alloc();
2842 if (tv->vval.v_dict != NULL)
2843 {
2844 ++tv->vval.v_dict->dv_refcount;
2845 tv->vval.v_dict->dv_type = alloc_type(type);
2846 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02002847 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00002848 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002849 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00002850 tv->vval.v_list = list_alloc();
2851 if (tv->vval.v_list != NULL)
2852 {
2853 ++tv->vval.v_list->lv_refcount;
2854 tv->vval.v_list->lv_type = alloc_type(type);
2855 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02002856 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00002857 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL)
2858 {
2859 tv->vval.v_blob = blob_alloc();
2860 if (tv->vval.v_blob != NULL)
2861 ++tv->vval.v_blob->bv_refcount;
2862 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002863 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002864 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002865 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002866 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002867
2868 name[len] = cc;
2869
2870 return ret;
2871}
2872
2873/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002874 * Check if variable "name[len]" is a local variable or an argument.
2875 * If so, "*eval_lavars_used" is set to TRUE.
2876 */
2877 void
2878check_vars(char_u *name, int len)
2879{
2880 int cc;
2881 char_u *varname;
2882 hashtab_T *ht;
2883
2884 if (eval_lavars_used == NULL)
2885 return;
2886
2887 // truncate the name, so that we can use strcmp()
2888 cc = name[len];
2889 name[len] = NUL;
2890
2891 ht = find_var_ht(name, &varname);
2892 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2893 {
2894 if (find_var(name, NULL, TRUE) != NULL)
2895 *eval_lavars_used = TRUE;
2896 }
2897
2898 name[len] = cc;
2899}
2900
2901/*
2902 * Find variable "name" in the list of variables.
2903 * Return a pointer to it if found, NULL if not found.
2904 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002905 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002906 */
2907 dictitem_T *
2908find_var(char_u *name, hashtab_T **htp, int no_autoload)
2909{
2910 char_u *varname;
2911 hashtab_T *ht;
2912 dictitem_T *ret = NULL;
2913
2914 ht = find_var_ht(name, &varname);
2915 if (htp != NULL)
2916 *htp = ht;
2917 if (ht == NULL)
2918 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002919 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002920 if (ret != NULL)
2921 return ret;
2922
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002923 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002924 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002925 if (ret != NULL)
2926 return ret;
2927
2928 // in Vim9 script items without a scope can be script-local
2929 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2930 {
2931 ht = get_script_local_ht();
2932 if (ht != NULL)
2933 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002934 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002935 if (ret != NULL)
2936 {
2937 if (htp != NULL)
2938 *htp = ht;
2939 return ret;
2940 }
2941 }
2942 }
2943
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002944 // When using "vim9script autoload" script-local items are prefixed but can
2945 // be used with s:name.
2946 if (SCRIPT_ID_VALID(current_sctx.sc_sid)
2947 && name[0] == 's' && name[1] == ':')
2948 {
2949 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2950
2951 if (si->sn_autoload_prefix != NULL)
2952 {
2953 char_u *auto_name = concat_str(si->sn_autoload_prefix, name + 2);
2954
2955 if (auto_name != NULL)
2956 {
2957 ht = &globvarht;
2958 ret = find_var_in_ht(ht, *name, auto_name, TRUE);
Bram Moolenaar130f65d2022-01-13 20:39:41 +00002959 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002960 if (ret != NULL)
2961 {
2962 if (htp != NULL)
2963 *htp = ht;
2964 return ret;
2965 }
2966 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002967 }
2968 }
2969
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002970 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002971}
2972
2973/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00002974 * Like find_var() but if the name starts with <SNR>99_ then look in the
2975 * referenced script (used for a funcref).
2976 */
2977 dictitem_T *
2978find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
2979{
2980 if (STRNCMP(name, "<SNR>", 5) == 0 && isdigit(name[5]))
2981 {
2982 char_u *p = name + 5;
2983 int sid = getdigits(&p);
2984
2985 if (SCRIPT_ID_VALID(sid) && *p == '_')
2986 {
2987 hashtab_T *ht = &SCRIPT_VARS(sid);
2988
2989 if (ht != NULL)
2990 {
2991 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
2992
2993 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00002994 {
2995 if (htp != NULL)
2996 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00002997 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00002998 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00002999 }
3000 }
3001 }
3002
3003 return find_var(name, htp, no_autoload);
3004}
3005
3006/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003007 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02003008 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003009 * Returns NULL if not found.
3010 */
3011 dictitem_T *
3012find_var_in_ht(
3013 hashtab_T *ht,
3014 int htname,
3015 char_u *varname,
3016 int no_autoload)
3017{
3018 hashitem_T *hi;
3019
3020 if (*varname == NUL)
3021 {
3022 // Must be something like "s:", otherwise "ht" would be NULL.
3023 switch (htname)
3024 {
3025 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
3026 case 'g': return &globvars_var;
3027 case 'v': return &vimvars_var;
3028 case 'b': return &curbuf->b_bufvar;
3029 case 'w': return &curwin->w_winvar;
3030 case 't': return &curtab->tp_winvar;
3031 case 'l': return get_funccal_local_var();
3032 case 'a': return get_funccal_args_var();
3033 }
3034 return NULL;
3035 }
3036
3037 hi = hash_find(ht, varname);
3038 if (HASHITEM_EMPTY(hi))
3039 {
3040 // For global variables we may try auto-loading the script. If it
3041 // worked find the variable again. Don't auto-load a script if it was
3042 // loaded already, otherwise it would be loaded every time when
3043 // checking if a function name is a Funcref variable.
3044 if (ht == &globvarht && !no_autoload)
3045 {
3046 // Note: script_autoload() may make "hi" invalid. It must either
3047 // be obtained again or not used.
3048 if (!script_autoload(varname, FALSE) || aborting())
3049 return NULL;
3050 hi = hash_find(ht, varname);
3051 }
3052 if (HASHITEM_EMPTY(hi))
3053 return NULL;
3054 }
3055 return HI2DI(hi);
3056}
3057
3058/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 * Get the script-local hashtab. NULL if not in a script context.
3060 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003061 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062get_script_local_ht(void)
3063{
3064 scid_T sid = current_sctx.sc_sid;
3065
Bram Moolenaare3d46852020-08-29 13:39:17 +02003066 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 return &SCRIPT_VARS(sid);
3068 return NULL;
3069}
3070
3071/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003072 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003073 * When "cmd" is TRUE it must look like a command, a function must be followed
3074 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003075 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003077 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003078lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003079 char_u *name,
3080 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003081 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003082 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083{
3084 hashtab_T *ht = get_script_local_ht();
3085 char_u buffer[30];
3086 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003087 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003089 int is_global = FALSE;
3090 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091
3092 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003093 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 if (len < sizeof(buffer) - 1)
3095 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003096 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 vim_strncpy(buffer, name, len);
3098 p = buffer;
3099 }
3100 else
3101 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003102 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003104 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 }
3106
3107 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003108 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109
3110 // if not script-local, then perhaps imported
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003111 if (res == FAIL && find_imported(p, 0, FALSE) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003112 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 if (p != buffer)
3114 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003115
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003116 // Find a function, so that a following "->" works.
3117 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3118 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003119 if (res != OK)
3120 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003121 p = skipwhite(name + len);
3122
3123 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003124 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003125 // Do not check for an internal function, since it might also be a
3126 // valid command, such as ":split" versus "split()".
3127 // Skip "g:" before a function name.
3128 if (name[0] == 'g' && name[1] == ':')
3129 {
3130 is_global = TRUE;
3131 fname = name + 2;
3132 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003133 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003134 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003135 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003136 }
3137
Bram Moolenaar709664c2020-12-12 14:33:41 +01003138 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139}
3140
3141/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003142 * Find the hashtab used for a variable name.
3143 * Return NULL if the name is not valid.
3144 * Set "varname" to the start of name without ':'.
3145 */
3146 hashtab_T *
3147find_var_ht(char_u *name, char_u **varname)
3148{
3149 hashitem_T *hi;
3150 hashtab_T *ht;
3151
3152 if (name[0] == NUL)
3153 return NULL;
3154 if (name[1] != ':')
3155 {
3156 // The name must not start with a colon or #.
3157 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3158 return NULL;
3159 *varname = name;
3160
3161 // "version" is "v:version" in all scopes if scriptversion < 3.
3162 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003163 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003164 {
3165 hi = hash_find(&compat_hashtab, name);
3166 if (!HASHITEM_EMPTY(hi))
3167 return &compat_hashtab;
3168 }
3169
3170 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 if (ht != NULL)
3172 return ht; // local variable
3173
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003174 // In Vim9 script items at the script level are script-local, except
3175 // for autoload names.
3176 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 {
3178 ht = get_script_local_ht();
3179 if (ht != NULL)
3180 return ht;
3181 }
3182
3183 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003184 }
3185 *varname = name + 2;
3186 if (*name == 'g') // global variable
3187 return &globvarht;
3188 // There must be no ':' or '#' in the rest of the name, unless g: is used
3189 if (vim_strchr(name + 2, ':') != NULL
3190 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3191 return NULL;
3192 if (*name == 'b') // buffer variable
3193 return &curbuf->b_vars->dv_hashtab;
3194 if (*name == 'w') // window variable
3195 return &curwin->w_vars->dv_hashtab;
3196 if (*name == 't') // tab page variable
3197 return &curtab->tp_vars->dv_hashtab;
3198 if (*name == 'v') // v: variable
3199 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003200 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003201 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003203 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 if (*name == 'a') // a: function argument
3205 return get_funccal_args_ht();
3206 if (*name == 'l') // l: local function variable
3207 return get_funccal_local_ht();
3208 }
3209 if (*name == 's') // script variable
3210 {
3211 ht = get_script_local_ht();
3212 if (ht != NULL)
3213 return ht;
3214 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003215 return NULL;
3216}
3217
3218/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003219 * Get the string value of a (global/local) variable.
3220 * Note: see tv_get_string() for how long the pointer remains valid.
3221 * Returns NULL when it doesn't exist.
3222 */
3223 char_u *
3224get_var_value(char_u *name)
3225{
3226 dictitem_T *v;
3227
3228 v = find_var(name, NULL, FALSE);
3229 if (v == NULL)
3230 return NULL;
3231 return tv_get_string(&v->di_tv);
3232}
3233
3234/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003235 * Allocate a new hashtab for a sourced script. It will be used while
3236 * sourcing this script and when executing functions defined in the script.
3237 */
3238 void
3239new_script_vars(scid_T id)
3240{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003241 scriptvar_T *sv;
3242
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003243 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3244 if (sv == NULL)
3245 return;
3246 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003247 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003248}
3249
3250/*
3251 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3252 * point to it.
3253 */
3254 void
3255init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3256{
3257 hash_init(&dict->dv_hashtab);
3258 dict->dv_lock = 0;
3259 dict->dv_scope = scope;
3260 dict->dv_refcount = DO_NOT_FREE_CNT;
3261 dict->dv_copyID = 0;
3262 dict_var->di_tv.vval.v_dict = dict;
3263 dict_var->di_tv.v_type = VAR_DICT;
3264 dict_var->di_tv.v_lock = VAR_FIXED;
3265 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3266 dict_var->di_key[0] = NUL;
3267}
3268
3269/*
3270 * Unreference a dictionary initialized by init_var_dict().
3271 */
3272 void
3273unref_var_dict(dict_T *dict)
3274{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003275 // Now the dict needs to be freed if no one else is using it, go back to
3276 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003277 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3278 dict_unref(dict);
3279}
3280
3281/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003282 * Clean up a list of internal variables.
3283 * Frees all allocated variables and the value they contain.
3284 * Clears hashtab "ht", does not free it.
3285 */
3286 void
3287vars_clear(hashtab_T *ht)
3288{
3289 vars_clear_ext(ht, TRUE);
3290}
3291
3292/*
3293 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3294 */
3295 void
3296vars_clear_ext(hashtab_T *ht, int free_val)
3297{
3298 int todo;
3299 hashitem_T *hi;
3300 dictitem_T *v;
3301
3302 hash_lock(ht);
3303 todo = (int)ht->ht_used;
3304 for (hi = ht->ht_array; todo > 0; ++hi)
3305 {
3306 if (!HASHITEM_EMPTY(hi))
3307 {
3308 --todo;
3309
3310 // Free the variable. Don't remove it from the hashtab,
3311 // ht_array might change then. hash_clear() takes care of it
3312 // later.
3313 v = HI2DI(hi);
3314 if (free_val)
3315 clear_tv(&v->di_tv);
3316 if (v->di_flags & DI_FLAGS_ALLOC)
3317 vim_free(v);
3318 }
3319 }
3320 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003321 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003322}
3323
3324/*
3325 * Delete a variable from hashtab "ht" at item "hi".
3326 * Clear the variable value and free the dictitem.
3327 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003328 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003329delete_var(hashtab_T *ht, hashitem_T *hi)
3330{
3331 dictitem_T *di = HI2DI(hi);
3332
3333 hash_remove(ht, hi);
3334 clear_tv(&di->di_tv);
3335 vim_free(di);
3336}
3337
3338/*
3339 * List the value of one internal variable.
3340 */
3341 static void
3342list_one_var(dictitem_T *v, char *prefix, int *first)
3343{
3344 char_u *tofree;
3345 char_u *s;
3346 char_u numbuf[NUMBUFLEN];
3347
3348 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3349 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3350 s == NULL ? (char_u *)"" : s, first);
3351 vim_free(tofree);
3352}
3353
3354 static void
3355list_one_var_a(
3356 char *prefix,
3357 char_u *name,
3358 int type,
3359 char_u *string,
3360 int *first) // when TRUE clear rest of screen and set to FALSE
3361{
3362 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3363 msg_start();
3364 msg_puts(prefix);
3365 if (name != NULL) // "a:" vars don't have a name stored
3366 msg_puts((char *)name);
3367 msg_putchar(' ');
3368 msg_advance(22);
3369 if (type == VAR_NUMBER)
3370 msg_putchar('#');
3371 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3372 msg_putchar('*');
3373 else if (type == VAR_LIST)
3374 {
3375 msg_putchar('[');
3376 if (*string == '[')
3377 ++string;
3378 }
3379 else if (type == VAR_DICT)
3380 {
3381 msg_putchar('{');
3382 if (*string == '{')
3383 ++string;
3384 }
3385 else
3386 msg_putchar(' ');
3387
3388 msg_outtrans(string);
3389
3390 if (type == VAR_FUNC || type == VAR_PARTIAL)
3391 msg_puts("()");
3392 if (*first)
3393 {
3394 msg_clr_eos();
3395 *first = FALSE;
3396 }
3397}
3398
3399/*
3400 * Set variable "name" to value in "tv".
3401 * If the variable already exists, the value is updated.
3402 * Otherwise the variable is created.
3403 */
3404 void
3405set_var(
3406 char_u *name,
3407 typval_T *tv,
3408 int copy) // make copy of value in "tv"
3409{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003410 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003411}
3412
3413/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003414 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003415 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003416 * If the variable already exists and "is_const" is FALSE the value is updated.
3417 * Otherwise the variable is created.
3418 */
3419 void
3420set_var_const(
3421 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003422 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003423 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003424 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003425 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003426 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003427 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003428{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003429 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003430 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003431 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 dictitem_T *di;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003433 typval_T *dest_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003434 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003435 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003436 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003438 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003439 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003440 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003441 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003442 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003443
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003444 if (sid != 0)
3445 {
3446 if (SCRIPT_ID_VALID(sid))
3447 ht = &SCRIPT_VARS(sid);
3448 varname = name;
3449 }
3450 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003451 {
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003452 scriptitem_T *si;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003453
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003454 if (in_vim9script() && is_export
3455 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3456 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
Bram Moolenaar0e1574c2022-03-03 17:05:35 +00003457 ->sn_autoload_prefix != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003458 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003459 // In a vim9 autoload script an exported variable is put in the
3460 // global namespace with the autoload prefix.
3461 var_in_autoload = TRUE;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003462 varname = concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003463 if (varname == NULL)
3464 goto failed;
3465 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003466 ht = &globvarht;
3467 }
3468 else
3469 ht = find_var_ht(name, &varname);
3470 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003471 if (ht == NULL || *varname == NUL)
3472 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003473 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003474 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003475 }
Bram Moolenaar54969f42022-02-07 13:56:44 +00003476 is_script_local = ht == get_script_local_ht() || sid != 0
3477 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003478
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003479 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003480 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003481 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003482 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003483 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003484 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003485 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003486 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003487 }
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003488 if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
3489 && vim_strchr((char_u *)"gwbt", name[0]) != NULL)
3490 // Do not make g:var, w:var, b:var or t:var final.
3491 flags &= ~ASSIGN_FINAL;
3492
Bram Moolenaare535db82021-03-31 21:07:24 +02003493 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003494 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3495 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003496 // For "[a, _] = list" the underscore is ignored.
3497 if ((flags & ASSIGN_UNPACK) == 0)
3498 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003499 goto failed;
3500 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003501
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003503
Bram Moolenaar24e93162021-07-18 20:40:33 +02003504 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003505 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003506 imported_T *import = find_imported(varname, 0, FALSE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003507
Bram Moolenaar24e93162021-07-18 20:40:33 +02003508 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003509 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003510 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02003511 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003513 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003514 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003516 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3517 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003518 }
Bram Moolenaar75e27d72022-02-13 13:56:29 +00003519 if (!in_vim9script())
3520 {
3521 semsg(_(e_cannot_create_vim9_script_variable_in_function_str),
3522 name);
3523 goto failed;
3524 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003525 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526
Bram Moolenaar993faa32022-02-21 15:59:11 +00003527 // Search in parent scope which is possible to reference from lambda
3528 if (di == NULL)
3529 di = find_var_in_scoped_ht(name, TRUE);
3530
3531 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
3532 && var_wrong_func_name(name, di == NULL))
3533 goto failed;
3534
3535 if (need_convert_to_bool(type, tv))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003536 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003537 // Destination is a bool and the value is not, but it can be
3538 // converted.
3539 CLEAR_FIELD(bool_tv);
3540 bool_tv.v_type = VAR_BOOL;
3541 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3542 tv = &bool_tv;
3543 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003544
Bram Moolenaar993faa32022-02-21 15:59:11 +00003545 if (di != NULL)
3546 {
3547 // Item already exists. Allowed to replace when reloading.
3548 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003549 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003550 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
3551 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003552 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003553 emsg(_(e_cannot_modify_existing_variable));
3554 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003555 }
3556
Bram Moolenaar993faa32022-02-21 15:59:11 +00003557 if (is_script_local && vim9script
3558 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003559 {
3560 semsg(_(e_redefining_script_item_str), name);
3561 goto failed;
3562 }
3563
Bram Moolenaar993faa32022-02-21 15:59:11 +00003564 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003565 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003566 where_T where = WHERE_INIT;
3567 svar_T *sv = find_typval_in_script(&di->di_tv, sid);
3568
3569 if (sv != NULL)
3570 {
3571 // check the type and adjust to bool if needed
3572 where.wt_index = var_idx;
3573 where.wt_variable = TRUE;
3574 if (check_script_var_type(sv, tv, name, where) == FAIL)
3575 goto failed;
3576 if (type == NULL)
3577 type = sv->sv_type;
3578 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003579 }
3580
Bram Moolenaar993faa32022-02-21 15:59:11 +00003581 if ((flags & ASSIGN_FOR_LOOP) == 0
3582 && var_check_permission(di, name) == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003583 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003584 }
3585 else
3586 {
3587 // can only redefine once
3588 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003589
Bram Moolenaar993faa32022-02-21 15:59:11 +00003590 // A Vim9 script-local variable is also present in sn_all_vars
3591 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003592 if (var_in_vim9script || var_in_autoload)
Bram Moolenaar993faa32022-02-21 15:59:11 +00003593 update_vim9_script_var(FALSE, di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003594 var_in_autoload ? name : di->di_key, flags,
Bram Moolenaar993faa32022-02-21 15:59:11 +00003595 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003596 }
3597
Bram Moolenaar993faa32022-02-21 15:59:11 +00003598 // existing variable, need to clear the value
3599
3600 // Handle setting internal di: variables separately where needed to
3601 // prevent changing the type.
3602 if (ht == &vimvarht)
3603 {
3604 if (di->di_tv.v_type == VAR_STRING)
3605 {
3606 VIM_CLEAR(di->di_tv.vval.v_string);
3607 if (copy || tv->v_type != VAR_STRING)
3608 {
3609 char_u *val = tv_get_string(tv);
3610
3611 // Careful: when assigning to v:errmsg and
3612 // tv_get_string() causes an error message the variable
3613 // will already be set.
3614 if (di->di_tv.vval.v_string == NULL)
3615 di->di_tv.vval.v_string = vim_strsave(val);
3616 }
3617 else
3618 {
3619 // Take over the string to avoid an extra alloc/free.
3620 di->di_tv.vval.v_string = tv->vval.v_string;
3621 tv->vval.v_string = NULL;
3622 }
3623 goto failed;
3624 }
3625 else if (di->di_tv.v_type == VAR_NUMBER)
3626 {
3627 di->di_tv.vval.v_number = tv_get_number(tv);
3628 if (STRCMP(varname, "searchforward") == 0)
3629 set_search_direction(di->di_tv.vval.v_number
3630 ? '/' : '?');
3631#ifdef FEAT_SEARCH_EXTRA
3632 else if (STRCMP(varname, "hlsearch") == 0)
3633 {
3634 no_hlsearch = !di->di_tv.vval.v_number;
3635 redraw_all_later(SOME_VALID);
3636 }
3637#endif
3638 goto failed;
3639 }
3640 else if (di->di_tv.v_type != tv->v_type)
3641 {
3642 semsg(_(e_setting_str_to_value_with_wrong_type), name);
3643 goto failed;
3644 }
3645 }
3646
3647 clear_tv(&di->di_tv);
3648 }
3649 else
3650 {
3651 // Item not found, check if a function already exists.
3652 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
3653 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
3654 {
3655 semsg(_(e_redefining_script_item_str), name);
3656 goto failed;
3657 }
3658
3659 // add a new variable
3660 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3661 {
3662 semsg(_(e_unknown_variable_str), name);
3663 goto failed;
3664 }
3665
3666 // Can't add "v:" or "a:" variable.
3667 if (ht == &vimvarht || ht == get_funccal_args_ht())
3668 {
3669 semsg(_(e_illegal_variable_name_str), name);
3670 goto failed;
3671 }
3672
3673 // Make sure the variable name is valid. In Vim9 script an
3674 // autoload variable must be prefixed with "g:" unless in an
3675 // autoload script.
3676 if (!valid_varname(varname, -1, !vim9script
3677 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
3678 goto failed;
3679
3680 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3681 if (di == NULL)
3682 goto failed;
3683 STRCPY(di->di_key, varname);
3684 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
3685 {
3686 vim_free(di);
3687 goto failed;
3688 }
3689 di->di_flags = DI_FLAGS_ALLOC;
3690 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3691 di->di_flags |= DI_FLAGS_LOCK;
3692
3693 // A Vim9 script-local variable is also added to sn_all_vars and
3694 // sn_var_vals. It may set "type" from "tv".
3695 if (var_in_vim9script || var_in_autoload)
3696 update_vim9_script_var(TRUE, di,
3697 var_in_autoload ? name : di->di_key, flags,
3698 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003699 }
3700
Bram Moolenaar993faa32022-02-21 15:59:11 +00003701 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003702 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003703 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003704 else
3705 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003706 *dest_tv = *tv;
3707 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003708 init_tv(tv);
3709 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003710 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003711
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003712 if (vim9script && type != NULL)
Bram Moolenaar381692b2022-02-02 20:01:27 +00003713 set_tv_type(dest_tv, type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003714
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003715 // ":const var = value" locks the value
3716 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003717 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003718 // Like :lockvar! name: lock the value and what it contains, but only
3719 // if the reference count is up to one. That locks only literal
3720 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003721 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003722
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003723failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003724 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003725 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003726 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003727}
3728
3729/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003730 * Check in this order for backwards compatibility:
3731 * - Whether the variable is read-only
3732 * - Whether the variable value is locked
3733 * - Whether the variable is locked
3734 */
3735 int
3736var_check_permission(dictitem_T *di, char_u *name)
3737{
3738 if (var_check_ro(di->di_flags, name, FALSE)
3739 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3740 || var_check_lock(di->di_flags, name, FALSE))
3741 return FAIL;
3742 return OK;
3743}
3744
3745/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003746 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3747 * Also give an error message.
3748 */
3749 int
3750var_check_ro(int flags, char_u *name, int use_gettext)
3751{
3752 if (flags & DI_FLAGS_RO)
3753 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003754 if (name == NULL)
3755 emsg(_(e_cannot_change_readonly_variable));
3756 else
3757 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02003758 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003759 return TRUE;
3760 }
3761 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3762 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003763 if (name == NULL)
3764 emsg(_(e_cannot_set_variable_in_sandbox));
3765 else
3766 semsg(_(e_cannot_set_variable_in_sandbox_str),
3767 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003768 return TRUE;
3769 }
3770 return FALSE;
3771}
3772
3773/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003774 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3775 * Also give an error message.
3776 */
3777 int
3778var_check_lock(int flags, char_u *name, int use_gettext)
3779{
3780 if (flags & DI_FLAGS_LOCK)
3781 {
3782 semsg(_(e_variable_is_locked_str),
3783 use_gettext ? (char_u *)_(name) : name);
3784 return TRUE;
3785 }
3786 return FALSE;
3787}
3788
3789/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003790 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3791 * Also give an error message.
3792 */
3793 int
3794var_check_fixed(int flags, char_u *name, int use_gettext)
3795{
3796 if (flags & DI_FLAGS_FIX)
3797 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003798 if (name == NULL)
3799 emsg(_(e_cannot_delete_variable));
3800 else
3801 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003802 use_gettext ? (char_u *)_(name) : name);
3803 return TRUE;
3804 }
3805 return FALSE;
3806}
3807
3808/*
3809 * Check if a funcref is assigned to a valid variable name.
3810 * Return TRUE and give an error if not.
3811 */
3812 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003813var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003814 char_u *name, // points to start of variable name
3815 int new_var) // TRUE when creating the variable
3816{
Bram Moolenaar32154662021-03-28 21:14:06 +02003817 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
3818 // the name can be used without the s: prefix.
3819 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
3820 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003821 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3822 ? name[2] : name[0]))
3823 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003824 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003825 return TRUE;
3826 }
3827 // Don't allow hiding a function. When "v" is not NULL we might be
3828 // assigning another function to the same var, the type is checked
3829 // below.
3830 if (new_var && function_exists(name, FALSE))
3831 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003832 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003833 name);
3834 return TRUE;
3835 }
3836 return FALSE;
3837}
3838
3839/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003840 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3841 * value. Also give an error message, using "name" or _("name") when
3842 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003843 */
3844 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003845value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003846{
3847 if (lock & VAR_LOCKED)
3848 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003849 if (name == NULL)
3850 emsg(_(e_value_is_locked));
3851 else
3852 semsg(_(e_value_is_locked_str),
3853 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003854 return TRUE;
3855 }
3856 if (lock & VAR_FIXED)
3857 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003858 if (name == NULL)
3859 emsg(_(e_cannot_change_value));
3860 else
3861 semsg(_(e_cannot_change_value_of_str),
3862 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003863 return TRUE;
3864 }
3865 return FALSE;
3866}
3867
3868/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003869 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003870 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003871 * Return FALSE and give an error if not.
3872 */
3873 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003874valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003875{
3876 char_u *p;
3877
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003878 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003879 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003880 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003881 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003882 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003883 return FALSE;
3884 }
3885 return TRUE;
3886}
3887
3888/*
3889 * getwinvar() and gettabwinvar()
3890 */
3891 static void
3892getwinvar(
3893 typval_T *argvars,
3894 typval_T *rettv,
3895 int off) // 1 for gettabwinvar()
3896{
3897 win_T *win;
3898 char_u *varname;
3899 dictitem_T *v;
3900 tabpage_T *tp = NULL;
3901 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00003902 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003903 int need_switch_win;
3904
3905 if (off == 1)
3906 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3907 else
3908 tp = curtab;
3909 win = find_win_by_nr(&argvars[off], tp);
3910 varname = tv_get_string_chk(&argvars[off + 1]);
3911 ++emsg_off;
3912
3913 rettv->v_type = VAR_STRING;
3914 rettv->vval.v_string = NULL;
3915
3916 if (win != NULL && varname != NULL)
3917 {
3918 // Set curwin to be our win, temporarily. Also set the tabpage,
3919 // otherwise the window is not valid. Only do this when needed,
3920 // autocommands get blocked.
3921 need_switch_win = !(tp == curtab && win == curwin);
3922 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00003923 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003924 {
3925 if (*varname == '&')
3926 {
3927 if (varname[1] == NUL)
3928 {
3929 // get all window-local options in a dict
3930 dict_T *opts = get_winbuf_options(FALSE);
3931
3932 if (opts != NULL)
3933 {
3934 rettv_dict_set(rettv, opts);
3935 done = TRUE;
3936 }
3937 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003938 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003939 // window-local-option
3940 done = TRUE;
3941 }
3942 else
3943 {
3944 // Look up the variable.
3945 // Let getwinvar({nr}, "") return the "w:" dictionary.
3946 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3947 varname, FALSE);
3948 if (v != NULL)
3949 {
3950 copy_tv(&v->di_tv, rettv);
3951 done = TRUE;
3952 }
3953 }
3954 }
3955
3956 if (need_switch_win)
3957 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00003958 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003959 }
3960
3961 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3962 // use the default return value
3963 copy_tv(&argvars[off + 2], rettv);
3964
3965 --emsg_off;
3966}
3967
3968/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003969 * Set option "varname" to the value of "varp" for the current buffer/window.
3970 */
3971 static void
3972set_option_from_tv(char_u *varname, typval_T *varp)
3973{
3974 long numval = 0;
3975 char_u *strval;
3976 char_u nbuf[NUMBUFLEN];
3977 int error = FALSE;
3978
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003979 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003980 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003981 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003982 strval = (char_u *)"0"; // avoid using "false"
3983 }
3984 else
3985 {
3986 if (!in_vim9script() || varp->v_type != VAR_STRING)
3987 numval = (long)tv_get_number_chk(varp, &error);
3988 strval = tv_get_string_buf_chk(varp, nbuf);
3989 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02003990 if (!error && strval != NULL)
3991 set_option_value(varname, numval, strval, OPT_LOCAL);
3992}
3993
3994/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003995 * "setwinvar()" and "settabwinvar()" functions
3996 */
3997 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003998setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003999{
4000 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004001 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004002 int need_switch_win;
4003 char_u *varname, *winvarname;
4004 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004005 tabpage_T *tp = NULL;
4006
4007 if (check_secure())
4008 return;
4009
4010 if (off == 1)
4011 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4012 else
4013 tp = curtab;
4014 win = find_win_by_nr(&argvars[off], tp);
4015 varname = tv_get_string_chk(&argvars[off + 1]);
4016 varp = &argvars[off + 2];
4017
4018 if (win != NULL && varname != NULL && varp != NULL)
4019 {
4020 need_switch_win = !(tp == curtab && win == curwin);
4021 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00004022 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004023 {
4024 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02004025 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004026 else
4027 {
4028 winvarname = alloc(STRLEN(varname) + 3);
4029 if (winvarname != NULL)
4030 {
4031 STRCPY(winvarname, "w:");
4032 STRCPY(winvarname + 2, varname);
4033 set_var(winvarname, varp, TRUE);
4034 vim_free(winvarname);
4035 }
4036 }
4037 }
4038 if (need_switch_win)
Bram Moolenaar18f47402022-01-06 13:24:51 +00004039 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004040 }
4041}
4042
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004043/*
4044 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4045 * v:option_type, and v:option_command.
4046 */
4047 void
4048reset_v_option_vars(void)
4049{
4050 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4051 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4052 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4053 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4054 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4055 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4056}
4057
4058/*
4059 * Add an assert error to v:errors.
4060 */
4061 void
4062assert_error(garray_T *gap)
4063{
4064 struct vimvar *vp = &vimvars[VV_ERRORS];
4065
Bram Moolenaard787e402021-12-24 21:36:12 +00004066 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004067 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004068 set_vim_var_list(VV_ERRORS, list_alloc());
4069 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4070}
4071
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004072 int
4073var_exists(char_u *var)
4074{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004075 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004076 char_u *name;
4077 char_u *tofree;
4078 typval_T tv;
4079 int len = 0;
4080 int n = FALSE;
4081
4082 // get_name_len() takes care of expanding curly braces
4083 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004084 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004085 if (len > 0)
4086 {
4087 if (tofree != NULL)
4088 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004089 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004090 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004091 if (n)
4092 {
4093 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004094 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004095 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4096 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004097 if (n)
4098 clear_tv(&tv);
4099 }
4100 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004101 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004102 n = FALSE;
4103
4104 vim_free(tofree);
4105 return n;
4106}
4107
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004108static lval_T *redir_lval = NULL;
4109#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4110static garray_T redir_ga; // only valid when redir_lval is not NULL
4111static char_u *redir_endp = NULL;
4112static char_u *redir_varname = NULL;
4113
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004114 int
4115alloc_redir_lval(void)
4116{
4117 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4118 if (redir_lval == NULL)
4119 return FAIL;
4120 return OK;
4121}
4122
4123 void
4124clear_redir_lval(void)
4125{
4126 VIM_CLEAR(redir_lval);
4127}
4128
4129 void
4130init_redir_ga(void)
4131{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004132 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004133}
4134
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004135/*
4136 * Start recording command output to a variable
4137 * When "append" is TRUE append to an existing variable.
4138 * Returns OK if successfully completed the setup. FAIL otherwise.
4139 */
4140 int
4141var_redir_start(char_u *name, int append)
4142{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004143 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004144 typval_T tv;
4145
4146 // Catch a bad name early.
4147 if (!eval_isnamec1(*name))
4148 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004149 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004150 return FAIL;
4151 }
4152
4153 // Make a copy of the name, it is used in redir_lval until redir ends.
4154 redir_varname = vim_strsave(name);
4155 if (redir_varname == NULL)
4156 return FAIL;
4157
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004158 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004159 {
4160 var_redir_stop();
4161 return FAIL;
4162 }
4163
4164 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004165 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004166
4167 // Parse the variable name (can be a dict or list entry).
4168 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4169 FNE_CHECK_START);
4170 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4171 {
4172 clear_lval(redir_lval);
4173 if (redir_endp != NULL && *redir_endp != NUL)
4174 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004175 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004176 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004177 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004178 redir_endp = NULL; // don't store a value, only cleanup
4179 var_redir_stop();
4180 return FAIL;
4181 }
4182
4183 // check if we can write to the variable: set it to or append an empty
4184 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004185 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004186 tv.v_type = VAR_STRING;
4187 tv.vval.v_string = (char_u *)"";
4188 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004189 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004190 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004191 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004192 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004193 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004194 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004195 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004196 {
4197 redir_endp = NULL; // don't store a value, only cleanup
4198 var_redir_stop();
4199 return FAIL;
4200 }
4201
4202 return OK;
4203}
4204
4205/*
4206 * Append "value[value_len]" to the variable set by var_redir_start().
4207 * The actual appending is postponed until redirection ends, because the value
4208 * appended may in fact be the string we write to, changing it may cause freed
4209 * memory to be used:
4210 * :redir => foo
4211 * :let foo
4212 * :redir END
4213 */
4214 void
4215var_redir_str(char_u *value, int value_len)
4216{
4217 int len;
4218
4219 if (redir_lval == NULL)
4220 return;
4221
4222 if (value_len == -1)
4223 len = (int)STRLEN(value); // Append the entire string
4224 else
4225 len = value_len; // Append only "value_len" characters
4226
4227 if (ga_grow(&redir_ga, len) == OK)
4228 {
4229 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4230 redir_ga.ga_len += len;
4231 }
4232 else
4233 var_redir_stop();
4234}
4235
4236/*
4237 * Stop redirecting command output to a variable.
4238 * Frees the allocated memory.
4239 */
4240 void
4241var_redir_stop(void)
4242{
4243 typval_T tv;
4244
4245 if (EVALCMD_BUSY)
4246 {
4247 redir_lval = NULL;
4248 return;
4249 }
4250
4251 if (redir_lval != NULL)
4252 {
4253 // If there was no error: assign the text to the variable.
4254 if (redir_endp != NULL)
4255 {
4256 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4257 tv.v_type = VAR_STRING;
4258 tv.vval.v_string = redir_ga.ga_data;
4259 // Call get_lval() again, if it's inside a Dict or List it may
4260 // have changed.
4261 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4262 FALSE, FALSE, 0, FNE_CHECK_START);
4263 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004265 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004266 clear_lval(redir_lval);
4267 }
4268
4269 // free the collected output
4270 VIM_CLEAR(redir_ga.ga_data);
4271
4272 VIM_CLEAR(redir_lval);
4273 }
4274 VIM_CLEAR(redir_varname);
4275}
4276
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004277/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004278 * Get the collected redirected text and clear redir_ga.
4279 */
4280 char_u *
4281get_clear_redir_ga(void)
4282{
4283 char_u *res;
4284
4285 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4286 res = redir_ga.ga_data;
4287 redir_ga.ga_data = NULL;
4288 return res;
4289}
4290
4291/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004292 * "gettabvar()" function
4293 */
4294 void
4295f_gettabvar(typval_T *argvars, typval_T *rettv)
4296{
Bram Moolenaar18f47402022-01-06 13:24:51 +00004297 switchwin_T switchwin;
4298 tabpage_T *tp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004299 dictitem_T *v;
4300 char_u *varname;
4301 int done = FALSE;
4302
4303 rettv->v_type = VAR_STRING;
4304 rettv->vval.v_string = NULL;
4305
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004306 if (in_vim9script()
4307 && (check_for_number_arg(argvars, 0) == FAIL
4308 || check_for_string_arg(argvars, 1) == FAIL))
4309 return;
4310
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004311 varname = tv_get_string_chk(&argvars[1]);
4312 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4313 if (tp != NULL && varname != NULL)
4314 {
4315 // Set tp to be our tabpage, temporarily. Also set the window to the
4316 // first window in the tabpage, otherwise the window is not valid.
Bram Moolenaar18f47402022-01-06 13:24:51 +00004317 if (switch_win(&switchwin,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004318 tp == curtab || tp->tp_firstwin == NULL ? firstwin
4319 : tp->tp_firstwin, tp, TRUE) == OK)
4320 {
4321 // look up the variable
4322 // Let gettabvar({nr}, "") return the "t:" dictionary.
4323 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
4324 if (v != NULL)
4325 {
4326 copy_tv(&v->di_tv, rettv);
4327 done = TRUE;
4328 }
4329 }
4330
4331 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004332 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004333 }
4334
4335 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4336 copy_tv(&argvars[2], rettv);
4337}
4338
4339/*
4340 * "gettabwinvar()" function
4341 */
4342 void
4343f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4344{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004345 if (in_vim9script()
4346 && (check_for_number_arg(argvars, 0) == FAIL
4347 || check_for_number_arg(argvars, 1) == FAIL
4348 || check_for_string_arg(argvars, 2) == FAIL))
4349 return;
4350
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004351 getwinvar(argvars, rettv, 1);
4352}
4353
4354/*
4355 * "getwinvar()" function
4356 */
4357 void
4358f_getwinvar(typval_T *argvars, typval_T *rettv)
4359{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004360 if (in_vim9script()
4361 && (check_for_number_arg(argvars, 0) == FAIL
4362 || check_for_string_arg(argvars, 1) == FAIL))
4363 return;
4364
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004365 getwinvar(argvars, rettv, 0);
4366}
4367
4368/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004369 * "getbufvar()" function
4370 */
4371 void
4372f_getbufvar(typval_T *argvars, typval_T *rettv)
4373{
4374 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004375 char_u *varname;
4376 dictitem_T *v;
4377 int done = FALSE;
4378
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004379 if (in_vim9script()
4380 && (check_for_buffer_arg(argvars, 0) == FAIL
4381 || check_for_string_arg(argvars, 1) == FAIL))
4382 return;
4383
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004384 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004385 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004386
4387 rettv->v_type = VAR_STRING;
4388 rettv->vval.v_string = NULL;
4389
4390 if (buf != NULL && varname != NULL)
4391 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004392 if (*varname == '&')
4393 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004394 buf_T *save_curbuf = curbuf;
4395
4396 // set curbuf to be our buf, temporarily
4397 curbuf = buf;
4398
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004399 if (varname[1] == NUL)
4400 {
4401 // get all buffer-local options in a dict
4402 dict_T *opts = get_winbuf_options(TRUE);
4403
4404 if (opts != NULL)
4405 {
4406 rettv_dict_set(rettv, opts);
4407 done = TRUE;
4408 }
4409 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004410 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004411 // buffer-local-option
4412 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02004413
4414 // restore previous notion of curbuf
4415 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004416 }
4417 else
4418 {
4419 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02004420 if (*varname == NUL)
4421 // Let getbufvar({nr}, "") return the "b:" dictionary.
4422 v = &buf->b_bufvar;
4423 else
4424 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
4425 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004426 if (v != NULL)
4427 {
4428 copy_tv(&v->di_tv, rettv);
4429 done = TRUE;
4430 }
4431 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004432 }
4433
4434 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4435 // use the default value
4436 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004437}
4438
4439/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004440 * "settabvar()" function
4441 */
4442 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004443f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004444{
4445 tabpage_T *save_curtab;
4446 tabpage_T *tp;
4447 char_u *varname, *tabvarname;
4448 typval_T *varp;
4449
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004450 if (check_secure())
4451 return;
4452
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004453 if (in_vim9script()
4454 && (check_for_number_arg(argvars, 0) == FAIL
4455 || check_for_string_arg(argvars, 1) == FAIL))
4456 return;
4457
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004458 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4459 varname = tv_get_string_chk(&argvars[1]);
4460 varp = &argvars[2];
4461
4462 if (varname != NULL && varp != NULL && tp != NULL)
4463 {
4464 save_curtab = curtab;
4465 goto_tabpage_tp(tp, FALSE, FALSE);
4466
4467 tabvarname = alloc(STRLEN(varname) + 3);
4468 if (tabvarname != NULL)
4469 {
4470 STRCPY(tabvarname, "t:");
4471 STRCPY(tabvarname + 2, varname);
4472 set_var(tabvarname, varp, TRUE);
4473 vim_free(tabvarname);
4474 }
4475
4476 // Restore current tabpage
4477 if (valid_tabpage(save_curtab))
4478 goto_tabpage_tp(save_curtab, FALSE, FALSE);
4479 }
4480}
4481
4482/*
4483 * "settabwinvar()" function
4484 */
4485 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004486f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004487{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004488 if (in_vim9script()
4489 && (check_for_number_arg(argvars, 0) == FAIL
4490 || check_for_number_arg(argvars, 1) == FAIL
4491 || check_for_string_arg(argvars, 2) == FAIL))
4492 return;
4493
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004494 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004495}
4496
4497/*
4498 * "setwinvar()" function
4499 */
4500 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004501f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004502{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004503 if (in_vim9script()
4504 && (check_for_number_arg(argvars, 0) == FAIL
4505 || check_for_string_arg(argvars, 1) == FAIL))
4506 return;
4507
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004508 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004509}
4510
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004511/*
4512 * "setbufvar()" function
4513 */
4514 void
4515f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4516{
4517 buf_T *buf;
4518 char_u *varname, *bufvarname;
4519 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004520
4521 if (check_secure())
4522 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004523
4524 if (in_vim9script()
4525 && (check_for_buffer_arg(argvars, 0) == FAIL
4526 || check_for_string_arg(argvars, 1) == FAIL))
4527 return;
4528
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004529 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004530 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004531 varp = &argvars[2];
4532
4533 if (buf != NULL && varname != NULL && varp != NULL)
4534 {
4535 if (*varname == '&')
4536 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004537 aco_save_T aco;
4538
4539 // set curbuf to be our buf, temporarily
4540 aucmd_prepbuf(&aco, buf);
4541
Bram Moolenaar191929b2020-08-19 21:20:49 +02004542 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004543
4544 // reset notion of buffer
4545 aucmd_restbuf(&aco);
4546 }
4547 else
4548 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004549 bufvarname = alloc(STRLEN(varname) + 3);
4550 if (bufvarname != NULL)
4551 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004552 buf_T *save_curbuf = curbuf;
4553
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004554 curbuf = buf;
4555 STRCPY(bufvarname, "b:");
4556 STRCPY(bufvarname + 2, varname);
4557 set_var(bufvarname, varp, TRUE);
4558 vim_free(bufvarname);
4559 curbuf = save_curbuf;
4560 }
4561 }
4562 }
4563}
4564
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004565/*
4566 * Get a callback from "arg". It can be a Funcref or a function name.
4567 * When "arg" is zero return an empty string.
4568 * "cb_name" is not allocated.
4569 * "cb_name" is set to NULL for an invalid argument.
4570 */
4571 callback_T
4572get_callback(typval_T *arg)
4573{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004574 callback_T res;
4575 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004576
4577 res.cb_free_name = FALSE;
4578 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4579 {
4580 res.cb_partial = arg->vval.v_partial;
4581 ++res.cb_partial->pt_refcount;
4582 res.cb_name = partial_name(res.cb_partial);
4583 }
4584 else
4585 {
4586 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004587 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4588 && isdigit(*arg->vval.v_string))
4589 r = FAIL;
4590 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004591 {
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004592 if (arg->v_type == VAR_STRING)
4593 {
4594 char_u *name;
4595
4596 name = get_scriptlocal_funcname(arg->vval.v_string);
4597 if (name != NULL)
4598 {
4599 vim_free(arg->vval.v_string);
4600 arg->vval.v_string = name;
4601 }
4602 }
4603
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004604 res.cb_name = arg->vval.v_string;
4605 func_ref(res.cb_name);
4606 }
4607 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004608 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004609 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004610 r = FAIL;
4611
4612 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004613 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004614 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004615 res.cb_name = NULL;
4616 }
4617 }
4618 return res;
4619}
4620
4621/*
4622 * Copy a callback into a typval_T.
4623 */
4624 void
4625put_callback(callback_T *cb, typval_T *tv)
4626{
4627 if (cb->cb_partial != NULL)
4628 {
4629 tv->v_type = VAR_PARTIAL;
4630 tv->vval.v_partial = cb->cb_partial;
4631 ++tv->vval.v_partial->pt_refcount;
4632 }
4633 else
4634 {
4635 tv->v_type = VAR_FUNC;
4636 tv->vval.v_string = vim_strsave(cb->cb_name);
4637 func_ref(cb->cb_name);
4638 }
4639}
4640
4641/*
4642 * Make a copy of "src" into "dest", allocating the function name if needed,
4643 * without incrementing the refcount.
4644 */
4645 void
4646set_callback(callback_T *dest, callback_T *src)
4647{
4648 if (src->cb_partial == NULL)
4649 {
4650 // just a function name, make a copy
4651 dest->cb_name = vim_strsave(src->cb_name);
4652 dest->cb_free_name = TRUE;
4653 }
4654 else
4655 {
4656 // cb_name is a pointer into cb_partial
4657 dest->cb_name = src->cb_name;
4658 dest->cb_free_name = FALSE;
4659 }
4660 dest->cb_partial = src->cb_partial;
4661}
4662
4663/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004664 * Copy callback from "src" to "dest", incrementing the refcounts.
4665 */
4666 void
4667copy_callback(callback_T *dest, callback_T *src)
4668{
4669 dest->cb_partial = src->cb_partial;
4670 if (dest->cb_partial != NULL)
4671 {
4672 dest->cb_name = src->cb_name;
4673 dest->cb_free_name = FALSE;
4674 ++dest->cb_partial->pt_refcount;
4675 }
4676 else
4677 {
4678 dest->cb_name = vim_strsave(src->cb_name);
4679 dest->cb_free_name = TRUE;
4680 func_ref(src->cb_name);
4681 }
4682}
4683
4684/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004685 * When a callback refers to an autoload import, change the function name to
4686 * the "path#name" form. Uses the current script context.
4687 * Only works when the name is allocated.
4688 */
4689 void
4690expand_autload_callback(callback_T *cb)
4691{
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004692 char_u *name;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004693 char_u *p;
4694 imported_T *import;
4695
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004696 if (!in_vim9script() || cb->cb_name == NULL
4697 || (!cb->cb_free_name
4698 && (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004699 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004700 if (cb->cb_partial != NULL)
4701 name = cb->cb_partial->pt_name;
4702 else
4703 name = cb->cb_name;
4704 p = vim_strchr(name, '.');
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004705 if (p == NULL)
4706 return;
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004707 import = find_imported(name, p - name, FALSE);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004708 if (import != NULL && SCRIPT_ID_VALID(import->imp_sid))
4709 {
4710 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
4711
4712 if (si->sn_autoload_prefix != NULL)
4713 {
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004714 char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004715
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004716 if (newname != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004717 {
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004718 if (cb->cb_partial != NULL)
4719 {
4720 if (cb->cb_name == cb->cb_partial->pt_name)
4721 cb->cb_name = newname;
4722 vim_free(cb->cb_partial->pt_name);
4723 cb->cb_partial->pt_name = newname;
4724 }
4725 else
4726 {
4727 vim_free(cb->cb_name);
4728 cb->cb_name = newname;
4729 }
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004730 }
4731 }
4732 }
4733}
4734
4735/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004736 * Unref/free "callback" returned by get_callback() or set_callback().
4737 */
4738 void
4739free_callback(callback_T *callback)
4740{
4741 if (callback->cb_partial != NULL)
4742 {
4743 partial_unref(callback->cb_partial);
4744 callback->cb_partial = NULL;
4745 }
4746 else if (callback->cb_name != NULL)
4747 func_unref(callback->cb_name);
4748 if (callback->cb_free_name)
4749 {
4750 vim_free(callback->cb_name);
4751 callback->cb_free_name = FALSE;
4752 }
4753 callback->cb_name = NULL;
4754}
4755
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004756#endif // FEAT_EVAL