blob: 862d5192bb4e5fbc9cf386ae2b8c2033098014aa [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
Bram Moolenaard787e402021-12-24 21:36:12 +000048 type_T *vv_type; // type or NULL
Bram Moolenaare5cdf152019-08-29 22:09:46 +020049 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
50} vimvars[VV_LEN] =
51{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020052 // The order here must match the VV_ defines in vim.h!
53 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaard787e402021-12-24 21:36:12 +000054 {VV_NAME("count", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
55 {VV_NAME("count1", VAR_NUMBER), NULL, VV_RO},
56 {VV_NAME("prevcount", VAR_NUMBER), NULL, VV_RO},
57 {VV_NAME("errmsg", VAR_STRING), NULL, VV_COMPAT},
58 {VV_NAME("warningmsg", VAR_STRING), NULL, 0},
59 {VV_NAME("statusmsg", VAR_STRING), NULL, 0},
60 {VV_NAME("shell_error", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
61 {VV_NAME("this_session", VAR_STRING), NULL, VV_COMPAT},
62 {VV_NAME("version", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
63 {VV_NAME("lnum", VAR_NUMBER), NULL, VV_RO_SBX},
64 {VV_NAME("termresponse", VAR_STRING), NULL, VV_RO},
65 {VV_NAME("fname", VAR_STRING), NULL, VV_RO},
66 {VV_NAME("lang", VAR_STRING), NULL, VV_RO},
67 {VV_NAME("lc_time", VAR_STRING), NULL, VV_RO},
68 {VV_NAME("ctype", VAR_STRING), NULL, VV_RO},
69 {VV_NAME("charconvert_from", VAR_STRING), NULL, VV_RO},
70 {VV_NAME("charconvert_to", VAR_STRING), NULL, VV_RO},
71 {VV_NAME("fname_in", VAR_STRING), NULL, VV_RO},
72 {VV_NAME("fname_out", VAR_STRING), NULL, VV_RO},
73 {VV_NAME("fname_new", VAR_STRING), NULL, VV_RO},
74 {VV_NAME("fname_diff", VAR_STRING), NULL, VV_RO},
75 {VV_NAME("cmdarg", VAR_STRING), NULL, VV_RO},
76 {VV_NAME("foldstart", VAR_NUMBER), NULL, VV_RO_SBX},
77 {VV_NAME("foldend", VAR_NUMBER), NULL, VV_RO_SBX},
78 {VV_NAME("folddashes", VAR_STRING), NULL, VV_RO_SBX},
79 {VV_NAME("foldlevel", VAR_NUMBER), NULL, VV_RO_SBX},
80 {VV_NAME("progname", VAR_STRING), NULL, VV_RO},
81 {VV_NAME("servername", VAR_STRING), NULL, VV_RO},
82 {VV_NAME("dying", VAR_NUMBER), NULL, VV_RO},
83 {VV_NAME("exception", VAR_STRING), NULL, VV_RO},
84 {VV_NAME("throwpoint", VAR_STRING), NULL, VV_RO},
85 {VV_NAME("register", VAR_STRING), NULL, VV_RO},
86 {VV_NAME("cmdbang", VAR_NUMBER), NULL, VV_RO},
87 {VV_NAME("insertmode", VAR_STRING), NULL, VV_RO},
88 {VV_NAME("val", VAR_UNKNOWN), NULL, VV_RO},
89 {VV_NAME("key", VAR_UNKNOWN), NULL, VV_RO},
90 {VV_NAME("profiling", VAR_NUMBER), NULL, VV_RO},
91 {VV_NAME("fcs_reason", VAR_STRING), NULL, VV_RO},
92 {VV_NAME("fcs_choice", VAR_STRING), NULL, 0},
93 {VV_NAME("beval_bufnr", VAR_NUMBER), NULL, VV_RO},
94 {VV_NAME("beval_winnr", VAR_NUMBER), NULL, VV_RO},
95 {VV_NAME("beval_winid", VAR_NUMBER), NULL, VV_RO},
96 {VV_NAME("beval_lnum", VAR_NUMBER), NULL, VV_RO},
97 {VV_NAME("beval_col", VAR_NUMBER), NULL, VV_RO},
98 {VV_NAME("beval_text", VAR_STRING), NULL, VV_RO},
99 {VV_NAME("scrollstart", VAR_STRING), NULL, 0},
100 {VV_NAME("swapname", VAR_STRING), NULL, VV_RO},
101 {VV_NAME("swapchoice", VAR_STRING), NULL, 0},
102 {VV_NAME("swapcommand", VAR_STRING), NULL, VV_RO},
103 {VV_NAME("char", VAR_STRING), NULL, 0},
104 {VV_NAME("mouse_win", VAR_NUMBER), NULL, 0},
105 {VV_NAME("mouse_winid", VAR_NUMBER), NULL, 0},
106 {VV_NAME("mouse_lnum", VAR_NUMBER), NULL, 0},
107 {VV_NAME("mouse_col", VAR_NUMBER), NULL, 0},
108 {VV_NAME("operator", VAR_STRING), NULL, VV_RO},
109 {VV_NAME("searchforward", VAR_NUMBER), NULL, 0},
110 {VV_NAME("hlsearch", VAR_NUMBER), NULL, 0},
111 {VV_NAME("oldfiles", VAR_LIST), &t_list_string, 0},
112 {VV_NAME("windowid", VAR_NUMBER), NULL, VV_RO},
113 {VV_NAME("progpath", VAR_STRING), NULL, VV_RO},
114 {VV_NAME("completed_item", VAR_DICT), &t_dict_string, VV_RO},
115 {VV_NAME("option_new", VAR_STRING), NULL, VV_RO},
116 {VV_NAME("option_old", VAR_STRING), NULL, VV_RO},
117 {VV_NAME("option_oldlocal", VAR_STRING), NULL, VV_RO},
118 {VV_NAME("option_oldglobal", VAR_STRING), NULL, VV_RO},
119 {VV_NAME("option_command", VAR_STRING), NULL, VV_RO},
120 {VV_NAME("option_type", VAR_STRING), NULL, VV_RO},
121 {VV_NAME("errors", VAR_LIST), &t_list_string, 0},
122 {VV_NAME("false", VAR_BOOL), NULL, VV_RO},
123 {VV_NAME("true", VAR_BOOL), NULL, VV_RO},
124 {VV_NAME("none", VAR_SPECIAL), NULL, VV_RO},
125 {VV_NAME("null", VAR_SPECIAL), NULL, VV_RO},
126 {VV_NAME("numbermax", VAR_NUMBER), NULL, VV_RO},
127 {VV_NAME("numbermin", VAR_NUMBER), NULL, VV_RO},
128 {VV_NAME("numbersize", VAR_NUMBER), NULL, VV_RO},
129 {VV_NAME("vim_did_enter", VAR_NUMBER), NULL, VV_RO},
130 {VV_NAME("testing", VAR_NUMBER), NULL, 0},
131 {VV_NAME("t_number", VAR_NUMBER), NULL, VV_RO},
132 {VV_NAME("t_string", VAR_NUMBER), NULL, VV_RO},
133 {VV_NAME("t_func", VAR_NUMBER), NULL, VV_RO},
134 {VV_NAME("t_list", VAR_NUMBER), NULL, VV_RO},
135 {VV_NAME("t_dict", VAR_NUMBER), NULL, VV_RO},
136 {VV_NAME("t_float", VAR_NUMBER), NULL, VV_RO},
137 {VV_NAME("t_bool", VAR_NUMBER), NULL, VV_RO},
138 {VV_NAME("t_none", VAR_NUMBER), NULL, VV_RO},
139 {VV_NAME("t_job", VAR_NUMBER), NULL, VV_RO},
140 {VV_NAME("t_channel", VAR_NUMBER), NULL, VV_RO},
141 {VV_NAME("t_blob", VAR_NUMBER), NULL, VV_RO},
142 {VV_NAME("termrfgresp", VAR_STRING), NULL, VV_RO},
143 {VV_NAME("termrbgresp", VAR_STRING), NULL, VV_RO},
144 {VV_NAME("termu7resp", VAR_STRING), NULL, VV_RO},
145 {VV_NAME("termstyleresp", VAR_STRING), NULL, VV_RO},
146 {VV_NAME("termblinkresp", VAR_STRING), NULL, VV_RO},
147 {VV_NAME("event", VAR_DICT), NULL, VV_RO},
148 {VV_NAME("versionlong", VAR_NUMBER), NULL, VV_RO},
149 {VV_NAME("echospace", VAR_NUMBER), NULL, VV_RO},
150 {VV_NAME("argv", VAR_LIST), &t_list_string, VV_RO},
151 {VV_NAME("collate", VAR_STRING), NULL, VV_RO},
152 {VV_NAME("exiting", VAR_SPECIAL), NULL, VV_RO},
153 {VV_NAME("colornames", VAR_DICT), &t_dict_string, VV_RO},
154 {VV_NAME("sizeofint", VAR_NUMBER), NULL, VV_RO},
155 {VV_NAME("sizeoflong", VAR_NUMBER), NULL, VV_RO},
156 {VV_NAME("sizeofpointer", VAR_NUMBER), NULL, VV_RO},
naohiro ono56200ee2022-01-01 14:59:44 +0000157 {VV_NAME("maxcol", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200158};
159
160// shorthand
Bram Moolenaard787e402021-12-24 21:36:12 +0000161#define vv_tv_type vv_di.di_tv.v_type
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200162#define vv_nr vv_di.di_tv.vval.v_number
163#define vv_float vv_di.di_tv.vval.v_float
164#define vv_str vv_di.di_tv.vval.v_string
165#define vv_list vv_di.di_tv.vval.v_list
166#define vv_dict vv_di.di_tv.vval.v_dict
167#define vv_blob vv_di.di_tv.vval.v_blob
168#define vv_tv vv_di.di_tv
169
170static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200171static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200172#define vimvarht vimvardict.dv_hashtab
173
174// for VIM_VERSION_ defines
175#include "version.h"
176
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200177static void list_glob_vars(int *first);
178static void list_buf_vars(int *first);
179static void list_win_vars(int *first);
180static void list_tab_vars(int *first);
181static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100182static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op, int var_idx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200183static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
184static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200185static void list_one_var(dictitem_T *v, char *prefix, int *first);
186static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
187
188/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200189 * Initialize global and vim special variables
190 */
191 void
192evalvars_init(void)
193{
194 int i;
195 struct vimvar *p;
196
197 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
198 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
199 vimvardict.dv_lock = VAR_FIXED;
200 hash_init(&compat_hashtab);
201
202 for (i = 0; i < VV_LEN; ++i)
203 {
204 p = &vimvars[i];
205 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
206 {
207 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
208 getout(1);
209 }
210 STRCPY(p->vv_di.di_key, p->vv_name);
211 if (p->vv_flags & VV_RO)
212 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
213 else if (p->vv_flags & VV_RO_SBX)
214 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
215 else
216 p->vv_di.di_flags = DI_FLAGS_FIX;
217
218 // add to v: scope dict, unless the value is not always available
Bram Moolenaard787e402021-12-24 21:36:12 +0000219 if (p->vv_tv_type != VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200220 hash_add(&vimvarht, p->vv_di.di_key);
221 if (p->vv_flags & VV_COMPAT)
222 // add to compat scope dict
223 hash_add(&compat_hashtab, p->vv_di.di_key);
224 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200225 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
226 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200227
228 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
229 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100230 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200231 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
232 set_vim_var_list(VV_ERRORS, list_alloc());
233 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
234
235 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
236 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
237 set_vim_var_nr(VV_NONE, VVAL_NONE);
238 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100239 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
240 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100241 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaar69b30722021-11-02 21:39:49 +0000242 set_vim_var_nr(VV_SIZEOFINT, sizeof(int));
243 set_vim_var_nr(VV_SIZEOFLONG, sizeof(long));
244 set_vim_var_nr(VV_SIZEOFPOINTER, sizeof(char *));
naohiro ono56200ee2022-01-01 14:59:44 +0000245 set_vim_var_nr(VV_MAXCOL, MAXCOL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200246
247 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
248 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
249 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
250 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
251 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
252 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
253 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
254 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
255 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
256 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
257 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
258
259 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
260
Drew Vogele30d1022021-10-24 20:35:07 +0100261 set_vim_var_dict(VV_COLORNAMES, dict_alloc());
262
Bram Moolenaar439c0362020-06-06 15:58:03 +0200263 // Default for v:register is not 0 but '"'. This is adjusted once the
264 // clipboard has been setup by calling reset_reg_var().
265 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200266}
267
268#if defined(EXITFREE) || defined(PROTO)
269/*
270 * Free all vim variables information on exit
271 */
272 void
273evalvars_clear(void)
274{
275 int i;
276 struct vimvar *p;
277
278 for (i = 0; i < VV_LEN; ++i)
279 {
280 p = &vimvars[i];
281 if (p->vv_di.di_tv.v_type == VAR_STRING)
282 VIM_CLEAR(p->vv_str);
283 else if (p->vv_di.di_tv.v_type == VAR_LIST)
284 {
285 list_unref(p->vv_list);
286 p->vv_list = NULL;
287 }
288 }
289 hash_clear(&vimvarht);
290 hash_init(&vimvarht); // garbage_collect() will access it
291 hash_clear(&compat_hashtab);
292
293 // global variables
294 vars_clear(&globvarht);
295
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100296 // Script-local variables. Clear all the variables here.
297 // The scriptvar_T is cleared later in free_scriptnames(), because a
298 // variable in one script might hold a reference to the whole scope of
299 // another script.
300 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200301 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200302}
303#endif
304
305 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200306garbage_collect_globvars(int copyID)
307{
308 return set_ref_in_ht(&globvarht, copyID, NULL);
309}
310
311 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200312garbage_collect_vimvars(int copyID)
313{
314 return set_ref_in_ht(&vimvarht, copyID, NULL);
315}
316
317 int
318garbage_collect_scriptvars(int copyID)
319{
Bram Moolenaared234f22020-10-15 20:42:20 +0200320 int i;
321 int idx;
322 int abort = FALSE;
323 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200324
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100325 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200326 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200327 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
328
Bram Moolenaared234f22020-10-15 20:42:20 +0200329 si = SCRIPT_ITEM(i);
330 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
331 {
332 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
333
Bram Moolenaard00a7fb2021-03-08 20:47:14 +0100334 if (sv->sv_name != NULL)
335 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
Bram Moolenaared234f22020-10-15 20:42:20 +0200336 }
337 }
338
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200339 return abort;
340}
341
342/*
343 * Set an internal variable to a string value. Creates the variable if it does
344 * not already exist.
345 */
346 void
347set_internal_string_var(char_u *name, char_u *value)
348{
349 char_u *val;
350 typval_T *tvp;
351
352 val = vim_strsave(value);
353 if (val != NULL)
354 {
355 tvp = alloc_string_tv(val);
356 if (tvp != NULL)
357 {
358 set_var(name, tvp, FALSE);
359 free_tv(tvp);
360 }
361 }
362}
363
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200364 int
365eval_charconvert(
366 char_u *enc_from,
367 char_u *enc_to,
368 char_u *fname_from,
369 char_u *fname_to)
370{
371 int err = FALSE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000372 sctx_T saved_sctx = current_sctx;
373 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200374
375 set_vim_var_string(VV_CC_FROM, enc_from, -1);
376 set_vim_var_string(VV_CC_TO, enc_to, -1);
377 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
378 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000379 ctx = get_option_sctx("charconvert");
380 if (ctx != NULL)
381 current_sctx = *ctx;
382
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200383 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
384 err = TRUE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000385
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200386 set_vim_var_string(VV_CC_FROM, NULL, -1);
387 set_vim_var_string(VV_CC_TO, NULL, -1);
388 set_vim_var_string(VV_FNAME_IN, NULL, -1);
389 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000390 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200391
392 if (err)
393 return FAIL;
394 return OK;
395}
396
397# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
398 int
399eval_printexpr(char_u *fname, char_u *args)
400{
401 int err = FALSE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000402 sctx_T saved_sctx = current_sctx;
403 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200404
405 set_vim_var_string(VV_FNAME_IN, fname, -1);
406 set_vim_var_string(VV_CMDARG, args, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000407 ctx = get_option_sctx("printexpr");
408 if (ctx != NULL)
409 current_sctx = *ctx;
410
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200411 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
412 err = TRUE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000413
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200414 set_vim_var_string(VV_FNAME_IN, NULL, -1);
415 set_vim_var_string(VV_CMDARG, NULL, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000416 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200417
418 if (err)
419 {
420 mch_remove(fname);
421 return FAIL;
422 }
423 return OK;
424}
425# endif
426
427# if defined(FEAT_DIFF) || defined(PROTO)
428 void
429eval_diff(
430 char_u *origfile,
431 char_u *newfile,
432 char_u *outfile)
433{
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000434 sctx_T saved_sctx = current_sctx;
435 sctx_T *ctx;
436 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200437
438 set_vim_var_string(VV_FNAME_IN, origfile, -1);
439 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
440 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000441
442 ctx = get_option_sctx("diffexpr");
443 if (ctx != NULL)
444 current_sctx = *ctx;
445
446 // errors are ignored
447 tv = eval_expr(p_dex, NULL);
Bram Moolenaar39b89442022-01-22 18:21:36 +0000448 free_tv(tv);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000449
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200450 set_vim_var_string(VV_FNAME_IN, NULL, -1);
451 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
452 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000453 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200454}
455
456 void
457eval_patch(
458 char_u *origfile,
459 char_u *difffile,
460 char_u *outfile)
461{
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000462 sctx_T saved_sctx = current_sctx;
463 sctx_T *ctx;
464 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200465
466 set_vim_var_string(VV_FNAME_IN, origfile, -1);
467 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
468 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000469
470 ctx = get_option_sctx("patchexpr");
471 if (ctx != NULL)
472 current_sctx = *ctx;
473
474 // errors are ignored
475 tv = eval_expr(p_pex, NULL);
476 free_tv(tv);
477
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200478 set_vim_var_string(VV_FNAME_IN, NULL, -1);
479 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
480 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000481 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200482}
483# endif
484
485#if defined(FEAT_SPELL) || defined(PROTO)
486/*
487 * Evaluate an expression to a list with suggestions.
488 * For the "expr:" part of 'spellsuggest'.
489 * Returns NULL when there is an error.
490 */
491 list_T *
492eval_spell_expr(char_u *badword, char_u *expr)
493{
494 typval_T save_val;
495 typval_T rettv;
496 list_T *list = NULL;
497 char_u *p = skipwhite(expr);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000498 sctx_T saved_sctx = current_sctx;
499 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200500
501 // Set "v:val" to the bad word.
502 prepare_vimvar(VV_VAL, &save_val);
503 set_vim_var_string(VV_VAL, badword, -1);
504 if (p_verbose == 0)
505 ++emsg_off;
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000506 ctx = get_option_sctx("spellsuggest");
507 if (ctx != NULL)
508 current_sctx = *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200509
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200510 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200511 {
512 if (rettv.v_type != VAR_LIST)
513 clear_tv(&rettv);
514 else
515 list = rettv.vval.v_list;
516 }
517
518 if (p_verbose == 0)
519 --emsg_off;
520 clear_tv(get_vim_var_tv(VV_VAL));
521 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000522 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200523
524 return list;
525}
526
527/*
528 * "list" is supposed to contain two items: a word and a number. Return the
529 * word in "pp" and the number as the return value.
530 * Return -1 if anything isn't right.
531 * Used to get the good word and score from the eval_spell_expr() result.
532 */
533 int
534get_spellword(list_T *list, char_u **pp)
535{
536 listitem_T *li;
537
538 li = list->lv_first;
539 if (li == NULL)
540 return -1;
541 *pp = tv_get_string(&li->li_tv);
542
543 li = li->li_next;
544 if (li == NULL)
545 return -1;
546 return (int)tv_get_number(&li->li_tv);
547}
548#endif
549
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200550/*
551 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200552 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200553 * When not used yet add the variable to the v: hashtable.
554 */
555 void
556prepare_vimvar(int idx, typval_T *save_tv)
557{
558 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200559 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaard787e402021-12-24 21:36:12 +0000560 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200561 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
562}
563
564/*
565 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200566 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200567 * When no longer defined, remove the variable from the v: hashtable.
568 */
569 void
570restore_vimvar(int idx, typval_T *save_tv)
571{
572 hashitem_T *hi;
573
574 vimvars[idx].vv_tv = *save_tv;
Bram Moolenaard787e402021-12-24 21:36:12 +0000575 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200576 {
577 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
578 if (HASHITEM_EMPTY(hi))
579 internal_error("restore_vimvar()");
580 else
581 hash_remove(&vimvarht, hi);
582 }
583}
584
585/*
586 * List Vim variables.
587 */
588 static void
589list_vim_vars(int *first)
590{
591 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
592}
593
594/*
595 * List script-local variables, if there is a script.
596 */
597 static void
598list_script_vars(int *first)
599{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200600 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200601 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
602 "s:", FALSE, first);
603}
604
605/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200606 * Get a list of lines from a HERE document. The here document is a list of
607 * lines surrounded by a marker.
608 * cmd << {marker}
609 * {line1}
610 * {line2}
611 * ....
612 * {marker}
613 *
614 * The {marker} is a string. If the optional 'trim' word is supplied before the
615 * marker, then the leading indentation before the lines (matching the
616 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200617 *
618 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
619 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
620 * missing, then '.' is accepted as a marker.
621 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200622 * Returns a List with {lines} or NULL.
623 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200625heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200626{
627 char_u *theline;
628 char_u *marker;
629 list_T *l;
630 char_u *p;
631 int marker_indent_len = 0;
632 int text_indent_len = 0;
633 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200634 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200635 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200636
637 if (eap->getline == NULL)
638 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000639 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200640 return NULL;
641 }
642
643 // Check for the optional 'trim' word before the marker
644 cmd = skipwhite(cmd);
645 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
646 {
647 cmd = skipwhite(cmd + 4);
648
649 // Trim the indentation from all the lines in the here document.
650 // The amount of indentation trimmed is the same as the indentation of
651 // the first line after the :let command line. To find the end marker
652 // the indent of the :let command line is trimmed.
653 p = *eap->cmdlinep;
654 while (VIM_ISWHITE(*p))
655 {
656 p++;
657 marker_indent_len++;
658 }
659 text_indent_len = -1;
660 }
661
662 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200663 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200664 {
665 marker = skipwhite(cmd);
666 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200667 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200668 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000669 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200670 return NULL;
671 }
672 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200673 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200674 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000675 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200676 return NULL;
677 }
678 }
679 else
680 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200681 // When getting lines for an embedded script, if the marker is missing,
682 // accept '.' as the marker.
683 if (script_get)
684 marker = dot;
685 else
686 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000687 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200688 return NULL;
689 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200690 }
691
692 l = list_alloc();
693 if (l == NULL)
694 return NULL;
695
696 for (;;)
697 {
698 int mi = 0;
699 int ti = 0;
700
701 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
702 if (theline == NULL)
703 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000704 semsg(_(e_missing_end_marker_str), marker);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200705 break;
706 }
707
708 // with "trim": skip the indent matching the :let line to find the
709 // marker
710 if (marker_indent_len > 0
711 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
712 mi = marker_indent_len;
713 if (STRCMP(marker, theline + mi) == 0)
714 {
715 vim_free(theline);
716 break;
717 }
718
719 if (text_indent_len == -1 && *theline != NUL)
720 {
721 // set the text indent from the first line.
722 p = theline;
723 text_indent_len = 0;
724 while (VIM_ISWHITE(*p))
725 {
726 p++;
727 text_indent_len++;
728 }
729 text_indent = vim_strnsave(theline, text_indent_len);
730 }
731 // with "trim": skip the indent matching the first line
732 if (text_indent != NULL)
733 for (ti = 0; ti < text_indent_len; ++ti)
734 if (theline[ti] != text_indent[ti])
735 break;
736
737 if (list_append_string(l, theline + ti, -1) == FAIL)
738 break;
739 vim_free(theline);
740 }
741 vim_free(text_indent);
742
743 return l;
744}
745
746/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200747 * Vim9 variable declaration:
748 * ":var name"
749 * ":var name: type"
750 * ":var name = expr"
751 * ":var name: type = expr"
752 * etc.
753 */
754 void
755ex_var(exarg_T *eap)
756{
757 if (!in_vim9script())
758 {
759 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
760 return;
761 }
762 ex_let(eap);
763}
764
765/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200766 * ":let" list all variable values
767 * ":let var1 var2" list variable values
768 * ":let var = expr" assignment command.
769 * ":let var += expr" assignment command.
770 * ":let var -= expr" assignment command.
771 * ":let var *= expr" assignment command.
772 * ":let var /= expr" assignment command.
773 * ":let var %= expr" assignment command.
774 * ":let var .= expr" assignment command.
775 * ":let var ..= expr" assignment command.
776 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100778 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200779 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200780 * ":final var = expr" assignment command.
781 * ":final [var1, var2] = expr" unpack list.
782 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200783 * ":const" list all variable values
784 * ":const var1 var2" list variable values
785 * ":const var = expr" assignment command.
786 * ":const [var1, var2] = expr" unpack list.
787 */
788 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200789ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200790{
791 char_u *arg = eap->arg;
792 char_u *expr = NULL;
793 typval_T rettv;
794 int i;
795 int var_count = 0;
796 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200797 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200798 char_u *argend;
799 int first = TRUE;
800 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200801 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100802 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200803 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200804
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200805 if (eap->cmdidx == CMD_final && !vim9script)
806 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100807 // In legacy Vim script ":final" is short for ":finally".
808 ex_finally(eap);
809 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200810 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200811 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200812 {
813 emsg(_(e_cannot_use_let_in_vim9_script));
814 return;
815 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200816
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100817 if (eap->cmdidx == CMD_const)
818 flags |= ASSIGN_CONST;
819 else if (eap->cmdidx == CMD_final)
820 flags |= ASSIGN_FINAL;
821
822 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200824 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200826 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200827 if (argend == NULL)
828 return;
829 if (argend > arg && argend[-1] == '.') // for var.='str'
830 --argend;
831 expr = skipwhite(argend);
832 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +0200833 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200834 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200835 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
836 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200837 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200838 {
839 // ":let" without "=": list variables
840 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000841 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200842 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000843 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200844 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200845 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200846 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200847 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100848 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +0000849 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100850 else
851 // Vim9 declaration ":var name: type"
852 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200853 }
854 else
855 {
856 // ":let var1 var2" - list values
857 arg = list_arg_vars(eap, arg, &first);
858 }
859 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200860 else if (!eap->skip)
861 {
862 // ":let"
863 list_glob_vars(&first);
864 list_buf_vars(&first);
865 list_win_vars(&first);
866 list_tab_vars(&first);
867 list_script_vars(&first);
868 list_func_vars(&first);
869 list_vim_vars(&first);
870 }
Bram Moolenaar63b91732021-08-05 20:40:03 +0200871 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200872 }
873 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
874 {
875 list_T *l;
Bram Moolenaar81530e32021-07-28 21:25:49 +0200876 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200877
878 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200879 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200880 if (l != NULL)
881 {
882 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200883 if (!eap->skip)
884 {
Bram Moolenaar81530e32021-07-28 21:25:49 +0200885 // errors are for the assignment, not the end marker
886 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200887 op[0] = '=';
888 op[1] = NUL;
889 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200891 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200892 clear_tv(&rettv);
893 }
894 }
895 else
896 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200897 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200898 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200899
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200900 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200901 i = FAIL;
902 if (has_assign || concat)
903 {
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100904 int cur_lnum;
905
Bram Moolenaar32e35112020-05-14 22:41:15 +0200906 op[0] = '=';
907 op[1] = NUL;
908 if (*expr != '=')
909 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200910 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200911 {
912 // +=, /=, etc. require an existing variable
913 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
914 i = FAIL;
915 }
916 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200917 {
918 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200919 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200920 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200921 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200922 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200923 ++len;
924 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200925 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200926 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200927 }
928 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200929 ++expr;
930
Bram Moolenaar7f2c3412021-11-29 16:01:49 +0000931 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200932 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200933 {
934 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100935 semsg(_(e_white_space_required_before_and_after_str_at_str),
936 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200937 i = FAIL;
938 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200939
940 if (eap->skip)
941 ++emsg_skip;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200942 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200943 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100944 cur_lnum = SOURCING_LNUM;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200945 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200946 if (eap->skip)
947 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200948 clear_evalarg(&evalarg, eap);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100949
950 // Restore the line number so that any type error is given for the
951 // declaration, not the expression.
952 SOURCING_LNUM = cur_lnum;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200953 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200954 if (eap->skip)
955 {
956 if (i != FAIL)
957 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200958 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200959 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200960 {
961 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200962 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200963 clear_tv(&rettv);
964 }
965 }
966}
967
968/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +0100969 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200970 * Handles both "var" with any type and "[var, var; var]" with a list type.
971 * When "op" is not NULL it points to a string with characters that
972 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
973 * or concatenate.
974 * Returns OK or FAIL;
975 */
976 int
977ex_let_vars(
978 char_u *arg_start,
979 typval_T *tv,
980 int copy, // copy values from "tv", don't move
981 int semicolon, // from skip_var_list()
982 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100983 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200984 char_u *op)
985{
986 char_u *arg = arg_start;
987 list_T *l;
988 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100989 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200990 listitem_T *item;
991 typval_T ltv;
992
993 if (*arg != '[')
994 {
995 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100996 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200997 return FAIL;
998 return OK;
999 }
1000
1001 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1002 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
1003 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001004 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001005 return FAIL;
1006 }
1007
1008 i = list_len(l);
1009 if (semicolon == 0 && var_count < i)
1010 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001011 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001012 return FAIL;
1013 }
1014 if (var_count - semicolon > i)
1015 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001016 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001017 return FAIL;
1018 }
1019
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001020 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001021 item = l->lv_first;
1022 while (*arg != ']')
1023 {
1024 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001025 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001026 arg = ex_let_one(arg, &item->li_tv, TRUE,
1027 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001028 item = item->li_next;
1029 if (arg == NULL)
1030 return FAIL;
1031
1032 arg = skipwhite(arg);
1033 if (*arg == ';')
1034 {
1035 // Put the rest of the list (may be empty) in the var after ';'.
1036 // Create a new list for this.
1037 l = list_alloc();
1038 if (l == NULL)
1039 return FAIL;
1040 while (item != NULL)
1041 {
1042 list_append_tv(l, &item->li_tv);
1043 item = item->li_next;
1044 }
1045
1046 ltv.v_type = VAR_LIST;
1047 ltv.v_lock = 0;
1048 ltv.vval.v_list = l;
1049 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001050 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001051
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001052 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1053 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001054 clear_tv(&ltv);
1055 if (arg == NULL)
1056 return FAIL;
1057 break;
1058 }
1059 else if (*arg != ',' && *arg != ']')
1060 {
1061 internal_error("ex_let_vars()");
1062 return FAIL;
1063 }
1064 }
1065
1066 return OK;
1067}
1068
1069/*
1070 * Skip over assignable variable "var" or list of variables "[var, var]".
1071 * Used for ":let varvar = expr" and ":for varvar in expr".
1072 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001073 * for "[var, var; var]" set "semicolon" to 1.
1074 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001075 * Return NULL for an error.
1076 */
1077 char_u *
1078skip_var_list(
1079 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001080 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001081 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001082 int *semicolon,
1083 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001084{
1085 char_u *p, *s;
1086
1087 if (*arg == '[')
1088 {
1089 // "[var, var]": find the matching ']'.
1090 p = arg;
1091 for (;;)
1092 {
1093 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001094 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001095 if (s == p)
1096 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001097 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001098 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001099 return NULL;
1100 }
1101 ++*var_count;
1102
1103 p = skipwhite(s);
1104 if (*p == ']')
1105 break;
1106 else if (*p == ';')
1107 {
1108 if (*semicolon == 1)
1109 {
Bram Moolenaar8b716f52022-02-15 21:17:56 +00001110 if (!silent)
1111 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001112 return NULL;
1113 }
1114 *semicolon = 1;
1115 }
1116 else if (*p != ',')
1117 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001118 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001119 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001120 return NULL;
1121 }
1122 }
1123 return p + 1;
1124 }
1125 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001127}
1128
1129/*
1130 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1131 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001133 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001134 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001136{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001137 char_u *end;
1138 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001139
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001140 if (*arg == '@' && arg[1] != NUL)
1141 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001143 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001144
1145 // "a: type" is declaring variable "a" with a type, not "a:".
1146 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001147 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001148 --end;
1149
Bram Moolenaar585587d2021-01-17 20:52:13 +01001150 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001151 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001152 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001153 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001154 }
1155 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001156}
1157
1158/*
1159 * List variables for hashtab "ht" with prefix "prefix".
1160 * If "empty" is TRUE also list NULL strings as empty strings.
1161 */
1162 void
1163list_hashtable_vars(
1164 hashtab_T *ht,
1165 char *prefix,
1166 int empty,
1167 int *first)
1168{
1169 hashitem_T *hi;
1170 dictitem_T *di;
1171 int todo;
1172 char_u buf[IOSIZE];
1173
1174 todo = (int)ht->ht_used;
1175 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1176 {
1177 if (!HASHITEM_EMPTY(hi))
1178 {
1179 --todo;
1180 di = HI2DI(hi);
1181
1182 // apply :filter /pat/ to variable name
1183 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1184 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1185 if (message_filtered(buf))
1186 continue;
1187
1188 if (empty || di->di_tv.v_type != VAR_STRING
1189 || di->di_tv.vval.v_string != NULL)
1190 list_one_var(di, prefix, first);
1191 }
1192 }
1193}
1194
1195/*
1196 * List global variables.
1197 */
1198 static void
1199list_glob_vars(int *first)
1200{
1201 list_hashtable_vars(&globvarht, "", TRUE, first);
1202}
1203
1204/*
1205 * List buffer variables.
1206 */
1207 static void
1208list_buf_vars(int *first)
1209{
1210 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1211}
1212
1213/*
1214 * List window variables.
1215 */
1216 static void
1217list_win_vars(int *first)
1218{
1219 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1220}
1221
1222/*
1223 * List tab page variables.
1224 */
1225 static void
1226list_tab_vars(int *first)
1227{
1228 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1229}
1230
1231/*
1232 * List variables in "arg".
1233 */
1234 static char_u *
1235list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1236{
1237 int error = FALSE;
1238 int len;
1239 char_u *name;
1240 char_u *name_start;
1241 char_u *arg_subsc;
1242 char_u *tofree;
1243 typval_T tv;
1244
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001245 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001246 {
1247 if (error || eap->skip)
1248 {
1249 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1250 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1251 {
1252 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001253 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001254 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001255 break;
1256 }
1257 }
1258 else
1259 {
1260 // get_name_len() takes care of expanding curly braces
1261 name_start = name = arg;
1262 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1263 if (len <= 0)
1264 {
1265 // This is mainly to keep test 49 working: when expanding
1266 // curly braces fails overrule the exception error message.
1267 if (len < 0 && !aborting())
1268 {
1269 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001270 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001271 break;
1272 }
1273 error = TRUE;
1274 }
1275 else
1276 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001277 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001278 if (tofree != NULL)
1279 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001280 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001281 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001282 error = TRUE;
1283 else
1284 {
1285 // handle d.key, l[idx], f(expr)
1286 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001287 if (handle_subscript(&arg, name_start, &tv,
1288 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001289 error = TRUE;
1290 else
1291 {
1292 if (arg == arg_subsc && len == 2 && name[1] == ':')
1293 {
1294 switch (*name)
1295 {
1296 case 'g': list_glob_vars(first); break;
1297 case 'b': list_buf_vars(first); break;
1298 case 'w': list_win_vars(first); break;
1299 case 't': list_tab_vars(first); break;
1300 case 'v': list_vim_vars(first); break;
1301 case 's': list_script_vars(first); break;
1302 case 'l': list_func_vars(first); break;
1303 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001304 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001305 }
1306 }
1307 else
1308 {
1309 char_u numbuf[NUMBUFLEN];
1310 char_u *tf;
1311 int c;
1312 char_u *s;
1313
1314 s = echo_string(&tv, &tf, numbuf, 0);
1315 c = *arg;
1316 *arg = NUL;
1317 list_one_var_a("",
1318 arg == arg_subsc ? name : name_start,
1319 tv.v_type,
1320 s == NULL ? (char_u *)"" : s,
1321 first);
1322 *arg = c;
1323 vim_free(tf);
1324 }
1325 clear_tv(&tv);
1326 }
1327 }
1328 }
1329
1330 vim_free(tofree);
1331 }
1332
1333 arg = skipwhite(arg);
1334 }
1335
1336 return arg;
1337}
1338
1339/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001340 * Set an environment variable, part of ex_let_one().
1341 */
1342 static char_u *
1343ex_let_env(
1344 char_u *arg,
1345 typval_T *tv,
1346 int flags,
1347 char_u *endchars,
1348 char_u *op)
1349{
1350 char_u *arg_end = NULL;
1351 char_u *name;
1352 int len;
1353
1354 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1355 && (flags & ASSIGN_FOR_LOOP) == 0)
1356 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001357 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001358 return NULL;
1359 }
1360
1361 // Find the end of the name.
1362 ++arg;
1363 name = arg;
1364 len = get_env_len(&arg);
1365 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001366 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001367 else
1368 {
1369 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001370 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001371 else if (endchars != NULL
1372 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1373 emsg(_(e_unexpected_characters_in_let));
1374 else if (!check_secure())
1375 {
1376 char_u *tofree = NULL;
1377 int c1 = name[len];
1378 char_u *p;
1379
1380 name[len] = NUL;
1381 p = tv_get_string_chk(tv);
1382 if (p != NULL && op != NULL && *op == '.')
1383 {
1384 int mustfree = FALSE;
1385 char_u *s = vim_getenv(name, &mustfree);
1386
1387 if (s != NULL)
1388 {
1389 p = tofree = concat_str(s, p);
1390 if (mustfree)
1391 vim_free(s);
1392 }
1393 }
1394 if (p != NULL)
1395 {
1396 vim_setenv_ext(name, p);
1397 arg_end = arg;
1398 }
1399 name[len] = c1;
1400 vim_free(tofree);
1401 }
1402 }
1403 return arg_end;
1404}
1405
1406/*
1407 * Set an option, part of ex_let_one().
1408 */
1409 static char_u *
1410ex_let_option(
1411 char_u *arg,
1412 typval_T *tv,
1413 int flags,
1414 char_u *endchars,
1415 char_u *op)
1416{
1417 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001418 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001419 char_u *arg_end = NULL;
1420
1421 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1422 && (flags & ASSIGN_FOR_LOOP) == 0)
1423 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001424 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001425 return NULL;
1426 }
1427
1428 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001429 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001430 if (p == NULL || (endchars != NULL
1431 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1432 emsg(_(e_unexpected_characters_in_let));
1433 else
1434 {
1435 int c1;
1436 long n = 0;
1437 getoption_T opt_type;
1438 long numval;
1439 char_u *stringval = NULL;
1440 char_u *s = NULL;
1441 int failed = FALSE;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001442 int opt_p_flags;
1443 char_u *tofree = NULL;
Bram Moolenaar92c33eb2021-12-07 11:03:39 +00001444 char_u numbuf[NUMBUFLEN];
1445
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001446 c1 = *p;
1447 *p = NUL;
1448
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001449 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags,
1450 scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001451 if ((opt_type == gov_bool
1452 || opt_type == gov_number
1453 || opt_type == gov_hidden_bool
1454 || opt_type == gov_hidden_number)
1455 && (tv->v_type != VAR_STRING || !in_vim9script()))
1456 {
1457 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1458 // bool, possibly hidden
1459 n = (long)tv_get_bool(tv);
1460 else
1461 // number, possibly hidden
1462 n = (long)tv_get_number(tv);
1463 }
1464
Bram Moolenaaref082e12021-12-12 21:02:03 +00001465 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001466 || tv->v_type == VAR_FUNC))
1467 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001468 // If the option can be set to a function reference or a lambda
1469 // and the passed value is a function reference, then convert it to
1470 // the name (string) of the function reference.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001471 s = tv2string(tv, &tofree, numbuf, 0);
1472 }
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001473 // Avoid setting a string option to the text "v:false" or similar.
1474 // In Vim9 script also don't convert a number to string.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001475 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001476 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1477 s = tv_get_string_chk(tv);
1478
1479 if (op != NULL && *op != '=')
1480 {
1481 if (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1482 || (opt_type == gov_string && *op != '.'))
1483 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001484 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001485 failed = TRUE; // don't set the value
1486
1487 }
1488 else
1489 {
1490 // number, in legacy script also bool
1491 if (opt_type == gov_number
1492 || (opt_type == gov_bool && !in_vim9script()))
1493 {
1494 switch (*op)
1495 {
1496 case '+': n = numval + n; break;
1497 case '-': n = numval - n; break;
1498 case '*': n = numval * n; break;
1499 case '/': n = (long)num_divide(numval, n,
1500 &failed); break;
1501 case '%': n = (long)num_modulus(numval, n,
1502 &failed); break;
1503 }
1504 s = NULL;
1505 }
1506 else if (opt_type == gov_string
1507 && stringval != NULL && s != NULL)
1508 {
1509 // string
1510 s = concat_str(stringval, s);
1511 vim_free(stringval);
1512 stringval = s;
1513 }
1514 }
1515 }
1516
1517 if (!failed)
1518 {
1519 if (opt_type != gov_string || s != NULL)
1520 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001521 set_option_value(arg, n, s, scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001522 arg_end = p;
1523 }
1524 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001525 emsg(_(e_string_required));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001526 }
1527 *p = c1;
1528 vim_free(stringval);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001529 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001530 }
1531 return arg_end;
1532}
1533
1534/*
1535 * Set a register, part of ex_let_one().
1536 */
1537 static char_u *
1538ex_let_register(
1539 char_u *arg,
1540 typval_T *tv,
1541 int flags,
1542 char_u *endchars,
1543 char_u *op)
1544{
1545 char_u *arg_end = NULL;
1546
1547 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1548 && (flags & ASSIGN_FOR_LOOP) == 0)
1549 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001550 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001551 return NULL;
1552 }
1553 ++arg;
1554 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001555 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001556 else if (endchars != NULL
1557 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1558 emsg(_(e_unexpected_characters_in_let));
1559 else
1560 {
1561 char_u *ptofree = NULL;
1562 char_u *p;
1563
1564 p = tv_get_string_chk(tv);
1565 if (p != NULL && op != NULL && *op == '.')
1566 {
1567 char_u *s = get_reg_contents(*arg == '@'
1568 ? '"' : *arg, GREG_EXPR_SRC);
1569
1570 if (s != NULL)
1571 {
1572 p = ptofree = concat_str(s, p);
1573 vim_free(s);
1574 }
1575 }
1576 if (p != NULL)
1577 {
1578 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1579 arg_end = arg + 1;
1580 }
1581 vim_free(ptofree);
1582 }
1583 return arg_end;
1584}
1585
1586/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001587 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1588 * Returns a pointer to the char just after the var name.
1589 * Returns NULL if there is an error.
1590 */
1591 static char_u *
1592ex_let_one(
1593 char_u *arg, // points to variable name
1594 typval_T *tv, // value to assign to variable
1595 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001596 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001597 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001598 char_u *op, // "+", "-", "." or NULL
1599 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001600{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001601 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001602
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001603 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001604 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001605 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1606 {
1607 vim9_declare_error(arg);
1608 return NULL;
1609 }
1610
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001611 if (*arg == '$')
1612 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001613 // ":let $VAR = expr": Set environment variable.
1614 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001615 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001616 else if (*arg == '&')
1617 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001618 // ":let &option = expr": Set option value.
1619 // ":let &l:option = expr": Set local option value.
1620 // ":let &g:option = expr": Set global option value.
1621 // ":for &ts in range(8)": Set option value for for loop
1622 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001623 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001624 else if (*arg == '@')
1625 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001626 // ":let @r = expr": Set register contents.
1627 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001628 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001629 else if (eval_isnamec1(*arg) || *arg == '{')
1630 {
1631 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001632 char_u *p;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001633
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001634 // ":let var = expr": Set internal variable.
1635 // ":let var: type = expr": Set internal variable with type.
1636 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001637 p = get_lval(arg, tv, &lv, FALSE, FALSE,
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001638 (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1639 ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001640 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001641 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642 if (endchars != NULL && vim_strchr(endchars,
1643 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001644 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001645 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001646 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001647 else
1648 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001649 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001650 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001651 }
1652 }
1653 clear_lval(&lv);
1654 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001655 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001656 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001657
1658 return arg_end;
1659}
1660
1661/*
1662 * ":unlet[!] var1 ... " command.
1663 */
1664 void
1665ex_unlet(exarg_T *eap)
1666{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001667 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001668}
1669
1670/*
1671 * ":lockvar" and ":unlockvar" commands
1672 */
1673 void
1674ex_lockvar(exarg_T *eap)
1675{
1676 char_u *arg = eap->arg;
1677 int deep = 2;
1678
1679 if (eap->forceit)
1680 deep = -1;
1681 else if (vim_isdigit(*arg))
1682 {
1683 deep = getdigits(&arg);
1684 arg = skipwhite(arg);
1685 }
1686
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001687 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001688}
1689
1690/*
1691 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001692 * Also used for Vim9 script. "callback" is invoked as:
1693 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001694 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001695 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001696ex_unletlock(
1697 exarg_T *eap,
1698 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001699 int deep,
1700 int glv_flags,
1701 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1702 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001703{
1704 char_u *arg = argstart;
1705 char_u *name_end;
1706 int error = FALSE;
1707 lval_T lv;
1708
1709 do
1710 {
1711 if (*arg == '$')
1712 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001713 lv.ll_name = arg;
1714 lv.ll_tv = NULL;
1715 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001716 if (get_env_len(&arg) == 0)
1717 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001718 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001719 return;
1720 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001721 if (!error && !eap->skip
1722 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1723 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001724 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001725 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001726 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001727 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001728 // Parse the name and find the end.
1729 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001730 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001731 if (lv.ll_name == NULL)
1732 error = TRUE; // error but continue parsing
1733 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1734 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001735 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001736 if (name_end != NULL)
1737 {
1738 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00001739 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001740 }
1741 if (!(eap->skip || error))
1742 clear_lval(&lv);
1743 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001744 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001745
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001746 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001747 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001748 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001749
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001750 if (!eap->skip)
1751 clear_lval(&lv);
1752 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001753
1754 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001755 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001756
Bram Moolenaar63b91732021-08-05 20:40:03 +02001757 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001758}
1759
1760 static int
1761do_unlet_var(
1762 lval_T *lp,
1763 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001764 exarg_T *eap,
1765 int deep UNUSED,
1766 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001767{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001768 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001769 int ret = OK;
1770 int cc;
1771
1772 if (lp->ll_tv == NULL)
1773 {
1774 cc = *name_end;
1775 *name_end = NUL;
1776
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001777 // Environment variable, normal name or expanded name.
1778 if (*lp->ll_name == '$')
1779 vim_unsetenv(lp->ll_name + 1);
1780 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001781 ret = FAIL;
1782 *name_end = cc;
1783 }
1784 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001785 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001786 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001787 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001788 return FAIL;
1789 else if (lp->ll_range)
1790 {
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001791 if (list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_name, lp->ll_n1,
1792 !lp->ll_empty2, lp->ll_n2) == FAIL)
1793 return FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001794 }
1795 else
1796 {
1797 if (lp->ll_list != NULL)
1798 // unlet a List item.
1799 listitem_remove(lp->ll_list, lp->ll_li);
1800 else
1801 // unlet a Dictionary item.
1802 dictitem_remove(lp->ll_dict, lp->ll_di);
1803 }
1804
1805 return ret;
1806}
1807
1808/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001809 * Unlet one item or a range of items from a list.
1810 * Return OK or FAIL.
1811 */
1812 int
1813list_unlet_range(
1814 list_T *l,
1815 listitem_T *li_first,
1816 char_u *name,
1817 long n1_arg,
1818 int has_n2,
1819 long n2)
1820{
1821 listitem_T *li = li_first;
1822 int n1 = n1_arg;
1823
1824 while (li != NULL && (!has_n2 || n2 >= n1))
1825 {
1826 if (value_check_lock(li->li_tv.v_lock, name, FALSE))
1827 return FAIL;
1828 li = li->li_next;
1829 ++n1;
1830 }
1831
1832 // Delete a range of List items.
1833 li = li_first;
1834 n1 = n1_arg;
1835 while (li != NULL && (!has_n2 || n2 >= n1))
1836 {
1837 listitem_T *next = li->li_next;
1838
1839 listitem_remove(l, li);
1840 li = next;
1841 ++n1;
1842 }
1843 return OK;
1844}
1845/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001846 * "unlet" a variable. Return OK if it existed, FAIL if not.
1847 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1848 */
1849 int
1850do_unlet(char_u *name, int forceit)
1851{
1852 hashtab_T *ht;
1853 hashitem_T *hi;
1854 char_u *varname;
1855 dict_T *d;
1856 dictitem_T *di;
1857
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001858 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001859 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001860 return FAIL;
1861
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001862 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001863
1864 // can't :unlet a script variable in Vim9 script from a function
1865 if (ht == get_script_local_ht()
1866 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1867 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1868 == SCRIPT_VERSION_VIM9
1869 && check_vim9_unlet(name) == FAIL)
1870 return FAIL;
1871
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001872 if (ht != NULL && *varname != NUL)
1873 {
1874 d = get_current_funccal_dict(ht);
1875 if (d == NULL)
1876 {
1877 if (ht == &globvarht)
1878 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001879 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001880 d = &vimvardict;
1881 else
1882 {
1883 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1884 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1885 }
1886 if (d == NULL)
1887 {
1888 internal_error("do_unlet()");
1889 return FAIL;
1890 }
1891 }
1892 hi = hash_find(ht, varname);
1893 if (HASHITEM_EMPTY(hi))
1894 hi = find_hi_in_scoped_ht(name, &ht);
1895 if (hi != NULL && !HASHITEM_EMPTY(hi))
1896 {
1897 di = HI2DI(hi);
1898 if (var_check_fixed(di->di_flags, name, FALSE)
1899 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001900 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001901 return FAIL;
1902
1903 delete_var(ht, hi);
1904 return OK;
1905 }
1906 }
1907 if (forceit)
1908 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00001909 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001910 return FAIL;
1911}
1912
1913/*
1914 * Lock or unlock variable indicated by "lp".
1915 * "deep" is the levels to go (-1 for unlimited);
1916 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1917 */
1918 static int
1919do_lock_var(
1920 lval_T *lp,
1921 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001922 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001923 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001924 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001925{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001926 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001927 int ret = OK;
1928 int cc;
1929 dictitem_T *di;
1930
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001931 if (lp->ll_tv == NULL)
1932 {
1933 cc = *name_end;
1934 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001935 if (*lp->ll_name == '$')
1936 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001937 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001938 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001939 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001940 else
1941 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001942 // Normal name or expanded name.
1943 di = find_var(lp->ll_name, NULL, TRUE);
1944 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001945 {
1946 if (in_vim9script())
1947 semsg(_(e_cannot_find_variable_to_unlock_str),
1948 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001949 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001950 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001951 else if ((di->di_flags & DI_FLAGS_FIX)
1952 && di->di_tv.v_type != VAR_DICT
1953 && di->di_tv.v_type != VAR_LIST)
1954 {
1955 // For historic reasons this error is not given for a list or
1956 // dict. E.g., the b: dict could be locked/unlocked.
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001957 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001958 ret = FAIL;
1959 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001960 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001961 {
1962 if (lock)
1963 di->di_flags |= DI_FLAGS_LOCK;
1964 else
1965 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001966 if (deep != 0)
1967 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001968 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001969 }
1970 *name_end = cc;
1971 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001972 else if (deep == 0)
1973 {
1974 // nothing to do
1975 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001976 else if (lp->ll_range)
1977 {
1978 listitem_T *li = lp->ll_li;
1979
1980 // (un)lock a range of List items.
1981 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1982 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001983 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001984 li = li->li_next;
1985 ++lp->ll_n1;
1986 }
1987 }
1988 else if (lp->ll_list != NULL)
1989 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001990 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001991 else
1992 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001993 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001994
1995 return ret;
1996}
1997
1998/*
1999 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002000 * When "check_refcount" is TRUE do not lock a list or dict with a reference
2001 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002002 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002003 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02002004item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002005{
2006 static int recurse = 0;
2007 list_T *l;
2008 listitem_T *li;
2009 dict_T *d;
2010 blob_T *b;
2011 hashitem_T *hi;
2012 int todo;
2013
2014 if (recurse >= DICT_MAXNEST)
2015 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002016 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002017 return;
2018 }
2019 if (deep == 0)
2020 return;
2021 ++recurse;
2022
2023 // lock/unlock the item itself
2024 if (lock)
2025 tv->v_lock |= VAR_LOCKED;
2026 else
2027 tv->v_lock &= ~VAR_LOCKED;
2028
2029 switch (tv->v_type)
2030 {
2031 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002032 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002033 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002034 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002036 case VAR_STRING:
2037 case VAR_FUNC:
2038 case VAR_PARTIAL:
2039 case VAR_FLOAT:
2040 case VAR_SPECIAL:
2041 case VAR_JOB:
2042 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002043 case VAR_INSTR:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002044 break;
2045
2046 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002047 if ((b = tv->vval.v_blob) != NULL
2048 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002049 {
2050 if (lock)
2051 b->bv_lock |= VAR_LOCKED;
2052 else
2053 b->bv_lock &= ~VAR_LOCKED;
2054 }
2055 break;
2056 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002057 if ((l = tv->vval.v_list) != NULL
2058 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002059 {
2060 if (lock)
2061 l->lv_lock |= VAR_LOCKED;
2062 else
2063 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002064 if (deep < 0 || deep > 1)
2065 {
2066 if (l->lv_first == &range_list_item)
2067 l->lv_lock |= VAR_ITEMS_LOCKED;
2068 else
2069 {
2070 // recursive: lock/unlock the items the List contains
2071 CHECK_LIST_MATERIALIZE(l);
2072 FOR_ALL_LIST_ITEMS(l, li) item_lock(&li->li_tv,
2073 deep - 1, lock, check_refcount);
2074 }
2075 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002076 }
2077 break;
2078 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002079 if ((d = tv->vval.v_dict) != NULL
2080 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002081 {
2082 if (lock)
2083 d->dv_lock |= VAR_LOCKED;
2084 else
2085 d->dv_lock &= ~VAR_LOCKED;
2086 if (deep < 0 || deep > 1)
2087 {
2088 // recursive: lock/unlock the items the List contains
2089 todo = (int)d->dv_hashtab.ht_used;
2090 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2091 {
2092 if (!HASHITEM_EMPTY(hi))
2093 {
2094 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002095 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2096 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002097 }
2098 }
2099 }
2100 }
2101 }
2102 --recurse;
2103}
2104
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002105#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2106/*
2107 * Delete all "menutrans_" variables.
2108 */
2109 void
2110del_menutrans_vars(void)
2111{
2112 hashitem_T *hi;
2113 int todo;
2114
2115 hash_lock(&globvarht);
2116 todo = (int)globvarht.ht_used;
2117 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2118 {
2119 if (!HASHITEM_EMPTY(hi))
2120 {
2121 --todo;
2122 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2123 delete_var(&globvarht, hi);
2124 }
2125 }
2126 hash_unlock(&globvarht);
2127}
2128#endif
2129
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002130/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002131 * Local string buffer for the next two functions to store a variable name
2132 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2133 * get_user_var_name().
2134 */
2135
2136static char_u *varnamebuf = NULL;
2137static int varnamebuflen = 0;
2138
2139/*
2140 * Function to concatenate a prefix and a variable name.
2141 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002142 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002143cat_prefix_varname(int prefix, char_u *name)
2144{
2145 int len;
2146
2147 len = (int)STRLEN(name) + 3;
2148 if (len > varnamebuflen)
2149 {
2150 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002151 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002152 varnamebuf = alloc(len);
2153 if (varnamebuf == NULL)
2154 {
2155 varnamebuflen = 0;
2156 return NULL;
2157 }
2158 varnamebuflen = len;
2159 }
2160 *varnamebuf = prefix;
2161 varnamebuf[1] = ':';
2162 STRCPY(varnamebuf + 2, name);
2163 return varnamebuf;
2164}
2165
2166/*
2167 * Function given to ExpandGeneric() to obtain the list of user defined
2168 * (global/buffer/window/built-in) variable names.
2169 */
2170 char_u *
2171get_user_var_name(expand_T *xp, int idx)
2172{
2173 static long_u gdone;
2174 static long_u bdone;
2175 static long_u wdone;
2176 static long_u tdone;
2177 static int vidx;
2178 static hashitem_T *hi;
2179 hashtab_T *ht;
2180
2181 if (idx == 0)
2182 {
2183 gdone = bdone = wdone = vidx = 0;
2184 tdone = 0;
2185 }
2186
2187 // Global variables
2188 if (gdone < globvarht.ht_used)
2189 {
2190 if (gdone++ == 0)
2191 hi = globvarht.ht_array;
2192 else
2193 ++hi;
2194 while (HASHITEM_EMPTY(hi))
2195 ++hi;
2196 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2197 return cat_prefix_varname('g', hi->hi_key);
2198 return hi->hi_key;
2199 }
2200
2201 // b: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002202 ht = &prevwin_curwin()->w_buffer->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002203 if (bdone < ht->ht_used)
2204 {
2205 if (bdone++ == 0)
2206 hi = ht->ht_array;
2207 else
2208 ++hi;
2209 while (HASHITEM_EMPTY(hi))
2210 ++hi;
2211 return cat_prefix_varname('b', hi->hi_key);
2212 }
2213
2214 // w: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002215 ht = &prevwin_curwin()->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002216 if (wdone < ht->ht_used)
2217 {
2218 if (wdone++ == 0)
2219 hi = ht->ht_array;
2220 else
2221 ++hi;
2222 while (HASHITEM_EMPTY(hi))
2223 ++hi;
2224 return cat_prefix_varname('w', hi->hi_key);
2225 }
2226
2227 // t: variables
2228 ht = &curtab->tp_vars->dv_hashtab;
2229 if (tdone < ht->ht_used)
2230 {
2231 if (tdone++ == 0)
2232 hi = ht->ht_array;
2233 else
2234 ++hi;
2235 while (HASHITEM_EMPTY(hi))
2236 ++hi;
2237 return cat_prefix_varname('t', hi->hi_key);
2238 }
2239
2240 // v: variables
2241 if (vidx < VV_LEN)
2242 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2243
2244 VIM_CLEAR(varnamebuf);
2245 varnamebuflen = 0;
2246 return NULL;
2247}
2248
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002249 char *
2250get_var_special_name(int nr)
2251{
2252 switch (nr)
2253 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002254 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2255 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002256 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002257 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002258 }
2259 internal_error("get_var_special_name()");
2260 return "42";
2261}
2262
2263/*
2264 * Returns the global variable dictionary
2265 */
2266 dict_T *
2267get_globvar_dict(void)
2268{
2269 return &globvardict;
2270}
2271
2272/*
2273 * Returns the global variable hash table
2274 */
2275 hashtab_T *
2276get_globvar_ht(void)
2277{
2278 return &globvarht;
2279}
2280
2281/*
2282 * Returns the v: variable dictionary
2283 */
2284 dict_T *
2285get_vimvar_dict(void)
2286{
2287 return &vimvardict;
2288}
2289
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002290/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002291 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002292 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002293 */
2294 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002295find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002297 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2298 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299
2300 if (di == NULL)
2301 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002302 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2304 return (int)(vv - vimvars);
2305}
2306
2307
2308/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002309 * Set type of v: variable to "type".
2310 */
2311 void
2312set_vim_var_type(int idx, vartype_T type)
2313{
Bram Moolenaard787e402021-12-24 21:36:12 +00002314 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002315}
2316
2317/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002318 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002319 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002320 */
2321 void
2322set_vim_var_nr(int idx, varnumber_T val)
2323{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002324 vimvars[idx].vv_nr = val;
2325}
2326
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002327 char *
2328get_vim_var_name(int idx)
2329{
2330 return vimvars[idx].vv_name;
2331}
2332
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002333/*
2334 * Get typval_T v: variable value.
2335 */
2336 typval_T *
2337get_vim_var_tv(int idx)
2338{
2339 return &vimvars[idx].vv_tv;
2340}
2341
Bram Moolenaard787e402021-12-24 21:36:12 +00002342 type_T *
2343get_vim_var_type(int idx, garray_T *type_list)
2344{
2345 if (vimvars[idx].vv_type != NULL)
2346 return vimvars[idx].vv_type;
2347 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2348}
2349
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002350/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002351 * Set v: variable to "tv". Only accepts the same type.
2352 * Takes over the value of "tv".
2353 */
2354 int
2355set_vim_var_tv(int idx, typval_T *tv)
2356{
Bram Moolenaard787e402021-12-24 21:36:12 +00002357 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002358 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002359 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002360 clear_tv(tv);
2361 return FAIL;
2362 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002363 // VV_RO is also checked when compiling, but let's check here as well.
2364 if (vimvars[idx].vv_flags & VV_RO)
2365 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002366 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002367 return FAIL;
2368 }
2369 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2370 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002371 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002372 return FAIL;
2373 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002374 clear_tv(&vimvars[idx].vv_di.di_tv);
2375 vimvars[idx].vv_di.di_tv = *tv;
2376 return OK;
2377}
2378
2379/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002380 * Get number v: variable value.
2381 */
2382 varnumber_T
2383get_vim_var_nr(int idx)
2384{
2385 return vimvars[idx].vv_nr;
2386}
2387
2388/*
2389 * Get string v: variable value. Uses a static buffer, can only be used once.
2390 * If the String variable has never been set, return an empty string.
2391 * Never returns NULL;
2392 */
2393 char_u *
2394get_vim_var_str(int idx)
2395{
2396 return tv_get_string(&vimvars[idx].vv_tv);
2397}
2398
2399/*
2400 * Get List v: variable value. Caller must take care of reference count when
2401 * needed.
2402 */
2403 list_T *
2404get_vim_var_list(int idx)
2405{
2406 return vimvars[idx].vv_list;
2407}
2408
2409/*
2410 * Get Dict v: variable value. Caller must take care of reference count when
2411 * needed.
2412 */
2413 dict_T *
2414get_vim_var_dict(int idx)
2415{
2416 return vimvars[idx].vv_dict;
2417}
2418
2419/*
2420 * Set v:char to character "c".
2421 */
2422 void
2423set_vim_var_char(int c)
2424{
2425 char_u buf[MB_MAXBYTES + 1];
2426
2427 if (has_mbyte)
2428 buf[(*mb_char2bytes)(c, buf)] = NUL;
2429 else
2430 {
2431 buf[0] = c;
2432 buf[1] = NUL;
2433 }
2434 set_vim_var_string(VV_CHAR, buf, -1);
2435}
2436
2437/*
2438 * Set v:count to "count" and v:count1 to "count1".
2439 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2440 */
2441 void
2442set_vcount(
2443 long count,
2444 long count1,
2445 int set_prevcount)
2446{
2447 if (set_prevcount)
2448 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2449 vimvars[VV_COUNT].vv_nr = count;
2450 vimvars[VV_COUNT1].vv_nr = count1;
2451}
2452
2453/*
2454 * Save variables that might be changed as a side effect. Used when executing
2455 * a timer callback.
2456 */
2457 void
2458save_vimvars(vimvars_save_T *vvsave)
2459{
2460 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2461 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2462 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2463}
2464
2465/*
2466 * Restore variables saved by save_vimvars().
2467 */
2468 void
2469restore_vimvars(vimvars_save_T *vvsave)
2470{
2471 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2472 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2473 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2474}
2475
2476/*
2477 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2478 * value.
2479 */
2480 void
2481set_vim_var_string(
2482 int idx,
2483 char_u *val,
2484 int len) // length of "val" to use or -1 (whole string)
2485{
2486 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002487 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002488 if (val == NULL)
2489 vimvars[idx].vv_str = NULL;
2490 else if (len == -1)
2491 vimvars[idx].vv_str = vim_strsave(val);
2492 else
2493 vimvars[idx].vv_str = vim_strnsave(val, len);
2494}
2495
2496/*
2497 * Set List v: variable to "val".
2498 */
2499 void
2500set_vim_var_list(int idx, list_T *val)
2501{
2502 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002503 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002504 vimvars[idx].vv_list = val;
2505 if (val != NULL)
2506 ++val->lv_refcount;
2507}
2508
2509/*
2510 * Set Dictionary v: variable to "val".
2511 */
2512 void
2513set_vim_var_dict(int idx, dict_T *val)
2514{
2515 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002516 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002517 vimvars[idx].vv_dict = val;
2518 if (val != NULL)
2519 {
2520 ++val->dv_refcount;
2521 dict_set_items_ro(val);
2522 }
2523}
2524
2525/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002526 * Set the v:argv list.
2527 */
2528 void
2529set_argv_var(char **argv, int argc)
2530{
2531 list_T *l = list_alloc();
2532 int i;
2533
2534 if (l == NULL)
2535 getout(1);
2536 l->lv_lock = VAR_FIXED;
2537 for (i = 0; i < argc; ++i)
2538 {
2539 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2540 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002541 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002542 }
2543 set_vim_var_list(VV_ARGV, l);
2544}
2545
2546/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002547 * Reset v:register, taking the 'clipboard' setting into account.
2548 */
2549 void
2550reset_reg_var(void)
2551{
2552 int regname = 0;
2553
2554 // Adjust the register according to 'clipboard', so that when
2555 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2556#ifdef FEAT_CLIPBOARD
2557 adjust_clip_reg(&regname);
2558#endif
2559 set_reg_var(regname);
2560}
2561
2562/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002563 * Set v:register if needed.
2564 */
2565 void
2566set_reg_var(int c)
2567{
2568 char_u regname;
2569
2570 if (c == 0 || c == ' ')
2571 regname = '"';
2572 else
2573 regname = c;
2574 // Avoid free/alloc when the value is already right.
2575 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2576 set_vim_var_string(VV_REG, &regname, 1);
2577}
2578
2579/*
2580 * Get or set v:exception. If "oldval" == NULL, return the current value.
2581 * Otherwise, restore the value to "oldval" and return NULL.
2582 * Must always be called in pairs to save and restore v:exception! Does not
2583 * take care of memory allocations.
2584 */
2585 char_u *
2586v_exception(char_u *oldval)
2587{
2588 if (oldval == NULL)
2589 return vimvars[VV_EXCEPTION].vv_str;
2590
2591 vimvars[VV_EXCEPTION].vv_str = oldval;
2592 return NULL;
2593}
2594
2595/*
2596 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2597 * Otherwise, restore the value to "oldval" and return NULL.
2598 * Must always be called in pairs to save and restore v:throwpoint! Does not
2599 * take care of memory allocations.
2600 */
2601 char_u *
2602v_throwpoint(char_u *oldval)
2603{
2604 if (oldval == NULL)
2605 return vimvars[VV_THROWPOINT].vv_str;
2606
2607 vimvars[VV_THROWPOINT].vv_str = oldval;
2608 return NULL;
2609}
2610
2611/*
2612 * Set v:cmdarg.
2613 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2614 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2615 * Must always be called in pairs!
2616 */
2617 char_u *
2618set_cmdarg(exarg_T *eap, char_u *oldarg)
2619{
2620 char_u *oldval;
2621 char_u *newval;
2622 unsigned len;
2623
2624 oldval = vimvars[VV_CMDARG].vv_str;
2625 if (eap == NULL)
2626 {
2627 vim_free(oldval);
2628 vimvars[VV_CMDARG].vv_str = oldarg;
2629 return NULL;
2630 }
2631
2632 if (eap->force_bin == FORCE_BIN)
2633 len = 6;
2634 else if (eap->force_bin == FORCE_NOBIN)
2635 len = 8;
2636 else
2637 len = 0;
2638
2639 if (eap->read_edit)
2640 len += 7;
2641
2642 if (eap->force_ff != 0)
2643 len += 10; // " ++ff=unix"
2644 if (eap->force_enc != 0)
2645 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2646 if (eap->bad_char != 0)
2647 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2648
2649 newval = alloc(len + 1);
2650 if (newval == NULL)
2651 return NULL;
2652
2653 if (eap->force_bin == FORCE_BIN)
2654 sprintf((char *)newval, " ++bin");
2655 else if (eap->force_bin == FORCE_NOBIN)
2656 sprintf((char *)newval, " ++nobin");
2657 else
2658 *newval = NUL;
2659
2660 if (eap->read_edit)
2661 STRCAT(newval, " ++edit");
2662
2663 if (eap->force_ff != 0)
2664 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2665 eap->force_ff == 'u' ? "unix"
2666 : eap->force_ff == 'd' ? "dos"
2667 : "mac");
2668 if (eap->force_enc != 0)
2669 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2670 eap->cmd + eap->force_enc);
2671 if (eap->bad_char == BAD_KEEP)
2672 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2673 else if (eap->bad_char == BAD_DROP)
2674 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2675 else if (eap->bad_char != 0)
2676 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2677 vimvars[VV_CMDARG].vv_str = newval;
2678 return oldval;
2679}
2680
2681/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002682 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002683 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2684 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002685 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2686 */
2687 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002688eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002689 char_u *name,
2690 int len, // length of "name"
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002691 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002692 typval_T *rettv, // NULL when only checking existence
2693 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002694 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002695{
2696 int ret = OK;
2697 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002698 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002699 hashtab_T *ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002700 int cc;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002701 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002702
2703 // truncate the name, so that we can use strcmp()
2704 cc = name[len];
2705 name[len] = NUL;
2706
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002707 // Check for local variable when debugging.
2708 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002709 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002710 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002711 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2712
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002713 if (v != NULL)
2714 {
2715 tv = &v->di_tv;
2716 if (dip != NULL)
2717 *dip = v;
2718 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002719 else
2720 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002721 }
2722
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002723 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002725 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002726 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2727
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002728 if (sid == 0)
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00002729 import = find_imported(p, 0, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730
2731 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002732 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002734 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002735 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00002736 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02002737 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002738 ht = &SCRIPT_VARS(sid);
2739 if (ht != NULL)
2740 {
2741 dictitem_T *v = find_var_in_ht(ht, 0, name,
2742 flags & EVAL_VAR_NOAUTOLOAD);
2743
2744 if (v != NULL)
2745 {
2746 tv = &v->di_tv;
2747 if (dip != NULL)
2748 *dip = v;
2749 }
2750 else
2751 ht = NULL;
2752 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002753 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002754 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002755 {
2756 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00002757 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002758 ret = FAIL;
2759 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002760 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002761 else
2762 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002763 if (rettv != NULL)
2764 {
2765 rettv->v_type = VAR_ANY;
2766 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
2767 }
2768 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002769 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00002771 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002772 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002773 int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002774 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002775
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002776 // In Vim9 script we can get a function reference by using the
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002777 // function name. For a global non-autoload function "g:" is
2778 // required.
2779 if (ufunc != NULL && (has_g_prefix
2780 || !func_requires_g_prefix(ufunc)))
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002781 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002782 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002783 if (rettv != NULL)
2784 {
2785 rettv->v_type = VAR_FUNC;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002786 if (has_g_prefix)
Bram Moolenaaref082e12021-12-12 21:02:03 +00002787 // Keep the "g:", otherwise script-local may be
2788 // assumed.
2789 rettv->vval.v_string = vim_strsave(name);
2790 else
2791 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002792 if (rettv->vval.v_string != NULL)
2793 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002794 }
2795 }
2796 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797 }
2798
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002799 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002800 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002801 if (tv == NULL)
2802 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002803 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002804 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002805 ret = FAIL;
2806 }
2807 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002808 {
Bram Moolenaar11005b02021-07-11 20:59:00 +02002809 if (ht != NULL && ht == get_script_local_ht()
2810 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002811 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002812 svar_T *sv = find_typval_in_script(tv, 0);
Bram Moolenaarf055d452021-07-08 20:57:24 +02002813
Bram Moolenaarf055d452021-07-08 20:57:24 +02002814 if (sv != NULL)
2815 type = sv->sv_type;
2816 }
2817
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002818 // If a list or dict variable wasn't initialized, do it now.
Bram Moolenaar7a222242022-03-01 19:23:24 +00002819 // Not for global variables, they are not declared.
2820 if (ht != &globvarht)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002821 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00002822 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002823 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00002824 tv->vval.v_dict = dict_alloc();
2825 if (tv->vval.v_dict != NULL)
2826 {
2827 ++tv->vval.v_dict->dv_refcount;
2828 tv->vval.v_dict->dv_type = alloc_type(type);
2829 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02002830 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00002831 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002832 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00002833 tv->vval.v_list = list_alloc();
2834 if (tv->vval.v_list != NULL)
2835 {
2836 ++tv->vval.v_list->lv_refcount;
2837 tv->vval.v_list->lv_type = alloc_type(type);
2838 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02002839 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00002840 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL)
2841 {
2842 tv->vval.v_blob = blob_alloc();
2843 if (tv->vval.v_blob != NULL)
2844 ++tv->vval.v_blob->bv_refcount;
2845 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002846 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002847 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002848 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002849 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002850
2851 name[len] = cc;
2852
2853 return ret;
2854}
2855
2856/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002857 * Check if variable "name[len]" is a local variable or an argument.
2858 * If so, "*eval_lavars_used" is set to TRUE.
2859 */
2860 void
2861check_vars(char_u *name, int len)
2862{
2863 int cc;
2864 char_u *varname;
2865 hashtab_T *ht;
2866
2867 if (eval_lavars_used == NULL)
2868 return;
2869
2870 // truncate the name, so that we can use strcmp()
2871 cc = name[len];
2872 name[len] = NUL;
2873
2874 ht = find_var_ht(name, &varname);
2875 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2876 {
2877 if (find_var(name, NULL, TRUE) != NULL)
2878 *eval_lavars_used = TRUE;
2879 }
2880
2881 name[len] = cc;
2882}
2883
2884/*
2885 * Find variable "name" in the list of variables.
2886 * Return a pointer to it if found, NULL if not found.
2887 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002888 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002889 */
2890 dictitem_T *
2891find_var(char_u *name, hashtab_T **htp, int no_autoload)
2892{
2893 char_u *varname;
2894 hashtab_T *ht;
2895 dictitem_T *ret = NULL;
2896
2897 ht = find_var_ht(name, &varname);
2898 if (htp != NULL)
2899 *htp = ht;
2900 if (ht == NULL)
2901 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002902 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002903 if (ret != NULL)
2904 return ret;
2905
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002906 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002907 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002908 if (ret != NULL)
2909 return ret;
2910
2911 // in Vim9 script items without a scope can be script-local
2912 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2913 {
2914 ht = get_script_local_ht();
2915 if (ht != NULL)
2916 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002917 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002918 if (ret != NULL)
2919 {
2920 if (htp != NULL)
2921 *htp = ht;
2922 return ret;
2923 }
2924 }
2925 }
2926
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002927 // When using "vim9script autoload" script-local items are prefixed but can
2928 // be used with s:name.
2929 if (SCRIPT_ID_VALID(current_sctx.sc_sid)
2930 && name[0] == 's' && name[1] == ':')
2931 {
2932 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2933
2934 if (si->sn_autoload_prefix != NULL)
2935 {
2936 char_u *auto_name = concat_str(si->sn_autoload_prefix, name + 2);
2937
2938 if (auto_name != NULL)
2939 {
2940 ht = &globvarht;
2941 ret = find_var_in_ht(ht, *name, auto_name, TRUE);
Bram Moolenaar130f65d2022-01-13 20:39:41 +00002942 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002943 if (ret != NULL)
2944 {
2945 if (htp != NULL)
2946 *htp = ht;
2947 return ret;
2948 }
2949 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002950 }
2951 }
2952
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002953 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002954}
2955
2956/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00002957 * Like find_var() but if the name starts with <SNR>99_ then look in the
2958 * referenced script (used for a funcref).
2959 */
2960 dictitem_T *
2961find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
2962{
2963 if (STRNCMP(name, "<SNR>", 5) == 0 && isdigit(name[5]))
2964 {
2965 char_u *p = name + 5;
2966 int sid = getdigits(&p);
2967
2968 if (SCRIPT_ID_VALID(sid) && *p == '_')
2969 {
2970 hashtab_T *ht = &SCRIPT_VARS(sid);
2971
2972 if (ht != NULL)
2973 {
2974 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
2975
2976 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00002977 {
2978 if (htp != NULL)
2979 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00002980 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00002981 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00002982 }
2983 }
2984 }
2985
2986 return find_var(name, htp, no_autoload);
2987}
2988
2989/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002990 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002991 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002992 * Returns NULL if not found.
2993 */
2994 dictitem_T *
2995find_var_in_ht(
2996 hashtab_T *ht,
2997 int htname,
2998 char_u *varname,
2999 int no_autoload)
3000{
3001 hashitem_T *hi;
3002
3003 if (*varname == NUL)
3004 {
3005 // Must be something like "s:", otherwise "ht" would be NULL.
3006 switch (htname)
3007 {
3008 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
3009 case 'g': return &globvars_var;
3010 case 'v': return &vimvars_var;
3011 case 'b': return &curbuf->b_bufvar;
3012 case 'w': return &curwin->w_winvar;
3013 case 't': return &curtab->tp_winvar;
3014 case 'l': return get_funccal_local_var();
3015 case 'a': return get_funccal_args_var();
3016 }
3017 return NULL;
3018 }
3019
3020 hi = hash_find(ht, varname);
3021 if (HASHITEM_EMPTY(hi))
3022 {
3023 // For global variables we may try auto-loading the script. If it
3024 // worked find the variable again. Don't auto-load a script if it was
3025 // loaded already, otherwise it would be loaded every time when
3026 // checking if a function name is a Funcref variable.
3027 if (ht == &globvarht && !no_autoload)
3028 {
3029 // Note: script_autoload() may make "hi" invalid. It must either
3030 // be obtained again or not used.
3031 if (!script_autoload(varname, FALSE) || aborting())
3032 return NULL;
3033 hi = hash_find(ht, varname);
3034 }
3035 if (HASHITEM_EMPTY(hi))
3036 return NULL;
3037 }
3038 return HI2DI(hi);
3039}
3040
3041/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 * Get the script-local hashtab. NULL if not in a script context.
3043 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003044 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045get_script_local_ht(void)
3046{
3047 scid_T sid = current_sctx.sc_sid;
3048
Bram Moolenaare3d46852020-08-29 13:39:17 +02003049 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 return &SCRIPT_VARS(sid);
3051 return NULL;
3052}
3053
3054/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003055 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003056 * When "cmd" is TRUE it must look like a command, a function must be followed
3057 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003058 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003060 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003061lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003062 char_u *name,
3063 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003064 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003065 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066{
3067 hashtab_T *ht = get_script_local_ht();
3068 char_u buffer[30];
3069 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003070 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003072 int is_global = FALSE;
3073 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074
3075 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003076 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003077 if (len < sizeof(buffer) - 1)
3078 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003079 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 vim_strncpy(buffer, name, len);
3081 p = buffer;
3082 }
3083 else
3084 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003085 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003087 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 }
3089
3090 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003091 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092
3093 // if not script-local, then perhaps imported
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003094 if (res == FAIL && find_imported(p, 0, FALSE) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003095 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 if (p != buffer)
3097 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003098
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003099 // Find a function, so that a following "->" works.
3100 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3101 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003102 if (res != OK)
3103 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003104 p = skipwhite(name + len);
3105
3106 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003107 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003108 // Do not check for an internal function, since it might also be a
3109 // valid command, such as ":split" versus "split()".
3110 // Skip "g:" before a function name.
3111 if (name[0] == 'g' && name[1] == ':')
3112 {
3113 is_global = TRUE;
3114 fname = name + 2;
3115 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003116 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003117 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003118 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003119 }
3120
Bram Moolenaar709664c2020-12-12 14:33:41 +01003121 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122}
3123
3124/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003125 * Find the hashtab used for a variable name.
3126 * Return NULL if the name is not valid.
3127 * Set "varname" to the start of name without ':'.
3128 */
3129 hashtab_T *
3130find_var_ht(char_u *name, char_u **varname)
3131{
3132 hashitem_T *hi;
3133 hashtab_T *ht;
3134
3135 if (name[0] == NUL)
3136 return NULL;
3137 if (name[1] != ':')
3138 {
3139 // The name must not start with a colon or #.
3140 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3141 return NULL;
3142 *varname = name;
3143
3144 // "version" is "v:version" in all scopes if scriptversion < 3.
3145 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003146 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003147 {
3148 hi = hash_find(&compat_hashtab, name);
3149 if (!HASHITEM_EMPTY(hi))
3150 return &compat_hashtab;
3151 }
3152
3153 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 if (ht != NULL)
3155 return ht; // local variable
3156
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003157 // In Vim9 script items at the script level are script-local, except
3158 // for autoload names.
3159 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 {
3161 ht = get_script_local_ht();
3162 if (ht != NULL)
3163 return ht;
3164 }
3165
3166 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003167 }
3168 *varname = name + 2;
3169 if (*name == 'g') // global variable
3170 return &globvarht;
3171 // There must be no ':' or '#' in the rest of the name, unless g: is used
3172 if (vim_strchr(name + 2, ':') != NULL
3173 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3174 return NULL;
3175 if (*name == 'b') // buffer variable
3176 return &curbuf->b_vars->dv_hashtab;
3177 if (*name == 'w') // window variable
3178 return &curwin->w_vars->dv_hashtab;
3179 if (*name == 't') // tab page variable
3180 return &curtab->tp_vars->dv_hashtab;
3181 if (*name == 'v') // v: variable
3182 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003183 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003184 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003186 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 if (*name == 'a') // a: function argument
3188 return get_funccal_args_ht();
3189 if (*name == 'l') // l: local function variable
3190 return get_funccal_local_ht();
3191 }
3192 if (*name == 's') // script variable
3193 {
3194 ht = get_script_local_ht();
3195 if (ht != NULL)
3196 return ht;
3197 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003198 return NULL;
3199}
3200
3201/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003202 * Get the string value of a (global/local) variable.
3203 * Note: see tv_get_string() for how long the pointer remains valid.
3204 * Returns NULL when it doesn't exist.
3205 */
3206 char_u *
3207get_var_value(char_u *name)
3208{
3209 dictitem_T *v;
3210
3211 v = find_var(name, NULL, FALSE);
3212 if (v == NULL)
3213 return NULL;
3214 return tv_get_string(&v->di_tv);
3215}
3216
3217/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003218 * Allocate a new hashtab for a sourced script. It will be used while
3219 * sourcing this script and when executing functions defined in the script.
3220 */
3221 void
3222new_script_vars(scid_T id)
3223{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003224 scriptvar_T *sv;
3225
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003226 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3227 if (sv == NULL)
3228 return;
3229 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003230 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003231}
3232
3233/*
3234 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3235 * point to it.
3236 */
3237 void
3238init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3239{
3240 hash_init(&dict->dv_hashtab);
3241 dict->dv_lock = 0;
3242 dict->dv_scope = scope;
3243 dict->dv_refcount = DO_NOT_FREE_CNT;
3244 dict->dv_copyID = 0;
3245 dict_var->di_tv.vval.v_dict = dict;
3246 dict_var->di_tv.v_type = VAR_DICT;
3247 dict_var->di_tv.v_lock = VAR_FIXED;
3248 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3249 dict_var->di_key[0] = NUL;
3250}
3251
3252/*
3253 * Unreference a dictionary initialized by init_var_dict().
3254 */
3255 void
3256unref_var_dict(dict_T *dict)
3257{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003258 // Now the dict needs to be freed if no one else is using it, go back to
3259 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003260 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3261 dict_unref(dict);
3262}
3263
3264/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003265 * Clean up a list of internal variables.
3266 * Frees all allocated variables and the value they contain.
3267 * Clears hashtab "ht", does not free it.
3268 */
3269 void
3270vars_clear(hashtab_T *ht)
3271{
3272 vars_clear_ext(ht, TRUE);
3273}
3274
3275/*
3276 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3277 */
3278 void
3279vars_clear_ext(hashtab_T *ht, int free_val)
3280{
3281 int todo;
3282 hashitem_T *hi;
3283 dictitem_T *v;
3284
3285 hash_lock(ht);
3286 todo = (int)ht->ht_used;
3287 for (hi = ht->ht_array; todo > 0; ++hi)
3288 {
3289 if (!HASHITEM_EMPTY(hi))
3290 {
3291 --todo;
3292
3293 // Free the variable. Don't remove it from the hashtab,
3294 // ht_array might change then. hash_clear() takes care of it
3295 // later.
3296 v = HI2DI(hi);
3297 if (free_val)
3298 clear_tv(&v->di_tv);
3299 if (v->di_flags & DI_FLAGS_ALLOC)
3300 vim_free(v);
3301 }
3302 }
3303 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003304 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003305}
3306
3307/*
3308 * Delete a variable from hashtab "ht" at item "hi".
3309 * Clear the variable value and free the dictitem.
3310 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003311 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003312delete_var(hashtab_T *ht, hashitem_T *hi)
3313{
3314 dictitem_T *di = HI2DI(hi);
3315
3316 hash_remove(ht, hi);
3317 clear_tv(&di->di_tv);
3318 vim_free(di);
3319}
3320
3321/*
3322 * List the value of one internal variable.
3323 */
3324 static void
3325list_one_var(dictitem_T *v, char *prefix, int *first)
3326{
3327 char_u *tofree;
3328 char_u *s;
3329 char_u numbuf[NUMBUFLEN];
3330
3331 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3332 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3333 s == NULL ? (char_u *)"" : s, first);
3334 vim_free(tofree);
3335}
3336
3337 static void
3338list_one_var_a(
3339 char *prefix,
3340 char_u *name,
3341 int type,
3342 char_u *string,
3343 int *first) // when TRUE clear rest of screen and set to FALSE
3344{
3345 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3346 msg_start();
3347 msg_puts(prefix);
3348 if (name != NULL) // "a:" vars don't have a name stored
3349 msg_puts((char *)name);
3350 msg_putchar(' ');
3351 msg_advance(22);
3352 if (type == VAR_NUMBER)
3353 msg_putchar('#');
3354 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3355 msg_putchar('*');
3356 else if (type == VAR_LIST)
3357 {
3358 msg_putchar('[');
3359 if (*string == '[')
3360 ++string;
3361 }
3362 else if (type == VAR_DICT)
3363 {
3364 msg_putchar('{');
3365 if (*string == '{')
3366 ++string;
3367 }
3368 else
3369 msg_putchar(' ');
3370
3371 msg_outtrans(string);
3372
3373 if (type == VAR_FUNC || type == VAR_PARTIAL)
3374 msg_puts("()");
3375 if (*first)
3376 {
3377 msg_clr_eos();
3378 *first = FALSE;
3379 }
3380}
3381
3382/*
3383 * Set variable "name" to value in "tv".
3384 * If the variable already exists, the value is updated.
3385 * Otherwise the variable is created.
3386 */
3387 void
3388set_var(
3389 char_u *name,
3390 typval_T *tv,
3391 int copy) // make copy of value in "tv"
3392{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003393 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003394}
3395
3396/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003397 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003398 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003399 * If the variable already exists and "is_const" is FALSE the value is updated.
3400 * Otherwise the variable is created.
3401 */
3402 void
3403set_var_const(
3404 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003405 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003406 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003407 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003408 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003409 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003410 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003411{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003412 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003413 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003414 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 dictitem_T *di;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003416 typval_T *dest_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003417 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003418 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003419 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003421 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003422 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003423 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003424 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003425 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003426
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003427 if (sid != 0)
3428 {
3429 if (SCRIPT_ID_VALID(sid))
3430 ht = &SCRIPT_VARS(sid);
3431 varname = name;
3432 }
3433 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003434 {
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003435 scriptitem_T *si;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003436
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003437 if (in_vim9script() && is_export
3438 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3439 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
3440 ->sn_autoload_prefix != NULL)
3441 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003442 // In a vim9 autoload script an exported variable is put in the
3443 // global namespace with the autoload prefix.
3444 var_in_autoload = TRUE;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003445 varname = concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003446 if (varname == NULL)
3447 goto failed;
3448 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003449 ht = &globvarht;
3450 }
3451 else
3452 ht = find_var_ht(name, &varname);
3453 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003454 if (ht == NULL || *varname == NUL)
3455 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003456 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003457 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003458 }
Bram Moolenaar54969f42022-02-07 13:56:44 +00003459 is_script_local = ht == get_script_local_ht() || sid != 0
3460 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003461
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003462 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003463 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003464 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003465 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003466 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003467 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003468 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003469 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003470 }
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003471 if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
3472 && vim_strchr((char_u *)"gwbt", name[0]) != NULL)
3473 // Do not make g:var, w:var, b:var or t:var final.
3474 flags &= ~ASSIGN_FINAL;
3475
Bram Moolenaare535db82021-03-31 21:07:24 +02003476 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003477 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3478 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003479 // For "[a, _] = list" the underscore is ignored.
3480 if ((flags & ASSIGN_UNPACK) == 0)
3481 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003482 goto failed;
3483 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003484
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003486
Bram Moolenaar24e93162021-07-18 20:40:33 +02003487 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003488 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003489 imported_T *import = find_imported(varname, 0, FALSE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003490
Bram Moolenaar24e93162021-07-18 20:40:33 +02003491 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003492 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003493 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02003494 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003496 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003497 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003499 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3500 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003501 }
Bram Moolenaar75e27d72022-02-13 13:56:29 +00003502 if (!in_vim9script())
3503 {
3504 semsg(_(e_cannot_create_vim9_script_variable_in_function_str),
3505 name);
3506 goto failed;
3507 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509
Bram Moolenaar993faa32022-02-21 15:59:11 +00003510 // Search in parent scope which is possible to reference from lambda
3511 if (di == NULL)
3512 di = find_var_in_scoped_ht(name, TRUE);
3513
3514 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
3515 && var_wrong_func_name(name, di == NULL))
3516 goto failed;
3517
3518 if (need_convert_to_bool(type, tv))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003519 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003520 // Destination is a bool and the value is not, but it can be
3521 // converted.
3522 CLEAR_FIELD(bool_tv);
3523 bool_tv.v_type = VAR_BOOL;
3524 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3525 tv = &bool_tv;
3526 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003527
Bram Moolenaar993faa32022-02-21 15:59:11 +00003528 if (di != NULL)
3529 {
3530 // Item already exists. Allowed to replace when reloading.
3531 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003532 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003533 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
3534 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003535 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003536 emsg(_(e_cannot_modify_existing_variable));
3537 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003538 }
3539
Bram Moolenaar993faa32022-02-21 15:59:11 +00003540 if (is_script_local && vim9script
3541 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003542 {
3543 semsg(_(e_redefining_script_item_str), name);
3544 goto failed;
3545 }
3546
Bram Moolenaar993faa32022-02-21 15:59:11 +00003547 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003548 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003549 where_T where = WHERE_INIT;
3550 svar_T *sv = find_typval_in_script(&di->di_tv, sid);
3551
3552 if (sv != NULL)
3553 {
3554 // check the type and adjust to bool if needed
3555 where.wt_index = var_idx;
3556 where.wt_variable = TRUE;
3557 if (check_script_var_type(sv, tv, name, where) == FAIL)
3558 goto failed;
3559 if (type == NULL)
3560 type = sv->sv_type;
3561 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003562 }
3563
Bram Moolenaar993faa32022-02-21 15:59:11 +00003564 if ((flags & ASSIGN_FOR_LOOP) == 0
3565 && var_check_permission(di, name) == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003566 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003567 }
3568 else
3569 {
3570 // can only redefine once
3571 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003572
Bram Moolenaar993faa32022-02-21 15:59:11 +00003573 // A Vim9 script-local variable is also present in sn_all_vars
3574 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003575 if (var_in_vim9script || var_in_autoload)
Bram Moolenaar993faa32022-02-21 15:59:11 +00003576 update_vim9_script_var(FALSE, di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003577 var_in_autoload ? name : di->di_key, flags,
Bram Moolenaar993faa32022-02-21 15:59:11 +00003578 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003579 }
3580
Bram Moolenaar993faa32022-02-21 15:59:11 +00003581 // existing variable, need to clear the value
3582
3583 // Handle setting internal di: variables separately where needed to
3584 // prevent changing the type.
3585 if (ht == &vimvarht)
3586 {
3587 if (di->di_tv.v_type == VAR_STRING)
3588 {
3589 VIM_CLEAR(di->di_tv.vval.v_string);
3590 if (copy || tv->v_type != VAR_STRING)
3591 {
3592 char_u *val = tv_get_string(tv);
3593
3594 // Careful: when assigning to v:errmsg and
3595 // tv_get_string() causes an error message the variable
3596 // will already be set.
3597 if (di->di_tv.vval.v_string == NULL)
3598 di->di_tv.vval.v_string = vim_strsave(val);
3599 }
3600 else
3601 {
3602 // Take over the string to avoid an extra alloc/free.
3603 di->di_tv.vval.v_string = tv->vval.v_string;
3604 tv->vval.v_string = NULL;
3605 }
3606 goto failed;
3607 }
3608 else if (di->di_tv.v_type == VAR_NUMBER)
3609 {
3610 di->di_tv.vval.v_number = tv_get_number(tv);
3611 if (STRCMP(varname, "searchforward") == 0)
3612 set_search_direction(di->di_tv.vval.v_number
3613 ? '/' : '?');
3614#ifdef FEAT_SEARCH_EXTRA
3615 else if (STRCMP(varname, "hlsearch") == 0)
3616 {
3617 no_hlsearch = !di->di_tv.vval.v_number;
3618 redraw_all_later(SOME_VALID);
3619 }
3620#endif
3621 goto failed;
3622 }
3623 else if (di->di_tv.v_type != tv->v_type)
3624 {
3625 semsg(_(e_setting_str_to_value_with_wrong_type), name);
3626 goto failed;
3627 }
3628 }
3629
3630 clear_tv(&di->di_tv);
3631 }
3632 else
3633 {
3634 // Item not found, check if a function already exists.
3635 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
3636 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
3637 {
3638 semsg(_(e_redefining_script_item_str), name);
3639 goto failed;
3640 }
3641
3642 // add a new variable
3643 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3644 {
3645 semsg(_(e_unknown_variable_str), name);
3646 goto failed;
3647 }
3648
3649 // Can't add "v:" or "a:" variable.
3650 if (ht == &vimvarht || ht == get_funccal_args_ht())
3651 {
3652 semsg(_(e_illegal_variable_name_str), name);
3653 goto failed;
3654 }
3655
3656 // Make sure the variable name is valid. In Vim9 script an
3657 // autoload variable must be prefixed with "g:" unless in an
3658 // autoload script.
3659 if (!valid_varname(varname, -1, !vim9script
3660 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
3661 goto failed;
3662
3663 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3664 if (di == NULL)
3665 goto failed;
3666 STRCPY(di->di_key, varname);
3667 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
3668 {
3669 vim_free(di);
3670 goto failed;
3671 }
3672 di->di_flags = DI_FLAGS_ALLOC;
3673 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3674 di->di_flags |= DI_FLAGS_LOCK;
3675
3676 // A Vim9 script-local variable is also added to sn_all_vars and
3677 // sn_var_vals. It may set "type" from "tv".
3678 if (var_in_vim9script || var_in_autoload)
3679 update_vim9_script_var(TRUE, di,
3680 var_in_autoload ? name : di->di_key, flags,
3681 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003682 }
3683
Bram Moolenaar993faa32022-02-21 15:59:11 +00003684 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003685 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003686 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003687 else
3688 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003689 *dest_tv = *tv;
3690 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003691 init_tv(tv);
3692 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003693 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003694
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003695 if (vim9script && type != NULL)
Bram Moolenaar381692b2022-02-02 20:01:27 +00003696 set_tv_type(dest_tv, type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003697
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003698 // ":const var = value" locks the value
3699 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003700 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003701 // Like :lockvar! name: lock the value and what it contains, but only
3702 // if the reference count is up to one. That locks only literal
3703 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003704 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003705
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003706failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003707 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003708 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003709 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003710}
3711
3712/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003713 * Check in this order for backwards compatibility:
3714 * - Whether the variable is read-only
3715 * - Whether the variable value is locked
3716 * - Whether the variable is locked
3717 */
3718 int
3719var_check_permission(dictitem_T *di, char_u *name)
3720{
3721 if (var_check_ro(di->di_flags, name, FALSE)
3722 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3723 || var_check_lock(di->di_flags, name, FALSE))
3724 return FAIL;
3725 return OK;
3726}
3727
3728/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003729 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3730 * Also give an error message.
3731 */
3732 int
3733var_check_ro(int flags, char_u *name, int use_gettext)
3734{
3735 if (flags & DI_FLAGS_RO)
3736 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003737 if (name == NULL)
3738 emsg(_(e_cannot_change_readonly_variable));
3739 else
3740 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02003741 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003742 return TRUE;
3743 }
3744 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3745 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003746 if (name == NULL)
3747 emsg(_(e_cannot_set_variable_in_sandbox));
3748 else
3749 semsg(_(e_cannot_set_variable_in_sandbox_str),
3750 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003751 return TRUE;
3752 }
3753 return FALSE;
3754}
3755
3756/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003757 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3758 * Also give an error message.
3759 */
3760 int
3761var_check_lock(int flags, char_u *name, int use_gettext)
3762{
3763 if (flags & DI_FLAGS_LOCK)
3764 {
3765 semsg(_(e_variable_is_locked_str),
3766 use_gettext ? (char_u *)_(name) : name);
3767 return TRUE;
3768 }
3769 return FALSE;
3770}
3771
3772/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003773 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3774 * Also give an error message.
3775 */
3776 int
3777var_check_fixed(int flags, char_u *name, int use_gettext)
3778{
3779 if (flags & DI_FLAGS_FIX)
3780 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003781 if (name == NULL)
3782 emsg(_(e_cannot_delete_variable));
3783 else
3784 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003785 use_gettext ? (char_u *)_(name) : name);
3786 return TRUE;
3787 }
3788 return FALSE;
3789}
3790
3791/*
3792 * Check if a funcref is assigned to a valid variable name.
3793 * Return TRUE and give an error if not.
3794 */
3795 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003796var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003797 char_u *name, // points to start of variable name
3798 int new_var) // TRUE when creating the variable
3799{
Bram Moolenaar32154662021-03-28 21:14:06 +02003800 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
3801 // the name can be used without the s: prefix.
3802 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
3803 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003804 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3805 ? name[2] : name[0]))
3806 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003807 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003808 return TRUE;
3809 }
3810 // Don't allow hiding a function. When "v" is not NULL we might be
3811 // assigning another function to the same var, the type is checked
3812 // below.
3813 if (new_var && function_exists(name, FALSE))
3814 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003815 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003816 name);
3817 return TRUE;
3818 }
3819 return FALSE;
3820}
3821
3822/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003823 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3824 * value. Also give an error message, using "name" or _("name") when
3825 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003826 */
3827 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003828value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003829{
3830 if (lock & VAR_LOCKED)
3831 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003832 if (name == NULL)
3833 emsg(_(e_value_is_locked));
3834 else
3835 semsg(_(e_value_is_locked_str),
3836 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003837 return TRUE;
3838 }
3839 if (lock & VAR_FIXED)
3840 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003841 if (name == NULL)
3842 emsg(_(e_cannot_change_value));
3843 else
3844 semsg(_(e_cannot_change_value_of_str),
3845 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003846 return TRUE;
3847 }
3848 return FALSE;
3849}
3850
3851/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003852 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003853 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003854 * Return FALSE and give an error if not.
3855 */
3856 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003857valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003858{
3859 char_u *p;
3860
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003861 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003862 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003863 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003864 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003865 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003866 return FALSE;
3867 }
3868 return TRUE;
3869}
3870
3871/*
3872 * getwinvar() and gettabwinvar()
3873 */
3874 static void
3875getwinvar(
3876 typval_T *argvars,
3877 typval_T *rettv,
3878 int off) // 1 for gettabwinvar()
3879{
3880 win_T *win;
3881 char_u *varname;
3882 dictitem_T *v;
3883 tabpage_T *tp = NULL;
3884 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00003885 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003886 int need_switch_win;
3887
3888 if (off == 1)
3889 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3890 else
3891 tp = curtab;
3892 win = find_win_by_nr(&argvars[off], tp);
3893 varname = tv_get_string_chk(&argvars[off + 1]);
3894 ++emsg_off;
3895
3896 rettv->v_type = VAR_STRING;
3897 rettv->vval.v_string = NULL;
3898
3899 if (win != NULL && varname != NULL)
3900 {
3901 // Set curwin to be our win, temporarily. Also set the tabpage,
3902 // otherwise the window is not valid. Only do this when needed,
3903 // autocommands get blocked.
3904 need_switch_win = !(tp == curtab && win == curwin);
3905 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00003906 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003907 {
3908 if (*varname == '&')
3909 {
3910 if (varname[1] == NUL)
3911 {
3912 // get all window-local options in a dict
3913 dict_T *opts = get_winbuf_options(FALSE);
3914
3915 if (opts != NULL)
3916 {
3917 rettv_dict_set(rettv, opts);
3918 done = TRUE;
3919 }
3920 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003921 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003922 // window-local-option
3923 done = TRUE;
3924 }
3925 else
3926 {
3927 // Look up the variable.
3928 // Let getwinvar({nr}, "") return the "w:" dictionary.
3929 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3930 varname, FALSE);
3931 if (v != NULL)
3932 {
3933 copy_tv(&v->di_tv, rettv);
3934 done = TRUE;
3935 }
3936 }
3937 }
3938
3939 if (need_switch_win)
3940 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00003941 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003942 }
3943
3944 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3945 // use the default return value
3946 copy_tv(&argvars[off + 2], rettv);
3947
3948 --emsg_off;
3949}
3950
3951/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003952 * Set option "varname" to the value of "varp" for the current buffer/window.
3953 */
3954 static void
3955set_option_from_tv(char_u *varname, typval_T *varp)
3956{
3957 long numval = 0;
3958 char_u *strval;
3959 char_u nbuf[NUMBUFLEN];
3960 int error = FALSE;
3961
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003962 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003963 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003964 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003965 strval = (char_u *)"0"; // avoid using "false"
3966 }
3967 else
3968 {
3969 if (!in_vim9script() || varp->v_type != VAR_STRING)
3970 numval = (long)tv_get_number_chk(varp, &error);
3971 strval = tv_get_string_buf_chk(varp, nbuf);
3972 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02003973 if (!error && strval != NULL)
3974 set_option_value(varname, numval, strval, OPT_LOCAL);
3975}
3976
3977/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003978 * "setwinvar()" and "settabwinvar()" functions
3979 */
3980 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003981setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003982{
3983 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00003984 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003985 int need_switch_win;
3986 char_u *varname, *winvarname;
3987 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003988 tabpage_T *tp = NULL;
3989
3990 if (check_secure())
3991 return;
3992
3993 if (off == 1)
3994 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3995 else
3996 tp = curtab;
3997 win = find_win_by_nr(&argvars[off], tp);
3998 varname = tv_get_string_chk(&argvars[off + 1]);
3999 varp = &argvars[off + 2];
4000
4001 if (win != NULL && varname != NULL && varp != NULL)
4002 {
4003 need_switch_win = !(tp == curtab && win == curwin);
4004 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00004005 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004006 {
4007 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02004008 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004009 else
4010 {
4011 winvarname = alloc(STRLEN(varname) + 3);
4012 if (winvarname != NULL)
4013 {
4014 STRCPY(winvarname, "w:");
4015 STRCPY(winvarname + 2, varname);
4016 set_var(winvarname, varp, TRUE);
4017 vim_free(winvarname);
4018 }
4019 }
4020 }
4021 if (need_switch_win)
Bram Moolenaar18f47402022-01-06 13:24:51 +00004022 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004023 }
4024}
4025
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004026/*
4027 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4028 * v:option_type, and v:option_command.
4029 */
4030 void
4031reset_v_option_vars(void)
4032{
4033 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4034 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4035 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4036 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4037 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4038 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4039}
4040
4041/*
4042 * Add an assert error to v:errors.
4043 */
4044 void
4045assert_error(garray_T *gap)
4046{
4047 struct vimvar *vp = &vimvars[VV_ERRORS];
4048
Bram Moolenaard787e402021-12-24 21:36:12 +00004049 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004050 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004051 set_vim_var_list(VV_ERRORS, list_alloc());
4052 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4053}
4054
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004055 int
4056var_exists(char_u *var)
4057{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004058 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004059 char_u *name;
4060 char_u *tofree;
4061 typval_T tv;
4062 int len = 0;
4063 int n = FALSE;
4064
4065 // get_name_len() takes care of expanding curly braces
4066 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004067 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004068 if (len > 0)
4069 {
4070 if (tofree != NULL)
4071 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004072 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004073 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004074 if (n)
4075 {
4076 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004077 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004078 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4079 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004080 if (n)
4081 clear_tv(&tv);
4082 }
4083 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004084 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004085 n = FALSE;
4086
4087 vim_free(tofree);
4088 return n;
4089}
4090
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004091static lval_T *redir_lval = NULL;
4092#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4093static garray_T redir_ga; // only valid when redir_lval is not NULL
4094static char_u *redir_endp = NULL;
4095static char_u *redir_varname = NULL;
4096
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004097 int
4098alloc_redir_lval(void)
4099{
4100 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4101 if (redir_lval == NULL)
4102 return FAIL;
4103 return OK;
4104}
4105
4106 void
4107clear_redir_lval(void)
4108{
4109 VIM_CLEAR(redir_lval);
4110}
4111
4112 void
4113init_redir_ga(void)
4114{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004115 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004116}
4117
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004118/*
4119 * Start recording command output to a variable
4120 * When "append" is TRUE append to an existing variable.
4121 * Returns OK if successfully completed the setup. FAIL otherwise.
4122 */
4123 int
4124var_redir_start(char_u *name, int append)
4125{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004126 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004127 typval_T tv;
4128
4129 // Catch a bad name early.
4130 if (!eval_isnamec1(*name))
4131 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004132 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004133 return FAIL;
4134 }
4135
4136 // Make a copy of the name, it is used in redir_lval until redir ends.
4137 redir_varname = vim_strsave(name);
4138 if (redir_varname == NULL)
4139 return FAIL;
4140
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004141 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004142 {
4143 var_redir_stop();
4144 return FAIL;
4145 }
4146
4147 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004148 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004149
4150 // Parse the variable name (can be a dict or list entry).
4151 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4152 FNE_CHECK_START);
4153 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4154 {
4155 clear_lval(redir_lval);
4156 if (redir_endp != NULL && *redir_endp != NUL)
4157 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004158 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004159 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004160 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004161 redir_endp = NULL; // don't store a value, only cleanup
4162 var_redir_stop();
4163 return FAIL;
4164 }
4165
4166 // check if we can write to the variable: set it to or append an empty
4167 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004168 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004169 tv.v_type = VAR_STRING;
4170 tv.vval.v_string = (char_u *)"";
4171 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004172 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004173 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004174 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004175 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004176 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004177 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004178 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004179 {
4180 redir_endp = NULL; // don't store a value, only cleanup
4181 var_redir_stop();
4182 return FAIL;
4183 }
4184
4185 return OK;
4186}
4187
4188/*
4189 * Append "value[value_len]" to the variable set by var_redir_start().
4190 * The actual appending is postponed until redirection ends, because the value
4191 * appended may in fact be the string we write to, changing it may cause freed
4192 * memory to be used:
4193 * :redir => foo
4194 * :let foo
4195 * :redir END
4196 */
4197 void
4198var_redir_str(char_u *value, int value_len)
4199{
4200 int len;
4201
4202 if (redir_lval == NULL)
4203 return;
4204
4205 if (value_len == -1)
4206 len = (int)STRLEN(value); // Append the entire string
4207 else
4208 len = value_len; // Append only "value_len" characters
4209
4210 if (ga_grow(&redir_ga, len) == OK)
4211 {
4212 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4213 redir_ga.ga_len += len;
4214 }
4215 else
4216 var_redir_stop();
4217}
4218
4219/*
4220 * Stop redirecting command output to a variable.
4221 * Frees the allocated memory.
4222 */
4223 void
4224var_redir_stop(void)
4225{
4226 typval_T tv;
4227
4228 if (EVALCMD_BUSY)
4229 {
4230 redir_lval = NULL;
4231 return;
4232 }
4233
4234 if (redir_lval != NULL)
4235 {
4236 // If there was no error: assign the text to the variable.
4237 if (redir_endp != NULL)
4238 {
4239 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4240 tv.v_type = VAR_STRING;
4241 tv.vval.v_string = redir_ga.ga_data;
4242 // Call get_lval() again, if it's inside a Dict or List it may
4243 // have changed.
4244 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4245 FALSE, FALSE, 0, FNE_CHECK_START);
4246 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004247 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004248 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004249 clear_lval(redir_lval);
4250 }
4251
4252 // free the collected output
4253 VIM_CLEAR(redir_ga.ga_data);
4254
4255 VIM_CLEAR(redir_lval);
4256 }
4257 VIM_CLEAR(redir_varname);
4258}
4259
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004260/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004261 * Get the collected redirected text and clear redir_ga.
4262 */
4263 char_u *
4264get_clear_redir_ga(void)
4265{
4266 char_u *res;
4267
4268 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4269 res = redir_ga.ga_data;
4270 redir_ga.ga_data = NULL;
4271 return res;
4272}
4273
4274/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004275 * "gettabvar()" function
4276 */
4277 void
4278f_gettabvar(typval_T *argvars, typval_T *rettv)
4279{
Bram Moolenaar18f47402022-01-06 13:24:51 +00004280 switchwin_T switchwin;
4281 tabpage_T *tp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004282 dictitem_T *v;
4283 char_u *varname;
4284 int done = FALSE;
4285
4286 rettv->v_type = VAR_STRING;
4287 rettv->vval.v_string = NULL;
4288
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004289 if (in_vim9script()
4290 && (check_for_number_arg(argvars, 0) == FAIL
4291 || check_for_string_arg(argvars, 1) == FAIL))
4292 return;
4293
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004294 varname = tv_get_string_chk(&argvars[1]);
4295 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4296 if (tp != NULL && varname != NULL)
4297 {
4298 // Set tp to be our tabpage, temporarily. Also set the window to the
4299 // first window in the tabpage, otherwise the window is not valid.
Bram Moolenaar18f47402022-01-06 13:24:51 +00004300 if (switch_win(&switchwin,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004301 tp == curtab || tp->tp_firstwin == NULL ? firstwin
4302 : tp->tp_firstwin, tp, TRUE) == OK)
4303 {
4304 // look up the variable
4305 // Let gettabvar({nr}, "") return the "t:" dictionary.
4306 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
4307 if (v != NULL)
4308 {
4309 copy_tv(&v->di_tv, rettv);
4310 done = TRUE;
4311 }
4312 }
4313
4314 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004315 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004316 }
4317
4318 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4319 copy_tv(&argvars[2], rettv);
4320}
4321
4322/*
4323 * "gettabwinvar()" function
4324 */
4325 void
4326f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4327{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004328 if (in_vim9script()
4329 && (check_for_number_arg(argvars, 0) == FAIL
4330 || check_for_number_arg(argvars, 1) == FAIL
4331 || check_for_string_arg(argvars, 2) == FAIL))
4332 return;
4333
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004334 getwinvar(argvars, rettv, 1);
4335}
4336
4337/*
4338 * "getwinvar()" function
4339 */
4340 void
4341f_getwinvar(typval_T *argvars, typval_T *rettv)
4342{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004343 if (in_vim9script()
4344 && (check_for_number_arg(argvars, 0) == FAIL
4345 || check_for_string_arg(argvars, 1) == FAIL))
4346 return;
4347
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004348 getwinvar(argvars, rettv, 0);
4349}
4350
4351/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004352 * "getbufvar()" function
4353 */
4354 void
4355f_getbufvar(typval_T *argvars, typval_T *rettv)
4356{
4357 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004358 char_u *varname;
4359 dictitem_T *v;
4360 int done = FALSE;
4361
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004362 if (in_vim9script()
4363 && (check_for_buffer_arg(argvars, 0) == FAIL
4364 || check_for_string_arg(argvars, 1) == FAIL))
4365 return;
4366
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004367 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004368 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004369
4370 rettv->v_type = VAR_STRING;
4371 rettv->vval.v_string = NULL;
4372
4373 if (buf != NULL && varname != NULL)
4374 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004375 if (*varname == '&')
4376 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004377 buf_T *save_curbuf = curbuf;
4378
4379 // set curbuf to be our buf, temporarily
4380 curbuf = buf;
4381
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004382 if (varname[1] == NUL)
4383 {
4384 // get all buffer-local options in a dict
4385 dict_T *opts = get_winbuf_options(TRUE);
4386
4387 if (opts != NULL)
4388 {
4389 rettv_dict_set(rettv, opts);
4390 done = TRUE;
4391 }
4392 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004393 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004394 // buffer-local-option
4395 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02004396
4397 // restore previous notion of curbuf
4398 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004399 }
4400 else
4401 {
4402 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02004403 if (*varname == NUL)
4404 // Let getbufvar({nr}, "") return the "b:" dictionary.
4405 v = &buf->b_bufvar;
4406 else
4407 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
4408 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004409 if (v != NULL)
4410 {
4411 copy_tv(&v->di_tv, rettv);
4412 done = TRUE;
4413 }
4414 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004415 }
4416
4417 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4418 // use the default value
4419 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004420}
4421
4422/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004423 * "settabvar()" function
4424 */
4425 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004426f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004427{
4428 tabpage_T *save_curtab;
4429 tabpage_T *tp;
4430 char_u *varname, *tabvarname;
4431 typval_T *varp;
4432
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004433 if (check_secure())
4434 return;
4435
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004436 if (in_vim9script()
4437 && (check_for_number_arg(argvars, 0) == FAIL
4438 || check_for_string_arg(argvars, 1) == FAIL))
4439 return;
4440
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004441 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4442 varname = tv_get_string_chk(&argvars[1]);
4443 varp = &argvars[2];
4444
4445 if (varname != NULL && varp != NULL && tp != NULL)
4446 {
4447 save_curtab = curtab;
4448 goto_tabpage_tp(tp, FALSE, FALSE);
4449
4450 tabvarname = alloc(STRLEN(varname) + 3);
4451 if (tabvarname != NULL)
4452 {
4453 STRCPY(tabvarname, "t:");
4454 STRCPY(tabvarname + 2, varname);
4455 set_var(tabvarname, varp, TRUE);
4456 vim_free(tabvarname);
4457 }
4458
4459 // Restore current tabpage
4460 if (valid_tabpage(save_curtab))
4461 goto_tabpage_tp(save_curtab, FALSE, FALSE);
4462 }
4463}
4464
4465/*
4466 * "settabwinvar()" function
4467 */
4468 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004469f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004470{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004471 if (in_vim9script()
4472 && (check_for_number_arg(argvars, 0) == FAIL
4473 || check_for_number_arg(argvars, 1) == FAIL
4474 || check_for_string_arg(argvars, 2) == FAIL))
4475 return;
4476
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004477 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004478}
4479
4480/*
4481 * "setwinvar()" function
4482 */
4483 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004484f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004485{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004486 if (in_vim9script()
4487 && (check_for_number_arg(argvars, 0) == FAIL
4488 || check_for_string_arg(argvars, 1) == FAIL))
4489 return;
4490
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004491 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004492}
4493
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004494/*
4495 * "setbufvar()" function
4496 */
4497 void
4498f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4499{
4500 buf_T *buf;
4501 char_u *varname, *bufvarname;
4502 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004503
4504 if (check_secure())
4505 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004506
4507 if (in_vim9script()
4508 && (check_for_buffer_arg(argvars, 0) == FAIL
4509 || check_for_string_arg(argvars, 1) == FAIL))
4510 return;
4511
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004512 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004513 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004514 varp = &argvars[2];
4515
4516 if (buf != NULL && varname != NULL && varp != NULL)
4517 {
4518 if (*varname == '&')
4519 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004520 aco_save_T aco;
4521
4522 // set curbuf to be our buf, temporarily
4523 aucmd_prepbuf(&aco, buf);
4524
Bram Moolenaar191929b2020-08-19 21:20:49 +02004525 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004526
4527 // reset notion of buffer
4528 aucmd_restbuf(&aco);
4529 }
4530 else
4531 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004532 bufvarname = alloc(STRLEN(varname) + 3);
4533 if (bufvarname != NULL)
4534 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004535 buf_T *save_curbuf = curbuf;
4536
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004537 curbuf = buf;
4538 STRCPY(bufvarname, "b:");
4539 STRCPY(bufvarname + 2, varname);
4540 set_var(bufvarname, varp, TRUE);
4541 vim_free(bufvarname);
4542 curbuf = save_curbuf;
4543 }
4544 }
4545 }
4546}
4547
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004548/*
4549 * Get a callback from "arg". It can be a Funcref or a function name.
4550 * When "arg" is zero return an empty string.
4551 * "cb_name" is not allocated.
4552 * "cb_name" is set to NULL for an invalid argument.
4553 */
4554 callback_T
4555get_callback(typval_T *arg)
4556{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004557 callback_T res;
4558 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004559
4560 res.cb_free_name = FALSE;
4561 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4562 {
4563 res.cb_partial = arg->vval.v_partial;
4564 ++res.cb_partial->pt_refcount;
4565 res.cb_name = partial_name(res.cb_partial);
4566 }
4567 else
4568 {
4569 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004570 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4571 && isdigit(*arg->vval.v_string))
4572 r = FAIL;
4573 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004574 {
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004575 if (arg->v_type == VAR_STRING)
4576 {
4577 char_u *name;
4578
4579 name = get_scriptlocal_funcname(arg->vval.v_string);
4580 if (name != NULL)
4581 {
4582 vim_free(arg->vval.v_string);
4583 arg->vval.v_string = name;
4584 }
4585 }
4586
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004587 res.cb_name = arg->vval.v_string;
4588 func_ref(res.cb_name);
4589 }
4590 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004591 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004592 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004593 r = FAIL;
4594
4595 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004596 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004597 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004598 res.cb_name = NULL;
4599 }
4600 }
4601 return res;
4602}
4603
4604/*
4605 * Copy a callback into a typval_T.
4606 */
4607 void
4608put_callback(callback_T *cb, typval_T *tv)
4609{
4610 if (cb->cb_partial != NULL)
4611 {
4612 tv->v_type = VAR_PARTIAL;
4613 tv->vval.v_partial = cb->cb_partial;
4614 ++tv->vval.v_partial->pt_refcount;
4615 }
4616 else
4617 {
4618 tv->v_type = VAR_FUNC;
4619 tv->vval.v_string = vim_strsave(cb->cb_name);
4620 func_ref(cb->cb_name);
4621 }
4622}
4623
4624/*
4625 * Make a copy of "src" into "dest", allocating the function name if needed,
4626 * without incrementing the refcount.
4627 */
4628 void
4629set_callback(callback_T *dest, callback_T *src)
4630{
4631 if (src->cb_partial == NULL)
4632 {
4633 // just a function name, make a copy
4634 dest->cb_name = vim_strsave(src->cb_name);
4635 dest->cb_free_name = TRUE;
4636 }
4637 else
4638 {
4639 // cb_name is a pointer into cb_partial
4640 dest->cb_name = src->cb_name;
4641 dest->cb_free_name = FALSE;
4642 }
4643 dest->cb_partial = src->cb_partial;
4644}
4645
4646/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004647 * Copy callback from "src" to "dest", incrementing the refcounts.
4648 */
4649 void
4650copy_callback(callback_T *dest, callback_T *src)
4651{
4652 dest->cb_partial = src->cb_partial;
4653 if (dest->cb_partial != NULL)
4654 {
4655 dest->cb_name = src->cb_name;
4656 dest->cb_free_name = FALSE;
4657 ++dest->cb_partial->pt_refcount;
4658 }
4659 else
4660 {
4661 dest->cb_name = vim_strsave(src->cb_name);
4662 dest->cb_free_name = TRUE;
4663 func_ref(src->cb_name);
4664 }
4665}
4666
4667/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004668 * When a callback refers to an autoload import, change the function name to
4669 * the "path#name" form. Uses the current script context.
4670 * Only works when the name is allocated.
4671 */
4672 void
4673expand_autload_callback(callback_T *cb)
4674{
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004675 char_u *name;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004676 char_u *p;
4677 imported_T *import;
4678
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004679 if (!in_vim9script() || cb->cb_name == NULL
4680 || (!cb->cb_free_name
4681 && (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004682 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004683 if (cb->cb_partial != NULL)
4684 name = cb->cb_partial->pt_name;
4685 else
4686 name = cb->cb_name;
4687 p = vim_strchr(name, '.');
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004688 if (p == NULL)
4689 return;
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004690 import = find_imported(name, p - name, FALSE);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004691 if (import != NULL && SCRIPT_ID_VALID(import->imp_sid))
4692 {
4693 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
4694
4695 if (si->sn_autoload_prefix != NULL)
4696 {
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004697 char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004698
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004699 if (newname != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004700 {
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004701 if (cb->cb_partial != NULL)
4702 {
4703 if (cb->cb_name == cb->cb_partial->pt_name)
4704 cb->cb_name = newname;
4705 vim_free(cb->cb_partial->pt_name);
4706 cb->cb_partial->pt_name = newname;
4707 }
4708 else
4709 {
4710 vim_free(cb->cb_name);
4711 cb->cb_name = newname;
4712 }
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004713 }
4714 }
4715 }
4716}
4717
4718/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004719 * Unref/free "callback" returned by get_callback() or set_callback().
4720 */
4721 void
4722free_callback(callback_T *callback)
4723{
4724 if (callback->cb_partial != NULL)
4725 {
4726 partial_unref(callback->cb_partial);
4727 callback->cb_partial = NULL;
4728 }
4729 else if (callback->cb_name != NULL)
4730 func_unref(callback->cb_name);
4731 if (callback->cb_free_name)
4732 {
4733 vim_free(callback->cb_name);
4734 callback->cb_free_name = FALSE;
4735 }
4736 callback->cb_name = NULL;
4737}
4738
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004739#endif // FEAT_EVAL