blob: e473e48763fac2ff4ff2e7de43190c962c7512be [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},
Shougo Matsushita61021aa2022-07-27 14:40:00 +0100114 {VV_NAME("completed_item", VAR_DICT), &t_dict_string, 0},
Bram Moolenaard787e402021-12-24 21:36:12 +0000115 {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 Moolenaaref2c3252022-11-25 16:31:51 +0000220 hash_add(&vimvarht, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200221 if (p->vv_flags & VV_COMPAT)
222 // add to compat scope dict
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000223 hash_add(&compat_hashtab, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200224 }
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);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000353 if (val == NULL)
354 return;
355
356 tvp = alloc_string_tv(val);
357 if (tvp == NULL)
358 return;
359
360 set_var(name, tvp, FALSE);
361 free_tv(tvp);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200362}
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 Moolenaara4e0b972022-10-01 19:43:52 +0100383 if (eval_to_bool(p_ccv, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200384 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 Moolenaara4e0b972022-10-01 19:43:52 +0100411 if (eval_to_bool(p_pexpr, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200412 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
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100447 tv = eval_expr_ext(p_dex, NULL, TRUE);
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
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100475 tv = eval_expr_ext(p_pex, NULL, TRUE);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000476 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 Moolenaara4e0b972022-10-01 19:43:52 +0100500 int r;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200501
502 // Set "v:val" to the bad word.
503 prepare_vimvar(VV_VAL, &save_val);
504 set_vim_var_string(VV_VAL, badword, -1);
505 if (p_verbose == 0)
506 ++emsg_off;
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000507 ctx = get_option_sctx("spellsuggest");
508 if (ctx != NULL)
509 current_sctx = *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200510
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100511 r = may_call_simple_func(p, &rettv);
512 if (r == NOTDONE)
513 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
514 if (r == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200515 {
516 if (rettv.v_type != VAR_LIST)
517 clear_tv(&rettv);
518 else
519 list = rettv.vval.v_list;
520 }
521
522 if (p_verbose == 0)
523 --emsg_off;
524 clear_tv(get_vim_var_tv(VV_VAL));
525 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000526 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200527
528 return list;
529}
530
531/*
532 * "list" is supposed to contain two items: a word and a number. Return the
533 * word in "pp" and the number as the return value.
534 * Return -1 if anything isn't right.
535 * Used to get the good word and score from the eval_spell_expr() result.
536 */
537 int
538get_spellword(list_T *list, char_u **pp)
539{
540 listitem_T *li;
541
542 li = list->lv_first;
543 if (li == NULL)
544 return -1;
545 *pp = tv_get_string(&li->li_tv);
546
547 li = li->li_next;
548 if (li == NULL)
549 return -1;
550 return (int)tv_get_number(&li->li_tv);
551}
552#endif
553
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200554/*
555 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200556 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200557 * When not used yet add the variable to the v: hashtable.
558 */
559 void
560prepare_vimvar(int idx, typval_T *save_tv)
561{
562 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200563 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaard787e402021-12-24 21:36:12 +0000564 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000565 hash_add(&vimvarht, vimvars[idx].vv_di.di_key, "prepare vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200566}
567
568/*
569 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200570 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200571 * When no longer defined, remove the variable from the v: hashtable.
572 */
573 void
574restore_vimvar(int idx, typval_T *save_tv)
575{
576 hashitem_T *hi;
577
578 vimvars[idx].vv_tv = *save_tv;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000579 if (vimvars[idx].vv_tv_type != VAR_UNKNOWN)
580 return;
581
582 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
583 if (HASHITEM_EMPTY(hi))
584 internal_error("restore_vimvar()");
585 else
586 hash_remove(&vimvarht, hi, "restore vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200587}
588
589/*
590 * List Vim variables.
591 */
592 static void
593list_vim_vars(int *first)
594{
595 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
596}
597
598/*
599 * List script-local variables, if there is a script.
600 */
601 static void
602list_script_vars(int *first)
603{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200604 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200605 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
606 "s:", FALSE, first);
607}
608
609/*
Bram Moolenaar9510d222022-09-11 15:14:05 +0100610 * Return TRUE if "name" starts with "g:", "w:", "t:" or "b:".
611 * But only when an identifier character follows.
612 */
613 int
614is_scoped_variable(char_u *name)
615{
616 return vim_strchr((char_u *)"gwbt", name[0]) != NULL
617 && name[1] == ':'
618 && eval_isnamec(name[2]);
619}
620
621/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100622 * Evaluate one Vim expression {expr} in string "p" and append the
623 * resulting string to "gap". "p" points to the opening "{".
Bram Moolenaar70c41242022-05-10 18:11:43 +0100624 * When "evaluate" is FALSE only skip over the expression.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100625 * Return a pointer to the character after "}", NULL for an error.
626 */
627 char_u *
Bram Moolenaar70c41242022-05-10 18:11:43 +0100628eval_one_expr_in_str(char_u *p, garray_T *gap, int evaluate)
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100629{
630 char_u *block_start = skipwhite(p + 1); // skip the opening {
631 char_u *block_end = block_start;
632 char_u *expr_val;
633
634 if (*block_start == NUL)
635 {
636 semsg(_(e_missing_close_curly_str), p);
637 return NULL;
638 }
639 if (skip_expr(&block_end, NULL) == FAIL)
640 return NULL;
641 block_end = skipwhite(block_end);
642 if (*block_end != '}')
643 {
644 semsg(_(e_missing_close_curly_str), p);
645 return NULL;
646 }
Bram Moolenaar70c41242022-05-10 18:11:43 +0100647 if (evaluate)
648 {
649 *block_end = NUL;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100650 expr_val = eval_to_string(block_start, TRUE, FALSE);
Bram Moolenaar70c41242022-05-10 18:11:43 +0100651 *block_end = '}';
652 if (expr_val == NULL)
653 return NULL;
654 ga_concat(gap, expr_val);
655 vim_free(expr_val);
656 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100657
658 return block_end + 1;
659}
660
661/*
662 * Evaluate all the Vim expressions {expr} in "str" and return the resulting
663 * string in allocated memory. "{{" is reduced to "{" and "}}" to "}".
664 * Used for a heredoc assignment.
665 * Returns NULL for an error.
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100666 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +0100667 static char_u *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100668eval_all_expr_in_str(char_u *str)
669{
670 garray_T ga;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100671 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100672
673 ga_init2(&ga, 1, 80);
674 p = str;
675
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100676 while (*p != NUL)
677 {
LemonBoy2eaef102022-05-06 13:14:50 +0100678 char_u *lit_start;
LemonBoy2eaef102022-05-06 13:14:50 +0100679 int escaped_brace = FALSE;
680
681 // Look for a block start.
682 lit_start = p;
683 while (*p != '{' && *p != '}' && *p != NUL)
684 ++p;
685
686 if (*p != NUL && *p == p[1])
687 {
688 // Escaped brace, unescape and continue.
689 // Include the brace in the literal string.
690 ++p;
691 escaped_brace = TRUE;
692 }
693 else if (*p == '}')
694 {
695 semsg(_(e_stray_closing_curly_str), str);
696 ga_clear(&ga);
697 return NULL;
698 }
699
700 // Append the literal part.
701 ga_concat_len(&ga, lit_start, (size_t)(p - lit_start));
702
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100703 if (*p == NUL)
LemonBoy2eaef102022-05-06 13:14:50 +0100704 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100705
LemonBoy2eaef102022-05-06 13:14:50 +0100706 if (escaped_brace)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100707 {
LemonBoy2eaef102022-05-06 13:14:50 +0100708 // Skip the second brace.
709 ++p;
710 continue;
711 }
712
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100713 // Evaluate the expression and append the result.
Bram Moolenaar70c41242022-05-10 18:11:43 +0100714 p = eval_one_expr_in_str(p, &ga, TRUE);
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100715 if (p == NULL)
LemonBoy2eaef102022-05-06 13:14:50 +0100716 {
717 ga_clear(&ga);
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100718 return NULL;
719 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100720 }
721 ga_append(&ga, NUL);
722
723 return ga.ga_data;
724}
725
726/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200727 * Get a list of lines from a HERE document. The here document is a list of
728 * lines surrounded by a marker.
729 * cmd << {marker}
730 * {line1}
731 * {line2}
732 * ....
733 * {marker}
734 *
735 * The {marker} is a string. If the optional 'trim' word is supplied before the
736 * marker, then the leading indentation before the lines (matching the
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100737 * indentation in the "cmd" line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200738 *
739 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100740 * tcl, mzscheme), "script_get" is set to TRUE. In this case, if the marker is
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200741 * missing, then '.' is accepted as a marker.
742 *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100743 * When compiling a heredoc assignment to a variable in a Vim9 def function,
744 * "vim9compile" is set to TRUE. In this case, instead of generating a list of
745 * string values from the heredoc, vim9 instructions are generated. On success
746 * the returned list will be empty.
747 *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100748 * Returns a List with {lines} or NULL on failure.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200749 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750 list_T *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100751heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200752{
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100753 char_u *theline = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200754 char_u *marker;
755 list_T *l;
756 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100757 char_u *str;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200758 int marker_indent_len = 0;
759 int text_indent_len = 0;
760 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200761 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200762 int comment_char = in_vim9script() ? '#' : '"';
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100763 int evalstr = FALSE;
764 int eval_failed = FALSE;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100765 cctx_T *cctx = vim9compile ? eap->cookie : NULL;
766 int count = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200767
768 if (eap->getline == NULL)
769 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000770 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200771 return NULL;
772 }
773
774 // Check for the optional 'trim' word before the marker
775 cmd = skipwhite(cmd);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200776
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100777 while (TRUE)
778 {
779 if (STRNCMP(cmd, "trim", 4) == 0
780 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200781 {
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100782 cmd = skipwhite(cmd + 4);
783
784 // Trim the indentation from all the lines in the here document.
785 // The amount of indentation trimmed is the same as the indentation
786 // of the first line after the :let command line. To find the end
787 // marker the indent of the :let command line is trimmed.
788 p = *eap->cmdlinep;
789 while (VIM_ISWHITE(*p))
790 {
791 p++;
792 marker_indent_len++;
793 }
794 text_indent_len = -1;
795
796 continue;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200797 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100798 if (STRNCMP(cmd, "eval", 4) == 0
799 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
800 {
801 cmd = skipwhite(cmd + 4);
802 evalstr = TRUE;
803 continue;
804 }
805 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200806 }
807
808 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200809 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200810 {
811 marker = skipwhite(cmd);
812 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200813 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200814 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000815 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200816 return NULL;
817 }
818 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200819 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200820 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000821 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200822 return NULL;
823 }
824 }
825 else
826 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200827 // When getting lines for an embedded script, if the marker is missing,
828 // accept '.' as the marker.
829 if (script_get)
830 marker = dot;
831 else
832 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000833 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200834 return NULL;
835 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200836 }
837
838 l = list_alloc();
839 if (l == NULL)
840 return NULL;
841
842 for (;;)
843 {
844 int mi = 0;
845 int ti = 0;
846
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100847 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200848 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
849 if (theline == NULL)
850 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000851 semsg(_(e_missing_end_marker_str), marker);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200852 break;
853 }
854
855 // with "trim": skip the indent matching the :let line to find the
856 // marker
857 if (marker_indent_len > 0
858 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
859 mi = marker_indent_len;
860 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200861 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200862
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100863 // If expression evaluation failed in the heredoc, then skip till the
864 // end marker.
865 if (eval_failed)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100866 continue;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100867
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200868 if (text_indent_len == -1 && *theline != NUL)
869 {
870 // set the text indent from the first line.
871 p = theline;
872 text_indent_len = 0;
873 while (VIM_ISWHITE(*p))
874 {
875 p++;
876 text_indent_len++;
877 }
878 text_indent = vim_strnsave(theline, text_indent_len);
879 }
880 // with "trim": skip the indent matching the first line
881 if (text_indent != NULL)
882 for (ti = 0; ti < text_indent_len; ++ti)
883 if (theline[ti] != text_indent[ti])
884 break;
885
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100886 str = theline + ti;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100887 if (vim9compile)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100888 {
LemonBoy2eaef102022-05-06 13:14:50 +0100889 if (compile_all_expr_in_str(str, evalstr, cctx) == FAIL)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100890 {
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100891 vim_free(theline);
892 vim_free(text_indent);
893 return FAIL;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100894 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100895 count++;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100896 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100897 else
898 {
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100899 if (evalstr && !eap->skip)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100900 {
901 str = eval_all_expr_in_str(str);
902 if (str == NULL)
903 {
904 // expression evaluation failed
905 eval_failed = TRUE;
906 continue;
907 }
908 vim_free(theline);
909 theline = str;
910 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100911
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100912 if (list_append_string(l, str, -1) == FAIL)
913 break;
914 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200915 }
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100916 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200917 vim_free(text_indent);
918
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100919 if (vim9compile && cctx->ctx_skip != SKIP_YES && !eval_failed)
920 generate_NEWLIST(cctx, count, FALSE);
921
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100922 if (eval_failed)
923 {
924 // expression evaluation in the heredoc failed
925 list_free(l);
926 return NULL;
927 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200928 return l;
929}
930
931/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200932 * Vim9 variable declaration:
933 * ":var name"
934 * ":var name: type"
935 * ":var name = expr"
936 * ":var name: type = expr"
937 * etc.
938 */
939 void
940ex_var(exarg_T *eap)
941{
Bram Moolenaar330a3882022-03-05 11:05:57 +0000942 char_u *p = eap->cmd;
Bram Moolenaare1d12112022-03-05 11:37:48 +0000943 int has_var;
Bram Moolenaar330a3882022-03-05 11:05:57 +0000944
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200945 if (!in_vim9script())
946 {
947 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
948 return;
949 }
Bram Moolenaare1d12112022-03-05 11:37:48 +0000950 has_var = checkforcmd_noparen(&p, "var", 3);
951 if (current_sctx.sc_sid == 0 && has_var)
Bram Moolenaar0e1574c2022-03-03 17:05:35 +0000952 {
953 emsg(_(e_cannot_declare_variable_on_command_line));
954 return;
955 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200956 ex_let(eap);
957}
958
959/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200960 * ":let" list all variable values
961 * ":let var1 var2" list variable values
962 * ":let var = expr" assignment command.
963 * ":let var += expr" assignment command.
964 * ":let var -= expr" assignment command.
965 * ":let var *= expr" assignment command.
966 * ":let var /= expr" assignment command.
967 * ":let var %= expr" assignment command.
968 * ":let var .= expr" assignment command.
969 * ":let var ..= expr" assignment command.
970 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100971 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100972 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200973 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200974 * ":final var = expr" assignment command.
975 * ":final [var1, var2] = expr" unpack list.
976 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200977 * ":const" list all variable values
978 * ":const var1 var2" list variable values
979 * ":const var = expr" assignment command.
980 * ":const [var1, var2] = expr" unpack list.
981 */
982 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200983ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200984{
985 char_u *arg = eap->arg;
986 char_u *expr = NULL;
987 typval_T rettv;
988 int i;
989 int var_count = 0;
990 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200991 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200992 char_u *argend;
993 int first = TRUE;
994 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200995 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100996 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200997 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200998
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200999 if (eap->cmdidx == CMD_final && !vim9script)
1000 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001001 // In legacy Vim script ":final" is short for ":finally".
1002 ex_finally(eap);
1003 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001004 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +02001005 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001006 {
1007 emsg(_(e_cannot_use_let_in_vim9_script));
1008 return;
1009 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001010
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001011 if (eap->cmdidx == CMD_const)
1012 flags |= ASSIGN_CONST;
1013 else if (eap->cmdidx == CMD_final)
1014 flags |= ASSIGN_FINAL;
1015
1016 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001017 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001018 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001019
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001020 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001021 if (argend == NULL)
1022 return;
1023 if (argend > arg && argend[-1] == '.') // for var.='str'
1024 --argend;
1025 expr = skipwhite(argend);
1026 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02001027 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001028 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +02001029 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
1030 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +02001031 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001032 {
1033 // ":let" without "=": list variables
1034 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001035 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001036 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001037 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001038 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001039 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001040 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001041 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001042 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001043 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001044 else
1045 // Vim9 declaration ":var name: type"
1046 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001047 }
1048 else
1049 {
1050 // ":let var1 var2" - list values
1051 arg = list_arg_vars(eap, arg, &first);
1052 }
1053 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001054 else if (!eap->skip)
1055 {
1056 // ":let"
1057 list_glob_vars(&first);
1058 list_buf_vars(&first);
1059 list_win_vars(&first);
1060 list_tab_vars(&first);
1061 list_script_vars(&first);
1062 list_func_vars(&first);
1063 list_vim_vars(&first);
1064 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02001065 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001066 }
1067 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
1068 {
Bram Moolenaard9876422022-10-12 12:58:54 +01001069 list_T *l = NULL;
Bram Moolenaar81530e32021-07-28 21:25:49 +02001070 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001071
Bram Moolenaard9876422022-10-12 12:58:54 +01001072 // :let text =<< [trim] [eval] END
1073 // :var text =<< [trim] [eval] END
1074 if (vim9script && !eap->skip && (!VIM_ISWHITE(expr[-1])
1075 || !IS_WHITE_OR_NUL(expr[3])))
1076 semsg(_(e_white_space_required_before_and_after_str_at_str),
1077 "=<<", expr);
1078 else
1079 l = heredoc_get(eap, expr + 3, FALSE, FALSE);
1080
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001081 if (l != NULL)
1082 {
1083 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001084 if (!eap->skip)
1085 {
Bram Moolenaar81530e32021-07-28 21:25:49 +02001086 // errors are for the assignment, not the end marker
1087 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001088 op[0] = '=';
1089 op[1] = NUL;
1090 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001091 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001092 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001093 clear_tv(&rettv);
1094 }
1095 }
1096 else
1097 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001098 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001099 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001100
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02001101 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001102 i = FAIL;
1103 if (has_assign || concat)
1104 {
Bram Moolenaar9a562c12021-01-23 13:39:14 +01001105 int cur_lnum;
1106
Bram Moolenaar32e35112020-05-14 22:41:15 +02001107 op[0] = '=';
1108 op[1] = NUL;
1109 if (*expr != '=')
1110 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001111 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +02001112 {
1113 // +=, /=, etc. require an existing variable
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001114 semsg(_(e_cannot_use_operator_on_new_variable_str),
1115 eap->arg);
Bram Moolenaar122616d2020-08-21 21:32:50 +02001116 }
1117 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +02001118 {
1119 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001120 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001121 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001122 {
Bram Moolenaar32e35112020-05-14 22:41:15 +02001123 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001124 ++len;
1125 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02001126 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001127 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001128 }
1129 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001130 ++expr;
1131
Bram Moolenaar7f2c3412021-11-29 16:01:49 +00001132 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
Bram Moolenaarc7e44a72020-07-29 21:37:43 +02001133 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001134 {
1135 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01001136 semsg(_(e_white_space_required_before_and_after_str_at_str),
1137 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001138 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02001139
1140 if (eap->skip)
1141 ++emsg_skip;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001142 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaarc7e44a72020-07-29 21:37:43 +02001143 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaar9a562c12021-01-23 13:39:14 +01001144 cur_lnum = SOURCING_LNUM;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001145 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001146 if (eap->skip)
1147 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001148 clear_evalarg(&evalarg, eap);
Bram Moolenaar9a562c12021-01-23 13:39:14 +01001149
1150 // Restore the line number so that any type error is given for the
1151 // declaration, not the expression.
1152 SOURCING_LNUM = cur_lnum;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001153 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001154 if (eap->skip)
1155 {
1156 if (i != FAIL)
1157 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001158 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001159 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001160 {
1161 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001162 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001163 clear_tv(&rettv);
1164 }
1165 }
1166}
1167
1168/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001169 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001170 * Handles both "var" with any type and "[var, var; var]" with a list type.
1171 * When "op" is not NULL it points to a string with characters that
1172 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1173 * or concatenate.
1174 * Returns OK or FAIL;
1175 */
1176 int
1177ex_let_vars(
1178 char_u *arg_start,
1179 typval_T *tv,
1180 int copy, // copy values from "tv", don't move
1181 int semicolon, // from skip_var_list()
1182 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001183 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001184 char_u *op)
1185{
1186 char_u *arg = arg_start;
1187 list_T *l;
1188 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001189 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001190 listitem_T *item;
1191 typval_T ltv;
1192
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00001193 if (tv->v_type == VAR_VOID)
1194 {
1195 emsg(_(e_cannot_use_void_value));
1196 return FAIL;
1197 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001198 if (*arg != '[')
1199 {
1200 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001201 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001202 return FAIL;
1203 return OK;
1204 }
1205
1206 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1207 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
1208 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001209 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001210 return FAIL;
1211 }
1212
1213 i = list_len(l);
1214 if (semicolon == 0 && var_count < i)
1215 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001216 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001217 return FAIL;
1218 }
1219 if (var_count - semicolon > i)
1220 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001221 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001222 return FAIL;
1223 }
1224
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001225 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001226 item = l->lv_first;
1227 while (*arg != ']')
1228 {
1229 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001230 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001231 arg = ex_let_one(arg, &item->li_tv, TRUE,
1232 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001233 item = item->li_next;
1234 if (arg == NULL)
1235 return FAIL;
1236
1237 arg = skipwhite(arg);
1238 if (*arg == ';')
1239 {
1240 // Put the rest of the list (may be empty) in the var after ';'.
1241 // Create a new list for this.
1242 l = list_alloc();
1243 if (l == NULL)
1244 return FAIL;
1245 while (item != NULL)
1246 {
1247 list_append_tv(l, &item->li_tv);
1248 item = item->li_next;
1249 }
1250
1251 ltv.v_type = VAR_LIST;
1252 ltv.v_lock = 0;
1253 ltv.vval.v_list = l;
1254 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001255 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001256
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001257 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1258 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001259 clear_tv(&ltv);
1260 if (arg == NULL)
1261 return FAIL;
1262 break;
1263 }
1264 else if (*arg != ',' && *arg != ']')
1265 {
1266 internal_error("ex_let_vars()");
1267 return FAIL;
1268 }
1269 }
1270
1271 return OK;
1272}
1273
1274/*
1275 * Skip over assignable variable "var" or list of variables "[var, var]".
1276 * Used for ":let varvar = expr" and ":for varvar in expr".
1277 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001278 * for "[var, var; var]" set "semicolon" to 1.
1279 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001280 * Return NULL for an error.
1281 */
1282 char_u *
1283skip_var_list(
1284 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001286 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001287 int *semicolon,
1288 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001289{
1290 char_u *p, *s;
1291
1292 if (*arg == '[')
1293 {
1294 // "[var, var]": find the matching ']'.
1295 p = arg;
1296 for (;;)
1297 {
1298 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001299 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001300 if (s == p)
1301 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001302 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001303 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001304 return NULL;
1305 }
1306 ++*var_count;
1307
1308 p = skipwhite(s);
1309 if (*p == ']')
1310 break;
1311 else if (*p == ';')
1312 {
1313 if (*semicolon == 1)
1314 {
Bram Moolenaar8b716f52022-02-15 21:17:56 +00001315 if (!silent)
1316 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001317 return NULL;
1318 }
1319 *semicolon = 1;
1320 }
1321 else if (*p != ',')
1322 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001323 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001324 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001325 return NULL;
1326 }
1327 }
1328 return p + 1;
1329 }
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01001330
Bram Moolenaare8e369a2022-09-21 18:59:14 +01001331 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001332}
1333
1334/*
1335 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1336 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001338 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001339 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001341{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001342 char_u *end;
1343 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001345 if (*arg == '@' && arg[1] != NUL)
1346 return arg + 2;
Bram Moolenaar1573e732022-11-16 20:33:21 +00001347
1348 // termcap option name may have non-alpha characters
1349 if (STRNCMP(arg, "&t_", 3) == 0 && arg[3] != NUL && arg[4] != NUL)
1350 return arg + 5;
1351
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001353 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001354
1355 // "a: type" is declaring variable "a" with a type, not "a:".
1356 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001357 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001358 --end;
1359
Bram Moolenaar585587d2021-01-17 20:52:13 +01001360 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001362 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001363 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364 }
1365 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001366}
1367
1368/*
1369 * List variables for hashtab "ht" with prefix "prefix".
1370 * If "empty" is TRUE also list NULL strings as empty strings.
1371 */
1372 void
1373list_hashtable_vars(
1374 hashtab_T *ht,
1375 char *prefix,
1376 int empty,
1377 int *first)
1378{
1379 hashitem_T *hi;
1380 dictitem_T *di;
1381 int todo;
1382 char_u buf[IOSIZE];
1383
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001384 int save_ht_flags = ht->ht_flags;
1385 ht->ht_flags |= HTFLAGS_FROZEN;
1386
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001387 todo = (int)ht->ht_used;
1388 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1389 {
1390 if (!HASHITEM_EMPTY(hi))
1391 {
1392 --todo;
1393 di = HI2DI(hi);
1394
1395 // apply :filter /pat/ to variable name
1396 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1397 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1398 if (message_filtered(buf))
1399 continue;
1400
1401 if (empty || di->di_tv.v_type != VAR_STRING
1402 || di->di_tv.vval.v_string != NULL)
1403 list_one_var(di, prefix, first);
1404 }
1405 }
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001406
1407 ht->ht_flags = save_ht_flags;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001408}
1409
1410/*
1411 * List global variables.
1412 */
1413 static void
1414list_glob_vars(int *first)
1415{
1416 list_hashtable_vars(&globvarht, "", TRUE, first);
1417}
1418
1419/*
1420 * List buffer variables.
1421 */
1422 static void
1423list_buf_vars(int *first)
1424{
1425 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1426}
1427
1428/*
1429 * List window variables.
1430 */
1431 static void
1432list_win_vars(int *first)
1433{
1434 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1435}
1436
1437/*
1438 * List tab page variables.
1439 */
1440 static void
1441list_tab_vars(int *first)
1442{
1443 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1444}
1445
1446/*
1447 * List variables in "arg".
1448 */
1449 static char_u *
1450list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1451{
1452 int error = FALSE;
1453 int len;
1454 char_u *name;
1455 char_u *name_start;
1456 char_u *arg_subsc;
1457 char_u *tofree;
1458 typval_T tv;
1459
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001460 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001461 {
1462 if (error || eap->skip)
1463 {
1464 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1465 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1466 {
1467 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001468 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001469 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001470 break;
1471 }
1472 }
1473 else
1474 {
1475 // get_name_len() takes care of expanding curly braces
1476 name_start = name = arg;
1477 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1478 if (len <= 0)
1479 {
1480 // This is mainly to keep test 49 working: when expanding
1481 // curly braces fails overrule the exception error message.
1482 if (len < 0 && !aborting())
1483 {
1484 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001485 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001486 break;
1487 }
1488 error = TRUE;
1489 }
1490 else
1491 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001492 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001493 if (tofree != NULL)
1494 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001495 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001496 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001497 error = TRUE;
1498 else
1499 {
1500 // handle d.key, l[idx], f(expr)
1501 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001502 if (handle_subscript(&arg, name_start, &tv,
1503 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001504 error = TRUE;
1505 else
1506 {
1507 if (arg == arg_subsc && len == 2 && name[1] == ':')
1508 {
1509 switch (*name)
1510 {
1511 case 'g': list_glob_vars(first); break;
1512 case 'b': list_buf_vars(first); break;
1513 case 'w': list_win_vars(first); break;
1514 case 't': list_tab_vars(first); break;
1515 case 'v': list_vim_vars(first); break;
1516 case 's': list_script_vars(first); break;
1517 case 'l': list_func_vars(first); break;
1518 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001519 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001520 }
1521 }
1522 else
1523 {
1524 char_u numbuf[NUMBUFLEN];
1525 char_u *tf;
1526 int c;
1527 char_u *s;
1528
1529 s = echo_string(&tv, &tf, numbuf, 0);
1530 c = *arg;
1531 *arg = NUL;
1532 list_one_var_a("",
1533 arg == arg_subsc ? name : name_start,
1534 tv.v_type,
1535 s == NULL ? (char_u *)"" : s,
1536 first);
1537 *arg = c;
1538 vim_free(tf);
1539 }
1540 clear_tv(&tv);
1541 }
1542 }
1543 }
1544
1545 vim_free(tofree);
1546 }
1547
1548 arg = skipwhite(arg);
1549 }
1550
1551 return arg;
1552}
1553
1554/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001555 * Set an environment variable, part of ex_let_one().
1556 */
1557 static char_u *
1558ex_let_env(
1559 char_u *arg,
1560 typval_T *tv,
1561 int flags,
1562 char_u *endchars,
1563 char_u *op)
1564{
1565 char_u *arg_end = NULL;
1566 char_u *name;
1567 int len;
1568
1569 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1570 && (flags & ASSIGN_FOR_LOOP) == 0)
1571 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001572 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001573 return NULL;
1574 }
1575
1576 // Find the end of the name.
1577 ++arg;
1578 name = arg;
1579 len = get_env_len(&arg);
1580 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001581 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001582 else
1583 {
1584 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001585 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001586 else if (endchars != NULL
1587 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1588 emsg(_(e_unexpected_characters_in_let));
1589 else if (!check_secure())
1590 {
1591 char_u *tofree = NULL;
1592 int c1 = name[len];
1593 char_u *p;
1594
1595 name[len] = NUL;
1596 p = tv_get_string_chk(tv);
1597 if (p != NULL && op != NULL && *op == '.')
1598 {
1599 int mustfree = FALSE;
1600 char_u *s = vim_getenv(name, &mustfree);
1601
1602 if (s != NULL)
1603 {
1604 p = tofree = concat_str(s, p);
1605 if (mustfree)
1606 vim_free(s);
1607 }
1608 }
1609 if (p != NULL)
1610 {
1611 vim_setenv_ext(name, p);
1612 arg_end = arg;
1613 }
1614 name[len] = c1;
1615 vim_free(tofree);
1616 }
1617 }
1618 return arg_end;
1619}
1620
1621/*
1622 * Set an option, part of ex_let_one().
1623 */
1624 static char_u *
1625ex_let_option(
1626 char_u *arg,
1627 typval_T *tv,
1628 int flags,
1629 char_u *endchars,
1630 char_u *op)
1631{
1632 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001633 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001634 char_u *arg_end = NULL;
1635
1636 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1637 && (flags & ASSIGN_FOR_LOOP) == 0)
1638 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001639 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001640 return NULL;
1641 }
1642
1643 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001644 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001645 if (p == NULL || (endchars != NULL
1646 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1647 emsg(_(e_unexpected_characters_in_let));
1648 else
1649 {
1650 int c1;
1651 long n = 0;
1652 getoption_T opt_type;
1653 long numval;
1654 char_u *stringval = NULL;
1655 char_u *s = NULL;
1656 int failed = FALSE;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001657 int opt_p_flags;
1658 char_u *tofree = NULL;
Bram Moolenaar92c33eb2021-12-07 11:03:39 +00001659 char_u numbuf[NUMBUFLEN];
1660
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001661 c1 = *p;
1662 *p = NUL;
1663
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001664 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags,
1665 scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001666 if ((opt_type == gov_bool
1667 || opt_type == gov_number
1668 || opt_type == gov_hidden_bool
1669 || opt_type == gov_hidden_number)
1670 && (tv->v_type != VAR_STRING || !in_vim9script()))
1671 {
1672 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1673 // bool, possibly hidden
1674 n = (long)tv_get_bool(tv);
1675 else
1676 // number, possibly hidden
1677 n = (long)tv_get_number(tv);
1678 }
1679
Bram Moolenaaref082e12021-12-12 21:02:03 +00001680 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001681 || tv->v_type == VAR_FUNC))
1682 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001683 // If the option can be set to a function reference or a lambda
1684 // and the passed value is a function reference, then convert it to
1685 // the name (string) of the function reference.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001686 s = tv2string(tv, &tofree, numbuf, 0);
1687 }
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001688 // Avoid setting a string option to the text "v:false" or similar.
1689 // In Vim9 script also don't convert a number to string.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001690 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001691 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1692 s = tv_get_string_chk(tv);
1693
1694 if (op != NULL && *op != '=')
1695 {
1696 if (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1697 || (opt_type == gov_string && *op != '.'))
1698 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001699 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001700 failed = TRUE; // don't set the value
1701
1702 }
1703 else
1704 {
1705 // number, in legacy script also bool
1706 if (opt_type == gov_number
1707 || (opt_type == gov_bool && !in_vim9script()))
1708 {
1709 switch (*op)
1710 {
1711 case '+': n = numval + n; break;
1712 case '-': n = numval - n; break;
1713 case '*': n = numval * n; break;
1714 case '/': n = (long)num_divide(numval, n,
1715 &failed); break;
1716 case '%': n = (long)num_modulus(numval, n,
1717 &failed); break;
1718 }
1719 s = NULL;
1720 }
1721 else if (opt_type == gov_string
1722 && stringval != NULL && s != NULL)
1723 {
1724 // string
1725 s = concat_str(stringval, s);
1726 vim_free(stringval);
1727 stringval = s;
1728 }
1729 }
1730 }
1731
1732 if (!failed)
1733 {
1734 if (opt_type != gov_string || s != NULL)
1735 {
Bram Moolenaar8ccbbeb2022-03-02 19:49:38 +00001736 char *err = set_option_value(arg, n, s, scope);
1737
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001738 arg_end = p;
Bram Moolenaar8ccbbeb2022-03-02 19:49:38 +00001739 if (err != NULL)
1740 emsg(_(err));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001741 }
1742 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001743 emsg(_(e_string_required));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001744 }
1745 *p = c1;
1746 vim_free(stringval);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001747 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001748 }
1749 return arg_end;
1750}
1751
1752/*
1753 * Set a register, part of ex_let_one().
1754 */
1755 static char_u *
1756ex_let_register(
1757 char_u *arg,
1758 typval_T *tv,
1759 int flags,
1760 char_u *endchars,
1761 char_u *op)
1762{
1763 char_u *arg_end = NULL;
1764
1765 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1766 && (flags & ASSIGN_FOR_LOOP) == 0)
1767 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001768 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001769 return NULL;
1770 }
1771 ++arg;
1772 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001773 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001774 else if (endchars != NULL
1775 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1776 emsg(_(e_unexpected_characters_in_let));
1777 else
1778 {
1779 char_u *ptofree = NULL;
1780 char_u *p;
1781
1782 p = tv_get_string_chk(tv);
1783 if (p != NULL && op != NULL && *op == '.')
1784 {
1785 char_u *s = get_reg_contents(*arg == '@'
1786 ? '"' : *arg, GREG_EXPR_SRC);
1787
1788 if (s != NULL)
1789 {
1790 p = ptofree = concat_str(s, p);
1791 vim_free(s);
1792 }
1793 }
1794 if (p != NULL)
1795 {
1796 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1797 arg_end = arg + 1;
1798 }
1799 vim_free(ptofree);
1800 }
1801 return arg_end;
1802}
1803
1804/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001805 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1806 * Returns a pointer to the char just after the var name.
1807 * Returns NULL if there is an error.
1808 */
1809 static char_u *
1810ex_let_one(
1811 char_u *arg, // points to variable name
1812 typval_T *tv, // value to assign to variable
1813 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001814 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001815 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001816 char_u *op, // "+", "-", "." or NULL
1817 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001818{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001819 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001820
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001821 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001822 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001823 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1824 {
1825 vim9_declare_error(arg);
1826 return NULL;
1827 }
1828
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001829 if (*arg == '$')
1830 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001831 // ":let $VAR = expr": Set environment variable.
1832 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001833 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001834 else if (*arg == '&')
1835 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001836 // ":let &option = expr": Set option value.
1837 // ":let &l:option = expr": Set local option value.
1838 // ":let &g:option = expr": Set global option value.
1839 // ":for &ts in range(8)": Set option value for for loop
1840 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001841 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001842 else if (*arg == '@')
1843 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001844 // ":let @r = expr": Set register contents.
1845 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001846 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001847 else if (eval_isnamec1(*arg) || *arg == '{')
1848 {
1849 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001850 char_u *p;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001851 int lval_flags = (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1852 ? GLV_NO_DECL : 0;
1853 if (op != NULL && *op != '=')
1854 lval_flags |= GLV_ASSIGN_WITH_OP;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001855
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001856 // ":let var = expr": Set internal variable.
1857 // ":let var: type = expr": Set internal variable with type.
1858 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001859 p = get_lval(arg, tv, &lv, FALSE, FALSE, lval_flags, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001860 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001861 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001862 if (endchars != NULL && vim_strchr(endchars,
1863 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001864 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001865 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001866 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001867 else
1868 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001869 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001870 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001871 }
1872 }
1873 clear_lval(&lv);
1874 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001875 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001876 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001877
1878 return arg_end;
1879}
1880
1881/*
1882 * ":unlet[!] var1 ... " command.
1883 */
1884 void
1885ex_unlet(exarg_T *eap)
1886{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001887 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001888}
1889
1890/*
1891 * ":lockvar" and ":unlockvar" commands
1892 */
1893 void
1894ex_lockvar(exarg_T *eap)
1895{
1896 char_u *arg = eap->arg;
1897 int deep = 2;
1898
1899 if (eap->forceit)
1900 deep = -1;
1901 else if (vim_isdigit(*arg))
1902 {
1903 deep = getdigits(&arg);
1904 arg = skipwhite(arg);
1905 }
1906
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001907 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001908}
1909
1910/*
1911 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001912 * Also used for Vim9 script. "callback" is invoked as:
1913 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001914 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001915 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001916ex_unletlock(
1917 exarg_T *eap,
1918 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001919 int deep,
1920 int glv_flags,
1921 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1922 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001923{
1924 char_u *arg = argstart;
1925 char_u *name_end;
1926 int error = FALSE;
1927 lval_T lv;
1928
1929 do
1930 {
1931 if (*arg == '$')
1932 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001933 lv.ll_name = arg;
1934 lv.ll_tv = NULL;
1935 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001936 if (get_env_len(&arg) == 0)
1937 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001938 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001939 return;
1940 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001941 if (!error && !eap->skip
1942 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1943 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001944 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001945 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001946 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001947 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001948 // Parse the name and find the end.
1949 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001950 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001951 if (lv.ll_name == NULL)
1952 error = TRUE; // error but continue parsing
1953 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1954 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001955 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001956 if (name_end != NULL)
1957 {
1958 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00001959 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001960 }
1961 if (!(eap->skip || error))
1962 clear_lval(&lv);
1963 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001964 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001965
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001966 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001967 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001968 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001969
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001970 if (!eap->skip)
1971 clear_lval(&lv);
1972 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001973
1974 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001975 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001976
Bram Moolenaar63b91732021-08-05 20:40:03 +02001977 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001978}
1979
1980 static int
1981do_unlet_var(
1982 lval_T *lp,
1983 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001984 exarg_T *eap,
1985 int deep UNUSED,
1986 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001987{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001988 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001989 int ret = OK;
1990 int cc;
1991
1992 if (lp->ll_tv == NULL)
1993 {
1994 cc = *name_end;
1995 *name_end = NUL;
1996
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001997 // Environment variable, normal name or expanded name.
1998 if (*lp->ll_name == '$')
LemonBoy77142312022-04-15 20:50:46 +01001999 vim_unsetenv_ext(lp->ll_name + 1);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002000 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002001 ret = FAIL;
2002 *name_end = cc;
2003 }
2004 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002005 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002006 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002007 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002008 return FAIL;
2009 else if (lp->ll_range)
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002010 list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_n1,
2011 !lp->ll_empty2, lp->ll_n2);
2012 else if (lp->ll_list != NULL)
2013 // unlet a List item.
2014 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002015 else
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002016 // unlet a Dictionary item.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002017 dictitem_remove(lp->ll_dict, lp->ll_di, "unlet");
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002018
2019 return ret;
2020}
2021
2022/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002023 * Unlet one item or a range of items from a list.
2024 * Return OK or FAIL.
2025 */
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002026 void
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002027list_unlet_range(
2028 list_T *l,
2029 listitem_T *li_first,
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002030 long n1_arg,
2031 int has_n2,
2032 long n2)
2033{
2034 listitem_T *li = li_first;
2035 int n1 = n1_arg;
2036
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002037 // Delete a range of List items.
2038 li = li_first;
2039 n1 = n1_arg;
2040 while (li != NULL && (!has_n2 || n2 >= n1))
2041 {
2042 listitem_T *next = li->li_next;
2043
2044 listitem_remove(l, li);
2045 li = next;
2046 ++n1;
2047 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002048}
2049/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002050 * "unlet" a variable. Return OK if it existed, FAIL if not.
2051 * When "forceit" is TRUE don't complain if the variable doesn't exist.
2052 */
2053 int
2054do_unlet(char_u *name, int forceit)
2055{
2056 hashtab_T *ht;
2057 hashitem_T *hi;
mityu8be36ee2022-05-25 17:29:46 +01002058 char_u *varname = NULL; // init to shut up gcc
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002059 dict_T *d;
2060 dictitem_T *di;
2061
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002062 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002063 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002064 return FAIL;
2065
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002066 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002067
2068 // can't :unlet a script variable in Vim9 script from a function
2069 if (ht == get_script_local_ht()
2070 && SCRIPT_ID_VALID(current_sctx.sc_sid)
2071 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2072 == SCRIPT_VERSION_VIM9
2073 && check_vim9_unlet(name) == FAIL)
2074 return FAIL;
2075
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002076 if (ht != NULL && *varname != NUL)
2077 {
2078 d = get_current_funccal_dict(ht);
2079 if (d == NULL)
2080 {
2081 if (ht == &globvarht)
2082 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002083 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002084 d = &vimvardict;
2085 else
2086 {
2087 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2088 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2089 }
2090 if (d == NULL)
2091 {
2092 internal_error("do_unlet()");
2093 return FAIL;
2094 }
2095 }
2096 hi = hash_find(ht, varname);
2097 if (HASHITEM_EMPTY(hi))
2098 hi = find_hi_in_scoped_ht(name, &ht);
2099 if (hi != NULL && !HASHITEM_EMPTY(hi))
2100 {
2101 di = HI2DI(hi);
2102 if (var_check_fixed(di->di_flags, name, FALSE)
2103 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002104 || value_check_lock(d->dv_lock, name, FALSE)
2105 || check_hashtab_frozen(ht, "unlet"))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002106 return FAIL;
2107
2108 delete_var(ht, hi);
2109 return OK;
2110 }
2111 }
2112 if (forceit)
2113 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00002114 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002115 return FAIL;
2116}
2117
2118/*
2119 * Lock or unlock variable indicated by "lp".
2120 * "deep" is the levels to go (-1 for unlimited);
2121 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2122 */
2123 static int
2124do_lock_var(
2125 lval_T *lp,
2126 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002127 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002128 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002129 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002130{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002131 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002132 int ret = OK;
2133 int cc;
2134 dictitem_T *di;
2135
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002136 if (lp->ll_tv == NULL)
2137 {
2138 cc = *name_end;
2139 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002140 if (*lp->ll_name == '$')
2141 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002142 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002143 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002144 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002145 else
2146 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002147 // Normal name or expanded name.
2148 di = find_var(lp->ll_name, NULL, TRUE);
2149 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002150 {
2151 if (in_vim9script())
2152 semsg(_(e_cannot_find_variable_to_unlock_str),
2153 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002154 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002155 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002156 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002157 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002158 if ((di->di_flags & DI_FLAGS_FIX)
2159 && di->di_tv.v_type != VAR_DICT
2160 && di->di_tv.v_type != VAR_LIST)
2161 {
2162 // For historic reasons this error is not given for a list
2163 // or dict. E.g., the b: dict could be locked/unlocked.
2164 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
2165 ret = FAIL;
2166 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002167 else
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002168 {
2169 if (in_vim9script())
2170 {
2171 svar_T *sv = find_typval_in_script(&di->di_tv,
2172 0, FALSE);
2173
2174 if (sv != NULL && sv->sv_const != 0)
2175 {
2176 semsg(_(e_cannot_change_readonly_variable_str),
2177 lp->ll_name);
2178 ret = FAIL;
2179 }
2180 }
2181
2182 if (ret == OK)
2183 {
2184 if (lock)
2185 di->di_flags |= DI_FLAGS_LOCK;
2186 else
2187 di->di_flags &= ~DI_FLAGS_LOCK;
2188 if (deep != 0)
2189 item_lock(&di->di_tv, deep, lock, FALSE);
2190 }
2191 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002192 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002193 }
2194 *name_end = cc;
2195 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002196 else if (deep == 0)
2197 {
2198 // nothing to do
2199 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002200 else if (lp->ll_range)
2201 {
2202 listitem_T *li = lp->ll_li;
2203
2204 // (un)lock a range of List items.
2205 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2206 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02002207 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002208 li = li->li_next;
2209 ++lp->ll_n1;
2210 }
2211 }
2212 else if (lp->ll_list != NULL)
2213 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002214 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002215 else
2216 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002217 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002218
2219 return ret;
2220}
2221
2222/*
2223 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002224 * When "check_refcount" is TRUE do not lock a list or dict with a reference
2225 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002226 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002227 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02002228item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002229{
2230 static int recurse = 0;
2231 list_T *l;
2232 listitem_T *li;
2233 dict_T *d;
2234 blob_T *b;
2235 hashitem_T *hi;
2236 int todo;
2237
2238 if (recurse >= DICT_MAXNEST)
2239 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002240 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002241 return;
2242 }
2243 if (deep == 0)
2244 return;
2245 ++recurse;
2246
2247 // lock/unlock the item itself
2248 if (lock)
2249 tv->v_lock |= VAR_LOCKED;
2250 else
2251 tv->v_lock &= ~VAR_LOCKED;
2252
2253 switch (tv->v_type)
2254 {
2255 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002256 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002258 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002260 case VAR_STRING:
2261 case VAR_FUNC:
2262 case VAR_PARTIAL:
2263 case VAR_FLOAT:
2264 case VAR_SPECIAL:
2265 case VAR_JOB:
2266 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002267 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002268 case VAR_CLASS:
2269 case VAR_OBJECT:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002270 break;
2271
2272 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002273 if ((b = tv->vval.v_blob) != NULL
2274 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002275 {
2276 if (lock)
2277 b->bv_lock |= VAR_LOCKED;
2278 else
2279 b->bv_lock &= ~VAR_LOCKED;
2280 }
2281 break;
2282 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002283 if ((l = tv->vval.v_list) != NULL
2284 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002285 {
2286 if (lock)
2287 l->lv_lock |= VAR_LOCKED;
2288 else
2289 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002290 if (deep < 0 || deep > 1)
2291 {
2292 if (l->lv_first == &range_list_item)
2293 l->lv_lock |= VAR_ITEMS_LOCKED;
2294 else
2295 {
2296 // recursive: lock/unlock the items the List contains
2297 CHECK_LIST_MATERIALIZE(l);
2298 FOR_ALL_LIST_ITEMS(l, li) item_lock(&li->li_tv,
2299 deep - 1, lock, check_refcount);
2300 }
2301 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002302 }
2303 break;
2304 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002305 if ((d = tv->vval.v_dict) != NULL
2306 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002307 {
2308 if (lock)
2309 d->dv_lock |= VAR_LOCKED;
2310 else
2311 d->dv_lock &= ~VAR_LOCKED;
2312 if (deep < 0 || deep > 1)
2313 {
2314 // recursive: lock/unlock the items the List contains
2315 todo = (int)d->dv_hashtab.ht_used;
2316 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2317 {
2318 if (!HASHITEM_EMPTY(hi))
2319 {
2320 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002321 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2322 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002323 }
2324 }
2325 }
2326 }
2327 }
2328 --recurse;
2329}
2330
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002331#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2332/*
2333 * Delete all "menutrans_" variables.
2334 */
2335 void
2336del_menutrans_vars(void)
2337{
2338 hashitem_T *hi;
2339 int todo;
2340
2341 hash_lock(&globvarht);
2342 todo = (int)globvarht.ht_used;
2343 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2344 {
2345 if (!HASHITEM_EMPTY(hi))
2346 {
2347 --todo;
2348 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2349 delete_var(&globvarht, hi);
2350 }
2351 }
2352 hash_unlock(&globvarht);
2353}
2354#endif
2355
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002356/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002357 * Local string buffer for the next two functions to store a variable name
2358 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2359 * get_user_var_name().
2360 */
2361
2362static char_u *varnamebuf = NULL;
2363static int varnamebuflen = 0;
2364
2365/*
2366 * Function to concatenate a prefix and a variable name.
2367 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002368 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002369cat_prefix_varname(int prefix, char_u *name)
2370{
2371 int len;
2372
2373 len = (int)STRLEN(name) + 3;
2374 if (len > varnamebuflen)
2375 {
2376 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002377 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002378 varnamebuf = alloc(len);
2379 if (varnamebuf == NULL)
2380 {
2381 varnamebuflen = 0;
2382 return NULL;
2383 }
2384 varnamebuflen = len;
2385 }
2386 *varnamebuf = prefix;
2387 varnamebuf[1] = ':';
2388 STRCPY(varnamebuf + 2, name);
2389 return varnamebuf;
2390}
2391
2392/*
2393 * Function given to ExpandGeneric() to obtain the list of user defined
2394 * (global/buffer/window/built-in) variable names.
2395 */
2396 char_u *
2397get_user_var_name(expand_T *xp, int idx)
2398{
2399 static long_u gdone;
2400 static long_u bdone;
2401 static long_u wdone;
2402 static long_u tdone;
2403 static int vidx;
2404 static hashitem_T *hi;
2405 hashtab_T *ht;
2406
2407 if (idx == 0)
2408 {
2409 gdone = bdone = wdone = vidx = 0;
2410 tdone = 0;
2411 }
2412
2413 // Global variables
2414 if (gdone < globvarht.ht_used)
2415 {
2416 if (gdone++ == 0)
2417 hi = globvarht.ht_array;
2418 else
2419 ++hi;
2420 while (HASHITEM_EMPTY(hi))
2421 ++hi;
2422 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2423 return cat_prefix_varname('g', hi->hi_key);
2424 return hi->hi_key;
2425 }
2426
2427 // b: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002428 ht = &prevwin_curwin()->w_buffer->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002429 if (bdone < ht->ht_used)
2430 {
2431 if (bdone++ == 0)
2432 hi = ht->ht_array;
2433 else
2434 ++hi;
2435 while (HASHITEM_EMPTY(hi))
2436 ++hi;
2437 return cat_prefix_varname('b', hi->hi_key);
2438 }
2439
2440 // w: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002441 ht = &prevwin_curwin()->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002442 if (wdone < ht->ht_used)
2443 {
2444 if (wdone++ == 0)
2445 hi = ht->ht_array;
2446 else
2447 ++hi;
2448 while (HASHITEM_EMPTY(hi))
2449 ++hi;
2450 return cat_prefix_varname('w', hi->hi_key);
2451 }
2452
2453 // t: variables
2454 ht = &curtab->tp_vars->dv_hashtab;
2455 if (tdone < ht->ht_used)
2456 {
2457 if (tdone++ == 0)
2458 hi = ht->ht_array;
2459 else
2460 ++hi;
2461 while (HASHITEM_EMPTY(hi))
2462 ++hi;
2463 return cat_prefix_varname('t', hi->hi_key);
2464 }
2465
2466 // v: variables
2467 if (vidx < VV_LEN)
2468 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2469
2470 VIM_CLEAR(varnamebuf);
2471 varnamebuflen = 0;
2472 return NULL;
2473}
2474
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002475 char *
2476get_var_special_name(int nr)
2477{
2478 switch (nr)
2479 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002480 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2481 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002482 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002483 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002484 }
2485 internal_error("get_var_special_name()");
2486 return "42";
2487}
2488
2489/*
2490 * Returns the global variable dictionary
2491 */
2492 dict_T *
2493get_globvar_dict(void)
2494{
2495 return &globvardict;
2496}
2497
2498/*
2499 * Returns the global variable hash table
2500 */
2501 hashtab_T *
2502get_globvar_ht(void)
2503{
2504 return &globvarht;
2505}
2506
2507/*
2508 * Returns the v: variable dictionary
2509 */
2510 dict_T *
2511get_vimvar_dict(void)
2512{
2513 return &vimvardict;
2514}
2515
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002516/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002518 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 */
2520 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002521find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002523 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2524 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525
2526 if (di == NULL)
2527 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002528 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2530 return (int)(vv - vimvars);
2531}
2532
2533
2534/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002535 * Set type of v: variable to "type".
2536 */
2537 void
2538set_vim_var_type(int idx, vartype_T type)
2539{
Bram Moolenaard787e402021-12-24 21:36:12 +00002540 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002541}
2542
2543/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002544 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002545 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002546 */
2547 void
2548set_vim_var_nr(int idx, varnumber_T val)
2549{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002550 vimvars[idx].vv_nr = val;
2551}
2552
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 char *
2554get_vim_var_name(int idx)
2555{
2556 return vimvars[idx].vv_name;
2557}
2558
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002559/*
2560 * Get typval_T v: variable value.
2561 */
2562 typval_T *
2563get_vim_var_tv(int idx)
2564{
2565 return &vimvars[idx].vv_tv;
2566}
2567
Bram Moolenaard787e402021-12-24 21:36:12 +00002568 type_T *
2569get_vim_var_type(int idx, garray_T *type_list)
2570{
2571 if (vimvars[idx].vv_type != NULL)
2572 return vimvars[idx].vv_type;
2573 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2574}
2575
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002576/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002577 * Set v: variable to "tv". Only accepts the same type.
2578 * Takes over the value of "tv".
2579 */
2580 int
2581set_vim_var_tv(int idx, typval_T *tv)
2582{
Bram Moolenaard787e402021-12-24 21:36:12 +00002583 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002584 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002585 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002586 clear_tv(tv);
2587 return FAIL;
2588 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002589 // VV_RO is also checked when compiling, but let's check here as well.
2590 if (vimvars[idx].vv_flags & VV_RO)
2591 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002592 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002593 return FAIL;
2594 }
2595 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2596 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002597 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002598 return FAIL;
2599 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002600 clear_tv(&vimvars[idx].vv_di.di_tv);
2601 vimvars[idx].vv_di.di_tv = *tv;
2602 return OK;
2603}
2604
2605/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002606 * Get number v: variable value.
2607 */
2608 varnumber_T
2609get_vim_var_nr(int idx)
2610{
2611 return vimvars[idx].vv_nr;
2612}
2613
2614/*
2615 * Get string v: variable value. Uses a static buffer, can only be used once.
2616 * If the String variable has never been set, return an empty string.
2617 * Never returns NULL;
2618 */
2619 char_u *
2620get_vim_var_str(int idx)
2621{
2622 return tv_get_string(&vimvars[idx].vv_tv);
2623}
2624
2625/*
2626 * Get List v: variable value. Caller must take care of reference count when
2627 * needed.
2628 */
2629 list_T *
2630get_vim_var_list(int idx)
2631{
2632 return vimvars[idx].vv_list;
2633}
2634
2635/*
2636 * Get Dict v: variable value. Caller must take care of reference count when
2637 * needed.
2638 */
2639 dict_T *
2640get_vim_var_dict(int idx)
2641{
2642 return vimvars[idx].vv_dict;
2643}
2644
2645/*
2646 * Set v:char to character "c".
2647 */
2648 void
2649set_vim_var_char(int c)
2650{
2651 char_u buf[MB_MAXBYTES + 1];
2652
2653 if (has_mbyte)
2654 buf[(*mb_char2bytes)(c, buf)] = NUL;
2655 else
2656 {
2657 buf[0] = c;
2658 buf[1] = NUL;
2659 }
2660 set_vim_var_string(VV_CHAR, buf, -1);
2661}
2662
2663/*
2664 * Set v:count to "count" and v:count1 to "count1".
2665 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2666 */
2667 void
2668set_vcount(
2669 long count,
2670 long count1,
2671 int set_prevcount)
2672{
2673 if (set_prevcount)
2674 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2675 vimvars[VV_COUNT].vv_nr = count;
2676 vimvars[VV_COUNT1].vv_nr = count1;
2677}
2678
2679/*
2680 * Save variables that might be changed as a side effect. Used when executing
2681 * a timer callback.
2682 */
2683 void
2684save_vimvars(vimvars_save_T *vvsave)
2685{
2686 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2687 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2688 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2689}
2690
2691/*
2692 * Restore variables saved by save_vimvars().
2693 */
2694 void
2695restore_vimvars(vimvars_save_T *vvsave)
2696{
2697 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2698 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2699 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2700}
2701
2702/*
2703 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2704 * value.
2705 */
2706 void
2707set_vim_var_string(
2708 int idx,
2709 char_u *val,
2710 int len) // length of "val" to use or -1 (whole string)
2711{
2712 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002713 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002714 if (val == NULL)
2715 vimvars[idx].vv_str = NULL;
2716 else if (len == -1)
2717 vimvars[idx].vv_str = vim_strsave(val);
2718 else
2719 vimvars[idx].vv_str = vim_strnsave(val, len);
2720}
2721
2722/*
2723 * Set List v: variable to "val".
2724 */
2725 void
2726set_vim_var_list(int idx, list_T *val)
2727{
2728 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002729 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002730 vimvars[idx].vv_list = val;
2731 if (val != NULL)
2732 ++val->lv_refcount;
2733}
2734
2735/*
2736 * Set Dictionary v: variable to "val".
2737 */
2738 void
2739set_vim_var_dict(int idx, dict_T *val)
2740{
2741 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002742 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002743 vimvars[idx].vv_dict = val;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002744 if (val == NULL)
2745 return;
2746
2747 ++val->dv_refcount;
2748 dict_set_items_ro(val);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002749}
2750
2751/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002752 * Set the v:argv list.
2753 */
2754 void
2755set_argv_var(char **argv, int argc)
2756{
2757 list_T *l = list_alloc();
2758 int i;
2759
2760 if (l == NULL)
2761 getout(1);
2762 l->lv_lock = VAR_FIXED;
2763 for (i = 0; i < argc; ++i)
2764 {
2765 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2766 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002767 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002768 }
2769 set_vim_var_list(VV_ARGV, l);
2770}
2771
2772/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002773 * Reset v:register, taking the 'clipboard' setting into account.
2774 */
2775 void
2776reset_reg_var(void)
2777{
2778 int regname = 0;
2779
2780 // Adjust the register according to 'clipboard', so that when
2781 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2782#ifdef FEAT_CLIPBOARD
2783 adjust_clip_reg(&regname);
2784#endif
2785 set_reg_var(regname);
2786}
2787
2788/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002789 * Set v:register if needed.
2790 */
2791 void
2792set_reg_var(int c)
2793{
2794 char_u regname;
2795
2796 if (c == 0 || c == ' ')
2797 regname = '"';
2798 else
2799 regname = c;
2800 // Avoid free/alloc when the value is already right.
2801 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2802 set_vim_var_string(VV_REG, &regname, 1);
2803}
2804
2805/*
2806 * Get or set v:exception. If "oldval" == NULL, return the current value.
2807 * Otherwise, restore the value to "oldval" and return NULL.
2808 * Must always be called in pairs to save and restore v:exception! Does not
2809 * take care of memory allocations.
2810 */
2811 char_u *
2812v_exception(char_u *oldval)
2813{
2814 if (oldval == NULL)
2815 return vimvars[VV_EXCEPTION].vv_str;
2816
2817 vimvars[VV_EXCEPTION].vv_str = oldval;
2818 return NULL;
2819}
2820
2821/*
2822 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2823 * Otherwise, restore the value to "oldval" and return NULL.
2824 * Must always be called in pairs to save and restore v:throwpoint! Does not
2825 * take care of memory allocations.
2826 */
2827 char_u *
2828v_throwpoint(char_u *oldval)
2829{
2830 if (oldval == NULL)
2831 return vimvars[VV_THROWPOINT].vv_str;
2832
2833 vimvars[VV_THROWPOINT].vv_str = oldval;
2834 return NULL;
2835}
2836
2837/*
2838 * Set v:cmdarg.
2839 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2840 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2841 * Must always be called in pairs!
2842 */
2843 char_u *
2844set_cmdarg(exarg_T *eap, char_u *oldarg)
2845{
2846 char_u *oldval;
2847 char_u *newval;
2848 unsigned len;
2849
2850 oldval = vimvars[VV_CMDARG].vv_str;
2851 if (eap == NULL)
2852 {
2853 vim_free(oldval);
2854 vimvars[VV_CMDARG].vv_str = oldarg;
2855 return NULL;
2856 }
2857
2858 if (eap->force_bin == FORCE_BIN)
2859 len = 6;
2860 else if (eap->force_bin == FORCE_NOBIN)
2861 len = 8;
2862 else
2863 len = 0;
2864
2865 if (eap->read_edit)
2866 len += 7;
2867
2868 if (eap->force_ff != 0)
2869 len += 10; // " ++ff=unix"
2870 if (eap->force_enc != 0)
2871 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2872 if (eap->bad_char != 0)
2873 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2874
2875 newval = alloc(len + 1);
2876 if (newval == NULL)
2877 return NULL;
2878
2879 if (eap->force_bin == FORCE_BIN)
2880 sprintf((char *)newval, " ++bin");
2881 else if (eap->force_bin == FORCE_NOBIN)
2882 sprintf((char *)newval, " ++nobin");
2883 else
2884 *newval = NUL;
2885
2886 if (eap->read_edit)
2887 STRCAT(newval, " ++edit");
2888
2889 if (eap->force_ff != 0)
2890 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2891 eap->force_ff == 'u' ? "unix"
2892 : eap->force_ff == 'd' ? "dos"
2893 : "mac");
2894 if (eap->force_enc != 0)
2895 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2896 eap->cmd + eap->force_enc);
2897 if (eap->bad_char == BAD_KEEP)
2898 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2899 else if (eap->bad_char == BAD_DROP)
2900 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2901 else if (eap->bad_char != 0)
2902 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2903 vimvars[VV_CMDARG].vv_str = newval;
2904 return oldval;
2905}
2906
2907/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002908 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002909 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2910 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002911 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2912 */
2913 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002914eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002915 char_u *name,
Bram Moolenaar94674f22023-01-06 18:42:20 +00002916 int len, // length of "name" or zero
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002917 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002918 typval_T *rettv, // NULL when only checking existence
2919 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002920 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002921{
2922 int ret = OK;
2923 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002924 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002925 hashtab_T *ht = NULL;
Bram Moolenaar94674f22023-01-06 18:42:20 +00002926 int cc = 0;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002927 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002928
Bram Moolenaar94674f22023-01-06 18:42:20 +00002929 if (len > 0)
2930 {
2931 // truncate the name, so that we can use strcmp()
2932 cc = name[len];
2933 name[len] = NUL;
2934 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002935
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002936 // Check for local variable when debugging.
2937 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002938 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002939 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002940 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2941
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002942 if (v != NULL)
2943 {
2944 tv = &v->di_tv;
2945 if (dip != NULL)
2946 *dip = v;
2947 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002948 else
2949 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002950 }
2951
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002952 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002954 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002955 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2956
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002957 if (sid == 0)
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00002958 import = find_imported(p, 0, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959
2960 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002961 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002963 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002964 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00002965 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02002966 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002967 ht = &SCRIPT_VARS(sid);
2968 if (ht != NULL)
2969 {
2970 dictitem_T *v = find_var_in_ht(ht, 0, name,
2971 flags & EVAL_VAR_NOAUTOLOAD);
2972
2973 if (v != NULL)
2974 {
2975 tv = &v->di_tv;
2976 if (dip != NULL)
2977 *dip = v;
2978 }
2979 else
2980 ht = NULL;
2981 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002982 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002983 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002984 {
2985 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00002986 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002987 ret = FAIL;
2988 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002989 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002990 else
2991 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002992 if (rettv != NULL)
2993 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01002994 // special value that is used in handle_subscript()
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002995 rettv->v_type = VAR_ANY;
2996 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
2997 }
2998 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002999 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00003001 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003002 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003003 int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003004 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003005
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003006 // In Vim9 script we can get a function reference by using the
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003007 // function name. For a global non-autoload function "g:" is
3008 // required.
3009 if (ufunc != NULL && (has_g_prefix
3010 || !func_requires_g_prefix(ufunc)))
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003011 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003012 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003013 if (rettv != NULL)
3014 {
3015 rettv->v_type = VAR_FUNC;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003016 if (has_g_prefix)
Bram Moolenaaref082e12021-12-12 21:02:03 +00003017 // Keep the "g:", otherwise script-local may be
3018 // assumed.
3019 rettv->vval.v_string = vim_strsave(name);
3020 else
3021 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003022 if (rettv->vval.v_string != NULL)
3023 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003024 }
3025 }
3026 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 }
3028
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003029 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003030 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02003031 if (tv == NULL)
3032 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003033 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003034 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02003035 ret = FAIL;
3036 }
3037 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003038 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003039 svar_T *sv = NULL;
3040 int was_assigned = FALSE;
3041
Bram Moolenaar11005b02021-07-11 20:59:00 +02003042 if (ht != NULL && ht == get_script_local_ht()
3043 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02003044 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003045 sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaarf055d452021-07-08 20:57:24 +02003046 if (sv != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003047 {
Bram Moolenaarf055d452021-07-08 20:57:24 +02003048 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003049 was_assigned = sv->sv_flags & SVFLAG_ASSIGNED;
3050 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003051 }
3052
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003053 // If a list or dict variable wasn't initialized and has meaningful
3054 // type, do it now. Not for global variables, they are not
3055 // declared.
Bram Moolenaar7a222242022-03-01 19:23:24 +00003056 if (ht != &globvarht)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003057 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003058 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003059 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003060 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003061 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003062 tv->vval.v_dict = dict_alloc();
3063 if (tv->vval.v_dict != NULL)
3064 {
3065 ++tv->vval.v_dict->dv_refcount;
3066 tv->vval.v_dict->dv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003067 if (sv != NULL)
3068 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003069 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003070 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003071 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003072 && ((type != NULL && !was_assigned)
Bram Moolenaar501f9782022-03-27 16:51:04 +01003073 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003074 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003075 tv->vval.v_list = list_alloc();
3076 if (tv->vval.v_list != NULL)
3077 {
3078 ++tv->vval.v_list->lv_refcount;
3079 tv->vval.v_list->lv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003080 if (sv != NULL)
3081 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003082 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003083 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003084 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003085 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003086 || !in_vim9script()))
3087 {
3088 tv->vval.v_blob = blob_alloc();
3089 if (tv->vval.v_blob != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003090 {
Bram Moolenaar859cc212022-03-28 15:22:35 +01003091 ++tv->vval.v_blob->bv_refcount;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003092 if (sv != NULL)
3093 sv->sv_flags |= SVFLAG_ASSIGNED;
3094 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003095 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02003096 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003097 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003098 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003099 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003100
Bram Moolenaar94674f22023-01-06 18:42:20 +00003101 if (len > 0)
3102 name[len] = cc;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003103
3104 return ret;
3105}
3106
3107/*
Bram Moolenaara86655a2023-01-12 17:06:27 +00003108 * Get the value of internal variable "name", also handling "import.name".
3109 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
3110 */
3111 int
3112eval_variable_import(
3113 char_u *name,
3114 typval_T *rettv)
3115{
3116 char_u *s = name;
3117 while (ASCII_ISALNUM(*s) || *s == '_')
3118 ++s;
3119 int len = (int)(s - name);
3120
3121 if (eval_variable(name, len, 0, rettv, NULL, EVAL_VAR_IMPORT) == FAIL)
3122 return FAIL;
3123 if (rettv->v_type == VAR_ANY && *s == '.')
3124 {
3125 int sid = rettv->vval.v_number;
3126 return eval_variable(s + 1, 0, sid, rettv, NULL, 0);
3127 }
3128 return OK;
3129}
3130
3131
3132/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003133 * Check if variable "name[len]" is a local variable or an argument.
3134 * If so, "*eval_lavars_used" is set to TRUE.
3135 */
3136 void
3137check_vars(char_u *name, int len)
3138{
3139 int cc;
3140 char_u *varname;
3141 hashtab_T *ht;
3142
3143 if (eval_lavars_used == NULL)
3144 return;
3145
3146 // truncate the name, so that we can use strcmp()
3147 cc = name[len];
3148 name[len] = NUL;
3149
3150 ht = find_var_ht(name, &varname);
3151 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
3152 {
3153 if (find_var(name, NULL, TRUE) != NULL)
3154 *eval_lavars_used = TRUE;
3155 }
3156
3157 name[len] = cc;
3158}
3159
3160/*
3161 * Find variable "name" in the list of variables.
3162 * Return a pointer to it if found, NULL if not found.
3163 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003164 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003165 */
3166 dictitem_T *
3167find_var(char_u *name, hashtab_T **htp, int no_autoload)
3168{
3169 char_u *varname;
3170 hashtab_T *ht;
3171 dictitem_T *ret = NULL;
3172
3173 ht = find_var_ht(name, &varname);
3174 if (htp != NULL)
3175 *htp = ht;
3176 if (ht == NULL)
3177 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003178 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003179 if (ret != NULL)
3180 return ret;
3181
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003182 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003183 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003184 if (ret != NULL)
3185 return ret;
3186
3187 // in Vim9 script items without a scope can be script-local
3188 if (in_vim9script() && name[0] != NUL && name[1] != ':')
3189 {
3190 ht = get_script_local_ht();
3191 if (ht != NULL)
3192 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003193 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003194 if (ret != NULL)
3195 {
3196 if (htp != NULL)
3197 *htp = ht;
3198 return ret;
3199 }
3200 }
3201 }
3202
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003203 // When using "vim9script autoload" script-local items are prefixed but can
3204 // be used with s:name.
3205 if (SCRIPT_ID_VALID(current_sctx.sc_sid)
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003206 && (in_vim9script() || (name[0] == 's' && name[1] == ':')))
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003207 {
3208 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
3209
3210 if (si->sn_autoload_prefix != NULL)
3211 {
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003212 char_u *base_name = (name[0] == 's' && name[1] == ':')
3213 ? name + 2 : name;
3214 char_u *auto_name = concat_str(si->sn_autoload_prefix, base_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003215
3216 if (auto_name != NULL)
3217 {
3218 ht = &globvarht;
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003219 ret = find_var_in_ht(ht, 'g', auto_name, TRUE);
Bram Moolenaar130f65d2022-01-13 20:39:41 +00003220 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003221 if (ret != NULL)
3222 {
3223 if (htp != NULL)
3224 *htp = ht;
3225 return ret;
3226 }
3227 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003228 }
3229 }
3230
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003231 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003232}
3233
3234/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00003235 * Like find_var() but if the name starts with <SNR>99_ then look in the
3236 * referenced script (used for a funcref).
3237 */
3238 dictitem_T *
3239find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
3240{
3241 if (STRNCMP(name, "<SNR>", 5) == 0 && isdigit(name[5]))
3242 {
3243 char_u *p = name + 5;
3244 int sid = getdigits(&p);
3245
3246 if (SCRIPT_ID_VALID(sid) && *p == '_')
3247 {
3248 hashtab_T *ht = &SCRIPT_VARS(sid);
3249
3250 if (ht != NULL)
3251 {
3252 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
3253
3254 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003255 {
3256 if (htp != NULL)
3257 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00003258 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003259 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00003260 }
3261 }
3262 }
3263
3264 return find_var(name, htp, no_autoload);
3265}
3266
3267/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003268 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02003269 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003270 * Returns NULL if not found.
3271 */
3272 dictitem_T *
3273find_var_in_ht(
3274 hashtab_T *ht,
3275 int htname,
3276 char_u *varname,
3277 int no_autoload)
3278{
3279 hashitem_T *hi;
3280
3281 if (*varname == NUL)
3282 {
3283 // Must be something like "s:", otherwise "ht" would be NULL.
3284 switch (htname)
3285 {
3286 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
3287 case 'g': return &globvars_var;
3288 case 'v': return &vimvars_var;
3289 case 'b': return &curbuf->b_bufvar;
3290 case 'w': return &curwin->w_winvar;
3291 case 't': return &curtab->tp_winvar;
3292 case 'l': return get_funccal_local_var();
3293 case 'a': return get_funccal_args_var();
3294 }
3295 return NULL;
3296 }
3297
3298 hi = hash_find(ht, varname);
3299 if (HASHITEM_EMPTY(hi))
3300 {
3301 // For global variables we may try auto-loading the script. If it
3302 // worked find the variable again. Don't auto-load a script if it was
3303 // loaded already, otherwise it would be loaded every time when
3304 // checking if a function name is a Funcref variable.
3305 if (ht == &globvarht && !no_autoload)
3306 {
3307 // Note: script_autoload() may make "hi" invalid. It must either
3308 // be obtained again or not used.
3309 if (!script_autoload(varname, FALSE) || aborting())
3310 return NULL;
3311 hi = hash_find(ht, varname);
3312 }
3313 if (HASHITEM_EMPTY(hi))
3314 return NULL;
3315 }
3316 return HI2DI(hi);
3317}
3318
3319/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 * Get the script-local hashtab. NULL if not in a script context.
3321 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003322 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003323get_script_local_ht(void)
3324{
3325 scid_T sid = current_sctx.sc_sid;
3326
Bram Moolenaare3d46852020-08-29 13:39:17 +02003327 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328 return &SCRIPT_VARS(sid);
3329 return NULL;
3330}
3331
3332/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003333 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003334 * When "cmd" is TRUE it must look like a command, a function must be followed
3335 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003336 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003337 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003338 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003339lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003340 char_u *name,
3341 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003342 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003343 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344{
3345 hashtab_T *ht = get_script_local_ht();
3346 char_u buffer[30];
3347 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003348 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003350 int is_global = FALSE;
3351 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352
3353 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003354 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 if (len < sizeof(buffer) - 1)
3356 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003357 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358 vim_strncpy(buffer, name, len);
3359 p = buffer;
3360 }
3361 else
3362 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003363 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003365 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003366 }
3367
3368 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003369 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370
3371 // if not script-local, then perhaps imported
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003372 if (res == FAIL && find_imported(p, 0, FALSE) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003373 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 if (p != buffer)
3375 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003376
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003377 // Find a function, so that a following "->" works.
3378 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3379 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003380 if (res != OK)
3381 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003382 p = skipwhite(name + len);
3383
3384 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003385 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003386 // Do not check for an internal function, since it might also be a
3387 // valid command, such as ":split" versus "split()".
3388 // Skip "g:" before a function name.
3389 if (name[0] == 'g' && name[1] == ':')
3390 {
3391 is_global = TRUE;
3392 fname = name + 2;
3393 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003394 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003395 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003396 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003397 }
3398
Bram Moolenaar709664c2020-12-12 14:33:41 +01003399 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400}
3401
3402/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003403 * Find the hashtab used for a variable name.
3404 * Return NULL if the name is not valid.
3405 * Set "varname" to the start of name without ':'.
3406 */
3407 hashtab_T *
3408find_var_ht(char_u *name, char_u **varname)
3409{
3410 hashitem_T *hi;
3411 hashtab_T *ht;
3412
3413 if (name[0] == NUL)
3414 return NULL;
3415 if (name[1] != ':')
3416 {
3417 // The name must not start with a colon or #.
3418 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3419 return NULL;
3420 *varname = name;
3421
3422 // "version" is "v:version" in all scopes if scriptversion < 3.
3423 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003424 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003425 {
3426 hi = hash_find(&compat_hashtab, name);
3427 if (!HASHITEM_EMPTY(hi))
3428 return &compat_hashtab;
3429 }
3430
3431 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 if (ht != NULL)
3433 return ht; // local variable
3434
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003435 // In Vim9 script items at the script level are script-local, except
3436 // for autoload names.
3437 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438 {
3439 ht = get_script_local_ht();
3440 if (ht != NULL)
3441 return ht;
3442 }
3443
3444 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003445 }
3446 *varname = name + 2;
3447 if (*name == 'g') // global variable
3448 return &globvarht;
3449 // There must be no ':' or '#' in the rest of the name, unless g: is used
3450 if (vim_strchr(name + 2, ':') != NULL
3451 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3452 return NULL;
3453 if (*name == 'b') // buffer variable
3454 return &curbuf->b_vars->dv_hashtab;
3455 if (*name == 'w') // window variable
3456 return &curwin->w_vars->dv_hashtab;
3457 if (*name == 't') // tab page variable
3458 return &curtab->tp_vars->dv_hashtab;
3459 if (*name == 'v') // v: variable
3460 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003461 if (get_current_funccal() != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01003462 && get_current_funccal()->fc_func->uf_def_status
3463 == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003465 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003466 if (*name == 'a') // a: function argument
3467 return get_funccal_args_ht();
3468 if (*name == 'l') // l: local function variable
3469 return get_funccal_local_ht();
3470 }
3471 if (*name == 's') // script variable
3472 {
3473 ht = get_script_local_ht();
3474 if (ht != NULL)
3475 return ht;
3476 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003477 return NULL;
3478}
3479
3480/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003481 * Get the string value of a (global/local) variable.
3482 * Note: see tv_get_string() for how long the pointer remains valid.
3483 * Returns NULL when it doesn't exist.
3484 */
3485 char_u *
3486get_var_value(char_u *name)
3487{
3488 dictitem_T *v;
3489
3490 v = find_var(name, NULL, FALSE);
3491 if (v == NULL)
3492 return NULL;
3493 return tv_get_string(&v->di_tv);
3494}
3495
3496/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003497 * Allocate a new hashtab for a sourced script. It will be used while
3498 * sourcing this script and when executing functions defined in the script.
3499 */
3500 void
3501new_script_vars(scid_T id)
3502{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003503 scriptvar_T *sv;
3504
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003505 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3506 if (sv == NULL)
3507 return;
3508 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003509 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003510}
3511
3512/*
3513 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3514 * point to it.
3515 */
3516 void
3517init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3518{
3519 hash_init(&dict->dv_hashtab);
3520 dict->dv_lock = 0;
3521 dict->dv_scope = scope;
3522 dict->dv_refcount = DO_NOT_FREE_CNT;
3523 dict->dv_copyID = 0;
3524 dict_var->di_tv.vval.v_dict = dict;
3525 dict_var->di_tv.v_type = VAR_DICT;
3526 dict_var->di_tv.v_lock = VAR_FIXED;
3527 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3528 dict_var->di_key[0] = NUL;
3529}
3530
3531/*
3532 * Unreference a dictionary initialized by init_var_dict().
3533 */
3534 void
3535unref_var_dict(dict_T *dict)
3536{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003537 // Now the dict needs to be freed if no one else is using it, go back to
3538 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003539 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3540 dict_unref(dict);
3541}
3542
3543/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003544 * Clean up a list of internal variables.
3545 * Frees all allocated variables and the value they contain.
3546 * Clears hashtab "ht", does not free it.
3547 */
3548 void
3549vars_clear(hashtab_T *ht)
3550{
3551 vars_clear_ext(ht, TRUE);
3552}
3553
3554/*
3555 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3556 */
3557 void
3558vars_clear_ext(hashtab_T *ht, int free_val)
3559{
3560 int todo;
3561 hashitem_T *hi;
3562 dictitem_T *v;
3563
3564 hash_lock(ht);
3565 todo = (int)ht->ht_used;
3566 for (hi = ht->ht_array; todo > 0; ++hi)
3567 {
3568 if (!HASHITEM_EMPTY(hi))
3569 {
3570 --todo;
3571
3572 // Free the variable. Don't remove it from the hashtab,
3573 // ht_array might change then. hash_clear() takes care of it
3574 // later.
3575 v = HI2DI(hi);
3576 if (free_val)
3577 clear_tv(&v->di_tv);
3578 if (v->di_flags & DI_FLAGS_ALLOC)
3579 vim_free(v);
3580 }
3581 }
3582 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003583 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003584}
3585
3586/*
3587 * Delete a variable from hashtab "ht" at item "hi".
3588 * Clear the variable value and free the dictitem.
3589 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003590 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003591delete_var(hashtab_T *ht, hashitem_T *hi)
3592{
3593 dictitem_T *di = HI2DI(hi);
3594
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003595 if (hash_remove(ht, hi, "delete variable") != OK)
3596 return;
3597
3598 clear_tv(&di->di_tv);
3599 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003600}
3601
3602/*
3603 * List the value of one internal variable.
3604 */
3605 static void
3606list_one_var(dictitem_T *v, char *prefix, int *first)
3607{
3608 char_u *tofree;
3609 char_u *s;
3610 char_u numbuf[NUMBUFLEN];
3611
3612 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3613 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3614 s == NULL ? (char_u *)"" : s, first);
3615 vim_free(tofree);
3616}
3617
3618 static void
3619list_one_var_a(
3620 char *prefix,
3621 char_u *name,
3622 int type,
3623 char_u *string,
3624 int *first) // when TRUE clear rest of screen and set to FALSE
3625{
3626 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3627 msg_start();
3628 msg_puts(prefix);
3629 if (name != NULL) // "a:" vars don't have a name stored
3630 msg_puts((char *)name);
3631 msg_putchar(' ');
3632 msg_advance(22);
3633 if (type == VAR_NUMBER)
3634 msg_putchar('#');
3635 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3636 msg_putchar('*');
3637 else if (type == VAR_LIST)
3638 {
3639 msg_putchar('[');
3640 if (*string == '[')
3641 ++string;
3642 }
3643 else if (type == VAR_DICT)
3644 {
3645 msg_putchar('{');
3646 if (*string == '{')
3647 ++string;
3648 }
3649 else
3650 msg_putchar(' ');
3651
3652 msg_outtrans(string);
3653
3654 if (type == VAR_FUNC || type == VAR_PARTIAL)
3655 msg_puts("()");
3656 if (*first)
3657 {
3658 msg_clr_eos();
3659 *first = FALSE;
3660 }
3661}
3662
3663/*
3664 * Set variable "name" to value in "tv".
3665 * If the variable already exists, the value is updated.
3666 * Otherwise the variable is created.
3667 */
3668 void
3669set_var(
3670 char_u *name,
3671 typval_T *tv,
3672 int copy) // make copy of value in "tv"
3673{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003674 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003675}
3676
3677/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003678 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003679 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003680 * If the variable already exists and "is_const" is FALSE the value is updated.
3681 * Otherwise the variable is created.
3682 */
3683 void
3684set_var_const(
3685 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003686 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003687 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003688 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003689 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003690 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003691 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003692{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003693 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003694 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003695 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 dictitem_T *di;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003697 typval_T *dest_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003698 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003699 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003700 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003702 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003703 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003704 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003705 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003706 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003707
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003708 if (sid != 0)
3709 {
3710 if (SCRIPT_ID_VALID(sid))
3711 ht = &SCRIPT_VARS(sid);
3712 varname = name;
3713 }
3714 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003715 {
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003716 scriptitem_T *si;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003717
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003718 if (in_vim9script() && is_export
3719 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3720 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
Bram Moolenaar0e1574c2022-03-03 17:05:35 +00003721 ->sn_autoload_prefix != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003722 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003723 // In a vim9 autoload script an exported variable is put in the
3724 // global namespace with the autoload prefix.
3725 var_in_autoload = TRUE;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003726 varname = concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003727 if (varname == NULL)
3728 goto failed;
3729 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003730 ht = &globvarht;
3731 }
3732 else
3733 ht = find_var_ht(name, &varname);
3734 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003735 if (ht == NULL || *varname == NUL)
3736 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003737 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003738 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003739 }
Bram Moolenaar54969f42022-02-07 13:56:44 +00003740 is_script_local = ht == get_script_local_ht() || sid != 0
3741 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003742
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003743 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003744 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003745 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003746 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003747 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003748 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003749 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003750 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003751 }
Bram Moolenaar9510d222022-09-11 15:14:05 +01003752 if ((flags & ASSIGN_FOR_LOOP) && is_scoped_variable(name))
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003753 // Do not make g:var, w:var, b:var or t:var final.
3754 flags &= ~ASSIGN_FINAL;
3755
Bram Moolenaare535db82021-03-31 21:07:24 +02003756 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003757 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3758 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003759 // For "[a, _] = list" the underscore is ignored.
3760 if ((flags & ASSIGN_UNPACK) == 0)
3761 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003762 goto failed;
3763 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003764
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003766
Bram Moolenaar24e93162021-07-18 20:40:33 +02003767 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003768 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003769 imported_T *import = find_imported(varname, 0, FALSE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003770
Bram Moolenaar24e93162021-07-18 20:40:33 +02003771 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003772 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003773 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02003774 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003776 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003777 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003779 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3780 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003781 }
Bram Moolenaar75e27d72022-02-13 13:56:29 +00003782 if (!in_vim9script())
3783 {
3784 semsg(_(e_cannot_create_vim9_script_variable_in_function_str),
3785 name);
3786 goto failed;
3787 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003788 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789
Bram Moolenaar993faa32022-02-21 15:59:11 +00003790 // Search in parent scope which is possible to reference from lambda
3791 if (di == NULL)
3792 di = find_var_in_scoped_ht(name, TRUE);
3793
3794 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
3795 && var_wrong_func_name(name, di == NULL))
3796 goto failed;
3797
3798 if (need_convert_to_bool(type, tv))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003799 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003800 // Destination is a bool and the value is not, but it can be
3801 // converted.
3802 CLEAR_FIELD(bool_tv);
3803 bool_tv.v_type = VAR_BOOL;
3804 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3805 tv = &bool_tv;
3806 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003807
Bram Moolenaar993faa32022-02-21 15:59:11 +00003808 if (di != NULL)
3809 {
3810 // Item already exists. Allowed to replace when reloading.
3811 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003812 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003813 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
3814 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003815 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003816 emsg(_(e_cannot_modify_existing_variable));
3817 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003818 }
3819
Bram Moolenaar993faa32022-02-21 15:59:11 +00003820 if (is_script_local && vim9script
3821 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003822 {
3823 semsg(_(e_redefining_script_item_str), name);
3824 goto failed;
3825 }
3826
Bram Moolenaar993faa32022-02-21 15:59:11 +00003827 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003828 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003829 where_T where = WHERE_INIT;
Bram Moolenaar7a411a32022-04-04 14:58:06 +01003830 svar_T *sv = find_typval_in_script(&di->di_tv, sid, TRUE);
Bram Moolenaar993faa32022-02-21 15:59:11 +00003831
3832 if (sv != NULL)
3833 {
3834 // check the type and adjust to bool if needed
3835 where.wt_index = var_idx;
3836 where.wt_variable = TRUE;
3837 if (check_script_var_type(sv, tv, name, where) == FAIL)
3838 goto failed;
3839 if (type == NULL)
3840 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003841 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003842 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003843 }
3844
Bram Moolenaar993faa32022-02-21 15:59:11 +00003845 if ((flags & ASSIGN_FOR_LOOP) == 0
3846 && var_check_permission(di, name) == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003847 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003848 }
3849 else
3850 {
3851 // can only redefine once
3852 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003853
Bram Moolenaar993faa32022-02-21 15:59:11 +00003854 // A Vim9 script-local variable is also present in sn_all_vars
3855 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003856 if (var_in_vim9script || var_in_autoload)
Bram Moolenaar993faa32022-02-21 15:59:11 +00003857 update_vim9_script_var(FALSE, di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003858 var_in_autoload ? name : di->di_key, flags,
Bram Moolenaar993faa32022-02-21 15:59:11 +00003859 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003860 }
3861
Bram Moolenaar993faa32022-02-21 15:59:11 +00003862 // existing variable, need to clear the value
3863
3864 // Handle setting internal di: variables separately where needed to
3865 // prevent changing the type.
3866 if (ht == &vimvarht)
3867 {
3868 if (di->di_tv.v_type == VAR_STRING)
3869 {
3870 VIM_CLEAR(di->di_tv.vval.v_string);
3871 if (copy || tv->v_type != VAR_STRING)
3872 {
3873 char_u *val = tv_get_string(tv);
3874
3875 // Careful: when assigning to v:errmsg and
3876 // tv_get_string() causes an error message the variable
3877 // will already be set.
3878 if (di->di_tv.vval.v_string == NULL)
3879 di->di_tv.vval.v_string = vim_strsave(val);
3880 }
3881 else
3882 {
3883 // Take over the string to avoid an extra alloc/free.
3884 di->di_tv.vval.v_string = tv->vval.v_string;
3885 tv->vval.v_string = NULL;
3886 }
3887 goto failed;
3888 }
3889 else if (di->di_tv.v_type == VAR_NUMBER)
3890 {
3891 di->di_tv.vval.v_number = tv_get_number(tv);
3892 if (STRCMP(varname, "searchforward") == 0)
3893 set_search_direction(di->di_tv.vval.v_number
3894 ? '/' : '?');
3895#ifdef FEAT_SEARCH_EXTRA
3896 else if (STRCMP(varname, "hlsearch") == 0)
3897 {
3898 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003899 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar993faa32022-02-21 15:59:11 +00003900 }
3901#endif
3902 goto failed;
3903 }
3904 else if (di->di_tv.v_type != tv->v_type)
3905 {
3906 semsg(_(e_setting_str_to_value_with_wrong_type), name);
3907 goto failed;
3908 }
3909 }
3910
3911 clear_tv(&di->di_tv);
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01003912
3913 if ((flags & ASSIGN_UPDATE_BLOCK_ID)
3914 && SCRIPT_ID_VALID(current_sctx.sc_sid))
3915 {
3916 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
3917
3918 update_script_var_block_id(name, si->sn_current_block_id);
3919 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00003920 }
3921 else
3922 {
3923 // Item not found, check if a function already exists.
3924 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
3925 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
3926 {
3927 semsg(_(e_redefining_script_item_str), name);
3928 goto failed;
3929 }
3930
3931 // add a new variable
3932 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3933 {
3934 semsg(_(e_unknown_variable_str), name);
3935 goto failed;
3936 }
3937
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003938 if (check_hashtab_frozen(ht, "add variable"))
3939 goto failed;
3940
Bram Moolenaar993faa32022-02-21 15:59:11 +00003941 // Can't add "v:" or "a:" variable.
3942 if (ht == &vimvarht || ht == get_funccal_args_ht())
3943 {
3944 semsg(_(e_illegal_variable_name_str), name);
3945 goto failed;
3946 }
3947
3948 // Make sure the variable name is valid. In Vim9 script an
3949 // autoload variable must be prefixed with "g:" unless in an
3950 // autoload script.
3951 if (!valid_varname(varname, -1, !vim9script
3952 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
3953 goto failed;
3954
3955 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3956 if (di == NULL)
3957 goto failed;
3958 STRCPY(di->di_key, varname);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003959 if (hash_add(ht, DI2HIKEY(di), "add variable") == FAIL)
Bram Moolenaar993faa32022-02-21 15:59:11 +00003960 {
3961 vim_free(di);
3962 goto failed;
3963 }
3964 di->di_flags = DI_FLAGS_ALLOC;
3965 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3966 di->di_flags |= DI_FLAGS_LOCK;
3967
3968 // A Vim9 script-local variable is also added to sn_all_vars and
3969 // sn_var_vals. It may set "type" from "tv".
3970 if (var_in_vim9script || var_in_autoload)
3971 update_vim9_script_var(TRUE, di,
3972 var_in_autoload ? name : di->di_key, flags,
3973 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003974 }
3975
Bram Moolenaar993faa32022-02-21 15:59:11 +00003976 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003977 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003978 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003979 else
3980 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003981 *dest_tv = *tv;
3982 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003983 init_tv(tv);
3984 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003985 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003986
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003987 if (vim9script && type != NULL)
Bram Moolenaar381692b2022-02-02 20:01:27 +00003988 set_tv_type(dest_tv, type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003989
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003990 // ":const var = value" locks the value
3991 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003992 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003993 // Like :lockvar! name: lock the value and what it contains, but only
3994 // if the reference count is up to one. That locks only literal
3995 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003996 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003997
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003998failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003999 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004000 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004001 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004002}
4003
4004/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004005 * Check in this order for backwards compatibility:
4006 * - Whether the variable is read-only
4007 * - Whether the variable value is locked
4008 * - Whether the variable is locked
4009 */
4010 int
4011var_check_permission(dictitem_T *di, char_u *name)
4012{
4013 if (var_check_ro(di->di_flags, name, FALSE)
4014 || value_check_lock(di->di_tv.v_lock, name, FALSE)
4015 || var_check_lock(di->di_flags, name, FALSE))
4016 return FAIL;
4017 return OK;
4018}
4019
4020/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004021 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
4022 * Also give an error message.
4023 */
4024 int
4025var_check_ro(int flags, char_u *name, int use_gettext)
4026{
4027 if (flags & DI_FLAGS_RO)
4028 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004029 if (name == NULL)
4030 emsg(_(e_cannot_change_readonly_variable));
4031 else
4032 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02004033 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004034 return TRUE;
4035 }
4036 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
4037 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004038 if (name == NULL)
4039 emsg(_(e_cannot_set_variable_in_sandbox));
4040 else
4041 semsg(_(e_cannot_set_variable_in_sandbox_str),
4042 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004043 return TRUE;
4044 }
4045 return FALSE;
4046}
4047
4048/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004049 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
4050 * Also give an error message.
4051 */
4052 int
4053var_check_lock(int flags, char_u *name, int use_gettext)
4054{
4055 if (flags & DI_FLAGS_LOCK)
4056 {
4057 semsg(_(e_variable_is_locked_str),
4058 use_gettext ? (char_u *)_(name) : name);
4059 return TRUE;
4060 }
4061 return FALSE;
4062}
4063
4064/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004065 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
4066 * Also give an error message.
4067 */
4068 int
4069var_check_fixed(int flags, char_u *name, int use_gettext)
4070{
4071 if (flags & DI_FLAGS_FIX)
4072 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004073 if (name == NULL)
4074 emsg(_(e_cannot_delete_variable));
4075 else
4076 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004077 use_gettext ? (char_u *)_(name) : name);
4078 return TRUE;
4079 }
4080 return FALSE;
4081}
4082
4083/*
4084 * Check if a funcref is assigned to a valid variable name.
4085 * Return TRUE and give an error if not.
4086 */
4087 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004088var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004089 char_u *name, // points to start of variable name
4090 int new_var) // TRUE when creating the variable
4091{
Bram Moolenaar32154662021-03-28 21:14:06 +02004092 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
4093 // the name can be used without the s: prefix.
thinca6c667bd2022-09-02 11:25:37 +01004094 // Allow autoload variable.
Bram Moolenaar32154662021-03-28 21:14:06 +02004095 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
4096 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004097 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
thinca6c667bd2022-09-02 11:25:37 +01004098 ? name[2] : name[0])
4099 && vim_strchr(name, '#') == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004100 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004101 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004102 return TRUE;
4103 }
4104 // Don't allow hiding a function. When "v" is not NULL we might be
4105 // assigning another function to the same var, the type is checked
4106 // below.
4107 if (new_var && function_exists(name, FALSE))
4108 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004109 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004110 name);
4111 return TRUE;
4112 }
4113 return FALSE;
4114}
4115
4116/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004117 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
4118 * value. Also give an error message, using "name" or _("name") when
4119 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004120 */
4121 int
Bram Moolenaara187c432020-09-16 21:08:28 +02004122value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004123{
4124 if (lock & VAR_LOCKED)
4125 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004126 if (name == NULL)
4127 emsg(_(e_value_is_locked));
4128 else
4129 semsg(_(e_value_is_locked_str),
4130 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004131 return TRUE;
4132 }
4133 if (lock & VAR_FIXED)
4134 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004135 if (name == NULL)
4136 emsg(_(e_cannot_change_value));
4137 else
4138 semsg(_(e_cannot_change_value_of_str),
4139 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004140 return TRUE;
4141 }
4142 return FALSE;
4143}
4144
4145/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01004146 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004147 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004148 * Return FALSE and give an error if not.
4149 */
4150 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004151valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004152{
4153 char_u *p;
4154
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004155 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004156 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01004157 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004158 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004159 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004160 return FALSE;
4161 }
4162 return TRUE;
4163}
4164
4165/*
LemonBoy47d4e312022-05-04 18:12:55 +01004166 * Implements the logic to retrieve local variable and option values.
4167 * Used by "getwinvar()" "gettabvar()" "gettabwinvar()" "getbufvar()".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004168 */
4169 static void
LemonBoy47d4e312022-05-04 18:12:55 +01004170get_var_from(
4171 char_u *varname,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004172 typval_T *rettv,
LemonBoy47d4e312022-05-04 18:12:55 +01004173 typval_T *deftv, // Default value if not found.
4174 int htname, // 't'ab, 'w'indow or 'b'uffer local.
4175 tabpage_T *tp, // can be NULL
4176 win_T *win,
4177 buf_T *buf) // Ignored if htname is not 'b'.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004178{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004179 dictitem_T *v;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004180 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004181 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004182 int need_switch_win;
zeertzjqcd6ad642022-07-25 12:28:09 +01004183 int do_change_curbuf = buf != NULL && htname == 'b';
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004184
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004185 ++emsg_off;
4186
4187 rettv->v_type = VAR_STRING;
4188 rettv->vval.v_string = NULL;
4189
LemonBoy47d4e312022-05-04 18:12:55 +01004190 if (varname != NULL && tp != NULL && win != NULL
4191 && (htname != 'b' || buf != NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004192 {
4193 // Set curwin to be our win, temporarily. Also set the tabpage,
4194 // otherwise the window is not valid. Only do this when needed,
4195 // autocommands get blocked.
LemonBoy47d4e312022-05-04 18:12:55 +01004196 // If we have a buffer reference avoid the switching, we're saving and
4197 // restoring curbuf directly.
zeertzjqcd6ad642022-07-25 12:28:09 +01004198 need_switch_win = !(tp == curtab && win == curwin) && !do_change_curbuf;
LemonBoy47d4e312022-05-04 18:12:55 +01004199 if (!need_switch_win || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004200 {
LemonBoy47d4e312022-05-04 18:12:55 +01004201 // Handle options. There are no tab-local options.
4202 if (*varname == '&' && htname != 't')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004203 {
LemonBoy47d4e312022-05-04 18:12:55 +01004204 buf_T *save_curbuf = curbuf;
4205
4206 // Change curbuf so the option is read from the correct buffer.
zeertzjqcd6ad642022-07-25 12:28:09 +01004207 if (do_change_curbuf)
LemonBoy47d4e312022-05-04 18:12:55 +01004208 curbuf = buf;
4209
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004210 if (varname[1] == NUL)
4211 {
zeertzjqcd6ad642022-07-25 12:28:09 +01004212 // get all window-local or buffer-local options in a dict
LemonBoy47d4e312022-05-04 18:12:55 +01004213 dict_T *opts = get_winbuf_options(htname == 'b');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004214
4215 if (opts != NULL)
4216 {
4217 rettv_dict_set(rettv, opts);
4218 done = TRUE;
4219 }
4220 }
LemonBoy47d4e312022-05-04 18:12:55 +01004221 else if (eval_option(&varname, rettv, TRUE) == OK)
4222 // Local option
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004223 done = TRUE;
LemonBoy47d4e312022-05-04 18:12:55 +01004224
4225 curbuf = save_curbuf;
4226 }
4227 else if (*varname == NUL)
4228 {
4229 // Empty string: return a dict with all the local variables.
4230 if (htname == 'b')
4231 v = &buf->b_bufvar;
4232 else if (htname == 'w')
4233 v = &win->w_winvar;
4234 else
4235 v = &tp->tp_winvar;
4236 copy_tv(&v->di_tv, rettv);
4237 done = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004238 }
4239 else
4240 {
LemonBoy47d4e312022-05-04 18:12:55 +01004241 hashtab_T *ht;
4242
4243 if (htname == 'b')
4244 ht = &buf->b_vars->dv_hashtab;
4245 else if (htname == 'w')
4246 ht = &win->w_vars->dv_hashtab;
4247 else
4248 ht = &tp->tp_vars->dv_hashtab;
4249
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004250 // Look up the variable.
LemonBoy47d4e312022-05-04 18:12:55 +01004251 v = find_var_in_ht(ht, htname, varname, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004252 if (v != NULL)
4253 {
4254 copy_tv(&v->di_tv, rettv);
4255 done = TRUE;
4256 }
4257 }
4258 }
4259
4260 if (need_switch_win)
4261 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004262 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004263 }
4264
LemonBoy47d4e312022-05-04 18:12:55 +01004265 if (!done && deftv->v_type != VAR_UNKNOWN)
4266 // use the default value
4267 copy_tv(deftv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004268
4269 --emsg_off;
4270}
4271
4272/*
LemonBoy47d4e312022-05-04 18:12:55 +01004273 * getwinvar() and gettabwinvar()
4274 */
4275 static void
4276getwinvar(
4277 typval_T *argvars,
4278 typval_T *rettv,
4279 int off) // 1 for gettabwinvar()
4280{
4281 char_u *varname;
4282 tabpage_T *tp;
4283 win_T *win;
4284
4285 if (off == 1)
4286 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4287 else
4288 tp = curtab;
4289 win = find_win_by_nr(&argvars[off], tp);
4290 varname = tv_get_string_chk(&argvars[off + 1]);
4291
4292 get_var_from(varname, rettv, &argvars[off + 2], 'w', tp, win, NULL);
4293}
4294
4295/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02004296 * Set option "varname" to the value of "varp" for the current buffer/window.
4297 */
4298 static void
4299set_option_from_tv(char_u *varname, typval_T *varp)
4300{
4301 long numval = 0;
4302 char_u *strval;
4303 char_u nbuf[NUMBUFLEN];
4304 int error = FALSE;
4305
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004306 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004307 {
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004308 if (is_string_option(varname))
4309 {
4310 emsg(_(e_string_required));
4311 return;
4312 }
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004313 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004314 strval = (char_u *)"0"; // avoid using "false"
4315 }
4316 else
4317 {
4318 if (!in_vim9script() || varp->v_type != VAR_STRING)
4319 numval = (long)tv_get_number_chk(varp, &error);
4320 strval = tv_get_string_buf_chk(varp, nbuf);
4321 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02004322 if (!error && strval != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004323 set_option_value_give_err(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar191929b2020-08-19 21:20:49 +02004324}
4325
4326/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004327 * "setwinvar()" and "settabwinvar()" functions
4328 */
4329 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004330setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004331{
4332 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004333 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004334 int need_switch_win;
4335 char_u *varname, *winvarname;
4336 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004337 tabpage_T *tp = NULL;
4338
4339 if (check_secure())
4340 return;
4341
4342 if (off == 1)
4343 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4344 else
4345 tp = curtab;
4346 win = find_win_by_nr(&argvars[off], tp);
4347 varname = tv_get_string_chk(&argvars[off + 1]);
4348 varp = &argvars[off + 2];
4349
zeertzjqea720ae2023-01-03 10:54:09 +00004350 if (win == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004351 return;
4352
4353 need_switch_win = !(tp == curtab && win == curwin);
4354 if (!need_switch_win
4355 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004356 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004357 if (*varname == '&')
4358 set_option_from_tv(varname + 1, varp);
4359 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004360 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004361 winvarname = alloc(STRLEN(varname) + 3);
4362 if (winvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004363 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004364 STRCPY(winvarname, "w:");
4365 STRCPY(winvarname + 2, varname);
4366 set_var(winvarname, varp, TRUE);
4367 vim_free(winvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004368 }
4369 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004370 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004371 if (need_switch_win)
4372 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004373}
4374
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004375/*
4376 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4377 * v:option_type, and v:option_command.
4378 */
4379 void
4380reset_v_option_vars(void)
4381{
4382 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4383 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4384 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4385 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4386 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4387 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4388}
4389
4390/*
4391 * Add an assert error to v:errors.
4392 */
4393 void
4394assert_error(garray_T *gap)
4395{
4396 struct vimvar *vp = &vimvars[VV_ERRORS];
4397
Bram Moolenaard787e402021-12-24 21:36:12 +00004398 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004399 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004400 set_vim_var_list(VV_ERRORS, list_alloc());
4401 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4402}
4403
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004404 int
4405var_exists(char_u *var)
4406{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004407 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004408 char_u *name;
4409 char_u *tofree;
4410 typval_T tv;
4411 int len = 0;
4412 int n = FALSE;
4413
4414 // get_name_len() takes care of expanding curly braces
4415 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004416 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004417 if (len > 0)
4418 {
4419 if (tofree != NULL)
4420 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004421 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004422 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004423 if (n)
4424 {
4425 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004426 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004427 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4428 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004429 if (n)
4430 clear_tv(&tv);
4431 }
4432 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004433 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004434 n = FALSE;
4435
4436 vim_free(tofree);
4437 return n;
4438}
4439
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004440static lval_T *redir_lval = NULL;
4441#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4442static garray_T redir_ga; // only valid when redir_lval is not NULL
4443static char_u *redir_endp = NULL;
4444static char_u *redir_varname = NULL;
4445
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004446 int
4447alloc_redir_lval(void)
4448{
4449 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4450 if (redir_lval == NULL)
4451 return FAIL;
4452 return OK;
4453}
4454
4455 void
4456clear_redir_lval(void)
4457{
4458 VIM_CLEAR(redir_lval);
4459}
4460
4461 void
4462init_redir_ga(void)
4463{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004464 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004465}
4466
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004467/*
4468 * Start recording command output to a variable
4469 * When "append" is TRUE append to an existing variable.
4470 * Returns OK if successfully completed the setup. FAIL otherwise.
4471 */
4472 int
4473var_redir_start(char_u *name, int append)
4474{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004475 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004476 typval_T tv;
4477
4478 // Catch a bad name early.
4479 if (!eval_isnamec1(*name))
4480 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004481 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004482 return FAIL;
4483 }
4484
4485 // Make a copy of the name, it is used in redir_lval until redir ends.
4486 redir_varname = vim_strsave(name);
4487 if (redir_varname == NULL)
4488 return FAIL;
4489
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004490 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004491 {
4492 var_redir_stop();
4493 return FAIL;
4494 }
4495
4496 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004497 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004498
4499 // Parse the variable name (can be a dict or list entry).
4500 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4501 FNE_CHECK_START);
4502 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4503 {
4504 clear_lval(redir_lval);
4505 if (redir_endp != NULL && *redir_endp != NUL)
4506 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004507 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004508 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004509 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004510 redir_endp = NULL; // don't store a value, only cleanup
4511 var_redir_stop();
4512 return FAIL;
4513 }
4514
4515 // check if we can write to the variable: set it to or append an empty
4516 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004517 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004518 tv.v_type = VAR_STRING;
4519 tv.vval.v_string = (char_u *)"";
4520 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004521 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004522 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004523 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004524 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004525 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004526 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004527 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004528 {
4529 redir_endp = NULL; // don't store a value, only cleanup
4530 var_redir_stop();
4531 return FAIL;
4532 }
4533
4534 return OK;
4535}
4536
4537/*
4538 * Append "value[value_len]" to the variable set by var_redir_start().
4539 * The actual appending is postponed until redirection ends, because the value
4540 * appended may in fact be the string we write to, changing it may cause freed
4541 * memory to be used:
4542 * :redir => foo
4543 * :let foo
4544 * :redir END
4545 */
4546 void
4547var_redir_str(char_u *value, int value_len)
4548{
4549 int len;
4550
4551 if (redir_lval == NULL)
4552 return;
4553
4554 if (value_len == -1)
4555 len = (int)STRLEN(value); // Append the entire string
4556 else
4557 len = value_len; // Append only "value_len" characters
4558
4559 if (ga_grow(&redir_ga, len) == OK)
4560 {
4561 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4562 redir_ga.ga_len += len;
4563 }
4564 else
4565 var_redir_stop();
4566}
4567
4568/*
4569 * Stop redirecting command output to a variable.
4570 * Frees the allocated memory.
4571 */
4572 void
4573var_redir_stop(void)
4574{
4575 typval_T tv;
4576
4577 if (EVALCMD_BUSY)
4578 {
4579 redir_lval = NULL;
4580 return;
4581 }
4582
4583 if (redir_lval != NULL)
4584 {
4585 // If there was no error: assign the text to the variable.
4586 if (redir_endp != NULL)
4587 {
4588 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4589 tv.v_type = VAR_STRING;
4590 tv.vval.v_string = redir_ga.ga_data;
4591 // Call get_lval() again, if it's inside a Dict or List it may
4592 // have changed.
4593 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4594 FALSE, FALSE, 0, FNE_CHECK_START);
4595 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004597 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004598 clear_lval(redir_lval);
4599 }
4600
4601 // free the collected output
4602 VIM_CLEAR(redir_ga.ga_data);
4603
4604 VIM_CLEAR(redir_lval);
4605 }
4606 VIM_CLEAR(redir_varname);
4607}
4608
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004609/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004610 * Get the collected redirected text and clear redir_ga.
4611 */
4612 char_u *
4613get_clear_redir_ga(void)
4614{
4615 char_u *res;
4616
4617 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4618 res = redir_ga.ga_data;
4619 redir_ga.ga_data = NULL;
4620 return res;
4621}
4622
4623/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004624 * "gettabvar()" function
4625 */
4626 void
4627f_gettabvar(typval_T *argvars, typval_T *rettv)
4628{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004629 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004630 tabpage_T *tp;
4631 win_T *win = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004632
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004633 if (in_vim9script()
4634 && (check_for_number_arg(argvars, 0) == FAIL
4635 || check_for_string_arg(argvars, 1) == FAIL))
4636 return;
4637
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004638 varname = tv_get_string_chk(&argvars[1]);
4639 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
LemonBoy47d4e312022-05-04 18:12:55 +01004640 if (tp != NULL)
4641 win = tp == curtab || tp->tp_firstwin == NULL ? firstwin
4642 : tp->tp_firstwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004643
LemonBoy47d4e312022-05-04 18:12:55 +01004644 get_var_from(varname, rettv, &argvars[2], 't', tp, win, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004645}
4646
4647/*
4648 * "gettabwinvar()" function
4649 */
4650 void
4651f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4652{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004653 if (in_vim9script()
4654 && (check_for_number_arg(argvars, 0) == FAIL
4655 || check_for_number_arg(argvars, 1) == FAIL
4656 || check_for_string_arg(argvars, 2) == FAIL))
4657 return;
4658
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004659 getwinvar(argvars, rettv, 1);
4660}
4661
4662/*
4663 * "getwinvar()" function
4664 */
4665 void
4666f_getwinvar(typval_T *argvars, typval_T *rettv)
4667{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004668 if (in_vim9script()
4669 && (check_for_number_arg(argvars, 0) == FAIL
4670 || check_for_string_arg(argvars, 1) == FAIL))
4671 return;
4672
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004673 getwinvar(argvars, rettv, 0);
4674}
4675
4676/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004677 * "getbufvar()" function
4678 */
4679 void
4680f_getbufvar(typval_T *argvars, typval_T *rettv)
4681{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004682 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004683 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004684
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004685 if (in_vim9script()
4686 && (check_for_buffer_arg(argvars, 0) == FAIL
4687 || check_for_string_arg(argvars, 1) == FAIL))
4688 return;
4689
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004690 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004691 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004692
LemonBoy47d4e312022-05-04 18:12:55 +01004693 get_var_from(varname, rettv, &argvars[2], 'b', curtab, curwin, buf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004694}
4695
4696/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004697 * "settabvar()" function
4698 */
4699 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004700f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004701{
4702 tabpage_T *save_curtab;
4703 tabpage_T *tp;
4704 char_u *varname, *tabvarname;
4705 typval_T *varp;
4706
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004707 if (check_secure())
4708 return;
4709
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004710 if (in_vim9script()
4711 && (check_for_number_arg(argvars, 0) == FAIL
4712 || check_for_string_arg(argvars, 1) == FAIL))
4713 return;
4714
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004715 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4716 varname = tv_get_string_chk(&argvars[1]);
4717 varp = &argvars[2];
4718
zeertzjqea720ae2023-01-03 10:54:09 +00004719 if (varname == NULL || tp == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004720 return;
4721
4722 save_curtab = curtab;
4723 goto_tabpage_tp(tp, FALSE, FALSE);
4724
4725 tabvarname = alloc(STRLEN(varname) + 3);
4726 if (tabvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004727 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004728 STRCPY(tabvarname, "t:");
4729 STRCPY(tabvarname + 2, varname);
4730 set_var(tabvarname, varp, TRUE);
4731 vim_free(tabvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004732 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004733
4734 // Restore current tabpage
4735 if (valid_tabpage(save_curtab))
4736 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004737}
4738
4739/*
4740 * "settabwinvar()" function
4741 */
4742 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004743f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004744{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004745 if (in_vim9script()
4746 && (check_for_number_arg(argvars, 0) == FAIL
4747 || check_for_number_arg(argvars, 1) == FAIL
4748 || check_for_string_arg(argvars, 2) == FAIL))
4749 return;
4750
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004751 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004752}
4753
4754/*
4755 * "setwinvar()" function
4756 */
4757 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004758f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004759{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004760 if (in_vim9script()
4761 && (check_for_number_arg(argvars, 0) == FAIL
4762 || check_for_string_arg(argvars, 1) == FAIL))
4763 return;
4764
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004765 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004766}
4767
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004768/*
4769 * "setbufvar()" function
4770 */
4771 void
4772f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4773{
4774 buf_T *buf;
4775 char_u *varname, *bufvarname;
4776 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004777
4778 if (check_secure())
4779 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004780
4781 if (in_vim9script()
4782 && (check_for_buffer_arg(argvars, 0) == FAIL
4783 || check_for_string_arg(argvars, 1) == FAIL))
4784 return;
4785
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004786 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004787 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004788 varp = &argvars[2];
4789
zeertzjqea720ae2023-01-03 10:54:09 +00004790 if (buf == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004791 return;
4792
4793 if (*varname == '&')
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004794 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004795 aco_save_T aco;
4796
4797 // Set curbuf to be our buf, temporarily.
4798 aucmd_prepbuf(&aco, buf);
4799 if (curbuf == buf)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004800 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004801 // Only when it worked to set "curbuf".
4802 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004803
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004804 // reset notion of buffer
4805 aucmd_restbuf(&aco);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004806 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004807 }
4808 else
4809 {
4810 bufvarname = alloc(STRLEN(varname) + 3);
4811 if (bufvarname != NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004812 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004813 buf_T *save_curbuf = curbuf;
Bram Moolenaar86015452020-03-29 15:12:15 +02004814
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004815 curbuf = buf;
4816 STRCPY(bufvarname, "b:");
4817 STRCPY(bufvarname + 2, varname);
4818 set_var(bufvarname, varp, TRUE);
4819 vim_free(bufvarname);
4820 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004821 }
4822 }
4823}
4824
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004825/*
4826 * Get a callback from "arg". It can be a Funcref or a function name.
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004827 * When "arg" is zero "res.cb_name" is set to an empty string.
4828 * If "res.cb_name" is allocated then "res.cb_free_name" is set to TRUE.
4829 * "res.cb_name" is set to NULL for an invalid argument.
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004830 */
4831 callback_T
4832get_callback(typval_T *arg)
4833{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004834 callback_T res;
4835 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004836
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004837 CLEAR_FIELD(res);
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004838 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4839 {
4840 res.cb_partial = arg->vval.v_partial;
4841 ++res.cb_partial->pt_refcount;
4842 res.cb_name = partial_name(res.cb_partial);
4843 }
4844 else
4845 {
Bram Moolenaar14e579092020-03-07 16:59:25 +01004846 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4847 && isdigit(*arg->vval.v_string))
4848 r = FAIL;
4849 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004850 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004851 res.cb_name = arg->vval.v_string;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004852 if (arg->v_type == VAR_STRING)
4853 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004854 char_u *name = get_scriptlocal_funcname(arg->vval.v_string);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004855 if (name != NULL)
4856 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004857 res.cb_name = name;
4858 res.cb_free_name = TRUE;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004859 }
4860 }
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004861 func_ref(res.cb_name);
4862 }
4863 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004864 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004865 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004866 r = FAIL;
4867
4868 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004869 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004870 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004871 res.cb_name = NULL;
4872 }
4873 }
4874 return res;
4875}
4876
4877/*
4878 * Copy a callback into a typval_T.
4879 */
4880 void
4881put_callback(callback_T *cb, typval_T *tv)
4882{
4883 if (cb->cb_partial != NULL)
4884 {
4885 tv->v_type = VAR_PARTIAL;
4886 tv->vval.v_partial = cb->cb_partial;
4887 ++tv->vval.v_partial->pt_refcount;
4888 }
4889 else
4890 {
4891 tv->v_type = VAR_FUNC;
4892 tv->vval.v_string = vim_strsave(cb->cb_name);
4893 func_ref(cb->cb_name);
4894 }
4895}
4896
4897/*
4898 * Make a copy of "src" into "dest", allocating the function name if needed,
4899 * without incrementing the refcount.
4900 */
4901 void
4902set_callback(callback_T *dest, callback_T *src)
4903{
4904 if (src->cb_partial == NULL)
4905 {
4906 // just a function name, make a copy
4907 dest->cb_name = vim_strsave(src->cb_name);
4908 dest->cb_free_name = TRUE;
4909 }
4910 else
4911 {
4912 // cb_name is a pointer into cb_partial
4913 dest->cb_name = src->cb_name;
4914 dest->cb_free_name = FALSE;
4915 }
4916 dest->cb_partial = src->cb_partial;
4917}
4918
4919/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004920 * Copy callback from "src" to "dest", incrementing the refcounts.
4921 */
4922 void
4923copy_callback(callback_T *dest, callback_T *src)
4924{
4925 dest->cb_partial = src->cb_partial;
4926 if (dest->cb_partial != NULL)
4927 {
4928 dest->cb_name = src->cb_name;
4929 dest->cb_free_name = FALSE;
4930 ++dest->cb_partial->pt_refcount;
4931 }
4932 else
4933 {
4934 dest->cb_name = vim_strsave(src->cb_name);
4935 dest->cb_free_name = TRUE;
4936 func_ref(src->cb_name);
4937 }
4938}
4939
4940/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004941 * When a callback refers to an autoload import, change the function name to
4942 * the "path#name" form. Uses the current script context.
4943 * Only works when the name is allocated.
4944 */
4945 void
4946expand_autload_callback(callback_T *cb)
4947{
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004948 char_u *name;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004949 char_u *p;
4950 imported_T *import;
4951
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004952 if (!in_vim9script() || cb->cb_name == NULL
4953 || (!cb->cb_free_name
4954 && (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004955 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004956 if (cb->cb_partial != NULL)
4957 name = cb->cb_partial->pt_name;
4958 else
4959 name = cb->cb_name;
4960 p = vim_strchr(name, '.');
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004961 if (p == NULL)
4962 return;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004963
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004964 import = find_imported(name, p - name, FALSE);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004965 if (import == NULL || !SCRIPT_ID_VALID(import->imp_sid))
4966 return;
4967
4968 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
4969 if (si->sn_autoload_prefix == NULL)
4970 return;
4971
4972 char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
4973 if (newname == NULL)
4974 return;
4975
4976 if (cb->cb_partial != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004977 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004978 if (cb->cb_name == cb->cb_partial->pt_name)
4979 cb->cb_name = newname;
4980 vim_free(cb->cb_partial->pt_name);
4981 cb->cb_partial->pt_name = newname;
4982 }
4983 else
4984 {
4985 vim_free(cb->cb_name);
4986 cb->cb_name = newname;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004987 }
4988}
4989
4990/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004991 * Unref/free "callback" returned by get_callback() or set_callback().
4992 */
4993 void
4994free_callback(callback_T *callback)
4995{
4996 if (callback->cb_partial != NULL)
4997 {
4998 partial_unref(callback->cb_partial);
4999 callback->cb_partial = NULL;
5000 }
5001 else if (callback->cb_name != NULL)
5002 func_unref(callback->cb_name);
5003 if (callback->cb_free_name)
5004 {
5005 vim_free(callback->cb_name);
5006 callback->cb_free_name = FALSE;
5007 }
5008 callback->cb_name = NULL;
5009}
5010
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005011#endif // FEAT_EVAL