blob: 305ffe548863b44eea94b79ae17a3f07033fa139 [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);
498
499 // Set "v:val" to the bad word.
500 prepare_vimvar(VV_VAL, &save_val);
501 set_vim_var_string(VV_VAL, badword, -1);
502 if (p_verbose == 0)
503 ++emsg_off;
504
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200505 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200506 {
507 if (rettv.v_type != VAR_LIST)
508 clear_tv(&rettv);
509 else
510 list = rettv.vval.v_list;
511 }
512
513 if (p_verbose == 0)
514 --emsg_off;
515 clear_tv(get_vim_var_tv(VV_VAL));
516 restore_vimvar(VV_VAL, &save_val);
517
518 return list;
519}
520
521/*
522 * "list" is supposed to contain two items: a word and a number. Return the
523 * word in "pp" and the number as the return value.
524 * Return -1 if anything isn't right.
525 * Used to get the good word and score from the eval_spell_expr() result.
526 */
527 int
528get_spellword(list_T *list, char_u **pp)
529{
530 listitem_T *li;
531
532 li = list->lv_first;
533 if (li == NULL)
534 return -1;
535 *pp = tv_get_string(&li->li_tv);
536
537 li = li->li_next;
538 if (li == NULL)
539 return -1;
540 return (int)tv_get_number(&li->li_tv);
541}
542#endif
543
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200544/*
545 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200546 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200547 * When not used yet add the variable to the v: hashtable.
548 */
549 void
550prepare_vimvar(int idx, typval_T *save_tv)
551{
552 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200553 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaard787e402021-12-24 21:36:12 +0000554 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200555 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
556}
557
558/*
559 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200560 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200561 * When no longer defined, remove the variable from the v: hashtable.
562 */
563 void
564restore_vimvar(int idx, typval_T *save_tv)
565{
566 hashitem_T *hi;
567
568 vimvars[idx].vv_tv = *save_tv;
Bram Moolenaard787e402021-12-24 21:36:12 +0000569 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200570 {
571 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
572 if (HASHITEM_EMPTY(hi))
573 internal_error("restore_vimvar()");
574 else
575 hash_remove(&vimvarht, hi);
576 }
577}
578
579/*
580 * List Vim variables.
581 */
582 static void
583list_vim_vars(int *first)
584{
585 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
586}
587
588/*
589 * List script-local variables, if there is a script.
590 */
591 static void
592list_script_vars(int *first)
593{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200594 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200595 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
596 "s:", FALSE, first);
597}
598
599/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200600 * Get a list of lines from a HERE document. The here document is a list of
601 * lines surrounded by a marker.
602 * cmd << {marker}
603 * {line1}
604 * {line2}
605 * ....
606 * {marker}
607 *
608 * The {marker} is a string. If the optional 'trim' word is supplied before the
609 * marker, then the leading indentation before the lines (matching the
610 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200611 *
612 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
613 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
614 * missing, then '.' is accepted as a marker.
615 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200616 * Returns a List with {lines} or NULL.
617 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200619heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200620{
621 char_u *theline;
622 char_u *marker;
623 list_T *l;
624 char_u *p;
625 int marker_indent_len = 0;
626 int text_indent_len = 0;
627 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200628 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200629 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200630
631 if (eap->getline == NULL)
632 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000633 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200634 return NULL;
635 }
636
637 // Check for the optional 'trim' word before the marker
638 cmd = skipwhite(cmd);
639 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
640 {
641 cmd = skipwhite(cmd + 4);
642
643 // Trim the indentation from all the lines in the here document.
644 // The amount of indentation trimmed is the same as the indentation of
645 // the first line after the :let command line. To find the end marker
646 // the indent of the :let command line is trimmed.
647 p = *eap->cmdlinep;
648 while (VIM_ISWHITE(*p))
649 {
650 p++;
651 marker_indent_len++;
652 }
653 text_indent_len = -1;
654 }
655
656 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200657 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200658 {
659 marker = skipwhite(cmd);
660 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200661 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200662 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000663 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200664 return NULL;
665 }
666 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200667 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200668 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000669 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200670 return NULL;
671 }
672 }
673 else
674 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200675 // When getting lines for an embedded script, if the marker is missing,
676 // accept '.' as the marker.
677 if (script_get)
678 marker = dot;
679 else
680 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000681 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200682 return NULL;
683 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200684 }
685
686 l = list_alloc();
687 if (l == NULL)
688 return NULL;
689
690 for (;;)
691 {
692 int mi = 0;
693 int ti = 0;
694
695 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
696 if (theline == NULL)
697 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000698 semsg(_(e_missing_end_marker_str), marker);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200699 break;
700 }
701
702 // with "trim": skip the indent matching the :let line to find the
703 // marker
704 if (marker_indent_len > 0
705 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
706 mi = marker_indent_len;
707 if (STRCMP(marker, theline + mi) == 0)
708 {
709 vim_free(theline);
710 break;
711 }
712
713 if (text_indent_len == -1 && *theline != NUL)
714 {
715 // set the text indent from the first line.
716 p = theline;
717 text_indent_len = 0;
718 while (VIM_ISWHITE(*p))
719 {
720 p++;
721 text_indent_len++;
722 }
723 text_indent = vim_strnsave(theline, text_indent_len);
724 }
725 // with "trim": skip the indent matching the first line
726 if (text_indent != NULL)
727 for (ti = 0; ti < text_indent_len; ++ti)
728 if (theline[ti] != text_indent[ti])
729 break;
730
731 if (list_append_string(l, theline + ti, -1) == FAIL)
732 break;
733 vim_free(theline);
734 }
735 vim_free(text_indent);
736
737 return l;
738}
739
740/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200741 * Vim9 variable declaration:
742 * ":var name"
743 * ":var name: type"
744 * ":var name = expr"
745 * ":var name: type = expr"
746 * etc.
747 */
748 void
749ex_var(exarg_T *eap)
750{
751 if (!in_vim9script())
752 {
753 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
754 return;
755 }
756 ex_let(eap);
757}
758
759/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200760 * ":let" list all variable values
761 * ":let var1 var2" list variable values
762 * ":let var = expr" assignment command.
763 * ":let var += expr" assignment command.
764 * ":let var -= expr" assignment command.
765 * ":let var *= expr" assignment command.
766 * ":let var /= expr" assignment command.
767 * ":let var %= expr" assignment command.
768 * ":let var .= expr" assignment command.
769 * ":let var ..= expr" assignment command.
770 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100771 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100772 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200773 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200774 * ":final var = expr" assignment command.
775 * ":final [var1, var2] = expr" unpack list.
776 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200777 * ":const" list all variable values
778 * ":const var1 var2" list variable values
779 * ":const var = expr" assignment command.
780 * ":const [var1, var2] = expr" unpack list.
781 */
782 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200783ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200784{
785 char_u *arg = eap->arg;
786 char_u *expr = NULL;
787 typval_T rettv;
788 int i;
789 int var_count = 0;
790 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200791 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200792 char_u *argend;
793 int first = TRUE;
794 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200795 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100796 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200797 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200798
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200799 if (eap->cmdidx == CMD_final && !vim9script)
800 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100801 // In legacy Vim script ":final" is short for ":finally".
802 ex_finally(eap);
803 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200804 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200805 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200806 {
807 emsg(_(e_cannot_use_let_in_vim9_script));
808 return;
809 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200810
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100811 if (eap->cmdidx == CMD_const)
812 flags |= ASSIGN_CONST;
813 else if (eap->cmdidx == CMD_final)
814 flags |= ASSIGN_FINAL;
815
816 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200818 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100819
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200820 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200821 if (argend == NULL)
822 return;
823 if (argend > arg && argend[-1] == '.') // for var.='str'
824 --argend;
825 expr = skipwhite(argend);
826 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +0200827 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200828 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200829 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
830 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200831 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200832 {
833 // ":let" without "=": list variables
834 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000835 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200836 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000837 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200838 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200839 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200840 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200841 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100842 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +0000843 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100844 else
845 // Vim9 declaration ":var name: type"
846 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200847 }
848 else
849 {
850 // ":let var1 var2" - list values
851 arg = list_arg_vars(eap, arg, &first);
852 }
853 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200854 else if (!eap->skip)
855 {
856 // ":let"
857 list_glob_vars(&first);
858 list_buf_vars(&first);
859 list_win_vars(&first);
860 list_tab_vars(&first);
861 list_script_vars(&first);
862 list_func_vars(&first);
863 list_vim_vars(&first);
864 }
Bram Moolenaar63b91732021-08-05 20:40:03 +0200865 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200866 }
867 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
868 {
869 list_T *l;
Bram Moolenaar81530e32021-07-28 21:25:49 +0200870 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200871
872 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200873 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200874 if (l != NULL)
875 {
876 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200877 if (!eap->skip)
878 {
Bram Moolenaar81530e32021-07-28 21:25:49 +0200879 // errors are for the assignment, not the end marker
880 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200881 op[0] = '=';
882 op[1] = NUL;
883 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100884 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200885 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200886 clear_tv(&rettv);
887 }
888 }
889 else
890 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200891 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200892 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200893
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200894 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200895 i = FAIL;
896 if (has_assign || concat)
897 {
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100898 int cur_lnum;
899
Bram Moolenaar32e35112020-05-14 22:41:15 +0200900 op[0] = '=';
901 op[1] = NUL;
902 if (*expr != '=')
903 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200904 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200905 {
906 // +=, /=, etc. require an existing variable
907 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
908 i = FAIL;
909 }
910 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200911 {
912 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200913 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200914 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200915 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200916 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200917 ++len;
918 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200919 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200920 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200921 }
922 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200923 ++expr;
924
Bram Moolenaar7f2c3412021-11-29 16:01:49 +0000925 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200926 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200927 {
928 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100929 semsg(_(e_white_space_required_before_and_after_str_at_str),
930 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200931 i = FAIL;
932 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200933
934 if (eap->skip)
935 ++emsg_skip;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200936 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200937 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100938 cur_lnum = SOURCING_LNUM;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200939 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200940 if (eap->skip)
941 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200942 clear_evalarg(&evalarg, eap);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100943
944 // Restore the line number so that any type error is given for the
945 // declaration, not the expression.
946 SOURCING_LNUM = cur_lnum;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200947 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200948 if (eap->skip)
949 {
950 if (i != FAIL)
951 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200952 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200953 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200954 {
955 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200956 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200957 clear_tv(&rettv);
958 }
959 }
960}
961
962/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +0100963 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200964 * Handles both "var" with any type and "[var, var; var]" with a list type.
965 * When "op" is not NULL it points to a string with characters that
966 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
967 * or concatenate.
968 * Returns OK or FAIL;
969 */
970 int
971ex_let_vars(
972 char_u *arg_start,
973 typval_T *tv,
974 int copy, // copy values from "tv", don't move
975 int semicolon, // from skip_var_list()
976 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100977 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200978 char_u *op)
979{
980 char_u *arg = arg_start;
981 list_T *l;
982 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100983 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200984 listitem_T *item;
985 typval_T ltv;
986
987 if (*arg != '[')
988 {
989 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100990 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200991 return FAIL;
992 return OK;
993 }
994
995 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
996 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
997 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000998 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200999 return FAIL;
1000 }
1001
1002 i = list_len(l);
1003 if (semicolon == 0 && var_count < i)
1004 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001005 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001006 return FAIL;
1007 }
1008 if (var_count - semicolon > i)
1009 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001010 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001011 return FAIL;
1012 }
1013
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001014 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001015 item = l->lv_first;
1016 while (*arg != ']')
1017 {
1018 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001019 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001020 arg = ex_let_one(arg, &item->li_tv, TRUE,
1021 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001022 item = item->li_next;
1023 if (arg == NULL)
1024 return FAIL;
1025
1026 arg = skipwhite(arg);
1027 if (*arg == ';')
1028 {
1029 // Put the rest of the list (may be empty) in the var after ';'.
1030 // Create a new list for this.
1031 l = list_alloc();
1032 if (l == NULL)
1033 return FAIL;
1034 while (item != NULL)
1035 {
1036 list_append_tv(l, &item->li_tv);
1037 item = item->li_next;
1038 }
1039
1040 ltv.v_type = VAR_LIST;
1041 ltv.v_lock = 0;
1042 ltv.vval.v_list = l;
1043 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001044 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001045
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001046 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1047 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001048 clear_tv(&ltv);
1049 if (arg == NULL)
1050 return FAIL;
1051 break;
1052 }
1053 else if (*arg != ',' && *arg != ']')
1054 {
1055 internal_error("ex_let_vars()");
1056 return FAIL;
1057 }
1058 }
1059
1060 return OK;
1061}
1062
1063/*
1064 * Skip over assignable variable "var" or list of variables "[var, var]".
1065 * Used for ":let varvar = expr" and ":for varvar in expr".
1066 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001067 * for "[var, var; var]" set "semicolon" to 1.
1068 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001069 * Return NULL for an error.
1070 */
1071 char_u *
1072skip_var_list(
1073 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001074 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001075 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001076 int *semicolon,
1077 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001078{
1079 char_u *p, *s;
1080
1081 if (*arg == '[')
1082 {
1083 // "[var, var]": find the matching ']'.
1084 p = arg;
1085 for (;;)
1086 {
1087 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001088 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001089 if (s == p)
1090 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001091 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001092 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001093 return NULL;
1094 }
1095 ++*var_count;
1096
1097 p = skipwhite(s);
1098 if (*p == ']')
1099 break;
1100 else if (*p == ';')
1101 {
1102 if (*semicolon == 1)
1103 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001104 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001105 return NULL;
1106 }
1107 *semicolon = 1;
1108 }
1109 else if (*p != ',')
1110 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001111 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001112 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001113 return NULL;
1114 }
1115 }
1116 return p + 1;
1117 }
1118 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001120}
1121
1122/*
1123 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1124 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001126 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001127 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001128skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001129{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001130 char_u *end;
1131 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001133 if (*arg == '@' && arg[1] != NUL)
1134 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001136 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001137
1138 // "a: type" is declaring variable "a" with a type, not "a:".
1139 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001140 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001141 --end;
1142
Bram Moolenaar585587d2021-01-17 20:52:13 +01001143 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001144 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001145 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001146 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001147 }
1148 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001149}
1150
1151/*
1152 * List variables for hashtab "ht" with prefix "prefix".
1153 * If "empty" is TRUE also list NULL strings as empty strings.
1154 */
1155 void
1156list_hashtable_vars(
1157 hashtab_T *ht,
1158 char *prefix,
1159 int empty,
1160 int *first)
1161{
1162 hashitem_T *hi;
1163 dictitem_T *di;
1164 int todo;
1165 char_u buf[IOSIZE];
1166
1167 todo = (int)ht->ht_used;
1168 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1169 {
1170 if (!HASHITEM_EMPTY(hi))
1171 {
1172 --todo;
1173 di = HI2DI(hi);
1174
1175 // apply :filter /pat/ to variable name
1176 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1177 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1178 if (message_filtered(buf))
1179 continue;
1180
1181 if (empty || di->di_tv.v_type != VAR_STRING
1182 || di->di_tv.vval.v_string != NULL)
1183 list_one_var(di, prefix, first);
1184 }
1185 }
1186}
1187
1188/*
1189 * List global variables.
1190 */
1191 static void
1192list_glob_vars(int *first)
1193{
1194 list_hashtable_vars(&globvarht, "", TRUE, first);
1195}
1196
1197/*
1198 * List buffer variables.
1199 */
1200 static void
1201list_buf_vars(int *first)
1202{
1203 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1204}
1205
1206/*
1207 * List window variables.
1208 */
1209 static void
1210list_win_vars(int *first)
1211{
1212 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1213}
1214
1215/*
1216 * List tab page variables.
1217 */
1218 static void
1219list_tab_vars(int *first)
1220{
1221 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1222}
1223
1224/*
1225 * List variables in "arg".
1226 */
1227 static char_u *
1228list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1229{
1230 int error = FALSE;
1231 int len;
1232 char_u *name;
1233 char_u *name_start;
1234 char_u *arg_subsc;
1235 char_u *tofree;
1236 typval_T tv;
1237
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001238 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001239 {
1240 if (error || eap->skip)
1241 {
1242 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1243 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1244 {
1245 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001246 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001247 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001248 break;
1249 }
1250 }
1251 else
1252 {
1253 // get_name_len() takes care of expanding curly braces
1254 name_start = name = arg;
1255 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1256 if (len <= 0)
1257 {
1258 // This is mainly to keep test 49 working: when expanding
1259 // curly braces fails overrule the exception error message.
1260 if (len < 0 && !aborting())
1261 {
1262 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001263 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001264 break;
1265 }
1266 error = TRUE;
1267 }
1268 else
1269 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001270 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001271 if (tofree != NULL)
1272 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001273 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001274 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001275 error = TRUE;
1276 else
1277 {
1278 // handle d.key, l[idx], f(expr)
1279 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001280 if (handle_subscript(&arg, name_start, &tv,
1281 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001282 error = TRUE;
1283 else
1284 {
1285 if (arg == arg_subsc && len == 2 && name[1] == ':')
1286 {
1287 switch (*name)
1288 {
1289 case 'g': list_glob_vars(first); break;
1290 case 'b': list_buf_vars(first); break;
1291 case 'w': list_win_vars(first); break;
1292 case 't': list_tab_vars(first); break;
1293 case 'v': list_vim_vars(first); break;
1294 case 's': list_script_vars(first); break;
1295 case 'l': list_func_vars(first); break;
1296 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001297 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001298 }
1299 }
1300 else
1301 {
1302 char_u numbuf[NUMBUFLEN];
1303 char_u *tf;
1304 int c;
1305 char_u *s;
1306
1307 s = echo_string(&tv, &tf, numbuf, 0);
1308 c = *arg;
1309 *arg = NUL;
1310 list_one_var_a("",
1311 arg == arg_subsc ? name : name_start,
1312 tv.v_type,
1313 s == NULL ? (char_u *)"" : s,
1314 first);
1315 *arg = c;
1316 vim_free(tf);
1317 }
1318 clear_tv(&tv);
1319 }
1320 }
1321 }
1322
1323 vim_free(tofree);
1324 }
1325
1326 arg = skipwhite(arg);
1327 }
1328
1329 return arg;
1330}
1331
1332/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001333 * Set an environment variable, part of ex_let_one().
1334 */
1335 static char_u *
1336ex_let_env(
1337 char_u *arg,
1338 typval_T *tv,
1339 int flags,
1340 char_u *endchars,
1341 char_u *op)
1342{
1343 char_u *arg_end = NULL;
1344 char_u *name;
1345 int len;
1346
1347 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1348 && (flags & ASSIGN_FOR_LOOP) == 0)
1349 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001350 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001351 return NULL;
1352 }
1353
1354 // Find the end of the name.
1355 ++arg;
1356 name = arg;
1357 len = get_env_len(&arg);
1358 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001359 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001360 else
1361 {
1362 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001363 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001364 else if (endchars != NULL
1365 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1366 emsg(_(e_unexpected_characters_in_let));
1367 else if (!check_secure())
1368 {
1369 char_u *tofree = NULL;
1370 int c1 = name[len];
1371 char_u *p;
1372
1373 name[len] = NUL;
1374 p = tv_get_string_chk(tv);
1375 if (p != NULL && op != NULL && *op == '.')
1376 {
1377 int mustfree = FALSE;
1378 char_u *s = vim_getenv(name, &mustfree);
1379
1380 if (s != NULL)
1381 {
1382 p = tofree = concat_str(s, p);
1383 if (mustfree)
1384 vim_free(s);
1385 }
1386 }
1387 if (p != NULL)
1388 {
1389 vim_setenv_ext(name, p);
1390 arg_end = arg;
1391 }
1392 name[len] = c1;
1393 vim_free(tofree);
1394 }
1395 }
1396 return arg_end;
1397}
1398
1399/*
1400 * Set an option, part of ex_let_one().
1401 */
1402 static char_u *
1403ex_let_option(
1404 char_u *arg,
1405 typval_T *tv,
1406 int flags,
1407 char_u *endchars,
1408 char_u *op)
1409{
1410 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001411 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001412 char_u *arg_end = NULL;
1413
1414 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1415 && (flags & ASSIGN_FOR_LOOP) == 0)
1416 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001417 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001418 return NULL;
1419 }
1420
1421 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001422 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001423 if (p == NULL || (endchars != NULL
1424 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1425 emsg(_(e_unexpected_characters_in_let));
1426 else
1427 {
1428 int c1;
1429 long n = 0;
1430 getoption_T opt_type;
1431 long numval;
1432 char_u *stringval = NULL;
1433 char_u *s = NULL;
1434 int failed = FALSE;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001435 int opt_p_flags;
1436 char_u *tofree = NULL;
Bram Moolenaar92c33eb2021-12-07 11:03:39 +00001437 char_u numbuf[NUMBUFLEN];
1438
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001439 c1 = *p;
1440 *p = NUL;
1441
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001442 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags,
1443 scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001444 if ((opt_type == gov_bool
1445 || opt_type == gov_number
1446 || opt_type == gov_hidden_bool
1447 || opt_type == gov_hidden_number)
1448 && (tv->v_type != VAR_STRING || !in_vim9script()))
1449 {
1450 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1451 // bool, possibly hidden
1452 n = (long)tv_get_bool(tv);
1453 else
1454 // number, possibly hidden
1455 n = (long)tv_get_number(tv);
1456 }
1457
Bram Moolenaaref082e12021-12-12 21:02:03 +00001458 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001459 || tv->v_type == VAR_FUNC))
1460 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001461 // If the option can be set to a function reference or a lambda
1462 // and the passed value is a function reference, then convert it to
1463 // the name (string) of the function reference.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001464 s = tv2string(tv, &tofree, numbuf, 0);
1465 }
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001466 // Avoid setting a string option to the text "v:false" or similar.
1467 // In Vim9 script also don't convert a number to string.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001468 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001469 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1470 s = tv_get_string_chk(tv);
1471
1472 if (op != NULL && *op != '=')
1473 {
1474 if (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1475 || (opt_type == gov_string && *op != '.'))
1476 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001477 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001478 failed = TRUE; // don't set the value
1479
1480 }
1481 else
1482 {
1483 // number, in legacy script also bool
1484 if (opt_type == gov_number
1485 || (opt_type == gov_bool && !in_vim9script()))
1486 {
1487 switch (*op)
1488 {
1489 case '+': n = numval + n; break;
1490 case '-': n = numval - n; break;
1491 case '*': n = numval * n; break;
1492 case '/': n = (long)num_divide(numval, n,
1493 &failed); break;
1494 case '%': n = (long)num_modulus(numval, n,
1495 &failed); break;
1496 }
1497 s = NULL;
1498 }
1499 else if (opt_type == gov_string
1500 && stringval != NULL && s != NULL)
1501 {
1502 // string
1503 s = concat_str(stringval, s);
1504 vim_free(stringval);
1505 stringval = s;
1506 }
1507 }
1508 }
1509
1510 if (!failed)
1511 {
1512 if (opt_type != gov_string || s != NULL)
1513 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001514 set_option_value(arg, n, s, scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001515 arg_end = p;
1516 }
1517 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001518 emsg(_(e_string_required));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001519 }
1520 *p = c1;
1521 vim_free(stringval);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001522 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001523 }
1524 return arg_end;
1525}
1526
1527/*
1528 * Set a register, part of ex_let_one().
1529 */
1530 static char_u *
1531ex_let_register(
1532 char_u *arg,
1533 typval_T *tv,
1534 int flags,
1535 char_u *endchars,
1536 char_u *op)
1537{
1538 char_u *arg_end = NULL;
1539
1540 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1541 && (flags & ASSIGN_FOR_LOOP) == 0)
1542 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001543 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001544 return NULL;
1545 }
1546 ++arg;
1547 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001548 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001549 else if (endchars != NULL
1550 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1551 emsg(_(e_unexpected_characters_in_let));
1552 else
1553 {
1554 char_u *ptofree = NULL;
1555 char_u *p;
1556
1557 p = tv_get_string_chk(tv);
1558 if (p != NULL && op != NULL && *op == '.')
1559 {
1560 char_u *s = get_reg_contents(*arg == '@'
1561 ? '"' : *arg, GREG_EXPR_SRC);
1562
1563 if (s != NULL)
1564 {
1565 p = ptofree = concat_str(s, p);
1566 vim_free(s);
1567 }
1568 }
1569 if (p != NULL)
1570 {
1571 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1572 arg_end = arg + 1;
1573 }
1574 vim_free(ptofree);
1575 }
1576 return arg_end;
1577}
1578
1579/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001580 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1581 * Returns a pointer to the char just after the var name.
1582 * Returns NULL if there is an error.
1583 */
1584 static char_u *
1585ex_let_one(
1586 char_u *arg, // points to variable name
1587 typval_T *tv, // value to assign to variable
1588 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001589 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001590 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001591 char_u *op, // "+", "-", "." or NULL
1592 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001593{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001594 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001595
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001596 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001597 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001598 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1599 {
1600 vim9_declare_error(arg);
1601 return NULL;
1602 }
1603
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001604 if (*arg == '$')
1605 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001606 // ":let $VAR = expr": Set environment variable.
1607 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001608 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001609 else if (*arg == '&')
1610 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001611 // ":let &option = expr": Set option value.
1612 // ":let &l:option = expr": Set local option value.
1613 // ":let &g:option = expr": Set global option value.
1614 // ":for &ts in range(8)": Set option value for for loop
1615 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001616 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001617 else if (*arg == '@')
1618 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001619 // ":let @r = expr": Set register contents.
1620 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001621 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001622 else if (eval_isnamec1(*arg) || *arg == '{')
1623 {
1624 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001625 char_u *p;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001626
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001627 // ":let var = expr": Set internal variable.
1628 // ":let var: type = expr": Set internal variable with type.
1629 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001630 p = get_lval(arg, tv, &lv, FALSE, FALSE,
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001631 (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1632 ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001633 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001634 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635 if (endchars != NULL && vim_strchr(endchars,
1636 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001637 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001638 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001639 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001640 else
1641 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001642 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001643 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001644 }
1645 }
1646 clear_lval(&lv);
1647 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001648 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001649 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001650
1651 return arg_end;
1652}
1653
1654/*
1655 * ":unlet[!] var1 ... " command.
1656 */
1657 void
1658ex_unlet(exarg_T *eap)
1659{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001660 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001661}
1662
1663/*
1664 * ":lockvar" and ":unlockvar" commands
1665 */
1666 void
1667ex_lockvar(exarg_T *eap)
1668{
1669 char_u *arg = eap->arg;
1670 int deep = 2;
1671
1672 if (eap->forceit)
1673 deep = -1;
1674 else if (vim_isdigit(*arg))
1675 {
1676 deep = getdigits(&arg);
1677 arg = skipwhite(arg);
1678 }
1679
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001680 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001681}
1682
1683/*
1684 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001685 * Also used for Vim9 script. "callback" is invoked as:
1686 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001687 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001688 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001689ex_unletlock(
1690 exarg_T *eap,
1691 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001692 int deep,
1693 int glv_flags,
1694 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1695 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001696{
1697 char_u *arg = argstart;
1698 char_u *name_end;
1699 int error = FALSE;
1700 lval_T lv;
1701
1702 do
1703 {
1704 if (*arg == '$')
1705 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001706 lv.ll_name = arg;
1707 lv.ll_tv = NULL;
1708 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001709 if (get_env_len(&arg) == 0)
1710 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001711 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001712 return;
1713 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001714 if (!error && !eap->skip
1715 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1716 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001717 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001718 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001719 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001720 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001721 // Parse the name and find the end.
1722 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001723 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001724 if (lv.ll_name == NULL)
1725 error = TRUE; // error but continue parsing
1726 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1727 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001728 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001729 if (name_end != NULL)
1730 {
1731 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00001732 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001733 }
1734 if (!(eap->skip || error))
1735 clear_lval(&lv);
1736 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001737 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001738
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001739 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001740 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001741 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001742
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001743 if (!eap->skip)
1744 clear_lval(&lv);
1745 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001746
1747 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001748 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001749
Bram Moolenaar63b91732021-08-05 20:40:03 +02001750 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001751}
1752
1753 static int
1754do_unlet_var(
1755 lval_T *lp,
1756 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001757 exarg_T *eap,
1758 int deep UNUSED,
1759 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001760{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001761 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001762 int ret = OK;
1763 int cc;
1764
1765 if (lp->ll_tv == NULL)
1766 {
1767 cc = *name_end;
1768 *name_end = NUL;
1769
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001770 // Environment variable, normal name or expanded name.
1771 if (*lp->ll_name == '$')
1772 vim_unsetenv(lp->ll_name + 1);
1773 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001774 ret = FAIL;
1775 *name_end = cc;
1776 }
1777 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001778 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001779 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001780 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001781 return FAIL;
1782 else if (lp->ll_range)
1783 {
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001784 if (list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_name, lp->ll_n1,
1785 !lp->ll_empty2, lp->ll_n2) == FAIL)
1786 return FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001787 }
1788 else
1789 {
1790 if (lp->ll_list != NULL)
1791 // unlet a List item.
1792 listitem_remove(lp->ll_list, lp->ll_li);
1793 else
1794 // unlet a Dictionary item.
1795 dictitem_remove(lp->ll_dict, lp->ll_di);
1796 }
1797
1798 return ret;
1799}
1800
1801/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001802 * Unlet one item or a range of items from a list.
1803 * Return OK or FAIL.
1804 */
1805 int
1806list_unlet_range(
1807 list_T *l,
1808 listitem_T *li_first,
1809 char_u *name,
1810 long n1_arg,
1811 int has_n2,
1812 long n2)
1813{
1814 listitem_T *li = li_first;
1815 int n1 = n1_arg;
1816
1817 while (li != NULL && (!has_n2 || n2 >= n1))
1818 {
1819 if (value_check_lock(li->li_tv.v_lock, name, FALSE))
1820 return FAIL;
1821 li = li->li_next;
1822 ++n1;
1823 }
1824
1825 // Delete a range of List items.
1826 li = li_first;
1827 n1 = n1_arg;
1828 while (li != NULL && (!has_n2 || n2 >= n1))
1829 {
1830 listitem_T *next = li->li_next;
1831
1832 listitem_remove(l, li);
1833 li = next;
1834 ++n1;
1835 }
1836 return OK;
1837}
1838/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001839 * "unlet" a variable. Return OK if it existed, FAIL if not.
1840 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1841 */
1842 int
1843do_unlet(char_u *name, int forceit)
1844{
1845 hashtab_T *ht;
1846 hashitem_T *hi;
1847 char_u *varname;
1848 dict_T *d;
1849 dictitem_T *di;
1850
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001851 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001852 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001853 return FAIL;
1854
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001855 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001856
1857 // can't :unlet a script variable in Vim9 script from a function
1858 if (ht == get_script_local_ht()
1859 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1860 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1861 == SCRIPT_VERSION_VIM9
1862 && check_vim9_unlet(name) == FAIL)
1863 return FAIL;
1864
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001865 if (ht != NULL && *varname != NUL)
1866 {
1867 d = get_current_funccal_dict(ht);
1868 if (d == NULL)
1869 {
1870 if (ht == &globvarht)
1871 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001872 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001873 d = &vimvardict;
1874 else
1875 {
1876 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1877 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1878 }
1879 if (d == NULL)
1880 {
1881 internal_error("do_unlet()");
1882 return FAIL;
1883 }
1884 }
1885 hi = hash_find(ht, varname);
1886 if (HASHITEM_EMPTY(hi))
1887 hi = find_hi_in_scoped_ht(name, &ht);
1888 if (hi != NULL && !HASHITEM_EMPTY(hi))
1889 {
1890 di = HI2DI(hi);
1891 if (var_check_fixed(di->di_flags, name, FALSE)
1892 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001893 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001894 return FAIL;
1895
1896 delete_var(ht, hi);
1897 return OK;
1898 }
1899 }
1900 if (forceit)
1901 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00001902 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001903 return FAIL;
1904}
1905
1906/*
1907 * Lock or unlock variable indicated by "lp".
1908 * "deep" is the levels to go (-1 for unlimited);
1909 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1910 */
1911 static int
1912do_lock_var(
1913 lval_T *lp,
1914 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001915 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001916 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001917 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001918{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001919 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001920 int ret = OK;
1921 int cc;
1922 dictitem_T *di;
1923
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001924 if (lp->ll_tv == NULL)
1925 {
1926 cc = *name_end;
1927 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001928 if (*lp->ll_name == '$')
1929 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001930 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001931 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001932 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001933 else
1934 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001935 // Normal name or expanded name.
1936 di = find_var(lp->ll_name, NULL, TRUE);
1937 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001938 {
1939 if (in_vim9script())
1940 semsg(_(e_cannot_find_variable_to_unlock_str),
1941 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001942 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001943 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001944 else if ((di->di_flags & DI_FLAGS_FIX)
1945 && di->di_tv.v_type != VAR_DICT
1946 && di->di_tv.v_type != VAR_LIST)
1947 {
1948 // For historic reasons this error is not given for a list or
1949 // dict. E.g., the b: dict could be locked/unlocked.
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001950 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001951 ret = FAIL;
1952 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001953 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001954 {
1955 if (lock)
1956 di->di_flags |= DI_FLAGS_LOCK;
1957 else
1958 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001959 if (deep != 0)
1960 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001961 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001962 }
1963 *name_end = cc;
1964 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001965 else if (deep == 0)
1966 {
1967 // nothing to do
1968 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001969 else if (lp->ll_range)
1970 {
1971 listitem_T *li = lp->ll_li;
1972
1973 // (un)lock a range of List items.
1974 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1975 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001976 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001977 li = li->li_next;
1978 ++lp->ll_n1;
1979 }
1980 }
1981 else if (lp->ll_list != NULL)
1982 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001983 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001984 else
1985 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001986 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001987
1988 return ret;
1989}
1990
1991/*
1992 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001993 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1994 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001995 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001996 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001997item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001998{
1999 static int recurse = 0;
2000 list_T *l;
2001 listitem_T *li;
2002 dict_T *d;
2003 blob_T *b;
2004 hashitem_T *hi;
2005 int todo;
2006
2007 if (recurse >= DICT_MAXNEST)
2008 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002009 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002010 return;
2011 }
2012 if (deep == 0)
2013 return;
2014 ++recurse;
2015
2016 // lock/unlock the item itself
2017 if (lock)
2018 tv->v_lock |= VAR_LOCKED;
2019 else
2020 tv->v_lock &= ~VAR_LOCKED;
2021
2022 switch (tv->v_type)
2023 {
2024 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002025 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002026 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002027 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002028 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002029 case VAR_STRING:
2030 case VAR_FUNC:
2031 case VAR_PARTIAL:
2032 case VAR_FLOAT:
2033 case VAR_SPECIAL:
2034 case VAR_JOB:
2035 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002036 case VAR_INSTR:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002037 break;
2038
2039 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002040 if ((b = tv->vval.v_blob) != NULL
2041 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002042 {
2043 if (lock)
2044 b->bv_lock |= VAR_LOCKED;
2045 else
2046 b->bv_lock &= ~VAR_LOCKED;
2047 }
2048 break;
2049 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002050 if ((l = tv->vval.v_list) != NULL
2051 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002052 {
2053 if (lock)
2054 l->lv_lock |= VAR_LOCKED;
2055 else
2056 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002057 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002058 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002059 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02002060 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002061 }
2062 break;
2063 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002064 if ((d = tv->vval.v_dict) != NULL
2065 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002066 {
2067 if (lock)
2068 d->dv_lock |= VAR_LOCKED;
2069 else
2070 d->dv_lock &= ~VAR_LOCKED;
2071 if (deep < 0 || deep > 1)
2072 {
2073 // recursive: lock/unlock the items the List contains
2074 todo = (int)d->dv_hashtab.ht_used;
2075 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2076 {
2077 if (!HASHITEM_EMPTY(hi))
2078 {
2079 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002080 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2081 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002082 }
2083 }
2084 }
2085 }
2086 }
2087 --recurse;
2088}
2089
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002090#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2091/*
2092 * Delete all "menutrans_" variables.
2093 */
2094 void
2095del_menutrans_vars(void)
2096{
2097 hashitem_T *hi;
2098 int todo;
2099
2100 hash_lock(&globvarht);
2101 todo = (int)globvarht.ht_used;
2102 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2103 {
2104 if (!HASHITEM_EMPTY(hi))
2105 {
2106 --todo;
2107 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2108 delete_var(&globvarht, hi);
2109 }
2110 }
2111 hash_unlock(&globvarht);
2112}
2113#endif
2114
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002115/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002116 * Local string buffer for the next two functions to store a variable name
2117 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2118 * get_user_var_name().
2119 */
2120
2121static char_u *varnamebuf = NULL;
2122static int varnamebuflen = 0;
2123
2124/*
2125 * Function to concatenate a prefix and a variable name.
2126 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002127 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002128cat_prefix_varname(int prefix, char_u *name)
2129{
2130 int len;
2131
2132 len = (int)STRLEN(name) + 3;
2133 if (len > varnamebuflen)
2134 {
2135 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002136 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002137 varnamebuf = alloc(len);
2138 if (varnamebuf == NULL)
2139 {
2140 varnamebuflen = 0;
2141 return NULL;
2142 }
2143 varnamebuflen = len;
2144 }
2145 *varnamebuf = prefix;
2146 varnamebuf[1] = ':';
2147 STRCPY(varnamebuf + 2, name);
2148 return varnamebuf;
2149}
2150
2151/*
2152 * Function given to ExpandGeneric() to obtain the list of user defined
2153 * (global/buffer/window/built-in) variable names.
2154 */
2155 char_u *
2156get_user_var_name(expand_T *xp, int idx)
2157{
2158 static long_u gdone;
2159 static long_u bdone;
2160 static long_u wdone;
2161 static long_u tdone;
2162 static int vidx;
2163 static hashitem_T *hi;
2164 hashtab_T *ht;
2165
2166 if (idx == 0)
2167 {
2168 gdone = bdone = wdone = vidx = 0;
2169 tdone = 0;
2170 }
2171
2172 // Global variables
2173 if (gdone < globvarht.ht_used)
2174 {
2175 if (gdone++ == 0)
2176 hi = globvarht.ht_array;
2177 else
2178 ++hi;
2179 while (HASHITEM_EMPTY(hi))
2180 ++hi;
2181 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2182 return cat_prefix_varname('g', hi->hi_key);
2183 return hi->hi_key;
2184 }
2185
2186 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002187 ht =
2188#ifdef FEAT_CMDWIN
2189 // In cmdwin, the alternative buffer should be used.
mityua1198122021-11-20 19:13:39 +00002190 is_in_cmdwin() ? &prevwin->w_buffer->b_vars->dv_hashtab :
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002191#endif
2192 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002193 if (bdone < ht->ht_used)
2194 {
2195 if (bdone++ == 0)
2196 hi = ht->ht_array;
2197 else
2198 ++hi;
2199 while (HASHITEM_EMPTY(hi))
2200 ++hi;
2201 return cat_prefix_varname('b', hi->hi_key);
2202 }
2203
2204 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002205 ht =
2206#ifdef FEAT_CMDWIN
2207 // In cmdwin, the alternative window should be used.
mityua1198122021-11-20 19:13:39 +00002208 is_in_cmdwin() ? &prevwin->w_vars->dv_hashtab :
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002209#endif
2210 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002211 if (wdone < ht->ht_used)
2212 {
2213 if (wdone++ == 0)
2214 hi = ht->ht_array;
2215 else
2216 ++hi;
2217 while (HASHITEM_EMPTY(hi))
2218 ++hi;
2219 return cat_prefix_varname('w', hi->hi_key);
2220 }
2221
2222 // t: variables
2223 ht = &curtab->tp_vars->dv_hashtab;
2224 if (tdone < ht->ht_used)
2225 {
2226 if (tdone++ == 0)
2227 hi = ht->ht_array;
2228 else
2229 ++hi;
2230 while (HASHITEM_EMPTY(hi))
2231 ++hi;
2232 return cat_prefix_varname('t', hi->hi_key);
2233 }
2234
2235 // v: variables
2236 if (vidx < VV_LEN)
2237 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2238
2239 VIM_CLEAR(varnamebuf);
2240 varnamebuflen = 0;
2241 return NULL;
2242}
2243
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002244 char *
2245get_var_special_name(int nr)
2246{
2247 switch (nr)
2248 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002249 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2250 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002251 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002252 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002253 }
2254 internal_error("get_var_special_name()");
2255 return "42";
2256}
2257
2258/*
2259 * Returns the global variable dictionary
2260 */
2261 dict_T *
2262get_globvar_dict(void)
2263{
2264 return &globvardict;
2265}
2266
2267/*
2268 * Returns the global variable hash table
2269 */
2270 hashtab_T *
2271get_globvar_ht(void)
2272{
2273 return &globvarht;
2274}
2275
2276/*
2277 * Returns the v: variable dictionary
2278 */
2279 dict_T *
2280get_vimvar_dict(void)
2281{
2282 return &vimvardict;
2283}
2284
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002285/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002286 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002287 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002288 */
2289 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002290find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002291{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002292 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2293 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002294
2295 if (di == NULL)
2296 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002297 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2299 return (int)(vv - vimvars);
2300}
2301
2302
2303/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002304 * Set type of v: variable to "type".
2305 */
2306 void
2307set_vim_var_type(int idx, vartype_T type)
2308{
Bram Moolenaard787e402021-12-24 21:36:12 +00002309 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002310}
2311
2312/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002313 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002314 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002315 */
2316 void
2317set_vim_var_nr(int idx, varnumber_T val)
2318{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002319 vimvars[idx].vv_nr = val;
2320}
2321
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322 char *
2323get_vim_var_name(int idx)
2324{
2325 return vimvars[idx].vv_name;
2326}
2327
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002328/*
2329 * Get typval_T v: variable value.
2330 */
2331 typval_T *
2332get_vim_var_tv(int idx)
2333{
2334 return &vimvars[idx].vv_tv;
2335}
2336
Bram Moolenaard787e402021-12-24 21:36:12 +00002337 type_T *
2338get_vim_var_type(int idx, garray_T *type_list)
2339{
2340 if (vimvars[idx].vv_type != NULL)
2341 return vimvars[idx].vv_type;
2342 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2343}
2344
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002345/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002346 * Set v: variable to "tv". Only accepts the same type.
2347 * Takes over the value of "tv".
2348 */
2349 int
2350set_vim_var_tv(int idx, typval_T *tv)
2351{
Bram Moolenaard787e402021-12-24 21:36:12 +00002352 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002353 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002354 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002355 clear_tv(tv);
2356 return FAIL;
2357 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002358 // VV_RO is also checked when compiling, but let's check here as well.
2359 if (vimvars[idx].vv_flags & VV_RO)
2360 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002361 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002362 return FAIL;
2363 }
2364 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2365 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002366 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002367 return FAIL;
2368 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002369 clear_tv(&vimvars[idx].vv_di.di_tv);
2370 vimvars[idx].vv_di.di_tv = *tv;
2371 return OK;
2372}
2373
2374/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002375 * Get number v: variable value.
2376 */
2377 varnumber_T
2378get_vim_var_nr(int idx)
2379{
2380 return vimvars[idx].vv_nr;
2381}
2382
2383/*
2384 * Get string v: variable value. Uses a static buffer, can only be used once.
2385 * If the String variable has never been set, return an empty string.
2386 * Never returns NULL;
2387 */
2388 char_u *
2389get_vim_var_str(int idx)
2390{
2391 return tv_get_string(&vimvars[idx].vv_tv);
2392}
2393
2394/*
2395 * Get List v: variable value. Caller must take care of reference count when
2396 * needed.
2397 */
2398 list_T *
2399get_vim_var_list(int idx)
2400{
2401 return vimvars[idx].vv_list;
2402}
2403
2404/*
2405 * Get Dict v: variable value. Caller must take care of reference count when
2406 * needed.
2407 */
2408 dict_T *
2409get_vim_var_dict(int idx)
2410{
2411 return vimvars[idx].vv_dict;
2412}
2413
2414/*
2415 * Set v:char to character "c".
2416 */
2417 void
2418set_vim_var_char(int c)
2419{
2420 char_u buf[MB_MAXBYTES + 1];
2421
2422 if (has_mbyte)
2423 buf[(*mb_char2bytes)(c, buf)] = NUL;
2424 else
2425 {
2426 buf[0] = c;
2427 buf[1] = NUL;
2428 }
2429 set_vim_var_string(VV_CHAR, buf, -1);
2430}
2431
2432/*
2433 * Set v:count to "count" and v:count1 to "count1".
2434 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2435 */
2436 void
2437set_vcount(
2438 long count,
2439 long count1,
2440 int set_prevcount)
2441{
2442 if (set_prevcount)
2443 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2444 vimvars[VV_COUNT].vv_nr = count;
2445 vimvars[VV_COUNT1].vv_nr = count1;
2446}
2447
2448/*
2449 * Save variables that might be changed as a side effect. Used when executing
2450 * a timer callback.
2451 */
2452 void
2453save_vimvars(vimvars_save_T *vvsave)
2454{
2455 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2456 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2457 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2458}
2459
2460/*
2461 * Restore variables saved by save_vimvars().
2462 */
2463 void
2464restore_vimvars(vimvars_save_T *vvsave)
2465{
2466 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2467 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2468 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2469}
2470
2471/*
2472 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2473 * value.
2474 */
2475 void
2476set_vim_var_string(
2477 int idx,
2478 char_u *val,
2479 int len) // length of "val" to use or -1 (whole string)
2480{
2481 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002482 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002483 if (val == NULL)
2484 vimvars[idx].vv_str = NULL;
2485 else if (len == -1)
2486 vimvars[idx].vv_str = vim_strsave(val);
2487 else
2488 vimvars[idx].vv_str = vim_strnsave(val, len);
2489}
2490
2491/*
2492 * Set List v: variable to "val".
2493 */
2494 void
2495set_vim_var_list(int idx, list_T *val)
2496{
2497 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002498 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002499 vimvars[idx].vv_list = val;
2500 if (val != NULL)
2501 ++val->lv_refcount;
2502}
2503
2504/*
2505 * Set Dictionary v: variable to "val".
2506 */
2507 void
2508set_vim_var_dict(int idx, dict_T *val)
2509{
2510 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002511 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002512 vimvars[idx].vv_dict = val;
2513 if (val != NULL)
2514 {
2515 ++val->dv_refcount;
2516 dict_set_items_ro(val);
2517 }
2518}
2519
2520/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002521 * Set the v:argv list.
2522 */
2523 void
2524set_argv_var(char **argv, int argc)
2525{
2526 list_T *l = list_alloc();
2527 int i;
2528
2529 if (l == NULL)
2530 getout(1);
2531 l->lv_lock = VAR_FIXED;
2532 for (i = 0; i < argc; ++i)
2533 {
2534 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2535 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002536 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002537 }
2538 set_vim_var_list(VV_ARGV, l);
2539}
2540
2541/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002542 * Reset v:register, taking the 'clipboard' setting into account.
2543 */
2544 void
2545reset_reg_var(void)
2546{
2547 int regname = 0;
2548
2549 // Adjust the register according to 'clipboard', so that when
2550 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2551#ifdef FEAT_CLIPBOARD
2552 adjust_clip_reg(&regname);
2553#endif
2554 set_reg_var(regname);
2555}
2556
2557/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002558 * Set v:register if needed.
2559 */
2560 void
2561set_reg_var(int c)
2562{
2563 char_u regname;
2564
2565 if (c == 0 || c == ' ')
2566 regname = '"';
2567 else
2568 regname = c;
2569 // Avoid free/alloc when the value is already right.
2570 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2571 set_vim_var_string(VV_REG, &regname, 1);
2572}
2573
2574/*
2575 * Get or set v:exception. If "oldval" == NULL, return the current value.
2576 * Otherwise, restore the value to "oldval" and return NULL.
2577 * Must always be called in pairs to save and restore v:exception! Does not
2578 * take care of memory allocations.
2579 */
2580 char_u *
2581v_exception(char_u *oldval)
2582{
2583 if (oldval == NULL)
2584 return vimvars[VV_EXCEPTION].vv_str;
2585
2586 vimvars[VV_EXCEPTION].vv_str = oldval;
2587 return NULL;
2588}
2589
2590/*
2591 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2592 * Otherwise, restore the value to "oldval" and return NULL.
2593 * Must always be called in pairs to save and restore v:throwpoint! Does not
2594 * take care of memory allocations.
2595 */
2596 char_u *
2597v_throwpoint(char_u *oldval)
2598{
2599 if (oldval == NULL)
2600 return vimvars[VV_THROWPOINT].vv_str;
2601
2602 vimvars[VV_THROWPOINT].vv_str = oldval;
2603 return NULL;
2604}
2605
2606/*
2607 * Set v:cmdarg.
2608 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2609 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2610 * Must always be called in pairs!
2611 */
2612 char_u *
2613set_cmdarg(exarg_T *eap, char_u *oldarg)
2614{
2615 char_u *oldval;
2616 char_u *newval;
2617 unsigned len;
2618
2619 oldval = vimvars[VV_CMDARG].vv_str;
2620 if (eap == NULL)
2621 {
2622 vim_free(oldval);
2623 vimvars[VV_CMDARG].vv_str = oldarg;
2624 return NULL;
2625 }
2626
2627 if (eap->force_bin == FORCE_BIN)
2628 len = 6;
2629 else if (eap->force_bin == FORCE_NOBIN)
2630 len = 8;
2631 else
2632 len = 0;
2633
2634 if (eap->read_edit)
2635 len += 7;
2636
2637 if (eap->force_ff != 0)
2638 len += 10; // " ++ff=unix"
2639 if (eap->force_enc != 0)
2640 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2641 if (eap->bad_char != 0)
2642 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2643
2644 newval = alloc(len + 1);
2645 if (newval == NULL)
2646 return NULL;
2647
2648 if (eap->force_bin == FORCE_BIN)
2649 sprintf((char *)newval, " ++bin");
2650 else if (eap->force_bin == FORCE_NOBIN)
2651 sprintf((char *)newval, " ++nobin");
2652 else
2653 *newval = NUL;
2654
2655 if (eap->read_edit)
2656 STRCAT(newval, " ++edit");
2657
2658 if (eap->force_ff != 0)
2659 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2660 eap->force_ff == 'u' ? "unix"
2661 : eap->force_ff == 'd' ? "dos"
2662 : "mac");
2663 if (eap->force_enc != 0)
2664 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2665 eap->cmd + eap->force_enc);
2666 if (eap->bad_char == BAD_KEEP)
2667 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2668 else if (eap->bad_char == BAD_DROP)
2669 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2670 else if (eap->bad_char != 0)
2671 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2672 vimvars[VV_CMDARG].vv_str = newval;
2673 return oldval;
2674}
2675
2676/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002677 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002678 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2679 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002680 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2681 */
2682 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002683eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002684 char_u *name,
2685 int len, // length of "name"
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002686 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002687 typval_T *rettv, // NULL when only checking existence
2688 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002689 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002690{
2691 int ret = OK;
2692 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002693 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002694 hashtab_T *ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002695 int cc;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002696 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002697
2698 // truncate the name, so that we can use strcmp()
2699 cc = name[len];
2700 name[len] = NUL;
2701
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002702 // Check for local variable when debugging.
2703 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002704 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002705 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002706 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2707
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002708 if (v != NULL)
2709 {
2710 tv = &v->di_tv;
2711 if (dip != NULL)
2712 *dip = v;
2713 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002714 else
2715 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002716 }
2717
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002718 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002720 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002721 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2722
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002723 if (sid == 0)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002724 import = find_imported(p, 0, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725
2726 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002727 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002729 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002730 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00002731 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02002732 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002733 ht = &SCRIPT_VARS(sid);
2734 if (ht != NULL)
2735 {
2736 dictitem_T *v = find_var_in_ht(ht, 0, name,
2737 flags & EVAL_VAR_NOAUTOLOAD);
2738
2739 if (v != NULL)
2740 {
2741 tv = &v->di_tv;
2742 if (dip != NULL)
2743 *dip = v;
2744 }
2745 else
2746 ht = NULL;
2747 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002748 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002749 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002750 {
2751 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00002752 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002753 ret = FAIL;
2754 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002755 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002756 else
2757 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002758 if (rettv != NULL)
2759 {
2760 rettv->v_type = VAR_ANY;
2761 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
2762 }
2763 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002764 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00002766 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002767 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002768 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002769
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002770 // In Vim9 script we can get a function reference by using the
2771 // function name.
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002772 if (ufunc != NULL)
2773 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002774 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002775 if (rettv != NULL)
2776 {
2777 rettv->v_type = VAR_FUNC;
Bram Moolenaaref082e12021-12-12 21:02:03 +00002778 if (STRNCMP(name, "g:", 2) == 0)
2779 // Keep the "g:", otherwise script-local may be
2780 // assumed.
2781 rettv->vval.v_string = vim_strsave(name);
2782 else
2783 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002784 if (rettv->vval.v_string != NULL)
2785 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002786 }
2787 }
2788 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 }
2790
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002791 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002792 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002793 if (tv == NULL)
2794 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002795 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002796 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002797 ret = FAIL;
2798 }
2799 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002800 {
Bram Moolenaar11005b02021-07-11 20:59:00 +02002801 if (ht != NULL && ht == get_script_local_ht()
2802 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002803 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002804 svar_T *sv = find_typval_in_script(tv, 0);
Bram Moolenaarf055d452021-07-08 20:57:24 +02002805
Bram Moolenaarf055d452021-07-08 20:57:24 +02002806 if (sv != NULL)
2807 type = sv->sv_type;
2808 }
2809
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002810 // If a list or dict variable wasn't initialized, do it now.
2811 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2812 {
2813 tv->vval.v_dict = dict_alloc();
2814 if (tv->vval.v_dict != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002815 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002816 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002817 tv->vval.v_dict->dv_type = alloc_type(type);
2818 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002819 }
2820 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2821 {
2822 tv->vval.v_list = list_alloc();
2823 if (tv->vval.v_list != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002824 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002825 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002826 tv->vval.v_list->lv_type = alloc_type(type);
2827 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002828 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002829 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL)
2830 {
2831 tv->vval.v_blob = blob_alloc();
2832 if (tv->vval.v_blob != NULL)
2833 ++tv->vval.v_blob->bv_refcount;
2834 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002835 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002836 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002837 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002838
2839 name[len] = cc;
2840
2841 return ret;
2842}
2843
2844/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002845 * Check if variable "name[len]" is a local variable or an argument.
2846 * If so, "*eval_lavars_used" is set to TRUE.
2847 */
2848 void
2849check_vars(char_u *name, int len)
2850{
2851 int cc;
2852 char_u *varname;
2853 hashtab_T *ht;
2854
2855 if (eval_lavars_used == NULL)
2856 return;
2857
2858 // truncate the name, so that we can use strcmp()
2859 cc = name[len];
2860 name[len] = NUL;
2861
2862 ht = find_var_ht(name, &varname);
2863 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2864 {
2865 if (find_var(name, NULL, TRUE) != NULL)
2866 *eval_lavars_used = TRUE;
2867 }
2868
2869 name[len] = cc;
2870}
2871
2872/*
2873 * Find variable "name" in the list of variables.
2874 * Return a pointer to it if found, NULL if not found.
2875 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002876 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002877 */
2878 dictitem_T *
2879find_var(char_u *name, hashtab_T **htp, int no_autoload)
2880{
2881 char_u *varname;
2882 hashtab_T *ht;
2883 dictitem_T *ret = NULL;
2884
2885 ht = find_var_ht(name, &varname);
2886 if (htp != NULL)
2887 *htp = ht;
2888 if (ht == NULL)
2889 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002890 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002891 if (ret != NULL)
2892 return ret;
2893
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002894 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002895 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002896 if (ret != NULL)
2897 return ret;
2898
2899 // in Vim9 script items without a scope can be script-local
2900 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2901 {
2902 ht = get_script_local_ht();
2903 if (ht != NULL)
2904 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002905 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002906 if (ret != NULL)
2907 {
2908 if (htp != NULL)
2909 *htp = ht;
2910 return ret;
2911 }
2912 }
2913 }
2914
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002915 // When using "vim9script autoload" script-local items are prefixed but can
2916 // be used with s:name.
2917 if (SCRIPT_ID_VALID(current_sctx.sc_sid)
2918 && name[0] == 's' && name[1] == ':')
2919 {
2920 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2921
2922 if (si->sn_autoload_prefix != NULL)
2923 {
2924 char_u *auto_name = concat_str(si->sn_autoload_prefix, name + 2);
2925
2926 if (auto_name != NULL)
2927 {
2928 ht = &globvarht;
2929 ret = find_var_in_ht(ht, *name, auto_name, TRUE);
Bram Moolenaar130f65d2022-01-13 20:39:41 +00002930 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002931 if (ret != NULL)
2932 {
2933 if (htp != NULL)
2934 *htp = ht;
2935 return ret;
2936 }
2937 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002938 }
2939 }
2940
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002941 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002942}
2943
2944/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00002945 * Like find_var() but if the name starts with <SNR>99_ then look in the
2946 * referenced script (used for a funcref).
2947 */
2948 dictitem_T *
2949find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
2950{
2951 if (STRNCMP(name, "<SNR>", 5) == 0 && isdigit(name[5]))
2952 {
2953 char_u *p = name + 5;
2954 int sid = getdigits(&p);
2955
2956 if (SCRIPT_ID_VALID(sid) && *p == '_')
2957 {
2958 hashtab_T *ht = &SCRIPT_VARS(sid);
2959
2960 if (ht != NULL)
2961 {
2962 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
2963
2964 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00002965 {
2966 if (htp != NULL)
2967 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00002968 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00002969 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00002970 }
2971 }
2972 }
2973
2974 return find_var(name, htp, no_autoload);
2975}
2976
2977/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002978 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002979 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002980 * Returns NULL if not found.
2981 */
2982 dictitem_T *
2983find_var_in_ht(
2984 hashtab_T *ht,
2985 int htname,
2986 char_u *varname,
2987 int no_autoload)
2988{
2989 hashitem_T *hi;
2990
2991 if (*varname == NUL)
2992 {
2993 // Must be something like "s:", otherwise "ht" would be NULL.
2994 switch (htname)
2995 {
2996 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2997 case 'g': return &globvars_var;
2998 case 'v': return &vimvars_var;
2999 case 'b': return &curbuf->b_bufvar;
3000 case 'w': return &curwin->w_winvar;
3001 case 't': return &curtab->tp_winvar;
3002 case 'l': return get_funccal_local_var();
3003 case 'a': return get_funccal_args_var();
3004 }
3005 return NULL;
3006 }
3007
3008 hi = hash_find(ht, varname);
3009 if (HASHITEM_EMPTY(hi))
3010 {
3011 // For global variables we may try auto-loading the script. If it
3012 // worked find the variable again. Don't auto-load a script if it was
3013 // loaded already, otherwise it would be loaded every time when
3014 // checking if a function name is a Funcref variable.
3015 if (ht == &globvarht && !no_autoload)
3016 {
3017 // Note: script_autoload() may make "hi" invalid. It must either
3018 // be obtained again or not used.
3019 if (!script_autoload(varname, FALSE) || aborting())
3020 return NULL;
3021 hi = hash_find(ht, varname);
3022 }
3023 if (HASHITEM_EMPTY(hi))
3024 return NULL;
3025 }
3026 return HI2DI(hi);
3027}
3028
3029/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 * Get the script-local hashtab. NULL if not in a script context.
3031 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003032 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033get_script_local_ht(void)
3034{
3035 scid_T sid = current_sctx.sc_sid;
3036
Bram Moolenaare3d46852020-08-29 13:39:17 +02003037 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 return &SCRIPT_VARS(sid);
3039 return NULL;
3040}
3041
3042/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003043 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003044 * When "cmd" is TRUE it must look like a command, a function must be followed
3045 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003046 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003048 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003049lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003050 char_u *name,
3051 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003052 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003053 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054{
3055 hashtab_T *ht = get_script_local_ht();
3056 char_u buffer[30];
3057 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003058 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003060 int is_global = FALSE;
3061 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062
3063 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003064 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 if (len < sizeof(buffer) - 1)
3066 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003067 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 vim_strncpy(buffer, name, len);
3069 p = buffer;
3070 }
3071 else
3072 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003073 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003075 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 }
3077
3078 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003079 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080
3081 // if not script-local, then perhaps imported
Bram Moolenaardc4451d2022-01-09 21:36:37 +00003082 if (res == FAIL && find_imported(p, 0, FALSE, NULL) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003083 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 if (p != buffer)
3085 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003086
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003087 // Find a function, so that a following "->" works.
3088 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3089 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003090 if (res != OK)
3091 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003092 p = skipwhite(name + len);
3093
3094 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003095 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003096 // Do not check for an internal function, since it might also be a
3097 // valid command, such as ":split" versus "split()".
3098 // Skip "g:" before a function name.
3099 if (name[0] == 'g' && name[1] == ':')
3100 {
3101 is_global = TRUE;
3102 fname = name + 2;
3103 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003104 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003105 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003106 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003107 }
3108
Bram Moolenaar709664c2020-12-12 14:33:41 +01003109 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110}
3111
3112/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003113 * Find the hashtab used for a variable name.
3114 * Return NULL if the name is not valid.
3115 * Set "varname" to the start of name without ':'.
3116 */
3117 hashtab_T *
3118find_var_ht(char_u *name, char_u **varname)
3119{
3120 hashitem_T *hi;
3121 hashtab_T *ht;
3122
3123 if (name[0] == NUL)
3124 return NULL;
3125 if (name[1] != ':')
3126 {
3127 // The name must not start with a colon or #.
3128 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3129 return NULL;
3130 *varname = name;
3131
3132 // "version" is "v:version" in all scopes if scriptversion < 3.
3133 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003134 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003135 {
3136 hi = hash_find(&compat_hashtab, name);
3137 if (!HASHITEM_EMPTY(hi))
3138 return &compat_hashtab;
3139 }
3140
3141 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 if (ht != NULL)
3143 return ht; // local variable
3144
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003145 // In Vim9 script items at the script level are script-local, except
3146 // for autoload names.
3147 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 {
3149 ht = get_script_local_ht();
3150 if (ht != NULL)
3151 return ht;
3152 }
3153
3154 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003155 }
3156 *varname = name + 2;
3157 if (*name == 'g') // global variable
3158 return &globvarht;
3159 // There must be no ':' or '#' in the rest of the name, unless g: is used
3160 if (vim_strchr(name + 2, ':') != NULL
3161 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3162 return NULL;
3163 if (*name == 'b') // buffer variable
3164 return &curbuf->b_vars->dv_hashtab;
3165 if (*name == 'w') // window variable
3166 return &curwin->w_vars->dv_hashtab;
3167 if (*name == 't') // tab page variable
3168 return &curtab->tp_vars->dv_hashtab;
3169 if (*name == 'v') // v: variable
3170 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003171 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003172 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003174 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 if (*name == 'a') // a: function argument
3176 return get_funccal_args_ht();
3177 if (*name == 'l') // l: local function variable
3178 return get_funccal_local_ht();
3179 }
3180 if (*name == 's') // script variable
3181 {
3182 ht = get_script_local_ht();
3183 if (ht != NULL)
3184 return ht;
3185 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003186 return NULL;
3187}
3188
3189/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003190 * Get the string value of a (global/local) variable.
3191 * Note: see tv_get_string() for how long the pointer remains valid.
3192 * Returns NULL when it doesn't exist.
3193 */
3194 char_u *
3195get_var_value(char_u *name)
3196{
3197 dictitem_T *v;
3198
3199 v = find_var(name, NULL, FALSE);
3200 if (v == NULL)
3201 return NULL;
3202 return tv_get_string(&v->di_tv);
3203}
3204
3205/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003206 * Allocate a new hashtab for a sourced script. It will be used while
3207 * sourcing this script and when executing functions defined in the script.
3208 */
3209 void
3210new_script_vars(scid_T id)
3211{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003212 scriptvar_T *sv;
3213
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003214 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3215 if (sv == NULL)
3216 return;
3217 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003218 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003219}
3220
3221/*
3222 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3223 * point to it.
3224 */
3225 void
3226init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3227{
3228 hash_init(&dict->dv_hashtab);
3229 dict->dv_lock = 0;
3230 dict->dv_scope = scope;
3231 dict->dv_refcount = DO_NOT_FREE_CNT;
3232 dict->dv_copyID = 0;
3233 dict_var->di_tv.vval.v_dict = dict;
3234 dict_var->di_tv.v_type = VAR_DICT;
3235 dict_var->di_tv.v_lock = VAR_FIXED;
3236 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3237 dict_var->di_key[0] = NUL;
3238}
3239
3240/*
3241 * Unreference a dictionary initialized by init_var_dict().
3242 */
3243 void
3244unref_var_dict(dict_T *dict)
3245{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003246 // Now the dict needs to be freed if no one else is using it, go back to
3247 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003248 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3249 dict_unref(dict);
3250}
3251
3252/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003253 * Clean up a list of internal variables.
3254 * Frees all allocated variables and the value they contain.
3255 * Clears hashtab "ht", does not free it.
3256 */
3257 void
3258vars_clear(hashtab_T *ht)
3259{
3260 vars_clear_ext(ht, TRUE);
3261}
3262
3263/*
3264 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3265 */
3266 void
3267vars_clear_ext(hashtab_T *ht, int free_val)
3268{
3269 int todo;
3270 hashitem_T *hi;
3271 dictitem_T *v;
3272
3273 hash_lock(ht);
3274 todo = (int)ht->ht_used;
3275 for (hi = ht->ht_array; todo > 0; ++hi)
3276 {
3277 if (!HASHITEM_EMPTY(hi))
3278 {
3279 --todo;
3280
3281 // Free the variable. Don't remove it from the hashtab,
3282 // ht_array might change then. hash_clear() takes care of it
3283 // later.
3284 v = HI2DI(hi);
3285 if (free_val)
3286 clear_tv(&v->di_tv);
3287 if (v->di_flags & DI_FLAGS_ALLOC)
3288 vim_free(v);
3289 }
3290 }
3291 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003292 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003293}
3294
3295/*
3296 * Delete a variable from hashtab "ht" at item "hi".
3297 * Clear the variable value and free the dictitem.
3298 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003299 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003300delete_var(hashtab_T *ht, hashitem_T *hi)
3301{
3302 dictitem_T *di = HI2DI(hi);
3303
3304 hash_remove(ht, hi);
3305 clear_tv(&di->di_tv);
3306 vim_free(di);
3307}
3308
3309/*
3310 * List the value of one internal variable.
3311 */
3312 static void
3313list_one_var(dictitem_T *v, char *prefix, int *first)
3314{
3315 char_u *tofree;
3316 char_u *s;
3317 char_u numbuf[NUMBUFLEN];
3318
3319 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3320 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3321 s == NULL ? (char_u *)"" : s, first);
3322 vim_free(tofree);
3323}
3324
3325 static void
3326list_one_var_a(
3327 char *prefix,
3328 char_u *name,
3329 int type,
3330 char_u *string,
3331 int *first) // when TRUE clear rest of screen and set to FALSE
3332{
3333 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3334 msg_start();
3335 msg_puts(prefix);
3336 if (name != NULL) // "a:" vars don't have a name stored
3337 msg_puts((char *)name);
3338 msg_putchar(' ');
3339 msg_advance(22);
3340 if (type == VAR_NUMBER)
3341 msg_putchar('#');
3342 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3343 msg_putchar('*');
3344 else if (type == VAR_LIST)
3345 {
3346 msg_putchar('[');
3347 if (*string == '[')
3348 ++string;
3349 }
3350 else if (type == VAR_DICT)
3351 {
3352 msg_putchar('{');
3353 if (*string == '{')
3354 ++string;
3355 }
3356 else
3357 msg_putchar(' ');
3358
3359 msg_outtrans(string);
3360
3361 if (type == VAR_FUNC || type == VAR_PARTIAL)
3362 msg_puts("()");
3363 if (*first)
3364 {
3365 msg_clr_eos();
3366 *first = FALSE;
3367 }
3368}
3369
3370/*
3371 * Set variable "name" to value in "tv".
3372 * If the variable already exists, the value is updated.
3373 * Otherwise the variable is created.
3374 */
3375 void
3376set_var(
3377 char_u *name,
3378 typval_T *tv,
3379 int copy) // make copy of value in "tv"
3380{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003381 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003382}
3383
3384/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003385 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003386 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003387 * If the variable already exists and "is_const" is FALSE the value is updated.
3388 * Otherwise the variable is created.
3389 */
3390 void
3391set_var_const(
3392 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003393 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003394 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003395 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003396 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003397 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003398 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003399{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003400 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003401 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003402 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403 dictitem_T *di;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003404 typval_T *dest_tv = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003405 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003406 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003407 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003409 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003410 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003411 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003412 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003413 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003414
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003415 if (sid != 0)
3416 {
3417 if (SCRIPT_ID_VALID(sid))
3418 ht = &SCRIPT_VARS(sid);
3419 varname = name;
3420 }
3421 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003422 {
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003423 scriptitem_T *si;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003424
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003425 if (in_vim9script() && is_export
3426 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3427 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
3428 ->sn_autoload_prefix != NULL)
3429 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003430 // In a vim9 autoload script an exported variable is put in the
3431 // global namespace with the autoload prefix.
3432 var_in_autoload = TRUE;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003433 varname = concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003434 if (varname == NULL)
3435 goto failed;
3436 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003437 ht = &globvarht;
3438 }
3439 else
3440 ht = find_var_ht(name, &varname);
3441 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003442 if (ht == NULL || *varname == NUL)
3443 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003444 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003445 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003446 }
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003447 is_script_local = ht == get_script_local_ht() || sid != 0 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003448
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003449 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003450 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003451 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003452 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003453 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003454 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003455 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003456 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003457 }
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003458 if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
3459 && vim_strchr((char_u *)"gwbt", name[0]) != NULL)
3460 // Do not make g:var, w:var, b:var or t:var final.
3461 flags &= ~ASSIGN_FINAL;
3462
Bram Moolenaare535db82021-03-31 21:07:24 +02003463 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003464 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3465 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003466 // For "[a, _] = list" the underscore is ignored.
3467 if ((flags & ASSIGN_UNPACK) == 0)
3468 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003469 goto failed;
3470 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003471
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003472 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003473
Bram Moolenaar24e93162021-07-18 20:40:33 +02003474 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003475 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +00003476 imported_T *import = find_imported(varname, 0, FALSE, NULL);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003477
Bram Moolenaar24e93162021-07-18 20:40:33 +02003478 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003479 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003480 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02003481 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003483 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003484 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003486 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3487 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003488 }
3489 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490
Bram Moolenaar24e93162021-07-18 20:40:33 +02003491 if (dest_tv == NULL)
3492 {
3493 // Search in parent scope which is possible to reference from lambda
3494 if (di == NULL)
3495 di = find_var_in_scoped_ht(name, TRUE);
3496
3497 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003498 && var_wrong_func_name(name, di == NULL))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003499 goto failed;
3500
3501 if (need_convert_to_bool(type, tv))
3502 {
Bram Moolenaarf6488542021-07-18 21:24:50 +02003503 // Destination is a bool and the value is not, but it can be
3504 // converted.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003505 CLEAR_FIELD(bool_tv);
3506 bool_tv.v_type = VAR_BOOL;
3507 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3508 tv = &bool_tv;
3509 }
3510
3511 if (di != NULL)
3512 {
3513 // Item already exists. Allowed to replace when reloading.
3514 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
3515 {
3516 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaarf6488542021-07-18 21:24:50 +02003517 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003518 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003519 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar24e93162021-07-18 20:40:33 +02003520 goto failed;
3521 }
3522
3523 if (is_script_local && vim9script
Bram Moolenaarf6488542021-07-18 21:24:50 +02003524 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003525 {
3526 semsg(_(e_redefining_script_item_str), name);
3527 goto failed;
3528 }
3529
Bram Moolenaara06758d2021-10-15 00:18:37 +01003530 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003531 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003532 where_T where = WHERE_INIT;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003533 svar_T *sv = find_typval_in_script(&di->di_tv, sid);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003534
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003535 if (sv != NULL)
3536 {
3537 // check the type and adjust to bool if needed
3538 where.wt_index = var_idx;
3539 where.wt_variable = TRUE;
3540 if (check_script_var_type(sv, tv, name, where) == FAIL)
3541 goto failed;
3542 if (type == NULL)
3543 type = sv->sv_type;
3544 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003545 }
3546
Bram Moolenaara06758d2021-10-15 00:18:37 +01003547 if ((flags & ASSIGN_FOR_LOOP) == 0
3548 && var_check_permission(di, name) == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003549 goto failed;
3550 }
3551 else
3552 {
3553 // can only redefine once
3554 di->di_flags &= ~DI_FLAGS_RELOAD;
3555
Bram Moolenaarf6488542021-07-18 21:24:50 +02003556 // A Vim9 script-local variable is also present in sn_all_vars
3557 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003558 if (var_in_vim9script || var_in_autoload)
3559 update_vim9_script_var(FALSE, di,
3560 var_in_autoload ? name : di->di_key, flags,
3561 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003562 }
3563
3564 // existing variable, need to clear the value
3565
3566 // Handle setting internal di: variables separately where needed to
3567 // prevent changing the type.
3568 if (ht == &vimvarht)
3569 {
3570 if (di->di_tv.v_type == VAR_STRING)
3571 {
3572 VIM_CLEAR(di->di_tv.vval.v_string);
3573 if (copy || tv->v_type != VAR_STRING)
3574 {
3575 char_u *val = tv_get_string(tv);
3576
Bram Moolenaarf6488542021-07-18 21:24:50 +02003577 // Careful: when assigning to v:errmsg and
3578 // tv_get_string() causes an error message the variable
3579 // will already be set.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003580 if (di->di_tv.vval.v_string == NULL)
3581 di->di_tv.vval.v_string = vim_strsave(val);
3582 }
3583 else
3584 {
3585 // Take over the string to avoid an extra alloc/free.
3586 di->di_tv.vval.v_string = tv->vval.v_string;
3587 tv->vval.v_string = NULL;
3588 }
3589 goto failed;
3590 }
3591 else if (di->di_tv.v_type == VAR_NUMBER)
3592 {
3593 di->di_tv.vval.v_number = tv_get_number(tv);
3594 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003595 set_search_direction(di->di_tv.vval.v_number
3596 ? '/' : '?');
Bram Moolenaar24e93162021-07-18 20:40:33 +02003597#ifdef FEAT_SEARCH_EXTRA
3598 else if (STRCMP(varname, "hlsearch") == 0)
3599 {
3600 no_hlsearch = !di->di_tv.vval.v_number;
3601 redraw_all_later(SOME_VALID);
3602 }
3603#endif
3604 goto failed;
3605 }
3606 else if (di->di_tv.v_type != tv->v_type)
3607 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003608 semsg(_(e_setting_str_to_value_with_wrong_type), name);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003609 goto failed;
3610 }
3611 }
3612
3613 clear_tv(&di->di_tv);
3614 }
3615 else
3616 {
3617 // Item not found, check if a function already exists.
3618 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaarf6488542021-07-18 21:24:50 +02003619 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003620 {
3621 semsg(_(e_redefining_script_item_str), name);
3622 goto failed;
3623 }
3624
Bram Moolenaar24e93162021-07-18 20:40:33 +02003625 // add a new variable
3626 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3627 {
3628 semsg(_(e_unknown_variable_str), name);
3629 goto failed;
3630 }
3631
3632 // Can't add "v:" or "a:" variable.
3633 if (ht == &vimvarht || ht == get_funccal_args_ht())
3634 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003635 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003636 goto failed;
3637 }
3638
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003639 // Make sure the variable name is valid. In Vim9 script an
3640 // autoload variable must be prefixed with "g:" unless in an
3641 // autoload script.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003642 if (!valid_varname(varname, -1, !vim9script
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003643 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003644 goto failed;
3645
3646 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3647 if (di == NULL)
3648 goto failed;
3649 STRCPY(di->di_key, varname);
3650 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
3651 {
3652 vim_free(di);
3653 goto failed;
3654 }
3655 di->di_flags = DI_FLAGS_ALLOC;
3656 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3657 di->di_flags |= DI_FLAGS_LOCK;
3658
3659 // A Vim9 script-local variable is also added to sn_all_vars and
3660 // sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003661 if (var_in_vim9script || var_in_autoload)
3662 update_vim9_script_var(TRUE, di,
3663 var_in_autoload ? name : di->di_key, flags,
3664 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003665 }
3666
Bram Moolenaar24e93162021-07-18 20:40:33 +02003667 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003668 }
3669
3670 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003671 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003672 else
3673 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003674 *dest_tv = *tv;
3675 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003676 init_tv(tv);
3677 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003678 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003679
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003680 if (vim9script && type != NULL)
3681 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003682 if (type->tt_type == VAR_DICT && dest_tv->vval.v_dict != NULL)
Bram Moolenaar464393a2021-09-11 23:07:44 +02003683 {
3684 if (dest_tv->vval.v_dict->dv_type != type)
3685 {
3686 free_type(dest_tv->vval.v_dict->dv_type);
3687 dest_tv->vval.v_dict->dv_type = alloc_type(type);
3688 }
3689 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003690 else if (type->tt_type == VAR_LIST && dest_tv->vval.v_list != NULL)
Bram Moolenaar464393a2021-09-11 23:07:44 +02003691 {
3692 if (dest_tv->vval.v_list->lv_type != type)
3693 {
3694 free_type(dest_tv->vval.v_list->lv_type);
3695 dest_tv->vval.v_list->lv_type = alloc_type(type);
3696 }
3697 }
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003698 }
3699
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003700 // ":const var = value" locks the value
3701 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003702 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003703 // Like :lockvar! name: lock the value and what it contains, but only
3704 // if the reference count is up to one. That locks only literal
3705 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003706 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003707
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003708failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003709 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003710 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003711 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003712}
3713
3714/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003715 * Check in this order for backwards compatibility:
3716 * - Whether the variable is read-only
3717 * - Whether the variable value is locked
3718 * - Whether the variable is locked
3719 */
3720 int
3721var_check_permission(dictitem_T *di, char_u *name)
3722{
3723 if (var_check_ro(di->di_flags, name, FALSE)
3724 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3725 || var_check_lock(di->di_flags, name, FALSE))
3726 return FAIL;
3727 return OK;
3728}
3729
3730/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003731 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3732 * Also give an error message.
3733 */
3734 int
3735var_check_ro(int flags, char_u *name, int use_gettext)
3736{
3737 if (flags & DI_FLAGS_RO)
3738 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003739 if (name == NULL)
3740 emsg(_(e_cannot_change_readonly_variable));
3741 else
3742 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02003743 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003744 return TRUE;
3745 }
3746 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3747 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003748 if (name == NULL)
3749 emsg(_(e_cannot_set_variable_in_sandbox));
3750 else
3751 semsg(_(e_cannot_set_variable_in_sandbox_str),
3752 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003753 return TRUE;
3754 }
3755 return FALSE;
3756}
3757
3758/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003759 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3760 * Also give an error message.
3761 */
3762 int
3763var_check_lock(int flags, char_u *name, int use_gettext)
3764{
3765 if (flags & DI_FLAGS_LOCK)
3766 {
3767 semsg(_(e_variable_is_locked_str),
3768 use_gettext ? (char_u *)_(name) : name);
3769 return TRUE;
3770 }
3771 return FALSE;
3772}
3773
3774/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003775 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3776 * Also give an error message.
3777 */
3778 int
3779var_check_fixed(int flags, char_u *name, int use_gettext)
3780{
3781 if (flags & DI_FLAGS_FIX)
3782 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003783 if (name == NULL)
3784 emsg(_(e_cannot_delete_variable));
3785 else
3786 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003787 use_gettext ? (char_u *)_(name) : name);
3788 return TRUE;
3789 }
3790 return FALSE;
3791}
3792
3793/*
3794 * Check if a funcref is assigned to a valid variable name.
3795 * Return TRUE and give an error if not.
3796 */
3797 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003798var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003799 char_u *name, // points to start of variable name
3800 int new_var) // TRUE when creating the variable
3801{
Bram Moolenaar32154662021-03-28 21:14:06 +02003802 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
3803 // the name can be used without the s: prefix.
3804 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
3805 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003806 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3807 ? name[2] : name[0]))
3808 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003809 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003810 return TRUE;
3811 }
3812 // Don't allow hiding a function. When "v" is not NULL we might be
3813 // assigning another function to the same var, the type is checked
3814 // below.
3815 if (new_var && function_exists(name, FALSE))
3816 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003817 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003818 name);
3819 return TRUE;
3820 }
3821 return FALSE;
3822}
3823
3824/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003825 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3826 * value. Also give an error message, using "name" or _("name") when
3827 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003828 */
3829 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003830value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003831{
3832 if (lock & VAR_LOCKED)
3833 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003834 if (name == NULL)
3835 emsg(_(e_value_is_locked));
3836 else
3837 semsg(_(e_value_is_locked_str),
3838 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003839 return TRUE;
3840 }
3841 if (lock & VAR_FIXED)
3842 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003843 if (name == NULL)
3844 emsg(_(e_cannot_change_value));
3845 else
3846 semsg(_(e_cannot_change_value_of_str),
3847 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003848 return TRUE;
3849 }
3850 return FALSE;
3851}
3852
3853/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003854 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003855 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003856 * Return FALSE and give an error if not.
3857 */
3858 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003859valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003860{
3861 char_u *p;
3862
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003863 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003864 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003865 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003866 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003867 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003868 return FALSE;
3869 }
3870 return TRUE;
3871}
3872
3873/*
3874 * getwinvar() and gettabwinvar()
3875 */
3876 static void
3877getwinvar(
3878 typval_T *argvars,
3879 typval_T *rettv,
3880 int off) // 1 for gettabwinvar()
3881{
3882 win_T *win;
3883 char_u *varname;
3884 dictitem_T *v;
3885 tabpage_T *tp = NULL;
3886 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00003887 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003888 int need_switch_win;
3889
3890 if (off == 1)
3891 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3892 else
3893 tp = curtab;
3894 win = find_win_by_nr(&argvars[off], tp);
3895 varname = tv_get_string_chk(&argvars[off + 1]);
3896 ++emsg_off;
3897
3898 rettv->v_type = VAR_STRING;
3899 rettv->vval.v_string = NULL;
3900
3901 if (win != NULL && varname != NULL)
3902 {
3903 // Set curwin to be our win, temporarily. Also set the tabpage,
3904 // otherwise the window is not valid. Only do this when needed,
3905 // autocommands get blocked.
3906 need_switch_win = !(tp == curtab && win == curwin);
3907 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00003908 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003909 {
3910 if (*varname == '&')
3911 {
3912 if (varname[1] == NUL)
3913 {
3914 // get all window-local options in a dict
3915 dict_T *opts = get_winbuf_options(FALSE);
3916
3917 if (opts != NULL)
3918 {
3919 rettv_dict_set(rettv, opts);
3920 done = TRUE;
3921 }
3922 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003923 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003924 // window-local-option
3925 done = TRUE;
3926 }
3927 else
3928 {
3929 // Look up the variable.
3930 // Let getwinvar({nr}, "") return the "w:" dictionary.
3931 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3932 varname, FALSE);
3933 if (v != NULL)
3934 {
3935 copy_tv(&v->di_tv, rettv);
3936 done = TRUE;
3937 }
3938 }
3939 }
3940
3941 if (need_switch_win)
3942 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00003943 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003944 }
3945
3946 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3947 // use the default return value
3948 copy_tv(&argvars[off + 2], rettv);
3949
3950 --emsg_off;
3951}
3952
3953/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003954 * Set option "varname" to the value of "varp" for the current buffer/window.
3955 */
3956 static void
3957set_option_from_tv(char_u *varname, typval_T *varp)
3958{
3959 long numval = 0;
3960 char_u *strval;
3961 char_u nbuf[NUMBUFLEN];
3962 int error = FALSE;
3963
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003964 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003965 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003966 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003967 strval = (char_u *)"0"; // avoid using "false"
3968 }
3969 else
3970 {
3971 if (!in_vim9script() || varp->v_type != VAR_STRING)
3972 numval = (long)tv_get_number_chk(varp, &error);
3973 strval = tv_get_string_buf_chk(varp, nbuf);
3974 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02003975 if (!error && strval != NULL)
3976 set_option_value(varname, numval, strval, OPT_LOCAL);
3977}
3978
3979/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003980 * "setwinvar()" and "settabwinvar()" functions
3981 */
3982 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003983setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003984{
3985 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00003986 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003987 int need_switch_win;
3988 char_u *varname, *winvarname;
3989 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003990 tabpage_T *tp = NULL;
3991
3992 if (check_secure())
3993 return;
3994
3995 if (off == 1)
3996 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3997 else
3998 tp = curtab;
3999 win = find_win_by_nr(&argvars[off], tp);
4000 varname = tv_get_string_chk(&argvars[off + 1]);
4001 varp = &argvars[off + 2];
4002
4003 if (win != NULL && varname != NULL && varp != NULL)
4004 {
4005 need_switch_win = !(tp == curtab && win == curwin);
4006 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00004007 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004008 {
4009 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02004010 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004011 else
4012 {
4013 winvarname = alloc(STRLEN(varname) + 3);
4014 if (winvarname != NULL)
4015 {
4016 STRCPY(winvarname, "w:");
4017 STRCPY(winvarname + 2, varname);
4018 set_var(winvarname, varp, TRUE);
4019 vim_free(winvarname);
4020 }
4021 }
4022 }
4023 if (need_switch_win)
Bram Moolenaar18f47402022-01-06 13:24:51 +00004024 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004025 }
4026}
4027
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004028/*
4029 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4030 * v:option_type, and v:option_command.
4031 */
4032 void
4033reset_v_option_vars(void)
4034{
4035 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4036 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4037 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4038 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4039 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4040 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4041}
4042
4043/*
4044 * Add an assert error to v:errors.
4045 */
4046 void
4047assert_error(garray_T *gap)
4048{
4049 struct vimvar *vp = &vimvars[VV_ERRORS];
4050
Bram Moolenaard787e402021-12-24 21:36:12 +00004051 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004052 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004053 set_vim_var_list(VV_ERRORS, list_alloc());
4054 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4055}
4056
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004057 int
4058var_exists(char_u *var)
4059{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004060 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004061 char_u *name;
4062 char_u *tofree;
4063 typval_T tv;
4064 int len = 0;
4065 int n = FALSE;
4066
4067 // get_name_len() takes care of expanding curly braces
4068 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004069 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004070 if (len > 0)
4071 {
4072 if (tofree != NULL)
4073 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004074 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004075 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004076 if (n)
4077 {
4078 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004079 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004080 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4081 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004082 if (n)
4083 clear_tv(&tv);
4084 }
4085 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004086 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004087 n = FALSE;
4088
4089 vim_free(tofree);
4090 return n;
4091}
4092
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004093static lval_T *redir_lval = NULL;
4094#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4095static garray_T redir_ga; // only valid when redir_lval is not NULL
4096static char_u *redir_endp = NULL;
4097static char_u *redir_varname = NULL;
4098
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004099 int
4100alloc_redir_lval(void)
4101{
4102 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4103 if (redir_lval == NULL)
4104 return FAIL;
4105 return OK;
4106}
4107
4108 void
4109clear_redir_lval(void)
4110{
4111 VIM_CLEAR(redir_lval);
4112}
4113
4114 void
4115init_redir_ga(void)
4116{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004117 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004118}
4119
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004120/*
4121 * Start recording command output to a variable
4122 * When "append" is TRUE append to an existing variable.
4123 * Returns OK if successfully completed the setup. FAIL otherwise.
4124 */
4125 int
4126var_redir_start(char_u *name, int append)
4127{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004128 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004129 typval_T tv;
4130
4131 // Catch a bad name early.
4132 if (!eval_isnamec1(*name))
4133 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004134 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004135 return FAIL;
4136 }
4137
4138 // Make a copy of the name, it is used in redir_lval until redir ends.
4139 redir_varname = vim_strsave(name);
4140 if (redir_varname == NULL)
4141 return FAIL;
4142
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004143 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004144 {
4145 var_redir_stop();
4146 return FAIL;
4147 }
4148
4149 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004150 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004151
4152 // Parse the variable name (can be a dict or list entry).
4153 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4154 FNE_CHECK_START);
4155 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4156 {
4157 clear_lval(redir_lval);
4158 if (redir_endp != NULL && *redir_endp != NUL)
4159 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004160 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004161 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004162 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004163 redir_endp = NULL; // don't store a value, only cleanup
4164 var_redir_stop();
4165 return FAIL;
4166 }
4167
4168 // check if we can write to the variable: set it to or append an empty
4169 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004170 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004171 tv.v_type = VAR_STRING;
4172 tv.vval.v_string = (char_u *)"";
4173 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004174 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004175 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004176 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004177 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004178 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004179 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004180 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004181 {
4182 redir_endp = NULL; // don't store a value, only cleanup
4183 var_redir_stop();
4184 return FAIL;
4185 }
4186
4187 return OK;
4188}
4189
4190/*
4191 * Append "value[value_len]" to the variable set by var_redir_start().
4192 * The actual appending is postponed until redirection ends, because the value
4193 * appended may in fact be the string we write to, changing it may cause freed
4194 * memory to be used:
4195 * :redir => foo
4196 * :let foo
4197 * :redir END
4198 */
4199 void
4200var_redir_str(char_u *value, int value_len)
4201{
4202 int len;
4203
4204 if (redir_lval == NULL)
4205 return;
4206
4207 if (value_len == -1)
4208 len = (int)STRLEN(value); // Append the entire string
4209 else
4210 len = value_len; // Append only "value_len" characters
4211
4212 if (ga_grow(&redir_ga, len) == OK)
4213 {
4214 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4215 redir_ga.ga_len += len;
4216 }
4217 else
4218 var_redir_stop();
4219}
4220
4221/*
4222 * Stop redirecting command output to a variable.
4223 * Frees the allocated memory.
4224 */
4225 void
4226var_redir_stop(void)
4227{
4228 typval_T tv;
4229
4230 if (EVALCMD_BUSY)
4231 {
4232 redir_lval = NULL;
4233 return;
4234 }
4235
4236 if (redir_lval != NULL)
4237 {
4238 // If there was no error: assign the text to the variable.
4239 if (redir_endp != NULL)
4240 {
4241 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4242 tv.v_type = VAR_STRING;
4243 tv.vval.v_string = redir_ga.ga_data;
4244 // Call get_lval() again, if it's inside a Dict or List it may
4245 // have changed.
4246 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4247 FALSE, FALSE, 0, FNE_CHECK_START);
4248 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004250 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004251 clear_lval(redir_lval);
4252 }
4253
4254 // free the collected output
4255 VIM_CLEAR(redir_ga.ga_data);
4256
4257 VIM_CLEAR(redir_lval);
4258 }
4259 VIM_CLEAR(redir_varname);
4260}
4261
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004262/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004263 * Get the collected redirected text and clear redir_ga.
4264 */
4265 char_u *
4266get_clear_redir_ga(void)
4267{
4268 char_u *res;
4269
4270 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4271 res = redir_ga.ga_data;
4272 redir_ga.ga_data = NULL;
4273 return res;
4274}
4275
4276/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004277 * "gettabvar()" function
4278 */
4279 void
4280f_gettabvar(typval_T *argvars, typval_T *rettv)
4281{
Bram Moolenaar18f47402022-01-06 13:24:51 +00004282 switchwin_T switchwin;
4283 tabpage_T *tp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004284 dictitem_T *v;
4285 char_u *varname;
4286 int done = FALSE;
4287
4288 rettv->v_type = VAR_STRING;
4289 rettv->vval.v_string = NULL;
4290
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004291 if (in_vim9script()
4292 && (check_for_number_arg(argvars, 0) == FAIL
4293 || check_for_string_arg(argvars, 1) == FAIL))
4294 return;
4295
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004296 varname = tv_get_string_chk(&argvars[1]);
4297 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4298 if (tp != NULL && varname != NULL)
4299 {
4300 // Set tp to be our tabpage, temporarily. Also set the window to the
4301 // first window in the tabpage, otherwise the window is not valid.
Bram Moolenaar18f47402022-01-06 13:24:51 +00004302 if (switch_win(&switchwin,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004303 tp == curtab || tp->tp_firstwin == NULL ? firstwin
4304 : tp->tp_firstwin, tp, TRUE) == OK)
4305 {
4306 // look up the variable
4307 // Let gettabvar({nr}, "") return the "t:" dictionary.
4308 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
4309 if (v != NULL)
4310 {
4311 copy_tv(&v->di_tv, rettv);
4312 done = TRUE;
4313 }
4314 }
4315
4316 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004317 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004318 }
4319
4320 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4321 copy_tv(&argvars[2], rettv);
4322}
4323
4324/*
4325 * "gettabwinvar()" function
4326 */
4327 void
4328f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4329{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004330 if (in_vim9script()
4331 && (check_for_number_arg(argvars, 0) == FAIL
4332 || check_for_number_arg(argvars, 1) == FAIL
4333 || check_for_string_arg(argvars, 2) == FAIL))
4334 return;
4335
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004336 getwinvar(argvars, rettv, 1);
4337}
4338
4339/*
4340 * "getwinvar()" function
4341 */
4342 void
4343f_getwinvar(typval_T *argvars, typval_T *rettv)
4344{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004345 if (in_vim9script()
4346 && (check_for_number_arg(argvars, 0) == FAIL
4347 || check_for_string_arg(argvars, 1) == FAIL))
4348 return;
4349
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004350 getwinvar(argvars, rettv, 0);
4351}
4352
4353/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004354 * "getbufvar()" function
4355 */
4356 void
4357f_getbufvar(typval_T *argvars, typval_T *rettv)
4358{
4359 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004360 char_u *varname;
4361 dictitem_T *v;
4362 int done = FALSE;
4363
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004364 if (in_vim9script()
4365 && (check_for_buffer_arg(argvars, 0) == FAIL
4366 || check_for_string_arg(argvars, 1) == FAIL))
4367 return;
4368
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004369 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004370 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004371
4372 rettv->v_type = VAR_STRING;
4373 rettv->vval.v_string = NULL;
4374
4375 if (buf != NULL && varname != NULL)
4376 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004377 if (*varname == '&')
4378 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004379 buf_T *save_curbuf = curbuf;
4380
4381 // set curbuf to be our buf, temporarily
4382 curbuf = buf;
4383
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004384 if (varname[1] == NUL)
4385 {
4386 // get all buffer-local options in a dict
4387 dict_T *opts = get_winbuf_options(TRUE);
4388
4389 if (opts != NULL)
4390 {
4391 rettv_dict_set(rettv, opts);
4392 done = TRUE;
4393 }
4394 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004395 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004396 // buffer-local-option
4397 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02004398
4399 // restore previous notion of curbuf
4400 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004401 }
4402 else
4403 {
4404 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02004405 if (*varname == NUL)
4406 // Let getbufvar({nr}, "") return the "b:" dictionary.
4407 v = &buf->b_bufvar;
4408 else
4409 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
4410 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004411 if (v != NULL)
4412 {
4413 copy_tv(&v->di_tv, rettv);
4414 done = TRUE;
4415 }
4416 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004417 }
4418
4419 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4420 // use the default value
4421 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004422}
4423
4424/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004425 * "settabvar()" function
4426 */
4427 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004428f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004429{
4430 tabpage_T *save_curtab;
4431 tabpage_T *tp;
4432 char_u *varname, *tabvarname;
4433 typval_T *varp;
4434
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004435 if (check_secure())
4436 return;
4437
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004438 if (in_vim9script()
4439 && (check_for_number_arg(argvars, 0) == FAIL
4440 || check_for_string_arg(argvars, 1) == FAIL))
4441 return;
4442
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004443 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4444 varname = tv_get_string_chk(&argvars[1]);
4445 varp = &argvars[2];
4446
4447 if (varname != NULL && varp != NULL && tp != NULL)
4448 {
4449 save_curtab = curtab;
4450 goto_tabpage_tp(tp, FALSE, FALSE);
4451
4452 tabvarname = alloc(STRLEN(varname) + 3);
4453 if (tabvarname != NULL)
4454 {
4455 STRCPY(tabvarname, "t:");
4456 STRCPY(tabvarname + 2, varname);
4457 set_var(tabvarname, varp, TRUE);
4458 vim_free(tabvarname);
4459 }
4460
4461 // Restore current tabpage
4462 if (valid_tabpage(save_curtab))
4463 goto_tabpage_tp(save_curtab, FALSE, FALSE);
4464 }
4465}
4466
4467/*
4468 * "settabwinvar()" function
4469 */
4470 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004471f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004472{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004473 if (in_vim9script()
4474 && (check_for_number_arg(argvars, 0) == FAIL
4475 || check_for_number_arg(argvars, 1) == FAIL
4476 || check_for_string_arg(argvars, 2) == FAIL))
4477 return;
4478
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004479 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004480}
4481
4482/*
4483 * "setwinvar()" function
4484 */
4485 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004486f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004487{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004488 if (in_vim9script()
4489 && (check_for_number_arg(argvars, 0) == FAIL
4490 || check_for_string_arg(argvars, 1) == FAIL))
4491 return;
4492
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004493 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004494}
4495
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004496/*
4497 * "setbufvar()" function
4498 */
4499 void
4500f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4501{
4502 buf_T *buf;
4503 char_u *varname, *bufvarname;
4504 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004505
4506 if (check_secure())
4507 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004508
4509 if (in_vim9script()
4510 && (check_for_buffer_arg(argvars, 0) == FAIL
4511 || check_for_string_arg(argvars, 1) == FAIL))
4512 return;
4513
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004514 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004515 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004516 varp = &argvars[2];
4517
4518 if (buf != NULL && varname != NULL && varp != NULL)
4519 {
4520 if (*varname == '&')
4521 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004522 aco_save_T aco;
4523
4524 // set curbuf to be our buf, temporarily
4525 aucmd_prepbuf(&aco, buf);
4526
Bram Moolenaar191929b2020-08-19 21:20:49 +02004527 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004528
4529 // reset notion of buffer
4530 aucmd_restbuf(&aco);
4531 }
4532 else
4533 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004534 bufvarname = alloc(STRLEN(varname) + 3);
4535 if (bufvarname != NULL)
4536 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004537 buf_T *save_curbuf = curbuf;
4538
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004539 curbuf = buf;
4540 STRCPY(bufvarname, "b:");
4541 STRCPY(bufvarname + 2, varname);
4542 set_var(bufvarname, varp, TRUE);
4543 vim_free(bufvarname);
4544 curbuf = save_curbuf;
4545 }
4546 }
4547 }
4548}
4549
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004550/*
4551 * Get a callback from "arg". It can be a Funcref or a function name.
4552 * When "arg" is zero return an empty string.
4553 * "cb_name" is not allocated.
4554 * "cb_name" is set to NULL for an invalid argument.
4555 */
4556 callback_T
4557get_callback(typval_T *arg)
4558{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004559 callback_T res;
4560 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004561
4562 res.cb_free_name = FALSE;
4563 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4564 {
4565 res.cb_partial = arg->vval.v_partial;
4566 ++res.cb_partial->pt_refcount;
4567 res.cb_name = partial_name(res.cb_partial);
4568 }
4569 else
4570 {
4571 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004572 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4573 && isdigit(*arg->vval.v_string))
4574 r = FAIL;
4575 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004576 {
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004577 if (arg->v_type == VAR_STRING)
4578 {
4579 char_u *name;
4580
4581 name = get_scriptlocal_funcname(arg->vval.v_string);
4582 if (name != NULL)
4583 {
4584 vim_free(arg->vval.v_string);
4585 arg->vval.v_string = name;
4586 }
4587 }
4588
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004589 res.cb_name = arg->vval.v_string;
4590 func_ref(res.cb_name);
4591 }
4592 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004593 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004594 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004595 r = FAIL;
4596
4597 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004598 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004599 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004600 res.cb_name = NULL;
4601 }
4602 }
4603 return res;
4604}
4605
4606/*
4607 * Copy a callback into a typval_T.
4608 */
4609 void
4610put_callback(callback_T *cb, typval_T *tv)
4611{
4612 if (cb->cb_partial != NULL)
4613 {
4614 tv->v_type = VAR_PARTIAL;
4615 tv->vval.v_partial = cb->cb_partial;
4616 ++tv->vval.v_partial->pt_refcount;
4617 }
4618 else
4619 {
4620 tv->v_type = VAR_FUNC;
4621 tv->vval.v_string = vim_strsave(cb->cb_name);
4622 func_ref(cb->cb_name);
4623 }
4624}
4625
4626/*
4627 * Make a copy of "src" into "dest", allocating the function name if needed,
4628 * without incrementing the refcount.
4629 */
4630 void
4631set_callback(callback_T *dest, callback_T *src)
4632{
4633 if (src->cb_partial == NULL)
4634 {
4635 // just a function name, make a copy
4636 dest->cb_name = vim_strsave(src->cb_name);
4637 dest->cb_free_name = TRUE;
4638 }
4639 else
4640 {
4641 // cb_name is a pointer into cb_partial
4642 dest->cb_name = src->cb_name;
4643 dest->cb_free_name = FALSE;
4644 }
4645 dest->cb_partial = src->cb_partial;
4646}
4647
4648/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004649 * Copy callback from "src" to "dest", incrementing the refcounts.
4650 */
4651 void
4652copy_callback(callback_T *dest, callback_T *src)
4653{
4654 dest->cb_partial = src->cb_partial;
4655 if (dest->cb_partial != NULL)
4656 {
4657 dest->cb_name = src->cb_name;
4658 dest->cb_free_name = FALSE;
4659 ++dest->cb_partial->pt_refcount;
4660 }
4661 else
4662 {
4663 dest->cb_name = vim_strsave(src->cb_name);
4664 dest->cb_free_name = TRUE;
4665 func_ref(src->cb_name);
4666 }
4667}
4668
4669/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004670 * When a callback refers to an autoload import, change the function name to
4671 * the "path#name" form. Uses the current script context.
4672 * Only works when the name is allocated.
4673 */
4674 void
4675expand_autload_callback(callback_T *cb)
4676{
4677 char_u *p;
4678 imported_T *import;
4679
4680 if (!in_vim9script() || cb->cb_name == NULL || !cb->cb_free_name)
4681 return;
4682 p = vim_strchr(cb->cb_name, '.');
4683 if (p == NULL)
4684 return;
4685 import = find_imported(cb->cb_name, p - cb->cb_name, FALSE, NULL);
4686 if (import != NULL && SCRIPT_ID_VALID(import->imp_sid))
4687 {
4688 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
4689
4690 if (si->sn_autoload_prefix != NULL)
4691 {
4692 char_u *name = concat_str(si->sn_autoload_prefix, p + 1);
4693
4694 if (name != NULL)
4695 {
4696 vim_free(cb->cb_name);
4697 cb->cb_name = name;
4698 }
4699 }
4700 }
4701}
4702
4703/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004704 * Unref/free "callback" returned by get_callback() or set_callback().
4705 */
4706 void
4707free_callback(callback_T *callback)
4708{
4709 if (callback->cb_partial != NULL)
4710 {
4711 partial_unref(callback->cb_partial);
4712 callback->cb_partial = NULL;
4713 }
4714 else if (callback->cb_name != NULL)
4715 func_unref(callback->cb_name);
4716 if (callback->cb_free_name)
4717 {
4718 vim_free(callback->cb_name);
4719 callback->cb_free_name = FALSE;
4720 }
4721 callback->cb_name = NULL;
4722}
4723
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004724#endif // FEAT_EVAL