blob: fd3c937b51c421f704ee878027bca80610ce5c90 [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},
Bram Moolenaarc0c2c262023-01-12 21:08:53 +0000142 {VV_NAME("t_class", VAR_NUMBER), NULL, VV_RO},
143 {VV_NAME("t_object", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaard787e402021-12-24 21:36:12 +0000144 {VV_NAME("termrfgresp", VAR_STRING), NULL, VV_RO},
145 {VV_NAME("termrbgresp", VAR_STRING), NULL, VV_RO},
146 {VV_NAME("termu7resp", VAR_STRING), NULL, VV_RO},
147 {VV_NAME("termstyleresp", VAR_STRING), NULL, VV_RO},
148 {VV_NAME("termblinkresp", VAR_STRING), NULL, VV_RO},
149 {VV_NAME("event", VAR_DICT), NULL, VV_RO},
150 {VV_NAME("versionlong", VAR_NUMBER), NULL, VV_RO},
151 {VV_NAME("echospace", VAR_NUMBER), NULL, VV_RO},
152 {VV_NAME("argv", VAR_LIST), &t_list_string, VV_RO},
153 {VV_NAME("collate", VAR_STRING), NULL, VV_RO},
154 {VV_NAME("exiting", VAR_SPECIAL), NULL, VV_RO},
155 {VV_NAME("colornames", VAR_DICT), &t_dict_string, VV_RO},
156 {VV_NAME("sizeofint", VAR_NUMBER), NULL, VV_RO},
157 {VV_NAME("sizeoflong", VAR_NUMBER), NULL, VV_RO},
158 {VV_NAME("sizeofpointer", VAR_NUMBER), NULL, VV_RO},
naohiro ono56200ee2022-01-01 14:59:44 +0000159 {VV_NAME("maxcol", VAR_NUMBER), NULL, VV_RO},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200160 {VV_NAME("python3_version", VAR_NUMBER), NULL, VV_RO},
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200161 {VV_NAME("t_typealias", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200162};
163
164// shorthand
Bram Moolenaard787e402021-12-24 21:36:12 +0000165#define vv_tv_type vv_di.di_tv.v_type
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200166#define vv_nr vv_di.di_tv.vval.v_number
167#define vv_float vv_di.di_tv.vval.v_float
168#define vv_str vv_di.di_tv.vval.v_string
169#define vv_list vv_di.di_tv.vval.v_list
170#define vv_dict vv_di.di_tv.vval.v_dict
171#define vv_blob vv_di.di_tv.vval.v_blob
172#define vv_tv vv_di.di_tv
173
174static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200175static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200176#define vimvarht vimvardict.dv_hashtab
177
178// for VIM_VERSION_ defines
179#include "version.h"
180
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200181static void list_glob_vars(int *first);
182static void list_buf_vars(int *first);
183static void list_win_vars(int *first);
184static void list_tab_vars(int *first);
185static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100186static 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 +0200187static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
188static 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 +0200189static void list_one_var(dictitem_T *v, char *prefix, int *first);
190static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
191
192/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200193 * Initialize global and vim special variables
194 */
195 void
196evalvars_init(void)
197{
198 int i;
199 struct vimvar *p;
200
201 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
202 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
203 vimvardict.dv_lock = VAR_FIXED;
204 hash_init(&compat_hashtab);
205
206 for (i = 0; i < VV_LEN; ++i)
207 {
208 p = &vimvars[i];
209 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
210 {
Bram Moolenaar097c5372023-05-24 21:02:24 +0100211 iemsg("Name too long, increase size of dictitem16_T");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200212 getout(1);
213 }
214 STRCPY(p->vv_di.di_key, p->vv_name);
215 if (p->vv_flags & VV_RO)
216 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
217 else if (p->vv_flags & VV_RO_SBX)
218 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
219 else
220 p->vv_di.di_flags = DI_FLAGS_FIX;
221
222 // add to v: scope dict, unless the value is not always available
Bram Moolenaard787e402021-12-24 21:36:12 +0000223 if (p->vv_tv_type != VAR_UNKNOWN)
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000224 hash_add(&vimvarht, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200225 if (p->vv_flags & VV_COMPAT)
226 // add to compat scope dict
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000227 hash_add(&compat_hashtab, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200228 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200229 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
230 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200231
232 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
233 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100234 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200235 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
236 set_vim_var_list(VV_ERRORS, list_alloc());
237 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
238
239 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
240 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
241 set_vim_var_nr(VV_NONE, VVAL_NONE);
242 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100243 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
244 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100245 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaar69b30722021-11-02 21:39:49 +0000246 set_vim_var_nr(VV_SIZEOFINT, sizeof(int));
247 set_vim_var_nr(VV_SIZEOFLONG, sizeof(long));
248 set_vim_var_nr(VV_SIZEOFPOINTER, sizeof(char *));
naohiro ono56200ee2022-01-01 14:59:44 +0000249 set_vim_var_nr(VV_MAXCOL, MAXCOL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200250
251 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
252 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
253 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
254 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
255 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
256 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
257 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
258 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
259 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
260 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
261 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarc0c2c262023-01-12 21:08:53 +0000262 set_vim_var_nr(VV_TYPE_CLASS, VAR_TYPE_CLASS);
263 set_vim_var_nr(VV_TYPE_OBJECT, VAR_TYPE_OBJECT);
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200264 set_vim_var_nr(VV_TYPE_TYPEALIAS, VAR_TYPE_TYPEALIAS);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200265
266 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
267
Drew Vogele30d1022021-10-24 20:35:07 +0100268 set_vim_var_dict(VV_COLORNAMES, dict_alloc());
269
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200270#ifdef FEAT_PYTHON3
271 set_vim_var_nr(VV_PYTHON3_VERSION, python3_version());
272#endif
273
Bram Moolenaar439c0362020-06-06 15:58:03 +0200274 // Default for v:register is not 0 but '"'. This is adjusted once the
275 // clipboard has been setup by calling reset_reg_var().
276 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200277}
278
279#if defined(EXITFREE) || defined(PROTO)
280/*
281 * Free all vim variables information on exit
282 */
283 void
284evalvars_clear(void)
285{
286 int i;
287 struct vimvar *p;
288
289 for (i = 0; i < VV_LEN; ++i)
290 {
291 p = &vimvars[i];
292 if (p->vv_di.di_tv.v_type == VAR_STRING)
293 VIM_CLEAR(p->vv_str);
294 else if (p->vv_di.di_tv.v_type == VAR_LIST)
295 {
296 list_unref(p->vv_list);
297 p->vv_list = NULL;
298 }
299 }
300 hash_clear(&vimvarht);
301 hash_init(&vimvarht); // garbage_collect() will access it
302 hash_clear(&compat_hashtab);
303
304 // global variables
305 vars_clear(&globvarht);
306
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100307 // Script-local variables. Clear all the variables here.
308 // The scriptvar_T is cleared later in free_scriptnames(), because a
309 // variable in one script might hold a reference to the whole scope of
310 // another script.
311 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200312 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200313}
314#endif
315
316 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200317garbage_collect_globvars(int copyID)
318{
319 return set_ref_in_ht(&globvarht, copyID, NULL);
320}
321
322 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200323garbage_collect_vimvars(int copyID)
324{
325 return set_ref_in_ht(&vimvarht, copyID, NULL);
326}
327
328 int
329garbage_collect_scriptvars(int copyID)
330{
Bram Moolenaared234f22020-10-15 20:42:20 +0200331 int i;
332 int idx;
333 int abort = FALSE;
334 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200335
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100336 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200337 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200338 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
339
Bram Moolenaared234f22020-10-15 20:42:20 +0200340 si = SCRIPT_ITEM(i);
341 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
342 {
343 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
344
Bram Moolenaard00a7fb2021-03-08 20:47:14 +0100345 if (sv->sv_name != NULL)
346 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
Bram Moolenaared234f22020-10-15 20:42:20 +0200347 }
348 }
349
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200350 return abort;
351}
352
353/*
354 * Set an internal variable to a string value. Creates the variable if it does
355 * not already exist.
356 */
357 void
358set_internal_string_var(char_u *name, char_u *value)
359{
360 char_u *val;
361 typval_T *tvp;
362
363 val = vim_strsave(value);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000364 if (val == NULL)
365 return;
366
367 tvp = alloc_string_tv(val);
368 if (tvp == NULL)
369 return;
370
371 set_var(name, tvp, FALSE);
372 free_tv(tvp);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200373}
374
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200375 int
376eval_charconvert(
377 char_u *enc_from,
378 char_u *enc_to,
379 char_u *fname_from,
380 char_u *fname_to)
381{
382 int err = FALSE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000383 sctx_T saved_sctx = current_sctx;
384 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200385
386 set_vim_var_string(VV_CC_FROM, enc_from, -1);
387 set_vim_var_string(VV_CC_TO, enc_to, -1);
388 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
389 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000390 ctx = get_option_sctx("charconvert");
391 if (ctx != NULL)
392 current_sctx = *ctx;
393
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100394 if (eval_to_bool(p_ccv, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200395 err = TRUE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000396
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200397 set_vim_var_string(VV_CC_FROM, NULL, -1);
398 set_vim_var_string(VV_CC_TO, NULL, -1);
399 set_vim_var_string(VV_FNAME_IN, NULL, -1);
400 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000401 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200402
403 if (err)
404 return FAIL;
405 return OK;
406}
407
408# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
409 int
410eval_printexpr(char_u *fname, char_u *args)
411{
412 int err = FALSE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000413 sctx_T saved_sctx = current_sctx;
414 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200415
416 set_vim_var_string(VV_FNAME_IN, fname, -1);
417 set_vim_var_string(VV_CMDARG, args, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000418 ctx = get_option_sctx("printexpr");
419 if (ctx != NULL)
420 current_sctx = *ctx;
421
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100422 if (eval_to_bool(p_pexpr, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200423 err = TRUE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000424
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200425 set_vim_var_string(VV_FNAME_IN, NULL, -1);
426 set_vim_var_string(VV_CMDARG, NULL, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000427 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200428
429 if (err)
430 {
431 mch_remove(fname);
432 return FAIL;
433 }
434 return OK;
435}
436# endif
437
438# if defined(FEAT_DIFF) || defined(PROTO)
439 void
440eval_diff(
441 char_u *origfile,
442 char_u *newfile,
443 char_u *outfile)
444{
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000445 sctx_T saved_sctx = current_sctx;
446 sctx_T *ctx;
447 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200448
449 set_vim_var_string(VV_FNAME_IN, origfile, -1);
450 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
451 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000452
453 ctx = get_option_sctx("diffexpr");
454 if (ctx != NULL)
455 current_sctx = *ctx;
456
457 // errors are ignored
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100458 tv = eval_expr_ext(p_dex, NULL, TRUE);
Bram Moolenaar39b89442022-01-22 18:21:36 +0000459 free_tv(tv);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000460
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200461 set_vim_var_string(VV_FNAME_IN, NULL, -1);
462 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
463 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000464 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200465}
466
467 void
468eval_patch(
469 char_u *origfile,
470 char_u *difffile,
471 char_u *outfile)
472{
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000473 sctx_T saved_sctx = current_sctx;
474 sctx_T *ctx;
475 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200476
477 set_vim_var_string(VV_FNAME_IN, origfile, -1);
478 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
479 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000480
481 ctx = get_option_sctx("patchexpr");
482 if (ctx != NULL)
483 current_sctx = *ctx;
484
485 // errors are ignored
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100486 tv = eval_expr_ext(p_pex, NULL, TRUE);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000487 free_tv(tv);
488
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200489 set_vim_var_string(VV_FNAME_IN, NULL, -1);
490 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
491 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000492 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200493}
494# endif
495
496#if defined(FEAT_SPELL) || defined(PROTO)
497/*
498 * Evaluate an expression to a list with suggestions.
499 * For the "expr:" part of 'spellsuggest'.
500 * Returns NULL when there is an error.
501 */
502 list_T *
503eval_spell_expr(char_u *badword, char_u *expr)
504{
505 typval_T save_val;
506 typval_T rettv;
507 list_T *list = NULL;
508 char_u *p = skipwhite(expr);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000509 sctx_T saved_sctx = current_sctx;
510 sctx_T *ctx;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100511 int r;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200512
513 // Set "v:val" to the bad word.
514 prepare_vimvar(VV_VAL, &save_val);
515 set_vim_var_string(VV_VAL, badword, -1);
516 if (p_verbose == 0)
517 ++emsg_off;
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000518 ctx = get_option_sctx("spellsuggest");
519 if (ctx != NULL)
520 current_sctx = *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200521
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100522 r = may_call_simple_func(p, &rettv);
523 if (r == NOTDONE)
524 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
525 if (r == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200526 {
527 if (rettv.v_type != VAR_LIST)
528 clear_tv(&rettv);
529 else
530 list = rettv.vval.v_list;
531 }
532
533 if (p_verbose == 0)
534 --emsg_off;
535 clear_tv(get_vim_var_tv(VV_VAL));
536 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000537 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200538
539 return list;
540}
541
542/*
543 * "list" is supposed to contain two items: a word and a number. Return the
544 * word in "pp" and the number as the return value.
545 * Return -1 if anything isn't right.
546 * Used to get the good word and score from the eval_spell_expr() result.
547 */
548 int
549get_spellword(list_T *list, char_u **pp)
550{
551 listitem_T *li;
552
553 li = list->lv_first;
554 if (li == NULL)
555 return -1;
556 *pp = tv_get_string(&li->li_tv);
557
558 li = li->li_next;
559 if (li == NULL)
560 return -1;
561 return (int)tv_get_number(&li->li_tv);
562}
563#endif
564
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200565/*
566 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200567 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200568 * When not used yet add the variable to the v: hashtable.
569 */
570 void
571prepare_vimvar(int idx, typval_T *save_tv)
572{
573 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200574 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaard787e402021-12-24 21:36:12 +0000575 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000576 hash_add(&vimvarht, vimvars[idx].vv_di.di_key, "prepare vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200577}
578
579/*
580 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200581 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200582 * When no longer defined, remove the variable from the v: hashtable.
583 */
584 void
585restore_vimvar(int idx, typval_T *save_tv)
586{
587 hashitem_T *hi;
588
589 vimvars[idx].vv_tv = *save_tv;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000590 if (vimvars[idx].vv_tv_type != VAR_UNKNOWN)
591 return;
592
593 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
594 if (HASHITEM_EMPTY(hi))
595 internal_error("restore_vimvar()");
596 else
597 hash_remove(&vimvarht, hi, "restore vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200598}
599
600/*
601 * List Vim variables.
602 */
603 static void
604list_vim_vars(int *first)
605{
606 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
607}
608
609/*
610 * List script-local variables, if there is a script.
611 */
612 static void
613list_script_vars(int *first)
614{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200615 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200616 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
617 "s:", FALSE, first);
618}
619
620/*
Bram Moolenaar9510d222022-09-11 15:14:05 +0100621 * Return TRUE if "name" starts with "g:", "w:", "t:" or "b:".
622 * But only when an identifier character follows.
623 */
624 int
625is_scoped_variable(char_u *name)
626{
627 return vim_strchr((char_u *)"gwbt", name[0]) != NULL
628 && name[1] == ':'
629 && eval_isnamec(name[2]);
630}
631
632/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100633 * Evaluate one Vim expression {expr} in string "p" and append the
634 * resulting string to "gap". "p" points to the opening "{".
Bram Moolenaar70c41242022-05-10 18:11:43 +0100635 * When "evaluate" is FALSE only skip over the expression.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100636 * Return a pointer to the character after "}", NULL for an error.
637 */
638 char_u *
Bram Moolenaar70c41242022-05-10 18:11:43 +0100639eval_one_expr_in_str(char_u *p, garray_T *gap, int evaluate)
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100640{
641 char_u *block_start = skipwhite(p + 1); // skip the opening {
642 char_u *block_end = block_start;
643 char_u *expr_val;
644
645 if (*block_start == NUL)
646 {
647 semsg(_(e_missing_close_curly_str), p);
648 return NULL;
649 }
650 if (skip_expr(&block_end, NULL) == FAIL)
651 return NULL;
652 block_end = skipwhite(block_end);
653 if (*block_end != '}')
654 {
655 semsg(_(e_missing_close_curly_str), p);
656 return NULL;
657 }
Bram Moolenaar70c41242022-05-10 18:11:43 +0100658 if (evaluate)
659 {
660 *block_end = NUL;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100661 expr_val = eval_to_string(block_start, TRUE, FALSE);
Bram Moolenaar70c41242022-05-10 18:11:43 +0100662 *block_end = '}';
663 if (expr_val == NULL)
664 return NULL;
665 ga_concat(gap, expr_val);
666 vim_free(expr_val);
667 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100668
669 return block_end + 1;
670}
671
672/*
673 * Evaluate all the Vim expressions {expr} in "str" and return the resulting
674 * string in allocated memory. "{{" is reduced to "{" and "}}" to "}".
675 * Used for a heredoc assignment.
676 * Returns NULL for an error.
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100677 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +0100678 static char_u *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100679eval_all_expr_in_str(char_u *str)
680{
681 garray_T ga;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100682 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100683
684 ga_init2(&ga, 1, 80);
685 p = str;
686
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100687 while (*p != NUL)
688 {
LemonBoy2eaef102022-05-06 13:14:50 +0100689 char_u *lit_start;
LemonBoy2eaef102022-05-06 13:14:50 +0100690 int escaped_brace = FALSE;
691
692 // Look for a block start.
693 lit_start = p;
694 while (*p != '{' && *p != '}' && *p != NUL)
695 ++p;
696
697 if (*p != NUL && *p == p[1])
698 {
699 // Escaped brace, unescape and continue.
700 // Include the brace in the literal string.
701 ++p;
702 escaped_brace = TRUE;
703 }
704 else if (*p == '}')
705 {
706 semsg(_(e_stray_closing_curly_str), str);
707 ga_clear(&ga);
708 return NULL;
709 }
710
711 // Append the literal part.
712 ga_concat_len(&ga, lit_start, (size_t)(p - lit_start));
713
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100714 if (*p == NUL)
LemonBoy2eaef102022-05-06 13:14:50 +0100715 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100716
LemonBoy2eaef102022-05-06 13:14:50 +0100717 if (escaped_brace)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100718 {
LemonBoy2eaef102022-05-06 13:14:50 +0100719 // Skip the second brace.
720 ++p;
721 continue;
722 }
723
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100724 // Evaluate the expression and append the result.
Bram Moolenaar70c41242022-05-10 18:11:43 +0100725 p = eval_one_expr_in_str(p, &ga, TRUE);
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100726 if (p == NULL)
LemonBoy2eaef102022-05-06 13:14:50 +0100727 {
728 ga_clear(&ga);
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100729 return NULL;
730 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100731 }
732 ga_append(&ga, NUL);
733
734 return ga.ga_data;
735}
736
737/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200738 * Get a list of lines from a HERE document. The here document is a list of
739 * lines surrounded by a marker.
740 * cmd << {marker}
741 * {line1}
742 * {line2}
743 * ....
744 * {marker}
745 *
746 * The {marker} is a string. If the optional 'trim' word is supplied before the
747 * marker, then the leading indentation before the lines (matching the
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100748 * indentation in the "cmd" line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200749 *
750 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100751 * tcl, mzscheme), "script_get" is set to TRUE. In this case, if the marker is
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200752 * missing, then '.' is accepted as a marker.
753 *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100754 * When compiling a heredoc assignment to a variable in a Vim9 def function,
755 * "vim9compile" is set to TRUE. In this case, instead of generating a list of
756 * string values from the heredoc, vim9 instructions are generated. On success
757 * the returned list will be empty.
758 *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100759 * Returns a List with {lines} or NULL on failure.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200760 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100761 list_T *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100762heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200763{
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100764 char_u *theline = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200765 char_u *marker;
766 list_T *l;
767 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100768 char_u *str;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200769 int marker_indent_len = 0;
770 int text_indent_len = 0;
771 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200772 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200773 int comment_char = in_vim9script() ? '#' : '"';
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100774 int evalstr = FALSE;
775 int eval_failed = FALSE;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100776 cctx_T *cctx = vim9compile ? eap->cookie : NULL;
777 int count = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200778
779 if (eap->getline == NULL)
780 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000781 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200782 return NULL;
783 }
784
785 // Check for the optional 'trim' word before the marker
786 cmd = skipwhite(cmd);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200787
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100788 while (TRUE)
789 {
790 if (STRNCMP(cmd, "trim", 4) == 0
791 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200792 {
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100793 cmd = skipwhite(cmd + 4);
794
795 // Trim the indentation from all the lines in the here document.
796 // The amount of indentation trimmed is the same as the indentation
797 // of the first line after the :let command line. To find the end
798 // marker the indent of the :let command line is trimmed.
799 p = *eap->cmdlinep;
800 while (VIM_ISWHITE(*p))
801 {
802 p++;
803 marker_indent_len++;
804 }
805 text_indent_len = -1;
806
807 continue;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200808 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100809 if (STRNCMP(cmd, "eval", 4) == 0
810 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
811 {
812 cmd = skipwhite(cmd + 4);
813 evalstr = TRUE;
814 continue;
815 }
816 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200817 }
818
819 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200820 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200821 {
822 marker = skipwhite(cmd);
823 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200824 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200825 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000826 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200827 return NULL;
828 }
829 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200830 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200831 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000832 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200833 return NULL;
834 }
835 }
836 else
837 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200838 // When getting lines for an embedded script, if the marker is missing,
839 // accept '.' as the marker.
840 if (script_get)
841 marker = dot;
842 else
843 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000844 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200845 return NULL;
846 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200847 }
848
849 l = list_alloc();
850 if (l == NULL)
851 return NULL;
852
853 for (;;)
854 {
855 int mi = 0;
856 int ti = 0;
857
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100858 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200859 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
860 if (theline == NULL)
861 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000862 semsg(_(e_missing_end_marker_str), marker);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200863 break;
864 }
865
866 // with "trim": skip the indent matching the :let line to find the
867 // marker
868 if (marker_indent_len > 0
869 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
870 mi = marker_indent_len;
871 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200872 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200873
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100874 // If expression evaluation failed in the heredoc, then skip till the
875 // end marker.
876 if (eval_failed)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100877 continue;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100878
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200879 if (text_indent_len == -1 && *theline != NUL)
880 {
881 // set the text indent from the first line.
882 p = theline;
883 text_indent_len = 0;
884 while (VIM_ISWHITE(*p))
885 {
886 p++;
887 text_indent_len++;
888 }
889 text_indent = vim_strnsave(theline, text_indent_len);
890 }
891 // with "trim": skip the indent matching the first line
892 if (text_indent != NULL)
893 for (ti = 0; ti < text_indent_len; ++ti)
894 if (theline[ti] != text_indent[ti])
895 break;
896
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100897 str = theline + ti;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100898 if (vim9compile)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100899 {
LemonBoy2eaef102022-05-06 13:14:50 +0100900 if (compile_all_expr_in_str(str, evalstr, cctx) == FAIL)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100901 {
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100902 vim_free(theline);
903 vim_free(text_indent);
904 return FAIL;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100905 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100906 count++;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100907 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100908 else
909 {
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100910 if (evalstr && !eap->skip)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100911 {
912 str = eval_all_expr_in_str(str);
913 if (str == NULL)
914 {
915 // expression evaluation failed
916 eval_failed = TRUE;
917 continue;
918 }
919 vim_free(theline);
920 theline = str;
921 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100922
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100923 if (list_append_string(l, str, -1) == FAIL)
924 break;
925 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200926 }
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100927 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200928 vim_free(text_indent);
929
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100930 if (vim9compile && cctx->ctx_skip != SKIP_YES && !eval_failed)
931 generate_NEWLIST(cctx, count, FALSE);
932
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100933 if (eval_failed)
934 {
935 // expression evaluation in the heredoc failed
936 list_free(l);
937 return NULL;
938 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200939 return l;
940}
941
942/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200943 * Vim9 variable declaration:
944 * ":var name"
945 * ":var name: type"
946 * ":var name = expr"
947 * ":var name: type = expr"
948 * etc.
949 */
950 void
951ex_var(exarg_T *eap)
952{
Bram Moolenaar330a3882022-03-05 11:05:57 +0000953 char_u *p = eap->cmd;
Bram Moolenaare1d12112022-03-05 11:37:48 +0000954 int has_var;
Bram Moolenaar330a3882022-03-05 11:05:57 +0000955
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200956 if (!in_vim9script())
957 {
958 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
959 return;
960 }
Bram Moolenaare1d12112022-03-05 11:37:48 +0000961 has_var = checkforcmd_noparen(&p, "var", 3);
962 if (current_sctx.sc_sid == 0 && has_var)
Bram Moolenaar0e1574c2022-03-03 17:05:35 +0000963 {
964 emsg(_(e_cannot_declare_variable_on_command_line));
965 return;
966 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200967 ex_let(eap);
968}
969
970/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200971 * ":let" list all variable values
972 * ":let var1 var2" list variable values
973 * ":let var = expr" assignment command.
974 * ":let var += expr" assignment command.
975 * ":let var -= expr" assignment command.
976 * ":let var *= expr" assignment command.
977 * ":let var /= expr" assignment command.
978 * ":let var %= expr" assignment command.
979 * ":let var .= expr" assignment command.
980 * ":let var ..= expr" assignment command.
981 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100982 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100983 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200984 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200985 * ":final var = expr" assignment command.
986 * ":final [var1, var2] = expr" unpack list.
987 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200988 * ":const" list all variable values
989 * ":const var1 var2" list variable values
990 * ":const var = expr" assignment command.
991 * ":const [var1, var2] = expr" unpack list.
992 */
993 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200994ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200995{
996 char_u *arg = eap->arg;
997 char_u *expr = NULL;
998 typval_T rettv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200999 int var_count = 0;
1000 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001001 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001002 char_u *argend;
1003 int first = TRUE;
1004 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001005 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001006 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001007 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001008
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001009 if (eap->cmdidx == CMD_final && !vim9script)
1010 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001011 // In legacy Vim script ":final" is short for ":finally".
1012 ex_finally(eap);
1013 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001014 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +02001015 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001016 {
1017 emsg(_(e_cannot_use_let_in_vim9_script));
1018 return;
1019 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001020
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001021 if (eap->cmdidx == CMD_const)
1022 flags |= ASSIGN_CONST;
1023 else if (eap->cmdidx == CMD_final)
1024 flags |= ASSIGN_FINAL;
1025
1026 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001027 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001028 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001030 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001031 if (argend == NULL)
1032 return;
1033 if (argend > arg && argend[-1] == '.') // for var.='str'
1034 --argend;
1035 expr = skipwhite(argend);
1036 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02001037 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001038 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +02001039 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
1040 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +02001041 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001042 {
1043 // ":let" without "=": list variables
1044 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001045 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001046 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001047 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001048 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001049 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001050 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001051 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001052 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001053 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001054 else
1055 // Vim9 declaration ":var name: type"
1056 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001057 }
1058 else
1059 {
1060 // ":let var1 var2" - list values
1061 arg = list_arg_vars(eap, arg, &first);
1062 }
1063 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001064 else if (!eap->skip)
1065 {
1066 // ":let"
1067 list_glob_vars(&first);
1068 list_buf_vars(&first);
1069 list_win_vars(&first);
1070 list_tab_vars(&first);
1071 list_script_vars(&first);
1072 list_func_vars(&first);
1073 list_vim_vars(&first);
1074 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02001075 set_nextcmd(eap, arg);
zeertzjq474891b2023-04-12 21:36:03 +01001076 return;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001077 }
zeertzjq474891b2023-04-12 21:36:03 +01001078
1079 if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001080 {
Bram Moolenaard9876422022-10-12 12:58:54 +01001081 list_T *l = NULL;
Bram Moolenaar81530e32021-07-28 21:25:49 +02001082 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001083
Bram Moolenaard9876422022-10-12 12:58:54 +01001084 // :let text =<< [trim] [eval] END
1085 // :var text =<< [trim] [eval] END
1086 if (vim9script && !eap->skip && (!VIM_ISWHITE(expr[-1])
1087 || !IS_WHITE_OR_NUL(expr[3])))
1088 semsg(_(e_white_space_required_before_and_after_str_at_str),
1089 "=<<", expr);
1090 else
1091 l = heredoc_get(eap, expr + 3, FALSE, FALSE);
1092
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001093 if (l != NULL)
1094 {
1095 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001096 if (!eap->skip)
1097 {
Bram Moolenaar81530e32021-07-28 21:25:49 +02001098 // errors are for the assignment, not the end marker
1099 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001100 op[0] = '=';
1101 op[1] = NUL;
1102 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001103 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001104 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001105 clear_tv(&rettv);
1106 }
zeertzjq474891b2023-04-12 21:36:03 +01001107 return;
1108 }
1109
1110 evalarg_T evalarg;
1111 int len = 1;
1112
1113 CLEAR_FIELD(rettv);
1114
1115 int cur_lnum;
1116
1117 op[0] = '=';
1118 op[1] = NUL;
1119 if (*expr != '=')
1120 {
1121 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
1122 {
1123 // +=, /=, etc. require an existing variable
1124 semsg(_(e_cannot_use_operator_on_new_variable_str), eap->arg);
1125 }
1126 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
1127 {
1128 op[0] = *expr; // +=, -=, *=, /=, %= or .=
1129 ++len;
1130 if (expr[0] == '.' && expr[1] == '.') // ..=
1131 {
1132 ++expr;
1133 ++len;
1134 }
1135 }
1136 expr += 2;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001137 }
1138 else
zeertzjq474891b2023-04-12 21:36:03 +01001139 ++expr;
1140
1141 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
1142 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001143 {
zeertzjq474891b2023-04-12 21:36:03 +01001144 vim_strncpy(op, expr - len, len);
1145 semsg(_(e_white_space_required_before_and_after_str_at_str),
1146 op, argend);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001147 }
zeertzjq474891b2023-04-12 21:36:03 +01001148
1149 if (eap->skip)
1150 ++emsg_skip;
1151 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
1152 expr = skipwhite_and_linebreak(expr, &evalarg);
1153 cur_lnum = SOURCING_LNUM;
1154 int eval_res = eval0(expr, &rettv, eap, &evalarg);
1155 if (eap->skip)
1156 --emsg_skip;
1157 clear_evalarg(&evalarg, eap);
1158
1159 // Restore the line number so that any type error is given for the
1160 // declaration, not the expression.
1161 SOURCING_LNUM = cur_lnum;
1162
1163 if (!eap->skip && eval_res != FAIL)
1164 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
1165 flags, op);
1166 if (eval_res != FAIL)
1167 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001168}
1169
1170/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001171 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001172 * Handles both "var" with any type and "[var, var; var]" with a list type.
1173 * When "op" is not NULL it points to a string with characters that
1174 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1175 * or concatenate.
1176 * Returns OK or FAIL;
1177 */
1178 int
1179ex_let_vars(
1180 char_u *arg_start,
1181 typval_T *tv,
1182 int copy, // copy values from "tv", don't move
1183 int semicolon, // from skip_var_list()
1184 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001185 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001186 char_u *op)
1187{
1188 char_u *arg = arg_start;
1189 list_T *l;
1190 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001191 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001192 listitem_T *item;
1193 typval_T ltv;
1194
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00001195 if (tv->v_type == VAR_VOID)
1196 {
1197 emsg(_(e_cannot_use_void_value));
1198 return FAIL;
1199 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001200 if (*arg != '[')
1201 {
1202 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001203 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001204 return FAIL;
1205 return OK;
1206 }
1207
1208 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1209 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
1210 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001211 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001212 return FAIL;
1213 }
1214
1215 i = list_len(l);
1216 if (semicolon == 0 && var_count < i)
1217 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001218 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001219 return FAIL;
1220 }
1221 if (var_count - semicolon > i)
1222 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001223 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001224 return FAIL;
1225 }
1226
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001227 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001228 item = l->lv_first;
1229 while (*arg != ']')
1230 {
1231 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001232 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001233 arg = ex_let_one(arg, &item->li_tv, TRUE,
1234 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001235 item = item->li_next;
1236 if (arg == NULL)
1237 return FAIL;
1238
1239 arg = skipwhite(arg);
1240 if (*arg == ';')
1241 {
1242 // Put the rest of the list (may be empty) in the var after ';'.
1243 // Create a new list for this.
1244 l = list_alloc();
1245 if (l == NULL)
1246 return FAIL;
1247 while (item != NULL)
1248 {
1249 list_append_tv(l, &item->li_tv);
1250 item = item->li_next;
1251 }
1252
1253 ltv.v_type = VAR_LIST;
1254 ltv.v_lock = 0;
1255 ltv.vval.v_list = l;
1256 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001257 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001258
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001259 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1260 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001261 clear_tv(&ltv);
1262 if (arg == NULL)
1263 return FAIL;
1264 break;
1265 }
1266 else if (*arg != ',' && *arg != ']')
1267 {
1268 internal_error("ex_let_vars()");
1269 return FAIL;
1270 }
1271 }
1272
1273 return OK;
1274}
1275
1276/*
1277 * Skip over assignable variable "var" or list of variables "[var, var]".
1278 * Used for ":let varvar = expr" and ":for varvar in expr".
1279 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001280 * for "[var, var; var]" set "semicolon" to 1.
1281 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001282 * Return NULL for an error.
1283 */
1284 char_u *
1285skip_var_list(
1286 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001288 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001289 int *semicolon,
1290 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001291{
1292 char_u *p, *s;
1293
1294 if (*arg == '[')
1295 {
1296 // "[var, var]": find the matching ']'.
1297 p = arg;
1298 for (;;)
1299 {
1300 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001301 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001302 if (s == p)
1303 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001304 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001305 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001306 return NULL;
1307 }
1308 ++*var_count;
1309
1310 p = skipwhite(s);
1311 if (*p == ']')
1312 break;
1313 else if (*p == ';')
1314 {
1315 if (*semicolon == 1)
1316 {
Bram Moolenaar8b716f52022-02-15 21:17:56 +00001317 if (!silent)
1318 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001319 return NULL;
1320 }
1321 *semicolon = 1;
1322 }
1323 else if (*p != ',')
1324 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001325 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001326 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001327 return NULL;
1328 }
1329 }
1330 return p + 1;
1331 }
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01001332
Bram Moolenaare8e369a2022-09-21 18:59:14 +01001333 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001334}
1335
1336/*
1337 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1338 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001340 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001341 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001343{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001344 char_u *end;
1345 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001347 if (*arg == '@' && arg[1] != NUL)
1348 return arg + 2;
Bram Moolenaar1573e732022-11-16 20:33:21 +00001349
1350 // termcap option name may have non-alpha characters
1351 if (STRNCMP(arg, "&t_", 3) == 0 && arg[3] != NUL && arg[4] != NUL)
1352 return arg + 5;
1353
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001355 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001356
1357 // "a: type" is declaring variable "a" with a type, not "a:".
1358 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001359 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001360 --end;
1361
Bram Moolenaar585587d2021-01-17 20:52:13 +01001362 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363 {
Bram Moolenaarce93d162023-01-30 21:12:34 +00001364 if (*skipwhite(end) == ':')
1365 end = skip_type(skipwhite(skipwhite(end) + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366 }
1367 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001368}
1369
1370/*
1371 * List variables for hashtab "ht" with prefix "prefix".
1372 * If "empty" is TRUE also list NULL strings as empty strings.
1373 */
1374 void
1375list_hashtable_vars(
1376 hashtab_T *ht,
1377 char *prefix,
1378 int empty,
1379 int *first)
1380{
1381 hashitem_T *hi;
1382 dictitem_T *di;
1383 int todo;
1384 char_u buf[IOSIZE];
1385
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001386 int save_ht_flags = ht->ht_flags;
1387 ht->ht_flags |= HTFLAGS_FROZEN;
1388
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001389 todo = (int)ht->ht_used;
1390 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1391 {
1392 if (!HASHITEM_EMPTY(hi))
1393 {
1394 --todo;
1395 di = HI2DI(hi);
1396
1397 // apply :filter /pat/ to variable name
1398 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1399 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1400 if (message_filtered(buf))
1401 continue;
1402
1403 if (empty || di->di_tv.v_type != VAR_STRING
1404 || di->di_tv.vval.v_string != NULL)
1405 list_one_var(di, prefix, first);
1406 }
1407 }
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001408
1409 ht->ht_flags = save_ht_flags;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001410}
1411
1412/*
1413 * List global variables.
1414 */
1415 static void
1416list_glob_vars(int *first)
1417{
1418 list_hashtable_vars(&globvarht, "", TRUE, first);
1419}
1420
1421/*
1422 * List buffer variables.
1423 */
1424 static void
1425list_buf_vars(int *first)
1426{
1427 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1428}
1429
1430/*
1431 * List window variables.
1432 */
1433 static void
1434list_win_vars(int *first)
1435{
1436 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1437}
1438
1439/*
1440 * List tab page variables.
1441 */
1442 static void
1443list_tab_vars(int *first)
1444{
1445 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1446}
1447
1448/*
1449 * List variables in "arg".
1450 */
1451 static char_u *
1452list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1453{
1454 int error = FALSE;
1455 int len;
1456 char_u *name;
1457 char_u *name_start;
1458 char_u *arg_subsc;
1459 char_u *tofree;
1460 typval_T tv;
1461
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001462 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001463 {
1464 if (error || eap->skip)
1465 {
1466 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1467 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1468 {
1469 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001470 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001471 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001472 break;
1473 }
1474 }
1475 else
1476 {
1477 // get_name_len() takes care of expanding curly braces
1478 name_start = name = arg;
1479 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1480 if (len <= 0)
1481 {
1482 // This is mainly to keep test 49 working: when expanding
1483 // curly braces fails overrule the exception error message.
1484 if (len < 0 && !aborting())
1485 {
1486 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001487 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001488 break;
1489 }
1490 error = TRUE;
1491 }
1492 else
1493 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001494 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001495 if (tofree != NULL)
1496 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001497 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001498 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001499 error = TRUE;
1500 else
1501 {
1502 // handle d.key, l[idx], f(expr)
1503 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001504 if (handle_subscript(&arg, name_start, &tv,
1505 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001506 error = TRUE;
1507 else
1508 {
1509 if (arg == arg_subsc && len == 2 && name[1] == ':')
1510 {
1511 switch (*name)
1512 {
1513 case 'g': list_glob_vars(first); break;
1514 case 'b': list_buf_vars(first); break;
1515 case 'w': list_win_vars(first); break;
1516 case 't': list_tab_vars(first); break;
1517 case 'v': list_vim_vars(first); break;
1518 case 's': list_script_vars(first); break;
1519 case 'l': list_func_vars(first); break;
1520 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001521 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001522 }
1523 }
1524 else
1525 {
1526 char_u numbuf[NUMBUFLEN];
1527 char_u *tf;
1528 int c;
1529 char_u *s;
1530
1531 s = echo_string(&tv, &tf, numbuf, 0);
1532 c = *arg;
1533 *arg = NUL;
1534 list_one_var_a("",
1535 arg == arg_subsc ? name : name_start,
1536 tv.v_type,
1537 s == NULL ? (char_u *)"" : s,
1538 first);
1539 *arg = c;
1540 vim_free(tf);
1541 }
1542 clear_tv(&tv);
1543 }
1544 }
1545 }
1546
1547 vim_free(tofree);
1548 }
1549
1550 arg = skipwhite(arg);
1551 }
1552
1553 return arg;
1554}
1555
1556/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001557 * Set an environment variable, part of ex_let_one().
1558 */
1559 static char_u *
1560ex_let_env(
1561 char_u *arg,
1562 typval_T *tv,
1563 int flags,
1564 char_u *endchars,
1565 char_u *op)
1566{
1567 char_u *arg_end = NULL;
1568 char_u *name;
1569 int len;
1570
1571 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1572 && (flags & ASSIGN_FOR_LOOP) == 0)
1573 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001574 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001575 return NULL;
1576 }
1577
1578 // Find the end of the name.
1579 ++arg;
1580 name = arg;
1581 len = get_env_len(&arg);
1582 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001583 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001584 else
1585 {
1586 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001587 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001588 else if (endchars != NULL
1589 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1590 emsg(_(e_unexpected_characters_in_let));
1591 else if (!check_secure())
1592 {
1593 char_u *tofree = NULL;
1594 int c1 = name[len];
1595 char_u *p;
1596
1597 name[len] = NUL;
1598 p = tv_get_string_chk(tv);
1599 if (p != NULL && op != NULL && *op == '.')
1600 {
1601 int mustfree = FALSE;
1602 char_u *s = vim_getenv(name, &mustfree);
1603
1604 if (s != NULL)
1605 {
1606 p = tofree = concat_str(s, p);
1607 if (mustfree)
1608 vim_free(s);
1609 }
1610 }
1611 if (p != NULL)
1612 {
1613 vim_setenv_ext(name, p);
1614 arg_end = arg;
1615 }
1616 name[len] = c1;
1617 vim_free(tofree);
1618 }
1619 }
1620 return arg_end;
1621}
1622
1623/*
1624 * Set an option, part of ex_let_one().
1625 */
1626 static char_u *
1627ex_let_option(
1628 char_u *arg,
1629 typval_T *tv,
1630 int flags,
1631 char_u *endchars,
1632 char_u *op)
1633{
1634 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001635 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001636 char_u *arg_end = NULL;
1637
1638 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1639 && (flags & ASSIGN_FOR_LOOP) == 0)
1640 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001641 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001642 return NULL;
1643 }
1644
1645 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001646 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001647 if (p == NULL || (endchars != NULL
1648 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001649 {
zeertzjq4c7cb372023-06-14 16:39:54 +01001650 emsg(_(e_unexpected_characters_in_let));
1651 return NULL;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001652 }
zeertzjq4c7cb372023-06-14 16:39:54 +01001653
1654 int c1;
1655 long n = 0;
1656 getoption_T opt_type;
1657 long numval;
1658 char_u *stringval = NULL;
1659 char_u *s = NULL;
1660 int failed = FALSE;
1661 int opt_p_flags;
1662 char_u *tofree = NULL;
1663 char_u numbuf[NUMBUFLEN];
1664
1665 c1 = *p;
1666 *p = NUL;
1667
1668 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags, scope);
1669 if (opt_type == gov_unknown && arg[0] != 't' && arg[1] != '_')
1670 {
1671 semsg(_(e_unknown_option_str_2), arg);
1672 goto theend;
1673 }
1674 if (op != NULL && *op != '='
1675 && (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1676 || (opt_type == gov_string && *op != '.')))
1677 {
1678 semsg(_(e_wrong_variable_type_for_str_equal), op);
1679 goto theend;
1680 }
1681
1682 if ((opt_type == gov_bool
1683 || opt_type == gov_number
1684 || opt_type == gov_hidden_bool
1685 || opt_type == gov_hidden_number)
1686 && (tv->v_type != VAR_STRING || !in_vim9script()))
1687 {
1688 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1689 // bool, possibly hidden
1690 n = (long)tv_get_bool_chk(tv, &failed);
1691 else
1692 // number, possibly hidden
1693 n = (long)tv_get_number_chk(tv, &failed);
1694 if (failed)
1695 goto theend;
1696 }
1697
1698 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
1699 || tv->v_type == VAR_FUNC))
1700 {
1701 // If the option can be set to a function reference or a lambda
1702 // and the passed value is a function reference, then convert it to
1703 // the name (string) of the function reference.
1704 s = tv2string(tv, &tofree, numbuf, 0);
1705 if (s == NULL)
1706 goto theend;
1707 }
1708 // Avoid setting a string option to the text "v:false" or similar.
1709 // In Vim9 script also don't convert a number to string.
1710 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1711 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1712 {
1713 s = tv_get_string_chk(tv);
1714 if (s == NULL)
1715 goto theend;
1716 }
1717 else if (opt_type == gov_string || opt_type == gov_hidden_string)
1718 {
1719 emsg(_(e_string_required));
1720 goto theend;
1721 }
1722
1723 if (op != NULL && *op != '=')
1724 {
1725 // number, in legacy script also bool
1726 if (opt_type == gov_number
1727 || (opt_type == gov_bool && !in_vim9script()))
1728 {
1729 switch (*op)
1730 {
1731 case '+': n = numval + n; break;
1732 case '-': n = numval - n; break;
1733 case '*': n = numval * n; break;
1734 case '/': n = (long)num_divide(numval, n, &failed); break;
1735 case '%': n = (long)num_modulus(numval, n, &failed); break;
1736 }
1737 s = NULL;
1738 if (failed)
1739 goto theend;
1740 }
1741 else if (opt_type == gov_string && stringval != NULL && s != NULL)
1742 {
1743 // string
1744 s = concat_str(stringval, s);
1745 vim_free(stringval);
1746 stringval = s;
1747 }
1748 }
1749
1750 char *err = set_option_value(arg, n, s, scope);
1751 arg_end = p;
1752 if (err != NULL)
1753 emsg(_(err));
1754
1755theend:
1756 *p = c1;
1757 vim_free(stringval);
1758 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001759 return arg_end;
1760}
1761
1762/*
1763 * Set a register, part of ex_let_one().
1764 */
1765 static char_u *
1766ex_let_register(
1767 char_u *arg,
1768 typval_T *tv,
1769 int flags,
1770 char_u *endchars,
1771 char_u *op)
1772{
1773 char_u *arg_end = NULL;
1774
1775 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1776 && (flags & ASSIGN_FOR_LOOP) == 0)
1777 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001778 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001779 return NULL;
1780 }
1781 ++arg;
1782 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001783 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001784 else if (endchars != NULL
1785 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1786 emsg(_(e_unexpected_characters_in_let));
1787 else
1788 {
1789 char_u *ptofree = NULL;
1790 char_u *p;
1791
1792 p = tv_get_string_chk(tv);
1793 if (p != NULL && op != NULL && *op == '.')
1794 {
1795 char_u *s = get_reg_contents(*arg == '@'
1796 ? '"' : *arg, GREG_EXPR_SRC);
1797
1798 if (s != NULL)
1799 {
1800 p = ptofree = concat_str(s, p);
1801 vim_free(s);
1802 }
1803 }
1804 if (p != NULL)
1805 {
1806 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1807 arg_end = arg + 1;
1808 }
1809 vim_free(ptofree);
1810 }
1811 return arg_end;
1812}
1813
1814/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001815 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1816 * Returns a pointer to the char just after the var name.
1817 * Returns NULL if there is an error.
1818 */
1819 static char_u *
1820ex_let_one(
1821 char_u *arg, // points to variable name
1822 typval_T *tv, // value to assign to variable
1823 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001824 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001825 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001826 char_u *op, // "+", "-", "." or NULL
1827 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001828{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001829 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001830
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001831 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001832 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001833 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1834 {
1835 vim9_declare_error(arg);
1836 return NULL;
1837 }
1838
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001839 if (tv->v_type == VAR_TYPEALIAS)
1840 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02001841 semsg(_(e_using_typealias_as_value), tv->vval.v_typealias->ta_name);
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001842 return NULL;
1843 }
1844
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001845 if (*arg == '$')
1846 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001847 // ":let $VAR = expr": Set environment variable.
1848 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001849 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001850 else if (*arg == '&')
1851 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001852 // ":let &option = expr": Set option value.
1853 // ":let &l:option = expr": Set local option value.
1854 // ":let &g:option = expr": Set global option value.
1855 // ":for &ts in range(8)": Set option value for for loop
1856 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001857 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001858 else if (*arg == '@')
1859 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001860 // ":let @r = expr": Set register contents.
1861 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001862 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001863 else if (eval_isnamec1(*arg) || *arg == '{')
1864 {
1865 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001866 char_u *p;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001867 int lval_flags = (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1868 ? GLV_NO_DECL : 0;
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02001869 lval_flags |= (flags & ASSIGN_FOR_LOOP) ? GLV_FOR_LOOP : 0;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001870 if (op != NULL && *op != '=')
1871 lval_flags |= GLV_ASSIGN_WITH_OP;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001872
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001873 // ":let var = expr": Set internal variable.
1874 // ":let var: type = expr": Set internal variable with type.
1875 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001876 p = get_lval(arg, tv, &lv, FALSE, FALSE, lval_flags, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001877 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001878 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001879 if (endchars != NULL && vim_strchr(endchars,
1880 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001881 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001882 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001883 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001884 else
1885 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001886 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001887 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001888 }
1889 }
1890 clear_lval(&lv);
1891 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001892 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001893 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001894
1895 return arg_end;
1896}
1897
1898/*
1899 * ":unlet[!] var1 ... " command.
1900 */
1901 void
1902ex_unlet(exarg_T *eap)
1903{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001904 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001905}
1906
1907/*
1908 * ":lockvar" and ":unlockvar" commands
1909 */
1910 void
1911ex_lockvar(exarg_T *eap)
1912{
1913 char_u *arg = eap->arg;
1914 int deep = 2;
1915
1916 if (eap->forceit)
1917 deep = -1;
1918 else if (vim_isdigit(*arg))
1919 {
1920 deep = getdigits(&arg);
1921 arg = skipwhite(arg);
1922 }
1923
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001924 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001925}
1926
1927/*
1928 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001929 * Also used for Vim9 script. "callback" is invoked as:
1930 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001931 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001932 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001933ex_unletlock(
1934 exarg_T *eap,
1935 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001936 int deep,
1937 int glv_flags,
1938 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1939 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001940{
1941 char_u *arg = argstart;
1942 char_u *name_end;
1943 int error = FALSE;
1944 lval_T lv;
1945
1946 do
1947 {
1948 if (*arg == '$')
1949 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001950 lv.ll_name = arg;
1951 lv.ll_tv = NULL;
1952 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001953 if (get_env_len(&arg) == 0)
1954 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001955 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001956 return;
1957 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001958 if (!error && !eap->skip
1959 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1960 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001961 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001962 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001963 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001964 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001965 // Parse the name and find the end.
1966 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001967 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001968 if (lv.ll_name == NULL)
1969 error = TRUE; // error but continue parsing
1970 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1971 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001972 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001973 if (name_end != NULL)
1974 {
1975 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00001976 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001977 }
1978 if (!(eap->skip || error))
1979 clear_lval(&lv);
1980 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001981 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001982
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001983 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001984 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001985 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001986
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001987 if (!eap->skip)
1988 clear_lval(&lv);
1989 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001990
1991 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001992 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001993
Bram Moolenaar63b91732021-08-05 20:40:03 +02001994 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001995}
1996
1997 static int
1998do_unlet_var(
1999 lval_T *lp,
2000 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002001 exarg_T *eap,
2002 int deep UNUSED,
2003 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002004{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002005 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002006 int ret = OK;
2007 int cc;
2008
2009 if (lp->ll_tv == NULL)
2010 {
2011 cc = *name_end;
2012 *name_end = NUL;
2013
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002014 // Environment variable, normal name or expanded name.
2015 if (*lp->ll_name == '$')
LemonBoy77142312022-04-15 20:50:46 +01002016 vim_unsetenv_ext(lp->ll_name + 1);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002017 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002018 ret = FAIL;
2019 *name_end = cc;
2020 }
2021 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002022 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002023 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002024 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002025 return FAIL;
2026 else if (lp->ll_range)
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002027 list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_n1,
2028 !lp->ll_empty2, lp->ll_n2);
2029 else if (lp->ll_list != NULL)
2030 // unlet a List item.
2031 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002032 else
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002033 // unlet a Dictionary item.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002034 dictitem_remove(lp->ll_dict, lp->ll_di, "unlet");
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002035
2036 return ret;
2037}
2038
2039/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002040 * Unlet one item or a range of items from a list.
2041 * Return OK or FAIL.
2042 */
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002043 void
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002044list_unlet_range(
2045 list_T *l,
2046 listitem_T *li_first,
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002047 long n1_arg,
2048 int has_n2,
2049 long n2)
2050{
2051 listitem_T *li = li_first;
2052 int n1 = n1_arg;
2053
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002054 // Delete a range of List items.
2055 li = li_first;
2056 n1 = n1_arg;
2057 while (li != NULL && (!has_n2 || n2 >= n1))
2058 {
2059 listitem_T *next = li->li_next;
2060
2061 listitem_remove(l, li);
2062 li = next;
2063 ++n1;
2064 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002065}
2066/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002067 * "unlet" a variable. Return OK if it existed, FAIL if not.
2068 * When "forceit" is TRUE don't complain if the variable doesn't exist.
2069 */
2070 int
2071do_unlet(char_u *name, int forceit)
2072{
2073 hashtab_T *ht;
2074 hashitem_T *hi;
mityu8be36ee2022-05-25 17:29:46 +01002075 char_u *varname = NULL; // init to shut up gcc
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002076 dict_T *d;
2077 dictitem_T *di;
2078
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002079 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002080 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002081 return FAIL;
2082
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002083 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002084
2085 // can't :unlet a script variable in Vim9 script from a function
2086 if (ht == get_script_local_ht()
2087 && SCRIPT_ID_VALID(current_sctx.sc_sid)
2088 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2089 == SCRIPT_VERSION_VIM9
2090 && check_vim9_unlet(name) == FAIL)
2091 return FAIL;
2092
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002093 if (ht != NULL && *varname != NUL)
2094 {
2095 d = get_current_funccal_dict(ht);
2096 if (d == NULL)
2097 {
2098 if (ht == &globvarht)
2099 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002100 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002101 d = &vimvardict;
2102 else
2103 {
2104 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2105 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2106 }
2107 if (d == NULL)
2108 {
2109 internal_error("do_unlet()");
2110 return FAIL;
2111 }
2112 }
2113 hi = hash_find(ht, varname);
2114 if (HASHITEM_EMPTY(hi))
2115 hi = find_hi_in_scoped_ht(name, &ht);
2116 if (hi != NULL && !HASHITEM_EMPTY(hi))
2117 {
2118 di = HI2DI(hi);
2119 if (var_check_fixed(di->di_flags, name, FALSE)
2120 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002121 || value_check_lock(d->dv_lock, name, FALSE)
2122 || check_hashtab_frozen(ht, "unlet"))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002123 return FAIL;
2124
2125 delete_var(ht, hi);
2126 return OK;
2127 }
2128 }
2129 if (forceit)
2130 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00002131 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002132 return FAIL;
2133}
2134
Ernie Raelee865f32023-09-29 19:53:55 +02002135 static void
2136report_lockvar_member(char *msg, lval_T *lp)
2137{
2138 int did_alloc = FALSE;
2139 char_u *vname = (char_u *)"";
2140 char_u *class_name = lp->ll_class != NULL
2141 ? lp->ll_class->class_name : (char_u *)"";
2142 if (lp->ll_name != NULL)
2143 {
2144 if (lp->ll_name_end == NULL)
2145 vname = lp->ll_name;
2146 else
2147 {
2148 vname = vim_strnsave(lp->ll_name, lp->ll_name_end - lp->ll_name);
2149 if (vname == NULL)
2150 return;
2151 did_alloc = TRUE;
2152 }
2153 }
2154 semsg(_(msg), vname, class_name);
2155 if (did_alloc)
2156 vim_free(vname);
2157}
2158
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002159/*
2160 * Lock or unlock variable indicated by "lp".
2161 * "deep" is the levels to go (-1 for unlimited);
2162 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2163 */
2164 static int
2165do_lock_var(
2166 lval_T *lp,
2167 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002168 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002169 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002170 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002171{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002172 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002173 int ret = OK;
2174 int cc;
2175 dictitem_T *di;
2176
Ernie Raelee865f32023-09-29 19:53:55 +02002177#ifdef LOG_LOCKVAR
2178 ch_log(NULL, "LKVAR: do_lock_var(): name %s, is_root %d", lp->ll_name, lp->ll_is_root);
2179#endif
2180
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002181 if (lp->ll_tv == NULL)
2182 {
2183 cc = *name_end;
2184 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002185 if (*lp->ll_name == '$')
2186 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002187 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002188 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002189 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002190 else
2191 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002192 // Normal name or expanded name.
2193 di = find_var(lp->ll_name, NULL, TRUE);
2194 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002195 {
2196 if (in_vim9script())
2197 semsg(_(e_cannot_find_variable_to_unlock_str),
2198 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002199 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002200 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002201 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002202 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002203 if ((di->di_flags & DI_FLAGS_FIX)
2204 && di->di_tv.v_type != VAR_DICT
2205 && di->di_tv.v_type != VAR_LIST)
2206 {
2207 // For historic reasons this error is not given for a list
2208 // or dict. E.g., the b: dict could be locked/unlocked.
2209 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
2210 ret = FAIL;
2211 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002212 else
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002213 {
2214 if (in_vim9script())
2215 {
2216 svar_T *sv = find_typval_in_script(&di->di_tv,
2217 0, FALSE);
2218
2219 if (sv != NULL && sv->sv_const != 0)
2220 {
2221 semsg(_(e_cannot_change_readonly_variable_str),
2222 lp->ll_name);
2223 ret = FAIL;
2224 }
2225 }
2226
2227 if (ret == OK)
2228 {
2229 if (lock)
2230 di->di_flags |= DI_FLAGS_LOCK;
2231 else
2232 di->di_flags &= ~DI_FLAGS_LOCK;
2233 if (deep != 0)
2234 item_lock(&di->di_tv, deep, lock, FALSE);
2235 }
2236 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002237 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002238 }
2239 *name_end = cc;
2240 }
Ernie Raelee865f32023-09-29 19:53:55 +02002241 else if (deep == 0 && lp->ll_object == NULL && lp->ll_class == NULL)
Bram Moolenaara187c432020-09-16 21:08:28 +02002242 {
2243 // nothing to do
2244 }
Ernie Raelee865f32023-09-29 19:53:55 +02002245 else if (lp->ll_is_root)
2246 // (un)lock the item.
2247 item_lock(lp->ll_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002248 else if (lp->ll_range)
2249 {
2250 listitem_T *li = lp->ll_li;
2251
2252 // (un)lock a range of List items.
2253 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2254 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02002255 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002256 li = li->li_next;
2257 ++lp->ll_n1;
2258 }
2259 }
2260 else if (lp->ll_list != NULL)
2261 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002262 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Ernie Raelee865f32023-09-29 19:53:55 +02002263 else if (lp->ll_object != NULL) // This check must be before ll_class.
2264 {
2265 // (un)lock an object variable.
2266 report_lockvar_member(e_cannot_lock_object_variable_str, lp);
2267 ret = FAIL;
2268 }
2269 else if (lp->ll_class != NULL)
2270 {
2271 // (un)lock a class variable.
2272 report_lockvar_member(e_cannot_lock_class_variable_str, lp);
2273 ret = FAIL;
2274 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002275 else
Ernie Raelee865f32023-09-29 19:53:55 +02002276 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002277 // (un)lock a Dictionary item.
Ernie Raelee865f32023-09-29 19:53:55 +02002278 if (lp->ll_di == NULL)
2279 {
2280 emsg(_(e_dictionary_required));
2281 ret = FAIL;
2282 }
2283 else
2284 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
2285 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002286
2287 return ret;
2288}
2289
2290/*
2291 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002292 * When "check_refcount" is TRUE do not lock a list or dict with a reference
2293 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002294 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002295 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02002296item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002297{
2298 static int recurse = 0;
2299 list_T *l;
2300 listitem_T *li;
2301 dict_T *d;
2302 blob_T *b;
2303 hashitem_T *hi;
2304 int todo;
2305
Ernie Raelee865f32023-09-29 19:53:55 +02002306#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02002307 ch_log(NULL, "LKVAR: item_lock(): type %s", vartype_name(tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02002308#endif
2309
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002310 if (recurse >= DICT_MAXNEST)
2311 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002312 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002313 return;
2314 }
2315 if (deep == 0)
2316 return;
2317 ++recurse;
2318
2319 // lock/unlock the item itself
2320 if (lock)
2321 tv->v_lock |= VAR_LOCKED;
2322 else
2323 tv->v_lock &= ~VAR_LOCKED;
2324
2325 switch (tv->v_type)
2326 {
2327 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002328 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002329 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002330 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002331 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002332 case VAR_STRING:
2333 case VAR_FUNC:
2334 case VAR_PARTIAL:
2335 case VAR_FLOAT:
2336 case VAR_SPECIAL:
2337 case VAR_JOB:
2338 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002339 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002340 case VAR_CLASS:
2341 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002342 case VAR_TYPEALIAS:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002343 break;
2344
2345 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002346 if ((b = tv->vval.v_blob) != NULL
2347 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002348 {
2349 if (lock)
2350 b->bv_lock |= VAR_LOCKED;
2351 else
2352 b->bv_lock &= ~VAR_LOCKED;
2353 }
2354 break;
2355 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002356 if ((l = tv->vval.v_list) != NULL
2357 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002358 {
2359 if (lock)
2360 l->lv_lock |= VAR_LOCKED;
2361 else
2362 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002363 if (deep < 0 || deep > 1)
2364 {
2365 if (l->lv_first == &range_list_item)
2366 l->lv_lock |= VAR_ITEMS_LOCKED;
2367 else
2368 {
2369 // recursive: lock/unlock the items the List contains
2370 CHECK_LIST_MATERIALIZE(l);
2371 FOR_ALL_LIST_ITEMS(l, li) item_lock(&li->li_tv,
2372 deep - 1, lock, check_refcount);
2373 }
2374 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002375 }
2376 break;
2377 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002378 if ((d = tv->vval.v_dict) != NULL
2379 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002380 {
2381 if (lock)
2382 d->dv_lock |= VAR_LOCKED;
2383 else
2384 d->dv_lock &= ~VAR_LOCKED;
2385 if (deep < 0 || deep > 1)
2386 {
2387 // recursive: lock/unlock the items the List contains
2388 todo = (int)d->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002389 FOR_ALL_HASHTAB_ITEMS(&d->dv_hashtab, hi, todo)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002390 {
2391 if (!HASHITEM_EMPTY(hi))
2392 {
2393 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002394 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2395 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002396 }
2397 }
2398 }
2399 }
2400 }
2401 --recurse;
2402}
2403
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002404#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2405/*
2406 * Delete all "menutrans_" variables.
2407 */
2408 void
2409del_menutrans_vars(void)
2410{
2411 hashitem_T *hi;
2412 int todo;
2413
2414 hash_lock(&globvarht);
2415 todo = (int)globvarht.ht_used;
2416 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2417 {
2418 if (!HASHITEM_EMPTY(hi))
2419 {
2420 --todo;
2421 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2422 delete_var(&globvarht, hi);
2423 }
2424 }
2425 hash_unlock(&globvarht);
2426}
2427#endif
2428
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002429/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002430 * Local string buffer for the next two functions to store a variable name
2431 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2432 * get_user_var_name().
2433 */
2434
2435static char_u *varnamebuf = NULL;
2436static int varnamebuflen = 0;
2437
2438/*
2439 * Function to concatenate a prefix and a variable name.
2440 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002441 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002442cat_prefix_varname(int prefix, char_u *name)
2443{
2444 int len;
2445
2446 len = (int)STRLEN(name) + 3;
2447 if (len > varnamebuflen)
2448 {
2449 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002450 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002451 varnamebuf = alloc(len);
2452 if (varnamebuf == NULL)
2453 {
2454 varnamebuflen = 0;
2455 return NULL;
2456 }
2457 varnamebuflen = len;
2458 }
2459 *varnamebuf = prefix;
2460 varnamebuf[1] = ':';
2461 STRCPY(varnamebuf + 2, name);
2462 return varnamebuf;
2463}
2464
2465/*
2466 * Function given to ExpandGeneric() to obtain the list of user defined
2467 * (global/buffer/window/built-in) variable names.
2468 */
2469 char_u *
2470get_user_var_name(expand_T *xp, int idx)
2471{
2472 static long_u gdone;
2473 static long_u bdone;
2474 static long_u wdone;
2475 static long_u tdone;
2476 static int vidx;
2477 static hashitem_T *hi;
2478 hashtab_T *ht;
2479
2480 if (idx == 0)
2481 {
2482 gdone = bdone = wdone = vidx = 0;
2483 tdone = 0;
2484 }
2485
2486 // Global variables
2487 if (gdone < globvarht.ht_used)
2488 {
2489 if (gdone++ == 0)
2490 hi = globvarht.ht_array;
2491 else
2492 ++hi;
2493 while (HASHITEM_EMPTY(hi))
2494 ++hi;
2495 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2496 return cat_prefix_varname('g', hi->hi_key);
2497 return hi->hi_key;
2498 }
2499
2500 // b: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002501 ht = &prevwin_curwin()->w_buffer->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002502 if (bdone < ht->ht_used)
2503 {
2504 if (bdone++ == 0)
2505 hi = ht->ht_array;
2506 else
2507 ++hi;
2508 while (HASHITEM_EMPTY(hi))
2509 ++hi;
2510 return cat_prefix_varname('b', hi->hi_key);
2511 }
2512
2513 // w: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002514 ht = &prevwin_curwin()->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002515 if (wdone < ht->ht_used)
2516 {
2517 if (wdone++ == 0)
2518 hi = ht->ht_array;
2519 else
2520 ++hi;
2521 while (HASHITEM_EMPTY(hi))
2522 ++hi;
2523 return cat_prefix_varname('w', hi->hi_key);
2524 }
2525
2526 // t: variables
2527 ht = &curtab->tp_vars->dv_hashtab;
2528 if (tdone < ht->ht_used)
2529 {
2530 if (tdone++ == 0)
2531 hi = ht->ht_array;
2532 else
2533 ++hi;
2534 while (HASHITEM_EMPTY(hi))
2535 ++hi;
2536 return cat_prefix_varname('t', hi->hi_key);
2537 }
2538
2539 // v: variables
2540 if (vidx < VV_LEN)
2541 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2542
2543 VIM_CLEAR(varnamebuf);
2544 varnamebuflen = 0;
2545 return NULL;
2546}
2547
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002548 char *
2549get_var_special_name(int nr)
2550{
2551 switch (nr)
2552 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002553 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2554 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002555 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002556 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002557 }
2558 internal_error("get_var_special_name()");
2559 return "42";
2560}
2561
2562/*
2563 * Returns the global variable dictionary
2564 */
2565 dict_T *
2566get_globvar_dict(void)
2567{
2568 return &globvardict;
2569}
2570
2571/*
2572 * Returns the global variable hash table
2573 */
2574 hashtab_T *
2575get_globvar_ht(void)
2576{
2577 return &globvarht;
2578}
2579
2580/*
2581 * Returns the v: variable dictionary
2582 */
2583 dict_T *
2584get_vimvar_dict(void)
2585{
2586 return &vimvardict;
2587}
2588
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002589/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002591 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592 */
2593 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002594find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002596 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2597 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598
2599 if (di == NULL)
2600 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002601 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2603 return (int)(vv - vimvars);
2604}
2605
2606
2607/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002608 * Set type of v: variable to "type".
2609 */
2610 void
2611set_vim_var_type(int idx, vartype_T type)
2612{
Bram Moolenaard787e402021-12-24 21:36:12 +00002613 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002614}
2615
2616/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002617 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002618 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002619 */
2620 void
2621set_vim_var_nr(int idx, varnumber_T val)
2622{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002623 vimvars[idx].vv_nr = val;
2624}
2625
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626 char *
2627get_vim_var_name(int idx)
2628{
2629 return vimvars[idx].vv_name;
2630}
2631
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002632/*
2633 * Get typval_T v: variable value.
2634 */
2635 typval_T *
2636get_vim_var_tv(int idx)
2637{
2638 return &vimvars[idx].vv_tv;
2639}
2640
Bram Moolenaard787e402021-12-24 21:36:12 +00002641 type_T *
2642get_vim_var_type(int idx, garray_T *type_list)
2643{
2644 if (vimvars[idx].vv_type != NULL)
2645 return vimvars[idx].vv_type;
2646 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2647}
2648
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002649/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002650 * Set v: variable to "tv". Only accepts the same type.
2651 * Takes over the value of "tv".
2652 */
2653 int
2654set_vim_var_tv(int idx, typval_T *tv)
2655{
Bram Moolenaard787e402021-12-24 21:36:12 +00002656 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002657 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002658 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002659 clear_tv(tv);
2660 return FAIL;
2661 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002662 // VV_RO is also checked when compiling, but let's check here as well.
2663 if (vimvars[idx].vv_flags & VV_RO)
2664 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002665 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002666 return FAIL;
2667 }
2668 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2669 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002670 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002671 return FAIL;
2672 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002673 clear_tv(&vimvars[idx].vv_di.di_tv);
2674 vimvars[idx].vv_di.di_tv = *tv;
2675 return OK;
2676}
2677
2678/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002679 * Get number v: variable value.
2680 */
2681 varnumber_T
2682get_vim_var_nr(int idx)
2683{
2684 return vimvars[idx].vv_nr;
2685}
2686
2687/*
2688 * Get string v: variable value. Uses a static buffer, can only be used once.
2689 * If the String variable has never been set, return an empty string.
2690 * Never returns NULL;
2691 */
2692 char_u *
2693get_vim_var_str(int idx)
2694{
2695 return tv_get_string(&vimvars[idx].vv_tv);
2696}
2697
2698/*
2699 * Get List v: variable value. Caller must take care of reference count when
2700 * needed.
2701 */
2702 list_T *
2703get_vim_var_list(int idx)
2704{
2705 return vimvars[idx].vv_list;
2706}
2707
2708/*
2709 * Get Dict v: variable value. Caller must take care of reference count when
2710 * needed.
2711 */
2712 dict_T *
2713get_vim_var_dict(int idx)
2714{
2715 return vimvars[idx].vv_dict;
2716}
2717
2718/*
2719 * Set v:char to character "c".
2720 */
2721 void
2722set_vim_var_char(int c)
2723{
2724 char_u buf[MB_MAXBYTES + 1];
2725
2726 if (has_mbyte)
2727 buf[(*mb_char2bytes)(c, buf)] = NUL;
2728 else
2729 {
2730 buf[0] = c;
2731 buf[1] = NUL;
2732 }
2733 set_vim_var_string(VV_CHAR, buf, -1);
2734}
2735
2736/*
2737 * Set v:count to "count" and v:count1 to "count1".
2738 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2739 */
2740 void
2741set_vcount(
2742 long count,
2743 long count1,
2744 int set_prevcount)
2745{
2746 if (set_prevcount)
2747 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2748 vimvars[VV_COUNT].vv_nr = count;
2749 vimvars[VV_COUNT1].vv_nr = count1;
2750}
2751
2752/*
2753 * Save variables that might be changed as a side effect. Used when executing
2754 * a timer callback.
2755 */
2756 void
2757save_vimvars(vimvars_save_T *vvsave)
2758{
2759 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2760 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2761 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2762}
2763
2764/*
2765 * Restore variables saved by save_vimvars().
2766 */
2767 void
2768restore_vimvars(vimvars_save_T *vvsave)
2769{
2770 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2771 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2772 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2773}
2774
2775/*
2776 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2777 * value.
2778 */
2779 void
2780set_vim_var_string(
2781 int idx,
2782 char_u *val,
2783 int len) // length of "val" to use or -1 (whole string)
2784{
2785 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002786 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002787 if (val == NULL)
2788 vimvars[idx].vv_str = NULL;
2789 else if (len == -1)
2790 vimvars[idx].vv_str = vim_strsave(val);
2791 else
2792 vimvars[idx].vv_str = vim_strnsave(val, len);
2793}
2794
2795/*
2796 * Set List v: variable to "val".
2797 */
2798 void
2799set_vim_var_list(int idx, list_T *val)
2800{
2801 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002802 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002803 vimvars[idx].vv_list = val;
2804 if (val != NULL)
2805 ++val->lv_refcount;
2806}
2807
2808/*
2809 * Set Dictionary v: variable to "val".
2810 */
2811 void
2812set_vim_var_dict(int idx, dict_T *val)
2813{
2814 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002815 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002816 vimvars[idx].vv_dict = val;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002817 if (val == NULL)
2818 return;
2819
2820 ++val->dv_refcount;
2821 dict_set_items_ro(val);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002822}
2823
2824/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002825 * Set the v:argv list.
2826 */
2827 void
2828set_argv_var(char **argv, int argc)
2829{
2830 list_T *l = list_alloc();
2831 int i;
2832
2833 if (l == NULL)
2834 getout(1);
2835 l->lv_lock = VAR_FIXED;
2836 for (i = 0; i < argc; ++i)
2837 {
2838 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2839 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002840 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002841 }
2842 set_vim_var_list(VV_ARGV, l);
2843}
2844
2845/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002846 * Reset v:register, taking the 'clipboard' setting into account.
2847 */
2848 void
2849reset_reg_var(void)
2850{
2851 int regname = 0;
2852
2853 // Adjust the register according to 'clipboard', so that when
2854 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2855#ifdef FEAT_CLIPBOARD
2856 adjust_clip_reg(&regname);
2857#endif
2858 set_reg_var(regname);
2859}
2860
2861/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002862 * Set v:register if needed.
2863 */
2864 void
2865set_reg_var(int c)
2866{
2867 char_u regname;
2868
2869 if (c == 0 || c == ' ')
2870 regname = '"';
2871 else
2872 regname = c;
2873 // Avoid free/alloc when the value is already right.
2874 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2875 set_vim_var_string(VV_REG, &regname, 1);
2876}
2877
2878/*
2879 * Get or set v:exception. If "oldval" == NULL, return the current value.
2880 * Otherwise, restore the value to "oldval" and return NULL.
2881 * Must always be called in pairs to save and restore v:exception! Does not
2882 * take care of memory allocations.
2883 */
2884 char_u *
2885v_exception(char_u *oldval)
2886{
2887 if (oldval == NULL)
2888 return vimvars[VV_EXCEPTION].vv_str;
2889
2890 vimvars[VV_EXCEPTION].vv_str = oldval;
2891 return NULL;
2892}
2893
2894/*
2895 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2896 * Otherwise, restore the value to "oldval" and return NULL.
2897 * Must always be called in pairs to save and restore v:throwpoint! Does not
2898 * take care of memory allocations.
2899 */
2900 char_u *
2901v_throwpoint(char_u *oldval)
2902{
2903 if (oldval == NULL)
2904 return vimvars[VV_THROWPOINT].vv_str;
2905
2906 vimvars[VV_THROWPOINT].vv_str = oldval;
2907 return NULL;
2908}
2909
2910/*
2911 * Set v:cmdarg.
2912 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2913 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2914 * Must always be called in pairs!
2915 */
2916 char_u *
2917set_cmdarg(exarg_T *eap, char_u *oldarg)
2918{
2919 char_u *oldval;
2920 char_u *newval;
2921 unsigned len;
2922
2923 oldval = vimvars[VV_CMDARG].vv_str;
2924 if (eap == NULL)
2925 {
2926 vim_free(oldval);
2927 vimvars[VV_CMDARG].vv_str = oldarg;
2928 return NULL;
2929 }
2930
2931 if (eap->force_bin == FORCE_BIN)
2932 len = 6;
2933 else if (eap->force_bin == FORCE_NOBIN)
2934 len = 8;
2935 else
2936 len = 0;
2937
2938 if (eap->read_edit)
2939 len += 7;
2940
2941 if (eap->force_ff != 0)
2942 len += 10; // " ++ff=unix"
2943 if (eap->force_enc != 0)
2944 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2945 if (eap->bad_char != 0)
2946 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2947
2948 newval = alloc(len + 1);
2949 if (newval == NULL)
2950 return NULL;
2951
2952 if (eap->force_bin == FORCE_BIN)
2953 sprintf((char *)newval, " ++bin");
2954 else if (eap->force_bin == FORCE_NOBIN)
2955 sprintf((char *)newval, " ++nobin");
2956 else
2957 *newval = NUL;
2958
2959 if (eap->read_edit)
2960 STRCAT(newval, " ++edit");
2961
2962 if (eap->force_ff != 0)
2963 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2964 eap->force_ff == 'u' ? "unix"
2965 : eap->force_ff == 'd' ? "dos"
2966 : "mac");
2967 if (eap->force_enc != 0)
2968 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2969 eap->cmd + eap->force_enc);
2970 if (eap->bad_char == BAD_KEEP)
2971 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2972 else if (eap->bad_char == BAD_DROP)
2973 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2974 else if (eap->bad_char != 0)
2975 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2976 vimvars[VV_CMDARG].vv_str = newval;
2977 return oldval;
2978}
2979
2980/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002981 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002982 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2983 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002984 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2985 */
2986 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002987eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002988 char_u *name,
Bram Moolenaar94674f22023-01-06 18:42:20 +00002989 int len, // length of "name" or zero
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002990 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002991 typval_T *rettv, // NULL when only checking existence
2992 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002993 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002994{
2995 int ret = OK;
2996 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002997 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002998 hashtab_T *ht = NULL;
Bram Moolenaar94674f22023-01-06 18:42:20 +00002999 int cc = 0;
Bram Moolenaarc967d572021-07-08 21:38:50 +02003000 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003001
Bram Moolenaar94674f22023-01-06 18:42:20 +00003002 if (len > 0)
3003 {
3004 // truncate the name, so that we can use strcmp()
3005 cc = name[len];
3006 name[len] = NUL;
3007 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003008
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003009 // Check for local variable when debugging.
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003010 if ((sid == 0) && (tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003011 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003012 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02003013 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
3014
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003015 if (v != NULL)
3016 {
3017 tv = &v->di_tv;
3018 if (dip != NULL)
3019 *dip = v;
3020 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02003021 else
3022 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003023 }
3024
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003025 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003027 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003028 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
3029
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003030 if (sid == 0)
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003031 import = find_imported(p, 0, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032
3033 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003034 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003036 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02003037 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00003038 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02003039 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003040 ht = &SCRIPT_VARS(sid);
3041 if (ht != NULL)
3042 {
3043 dictitem_T *v = find_var_in_ht(ht, 0, name,
3044 flags & EVAL_VAR_NOAUTOLOAD);
3045
3046 if (v != NULL)
3047 {
3048 tv = &v->di_tv;
3049 if (dip != NULL)
3050 *dip = v;
3051 }
3052 else
3053 ht = NULL;
3054 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003055 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003056 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003057 {
3058 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00003059 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003060 ret = FAIL;
3061 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02003062 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003063 else
3064 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003065 if (rettv != NULL)
3066 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01003067 // special value that is used in handle_subscript()
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003068 rettv->v_type = VAR_ANY;
3069 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
3070 }
3071 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02003072 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00003074 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003075 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003076 int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003077 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003078
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003079 // In Vim9 script we can get a function reference by using the
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003080 // function name. For a global non-autoload function "g:" is
3081 // required.
3082 if (ufunc != NULL && (has_g_prefix
3083 || !func_requires_g_prefix(ufunc)))
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003084 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003085 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003086 if (rettv != NULL)
3087 {
3088 rettv->v_type = VAR_FUNC;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003089 if (has_g_prefix)
Bram Moolenaaref082e12021-12-12 21:02:03 +00003090 // Keep the "g:", otherwise script-local may be
3091 // assumed.
3092 rettv->vval.v_string = vim_strsave(name);
3093 else
3094 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003095 if (rettv->vval.v_string != NULL)
3096 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003097 }
3098 }
3099 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 }
3101
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003102 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003103 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02003104 if (tv == NULL)
3105 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003106 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003107 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02003108 ret = FAIL;
3109 }
3110 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003111 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003112 svar_T *sv = NULL;
3113 int was_assigned = FALSE;
3114
Bram Moolenaar11005b02021-07-11 20:59:00 +02003115 if (ht != NULL && ht == get_script_local_ht()
3116 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02003117 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003118 sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaarf055d452021-07-08 20:57:24 +02003119 if (sv != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003120 {
Bram Moolenaarf055d452021-07-08 20:57:24 +02003121 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003122 was_assigned = sv->sv_flags & SVFLAG_ASSIGNED;
3123 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003124 }
3125
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003126 if ((tv->v_type == VAR_TYPEALIAS || tv->v_type == VAR_CLASS)
3127 && sid != 0)
3128 {
3129 // type alias or class imported from another script. Check
3130 // whether it is exported from the other script.
3131 sv = find_typval_in_script(tv, sid, TRUE);
3132 if (sv == NULL)
3133 {
3134 ret = FAIL;
3135 goto done;
3136 }
3137 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
3138 {
3139 semsg(_(e_item_not_exported_in_script_str), name);
3140 ret = FAIL;
3141 goto done;
3142 }
3143 }
3144
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003145 // If a list or dict variable wasn't initialized and has meaningful
3146 // type, do it now. Not for global variables, they are not
3147 // declared.
Bram Moolenaar7a222242022-03-01 19:23:24 +00003148 if (ht != &globvarht)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003149 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003150 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003151 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003152 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003153 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003154 tv->vval.v_dict = dict_alloc();
3155 if (tv->vval.v_dict != NULL)
3156 {
3157 ++tv->vval.v_dict->dv_refcount;
3158 tv->vval.v_dict->dv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003159 if (sv != NULL)
3160 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003161 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003162 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003163 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003164 && ((type != NULL && !was_assigned)
Bram Moolenaar501f9782022-03-27 16:51:04 +01003165 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003166 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003167 tv->vval.v_list = list_alloc();
3168 if (tv->vval.v_list != NULL)
3169 {
3170 ++tv->vval.v_list->lv_refcount;
3171 tv->vval.v_list->lv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003172 if (sv != NULL)
3173 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003174 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003175 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003176 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003177 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003178 || !in_vim9script()))
3179 {
3180 tv->vval.v_blob = blob_alloc();
3181 if (tv->vval.v_blob != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003182 {
Bram Moolenaar859cc212022-03-28 15:22:35 +01003183 ++tv->vval.v_blob->bv_refcount;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003184 if (sv != NULL)
3185 sv->sv_flags |= SVFLAG_ASSIGNED;
3186 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003187 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02003188 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003189 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003190 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003191 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003192
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003193done:
Bram Moolenaar94674f22023-01-06 18:42:20 +00003194 if (len > 0)
3195 name[len] = cc;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003196
3197 return ret;
3198}
3199
3200/*
Bram Moolenaara86655a2023-01-12 17:06:27 +00003201 * Get the value of internal variable "name", also handling "import.name".
3202 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
3203 */
3204 int
3205eval_variable_import(
3206 char_u *name,
3207 typval_T *rettv)
3208{
3209 char_u *s = name;
3210 while (ASCII_ISALNUM(*s) || *s == '_')
3211 ++s;
3212 int len = (int)(s - name);
3213
3214 if (eval_variable(name, len, 0, rettv, NULL, EVAL_VAR_IMPORT) == FAIL)
3215 return FAIL;
3216 if (rettv->v_type == VAR_ANY && *s == '.')
3217 {
Bram Moolenaar40594002023-01-12 20:04:51 +00003218 char_u *ns = s + 1;
3219 s = ns;
3220 while (ASCII_ISALNUM(*s) || *s == '_')
3221 ++s;
Bram Moolenaara86655a2023-01-12 17:06:27 +00003222 int sid = rettv->vval.v_number;
Bram Moolenaar40594002023-01-12 20:04:51 +00003223 return eval_variable(ns, (int)(s - ns), sid, rettv, NULL, 0);
Bram Moolenaara86655a2023-01-12 17:06:27 +00003224 }
3225 return OK;
3226}
3227
3228
3229/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003230 * Check if variable "name[len]" is a local variable or an argument.
3231 * If so, "*eval_lavars_used" is set to TRUE.
3232 */
3233 void
3234check_vars(char_u *name, int len)
3235{
3236 int cc;
3237 char_u *varname;
3238 hashtab_T *ht;
3239
3240 if (eval_lavars_used == NULL)
3241 return;
3242
3243 // truncate the name, so that we can use strcmp()
3244 cc = name[len];
3245 name[len] = NUL;
3246
3247 ht = find_var_ht(name, &varname);
3248 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
3249 {
3250 if (find_var(name, NULL, TRUE) != NULL)
3251 *eval_lavars_used = TRUE;
3252 }
3253
3254 name[len] = cc;
3255}
3256
3257/*
3258 * Find variable "name" in the list of variables.
3259 * Return a pointer to it if found, NULL if not found.
3260 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003261 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003262 */
3263 dictitem_T *
3264find_var(char_u *name, hashtab_T **htp, int no_autoload)
3265{
3266 char_u *varname;
3267 hashtab_T *ht;
3268 dictitem_T *ret = NULL;
3269
3270 ht = find_var_ht(name, &varname);
3271 if (htp != NULL)
3272 *htp = ht;
3273 if (ht == NULL)
3274 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003275 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003276 if (ret != NULL)
3277 return ret;
3278
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003279 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003280 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003281 if (ret != NULL)
3282 return ret;
3283
3284 // in Vim9 script items without a scope can be script-local
3285 if (in_vim9script() && name[0] != NUL && name[1] != ':')
3286 {
3287 ht = get_script_local_ht();
3288 if (ht != NULL)
3289 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003290 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003291 if (ret != NULL)
3292 {
3293 if (htp != NULL)
3294 *htp = ht;
3295 return ret;
3296 }
3297 }
3298 }
3299
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003300 // When using "vim9script autoload" script-local items are prefixed but can
3301 // be used with s:name.
3302 if (SCRIPT_ID_VALID(current_sctx.sc_sid)
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003303 && (in_vim9script() || (name[0] == 's' && name[1] == ':')))
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003304 {
3305 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
3306
3307 if (si->sn_autoload_prefix != NULL)
3308 {
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003309 char_u *base_name = (name[0] == 's' && name[1] == ':')
3310 ? name + 2 : name;
3311 char_u *auto_name = concat_str(si->sn_autoload_prefix, base_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003312
3313 if (auto_name != NULL)
3314 {
3315 ht = &globvarht;
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003316 ret = find_var_in_ht(ht, 'g', auto_name, TRUE);
Bram Moolenaar130f65d2022-01-13 20:39:41 +00003317 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003318 if (ret != NULL)
3319 {
3320 if (htp != NULL)
3321 *htp = ht;
3322 return ret;
3323 }
3324 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003325 }
3326 }
3327
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003328 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003329}
3330
3331/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00003332 * Like find_var() but if the name starts with <SNR>99_ then look in the
3333 * referenced script (used for a funcref).
3334 */
3335 dictitem_T *
3336find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
3337{
3338 if (STRNCMP(name, "<SNR>", 5) == 0 && isdigit(name[5]))
3339 {
3340 char_u *p = name + 5;
3341 int sid = getdigits(&p);
3342
3343 if (SCRIPT_ID_VALID(sid) && *p == '_')
3344 {
3345 hashtab_T *ht = &SCRIPT_VARS(sid);
3346
3347 if (ht != NULL)
3348 {
3349 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
3350
3351 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003352 {
3353 if (htp != NULL)
3354 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00003355 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003356 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00003357 }
3358 }
3359 }
3360
3361 return find_var(name, htp, no_autoload);
3362}
3363
3364/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003365 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02003366 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003367 * Returns NULL if not found.
3368 */
3369 dictitem_T *
3370find_var_in_ht(
3371 hashtab_T *ht,
3372 int htname,
3373 char_u *varname,
3374 int no_autoload)
3375{
3376 hashitem_T *hi;
3377
3378 if (*varname == NUL)
3379 {
3380 // Must be something like "s:", otherwise "ht" would be NULL.
3381 switch (htname)
3382 {
3383 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
3384 case 'g': return &globvars_var;
3385 case 'v': return &vimvars_var;
3386 case 'b': return &curbuf->b_bufvar;
3387 case 'w': return &curwin->w_winvar;
3388 case 't': return &curtab->tp_winvar;
3389 case 'l': return get_funccal_local_var();
3390 case 'a': return get_funccal_args_var();
3391 }
3392 return NULL;
3393 }
3394
3395 hi = hash_find(ht, varname);
3396 if (HASHITEM_EMPTY(hi))
3397 {
3398 // For global variables we may try auto-loading the script. If it
3399 // worked find the variable again. Don't auto-load a script if it was
3400 // loaded already, otherwise it would be loaded every time when
3401 // checking if a function name is a Funcref variable.
3402 if (ht == &globvarht && !no_autoload)
3403 {
3404 // Note: script_autoload() may make "hi" invalid. It must either
3405 // be obtained again or not used.
3406 if (!script_autoload(varname, FALSE) || aborting())
3407 return NULL;
3408 hi = hash_find(ht, varname);
3409 }
3410 if (HASHITEM_EMPTY(hi))
3411 return NULL;
3412 }
3413 return HI2DI(hi);
3414}
3415
3416/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 * Get the script-local hashtab. NULL if not in a script context.
3418 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003419 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420get_script_local_ht(void)
3421{
3422 scid_T sid = current_sctx.sc_sid;
3423
Bram Moolenaare3d46852020-08-29 13:39:17 +02003424 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 return &SCRIPT_VARS(sid);
3426 return NULL;
3427}
3428
3429/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003430 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003431 * When "cmd" is TRUE it must look like a command, a function must be followed
3432 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003433 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003435 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003436lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003437 char_u *name,
3438 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003439 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003440 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441{
3442 hashtab_T *ht = get_script_local_ht();
3443 char_u buffer[30];
3444 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003445 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003447 int is_global = FALSE;
3448 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449
3450 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003451 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 if (len < sizeof(buffer) - 1)
3453 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003454 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003455 vim_strncpy(buffer, name, len);
3456 p = buffer;
3457 }
3458 else
3459 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003460 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003462 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 }
3464
3465 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003466 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467
3468 // if not script-local, then perhaps imported
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003469 if (res == FAIL && find_imported(p, 0, FALSE) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003470 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003471 if (p != buffer)
3472 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003473
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003474 // Find a function, so that a following "->" works.
3475 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3476 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003477 if (res != OK)
3478 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003479 p = skipwhite(name + len);
3480
3481 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003482 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003483 // Do not check for an internal function, since it might also be a
3484 // valid command, such as ":split" versus "split()".
3485 // Skip "g:" before a function name.
3486 if (name[0] == 'g' && name[1] == ':')
3487 {
3488 is_global = TRUE;
3489 fname = name + 2;
3490 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003491 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003492 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003493 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003494 }
3495
Bram Moolenaar709664c2020-12-12 14:33:41 +01003496 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497}
3498
3499/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003500 * Find the hashtab used for a variable name.
3501 * Return NULL if the name is not valid.
3502 * Set "varname" to the start of name without ':'.
3503 */
3504 hashtab_T *
3505find_var_ht(char_u *name, char_u **varname)
3506{
3507 hashitem_T *hi;
3508 hashtab_T *ht;
3509
3510 if (name[0] == NUL)
3511 return NULL;
3512 if (name[1] != ':')
3513 {
3514 // The name must not start with a colon or #.
3515 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3516 return NULL;
3517 *varname = name;
3518
3519 // "version" is "v:version" in all scopes if scriptversion < 3.
3520 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003521 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003522 {
3523 hi = hash_find(&compat_hashtab, name);
3524 if (!HASHITEM_EMPTY(hi))
3525 return &compat_hashtab;
3526 }
3527
3528 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529 if (ht != NULL)
3530 return ht; // local variable
3531
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003532 // In Vim9 script items at the script level are script-local, except
3533 // for autoload names.
3534 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 {
3536 ht = get_script_local_ht();
3537 if (ht != NULL)
3538 return ht;
3539 }
3540
3541 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003542 }
3543 *varname = name + 2;
3544 if (*name == 'g') // global variable
3545 return &globvarht;
3546 // There must be no ':' or '#' in the rest of the name, unless g: is used
3547 if (vim_strchr(name + 2, ':') != NULL
3548 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3549 return NULL;
3550 if (*name == 'b') // buffer variable
3551 return &curbuf->b_vars->dv_hashtab;
3552 if (*name == 'w') // window variable
3553 return &curwin->w_vars->dv_hashtab;
3554 if (*name == 't') // tab page variable
3555 return &curtab->tp_vars->dv_hashtab;
3556 if (*name == 'v') // v: variable
3557 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003558 if (get_current_funccal() != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01003559 && get_current_funccal()->fc_func->uf_def_status
3560 == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003562 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003563 if (*name == 'a') // a: function argument
3564 return get_funccal_args_ht();
3565 if (*name == 'l') // l: local function variable
3566 return get_funccal_local_ht();
3567 }
3568 if (*name == 's') // script variable
3569 {
3570 ht = get_script_local_ht();
3571 if (ht != NULL)
3572 return ht;
3573 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003574 return NULL;
3575}
3576
3577/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003578 * Get the string value of a (global/local) variable.
3579 * Note: see tv_get_string() for how long the pointer remains valid.
3580 * Returns NULL when it doesn't exist.
3581 */
3582 char_u *
3583get_var_value(char_u *name)
3584{
3585 dictitem_T *v;
3586
3587 v = find_var(name, NULL, FALSE);
3588 if (v == NULL)
3589 return NULL;
3590 return tv_get_string(&v->di_tv);
3591}
3592
3593/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003594 * Allocate a new hashtab for a sourced script. It will be used while
3595 * sourcing this script and when executing functions defined in the script.
3596 */
3597 void
3598new_script_vars(scid_T id)
3599{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003600 scriptvar_T *sv;
3601
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003602 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3603 if (sv == NULL)
3604 return;
3605 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003606 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003607}
3608
3609/*
3610 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3611 * point to it.
3612 */
3613 void
3614init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3615{
3616 hash_init(&dict->dv_hashtab);
3617 dict->dv_lock = 0;
3618 dict->dv_scope = scope;
3619 dict->dv_refcount = DO_NOT_FREE_CNT;
3620 dict->dv_copyID = 0;
3621 dict_var->di_tv.vval.v_dict = dict;
3622 dict_var->di_tv.v_type = VAR_DICT;
3623 dict_var->di_tv.v_lock = VAR_FIXED;
3624 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3625 dict_var->di_key[0] = NUL;
3626}
3627
3628/*
3629 * Unreference a dictionary initialized by init_var_dict().
3630 */
3631 void
3632unref_var_dict(dict_T *dict)
3633{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003634 // Now the dict needs to be freed if no one else is using it, go back to
3635 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003636 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3637 dict_unref(dict);
3638}
3639
3640/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003641 * Clean up a list of internal variables.
3642 * Frees all allocated variables and the value they contain.
3643 * Clears hashtab "ht", does not free it.
3644 */
3645 void
3646vars_clear(hashtab_T *ht)
3647{
3648 vars_clear_ext(ht, TRUE);
3649}
3650
3651/*
3652 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3653 */
3654 void
3655vars_clear_ext(hashtab_T *ht, int free_val)
3656{
3657 int todo;
3658 hashitem_T *hi;
3659 dictitem_T *v;
3660
3661 hash_lock(ht);
3662 todo = (int)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003663 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003664 {
3665 if (!HASHITEM_EMPTY(hi))
3666 {
3667 --todo;
3668
3669 // Free the variable. Don't remove it from the hashtab,
3670 // ht_array might change then. hash_clear() takes care of it
3671 // later.
3672 v = HI2DI(hi);
3673 if (free_val)
3674 clear_tv(&v->di_tv);
3675 if (v->di_flags & DI_FLAGS_ALLOC)
3676 vim_free(v);
3677 }
3678 }
3679 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003680 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003681}
3682
3683/*
3684 * Delete a variable from hashtab "ht" at item "hi".
3685 * Clear the variable value and free the dictitem.
3686 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003687 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003688delete_var(hashtab_T *ht, hashitem_T *hi)
3689{
3690 dictitem_T *di = HI2DI(hi);
3691
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003692 if (hash_remove(ht, hi, "delete variable") != OK)
3693 return;
3694
3695 clear_tv(&di->di_tv);
3696 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003697}
3698
3699/*
3700 * List the value of one internal variable.
3701 */
3702 static void
3703list_one_var(dictitem_T *v, char *prefix, int *first)
3704{
3705 char_u *tofree;
3706 char_u *s;
3707 char_u numbuf[NUMBUFLEN];
3708
3709 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3710 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3711 s == NULL ? (char_u *)"" : s, first);
3712 vim_free(tofree);
3713}
3714
3715 static void
3716list_one_var_a(
3717 char *prefix,
3718 char_u *name,
3719 int type,
3720 char_u *string,
3721 int *first) // when TRUE clear rest of screen and set to FALSE
3722{
3723 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3724 msg_start();
3725 msg_puts(prefix);
3726 if (name != NULL) // "a:" vars don't have a name stored
3727 msg_puts((char *)name);
3728 msg_putchar(' ');
3729 msg_advance(22);
3730 if (type == VAR_NUMBER)
3731 msg_putchar('#');
3732 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3733 msg_putchar('*');
3734 else if (type == VAR_LIST)
3735 {
3736 msg_putchar('[');
3737 if (*string == '[')
3738 ++string;
3739 }
3740 else if (type == VAR_DICT)
3741 {
3742 msg_putchar('{');
3743 if (*string == '{')
3744 ++string;
3745 }
3746 else
3747 msg_putchar(' ');
3748
3749 msg_outtrans(string);
3750
3751 if (type == VAR_FUNC || type == VAR_PARTIAL)
3752 msg_puts("()");
3753 if (*first)
3754 {
3755 msg_clr_eos();
3756 *first = FALSE;
3757 }
3758}
3759
3760/*
zeertzjqedcba962023-09-24 23:13:51 +02003761 * Addition handling for setting a v: variable.
3762 * Return TRUE if the variable should be set normally,
3763 * FALSE if nothing else needs to be done.
3764 */
3765 int
3766before_set_vvar(
3767 char_u *varname,
3768 dictitem_T *di,
3769 typval_T *tv,
3770 int copy,
3771 int *type_error)
3772{
3773 if (di->di_tv.v_type == VAR_STRING)
3774 {
3775 VIM_CLEAR(di->di_tv.vval.v_string);
3776 if (copy || tv->v_type != VAR_STRING)
3777 {
3778 char_u *val = tv_get_string(tv);
3779
3780 // Careful: when assigning to v:errmsg and
3781 // tv_get_string() causes an error message the variable
3782 // will already be set.
3783 if (di->di_tv.vval.v_string == NULL)
3784 di->di_tv.vval.v_string = vim_strsave(val);
3785 }
3786 else
3787 {
3788 // Take over the string to avoid an extra alloc/free.
3789 di->di_tv.vval.v_string = tv->vval.v_string;
3790 tv->vval.v_string = NULL;
3791 }
3792 return FALSE;
3793 }
3794 else if (di->di_tv.v_type == VAR_NUMBER)
3795 {
3796 di->di_tv.vval.v_number = tv_get_number(tv);
3797 if (STRCMP(varname, "searchforward") == 0)
3798 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
3799#ifdef FEAT_SEARCH_EXTRA
3800 else if (STRCMP(varname, "hlsearch") == 0)
3801 {
3802 no_hlsearch = !di->di_tv.vval.v_number;
3803 redraw_all_later(UPD_SOME_VALID);
3804 }
3805#endif
3806 return FALSE;
3807 }
3808 else if (di->di_tv.v_type != tv->v_type)
3809 {
3810 *type_error = TRUE;
3811 return FALSE;
3812 }
3813 return TRUE;
3814}
3815
3816/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003817 * Set variable "name" to value in "tv".
3818 * If the variable already exists, the value is updated.
3819 * Otherwise the variable is created.
3820 */
3821 void
3822set_var(
3823 char_u *name,
3824 typval_T *tv,
3825 int copy) // make copy of value in "tv"
3826{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003827 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003828}
3829
3830/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003831 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003832 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003833 * If the variable already exists and "is_const" is FALSE the value is updated.
3834 * Otherwise the variable is created.
3835 */
3836 void
3837set_var_const(
3838 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003839 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003840 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003841 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003842 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003843 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003844 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003845{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003846 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003847 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003848 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 dictitem_T *di;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003850 typval_T *dest_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003851 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003852 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003853 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003855 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003856 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003857 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003858 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003859 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003860
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003861 if (sid != 0)
3862 {
3863 if (SCRIPT_ID_VALID(sid))
3864 ht = &SCRIPT_VARS(sid);
3865 varname = name;
3866 }
3867 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003868 {
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003869 scriptitem_T *si;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003870
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003871 if (in_vim9script() && is_export
3872 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3873 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
Bram Moolenaar0e1574c2022-03-03 17:05:35 +00003874 ->sn_autoload_prefix != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003875 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003876 // In a vim9 autoload script an exported variable is put in the
3877 // global namespace with the autoload prefix.
3878 var_in_autoload = TRUE;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003879 varname = concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003880 if (varname == NULL)
3881 goto failed;
3882 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003883 ht = &globvarht;
3884 }
3885 else
3886 ht = find_var_ht(name, &varname);
3887 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003888 if (ht == NULL || *varname == NUL)
3889 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003890 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003891 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003892 }
Bram Moolenaar54969f42022-02-07 13:56:44 +00003893 is_script_local = ht == get_script_local_ht() || sid != 0
3894 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003895
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003896 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003897 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003898 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003899 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003900 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003901 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003902 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003903 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003904 }
Bram Moolenaar9510d222022-09-11 15:14:05 +01003905 if ((flags & ASSIGN_FOR_LOOP) && is_scoped_variable(name))
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003906 // Do not make g:var, w:var, b:var or t:var final.
3907 flags &= ~ASSIGN_FINAL;
3908
Bram Moolenaare535db82021-03-31 21:07:24 +02003909 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003910 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3911 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003912 // For "[a, _] = list" the underscore is ignored.
3913 if ((flags & ASSIGN_UNPACK) == 0)
3914 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003915 goto failed;
3916 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003917
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003919
Bram Moolenaar24e93162021-07-18 20:40:33 +02003920 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003921 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003922 imported_T *import = find_imported(varname, 0, FALSE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003923
Bram Moolenaar24e93162021-07-18 20:40:33 +02003924 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003925 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003926 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02003927 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003929 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003930 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003932 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3933 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003934 }
Bram Moolenaar75e27d72022-02-13 13:56:29 +00003935 if (!in_vim9script())
3936 {
3937 semsg(_(e_cannot_create_vim9_script_variable_in_function_str),
3938 name);
3939 goto failed;
3940 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003941 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942
Bram Moolenaar993faa32022-02-21 15:59:11 +00003943 // Search in parent scope which is possible to reference from lambda
3944 if (di == NULL)
3945 di = find_var_in_scoped_ht(name, TRUE);
3946
3947 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
3948 && var_wrong_func_name(name, di == NULL))
3949 goto failed;
3950
3951 if (need_convert_to_bool(type, tv))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003952 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003953 // Destination is a bool and the value is not, but it can be
3954 // converted.
3955 CLEAR_FIELD(bool_tv);
3956 bool_tv.v_type = VAR_BOOL;
3957 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3958 tv = &bool_tv;
3959 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003960
Bram Moolenaar993faa32022-02-21 15:59:11 +00003961 if (di != NULL)
3962 {
3963 // Item already exists. Allowed to replace when reloading.
3964 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003965 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003966 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
3967 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003968 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003969 emsg(_(e_cannot_modify_existing_variable));
3970 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003971 }
3972
Bram Moolenaar993faa32022-02-21 15:59:11 +00003973 if (is_script_local && vim9script
3974 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003975 {
3976 semsg(_(e_redefining_script_item_str), name);
3977 goto failed;
3978 }
3979
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003980 if (di->di_tv.v_type == VAR_TYPEALIAS)
3981 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02003982 semsg(_(e_cannot_modify_typealias),
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003983 di->di_tv.vval.v_typealias->ta_name);
3984 clear_tv(&di->di_tv);
3985 goto failed;
3986 }
3987
Bram Moolenaar993faa32022-02-21 15:59:11 +00003988 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003989 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003990 where_T where = WHERE_INIT;
Bram Moolenaar7a411a32022-04-04 14:58:06 +01003991 svar_T *sv = find_typval_in_script(&di->di_tv, sid, TRUE);
Bram Moolenaar993faa32022-02-21 15:59:11 +00003992
3993 if (sv != NULL)
3994 {
3995 // check the type and adjust to bool if needed
LemonBoyc5d27442023-08-19 13:02:35 +02003996 if (var_idx > 0)
3997 {
3998 where.wt_index = var_idx;
3999 where.wt_kind = WT_VARIABLE;
4000 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00004001 if (check_script_var_type(sv, tv, name, where) == FAIL)
4002 goto failed;
4003 if (type == NULL)
4004 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01004005 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004006 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02004007 }
4008
Bram Moolenaar993faa32022-02-21 15:59:11 +00004009 if ((flags & ASSIGN_FOR_LOOP) == 0
Bram Moolenaar16d2c022023-06-05 19:46:18 +01004010 ? var_check_permission(di, name) == FAIL
4011 : var_check_ro(di->di_flags, name, FALSE))
Bram Moolenaar24e93162021-07-18 20:40:33 +02004012 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004013 }
4014 else
4015 {
4016 // can only redefine once
4017 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar24e93162021-07-18 20:40:33 +02004018
Bram Moolenaar993faa32022-02-21 15:59:11 +00004019 // A Vim9 script-local variable is also present in sn_all_vars
4020 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004021 if (var_in_vim9script || var_in_autoload)
Bram Moolenaar993faa32022-02-21 15:59:11 +00004022 update_vim9_script_var(FALSE, di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004023 var_in_autoload ? name : di->di_key, flags,
Bram Moolenaar993faa32022-02-21 15:59:11 +00004024 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01004025 }
4026
Bram Moolenaar993faa32022-02-21 15:59:11 +00004027 // existing variable, need to clear the value
4028
zeertzjqedcba962023-09-24 23:13:51 +02004029 // Handle setting internal v: variables separately where needed to
Bram Moolenaar993faa32022-02-21 15:59:11 +00004030 // prevent changing the type.
zeertzjqedcba962023-09-24 23:13:51 +02004031 int type_error = FALSE;
4032 if (ht == &vimvarht
4033 && !before_set_vvar(varname, di, tv, copy, &type_error))
Bram Moolenaar993faa32022-02-21 15:59:11 +00004034 {
zeertzjqedcba962023-09-24 23:13:51 +02004035 if (type_error)
4036 semsg(_(e_setting_v_str_to_value_with_wrong_type), varname);
4037 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004038 }
4039
4040 clear_tv(&di->di_tv);
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01004041
4042 if ((flags & ASSIGN_UPDATE_BLOCK_ID)
4043 && SCRIPT_ID_VALID(current_sctx.sc_sid))
4044 {
4045 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
4046
4047 update_script_var_block_id(name, si->sn_current_block_id);
4048 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00004049 }
4050 else
4051 {
4052 // Item not found, check if a function already exists.
4053 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
4054 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
4055 {
4056 semsg(_(e_redefining_script_item_str), name);
4057 goto failed;
4058 }
4059
4060 // add a new variable
4061 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
4062 {
4063 semsg(_(e_unknown_variable_str), name);
4064 goto failed;
4065 }
4066
Bram Moolenaaref2c3252022-11-25 16:31:51 +00004067 if (check_hashtab_frozen(ht, "add variable"))
4068 goto failed;
4069
Bram Moolenaar993faa32022-02-21 15:59:11 +00004070 // Can't add "v:" or "a:" variable.
4071 if (ht == &vimvarht || ht == get_funccal_args_ht())
4072 {
4073 semsg(_(e_illegal_variable_name_str), name);
4074 goto failed;
4075 }
4076
4077 // Make sure the variable name is valid. In Vim9 script an
4078 // autoload variable must be prefixed with "g:" unless in an
4079 // autoload script.
4080 if (!valid_varname(varname, -1, !vim9script
4081 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
4082 goto failed;
4083
zeertzjq1b438a82023-02-01 13:11:15 +00004084 di = alloc(offsetof(dictitem_T, di_key) + STRLEN(varname) + 1);
Bram Moolenaar993faa32022-02-21 15:59:11 +00004085 if (di == NULL)
4086 goto failed;
4087 STRCPY(di->di_key, varname);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00004088 if (hash_add(ht, DI2HIKEY(di), "add variable") == FAIL)
Bram Moolenaar993faa32022-02-21 15:59:11 +00004089 {
4090 vim_free(di);
4091 goto failed;
4092 }
4093 di->di_flags = DI_FLAGS_ALLOC;
4094 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
4095 di->di_flags |= DI_FLAGS_LOCK;
4096
4097 // A Vim9 script-local variable is also added to sn_all_vars and
4098 // sn_var_vals. It may set "type" from "tv".
4099 if (var_in_vim9script || var_in_autoload)
4100 update_vim9_script_var(TRUE, di,
4101 var_in_autoload ? name : di->di_key, flags,
4102 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004103 }
4104
Bram Moolenaar993faa32022-02-21 15:59:11 +00004105 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004106 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02004107 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004108 else
4109 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02004110 *dest_tv = *tv;
4111 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004112 init_tv(tv);
4113 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004114 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004115
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004116 if (vim9script && type != NULL)
Bram Moolenaar381692b2022-02-02 20:01:27 +00004117 set_tv_type(dest_tv, type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004118
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01004119 // ":const var = value" locks the value
4120 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004121 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02004122 // Like :lockvar! name: lock the value and what it contains, but only
4123 // if the reference count is up to one. That locks only literal
4124 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02004125 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02004126
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004127failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004128 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004129 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004130 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004131}
4132
4133/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004134 * Check in this order for backwards compatibility:
4135 * - Whether the variable is read-only
4136 * - Whether the variable value is locked
4137 * - Whether the variable is locked
4138 */
4139 int
4140var_check_permission(dictitem_T *di, char_u *name)
4141{
4142 if (var_check_ro(di->di_flags, name, FALSE)
4143 || value_check_lock(di->di_tv.v_lock, name, FALSE)
4144 || var_check_lock(di->di_flags, name, FALSE))
4145 return FAIL;
4146 return OK;
4147}
4148
4149/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004150 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
4151 * Also give an error message.
4152 */
4153 int
4154var_check_ro(int flags, char_u *name, int use_gettext)
4155{
4156 if (flags & DI_FLAGS_RO)
4157 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004158 if (name == NULL)
4159 emsg(_(e_cannot_change_readonly_variable));
4160 else
4161 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02004162 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004163 return TRUE;
4164 }
4165 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
4166 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004167 if (name == NULL)
4168 emsg(_(e_cannot_set_variable_in_sandbox));
4169 else
4170 semsg(_(e_cannot_set_variable_in_sandbox_str),
4171 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004172 return TRUE;
4173 }
4174 return FALSE;
4175}
4176
4177/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004178 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
4179 * Also give an error message.
4180 */
4181 int
4182var_check_lock(int flags, char_u *name, int use_gettext)
4183{
4184 if (flags & DI_FLAGS_LOCK)
4185 {
4186 semsg(_(e_variable_is_locked_str),
4187 use_gettext ? (char_u *)_(name) : name);
4188 return TRUE;
4189 }
4190 return FALSE;
4191}
4192
4193/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004194 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
4195 * Also give an error message.
4196 */
4197 int
4198var_check_fixed(int flags, char_u *name, int use_gettext)
4199{
4200 if (flags & DI_FLAGS_FIX)
4201 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004202 if (name == NULL)
4203 emsg(_(e_cannot_delete_variable));
4204 else
4205 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004206 use_gettext ? (char_u *)_(name) : name);
4207 return TRUE;
4208 }
4209 return FALSE;
4210}
4211
4212/*
4213 * Check if a funcref is assigned to a valid variable name.
4214 * Return TRUE and give an error if not.
4215 */
4216 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004217var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004218 char_u *name, // points to start of variable name
4219 int new_var) // TRUE when creating the variable
4220{
Bram Moolenaar32154662021-03-28 21:14:06 +02004221 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
4222 // the name can be used without the s: prefix.
thinca6c667bd2022-09-02 11:25:37 +01004223 // Allow autoload variable.
Bram Moolenaar32154662021-03-28 21:14:06 +02004224 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
4225 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004226 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
thinca6c667bd2022-09-02 11:25:37 +01004227 ? name[2] : name[0])
4228 && vim_strchr(name, '#') == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004229 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004230 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004231 return TRUE;
4232 }
4233 // Don't allow hiding a function. When "v" is not NULL we might be
4234 // assigning another function to the same var, the type is checked
4235 // below.
4236 if (new_var && function_exists(name, FALSE))
4237 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004238 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004239 name);
4240 return TRUE;
4241 }
4242 return FALSE;
4243}
4244
4245/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004246 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
4247 * value. Also give an error message, using "name" or _("name") when
4248 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004249 */
4250 int
Bram Moolenaara187c432020-09-16 21:08:28 +02004251value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004252{
4253 if (lock & VAR_LOCKED)
4254 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004255 if (name == NULL)
4256 emsg(_(e_value_is_locked));
4257 else
4258 semsg(_(e_value_is_locked_str),
4259 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004260 return TRUE;
4261 }
4262 if (lock & VAR_FIXED)
4263 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004264 if (name == NULL)
4265 emsg(_(e_cannot_change_value));
4266 else
4267 semsg(_(e_cannot_change_value_of_str),
4268 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004269 return TRUE;
4270 }
4271 return FALSE;
4272}
4273
4274/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01004275 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004276 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004277 * Return FALSE and give an error if not.
4278 */
4279 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004280valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004281{
4282 char_u *p;
4283
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004284 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004285 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01004286 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004287 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004288 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004289 return FALSE;
4290 }
4291 return TRUE;
4292}
4293
4294/*
LemonBoy47d4e312022-05-04 18:12:55 +01004295 * Implements the logic to retrieve local variable and option values.
4296 * Used by "getwinvar()" "gettabvar()" "gettabwinvar()" "getbufvar()".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004297 */
4298 static void
LemonBoy47d4e312022-05-04 18:12:55 +01004299get_var_from(
4300 char_u *varname,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004301 typval_T *rettv,
LemonBoy47d4e312022-05-04 18:12:55 +01004302 typval_T *deftv, // Default value if not found.
4303 int htname, // 't'ab, 'w'indow or 'b'uffer local.
4304 tabpage_T *tp, // can be NULL
4305 win_T *win,
4306 buf_T *buf) // Ignored if htname is not 'b'.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004307{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004308 dictitem_T *v;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004309 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004310 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004311 int need_switch_win;
zeertzjqcd6ad642022-07-25 12:28:09 +01004312 int do_change_curbuf = buf != NULL && htname == 'b';
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004313
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004314 ++emsg_off;
4315
4316 rettv->v_type = VAR_STRING;
4317 rettv->vval.v_string = NULL;
4318
LemonBoy47d4e312022-05-04 18:12:55 +01004319 if (varname != NULL && tp != NULL && win != NULL
4320 && (htname != 'b' || buf != NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004321 {
4322 // Set curwin to be our win, temporarily. Also set the tabpage,
4323 // otherwise the window is not valid. Only do this when needed,
4324 // autocommands get blocked.
LemonBoy47d4e312022-05-04 18:12:55 +01004325 // If we have a buffer reference avoid the switching, we're saving and
4326 // restoring curbuf directly.
zeertzjqcd6ad642022-07-25 12:28:09 +01004327 need_switch_win = !(tp == curtab && win == curwin) && !do_change_curbuf;
LemonBoy47d4e312022-05-04 18:12:55 +01004328 if (!need_switch_win || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004329 {
LemonBoy47d4e312022-05-04 18:12:55 +01004330 // Handle options. There are no tab-local options.
4331 if (*varname == '&' && htname != 't')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004332 {
LemonBoy47d4e312022-05-04 18:12:55 +01004333 buf_T *save_curbuf = curbuf;
4334
4335 // Change curbuf so the option is read from the correct buffer.
zeertzjqcd6ad642022-07-25 12:28:09 +01004336 if (do_change_curbuf)
LemonBoy47d4e312022-05-04 18:12:55 +01004337 curbuf = buf;
4338
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004339 if (varname[1] == NUL)
4340 {
zeertzjqcd6ad642022-07-25 12:28:09 +01004341 // get all window-local or buffer-local options in a dict
LemonBoy47d4e312022-05-04 18:12:55 +01004342 dict_T *opts = get_winbuf_options(htname == 'b');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004343
4344 if (opts != NULL)
4345 {
4346 rettv_dict_set(rettv, opts);
4347 done = TRUE;
4348 }
4349 }
LemonBoy47d4e312022-05-04 18:12:55 +01004350 else if (eval_option(&varname, rettv, TRUE) == OK)
4351 // Local option
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004352 done = TRUE;
LemonBoy47d4e312022-05-04 18:12:55 +01004353
4354 curbuf = save_curbuf;
4355 }
4356 else if (*varname == NUL)
4357 {
4358 // Empty string: return a dict with all the local variables.
4359 if (htname == 'b')
4360 v = &buf->b_bufvar;
4361 else if (htname == 'w')
4362 v = &win->w_winvar;
4363 else
4364 v = &tp->tp_winvar;
4365 copy_tv(&v->di_tv, rettv);
4366 done = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004367 }
4368 else
4369 {
LemonBoy47d4e312022-05-04 18:12:55 +01004370 hashtab_T *ht;
4371
4372 if (htname == 'b')
4373 ht = &buf->b_vars->dv_hashtab;
4374 else if (htname == 'w')
4375 ht = &win->w_vars->dv_hashtab;
4376 else
4377 ht = &tp->tp_vars->dv_hashtab;
4378
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004379 // Look up the variable.
LemonBoy47d4e312022-05-04 18:12:55 +01004380 v = find_var_in_ht(ht, htname, varname, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004381 if (v != NULL)
4382 {
4383 copy_tv(&v->di_tv, rettv);
4384 done = TRUE;
4385 }
4386 }
4387 }
4388
4389 if (need_switch_win)
4390 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004391 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004392 }
4393
LemonBoy47d4e312022-05-04 18:12:55 +01004394 if (!done && deftv->v_type != VAR_UNKNOWN)
4395 // use the default value
4396 copy_tv(deftv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004397
4398 --emsg_off;
4399}
4400
4401/*
LemonBoy47d4e312022-05-04 18:12:55 +01004402 * getwinvar() and gettabwinvar()
4403 */
4404 static void
4405getwinvar(
4406 typval_T *argvars,
4407 typval_T *rettv,
4408 int off) // 1 for gettabwinvar()
4409{
4410 char_u *varname;
4411 tabpage_T *tp;
4412 win_T *win;
4413
4414 if (off == 1)
4415 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4416 else
4417 tp = curtab;
4418 win = find_win_by_nr(&argvars[off], tp);
4419 varname = tv_get_string_chk(&argvars[off + 1]);
4420
4421 get_var_from(varname, rettv, &argvars[off + 2], 'w', tp, win, NULL);
4422}
4423
4424/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02004425 * Set option "varname" to the value of "varp" for the current buffer/window.
4426 */
4427 static void
4428set_option_from_tv(char_u *varname, typval_T *varp)
4429{
4430 long numval = 0;
4431 char_u *strval;
4432 char_u nbuf[NUMBUFLEN];
4433 int error = FALSE;
4434
zeertzjq4c7cb372023-06-14 16:39:54 +01004435 int opt_idx = findoption(varname);
4436 if (opt_idx < 0)
4437 {
4438 semsg(_(e_unknown_option_str_2), varname);
4439 return;
4440 }
4441 int opt_p_flags = get_option_flags(opt_idx);
4442
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004443 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004444 {
zeertzjq4c7cb372023-06-14 16:39:54 +01004445 if (opt_p_flags & P_STRING)
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004446 {
4447 emsg(_(e_string_required));
4448 return;
4449 }
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004450 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004451 strval = (char_u *)"0"; // avoid using "false"
4452 }
4453 else
4454 {
zeertzjq4c7cb372023-06-14 16:39:54 +01004455 if ((opt_p_flags & (P_NUM|P_BOOL))
4456 && (!in_vim9script() || varp->v_type != VAR_STRING))
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004457 numval = (long)tv_get_number_chk(varp, &error);
zeertzjq4c7cb372023-06-14 16:39:54 +01004458 if (!error)
4459 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004460 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02004461 if (!error && strval != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004462 set_option_value_give_err(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar191929b2020-08-19 21:20:49 +02004463}
4464
4465/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004466 * "setwinvar()" and "settabwinvar()" functions
4467 */
4468 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004469setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004470{
4471 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004472 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004473 int need_switch_win;
4474 char_u *varname, *winvarname;
4475 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004476 tabpage_T *tp = NULL;
4477
4478 if (check_secure())
4479 return;
4480
4481 if (off == 1)
4482 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4483 else
4484 tp = curtab;
4485 win = find_win_by_nr(&argvars[off], tp);
4486 varname = tv_get_string_chk(&argvars[off + 1]);
4487 varp = &argvars[off + 2];
4488
zeertzjqea720ae2023-01-03 10:54:09 +00004489 if (win == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004490 return;
4491
4492 need_switch_win = !(tp == curtab && win == curwin);
4493 if (!need_switch_win
4494 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004495 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004496 if (*varname == '&')
4497 set_option_from_tv(varname + 1, varp);
4498 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004499 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004500 winvarname = alloc(STRLEN(varname) + 3);
4501 if (winvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004502 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004503 STRCPY(winvarname, "w:");
4504 STRCPY(winvarname + 2, varname);
4505 set_var(winvarname, varp, TRUE);
4506 vim_free(winvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004507 }
4508 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004509 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004510 if (need_switch_win)
4511 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004512}
4513
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004514/*
4515 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4516 * v:option_type, and v:option_command.
4517 */
4518 void
4519reset_v_option_vars(void)
4520{
4521 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4522 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4523 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4524 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4525 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4526 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4527}
4528
4529/*
4530 * Add an assert error to v:errors.
4531 */
4532 void
4533assert_error(garray_T *gap)
4534{
4535 struct vimvar *vp = &vimvars[VV_ERRORS];
4536
Bram Moolenaard787e402021-12-24 21:36:12 +00004537 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004538 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004539 set_vim_var_list(VV_ERRORS, list_alloc());
4540 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4541}
4542
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004543 int
4544var_exists(char_u *var)
4545{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004546 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004547 char_u *name;
4548 char_u *tofree;
4549 typval_T tv;
4550 int len = 0;
4551 int n = FALSE;
4552
4553 // get_name_len() takes care of expanding curly braces
4554 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004555 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004556 if (len > 0)
4557 {
4558 if (tofree != NULL)
4559 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004560 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004561 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004562 if (n)
4563 {
4564 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004565 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004566 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4567 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004568 if (n)
4569 clear_tv(&tv);
4570 }
4571 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004572 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004573 n = FALSE;
4574
4575 vim_free(tofree);
4576 return n;
4577}
4578
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004579static lval_T *redir_lval = NULL;
4580#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4581static garray_T redir_ga; // only valid when redir_lval is not NULL
4582static char_u *redir_endp = NULL;
4583static char_u *redir_varname = NULL;
4584
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004585 int
4586alloc_redir_lval(void)
4587{
4588 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4589 if (redir_lval == NULL)
4590 return FAIL;
4591 return OK;
4592}
4593
4594 void
4595clear_redir_lval(void)
4596{
4597 VIM_CLEAR(redir_lval);
4598}
4599
4600 void
4601init_redir_ga(void)
4602{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004603 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004604}
4605
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004606/*
4607 * Start recording command output to a variable
4608 * When "append" is TRUE append to an existing variable.
4609 * Returns OK if successfully completed the setup. FAIL otherwise.
4610 */
4611 int
4612var_redir_start(char_u *name, int append)
4613{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004614 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004615 typval_T tv;
4616
4617 // Catch a bad name early.
4618 if (!eval_isnamec1(*name))
4619 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004620 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004621 return FAIL;
4622 }
4623
4624 // Make a copy of the name, it is used in redir_lval until redir ends.
4625 redir_varname = vim_strsave(name);
4626 if (redir_varname == NULL)
4627 return FAIL;
4628
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004629 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004630 {
4631 var_redir_stop();
4632 return FAIL;
4633 }
4634
4635 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004636 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004637
4638 // Parse the variable name (can be a dict or list entry).
4639 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4640 FNE_CHECK_START);
4641 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4642 {
4643 clear_lval(redir_lval);
4644 if (redir_endp != NULL && *redir_endp != NUL)
4645 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004646 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004647 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004648 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004649 redir_endp = NULL; // don't store a value, only cleanup
4650 var_redir_stop();
4651 return FAIL;
4652 }
4653
4654 // check if we can write to the variable: set it to or append an empty
4655 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004656 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004657 tv.v_type = VAR_STRING;
4658 tv.vval.v_string = (char_u *)"";
4659 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004660 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004661 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004662 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004663 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004664 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004665 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004666 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004667 {
4668 redir_endp = NULL; // don't store a value, only cleanup
4669 var_redir_stop();
4670 return FAIL;
4671 }
4672
4673 return OK;
4674}
4675
4676/*
4677 * Append "value[value_len]" to the variable set by var_redir_start().
4678 * The actual appending is postponed until redirection ends, because the value
4679 * appended may in fact be the string we write to, changing it may cause freed
4680 * memory to be used:
4681 * :redir => foo
4682 * :let foo
4683 * :redir END
4684 */
4685 void
4686var_redir_str(char_u *value, int value_len)
4687{
4688 int len;
4689
4690 if (redir_lval == NULL)
4691 return;
4692
4693 if (value_len == -1)
4694 len = (int)STRLEN(value); // Append the entire string
4695 else
4696 len = value_len; // Append only "value_len" characters
4697
4698 if (ga_grow(&redir_ga, len) == OK)
4699 {
4700 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4701 redir_ga.ga_len += len;
4702 }
4703 else
4704 var_redir_stop();
4705}
4706
4707/*
4708 * Stop redirecting command output to a variable.
4709 * Frees the allocated memory.
4710 */
4711 void
4712var_redir_stop(void)
4713{
4714 typval_T tv;
4715
4716 if (EVALCMD_BUSY)
4717 {
4718 redir_lval = NULL;
4719 return;
4720 }
4721
4722 if (redir_lval != NULL)
4723 {
4724 // If there was no error: assign the text to the variable.
4725 if (redir_endp != NULL)
4726 {
4727 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4728 tv.v_type = VAR_STRING;
4729 tv.vval.v_string = redir_ga.ga_data;
4730 // Call get_lval() again, if it's inside a Dict or List it may
4731 // have changed.
4732 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4733 FALSE, FALSE, 0, FNE_CHECK_START);
4734 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004736 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004737 clear_lval(redir_lval);
4738 }
4739
4740 // free the collected output
4741 VIM_CLEAR(redir_ga.ga_data);
4742
4743 VIM_CLEAR(redir_lval);
4744 }
4745 VIM_CLEAR(redir_varname);
4746}
4747
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004748/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004749 * Get the collected redirected text and clear redir_ga.
4750 */
4751 char_u *
4752get_clear_redir_ga(void)
4753{
4754 char_u *res;
4755
4756 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4757 res = redir_ga.ga_data;
4758 redir_ga.ga_data = NULL;
4759 return res;
4760}
4761
4762/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004763 * "gettabvar()" function
4764 */
4765 void
4766f_gettabvar(typval_T *argvars, typval_T *rettv)
4767{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004768 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004769 tabpage_T *tp;
4770 win_T *win = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004771
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004772 if (in_vim9script()
4773 && (check_for_number_arg(argvars, 0) == FAIL
4774 || check_for_string_arg(argvars, 1) == FAIL))
4775 return;
4776
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004777 varname = tv_get_string_chk(&argvars[1]);
4778 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
LemonBoy47d4e312022-05-04 18:12:55 +01004779 if (tp != NULL)
4780 win = tp == curtab || tp->tp_firstwin == NULL ? firstwin
4781 : tp->tp_firstwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004782
LemonBoy47d4e312022-05-04 18:12:55 +01004783 get_var_from(varname, rettv, &argvars[2], 't', tp, win, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004784}
4785
4786/*
4787 * "gettabwinvar()" function
4788 */
4789 void
4790f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4791{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004792 if (in_vim9script()
4793 && (check_for_number_arg(argvars, 0) == FAIL
4794 || check_for_number_arg(argvars, 1) == FAIL
4795 || check_for_string_arg(argvars, 2) == FAIL))
4796 return;
4797
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004798 getwinvar(argvars, rettv, 1);
4799}
4800
4801/*
4802 * "getwinvar()" function
4803 */
4804 void
4805f_getwinvar(typval_T *argvars, typval_T *rettv)
4806{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004807 if (in_vim9script()
4808 && (check_for_number_arg(argvars, 0) == FAIL
4809 || check_for_string_arg(argvars, 1) == FAIL))
4810 return;
4811
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004812 getwinvar(argvars, rettv, 0);
4813}
4814
4815/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004816 * "getbufvar()" function
4817 */
4818 void
4819f_getbufvar(typval_T *argvars, typval_T *rettv)
4820{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004821 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004822 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004823
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004824 if (in_vim9script()
4825 && (check_for_buffer_arg(argvars, 0) == FAIL
4826 || check_for_string_arg(argvars, 1) == FAIL))
4827 return;
4828
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004829 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004830 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004831
LemonBoy47d4e312022-05-04 18:12:55 +01004832 get_var_from(varname, rettv, &argvars[2], 'b', curtab, curwin, buf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004833}
4834
4835/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004836 * "settabvar()" function
4837 */
4838 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004839f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004840{
4841 tabpage_T *save_curtab;
4842 tabpage_T *tp;
4843 char_u *varname, *tabvarname;
4844 typval_T *varp;
4845
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004846 if (check_secure())
4847 return;
4848
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004849 if (in_vim9script()
4850 && (check_for_number_arg(argvars, 0) == FAIL
4851 || check_for_string_arg(argvars, 1) == FAIL))
4852 return;
4853
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004854 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4855 varname = tv_get_string_chk(&argvars[1]);
4856 varp = &argvars[2];
4857
zeertzjqea720ae2023-01-03 10:54:09 +00004858 if (varname == NULL || tp == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004859 return;
4860
4861 save_curtab = curtab;
4862 goto_tabpage_tp(tp, FALSE, FALSE);
4863
4864 tabvarname = alloc(STRLEN(varname) + 3);
4865 if (tabvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004866 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004867 STRCPY(tabvarname, "t:");
4868 STRCPY(tabvarname + 2, varname);
4869 set_var(tabvarname, varp, TRUE);
4870 vim_free(tabvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004871 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004872
4873 // Restore current tabpage
4874 if (valid_tabpage(save_curtab))
4875 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004876}
4877
4878/*
4879 * "settabwinvar()" function
4880 */
4881 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004882f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004883{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004884 if (in_vim9script()
4885 && (check_for_number_arg(argvars, 0) == FAIL
4886 || check_for_number_arg(argvars, 1) == FAIL
4887 || check_for_string_arg(argvars, 2) == FAIL))
4888 return;
4889
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004890 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004891}
4892
4893/*
4894 * "setwinvar()" function
4895 */
4896 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004897f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004898{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004899 if (in_vim9script()
4900 && (check_for_number_arg(argvars, 0) == FAIL
4901 || check_for_string_arg(argvars, 1) == FAIL))
4902 return;
4903
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004904 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004905}
4906
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004907/*
4908 * "setbufvar()" function
4909 */
4910 void
4911f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4912{
4913 buf_T *buf;
4914 char_u *varname, *bufvarname;
4915 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004916
4917 if (check_secure())
4918 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004919
4920 if (in_vim9script()
4921 && (check_for_buffer_arg(argvars, 0) == FAIL
4922 || check_for_string_arg(argvars, 1) == FAIL))
4923 return;
4924
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004925 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004926 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004927 varp = &argvars[2];
4928
zeertzjqea720ae2023-01-03 10:54:09 +00004929 if (buf == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004930 return;
4931
4932 if (*varname == '&')
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004933 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004934 aco_save_T aco;
4935
4936 // Set curbuf to be our buf, temporarily.
4937 aucmd_prepbuf(&aco, buf);
4938 if (curbuf == buf)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004939 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004940 // Only when it worked to set "curbuf".
4941 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004942
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004943 // reset notion of buffer
4944 aucmd_restbuf(&aco);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004945 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004946 }
4947 else
4948 {
4949 bufvarname = alloc(STRLEN(varname) + 3);
4950 if (bufvarname != NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004951 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004952 buf_T *save_curbuf = curbuf;
Bram Moolenaar86015452020-03-29 15:12:15 +02004953
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004954 curbuf = buf;
4955 STRCPY(bufvarname, "b:");
4956 STRCPY(bufvarname + 2, varname);
4957 set_var(bufvarname, varp, TRUE);
4958 vim_free(bufvarname);
4959 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004960 }
4961 }
4962}
4963
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004964/*
4965 * Get a callback from "arg". It can be a Funcref or a function name.
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004966 * When "arg" is zero "res.cb_name" is set to an empty string.
4967 * If "res.cb_name" is allocated then "res.cb_free_name" is set to TRUE.
4968 * "res.cb_name" is set to NULL for an invalid argument.
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004969 */
4970 callback_T
4971get_callback(typval_T *arg)
4972{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004973 callback_T res;
4974 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004975
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004976 CLEAR_FIELD(res);
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004977 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4978 {
4979 res.cb_partial = arg->vval.v_partial;
4980 ++res.cb_partial->pt_refcount;
4981 res.cb_name = partial_name(res.cb_partial);
4982 }
4983 else
4984 {
Bram Moolenaar14e579092020-03-07 16:59:25 +01004985 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4986 && isdigit(*arg->vval.v_string))
4987 r = FAIL;
4988 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004989 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004990 res.cb_name = arg->vval.v_string;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004991 if (arg->v_type == VAR_STRING)
4992 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004993 char_u *name = get_scriptlocal_funcname(arg->vval.v_string);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004994 if (name != NULL)
4995 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004996 res.cb_name = name;
4997 res.cb_free_name = TRUE;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004998 }
4999 }
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005000 func_ref(res.cb_name);
5001 }
5002 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005003 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005004 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01005005 r = FAIL;
5006
5007 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005008 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005009 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005010 res.cb_name = NULL;
5011 }
5012 }
5013 return res;
5014}
5015
5016/*
5017 * Copy a callback into a typval_T.
5018 */
5019 void
5020put_callback(callback_T *cb, typval_T *tv)
5021{
5022 if (cb->cb_partial != NULL)
5023 {
5024 tv->v_type = VAR_PARTIAL;
5025 tv->vval.v_partial = cb->cb_partial;
5026 ++tv->vval.v_partial->pt_refcount;
5027 }
5028 else
5029 {
5030 tv->v_type = VAR_FUNC;
5031 tv->vval.v_string = vim_strsave(cb->cb_name);
5032 func_ref(cb->cb_name);
5033 }
5034}
5035
5036/*
5037 * Make a copy of "src" into "dest", allocating the function name if needed,
5038 * without incrementing the refcount.
5039 */
5040 void
5041set_callback(callback_T *dest, callback_T *src)
5042{
5043 if (src->cb_partial == NULL)
5044 {
5045 // just a function name, make a copy
5046 dest->cb_name = vim_strsave(src->cb_name);
5047 dest->cb_free_name = TRUE;
5048 }
5049 else
5050 {
5051 // cb_name is a pointer into cb_partial
5052 dest->cb_name = src->cb_name;
5053 dest->cb_free_name = FALSE;
5054 }
5055 dest->cb_partial = src->cb_partial;
5056}
5057
5058/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02005059 * Copy callback from "src" to "dest", incrementing the refcounts.
5060 */
5061 void
5062copy_callback(callback_T *dest, callback_T *src)
5063{
5064 dest->cb_partial = src->cb_partial;
5065 if (dest->cb_partial != NULL)
5066 {
5067 dest->cb_name = src->cb_name;
5068 dest->cb_free_name = FALSE;
5069 ++dest->cb_partial->pt_refcount;
5070 }
5071 else
5072 {
5073 dest->cb_name = vim_strsave(src->cb_name);
5074 dest->cb_free_name = TRUE;
5075 func_ref(src->cb_name);
5076 }
5077}
5078
5079/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005080 * When a callback refers to an autoload import, change the function name to
5081 * the "path#name" form. Uses the current script context.
5082 * Only works when the name is allocated.
5083 */
5084 void
5085expand_autload_callback(callback_T *cb)
5086{
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005087 char_u *name;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005088 char_u *p;
5089 imported_T *import;
5090
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005091 if (!in_vim9script() || cb->cb_name == NULL
5092 || (!cb->cb_free_name
5093 && (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005094 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005095 if (cb->cb_partial != NULL)
5096 name = cb->cb_partial->pt_name;
5097 else
5098 name = cb->cb_name;
5099 p = vim_strchr(name, '.');
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005100 if (p == NULL)
5101 return;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005102
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005103 import = find_imported(name, p - name, FALSE);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005104 if (import == NULL || !SCRIPT_ID_VALID(import->imp_sid))
5105 return;
5106
5107 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
5108 if (si->sn_autoload_prefix == NULL)
5109 return;
5110
5111 char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
5112 if (newname == NULL)
5113 return;
5114
5115 if (cb->cb_partial != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005116 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005117 if (cb->cb_name == cb->cb_partial->pt_name)
5118 cb->cb_name = newname;
5119 vim_free(cb->cb_partial->pt_name);
5120 cb->cb_partial->pt_name = newname;
5121 }
5122 else
5123 {
5124 vim_free(cb->cb_name);
5125 cb->cb_name = newname;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005126 }
5127}
5128
5129/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005130 * Unref/free "callback" returned by get_callback() or set_callback().
5131 */
5132 void
5133free_callback(callback_T *callback)
5134{
5135 if (callback->cb_partial != NULL)
5136 {
5137 partial_unref(callback->cb_partial);
5138 callback->cb_partial = NULL;
5139 }
5140 else if (callback->cb_name != NULL)
5141 func_unref(callback->cb_name);
5142 if (callback->cb_free_name)
5143 {
5144 vim_free(callback->cb_name);
5145 callback->cb_free_name = FALSE;
5146 }
5147 callback->cb_name = NULL;
5148}
5149
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005150#endif // FEAT_EVAL