blob: 44882cae2f5335f4f08176e4d97d0f5caf22e700 [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
Bram Moolenaard787e402021-12-24 21:36:12 +000048 type_T *vv_type; // type or NULL
Bram Moolenaare5cdf152019-08-29 22:09:46 +020049 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
50} vimvars[VV_LEN] =
51{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020052 // The order here must match the VV_ defines in vim.h!
53 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaard787e402021-12-24 21:36:12 +000054 {VV_NAME("count", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
55 {VV_NAME("count1", VAR_NUMBER), NULL, VV_RO},
56 {VV_NAME("prevcount", VAR_NUMBER), NULL, VV_RO},
57 {VV_NAME("errmsg", VAR_STRING), NULL, VV_COMPAT},
58 {VV_NAME("warningmsg", VAR_STRING), NULL, 0},
59 {VV_NAME("statusmsg", VAR_STRING), NULL, 0},
60 {VV_NAME("shell_error", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
61 {VV_NAME("this_session", VAR_STRING), NULL, VV_COMPAT},
62 {VV_NAME("version", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
63 {VV_NAME("lnum", VAR_NUMBER), NULL, VV_RO_SBX},
64 {VV_NAME("termresponse", VAR_STRING), NULL, VV_RO},
65 {VV_NAME("fname", VAR_STRING), NULL, VV_RO},
66 {VV_NAME("lang", VAR_STRING), NULL, VV_RO},
67 {VV_NAME("lc_time", VAR_STRING), NULL, VV_RO},
68 {VV_NAME("ctype", VAR_STRING), NULL, VV_RO},
69 {VV_NAME("charconvert_from", VAR_STRING), NULL, VV_RO},
70 {VV_NAME("charconvert_to", VAR_STRING), NULL, VV_RO},
71 {VV_NAME("fname_in", VAR_STRING), NULL, VV_RO},
72 {VV_NAME("fname_out", VAR_STRING), NULL, VV_RO},
73 {VV_NAME("fname_new", VAR_STRING), NULL, VV_RO},
74 {VV_NAME("fname_diff", VAR_STRING), NULL, VV_RO},
75 {VV_NAME("cmdarg", VAR_STRING), NULL, VV_RO},
76 {VV_NAME("foldstart", VAR_NUMBER), NULL, VV_RO_SBX},
77 {VV_NAME("foldend", VAR_NUMBER), NULL, VV_RO_SBX},
78 {VV_NAME("folddashes", VAR_STRING), NULL, VV_RO_SBX},
79 {VV_NAME("foldlevel", VAR_NUMBER), NULL, VV_RO_SBX},
80 {VV_NAME("progname", VAR_STRING), NULL, VV_RO},
81 {VV_NAME("servername", VAR_STRING), NULL, VV_RO},
82 {VV_NAME("dying", VAR_NUMBER), NULL, VV_RO},
83 {VV_NAME("exception", VAR_STRING), NULL, VV_RO},
84 {VV_NAME("throwpoint", VAR_STRING), NULL, VV_RO},
85 {VV_NAME("register", VAR_STRING), NULL, VV_RO},
86 {VV_NAME("cmdbang", VAR_NUMBER), NULL, VV_RO},
87 {VV_NAME("insertmode", VAR_STRING), NULL, VV_RO},
88 {VV_NAME("val", VAR_UNKNOWN), NULL, VV_RO},
89 {VV_NAME("key", VAR_UNKNOWN), NULL, VV_RO},
90 {VV_NAME("profiling", VAR_NUMBER), NULL, VV_RO},
91 {VV_NAME("fcs_reason", VAR_STRING), NULL, VV_RO},
92 {VV_NAME("fcs_choice", VAR_STRING), NULL, 0},
93 {VV_NAME("beval_bufnr", VAR_NUMBER), NULL, VV_RO},
94 {VV_NAME("beval_winnr", VAR_NUMBER), NULL, VV_RO},
95 {VV_NAME("beval_winid", VAR_NUMBER), NULL, VV_RO},
96 {VV_NAME("beval_lnum", VAR_NUMBER), NULL, VV_RO},
97 {VV_NAME("beval_col", VAR_NUMBER), NULL, VV_RO},
98 {VV_NAME("beval_text", VAR_STRING), NULL, VV_RO},
99 {VV_NAME("scrollstart", VAR_STRING), NULL, 0},
100 {VV_NAME("swapname", VAR_STRING), NULL, VV_RO},
101 {VV_NAME("swapchoice", VAR_STRING), NULL, 0},
102 {VV_NAME("swapcommand", VAR_STRING), NULL, VV_RO},
103 {VV_NAME("char", VAR_STRING), NULL, 0},
104 {VV_NAME("mouse_win", VAR_NUMBER), NULL, 0},
105 {VV_NAME("mouse_winid", VAR_NUMBER), NULL, 0},
106 {VV_NAME("mouse_lnum", VAR_NUMBER), NULL, 0},
107 {VV_NAME("mouse_col", VAR_NUMBER), NULL, 0},
108 {VV_NAME("operator", VAR_STRING), NULL, VV_RO},
109 {VV_NAME("searchforward", VAR_NUMBER), NULL, 0},
110 {VV_NAME("hlsearch", VAR_NUMBER), NULL, 0},
111 {VV_NAME("oldfiles", VAR_LIST), &t_list_string, 0},
112 {VV_NAME("windowid", VAR_NUMBER), NULL, VV_RO},
113 {VV_NAME("progpath", VAR_STRING), NULL, VV_RO},
114 {VV_NAME("completed_item", VAR_DICT), &t_dict_string, VV_RO},
115 {VV_NAME("option_new", VAR_STRING), NULL, VV_RO},
116 {VV_NAME("option_old", VAR_STRING), NULL, VV_RO},
117 {VV_NAME("option_oldlocal", VAR_STRING), NULL, VV_RO},
118 {VV_NAME("option_oldglobal", VAR_STRING), NULL, VV_RO},
119 {VV_NAME("option_command", VAR_STRING), NULL, VV_RO},
120 {VV_NAME("option_type", VAR_STRING), NULL, VV_RO},
121 {VV_NAME("errors", VAR_LIST), &t_list_string, 0},
122 {VV_NAME("false", VAR_BOOL), NULL, VV_RO},
123 {VV_NAME("true", VAR_BOOL), NULL, VV_RO},
124 {VV_NAME("none", VAR_SPECIAL), NULL, VV_RO},
125 {VV_NAME("null", VAR_SPECIAL), NULL, VV_RO},
126 {VV_NAME("numbermax", VAR_NUMBER), NULL, VV_RO},
127 {VV_NAME("numbermin", VAR_NUMBER), NULL, VV_RO},
128 {VV_NAME("numbersize", VAR_NUMBER), NULL, VV_RO},
129 {VV_NAME("vim_did_enter", VAR_NUMBER), NULL, VV_RO},
130 {VV_NAME("testing", VAR_NUMBER), NULL, 0},
131 {VV_NAME("t_number", VAR_NUMBER), NULL, VV_RO},
132 {VV_NAME("t_string", VAR_NUMBER), NULL, VV_RO},
133 {VV_NAME("t_func", VAR_NUMBER), NULL, VV_RO},
134 {VV_NAME("t_list", VAR_NUMBER), NULL, VV_RO},
135 {VV_NAME("t_dict", VAR_NUMBER), NULL, VV_RO},
136 {VV_NAME("t_float", VAR_NUMBER), NULL, VV_RO},
137 {VV_NAME("t_bool", VAR_NUMBER), NULL, VV_RO},
138 {VV_NAME("t_none", VAR_NUMBER), NULL, VV_RO},
139 {VV_NAME("t_job", VAR_NUMBER), NULL, VV_RO},
140 {VV_NAME("t_channel", VAR_NUMBER), NULL, VV_RO},
141 {VV_NAME("t_blob", VAR_NUMBER), NULL, VV_RO},
142 {VV_NAME("termrfgresp", VAR_STRING), NULL, VV_RO},
143 {VV_NAME("termrbgresp", VAR_STRING), NULL, VV_RO},
144 {VV_NAME("termu7resp", VAR_STRING), NULL, VV_RO},
145 {VV_NAME("termstyleresp", VAR_STRING), NULL, VV_RO},
146 {VV_NAME("termblinkresp", VAR_STRING), NULL, VV_RO},
147 {VV_NAME("event", VAR_DICT), NULL, VV_RO},
148 {VV_NAME("versionlong", VAR_NUMBER), NULL, VV_RO},
149 {VV_NAME("echospace", VAR_NUMBER), NULL, VV_RO},
150 {VV_NAME("argv", VAR_LIST), &t_list_string, VV_RO},
151 {VV_NAME("collate", VAR_STRING), NULL, VV_RO},
152 {VV_NAME("exiting", VAR_SPECIAL), NULL, VV_RO},
153 {VV_NAME("colornames", VAR_DICT), &t_dict_string, VV_RO},
154 {VV_NAME("sizeofint", VAR_NUMBER), NULL, VV_RO},
155 {VV_NAME("sizeoflong", VAR_NUMBER), NULL, VV_RO},
156 {VV_NAME("sizeofpointer", VAR_NUMBER), NULL, VV_RO},
naohiro ono56200ee2022-01-01 14:59:44 +0000157 {VV_NAME("maxcol", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200158};
159
160// shorthand
Bram Moolenaard787e402021-12-24 21:36:12 +0000161#define vv_tv_type vv_di.di_tv.v_type
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200162#define vv_nr vv_di.di_tv.vval.v_number
163#define vv_float vv_di.di_tv.vval.v_float
164#define vv_str vv_di.di_tv.vval.v_string
165#define vv_list vv_di.di_tv.vval.v_list
166#define vv_dict vv_di.di_tv.vval.v_dict
167#define vv_blob vv_di.di_tv.vval.v_blob
168#define vv_tv vv_di.di_tv
169
170static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200171static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200172#define vimvarht vimvardict.dv_hashtab
173
174// for VIM_VERSION_ defines
175#include "version.h"
176
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200177static void list_glob_vars(int *first);
178static void list_buf_vars(int *first);
179static void list_win_vars(int *first);
180static void list_tab_vars(int *first);
181static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100182static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op, int var_idx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200183static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
184static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200185static void list_one_var(dictitem_T *v, char *prefix, int *first);
186static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
187
188/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200189 * Initialize global and vim special variables
190 */
191 void
192evalvars_init(void)
193{
194 int i;
195 struct vimvar *p;
196
197 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
198 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
199 vimvardict.dv_lock = VAR_FIXED;
200 hash_init(&compat_hashtab);
201
202 for (i = 0; i < VV_LEN; ++i)
203 {
204 p = &vimvars[i];
205 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
206 {
207 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
208 getout(1);
209 }
210 STRCPY(p->vv_di.di_key, p->vv_name);
211 if (p->vv_flags & VV_RO)
212 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
213 else if (p->vv_flags & VV_RO_SBX)
214 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
215 else
216 p->vv_di.di_flags = DI_FLAGS_FIX;
217
218 // add to v: scope dict, unless the value is not always available
Bram Moolenaard787e402021-12-24 21:36:12 +0000219 if (p->vv_tv_type != VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200220 hash_add(&vimvarht, p->vv_di.di_key);
221 if (p->vv_flags & VV_COMPAT)
222 // add to compat scope dict
223 hash_add(&compat_hashtab, p->vv_di.di_key);
224 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200225 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
226 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200227
228 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
229 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100230 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200231 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
232 set_vim_var_list(VV_ERRORS, list_alloc());
233 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
234
235 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
236 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
237 set_vim_var_nr(VV_NONE, VVAL_NONE);
238 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100239 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
240 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100241 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaar69b30722021-11-02 21:39:49 +0000242 set_vim_var_nr(VV_SIZEOFINT, sizeof(int));
243 set_vim_var_nr(VV_SIZEOFLONG, sizeof(long));
244 set_vim_var_nr(VV_SIZEOFPOINTER, sizeof(char *));
naohiro ono56200ee2022-01-01 14:59:44 +0000245 set_vim_var_nr(VV_MAXCOL, MAXCOL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200246
247 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
248 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
249 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
250 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
251 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
252 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
253 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
254 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
255 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
256 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
257 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
258
259 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
260
Drew Vogele30d1022021-10-24 20:35:07 +0100261 set_vim_var_dict(VV_COLORNAMES, dict_alloc());
262
Bram Moolenaar439c0362020-06-06 15:58:03 +0200263 // Default for v:register is not 0 but '"'. This is adjusted once the
264 // clipboard has been setup by calling reset_reg_var().
265 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200266}
267
268#if defined(EXITFREE) || defined(PROTO)
269/*
270 * Free all vim variables information on exit
271 */
272 void
273evalvars_clear(void)
274{
275 int i;
276 struct vimvar *p;
277
278 for (i = 0; i < VV_LEN; ++i)
279 {
280 p = &vimvars[i];
281 if (p->vv_di.di_tv.v_type == VAR_STRING)
282 VIM_CLEAR(p->vv_str);
283 else if (p->vv_di.di_tv.v_type == VAR_LIST)
284 {
285 list_unref(p->vv_list);
286 p->vv_list = NULL;
287 }
288 }
289 hash_clear(&vimvarht);
290 hash_init(&vimvarht); // garbage_collect() will access it
291 hash_clear(&compat_hashtab);
292
293 // global variables
294 vars_clear(&globvarht);
295
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100296 // Script-local variables. Clear all the variables here.
297 // The scriptvar_T is cleared later in free_scriptnames(), because a
298 // variable in one script might hold a reference to the whole scope of
299 // another script.
300 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200301 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200302}
303#endif
304
305 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200306garbage_collect_globvars(int copyID)
307{
308 return set_ref_in_ht(&globvarht, copyID, NULL);
309}
310
311 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200312garbage_collect_vimvars(int copyID)
313{
314 return set_ref_in_ht(&vimvarht, copyID, NULL);
315}
316
317 int
318garbage_collect_scriptvars(int copyID)
319{
Bram Moolenaared234f22020-10-15 20:42:20 +0200320 int i;
321 int idx;
322 int abort = FALSE;
323 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200324
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100325 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200326 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200327 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
328
Bram Moolenaared234f22020-10-15 20:42:20 +0200329 si = SCRIPT_ITEM(i);
330 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
331 {
332 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
333
Bram Moolenaard00a7fb2021-03-08 20:47:14 +0100334 if (sv->sv_name != NULL)
335 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
Bram Moolenaared234f22020-10-15 20:42:20 +0200336 }
337 }
338
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200339 return abort;
340}
341
342/*
343 * Set an internal variable to a string value. Creates the variable if it does
344 * not already exist.
345 */
346 void
347set_internal_string_var(char_u *name, char_u *value)
348{
349 char_u *val;
350 typval_T *tvp;
351
352 val = vim_strsave(value);
353 if (val != NULL)
354 {
355 tvp = alloc_string_tv(val);
356 if (tvp != NULL)
357 {
358 set_var(name, tvp, FALSE);
359 free_tv(tvp);
360 }
361 }
362}
363
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200364 int
365eval_charconvert(
366 char_u *enc_from,
367 char_u *enc_to,
368 char_u *fname_from,
369 char_u *fname_to)
370{
371 int err = FALSE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000372 sctx_T saved_sctx = current_sctx;
373 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200374
375 set_vim_var_string(VV_CC_FROM, enc_from, -1);
376 set_vim_var_string(VV_CC_TO, enc_to, -1);
377 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
378 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000379 ctx = get_option_sctx("charconvert");
380 if (ctx != NULL)
381 current_sctx = *ctx;
382
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200383 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
384 err = TRUE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000385
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200386 set_vim_var_string(VV_CC_FROM, NULL, -1);
387 set_vim_var_string(VV_CC_TO, NULL, -1);
388 set_vim_var_string(VV_FNAME_IN, NULL, -1);
389 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000390 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200391
392 if (err)
393 return FAIL;
394 return OK;
395}
396
397# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
398 int
399eval_printexpr(char_u *fname, char_u *args)
400{
401 int err = FALSE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000402 sctx_T saved_sctx = current_sctx;
403 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200404
405 set_vim_var_string(VV_FNAME_IN, fname, -1);
406 set_vim_var_string(VV_CMDARG, args, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000407 ctx = get_option_sctx("printexpr");
408 if (ctx != NULL)
409 current_sctx = *ctx;
410
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200411 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
412 err = TRUE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000413
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200414 set_vim_var_string(VV_FNAME_IN, NULL, -1);
415 set_vim_var_string(VV_CMDARG, NULL, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000416 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200417
418 if (err)
419 {
420 mch_remove(fname);
421 return FAIL;
422 }
423 return OK;
424}
425# endif
426
427# if defined(FEAT_DIFF) || defined(PROTO)
428 void
429eval_diff(
430 char_u *origfile,
431 char_u *newfile,
432 char_u *outfile)
433{
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000434 sctx_T saved_sctx = current_sctx;
435 sctx_T *ctx;
436 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200437
438 set_vim_var_string(VV_FNAME_IN, origfile, -1);
439 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
440 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000441
442 ctx = get_option_sctx("diffexpr");
443 if (ctx != NULL)
444 current_sctx = *ctx;
445
446 // errors are ignored
447 tv = eval_expr(p_dex, NULL);
Bram Moolenaar39b89442022-01-22 18:21:36 +0000448 free_tv(tv);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000449
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200450 set_vim_var_string(VV_FNAME_IN, NULL, -1);
451 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
452 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000453 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200454}
455
456 void
457eval_patch(
458 char_u *origfile,
459 char_u *difffile,
460 char_u *outfile)
461{
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000462 sctx_T saved_sctx = current_sctx;
463 sctx_T *ctx;
464 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200465
466 set_vim_var_string(VV_FNAME_IN, origfile, -1);
467 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
468 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000469
470 ctx = get_option_sctx("patchexpr");
471 if (ctx != NULL)
472 current_sctx = *ctx;
473
474 // errors are ignored
475 tv = eval_expr(p_pex, NULL);
476 free_tv(tv);
477
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200478 set_vim_var_string(VV_FNAME_IN, NULL, -1);
479 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
480 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000481 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200482}
483# endif
484
485#if defined(FEAT_SPELL) || defined(PROTO)
486/*
487 * Evaluate an expression to a list with suggestions.
488 * For the "expr:" part of 'spellsuggest'.
489 * Returns NULL when there is an error.
490 */
491 list_T *
492eval_spell_expr(char_u *badword, char_u *expr)
493{
494 typval_T save_val;
495 typval_T rettv;
496 list_T *list = NULL;
497 char_u *p = skipwhite(expr);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000498 sctx_T saved_sctx = current_sctx;
499 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200500
501 // Set "v:val" to the bad word.
502 prepare_vimvar(VV_VAL, &save_val);
503 set_vim_var_string(VV_VAL, badword, -1);
504 if (p_verbose == 0)
505 ++emsg_off;
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000506 ctx = get_option_sctx("spellsuggest");
507 if (ctx != NULL)
508 current_sctx = *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200509
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200510 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200511 {
512 if (rettv.v_type != VAR_LIST)
513 clear_tv(&rettv);
514 else
515 list = rettv.vval.v_list;
516 }
517
518 if (p_verbose == 0)
519 --emsg_off;
520 clear_tv(get_vim_var_tv(VV_VAL));
521 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000522 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200523
524 return list;
525}
526
527/*
528 * "list" is supposed to contain two items: a word and a number. Return the
529 * word in "pp" and the number as the return value.
530 * Return -1 if anything isn't right.
531 * Used to get the good word and score from the eval_spell_expr() result.
532 */
533 int
534get_spellword(list_T *list, char_u **pp)
535{
536 listitem_T *li;
537
538 li = list->lv_first;
539 if (li == NULL)
540 return -1;
541 *pp = tv_get_string(&li->li_tv);
542
543 li = li->li_next;
544 if (li == NULL)
545 return -1;
546 return (int)tv_get_number(&li->li_tv);
547}
548#endif
549
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200550/*
551 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200552 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200553 * When not used yet add the variable to the v: hashtable.
554 */
555 void
556prepare_vimvar(int idx, typval_T *save_tv)
557{
558 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200559 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaard787e402021-12-24 21:36:12 +0000560 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200561 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
562}
563
564/*
565 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200566 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200567 * When no longer defined, remove the variable from the v: hashtable.
568 */
569 void
570restore_vimvar(int idx, typval_T *save_tv)
571{
572 hashitem_T *hi;
573
574 vimvars[idx].vv_tv = *save_tv;
Bram Moolenaard787e402021-12-24 21:36:12 +0000575 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200576 {
577 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
578 if (HASHITEM_EMPTY(hi))
579 internal_error("restore_vimvar()");
580 else
581 hash_remove(&vimvarht, hi);
582 }
583}
584
585/*
586 * List Vim variables.
587 */
588 static void
589list_vim_vars(int *first)
590{
591 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
592}
593
594/*
595 * List script-local variables, if there is a script.
596 */
597 static void
598list_script_vars(int *first)
599{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200600 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200601 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
602 "s:", FALSE, first);
603}
604
605/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200606 * Get a list of lines from a HERE document. The here document is a list of
607 * lines surrounded by a marker.
608 * cmd << {marker}
609 * {line1}
610 * {line2}
611 * ....
612 * {marker}
613 *
614 * The {marker} is a string. If the optional 'trim' word is supplied before the
615 * marker, then the leading indentation before the lines (matching the
616 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200617 *
618 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
619 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
620 * missing, then '.' is accepted as a marker.
621 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200622 * Returns a List with {lines} or NULL.
623 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200625heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200626{
627 char_u *theline;
628 char_u *marker;
629 list_T *l;
630 char_u *p;
631 int marker_indent_len = 0;
632 int text_indent_len = 0;
633 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200634 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200635 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200636
637 if (eap->getline == NULL)
638 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000639 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200640 return NULL;
641 }
642
643 // Check for the optional 'trim' word before the marker
644 cmd = skipwhite(cmd);
645 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
646 {
647 cmd = skipwhite(cmd + 4);
648
649 // Trim the indentation from all the lines in the here document.
650 // The amount of indentation trimmed is the same as the indentation of
651 // the first line after the :let command line. To find the end marker
652 // the indent of the :let command line is trimmed.
653 p = *eap->cmdlinep;
654 while (VIM_ISWHITE(*p))
655 {
656 p++;
657 marker_indent_len++;
658 }
659 text_indent_len = -1;
660 }
661
662 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200663 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200664 {
665 marker = skipwhite(cmd);
666 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200667 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200668 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000669 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200670 return NULL;
671 }
672 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200673 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200674 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000675 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200676 return NULL;
677 }
678 }
679 else
680 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200681 // When getting lines for an embedded script, if the marker is missing,
682 // accept '.' as the marker.
683 if (script_get)
684 marker = dot;
685 else
686 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000687 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200688 return NULL;
689 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200690 }
691
692 l = list_alloc();
693 if (l == NULL)
694 return NULL;
695
696 for (;;)
697 {
698 int mi = 0;
699 int ti = 0;
700
701 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
702 if (theline == NULL)
703 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000704 semsg(_(e_missing_end_marker_str), marker);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200705 break;
706 }
707
708 // with "trim": skip the indent matching the :let line to find the
709 // marker
710 if (marker_indent_len > 0
711 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
712 mi = marker_indent_len;
713 if (STRCMP(marker, theline + mi) == 0)
714 {
715 vim_free(theline);
716 break;
717 }
718
719 if (text_indent_len == -1 && *theline != NUL)
720 {
721 // set the text indent from the first line.
722 p = theline;
723 text_indent_len = 0;
724 while (VIM_ISWHITE(*p))
725 {
726 p++;
727 text_indent_len++;
728 }
729 text_indent = vim_strnsave(theline, text_indent_len);
730 }
731 // with "trim": skip the indent matching the first line
732 if (text_indent != NULL)
733 for (ti = 0; ti < text_indent_len; ++ti)
734 if (theline[ti] != text_indent[ti])
735 break;
736
737 if (list_append_string(l, theline + ti, -1) == FAIL)
738 break;
739 vim_free(theline);
740 }
741 vim_free(text_indent);
742
743 return l;
744}
745
746/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200747 * Vim9 variable declaration:
748 * ":var name"
749 * ":var name: type"
750 * ":var name = expr"
751 * ":var name: type = expr"
752 * etc.
753 */
754 void
755ex_var(exarg_T *eap)
756{
Bram Moolenaar330a3882022-03-05 11:05:57 +0000757 char_u *p = eap->cmd;
Bram Moolenaare1d12112022-03-05 11:37:48 +0000758 int has_var;
Bram Moolenaar330a3882022-03-05 11:05:57 +0000759
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200760 if (!in_vim9script())
761 {
762 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
763 return;
764 }
Bram Moolenaare1d12112022-03-05 11:37:48 +0000765 has_var = checkforcmd_noparen(&p, "var", 3);
766 if (current_sctx.sc_sid == 0 && has_var)
Bram Moolenaar0e1574c2022-03-03 17:05:35 +0000767 {
768 emsg(_(e_cannot_declare_variable_on_command_line));
769 return;
770 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200771 ex_let(eap);
772}
773
774/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200775 * ":let" list all variable values
776 * ":let var1 var2" list variable values
777 * ":let var = expr" assignment command.
778 * ":let var += expr" assignment command.
779 * ":let var -= expr" assignment command.
780 * ":let var *= expr" assignment command.
781 * ":let var /= expr" assignment command.
782 * ":let var %= expr" assignment command.
783 * ":let var .= expr" assignment command.
784 * ":let var ..= expr" assignment command.
785 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100786 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100787 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200788 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200789 * ":final var = expr" assignment command.
790 * ":final [var1, var2] = expr" unpack list.
791 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200792 * ":const" list all variable values
793 * ":const var1 var2" list variable values
794 * ":const var = expr" assignment command.
795 * ":const [var1, var2] = expr" unpack list.
796 */
797 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200798ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200799{
800 char_u *arg = eap->arg;
801 char_u *expr = NULL;
802 typval_T rettv;
803 int i;
804 int var_count = 0;
805 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200806 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200807 char_u *argend;
808 int first = TRUE;
809 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200810 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100811 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200812 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200813
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200814 if (eap->cmdidx == CMD_final && !vim9script)
815 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100816 // In legacy Vim script ":final" is short for ":finally".
817 ex_finally(eap);
818 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200819 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200820 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200821 {
822 emsg(_(e_cannot_use_let_in_vim9_script));
823 return;
824 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200825
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100826 if (eap->cmdidx == CMD_const)
827 flags |= ASSIGN_CONST;
828 else if (eap->cmdidx == CMD_final)
829 flags |= ASSIGN_FINAL;
830
831 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100832 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200833 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200835 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200836 if (argend == NULL)
837 return;
838 if (argend > arg && argend[-1] == '.') // for var.='str'
839 --argend;
840 expr = skipwhite(argend);
841 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +0200842 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200843 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200844 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
845 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200846 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200847 {
848 // ":let" without "=": list variables
849 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000850 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200851 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000852 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200853 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200854 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200855 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200856 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100857 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +0000858 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100859 else
860 // Vim9 declaration ":var name: type"
861 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200862 }
863 else
864 {
865 // ":let var1 var2" - list values
866 arg = list_arg_vars(eap, arg, &first);
867 }
868 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200869 else if (!eap->skip)
870 {
871 // ":let"
872 list_glob_vars(&first);
873 list_buf_vars(&first);
874 list_win_vars(&first);
875 list_tab_vars(&first);
876 list_script_vars(&first);
877 list_func_vars(&first);
878 list_vim_vars(&first);
879 }
Bram Moolenaar63b91732021-08-05 20:40:03 +0200880 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200881 }
882 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
883 {
884 list_T *l;
Bram Moolenaar81530e32021-07-28 21:25:49 +0200885 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200886
887 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200888 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200889 if (l != NULL)
890 {
891 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200892 if (!eap->skip)
893 {
Bram Moolenaar81530e32021-07-28 21:25:49 +0200894 // errors are for the assignment, not the end marker
895 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200896 op[0] = '=';
897 op[1] = NUL;
898 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100899 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200900 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200901 clear_tv(&rettv);
902 }
903 }
904 else
905 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200906 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200907 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200908
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200909 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200910 i = FAIL;
911 if (has_assign || concat)
912 {
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100913 int cur_lnum;
914
Bram Moolenaar32e35112020-05-14 22:41:15 +0200915 op[0] = '=';
916 op[1] = NUL;
917 if (*expr != '=')
918 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200919 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200920 {
921 // +=, /=, etc. require an existing variable
922 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
923 i = FAIL;
924 }
925 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200926 {
927 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200928 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200929 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200930 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200931 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200932 ++len;
933 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200934 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200935 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200936 }
937 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200938 ++expr;
939
Bram Moolenaar7f2c3412021-11-29 16:01:49 +0000940 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200941 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200942 {
943 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100944 semsg(_(e_white_space_required_before_and_after_str_at_str),
945 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200946 i = FAIL;
947 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200948
949 if (eap->skip)
950 ++emsg_skip;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200951 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200952 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100953 cur_lnum = SOURCING_LNUM;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200954 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200955 if (eap->skip)
956 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200957 clear_evalarg(&evalarg, eap);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100958
959 // Restore the line number so that any type error is given for the
960 // declaration, not the expression.
961 SOURCING_LNUM = cur_lnum;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200962 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200963 if (eap->skip)
964 {
965 if (i != FAIL)
966 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200967 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200968 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200969 {
970 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200971 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200972 clear_tv(&rettv);
973 }
974 }
975}
976
977/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +0100978 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200979 * Handles both "var" with any type and "[var, var; var]" with a list type.
980 * When "op" is not NULL it points to a string with characters that
981 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
982 * or concatenate.
983 * Returns OK or FAIL;
984 */
985 int
986ex_let_vars(
987 char_u *arg_start,
988 typval_T *tv,
989 int copy, // copy values from "tv", don't move
990 int semicolon, // from skip_var_list()
991 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100992 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200993 char_u *op)
994{
995 char_u *arg = arg_start;
996 list_T *l;
997 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100998 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200999 listitem_T *item;
1000 typval_T ltv;
1001
1002 if (*arg != '[')
1003 {
1004 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001005 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001006 return FAIL;
1007 return OK;
1008 }
1009
1010 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1011 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
1012 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001013 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001014 return FAIL;
1015 }
1016
1017 i = list_len(l);
1018 if (semicolon == 0 && var_count < i)
1019 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001020 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001021 return FAIL;
1022 }
1023 if (var_count - semicolon > i)
1024 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001025 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001026 return FAIL;
1027 }
1028
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001029 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001030 item = l->lv_first;
1031 while (*arg != ']')
1032 {
1033 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001034 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001035 arg = ex_let_one(arg, &item->li_tv, TRUE,
1036 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001037 item = item->li_next;
1038 if (arg == NULL)
1039 return FAIL;
1040
1041 arg = skipwhite(arg);
1042 if (*arg == ';')
1043 {
1044 // Put the rest of the list (may be empty) in the var after ';'.
1045 // Create a new list for this.
1046 l = list_alloc();
1047 if (l == NULL)
1048 return FAIL;
1049 while (item != NULL)
1050 {
1051 list_append_tv(l, &item->li_tv);
1052 item = item->li_next;
1053 }
1054
1055 ltv.v_type = VAR_LIST;
1056 ltv.v_lock = 0;
1057 ltv.vval.v_list = l;
1058 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001059 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001060
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001061 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1062 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001063 clear_tv(&ltv);
1064 if (arg == NULL)
1065 return FAIL;
1066 break;
1067 }
1068 else if (*arg != ',' && *arg != ']')
1069 {
1070 internal_error("ex_let_vars()");
1071 return FAIL;
1072 }
1073 }
1074
1075 return OK;
1076}
1077
1078/*
1079 * Skip over assignable variable "var" or list of variables "[var, var]".
1080 * Used for ":let varvar = expr" and ":for varvar in expr".
1081 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001082 * for "[var, var; var]" set "semicolon" to 1.
1083 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001084 * Return NULL for an error.
1085 */
1086 char_u *
1087skip_var_list(
1088 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001089 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001090 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001091 int *semicolon,
1092 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001093{
1094 char_u *p, *s;
1095
1096 if (*arg == '[')
1097 {
1098 // "[var, var]": find the matching ']'.
1099 p = arg;
1100 for (;;)
1101 {
1102 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001103 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001104 if (s == p)
1105 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001106 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001107 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001108 return NULL;
1109 }
1110 ++*var_count;
1111
1112 p = skipwhite(s);
1113 if (*p == ']')
1114 break;
1115 else if (*p == ';')
1116 {
1117 if (*semicolon == 1)
1118 {
Bram Moolenaar8b716f52022-02-15 21:17:56 +00001119 if (!silent)
1120 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001121 return NULL;
1122 }
1123 *semicolon = 1;
1124 }
1125 else if (*p != ',')
1126 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001127 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001128 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001129 return NULL;
1130 }
1131 }
1132 return p + 1;
1133 }
1134 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001136}
1137
1138/*
1139 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1140 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001141 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001142 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001143 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001144skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001145{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001146 char_u *end;
1147 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001148
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001149 if (*arg == '@' && arg[1] != NUL)
1150 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001151 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001152 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001153
1154 // "a: type" is declaring variable "a" with a type, not "a:".
1155 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001156 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001157 --end;
1158
Bram Moolenaar585587d2021-01-17 20:52:13 +01001159 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001160 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001161 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001162 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001163 }
1164 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001165}
1166
1167/*
1168 * List variables for hashtab "ht" with prefix "prefix".
1169 * If "empty" is TRUE also list NULL strings as empty strings.
1170 */
1171 void
1172list_hashtable_vars(
1173 hashtab_T *ht,
1174 char *prefix,
1175 int empty,
1176 int *first)
1177{
1178 hashitem_T *hi;
1179 dictitem_T *di;
1180 int todo;
1181 char_u buf[IOSIZE];
1182
1183 todo = (int)ht->ht_used;
1184 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1185 {
1186 if (!HASHITEM_EMPTY(hi))
1187 {
1188 --todo;
1189 di = HI2DI(hi);
1190
1191 // apply :filter /pat/ to variable name
1192 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1193 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1194 if (message_filtered(buf))
1195 continue;
1196
1197 if (empty || di->di_tv.v_type != VAR_STRING
1198 || di->di_tv.vval.v_string != NULL)
1199 list_one_var(di, prefix, first);
1200 }
1201 }
1202}
1203
1204/*
1205 * List global variables.
1206 */
1207 static void
1208list_glob_vars(int *first)
1209{
1210 list_hashtable_vars(&globvarht, "", TRUE, first);
1211}
1212
1213/*
1214 * List buffer variables.
1215 */
1216 static void
1217list_buf_vars(int *first)
1218{
1219 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1220}
1221
1222/*
1223 * List window variables.
1224 */
1225 static void
1226list_win_vars(int *first)
1227{
1228 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1229}
1230
1231/*
1232 * List tab page variables.
1233 */
1234 static void
1235list_tab_vars(int *first)
1236{
1237 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1238}
1239
1240/*
1241 * List variables in "arg".
1242 */
1243 static char_u *
1244list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1245{
1246 int error = FALSE;
1247 int len;
1248 char_u *name;
1249 char_u *name_start;
1250 char_u *arg_subsc;
1251 char_u *tofree;
1252 typval_T tv;
1253
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001254 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001255 {
1256 if (error || eap->skip)
1257 {
1258 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1259 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1260 {
1261 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001262 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001263 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001264 break;
1265 }
1266 }
1267 else
1268 {
1269 // get_name_len() takes care of expanding curly braces
1270 name_start = name = arg;
1271 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1272 if (len <= 0)
1273 {
1274 // This is mainly to keep test 49 working: when expanding
1275 // curly braces fails overrule the exception error message.
1276 if (len < 0 && !aborting())
1277 {
1278 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001279 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001280 break;
1281 }
1282 error = TRUE;
1283 }
1284 else
1285 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001286 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001287 if (tofree != NULL)
1288 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001289 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001290 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001291 error = TRUE;
1292 else
1293 {
1294 // handle d.key, l[idx], f(expr)
1295 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001296 if (handle_subscript(&arg, name_start, &tv,
1297 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001298 error = TRUE;
1299 else
1300 {
1301 if (arg == arg_subsc && len == 2 && name[1] == ':')
1302 {
1303 switch (*name)
1304 {
1305 case 'g': list_glob_vars(first); break;
1306 case 'b': list_buf_vars(first); break;
1307 case 'w': list_win_vars(first); break;
1308 case 't': list_tab_vars(first); break;
1309 case 'v': list_vim_vars(first); break;
1310 case 's': list_script_vars(first); break;
1311 case 'l': list_func_vars(first); break;
1312 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001313 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001314 }
1315 }
1316 else
1317 {
1318 char_u numbuf[NUMBUFLEN];
1319 char_u *tf;
1320 int c;
1321 char_u *s;
1322
1323 s = echo_string(&tv, &tf, numbuf, 0);
1324 c = *arg;
1325 *arg = NUL;
1326 list_one_var_a("",
1327 arg == arg_subsc ? name : name_start,
1328 tv.v_type,
1329 s == NULL ? (char_u *)"" : s,
1330 first);
1331 *arg = c;
1332 vim_free(tf);
1333 }
1334 clear_tv(&tv);
1335 }
1336 }
1337 }
1338
1339 vim_free(tofree);
1340 }
1341
1342 arg = skipwhite(arg);
1343 }
1344
1345 return arg;
1346}
1347
1348/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001349 * Set an environment variable, part of ex_let_one().
1350 */
1351 static char_u *
1352ex_let_env(
1353 char_u *arg,
1354 typval_T *tv,
1355 int flags,
1356 char_u *endchars,
1357 char_u *op)
1358{
1359 char_u *arg_end = NULL;
1360 char_u *name;
1361 int len;
1362
1363 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1364 && (flags & ASSIGN_FOR_LOOP) == 0)
1365 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001366 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001367 return NULL;
1368 }
1369
1370 // Find the end of the name.
1371 ++arg;
1372 name = arg;
1373 len = get_env_len(&arg);
1374 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001375 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001376 else
1377 {
1378 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001379 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001380 else if (endchars != NULL
1381 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1382 emsg(_(e_unexpected_characters_in_let));
1383 else if (!check_secure())
1384 {
1385 char_u *tofree = NULL;
1386 int c1 = name[len];
1387 char_u *p;
1388
1389 name[len] = NUL;
1390 p = tv_get_string_chk(tv);
1391 if (p != NULL && op != NULL && *op == '.')
1392 {
1393 int mustfree = FALSE;
1394 char_u *s = vim_getenv(name, &mustfree);
1395
1396 if (s != NULL)
1397 {
1398 p = tofree = concat_str(s, p);
1399 if (mustfree)
1400 vim_free(s);
1401 }
1402 }
1403 if (p != NULL)
1404 {
1405 vim_setenv_ext(name, p);
1406 arg_end = arg;
1407 }
1408 name[len] = c1;
1409 vim_free(tofree);
1410 }
1411 }
1412 return arg_end;
1413}
1414
1415/*
1416 * Set an option, part of ex_let_one().
1417 */
1418 static char_u *
1419ex_let_option(
1420 char_u *arg,
1421 typval_T *tv,
1422 int flags,
1423 char_u *endchars,
1424 char_u *op)
1425{
1426 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001427 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001428 char_u *arg_end = NULL;
1429
1430 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1431 && (flags & ASSIGN_FOR_LOOP) == 0)
1432 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001433 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001434 return NULL;
1435 }
1436
1437 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001438 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001439 if (p == NULL || (endchars != NULL
1440 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1441 emsg(_(e_unexpected_characters_in_let));
1442 else
1443 {
1444 int c1;
1445 long n = 0;
1446 getoption_T opt_type;
1447 long numval;
1448 char_u *stringval = NULL;
1449 char_u *s = NULL;
1450 int failed = FALSE;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001451 int opt_p_flags;
1452 char_u *tofree = NULL;
Bram Moolenaar92c33eb2021-12-07 11:03:39 +00001453 char_u numbuf[NUMBUFLEN];
1454
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001455 c1 = *p;
1456 *p = NUL;
1457
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001458 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags,
1459 scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001460 if ((opt_type == gov_bool
1461 || opt_type == gov_number
1462 || opt_type == gov_hidden_bool
1463 || opt_type == gov_hidden_number)
1464 && (tv->v_type != VAR_STRING || !in_vim9script()))
1465 {
1466 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1467 // bool, possibly hidden
1468 n = (long)tv_get_bool(tv);
1469 else
1470 // number, possibly hidden
1471 n = (long)tv_get_number(tv);
1472 }
1473
Bram Moolenaaref082e12021-12-12 21:02:03 +00001474 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001475 || tv->v_type == VAR_FUNC))
1476 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001477 // If the option can be set to a function reference or a lambda
1478 // and the passed value is a function reference, then convert it to
1479 // the name (string) of the function reference.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001480 s = tv2string(tv, &tofree, numbuf, 0);
1481 }
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001482 // Avoid setting a string option to the text "v:false" or similar.
1483 // In Vim9 script also don't convert a number to string.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001484 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001485 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1486 s = tv_get_string_chk(tv);
1487
1488 if (op != NULL && *op != '=')
1489 {
1490 if (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1491 || (opt_type == gov_string && *op != '.'))
1492 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001493 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001494 failed = TRUE; // don't set the value
1495
1496 }
1497 else
1498 {
1499 // number, in legacy script also bool
1500 if (opt_type == gov_number
1501 || (opt_type == gov_bool && !in_vim9script()))
1502 {
1503 switch (*op)
1504 {
1505 case '+': n = numval + n; break;
1506 case '-': n = numval - n; break;
1507 case '*': n = numval * n; break;
1508 case '/': n = (long)num_divide(numval, n,
1509 &failed); break;
1510 case '%': n = (long)num_modulus(numval, n,
1511 &failed); break;
1512 }
1513 s = NULL;
1514 }
1515 else if (opt_type == gov_string
1516 && stringval != NULL && s != NULL)
1517 {
1518 // string
1519 s = concat_str(stringval, s);
1520 vim_free(stringval);
1521 stringval = s;
1522 }
1523 }
1524 }
1525
1526 if (!failed)
1527 {
1528 if (opt_type != gov_string || s != NULL)
1529 {
Bram Moolenaar8ccbbeb2022-03-02 19:49:38 +00001530 char *err = set_option_value(arg, n, s, scope);
1531
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001532 arg_end = p;
Bram Moolenaar8ccbbeb2022-03-02 19:49:38 +00001533 if (err != NULL)
1534 emsg(_(err));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001535 }
1536 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001537 emsg(_(e_string_required));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001538 }
1539 *p = c1;
1540 vim_free(stringval);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001541 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001542 }
1543 return arg_end;
1544}
1545
1546/*
1547 * Set a register, part of ex_let_one().
1548 */
1549 static char_u *
1550ex_let_register(
1551 char_u *arg,
1552 typval_T *tv,
1553 int flags,
1554 char_u *endchars,
1555 char_u *op)
1556{
1557 char_u *arg_end = NULL;
1558
1559 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1560 && (flags & ASSIGN_FOR_LOOP) == 0)
1561 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001562 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001563 return NULL;
1564 }
1565 ++arg;
1566 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001567 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001568 else if (endchars != NULL
1569 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1570 emsg(_(e_unexpected_characters_in_let));
1571 else
1572 {
1573 char_u *ptofree = NULL;
1574 char_u *p;
1575
1576 p = tv_get_string_chk(tv);
1577 if (p != NULL && op != NULL && *op == '.')
1578 {
1579 char_u *s = get_reg_contents(*arg == '@'
1580 ? '"' : *arg, GREG_EXPR_SRC);
1581
1582 if (s != NULL)
1583 {
1584 p = ptofree = concat_str(s, p);
1585 vim_free(s);
1586 }
1587 }
1588 if (p != NULL)
1589 {
1590 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1591 arg_end = arg + 1;
1592 }
1593 vim_free(ptofree);
1594 }
1595 return arg_end;
1596}
1597
1598/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001599 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1600 * Returns a pointer to the char just after the var name.
1601 * Returns NULL if there is an error.
1602 */
1603 static char_u *
1604ex_let_one(
1605 char_u *arg, // points to variable name
1606 typval_T *tv, // value to assign to variable
1607 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001608 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001609 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001610 char_u *op, // "+", "-", "." or NULL
1611 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001612{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001613 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001614
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001615 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001616 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001617 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1618 {
1619 vim9_declare_error(arg);
1620 return NULL;
1621 }
1622
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001623 if (*arg == '$')
1624 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001625 // ":let $VAR = expr": Set environment variable.
1626 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001627 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001628 else if (*arg == '&')
1629 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001630 // ":let &option = expr": Set option value.
1631 // ":let &l:option = expr": Set local option value.
1632 // ":let &g:option = expr": Set global option value.
1633 // ":for &ts in range(8)": Set option value for for loop
1634 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001635 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001636 else if (*arg == '@')
1637 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001638 // ":let @r = expr": Set register contents.
1639 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001640 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001641 else if (eval_isnamec1(*arg) || *arg == '{')
1642 {
1643 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001644 char_u *p;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001645
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001646 // ":let var = expr": Set internal variable.
1647 // ":let var: type = expr": Set internal variable with type.
1648 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001649 p = get_lval(arg, tv, &lv, FALSE, FALSE,
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001650 (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1651 ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001652 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001653 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654 if (endchars != NULL && vim_strchr(endchars,
1655 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001656 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001657 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001658 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001659 else
1660 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001661 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001662 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001663 }
1664 }
1665 clear_lval(&lv);
1666 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001667 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001668 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001669
1670 return arg_end;
1671}
1672
1673/*
1674 * ":unlet[!] var1 ... " command.
1675 */
1676 void
1677ex_unlet(exarg_T *eap)
1678{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001679 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001680}
1681
1682/*
1683 * ":lockvar" and ":unlockvar" commands
1684 */
1685 void
1686ex_lockvar(exarg_T *eap)
1687{
1688 char_u *arg = eap->arg;
1689 int deep = 2;
1690
1691 if (eap->forceit)
1692 deep = -1;
1693 else if (vim_isdigit(*arg))
1694 {
1695 deep = getdigits(&arg);
1696 arg = skipwhite(arg);
1697 }
1698
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001699 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001700}
1701
1702/*
1703 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001704 * Also used for Vim9 script. "callback" is invoked as:
1705 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001706 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001707 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001708ex_unletlock(
1709 exarg_T *eap,
1710 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001711 int deep,
1712 int glv_flags,
1713 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1714 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001715{
1716 char_u *arg = argstart;
1717 char_u *name_end;
1718 int error = FALSE;
1719 lval_T lv;
1720
1721 do
1722 {
1723 if (*arg == '$')
1724 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001725 lv.ll_name = arg;
1726 lv.ll_tv = NULL;
1727 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001728 if (get_env_len(&arg) == 0)
1729 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001730 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001731 return;
1732 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001733 if (!error && !eap->skip
1734 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1735 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001736 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001737 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001738 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001739 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001740 // Parse the name and find the end.
1741 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001742 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001743 if (lv.ll_name == NULL)
1744 error = TRUE; // error but continue parsing
1745 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1746 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001747 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001748 if (name_end != NULL)
1749 {
1750 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00001751 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001752 }
1753 if (!(eap->skip || error))
1754 clear_lval(&lv);
1755 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001756 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001757
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001758 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001759 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001760 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001761
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001762 if (!eap->skip)
1763 clear_lval(&lv);
1764 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001765
1766 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001767 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001768
Bram Moolenaar63b91732021-08-05 20:40:03 +02001769 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001770}
1771
1772 static int
1773do_unlet_var(
1774 lval_T *lp,
1775 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001776 exarg_T *eap,
1777 int deep UNUSED,
1778 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001779{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001780 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001781 int ret = OK;
1782 int cc;
1783
1784 if (lp->ll_tv == NULL)
1785 {
1786 cc = *name_end;
1787 *name_end = NUL;
1788
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001789 // Environment variable, normal name or expanded name.
1790 if (*lp->ll_name == '$')
1791 vim_unsetenv(lp->ll_name + 1);
1792 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001793 ret = FAIL;
1794 *name_end = cc;
1795 }
1796 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001797 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001798 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001799 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001800 return FAIL;
1801 else if (lp->ll_range)
1802 {
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001803 if (list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_name, lp->ll_n1,
1804 !lp->ll_empty2, lp->ll_n2) == FAIL)
1805 return FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001806 }
1807 else
1808 {
1809 if (lp->ll_list != NULL)
1810 // unlet a List item.
1811 listitem_remove(lp->ll_list, lp->ll_li);
1812 else
1813 // unlet a Dictionary item.
1814 dictitem_remove(lp->ll_dict, lp->ll_di);
1815 }
1816
1817 return ret;
1818}
1819
1820/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001821 * Unlet one item or a range of items from a list.
1822 * Return OK or FAIL.
1823 */
1824 int
1825list_unlet_range(
1826 list_T *l,
1827 listitem_T *li_first,
1828 char_u *name,
1829 long n1_arg,
1830 int has_n2,
1831 long n2)
1832{
1833 listitem_T *li = li_first;
1834 int n1 = n1_arg;
1835
1836 while (li != NULL && (!has_n2 || n2 >= n1))
1837 {
1838 if (value_check_lock(li->li_tv.v_lock, name, FALSE))
1839 return FAIL;
1840 li = li->li_next;
1841 ++n1;
1842 }
1843
1844 // Delete a range of List items.
1845 li = li_first;
1846 n1 = n1_arg;
1847 while (li != NULL && (!has_n2 || n2 >= n1))
1848 {
1849 listitem_T *next = li->li_next;
1850
1851 listitem_remove(l, li);
1852 li = next;
1853 ++n1;
1854 }
1855 return OK;
1856}
1857/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001858 * "unlet" a variable. Return OK if it existed, FAIL if not.
1859 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1860 */
1861 int
1862do_unlet(char_u *name, int forceit)
1863{
1864 hashtab_T *ht;
1865 hashitem_T *hi;
1866 char_u *varname;
1867 dict_T *d;
1868 dictitem_T *di;
1869
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001870 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001871 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001872 return FAIL;
1873
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001874 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001875
1876 // can't :unlet a script variable in Vim9 script from a function
1877 if (ht == get_script_local_ht()
1878 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1879 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1880 == SCRIPT_VERSION_VIM9
1881 && check_vim9_unlet(name) == FAIL)
1882 return FAIL;
1883
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001884 if (ht != NULL && *varname != NUL)
1885 {
1886 d = get_current_funccal_dict(ht);
1887 if (d == NULL)
1888 {
1889 if (ht == &globvarht)
1890 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001891 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001892 d = &vimvardict;
1893 else
1894 {
1895 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1896 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1897 }
1898 if (d == NULL)
1899 {
1900 internal_error("do_unlet()");
1901 return FAIL;
1902 }
1903 }
1904 hi = hash_find(ht, varname);
1905 if (HASHITEM_EMPTY(hi))
1906 hi = find_hi_in_scoped_ht(name, &ht);
1907 if (hi != NULL && !HASHITEM_EMPTY(hi))
1908 {
1909 di = HI2DI(hi);
1910 if (var_check_fixed(di->di_flags, name, FALSE)
1911 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001912 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001913 return FAIL;
1914
1915 delete_var(ht, hi);
1916 return OK;
1917 }
1918 }
1919 if (forceit)
1920 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00001921 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001922 return FAIL;
1923}
1924
1925/*
1926 * Lock or unlock variable indicated by "lp".
1927 * "deep" is the levels to go (-1 for unlimited);
1928 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1929 */
1930 static int
1931do_lock_var(
1932 lval_T *lp,
1933 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001934 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001935 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001936 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001937{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001938 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001939 int ret = OK;
1940 int cc;
1941 dictitem_T *di;
1942
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001943 if (lp->ll_tv == NULL)
1944 {
1945 cc = *name_end;
1946 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001947 if (*lp->ll_name == '$')
1948 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001949 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001950 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001951 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001952 else
1953 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001954 // Normal name or expanded name.
1955 di = find_var(lp->ll_name, NULL, TRUE);
1956 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001957 {
1958 if (in_vim9script())
1959 semsg(_(e_cannot_find_variable_to_unlock_str),
1960 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001961 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001962 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001963 else if ((di->di_flags & DI_FLAGS_FIX)
1964 && di->di_tv.v_type != VAR_DICT
1965 && di->di_tv.v_type != VAR_LIST)
1966 {
1967 // For historic reasons this error is not given for a list or
1968 // dict. E.g., the b: dict could be locked/unlocked.
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001969 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001970 ret = FAIL;
1971 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001972 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001973 {
1974 if (lock)
1975 di->di_flags |= DI_FLAGS_LOCK;
1976 else
1977 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001978 if (deep != 0)
1979 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001980 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001981 }
1982 *name_end = cc;
1983 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001984 else if (deep == 0)
1985 {
1986 // nothing to do
1987 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001988 else if (lp->ll_range)
1989 {
1990 listitem_T *li = lp->ll_li;
1991
1992 // (un)lock a range of List items.
1993 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1994 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001995 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001996 li = li->li_next;
1997 ++lp->ll_n1;
1998 }
1999 }
2000 else if (lp->ll_list != NULL)
2001 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002002 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002003 else
2004 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002005 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002006
2007 return ret;
2008}
2009
2010/*
2011 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002012 * When "check_refcount" is TRUE do not lock a list or dict with a reference
2013 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002014 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002015 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02002016item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002017{
2018 static int recurse = 0;
2019 list_T *l;
2020 listitem_T *li;
2021 dict_T *d;
2022 blob_T *b;
2023 hashitem_T *hi;
2024 int todo;
2025
2026 if (recurse >= DICT_MAXNEST)
2027 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002028 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002029 return;
2030 }
2031 if (deep == 0)
2032 return;
2033 ++recurse;
2034
2035 // lock/unlock the item itself
2036 if (lock)
2037 tv->v_lock |= VAR_LOCKED;
2038 else
2039 tv->v_lock &= ~VAR_LOCKED;
2040
2041 switch (tv->v_type)
2042 {
2043 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002044 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002045 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002046 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002047 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002048 case VAR_STRING:
2049 case VAR_FUNC:
2050 case VAR_PARTIAL:
2051 case VAR_FLOAT:
2052 case VAR_SPECIAL:
2053 case VAR_JOB:
2054 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002055 case VAR_INSTR:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002056 break;
2057
2058 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002059 if ((b = tv->vval.v_blob) != NULL
2060 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002061 {
2062 if (lock)
2063 b->bv_lock |= VAR_LOCKED;
2064 else
2065 b->bv_lock &= ~VAR_LOCKED;
2066 }
2067 break;
2068 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002069 if ((l = tv->vval.v_list) != NULL
2070 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002071 {
2072 if (lock)
2073 l->lv_lock |= VAR_LOCKED;
2074 else
2075 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002076 if (deep < 0 || deep > 1)
2077 {
2078 if (l->lv_first == &range_list_item)
2079 l->lv_lock |= VAR_ITEMS_LOCKED;
2080 else
2081 {
2082 // recursive: lock/unlock the items the List contains
2083 CHECK_LIST_MATERIALIZE(l);
2084 FOR_ALL_LIST_ITEMS(l, li) item_lock(&li->li_tv,
2085 deep - 1, lock, check_refcount);
2086 }
2087 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002088 }
2089 break;
2090 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002091 if ((d = tv->vval.v_dict) != NULL
2092 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002093 {
2094 if (lock)
2095 d->dv_lock |= VAR_LOCKED;
2096 else
2097 d->dv_lock &= ~VAR_LOCKED;
2098 if (deep < 0 || deep > 1)
2099 {
2100 // recursive: lock/unlock the items the List contains
2101 todo = (int)d->dv_hashtab.ht_used;
2102 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2103 {
2104 if (!HASHITEM_EMPTY(hi))
2105 {
2106 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002107 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2108 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002109 }
2110 }
2111 }
2112 }
2113 }
2114 --recurse;
2115}
2116
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002117#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2118/*
2119 * Delete all "menutrans_" variables.
2120 */
2121 void
2122del_menutrans_vars(void)
2123{
2124 hashitem_T *hi;
2125 int todo;
2126
2127 hash_lock(&globvarht);
2128 todo = (int)globvarht.ht_used;
2129 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2130 {
2131 if (!HASHITEM_EMPTY(hi))
2132 {
2133 --todo;
2134 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2135 delete_var(&globvarht, hi);
2136 }
2137 }
2138 hash_unlock(&globvarht);
2139}
2140#endif
2141
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002142/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002143 * Local string buffer for the next two functions to store a variable name
2144 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2145 * get_user_var_name().
2146 */
2147
2148static char_u *varnamebuf = NULL;
2149static int varnamebuflen = 0;
2150
2151/*
2152 * Function to concatenate a prefix and a variable name.
2153 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002154 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002155cat_prefix_varname(int prefix, char_u *name)
2156{
2157 int len;
2158
2159 len = (int)STRLEN(name) + 3;
2160 if (len > varnamebuflen)
2161 {
2162 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002163 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002164 varnamebuf = alloc(len);
2165 if (varnamebuf == NULL)
2166 {
2167 varnamebuflen = 0;
2168 return NULL;
2169 }
2170 varnamebuflen = len;
2171 }
2172 *varnamebuf = prefix;
2173 varnamebuf[1] = ':';
2174 STRCPY(varnamebuf + 2, name);
2175 return varnamebuf;
2176}
2177
2178/*
2179 * Function given to ExpandGeneric() to obtain the list of user defined
2180 * (global/buffer/window/built-in) variable names.
2181 */
2182 char_u *
2183get_user_var_name(expand_T *xp, int idx)
2184{
2185 static long_u gdone;
2186 static long_u bdone;
2187 static long_u wdone;
2188 static long_u tdone;
2189 static int vidx;
2190 static hashitem_T *hi;
2191 hashtab_T *ht;
2192
2193 if (idx == 0)
2194 {
2195 gdone = bdone = wdone = vidx = 0;
2196 tdone = 0;
2197 }
2198
2199 // Global variables
2200 if (gdone < globvarht.ht_used)
2201 {
2202 if (gdone++ == 0)
2203 hi = globvarht.ht_array;
2204 else
2205 ++hi;
2206 while (HASHITEM_EMPTY(hi))
2207 ++hi;
2208 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2209 return cat_prefix_varname('g', hi->hi_key);
2210 return hi->hi_key;
2211 }
2212
2213 // b: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002214 ht = &prevwin_curwin()->w_buffer->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002215 if (bdone < ht->ht_used)
2216 {
2217 if (bdone++ == 0)
2218 hi = ht->ht_array;
2219 else
2220 ++hi;
2221 while (HASHITEM_EMPTY(hi))
2222 ++hi;
2223 return cat_prefix_varname('b', hi->hi_key);
2224 }
2225
2226 // w: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002227 ht = &prevwin_curwin()->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002228 if (wdone < ht->ht_used)
2229 {
2230 if (wdone++ == 0)
2231 hi = ht->ht_array;
2232 else
2233 ++hi;
2234 while (HASHITEM_EMPTY(hi))
2235 ++hi;
2236 return cat_prefix_varname('w', hi->hi_key);
2237 }
2238
2239 // t: variables
2240 ht = &curtab->tp_vars->dv_hashtab;
2241 if (tdone < ht->ht_used)
2242 {
2243 if (tdone++ == 0)
2244 hi = ht->ht_array;
2245 else
2246 ++hi;
2247 while (HASHITEM_EMPTY(hi))
2248 ++hi;
2249 return cat_prefix_varname('t', hi->hi_key);
2250 }
2251
2252 // v: variables
2253 if (vidx < VV_LEN)
2254 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2255
2256 VIM_CLEAR(varnamebuf);
2257 varnamebuflen = 0;
2258 return NULL;
2259}
2260
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002261 char *
2262get_var_special_name(int nr)
2263{
2264 switch (nr)
2265 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002266 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2267 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002268 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002269 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002270 }
2271 internal_error("get_var_special_name()");
2272 return "42";
2273}
2274
2275/*
2276 * Returns the global variable dictionary
2277 */
2278 dict_T *
2279get_globvar_dict(void)
2280{
2281 return &globvardict;
2282}
2283
2284/*
2285 * Returns the global variable hash table
2286 */
2287 hashtab_T *
2288get_globvar_ht(void)
2289{
2290 return &globvarht;
2291}
2292
2293/*
2294 * Returns the v: variable dictionary
2295 */
2296 dict_T *
2297get_vimvar_dict(void)
2298{
2299 return &vimvardict;
2300}
2301
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002302/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002304 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305 */
2306 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002307find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002309 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2310 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311
2312 if (di == NULL)
2313 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002314 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002315 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2316 return (int)(vv - vimvars);
2317}
2318
2319
2320/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002321 * Set type of v: variable to "type".
2322 */
2323 void
2324set_vim_var_type(int idx, vartype_T type)
2325{
Bram Moolenaard787e402021-12-24 21:36:12 +00002326 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002327}
2328
2329/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002330 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002331 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002332 */
2333 void
2334set_vim_var_nr(int idx, varnumber_T val)
2335{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002336 vimvars[idx].vv_nr = val;
2337}
2338
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339 char *
2340get_vim_var_name(int idx)
2341{
2342 return vimvars[idx].vv_name;
2343}
2344
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002345/*
2346 * Get typval_T v: variable value.
2347 */
2348 typval_T *
2349get_vim_var_tv(int idx)
2350{
2351 return &vimvars[idx].vv_tv;
2352}
2353
Bram Moolenaard787e402021-12-24 21:36:12 +00002354 type_T *
2355get_vim_var_type(int idx, garray_T *type_list)
2356{
2357 if (vimvars[idx].vv_type != NULL)
2358 return vimvars[idx].vv_type;
2359 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2360}
2361
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002362/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002363 * Set v: variable to "tv". Only accepts the same type.
2364 * Takes over the value of "tv".
2365 */
2366 int
2367set_vim_var_tv(int idx, typval_T *tv)
2368{
Bram Moolenaard787e402021-12-24 21:36:12 +00002369 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002370 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002371 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002372 clear_tv(tv);
2373 return FAIL;
2374 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002375 // VV_RO is also checked when compiling, but let's check here as well.
2376 if (vimvars[idx].vv_flags & VV_RO)
2377 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002378 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002379 return FAIL;
2380 }
2381 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2382 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002383 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002384 return FAIL;
2385 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002386 clear_tv(&vimvars[idx].vv_di.di_tv);
2387 vimvars[idx].vv_di.di_tv = *tv;
2388 return OK;
2389}
2390
2391/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002392 * Get number v: variable value.
2393 */
2394 varnumber_T
2395get_vim_var_nr(int idx)
2396{
2397 return vimvars[idx].vv_nr;
2398}
2399
2400/*
2401 * Get string v: variable value. Uses a static buffer, can only be used once.
2402 * If the String variable has never been set, return an empty string.
2403 * Never returns NULL;
2404 */
2405 char_u *
2406get_vim_var_str(int idx)
2407{
2408 return tv_get_string(&vimvars[idx].vv_tv);
2409}
2410
2411/*
2412 * Get List v: variable value. Caller must take care of reference count when
2413 * needed.
2414 */
2415 list_T *
2416get_vim_var_list(int idx)
2417{
2418 return vimvars[idx].vv_list;
2419}
2420
2421/*
2422 * Get Dict v: variable value. Caller must take care of reference count when
2423 * needed.
2424 */
2425 dict_T *
2426get_vim_var_dict(int idx)
2427{
2428 return vimvars[idx].vv_dict;
2429}
2430
2431/*
2432 * Set v:char to character "c".
2433 */
2434 void
2435set_vim_var_char(int c)
2436{
2437 char_u buf[MB_MAXBYTES + 1];
2438
2439 if (has_mbyte)
2440 buf[(*mb_char2bytes)(c, buf)] = NUL;
2441 else
2442 {
2443 buf[0] = c;
2444 buf[1] = NUL;
2445 }
2446 set_vim_var_string(VV_CHAR, buf, -1);
2447}
2448
2449/*
2450 * Set v:count to "count" and v:count1 to "count1".
2451 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2452 */
2453 void
2454set_vcount(
2455 long count,
2456 long count1,
2457 int set_prevcount)
2458{
2459 if (set_prevcount)
2460 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2461 vimvars[VV_COUNT].vv_nr = count;
2462 vimvars[VV_COUNT1].vv_nr = count1;
2463}
2464
2465/*
2466 * Save variables that might be changed as a side effect. Used when executing
2467 * a timer callback.
2468 */
2469 void
2470save_vimvars(vimvars_save_T *vvsave)
2471{
2472 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2473 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2474 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2475}
2476
2477/*
2478 * Restore variables saved by save_vimvars().
2479 */
2480 void
2481restore_vimvars(vimvars_save_T *vvsave)
2482{
2483 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2484 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2485 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2486}
2487
2488/*
2489 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2490 * value.
2491 */
2492 void
2493set_vim_var_string(
2494 int idx,
2495 char_u *val,
2496 int len) // length of "val" to use or -1 (whole string)
2497{
2498 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002499 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002500 if (val == NULL)
2501 vimvars[idx].vv_str = NULL;
2502 else if (len == -1)
2503 vimvars[idx].vv_str = vim_strsave(val);
2504 else
2505 vimvars[idx].vv_str = vim_strnsave(val, len);
2506}
2507
2508/*
2509 * Set List v: variable to "val".
2510 */
2511 void
2512set_vim_var_list(int idx, list_T *val)
2513{
2514 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002515 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002516 vimvars[idx].vv_list = val;
2517 if (val != NULL)
2518 ++val->lv_refcount;
2519}
2520
2521/*
2522 * Set Dictionary v: variable to "val".
2523 */
2524 void
2525set_vim_var_dict(int idx, dict_T *val)
2526{
2527 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002528 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002529 vimvars[idx].vv_dict = val;
2530 if (val != NULL)
2531 {
2532 ++val->dv_refcount;
2533 dict_set_items_ro(val);
2534 }
2535}
2536
2537/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002538 * Set the v:argv list.
2539 */
2540 void
2541set_argv_var(char **argv, int argc)
2542{
2543 list_T *l = list_alloc();
2544 int i;
2545
2546 if (l == NULL)
2547 getout(1);
2548 l->lv_lock = VAR_FIXED;
2549 for (i = 0; i < argc; ++i)
2550 {
2551 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2552 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002553 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002554 }
2555 set_vim_var_list(VV_ARGV, l);
2556}
2557
2558/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002559 * Reset v:register, taking the 'clipboard' setting into account.
2560 */
2561 void
2562reset_reg_var(void)
2563{
2564 int regname = 0;
2565
2566 // Adjust the register according to 'clipboard', so that when
2567 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2568#ifdef FEAT_CLIPBOARD
2569 adjust_clip_reg(&regname);
2570#endif
2571 set_reg_var(regname);
2572}
2573
2574/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002575 * Set v:register if needed.
2576 */
2577 void
2578set_reg_var(int c)
2579{
2580 char_u regname;
2581
2582 if (c == 0 || c == ' ')
2583 regname = '"';
2584 else
2585 regname = c;
2586 // Avoid free/alloc when the value is already right.
2587 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2588 set_vim_var_string(VV_REG, &regname, 1);
2589}
2590
2591/*
2592 * Get or set v:exception. If "oldval" == NULL, return the current value.
2593 * Otherwise, restore the value to "oldval" and return NULL.
2594 * Must always be called in pairs to save and restore v:exception! Does not
2595 * take care of memory allocations.
2596 */
2597 char_u *
2598v_exception(char_u *oldval)
2599{
2600 if (oldval == NULL)
2601 return vimvars[VV_EXCEPTION].vv_str;
2602
2603 vimvars[VV_EXCEPTION].vv_str = oldval;
2604 return NULL;
2605}
2606
2607/*
2608 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2609 * Otherwise, restore the value to "oldval" and return NULL.
2610 * Must always be called in pairs to save and restore v:throwpoint! Does not
2611 * take care of memory allocations.
2612 */
2613 char_u *
2614v_throwpoint(char_u *oldval)
2615{
2616 if (oldval == NULL)
2617 return vimvars[VV_THROWPOINT].vv_str;
2618
2619 vimvars[VV_THROWPOINT].vv_str = oldval;
2620 return NULL;
2621}
2622
2623/*
2624 * Set v:cmdarg.
2625 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2626 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2627 * Must always be called in pairs!
2628 */
2629 char_u *
2630set_cmdarg(exarg_T *eap, char_u *oldarg)
2631{
2632 char_u *oldval;
2633 char_u *newval;
2634 unsigned len;
2635
2636 oldval = vimvars[VV_CMDARG].vv_str;
2637 if (eap == NULL)
2638 {
2639 vim_free(oldval);
2640 vimvars[VV_CMDARG].vv_str = oldarg;
2641 return NULL;
2642 }
2643
2644 if (eap->force_bin == FORCE_BIN)
2645 len = 6;
2646 else if (eap->force_bin == FORCE_NOBIN)
2647 len = 8;
2648 else
2649 len = 0;
2650
2651 if (eap->read_edit)
2652 len += 7;
2653
2654 if (eap->force_ff != 0)
2655 len += 10; // " ++ff=unix"
2656 if (eap->force_enc != 0)
2657 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2658 if (eap->bad_char != 0)
2659 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2660
2661 newval = alloc(len + 1);
2662 if (newval == NULL)
2663 return NULL;
2664
2665 if (eap->force_bin == FORCE_BIN)
2666 sprintf((char *)newval, " ++bin");
2667 else if (eap->force_bin == FORCE_NOBIN)
2668 sprintf((char *)newval, " ++nobin");
2669 else
2670 *newval = NUL;
2671
2672 if (eap->read_edit)
2673 STRCAT(newval, " ++edit");
2674
2675 if (eap->force_ff != 0)
2676 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2677 eap->force_ff == 'u' ? "unix"
2678 : eap->force_ff == 'd' ? "dos"
2679 : "mac");
2680 if (eap->force_enc != 0)
2681 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2682 eap->cmd + eap->force_enc);
2683 if (eap->bad_char == BAD_KEEP)
2684 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2685 else if (eap->bad_char == BAD_DROP)
2686 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2687 else if (eap->bad_char != 0)
2688 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2689 vimvars[VV_CMDARG].vv_str = newval;
2690 return oldval;
2691}
2692
2693/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002694 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002695 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2696 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002697 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2698 */
2699 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002700eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002701 char_u *name,
2702 int len, // length of "name"
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002703 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002704 typval_T *rettv, // NULL when only checking existence
2705 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002706 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002707{
2708 int ret = OK;
2709 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002710 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002711 hashtab_T *ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002712 int cc;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002713 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002714
2715 // truncate the name, so that we can use strcmp()
2716 cc = name[len];
2717 name[len] = NUL;
2718
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002719 // Check for local variable when debugging.
2720 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002721 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002722 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002723 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2724
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002725 if (v != NULL)
2726 {
2727 tv = &v->di_tv;
2728 if (dip != NULL)
2729 *dip = v;
2730 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002731 else
2732 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002733 }
2734
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002735 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002736 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002737 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002738 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2739
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002740 if (sid == 0)
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00002741 import = find_imported(p, 0, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742
2743 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002744 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002746 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002747 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00002748 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02002749 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002750 ht = &SCRIPT_VARS(sid);
2751 if (ht != NULL)
2752 {
2753 dictitem_T *v = find_var_in_ht(ht, 0, name,
2754 flags & EVAL_VAR_NOAUTOLOAD);
2755
2756 if (v != NULL)
2757 {
2758 tv = &v->di_tv;
2759 if (dip != NULL)
2760 *dip = v;
2761 }
2762 else
2763 ht = NULL;
2764 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002765 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002766 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002767 {
2768 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00002769 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002770 ret = FAIL;
2771 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002772 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002773 else
2774 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002775 if (rettv != NULL)
2776 {
2777 rettv->v_type = VAR_ANY;
2778 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
2779 }
2780 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002781 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00002783 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002784 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002785 int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002786 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002787
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002788 // In Vim9 script we can get a function reference by using the
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002789 // function name. For a global non-autoload function "g:" is
2790 // required.
2791 if (ufunc != NULL && (has_g_prefix
2792 || !func_requires_g_prefix(ufunc)))
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002793 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002794 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002795 if (rettv != NULL)
2796 {
2797 rettv->v_type = VAR_FUNC;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002798 if (has_g_prefix)
Bram Moolenaaref082e12021-12-12 21:02:03 +00002799 // Keep the "g:", otherwise script-local may be
2800 // assumed.
2801 rettv->vval.v_string = vim_strsave(name);
2802 else
2803 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002804 if (rettv->vval.v_string != NULL)
2805 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002806 }
2807 }
2808 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002809 }
2810
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002811 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002812 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002813 if (tv == NULL)
2814 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002815 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002816 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002817 ret = FAIL;
2818 }
2819 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002820 {
Bram Moolenaar11005b02021-07-11 20:59:00 +02002821 if (ht != NULL && ht == get_script_local_ht()
2822 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002823 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002824 svar_T *sv = find_typval_in_script(tv, 0);
Bram Moolenaarf055d452021-07-08 20:57:24 +02002825
Bram Moolenaarf055d452021-07-08 20:57:24 +02002826 if (sv != NULL)
2827 type = sv->sv_type;
2828 }
2829
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002830 // If a list or dict variable wasn't initialized, do it now.
Bram Moolenaar7a222242022-03-01 19:23:24 +00002831 // Not for global variables, they are not declared.
2832 if (ht != &globvarht)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002833 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00002834 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002835 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00002836 tv->vval.v_dict = dict_alloc();
2837 if (tv->vval.v_dict != NULL)
2838 {
2839 ++tv->vval.v_dict->dv_refcount;
2840 tv->vval.v_dict->dv_type = alloc_type(type);
2841 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02002842 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00002843 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002844 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00002845 tv->vval.v_list = list_alloc();
2846 if (tv->vval.v_list != NULL)
2847 {
2848 ++tv->vval.v_list->lv_refcount;
2849 tv->vval.v_list->lv_type = alloc_type(type);
2850 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02002851 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00002852 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL)
2853 {
2854 tv->vval.v_blob = blob_alloc();
2855 if (tv->vval.v_blob != NULL)
2856 ++tv->vval.v_blob->bv_refcount;
2857 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002858 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002859 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002860 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002861 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002862
2863 name[len] = cc;
2864
2865 return ret;
2866}
2867
2868/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002869 * Check if variable "name[len]" is a local variable or an argument.
2870 * If so, "*eval_lavars_used" is set to TRUE.
2871 */
2872 void
2873check_vars(char_u *name, int len)
2874{
2875 int cc;
2876 char_u *varname;
2877 hashtab_T *ht;
2878
2879 if (eval_lavars_used == NULL)
2880 return;
2881
2882 // truncate the name, so that we can use strcmp()
2883 cc = name[len];
2884 name[len] = NUL;
2885
2886 ht = find_var_ht(name, &varname);
2887 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2888 {
2889 if (find_var(name, NULL, TRUE) != NULL)
2890 *eval_lavars_used = TRUE;
2891 }
2892
2893 name[len] = cc;
2894}
2895
2896/*
2897 * Find variable "name" in the list of variables.
2898 * Return a pointer to it if found, NULL if not found.
2899 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002900 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002901 */
2902 dictitem_T *
2903find_var(char_u *name, hashtab_T **htp, int no_autoload)
2904{
2905 char_u *varname;
2906 hashtab_T *ht;
2907 dictitem_T *ret = NULL;
2908
2909 ht = find_var_ht(name, &varname);
2910 if (htp != NULL)
2911 *htp = ht;
2912 if (ht == NULL)
2913 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002914 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002915 if (ret != NULL)
2916 return ret;
2917
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002918 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002919 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002920 if (ret != NULL)
2921 return ret;
2922
2923 // in Vim9 script items without a scope can be script-local
2924 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2925 {
2926 ht = get_script_local_ht();
2927 if (ht != NULL)
2928 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002929 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002930 if (ret != NULL)
2931 {
2932 if (htp != NULL)
2933 *htp = ht;
2934 return ret;
2935 }
2936 }
2937 }
2938
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002939 // When using "vim9script autoload" script-local items are prefixed but can
2940 // be used with s:name.
2941 if (SCRIPT_ID_VALID(current_sctx.sc_sid)
2942 && name[0] == 's' && name[1] == ':')
2943 {
2944 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2945
2946 if (si->sn_autoload_prefix != NULL)
2947 {
2948 char_u *auto_name = concat_str(si->sn_autoload_prefix, name + 2);
2949
2950 if (auto_name != NULL)
2951 {
2952 ht = &globvarht;
2953 ret = find_var_in_ht(ht, *name, auto_name, TRUE);
Bram Moolenaar130f65d2022-01-13 20:39:41 +00002954 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002955 if (ret != NULL)
2956 {
2957 if (htp != NULL)
2958 *htp = ht;
2959 return ret;
2960 }
2961 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002962 }
2963 }
2964
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002965 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002966}
2967
2968/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00002969 * Like find_var() but if the name starts with <SNR>99_ then look in the
2970 * referenced script (used for a funcref).
2971 */
2972 dictitem_T *
2973find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
2974{
2975 if (STRNCMP(name, "<SNR>", 5) == 0 && isdigit(name[5]))
2976 {
2977 char_u *p = name + 5;
2978 int sid = getdigits(&p);
2979
2980 if (SCRIPT_ID_VALID(sid) && *p == '_')
2981 {
2982 hashtab_T *ht = &SCRIPT_VARS(sid);
2983
2984 if (ht != NULL)
2985 {
2986 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
2987
2988 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00002989 {
2990 if (htp != NULL)
2991 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00002992 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00002993 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00002994 }
2995 }
2996 }
2997
2998 return find_var(name, htp, no_autoload);
2999}
3000
3001/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003002 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02003003 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003004 * Returns NULL if not found.
3005 */
3006 dictitem_T *
3007find_var_in_ht(
3008 hashtab_T *ht,
3009 int htname,
3010 char_u *varname,
3011 int no_autoload)
3012{
3013 hashitem_T *hi;
3014
3015 if (*varname == NUL)
3016 {
3017 // Must be something like "s:", otherwise "ht" would be NULL.
3018 switch (htname)
3019 {
3020 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
3021 case 'g': return &globvars_var;
3022 case 'v': return &vimvars_var;
3023 case 'b': return &curbuf->b_bufvar;
3024 case 'w': return &curwin->w_winvar;
3025 case 't': return &curtab->tp_winvar;
3026 case 'l': return get_funccal_local_var();
3027 case 'a': return get_funccal_args_var();
3028 }
3029 return NULL;
3030 }
3031
3032 hi = hash_find(ht, varname);
3033 if (HASHITEM_EMPTY(hi))
3034 {
3035 // For global variables we may try auto-loading the script. If it
3036 // worked find the variable again. Don't auto-load a script if it was
3037 // loaded already, otherwise it would be loaded every time when
3038 // checking if a function name is a Funcref variable.
3039 if (ht == &globvarht && !no_autoload)
3040 {
3041 // Note: script_autoload() may make "hi" invalid. It must either
3042 // be obtained again or not used.
3043 if (!script_autoload(varname, FALSE) || aborting())
3044 return NULL;
3045 hi = hash_find(ht, varname);
3046 }
3047 if (HASHITEM_EMPTY(hi))
3048 return NULL;
3049 }
3050 return HI2DI(hi);
3051}
3052
3053/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 * Get the script-local hashtab. NULL if not in a script context.
3055 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003056 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057get_script_local_ht(void)
3058{
3059 scid_T sid = current_sctx.sc_sid;
3060
Bram Moolenaare3d46852020-08-29 13:39:17 +02003061 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 return &SCRIPT_VARS(sid);
3063 return NULL;
3064}
3065
3066/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003067 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003068 * When "cmd" is TRUE it must look like a command, a function must be followed
3069 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003070 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003072 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003073lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003074 char_u *name,
3075 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003076 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003077 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078{
3079 hashtab_T *ht = get_script_local_ht();
3080 char_u buffer[30];
3081 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003082 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003084 int is_global = FALSE;
3085 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086
3087 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003088 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 if (len < sizeof(buffer) - 1)
3090 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003091 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092 vim_strncpy(buffer, name, len);
3093 p = buffer;
3094 }
3095 else
3096 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003097 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003099 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 }
3101
3102 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003103 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104
3105 // if not script-local, then perhaps imported
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003106 if (res == FAIL && find_imported(p, 0, FALSE) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003107 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 if (p != buffer)
3109 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003110
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003111 // Find a function, so that a following "->" works.
3112 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3113 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003114 if (res != OK)
3115 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003116 p = skipwhite(name + len);
3117
3118 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003119 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003120 // Do not check for an internal function, since it might also be a
3121 // valid command, such as ":split" versus "split()".
3122 // Skip "g:" before a function name.
3123 if (name[0] == 'g' && name[1] == ':')
3124 {
3125 is_global = TRUE;
3126 fname = name + 2;
3127 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003128 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003129 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003130 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003131 }
3132
Bram Moolenaar709664c2020-12-12 14:33:41 +01003133 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134}
3135
3136/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003137 * Find the hashtab used for a variable name.
3138 * Return NULL if the name is not valid.
3139 * Set "varname" to the start of name without ':'.
3140 */
3141 hashtab_T *
3142find_var_ht(char_u *name, char_u **varname)
3143{
3144 hashitem_T *hi;
3145 hashtab_T *ht;
3146
3147 if (name[0] == NUL)
3148 return NULL;
3149 if (name[1] != ':')
3150 {
3151 // The name must not start with a colon or #.
3152 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3153 return NULL;
3154 *varname = name;
3155
3156 // "version" is "v:version" in all scopes if scriptversion < 3.
3157 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003158 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003159 {
3160 hi = hash_find(&compat_hashtab, name);
3161 if (!HASHITEM_EMPTY(hi))
3162 return &compat_hashtab;
3163 }
3164
3165 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 if (ht != NULL)
3167 return ht; // local variable
3168
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003169 // In Vim9 script items at the script level are script-local, except
3170 // for autoload names.
3171 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 {
3173 ht = get_script_local_ht();
3174 if (ht != NULL)
3175 return ht;
3176 }
3177
3178 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003179 }
3180 *varname = name + 2;
3181 if (*name == 'g') // global variable
3182 return &globvarht;
3183 // There must be no ':' or '#' in the rest of the name, unless g: is used
3184 if (vim_strchr(name + 2, ':') != NULL
3185 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3186 return NULL;
3187 if (*name == 'b') // buffer variable
3188 return &curbuf->b_vars->dv_hashtab;
3189 if (*name == 'w') // window variable
3190 return &curwin->w_vars->dv_hashtab;
3191 if (*name == 't') // tab page variable
3192 return &curtab->tp_vars->dv_hashtab;
3193 if (*name == 'v') // v: variable
3194 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003195 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003196 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003198 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 if (*name == 'a') // a: function argument
3200 return get_funccal_args_ht();
3201 if (*name == 'l') // l: local function variable
3202 return get_funccal_local_ht();
3203 }
3204 if (*name == 's') // script variable
3205 {
3206 ht = get_script_local_ht();
3207 if (ht != NULL)
3208 return ht;
3209 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003210 return NULL;
3211}
3212
3213/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003214 * Get the string value of a (global/local) variable.
3215 * Note: see tv_get_string() for how long the pointer remains valid.
3216 * Returns NULL when it doesn't exist.
3217 */
3218 char_u *
3219get_var_value(char_u *name)
3220{
3221 dictitem_T *v;
3222
3223 v = find_var(name, NULL, FALSE);
3224 if (v == NULL)
3225 return NULL;
3226 return tv_get_string(&v->di_tv);
3227}
3228
3229/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003230 * Allocate a new hashtab for a sourced script. It will be used while
3231 * sourcing this script and when executing functions defined in the script.
3232 */
3233 void
3234new_script_vars(scid_T id)
3235{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003236 scriptvar_T *sv;
3237
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003238 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3239 if (sv == NULL)
3240 return;
3241 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003242 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003243}
3244
3245/*
3246 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3247 * point to it.
3248 */
3249 void
3250init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3251{
3252 hash_init(&dict->dv_hashtab);
3253 dict->dv_lock = 0;
3254 dict->dv_scope = scope;
3255 dict->dv_refcount = DO_NOT_FREE_CNT;
3256 dict->dv_copyID = 0;
3257 dict_var->di_tv.vval.v_dict = dict;
3258 dict_var->di_tv.v_type = VAR_DICT;
3259 dict_var->di_tv.v_lock = VAR_FIXED;
3260 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3261 dict_var->di_key[0] = NUL;
3262}
3263
3264/*
3265 * Unreference a dictionary initialized by init_var_dict().
3266 */
3267 void
3268unref_var_dict(dict_T *dict)
3269{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003270 // Now the dict needs to be freed if no one else is using it, go back to
3271 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003272 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3273 dict_unref(dict);
3274}
3275
3276/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003277 * Clean up a list of internal variables.
3278 * Frees all allocated variables and the value they contain.
3279 * Clears hashtab "ht", does not free it.
3280 */
3281 void
3282vars_clear(hashtab_T *ht)
3283{
3284 vars_clear_ext(ht, TRUE);
3285}
3286
3287/*
3288 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3289 */
3290 void
3291vars_clear_ext(hashtab_T *ht, int free_val)
3292{
3293 int todo;
3294 hashitem_T *hi;
3295 dictitem_T *v;
3296
3297 hash_lock(ht);
3298 todo = (int)ht->ht_used;
3299 for (hi = ht->ht_array; todo > 0; ++hi)
3300 {
3301 if (!HASHITEM_EMPTY(hi))
3302 {
3303 --todo;
3304
3305 // Free the variable. Don't remove it from the hashtab,
3306 // ht_array might change then. hash_clear() takes care of it
3307 // later.
3308 v = HI2DI(hi);
3309 if (free_val)
3310 clear_tv(&v->di_tv);
3311 if (v->di_flags & DI_FLAGS_ALLOC)
3312 vim_free(v);
3313 }
3314 }
3315 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003316 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003317}
3318
3319/*
3320 * Delete a variable from hashtab "ht" at item "hi".
3321 * Clear the variable value and free the dictitem.
3322 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003323 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003324delete_var(hashtab_T *ht, hashitem_T *hi)
3325{
3326 dictitem_T *di = HI2DI(hi);
3327
3328 hash_remove(ht, hi);
3329 clear_tv(&di->di_tv);
3330 vim_free(di);
3331}
3332
3333/*
3334 * List the value of one internal variable.
3335 */
3336 static void
3337list_one_var(dictitem_T *v, char *prefix, int *first)
3338{
3339 char_u *tofree;
3340 char_u *s;
3341 char_u numbuf[NUMBUFLEN];
3342
3343 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3344 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3345 s == NULL ? (char_u *)"" : s, first);
3346 vim_free(tofree);
3347}
3348
3349 static void
3350list_one_var_a(
3351 char *prefix,
3352 char_u *name,
3353 int type,
3354 char_u *string,
3355 int *first) // when TRUE clear rest of screen and set to FALSE
3356{
3357 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3358 msg_start();
3359 msg_puts(prefix);
3360 if (name != NULL) // "a:" vars don't have a name stored
3361 msg_puts((char *)name);
3362 msg_putchar(' ');
3363 msg_advance(22);
3364 if (type == VAR_NUMBER)
3365 msg_putchar('#');
3366 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3367 msg_putchar('*');
3368 else if (type == VAR_LIST)
3369 {
3370 msg_putchar('[');
3371 if (*string == '[')
3372 ++string;
3373 }
3374 else if (type == VAR_DICT)
3375 {
3376 msg_putchar('{');
3377 if (*string == '{')
3378 ++string;
3379 }
3380 else
3381 msg_putchar(' ');
3382
3383 msg_outtrans(string);
3384
3385 if (type == VAR_FUNC || type == VAR_PARTIAL)
3386 msg_puts("()");
3387 if (*first)
3388 {
3389 msg_clr_eos();
3390 *first = FALSE;
3391 }
3392}
3393
3394/*
3395 * Set variable "name" to value in "tv".
3396 * If the variable already exists, the value is updated.
3397 * Otherwise the variable is created.
3398 */
3399 void
3400set_var(
3401 char_u *name,
3402 typval_T *tv,
3403 int copy) // make copy of value in "tv"
3404{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003405 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003406}
3407
3408/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003409 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003410 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003411 * If the variable already exists and "is_const" is FALSE the value is updated.
3412 * Otherwise the variable is created.
3413 */
3414 void
3415set_var_const(
3416 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003417 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003418 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003419 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003420 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003421 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003422 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003423{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003424 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003425 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003426 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 dictitem_T *di;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003428 typval_T *dest_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003429 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003430 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003431 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003433 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003434 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003435 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003436 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003437 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003438
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003439 if (sid != 0)
3440 {
3441 if (SCRIPT_ID_VALID(sid))
3442 ht = &SCRIPT_VARS(sid);
3443 varname = name;
3444 }
3445 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003446 {
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003447 scriptitem_T *si;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003448
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003449 if (in_vim9script() && is_export
3450 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3451 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
Bram Moolenaar0e1574c2022-03-03 17:05:35 +00003452 ->sn_autoload_prefix != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003453 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003454 // In a vim9 autoload script an exported variable is put in the
3455 // global namespace with the autoload prefix.
3456 var_in_autoload = TRUE;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003457 varname = concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003458 if (varname == NULL)
3459 goto failed;
3460 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003461 ht = &globvarht;
3462 }
3463 else
3464 ht = find_var_ht(name, &varname);
3465 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003466 if (ht == NULL || *varname == NUL)
3467 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003468 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003469 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003470 }
Bram Moolenaar54969f42022-02-07 13:56:44 +00003471 is_script_local = ht == get_script_local_ht() || sid != 0
3472 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003473
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003474 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003475 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003476 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003477 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003478 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003479 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003480 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003481 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003482 }
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003483 if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
3484 && vim_strchr((char_u *)"gwbt", name[0]) != NULL)
3485 // Do not make g:var, w:var, b:var or t:var final.
3486 flags &= ~ASSIGN_FINAL;
3487
Bram Moolenaare535db82021-03-31 21:07:24 +02003488 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003489 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3490 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003491 // For "[a, _] = list" the underscore is ignored.
3492 if ((flags & ASSIGN_UNPACK) == 0)
3493 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003494 goto failed;
3495 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003496
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003498
Bram Moolenaar24e93162021-07-18 20:40:33 +02003499 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003500 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003501 imported_T *import = find_imported(varname, 0, FALSE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003502
Bram Moolenaar24e93162021-07-18 20:40:33 +02003503 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003504 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003505 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02003506 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003508 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003509 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003511 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3512 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003513 }
Bram Moolenaar75e27d72022-02-13 13:56:29 +00003514 if (!in_vim9script())
3515 {
3516 semsg(_(e_cannot_create_vim9_script_variable_in_function_str),
3517 name);
3518 goto failed;
3519 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003520 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521
Bram Moolenaar993faa32022-02-21 15:59:11 +00003522 // Search in parent scope which is possible to reference from lambda
3523 if (di == NULL)
3524 di = find_var_in_scoped_ht(name, TRUE);
3525
3526 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
3527 && var_wrong_func_name(name, di == NULL))
3528 goto failed;
3529
3530 if (need_convert_to_bool(type, tv))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003531 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003532 // Destination is a bool and the value is not, but it can be
3533 // converted.
3534 CLEAR_FIELD(bool_tv);
3535 bool_tv.v_type = VAR_BOOL;
3536 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3537 tv = &bool_tv;
3538 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003539
Bram Moolenaar993faa32022-02-21 15:59:11 +00003540 if (di != NULL)
3541 {
3542 // Item already exists. Allowed to replace when reloading.
3543 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003544 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003545 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
3546 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003547 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003548 emsg(_(e_cannot_modify_existing_variable));
3549 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003550 }
3551
Bram Moolenaar993faa32022-02-21 15:59:11 +00003552 if (is_script_local && vim9script
3553 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003554 {
3555 semsg(_(e_redefining_script_item_str), name);
3556 goto failed;
3557 }
3558
Bram Moolenaar993faa32022-02-21 15:59:11 +00003559 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003560 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003561 where_T where = WHERE_INIT;
3562 svar_T *sv = find_typval_in_script(&di->di_tv, sid);
3563
3564 if (sv != NULL)
3565 {
3566 // check the type and adjust to bool if needed
3567 where.wt_index = var_idx;
3568 where.wt_variable = TRUE;
3569 if (check_script_var_type(sv, tv, name, where) == FAIL)
3570 goto failed;
3571 if (type == NULL)
3572 type = sv->sv_type;
3573 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003574 }
3575
Bram Moolenaar993faa32022-02-21 15:59:11 +00003576 if ((flags & ASSIGN_FOR_LOOP) == 0
3577 && var_check_permission(di, name) == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003578 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003579 }
3580 else
3581 {
3582 // can only redefine once
3583 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003584
Bram Moolenaar993faa32022-02-21 15:59:11 +00003585 // A Vim9 script-local variable is also present in sn_all_vars
3586 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003587 if (var_in_vim9script || var_in_autoload)
Bram Moolenaar993faa32022-02-21 15:59:11 +00003588 update_vim9_script_var(FALSE, di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003589 var_in_autoload ? name : di->di_key, flags,
Bram Moolenaar993faa32022-02-21 15:59:11 +00003590 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003591 }
3592
Bram Moolenaar993faa32022-02-21 15:59:11 +00003593 // existing variable, need to clear the value
3594
3595 // Handle setting internal di: variables separately where needed to
3596 // prevent changing the type.
3597 if (ht == &vimvarht)
3598 {
3599 if (di->di_tv.v_type == VAR_STRING)
3600 {
3601 VIM_CLEAR(di->di_tv.vval.v_string);
3602 if (copy || tv->v_type != VAR_STRING)
3603 {
3604 char_u *val = tv_get_string(tv);
3605
3606 // Careful: when assigning to v:errmsg and
3607 // tv_get_string() causes an error message the variable
3608 // will already be set.
3609 if (di->di_tv.vval.v_string == NULL)
3610 di->di_tv.vval.v_string = vim_strsave(val);
3611 }
3612 else
3613 {
3614 // Take over the string to avoid an extra alloc/free.
3615 di->di_tv.vval.v_string = tv->vval.v_string;
3616 tv->vval.v_string = NULL;
3617 }
3618 goto failed;
3619 }
3620 else if (di->di_tv.v_type == VAR_NUMBER)
3621 {
3622 di->di_tv.vval.v_number = tv_get_number(tv);
3623 if (STRCMP(varname, "searchforward") == 0)
3624 set_search_direction(di->di_tv.vval.v_number
3625 ? '/' : '?');
3626#ifdef FEAT_SEARCH_EXTRA
3627 else if (STRCMP(varname, "hlsearch") == 0)
3628 {
3629 no_hlsearch = !di->di_tv.vval.v_number;
3630 redraw_all_later(SOME_VALID);
3631 }
3632#endif
3633 goto failed;
3634 }
3635 else if (di->di_tv.v_type != tv->v_type)
3636 {
3637 semsg(_(e_setting_str_to_value_with_wrong_type), name);
3638 goto failed;
3639 }
3640 }
3641
3642 clear_tv(&di->di_tv);
3643 }
3644 else
3645 {
3646 // Item not found, check if a function already exists.
3647 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
3648 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
3649 {
3650 semsg(_(e_redefining_script_item_str), name);
3651 goto failed;
3652 }
3653
3654 // add a new variable
3655 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3656 {
3657 semsg(_(e_unknown_variable_str), name);
3658 goto failed;
3659 }
3660
3661 // Can't add "v:" or "a:" variable.
3662 if (ht == &vimvarht || ht == get_funccal_args_ht())
3663 {
3664 semsg(_(e_illegal_variable_name_str), name);
3665 goto failed;
3666 }
3667
3668 // Make sure the variable name is valid. In Vim9 script an
3669 // autoload variable must be prefixed with "g:" unless in an
3670 // autoload script.
3671 if (!valid_varname(varname, -1, !vim9script
3672 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
3673 goto failed;
3674
3675 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3676 if (di == NULL)
3677 goto failed;
3678 STRCPY(di->di_key, varname);
3679 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
3680 {
3681 vim_free(di);
3682 goto failed;
3683 }
3684 di->di_flags = DI_FLAGS_ALLOC;
3685 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3686 di->di_flags |= DI_FLAGS_LOCK;
3687
3688 // A Vim9 script-local variable is also added to sn_all_vars and
3689 // sn_var_vals. It may set "type" from "tv".
3690 if (var_in_vim9script || var_in_autoload)
3691 update_vim9_script_var(TRUE, di,
3692 var_in_autoload ? name : di->di_key, flags,
3693 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003694 }
3695
Bram Moolenaar993faa32022-02-21 15:59:11 +00003696 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003697 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003698 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003699 else
3700 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003701 *dest_tv = *tv;
3702 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003703 init_tv(tv);
3704 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003705 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003706
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003707 if (vim9script && type != NULL)
Bram Moolenaar381692b2022-02-02 20:01:27 +00003708 set_tv_type(dest_tv, type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003709
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003710 // ":const var = value" locks the value
3711 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003712 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003713 // Like :lockvar! name: lock the value and what it contains, but only
3714 // if the reference count is up to one. That locks only literal
3715 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003716 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003717
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003718failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003719 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003720 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003721 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003722}
3723
3724/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003725 * Check in this order for backwards compatibility:
3726 * - Whether the variable is read-only
3727 * - Whether the variable value is locked
3728 * - Whether the variable is locked
3729 */
3730 int
3731var_check_permission(dictitem_T *di, char_u *name)
3732{
3733 if (var_check_ro(di->di_flags, name, FALSE)
3734 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3735 || var_check_lock(di->di_flags, name, FALSE))
3736 return FAIL;
3737 return OK;
3738}
3739
3740/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003741 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3742 * Also give an error message.
3743 */
3744 int
3745var_check_ro(int flags, char_u *name, int use_gettext)
3746{
3747 if (flags & DI_FLAGS_RO)
3748 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003749 if (name == NULL)
3750 emsg(_(e_cannot_change_readonly_variable));
3751 else
3752 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02003753 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003754 return TRUE;
3755 }
3756 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3757 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003758 if (name == NULL)
3759 emsg(_(e_cannot_set_variable_in_sandbox));
3760 else
3761 semsg(_(e_cannot_set_variable_in_sandbox_str),
3762 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003763 return TRUE;
3764 }
3765 return FALSE;
3766}
3767
3768/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003769 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3770 * Also give an error message.
3771 */
3772 int
3773var_check_lock(int flags, char_u *name, int use_gettext)
3774{
3775 if (flags & DI_FLAGS_LOCK)
3776 {
3777 semsg(_(e_variable_is_locked_str),
3778 use_gettext ? (char_u *)_(name) : name);
3779 return TRUE;
3780 }
3781 return FALSE;
3782}
3783
3784/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003785 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3786 * Also give an error message.
3787 */
3788 int
3789var_check_fixed(int flags, char_u *name, int use_gettext)
3790{
3791 if (flags & DI_FLAGS_FIX)
3792 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003793 if (name == NULL)
3794 emsg(_(e_cannot_delete_variable));
3795 else
3796 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003797 use_gettext ? (char_u *)_(name) : name);
3798 return TRUE;
3799 }
3800 return FALSE;
3801}
3802
3803/*
3804 * Check if a funcref is assigned to a valid variable name.
3805 * Return TRUE and give an error if not.
3806 */
3807 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003808var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003809 char_u *name, // points to start of variable name
3810 int new_var) // TRUE when creating the variable
3811{
Bram Moolenaar32154662021-03-28 21:14:06 +02003812 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
3813 // the name can be used without the s: prefix.
3814 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
3815 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003816 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3817 ? name[2] : name[0]))
3818 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003819 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003820 return TRUE;
3821 }
3822 // Don't allow hiding a function. When "v" is not NULL we might be
3823 // assigning another function to the same var, the type is checked
3824 // below.
3825 if (new_var && function_exists(name, FALSE))
3826 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003827 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003828 name);
3829 return TRUE;
3830 }
3831 return FALSE;
3832}
3833
3834/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003835 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3836 * value. Also give an error message, using "name" or _("name") when
3837 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003838 */
3839 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003840value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003841{
3842 if (lock & VAR_LOCKED)
3843 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003844 if (name == NULL)
3845 emsg(_(e_value_is_locked));
3846 else
3847 semsg(_(e_value_is_locked_str),
3848 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003849 return TRUE;
3850 }
3851 if (lock & VAR_FIXED)
3852 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003853 if (name == NULL)
3854 emsg(_(e_cannot_change_value));
3855 else
3856 semsg(_(e_cannot_change_value_of_str),
3857 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003858 return TRUE;
3859 }
3860 return FALSE;
3861}
3862
3863/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003864 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003865 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003866 * Return FALSE and give an error if not.
3867 */
3868 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003869valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003870{
3871 char_u *p;
3872
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003873 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003874 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003875 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003876 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003877 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003878 return FALSE;
3879 }
3880 return TRUE;
3881}
3882
3883/*
3884 * getwinvar() and gettabwinvar()
3885 */
3886 static void
3887getwinvar(
3888 typval_T *argvars,
3889 typval_T *rettv,
3890 int off) // 1 for gettabwinvar()
3891{
3892 win_T *win;
3893 char_u *varname;
3894 dictitem_T *v;
3895 tabpage_T *tp = NULL;
3896 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00003897 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003898 int need_switch_win;
3899
3900 if (off == 1)
3901 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3902 else
3903 tp = curtab;
3904 win = find_win_by_nr(&argvars[off], tp);
3905 varname = tv_get_string_chk(&argvars[off + 1]);
3906 ++emsg_off;
3907
3908 rettv->v_type = VAR_STRING;
3909 rettv->vval.v_string = NULL;
3910
3911 if (win != NULL && varname != NULL)
3912 {
3913 // Set curwin to be our win, temporarily. Also set the tabpage,
3914 // otherwise the window is not valid. Only do this when needed,
3915 // autocommands get blocked.
3916 need_switch_win = !(tp == curtab && win == curwin);
3917 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00003918 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003919 {
3920 if (*varname == '&')
3921 {
3922 if (varname[1] == NUL)
3923 {
3924 // get all window-local options in a dict
3925 dict_T *opts = get_winbuf_options(FALSE);
3926
3927 if (opts != NULL)
3928 {
3929 rettv_dict_set(rettv, opts);
3930 done = TRUE;
3931 }
3932 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003933 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003934 // window-local-option
3935 done = TRUE;
3936 }
3937 else
3938 {
3939 // Look up the variable.
3940 // Let getwinvar({nr}, "") return the "w:" dictionary.
3941 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3942 varname, FALSE);
3943 if (v != NULL)
3944 {
3945 copy_tv(&v->di_tv, rettv);
3946 done = TRUE;
3947 }
3948 }
3949 }
3950
3951 if (need_switch_win)
3952 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00003953 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003954 }
3955
3956 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3957 // use the default return value
3958 copy_tv(&argvars[off + 2], rettv);
3959
3960 --emsg_off;
3961}
3962
3963/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003964 * Set option "varname" to the value of "varp" for the current buffer/window.
3965 */
3966 static void
3967set_option_from_tv(char_u *varname, typval_T *varp)
3968{
3969 long numval = 0;
3970 char_u *strval;
3971 char_u nbuf[NUMBUFLEN];
3972 int error = FALSE;
3973
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003974 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003975 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003976 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003977 strval = (char_u *)"0"; // avoid using "false"
3978 }
3979 else
3980 {
3981 if (!in_vim9script() || varp->v_type != VAR_STRING)
3982 numval = (long)tv_get_number_chk(varp, &error);
3983 strval = tv_get_string_buf_chk(varp, nbuf);
3984 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02003985 if (!error && strval != NULL)
3986 set_option_value(varname, numval, strval, OPT_LOCAL);
3987}
3988
3989/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003990 * "setwinvar()" and "settabwinvar()" functions
3991 */
3992 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003993setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003994{
3995 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00003996 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003997 int need_switch_win;
3998 char_u *varname, *winvarname;
3999 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004000 tabpage_T *tp = NULL;
4001
4002 if (check_secure())
4003 return;
4004
4005 if (off == 1)
4006 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4007 else
4008 tp = curtab;
4009 win = find_win_by_nr(&argvars[off], tp);
4010 varname = tv_get_string_chk(&argvars[off + 1]);
4011 varp = &argvars[off + 2];
4012
4013 if (win != NULL && varname != NULL && varp != NULL)
4014 {
4015 need_switch_win = !(tp == curtab && win == curwin);
4016 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00004017 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004018 {
4019 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02004020 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004021 else
4022 {
4023 winvarname = alloc(STRLEN(varname) + 3);
4024 if (winvarname != NULL)
4025 {
4026 STRCPY(winvarname, "w:");
4027 STRCPY(winvarname + 2, varname);
4028 set_var(winvarname, varp, TRUE);
4029 vim_free(winvarname);
4030 }
4031 }
4032 }
4033 if (need_switch_win)
Bram Moolenaar18f47402022-01-06 13:24:51 +00004034 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004035 }
4036}
4037
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004038/*
4039 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4040 * v:option_type, and v:option_command.
4041 */
4042 void
4043reset_v_option_vars(void)
4044{
4045 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4046 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4047 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4048 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4049 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4050 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4051}
4052
4053/*
4054 * Add an assert error to v:errors.
4055 */
4056 void
4057assert_error(garray_T *gap)
4058{
4059 struct vimvar *vp = &vimvars[VV_ERRORS];
4060
Bram Moolenaard787e402021-12-24 21:36:12 +00004061 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004062 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004063 set_vim_var_list(VV_ERRORS, list_alloc());
4064 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4065}
4066
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004067 int
4068var_exists(char_u *var)
4069{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004070 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004071 char_u *name;
4072 char_u *tofree;
4073 typval_T tv;
4074 int len = 0;
4075 int n = FALSE;
4076
4077 // get_name_len() takes care of expanding curly braces
4078 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004079 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004080 if (len > 0)
4081 {
4082 if (tofree != NULL)
4083 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004084 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004085 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004086 if (n)
4087 {
4088 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004089 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004090 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4091 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004092 if (n)
4093 clear_tv(&tv);
4094 }
4095 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004096 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004097 n = FALSE;
4098
4099 vim_free(tofree);
4100 return n;
4101}
4102
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004103static lval_T *redir_lval = NULL;
4104#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4105static garray_T redir_ga; // only valid when redir_lval is not NULL
4106static char_u *redir_endp = NULL;
4107static char_u *redir_varname = NULL;
4108
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004109 int
4110alloc_redir_lval(void)
4111{
4112 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4113 if (redir_lval == NULL)
4114 return FAIL;
4115 return OK;
4116}
4117
4118 void
4119clear_redir_lval(void)
4120{
4121 VIM_CLEAR(redir_lval);
4122}
4123
4124 void
4125init_redir_ga(void)
4126{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004127 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004128}
4129
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004130/*
4131 * Start recording command output to a variable
4132 * When "append" is TRUE append to an existing variable.
4133 * Returns OK if successfully completed the setup. FAIL otherwise.
4134 */
4135 int
4136var_redir_start(char_u *name, int append)
4137{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004138 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004139 typval_T tv;
4140
4141 // Catch a bad name early.
4142 if (!eval_isnamec1(*name))
4143 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004144 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004145 return FAIL;
4146 }
4147
4148 // Make a copy of the name, it is used in redir_lval until redir ends.
4149 redir_varname = vim_strsave(name);
4150 if (redir_varname == NULL)
4151 return FAIL;
4152
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004153 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004154 {
4155 var_redir_stop();
4156 return FAIL;
4157 }
4158
4159 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004160 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004161
4162 // Parse the variable name (can be a dict or list entry).
4163 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4164 FNE_CHECK_START);
4165 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4166 {
4167 clear_lval(redir_lval);
4168 if (redir_endp != NULL && *redir_endp != NUL)
4169 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004170 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004171 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004172 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004173 redir_endp = NULL; // don't store a value, only cleanup
4174 var_redir_stop();
4175 return FAIL;
4176 }
4177
4178 // check if we can write to the variable: set it to or append an empty
4179 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004180 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004181 tv.v_type = VAR_STRING;
4182 tv.vval.v_string = (char_u *)"";
4183 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004184 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004185 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004186 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004187 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004188 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004189 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004190 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004191 {
4192 redir_endp = NULL; // don't store a value, only cleanup
4193 var_redir_stop();
4194 return FAIL;
4195 }
4196
4197 return OK;
4198}
4199
4200/*
4201 * Append "value[value_len]" to the variable set by var_redir_start().
4202 * The actual appending is postponed until redirection ends, because the value
4203 * appended may in fact be the string we write to, changing it may cause freed
4204 * memory to be used:
4205 * :redir => foo
4206 * :let foo
4207 * :redir END
4208 */
4209 void
4210var_redir_str(char_u *value, int value_len)
4211{
4212 int len;
4213
4214 if (redir_lval == NULL)
4215 return;
4216
4217 if (value_len == -1)
4218 len = (int)STRLEN(value); // Append the entire string
4219 else
4220 len = value_len; // Append only "value_len" characters
4221
4222 if (ga_grow(&redir_ga, len) == OK)
4223 {
4224 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4225 redir_ga.ga_len += len;
4226 }
4227 else
4228 var_redir_stop();
4229}
4230
4231/*
4232 * Stop redirecting command output to a variable.
4233 * Frees the allocated memory.
4234 */
4235 void
4236var_redir_stop(void)
4237{
4238 typval_T tv;
4239
4240 if (EVALCMD_BUSY)
4241 {
4242 redir_lval = NULL;
4243 return;
4244 }
4245
4246 if (redir_lval != NULL)
4247 {
4248 // If there was no error: assign the text to the variable.
4249 if (redir_endp != NULL)
4250 {
4251 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4252 tv.v_type = VAR_STRING;
4253 tv.vval.v_string = redir_ga.ga_data;
4254 // Call get_lval() again, if it's inside a Dict or List it may
4255 // have changed.
4256 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4257 FALSE, FALSE, 0, FNE_CHECK_START);
4258 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004260 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004261 clear_lval(redir_lval);
4262 }
4263
4264 // free the collected output
4265 VIM_CLEAR(redir_ga.ga_data);
4266
4267 VIM_CLEAR(redir_lval);
4268 }
4269 VIM_CLEAR(redir_varname);
4270}
4271
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004272/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004273 * Get the collected redirected text and clear redir_ga.
4274 */
4275 char_u *
4276get_clear_redir_ga(void)
4277{
4278 char_u *res;
4279
4280 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4281 res = redir_ga.ga_data;
4282 redir_ga.ga_data = NULL;
4283 return res;
4284}
4285
4286/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004287 * "gettabvar()" function
4288 */
4289 void
4290f_gettabvar(typval_T *argvars, typval_T *rettv)
4291{
Bram Moolenaar18f47402022-01-06 13:24:51 +00004292 switchwin_T switchwin;
4293 tabpage_T *tp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004294 dictitem_T *v;
4295 char_u *varname;
4296 int done = FALSE;
4297
4298 rettv->v_type = VAR_STRING;
4299 rettv->vval.v_string = NULL;
4300
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004301 if (in_vim9script()
4302 && (check_for_number_arg(argvars, 0) == FAIL
4303 || check_for_string_arg(argvars, 1) == FAIL))
4304 return;
4305
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004306 varname = tv_get_string_chk(&argvars[1]);
4307 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4308 if (tp != NULL && varname != NULL)
4309 {
4310 // Set tp to be our tabpage, temporarily. Also set the window to the
4311 // first window in the tabpage, otherwise the window is not valid.
Bram Moolenaar18f47402022-01-06 13:24:51 +00004312 if (switch_win(&switchwin,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004313 tp == curtab || tp->tp_firstwin == NULL ? firstwin
4314 : tp->tp_firstwin, tp, TRUE) == OK)
4315 {
4316 // look up the variable
4317 // Let gettabvar({nr}, "") return the "t:" dictionary.
4318 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
4319 if (v != NULL)
4320 {
4321 copy_tv(&v->di_tv, rettv);
4322 done = TRUE;
4323 }
4324 }
4325
4326 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004327 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004328 }
4329
4330 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4331 copy_tv(&argvars[2], rettv);
4332}
4333
4334/*
4335 * "gettabwinvar()" function
4336 */
4337 void
4338f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4339{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004340 if (in_vim9script()
4341 && (check_for_number_arg(argvars, 0) == FAIL
4342 || check_for_number_arg(argvars, 1) == FAIL
4343 || check_for_string_arg(argvars, 2) == FAIL))
4344 return;
4345
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004346 getwinvar(argvars, rettv, 1);
4347}
4348
4349/*
4350 * "getwinvar()" function
4351 */
4352 void
4353f_getwinvar(typval_T *argvars, typval_T *rettv)
4354{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004355 if (in_vim9script()
4356 && (check_for_number_arg(argvars, 0) == FAIL
4357 || check_for_string_arg(argvars, 1) == FAIL))
4358 return;
4359
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004360 getwinvar(argvars, rettv, 0);
4361}
4362
4363/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004364 * "getbufvar()" function
4365 */
4366 void
4367f_getbufvar(typval_T *argvars, typval_T *rettv)
4368{
4369 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004370 char_u *varname;
4371 dictitem_T *v;
4372 int done = FALSE;
4373
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004374 if (in_vim9script()
4375 && (check_for_buffer_arg(argvars, 0) == FAIL
4376 || check_for_string_arg(argvars, 1) == FAIL))
4377 return;
4378
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004379 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004380 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004381
4382 rettv->v_type = VAR_STRING;
4383 rettv->vval.v_string = NULL;
4384
4385 if (buf != NULL && varname != NULL)
4386 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004387 if (*varname == '&')
4388 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004389 buf_T *save_curbuf = curbuf;
4390
4391 // set curbuf to be our buf, temporarily
4392 curbuf = buf;
4393
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004394 if (varname[1] == NUL)
4395 {
4396 // get all buffer-local options in a dict
4397 dict_T *opts = get_winbuf_options(TRUE);
4398
4399 if (opts != NULL)
4400 {
4401 rettv_dict_set(rettv, opts);
4402 done = TRUE;
4403 }
4404 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004405 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004406 // buffer-local-option
4407 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02004408
4409 // restore previous notion of curbuf
4410 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004411 }
4412 else
4413 {
4414 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02004415 if (*varname == NUL)
4416 // Let getbufvar({nr}, "") return the "b:" dictionary.
4417 v = &buf->b_bufvar;
4418 else
4419 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
4420 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004421 if (v != NULL)
4422 {
4423 copy_tv(&v->di_tv, rettv);
4424 done = TRUE;
4425 }
4426 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004427 }
4428
4429 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4430 // use the default value
4431 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004432}
4433
4434/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004435 * "settabvar()" function
4436 */
4437 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004438f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004439{
4440 tabpage_T *save_curtab;
4441 tabpage_T *tp;
4442 char_u *varname, *tabvarname;
4443 typval_T *varp;
4444
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004445 if (check_secure())
4446 return;
4447
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004448 if (in_vim9script()
4449 && (check_for_number_arg(argvars, 0) == FAIL
4450 || check_for_string_arg(argvars, 1) == FAIL))
4451 return;
4452
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004453 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4454 varname = tv_get_string_chk(&argvars[1]);
4455 varp = &argvars[2];
4456
4457 if (varname != NULL && varp != NULL && tp != NULL)
4458 {
4459 save_curtab = curtab;
4460 goto_tabpage_tp(tp, FALSE, FALSE);
4461
4462 tabvarname = alloc(STRLEN(varname) + 3);
4463 if (tabvarname != NULL)
4464 {
4465 STRCPY(tabvarname, "t:");
4466 STRCPY(tabvarname + 2, varname);
4467 set_var(tabvarname, varp, TRUE);
4468 vim_free(tabvarname);
4469 }
4470
4471 // Restore current tabpage
4472 if (valid_tabpage(save_curtab))
4473 goto_tabpage_tp(save_curtab, FALSE, FALSE);
4474 }
4475}
4476
4477/*
4478 * "settabwinvar()" function
4479 */
4480 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004481f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004482{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004483 if (in_vim9script()
4484 && (check_for_number_arg(argvars, 0) == FAIL
4485 || check_for_number_arg(argvars, 1) == FAIL
4486 || check_for_string_arg(argvars, 2) == FAIL))
4487 return;
4488
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004489 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004490}
4491
4492/*
4493 * "setwinvar()" function
4494 */
4495 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004496f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004497{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004498 if (in_vim9script()
4499 && (check_for_number_arg(argvars, 0) == FAIL
4500 || check_for_string_arg(argvars, 1) == FAIL))
4501 return;
4502
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004503 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004504}
4505
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004506/*
4507 * "setbufvar()" function
4508 */
4509 void
4510f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4511{
4512 buf_T *buf;
4513 char_u *varname, *bufvarname;
4514 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004515
4516 if (check_secure())
4517 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004518
4519 if (in_vim9script()
4520 && (check_for_buffer_arg(argvars, 0) == FAIL
4521 || check_for_string_arg(argvars, 1) == FAIL))
4522 return;
4523
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004524 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004525 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004526 varp = &argvars[2];
4527
4528 if (buf != NULL && varname != NULL && varp != NULL)
4529 {
4530 if (*varname == '&')
4531 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004532 aco_save_T aco;
4533
4534 // set curbuf to be our buf, temporarily
4535 aucmd_prepbuf(&aco, buf);
4536
Bram Moolenaar191929b2020-08-19 21:20:49 +02004537 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004538
4539 // reset notion of buffer
4540 aucmd_restbuf(&aco);
4541 }
4542 else
4543 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004544 bufvarname = alloc(STRLEN(varname) + 3);
4545 if (bufvarname != NULL)
4546 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004547 buf_T *save_curbuf = curbuf;
4548
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004549 curbuf = buf;
4550 STRCPY(bufvarname, "b:");
4551 STRCPY(bufvarname + 2, varname);
4552 set_var(bufvarname, varp, TRUE);
4553 vim_free(bufvarname);
4554 curbuf = save_curbuf;
4555 }
4556 }
4557 }
4558}
4559
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004560/*
4561 * Get a callback from "arg". It can be a Funcref or a function name.
4562 * When "arg" is zero return an empty string.
4563 * "cb_name" is not allocated.
4564 * "cb_name" is set to NULL for an invalid argument.
4565 */
4566 callback_T
4567get_callback(typval_T *arg)
4568{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004569 callback_T res;
4570 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004571
4572 res.cb_free_name = FALSE;
4573 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4574 {
4575 res.cb_partial = arg->vval.v_partial;
4576 ++res.cb_partial->pt_refcount;
4577 res.cb_name = partial_name(res.cb_partial);
4578 }
4579 else
4580 {
4581 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004582 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4583 && isdigit(*arg->vval.v_string))
4584 r = FAIL;
4585 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004586 {
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004587 if (arg->v_type == VAR_STRING)
4588 {
4589 char_u *name;
4590
4591 name = get_scriptlocal_funcname(arg->vval.v_string);
4592 if (name != NULL)
4593 {
4594 vim_free(arg->vval.v_string);
4595 arg->vval.v_string = name;
4596 }
4597 }
4598
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004599 res.cb_name = arg->vval.v_string;
4600 func_ref(res.cb_name);
4601 }
4602 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004603 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004604 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004605 r = FAIL;
4606
4607 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004608 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004609 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004610 res.cb_name = NULL;
4611 }
4612 }
4613 return res;
4614}
4615
4616/*
4617 * Copy a callback into a typval_T.
4618 */
4619 void
4620put_callback(callback_T *cb, typval_T *tv)
4621{
4622 if (cb->cb_partial != NULL)
4623 {
4624 tv->v_type = VAR_PARTIAL;
4625 tv->vval.v_partial = cb->cb_partial;
4626 ++tv->vval.v_partial->pt_refcount;
4627 }
4628 else
4629 {
4630 tv->v_type = VAR_FUNC;
4631 tv->vval.v_string = vim_strsave(cb->cb_name);
4632 func_ref(cb->cb_name);
4633 }
4634}
4635
4636/*
4637 * Make a copy of "src" into "dest", allocating the function name if needed,
4638 * without incrementing the refcount.
4639 */
4640 void
4641set_callback(callback_T *dest, callback_T *src)
4642{
4643 if (src->cb_partial == NULL)
4644 {
4645 // just a function name, make a copy
4646 dest->cb_name = vim_strsave(src->cb_name);
4647 dest->cb_free_name = TRUE;
4648 }
4649 else
4650 {
4651 // cb_name is a pointer into cb_partial
4652 dest->cb_name = src->cb_name;
4653 dest->cb_free_name = FALSE;
4654 }
4655 dest->cb_partial = src->cb_partial;
4656}
4657
4658/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004659 * Copy callback from "src" to "dest", incrementing the refcounts.
4660 */
4661 void
4662copy_callback(callback_T *dest, callback_T *src)
4663{
4664 dest->cb_partial = src->cb_partial;
4665 if (dest->cb_partial != NULL)
4666 {
4667 dest->cb_name = src->cb_name;
4668 dest->cb_free_name = FALSE;
4669 ++dest->cb_partial->pt_refcount;
4670 }
4671 else
4672 {
4673 dest->cb_name = vim_strsave(src->cb_name);
4674 dest->cb_free_name = TRUE;
4675 func_ref(src->cb_name);
4676 }
4677}
4678
4679/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004680 * When a callback refers to an autoload import, change the function name to
4681 * the "path#name" form. Uses the current script context.
4682 * Only works when the name is allocated.
4683 */
4684 void
4685expand_autload_callback(callback_T *cb)
4686{
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004687 char_u *name;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004688 char_u *p;
4689 imported_T *import;
4690
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004691 if (!in_vim9script() || cb->cb_name == NULL
4692 || (!cb->cb_free_name
4693 && (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004694 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004695 if (cb->cb_partial != NULL)
4696 name = cb->cb_partial->pt_name;
4697 else
4698 name = cb->cb_name;
4699 p = vim_strchr(name, '.');
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004700 if (p == NULL)
4701 return;
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004702 import = find_imported(name, p - name, FALSE);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004703 if (import != NULL && SCRIPT_ID_VALID(import->imp_sid))
4704 {
4705 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
4706
4707 if (si->sn_autoload_prefix != NULL)
4708 {
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004709 char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004710
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004711 if (newname != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004712 {
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004713 if (cb->cb_partial != NULL)
4714 {
4715 if (cb->cb_name == cb->cb_partial->pt_name)
4716 cb->cb_name = newname;
4717 vim_free(cb->cb_partial->pt_name);
4718 cb->cb_partial->pt_name = newname;
4719 }
4720 else
4721 {
4722 vim_free(cb->cb_name);
4723 cb->cb_name = newname;
4724 }
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004725 }
4726 }
4727 }
4728}
4729
4730/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004731 * Unref/free "callback" returned by get_callback() or set_callback().
4732 */
4733 void
4734free_callback(callback_T *callback)
4735{
4736 if (callback->cb_partial != NULL)
4737 {
4738 partial_unref(callback->cb_partial);
4739 callback->cb_partial = NULL;
4740 }
4741 else if (callback->cb_name != NULL)
4742 func_unref(callback->cb_name);
4743 if (callback->cb_free_name)
4744 {
4745 vim_free(callback->cb_name);
4746 callback->cb_free_name = FALSE;
4747 }
4748 callback->cb_name = NULL;
4749}
4750
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004751#endif // FEAT_EVAL