blob: 496666e1eac5e3561c840dff16dcd9dda637af1c [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{
757 if (!in_vim9script())
758 {
759 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
760 return;
761 }
762 ex_let(eap);
763}
764
765/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200766 * ":let" list all variable values
767 * ":let var1 var2" list variable values
768 * ":let var = expr" assignment command.
769 * ":let var += expr" assignment command.
770 * ":let var -= expr" assignment command.
771 * ":let var *= expr" assignment command.
772 * ":let var /= expr" assignment command.
773 * ":let var %= expr" assignment command.
774 * ":let var .= expr" assignment command.
775 * ":let var ..= expr" assignment command.
776 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100778 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200779 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200780 * ":final var = expr" assignment command.
781 * ":final [var1, var2] = expr" unpack list.
782 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200783 * ":const" list all variable values
784 * ":const var1 var2" list variable values
785 * ":const var = expr" assignment command.
786 * ":const [var1, var2] = expr" unpack list.
787 */
788 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200789ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200790{
791 char_u *arg = eap->arg;
792 char_u *expr = NULL;
793 typval_T rettv;
794 int i;
795 int var_count = 0;
796 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200797 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200798 char_u *argend;
799 int first = TRUE;
800 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200801 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100802 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200803 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200804
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200805 if (eap->cmdidx == CMD_final && !vim9script)
806 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100807 // In legacy Vim script ":final" is short for ":finally".
808 ex_finally(eap);
809 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200810 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200811 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200812 {
813 emsg(_(e_cannot_use_let_in_vim9_script));
814 return;
815 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200816
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100817 if (eap->cmdidx == CMD_const)
818 flags |= ASSIGN_CONST;
819 else if (eap->cmdidx == CMD_final)
820 flags |= ASSIGN_FINAL;
821
822 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200824 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200826 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200827 if (argend == NULL)
828 return;
829 if (argend > arg && argend[-1] == '.') // for var.='str'
830 --argend;
831 expr = skipwhite(argend);
832 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +0200833 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200834 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200835 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
836 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200837 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200838 {
839 // ":let" without "=": list variables
840 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000841 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200842 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000843 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200844 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200845 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200846 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200847 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100848 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +0000849 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100850 else
851 // Vim9 declaration ":var name: type"
852 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200853 }
854 else
855 {
856 // ":let var1 var2" - list values
857 arg = list_arg_vars(eap, arg, &first);
858 }
859 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200860 else if (!eap->skip)
861 {
862 // ":let"
863 list_glob_vars(&first);
864 list_buf_vars(&first);
865 list_win_vars(&first);
866 list_tab_vars(&first);
867 list_script_vars(&first);
868 list_func_vars(&first);
869 list_vim_vars(&first);
870 }
Bram Moolenaar63b91732021-08-05 20:40:03 +0200871 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200872 }
873 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
874 {
875 list_T *l;
Bram Moolenaar81530e32021-07-28 21:25:49 +0200876 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200877
878 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200879 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200880 if (l != NULL)
881 {
882 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200883 if (!eap->skip)
884 {
Bram Moolenaar81530e32021-07-28 21:25:49 +0200885 // errors are for the assignment, not the end marker
886 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200887 op[0] = '=';
888 op[1] = NUL;
889 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200891 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200892 clear_tv(&rettv);
893 }
894 }
895 else
896 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200897 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200898 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200899
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200900 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200901 i = FAIL;
902 if (has_assign || concat)
903 {
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100904 int cur_lnum;
905
Bram Moolenaar32e35112020-05-14 22:41:15 +0200906 op[0] = '=';
907 op[1] = NUL;
908 if (*expr != '=')
909 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200910 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200911 {
912 // +=, /=, etc. require an existing variable
913 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
914 i = FAIL;
915 }
916 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200917 {
918 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200919 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200920 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200921 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200922 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200923 ++len;
924 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200925 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200926 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200927 }
928 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200929 ++expr;
930
Bram Moolenaar7f2c3412021-11-29 16:01:49 +0000931 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200932 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200933 {
934 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100935 semsg(_(e_white_space_required_before_and_after_str_at_str),
936 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200937 i = FAIL;
938 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200939
940 if (eap->skip)
941 ++emsg_skip;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200942 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200943 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100944 cur_lnum = SOURCING_LNUM;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200945 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200946 if (eap->skip)
947 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200948 clear_evalarg(&evalarg, eap);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100949
950 // Restore the line number so that any type error is given for the
951 // declaration, not the expression.
952 SOURCING_LNUM = cur_lnum;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200953 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200954 if (eap->skip)
955 {
956 if (i != FAIL)
957 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200958 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200959 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200960 {
961 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200962 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200963 clear_tv(&rettv);
964 }
965 }
966}
967
968/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +0100969 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200970 * Handles both "var" with any type and "[var, var; var]" with a list type.
971 * When "op" is not NULL it points to a string with characters that
972 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
973 * or concatenate.
974 * Returns OK or FAIL;
975 */
976 int
977ex_let_vars(
978 char_u *arg_start,
979 typval_T *tv,
980 int copy, // copy values from "tv", don't move
981 int semicolon, // from skip_var_list()
982 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100983 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200984 char_u *op)
985{
986 char_u *arg = arg_start;
987 list_T *l;
988 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100989 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200990 listitem_T *item;
991 typval_T ltv;
992
993 if (*arg != '[')
994 {
995 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100996 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200997 return FAIL;
998 return OK;
999 }
1000
1001 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1002 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
1003 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001004 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001005 return FAIL;
1006 }
1007
1008 i = list_len(l);
1009 if (semicolon == 0 && var_count < i)
1010 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001011 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001012 return FAIL;
1013 }
1014 if (var_count - semicolon > i)
1015 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001016 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001017 return FAIL;
1018 }
1019
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001020 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001021 item = l->lv_first;
1022 while (*arg != ']')
1023 {
1024 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001025 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001026 arg = ex_let_one(arg, &item->li_tv, TRUE,
1027 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001028 item = item->li_next;
1029 if (arg == NULL)
1030 return FAIL;
1031
1032 arg = skipwhite(arg);
1033 if (*arg == ';')
1034 {
1035 // Put the rest of the list (may be empty) in the var after ';'.
1036 // Create a new list for this.
1037 l = list_alloc();
1038 if (l == NULL)
1039 return FAIL;
1040 while (item != NULL)
1041 {
1042 list_append_tv(l, &item->li_tv);
1043 item = item->li_next;
1044 }
1045
1046 ltv.v_type = VAR_LIST;
1047 ltv.v_lock = 0;
1048 ltv.vval.v_list = l;
1049 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001050 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001051
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001052 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1053 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001054 clear_tv(&ltv);
1055 if (arg == NULL)
1056 return FAIL;
1057 break;
1058 }
1059 else if (*arg != ',' && *arg != ']')
1060 {
1061 internal_error("ex_let_vars()");
1062 return FAIL;
1063 }
1064 }
1065
1066 return OK;
1067}
1068
1069/*
1070 * Skip over assignable variable "var" or list of variables "[var, var]".
1071 * Used for ":let varvar = expr" and ":for varvar in expr".
1072 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001073 * for "[var, var; var]" set "semicolon" to 1.
1074 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001075 * Return NULL for an error.
1076 */
1077 char_u *
1078skip_var_list(
1079 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001080 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001081 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001082 int *semicolon,
1083 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001084{
1085 char_u *p, *s;
1086
1087 if (*arg == '[')
1088 {
1089 // "[var, var]": find the matching ']'.
1090 p = arg;
1091 for (;;)
1092 {
1093 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001094 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001095 if (s == p)
1096 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001097 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001098 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001099 return NULL;
1100 }
1101 ++*var_count;
1102
1103 p = skipwhite(s);
1104 if (*p == ']')
1105 break;
1106 else if (*p == ';')
1107 {
1108 if (*semicolon == 1)
1109 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001110 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001111 return NULL;
1112 }
1113 *semicolon = 1;
1114 }
1115 else if (*p != ',')
1116 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001117 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001118 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001119 return NULL;
1120 }
1121 }
1122 return p + 1;
1123 }
1124 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001126}
1127
1128/*
1129 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1130 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001131 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001132 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001133 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001134skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001135{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001136 char_u *end;
1137 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001138
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001139 if (*arg == '@' && arg[1] != NUL)
1140 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001141 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001142 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001143
1144 // "a: type" is declaring variable "a" with a type, not "a:".
1145 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001146 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001147 --end;
1148
Bram Moolenaar585587d2021-01-17 20:52:13 +01001149 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001150 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001151 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001152 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001153 }
1154 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001155}
1156
1157/*
1158 * List variables for hashtab "ht" with prefix "prefix".
1159 * If "empty" is TRUE also list NULL strings as empty strings.
1160 */
1161 void
1162list_hashtable_vars(
1163 hashtab_T *ht,
1164 char *prefix,
1165 int empty,
1166 int *first)
1167{
1168 hashitem_T *hi;
1169 dictitem_T *di;
1170 int todo;
1171 char_u buf[IOSIZE];
1172
1173 todo = (int)ht->ht_used;
1174 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1175 {
1176 if (!HASHITEM_EMPTY(hi))
1177 {
1178 --todo;
1179 di = HI2DI(hi);
1180
1181 // apply :filter /pat/ to variable name
1182 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1183 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1184 if (message_filtered(buf))
1185 continue;
1186
1187 if (empty || di->di_tv.v_type != VAR_STRING
1188 || di->di_tv.vval.v_string != NULL)
1189 list_one_var(di, prefix, first);
1190 }
1191 }
1192}
1193
1194/*
1195 * List global variables.
1196 */
1197 static void
1198list_glob_vars(int *first)
1199{
1200 list_hashtable_vars(&globvarht, "", TRUE, first);
1201}
1202
1203/*
1204 * List buffer variables.
1205 */
1206 static void
1207list_buf_vars(int *first)
1208{
1209 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1210}
1211
1212/*
1213 * List window variables.
1214 */
1215 static void
1216list_win_vars(int *first)
1217{
1218 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1219}
1220
1221/*
1222 * List tab page variables.
1223 */
1224 static void
1225list_tab_vars(int *first)
1226{
1227 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1228}
1229
1230/*
1231 * List variables in "arg".
1232 */
1233 static char_u *
1234list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1235{
1236 int error = FALSE;
1237 int len;
1238 char_u *name;
1239 char_u *name_start;
1240 char_u *arg_subsc;
1241 char_u *tofree;
1242 typval_T tv;
1243
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001244 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001245 {
1246 if (error || eap->skip)
1247 {
1248 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1249 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1250 {
1251 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001252 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001253 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001254 break;
1255 }
1256 }
1257 else
1258 {
1259 // get_name_len() takes care of expanding curly braces
1260 name_start = name = arg;
1261 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1262 if (len <= 0)
1263 {
1264 // This is mainly to keep test 49 working: when expanding
1265 // curly braces fails overrule the exception error message.
1266 if (len < 0 && !aborting())
1267 {
1268 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001269 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001270 break;
1271 }
1272 error = TRUE;
1273 }
1274 else
1275 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001276 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001277 if (tofree != NULL)
1278 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001279 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001280 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001281 error = TRUE;
1282 else
1283 {
1284 // handle d.key, l[idx], f(expr)
1285 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001286 if (handle_subscript(&arg, name_start, &tv,
1287 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001288 error = TRUE;
1289 else
1290 {
1291 if (arg == arg_subsc && len == 2 && name[1] == ':')
1292 {
1293 switch (*name)
1294 {
1295 case 'g': list_glob_vars(first); break;
1296 case 'b': list_buf_vars(first); break;
1297 case 'w': list_win_vars(first); break;
1298 case 't': list_tab_vars(first); break;
1299 case 'v': list_vim_vars(first); break;
1300 case 's': list_script_vars(first); break;
1301 case 'l': list_func_vars(first); break;
1302 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001303 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001304 }
1305 }
1306 else
1307 {
1308 char_u numbuf[NUMBUFLEN];
1309 char_u *tf;
1310 int c;
1311 char_u *s;
1312
1313 s = echo_string(&tv, &tf, numbuf, 0);
1314 c = *arg;
1315 *arg = NUL;
1316 list_one_var_a("",
1317 arg == arg_subsc ? name : name_start,
1318 tv.v_type,
1319 s == NULL ? (char_u *)"" : s,
1320 first);
1321 *arg = c;
1322 vim_free(tf);
1323 }
1324 clear_tv(&tv);
1325 }
1326 }
1327 }
1328
1329 vim_free(tofree);
1330 }
1331
1332 arg = skipwhite(arg);
1333 }
1334
1335 return arg;
1336}
1337
1338/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001339 * Set an environment variable, part of ex_let_one().
1340 */
1341 static char_u *
1342ex_let_env(
1343 char_u *arg,
1344 typval_T *tv,
1345 int flags,
1346 char_u *endchars,
1347 char_u *op)
1348{
1349 char_u *arg_end = NULL;
1350 char_u *name;
1351 int len;
1352
1353 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1354 && (flags & ASSIGN_FOR_LOOP) == 0)
1355 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001356 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001357 return NULL;
1358 }
1359
1360 // Find the end of the name.
1361 ++arg;
1362 name = arg;
1363 len = get_env_len(&arg);
1364 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001365 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001366 else
1367 {
1368 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001369 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001370 else if (endchars != NULL
1371 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1372 emsg(_(e_unexpected_characters_in_let));
1373 else if (!check_secure())
1374 {
1375 char_u *tofree = NULL;
1376 int c1 = name[len];
1377 char_u *p;
1378
1379 name[len] = NUL;
1380 p = tv_get_string_chk(tv);
1381 if (p != NULL && op != NULL && *op == '.')
1382 {
1383 int mustfree = FALSE;
1384 char_u *s = vim_getenv(name, &mustfree);
1385
1386 if (s != NULL)
1387 {
1388 p = tofree = concat_str(s, p);
1389 if (mustfree)
1390 vim_free(s);
1391 }
1392 }
1393 if (p != NULL)
1394 {
1395 vim_setenv_ext(name, p);
1396 arg_end = arg;
1397 }
1398 name[len] = c1;
1399 vim_free(tofree);
1400 }
1401 }
1402 return arg_end;
1403}
1404
1405/*
1406 * Set an option, part of ex_let_one().
1407 */
1408 static char_u *
1409ex_let_option(
1410 char_u *arg,
1411 typval_T *tv,
1412 int flags,
1413 char_u *endchars,
1414 char_u *op)
1415{
1416 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001417 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001418 char_u *arg_end = NULL;
1419
1420 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1421 && (flags & ASSIGN_FOR_LOOP) == 0)
1422 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001423 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001424 return NULL;
1425 }
1426
1427 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001428 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001429 if (p == NULL || (endchars != NULL
1430 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1431 emsg(_(e_unexpected_characters_in_let));
1432 else
1433 {
1434 int c1;
1435 long n = 0;
1436 getoption_T opt_type;
1437 long numval;
1438 char_u *stringval = NULL;
1439 char_u *s = NULL;
1440 int failed = FALSE;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001441 int opt_p_flags;
1442 char_u *tofree = NULL;
Bram Moolenaar92c33eb2021-12-07 11:03:39 +00001443 char_u numbuf[NUMBUFLEN];
1444
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001445 c1 = *p;
1446 *p = NUL;
1447
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001448 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags,
1449 scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001450 if ((opt_type == gov_bool
1451 || opt_type == gov_number
1452 || opt_type == gov_hidden_bool
1453 || opt_type == gov_hidden_number)
1454 && (tv->v_type != VAR_STRING || !in_vim9script()))
1455 {
1456 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1457 // bool, possibly hidden
1458 n = (long)tv_get_bool(tv);
1459 else
1460 // number, possibly hidden
1461 n = (long)tv_get_number(tv);
1462 }
1463
Bram Moolenaaref082e12021-12-12 21:02:03 +00001464 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001465 || tv->v_type == VAR_FUNC))
1466 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001467 // If the option can be set to a function reference or a lambda
1468 // and the passed value is a function reference, then convert it to
1469 // the name (string) of the function reference.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001470 s = tv2string(tv, &tofree, numbuf, 0);
1471 }
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001472 // Avoid setting a string option to the text "v:false" or similar.
1473 // In Vim9 script also don't convert a number to string.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001474 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001475 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1476 s = tv_get_string_chk(tv);
1477
1478 if (op != NULL && *op != '=')
1479 {
1480 if (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1481 || (opt_type == gov_string && *op != '.'))
1482 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001483 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001484 failed = TRUE; // don't set the value
1485
1486 }
1487 else
1488 {
1489 // number, in legacy script also bool
1490 if (opt_type == gov_number
1491 || (opt_type == gov_bool && !in_vim9script()))
1492 {
1493 switch (*op)
1494 {
1495 case '+': n = numval + n; break;
1496 case '-': n = numval - n; break;
1497 case '*': n = numval * n; break;
1498 case '/': n = (long)num_divide(numval, n,
1499 &failed); break;
1500 case '%': n = (long)num_modulus(numval, n,
1501 &failed); break;
1502 }
1503 s = NULL;
1504 }
1505 else if (opt_type == gov_string
1506 && stringval != NULL && s != NULL)
1507 {
1508 // string
1509 s = concat_str(stringval, s);
1510 vim_free(stringval);
1511 stringval = s;
1512 }
1513 }
1514 }
1515
1516 if (!failed)
1517 {
1518 if (opt_type != gov_string || s != NULL)
1519 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001520 set_option_value(arg, n, s, scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001521 arg_end = p;
1522 }
1523 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001524 emsg(_(e_string_required));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001525 }
1526 *p = c1;
1527 vim_free(stringval);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001528 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001529 }
1530 return arg_end;
1531}
1532
1533/*
1534 * Set a register, part of ex_let_one().
1535 */
1536 static char_u *
1537ex_let_register(
1538 char_u *arg,
1539 typval_T *tv,
1540 int flags,
1541 char_u *endchars,
1542 char_u *op)
1543{
1544 char_u *arg_end = NULL;
1545
1546 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1547 && (flags & ASSIGN_FOR_LOOP) == 0)
1548 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001549 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001550 return NULL;
1551 }
1552 ++arg;
1553 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001554 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001555 else if (endchars != NULL
1556 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1557 emsg(_(e_unexpected_characters_in_let));
1558 else
1559 {
1560 char_u *ptofree = NULL;
1561 char_u *p;
1562
1563 p = tv_get_string_chk(tv);
1564 if (p != NULL && op != NULL && *op == '.')
1565 {
1566 char_u *s = get_reg_contents(*arg == '@'
1567 ? '"' : *arg, GREG_EXPR_SRC);
1568
1569 if (s != NULL)
1570 {
1571 p = ptofree = concat_str(s, p);
1572 vim_free(s);
1573 }
1574 }
1575 if (p != NULL)
1576 {
1577 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1578 arg_end = arg + 1;
1579 }
1580 vim_free(ptofree);
1581 }
1582 return arg_end;
1583}
1584
1585/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001586 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1587 * Returns a pointer to the char just after the var name.
1588 * Returns NULL if there is an error.
1589 */
1590 static char_u *
1591ex_let_one(
1592 char_u *arg, // points to variable name
1593 typval_T *tv, // value to assign to variable
1594 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001595 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001596 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001597 char_u *op, // "+", "-", "." or NULL
1598 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001599{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001600 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001601
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001602 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001603 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001604 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1605 {
1606 vim9_declare_error(arg);
1607 return NULL;
1608 }
1609
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001610 if (*arg == '$')
1611 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001612 // ":let $VAR = expr": Set environment variable.
1613 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001614 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001615 else if (*arg == '&')
1616 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001617 // ":let &option = expr": Set option value.
1618 // ":let &l:option = expr": Set local option value.
1619 // ":let &g:option = expr": Set global option value.
1620 // ":for &ts in range(8)": Set option value for for loop
1621 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001622 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001623 else if (*arg == '@')
1624 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001625 // ":let @r = expr": Set register contents.
1626 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001627 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001628 else if (eval_isnamec1(*arg) || *arg == '{')
1629 {
1630 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001631 char_u *p;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001632
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001633 // ":let var = expr": Set internal variable.
1634 // ":let var: type = expr": Set internal variable with type.
1635 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001636 p = get_lval(arg, tv, &lv, FALSE, FALSE,
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001637 (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1638 ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001639 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001640 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 if (endchars != NULL && vim_strchr(endchars,
1642 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001643 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001644 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001645 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001646 else
1647 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001648 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001649 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001650 }
1651 }
1652 clear_lval(&lv);
1653 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001654 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001655 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001656
1657 return arg_end;
1658}
1659
1660/*
1661 * ":unlet[!] var1 ... " command.
1662 */
1663 void
1664ex_unlet(exarg_T *eap)
1665{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001666 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001667}
1668
1669/*
1670 * ":lockvar" and ":unlockvar" commands
1671 */
1672 void
1673ex_lockvar(exarg_T *eap)
1674{
1675 char_u *arg = eap->arg;
1676 int deep = 2;
1677
1678 if (eap->forceit)
1679 deep = -1;
1680 else if (vim_isdigit(*arg))
1681 {
1682 deep = getdigits(&arg);
1683 arg = skipwhite(arg);
1684 }
1685
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001686 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001687}
1688
1689/*
1690 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001691 * Also used for Vim9 script. "callback" is invoked as:
1692 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001693 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001694 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001695ex_unletlock(
1696 exarg_T *eap,
1697 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001698 int deep,
1699 int glv_flags,
1700 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1701 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001702{
1703 char_u *arg = argstart;
1704 char_u *name_end;
1705 int error = FALSE;
1706 lval_T lv;
1707
1708 do
1709 {
1710 if (*arg == '$')
1711 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001712 lv.ll_name = arg;
1713 lv.ll_tv = NULL;
1714 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001715 if (get_env_len(&arg) == 0)
1716 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001717 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001718 return;
1719 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001720 if (!error && !eap->skip
1721 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1722 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001723 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001724 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001725 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001726 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001727 // Parse the name and find the end.
1728 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001729 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001730 if (lv.ll_name == NULL)
1731 error = TRUE; // error but continue parsing
1732 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1733 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001734 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001735 if (name_end != NULL)
1736 {
1737 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00001738 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001739 }
1740 if (!(eap->skip || error))
1741 clear_lval(&lv);
1742 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001743 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001744
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001745 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001746 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001747 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001748
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001749 if (!eap->skip)
1750 clear_lval(&lv);
1751 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001752
1753 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001754 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001755
Bram Moolenaar63b91732021-08-05 20:40:03 +02001756 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001757}
1758
1759 static int
1760do_unlet_var(
1761 lval_T *lp,
1762 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001763 exarg_T *eap,
1764 int deep UNUSED,
1765 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001766{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001767 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001768 int ret = OK;
1769 int cc;
1770
1771 if (lp->ll_tv == NULL)
1772 {
1773 cc = *name_end;
1774 *name_end = NUL;
1775
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001776 // Environment variable, normal name or expanded name.
1777 if (*lp->ll_name == '$')
1778 vim_unsetenv(lp->ll_name + 1);
1779 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001780 ret = FAIL;
1781 *name_end = cc;
1782 }
1783 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001784 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001785 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001786 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001787 return FAIL;
1788 else if (lp->ll_range)
1789 {
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001790 if (list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_name, lp->ll_n1,
1791 !lp->ll_empty2, lp->ll_n2) == FAIL)
1792 return FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001793 }
1794 else
1795 {
1796 if (lp->ll_list != NULL)
1797 // unlet a List item.
1798 listitem_remove(lp->ll_list, lp->ll_li);
1799 else
1800 // unlet a Dictionary item.
1801 dictitem_remove(lp->ll_dict, lp->ll_di);
1802 }
1803
1804 return ret;
1805}
1806
1807/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001808 * Unlet one item or a range of items from a list.
1809 * Return OK or FAIL.
1810 */
1811 int
1812list_unlet_range(
1813 list_T *l,
1814 listitem_T *li_first,
1815 char_u *name,
1816 long n1_arg,
1817 int has_n2,
1818 long n2)
1819{
1820 listitem_T *li = li_first;
1821 int n1 = n1_arg;
1822
1823 while (li != NULL && (!has_n2 || n2 >= n1))
1824 {
1825 if (value_check_lock(li->li_tv.v_lock, name, FALSE))
1826 return FAIL;
1827 li = li->li_next;
1828 ++n1;
1829 }
1830
1831 // Delete a range of List items.
1832 li = li_first;
1833 n1 = n1_arg;
1834 while (li != NULL && (!has_n2 || n2 >= n1))
1835 {
1836 listitem_T *next = li->li_next;
1837
1838 listitem_remove(l, li);
1839 li = next;
1840 ++n1;
1841 }
1842 return OK;
1843}
1844/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001845 * "unlet" a variable. Return OK if it existed, FAIL if not.
1846 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1847 */
1848 int
1849do_unlet(char_u *name, int forceit)
1850{
1851 hashtab_T *ht;
1852 hashitem_T *hi;
1853 char_u *varname;
1854 dict_T *d;
1855 dictitem_T *di;
1856
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001857 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001858 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001859 return FAIL;
1860
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001861 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001862
1863 // can't :unlet a script variable in Vim9 script from a function
1864 if (ht == get_script_local_ht()
1865 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1866 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1867 == SCRIPT_VERSION_VIM9
1868 && check_vim9_unlet(name) == FAIL)
1869 return FAIL;
1870
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001871 if (ht != NULL && *varname != NUL)
1872 {
1873 d = get_current_funccal_dict(ht);
1874 if (d == NULL)
1875 {
1876 if (ht == &globvarht)
1877 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001878 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001879 d = &vimvardict;
1880 else
1881 {
1882 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1883 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1884 }
1885 if (d == NULL)
1886 {
1887 internal_error("do_unlet()");
1888 return FAIL;
1889 }
1890 }
1891 hi = hash_find(ht, varname);
1892 if (HASHITEM_EMPTY(hi))
1893 hi = find_hi_in_scoped_ht(name, &ht);
1894 if (hi != NULL && !HASHITEM_EMPTY(hi))
1895 {
1896 di = HI2DI(hi);
1897 if (var_check_fixed(di->di_flags, name, FALSE)
1898 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001899 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001900 return FAIL;
1901
1902 delete_var(ht, hi);
1903 return OK;
1904 }
1905 }
1906 if (forceit)
1907 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00001908 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001909 return FAIL;
1910}
1911
1912/*
1913 * Lock or unlock variable indicated by "lp".
1914 * "deep" is the levels to go (-1 for unlimited);
1915 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1916 */
1917 static int
1918do_lock_var(
1919 lval_T *lp,
1920 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001921 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001922 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001923 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001924{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001925 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001926 int ret = OK;
1927 int cc;
1928 dictitem_T *di;
1929
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001930 if (lp->ll_tv == NULL)
1931 {
1932 cc = *name_end;
1933 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001934 if (*lp->ll_name == '$')
1935 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001936 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001937 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001938 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001939 else
1940 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001941 // Normal name or expanded name.
1942 di = find_var(lp->ll_name, NULL, TRUE);
1943 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001944 {
1945 if (in_vim9script())
1946 semsg(_(e_cannot_find_variable_to_unlock_str),
1947 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001948 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001949 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001950 else if ((di->di_flags & DI_FLAGS_FIX)
1951 && di->di_tv.v_type != VAR_DICT
1952 && di->di_tv.v_type != VAR_LIST)
1953 {
1954 // For historic reasons this error is not given for a list or
1955 // dict. E.g., the b: dict could be locked/unlocked.
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001956 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001957 ret = FAIL;
1958 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001959 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001960 {
1961 if (lock)
1962 di->di_flags |= DI_FLAGS_LOCK;
1963 else
1964 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001965 if (deep != 0)
1966 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001967 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001968 }
1969 *name_end = cc;
1970 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001971 else if (deep == 0)
1972 {
1973 // nothing to do
1974 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001975 else if (lp->ll_range)
1976 {
1977 listitem_T *li = lp->ll_li;
1978
1979 // (un)lock a range of List items.
1980 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1981 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001982 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001983 li = li->li_next;
1984 ++lp->ll_n1;
1985 }
1986 }
1987 else if (lp->ll_list != NULL)
1988 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001989 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001990 else
1991 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001992 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001993
1994 return ret;
1995}
1996
1997/*
1998 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001999 * When "check_refcount" is TRUE do not lock a list or dict with a reference
2000 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002001 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002002 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02002003item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002004{
2005 static int recurse = 0;
2006 list_T *l;
2007 listitem_T *li;
2008 dict_T *d;
2009 blob_T *b;
2010 hashitem_T *hi;
2011 int todo;
2012
2013 if (recurse >= DICT_MAXNEST)
2014 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002015 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002016 return;
2017 }
2018 if (deep == 0)
2019 return;
2020 ++recurse;
2021
2022 // lock/unlock the item itself
2023 if (lock)
2024 tv->v_lock |= VAR_LOCKED;
2025 else
2026 tv->v_lock &= ~VAR_LOCKED;
2027
2028 switch (tv->v_type)
2029 {
2030 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002031 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002033 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002034 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002035 case VAR_STRING:
2036 case VAR_FUNC:
2037 case VAR_PARTIAL:
2038 case VAR_FLOAT:
2039 case VAR_SPECIAL:
2040 case VAR_JOB:
2041 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002042 case VAR_INSTR:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002043 break;
2044
2045 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002046 if ((b = tv->vval.v_blob) != NULL
2047 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002048 {
2049 if (lock)
2050 b->bv_lock |= VAR_LOCKED;
2051 else
2052 b->bv_lock &= ~VAR_LOCKED;
2053 }
2054 break;
2055 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002056 if ((l = tv->vval.v_list) != NULL
2057 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002058 {
2059 if (lock)
2060 l->lv_lock |= VAR_LOCKED;
2061 else
2062 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002063 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002064 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002065 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02002066 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002067 }
2068 break;
2069 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002070 if ((d = tv->vval.v_dict) != NULL
2071 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002072 {
2073 if (lock)
2074 d->dv_lock |= VAR_LOCKED;
2075 else
2076 d->dv_lock &= ~VAR_LOCKED;
2077 if (deep < 0 || deep > 1)
2078 {
2079 // recursive: lock/unlock the items the List contains
2080 todo = (int)d->dv_hashtab.ht_used;
2081 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2082 {
2083 if (!HASHITEM_EMPTY(hi))
2084 {
2085 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002086 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2087 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002088 }
2089 }
2090 }
2091 }
2092 }
2093 --recurse;
2094}
2095
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002096#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2097/*
2098 * Delete all "menutrans_" variables.
2099 */
2100 void
2101del_menutrans_vars(void)
2102{
2103 hashitem_T *hi;
2104 int todo;
2105
2106 hash_lock(&globvarht);
2107 todo = (int)globvarht.ht_used;
2108 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2109 {
2110 if (!HASHITEM_EMPTY(hi))
2111 {
2112 --todo;
2113 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2114 delete_var(&globvarht, hi);
2115 }
2116 }
2117 hash_unlock(&globvarht);
2118}
2119#endif
2120
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002121/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002122 * Local string buffer for the next two functions to store a variable name
2123 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2124 * get_user_var_name().
2125 */
2126
2127static char_u *varnamebuf = NULL;
2128static int varnamebuflen = 0;
2129
2130/*
2131 * Function to concatenate a prefix and a variable name.
2132 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002133 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002134cat_prefix_varname(int prefix, char_u *name)
2135{
2136 int len;
2137
2138 len = (int)STRLEN(name) + 3;
2139 if (len > varnamebuflen)
2140 {
2141 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002142 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002143 varnamebuf = alloc(len);
2144 if (varnamebuf == NULL)
2145 {
2146 varnamebuflen = 0;
2147 return NULL;
2148 }
2149 varnamebuflen = len;
2150 }
2151 *varnamebuf = prefix;
2152 varnamebuf[1] = ':';
2153 STRCPY(varnamebuf + 2, name);
2154 return varnamebuf;
2155}
2156
2157/*
2158 * Function given to ExpandGeneric() to obtain the list of user defined
2159 * (global/buffer/window/built-in) variable names.
2160 */
2161 char_u *
2162get_user_var_name(expand_T *xp, int idx)
2163{
2164 static long_u gdone;
2165 static long_u bdone;
2166 static long_u wdone;
2167 static long_u tdone;
2168 static int vidx;
2169 static hashitem_T *hi;
2170 hashtab_T *ht;
2171
2172 if (idx == 0)
2173 {
2174 gdone = bdone = wdone = vidx = 0;
2175 tdone = 0;
2176 }
2177
2178 // Global variables
2179 if (gdone < globvarht.ht_used)
2180 {
2181 if (gdone++ == 0)
2182 hi = globvarht.ht_array;
2183 else
2184 ++hi;
2185 while (HASHITEM_EMPTY(hi))
2186 ++hi;
2187 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2188 return cat_prefix_varname('g', hi->hi_key);
2189 return hi->hi_key;
2190 }
2191
2192 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002193 ht =
2194#ifdef FEAT_CMDWIN
2195 // In cmdwin, the alternative buffer should be used.
mityua1198122021-11-20 19:13:39 +00002196 is_in_cmdwin() ? &prevwin->w_buffer->b_vars->dv_hashtab :
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002197#endif
2198 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002199 if (bdone < ht->ht_used)
2200 {
2201 if (bdone++ == 0)
2202 hi = ht->ht_array;
2203 else
2204 ++hi;
2205 while (HASHITEM_EMPTY(hi))
2206 ++hi;
2207 return cat_prefix_varname('b', hi->hi_key);
2208 }
2209
2210 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002211 ht =
2212#ifdef FEAT_CMDWIN
2213 // In cmdwin, the alternative window should be used.
mityua1198122021-11-20 19:13:39 +00002214 is_in_cmdwin() ? &prevwin->w_vars->dv_hashtab :
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002215#endif
2216 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002217 if (wdone < ht->ht_used)
2218 {
2219 if (wdone++ == 0)
2220 hi = ht->ht_array;
2221 else
2222 ++hi;
2223 while (HASHITEM_EMPTY(hi))
2224 ++hi;
2225 return cat_prefix_varname('w', hi->hi_key);
2226 }
2227
2228 // t: variables
2229 ht = &curtab->tp_vars->dv_hashtab;
2230 if (tdone < ht->ht_used)
2231 {
2232 if (tdone++ == 0)
2233 hi = ht->ht_array;
2234 else
2235 ++hi;
2236 while (HASHITEM_EMPTY(hi))
2237 ++hi;
2238 return cat_prefix_varname('t', hi->hi_key);
2239 }
2240
2241 // v: variables
2242 if (vidx < VV_LEN)
2243 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2244
2245 VIM_CLEAR(varnamebuf);
2246 varnamebuflen = 0;
2247 return NULL;
2248}
2249
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002250 char *
2251get_var_special_name(int nr)
2252{
2253 switch (nr)
2254 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002255 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2256 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002257 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002258 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002259 }
2260 internal_error("get_var_special_name()");
2261 return "42";
2262}
2263
2264/*
2265 * Returns the global variable dictionary
2266 */
2267 dict_T *
2268get_globvar_dict(void)
2269{
2270 return &globvardict;
2271}
2272
2273/*
2274 * Returns the global variable hash table
2275 */
2276 hashtab_T *
2277get_globvar_ht(void)
2278{
2279 return &globvarht;
2280}
2281
2282/*
2283 * Returns the v: variable dictionary
2284 */
2285 dict_T *
2286get_vimvar_dict(void)
2287{
2288 return &vimvardict;
2289}
2290
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002291/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002293 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002294 */
2295 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002296find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002298 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2299 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300
2301 if (di == NULL)
2302 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002303 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002304 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2305 return (int)(vv - vimvars);
2306}
2307
2308
2309/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002310 * Set type of v: variable to "type".
2311 */
2312 void
2313set_vim_var_type(int idx, vartype_T type)
2314{
Bram Moolenaard787e402021-12-24 21:36:12 +00002315 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002316}
2317
2318/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002319 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002320 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002321 */
2322 void
2323set_vim_var_nr(int idx, varnumber_T val)
2324{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002325 vimvars[idx].vv_nr = val;
2326}
2327
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002328 char *
2329get_vim_var_name(int idx)
2330{
2331 return vimvars[idx].vv_name;
2332}
2333
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002334/*
2335 * Get typval_T v: variable value.
2336 */
2337 typval_T *
2338get_vim_var_tv(int idx)
2339{
2340 return &vimvars[idx].vv_tv;
2341}
2342
Bram Moolenaard787e402021-12-24 21:36:12 +00002343 type_T *
2344get_vim_var_type(int idx, garray_T *type_list)
2345{
2346 if (vimvars[idx].vv_type != NULL)
2347 return vimvars[idx].vv_type;
2348 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2349}
2350
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002351/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002352 * Set v: variable to "tv". Only accepts the same type.
2353 * Takes over the value of "tv".
2354 */
2355 int
2356set_vim_var_tv(int idx, typval_T *tv)
2357{
Bram Moolenaard787e402021-12-24 21:36:12 +00002358 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002359 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002360 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002361 clear_tv(tv);
2362 return FAIL;
2363 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002364 // VV_RO is also checked when compiling, but let's check here as well.
2365 if (vimvars[idx].vv_flags & VV_RO)
2366 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002367 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002368 return FAIL;
2369 }
2370 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2371 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002372 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002373 return FAIL;
2374 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002375 clear_tv(&vimvars[idx].vv_di.di_tv);
2376 vimvars[idx].vv_di.di_tv = *tv;
2377 return OK;
2378}
2379
2380/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002381 * Get number v: variable value.
2382 */
2383 varnumber_T
2384get_vim_var_nr(int idx)
2385{
2386 return vimvars[idx].vv_nr;
2387}
2388
2389/*
2390 * Get string v: variable value. Uses a static buffer, can only be used once.
2391 * If the String variable has never been set, return an empty string.
2392 * Never returns NULL;
2393 */
2394 char_u *
2395get_vim_var_str(int idx)
2396{
2397 return tv_get_string(&vimvars[idx].vv_tv);
2398}
2399
2400/*
2401 * Get List v: variable value. Caller must take care of reference count when
2402 * needed.
2403 */
2404 list_T *
2405get_vim_var_list(int idx)
2406{
2407 return vimvars[idx].vv_list;
2408}
2409
2410/*
2411 * Get Dict v: variable value. Caller must take care of reference count when
2412 * needed.
2413 */
2414 dict_T *
2415get_vim_var_dict(int idx)
2416{
2417 return vimvars[idx].vv_dict;
2418}
2419
2420/*
2421 * Set v:char to character "c".
2422 */
2423 void
2424set_vim_var_char(int c)
2425{
2426 char_u buf[MB_MAXBYTES + 1];
2427
2428 if (has_mbyte)
2429 buf[(*mb_char2bytes)(c, buf)] = NUL;
2430 else
2431 {
2432 buf[0] = c;
2433 buf[1] = NUL;
2434 }
2435 set_vim_var_string(VV_CHAR, buf, -1);
2436}
2437
2438/*
2439 * Set v:count to "count" and v:count1 to "count1".
2440 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2441 */
2442 void
2443set_vcount(
2444 long count,
2445 long count1,
2446 int set_prevcount)
2447{
2448 if (set_prevcount)
2449 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2450 vimvars[VV_COUNT].vv_nr = count;
2451 vimvars[VV_COUNT1].vv_nr = count1;
2452}
2453
2454/*
2455 * Save variables that might be changed as a side effect. Used when executing
2456 * a timer callback.
2457 */
2458 void
2459save_vimvars(vimvars_save_T *vvsave)
2460{
2461 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2462 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2463 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2464}
2465
2466/*
2467 * Restore variables saved by save_vimvars().
2468 */
2469 void
2470restore_vimvars(vimvars_save_T *vvsave)
2471{
2472 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2473 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2474 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2475}
2476
2477/*
2478 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2479 * value.
2480 */
2481 void
2482set_vim_var_string(
2483 int idx,
2484 char_u *val,
2485 int len) // length of "val" to use or -1 (whole string)
2486{
2487 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002488 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002489 if (val == NULL)
2490 vimvars[idx].vv_str = NULL;
2491 else if (len == -1)
2492 vimvars[idx].vv_str = vim_strsave(val);
2493 else
2494 vimvars[idx].vv_str = vim_strnsave(val, len);
2495}
2496
2497/*
2498 * Set List v: variable to "val".
2499 */
2500 void
2501set_vim_var_list(int idx, list_T *val)
2502{
2503 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002504 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002505 vimvars[idx].vv_list = val;
2506 if (val != NULL)
2507 ++val->lv_refcount;
2508}
2509
2510/*
2511 * Set Dictionary v: variable to "val".
2512 */
2513 void
2514set_vim_var_dict(int idx, dict_T *val)
2515{
2516 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002517 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002518 vimvars[idx].vv_dict = val;
2519 if (val != NULL)
2520 {
2521 ++val->dv_refcount;
2522 dict_set_items_ro(val);
2523 }
2524}
2525
2526/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002527 * Set the v:argv list.
2528 */
2529 void
2530set_argv_var(char **argv, int argc)
2531{
2532 list_T *l = list_alloc();
2533 int i;
2534
2535 if (l == NULL)
2536 getout(1);
2537 l->lv_lock = VAR_FIXED;
2538 for (i = 0; i < argc; ++i)
2539 {
2540 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2541 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002542 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002543 }
2544 set_vim_var_list(VV_ARGV, l);
2545}
2546
2547/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002548 * Reset v:register, taking the 'clipboard' setting into account.
2549 */
2550 void
2551reset_reg_var(void)
2552{
2553 int regname = 0;
2554
2555 // Adjust the register according to 'clipboard', so that when
2556 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2557#ifdef FEAT_CLIPBOARD
2558 adjust_clip_reg(&regname);
2559#endif
2560 set_reg_var(regname);
2561}
2562
2563/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002564 * Set v:register if needed.
2565 */
2566 void
2567set_reg_var(int c)
2568{
2569 char_u regname;
2570
2571 if (c == 0 || c == ' ')
2572 regname = '"';
2573 else
2574 regname = c;
2575 // Avoid free/alloc when the value is already right.
2576 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2577 set_vim_var_string(VV_REG, &regname, 1);
2578}
2579
2580/*
2581 * Get or set v:exception. If "oldval" == NULL, return the current value.
2582 * Otherwise, restore the value to "oldval" and return NULL.
2583 * Must always be called in pairs to save and restore v:exception! Does not
2584 * take care of memory allocations.
2585 */
2586 char_u *
2587v_exception(char_u *oldval)
2588{
2589 if (oldval == NULL)
2590 return vimvars[VV_EXCEPTION].vv_str;
2591
2592 vimvars[VV_EXCEPTION].vv_str = oldval;
2593 return NULL;
2594}
2595
2596/*
2597 * Get or set v:throwpoint. 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:throwpoint! Does not
2600 * take care of memory allocations.
2601 */
2602 char_u *
2603v_throwpoint(char_u *oldval)
2604{
2605 if (oldval == NULL)
2606 return vimvars[VV_THROWPOINT].vv_str;
2607
2608 vimvars[VV_THROWPOINT].vv_str = oldval;
2609 return NULL;
2610}
2611
2612/*
2613 * Set v:cmdarg.
2614 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2615 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2616 * Must always be called in pairs!
2617 */
2618 char_u *
2619set_cmdarg(exarg_T *eap, char_u *oldarg)
2620{
2621 char_u *oldval;
2622 char_u *newval;
2623 unsigned len;
2624
2625 oldval = vimvars[VV_CMDARG].vv_str;
2626 if (eap == NULL)
2627 {
2628 vim_free(oldval);
2629 vimvars[VV_CMDARG].vv_str = oldarg;
2630 return NULL;
2631 }
2632
2633 if (eap->force_bin == FORCE_BIN)
2634 len = 6;
2635 else if (eap->force_bin == FORCE_NOBIN)
2636 len = 8;
2637 else
2638 len = 0;
2639
2640 if (eap->read_edit)
2641 len += 7;
2642
2643 if (eap->force_ff != 0)
2644 len += 10; // " ++ff=unix"
2645 if (eap->force_enc != 0)
2646 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2647 if (eap->bad_char != 0)
2648 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2649
2650 newval = alloc(len + 1);
2651 if (newval == NULL)
2652 return NULL;
2653
2654 if (eap->force_bin == FORCE_BIN)
2655 sprintf((char *)newval, " ++bin");
2656 else if (eap->force_bin == FORCE_NOBIN)
2657 sprintf((char *)newval, " ++nobin");
2658 else
2659 *newval = NUL;
2660
2661 if (eap->read_edit)
2662 STRCAT(newval, " ++edit");
2663
2664 if (eap->force_ff != 0)
2665 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2666 eap->force_ff == 'u' ? "unix"
2667 : eap->force_ff == 'd' ? "dos"
2668 : "mac");
2669 if (eap->force_enc != 0)
2670 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2671 eap->cmd + eap->force_enc);
2672 if (eap->bad_char == BAD_KEEP)
2673 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2674 else if (eap->bad_char == BAD_DROP)
2675 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2676 else if (eap->bad_char != 0)
2677 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2678 vimvars[VV_CMDARG].vv_str = newval;
2679 return oldval;
2680}
2681
2682/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002683 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002684 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2685 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002686 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2687 */
2688 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002689eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002690 char_u *name,
2691 int len, // length of "name"
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002692 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002693 typval_T *rettv, // NULL when only checking existence
2694 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002695 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002696{
2697 int ret = OK;
2698 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002699 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002700 hashtab_T *ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002701 int cc;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002702 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002703
2704 // truncate the name, so that we can use strcmp()
2705 cc = name[len];
2706 name[len] = NUL;
2707
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002708 // Check for local variable when debugging.
2709 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002710 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002711 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002712 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2713
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002714 if (v != NULL)
2715 {
2716 tv = &v->di_tv;
2717 if (dip != NULL)
2718 *dip = v;
2719 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002720 else
2721 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002722 }
2723
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002724 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002726 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002727 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2728
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002729 if (sid == 0)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002730 import = find_imported(p, 0, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002731
2732 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002733 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002735 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002736 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00002737 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02002738 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002739 ht = &SCRIPT_VARS(sid);
2740 if (ht != NULL)
2741 {
2742 dictitem_T *v = find_var_in_ht(ht, 0, name,
2743 flags & EVAL_VAR_NOAUTOLOAD);
2744
2745 if (v != NULL)
2746 {
2747 tv = &v->di_tv;
2748 if (dip != NULL)
2749 *dip = v;
2750 }
2751 else
2752 ht = NULL;
2753 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002754 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002755 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002756 {
2757 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00002758 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002759 ret = FAIL;
2760 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002761 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002762 else
2763 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002764 if (rettv != NULL)
2765 {
2766 rettv->v_type = VAR_ANY;
2767 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
2768 }
2769 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002770 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00002772 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002773 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002774 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002775
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002776 // In Vim9 script we can get a function reference by using the
2777 // function name.
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002778 if (ufunc != NULL)
2779 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002780 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002781 if (rettv != NULL)
2782 {
2783 rettv->v_type = VAR_FUNC;
Bram Moolenaaref082e12021-12-12 21:02:03 +00002784 if (STRNCMP(name, "g:", 2) == 0)
2785 // Keep the "g:", otherwise script-local may be
2786 // assumed.
2787 rettv->vval.v_string = vim_strsave(name);
2788 else
2789 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002790 if (rettv->vval.v_string != NULL)
2791 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002792 }
2793 }
2794 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002795 }
2796
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002797 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002798 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002799 if (tv == NULL)
2800 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002801 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002802 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002803 ret = FAIL;
2804 }
2805 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002806 {
Bram Moolenaar11005b02021-07-11 20:59:00 +02002807 if (ht != NULL && ht == get_script_local_ht()
2808 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002809 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002810 svar_T *sv = find_typval_in_script(tv, 0);
Bram Moolenaarf055d452021-07-08 20:57:24 +02002811
Bram Moolenaarf055d452021-07-08 20:57:24 +02002812 if (sv != NULL)
2813 type = sv->sv_type;
2814 }
2815
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002816 // If a list or dict variable wasn't initialized, do it now.
2817 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2818 {
2819 tv->vval.v_dict = dict_alloc();
2820 if (tv->vval.v_dict != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002821 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002822 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002823 tv->vval.v_dict->dv_type = alloc_type(type);
2824 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002825 }
2826 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2827 {
2828 tv->vval.v_list = list_alloc();
2829 if (tv->vval.v_list != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002830 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002831 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002832 tv->vval.v_list->lv_type = alloc_type(type);
2833 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002834 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002835 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL)
2836 {
2837 tv->vval.v_blob = blob_alloc();
2838 if (tv->vval.v_blob != NULL)
2839 ++tv->vval.v_blob->bv_refcount;
2840 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002841 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002842 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002843 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002844
2845 name[len] = cc;
2846
2847 return ret;
2848}
2849
2850/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002851 * Check if variable "name[len]" is a local variable or an argument.
2852 * If so, "*eval_lavars_used" is set to TRUE.
2853 */
2854 void
2855check_vars(char_u *name, int len)
2856{
2857 int cc;
2858 char_u *varname;
2859 hashtab_T *ht;
2860
2861 if (eval_lavars_used == NULL)
2862 return;
2863
2864 // truncate the name, so that we can use strcmp()
2865 cc = name[len];
2866 name[len] = NUL;
2867
2868 ht = find_var_ht(name, &varname);
2869 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2870 {
2871 if (find_var(name, NULL, TRUE) != NULL)
2872 *eval_lavars_used = TRUE;
2873 }
2874
2875 name[len] = cc;
2876}
2877
2878/*
2879 * Find variable "name" in the list of variables.
2880 * Return a pointer to it if found, NULL if not found.
2881 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002882 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002883 */
2884 dictitem_T *
2885find_var(char_u *name, hashtab_T **htp, int no_autoload)
2886{
2887 char_u *varname;
2888 hashtab_T *ht;
2889 dictitem_T *ret = NULL;
2890
2891 ht = find_var_ht(name, &varname);
2892 if (htp != NULL)
2893 *htp = ht;
2894 if (ht == NULL)
2895 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002896 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002897 if (ret != NULL)
2898 return ret;
2899
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002900 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002901 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002902 if (ret != NULL)
2903 return ret;
2904
2905 // in Vim9 script items without a scope can be script-local
2906 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2907 {
2908 ht = get_script_local_ht();
2909 if (ht != NULL)
2910 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002911 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002912 if (ret != NULL)
2913 {
2914 if (htp != NULL)
2915 *htp = ht;
2916 return ret;
2917 }
2918 }
2919 }
2920
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002921 // When using "vim9script autoload" script-local items are prefixed but can
2922 // be used with s:name.
2923 if (SCRIPT_ID_VALID(current_sctx.sc_sid)
2924 && name[0] == 's' && name[1] == ':')
2925 {
2926 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2927
2928 if (si->sn_autoload_prefix != NULL)
2929 {
2930 char_u *auto_name = concat_str(si->sn_autoload_prefix, name + 2);
2931
2932 if (auto_name != NULL)
2933 {
2934 ht = &globvarht;
2935 ret = find_var_in_ht(ht, *name, auto_name, TRUE);
Bram Moolenaar130f65d2022-01-13 20:39:41 +00002936 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002937 if (ret != NULL)
2938 {
2939 if (htp != NULL)
2940 *htp = ht;
2941 return ret;
2942 }
2943 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002944 }
2945 }
2946
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002947 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002948}
2949
2950/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00002951 * Like find_var() but if the name starts with <SNR>99_ then look in the
2952 * referenced script (used for a funcref).
2953 */
2954 dictitem_T *
2955find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
2956{
2957 if (STRNCMP(name, "<SNR>", 5) == 0 && isdigit(name[5]))
2958 {
2959 char_u *p = name + 5;
2960 int sid = getdigits(&p);
2961
2962 if (SCRIPT_ID_VALID(sid) && *p == '_')
2963 {
2964 hashtab_T *ht = &SCRIPT_VARS(sid);
2965
2966 if (ht != NULL)
2967 {
2968 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
2969
2970 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00002971 {
2972 if (htp != NULL)
2973 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00002974 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00002975 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00002976 }
2977 }
2978 }
2979
2980 return find_var(name, htp, no_autoload);
2981}
2982
2983/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002984 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002985 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002986 * Returns NULL if not found.
2987 */
2988 dictitem_T *
2989find_var_in_ht(
2990 hashtab_T *ht,
2991 int htname,
2992 char_u *varname,
2993 int no_autoload)
2994{
2995 hashitem_T *hi;
2996
2997 if (*varname == NUL)
2998 {
2999 // Must be something like "s:", otherwise "ht" would be NULL.
3000 switch (htname)
3001 {
3002 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
3003 case 'g': return &globvars_var;
3004 case 'v': return &vimvars_var;
3005 case 'b': return &curbuf->b_bufvar;
3006 case 'w': return &curwin->w_winvar;
3007 case 't': return &curtab->tp_winvar;
3008 case 'l': return get_funccal_local_var();
3009 case 'a': return get_funccal_args_var();
3010 }
3011 return NULL;
3012 }
3013
3014 hi = hash_find(ht, varname);
3015 if (HASHITEM_EMPTY(hi))
3016 {
3017 // For global variables we may try auto-loading the script. If it
3018 // worked find the variable again. Don't auto-load a script if it was
3019 // loaded already, otherwise it would be loaded every time when
3020 // checking if a function name is a Funcref variable.
3021 if (ht == &globvarht && !no_autoload)
3022 {
3023 // Note: script_autoload() may make "hi" invalid. It must either
3024 // be obtained again or not used.
3025 if (!script_autoload(varname, FALSE) || aborting())
3026 return NULL;
3027 hi = hash_find(ht, varname);
3028 }
3029 if (HASHITEM_EMPTY(hi))
3030 return NULL;
3031 }
3032 return HI2DI(hi);
3033}
3034
3035/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 * Get the script-local hashtab. NULL if not in a script context.
3037 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003038 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039get_script_local_ht(void)
3040{
3041 scid_T sid = current_sctx.sc_sid;
3042
Bram Moolenaare3d46852020-08-29 13:39:17 +02003043 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 return &SCRIPT_VARS(sid);
3045 return NULL;
3046}
3047
3048/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003049 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003050 * When "cmd" is TRUE it must look like a command, a function must be followed
3051 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003052 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003054 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003055lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003056 char_u *name,
3057 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003058 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003059 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060{
3061 hashtab_T *ht = get_script_local_ht();
3062 char_u buffer[30];
3063 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003064 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003066 int is_global = FALSE;
3067 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068
3069 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003070 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 if (len < sizeof(buffer) - 1)
3072 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003073 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 vim_strncpy(buffer, name, len);
3075 p = buffer;
3076 }
3077 else
3078 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003079 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003081 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082 }
3083
3084 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003085 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086
3087 // if not script-local, then perhaps imported
Bram Moolenaardc4451d2022-01-09 21:36:37 +00003088 if (res == FAIL && find_imported(p, 0, FALSE, NULL) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003089 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 if (p != buffer)
3091 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003092
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003093 // Find a function, so that a following "->" works.
3094 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3095 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003096 if (res != OK)
3097 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003098 p = skipwhite(name + len);
3099
3100 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003101 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003102 // Do not check for an internal function, since it might also be a
3103 // valid command, such as ":split" versus "split()".
3104 // Skip "g:" before a function name.
3105 if (name[0] == 'g' && name[1] == ':')
3106 {
3107 is_global = TRUE;
3108 fname = name + 2;
3109 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003110 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003111 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003112 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003113 }
3114
Bram Moolenaar709664c2020-12-12 14:33:41 +01003115 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116}
3117
3118/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003119 * Find the hashtab used for a variable name.
3120 * Return NULL if the name is not valid.
3121 * Set "varname" to the start of name without ':'.
3122 */
3123 hashtab_T *
3124find_var_ht(char_u *name, char_u **varname)
3125{
3126 hashitem_T *hi;
3127 hashtab_T *ht;
3128
3129 if (name[0] == NUL)
3130 return NULL;
3131 if (name[1] != ':')
3132 {
3133 // The name must not start with a colon or #.
3134 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3135 return NULL;
3136 *varname = name;
3137
3138 // "version" is "v:version" in all scopes if scriptversion < 3.
3139 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003140 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003141 {
3142 hi = hash_find(&compat_hashtab, name);
3143 if (!HASHITEM_EMPTY(hi))
3144 return &compat_hashtab;
3145 }
3146
3147 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 if (ht != NULL)
3149 return ht; // local variable
3150
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003151 // In Vim9 script items at the script level are script-local, except
3152 // for autoload names.
3153 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 {
3155 ht = get_script_local_ht();
3156 if (ht != NULL)
3157 return ht;
3158 }
3159
3160 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003161 }
3162 *varname = name + 2;
3163 if (*name == 'g') // global variable
3164 return &globvarht;
3165 // There must be no ':' or '#' in the rest of the name, unless g: is used
3166 if (vim_strchr(name + 2, ':') != NULL
3167 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3168 return NULL;
3169 if (*name == 'b') // buffer variable
3170 return &curbuf->b_vars->dv_hashtab;
3171 if (*name == 'w') // window variable
3172 return &curwin->w_vars->dv_hashtab;
3173 if (*name == 't') // tab page variable
3174 return &curtab->tp_vars->dv_hashtab;
3175 if (*name == 'v') // v: variable
3176 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003177 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003178 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003180 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 if (*name == 'a') // a: function argument
3182 return get_funccal_args_ht();
3183 if (*name == 'l') // l: local function variable
3184 return get_funccal_local_ht();
3185 }
3186 if (*name == 's') // script variable
3187 {
3188 ht = get_script_local_ht();
3189 if (ht != NULL)
3190 return ht;
3191 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003192 return NULL;
3193}
3194
3195/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003196 * Get the string value of a (global/local) variable.
3197 * Note: see tv_get_string() for how long the pointer remains valid.
3198 * Returns NULL when it doesn't exist.
3199 */
3200 char_u *
3201get_var_value(char_u *name)
3202{
3203 dictitem_T *v;
3204
3205 v = find_var(name, NULL, FALSE);
3206 if (v == NULL)
3207 return NULL;
3208 return tv_get_string(&v->di_tv);
3209}
3210
3211/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003212 * Allocate a new hashtab for a sourced script. It will be used while
3213 * sourcing this script and when executing functions defined in the script.
3214 */
3215 void
3216new_script_vars(scid_T id)
3217{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003218 scriptvar_T *sv;
3219
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003220 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3221 if (sv == NULL)
3222 return;
3223 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003224 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003225}
3226
3227/*
3228 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3229 * point to it.
3230 */
3231 void
3232init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3233{
3234 hash_init(&dict->dv_hashtab);
3235 dict->dv_lock = 0;
3236 dict->dv_scope = scope;
3237 dict->dv_refcount = DO_NOT_FREE_CNT;
3238 dict->dv_copyID = 0;
3239 dict_var->di_tv.vval.v_dict = dict;
3240 dict_var->di_tv.v_type = VAR_DICT;
3241 dict_var->di_tv.v_lock = VAR_FIXED;
3242 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3243 dict_var->di_key[0] = NUL;
3244}
3245
3246/*
3247 * Unreference a dictionary initialized by init_var_dict().
3248 */
3249 void
3250unref_var_dict(dict_T *dict)
3251{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003252 // Now the dict needs to be freed if no one else is using it, go back to
3253 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003254 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3255 dict_unref(dict);
3256}
3257
3258/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003259 * Clean up a list of internal variables.
3260 * Frees all allocated variables and the value they contain.
3261 * Clears hashtab "ht", does not free it.
3262 */
3263 void
3264vars_clear(hashtab_T *ht)
3265{
3266 vars_clear_ext(ht, TRUE);
3267}
3268
3269/*
3270 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3271 */
3272 void
3273vars_clear_ext(hashtab_T *ht, int free_val)
3274{
3275 int todo;
3276 hashitem_T *hi;
3277 dictitem_T *v;
3278
3279 hash_lock(ht);
3280 todo = (int)ht->ht_used;
3281 for (hi = ht->ht_array; todo > 0; ++hi)
3282 {
3283 if (!HASHITEM_EMPTY(hi))
3284 {
3285 --todo;
3286
3287 // Free the variable. Don't remove it from the hashtab,
3288 // ht_array might change then. hash_clear() takes care of it
3289 // later.
3290 v = HI2DI(hi);
3291 if (free_val)
3292 clear_tv(&v->di_tv);
3293 if (v->di_flags & DI_FLAGS_ALLOC)
3294 vim_free(v);
3295 }
3296 }
3297 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003298 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003299}
3300
3301/*
3302 * Delete a variable from hashtab "ht" at item "hi".
3303 * Clear the variable value and free the dictitem.
3304 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003305 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003306delete_var(hashtab_T *ht, hashitem_T *hi)
3307{
3308 dictitem_T *di = HI2DI(hi);
3309
3310 hash_remove(ht, hi);
3311 clear_tv(&di->di_tv);
3312 vim_free(di);
3313}
3314
3315/*
3316 * List the value of one internal variable.
3317 */
3318 static void
3319list_one_var(dictitem_T *v, char *prefix, int *first)
3320{
3321 char_u *tofree;
3322 char_u *s;
3323 char_u numbuf[NUMBUFLEN];
3324
3325 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3326 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3327 s == NULL ? (char_u *)"" : s, first);
3328 vim_free(tofree);
3329}
3330
3331 static void
3332list_one_var_a(
3333 char *prefix,
3334 char_u *name,
3335 int type,
3336 char_u *string,
3337 int *first) // when TRUE clear rest of screen and set to FALSE
3338{
3339 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3340 msg_start();
3341 msg_puts(prefix);
3342 if (name != NULL) // "a:" vars don't have a name stored
3343 msg_puts((char *)name);
3344 msg_putchar(' ');
3345 msg_advance(22);
3346 if (type == VAR_NUMBER)
3347 msg_putchar('#');
3348 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3349 msg_putchar('*');
3350 else if (type == VAR_LIST)
3351 {
3352 msg_putchar('[');
3353 if (*string == '[')
3354 ++string;
3355 }
3356 else if (type == VAR_DICT)
3357 {
3358 msg_putchar('{');
3359 if (*string == '{')
3360 ++string;
3361 }
3362 else
3363 msg_putchar(' ');
3364
3365 msg_outtrans(string);
3366
3367 if (type == VAR_FUNC || type == VAR_PARTIAL)
3368 msg_puts("()");
3369 if (*first)
3370 {
3371 msg_clr_eos();
3372 *first = FALSE;
3373 }
3374}
3375
3376/*
3377 * Set variable "name" to value in "tv".
3378 * If the variable already exists, the value is updated.
3379 * Otherwise the variable is created.
3380 */
3381 void
3382set_var(
3383 char_u *name,
3384 typval_T *tv,
3385 int copy) // make copy of value in "tv"
3386{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003387 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003388}
3389
3390/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003391 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003392 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003393 * If the variable already exists and "is_const" is FALSE the value is updated.
3394 * Otherwise the variable is created.
3395 */
3396 void
3397set_var_const(
3398 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003399 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003400 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003401 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003402 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003403 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003404 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003405{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003406 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003407 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003408 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 dictitem_T *di;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003410 typval_T *dest_tv = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003411 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003412 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003413 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003415 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003416 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003417 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003418 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003419 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003420
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003421 if (sid != 0)
3422 {
3423 if (SCRIPT_ID_VALID(sid))
3424 ht = &SCRIPT_VARS(sid);
3425 varname = name;
3426 }
3427 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003428 {
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003429 scriptitem_T *si;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003430
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003431 if (in_vim9script() && is_export
3432 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3433 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
3434 ->sn_autoload_prefix != NULL)
3435 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003436 // In a vim9 autoload script an exported variable is put in the
3437 // global namespace with the autoload prefix.
3438 var_in_autoload = TRUE;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003439 varname = concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003440 if (varname == NULL)
3441 goto failed;
3442 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003443 ht = &globvarht;
3444 }
3445 else
3446 ht = find_var_ht(name, &varname);
3447 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003448 if (ht == NULL || *varname == NUL)
3449 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003450 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003451 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003452 }
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003453 is_script_local = ht == get_script_local_ht() || sid != 0 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003454
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003455 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003456 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003457 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003458 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003459 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003460 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003461 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003462 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003463 }
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003464 if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
3465 && vim_strchr((char_u *)"gwbt", name[0]) != NULL)
3466 // Do not make g:var, w:var, b:var or t:var final.
3467 flags &= ~ASSIGN_FINAL;
3468
Bram Moolenaare535db82021-03-31 21:07:24 +02003469 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003470 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3471 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003472 // For "[a, _] = list" the underscore is ignored.
3473 if ((flags & ASSIGN_UNPACK) == 0)
3474 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003475 goto failed;
3476 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003477
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003479
Bram Moolenaar24e93162021-07-18 20:40:33 +02003480 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003481 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +00003482 imported_T *import = find_imported(varname, 0, FALSE, NULL);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003483
Bram Moolenaar24e93162021-07-18 20:40:33 +02003484 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003485 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003486 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02003487 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003489 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003490 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003492 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3493 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003494 }
3495 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496
Bram Moolenaar24e93162021-07-18 20:40:33 +02003497 if (dest_tv == NULL)
3498 {
3499 // Search in parent scope which is possible to reference from lambda
3500 if (di == NULL)
3501 di = find_var_in_scoped_ht(name, TRUE);
3502
3503 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003504 && var_wrong_func_name(name, di == NULL))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003505 goto failed;
3506
3507 if (need_convert_to_bool(type, tv))
3508 {
Bram Moolenaarf6488542021-07-18 21:24:50 +02003509 // Destination is a bool and the value is not, but it can be
3510 // converted.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003511 CLEAR_FIELD(bool_tv);
3512 bool_tv.v_type = VAR_BOOL;
3513 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3514 tv = &bool_tv;
3515 }
3516
3517 if (di != NULL)
3518 {
3519 // Item already exists. Allowed to replace when reloading.
3520 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
3521 {
3522 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaarf6488542021-07-18 21:24:50 +02003523 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003524 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003525 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar24e93162021-07-18 20:40:33 +02003526 goto failed;
3527 }
3528
3529 if (is_script_local && vim9script
Bram Moolenaarf6488542021-07-18 21:24:50 +02003530 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003531 {
3532 semsg(_(e_redefining_script_item_str), name);
3533 goto failed;
3534 }
3535
Bram Moolenaara06758d2021-10-15 00:18:37 +01003536 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003537 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003538 where_T where = WHERE_INIT;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003539 svar_T *sv = find_typval_in_script(&di->di_tv, sid);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003540
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003541 if (sv != NULL)
3542 {
3543 // check the type and adjust to bool if needed
3544 where.wt_index = var_idx;
3545 where.wt_variable = TRUE;
3546 if (check_script_var_type(sv, tv, name, where) == FAIL)
3547 goto failed;
3548 if (type == NULL)
3549 type = sv->sv_type;
3550 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003551 }
3552
Bram Moolenaara06758d2021-10-15 00:18:37 +01003553 if ((flags & ASSIGN_FOR_LOOP) == 0
3554 && var_check_permission(di, name) == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003555 goto failed;
3556 }
3557 else
3558 {
3559 // can only redefine once
3560 di->di_flags &= ~DI_FLAGS_RELOAD;
3561
Bram Moolenaarf6488542021-07-18 21:24:50 +02003562 // A Vim9 script-local variable is also present in sn_all_vars
3563 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003564 if (var_in_vim9script || var_in_autoload)
3565 update_vim9_script_var(FALSE, di,
3566 var_in_autoload ? name : di->di_key, flags,
3567 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003568 }
3569
3570 // existing variable, need to clear the value
3571
3572 // Handle setting internal di: variables separately where needed to
3573 // prevent changing the type.
3574 if (ht == &vimvarht)
3575 {
3576 if (di->di_tv.v_type == VAR_STRING)
3577 {
3578 VIM_CLEAR(di->di_tv.vval.v_string);
3579 if (copy || tv->v_type != VAR_STRING)
3580 {
3581 char_u *val = tv_get_string(tv);
3582
Bram Moolenaarf6488542021-07-18 21:24:50 +02003583 // Careful: when assigning to v:errmsg and
3584 // tv_get_string() causes an error message the variable
3585 // will already be set.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003586 if (di->di_tv.vval.v_string == NULL)
3587 di->di_tv.vval.v_string = vim_strsave(val);
3588 }
3589 else
3590 {
3591 // Take over the string to avoid an extra alloc/free.
3592 di->di_tv.vval.v_string = tv->vval.v_string;
3593 tv->vval.v_string = NULL;
3594 }
3595 goto failed;
3596 }
3597 else if (di->di_tv.v_type == VAR_NUMBER)
3598 {
3599 di->di_tv.vval.v_number = tv_get_number(tv);
3600 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003601 set_search_direction(di->di_tv.vval.v_number
3602 ? '/' : '?');
Bram Moolenaar24e93162021-07-18 20:40:33 +02003603#ifdef FEAT_SEARCH_EXTRA
3604 else if (STRCMP(varname, "hlsearch") == 0)
3605 {
3606 no_hlsearch = !di->di_tv.vval.v_number;
3607 redraw_all_later(SOME_VALID);
3608 }
3609#endif
3610 goto failed;
3611 }
3612 else if (di->di_tv.v_type != tv->v_type)
3613 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003614 semsg(_(e_setting_str_to_value_with_wrong_type), name);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003615 goto failed;
3616 }
3617 }
3618
3619 clear_tv(&di->di_tv);
3620 }
3621 else
3622 {
3623 // Item not found, check if a function already exists.
3624 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaarf6488542021-07-18 21:24:50 +02003625 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003626 {
3627 semsg(_(e_redefining_script_item_str), name);
3628 goto failed;
3629 }
3630
Bram Moolenaar24e93162021-07-18 20:40:33 +02003631 // add a new variable
3632 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3633 {
3634 semsg(_(e_unknown_variable_str), name);
3635 goto failed;
3636 }
3637
3638 // Can't add "v:" or "a:" variable.
3639 if (ht == &vimvarht || ht == get_funccal_args_ht())
3640 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003641 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003642 goto failed;
3643 }
3644
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003645 // Make sure the variable name is valid. In Vim9 script an
3646 // autoload variable must be prefixed with "g:" unless in an
3647 // autoload script.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003648 if (!valid_varname(varname, -1, !vim9script
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003649 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003650 goto failed;
3651
3652 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3653 if (di == NULL)
3654 goto failed;
3655 STRCPY(di->di_key, varname);
3656 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
3657 {
3658 vim_free(di);
3659 goto failed;
3660 }
3661 di->di_flags = DI_FLAGS_ALLOC;
3662 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3663 di->di_flags |= DI_FLAGS_LOCK;
3664
3665 // A Vim9 script-local variable is also added to sn_all_vars and
3666 // sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003667 if (var_in_vim9script || var_in_autoload)
3668 update_vim9_script_var(TRUE, di,
3669 var_in_autoload ? name : di->di_key, flags,
3670 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003671 }
3672
Bram Moolenaar24e93162021-07-18 20:40:33 +02003673 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003674 }
3675
3676 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003677 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003678 else
3679 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003680 *dest_tv = *tv;
3681 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003682 init_tv(tv);
3683 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003684 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003685
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003686 if (vim9script && type != NULL)
3687 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003688 if (type->tt_type == VAR_DICT && dest_tv->vval.v_dict != NULL)
Bram Moolenaar464393a2021-09-11 23:07:44 +02003689 {
3690 if (dest_tv->vval.v_dict->dv_type != type)
3691 {
3692 free_type(dest_tv->vval.v_dict->dv_type);
3693 dest_tv->vval.v_dict->dv_type = alloc_type(type);
3694 }
3695 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003696 else if (type->tt_type == VAR_LIST && dest_tv->vval.v_list != NULL)
Bram Moolenaar464393a2021-09-11 23:07:44 +02003697 {
3698 if (dest_tv->vval.v_list->lv_type != type)
3699 {
3700 free_type(dest_tv->vval.v_list->lv_type);
3701 dest_tv->vval.v_list->lv_type = alloc_type(type);
3702 }
3703 }
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003704 }
3705
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003706 // ":const var = value" locks the value
3707 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003708 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003709 // Like :lockvar! name: lock the value and what it contains, but only
3710 // if the reference count is up to one. That locks only literal
3711 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003712 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003713
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003714failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003715 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003716 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003717 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003718}
3719
3720/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003721 * Check in this order for backwards compatibility:
3722 * - Whether the variable is read-only
3723 * - Whether the variable value is locked
3724 * - Whether the variable is locked
3725 */
3726 int
3727var_check_permission(dictitem_T *di, char_u *name)
3728{
3729 if (var_check_ro(di->di_flags, name, FALSE)
3730 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3731 || var_check_lock(di->di_flags, name, FALSE))
3732 return FAIL;
3733 return OK;
3734}
3735
3736/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003737 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3738 * Also give an error message.
3739 */
3740 int
3741var_check_ro(int flags, char_u *name, int use_gettext)
3742{
3743 if (flags & DI_FLAGS_RO)
3744 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003745 if (name == NULL)
3746 emsg(_(e_cannot_change_readonly_variable));
3747 else
3748 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02003749 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003750 return TRUE;
3751 }
3752 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3753 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003754 if (name == NULL)
3755 emsg(_(e_cannot_set_variable_in_sandbox));
3756 else
3757 semsg(_(e_cannot_set_variable_in_sandbox_str),
3758 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003759 return TRUE;
3760 }
3761 return FALSE;
3762}
3763
3764/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003765 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3766 * Also give an error message.
3767 */
3768 int
3769var_check_lock(int flags, char_u *name, int use_gettext)
3770{
3771 if (flags & DI_FLAGS_LOCK)
3772 {
3773 semsg(_(e_variable_is_locked_str),
3774 use_gettext ? (char_u *)_(name) : name);
3775 return TRUE;
3776 }
3777 return FALSE;
3778}
3779
3780/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003781 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3782 * Also give an error message.
3783 */
3784 int
3785var_check_fixed(int flags, char_u *name, int use_gettext)
3786{
3787 if (flags & DI_FLAGS_FIX)
3788 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003789 if (name == NULL)
3790 emsg(_(e_cannot_delete_variable));
3791 else
3792 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003793 use_gettext ? (char_u *)_(name) : name);
3794 return TRUE;
3795 }
3796 return FALSE;
3797}
3798
3799/*
3800 * Check if a funcref is assigned to a valid variable name.
3801 * Return TRUE and give an error if not.
3802 */
3803 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003804var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003805 char_u *name, // points to start of variable name
3806 int new_var) // TRUE when creating the variable
3807{
Bram Moolenaar32154662021-03-28 21:14:06 +02003808 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
3809 // the name can be used without the s: prefix.
3810 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
3811 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003812 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3813 ? name[2] : name[0]))
3814 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003815 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003816 return TRUE;
3817 }
3818 // Don't allow hiding a function. When "v" is not NULL we might be
3819 // assigning another function to the same var, the type is checked
3820 // below.
3821 if (new_var && function_exists(name, FALSE))
3822 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003823 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003824 name);
3825 return TRUE;
3826 }
3827 return FALSE;
3828}
3829
3830/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003831 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3832 * value. Also give an error message, using "name" or _("name") when
3833 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003834 */
3835 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003836value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003837{
3838 if (lock & VAR_LOCKED)
3839 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003840 if (name == NULL)
3841 emsg(_(e_value_is_locked));
3842 else
3843 semsg(_(e_value_is_locked_str),
3844 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003845 return TRUE;
3846 }
3847 if (lock & VAR_FIXED)
3848 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003849 if (name == NULL)
3850 emsg(_(e_cannot_change_value));
3851 else
3852 semsg(_(e_cannot_change_value_of_str),
3853 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003854 return TRUE;
3855 }
3856 return FALSE;
3857}
3858
3859/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003860 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003861 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003862 * Return FALSE and give an error if not.
3863 */
3864 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003865valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003866{
3867 char_u *p;
3868
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003869 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003870 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003871 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003872 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003873 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003874 return FALSE;
3875 }
3876 return TRUE;
3877}
3878
3879/*
3880 * getwinvar() and gettabwinvar()
3881 */
3882 static void
3883getwinvar(
3884 typval_T *argvars,
3885 typval_T *rettv,
3886 int off) // 1 for gettabwinvar()
3887{
3888 win_T *win;
3889 char_u *varname;
3890 dictitem_T *v;
3891 tabpage_T *tp = NULL;
3892 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00003893 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003894 int need_switch_win;
3895
3896 if (off == 1)
3897 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3898 else
3899 tp = curtab;
3900 win = find_win_by_nr(&argvars[off], tp);
3901 varname = tv_get_string_chk(&argvars[off + 1]);
3902 ++emsg_off;
3903
3904 rettv->v_type = VAR_STRING;
3905 rettv->vval.v_string = NULL;
3906
3907 if (win != NULL && varname != NULL)
3908 {
3909 // Set curwin to be our win, temporarily. Also set the tabpage,
3910 // otherwise the window is not valid. Only do this when needed,
3911 // autocommands get blocked.
3912 need_switch_win = !(tp == curtab && win == curwin);
3913 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00003914 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003915 {
3916 if (*varname == '&')
3917 {
3918 if (varname[1] == NUL)
3919 {
3920 // get all window-local options in a dict
3921 dict_T *opts = get_winbuf_options(FALSE);
3922
3923 if (opts != NULL)
3924 {
3925 rettv_dict_set(rettv, opts);
3926 done = TRUE;
3927 }
3928 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003929 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003930 // window-local-option
3931 done = TRUE;
3932 }
3933 else
3934 {
3935 // Look up the variable.
3936 // Let getwinvar({nr}, "") return the "w:" dictionary.
3937 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3938 varname, FALSE);
3939 if (v != NULL)
3940 {
3941 copy_tv(&v->di_tv, rettv);
3942 done = TRUE;
3943 }
3944 }
3945 }
3946
3947 if (need_switch_win)
3948 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00003949 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003950 }
3951
3952 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3953 // use the default return value
3954 copy_tv(&argvars[off + 2], rettv);
3955
3956 --emsg_off;
3957}
3958
3959/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003960 * Set option "varname" to the value of "varp" for the current buffer/window.
3961 */
3962 static void
3963set_option_from_tv(char_u *varname, typval_T *varp)
3964{
3965 long numval = 0;
3966 char_u *strval;
3967 char_u nbuf[NUMBUFLEN];
3968 int error = FALSE;
3969
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003970 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003971 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003972 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003973 strval = (char_u *)"0"; // avoid using "false"
3974 }
3975 else
3976 {
3977 if (!in_vim9script() || varp->v_type != VAR_STRING)
3978 numval = (long)tv_get_number_chk(varp, &error);
3979 strval = tv_get_string_buf_chk(varp, nbuf);
3980 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02003981 if (!error && strval != NULL)
3982 set_option_value(varname, numval, strval, OPT_LOCAL);
3983}
3984
3985/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003986 * "setwinvar()" and "settabwinvar()" functions
3987 */
3988 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003989setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003990{
3991 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00003992 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003993 int need_switch_win;
3994 char_u *varname, *winvarname;
3995 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003996 tabpage_T *tp = NULL;
3997
3998 if (check_secure())
3999 return;
4000
4001 if (off == 1)
4002 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4003 else
4004 tp = curtab;
4005 win = find_win_by_nr(&argvars[off], tp);
4006 varname = tv_get_string_chk(&argvars[off + 1]);
4007 varp = &argvars[off + 2];
4008
4009 if (win != NULL && varname != NULL && varp != NULL)
4010 {
4011 need_switch_win = !(tp == curtab && win == curwin);
4012 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00004013 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004014 {
4015 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02004016 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004017 else
4018 {
4019 winvarname = alloc(STRLEN(varname) + 3);
4020 if (winvarname != NULL)
4021 {
4022 STRCPY(winvarname, "w:");
4023 STRCPY(winvarname + 2, varname);
4024 set_var(winvarname, varp, TRUE);
4025 vim_free(winvarname);
4026 }
4027 }
4028 }
4029 if (need_switch_win)
Bram Moolenaar18f47402022-01-06 13:24:51 +00004030 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004031 }
4032}
4033
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004034/*
4035 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4036 * v:option_type, and v:option_command.
4037 */
4038 void
4039reset_v_option_vars(void)
4040{
4041 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4042 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4043 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4044 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4045 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4046 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4047}
4048
4049/*
4050 * Add an assert error to v:errors.
4051 */
4052 void
4053assert_error(garray_T *gap)
4054{
4055 struct vimvar *vp = &vimvars[VV_ERRORS];
4056
Bram Moolenaard787e402021-12-24 21:36:12 +00004057 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004058 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004059 set_vim_var_list(VV_ERRORS, list_alloc());
4060 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4061}
4062
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004063 int
4064var_exists(char_u *var)
4065{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004066 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004067 char_u *name;
4068 char_u *tofree;
4069 typval_T tv;
4070 int len = 0;
4071 int n = FALSE;
4072
4073 // get_name_len() takes care of expanding curly braces
4074 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004075 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004076 if (len > 0)
4077 {
4078 if (tofree != NULL)
4079 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004080 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004081 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004082 if (n)
4083 {
4084 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004085 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004086 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4087 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004088 if (n)
4089 clear_tv(&tv);
4090 }
4091 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004092 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004093 n = FALSE;
4094
4095 vim_free(tofree);
4096 return n;
4097}
4098
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004099static lval_T *redir_lval = NULL;
4100#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4101static garray_T redir_ga; // only valid when redir_lval is not NULL
4102static char_u *redir_endp = NULL;
4103static char_u *redir_varname = NULL;
4104
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004105 int
4106alloc_redir_lval(void)
4107{
4108 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4109 if (redir_lval == NULL)
4110 return FAIL;
4111 return OK;
4112}
4113
4114 void
4115clear_redir_lval(void)
4116{
4117 VIM_CLEAR(redir_lval);
4118}
4119
4120 void
4121init_redir_ga(void)
4122{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004123 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004124}
4125
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004126/*
4127 * Start recording command output to a variable
4128 * When "append" is TRUE append to an existing variable.
4129 * Returns OK if successfully completed the setup. FAIL otherwise.
4130 */
4131 int
4132var_redir_start(char_u *name, int append)
4133{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004134 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004135 typval_T tv;
4136
4137 // Catch a bad name early.
4138 if (!eval_isnamec1(*name))
4139 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004140 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004141 return FAIL;
4142 }
4143
4144 // Make a copy of the name, it is used in redir_lval until redir ends.
4145 redir_varname = vim_strsave(name);
4146 if (redir_varname == NULL)
4147 return FAIL;
4148
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004149 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004150 {
4151 var_redir_stop();
4152 return FAIL;
4153 }
4154
4155 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004156 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004157
4158 // Parse the variable name (can be a dict or list entry).
4159 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4160 FNE_CHECK_START);
4161 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4162 {
4163 clear_lval(redir_lval);
4164 if (redir_endp != NULL && *redir_endp != NUL)
4165 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004166 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004167 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004168 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004169 redir_endp = NULL; // don't store a value, only cleanup
4170 var_redir_stop();
4171 return FAIL;
4172 }
4173
4174 // check if we can write to the variable: set it to or append an empty
4175 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004176 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004177 tv.v_type = VAR_STRING;
4178 tv.vval.v_string = (char_u *)"";
4179 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004180 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004181 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004182 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004183 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004184 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004185 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004186 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004187 {
4188 redir_endp = NULL; // don't store a value, only cleanup
4189 var_redir_stop();
4190 return FAIL;
4191 }
4192
4193 return OK;
4194}
4195
4196/*
4197 * Append "value[value_len]" to the variable set by var_redir_start().
4198 * The actual appending is postponed until redirection ends, because the value
4199 * appended may in fact be the string we write to, changing it may cause freed
4200 * memory to be used:
4201 * :redir => foo
4202 * :let foo
4203 * :redir END
4204 */
4205 void
4206var_redir_str(char_u *value, int value_len)
4207{
4208 int len;
4209
4210 if (redir_lval == NULL)
4211 return;
4212
4213 if (value_len == -1)
4214 len = (int)STRLEN(value); // Append the entire string
4215 else
4216 len = value_len; // Append only "value_len" characters
4217
4218 if (ga_grow(&redir_ga, len) == OK)
4219 {
4220 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4221 redir_ga.ga_len += len;
4222 }
4223 else
4224 var_redir_stop();
4225}
4226
4227/*
4228 * Stop redirecting command output to a variable.
4229 * Frees the allocated memory.
4230 */
4231 void
4232var_redir_stop(void)
4233{
4234 typval_T tv;
4235
4236 if (EVALCMD_BUSY)
4237 {
4238 redir_lval = NULL;
4239 return;
4240 }
4241
4242 if (redir_lval != NULL)
4243 {
4244 // If there was no error: assign the text to the variable.
4245 if (redir_endp != NULL)
4246 {
4247 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4248 tv.v_type = VAR_STRING;
4249 tv.vval.v_string = redir_ga.ga_data;
4250 // Call get_lval() again, if it's inside a Dict or List it may
4251 // have changed.
4252 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4253 FALSE, FALSE, 0, FNE_CHECK_START);
4254 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004256 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004257 clear_lval(redir_lval);
4258 }
4259
4260 // free the collected output
4261 VIM_CLEAR(redir_ga.ga_data);
4262
4263 VIM_CLEAR(redir_lval);
4264 }
4265 VIM_CLEAR(redir_varname);
4266}
4267
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004268/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004269 * Get the collected redirected text and clear redir_ga.
4270 */
4271 char_u *
4272get_clear_redir_ga(void)
4273{
4274 char_u *res;
4275
4276 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4277 res = redir_ga.ga_data;
4278 redir_ga.ga_data = NULL;
4279 return res;
4280}
4281
4282/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004283 * "gettabvar()" function
4284 */
4285 void
4286f_gettabvar(typval_T *argvars, typval_T *rettv)
4287{
Bram Moolenaar18f47402022-01-06 13:24:51 +00004288 switchwin_T switchwin;
4289 tabpage_T *tp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004290 dictitem_T *v;
4291 char_u *varname;
4292 int done = FALSE;
4293
4294 rettv->v_type = VAR_STRING;
4295 rettv->vval.v_string = NULL;
4296
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004297 if (in_vim9script()
4298 && (check_for_number_arg(argvars, 0) == FAIL
4299 || check_for_string_arg(argvars, 1) == FAIL))
4300 return;
4301
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004302 varname = tv_get_string_chk(&argvars[1]);
4303 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4304 if (tp != NULL && varname != NULL)
4305 {
4306 // Set tp to be our tabpage, temporarily. Also set the window to the
4307 // first window in the tabpage, otherwise the window is not valid.
Bram Moolenaar18f47402022-01-06 13:24:51 +00004308 if (switch_win(&switchwin,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004309 tp == curtab || tp->tp_firstwin == NULL ? firstwin
4310 : tp->tp_firstwin, tp, TRUE) == OK)
4311 {
4312 // look up the variable
4313 // Let gettabvar({nr}, "") return the "t:" dictionary.
4314 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
4315 if (v != NULL)
4316 {
4317 copy_tv(&v->di_tv, rettv);
4318 done = TRUE;
4319 }
4320 }
4321
4322 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004323 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004324 }
4325
4326 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4327 copy_tv(&argvars[2], rettv);
4328}
4329
4330/*
4331 * "gettabwinvar()" function
4332 */
4333 void
4334f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4335{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004336 if (in_vim9script()
4337 && (check_for_number_arg(argvars, 0) == FAIL
4338 || check_for_number_arg(argvars, 1) == FAIL
4339 || check_for_string_arg(argvars, 2) == FAIL))
4340 return;
4341
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004342 getwinvar(argvars, rettv, 1);
4343}
4344
4345/*
4346 * "getwinvar()" function
4347 */
4348 void
4349f_getwinvar(typval_T *argvars, typval_T *rettv)
4350{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004351 if (in_vim9script()
4352 && (check_for_number_arg(argvars, 0) == FAIL
4353 || check_for_string_arg(argvars, 1) == FAIL))
4354 return;
4355
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004356 getwinvar(argvars, rettv, 0);
4357}
4358
4359/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004360 * "getbufvar()" function
4361 */
4362 void
4363f_getbufvar(typval_T *argvars, typval_T *rettv)
4364{
4365 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004366 char_u *varname;
4367 dictitem_T *v;
4368 int done = FALSE;
4369
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004370 if (in_vim9script()
4371 && (check_for_buffer_arg(argvars, 0) == FAIL
4372 || check_for_string_arg(argvars, 1) == FAIL))
4373 return;
4374
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004375 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004376 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004377
4378 rettv->v_type = VAR_STRING;
4379 rettv->vval.v_string = NULL;
4380
4381 if (buf != NULL && varname != NULL)
4382 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004383 if (*varname == '&')
4384 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004385 buf_T *save_curbuf = curbuf;
4386
4387 // set curbuf to be our buf, temporarily
4388 curbuf = buf;
4389
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004390 if (varname[1] == NUL)
4391 {
4392 // get all buffer-local options in a dict
4393 dict_T *opts = get_winbuf_options(TRUE);
4394
4395 if (opts != NULL)
4396 {
4397 rettv_dict_set(rettv, opts);
4398 done = TRUE;
4399 }
4400 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004401 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004402 // buffer-local-option
4403 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02004404
4405 // restore previous notion of curbuf
4406 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004407 }
4408 else
4409 {
4410 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02004411 if (*varname == NUL)
4412 // Let getbufvar({nr}, "") return the "b:" dictionary.
4413 v = &buf->b_bufvar;
4414 else
4415 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
4416 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004417 if (v != NULL)
4418 {
4419 copy_tv(&v->di_tv, rettv);
4420 done = TRUE;
4421 }
4422 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004423 }
4424
4425 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4426 // use the default value
4427 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004428}
4429
4430/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004431 * "settabvar()" function
4432 */
4433 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004434f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004435{
4436 tabpage_T *save_curtab;
4437 tabpage_T *tp;
4438 char_u *varname, *tabvarname;
4439 typval_T *varp;
4440
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004441 if (check_secure())
4442 return;
4443
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004444 if (in_vim9script()
4445 && (check_for_number_arg(argvars, 0) == FAIL
4446 || check_for_string_arg(argvars, 1) == FAIL))
4447 return;
4448
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004449 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4450 varname = tv_get_string_chk(&argvars[1]);
4451 varp = &argvars[2];
4452
4453 if (varname != NULL && varp != NULL && tp != NULL)
4454 {
4455 save_curtab = curtab;
4456 goto_tabpage_tp(tp, FALSE, FALSE);
4457
4458 tabvarname = alloc(STRLEN(varname) + 3);
4459 if (tabvarname != NULL)
4460 {
4461 STRCPY(tabvarname, "t:");
4462 STRCPY(tabvarname + 2, varname);
4463 set_var(tabvarname, varp, TRUE);
4464 vim_free(tabvarname);
4465 }
4466
4467 // Restore current tabpage
4468 if (valid_tabpage(save_curtab))
4469 goto_tabpage_tp(save_curtab, FALSE, FALSE);
4470 }
4471}
4472
4473/*
4474 * "settabwinvar()" function
4475 */
4476 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004477f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004478{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004479 if (in_vim9script()
4480 && (check_for_number_arg(argvars, 0) == FAIL
4481 || check_for_number_arg(argvars, 1) == FAIL
4482 || check_for_string_arg(argvars, 2) == FAIL))
4483 return;
4484
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004485 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004486}
4487
4488/*
4489 * "setwinvar()" function
4490 */
4491 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004492f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004493{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004494 if (in_vim9script()
4495 && (check_for_number_arg(argvars, 0) == FAIL
4496 || check_for_string_arg(argvars, 1) == FAIL))
4497 return;
4498
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004499 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004500}
4501
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004502/*
4503 * "setbufvar()" function
4504 */
4505 void
4506f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4507{
4508 buf_T *buf;
4509 char_u *varname, *bufvarname;
4510 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004511
4512 if (check_secure())
4513 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004514
4515 if (in_vim9script()
4516 && (check_for_buffer_arg(argvars, 0) == FAIL
4517 || check_for_string_arg(argvars, 1) == FAIL))
4518 return;
4519
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004520 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004521 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004522 varp = &argvars[2];
4523
4524 if (buf != NULL && varname != NULL && varp != NULL)
4525 {
4526 if (*varname == '&')
4527 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004528 aco_save_T aco;
4529
4530 // set curbuf to be our buf, temporarily
4531 aucmd_prepbuf(&aco, buf);
4532
Bram Moolenaar191929b2020-08-19 21:20:49 +02004533 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004534
4535 // reset notion of buffer
4536 aucmd_restbuf(&aco);
4537 }
4538 else
4539 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004540 bufvarname = alloc(STRLEN(varname) + 3);
4541 if (bufvarname != NULL)
4542 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004543 buf_T *save_curbuf = curbuf;
4544
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004545 curbuf = buf;
4546 STRCPY(bufvarname, "b:");
4547 STRCPY(bufvarname + 2, varname);
4548 set_var(bufvarname, varp, TRUE);
4549 vim_free(bufvarname);
4550 curbuf = save_curbuf;
4551 }
4552 }
4553 }
4554}
4555
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004556/*
4557 * Get a callback from "arg". It can be a Funcref or a function name.
4558 * When "arg" is zero return an empty string.
4559 * "cb_name" is not allocated.
4560 * "cb_name" is set to NULL for an invalid argument.
4561 */
4562 callback_T
4563get_callback(typval_T *arg)
4564{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004565 callback_T res;
4566 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004567
4568 res.cb_free_name = FALSE;
4569 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4570 {
4571 res.cb_partial = arg->vval.v_partial;
4572 ++res.cb_partial->pt_refcount;
4573 res.cb_name = partial_name(res.cb_partial);
4574 }
4575 else
4576 {
4577 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004578 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4579 && isdigit(*arg->vval.v_string))
4580 r = FAIL;
4581 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004582 {
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004583 if (arg->v_type == VAR_STRING)
4584 {
4585 char_u *name;
4586
4587 name = get_scriptlocal_funcname(arg->vval.v_string);
4588 if (name != NULL)
4589 {
4590 vim_free(arg->vval.v_string);
4591 arg->vval.v_string = name;
4592 }
4593 }
4594
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004595 res.cb_name = arg->vval.v_string;
4596 func_ref(res.cb_name);
4597 }
4598 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004599 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004600 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004601 r = FAIL;
4602
4603 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004604 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004605 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004606 res.cb_name = NULL;
4607 }
4608 }
4609 return res;
4610}
4611
4612/*
4613 * Copy a callback into a typval_T.
4614 */
4615 void
4616put_callback(callback_T *cb, typval_T *tv)
4617{
4618 if (cb->cb_partial != NULL)
4619 {
4620 tv->v_type = VAR_PARTIAL;
4621 tv->vval.v_partial = cb->cb_partial;
4622 ++tv->vval.v_partial->pt_refcount;
4623 }
4624 else
4625 {
4626 tv->v_type = VAR_FUNC;
4627 tv->vval.v_string = vim_strsave(cb->cb_name);
4628 func_ref(cb->cb_name);
4629 }
4630}
4631
4632/*
4633 * Make a copy of "src" into "dest", allocating the function name if needed,
4634 * without incrementing the refcount.
4635 */
4636 void
4637set_callback(callback_T *dest, callback_T *src)
4638{
4639 if (src->cb_partial == NULL)
4640 {
4641 // just a function name, make a copy
4642 dest->cb_name = vim_strsave(src->cb_name);
4643 dest->cb_free_name = TRUE;
4644 }
4645 else
4646 {
4647 // cb_name is a pointer into cb_partial
4648 dest->cb_name = src->cb_name;
4649 dest->cb_free_name = FALSE;
4650 }
4651 dest->cb_partial = src->cb_partial;
4652}
4653
4654/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004655 * Copy callback from "src" to "dest", incrementing the refcounts.
4656 */
4657 void
4658copy_callback(callback_T *dest, callback_T *src)
4659{
4660 dest->cb_partial = src->cb_partial;
4661 if (dest->cb_partial != NULL)
4662 {
4663 dest->cb_name = src->cb_name;
4664 dest->cb_free_name = FALSE;
4665 ++dest->cb_partial->pt_refcount;
4666 }
4667 else
4668 {
4669 dest->cb_name = vim_strsave(src->cb_name);
4670 dest->cb_free_name = TRUE;
4671 func_ref(src->cb_name);
4672 }
4673}
4674
4675/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004676 * When a callback refers to an autoload import, change the function name to
4677 * the "path#name" form. Uses the current script context.
4678 * Only works when the name is allocated.
4679 */
4680 void
4681expand_autload_callback(callback_T *cb)
4682{
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004683 char_u *name;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004684 char_u *p;
4685 imported_T *import;
4686
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004687 if (!in_vim9script() || cb->cb_name == NULL
4688 || (!cb->cb_free_name
4689 && (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004690 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004691 if (cb->cb_partial != NULL)
4692 name = cb->cb_partial->pt_name;
4693 else
4694 name = cb->cb_name;
4695 p = vim_strchr(name, '.');
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004696 if (p == NULL)
4697 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004698 import = find_imported(name, p - name, FALSE, NULL);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004699 if (import != NULL && SCRIPT_ID_VALID(import->imp_sid))
4700 {
4701 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
4702
4703 if (si->sn_autoload_prefix != NULL)
4704 {
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004705 char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004706
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004707 if (newname != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004708 {
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004709 if (cb->cb_partial != NULL)
4710 {
4711 if (cb->cb_name == cb->cb_partial->pt_name)
4712 cb->cb_name = newname;
4713 vim_free(cb->cb_partial->pt_name);
4714 cb->cb_partial->pt_name = newname;
4715 }
4716 else
4717 {
4718 vim_free(cb->cb_name);
4719 cb->cb_name = newname;
4720 }
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004721 }
4722 }
4723 }
4724}
4725
4726/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004727 * Unref/free "callback" returned by get_callback() or set_callback().
4728 */
4729 void
4730free_callback(callback_T *callback)
4731{
4732 if (callback->cb_partial != NULL)
4733 {
4734 partial_unref(callback->cb_partial);
4735 callback->cb_partial = NULL;
4736 }
4737 else if (callback->cb_name != NULL)
4738 func_unref(callback->cb_name);
4739 if (callback->cb_free_name)
4740 {
4741 vim_free(callback->cb_name);
4742 callback->cb_free_name = FALSE;
4743 }
4744 callback->cb_name = NULL;
4745}
4746
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004747#endif // FEAT_EVAL