blob: cbcf2a707ece39462f99642f1ef12d6ed20abc72 [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
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100779 if (eap->ea_getline == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200780 {
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);
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100859 theline = eap->ea_getline(NUL, eap->cookie, 0, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200860 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
Ernie Rael9ed53752023-12-11 17:40:46 +01001839 if (check_typval_is_value(tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001840 return NULL;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001841
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001842 if (*arg == '$')
1843 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001844 // ":let $VAR = expr": Set environment variable.
1845 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001846 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001847 else if (*arg == '&')
1848 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001849 // ":let &option = expr": Set option value.
1850 // ":let &l:option = expr": Set local option value.
1851 // ":let &g:option = expr": Set global option value.
1852 // ":for &ts in range(8)": Set option value for for loop
1853 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001854 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001855 else if (*arg == '@')
1856 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001857 // ":let @r = expr": Set register contents.
1858 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001859 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001860 else if (eval_isnamec1(*arg) || *arg == '{')
1861 {
1862 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001863 char_u *p;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001864 int lval_flags = (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1865 ? GLV_NO_DECL : 0;
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02001866 lval_flags |= (flags & ASSIGN_FOR_LOOP) ? GLV_FOR_LOOP : 0;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001867 if (op != NULL && *op != '=')
1868 lval_flags |= GLV_ASSIGN_WITH_OP;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001869
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001870 // ":let var = expr": Set internal variable.
1871 // ":let var: type = expr": Set internal variable with type.
1872 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001873 p = get_lval(arg, tv, &lv, FALSE, FALSE, lval_flags, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001874 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001875 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001876 if (endchars != NULL && vim_strchr(endchars,
1877 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001878 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001879 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001880 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001881 else
1882 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001883 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001884 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001885 }
1886 }
1887 clear_lval(&lv);
1888 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001889 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001890 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001891
1892 return arg_end;
1893}
1894
1895/*
1896 * ":unlet[!] var1 ... " command.
1897 */
1898 void
1899ex_unlet(exarg_T *eap)
1900{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001901 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001902}
1903
1904/*
1905 * ":lockvar" and ":unlockvar" commands
1906 */
1907 void
1908ex_lockvar(exarg_T *eap)
1909{
1910 char_u *arg = eap->arg;
1911 int deep = 2;
1912
1913 if (eap->forceit)
1914 deep = -1;
1915 else if (vim_isdigit(*arg))
1916 {
1917 deep = getdigits(&arg);
1918 arg = skipwhite(arg);
1919 }
1920
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001921 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001922}
1923
1924/*
1925 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001926 * Also used for Vim9 script. "callback" is invoked as:
1927 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001928 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001929 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001930ex_unletlock(
1931 exarg_T *eap,
1932 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001933 int deep,
1934 int glv_flags,
1935 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1936 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001937{
1938 char_u *arg = argstart;
1939 char_u *name_end;
1940 int error = FALSE;
1941 lval_T lv;
1942
1943 do
1944 {
1945 if (*arg == '$')
1946 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001947 lv.ll_name = arg;
1948 lv.ll_tv = NULL;
1949 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001950 if (get_env_len(&arg) == 0)
1951 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001952 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001953 return;
1954 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001955 if (!error && !eap->skip
1956 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1957 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001958 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001959 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001960 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001961 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001962 // Parse the name and find the end.
1963 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001964 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001965 if (lv.ll_name == NULL)
1966 error = TRUE; // error but continue parsing
1967 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1968 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001969 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001970 if (name_end != NULL)
1971 {
1972 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00001973 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001974 }
1975 if (!(eap->skip || error))
1976 clear_lval(&lv);
1977 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001978 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001979
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001980 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001981 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001982 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001983
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001984 if (!eap->skip)
1985 clear_lval(&lv);
1986 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001987
1988 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001989 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001990
Bram Moolenaar63b91732021-08-05 20:40:03 +02001991 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001992}
1993
1994 static int
1995do_unlet_var(
1996 lval_T *lp,
1997 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001998 exarg_T *eap,
1999 int deep UNUSED,
2000 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002001{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002002 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002003 int ret = OK;
2004 int cc;
2005
2006 if (lp->ll_tv == NULL)
2007 {
2008 cc = *name_end;
2009 *name_end = NUL;
2010
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002011 // Environment variable, normal name or expanded name.
2012 if (*lp->ll_name == '$')
LemonBoy77142312022-04-15 20:50:46 +01002013 vim_unsetenv_ext(lp->ll_name + 1);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002014 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002015 ret = FAIL;
2016 *name_end = cc;
2017 }
2018 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002019 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002020 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002021 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002022 return FAIL;
2023 else if (lp->ll_range)
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002024 list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_n1,
2025 !lp->ll_empty2, lp->ll_n2);
2026 else if (lp->ll_list != NULL)
2027 // unlet a List item.
2028 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002029 else
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002030 // unlet a Dictionary item.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002031 dictitem_remove(lp->ll_dict, lp->ll_di, "unlet");
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002032
2033 return ret;
2034}
2035
2036/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002037 * Unlet one item or a range of items from a list.
2038 * Return OK or FAIL.
2039 */
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002040 void
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002041list_unlet_range(
2042 list_T *l,
2043 listitem_T *li_first,
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002044 long n1_arg,
2045 int has_n2,
2046 long n2)
2047{
2048 listitem_T *li = li_first;
2049 int n1 = n1_arg;
2050
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002051 // Delete a range of List items.
2052 li = li_first;
2053 n1 = n1_arg;
2054 while (li != NULL && (!has_n2 || n2 >= n1))
2055 {
2056 listitem_T *next = li->li_next;
2057
2058 listitem_remove(l, li);
2059 li = next;
2060 ++n1;
2061 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002062}
2063/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002064 * "unlet" a variable. Return OK if it existed, FAIL if not.
2065 * When "forceit" is TRUE don't complain if the variable doesn't exist.
2066 */
2067 int
2068do_unlet(char_u *name, int forceit)
2069{
2070 hashtab_T *ht;
2071 hashitem_T *hi;
mityu8be36ee2022-05-25 17:29:46 +01002072 char_u *varname = NULL; // init to shut up gcc
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002073 dict_T *d;
2074 dictitem_T *di;
2075
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002076 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002077 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002078 return FAIL;
2079
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002080 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002081
2082 // can't :unlet a script variable in Vim9 script from a function
2083 if (ht == get_script_local_ht()
2084 && SCRIPT_ID_VALID(current_sctx.sc_sid)
2085 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2086 == SCRIPT_VERSION_VIM9
2087 && check_vim9_unlet(name) == FAIL)
2088 return FAIL;
2089
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002090 if (ht != NULL && *varname != NUL)
2091 {
2092 d = get_current_funccal_dict(ht);
2093 if (d == NULL)
2094 {
2095 if (ht == &globvarht)
2096 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002097 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002098 d = &vimvardict;
2099 else
2100 {
2101 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2102 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2103 }
2104 if (d == NULL)
2105 {
2106 internal_error("do_unlet()");
2107 return FAIL;
2108 }
2109 }
2110 hi = hash_find(ht, varname);
2111 if (HASHITEM_EMPTY(hi))
2112 hi = find_hi_in_scoped_ht(name, &ht);
2113 if (hi != NULL && !HASHITEM_EMPTY(hi))
2114 {
2115 di = HI2DI(hi);
2116 if (var_check_fixed(di->di_flags, name, FALSE)
2117 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002118 || value_check_lock(d->dv_lock, name, FALSE)
2119 || check_hashtab_frozen(ht, "unlet"))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002120 return FAIL;
2121
2122 delete_var(ht, hi);
2123 return OK;
2124 }
2125 }
2126 if (forceit)
2127 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00002128 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002129 return FAIL;
2130}
2131
Ernie Raelee865f32023-09-29 19:53:55 +02002132 static void
2133report_lockvar_member(char *msg, lval_T *lp)
2134{
2135 int did_alloc = FALSE;
2136 char_u *vname = (char_u *)"";
2137 char_u *class_name = lp->ll_class != NULL
2138 ? lp->ll_class->class_name : (char_u *)"";
2139 if (lp->ll_name != NULL)
2140 {
2141 if (lp->ll_name_end == NULL)
2142 vname = lp->ll_name;
2143 else
2144 {
2145 vname = vim_strnsave(lp->ll_name, lp->ll_name_end - lp->ll_name);
2146 if (vname == NULL)
2147 return;
2148 did_alloc = TRUE;
2149 }
2150 }
2151 semsg(_(msg), vname, class_name);
2152 if (did_alloc)
2153 vim_free(vname);
2154}
2155
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002156/*
2157 * Lock or unlock variable indicated by "lp".
2158 * "deep" is the levels to go (-1 for unlimited);
2159 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2160 */
2161 static int
2162do_lock_var(
2163 lval_T *lp,
2164 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002165 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002166 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002167 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002168{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002169 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002170 int ret = OK;
2171 int cc;
2172 dictitem_T *di;
2173
Ernie Raelee865f32023-09-29 19:53:55 +02002174#ifdef LOG_LOCKVAR
2175 ch_log(NULL, "LKVAR: do_lock_var(): name %s, is_root %d", lp->ll_name, lp->ll_is_root);
2176#endif
2177
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002178 if (lp->ll_tv == NULL)
2179 {
2180 cc = *name_end;
2181 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002182 if (*lp->ll_name == '$')
2183 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002184 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002185 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002186 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002187 else
2188 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002189 // Normal name or expanded name.
2190 di = find_var(lp->ll_name, NULL, TRUE);
2191 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002192 {
2193 if (in_vim9script())
2194 semsg(_(e_cannot_find_variable_to_unlock_str),
2195 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002196 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002197 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002198 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002199 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002200 if ((di->di_flags & DI_FLAGS_FIX)
2201 && di->di_tv.v_type != VAR_DICT
2202 && di->di_tv.v_type != VAR_LIST)
2203 {
2204 // For historic reasons this error is not given for a list
2205 // or dict. E.g., the b: dict could be locked/unlocked.
2206 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
2207 ret = FAIL;
2208 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002209 else
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002210 {
2211 if (in_vim9script())
2212 {
2213 svar_T *sv = find_typval_in_script(&di->di_tv,
2214 0, FALSE);
2215
2216 if (sv != NULL && sv->sv_const != 0)
2217 {
2218 semsg(_(e_cannot_change_readonly_variable_str),
2219 lp->ll_name);
2220 ret = FAIL;
2221 }
2222 }
2223
2224 if (ret == OK)
2225 {
2226 if (lock)
2227 di->di_flags |= DI_FLAGS_LOCK;
2228 else
2229 di->di_flags &= ~DI_FLAGS_LOCK;
2230 if (deep != 0)
2231 item_lock(&di->di_tv, deep, lock, FALSE);
2232 }
2233 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002234 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002235 }
2236 *name_end = cc;
2237 }
Ernie Raelee865f32023-09-29 19:53:55 +02002238 else if (deep == 0 && lp->ll_object == NULL && lp->ll_class == NULL)
Bram Moolenaara187c432020-09-16 21:08:28 +02002239 {
2240 // nothing to do
2241 }
Ernie Raelee865f32023-09-29 19:53:55 +02002242 else if (lp->ll_is_root)
2243 // (un)lock the item.
2244 item_lock(lp->ll_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002245 else if (lp->ll_range)
2246 {
2247 listitem_T *li = lp->ll_li;
2248
2249 // (un)lock a range of List items.
2250 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2251 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02002252 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002253 li = li->li_next;
2254 ++lp->ll_n1;
2255 }
2256 }
2257 else if (lp->ll_list != NULL)
2258 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002259 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Ernie Raelee865f32023-09-29 19:53:55 +02002260 else if (lp->ll_object != NULL) // This check must be before ll_class.
2261 {
2262 // (un)lock an object variable.
2263 report_lockvar_member(e_cannot_lock_object_variable_str, lp);
2264 ret = FAIL;
2265 }
2266 else if (lp->ll_class != NULL)
2267 {
2268 // (un)lock a class variable.
2269 report_lockvar_member(e_cannot_lock_class_variable_str, lp);
2270 ret = FAIL;
2271 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002272 else
Ernie Raelee865f32023-09-29 19:53:55 +02002273 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002274 // (un)lock a Dictionary item.
Ernie Raelee865f32023-09-29 19:53:55 +02002275 if (lp->ll_di == NULL)
2276 {
2277 emsg(_(e_dictionary_required));
2278 ret = FAIL;
2279 }
2280 else
2281 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
2282 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002283
2284 return ret;
2285}
2286
2287/*
2288 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002289 * When "check_refcount" is TRUE do not lock a list or dict with a reference
2290 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002291 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002292 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02002293item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002294{
2295 static int recurse = 0;
2296 list_T *l;
2297 listitem_T *li;
2298 dict_T *d;
2299 blob_T *b;
2300 hashitem_T *hi;
2301 int todo;
2302
Ernie Raelee865f32023-09-29 19:53:55 +02002303#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02002304 ch_log(NULL, "LKVAR: item_lock(): type %s", vartype_name(tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02002305#endif
2306
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002307 if (recurse >= DICT_MAXNEST)
2308 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002309 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002310 return;
2311 }
2312 if (deep == 0)
2313 return;
2314 ++recurse;
2315
2316 // lock/unlock the item itself
2317 if (lock)
2318 tv->v_lock |= VAR_LOCKED;
2319 else
2320 tv->v_lock &= ~VAR_LOCKED;
2321
2322 switch (tv->v_type)
2323 {
2324 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002325 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002326 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002327 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002328 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002329 case VAR_STRING:
2330 case VAR_FUNC:
2331 case VAR_PARTIAL:
2332 case VAR_FLOAT:
2333 case VAR_SPECIAL:
2334 case VAR_JOB:
2335 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002336 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002337 case VAR_CLASS:
2338 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002339 case VAR_TYPEALIAS:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002340 break;
2341
2342 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002343 if ((b = tv->vval.v_blob) != NULL
2344 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002345 {
2346 if (lock)
2347 b->bv_lock |= VAR_LOCKED;
2348 else
2349 b->bv_lock &= ~VAR_LOCKED;
2350 }
2351 break;
2352 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002353 if ((l = tv->vval.v_list) != NULL
2354 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002355 {
2356 if (lock)
2357 l->lv_lock |= VAR_LOCKED;
2358 else
2359 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002360 if (deep < 0 || deep > 1)
2361 {
2362 if (l->lv_first == &range_list_item)
2363 l->lv_lock |= VAR_ITEMS_LOCKED;
2364 else
2365 {
2366 // recursive: lock/unlock the items the List contains
2367 CHECK_LIST_MATERIALIZE(l);
2368 FOR_ALL_LIST_ITEMS(l, li) item_lock(&li->li_tv,
2369 deep - 1, lock, check_refcount);
2370 }
2371 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002372 }
2373 break;
2374 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002375 if ((d = tv->vval.v_dict) != NULL
2376 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002377 {
2378 if (lock)
2379 d->dv_lock |= VAR_LOCKED;
2380 else
2381 d->dv_lock &= ~VAR_LOCKED;
2382 if (deep < 0 || deep > 1)
2383 {
2384 // recursive: lock/unlock the items the List contains
2385 todo = (int)d->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002386 FOR_ALL_HASHTAB_ITEMS(&d->dv_hashtab, hi, todo)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002387 {
2388 if (!HASHITEM_EMPTY(hi))
2389 {
2390 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002391 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2392 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002393 }
2394 }
2395 }
2396 }
2397 }
2398 --recurse;
2399}
2400
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002401#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2402/*
2403 * Delete all "menutrans_" variables.
2404 */
2405 void
2406del_menutrans_vars(void)
2407{
2408 hashitem_T *hi;
2409 int todo;
2410
2411 hash_lock(&globvarht);
2412 todo = (int)globvarht.ht_used;
2413 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2414 {
2415 if (!HASHITEM_EMPTY(hi))
2416 {
2417 --todo;
2418 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2419 delete_var(&globvarht, hi);
2420 }
2421 }
2422 hash_unlock(&globvarht);
2423}
2424#endif
2425
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002426/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002427 * Local string buffer for the next two functions to store a variable name
2428 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2429 * get_user_var_name().
2430 */
2431
2432static char_u *varnamebuf = NULL;
2433static int varnamebuflen = 0;
2434
2435/*
2436 * Function to concatenate a prefix and a variable name.
2437 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002438 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002439cat_prefix_varname(int prefix, char_u *name)
2440{
2441 int len;
2442
2443 len = (int)STRLEN(name) + 3;
2444 if (len > varnamebuflen)
2445 {
2446 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002447 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002448 varnamebuf = alloc(len);
2449 if (varnamebuf == NULL)
2450 {
2451 varnamebuflen = 0;
2452 return NULL;
2453 }
2454 varnamebuflen = len;
2455 }
2456 *varnamebuf = prefix;
2457 varnamebuf[1] = ':';
2458 STRCPY(varnamebuf + 2, name);
2459 return varnamebuf;
2460}
2461
2462/*
2463 * Function given to ExpandGeneric() to obtain the list of user defined
2464 * (global/buffer/window/built-in) variable names.
2465 */
2466 char_u *
2467get_user_var_name(expand_T *xp, int idx)
2468{
2469 static long_u gdone;
2470 static long_u bdone;
2471 static long_u wdone;
2472 static long_u tdone;
2473 static int vidx;
2474 static hashitem_T *hi;
2475 hashtab_T *ht;
2476
2477 if (idx == 0)
2478 {
2479 gdone = bdone = wdone = vidx = 0;
2480 tdone = 0;
2481 }
2482
2483 // Global variables
2484 if (gdone < globvarht.ht_used)
2485 {
2486 if (gdone++ == 0)
2487 hi = globvarht.ht_array;
2488 else
2489 ++hi;
2490 while (HASHITEM_EMPTY(hi))
2491 ++hi;
2492 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2493 return cat_prefix_varname('g', hi->hi_key);
2494 return hi->hi_key;
2495 }
2496
2497 // b: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002498 ht = &prevwin_curwin()->w_buffer->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002499 if (bdone < ht->ht_used)
2500 {
2501 if (bdone++ == 0)
2502 hi = ht->ht_array;
2503 else
2504 ++hi;
2505 while (HASHITEM_EMPTY(hi))
2506 ++hi;
2507 return cat_prefix_varname('b', hi->hi_key);
2508 }
2509
2510 // w: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002511 ht = &prevwin_curwin()->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002512 if (wdone < ht->ht_used)
2513 {
2514 if (wdone++ == 0)
2515 hi = ht->ht_array;
2516 else
2517 ++hi;
2518 while (HASHITEM_EMPTY(hi))
2519 ++hi;
2520 return cat_prefix_varname('w', hi->hi_key);
2521 }
2522
2523 // t: variables
2524 ht = &curtab->tp_vars->dv_hashtab;
2525 if (tdone < ht->ht_used)
2526 {
2527 if (tdone++ == 0)
2528 hi = ht->ht_array;
2529 else
2530 ++hi;
2531 while (HASHITEM_EMPTY(hi))
2532 ++hi;
2533 return cat_prefix_varname('t', hi->hi_key);
2534 }
2535
2536 // v: variables
2537 if (vidx < VV_LEN)
2538 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2539
2540 VIM_CLEAR(varnamebuf);
2541 varnamebuflen = 0;
2542 return NULL;
2543}
2544
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002545 char *
2546get_var_special_name(int nr)
2547{
2548 switch (nr)
2549 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002550 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2551 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002552 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002553 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002554 }
2555 internal_error("get_var_special_name()");
2556 return "42";
2557}
2558
2559/*
2560 * Returns the global variable dictionary
2561 */
2562 dict_T *
2563get_globvar_dict(void)
2564{
2565 return &globvardict;
2566}
2567
2568/*
2569 * Returns the global variable hash table
2570 */
2571 hashtab_T *
2572get_globvar_ht(void)
2573{
2574 return &globvarht;
2575}
2576
2577/*
2578 * Returns the v: variable dictionary
2579 */
2580 dict_T *
2581get_vimvar_dict(void)
2582{
2583 return &vimvardict;
2584}
2585
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002586/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002588 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 */
2590 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002591find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002593 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2594 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595
2596 if (di == NULL)
2597 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002598 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2600 return (int)(vv - vimvars);
2601}
2602
2603
2604/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002605 * Set type of v: variable to "type".
2606 */
2607 void
2608set_vim_var_type(int idx, vartype_T type)
2609{
Bram Moolenaard787e402021-12-24 21:36:12 +00002610 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002611}
2612
2613/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002614 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002615 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002616 */
2617 void
2618set_vim_var_nr(int idx, varnumber_T val)
2619{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002620 vimvars[idx].vv_nr = val;
2621}
2622
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002623 char *
2624get_vim_var_name(int idx)
2625{
2626 return vimvars[idx].vv_name;
2627}
2628
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002629/*
2630 * Get typval_T v: variable value.
2631 */
2632 typval_T *
2633get_vim_var_tv(int idx)
2634{
2635 return &vimvars[idx].vv_tv;
2636}
2637
Bram Moolenaard787e402021-12-24 21:36:12 +00002638 type_T *
2639get_vim_var_type(int idx, garray_T *type_list)
2640{
2641 if (vimvars[idx].vv_type != NULL)
2642 return vimvars[idx].vv_type;
2643 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2644}
2645
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002646/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002647 * Set v: variable to "tv". Only accepts the same type.
2648 * Takes over the value of "tv".
2649 */
2650 int
2651set_vim_var_tv(int idx, typval_T *tv)
2652{
Bram Moolenaard787e402021-12-24 21:36:12 +00002653 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002654 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002655 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002656 clear_tv(tv);
2657 return FAIL;
2658 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002659 // VV_RO is also checked when compiling, but let's check here as well.
2660 if (vimvars[idx].vv_flags & VV_RO)
2661 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002662 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002663 return FAIL;
2664 }
2665 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2666 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002667 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002668 return FAIL;
2669 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002670 clear_tv(&vimvars[idx].vv_di.di_tv);
2671 vimvars[idx].vv_di.di_tv = *tv;
2672 return OK;
2673}
2674
2675/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002676 * Get number v: variable value.
2677 */
2678 varnumber_T
2679get_vim_var_nr(int idx)
2680{
2681 return vimvars[idx].vv_nr;
2682}
2683
2684/*
2685 * Get string v: variable value. Uses a static buffer, can only be used once.
2686 * If the String variable has never been set, return an empty string.
2687 * Never returns NULL;
2688 */
2689 char_u *
2690get_vim_var_str(int idx)
2691{
2692 return tv_get_string(&vimvars[idx].vv_tv);
2693}
2694
2695/*
2696 * Get List v: variable value. Caller must take care of reference count when
2697 * needed.
2698 */
2699 list_T *
2700get_vim_var_list(int idx)
2701{
2702 return vimvars[idx].vv_list;
2703}
2704
2705/*
2706 * Get Dict v: variable value. Caller must take care of reference count when
2707 * needed.
2708 */
2709 dict_T *
2710get_vim_var_dict(int idx)
2711{
2712 return vimvars[idx].vv_dict;
2713}
2714
2715/*
2716 * Set v:char to character "c".
2717 */
2718 void
2719set_vim_var_char(int c)
2720{
2721 char_u buf[MB_MAXBYTES + 1];
2722
2723 if (has_mbyte)
2724 buf[(*mb_char2bytes)(c, buf)] = NUL;
2725 else
2726 {
2727 buf[0] = c;
2728 buf[1] = NUL;
2729 }
2730 set_vim_var_string(VV_CHAR, buf, -1);
2731}
2732
2733/*
2734 * Set v:count to "count" and v:count1 to "count1".
2735 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2736 */
2737 void
2738set_vcount(
2739 long count,
2740 long count1,
2741 int set_prevcount)
2742{
2743 if (set_prevcount)
2744 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2745 vimvars[VV_COUNT].vv_nr = count;
2746 vimvars[VV_COUNT1].vv_nr = count1;
2747}
2748
2749/*
2750 * Save variables that might be changed as a side effect. Used when executing
2751 * a timer callback.
2752 */
2753 void
2754save_vimvars(vimvars_save_T *vvsave)
2755{
2756 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2757 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2758 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2759}
2760
2761/*
2762 * Restore variables saved by save_vimvars().
2763 */
2764 void
2765restore_vimvars(vimvars_save_T *vvsave)
2766{
2767 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2768 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2769 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2770}
2771
2772/*
2773 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2774 * value.
2775 */
2776 void
2777set_vim_var_string(
2778 int idx,
2779 char_u *val,
2780 int len) // length of "val" to use or -1 (whole string)
2781{
2782 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002783 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002784 if (val == NULL)
2785 vimvars[idx].vv_str = NULL;
2786 else if (len == -1)
2787 vimvars[idx].vv_str = vim_strsave(val);
2788 else
2789 vimvars[idx].vv_str = vim_strnsave(val, len);
2790}
2791
2792/*
2793 * Set List v: variable to "val".
2794 */
2795 void
2796set_vim_var_list(int idx, list_T *val)
2797{
2798 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002799 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002800 vimvars[idx].vv_list = val;
2801 if (val != NULL)
2802 ++val->lv_refcount;
2803}
2804
2805/*
2806 * Set Dictionary v: variable to "val".
2807 */
2808 void
2809set_vim_var_dict(int idx, dict_T *val)
2810{
2811 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002812 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002813 vimvars[idx].vv_dict = val;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002814 if (val == NULL)
2815 return;
2816
2817 ++val->dv_refcount;
2818 dict_set_items_ro(val);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002819}
2820
2821/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002822 * Set the v:argv list.
2823 */
2824 void
2825set_argv_var(char **argv, int argc)
2826{
2827 list_T *l = list_alloc();
2828 int i;
2829
2830 if (l == NULL)
2831 getout(1);
2832 l->lv_lock = VAR_FIXED;
2833 for (i = 0; i < argc; ++i)
2834 {
2835 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2836 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002837 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002838 }
2839 set_vim_var_list(VV_ARGV, l);
2840}
2841
2842/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002843 * Reset v:register, taking the 'clipboard' setting into account.
2844 */
2845 void
2846reset_reg_var(void)
2847{
2848 int regname = 0;
2849
2850 // Adjust the register according to 'clipboard', so that when
2851 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2852#ifdef FEAT_CLIPBOARD
2853 adjust_clip_reg(&regname);
2854#endif
2855 set_reg_var(regname);
2856}
2857
2858/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002859 * Set v:register if needed.
2860 */
2861 void
2862set_reg_var(int c)
2863{
2864 char_u regname;
2865
2866 if (c == 0 || c == ' ')
2867 regname = '"';
2868 else
2869 regname = c;
2870 // Avoid free/alloc when the value is already right.
2871 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2872 set_vim_var_string(VV_REG, &regname, 1);
2873}
2874
2875/*
2876 * Get or set v:exception. If "oldval" == NULL, return the current value.
2877 * Otherwise, restore the value to "oldval" and return NULL.
2878 * Must always be called in pairs to save and restore v:exception! Does not
2879 * take care of memory allocations.
2880 */
2881 char_u *
2882v_exception(char_u *oldval)
2883{
2884 if (oldval == NULL)
2885 return vimvars[VV_EXCEPTION].vv_str;
2886
2887 vimvars[VV_EXCEPTION].vv_str = oldval;
2888 return NULL;
2889}
2890
2891/*
2892 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2893 * Otherwise, restore the value to "oldval" and return NULL.
2894 * Must always be called in pairs to save and restore v:throwpoint! Does not
2895 * take care of memory allocations.
2896 */
2897 char_u *
2898v_throwpoint(char_u *oldval)
2899{
2900 if (oldval == NULL)
2901 return vimvars[VV_THROWPOINT].vv_str;
2902
2903 vimvars[VV_THROWPOINT].vv_str = oldval;
2904 return NULL;
2905}
2906
2907/*
2908 * Set v:cmdarg.
2909 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2910 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2911 * Must always be called in pairs!
2912 */
2913 char_u *
2914set_cmdarg(exarg_T *eap, char_u *oldarg)
2915{
2916 char_u *oldval;
2917 char_u *newval;
2918 unsigned len;
2919
2920 oldval = vimvars[VV_CMDARG].vv_str;
2921 if (eap == NULL)
2922 {
2923 vim_free(oldval);
2924 vimvars[VV_CMDARG].vv_str = oldarg;
2925 return NULL;
2926 }
2927
2928 if (eap->force_bin == FORCE_BIN)
2929 len = 6;
2930 else if (eap->force_bin == FORCE_NOBIN)
2931 len = 8;
2932 else
2933 len = 0;
2934
2935 if (eap->read_edit)
2936 len += 7;
2937
2938 if (eap->force_ff != 0)
2939 len += 10; // " ++ff=unix"
2940 if (eap->force_enc != 0)
2941 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2942 if (eap->bad_char != 0)
2943 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2944
2945 newval = alloc(len + 1);
2946 if (newval == NULL)
2947 return NULL;
2948
2949 if (eap->force_bin == FORCE_BIN)
2950 sprintf((char *)newval, " ++bin");
2951 else if (eap->force_bin == FORCE_NOBIN)
2952 sprintf((char *)newval, " ++nobin");
2953 else
2954 *newval = NUL;
2955
2956 if (eap->read_edit)
2957 STRCAT(newval, " ++edit");
2958
2959 if (eap->force_ff != 0)
2960 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2961 eap->force_ff == 'u' ? "unix"
2962 : eap->force_ff == 'd' ? "dos"
2963 : "mac");
2964 if (eap->force_enc != 0)
2965 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2966 eap->cmd + eap->force_enc);
2967 if (eap->bad_char == BAD_KEEP)
2968 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2969 else if (eap->bad_char == BAD_DROP)
2970 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2971 else if (eap->bad_char != 0)
2972 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2973 vimvars[VV_CMDARG].vv_str = newval;
2974 return oldval;
2975}
2976
2977/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002978 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002979 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2980 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002981 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2982 */
2983 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002984eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002985 char_u *name,
Bram Moolenaar94674f22023-01-06 18:42:20 +00002986 int len, // length of "name" or zero
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002987 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002988 typval_T *rettv, // NULL when only checking existence
2989 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002990 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002991{
2992 int ret = OK;
2993 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002994 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002995 hashtab_T *ht = NULL;
Bram Moolenaar94674f22023-01-06 18:42:20 +00002996 int cc = 0;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002997 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002998
Bram Moolenaar94674f22023-01-06 18:42:20 +00002999 if (len > 0)
3000 {
3001 // truncate the name, so that we can use strcmp()
3002 cc = name[len];
3003 name[len] = NUL;
3004 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003005
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003006 // Check for local variable when debugging.
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003007 if ((sid == 0) && (tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003008 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003009 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02003010 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
3011
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003012 if (v != NULL)
3013 {
3014 tv = &v->di_tv;
3015 if (dip != NULL)
3016 *dip = v;
3017 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02003018 else
3019 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003020 }
3021
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003022 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003024 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003025 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
3026
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003027 if (sid == 0)
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003028 import = find_imported(p, 0, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029
3030 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003031 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003033 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02003034 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00003035 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02003036 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003037 ht = &SCRIPT_VARS(sid);
3038 if (ht != NULL)
3039 {
3040 dictitem_T *v = find_var_in_ht(ht, 0, name,
3041 flags & EVAL_VAR_NOAUTOLOAD);
3042
3043 if (v != NULL)
3044 {
3045 tv = &v->di_tv;
3046 if (dip != NULL)
3047 *dip = v;
3048 }
3049 else
3050 ht = NULL;
3051 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003052 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003053 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003054 {
3055 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00003056 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003057 ret = FAIL;
3058 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02003059 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003060 else
3061 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003062 if (rettv != NULL)
3063 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01003064 // special value that is used in handle_subscript()
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003065 rettv->v_type = VAR_ANY;
3066 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
3067 }
3068 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02003069 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00003071 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003072 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003073 int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003074 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003075
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003076 // In Vim9 script we can get a function reference by using the
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003077 // function name. For a global non-autoload function "g:" is
3078 // required.
3079 if (ufunc != NULL && (has_g_prefix
3080 || !func_requires_g_prefix(ufunc)))
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003081 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003082 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003083 if (rettv != NULL)
3084 {
3085 rettv->v_type = VAR_FUNC;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003086 if (has_g_prefix)
Bram Moolenaaref082e12021-12-12 21:02:03 +00003087 // Keep the "g:", otherwise script-local may be
3088 // assumed.
3089 rettv->vval.v_string = vim_strsave(name);
3090 else
3091 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003092 if (rettv->vval.v_string != NULL)
3093 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003094 }
3095 }
3096 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 }
3098
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003099 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003100 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02003101 if (tv == NULL)
3102 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003103 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003104 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02003105 ret = FAIL;
3106 }
3107 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003108 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003109 svar_T *sv = NULL;
3110 int was_assigned = FALSE;
3111
Bram Moolenaar11005b02021-07-11 20:59:00 +02003112 if (ht != NULL && ht == get_script_local_ht()
3113 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02003114 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003115 sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaarf055d452021-07-08 20:57:24 +02003116 if (sv != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003117 {
Bram Moolenaarf055d452021-07-08 20:57:24 +02003118 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003119 was_assigned = sv->sv_flags & SVFLAG_ASSIGNED;
3120 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003121 }
3122
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003123 if ((tv->v_type == VAR_TYPEALIAS || tv->v_type == VAR_CLASS)
3124 && sid != 0)
3125 {
3126 // type alias or class imported from another script. Check
3127 // whether it is exported from the other script.
3128 sv = find_typval_in_script(tv, sid, TRUE);
3129 if (sv == NULL)
3130 {
3131 ret = FAIL;
3132 goto done;
3133 }
3134 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
3135 {
3136 semsg(_(e_item_not_exported_in_script_str), name);
3137 ret = FAIL;
3138 goto done;
3139 }
3140 }
3141
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003142 // If a list or dict variable wasn't initialized and has meaningful
3143 // type, do it now. Not for global variables, they are not
3144 // declared.
Bram Moolenaar7a222242022-03-01 19:23:24 +00003145 if (ht != &globvarht)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003146 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003147 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003148 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003149 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003150 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003151 tv->vval.v_dict = dict_alloc();
3152 if (tv->vval.v_dict != NULL)
3153 {
3154 ++tv->vval.v_dict->dv_refcount;
3155 tv->vval.v_dict->dv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003156 if (sv != NULL)
3157 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003158 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003159 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003160 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003161 && ((type != NULL && !was_assigned)
Bram Moolenaar501f9782022-03-27 16:51:04 +01003162 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003163 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003164 tv->vval.v_list = list_alloc();
3165 if (tv->vval.v_list != NULL)
3166 {
3167 ++tv->vval.v_list->lv_refcount;
3168 tv->vval.v_list->lv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003169 if (sv != NULL)
3170 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003171 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003172 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003173 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003174 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003175 || !in_vim9script()))
3176 {
3177 tv->vval.v_blob = blob_alloc();
3178 if (tv->vval.v_blob != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003179 {
Bram Moolenaar859cc212022-03-28 15:22:35 +01003180 ++tv->vval.v_blob->bv_refcount;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003181 if (sv != NULL)
3182 sv->sv_flags |= SVFLAG_ASSIGNED;
3183 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003184 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02003185 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003186 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003187 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003188 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003189
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003190done:
Bram Moolenaar94674f22023-01-06 18:42:20 +00003191 if (len > 0)
3192 name[len] = cc;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003193
3194 return ret;
3195}
3196
3197/*
Bram Moolenaara86655a2023-01-12 17:06:27 +00003198 * Get the value of internal variable "name", also handling "import.name".
3199 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
3200 */
3201 int
3202eval_variable_import(
3203 char_u *name,
3204 typval_T *rettv)
3205{
3206 char_u *s = name;
3207 while (ASCII_ISALNUM(*s) || *s == '_')
3208 ++s;
3209 int len = (int)(s - name);
3210
3211 if (eval_variable(name, len, 0, rettv, NULL, EVAL_VAR_IMPORT) == FAIL)
3212 return FAIL;
3213 if (rettv->v_type == VAR_ANY && *s == '.')
3214 {
Bram Moolenaar40594002023-01-12 20:04:51 +00003215 char_u *ns = s + 1;
3216 s = ns;
3217 while (ASCII_ISALNUM(*s) || *s == '_')
3218 ++s;
Bram Moolenaara86655a2023-01-12 17:06:27 +00003219 int sid = rettv->vval.v_number;
Bram Moolenaar40594002023-01-12 20:04:51 +00003220 return eval_variable(ns, (int)(s - ns), sid, rettv, NULL, 0);
Bram Moolenaara86655a2023-01-12 17:06:27 +00003221 }
3222 return OK;
3223}
3224
3225
3226/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003227 * Check if variable "name[len]" is a local variable or an argument.
3228 * If so, "*eval_lavars_used" is set to TRUE.
3229 */
3230 void
3231check_vars(char_u *name, int len)
3232{
3233 int cc;
3234 char_u *varname;
3235 hashtab_T *ht;
3236
3237 if (eval_lavars_used == NULL)
3238 return;
3239
3240 // truncate the name, so that we can use strcmp()
3241 cc = name[len];
3242 name[len] = NUL;
3243
3244 ht = find_var_ht(name, &varname);
3245 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
3246 {
3247 if (find_var(name, NULL, TRUE) != NULL)
3248 *eval_lavars_used = TRUE;
3249 }
3250
3251 name[len] = cc;
3252}
3253
3254/*
3255 * Find variable "name" in the list of variables.
3256 * Return a pointer to it if found, NULL if not found.
3257 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003258 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003259 */
3260 dictitem_T *
3261find_var(char_u *name, hashtab_T **htp, int no_autoload)
3262{
3263 char_u *varname;
3264 hashtab_T *ht;
3265 dictitem_T *ret = NULL;
3266
3267 ht = find_var_ht(name, &varname);
3268 if (htp != NULL)
3269 *htp = ht;
3270 if (ht == NULL)
3271 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003272 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003273 if (ret != NULL)
3274 return ret;
3275
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003276 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003277 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003278 if (ret != NULL)
3279 return ret;
3280
3281 // in Vim9 script items without a scope can be script-local
3282 if (in_vim9script() && name[0] != NUL && name[1] != ':')
3283 {
3284 ht = get_script_local_ht();
3285 if (ht != NULL)
3286 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003287 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003288 if (ret != NULL)
3289 {
3290 if (htp != NULL)
3291 *htp = ht;
3292 return ret;
3293 }
3294 }
3295 }
3296
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003297 // When using "vim9script autoload" script-local items are prefixed but can
3298 // be used with s:name.
3299 if (SCRIPT_ID_VALID(current_sctx.sc_sid)
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003300 && (in_vim9script() || (name[0] == 's' && name[1] == ':')))
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003301 {
3302 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
3303
3304 if (si->sn_autoload_prefix != NULL)
3305 {
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003306 char_u *base_name = (name[0] == 's' && name[1] == ':')
3307 ? name + 2 : name;
3308 char_u *auto_name = concat_str(si->sn_autoload_prefix, base_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003309
3310 if (auto_name != NULL)
3311 {
3312 ht = &globvarht;
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003313 ret = find_var_in_ht(ht, 'g', auto_name, TRUE);
Bram Moolenaar130f65d2022-01-13 20:39:41 +00003314 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003315 if (ret != NULL)
3316 {
3317 if (htp != NULL)
3318 *htp = ht;
3319 return ret;
3320 }
3321 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003322 }
3323 }
3324
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003325 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003326}
3327
3328/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00003329 * Like find_var() but if the name starts with <SNR>99_ then look in the
3330 * referenced script (used for a funcref).
3331 */
3332 dictitem_T *
3333find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
3334{
Keith Thompson184f71c2024-01-04 21:19:04 +01003335 if (STRNCMP(name, "<SNR>", 5) == 0 && SAFE_isdigit(name[5]))
Bram Moolenaar71f21932022-01-07 18:20:55 +00003336 {
3337 char_u *p = name + 5;
3338 int sid = getdigits(&p);
3339
3340 if (SCRIPT_ID_VALID(sid) && *p == '_')
3341 {
3342 hashtab_T *ht = &SCRIPT_VARS(sid);
3343
3344 if (ht != NULL)
3345 {
3346 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
3347
3348 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003349 {
3350 if (htp != NULL)
3351 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00003352 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003353 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00003354 }
3355 }
3356 }
3357
3358 return find_var(name, htp, no_autoload);
3359}
3360
3361/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003362 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02003363 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003364 * Returns NULL if not found.
3365 */
3366 dictitem_T *
3367find_var_in_ht(
3368 hashtab_T *ht,
3369 int htname,
3370 char_u *varname,
3371 int no_autoload)
3372{
3373 hashitem_T *hi;
3374
3375 if (*varname == NUL)
3376 {
3377 // Must be something like "s:", otherwise "ht" would be NULL.
3378 switch (htname)
3379 {
3380 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
3381 case 'g': return &globvars_var;
3382 case 'v': return &vimvars_var;
3383 case 'b': return &curbuf->b_bufvar;
3384 case 'w': return &curwin->w_winvar;
3385 case 't': return &curtab->tp_winvar;
3386 case 'l': return get_funccal_local_var();
3387 case 'a': return get_funccal_args_var();
3388 }
3389 return NULL;
3390 }
3391
3392 hi = hash_find(ht, varname);
3393 if (HASHITEM_EMPTY(hi))
3394 {
3395 // For global variables we may try auto-loading the script. If it
3396 // worked find the variable again. Don't auto-load a script if it was
3397 // loaded already, otherwise it would be loaded every time when
3398 // checking if a function name is a Funcref variable.
3399 if (ht == &globvarht && !no_autoload)
3400 {
3401 // Note: script_autoload() may make "hi" invalid. It must either
3402 // be obtained again or not used.
3403 if (!script_autoload(varname, FALSE) || aborting())
3404 return NULL;
3405 hi = hash_find(ht, varname);
3406 }
3407 if (HASHITEM_EMPTY(hi))
3408 return NULL;
3409 }
3410 return HI2DI(hi);
3411}
3412
3413/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 * Get the script-local hashtab. NULL if not in a script context.
3415 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003416 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417get_script_local_ht(void)
3418{
3419 scid_T sid = current_sctx.sc_sid;
3420
Bram Moolenaare3d46852020-08-29 13:39:17 +02003421 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 return &SCRIPT_VARS(sid);
3423 return NULL;
3424}
3425
3426/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003427 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003428 * When "cmd" is TRUE it must look like a command, a function must be followed
3429 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003430 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003432 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003433lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003434 char_u *name,
3435 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003436 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003437 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438{
3439 hashtab_T *ht = get_script_local_ht();
3440 char_u buffer[30];
3441 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003442 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003444 int is_global = FALSE;
3445 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446
3447 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003448 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 if (len < sizeof(buffer) - 1)
3450 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003451 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 vim_strncpy(buffer, name, len);
3453 p = buffer;
3454 }
3455 else
3456 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003457 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003459 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460 }
3461
3462 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003463 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464
3465 // if not script-local, then perhaps imported
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003466 if (res == FAIL && find_imported(p, 0, FALSE) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003467 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468 if (p != buffer)
3469 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003470
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003471 // Find a function, so that a following "->" works.
3472 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3473 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003474 if (res != OK)
3475 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003476 p = skipwhite(name + len);
3477
3478 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003479 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003480 // Do not check for an internal function, since it might also be a
3481 // valid command, such as ":split" versus "split()".
3482 // Skip "g:" before a function name.
3483 if (name[0] == 'g' && name[1] == ':')
3484 {
3485 is_global = TRUE;
3486 fname = name + 2;
3487 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003488 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003489 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003490 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003491 }
3492
Bram Moolenaar709664c2020-12-12 14:33:41 +01003493 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494}
3495
3496/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003497 * Find the hashtab used for a variable name.
3498 * Return NULL if the name is not valid.
3499 * Set "varname" to the start of name without ':'.
3500 */
3501 hashtab_T *
3502find_var_ht(char_u *name, char_u **varname)
3503{
3504 hashitem_T *hi;
3505 hashtab_T *ht;
3506
3507 if (name[0] == NUL)
3508 return NULL;
3509 if (name[1] != ':')
3510 {
3511 // The name must not start with a colon or #.
3512 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3513 return NULL;
3514 *varname = name;
3515
3516 // "version" is "v:version" in all scopes if scriptversion < 3.
3517 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003518 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003519 {
3520 hi = hash_find(&compat_hashtab, name);
3521 if (!HASHITEM_EMPTY(hi))
3522 return &compat_hashtab;
3523 }
3524
3525 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 if (ht != NULL)
3527 return ht; // local variable
3528
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003529 // In Vim9 script items at the script level are script-local, except
3530 // for autoload names.
3531 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 {
3533 ht = get_script_local_ht();
3534 if (ht != NULL)
3535 return ht;
3536 }
3537
3538 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003539 }
3540 *varname = name + 2;
3541 if (*name == 'g') // global variable
3542 return &globvarht;
3543 // There must be no ':' or '#' in the rest of the name, unless g: is used
3544 if (vim_strchr(name + 2, ':') != NULL
3545 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3546 return NULL;
3547 if (*name == 'b') // buffer variable
3548 return &curbuf->b_vars->dv_hashtab;
3549 if (*name == 'w') // window variable
3550 return &curwin->w_vars->dv_hashtab;
3551 if (*name == 't') // tab page variable
3552 return &curtab->tp_vars->dv_hashtab;
3553 if (*name == 'v') // v: variable
3554 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003555 if (get_current_funccal() != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01003556 && get_current_funccal()->fc_func->uf_def_status
3557 == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003559 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 if (*name == 'a') // a: function argument
3561 return get_funccal_args_ht();
3562 if (*name == 'l') // l: local function variable
3563 return get_funccal_local_ht();
3564 }
3565 if (*name == 's') // script variable
3566 {
3567 ht = get_script_local_ht();
3568 if (ht != NULL)
3569 return ht;
3570 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003571 return NULL;
3572}
3573
3574/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003575 * Get the string value of a (global/local) variable.
3576 * Note: see tv_get_string() for how long the pointer remains valid.
3577 * Returns NULL when it doesn't exist.
3578 */
3579 char_u *
3580get_var_value(char_u *name)
3581{
3582 dictitem_T *v;
3583
3584 v = find_var(name, NULL, FALSE);
3585 if (v == NULL)
3586 return NULL;
3587 return tv_get_string(&v->di_tv);
3588}
3589
3590/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003591 * Allocate a new hashtab for a sourced script. It will be used while
3592 * sourcing this script and when executing functions defined in the script.
3593 */
3594 void
3595new_script_vars(scid_T id)
3596{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003597 scriptvar_T *sv;
3598
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003599 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3600 if (sv == NULL)
3601 return;
3602 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003603 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003604}
3605
3606/*
3607 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3608 * point to it.
3609 */
3610 void
3611init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3612{
3613 hash_init(&dict->dv_hashtab);
3614 dict->dv_lock = 0;
3615 dict->dv_scope = scope;
3616 dict->dv_refcount = DO_NOT_FREE_CNT;
3617 dict->dv_copyID = 0;
3618 dict_var->di_tv.vval.v_dict = dict;
3619 dict_var->di_tv.v_type = VAR_DICT;
3620 dict_var->di_tv.v_lock = VAR_FIXED;
3621 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3622 dict_var->di_key[0] = NUL;
3623}
3624
3625/*
3626 * Unreference a dictionary initialized by init_var_dict().
3627 */
3628 void
3629unref_var_dict(dict_T *dict)
3630{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003631 // Now the dict needs to be freed if no one else is using it, go back to
3632 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003633 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3634 dict_unref(dict);
3635}
3636
3637/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003638 * Clean up a list of internal variables.
3639 * Frees all allocated variables and the value they contain.
3640 * Clears hashtab "ht", does not free it.
3641 */
3642 void
3643vars_clear(hashtab_T *ht)
3644{
3645 vars_clear_ext(ht, TRUE);
3646}
3647
3648/*
3649 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3650 */
3651 void
3652vars_clear_ext(hashtab_T *ht, int free_val)
3653{
3654 int todo;
3655 hashitem_T *hi;
3656 dictitem_T *v;
3657
3658 hash_lock(ht);
3659 todo = (int)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003660 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003661 {
3662 if (!HASHITEM_EMPTY(hi))
3663 {
3664 --todo;
3665
3666 // Free the variable. Don't remove it from the hashtab,
3667 // ht_array might change then. hash_clear() takes care of it
3668 // later.
3669 v = HI2DI(hi);
3670 if (free_val)
3671 clear_tv(&v->di_tv);
3672 if (v->di_flags & DI_FLAGS_ALLOC)
3673 vim_free(v);
3674 }
3675 }
3676 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003677 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003678}
3679
3680/*
3681 * Delete a variable from hashtab "ht" at item "hi".
3682 * Clear the variable value and free the dictitem.
3683 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003684 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003685delete_var(hashtab_T *ht, hashitem_T *hi)
3686{
3687 dictitem_T *di = HI2DI(hi);
3688
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003689 if (hash_remove(ht, hi, "delete variable") != OK)
3690 return;
3691
3692 clear_tv(&di->di_tv);
3693 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003694}
3695
3696/*
3697 * List the value of one internal variable.
3698 */
3699 static void
3700list_one_var(dictitem_T *v, char *prefix, int *first)
3701{
3702 char_u *tofree;
3703 char_u *s;
3704 char_u numbuf[NUMBUFLEN];
3705
3706 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3707 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3708 s == NULL ? (char_u *)"" : s, first);
3709 vim_free(tofree);
3710}
3711
3712 static void
3713list_one_var_a(
3714 char *prefix,
3715 char_u *name,
3716 int type,
3717 char_u *string,
3718 int *first) // when TRUE clear rest of screen and set to FALSE
3719{
3720 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3721 msg_start();
3722 msg_puts(prefix);
3723 if (name != NULL) // "a:" vars don't have a name stored
3724 msg_puts((char *)name);
3725 msg_putchar(' ');
3726 msg_advance(22);
3727 if (type == VAR_NUMBER)
3728 msg_putchar('#');
3729 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3730 msg_putchar('*');
3731 else if (type == VAR_LIST)
3732 {
3733 msg_putchar('[');
3734 if (*string == '[')
3735 ++string;
3736 }
3737 else if (type == VAR_DICT)
3738 {
3739 msg_putchar('{');
3740 if (*string == '{')
3741 ++string;
3742 }
3743 else
3744 msg_putchar(' ');
3745
3746 msg_outtrans(string);
3747
3748 if (type == VAR_FUNC || type == VAR_PARTIAL)
3749 msg_puts("()");
3750 if (*first)
3751 {
3752 msg_clr_eos();
3753 *first = FALSE;
3754 }
3755}
3756
3757/*
zeertzjqedcba962023-09-24 23:13:51 +02003758 * Addition handling for setting a v: variable.
3759 * Return TRUE if the variable should be set normally,
3760 * FALSE if nothing else needs to be done.
3761 */
3762 int
3763before_set_vvar(
3764 char_u *varname,
3765 dictitem_T *di,
3766 typval_T *tv,
3767 int copy,
3768 int *type_error)
3769{
3770 if (di->di_tv.v_type == VAR_STRING)
3771 {
3772 VIM_CLEAR(di->di_tv.vval.v_string);
3773 if (copy || tv->v_type != VAR_STRING)
3774 {
3775 char_u *val = tv_get_string(tv);
3776
3777 // Careful: when assigning to v:errmsg and
3778 // tv_get_string() causes an error message the variable
3779 // will already be set.
3780 if (di->di_tv.vval.v_string == NULL)
3781 di->di_tv.vval.v_string = vim_strsave(val);
3782 }
3783 else
3784 {
3785 // Take over the string to avoid an extra alloc/free.
3786 di->di_tv.vval.v_string = tv->vval.v_string;
3787 tv->vval.v_string = NULL;
3788 }
3789 return FALSE;
3790 }
3791 else if (di->di_tv.v_type == VAR_NUMBER)
3792 {
3793 di->di_tv.vval.v_number = tv_get_number(tv);
3794 if (STRCMP(varname, "searchforward") == 0)
3795 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
3796#ifdef FEAT_SEARCH_EXTRA
3797 else if (STRCMP(varname, "hlsearch") == 0)
3798 {
3799 no_hlsearch = !di->di_tv.vval.v_number;
3800 redraw_all_later(UPD_SOME_VALID);
3801 }
3802#endif
3803 return FALSE;
3804 }
3805 else if (di->di_tv.v_type != tv->v_type)
3806 {
3807 *type_error = TRUE;
3808 return FALSE;
3809 }
3810 return TRUE;
3811}
3812
3813/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003814 * Set variable "name" to value in "tv".
3815 * If the variable already exists, the value is updated.
3816 * Otherwise the variable is created.
3817 */
3818 void
3819set_var(
3820 char_u *name,
3821 typval_T *tv,
3822 int copy) // make copy of value in "tv"
3823{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003824 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003825}
3826
3827/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003828 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003829 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003830 * If the variable already exists and "is_const" is FALSE the value is updated.
3831 * Otherwise the variable is created.
3832 */
3833 void
3834set_var_const(
3835 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003836 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003837 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003838 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003839 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003840 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003841 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003842{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003843 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003844 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003845 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846 dictitem_T *di;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003847 typval_T *dest_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003848 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003849 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003850 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003852 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003853 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003854 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003855 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003856 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003857
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003858 if (sid != 0)
3859 {
3860 if (SCRIPT_ID_VALID(sid))
3861 ht = &SCRIPT_VARS(sid);
3862 varname = name;
3863 }
3864 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003865 {
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003866 scriptitem_T *si;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003867
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003868 if (in_vim9script() && is_export
3869 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3870 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
Bram Moolenaar0e1574c2022-03-03 17:05:35 +00003871 ->sn_autoload_prefix != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003872 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003873 // In a vim9 autoload script an exported variable is put in the
3874 // global namespace with the autoload prefix.
3875 var_in_autoload = TRUE;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003876 varname = concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003877 if (varname == NULL)
3878 goto failed;
3879 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003880 ht = &globvarht;
3881 }
3882 else
3883 ht = find_var_ht(name, &varname);
3884 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003885 if (ht == NULL || *varname == NUL)
3886 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003887 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003888 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003889 }
Bram Moolenaar54969f42022-02-07 13:56:44 +00003890 is_script_local = ht == get_script_local_ht() || sid != 0
3891 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003892
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003893 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003894 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003895 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003896 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003897 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003898 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003899 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003900 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003901 }
Bram Moolenaar9510d222022-09-11 15:14:05 +01003902 if ((flags & ASSIGN_FOR_LOOP) && is_scoped_variable(name))
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003903 // Do not make g:var, w:var, b:var or t:var final.
3904 flags &= ~ASSIGN_FINAL;
3905
Bram Moolenaare535db82021-03-31 21:07:24 +02003906 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003907 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3908 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003909 // For "[a, _] = list" the underscore is ignored.
3910 if ((flags & ASSIGN_UNPACK) == 0)
3911 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003912 goto failed;
3913 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003914
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003916
Bram Moolenaar24e93162021-07-18 20:40:33 +02003917 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003918 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003919 imported_T *import = find_imported(varname, 0, FALSE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003920
Bram Moolenaar24e93162021-07-18 20:40:33 +02003921 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003922 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003923 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02003924 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003926 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003927 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003929 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3930 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003931 }
Bram Moolenaar75e27d72022-02-13 13:56:29 +00003932 if (!in_vim9script())
3933 {
3934 semsg(_(e_cannot_create_vim9_script_variable_in_function_str),
3935 name);
3936 goto failed;
3937 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003938 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939
Bram Moolenaar993faa32022-02-21 15:59:11 +00003940 // Search in parent scope which is possible to reference from lambda
3941 if (di == NULL)
3942 di = find_var_in_scoped_ht(name, TRUE);
3943
3944 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
3945 && var_wrong_func_name(name, di == NULL))
3946 goto failed;
3947
3948 if (need_convert_to_bool(type, tv))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003949 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003950 // Destination is a bool and the value is not, but it can be
3951 // converted.
3952 CLEAR_FIELD(bool_tv);
3953 bool_tv.v_type = VAR_BOOL;
3954 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3955 tv = &bool_tv;
3956 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003957
Bram Moolenaar993faa32022-02-21 15:59:11 +00003958 if (di != NULL)
3959 {
3960 // Item already exists. Allowed to replace when reloading.
3961 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003962 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003963 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
3964 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003965 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003966 emsg(_(e_cannot_modify_existing_variable));
3967 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003968 }
3969
Bram Moolenaar993faa32022-02-21 15:59:11 +00003970 if (is_script_local && vim9script
3971 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003972 {
3973 semsg(_(e_redefining_script_item_str), name);
3974 goto failed;
3975 }
3976
Ernie Raele75fde62023-12-21 17:18:54 +01003977 if (check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003978 goto failed;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003979
Bram Moolenaar993faa32022-02-21 15:59:11 +00003980 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003981 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003982 where_T where = WHERE_INIT;
Bram Moolenaar7a411a32022-04-04 14:58:06 +01003983 svar_T *sv = find_typval_in_script(&di->di_tv, sid, TRUE);
Bram Moolenaar993faa32022-02-21 15:59:11 +00003984
3985 if (sv != NULL)
3986 {
3987 // check the type and adjust to bool if needed
LemonBoyc5d27442023-08-19 13:02:35 +02003988 if (var_idx > 0)
3989 {
3990 where.wt_index = var_idx;
3991 where.wt_kind = WT_VARIABLE;
3992 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00003993 if (check_script_var_type(sv, tv, name, where) == FAIL)
3994 goto failed;
3995 if (type == NULL)
3996 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003997 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003998 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003999 }
4000
Bram Moolenaar993faa32022-02-21 15:59:11 +00004001 if ((flags & ASSIGN_FOR_LOOP) == 0
Bram Moolenaar16d2c022023-06-05 19:46:18 +01004002 ? var_check_permission(di, name) == FAIL
4003 : var_check_ro(di->di_flags, name, FALSE))
Bram Moolenaar24e93162021-07-18 20:40:33 +02004004 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004005 }
4006 else
4007 {
4008 // can only redefine once
4009 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar24e93162021-07-18 20:40:33 +02004010
Bram Moolenaar993faa32022-02-21 15:59:11 +00004011 // A Vim9 script-local variable is also present in sn_all_vars
4012 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004013 if (var_in_vim9script || var_in_autoload)
Bram Moolenaar993faa32022-02-21 15:59:11 +00004014 update_vim9_script_var(FALSE, di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004015 var_in_autoload ? name : di->di_key, flags,
Bram Moolenaar993faa32022-02-21 15:59:11 +00004016 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01004017 }
4018
Bram Moolenaar993faa32022-02-21 15:59:11 +00004019 // existing variable, need to clear the value
4020
zeertzjqedcba962023-09-24 23:13:51 +02004021 // Handle setting internal v: variables separately where needed to
Bram Moolenaar993faa32022-02-21 15:59:11 +00004022 // prevent changing the type.
zeertzjqedcba962023-09-24 23:13:51 +02004023 int type_error = FALSE;
4024 if (ht == &vimvarht
4025 && !before_set_vvar(varname, di, tv, copy, &type_error))
Bram Moolenaar993faa32022-02-21 15:59:11 +00004026 {
zeertzjqedcba962023-09-24 23:13:51 +02004027 if (type_error)
4028 semsg(_(e_setting_v_str_to_value_with_wrong_type), varname);
4029 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004030 }
4031
4032 clear_tv(&di->di_tv);
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01004033
4034 if ((flags & ASSIGN_UPDATE_BLOCK_ID)
4035 && SCRIPT_ID_VALID(current_sctx.sc_sid))
4036 {
4037 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
4038
4039 update_script_var_block_id(name, si->sn_current_block_id);
4040 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00004041 }
4042 else
4043 {
4044 // Item not found, check if a function already exists.
4045 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
4046 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
4047 {
4048 semsg(_(e_redefining_script_item_str), name);
4049 goto failed;
4050 }
4051
4052 // add a new variable
4053 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
4054 {
4055 semsg(_(e_unknown_variable_str), name);
4056 goto failed;
4057 }
4058
Bram Moolenaaref2c3252022-11-25 16:31:51 +00004059 if (check_hashtab_frozen(ht, "add variable"))
4060 goto failed;
4061
Bram Moolenaar993faa32022-02-21 15:59:11 +00004062 // Can't add "v:" or "a:" variable.
4063 if (ht == &vimvarht || ht == get_funccal_args_ht())
4064 {
4065 semsg(_(e_illegal_variable_name_str), name);
4066 goto failed;
4067 }
4068
4069 // Make sure the variable name is valid. In Vim9 script an
4070 // autoload variable must be prefixed with "g:" unless in an
4071 // autoload script.
4072 if (!valid_varname(varname, -1, !vim9script
4073 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
4074 goto failed;
4075
zeertzjq1b438a82023-02-01 13:11:15 +00004076 di = alloc(offsetof(dictitem_T, di_key) + STRLEN(varname) + 1);
Bram Moolenaar993faa32022-02-21 15:59:11 +00004077 if (di == NULL)
4078 goto failed;
4079 STRCPY(di->di_key, varname);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00004080 if (hash_add(ht, DI2HIKEY(di), "add variable") == FAIL)
Bram Moolenaar993faa32022-02-21 15:59:11 +00004081 {
4082 vim_free(di);
4083 goto failed;
4084 }
4085 di->di_flags = DI_FLAGS_ALLOC;
4086 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
4087 di->di_flags |= DI_FLAGS_LOCK;
4088
4089 // A Vim9 script-local variable is also added to sn_all_vars and
4090 // sn_var_vals. It may set "type" from "tv".
4091 if (var_in_vim9script || var_in_autoload)
4092 update_vim9_script_var(TRUE, di,
4093 var_in_autoload ? name : di->di_key, flags,
4094 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004095 }
4096
Bram Moolenaar993faa32022-02-21 15:59:11 +00004097 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004098 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02004099 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004100 else
4101 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02004102 *dest_tv = *tv;
4103 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004104 init_tv(tv);
4105 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004106 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004107
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004108 if (vim9script && type != NULL)
Bram Moolenaar381692b2022-02-02 20:01:27 +00004109 set_tv_type(dest_tv, type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004110
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01004111 // ":const var = value" locks the value
4112 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004113 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02004114 // Like :lockvar! name: lock the value and what it contains, but only
4115 // if the reference count is up to one. That locks only literal
4116 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02004117 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02004118
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004119failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004120 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004121 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004122 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004123}
4124
4125/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004126 * Check in this order for backwards compatibility:
4127 * - Whether the variable is read-only
4128 * - Whether the variable value is locked
4129 * - Whether the variable is locked
4130 */
4131 int
4132var_check_permission(dictitem_T *di, char_u *name)
4133{
4134 if (var_check_ro(di->di_flags, name, FALSE)
4135 || value_check_lock(di->di_tv.v_lock, name, FALSE)
4136 || var_check_lock(di->di_flags, name, FALSE))
4137 return FAIL;
4138 return OK;
4139}
4140
4141/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004142 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
4143 * Also give an error message.
4144 */
4145 int
4146var_check_ro(int flags, char_u *name, int use_gettext)
4147{
4148 if (flags & DI_FLAGS_RO)
4149 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004150 if (name == NULL)
4151 emsg(_(e_cannot_change_readonly_variable));
4152 else
4153 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02004154 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004155 return TRUE;
4156 }
4157 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
4158 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004159 if (name == NULL)
4160 emsg(_(e_cannot_set_variable_in_sandbox));
4161 else
4162 semsg(_(e_cannot_set_variable_in_sandbox_str),
4163 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004164 return TRUE;
4165 }
4166 return FALSE;
4167}
4168
4169/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004170 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
4171 * Also give an error message.
4172 */
4173 int
4174var_check_lock(int flags, char_u *name, int use_gettext)
4175{
4176 if (flags & DI_FLAGS_LOCK)
4177 {
4178 semsg(_(e_variable_is_locked_str),
4179 use_gettext ? (char_u *)_(name) : name);
4180 return TRUE;
4181 }
4182 return FALSE;
4183}
4184
4185/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004186 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
4187 * Also give an error message.
4188 */
4189 int
4190var_check_fixed(int flags, char_u *name, int use_gettext)
4191{
4192 if (flags & DI_FLAGS_FIX)
4193 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004194 if (name == NULL)
4195 emsg(_(e_cannot_delete_variable));
4196 else
4197 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004198 use_gettext ? (char_u *)_(name) : name);
4199 return TRUE;
4200 }
4201 return FALSE;
4202}
4203
4204/*
4205 * Check if a funcref is assigned to a valid variable name.
4206 * Return TRUE and give an error if not.
4207 */
4208 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004209var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004210 char_u *name, // points to start of variable name
4211 int new_var) // TRUE when creating the variable
4212{
Bram Moolenaar32154662021-03-28 21:14:06 +02004213 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
4214 // the name can be used without the s: prefix.
thinca6c667bd2022-09-02 11:25:37 +01004215 // Allow autoload variable.
Bram Moolenaar32154662021-03-28 21:14:06 +02004216 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
4217 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004218 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
thinca6c667bd2022-09-02 11:25:37 +01004219 ? name[2] : name[0])
4220 && vim_strchr(name, '#') == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004221 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004222 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004223 return TRUE;
4224 }
4225 // Don't allow hiding a function. When "v" is not NULL we might be
4226 // assigning another function to the same var, the type is checked
4227 // below.
4228 if (new_var && function_exists(name, FALSE))
4229 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004230 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004231 name);
4232 return TRUE;
4233 }
4234 return FALSE;
4235}
4236
4237/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004238 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
4239 * value. Also give an error message, using "name" or _("name") when
4240 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004241 */
4242 int
Bram Moolenaara187c432020-09-16 21:08:28 +02004243value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004244{
4245 if (lock & VAR_LOCKED)
4246 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004247 if (name == NULL)
4248 emsg(_(e_value_is_locked));
4249 else
4250 semsg(_(e_value_is_locked_str),
4251 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004252 return TRUE;
4253 }
4254 if (lock & VAR_FIXED)
4255 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004256 if (name == NULL)
4257 emsg(_(e_cannot_change_value));
4258 else
4259 semsg(_(e_cannot_change_value_of_str),
4260 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004261 return TRUE;
4262 }
4263 return FALSE;
4264}
4265
4266/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01004267 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004268 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004269 * Return FALSE and give an error if not.
4270 */
4271 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004272valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004273{
4274 char_u *p;
4275
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004276 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004277 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01004278 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004279 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004280 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004281 return FALSE;
4282 }
4283 return TRUE;
4284}
4285
4286/*
LemonBoy47d4e312022-05-04 18:12:55 +01004287 * Implements the logic to retrieve local variable and option values.
4288 * Used by "getwinvar()" "gettabvar()" "gettabwinvar()" "getbufvar()".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004289 */
4290 static void
LemonBoy47d4e312022-05-04 18:12:55 +01004291get_var_from(
4292 char_u *varname,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004293 typval_T *rettv,
LemonBoy47d4e312022-05-04 18:12:55 +01004294 typval_T *deftv, // Default value if not found.
4295 int htname, // 't'ab, 'w'indow or 'b'uffer local.
4296 tabpage_T *tp, // can be NULL
4297 win_T *win,
4298 buf_T *buf) // Ignored if htname is not 'b'.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004299{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004300 dictitem_T *v;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004301 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004302 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004303 int need_switch_win;
zeertzjqcd6ad642022-07-25 12:28:09 +01004304 int do_change_curbuf = buf != NULL && htname == 'b';
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004305
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004306 ++emsg_off;
4307
4308 rettv->v_type = VAR_STRING;
4309 rettv->vval.v_string = NULL;
4310
LemonBoy47d4e312022-05-04 18:12:55 +01004311 if (varname != NULL && tp != NULL && win != NULL
4312 && (htname != 'b' || buf != NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004313 {
4314 // Set curwin to be our win, temporarily. Also set the tabpage,
4315 // otherwise the window is not valid. Only do this when needed,
4316 // autocommands get blocked.
LemonBoy47d4e312022-05-04 18:12:55 +01004317 // If we have a buffer reference avoid the switching, we're saving and
4318 // restoring curbuf directly.
zeertzjqcd6ad642022-07-25 12:28:09 +01004319 need_switch_win = !(tp == curtab && win == curwin) && !do_change_curbuf;
LemonBoy47d4e312022-05-04 18:12:55 +01004320 if (!need_switch_win || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004321 {
LemonBoy47d4e312022-05-04 18:12:55 +01004322 // Handle options. There are no tab-local options.
4323 if (*varname == '&' && htname != 't')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004324 {
LemonBoy47d4e312022-05-04 18:12:55 +01004325 buf_T *save_curbuf = curbuf;
4326
4327 // Change curbuf so the option is read from the correct buffer.
zeertzjqcd6ad642022-07-25 12:28:09 +01004328 if (do_change_curbuf)
LemonBoy47d4e312022-05-04 18:12:55 +01004329 curbuf = buf;
4330
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004331 if (varname[1] == NUL)
4332 {
zeertzjqcd6ad642022-07-25 12:28:09 +01004333 // get all window-local or buffer-local options in a dict
LemonBoy47d4e312022-05-04 18:12:55 +01004334 dict_T *opts = get_winbuf_options(htname == 'b');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004335
4336 if (opts != NULL)
4337 {
4338 rettv_dict_set(rettv, opts);
4339 done = TRUE;
4340 }
4341 }
LemonBoy47d4e312022-05-04 18:12:55 +01004342 else if (eval_option(&varname, rettv, TRUE) == OK)
4343 // Local option
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004344 done = TRUE;
LemonBoy47d4e312022-05-04 18:12:55 +01004345
4346 curbuf = save_curbuf;
4347 }
4348 else if (*varname == NUL)
4349 {
4350 // Empty string: return a dict with all the local variables.
4351 if (htname == 'b')
4352 v = &buf->b_bufvar;
4353 else if (htname == 'w')
4354 v = &win->w_winvar;
4355 else
4356 v = &tp->tp_winvar;
4357 copy_tv(&v->di_tv, rettv);
4358 done = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004359 }
4360 else
4361 {
LemonBoy47d4e312022-05-04 18:12:55 +01004362 hashtab_T *ht;
4363
4364 if (htname == 'b')
4365 ht = &buf->b_vars->dv_hashtab;
4366 else if (htname == 'w')
4367 ht = &win->w_vars->dv_hashtab;
4368 else
4369 ht = &tp->tp_vars->dv_hashtab;
4370
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004371 // Look up the variable.
LemonBoy47d4e312022-05-04 18:12:55 +01004372 v = find_var_in_ht(ht, htname, varname, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004373 if (v != NULL)
4374 {
4375 copy_tv(&v->di_tv, rettv);
4376 done = TRUE;
4377 }
4378 }
4379 }
4380
4381 if (need_switch_win)
4382 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004383 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004384 }
4385
LemonBoy47d4e312022-05-04 18:12:55 +01004386 if (!done && deftv->v_type != VAR_UNKNOWN)
4387 // use the default value
4388 copy_tv(deftv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004389
4390 --emsg_off;
4391}
4392
4393/*
LemonBoy47d4e312022-05-04 18:12:55 +01004394 * getwinvar() and gettabwinvar()
4395 */
4396 static void
4397getwinvar(
4398 typval_T *argvars,
4399 typval_T *rettv,
4400 int off) // 1 for gettabwinvar()
4401{
4402 char_u *varname;
4403 tabpage_T *tp;
4404 win_T *win;
4405
4406 if (off == 1)
4407 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4408 else
4409 tp = curtab;
4410 win = find_win_by_nr(&argvars[off], tp);
4411 varname = tv_get_string_chk(&argvars[off + 1]);
4412
4413 get_var_from(varname, rettv, &argvars[off + 2], 'w', tp, win, NULL);
4414}
4415
4416/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02004417 * Set option "varname" to the value of "varp" for the current buffer/window.
4418 */
4419 static void
4420set_option_from_tv(char_u *varname, typval_T *varp)
4421{
4422 long numval = 0;
4423 char_u *strval;
4424 char_u nbuf[NUMBUFLEN];
4425 int error = FALSE;
4426
zeertzjq4c7cb372023-06-14 16:39:54 +01004427 int opt_idx = findoption(varname);
4428 if (opt_idx < 0)
4429 {
4430 semsg(_(e_unknown_option_str_2), varname);
4431 return;
4432 }
4433 int opt_p_flags = get_option_flags(opt_idx);
4434
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004435 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004436 {
zeertzjq4c7cb372023-06-14 16:39:54 +01004437 if (opt_p_flags & P_STRING)
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004438 {
4439 emsg(_(e_string_required));
4440 return;
4441 }
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004442 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004443 strval = (char_u *)"0"; // avoid using "false"
4444 }
4445 else
4446 {
zeertzjq4c7cb372023-06-14 16:39:54 +01004447 if ((opt_p_flags & (P_NUM|P_BOOL))
4448 && (!in_vim9script() || varp->v_type != VAR_STRING))
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004449 numval = (long)tv_get_number_chk(varp, &error);
zeertzjq4c7cb372023-06-14 16:39:54 +01004450 if (!error)
4451 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004452 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02004453 if (!error && strval != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004454 set_option_value_give_err(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar191929b2020-08-19 21:20:49 +02004455}
4456
4457/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004458 * "setwinvar()" and "settabwinvar()" functions
4459 */
4460 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004461setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004462{
4463 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004464 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004465 int need_switch_win;
4466 char_u *varname, *winvarname;
4467 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004468 tabpage_T *tp = NULL;
4469
4470 if (check_secure())
4471 return;
4472
4473 if (off == 1)
4474 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4475 else
4476 tp = curtab;
4477 win = find_win_by_nr(&argvars[off], tp);
4478 varname = tv_get_string_chk(&argvars[off + 1]);
4479 varp = &argvars[off + 2];
4480
zeertzjqea720ae2023-01-03 10:54:09 +00004481 if (win == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004482 return;
4483
4484 need_switch_win = !(tp == curtab && win == curwin);
4485 if (!need_switch_win
4486 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004487 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004488 if (*varname == '&')
4489 set_option_from_tv(varname + 1, varp);
4490 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004491 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004492 winvarname = alloc(STRLEN(varname) + 3);
4493 if (winvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004494 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004495 STRCPY(winvarname, "w:");
4496 STRCPY(winvarname + 2, varname);
4497 set_var(winvarname, varp, TRUE);
4498 vim_free(winvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004499 }
4500 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004501 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004502 if (need_switch_win)
4503 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004504}
4505
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004506/*
4507 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4508 * v:option_type, and v:option_command.
4509 */
4510 void
4511reset_v_option_vars(void)
4512{
4513 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4514 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4515 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4516 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4517 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4518 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4519}
4520
4521/*
4522 * Add an assert error to v:errors.
4523 */
4524 void
4525assert_error(garray_T *gap)
4526{
4527 struct vimvar *vp = &vimvars[VV_ERRORS];
4528
Bram Moolenaard787e402021-12-24 21:36:12 +00004529 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004530 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004531 set_vim_var_list(VV_ERRORS, list_alloc());
4532 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4533}
4534
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004535 int
4536var_exists(char_u *var)
4537{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004538 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004539 char_u *name;
4540 char_u *tofree;
4541 typval_T tv;
4542 int len = 0;
4543 int n = FALSE;
4544
4545 // get_name_len() takes care of expanding curly braces
4546 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004547 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004548 if (len > 0)
4549 {
4550 if (tofree != NULL)
4551 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004552 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004553 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004554 if (n)
4555 {
4556 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004557 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004558 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4559 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004560 if (n)
4561 clear_tv(&tv);
4562 }
4563 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004564 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004565 n = FALSE;
4566
4567 vim_free(tofree);
4568 return n;
4569}
4570
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004571static lval_T *redir_lval = NULL;
4572#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4573static garray_T redir_ga; // only valid when redir_lval is not NULL
4574static char_u *redir_endp = NULL;
4575static char_u *redir_varname = NULL;
4576
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004577 int
4578alloc_redir_lval(void)
4579{
4580 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4581 if (redir_lval == NULL)
4582 return FAIL;
4583 return OK;
4584}
4585
4586 void
4587clear_redir_lval(void)
4588{
4589 VIM_CLEAR(redir_lval);
4590}
4591
4592 void
4593init_redir_ga(void)
4594{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004595 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004596}
4597
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004598/*
4599 * Start recording command output to a variable
4600 * When "append" is TRUE append to an existing variable.
4601 * Returns OK if successfully completed the setup. FAIL otherwise.
4602 */
4603 int
4604var_redir_start(char_u *name, int append)
4605{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004606 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004607 typval_T tv;
4608
4609 // Catch a bad name early.
4610 if (!eval_isnamec1(*name))
4611 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004612 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004613 return FAIL;
4614 }
4615
4616 // Make a copy of the name, it is used in redir_lval until redir ends.
4617 redir_varname = vim_strsave(name);
4618 if (redir_varname == NULL)
4619 return FAIL;
4620
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004621 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004622 {
4623 var_redir_stop();
4624 return FAIL;
4625 }
4626
4627 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004628 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004629
4630 // Parse the variable name (can be a dict or list entry).
4631 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4632 FNE_CHECK_START);
4633 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4634 {
4635 clear_lval(redir_lval);
4636 if (redir_endp != NULL && *redir_endp != NUL)
4637 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004638 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004639 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004640 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004641 redir_endp = NULL; // don't store a value, only cleanup
4642 var_redir_stop();
4643 return FAIL;
4644 }
4645
4646 // check if we can write to the variable: set it to or append an empty
4647 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004648 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004649 tv.v_type = VAR_STRING;
4650 tv.vval.v_string = (char_u *)"";
4651 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004652 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004653 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004654 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004655 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004656 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004657 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004658 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004659 {
4660 redir_endp = NULL; // don't store a value, only cleanup
4661 var_redir_stop();
4662 return FAIL;
4663 }
4664
4665 return OK;
4666}
4667
4668/*
4669 * Append "value[value_len]" to the variable set by var_redir_start().
4670 * The actual appending is postponed until redirection ends, because the value
4671 * appended may in fact be the string we write to, changing it may cause freed
4672 * memory to be used:
4673 * :redir => foo
4674 * :let foo
4675 * :redir END
4676 */
4677 void
4678var_redir_str(char_u *value, int value_len)
4679{
4680 int len;
4681
4682 if (redir_lval == NULL)
4683 return;
4684
4685 if (value_len == -1)
4686 len = (int)STRLEN(value); // Append the entire string
4687 else
4688 len = value_len; // Append only "value_len" characters
4689
4690 if (ga_grow(&redir_ga, len) == OK)
4691 {
4692 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4693 redir_ga.ga_len += len;
4694 }
4695 else
4696 var_redir_stop();
4697}
4698
4699/*
4700 * Stop redirecting command output to a variable.
4701 * Frees the allocated memory.
4702 */
4703 void
4704var_redir_stop(void)
4705{
4706 typval_T tv;
4707
4708 if (EVALCMD_BUSY)
4709 {
4710 redir_lval = NULL;
4711 return;
4712 }
4713
4714 if (redir_lval != NULL)
4715 {
4716 // If there was no error: assign the text to the variable.
4717 if (redir_endp != NULL)
4718 {
4719 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4720 tv.v_type = VAR_STRING;
4721 tv.vval.v_string = redir_ga.ga_data;
4722 // Call get_lval() again, if it's inside a Dict or List it may
4723 // have changed.
4724 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4725 FALSE, FALSE, 0, FNE_CHECK_START);
4726 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004728 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004729 clear_lval(redir_lval);
4730 }
4731
4732 // free the collected output
4733 VIM_CLEAR(redir_ga.ga_data);
4734
4735 VIM_CLEAR(redir_lval);
4736 }
4737 VIM_CLEAR(redir_varname);
4738}
4739
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004740/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004741 * Get the collected redirected text and clear redir_ga.
4742 */
4743 char_u *
4744get_clear_redir_ga(void)
4745{
4746 char_u *res;
4747
4748 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4749 res = redir_ga.ga_data;
4750 redir_ga.ga_data = NULL;
4751 return res;
4752}
4753
4754/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004755 * "gettabvar()" function
4756 */
4757 void
4758f_gettabvar(typval_T *argvars, typval_T *rettv)
4759{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004760 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004761 tabpage_T *tp;
4762 win_T *win = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004763
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004764 if (in_vim9script()
4765 && (check_for_number_arg(argvars, 0) == FAIL
4766 || check_for_string_arg(argvars, 1) == FAIL))
4767 return;
4768
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004769 varname = tv_get_string_chk(&argvars[1]);
4770 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
LemonBoy47d4e312022-05-04 18:12:55 +01004771 if (tp != NULL)
4772 win = tp == curtab || tp->tp_firstwin == NULL ? firstwin
4773 : tp->tp_firstwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004774
LemonBoy47d4e312022-05-04 18:12:55 +01004775 get_var_from(varname, rettv, &argvars[2], 't', tp, win, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004776}
4777
4778/*
4779 * "gettabwinvar()" function
4780 */
4781 void
4782f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4783{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004784 if (in_vim9script()
4785 && (check_for_number_arg(argvars, 0) == FAIL
4786 || check_for_number_arg(argvars, 1) == FAIL
4787 || check_for_string_arg(argvars, 2) == FAIL))
4788 return;
4789
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004790 getwinvar(argvars, rettv, 1);
4791}
4792
4793/*
4794 * "getwinvar()" function
4795 */
4796 void
4797f_getwinvar(typval_T *argvars, typval_T *rettv)
4798{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004799 if (in_vim9script()
4800 && (check_for_number_arg(argvars, 0) == FAIL
4801 || check_for_string_arg(argvars, 1) == FAIL))
4802 return;
4803
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004804 getwinvar(argvars, rettv, 0);
4805}
4806
4807/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004808 * "getbufvar()" function
4809 */
4810 void
4811f_getbufvar(typval_T *argvars, typval_T *rettv)
4812{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004813 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004814 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004815
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004816 if (in_vim9script()
4817 && (check_for_buffer_arg(argvars, 0) == FAIL
4818 || check_for_string_arg(argvars, 1) == FAIL))
4819 return;
4820
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004821 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004822 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004823
LemonBoy47d4e312022-05-04 18:12:55 +01004824 get_var_from(varname, rettv, &argvars[2], 'b', curtab, curwin, buf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004825}
4826
4827/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004828 * "settabvar()" function
4829 */
4830 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004831f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004832{
4833 tabpage_T *save_curtab;
4834 tabpage_T *tp;
4835 char_u *varname, *tabvarname;
4836 typval_T *varp;
4837
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004838 if (check_secure())
4839 return;
4840
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004841 if (in_vim9script()
4842 && (check_for_number_arg(argvars, 0) == FAIL
4843 || check_for_string_arg(argvars, 1) == FAIL))
4844 return;
4845
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004846 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4847 varname = tv_get_string_chk(&argvars[1]);
4848 varp = &argvars[2];
4849
zeertzjqea720ae2023-01-03 10:54:09 +00004850 if (varname == NULL || tp == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004851 return;
4852
4853 save_curtab = curtab;
4854 goto_tabpage_tp(tp, FALSE, FALSE);
4855
4856 tabvarname = alloc(STRLEN(varname) + 3);
4857 if (tabvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004858 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004859 STRCPY(tabvarname, "t:");
4860 STRCPY(tabvarname + 2, varname);
4861 set_var(tabvarname, varp, TRUE);
4862 vim_free(tabvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004863 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004864
4865 // Restore current tabpage
4866 if (valid_tabpage(save_curtab))
4867 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004868}
4869
4870/*
4871 * "settabwinvar()" function
4872 */
4873 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004874f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004875{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004876 if (in_vim9script()
4877 && (check_for_number_arg(argvars, 0) == FAIL
4878 || check_for_number_arg(argvars, 1) == FAIL
4879 || check_for_string_arg(argvars, 2) == FAIL))
4880 return;
4881
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004882 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004883}
4884
4885/*
4886 * "setwinvar()" function
4887 */
4888 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004889f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004890{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004891 if (in_vim9script()
4892 && (check_for_number_arg(argvars, 0) == FAIL
4893 || check_for_string_arg(argvars, 1) == FAIL))
4894 return;
4895
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004896 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004897}
4898
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004899/*
4900 * "setbufvar()" function
4901 */
4902 void
4903f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4904{
4905 buf_T *buf;
4906 char_u *varname, *bufvarname;
4907 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004908
4909 if (check_secure())
4910 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004911
4912 if (in_vim9script()
4913 && (check_for_buffer_arg(argvars, 0) == FAIL
4914 || check_for_string_arg(argvars, 1) == FAIL))
4915 return;
4916
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004917 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004918 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004919 varp = &argvars[2];
4920
zeertzjqea720ae2023-01-03 10:54:09 +00004921 if (buf == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004922 return;
4923
4924 if (*varname == '&')
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004925 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004926 aco_save_T aco;
Christian Brabandtac4cffc2024-01-16 17:22:38 +01004927 // safe the current window position, it could
4928 // change because of 'scrollbind' window-local
4929 // options
4930 linenr_T old_topline = curwin->w_topline;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004931
4932 // Set curbuf to be our buf, temporarily.
4933 aucmd_prepbuf(&aco, buf);
4934 if (curbuf == buf)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004935 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004936 // Only when it worked to set "curbuf".
4937 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004938
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004939 // reset notion of buffer
4940 aucmd_restbuf(&aco);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004941 }
Christian Brabandtac4cffc2024-01-16 17:22:38 +01004942 curwin->w_topline = old_topline;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004943 }
4944 else
4945 {
4946 bufvarname = alloc(STRLEN(varname) + 3);
4947 if (bufvarname != NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004948 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004949 buf_T *save_curbuf = curbuf;
Bram Moolenaar86015452020-03-29 15:12:15 +02004950
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004951 curbuf = buf;
4952 STRCPY(bufvarname, "b:");
4953 STRCPY(bufvarname + 2, varname);
4954 set_var(bufvarname, varp, TRUE);
4955 vim_free(bufvarname);
4956 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004957 }
4958 }
4959}
4960
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004961/*
4962 * Get a callback from "arg". It can be a Funcref or a function name.
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004963 * When "arg" is zero "res.cb_name" is set to an empty string.
4964 * If "res.cb_name" is allocated then "res.cb_free_name" is set to TRUE.
4965 * "res.cb_name" is set to NULL for an invalid argument.
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004966 */
4967 callback_T
4968get_callback(typval_T *arg)
4969{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004970 callback_T res;
4971 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004972
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004973 CLEAR_FIELD(res);
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004974 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4975 {
4976 res.cb_partial = arg->vval.v_partial;
4977 ++res.cb_partial->pt_refcount;
4978 res.cb_name = partial_name(res.cb_partial);
4979 }
4980 else
4981 {
Bram Moolenaar14e579092020-03-07 16:59:25 +01004982 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
Keith Thompson184f71c2024-01-04 21:19:04 +01004983 && SAFE_isdigit(*arg->vval.v_string))
Bram Moolenaar14e579092020-03-07 16:59:25 +01004984 r = FAIL;
4985 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004986 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004987 res.cb_name = arg->vval.v_string;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004988 if (arg->v_type == VAR_STRING)
4989 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004990 char_u *name = get_scriptlocal_funcname(arg->vval.v_string);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004991 if (name != NULL)
4992 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004993 res.cb_name = name;
4994 res.cb_free_name = TRUE;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004995 }
4996 }
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004997 func_ref(res.cb_name);
4998 }
4999 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005000 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005001 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01005002 r = FAIL;
5003
5004 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005005 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005006 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005007 res.cb_name = NULL;
5008 }
5009 }
5010 return res;
5011}
5012
5013/*
5014 * Copy a callback into a typval_T.
5015 */
5016 void
5017put_callback(callback_T *cb, typval_T *tv)
5018{
5019 if (cb->cb_partial != NULL)
5020 {
5021 tv->v_type = VAR_PARTIAL;
5022 tv->vval.v_partial = cb->cb_partial;
5023 ++tv->vval.v_partial->pt_refcount;
5024 }
5025 else
5026 {
5027 tv->v_type = VAR_FUNC;
5028 tv->vval.v_string = vim_strsave(cb->cb_name);
5029 func_ref(cb->cb_name);
5030 }
5031}
5032
5033/*
5034 * Make a copy of "src" into "dest", allocating the function name if needed,
5035 * without incrementing the refcount.
5036 */
5037 void
5038set_callback(callback_T *dest, callback_T *src)
5039{
5040 if (src->cb_partial == NULL)
5041 {
5042 // just a function name, make a copy
5043 dest->cb_name = vim_strsave(src->cb_name);
5044 dest->cb_free_name = TRUE;
5045 }
5046 else
5047 {
5048 // cb_name is a pointer into cb_partial
5049 dest->cb_name = src->cb_name;
5050 dest->cb_free_name = FALSE;
5051 }
5052 dest->cb_partial = src->cb_partial;
5053}
5054
5055/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02005056 * Copy callback from "src" to "dest", incrementing the refcounts.
5057 */
5058 void
5059copy_callback(callback_T *dest, callback_T *src)
5060{
5061 dest->cb_partial = src->cb_partial;
5062 if (dest->cb_partial != NULL)
5063 {
5064 dest->cb_name = src->cb_name;
5065 dest->cb_free_name = FALSE;
5066 ++dest->cb_partial->pt_refcount;
5067 }
5068 else
5069 {
5070 dest->cb_name = vim_strsave(src->cb_name);
5071 dest->cb_free_name = TRUE;
5072 func_ref(src->cb_name);
5073 }
5074}
5075
5076/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005077 * When a callback refers to an autoload import, change the function name to
5078 * the "path#name" form. Uses the current script context.
5079 * Only works when the name is allocated.
5080 */
5081 void
5082expand_autload_callback(callback_T *cb)
5083{
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005084 char_u *name;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005085 char_u *p;
5086 imported_T *import;
5087
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005088 if (!in_vim9script() || cb->cb_name == NULL
5089 || (!cb->cb_free_name
5090 && (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005091 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005092 if (cb->cb_partial != NULL)
5093 name = cb->cb_partial->pt_name;
5094 else
5095 name = cb->cb_name;
5096 p = vim_strchr(name, '.');
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005097 if (p == NULL)
5098 return;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005099
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005100 import = find_imported(name, p - name, FALSE);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005101 if (import == NULL || !SCRIPT_ID_VALID(import->imp_sid))
5102 return;
5103
5104 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
5105 if (si->sn_autoload_prefix == NULL)
5106 return;
5107
5108 char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
5109 if (newname == NULL)
5110 return;
5111
5112 if (cb->cb_partial != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005113 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005114 if (cb->cb_name == cb->cb_partial->pt_name)
5115 cb->cb_name = newname;
5116 vim_free(cb->cb_partial->pt_name);
5117 cb->cb_partial->pt_name = newname;
5118 }
5119 else
5120 {
5121 vim_free(cb->cb_name);
5122 cb->cb_name = newname;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005123 }
5124}
5125
5126/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005127 * Unref/free "callback" returned by get_callback() or set_callback().
5128 */
5129 void
5130free_callback(callback_T *callback)
5131{
5132 if (callback->cb_partial != NULL)
5133 {
5134 partial_unref(callback->cb_partial);
5135 callback->cb_partial = NULL;
5136 }
5137 else if (callback->cb_name != NULL)
5138 func_unref(callback->cb_name);
5139 if (callback->cb_free_name)
5140 {
5141 vim_free(callback->cb_name);
5142 callback->cb_free_name = FALSE;
5143 }
5144 callback->cb_name = NULL;
5145}
5146
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005147#endif // FEAT_EVAL