blob: 9382842f37f46e6d6233b8bf31af627efdddcdf3 [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},
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100162 {VV_NAME("t_enum", VAR_NUMBER), NULL, VV_RO},
ichizok663d18d2025-01-02 18:06:00 +0100163 {VV_NAME("t_enumvalue", VAR_NUMBER), NULL, VV_RO},
zeertzjq6655bef2025-01-06 18:32:13 +0100164 {VV_NAME("stacktrace", VAR_LIST), &t_list_dict_any, VV_RO},
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100165 {VV_NAME("t_tuple", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200166};
167
168// shorthand
Bram Moolenaard787e402021-12-24 21:36:12 +0000169#define vv_tv_type vv_di.di_tv.v_type
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200170#define vv_nr vv_di.di_tv.vval.v_number
171#define vv_float vv_di.di_tv.vval.v_float
172#define vv_str vv_di.di_tv.vval.v_string
173#define vv_list vv_di.di_tv.vval.v_list
174#define vv_dict vv_di.di_tv.vval.v_dict
175#define vv_blob vv_di.di_tv.vval.v_blob
176#define vv_tv vv_di.di_tv
177
178static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200179static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200180#define vimvarht vimvardict.dv_hashtab
181
182// for VIM_VERSION_ defines
183#include "version.h"
184
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200185static void list_glob_vars(int *first);
186static void list_buf_vars(int *first);
187static void list_win_vars(int *first);
188static void list_tab_vars(int *first);
189static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100190static 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 +0200191static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
192static 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 +0200193static void list_one_var(dictitem_T *v, char *prefix, int *first);
194static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
195
196/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200197 * Initialize global and vim special variables
198 */
199 void
200evalvars_init(void)
201{
202 int i;
203 struct vimvar *p;
204
205 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
206 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
207 vimvardict.dv_lock = VAR_FIXED;
208 hash_init(&compat_hashtab);
209
210 for (i = 0; i < VV_LEN; ++i)
211 {
212 p = &vimvars[i];
213 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
214 {
Bram Moolenaar097c5372023-05-24 21:02:24 +0100215 iemsg("Name too long, increase size of dictitem16_T");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200216 getout(1);
217 }
218 STRCPY(p->vv_di.di_key, p->vv_name);
219 if (p->vv_flags & VV_RO)
220 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
221 else if (p->vv_flags & VV_RO_SBX)
222 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
223 else
224 p->vv_di.di_flags = DI_FLAGS_FIX;
225
226 // add to v: scope dict, unless the value is not always available
Bram Moolenaard787e402021-12-24 21:36:12 +0000227 if (p->vv_tv_type != VAR_UNKNOWN)
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000228 hash_add(&vimvarht, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200229 if (p->vv_flags & VV_COMPAT)
230 // add to compat scope dict
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000231 hash_add(&compat_hashtab, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200232 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200233 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
234 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200235
236 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
237 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100238 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200239 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
240 set_vim_var_list(VV_ERRORS, list_alloc());
241 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
242
243 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
244 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
245 set_vim_var_nr(VV_NONE, VVAL_NONE);
246 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100247 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
248 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100249 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaar69b30722021-11-02 21:39:49 +0000250 set_vim_var_nr(VV_SIZEOFINT, sizeof(int));
251 set_vim_var_nr(VV_SIZEOFLONG, sizeof(long));
252 set_vim_var_nr(VV_SIZEOFPOINTER, sizeof(char *));
naohiro ono56200ee2022-01-01 14:59:44 +0000253 set_vim_var_nr(VV_MAXCOL, MAXCOL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200254
255 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
256 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
257 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
258 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
259 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
260 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
261 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
262 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
263 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
264 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
265 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarc0c2c262023-01-12 21:08:53 +0000266 set_vim_var_nr(VV_TYPE_CLASS, VAR_TYPE_CLASS);
267 set_vim_var_nr(VV_TYPE_OBJECT, VAR_TYPE_OBJECT);
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200268 set_vim_var_nr(VV_TYPE_TYPEALIAS, VAR_TYPE_TYPEALIAS);
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100269 set_vim_var_nr(VV_TYPE_ENUM, VAR_TYPE_ENUM);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100270 set_vim_var_nr(VV_TYPE_ENUMVALUE, VAR_TYPE_ENUMVALUE);
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100271 set_vim_var_nr(VV_TYPE_TUPLE, VAR_TYPE_TUPLE);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200272
273 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
274
Drew Vogele30d1022021-10-24 20:35:07 +0100275 set_vim_var_dict(VV_COLORNAMES, dict_alloc());
276
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200277#ifdef FEAT_PYTHON3
278 set_vim_var_nr(VV_PYTHON3_VERSION, python3_version());
279#endif
280
Bram Moolenaar439c0362020-06-06 15:58:03 +0200281 // Default for v:register is not 0 but '"'. This is adjusted once the
282 // clipboard has been setup by calling reset_reg_var().
283 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200284}
285
286#if defined(EXITFREE) || defined(PROTO)
287/*
288 * Free all vim variables information on exit
289 */
290 void
291evalvars_clear(void)
292{
293 int i;
294 struct vimvar *p;
295
296 for (i = 0; i < VV_LEN; ++i)
297 {
298 p = &vimvars[i];
299 if (p->vv_di.di_tv.v_type == VAR_STRING)
300 VIM_CLEAR(p->vv_str);
301 else if (p->vv_di.di_tv.v_type == VAR_LIST)
302 {
303 list_unref(p->vv_list);
304 p->vv_list = NULL;
305 }
306 }
307 hash_clear(&vimvarht);
308 hash_init(&vimvarht); // garbage_collect() will access it
309 hash_clear(&compat_hashtab);
310
311 // global variables
312 vars_clear(&globvarht);
313
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100314 // Script-local variables. Clear all the variables here.
315 // The scriptvar_T is cleared later in free_scriptnames(), because a
316 // variable in one script might hold a reference to the whole scope of
317 // another script.
318 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200319 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200320}
321#endif
322
323 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200324garbage_collect_globvars(int copyID)
325{
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100326 return set_ref_in_ht(&globvarht, copyID, NULL, NULL);
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200327}
328
329 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200330garbage_collect_vimvars(int copyID)
331{
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100332 return set_ref_in_ht(&vimvarht, copyID, NULL, NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200333}
334
335 int
336garbage_collect_scriptvars(int copyID)
337{
Bram Moolenaared234f22020-10-15 20:42:20 +0200338 int i;
339 int idx;
340 int abort = FALSE;
341 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200342
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100343 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200344 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100345 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL, NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200346
Bram Moolenaared234f22020-10-15 20:42:20 +0200347 si = SCRIPT_ITEM(i);
348 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
349 {
350 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
351
Bram Moolenaard00a7fb2021-03-08 20:47:14 +0100352 if (sv->sv_name != NULL)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100353 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL, NULL);
Bram Moolenaared234f22020-10-15 20:42:20 +0200354 }
355 }
356
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200357 return abort;
358}
359
360/*
361 * Set an internal variable to a string value. Creates the variable if it does
362 * not already exist.
363 */
364 void
365set_internal_string_var(char_u *name, char_u *value)
366{
367 char_u *val;
368 typval_T *tvp;
369
370 val = vim_strsave(value);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000371 if (val == NULL)
372 return;
373
374 tvp = alloc_string_tv(val);
375 if (tvp == NULL)
376 return;
377
378 set_var(name, tvp, FALSE);
379 free_tv(tvp);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200380}
381
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200382 int
383eval_charconvert(
384 char_u *enc_from,
385 char_u *enc_to,
386 char_u *fname_from,
387 char_u *fname_to)
388{
389 int err = FALSE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000390 sctx_T saved_sctx = current_sctx;
391 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200392
393 set_vim_var_string(VV_CC_FROM, enc_from, -1);
394 set_vim_var_string(VV_CC_TO, enc_to, -1);
395 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
396 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000397 ctx = get_option_sctx("charconvert");
398 if (ctx != NULL)
399 current_sctx = *ctx;
400
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100401 if (eval_to_bool(p_ccv, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200402 err = TRUE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000403
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200404 set_vim_var_string(VV_CC_FROM, NULL, -1);
405 set_vim_var_string(VV_CC_TO, NULL, -1);
406 set_vim_var_string(VV_FNAME_IN, NULL, -1);
407 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000408 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200409
410 if (err)
411 return FAIL;
412 return OK;
413}
414
415# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
416 int
417eval_printexpr(char_u *fname, char_u *args)
418{
419 int err = FALSE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000420 sctx_T saved_sctx = current_sctx;
421 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200422
423 set_vim_var_string(VV_FNAME_IN, fname, -1);
424 set_vim_var_string(VV_CMDARG, args, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000425 ctx = get_option_sctx("printexpr");
426 if (ctx != NULL)
427 current_sctx = *ctx;
428
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100429 if (eval_to_bool(p_pexpr, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200430 err = TRUE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000431
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200432 set_vim_var_string(VV_FNAME_IN, NULL, -1);
433 set_vim_var_string(VV_CMDARG, NULL, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000434 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200435
436 if (err)
437 {
438 mch_remove(fname);
439 return FAIL;
440 }
441 return OK;
442}
443# endif
444
445# if defined(FEAT_DIFF) || defined(PROTO)
446 void
447eval_diff(
448 char_u *origfile,
449 char_u *newfile,
450 char_u *outfile)
451{
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000452 sctx_T saved_sctx = current_sctx;
453 sctx_T *ctx;
454 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200455
456 set_vim_var_string(VV_FNAME_IN, origfile, -1);
457 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
458 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000459
460 ctx = get_option_sctx("diffexpr");
461 if (ctx != NULL)
462 current_sctx = *ctx;
463
464 // errors are ignored
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100465 tv = eval_expr_ext(p_dex, NULL, TRUE);
Bram Moolenaar39b89442022-01-22 18:21:36 +0000466 free_tv(tv);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000467
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200468 set_vim_var_string(VV_FNAME_IN, NULL, -1);
469 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
470 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000471 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200472}
473
474 void
475eval_patch(
476 char_u *origfile,
477 char_u *difffile,
478 char_u *outfile)
479{
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000480 sctx_T saved_sctx = current_sctx;
481 sctx_T *ctx;
482 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200483
484 set_vim_var_string(VV_FNAME_IN, origfile, -1);
485 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
486 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000487
488 ctx = get_option_sctx("patchexpr");
489 if (ctx != NULL)
490 current_sctx = *ctx;
491
492 // errors are ignored
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100493 tv = eval_expr_ext(p_pex, NULL, TRUE);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000494 free_tv(tv);
495
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200496 set_vim_var_string(VV_FNAME_IN, NULL, -1);
497 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
498 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000499 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200500}
501# endif
502
503#if defined(FEAT_SPELL) || defined(PROTO)
504/*
505 * Evaluate an expression to a list with suggestions.
506 * For the "expr:" part of 'spellsuggest'.
507 * Returns NULL when there is an error.
508 */
509 list_T *
510eval_spell_expr(char_u *badword, char_u *expr)
511{
512 typval_T save_val;
513 typval_T rettv;
514 list_T *list = NULL;
515 char_u *p = skipwhite(expr);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000516 sctx_T saved_sctx = current_sctx;
517 sctx_T *ctx;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100518 int r;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200519
520 // Set "v:val" to the bad word.
521 prepare_vimvar(VV_VAL, &save_val);
522 set_vim_var_string(VV_VAL, badword, -1);
523 if (p_verbose == 0)
524 ++emsg_off;
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000525 ctx = get_option_sctx("spellsuggest");
526 if (ctx != NULL)
527 current_sctx = *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200528
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100529 r = may_call_simple_func(p, &rettv);
530 if (r == NOTDONE)
531 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
532 if (r == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200533 {
534 if (rettv.v_type != VAR_LIST)
535 clear_tv(&rettv);
536 else
537 list = rettv.vval.v_list;
538 }
539
540 if (p_verbose == 0)
541 --emsg_off;
542 clear_tv(get_vim_var_tv(VV_VAL));
543 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000544 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200545
546 return list;
547}
548
549/*
550 * "list" is supposed to contain two items: a word and a number. Return the
551 * word in "pp" and the number as the return value.
552 * Return -1 if anything isn't right.
553 * Used to get the good word and score from the eval_spell_expr() result.
554 */
555 int
556get_spellword(list_T *list, char_u **pp)
557{
558 listitem_T *li;
559
560 li = list->lv_first;
561 if (li == NULL)
562 return -1;
563 *pp = tv_get_string(&li->li_tv);
564
565 li = li->li_next;
566 if (li == NULL)
567 return -1;
568 return (int)tv_get_number(&li->li_tv);
569}
570#endif
571
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200572/*
573 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200574 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200575 * When not used yet add the variable to the v: hashtable.
576 */
577 void
578prepare_vimvar(int idx, typval_T *save_tv)
579{
580 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200581 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaard787e402021-12-24 21:36:12 +0000582 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000583 hash_add(&vimvarht, vimvars[idx].vv_di.di_key, "prepare vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200584}
585
586/*
587 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200588 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200589 * When no longer defined, remove the variable from the v: hashtable.
590 */
591 void
592restore_vimvar(int idx, typval_T *save_tv)
593{
594 hashitem_T *hi;
595
596 vimvars[idx].vv_tv = *save_tv;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000597 if (vimvars[idx].vv_tv_type != VAR_UNKNOWN)
598 return;
599
600 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
601 if (HASHITEM_EMPTY(hi))
602 internal_error("restore_vimvar()");
603 else
604 hash_remove(&vimvarht, hi, "restore vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200605}
606
607/*
608 * List Vim variables.
609 */
610 static void
611list_vim_vars(int *first)
612{
613 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
614}
615
616/*
617 * List script-local variables, if there is a script.
618 */
619 static void
620list_script_vars(int *first)
621{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200622 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200623 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
624 "s:", FALSE, first);
625}
626
627/*
Bram Moolenaar9510d222022-09-11 15:14:05 +0100628 * Return TRUE if "name" starts with "g:", "w:", "t:" or "b:".
629 * But only when an identifier character follows.
630 */
631 int
632is_scoped_variable(char_u *name)
633{
634 return vim_strchr((char_u *)"gwbt", name[0]) != NULL
635 && name[1] == ':'
636 && eval_isnamec(name[2]);
637}
638
639/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100640 * Evaluate one Vim expression {expr} in string "p" and append the
641 * resulting string to "gap". "p" points to the opening "{".
Bram Moolenaar70c41242022-05-10 18:11:43 +0100642 * When "evaluate" is FALSE only skip over the expression.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100643 * Return a pointer to the character after "}", NULL for an error.
644 */
645 char_u *
Bram Moolenaar70c41242022-05-10 18:11:43 +0100646eval_one_expr_in_str(char_u *p, garray_T *gap, int evaluate)
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100647{
648 char_u *block_start = skipwhite(p + 1); // skip the opening {
649 char_u *block_end = block_start;
650 char_u *expr_val;
651
652 if (*block_start == NUL)
653 {
654 semsg(_(e_missing_close_curly_str), p);
655 return NULL;
656 }
657 if (skip_expr(&block_end, NULL) == FAIL)
658 return NULL;
659 block_end = skipwhite(block_end);
660 if (*block_end != '}')
661 {
662 semsg(_(e_missing_close_curly_str), p);
663 return NULL;
664 }
Bram Moolenaar70c41242022-05-10 18:11:43 +0100665 if (evaluate)
666 {
667 *block_end = NUL;
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200668 expr_val = eval_to_string(block_start, FALSE, FALSE);
Bram Moolenaar70c41242022-05-10 18:11:43 +0100669 *block_end = '}';
670 if (expr_val == NULL)
671 return NULL;
672 ga_concat(gap, expr_val);
673 vim_free(expr_val);
674 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100675
676 return block_end + 1;
677}
678
679/*
680 * Evaluate all the Vim expressions {expr} in "str" and return the resulting
681 * string in allocated memory. "{{" is reduced to "{" and "}}" to "}".
682 * Used for a heredoc assignment.
683 * Returns NULL for an error.
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100684 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +0100685 static char_u *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100686eval_all_expr_in_str(char_u *str)
687{
688 garray_T ga;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100689 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100690
691 ga_init2(&ga, 1, 80);
692 p = str;
693
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100694 while (*p != NUL)
695 {
LemonBoy2eaef102022-05-06 13:14:50 +0100696 char_u *lit_start;
LemonBoy2eaef102022-05-06 13:14:50 +0100697 int escaped_brace = FALSE;
698
699 // Look for a block start.
700 lit_start = p;
701 while (*p != '{' && *p != '}' && *p != NUL)
702 ++p;
703
704 if (*p != NUL && *p == p[1])
705 {
706 // Escaped brace, unescape and continue.
707 // Include the brace in the literal string.
708 ++p;
709 escaped_brace = TRUE;
710 }
711 else if (*p == '}')
712 {
713 semsg(_(e_stray_closing_curly_str), str);
714 ga_clear(&ga);
715 return NULL;
716 }
717
718 // Append the literal part.
719 ga_concat_len(&ga, lit_start, (size_t)(p - lit_start));
720
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100721 if (*p == NUL)
LemonBoy2eaef102022-05-06 13:14:50 +0100722 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100723
LemonBoy2eaef102022-05-06 13:14:50 +0100724 if (escaped_brace)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100725 {
LemonBoy2eaef102022-05-06 13:14:50 +0100726 // Skip the second brace.
727 ++p;
728 continue;
729 }
730
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100731 // Evaluate the expression and append the result.
Bram Moolenaar70c41242022-05-10 18:11:43 +0100732 p = eval_one_expr_in_str(p, &ga, TRUE);
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100733 if (p == NULL)
LemonBoy2eaef102022-05-06 13:14:50 +0100734 {
735 ga_clear(&ga);
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100736 return NULL;
737 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100738 }
739 ga_append(&ga, NUL);
740
741 return ga.ga_data;
742}
743
744/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200745 * Get a list of lines from a HERE document. The here document is a list of
746 * lines surrounded by a marker.
747 * cmd << {marker}
748 * {line1}
749 * {line2}
750 * ....
751 * {marker}
752 *
753 * The {marker} is a string. If the optional 'trim' word is supplied before the
754 * marker, then the leading indentation before the lines (matching the
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100755 * indentation in the "cmd" line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200756 *
757 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100758 * tcl, mzscheme), "script_get" is set to TRUE. In this case, if the marker is
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200759 * missing, then '.' is accepted as a marker.
760 *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100761 * When compiling a heredoc assignment to a variable in a Vim9 def function,
762 * "vim9compile" is set to TRUE. In this case, instead of generating a list of
763 * string values from the heredoc, vim9 instructions are generated. On success
764 * the returned list will be empty.
765 *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100766 * Returns a List with {lines} or NULL on failure.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200767 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100768 list_T *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100769heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200770{
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100771 char_u *theline = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200772 char_u *marker;
773 list_T *l;
774 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100775 char_u *str;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200776 int marker_indent_len = 0;
777 int text_indent_len = 0;
778 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200779 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200780 int comment_char = in_vim9script() ? '#' : '"';
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100781 int evalstr = FALSE;
782 int eval_failed = FALSE;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100783 cctx_T *cctx = vim9compile ? eap->cookie : NULL;
784 int count = 0;
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200785 int heredoc_in_string = FALSE;
786 char_u *line_arg = NULL;
zeertzjq1f5175d2024-04-13 17:52:26 +0200787 char_u *nl_ptr = vim_strchr(cmd, '\n');
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200788
zeertzjq1f5175d2024-04-13 17:52:26 +0200789 if (nl_ptr != NULL)
790 {
791 heredoc_in_string = TRUE;
792 line_arg = nl_ptr + 1;
793 *nl_ptr = NUL;
794 }
795 else if (eap->ea_getline == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200796 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000797 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200798 return NULL;
799 }
800
801 // Check for the optional 'trim' word before the marker
802 cmd = skipwhite(cmd);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200803
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100804 while (TRUE)
805 {
806 if (STRNCMP(cmd, "trim", 4) == 0
807 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200808 {
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100809 cmd = skipwhite(cmd + 4);
810
811 // Trim the indentation from all the lines in the here document.
812 // The amount of indentation trimmed is the same as the indentation
813 // of the first line after the :let command line. To find the end
814 // marker the indent of the :let command line is trimmed.
815 p = *eap->cmdlinep;
816 while (VIM_ISWHITE(*p))
817 {
818 p++;
819 marker_indent_len++;
820 }
821 text_indent_len = -1;
822
823 continue;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200824 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100825 if (STRNCMP(cmd, "eval", 4) == 0
826 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
827 {
828 cmd = skipwhite(cmd + 4);
829 evalstr = TRUE;
830 continue;
831 }
832 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200833 }
834
835 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200836 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200837 {
838 marker = skipwhite(cmd);
zeertzjq1f5175d2024-04-13 17:52:26 +0200839 p = skiptowhite(marker);
840 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200841 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000842 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200843 return NULL;
844 }
845 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200846 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200847 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000848 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200849 return NULL;
850 }
851 }
852 else
853 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200854 // When getting lines for an embedded script, if the marker is missing,
855 // accept '.' as the marker.
856 if (script_get)
857 marker = dot;
858 else
859 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000860 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200861 return NULL;
862 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200863 }
864
865 l = list_alloc();
866 if (l == NULL)
867 return NULL;
868
869 for (;;)
870 {
871 int mi = 0;
872 int ti = 0;
873
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200874 if (heredoc_in_string)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200875 {
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200876 char_u *next_line;
877
878 // heredoc in a string separated by newlines. Get the next line
879 // from the string.
880
881 if (*line_arg == NUL)
882 {
883 semsg(_(e_missing_end_marker_str), marker);
884 break;
885 }
886
887 theline = line_arg;
888 next_line = vim_strchr(theline, '\n');
889 if (next_line == NULL)
890 line_arg += STRLEN(line_arg);
891 else
892 {
893 *next_line = NUL;
894 line_arg = next_line + 1;
895 }
896 }
897 else
898 {
899 vim_free(theline);
900 theline = eap->ea_getline(NUL, eap->cookie, 0, FALSE);
901 if (theline == NULL)
902 {
903 semsg(_(e_missing_end_marker_str), marker);
904 break;
905 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200906 }
907
908 // with "trim": skip the indent matching the :let line to find the
909 // marker
910 if (marker_indent_len > 0
911 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
912 mi = marker_indent_len;
913 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200914 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200915
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100916 // If expression evaluation failed in the heredoc, then skip till the
917 // end marker.
918 if (eval_failed)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100919 continue;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100920
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200921 if (text_indent_len == -1 && *theline != NUL)
922 {
923 // set the text indent from the first line.
924 p = theline;
925 text_indent_len = 0;
926 while (VIM_ISWHITE(*p))
927 {
928 p++;
929 text_indent_len++;
930 }
931 text_indent = vim_strnsave(theline, text_indent_len);
932 }
933 // with "trim": skip the indent matching the first line
934 if (text_indent != NULL)
935 for (ti = 0; ti < text_indent_len; ++ti)
936 if (theline[ti] != text_indent[ti])
937 break;
938
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100939 str = theline + ti;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100940 if (vim9compile)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100941 {
LemonBoy2eaef102022-05-06 13:14:50 +0100942 if (compile_all_expr_in_str(str, evalstr, cctx) == FAIL)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100943 {
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100944 vim_free(theline);
945 vim_free(text_indent);
946 return FAIL;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100947 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100948 count++;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100949 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100950 else
951 {
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200952 int free_str = FALSE;
953
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100954 if (evalstr && !eap->skip)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100955 {
956 str = eval_all_expr_in_str(str);
957 if (str == NULL)
958 {
959 // expression evaluation failed
960 eval_failed = TRUE;
961 continue;
962 }
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200963 free_str = TRUE;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100964 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100965
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100966 if (list_append_string(l, str, -1) == FAIL)
967 break;
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200968 if (free_str)
969 vim_free(str);
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100970 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200971 }
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200972 if (heredoc_in_string)
973 // Next command follows the heredoc in the string.
974 eap->nextcmd = line_arg;
975 else
976 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200977 vim_free(text_indent);
978
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100979 if (vim9compile && cctx->ctx_skip != SKIP_YES && !eval_failed)
980 generate_NEWLIST(cctx, count, FALSE);
981
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100982 if (eval_failed)
983 {
984 // expression evaluation in the heredoc failed
985 list_free(l);
986 return NULL;
987 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200988 return l;
989}
990
991/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200992 * Vim9 variable declaration:
993 * ":var name"
994 * ":var name: type"
995 * ":var name = expr"
996 * ":var name: type = expr"
997 * etc.
998 */
999 void
1000ex_var(exarg_T *eap)
1001{
Bram Moolenaar330a3882022-03-05 11:05:57 +00001002 char_u *p = eap->cmd;
Bram Moolenaare1d12112022-03-05 11:37:48 +00001003 int has_var;
Bram Moolenaar330a3882022-03-05 11:05:57 +00001004
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001005 if (!in_vim9script())
1006 {
1007 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
1008 return;
1009 }
Bram Moolenaare1d12112022-03-05 11:37:48 +00001010 has_var = checkforcmd_noparen(&p, "var", 3);
1011 if (current_sctx.sc_sid == 0 && has_var)
Bram Moolenaar0e1574c2022-03-03 17:05:35 +00001012 {
1013 emsg(_(e_cannot_declare_variable_on_command_line));
1014 return;
1015 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001016 ex_let(eap);
1017}
1018
1019/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001020 * ":let" list all variable values
1021 * ":let var1 var2" list variable values
1022 * ":let var = expr" assignment command.
1023 * ":let var += expr" assignment command.
1024 * ":let var -= expr" assignment command.
1025 * ":let var *= expr" assignment command.
1026 * ":let var /= expr" assignment command.
1027 * ":let var %= expr" assignment command.
1028 * ":let var .= expr" assignment command.
1029 * ":let var ..= expr" assignment command.
1030 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001031 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +01001032 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +02001033 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001034 * ":final var = expr" assignment command.
1035 * ":final [var1, var2] = expr" unpack list.
1036 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001037 * ":const" list all variable values
1038 * ":const var1 var2" list variable values
1039 * ":const var = expr" assignment command.
1040 * ":const [var1, var2] = expr" unpack list.
1041 */
1042 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +02001043ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001044{
1045 char_u *arg = eap->arg;
1046 char_u *expr = NULL;
1047 typval_T rettv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001048 int var_count = 0;
1049 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001050 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001051 char_u *argend;
1052 int first = TRUE;
1053 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001054 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001055 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001056 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001057
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001058 if (eap->cmdidx == CMD_final && !vim9script)
1059 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001060 // In legacy Vim script ":final" is short for ":finally".
1061 ex_finally(eap);
1062 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001063 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +02001064 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001065 {
1066 emsg(_(e_cannot_use_let_in_vim9_script));
1067 return;
1068 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001069
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001070 if (eap->cmdidx == CMD_const)
1071 flags |= ASSIGN_CONST;
1072 else if (eap->cmdidx == CMD_final)
1073 flags |= ASSIGN_FINAL;
1074
1075 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001077 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001078
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001079 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001080 if (argend == NULL)
1081 return;
1082 if (argend > arg && argend[-1] == '.') // for var.='str'
1083 --argend;
1084 expr = skipwhite(argend);
1085 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02001086 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001087 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +02001088 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
1089 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +02001090 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001091 {
1092 // ":let" without "=": list variables
1093 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001094 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001095 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001096 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001097 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001098 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001099 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001100 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001101 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001102 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001103 else
1104 // Vim9 declaration ":var name: type"
1105 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001106 }
1107 else
1108 {
1109 // ":let var1 var2" - list values
1110 arg = list_arg_vars(eap, arg, &first);
1111 }
1112 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001113 else if (!eap->skip)
1114 {
1115 // ":let"
1116 list_glob_vars(&first);
1117 list_buf_vars(&first);
1118 list_win_vars(&first);
1119 list_tab_vars(&first);
1120 list_script_vars(&first);
1121 list_func_vars(&first);
1122 list_vim_vars(&first);
1123 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02001124 set_nextcmd(eap, arg);
zeertzjq474891b2023-04-12 21:36:03 +01001125 return;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001126 }
zeertzjq474891b2023-04-12 21:36:03 +01001127
1128 if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001129 {
Bram Moolenaard9876422022-10-12 12:58:54 +01001130 list_T *l = NULL;
Bram Moolenaar81530e32021-07-28 21:25:49 +02001131 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001132
Bram Moolenaard9876422022-10-12 12:58:54 +01001133 // :let text =<< [trim] [eval] END
1134 // :var text =<< [trim] [eval] END
1135 if (vim9script && !eap->skip && (!VIM_ISWHITE(expr[-1])
1136 || !IS_WHITE_OR_NUL(expr[3])))
1137 semsg(_(e_white_space_required_before_and_after_str_at_str),
1138 "=<<", expr);
1139 else
1140 l = heredoc_get(eap, expr + 3, FALSE, FALSE);
1141
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001142 if (l != NULL)
1143 {
1144 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001145 if (!eap->skip)
1146 {
Bram Moolenaar81530e32021-07-28 21:25:49 +02001147 // errors are for the assignment, not the end marker
1148 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001149 op[0] = '=';
1150 op[1] = NUL;
1151 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001152 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001153 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001154 clear_tv(&rettv);
1155 }
zeertzjq474891b2023-04-12 21:36:03 +01001156 return;
1157 }
1158
1159 evalarg_T evalarg;
1160 int len = 1;
1161
1162 CLEAR_FIELD(rettv);
1163
1164 int cur_lnum;
1165
1166 op[0] = '=';
1167 op[1] = NUL;
1168 if (*expr != '=')
1169 {
1170 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
1171 {
1172 // +=, /=, etc. require an existing variable
1173 semsg(_(e_cannot_use_operator_on_new_variable_str), eap->arg);
1174 }
1175 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
1176 {
1177 op[0] = *expr; // +=, -=, *=, /=, %= or .=
1178 ++len;
1179 if (expr[0] == '.' && expr[1] == '.') // ..=
1180 {
1181 ++expr;
1182 ++len;
1183 }
1184 }
1185 expr += 2;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001186 }
1187 else
zeertzjq474891b2023-04-12 21:36:03 +01001188 ++expr;
1189
1190 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
1191 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001192 {
zeertzjq474891b2023-04-12 21:36:03 +01001193 vim_strncpy(op, expr - len, len);
1194 semsg(_(e_white_space_required_before_and_after_str_at_str),
1195 op, argend);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001196 }
zeertzjq474891b2023-04-12 21:36:03 +01001197
1198 if (eap->skip)
1199 ++emsg_skip;
1200 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
1201 expr = skipwhite_and_linebreak(expr, &evalarg);
1202 cur_lnum = SOURCING_LNUM;
1203 int eval_res = eval0(expr, &rettv, eap, &evalarg);
1204 if (eap->skip)
1205 --emsg_skip;
1206 clear_evalarg(&evalarg, eap);
1207
1208 // Restore the line number so that any type error is given for the
1209 // declaration, not the expression.
1210 SOURCING_LNUM = cur_lnum;
1211
1212 if (!eap->skip && eval_res != FAIL)
1213 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
1214 flags, op);
1215 if (eval_res != FAIL)
1216 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001217}
1218
1219/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001220 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001221 * Handles both "var" with any type and "[var, var; var]" with a list type.
1222 * When "op" is not NULL it points to a string with characters that
1223 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1224 * or concatenate.
1225 * Returns OK or FAIL;
1226 */
1227 int
1228ex_let_vars(
1229 char_u *arg_start,
1230 typval_T *tv,
1231 int copy, // copy values from "tv", don't move
1232 int semicolon, // from skip_var_list()
1233 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001234 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001235 char_u *op)
1236{
1237 char_u *arg = arg_start;
1238 list_T *l;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001239 tuple_T *tuple = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001240 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001241 int var_idx = 0;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001242 listitem_T *item = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001243 typval_T ltv;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001244 int is_list = tv->v_type == VAR_LIST;
1245 int idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001246
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00001247 if (tv->v_type == VAR_VOID)
1248 {
1249 emsg(_(e_cannot_use_void_value));
1250 return FAIL;
1251 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001252 if (*arg != '[')
1253 {
1254 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001255 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001256 return FAIL;
1257 return OK;
1258 }
1259
1260 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001261 // or
1262 // ":let [v1, v2] = tuple" or ":for [v1, v2] in tupletuple"
1263 if (tv->v_type != VAR_LIST && tv->v_type != VAR_TUPLE)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001264 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001265 emsg(_(e_list_or_tuple_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001266 return FAIL;
1267 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001268 if (is_list)
1269 {
1270 l = tv->vval.v_list;
1271 if (l == NULL)
1272 {
1273 emsg(_(e_list_required));
1274 return FAIL;
1275 }
1276 i = list_len(l);
1277 }
1278 else
1279 {
1280 tuple = tv->vval.v_tuple;
1281 if (tuple == NULL)
1282 {
1283 emsg(_(e_tuple_required));
1284 return FAIL;
1285 }
1286 i = tuple_len(tuple);
1287 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001288
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001289 if (semicolon == 0 && var_count < i)
1290 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001291 emsg(_(is_list ? e_less_targets_than_list_items
1292 : e_less_targets_than_tuple_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001293 return FAIL;
1294 }
1295 if (var_count - semicolon > i)
1296 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001297 emsg(_(is_list ? e_more_targets_than_list_items
1298 : e_more_targets_than_tuple_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001299 return FAIL;
1300 }
1301
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001302 if (is_list)
1303 {
1304 CHECK_LIST_MATERIALIZE(l);
1305 item = l->lv_first;
1306 }
1307 else
1308 idx = 0;
1309
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001310 while (*arg != ']')
1311 {
1312 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001313 ++var_idx;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001314 arg = ex_let_one(arg, is_list ? &item->li_tv : TUPLE_ITEM(tuple, idx),
1315 TRUE, flags | ASSIGN_UNPACK, (char_u *)",;]", op,
1316 var_idx);
1317 if (is_list)
1318 item = item->li_next;
1319 else
1320 idx++;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001321 if (arg == NULL)
1322 return FAIL;
1323
1324 arg = skipwhite(arg);
1325 if (*arg == ';')
1326 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001327 // Put the rest of the list or tuple (may be empty) in the var
1328 // after ';'. Create a new list or tuple for this.
1329 if (is_list)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001330 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001331 // Put the rest of the list (may be empty) in the var
1332 // after ';'. Create a new list for this.
1333 l = list_alloc();
1334 if (l == NULL)
1335 return FAIL;
1336
1337 // list
1338 while (item != NULL)
1339 {
1340 list_append_tv(l, &item->li_tv);
1341 item = item->li_next;
1342 }
1343
1344 ltv.v_type = VAR_LIST;
1345 ltv.v_lock = 0;
1346 ltv.vval.v_list = l;
1347 l->lv_refcount = 1;
1348 }
1349 else
1350 {
1351 tuple_T *new_tuple = tuple_alloc();
1352 if (new_tuple == NULL)
1353 return FAIL;
1354
1355 // Put the rest of the tuple (may be empty) in the var
1356 // after ';'. Create a new tuple for this.
1357 while (idx < TUPLE_LEN(tuple))
1358 {
1359 typval_T new_tv;
1360
1361 copy_tv(TUPLE_ITEM(tuple, idx), &new_tv);
1362 if (tuple_append_tv(new_tuple, &new_tv) == FAIL)
1363 return FAIL;
1364 idx++;
1365 }
1366
1367 ltv.v_type = VAR_TUPLE;
1368 ltv.v_lock = 0;
1369 ltv.vval.v_tuple = new_tuple;
1370 new_tuple->tv_refcount = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001371 }
1372
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001373 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001374 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001375 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001376 clear_tv(&ltv);
1377 if (arg == NULL)
1378 return FAIL;
1379 break;
1380 }
1381 else if (*arg != ',' && *arg != ']')
1382 {
1383 internal_error("ex_let_vars()");
1384 return FAIL;
1385 }
1386 }
1387
1388 return OK;
1389}
1390
1391/*
1392 * Skip over assignable variable "var" or list of variables "[var, var]".
1393 * Used for ":let varvar = expr" and ":for varvar in expr".
1394 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001395 * for "[var, var; var]" set "semicolon" to 1.
1396 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001397 * Return NULL for an error.
1398 */
1399 char_u *
1400skip_var_list(
1401 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001403 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001404 int *semicolon,
1405 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001406{
1407 char_u *p, *s;
1408
1409 if (*arg == '[')
1410 {
1411 // "[var, var]": find the matching ']'.
1412 p = arg;
1413 for (;;)
1414 {
1415 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001416 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001417 if (s == p)
1418 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001419 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001420 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001421 return NULL;
1422 }
1423 ++*var_count;
1424
1425 p = skipwhite(s);
1426 if (*p == ']')
1427 break;
1428 else if (*p == ';')
1429 {
1430 if (*semicolon == 1)
1431 {
Bram Moolenaar8b716f52022-02-15 21:17:56 +00001432 if (!silent)
1433 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001434 return NULL;
1435 }
1436 *semicolon = 1;
1437 }
1438 else if (*p != ',')
1439 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001440 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001441 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001442 return NULL;
1443 }
1444 }
1445 return p + 1;
1446 }
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01001447
Bram Moolenaare8e369a2022-09-21 18:59:14 +01001448 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001449}
1450
1451/*
1452 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1453 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001455 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001456 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001458{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001459 char_u *end;
1460 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001462 if (*arg == '@' && arg[1] != NUL)
1463 return arg + 2;
Bram Moolenaar1573e732022-11-16 20:33:21 +00001464
1465 // termcap option name may have non-alpha characters
1466 if (STRNCMP(arg, "&t_", 3) == 0 && arg[3] != NUL && arg[4] != NUL)
1467 return arg + 5;
1468
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001470 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001471
1472 // "a: type" is declaring variable "a" with a type, not "a:".
1473 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001474 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001475 --end;
1476
Bram Moolenaar585587d2021-01-17 20:52:13 +01001477 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478 {
Bram Moolenaarce93d162023-01-30 21:12:34 +00001479 if (*skipwhite(end) == ':')
1480 end = skip_type(skipwhite(skipwhite(end) + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001481 }
1482 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001483}
1484
1485/*
1486 * List variables for hashtab "ht" with prefix "prefix".
1487 * If "empty" is TRUE also list NULL strings as empty strings.
1488 */
1489 void
1490list_hashtable_vars(
1491 hashtab_T *ht,
1492 char *prefix,
1493 int empty,
1494 int *first)
1495{
1496 hashitem_T *hi;
1497 dictitem_T *di;
1498 int todo;
1499 char_u buf[IOSIZE];
1500
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001501 int save_ht_flags = ht->ht_flags;
1502 ht->ht_flags |= HTFLAGS_FROZEN;
1503
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001504 todo = (int)ht->ht_used;
1505 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1506 {
1507 if (!HASHITEM_EMPTY(hi))
1508 {
1509 --todo;
1510 di = HI2DI(hi);
1511
1512 // apply :filter /pat/ to variable name
1513 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1514 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1515 if (message_filtered(buf))
1516 continue;
1517
1518 if (empty || di->di_tv.v_type != VAR_STRING
1519 || di->di_tv.vval.v_string != NULL)
1520 list_one_var(di, prefix, first);
1521 }
1522 }
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001523
1524 ht->ht_flags = save_ht_flags;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001525}
1526
1527/*
1528 * List global variables.
1529 */
1530 static void
1531list_glob_vars(int *first)
1532{
1533 list_hashtable_vars(&globvarht, "", TRUE, first);
1534}
1535
1536/*
1537 * List buffer variables.
1538 */
1539 static void
1540list_buf_vars(int *first)
1541{
1542 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1543}
1544
1545/*
1546 * List window variables.
1547 */
1548 static void
1549list_win_vars(int *first)
1550{
1551 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1552}
1553
1554/*
1555 * List tab page variables.
1556 */
1557 static void
1558list_tab_vars(int *first)
1559{
1560 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1561}
1562
1563/*
1564 * List variables in "arg".
1565 */
1566 static char_u *
1567list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1568{
1569 int error = FALSE;
1570 int len;
1571 char_u *name;
1572 char_u *name_start;
1573 char_u *arg_subsc;
1574 char_u *tofree;
1575 typval_T tv;
1576
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001577 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001578 {
1579 if (error || eap->skip)
1580 {
1581 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1582 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1583 {
1584 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001585 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001586 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001587 break;
1588 }
1589 }
1590 else
1591 {
1592 // get_name_len() takes care of expanding curly braces
1593 name_start = name = arg;
1594 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1595 if (len <= 0)
1596 {
1597 // This is mainly to keep test 49 working: when expanding
1598 // curly braces fails overrule the exception error message.
1599 if (len < 0 && !aborting())
1600 {
1601 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001602 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001603 break;
1604 }
1605 error = TRUE;
1606 }
1607 else
1608 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001609 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001610 if (tofree != NULL)
1611 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001612 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001613 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001614 error = TRUE;
1615 else
1616 {
1617 // handle d.key, l[idx], f(expr)
1618 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001619 if (handle_subscript(&arg, name_start, &tv,
1620 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001621 error = TRUE;
1622 else
1623 {
1624 if (arg == arg_subsc && len == 2 && name[1] == ':')
1625 {
1626 switch (*name)
1627 {
1628 case 'g': list_glob_vars(first); break;
1629 case 'b': list_buf_vars(first); break;
1630 case 'w': list_win_vars(first); break;
1631 case 't': list_tab_vars(first); break;
1632 case 'v': list_vim_vars(first); break;
1633 case 's': list_script_vars(first); break;
1634 case 'l': list_func_vars(first); break;
1635 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001636 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001637 }
1638 }
1639 else
1640 {
1641 char_u numbuf[NUMBUFLEN];
1642 char_u *tf;
1643 int c;
1644 char_u *s;
1645
1646 s = echo_string(&tv, &tf, numbuf, 0);
1647 c = *arg;
1648 *arg = NUL;
1649 list_one_var_a("",
1650 arg == arg_subsc ? name : name_start,
1651 tv.v_type,
1652 s == NULL ? (char_u *)"" : s,
1653 first);
1654 *arg = c;
1655 vim_free(tf);
1656 }
1657 clear_tv(&tv);
1658 }
1659 }
1660 }
1661
1662 vim_free(tofree);
1663 }
1664
1665 arg = skipwhite(arg);
1666 }
1667
1668 return arg;
1669}
1670
1671/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001672 * Set an environment variable, part of ex_let_one().
1673 */
1674 static char_u *
1675ex_let_env(
1676 char_u *arg,
1677 typval_T *tv,
1678 int flags,
1679 char_u *endchars,
1680 char_u *op)
1681{
1682 char_u *arg_end = NULL;
1683 char_u *name;
1684 int len;
1685
1686 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1687 && (flags & ASSIGN_FOR_LOOP) == 0)
1688 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001689 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001690 return NULL;
1691 }
1692
1693 // Find the end of the name.
1694 ++arg;
1695 name = arg;
1696 len = get_env_len(&arg);
1697 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001698 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001699 else
1700 {
1701 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001702 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001703 else if (endchars != NULL
1704 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1705 emsg(_(e_unexpected_characters_in_let));
1706 else if (!check_secure())
1707 {
1708 char_u *tofree = NULL;
1709 int c1 = name[len];
1710 char_u *p;
1711
1712 name[len] = NUL;
1713 p = tv_get_string_chk(tv);
1714 if (p != NULL && op != NULL && *op == '.')
1715 {
1716 int mustfree = FALSE;
1717 char_u *s = vim_getenv(name, &mustfree);
1718
1719 if (s != NULL)
1720 {
1721 p = tofree = concat_str(s, p);
1722 if (mustfree)
1723 vim_free(s);
1724 }
1725 }
1726 if (p != NULL)
1727 {
1728 vim_setenv_ext(name, p);
1729 arg_end = arg;
1730 }
1731 name[len] = c1;
1732 vim_free(tofree);
1733 }
1734 }
1735 return arg_end;
1736}
1737
1738/*
1739 * Set an option, part of ex_let_one().
1740 */
1741 static char_u *
1742ex_let_option(
1743 char_u *arg,
1744 typval_T *tv,
1745 int flags,
1746 char_u *endchars,
1747 char_u *op)
1748{
1749 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001750 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001751 char_u *arg_end = NULL;
1752
1753 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1754 && (flags & ASSIGN_FOR_LOOP) == 0)
1755 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001756 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001757 return NULL;
1758 }
1759
1760 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001761 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001762 if (p == NULL || (endchars != NULL
1763 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001764 {
zeertzjq4c7cb372023-06-14 16:39:54 +01001765 emsg(_(e_unexpected_characters_in_let));
1766 return NULL;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001767 }
zeertzjq4c7cb372023-06-14 16:39:54 +01001768
1769 int c1;
1770 long n = 0;
1771 getoption_T opt_type;
1772 long numval;
1773 char_u *stringval = NULL;
1774 char_u *s = NULL;
1775 int failed = FALSE;
1776 int opt_p_flags;
1777 char_u *tofree = NULL;
1778 char_u numbuf[NUMBUFLEN];
1779
1780 c1 = *p;
1781 *p = NUL;
1782
1783 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags, scope);
1784 if (opt_type == gov_unknown && arg[0] != 't' && arg[1] != '_')
1785 {
1786 semsg(_(e_unknown_option_str_2), arg);
1787 goto theend;
1788 }
1789 if (op != NULL && *op != '='
1790 && (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1791 || (opt_type == gov_string && *op != '.')))
1792 {
1793 semsg(_(e_wrong_variable_type_for_str_equal), op);
1794 goto theend;
1795 }
1796
1797 if ((opt_type == gov_bool
1798 || opt_type == gov_number
1799 || opt_type == gov_hidden_bool
1800 || opt_type == gov_hidden_number)
1801 && (tv->v_type != VAR_STRING || !in_vim9script()))
1802 {
1803 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1804 // bool, possibly hidden
1805 n = (long)tv_get_bool_chk(tv, &failed);
1806 else
1807 // number, possibly hidden
1808 n = (long)tv_get_number_chk(tv, &failed);
1809 if (failed)
1810 goto theend;
1811 }
1812
1813 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
1814 || tv->v_type == VAR_FUNC))
1815 {
1816 // If the option can be set to a function reference or a lambda
1817 // and the passed value is a function reference, then convert it to
1818 // the name (string) of the function reference.
1819 s = tv2string(tv, &tofree, numbuf, 0);
1820 if (s == NULL)
1821 goto theend;
1822 }
1823 // Avoid setting a string option to the text "v:false" or similar.
1824 // In Vim9 script also don't convert a number to string.
1825 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1826 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1827 {
1828 s = tv_get_string_chk(tv);
1829 if (s == NULL)
1830 goto theend;
1831 }
1832 else if (opt_type == gov_string || opt_type == gov_hidden_string)
1833 {
1834 emsg(_(e_string_required));
1835 goto theend;
1836 }
1837
1838 if (op != NULL && *op != '=')
1839 {
1840 // number, in legacy script also bool
1841 if (opt_type == gov_number
1842 || (opt_type == gov_bool && !in_vim9script()))
1843 {
1844 switch (*op)
1845 {
1846 case '+': n = numval + n; break;
1847 case '-': n = numval - n; break;
1848 case '*': n = numval * n; break;
1849 case '/': n = (long)num_divide(numval, n, &failed); break;
1850 case '%': n = (long)num_modulus(numval, n, &failed); break;
1851 }
1852 s = NULL;
1853 if (failed)
1854 goto theend;
1855 }
1856 else if (opt_type == gov_string && stringval != NULL && s != NULL)
1857 {
1858 // string
1859 s = concat_str(stringval, s);
1860 vim_free(stringval);
1861 stringval = s;
1862 }
1863 }
1864
1865 char *err = set_option_value(arg, n, s, scope);
1866 arg_end = p;
1867 if (err != NULL)
1868 emsg(_(err));
1869
1870theend:
1871 *p = c1;
1872 vim_free(stringval);
1873 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001874 return arg_end;
1875}
1876
1877/*
1878 * Set a register, part of ex_let_one().
1879 */
1880 static char_u *
1881ex_let_register(
1882 char_u *arg,
1883 typval_T *tv,
1884 int flags,
1885 char_u *endchars,
1886 char_u *op)
1887{
1888 char_u *arg_end = NULL;
1889
1890 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1891 && (flags & ASSIGN_FOR_LOOP) == 0)
1892 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001893 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001894 return NULL;
1895 }
1896 ++arg;
1897 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001898 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001899 else if (endchars != NULL
1900 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1901 emsg(_(e_unexpected_characters_in_let));
1902 else
1903 {
1904 char_u *ptofree = NULL;
1905 char_u *p;
1906
1907 p = tv_get_string_chk(tv);
1908 if (p != NULL && op != NULL && *op == '.')
1909 {
1910 char_u *s = get_reg_contents(*arg == '@'
1911 ? '"' : *arg, GREG_EXPR_SRC);
1912
1913 if (s != NULL)
1914 {
1915 p = ptofree = concat_str(s, p);
1916 vim_free(s);
1917 }
1918 }
1919 if (p != NULL)
1920 {
1921 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1922 arg_end = arg + 1;
1923 }
1924 vim_free(ptofree);
1925 }
1926 return arg_end;
1927}
1928
1929/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001930 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1931 * Returns a pointer to the char just after the var name.
1932 * Returns NULL if there is an error.
1933 */
1934 static char_u *
1935ex_let_one(
1936 char_u *arg, // points to variable name
1937 typval_T *tv, // value to assign to variable
1938 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001939 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001940 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001941 char_u *op, // "+", "-", "." or NULL
1942 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001943{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001944 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001945
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001946 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001947 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001948 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1949 {
1950 vim9_declare_error(arg);
1951 return NULL;
1952 }
1953
Ernie Rael9ed53752023-12-11 17:40:46 +01001954 if (check_typval_is_value(tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001955 return NULL;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001956
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001957 if (*arg == '$')
1958 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001959 // ":let $VAR = expr": Set environment variable.
1960 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001961 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001962 else if (*arg == '&')
1963 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001964 // ":let &option = expr": Set option value.
1965 // ":let &l:option = expr": Set local option value.
1966 // ":let &g:option = expr": Set global option value.
1967 // ":for &ts in range(8)": Set option value for for loop
1968 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001969 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001970 else if (*arg == '@')
1971 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001972 // ":let @r = expr": Set register contents.
1973 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001974 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001975 else if (eval_isnamec1(*arg) || *arg == '{')
1976 {
1977 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001978 char_u *p;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001979 int lval_flags = (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1980 ? GLV_NO_DECL : 0;
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02001981 lval_flags |= (flags & ASSIGN_FOR_LOOP) ? GLV_FOR_LOOP : 0;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001982 if (op != NULL && *op != '=')
1983 lval_flags |= GLV_ASSIGN_WITH_OP;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001984
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001985 // ":let var = expr": Set internal variable.
1986 // ":let var: type = expr": Set internal variable with type.
1987 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001988 p = get_lval(arg, tv, &lv, FALSE, FALSE, lval_flags, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001989 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001990 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 if (endchars != NULL && vim_strchr(endchars,
1992 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001993 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001994 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001995 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001996 else
1997 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001998 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001999 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002000 }
2001 }
2002 clear_lval(&lv);
2003 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002004 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002005 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002006
2007 return arg_end;
2008}
2009
2010/*
2011 * ":unlet[!] var1 ... " command.
2012 */
2013 void
2014ex_unlet(exarg_T *eap)
2015{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002016 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002017}
2018
2019/*
2020 * ":lockvar" and ":unlockvar" commands
2021 */
2022 void
2023ex_lockvar(exarg_T *eap)
2024{
2025 char_u *arg = eap->arg;
2026 int deep = 2;
2027
2028 if (eap->forceit)
2029 deep = -1;
2030 else if (vim_isdigit(*arg))
2031 {
2032 deep = getdigits(&arg);
2033 arg = skipwhite(arg);
2034 }
2035
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002036 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002037}
2038
2039/*
2040 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002041 * Also used for Vim9 script. "callback" is invoked as:
2042 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002043 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002044 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002045ex_unletlock(
2046 exarg_T *eap,
2047 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002048 int deep,
2049 int glv_flags,
2050 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
2051 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002052{
2053 char_u *arg = argstart;
2054 char_u *name_end;
2055 int error = FALSE;
2056 lval_T lv;
2057
2058 do
2059 {
2060 if (*arg == '$')
2061 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002062 lv.ll_name = arg;
2063 lv.ll_tv = NULL;
2064 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002065 if (get_env_len(&arg) == 0)
2066 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002067 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002068 return;
2069 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002070 if (!error && !eap->skip
2071 && callback(&lv, arg, eap, deep, cookie) == FAIL)
2072 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002073 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002074 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002075 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002076 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002077 // Parse the name and find the end.
2078 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01002079 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002080 if (lv.ll_name == NULL)
2081 error = TRUE; // error but continue parsing
2082 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
2083 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002084 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002085 if (name_end != NULL)
2086 {
2087 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00002088 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002089 }
2090 if (!(eap->skip || error))
2091 clear_lval(&lv);
2092 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002093 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002094
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002095 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002096 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002097 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002098
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002099 if (!eap->skip)
2100 clear_lval(&lv);
2101 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002102
2103 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002104 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002105
Bram Moolenaar63b91732021-08-05 20:40:03 +02002106 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002107}
2108
2109 static int
2110do_unlet_var(
2111 lval_T *lp,
2112 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002113 exarg_T *eap,
2114 int deep UNUSED,
2115 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002116{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002117 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002118 int ret = OK;
2119 int cc;
2120
2121 if (lp->ll_tv == NULL)
2122 {
2123 cc = *name_end;
2124 *name_end = NUL;
2125
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002126 // Environment variable, normal name or expanded name.
2127 if (*lp->ll_name == '$')
LemonBoy77142312022-04-15 20:50:46 +01002128 vim_unsetenv_ext(lp->ll_name + 1);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002129 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002130 ret = FAIL;
2131 *name_end = cc;
2132 }
2133 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002134 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002135 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002136 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002137 return FAIL;
2138 else if (lp->ll_range)
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002139 list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_n1,
2140 !lp->ll_empty2, lp->ll_n2);
2141 else if (lp->ll_list != NULL)
2142 // unlet a List item.
2143 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002144 else
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002145 // unlet a Dictionary item.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002146 dictitem_remove(lp->ll_dict, lp->ll_di, "unlet");
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002147
2148 return ret;
2149}
2150
2151/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002152 * Unlet one item or a range of items from a list.
2153 * Return OK or FAIL.
2154 */
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002155 void
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002156list_unlet_range(
2157 list_T *l,
2158 listitem_T *li_first,
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002159 long n1_arg,
2160 int has_n2,
2161 long n2)
2162{
2163 listitem_T *li = li_first;
2164 int n1 = n1_arg;
2165
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002166 // Delete a range of List items.
2167 li = li_first;
2168 n1 = n1_arg;
2169 while (li != NULL && (!has_n2 || n2 >= n1))
2170 {
2171 listitem_T *next = li->li_next;
2172
2173 listitem_remove(l, li);
2174 li = next;
2175 ++n1;
2176 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002177}
2178/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002179 * "unlet" a variable. Return OK if it existed, FAIL if not.
2180 * When "forceit" is TRUE don't complain if the variable doesn't exist.
2181 */
2182 int
2183do_unlet(char_u *name, int forceit)
2184{
2185 hashtab_T *ht;
2186 hashitem_T *hi;
mityu8be36ee2022-05-25 17:29:46 +01002187 char_u *varname = NULL; // init to shut up gcc
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002188 dict_T *d;
2189 dictitem_T *di;
2190
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002191 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002192 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002193 return FAIL;
2194
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002195 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002196
2197 // can't :unlet a script variable in Vim9 script from a function
2198 if (ht == get_script_local_ht()
2199 && SCRIPT_ID_VALID(current_sctx.sc_sid)
2200 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2201 == SCRIPT_VERSION_VIM9
2202 && check_vim9_unlet(name) == FAIL)
2203 return FAIL;
2204
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002205 if (ht != NULL && *varname != NUL)
2206 {
2207 d = get_current_funccal_dict(ht);
2208 if (d == NULL)
2209 {
2210 if (ht == &globvarht)
2211 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002212 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002213 d = &vimvardict;
2214 else
2215 {
2216 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2217 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2218 }
2219 if (d == NULL)
2220 {
2221 internal_error("do_unlet()");
2222 return FAIL;
2223 }
2224 }
2225 hi = hash_find(ht, varname);
2226 if (HASHITEM_EMPTY(hi))
2227 hi = find_hi_in_scoped_ht(name, &ht);
2228 if (hi != NULL && !HASHITEM_EMPTY(hi))
2229 {
2230 di = HI2DI(hi);
2231 if (var_check_fixed(di->di_flags, name, FALSE)
2232 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002233 || value_check_lock(d->dv_lock, name, FALSE)
2234 || check_hashtab_frozen(ht, "unlet"))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002235 return FAIL;
2236
2237 delete_var(ht, hi);
2238 return OK;
2239 }
2240 }
2241 if (forceit)
2242 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00002243 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002244 return FAIL;
2245}
2246
Ernie Raelee865f32023-09-29 19:53:55 +02002247 static void
2248report_lockvar_member(char *msg, lval_T *lp)
2249{
2250 int did_alloc = FALSE;
2251 char_u *vname = (char_u *)"";
2252 char_u *class_name = lp->ll_class != NULL
2253 ? lp->ll_class->class_name : (char_u *)"";
2254 if (lp->ll_name != NULL)
2255 {
2256 if (lp->ll_name_end == NULL)
2257 vname = lp->ll_name;
2258 else
2259 {
2260 vname = vim_strnsave(lp->ll_name, lp->ll_name_end - lp->ll_name);
2261 if (vname == NULL)
2262 return;
2263 did_alloc = TRUE;
2264 }
2265 }
2266 semsg(_(msg), vname, class_name);
2267 if (did_alloc)
2268 vim_free(vname);
2269}
2270
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002271/*
2272 * Lock or unlock variable indicated by "lp".
2273 * "deep" is the levels to go (-1 for unlimited);
2274 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2275 */
2276 static int
2277do_lock_var(
2278 lval_T *lp,
2279 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002280 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002281 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002282 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002283{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002284 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002285 int ret = OK;
2286 int cc;
2287 dictitem_T *di;
2288
Ernie Raelee865f32023-09-29 19:53:55 +02002289#ifdef LOG_LOCKVAR
2290 ch_log(NULL, "LKVAR: do_lock_var(): name %s, is_root %d", lp->ll_name, lp->ll_is_root);
2291#endif
2292
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002293 if (lp->ll_tv == NULL)
2294 {
2295 cc = *name_end;
2296 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002297 if (*lp->ll_name == '$')
2298 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002299 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002300 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002301 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002302 else
2303 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002304 // Normal name or expanded name.
2305 di = find_var(lp->ll_name, NULL, TRUE);
2306 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002307 {
2308 if (in_vim9script())
2309 semsg(_(e_cannot_find_variable_to_unlock_str),
2310 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002311 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002312 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002313 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002314 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002315 if ((di->di_flags & DI_FLAGS_FIX)
2316 && di->di_tv.v_type != VAR_DICT
2317 && di->di_tv.v_type != VAR_LIST)
2318 {
2319 // For historic reasons this error is not given for a list
2320 // or dict. E.g., the b: dict could be locked/unlocked.
2321 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
2322 ret = FAIL;
2323 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002324 else
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002325 {
2326 if (in_vim9script())
2327 {
2328 svar_T *sv = find_typval_in_script(&di->di_tv,
2329 0, FALSE);
2330
2331 if (sv != NULL && sv->sv_const != 0)
2332 {
2333 semsg(_(e_cannot_change_readonly_variable_str),
2334 lp->ll_name);
2335 ret = FAIL;
2336 }
2337 }
2338
2339 if (ret == OK)
2340 {
2341 if (lock)
2342 di->di_flags |= DI_FLAGS_LOCK;
2343 else
2344 di->di_flags &= ~DI_FLAGS_LOCK;
2345 if (deep != 0)
2346 item_lock(&di->di_tv, deep, lock, FALSE);
2347 }
2348 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002349 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002350 }
2351 *name_end = cc;
2352 }
Ernie Raelee865f32023-09-29 19:53:55 +02002353 else if (deep == 0 && lp->ll_object == NULL && lp->ll_class == NULL)
Bram Moolenaara187c432020-09-16 21:08:28 +02002354 {
2355 // nothing to do
2356 }
Ernie Raelee865f32023-09-29 19:53:55 +02002357 else if (lp->ll_is_root)
2358 // (un)lock the item.
2359 item_lock(lp->ll_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002360 else if (lp->ll_range)
2361 {
2362 listitem_T *li = lp->ll_li;
2363
2364 // (un)lock a range of List items.
2365 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2366 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02002367 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002368 li = li->li_next;
2369 ++lp->ll_n1;
2370 }
2371 }
2372 else if (lp->ll_list != NULL)
2373 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002374 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Ernie Raelee865f32023-09-29 19:53:55 +02002375 else if (lp->ll_object != NULL) // This check must be before ll_class.
2376 {
2377 // (un)lock an object variable.
2378 report_lockvar_member(e_cannot_lock_object_variable_str, lp);
2379 ret = FAIL;
2380 }
2381 else if (lp->ll_class != NULL)
2382 {
2383 // (un)lock a class variable.
2384 report_lockvar_member(e_cannot_lock_class_variable_str, lp);
2385 ret = FAIL;
2386 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002387 else
Ernie Raelee865f32023-09-29 19:53:55 +02002388 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002389 // (un)lock a Dictionary item.
Ernie Raelee865f32023-09-29 19:53:55 +02002390 if (lp->ll_di == NULL)
2391 {
2392 emsg(_(e_dictionary_required));
2393 ret = FAIL;
2394 }
2395 else
2396 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
2397 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002398
2399 return ret;
2400}
2401
2402/*
2403 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002404 * When "check_refcount" is TRUE do not lock a list or dict with a reference
2405 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002406 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002407 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02002408item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002409{
2410 static int recurse = 0;
2411 list_T *l;
2412 listitem_T *li;
2413 dict_T *d;
2414 blob_T *b;
2415 hashitem_T *hi;
2416 int todo;
2417
Ernie Raelee865f32023-09-29 19:53:55 +02002418#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02002419 ch_log(NULL, "LKVAR: item_lock(): type %s", vartype_name(tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02002420#endif
2421
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002422 if (recurse >= DICT_MAXNEST)
2423 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002424 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002425 return;
2426 }
2427 if (deep == 0)
2428 return;
2429 ++recurse;
2430
2431 // lock/unlock the item itself
2432 if (lock)
2433 tv->v_lock |= VAR_LOCKED;
2434 else
2435 tv->v_lock &= ~VAR_LOCKED;
2436
2437 switch (tv->v_type)
2438 {
2439 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002440 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002441 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002442 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002444 case VAR_STRING:
2445 case VAR_FUNC:
2446 case VAR_PARTIAL:
2447 case VAR_FLOAT:
2448 case VAR_SPECIAL:
2449 case VAR_JOB:
2450 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002451 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002452 case VAR_CLASS:
2453 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002454 case VAR_TYPEALIAS:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002455 break;
2456
2457 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002458 if ((b = tv->vval.v_blob) != NULL
2459 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002460 {
2461 if (lock)
2462 b->bv_lock |= VAR_LOCKED;
2463 else
2464 b->bv_lock &= ~VAR_LOCKED;
2465 }
2466 break;
2467 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002468 if ((l = tv->vval.v_list) != NULL
2469 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002470 {
2471 if (lock)
2472 l->lv_lock |= VAR_LOCKED;
2473 else
2474 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002475 if (deep < 0 || deep > 1)
2476 {
2477 if (l->lv_first == &range_list_item)
2478 l->lv_lock |= VAR_ITEMS_LOCKED;
2479 else
2480 {
2481 // recursive: lock/unlock the items the List contains
2482 CHECK_LIST_MATERIALIZE(l);
2483 FOR_ALL_LIST_ITEMS(l, li) item_lock(&li->li_tv,
2484 deep - 1, lock, check_refcount);
2485 }
2486 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002487 }
2488 break;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002489 case VAR_TUPLE:
2490 tuple_lock(tv->vval.v_tuple, deep, lock, check_refcount);
2491 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002492 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002493 if ((d = tv->vval.v_dict) != NULL
2494 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002495 {
2496 if (lock)
2497 d->dv_lock |= VAR_LOCKED;
2498 else
2499 d->dv_lock &= ~VAR_LOCKED;
2500 if (deep < 0 || deep > 1)
2501 {
2502 // recursive: lock/unlock the items the List contains
2503 todo = (int)d->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002504 FOR_ALL_HASHTAB_ITEMS(&d->dv_hashtab, hi, todo)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002505 {
2506 if (!HASHITEM_EMPTY(hi))
2507 {
2508 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002509 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2510 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002511 }
2512 }
2513 }
2514 }
2515 }
2516 --recurse;
2517}
2518
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002519#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2520/*
2521 * Delete all "menutrans_" variables.
2522 */
2523 void
2524del_menutrans_vars(void)
2525{
2526 hashitem_T *hi;
2527 int todo;
2528
2529 hash_lock(&globvarht);
2530 todo = (int)globvarht.ht_used;
2531 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2532 {
2533 if (!HASHITEM_EMPTY(hi))
2534 {
2535 --todo;
2536 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2537 delete_var(&globvarht, hi);
2538 }
2539 }
2540 hash_unlock(&globvarht);
2541}
2542#endif
2543
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002544/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002545 * Local string buffer for the next two functions to store a variable name
2546 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2547 * get_user_var_name().
2548 */
2549
2550static char_u *varnamebuf = NULL;
2551static int varnamebuflen = 0;
2552
2553/*
2554 * Function to concatenate a prefix and a variable name.
2555 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002556 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002557cat_prefix_varname(int prefix, char_u *name)
2558{
2559 int len;
2560
2561 len = (int)STRLEN(name) + 3;
2562 if (len > varnamebuflen)
2563 {
2564 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002565 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002566 varnamebuf = alloc(len);
2567 if (varnamebuf == NULL)
2568 {
2569 varnamebuflen = 0;
2570 return NULL;
2571 }
2572 varnamebuflen = len;
2573 }
2574 *varnamebuf = prefix;
2575 varnamebuf[1] = ':';
2576 STRCPY(varnamebuf + 2, name);
2577 return varnamebuf;
2578}
2579
2580/*
2581 * Function given to ExpandGeneric() to obtain the list of user defined
2582 * (global/buffer/window/built-in) variable names.
2583 */
2584 char_u *
2585get_user_var_name(expand_T *xp, int idx)
2586{
2587 static long_u gdone;
2588 static long_u bdone;
2589 static long_u wdone;
2590 static long_u tdone;
2591 static int vidx;
2592 static hashitem_T *hi;
2593 hashtab_T *ht;
2594
2595 if (idx == 0)
2596 {
2597 gdone = bdone = wdone = vidx = 0;
2598 tdone = 0;
2599 }
2600
2601 // Global variables
2602 if (gdone < globvarht.ht_used)
2603 {
2604 if (gdone++ == 0)
2605 hi = globvarht.ht_array;
2606 else
2607 ++hi;
2608 while (HASHITEM_EMPTY(hi))
2609 ++hi;
2610 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2611 return cat_prefix_varname('g', hi->hi_key);
2612 return hi->hi_key;
2613 }
2614
2615 // b: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002616 ht = &prevwin_curwin()->w_buffer->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002617 if (bdone < ht->ht_used)
2618 {
2619 if (bdone++ == 0)
2620 hi = ht->ht_array;
2621 else
2622 ++hi;
2623 while (HASHITEM_EMPTY(hi))
2624 ++hi;
2625 return cat_prefix_varname('b', hi->hi_key);
2626 }
2627
2628 // w: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002629 ht = &prevwin_curwin()->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002630 if (wdone < ht->ht_used)
2631 {
2632 if (wdone++ == 0)
2633 hi = ht->ht_array;
2634 else
2635 ++hi;
2636 while (HASHITEM_EMPTY(hi))
2637 ++hi;
2638 return cat_prefix_varname('w', hi->hi_key);
2639 }
2640
2641 // t: variables
2642 ht = &curtab->tp_vars->dv_hashtab;
2643 if (tdone < ht->ht_used)
2644 {
2645 if (tdone++ == 0)
2646 hi = ht->ht_array;
2647 else
2648 ++hi;
2649 while (HASHITEM_EMPTY(hi))
2650 ++hi;
2651 return cat_prefix_varname('t', hi->hi_key);
2652 }
2653
2654 // v: variables
2655 if (vidx < VV_LEN)
2656 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2657
2658 VIM_CLEAR(varnamebuf);
2659 varnamebuflen = 0;
2660 return NULL;
2661}
2662
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002663 char *
2664get_var_special_name(int nr)
2665{
2666 switch (nr)
2667 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002668 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2669 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002670 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002671 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002672 }
2673 internal_error("get_var_special_name()");
2674 return "42";
2675}
2676
2677/*
2678 * Returns the global variable dictionary
2679 */
2680 dict_T *
2681get_globvar_dict(void)
2682{
2683 return &globvardict;
2684}
2685
2686/*
2687 * Returns the global variable hash table
2688 */
2689 hashtab_T *
2690get_globvar_ht(void)
2691{
2692 return &globvarht;
2693}
2694
2695/*
2696 * Returns the v: variable dictionary
2697 */
2698 dict_T *
2699get_vimvar_dict(void)
2700{
2701 return &vimvardict;
2702}
2703
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002704/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002706 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707 */
2708 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002709find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002711 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2712 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713
2714 if (di == NULL)
2715 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002716 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002717 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2718 return (int)(vv - vimvars);
2719}
2720
2721
2722/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002723 * Set type of v: variable to "type".
2724 */
2725 void
2726set_vim_var_type(int idx, vartype_T type)
2727{
Bram Moolenaard787e402021-12-24 21:36:12 +00002728 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002729}
2730
2731/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002732 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002733 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002734 */
2735 void
2736set_vim_var_nr(int idx, varnumber_T val)
2737{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002738 vimvars[idx].vv_nr = val;
2739}
2740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 char *
2742get_vim_var_name(int idx)
2743{
2744 return vimvars[idx].vv_name;
2745}
2746
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002747/*
2748 * Get typval_T v: variable value.
2749 */
2750 typval_T *
2751get_vim_var_tv(int idx)
2752{
2753 return &vimvars[idx].vv_tv;
2754}
2755
Bram Moolenaard787e402021-12-24 21:36:12 +00002756 type_T *
2757get_vim_var_type(int idx, garray_T *type_list)
2758{
2759 if (vimvars[idx].vv_type != NULL)
2760 return vimvars[idx].vv_type;
2761 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2762}
2763
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002764/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002765 * Set v: variable to "tv". Only accepts the same type.
2766 * Takes over the value of "tv".
2767 */
2768 int
2769set_vim_var_tv(int idx, typval_T *tv)
2770{
Bram Moolenaard787e402021-12-24 21:36:12 +00002771 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002772 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002773 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002774 clear_tv(tv);
2775 return FAIL;
2776 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002777 // VV_RO is also checked when compiling, but let's check here as well.
2778 if (vimvars[idx].vv_flags & VV_RO)
2779 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002780 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002781 return FAIL;
2782 }
2783 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2784 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002785 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002786 return FAIL;
2787 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002788 clear_tv(&vimvars[idx].vv_di.di_tv);
2789 vimvars[idx].vv_di.di_tv = *tv;
2790 return OK;
2791}
2792
2793/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002794 * Get number v: variable value.
2795 */
2796 varnumber_T
2797get_vim_var_nr(int idx)
2798{
2799 return vimvars[idx].vv_nr;
2800}
2801
2802/*
2803 * Get string v: variable value. Uses a static buffer, can only be used once.
2804 * If the String variable has never been set, return an empty string.
2805 * Never returns NULL;
2806 */
2807 char_u *
2808get_vim_var_str(int idx)
2809{
2810 return tv_get_string(&vimvars[idx].vv_tv);
2811}
2812
2813/*
2814 * Get List v: variable value. Caller must take care of reference count when
2815 * needed.
2816 */
2817 list_T *
2818get_vim_var_list(int idx)
2819{
2820 return vimvars[idx].vv_list;
2821}
2822
2823/*
2824 * Get Dict v: variable value. Caller must take care of reference count when
2825 * needed.
2826 */
2827 dict_T *
2828get_vim_var_dict(int idx)
2829{
2830 return vimvars[idx].vv_dict;
2831}
2832
2833/*
2834 * Set v:char to character "c".
2835 */
2836 void
2837set_vim_var_char(int c)
2838{
2839 char_u buf[MB_MAXBYTES + 1];
2840
2841 if (has_mbyte)
2842 buf[(*mb_char2bytes)(c, buf)] = NUL;
2843 else
2844 {
2845 buf[0] = c;
2846 buf[1] = NUL;
2847 }
2848 set_vim_var_string(VV_CHAR, buf, -1);
2849}
2850
2851/*
2852 * Set v:count to "count" and v:count1 to "count1".
2853 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2854 */
2855 void
2856set_vcount(
2857 long count,
2858 long count1,
2859 int set_prevcount)
2860{
2861 if (set_prevcount)
2862 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2863 vimvars[VV_COUNT].vv_nr = count;
2864 vimvars[VV_COUNT1].vv_nr = count1;
2865}
2866
2867/*
2868 * Save variables that might be changed as a side effect. Used when executing
2869 * a timer callback.
2870 */
2871 void
2872save_vimvars(vimvars_save_T *vvsave)
2873{
2874 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2875 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2876 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2877}
2878
2879/*
2880 * Restore variables saved by save_vimvars().
2881 */
2882 void
2883restore_vimvars(vimvars_save_T *vvsave)
2884{
2885 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2886 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2887 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2888}
2889
2890/*
2891 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2892 * value.
2893 */
2894 void
2895set_vim_var_string(
2896 int idx,
2897 char_u *val,
2898 int len) // length of "val" to use or -1 (whole string)
2899{
2900 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002901 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002902 if (val == NULL)
2903 vimvars[idx].vv_str = NULL;
2904 else if (len == -1)
2905 vimvars[idx].vv_str = vim_strsave(val);
2906 else
2907 vimvars[idx].vv_str = vim_strnsave(val, len);
2908}
2909
2910/*
2911 * Set List v: variable to "val".
2912 */
2913 void
2914set_vim_var_list(int idx, list_T *val)
2915{
2916 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002917 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002918 vimvars[idx].vv_list = val;
2919 if (val != NULL)
2920 ++val->lv_refcount;
2921}
2922
2923/*
2924 * Set Dictionary v: variable to "val".
2925 */
2926 void
2927set_vim_var_dict(int idx, dict_T *val)
2928{
2929 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002930 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002931 vimvars[idx].vv_dict = val;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002932 if (val == NULL)
2933 return;
2934
2935 ++val->dv_refcount;
2936 dict_set_items_ro(val);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002937}
2938
2939/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002940 * Set the v:argv list.
2941 */
2942 void
2943set_argv_var(char **argv, int argc)
2944{
2945 list_T *l = list_alloc();
2946 int i;
2947
2948 if (l == NULL)
2949 getout(1);
2950 l->lv_lock = VAR_FIXED;
2951 for (i = 0; i < argc; ++i)
2952 {
2953 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2954 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002955 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002956 }
2957 set_vim_var_list(VV_ARGV, l);
2958}
2959
2960/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002961 * Reset v:register, taking the 'clipboard' setting into account.
2962 */
2963 void
2964reset_reg_var(void)
2965{
2966 int regname = 0;
2967
2968 // Adjust the register according to 'clipboard', so that when
2969 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2970#ifdef FEAT_CLIPBOARD
2971 adjust_clip_reg(&regname);
2972#endif
2973 set_reg_var(regname);
2974}
2975
2976/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002977 * Set v:register if needed.
2978 */
2979 void
2980set_reg_var(int c)
2981{
2982 char_u regname;
2983
2984 if (c == 0 || c == ' ')
2985 regname = '"';
2986 else
2987 regname = c;
2988 // Avoid free/alloc when the value is already right.
2989 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2990 set_vim_var_string(VV_REG, &regname, 1);
2991}
2992
2993/*
2994 * Get or set v:exception. If "oldval" == NULL, return the current value.
2995 * Otherwise, restore the value to "oldval" and return NULL.
2996 * Must always be called in pairs to save and restore v:exception! Does not
2997 * take care of memory allocations.
2998 */
2999 char_u *
3000v_exception(char_u *oldval)
3001{
3002 if (oldval == NULL)
3003 return vimvars[VV_EXCEPTION].vv_str;
3004
3005 vimvars[VV_EXCEPTION].vv_str = oldval;
3006 return NULL;
3007}
3008
3009/*
3010 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
3011 * Otherwise, restore the value to "oldval" and return NULL.
3012 * Must always be called in pairs to save and restore v:throwpoint! Does not
3013 * take care of memory allocations.
3014 */
3015 char_u *
3016v_throwpoint(char_u *oldval)
3017{
3018 if (oldval == NULL)
3019 return vimvars[VV_THROWPOINT].vv_str;
3020
3021 vimvars[VV_THROWPOINT].vv_str = oldval;
3022 return NULL;
3023}
3024
3025/*
3026 * Set v:cmdarg.
3027 * If "eap" != NULL, use "eap" to generate the value and return the old value.
3028 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
3029 * Must always be called in pairs!
3030 */
3031 char_u *
3032set_cmdarg(exarg_T *eap, char_u *oldarg)
3033{
3034 char_u *oldval;
3035 char_u *newval;
3036 unsigned len;
3037
3038 oldval = vimvars[VV_CMDARG].vv_str;
3039 if (eap == NULL)
3040 {
3041 vim_free(oldval);
3042 vimvars[VV_CMDARG].vv_str = oldarg;
3043 return NULL;
3044 }
3045
3046 if (eap->force_bin == FORCE_BIN)
3047 len = 6;
3048 else if (eap->force_bin == FORCE_NOBIN)
3049 len = 8;
3050 else
3051 len = 0;
3052
3053 if (eap->read_edit)
3054 len += 7;
3055
3056 if (eap->force_ff != 0)
3057 len += 10; // " ++ff=unix"
3058 if (eap->force_enc != 0)
3059 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
3060 if (eap->bad_char != 0)
3061 len += 7 + 4; // " ++bad=" + "keep" or "drop"
3062
3063 newval = alloc(len + 1);
3064 if (newval == NULL)
3065 return NULL;
3066
3067 if (eap->force_bin == FORCE_BIN)
3068 sprintf((char *)newval, " ++bin");
3069 else if (eap->force_bin == FORCE_NOBIN)
3070 sprintf((char *)newval, " ++nobin");
3071 else
3072 *newval = NUL;
3073
3074 if (eap->read_edit)
3075 STRCAT(newval, " ++edit");
3076
3077 if (eap->force_ff != 0)
3078 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
3079 eap->force_ff == 'u' ? "unix"
3080 : eap->force_ff == 'd' ? "dos"
3081 : "mac");
3082 if (eap->force_enc != 0)
3083 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
3084 eap->cmd + eap->force_enc);
3085 if (eap->bad_char == BAD_KEEP)
3086 STRCPY(newval + STRLEN(newval), " ++bad=keep");
3087 else if (eap->bad_char == BAD_DROP)
3088 STRCPY(newval + STRLEN(newval), " ++bad=drop");
3089 else if (eap->bad_char != 0)
3090 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
3091 vimvars[VV_CMDARG].vv_str = newval;
3092 return oldval;
3093}
3094
3095/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003096 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003097 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
3098 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003099 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
3100 */
3101 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003102eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003103 char_u *name,
Bram Moolenaar94674f22023-01-06 18:42:20 +00003104 int len, // length of "name" or zero
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003105 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003106 typval_T *rettv, // NULL when only checking existence
3107 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003108 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003109{
3110 int ret = OK;
3111 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003112 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02003113 hashtab_T *ht = NULL;
Bram Moolenaar94674f22023-01-06 18:42:20 +00003114 int cc = 0;
Bram Moolenaarc967d572021-07-08 21:38:50 +02003115 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003116
Bram Moolenaar94674f22023-01-06 18:42:20 +00003117 if (len > 0)
3118 {
3119 // truncate the name, so that we can use strcmp()
3120 cc = name[len];
3121 name[len] = NUL;
3122 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003123
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003124 // Check for local variable when debugging.
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003125 if ((sid == 0) && (tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003126 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003127 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02003128 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
3129
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003130 if (v != NULL)
3131 {
3132 tv = &v->di_tv;
3133 if (dip != NULL)
3134 *dip = v;
3135 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02003136 else
3137 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003138 }
3139
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003140 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003142 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003143 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
3144
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003145 if (sid == 0)
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003146 import = find_imported(p, 0, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147
3148 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003149 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003151 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02003152 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00003153 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02003154 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003155 ht = &SCRIPT_VARS(sid);
3156 if (ht != NULL)
3157 {
3158 dictitem_T *v = find_var_in_ht(ht, 0, name,
3159 flags & EVAL_VAR_NOAUTOLOAD);
3160
Yegappan Lakshmanane9ae35f2025-02-27 19:12:00 +01003161 if (v == NULL)
3162 v = find_var_autoload_prefix(name, sid, NULL, NULL);
3163
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003164 if (v != NULL)
3165 {
3166 tv = &v->di_tv;
3167 if (dip != NULL)
3168 *dip = v;
3169 }
3170 else
3171 ht = NULL;
3172 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003173 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003174 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003175 {
3176 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00003177 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003178 ret = FAIL;
3179 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02003180 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003181 else
3182 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003183 if (rettv != NULL)
3184 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01003185 // special value that is used in handle_subscript()
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003186 rettv->v_type = VAR_ANY;
3187 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
3188 }
3189 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02003190 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00003192 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003193 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003194 int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003195 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003196
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003197 // In Vim9 script we can get a function reference by using the
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003198 // function name. For a global non-autoload function "g:" is
3199 // required.
3200 if (ufunc != NULL && (has_g_prefix
3201 || !func_requires_g_prefix(ufunc)))
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003202 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003203 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003204 if (rettv != NULL)
3205 {
3206 rettv->v_type = VAR_FUNC;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003207 if (has_g_prefix)
Bram Moolenaaref082e12021-12-12 21:02:03 +00003208 // Keep the "g:", otherwise script-local may be
3209 // assumed.
3210 rettv->vval.v_string = vim_strsave(name);
3211 else
John Marriottb32800f2025-02-01 15:25:34 +01003212 rettv->vval.v_string = vim_strnsave(ufunc->uf_name, ufunc->uf_namelen);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003213 if (rettv->vval.v_string != NULL)
3214 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003215 }
3216 }
3217 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 }
3219
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003220 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003221 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02003222 if (tv == NULL)
3223 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003224 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003225 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02003226 ret = FAIL;
3227 }
3228 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003229 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003230 svar_T *sv = NULL;
3231 int was_assigned = FALSE;
3232
Bram Moolenaar11005b02021-07-11 20:59:00 +02003233 if (ht != NULL && ht == get_script_local_ht()
3234 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02003235 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003236 sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaarf055d452021-07-08 20:57:24 +02003237 if (sv != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003238 {
Bram Moolenaarf055d452021-07-08 20:57:24 +02003239 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003240 was_assigned = sv->sv_flags & SVFLAG_ASSIGNED;
3241 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003242 }
3243
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003244 if ((tv->v_type == VAR_TYPEALIAS || tv->v_type == VAR_CLASS)
3245 && sid != 0)
3246 {
3247 // type alias or class imported from another script. Check
3248 // whether it is exported from the other script.
3249 sv = find_typval_in_script(tv, sid, TRUE);
3250 if (sv == NULL)
3251 {
3252 ret = FAIL;
3253 goto done;
3254 }
3255 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
3256 {
3257 semsg(_(e_item_not_exported_in_script_str), name);
3258 ret = FAIL;
3259 goto done;
3260 }
3261 }
3262
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003263 // If a list or tuple or dict variable wasn't initialized and has
3264 // meaningful type, do it now. Not for global variables, they are
3265 // not declared.
Bram Moolenaar7a222242022-03-01 19:23:24 +00003266 if (ht != &globvarht)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003267 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003268 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003269 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003270 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003271 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003272 tv->vval.v_dict = dict_alloc();
3273 if (tv->vval.v_dict != NULL)
3274 {
3275 ++tv->vval.v_dict->dv_refcount;
3276 tv->vval.v_dict->dv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003277 if (sv != NULL)
3278 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003279 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003280 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003281 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003282 && ((type != NULL && !was_assigned)
Bram Moolenaar501f9782022-03-27 16:51:04 +01003283 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003284 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003285 tv->vval.v_list = list_alloc();
3286 if (tv->vval.v_list != NULL)
3287 {
3288 ++tv->vval.v_list->lv_refcount;
3289 tv->vval.v_list->lv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003290 if (sv != NULL)
3291 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003292 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003293 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003294 else if (tv->v_type == VAR_TUPLE && tv->vval.v_tuple == NULL
3295 && ((type != NULL && !was_assigned)
3296 || !in_vim9script()))
3297 {
3298 tv->vval.v_tuple = tuple_alloc();
3299 if (tv->vval.v_tuple != NULL)
3300 {
3301 ++tv->vval.v_tuple->tv_refcount;
3302 tv->vval.v_tuple->tv_type = alloc_type(type);
3303 if (sv != NULL)
3304 sv->sv_flags |= SVFLAG_ASSIGNED;
3305 }
3306 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003307 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003308 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003309 || !in_vim9script()))
3310 {
3311 tv->vval.v_blob = blob_alloc();
3312 if (tv->vval.v_blob != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003313 {
Bram Moolenaar859cc212022-03-28 15:22:35 +01003314 ++tv->vval.v_blob->bv_refcount;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003315 if (sv != NULL)
3316 sv->sv_flags |= SVFLAG_ASSIGNED;
3317 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003318 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02003319 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003320 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003321 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003322 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003323
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003324done:
Bram Moolenaar94674f22023-01-06 18:42:20 +00003325 if (len > 0)
3326 name[len] = cc;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003327
3328 return ret;
3329}
3330
3331/*
Bram Moolenaara86655a2023-01-12 17:06:27 +00003332 * Get the value of internal variable "name", also handling "import.name".
3333 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
3334 */
3335 int
3336eval_variable_import(
3337 char_u *name,
3338 typval_T *rettv)
3339{
3340 char_u *s = name;
3341 while (ASCII_ISALNUM(*s) || *s == '_')
3342 ++s;
3343 int len = (int)(s - name);
3344
3345 if (eval_variable(name, len, 0, rettv, NULL, EVAL_VAR_IMPORT) == FAIL)
3346 return FAIL;
3347 if (rettv->v_type == VAR_ANY && *s == '.')
3348 {
Bram Moolenaar40594002023-01-12 20:04:51 +00003349 char_u *ns = s + 1;
3350 s = ns;
3351 while (ASCII_ISALNUM(*s) || *s == '_')
3352 ++s;
Bram Moolenaara86655a2023-01-12 17:06:27 +00003353 int sid = rettv->vval.v_number;
Bram Moolenaar40594002023-01-12 20:04:51 +00003354 return eval_variable(ns, (int)(s - ns), sid, rettv, NULL, 0);
Bram Moolenaara86655a2023-01-12 17:06:27 +00003355 }
3356 return OK;
3357}
3358
3359
3360/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003361 * Check if variable "name[len]" is a local variable or an argument.
3362 * If so, "*eval_lavars_used" is set to TRUE.
3363 */
3364 void
3365check_vars(char_u *name, int len)
3366{
3367 int cc;
3368 char_u *varname;
3369 hashtab_T *ht;
3370
3371 if (eval_lavars_used == NULL)
3372 return;
3373
3374 // truncate the name, so that we can use strcmp()
3375 cc = name[len];
3376 name[len] = NUL;
3377
3378 ht = find_var_ht(name, &varname);
3379 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
3380 {
3381 if (find_var(name, NULL, TRUE) != NULL)
3382 *eval_lavars_used = TRUE;
3383 }
3384
3385 name[len] = cc;
3386}
3387
3388/*
3389 * Find variable "name" in the list of variables.
3390 * Return a pointer to it if found, NULL if not found.
3391 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003392 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003393 */
3394 dictitem_T *
3395find_var(char_u *name, hashtab_T **htp, int no_autoload)
3396{
3397 char_u *varname;
3398 hashtab_T *ht;
3399 dictitem_T *ret = NULL;
3400
3401 ht = find_var_ht(name, &varname);
3402 if (htp != NULL)
3403 *htp = ht;
3404 if (ht == NULL)
3405 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003406 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003407 if (ret != NULL)
3408 return ret;
3409
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003410 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003411 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003412 if (ret != NULL)
3413 return ret;
3414
3415 // in Vim9 script items without a scope can be script-local
3416 if (in_vim9script() && name[0] != NUL && name[1] != ':')
3417 {
3418 ht = get_script_local_ht();
3419 if (ht != NULL)
3420 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003421 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003422 if (ret != NULL)
3423 {
3424 if (htp != NULL)
3425 *htp = ht;
3426 return ret;
3427 }
3428 }
3429 }
3430
Ernie Rael84f6dc72024-04-21 14:45:48 +02003431 // and finally try
3432 return find_var_autoload_prefix(name, 0, htp, NULL);
3433}
3434
3435/*
3436 * Find variable "name" with sn_autoload_prefix.
3437 * Return a pointer to it if found, NULL if not found.
3438 * When "sid" > 0, use it otherwise use "current_sctx.sc_sid".
3439 * When "htp" is not NULL set "htp" to the hashtab_T used.
3440 * When "namep" is not NULL set "namep" to the generated name, and
3441 * then the caller gets ownership and is responsible for freeing the name.
3442 */
3443 dictitem_T *
3444find_var_autoload_prefix(char_u *name, int sid, hashtab_T **htp,
3445 char_u **namep)
3446{
3447 hashtab_T *ht;
3448 dictitem_T *ret = NULL;
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003449 // When using "vim9script autoload" script-local items are prefixed but can
3450 // be used with s:name.
Ernie Rael84f6dc72024-04-21 14:45:48 +02003451 int check_sid = sid > 0 ? sid : current_sctx.sc_sid;
3452 if (SCRIPT_ID_VALID(check_sid)
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003453 && (in_vim9script() || (name[0] == 's' && name[1] == ':')))
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003454 {
Ernie Rael84f6dc72024-04-21 14:45:48 +02003455 scriptitem_T *si = SCRIPT_ITEM(check_sid);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003456
3457 if (si->sn_autoload_prefix != NULL)
3458 {
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003459 char_u *base_name = (name[0] == 's' && name[1] == ':')
3460 ? name + 2 : name;
3461 char_u *auto_name = concat_str(si->sn_autoload_prefix, base_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003462
3463 if (auto_name != NULL)
3464 {
Ernie Rael84f6dc72024-04-21 14:45:48 +02003465 int free_auto_name = TRUE;
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003466 ht = &globvarht;
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003467 ret = find_var_in_ht(ht, 'g', auto_name, TRUE);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003468 if (ret != NULL)
3469 {
3470 if (htp != NULL)
3471 *htp = ht;
Ernie Rael84f6dc72024-04-21 14:45:48 +02003472 if (namep != NULL)
3473 {
3474 free_auto_name = FALSE;
3475 *namep = auto_name;
3476 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003477 }
Ernie Rael84f6dc72024-04-21 14:45:48 +02003478 if (free_auto_name)
3479 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003480 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003481 }
3482 }
3483
Ernie Rael84f6dc72024-04-21 14:45:48 +02003484 return ret;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003485}
3486
3487/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00003488 * Like find_var() but if the name starts with <SNR>99_ then look in the
3489 * referenced script (used for a funcref).
3490 */
3491 dictitem_T *
3492find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
3493{
Keith Thompson184f71c2024-01-04 21:19:04 +01003494 if (STRNCMP(name, "<SNR>", 5) == 0 && SAFE_isdigit(name[5]))
Bram Moolenaar71f21932022-01-07 18:20:55 +00003495 {
3496 char_u *p = name + 5;
3497 int sid = getdigits(&p);
3498
3499 if (SCRIPT_ID_VALID(sid) && *p == '_')
3500 {
3501 hashtab_T *ht = &SCRIPT_VARS(sid);
3502
3503 if (ht != NULL)
3504 {
3505 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
3506
3507 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003508 {
3509 if (htp != NULL)
3510 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00003511 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003512 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00003513 }
3514 }
3515 }
3516
3517 return find_var(name, htp, no_autoload);
3518}
3519
3520/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003521 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02003522 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003523 * Returns NULL if not found.
3524 */
3525 dictitem_T *
3526find_var_in_ht(
3527 hashtab_T *ht,
3528 int htname,
3529 char_u *varname,
3530 int no_autoload)
3531{
3532 hashitem_T *hi;
3533
3534 if (*varname == NUL)
3535 {
3536 // Must be something like "s:", otherwise "ht" would be NULL.
3537 switch (htname)
3538 {
3539 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
3540 case 'g': return &globvars_var;
3541 case 'v': return &vimvars_var;
3542 case 'b': return &curbuf->b_bufvar;
3543 case 'w': return &curwin->w_winvar;
3544 case 't': return &curtab->tp_winvar;
3545 case 'l': return get_funccal_local_var();
3546 case 'a': return get_funccal_args_var();
3547 }
3548 return NULL;
3549 }
3550
3551 hi = hash_find(ht, varname);
3552 if (HASHITEM_EMPTY(hi))
3553 {
3554 // For global variables we may try auto-loading the script. If it
3555 // worked find the variable again. Don't auto-load a script if it was
3556 // loaded already, otherwise it would be loaded every time when
3557 // checking if a function name is a Funcref variable.
3558 if (ht == &globvarht && !no_autoload)
3559 {
3560 // Note: script_autoload() may make "hi" invalid. It must either
3561 // be obtained again or not used.
3562 if (!script_autoload(varname, FALSE) || aborting())
3563 return NULL;
3564 hi = hash_find(ht, varname);
3565 }
3566 if (HASHITEM_EMPTY(hi))
3567 return NULL;
3568 }
3569 return HI2DI(hi);
3570}
3571
3572/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 * Get the script-local hashtab. NULL if not in a script context.
3574 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003575 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576get_script_local_ht(void)
3577{
3578 scid_T sid = current_sctx.sc_sid;
3579
Bram Moolenaare3d46852020-08-29 13:39:17 +02003580 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581 return &SCRIPT_VARS(sid);
3582 return NULL;
3583}
3584
3585/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003586 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003587 * When "cmd" is TRUE it must look like a command, a function must be followed
3588 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003589 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003591 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003592lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003593 char_u *name,
3594 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003595 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003596 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597{
3598 hashtab_T *ht = get_script_local_ht();
3599 char_u buffer[30];
3600 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003601 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003603 int is_global = FALSE;
3604 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605
3606 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003607 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 if (len < sizeof(buffer) - 1)
3609 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003610 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 vim_strncpy(buffer, name, len);
3612 p = buffer;
3613 }
3614 else
3615 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003616 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003618 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619 }
3620
3621 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003622 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623
Ernie Rael84f6dc72024-04-21 14:45:48 +02003624 // if not script-local, then perhaps autoload-exported
3625 if (res == FAIL && find_var_autoload_prefix(p, 0, NULL, NULL) != NULL)
3626 res = OK;
3627
3628 // if not script-local or autoload, then perhaps imported
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003629 if (res == FAIL && find_imported(p, 0, FALSE) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003630 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631 if (p != buffer)
3632 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003633
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003634 // Find a function, so that a following "->" works.
3635 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3636 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003637 if (res != OK)
3638 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003639 p = skipwhite(name + len);
3640
3641 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003642 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003643 // Do not check for an internal function, since it might also be a
3644 // valid command, such as ":split" versus "split()".
3645 // Skip "g:" before a function name.
3646 if (name[0] == 'g' && name[1] == ':')
3647 {
3648 is_global = TRUE;
3649 fname = name + 2;
3650 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003651 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003652 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003653 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003654 }
3655
Bram Moolenaar709664c2020-12-12 14:33:41 +01003656 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657}
3658
3659/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003660 * Find the hashtab used for a variable name.
3661 * Return NULL if the name is not valid.
3662 * Set "varname" to the start of name without ':'.
3663 */
3664 hashtab_T *
3665find_var_ht(char_u *name, char_u **varname)
3666{
3667 hashitem_T *hi;
3668 hashtab_T *ht;
3669
3670 if (name[0] == NUL)
3671 return NULL;
3672 if (name[1] != ':')
3673 {
3674 // The name must not start with a colon or #.
3675 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3676 return NULL;
3677 *varname = name;
3678
3679 // "version" is "v:version" in all scopes if scriptversion < 3.
3680 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003681 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003682 {
3683 hi = hash_find(&compat_hashtab, name);
3684 if (!HASHITEM_EMPTY(hi))
3685 return &compat_hashtab;
3686 }
3687
3688 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 if (ht != NULL)
3690 return ht; // local variable
3691
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003692 // In Vim9 script items at the script level are script-local, except
3693 // for autoload names.
3694 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 {
3696 ht = get_script_local_ht();
3697 if (ht != NULL)
3698 return ht;
3699 }
3700
3701 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003702 }
3703 *varname = name + 2;
3704 if (*name == 'g') // global variable
3705 return &globvarht;
3706 // There must be no ':' or '#' in the rest of the name, unless g: is used
3707 if (vim_strchr(name + 2, ':') != NULL
3708 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3709 return NULL;
3710 if (*name == 'b') // buffer variable
3711 return &curbuf->b_vars->dv_hashtab;
3712 if (*name == 'w') // window variable
3713 return &curwin->w_vars->dv_hashtab;
3714 if (*name == 't') // tab page variable
3715 return &curtab->tp_vars->dv_hashtab;
3716 if (*name == 'v') // v: variable
3717 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003718 if (get_current_funccal() != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01003719 && get_current_funccal()->fc_func->uf_def_status
3720 == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003722 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723 if (*name == 'a') // a: function argument
3724 return get_funccal_args_ht();
3725 if (*name == 'l') // l: local function variable
3726 return get_funccal_local_ht();
3727 }
3728 if (*name == 's') // script variable
3729 {
3730 ht = get_script_local_ht();
3731 if (ht != NULL)
3732 return ht;
3733 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003734 return NULL;
3735}
3736
3737/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003738 * Get the string value of a (global/local) variable.
3739 * Note: see tv_get_string() for how long the pointer remains valid.
3740 * Returns NULL when it doesn't exist.
3741 */
3742 char_u *
3743get_var_value(char_u *name)
3744{
3745 dictitem_T *v;
3746
3747 v = find_var(name, NULL, FALSE);
3748 if (v == NULL)
3749 return NULL;
3750 return tv_get_string(&v->di_tv);
3751}
3752
3753/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003754 * Allocate a new hashtab for a sourced script. It will be used while
3755 * sourcing this script and when executing functions defined in the script.
3756 */
3757 void
3758new_script_vars(scid_T id)
3759{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003760 scriptvar_T *sv;
3761
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003762 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3763 if (sv == NULL)
3764 return;
3765 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003766 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003767}
3768
3769/*
3770 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3771 * point to it.
3772 */
3773 void
3774init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3775{
3776 hash_init(&dict->dv_hashtab);
3777 dict->dv_lock = 0;
3778 dict->dv_scope = scope;
3779 dict->dv_refcount = DO_NOT_FREE_CNT;
3780 dict->dv_copyID = 0;
3781 dict_var->di_tv.vval.v_dict = dict;
3782 dict_var->di_tv.v_type = VAR_DICT;
3783 dict_var->di_tv.v_lock = VAR_FIXED;
3784 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3785 dict_var->di_key[0] = NUL;
3786}
3787
3788/*
3789 * Unreference a dictionary initialized by init_var_dict().
3790 */
3791 void
3792unref_var_dict(dict_T *dict)
3793{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003794 // Now the dict needs to be freed if no one else is using it, go back to
3795 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003796 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3797 dict_unref(dict);
3798}
3799
3800/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003801 * Clean up a list of internal variables.
3802 * Frees all allocated variables and the value they contain.
3803 * Clears hashtab "ht", does not free it.
3804 */
3805 void
3806vars_clear(hashtab_T *ht)
3807{
3808 vars_clear_ext(ht, TRUE);
3809}
3810
3811/*
3812 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3813 */
3814 void
3815vars_clear_ext(hashtab_T *ht, int free_val)
3816{
3817 int todo;
3818 hashitem_T *hi;
3819 dictitem_T *v;
3820
3821 hash_lock(ht);
3822 todo = (int)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003823 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003824 {
3825 if (!HASHITEM_EMPTY(hi))
3826 {
3827 --todo;
3828
3829 // Free the variable. Don't remove it from the hashtab,
3830 // ht_array might change then. hash_clear() takes care of it
3831 // later.
3832 v = HI2DI(hi);
3833 if (free_val)
3834 clear_tv(&v->di_tv);
3835 if (v->di_flags & DI_FLAGS_ALLOC)
3836 vim_free(v);
3837 }
3838 }
3839 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003840 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003841}
3842
3843/*
3844 * Delete a variable from hashtab "ht" at item "hi".
3845 * Clear the variable value and free the dictitem.
3846 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003847 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003848delete_var(hashtab_T *ht, hashitem_T *hi)
3849{
3850 dictitem_T *di = HI2DI(hi);
3851
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003852 if (hash_remove(ht, hi, "delete variable") != OK)
3853 return;
3854
3855 clear_tv(&di->di_tv);
3856 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003857}
3858
3859/*
3860 * List the value of one internal variable.
3861 */
3862 static void
3863list_one_var(dictitem_T *v, char *prefix, int *first)
3864{
3865 char_u *tofree;
3866 char_u *s;
3867 char_u numbuf[NUMBUFLEN];
3868
3869 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3870 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3871 s == NULL ? (char_u *)"" : s, first);
3872 vim_free(tofree);
3873}
3874
3875 static void
3876list_one_var_a(
3877 char *prefix,
3878 char_u *name,
3879 int type,
3880 char_u *string,
3881 int *first) // when TRUE clear rest of screen and set to FALSE
3882{
3883 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3884 msg_start();
3885 msg_puts(prefix);
3886 if (name != NULL) // "a:" vars don't have a name stored
3887 msg_puts((char *)name);
3888 msg_putchar(' ');
3889 msg_advance(22);
3890 if (type == VAR_NUMBER)
3891 msg_putchar('#');
3892 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3893 msg_putchar('*');
3894 else if (type == VAR_LIST)
3895 {
3896 msg_putchar('[');
3897 if (*string == '[')
3898 ++string;
3899 }
3900 else if (type == VAR_DICT)
3901 {
3902 msg_putchar('{');
3903 if (*string == '{')
3904 ++string;
3905 }
3906 else
3907 msg_putchar(' ');
3908
3909 msg_outtrans(string);
3910
3911 if (type == VAR_FUNC || type == VAR_PARTIAL)
3912 msg_puts("()");
3913 if (*first)
3914 {
3915 msg_clr_eos();
3916 *first = FALSE;
3917 }
3918}
3919
3920/*
zeertzjqedcba962023-09-24 23:13:51 +02003921 * Addition handling for setting a v: variable.
3922 * Return TRUE if the variable should be set normally,
3923 * FALSE if nothing else needs to be done.
3924 */
3925 int
3926before_set_vvar(
3927 char_u *varname,
3928 dictitem_T *di,
3929 typval_T *tv,
3930 int copy,
3931 int *type_error)
3932{
3933 if (di->di_tv.v_type == VAR_STRING)
3934 {
3935 VIM_CLEAR(di->di_tv.vval.v_string);
3936 if (copy || tv->v_type != VAR_STRING)
3937 {
3938 char_u *val = tv_get_string(tv);
3939
3940 // Careful: when assigning to v:errmsg and
3941 // tv_get_string() causes an error message the variable
3942 // will already be set.
3943 if (di->di_tv.vval.v_string == NULL)
3944 di->di_tv.vval.v_string = vim_strsave(val);
3945 }
3946 else
3947 {
3948 // Take over the string to avoid an extra alloc/free.
3949 di->di_tv.vval.v_string = tv->vval.v_string;
3950 tv->vval.v_string = NULL;
3951 }
3952 return FALSE;
3953 }
3954 else if (di->di_tv.v_type == VAR_NUMBER)
3955 {
3956 di->di_tv.vval.v_number = tv_get_number(tv);
3957 if (STRCMP(varname, "searchforward") == 0)
3958 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
3959#ifdef FEAT_SEARCH_EXTRA
3960 else if (STRCMP(varname, "hlsearch") == 0)
3961 {
3962 no_hlsearch = !di->di_tv.vval.v_number;
3963 redraw_all_later(UPD_SOME_VALID);
3964 }
3965#endif
3966 return FALSE;
3967 }
3968 else if (di->di_tv.v_type != tv->v_type)
3969 {
3970 *type_error = TRUE;
3971 return FALSE;
3972 }
3973 return TRUE;
3974}
3975
3976/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003977 * Set variable "name" to value in "tv".
3978 * If the variable already exists, the value is updated.
3979 * Otherwise the variable is created.
3980 */
3981 void
3982set_var(
3983 char_u *name,
3984 typval_T *tv,
3985 int copy) // make copy of value in "tv"
3986{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003987 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003988}
3989
3990/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003991 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003992 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003993 * If the variable already exists and "is_const" is FALSE the value is updated.
3994 * Otherwise the variable is created.
3995 */
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01003996 int
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003997set_var_const(
3998 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003999 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00004000 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02004001 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004002 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02004003 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004004 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004005{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02004006 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00004007 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02004008 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009 dictitem_T *di;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004010 typval_T *dest_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004011 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004012 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004013 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02004015 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02004016 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004017 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02004018 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004019 int free_tv_arg = !copy; // free tv_arg if not used
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01004020 int rc = FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004021
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004022 if (sid != 0)
4023 {
Ernie Rael84f6dc72024-04-21 14:45:48 +02004024 varname = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004025 if (SCRIPT_ID_VALID(sid))
Ernie Rael84f6dc72024-04-21 14:45:48 +02004026 {
4027 char_u *auto_name = NULL;
4028 if (find_var_autoload_prefix(name, sid, &ht, &auto_name) != NULL)
4029 {
4030 var_in_autoload = TRUE;
4031 varname = auto_name;
4032 name_tofree = varname;
4033 }
4034 else
4035 ht = &SCRIPT_VARS(sid);
4036 }
4037 if (varname == NULL)
4038 varname = name;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004039 }
4040 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004041 {
Ernie Rael84f6dc72024-04-21 14:45:48 +02004042 scriptitem_T *si;
4043 char_u *auto_name = NULL;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004044
Ernie Rael84f6dc72024-04-21 14:45:48 +02004045 if (in_vim9script()
4046 && SCRIPT_ID_VALID(current_sctx.sc_sid)
4047 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
4048 ->sn_autoload_prefix != NULL
4049 && (is_export
4050 || find_var_autoload_prefix(name, 0, NULL, &auto_name)
4051 != NULL))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004052 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004053 // In a vim9 autoload script an exported variable is put in the
4054 // global namespace with the autoload prefix.
4055 var_in_autoload = TRUE;
Ernie Rael84f6dc72024-04-21 14:45:48 +02004056 varname = auto_name != NULL ? auto_name
4057 : concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004058 if (varname == NULL)
4059 goto failed;
4060 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004061 ht = &globvarht;
4062 }
4063 else
4064 ht = find_var_ht(name, &varname);
4065 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004066 if (ht == NULL || *varname == NUL)
4067 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004068 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004069 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004070 }
Bram Moolenaar54969f42022-02-07 13:56:44 +00004071 is_script_local = ht == get_script_local_ht() || sid != 0
4072 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004073
Bram Moolenaardbeecb22020-09-14 18:15:09 +02004074 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004075 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01004076 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01004077 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004078 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02004079 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004080 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004081 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02004082 }
Bram Moolenaar9510d222022-09-11 15:14:05 +01004083 if ((flags & ASSIGN_FOR_LOOP) && is_scoped_variable(name))
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02004084 // Do not make g:var, w:var, b:var or t:var final.
4085 flags &= ~ASSIGN_FINAL;
4086
Bram Moolenaare535db82021-03-31 21:07:24 +02004087 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004088 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
4089 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02004090 // For "[a, _] = list" the underscore is ignored.
4091 if ((flags & ASSIGN_UNPACK) == 0)
4092 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004093 goto failed;
4094 }
Bram Moolenaar67979662020-06-20 22:50:47 +02004095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004097
Bram Moolenaar24e93162021-07-18 20:40:33 +02004098 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02004099 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004100 imported_T *import = find_imported(varname, 0, FALSE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02004101
Bram Moolenaar24e93162021-07-18 20:40:33 +02004102 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004103 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004104 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02004105 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02004107 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004108 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004109 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004110 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
4111 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02004112 }
Bram Moolenaar75e27d72022-02-13 13:56:29 +00004113 if (!in_vim9script())
4114 {
4115 semsg(_(e_cannot_create_vim9_script_variable_in_function_str),
4116 name);
4117 goto failed;
4118 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02004119 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120
Bram Moolenaar993faa32022-02-21 15:59:11 +00004121 // Search in parent scope which is possible to reference from lambda
4122 if (di == NULL)
4123 di = find_var_in_scoped_ht(name, TRUE);
4124
4125 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
4126 && var_wrong_func_name(name, di == NULL))
4127 goto failed;
4128
4129 if (need_convert_to_bool(type, tv))
Bram Moolenaar24e93162021-07-18 20:40:33 +02004130 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00004131 // Destination is a bool and the value is not, but it can be
4132 // converted.
4133 CLEAR_FIELD(bool_tv);
4134 bool_tv.v_type = VAR_BOOL;
4135 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
4136 tv = &bool_tv;
4137 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02004138
Bram Moolenaar993faa32022-02-21 15:59:11 +00004139 if (di != NULL)
4140 {
4141 // Item already exists. Allowed to replace when reloading.
4142 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02004143 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00004144 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
4145 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02004146 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00004147 emsg(_(e_cannot_modify_existing_variable));
4148 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02004149 }
4150
Bram Moolenaar993faa32022-02-21 15:59:11 +00004151 if (is_script_local && vim9script
4152 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar12be7342021-03-31 21:47:33 +02004153 {
4154 semsg(_(e_redefining_script_item_str), name);
4155 goto failed;
4156 }
4157
Ernie Raele75fde62023-12-21 17:18:54 +01004158 if (check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02004159 goto failed;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02004160
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01004161 // List and Blob types can be modified in-place using the "+="
4162 // compound operator. For other types, this is not allowed.
4163 int type_inplace_modifiable =
4164 (di->di_tv.v_type == VAR_LIST || di->di_tv.v_type == VAR_BLOB);
4165
4166 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0
4167 && ((flags & ASSIGN_COMPOUND_OP) == 0
4168 || !type_inplace_modifiable))
Bram Moolenaar24e93162021-07-18 20:40:33 +02004169 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00004170 where_T where = WHERE_INIT;
Bram Moolenaar7a411a32022-04-04 14:58:06 +01004171 svar_T *sv = find_typval_in_script(&di->di_tv, sid, TRUE);
Bram Moolenaar993faa32022-02-21 15:59:11 +00004172
4173 if (sv != NULL)
4174 {
4175 // check the type and adjust to bool if needed
LemonBoyc5d27442023-08-19 13:02:35 +02004176 if (var_idx > 0)
4177 {
4178 where.wt_index = var_idx;
4179 where.wt_kind = WT_VARIABLE;
4180 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00004181 if (check_script_var_type(sv, tv, name, where) == FAIL)
4182 goto failed;
4183 if (type == NULL)
4184 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01004185 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004186 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02004187 }
4188
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01004189 // Modifying a final variable with a List value using the "+="
4190 // operator is allowed. For other types, it is not allowed.
zeertzjq6b97d7a2024-08-08 21:05:57 +02004191 if ((((flags & ASSIGN_FOR_LOOP) == 0 || (flags & ASSIGN_DECL) == 0)
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01004192 && ((flags & ASSIGN_COMPOUND_OP) == 0
4193 || !type_inplace_modifiable))
Bram Moolenaar16d2c022023-06-05 19:46:18 +01004194 ? var_check_permission(di, name) == FAIL
4195 : var_check_ro(di->di_flags, name, FALSE))
Bram Moolenaar24e93162021-07-18 20:40:33 +02004196 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004197 }
4198 else
4199 {
4200 // can only redefine once
4201 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar24e93162021-07-18 20:40:33 +02004202
Bram Moolenaar993faa32022-02-21 15:59:11 +00004203 // A Vim9 script-local variable is also present in sn_all_vars
4204 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004205 if (var_in_vim9script || var_in_autoload)
Bram Moolenaar993faa32022-02-21 15:59:11 +00004206 update_vim9_script_var(FALSE, di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004207 var_in_autoload ? name : di->di_key, flags,
Bram Moolenaar993faa32022-02-21 15:59:11 +00004208 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01004209 }
4210
Bram Moolenaar993faa32022-02-21 15:59:11 +00004211 // existing variable, need to clear the value
4212
zeertzjqedcba962023-09-24 23:13:51 +02004213 // Handle setting internal v: variables separately where needed to
Bram Moolenaar993faa32022-02-21 15:59:11 +00004214 // prevent changing the type.
zeertzjqedcba962023-09-24 23:13:51 +02004215 int type_error = FALSE;
4216 if (ht == &vimvarht
4217 && !before_set_vvar(varname, di, tv, copy, &type_error))
Bram Moolenaar993faa32022-02-21 15:59:11 +00004218 {
zeertzjqedcba962023-09-24 23:13:51 +02004219 if (type_error)
4220 semsg(_(e_setting_v_str_to_value_with_wrong_type), varname);
4221 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004222 }
4223
4224 clear_tv(&di->di_tv);
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01004225
4226 if ((flags & ASSIGN_UPDATE_BLOCK_ID)
4227 && SCRIPT_ID_VALID(current_sctx.sc_sid))
4228 {
4229 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
4230
4231 update_script_var_block_id(name, si->sn_current_block_id);
4232 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00004233 }
4234 else
4235 {
4236 // Item not found, check if a function already exists.
4237 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
4238 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
4239 {
4240 semsg(_(e_redefining_script_item_str), name);
4241 goto failed;
4242 }
4243
4244 // add a new variable
4245 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
4246 {
4247 semsg(_(e_unknown_variable_str), name);
4248 goto failed;
4249 }
4250
Bram Moolenaaref2c3252022-11-25 16:31:51 +00004251 if (check_hashtab_frozen(ht, "add variable"))
4252 goto failed;
4253
Bram Moolenaar993faa32022-02-21 15:59:11 +00004254 // Can't add "v:" or "a:" variable.
4255 if (ht == &vimvarht || ht == get_funccal_args_ht())
4256 {
4257 semsg(_(e_illegal_variable_name_str), name);
4258 goto failed;
4259 }
4260
4261 // Make sure the variable name is valid. In Vim9 script an
4262 // autoload variable must be prefixed with "g:" unless in an
4263 // autoload script.
4264 if (!valid_varname(varname, -1, !vim9script
4265 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
4266 goto failed;
4267
zeertzjq1b438a82023-02-01 13:11:15 +00004268 di = alloc(offsetof(dictitem_T, di_key) + STRLEN(varname) + 1);
Bram Moolenaar993faa32022-02-21 15:59:11 +00004269 if (di == NULL)
4270 goto failed;
4271 STRCPY(di->di_key, varname);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00004272 if (hash_add(ht, DI2HIKEY(di), "add variable") == FAIL)
Bram Moolenaar993faa32022-02-21 15:59:11 +00004273 {
4274 vim_free(di);
4275 goto failed;
4276 }
4277 di->di_flags = DI_FLAGS_ALLOC;
4278 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
4279 di->di_flags |= DI_FLAGS_LOCK;
4280
4281 // A Vim9 script-local variable is also added to sn_all_vars and
4282 // sn_var_vals. It may set "type" from "tv".
4283 if (var_in_vim9script || var_in_autoload)
4284 update_vim9_script_var(TRUE, di,
4285 var_in_autoload ? name : di->di_key, flags,
4286 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004287 }
4288
Bram Moolenaar993faa32022-02-21 15:59:11 +00004289 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004290 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02004291 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004292 else
4293 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02004294 *dest_tv = *tv;
4295 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004296 init_tv(tv);
4297 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004298 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004299
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004300 if (vim9script && type != NULL)
Bram Moolenaar381692b2022-02-02 20:01:27 +00004301 set_tv_type(dest_tv, type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004302
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01004303 // ":const var = value" locks the value
4304 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004305 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02004306 // Like :lockvar! name: lock the value and what it contains, but only
4307 // if the reference count is up to one. That locks only literal
4308 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02004309 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02004310
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01004311 rc = OK;
4312
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004313failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004314 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004315 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004316 clear_tv(tv_arg);
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01004317
4318 return rc;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004319}
4320
4321/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004322 * Check in this order for backwards compatibility:
4323 * - Whether the variable is read-only
4324 * - Whether the variable value is locked
4325 * - Whether the variable is locked
Ernie Rael3f821d62024-04-24 20:07:50 +02004326 * NOTE: "name" is only used for error messages.
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004327 */
4328 int
4329var_check_permission(dictitem_T *di, char_u *name)
4330{
4331 if (var_check_ro(di->di_flags, name, FALSE)
4332 || value_check_lock(di->di_tv.v_lock, name, FALSE)
4333 || var_check_lock(di->di_flags, name, FALSE))
4334 return FAIL;
4335 return OK;
4336}
4337
4338/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004339 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
4340 * Also give an error message.
4341 */
4342 int
4343var_check_ro(int flags, char_u *name, int use_gettext)
4344{
4345 if (flags & DI_FLAGS_RO)
4346 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004347 if (name == NULL)
4348 emsg(_(e_cannot_change_readonly_variable));
4349 else
4350 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02004351 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004352 return TRUE;
4353 }
4354 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
4355 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004356 if (name == NULL)
4357 emsg(_(e_cannot_set_variable_in_sandbox));
4358 else
4359 semsg(_(e_cannot_set_variable_in_sandbox_str),
4360 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004361 return TRUE;
4362 }
4363 return FALSE;
4364}
4365
4366/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004367 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
4368 * Also give an error message.
4369 */
4370 int
4371var_check_lock(int flags, char_u *name, int use_gettext)
4372{
4373 if (flags & DI_FLAGS_LOCK)
4374 {
4375 semsg(_(e_variable_is_locked_str),
4376 use_gettext ? (char_u *)_(name) : name);
4377 return TRUE;
4378 }
4379 return FALSE;
4380}
4381
4382/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004383 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
4384 * Also give an error message.
4385 */
4386 int
4387var_check_fixed(int flags, char_u *name, int use_gettext)
4388{
4389 if (flags & DI_FLAGS_FIX)
4390 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004391 if (name == NULL)
4392 emsg(_(e_cannot_delete_variable));
4393 else
4394 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004395 use_gettext ? (char_u *)_(name) : name);
4396 return TRUE;
4397 }
4398 return FALSE;
4399}
4400
4401/*
4402 * Check if a funcref is assigned to a valid variable name.
4403 * Return TRUE and give an error if not.
4404 */
4405 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004406var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004407 char_u *name, // points to start of variable name
4408 int new_var) // TRUE when creating the variable
4409{
Bram Moolenaar32154662021-03-28 21:14:06 +02004410 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
4411 // the name can be used without the s: prefix.
thinca6c667bd2022-09-02 11:25:37 +01004412 // Allow autoload variable.
Bram Moolenaar32154662021-03-28 21:14:06 +02004413 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
4414 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004415 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
thinca6c667bd2022-09-02 11:25:37 +01004416 ? name[2] : name[0])
4417 && vim_strchr(name, '#') == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004418 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004419 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004420 return TRUE;
4421 }
4422 // Don't allow hiding a function. When "v" is not NULL we might be
4423 // assigning another function to the same var, the type is checked
4424 // below.
4425 if (new_var && function_exists(name, FALSE))
4426 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004427 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004428 name);
4429 return TRUE;
4430 }
4431 return FALSE;
4432}
4433
4434/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004435 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
4436 * value. Also give an error message, using "name" or _("name") when
4437 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004438 */
4439 int
Bram Moolenaara187c432020-09-16 21:08:28 +02004440value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004441{
4442 if (lock & VAR_LOCKED)
4443 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004444 if (name == NULL)
4445 emsg(_(e_value_is_locked));
4446 else
4447 semsg(_(e_value_is_locked_str),
4448 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004449 return TRUE;
4450 }
4451 if (lock & VAR_FIXED)
4452 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004453 if (name == NULL)
4454 emsg(_(e_cannot_change_value));
4455 else
4456 semsg(_(e_cannot_change_value_of_str),
4457 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004458 return TRUE;
4459 }
4460 return FALSE;
4461}
4462
4463/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01004464 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004465 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004466 * Return FALSE and give an error if not.
4467 */
4468 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004469valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004470{
4471 char_u *p;
4472
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004473 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004474 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01004475 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004476 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004477 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004478 return FALSE;
4479 }
4480 return TRUE;
4481}
4482
4483/*
LemonBoy47d4e312022-05-04 18:12:55 +01004484 * Implements the logic to retrieve local variable and option values.
4485 * Used by "getwinvar()" "gettabvar()" "gettabwinvar()" "getbufvar()".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004486 */
4487 static void
LemonBoy47d4e312022-05-04 18:12:55 +01004488get_var_from(
4489 char_u *varname,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004490 typval_T *rettv,
LemonBoy47d4e312022-05-04 18:12:55 +01004491 typval_T *deftv, // Default value if not found.
4492 int htname, // 't'ab, 'w'indow or 'b'uffer local.
4493 tabpage_T *tp, // can be NULL
4494 win_T *win,
4495 buf_T *buf) // Ignored if htname is not 'b'.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004496{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004497 dictitem_T *v;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004498 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004499 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004500 int need_switch_win;
zeertzjqcd6ad642022-07-25 12:28:09 +01004501 int do_change_curbuf = buf != NULL && htname == 'b';
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004502
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004503 ++emsg_off;
4504
4505 rettv->v_type = VAR_STRING;
4506 rettv->vval.v_string = NULL;
4507
LemonBoy47d4e312022-05-04 18:12:55 +01004508 if (varname != NULL && tp != NULL && win != NULL
4509 && (htname != 'b' || buf != NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004510 {
4511 // Set curwin to be our win, temporarily. Also set the tabpage,
4512 // otherwise the window is not valid. Only do this when needed,
4513 // autocommands get blocked.
LemonBoy47d4e312022-05-04 18:12:55 +01004514 // If we have a buffer reference avoid the switching, we're saving and
4515 // restoring curbuf directly.
zeertzjqcd6ad642022-07-25 12:28:09 +01004516 need_switch_win = !(tp == curtab && win == curwin) && !do_change_curbuf;
LemonBoy47d4e312022-05-04 18:12:55 +01004517 if (!need_switch_win || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004518 {
LemonBoy47d4e312022-05-04 18:12:55 +01004519 // Handle options. There are no tab-local options.
4520 if (*varname == '&' && htname != 't')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004521 {
LemonBoy47d4e312022-05-04 18:12:55 +01004522 buf_T *save_curbuf = curbuf;
4523
4524 // Change curbuf so the option is read from the correct buffer.
zeertzjqcd6ad642022-07-25 12:28:09 +01004525 if (do_change_curbuf)
LemonBoy47d4e312022-05-04 18:12:55 +01004526 curbuf = buf;
4527
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004528 if (varname[1] == NUL)
4529 {
zeertzjqcd6ad642022-07-25 12:28:09 +01004530 // get all window-local or buffer-local options in a dict
LemonBoy47d4e312022-05-04 18:12:55 +01004531 dict_T *opts = get_winbuf_options(htname == 'b');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004532
4533 if (opts != NULL)
4534 {
4535 rettv_dict_set(rettv, opts);
4536 done = TRUE;
4537 }
4538 }
LemonBoy47d4e312022-05-04 18:12:55 +01004539 else if (eval_option(&varname, rettv, TRUE) == OK)
4540 // Local option
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004541 done = TRUE;
LemonBoy47d4e312022-05-04 18:12:55 +01004542
4543 curbuf = save_curbuf;
4544 }
4545 else if (*varname == NUL)
4546 {
4547 // Empty string: return a dict with all the local variables.
4548 if (htname == 'b')
4549 v = &buf->b_bufvar;
4550 else if (htname == 'w')
4551 v = &win->w_winvar;
4552 else
4553 v = &tp->tp_winvar;
4554 copy_tv(&v->di_tv, rettv);
4555 done = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004556 }
4557 else
4558 {
LemonBoy47d4e312022-05-04 18:12:55 +01004559 hashtab_T *ht;
4560
4561 if (htname == 'b')
4562 ht = &buf->b_vars->dv_hashtab;
4563 else if (htname == 'w')
4564 ht = &win->w_vars->dv_hashtab;
4565 else
4566 ht = &tp->tp_vars->dv_hashtab;
4567
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004568 // Look up the variable.
LemonBoy47d4e312022-05-04 18:12:55 +01004569 v = find_var_in_ht(ht, htname, varname, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004570 if (v != NULL)
4571 {
4572 copy_tv(&v->di_tv, rettv);
4573 done = TRUE;
4574 }
4575 }
4576 }
4577
4578 if (need_switch_win)
4579 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004580 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004581 }
4582
LemonBoy47d4e312022-05-04 18:12:55 +01004583 if (!done && deftv->v_type != VAR_UNKNOWN)
4584 // use the default value
4585 copy_tv(deftv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004586
4587 --emsg_off;
4588}
4589
4590/*
LemonBoy47d4e312022-05-04 18:12:55 +01004591 * getwinvar() and gettabwinvar()
4592 */
4593 static void
4594getwinvar(
4595 typval_T *argvars,
4596 typval_T *rettv,
4597 int off) // 1 for gettabwinvar()
4598{
4599 char_u *varname;
4600 tabpage_T *tp;
4601 win_T *win;
4602
4603 if (off == 1)
4604 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4605 else
4606 tp = curtab;
4607 win = find_win_by_nr(&argvars[off], tp);
4608 varname = tv_get_string_chk(&argvars[off + 1]);
4609
4610 get_var_from(varname, rettv, &argvars[off + 2], 'w', tp, win, NULL);
4611}
4612
4613/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02004614 * Set option "varname" to the value of "varp" for the current buffer/window.
4615 */
4616 static void
4617set_option_from_tv(char_u *varname, typval_T *varp)
4618{
4619 long numval = 0;
4620 char_u *strval;
4621 char_u nbuf[NUMBUFLEN];
4622 int error = FALSE;
4623
zeertzjq4c7cb372023-06-14 16:39:54 +01004624 int opt_idx = findoption(varname);
4625 if (opt_idx < 0)
4626 {
4627 semsg(_(e_unknown_option_str_2), varname);
4628 return;
4629 }
4630 int opt_p_flags = get_option_flags(opt_idx);
4631
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004632 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004633 {
zeertzjq4c7cb372023-06-14 16:39:54 +01004634 if (opt_p_flags & P_STRING)
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004635 {
4636 emsg(_(e_string_required));
4637 return;
4638 }
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004639 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004640 strval = (char_u *)"0"; // avoid using "false"
4641 }
4642 else
4643 {
zeertzjq4c7cb372023-06-14 16:39:54 +01004644 if ((opt_p_flags & (P_NUM|P_BOOL))
4645 && (!in_vim9script() || varp->v_type != VAR_STRING))
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004646 numval = (long)tv_get_number_chk(varp, &error);
zeertzjq4c7cb372023-06-14 16:39:54 +01004647 if (!error)
4648 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004649 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02004650 if (!error && strval != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004651 set_option_value_give_err(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar191929b2020-08-19 21:20:49 +02004652}
4653
4654/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004655 * "setwinvar()" and "settabwinvar()" functions
4656 */
4657 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004658setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004659{
4660 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004661 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004662 int need_switch_win;
4663 char_u *varname, *winvarname;
4664 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004665 tabpage_T *tp = NULL;
4666
4667 if (check_secure())
4668 return;
4669
4670 if (off == 1)
4671 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4672 else
4673 tp = curtab;
4674 win = find_win_by_nr(&argvars[off], tp);
4675 varname = tv_get_string_chk(&argvars[off + 1]);
4676 varp = &argvars[off + 2];
4677
zeertzjqea720ae2023-01-03 10:54:09 +00004678 if (win == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004679 return;
4680
4681 need_switch_win = !(tp == curtab && win == curwin);
4682 if (!need_switch_win
4683 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004684 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004685 if (*varname == '&')
4686 set_option_from_tv(varname + 1, varp);
4687 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004688 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004689 winvarname = alloc(STRLEN(varname) + 3);
4690 if (winvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004691 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004692 STRCPY(winvarname, "w:");
4693 STRCPY(winvarname + 2, varname);
4694 set_var(winvarname, varp, TRUE);
4695 vim_free(winvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004696 }
4697 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004698 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004699 if (need_switch_win)
4700 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004701}
4702
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004703/*
4704 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4705 * v:option_type, and v:option_command.
4706 */
4707 void
4708reset_v_option_vars(void)
4709{
4710 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4711 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4712 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4713 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4714 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4715 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4716}
4717
4718/*
4719 * Add an assert error to v:errors.
4720 */
4721 void
4722assert_error(garray_T *gap)
4723{
4724 struct vimvar *vp = &vimvars[VV_ERRORS];
4725
Bram Moolenaard787e402021-12-24 21:36:12 +00004726 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004727 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004728 set_vim_var_list(VV_ERRORS, list_alloc());
4729 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4730}
4731
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004732 int
4733var_exists(char_u *var)
4734{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004735 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004736 char_u *name;
4737 char_u *tofree;
4738 typval_T tv;
4739 int len = 0;
4740 int n = FALSE;
4741
4742 // get_name_len() takes care of expanding curly braces
4743 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004744 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004745 if (len > 0)
4746 {
4747 if (tofree != NULL)
4748 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004749 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004750 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004751 if (n)
4752 {
4753 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004754 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004755 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4756 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004757 if (n)
4758 clear_tv(&tv);
4759 }
4760 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004761 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004762 n = FALSE;
4763
4764 vim_free(tofree);
4765 return n;
4766}
4767
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004768static lval_T *redir_lval = NULL;
4769#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4770static garray_T redir_ga; // only valid when redir_lval is not NULL
4771static char_u *redir_endp = NULL;
4772static char_u *redir_varname = NULL;
4773
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004774 int
4775alloc_redir_lval(void)
4776{
4777 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4778 if (redir_lval == NULL)
4779 return FAIL;
4780 return OK;
4781}
4782
4783 void
4784clear_redir_lval(void)
4785{
4786 VIM_CLEAR(redir_lval);
4787}
4788
4789 void
4790init_redir_ga(void)
4791{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004792 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004793}
4794
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004795/*
4796 * Start recording command output to a variable
4797 * When "append" is TRUE append to an existing variable.
4798 * Returns OK if successfully completed the setup. FAIL otherwise.
4799 */
4800 int
4801var_redir_start(char_u *name, int append)
4802{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004803 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004804 typval_T tv;
4805
4806 // Catch a bad name early.
4807 if (!eval_isnamec1(*name))
4808 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004809 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004810 return FAIL;
4811 }
4812
4813 // Make a copy of the name, it is used in redir_lval until redir ends.
4814 redir_varname = vim_strsave(name);
4815 if (redir_varname == NULL)
4816 return FAIL;
4817
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004818 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004819 {
4820 var_redir_stop();
4821 return FAIL;
4822 }
4823
4824 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004825 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004826
4827 // Parse the variable name (can be a dict or list entry).
4828 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4829 FNE_CHECK_START);
4830 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4831 {
4832 clear_lval(redir_lval);
4833 if (redir_endp != NULL && *redir_endp != NUL)
4834 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004835 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004836 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004837 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004838 redir_endp = NULL; // don't store a value, only cleanup
4839 var_redir_stop();
4840 return FAIL;
4841 }
4842
4843 // check if we can write to the variable: set it to or append an empty
4844 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004845 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004846 tv.v_type = VAR_STRING;
4847 tv.vval.v_string = (char_u *)"";
4848 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004849 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004850 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004851 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004852 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004853 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004854 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004855 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004856 {
4857 redir_endp = NULL; // don't store a value, only cleanup
4858 var_redir_stop();
4859 return FAIL;
4860 }
4861
4862 return OK;
4863}
4864
4865/*
4866 * Append "value[value_len]" to the variable set by var_redir_start().
4867 * The actual appending is postponed until redirection ends, because the value
4868 * appended may in fact be the string we write to, changing it may cause freed
4869 * memory to be used:
4870 * :redir => foo
4871 * :let foo
4872 * :redir END
4873 */
4874 void
4875var_redir_str(char_u *value, int value_len)
4876{
4877 int len;
4878
4879 if (redir_lval == NULL)
4880 return;
4881
4882 if (value_len == -1)
4883 len = (int)STRLEN(value); // Append the entire string
4884 else
4885 len = value_len; // Append only "value_len" characters
4886
4887 if (ga_grow(&redir_ga, len) == OK)
4888 {
4889 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4890 redir_ga.ga_len += len;
4891 }
4892 else
4893 var_redir_stop();
4894}
4895
4896/*
4897 * Stop redirecting command output to a variable.
4898 * Frees the allocated memory.
4899 */
4900 void
4901var_redir_stop(void)
4902{
4903 typval_T tv;
4904
4905 if (EVALCMD_BUSY)
4906 {
4907 redir_lval = NULL;
4908 return;
4909 }
4910
4911 if (redir_lval != NULL)
4912 {
4913 // If there was no error: assign the text to the variable.
4914 if (redir_endp != NULL)
4915 {
4916 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4917 tv.v_type = VAR_STRING;
4918 tv.vval.v_string = redir_ga.ga_data;
4919 // Call get_lval() again, if it's inside a Dict or List it may
4920 // have changed.
4921 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4922 FALSE, FALSE, 0, FNE_CHECK_START);
4923 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004924 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004925 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004926 clear_lval(redir_lval);
4927 }
4928
4929 // free the collected output
4930 VIM_CLEAR(redir_ga.ga_data);
4931
4932 VIM_CLEAR(redir_lval);
4933 }
4934 VIM_CLEAR(redir_varname);
4935}
4936
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004937/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004938 * Get the collected redirected text and clear redir_ga.
4939 */
4940 char_u *
4941get_clear_redir_ga(void)
4942{
4943 char_u *res;
4944
4945 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4946 res = redir_ga.ga_data;
4947 redir_ga.ga_data = NULL;
4948 return res;
4949}
4950
4951/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004952 * "gettabvar()" function
4953 */
4954 void
4955f_gettabvar(typval_T *argvars, typval_T *rettv)
4956{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004957 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004958 tabpage_T *tp;
4959 win_T *win = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004960
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004961 if (in_vim9script()
4962 && (check_for_number_arg(argvars, 0) == FAIL
4963 || check_for_string_arg(argvars, 1) == FAIL))
4964 return;
4965
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004966 varname = tv_get_string_chk(&argvars[1]);
4967 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
LemonBoy47d4e312022-05-04 18:12:55 +01004968 if (tp != NULL)
4969 win = tp == curtab || tp->tp_firstwin == NULL ? firstwin
4970 : tp->tp_firstwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004971
LemonBoy47d4e312022-05-04 18:12:55 +01004972 get_var_from(varname, rettv, &argvars[2], 't', tp, win, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004973}
4974
4975/*
4976 * "gettabwinvar()" function
4977 */
4978 void
4979f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4980{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004981 if (in_vim9script()
4982 && (check_for_number_arg(argvars, 0) == FAIL
4983 || check_for_number_arg(argvars, 1) == FAIL
4984 || check_for_string_arg(argvars, 2) == FAIL))
4985 return;
4986
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004987 getwinvar(argvars, rettv, 1);
4988}
4989
4990/*
4991 * "getwinvar()" function
4992 */
4993 void
4994f_getwinvar(typval_T *argvars, typval_T *rettv)
4995{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004996 if (in_vim9script()
4997 && (check_for_number_arg(argvars, 0) == FAIL
4998 || check_for_string_arg(argvars, 1) == FAIL))
4999 return;
5000
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005001 getwinvar(argvars, rettv, 0);
5002}
5003
5004/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005005 * "getbufvar()" function
5006 */
5007 void
5008f_getbufvar(typval_T *argvars, typval_T *rettv)
5009{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005010 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01005011 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005012
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02005013 if (in_vim9script()
5014 && (check_for_buffer_arg(argvars, 0) == FAIL
5015 || check_for_string_arg(argvars, 1) == FAIL))
5016 return;
5017
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005018 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02005019 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005020
LemonBoy47d4e312022-05-04 18:12:55 +01005021 get_var_from(varname, rettv, &argvars[2], 'b', curtab, curwin, buf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005022}
5023
5024/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005025 * "settabvar()" function
5026 */
5027 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01005028f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005029{
5030 tabpage_T *save_curtab;
5031 tabpage_T *tp;
zeertzjqb47fbb42024-02-12 22:50:26 +01005032 tabpage_T *save_lu_tp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005033 char_u *varname, *tabvarname;
5034 typval_T *varp;
5035
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005036 if (check_secure())
5037 return;
5038
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005039 if (in_vim9script()
5040 && (check_for_number_arg(argvars, 0) == FAIL
5041 || check_for_string_arg(argvars, 1) == FAIL))
5042 return;
5043
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005044 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
5045 varname = tv_get_string_chk(&argvars[1]);
5046 varp = &argvars[2];
5047
zeertzjqea720ae2023-01-03 10:54:09 +00005048 if (varname == NULL || tp == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005049 return;
5050
5051 save_curtab = curtab;
zeertzjqb47fbb42024-02-12 22:50:26 +01005052 save_lu_tp = lastused_tabpage;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005053 goto_tabpage_tp(tp, FALSE, FALSE);
5054
5055 tabvarname = alloc(STRLEN(varname) + 3);
5056 if (tabvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005057 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005058 STRCPY(tabvarname, "t:");
5059 STRCPY(tabvarname + 2, varname);
5060 set_var(tabvarname, varp, TRUE);
5061 vim_free(tabvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005062 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005063
zeertzjqb47fbb42024-02-12 22:50:26 +01005064 // Restore current tabpage and last accessed tabpage.
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005065 if (valid_tabpage(save_curtab))
zeertzjqb47fbb42024-02-12 22:50:26 +01005066 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005067 goto_tabpage_tp(save_curtab, FALSE, FALSE);
zeertzjqb47fbb42024-02-12 22:50:26 +01005068 if (valid_tabpage(save_lu_tp))
5069 lastused_tabpage = save_lu_tp;
5070 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005071}
5072
5073/*
5074 * "settabwinvar()" function
5075 */
5076 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01005077f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005078{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005079 if (in_vim9script()
5080 && (check_for_number_arg(argvars, 0) == FAIL
5081 || check_for_number_arg(argvars, 1) == FAIL
5082 || check_for_string_arg(argvars, 2) == FAIL))
5083 return;
5084
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01005085 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005086}
5087
5088/*
5089 * "setwinvar()" function
5090 */
5091 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01005092f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005093{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005094 if (in_vim9script()
5095 && (check_for_number_arg(argvars, 0) == FAIL
5096 || check_for_string_arg(argvars, 1) == FAIL))
5097 return;
5098
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01005099 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005100}
5101
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005102/*
5103 * "setbufvar()" function
5104 */
5105 void
5106f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
5107{
5108 buf_T *buf;
5109 char_u *varname, *bufvarname;
5110 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005111
5112 if (check_secure())
5113 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02005114
5115 if (in_vim9script()
5116 && (check_for_buffer_arg(argvars, 0) == FAIL
5117 || check_for_string_arg(argvars, 1) == FAIL))
5118 return;
5119
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005120 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02005121 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005122 varp = &argvars[2];
5123
zeertzjqea720ae2023-01-03 10:54:09 +00005124 if (buf == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005125 return;
5126
5127 if (*varname == '&')
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005128 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005129 aco_save_T aco;
Christian Brabandtac4cffc2024-01-16 17:22:38 +01005130 // safe the current window position, it could
5131 // change because of 'scrollbind' window-local
5132 // options
5133 linenr_T old_topline = curwin->w_topline;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005134
5135 // Set curbuf to be our buf, temporarily.
5136 aucmd_prepbuf(&aco, buf);
5137 if (curbuf == buf)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005138 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005139 // Only when it worked to set "curbuf".
5140 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005141
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005142 // reset notion of buffer
5143 aucmd_restbuf(&aco);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005144 }
Christian Brabandtac4cffc2024-01-16 17:22:38 +01005145 curwin->w_topline = old_topline;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005146 }
5147 else
5148 {
5149 bufvarname = alloc(STRLEN(varname) + 3);
5150 if (bufvarname != NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005151 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005152 buf_T *save_curbuf = curbuf;
Bram Moolenaar86015452020-03-29 15:12:15 +02005153
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005154 curbuf = buf;
5155 STRCPY(bufvarname, "b:");
5156 STRCPY(bufvarname + 2, varname);
5157 set_var(bufvarname, varp, TRUE);
5158 vim_free(bufvarname);
5159 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005160 }
5161 }
5162}
5163
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005164/*
5165 * Get a callback from "arg". It can be a Funcref or a function name.
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005166 * When "arg" is zero "res.cb_name" is set to an empty string.
5167 * If "res.cb_name" is allocated then "res.cb_free_name" is set to TRUE.
5168 * "res.cb_name" is set to NULL for an invalid argument.
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005169 */
5170 callback_T
5171get_callback(typval_T *arg)
5172{
Bram Moolenaar14e579092020-03-07 16:59:25 +01005173 callback_T res;
5174 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005175
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005176 CLEAR_FIELD(res);
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005177 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
5178 {
5179 res.cb_partial = arg->vval.v_partial;
5180 ++res.cb_partial->pt_refcount;
5181 res.cb_name = partial_name(res.cb_partial);
5182 }
5183 else
5184 {
Bram Moolenaar14e579092020-03-07 16:59:25 +01005185 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
Keith Thompson184f71c2024-01-04 21:19:04 +01005186 && SAFE_isdigit(*arg->vval.v_string))
Bram Moolenaar14e579092020-03-07 16:59:25 +01005187 r = FAIL;
5188 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005189 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005190 res.cb_name = arg->vval.v_string;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00005191 if (arg->v_type == VAR_STRING)
5192 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005193 char_u *name = get_scriptlocal_funcname(arg->vval.v_string);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00005194 if (name != NULL)
5195 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005196 res.cb_name = name;
5197 res.cb_free_name = TRUE;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00005198 }
5199 }
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005200 func_ref(res.cb_name);
5201 }
5202 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005203 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005204 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01005205 r = FAIL;
5206
5207 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005208 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005209 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005210 res.cb_name = NULL;
5211 }
5212 }
5213 return res;
5214}
5215
5216/*
5217 * Copy a callback into a typval_T.
5218 */
5219 void
5220put_callback(callback_T *cb, typval_T *tv)
5221{
5222 if (cb->cb_partial != NULL)
5223 {
5224 tv->v_type = VAR_PARTIAL;
5225 tv->vval.v_partial = cb->cb_partial;
5226 ++tv->vval.v_partial->pt_refcount;
5227 }
5228 else
5229 {
5230 tv->v_type = VAR_FUNC;
5231 tv->vval.v_string = vim_strsave(cb->cb_name);
5232 func_ref(cb->cb_name);
5233 }
5234}
5235
5236/*
5237 * Make a copy of "src" into "dest", allocating the function name if needed,
5238 * without incrementing the refcount.
5239 */
5240 void
5241set_callback(callback_T *dest, callback_T *src)
5242{
5243 if (src->cb_partial == NULL)
5244 {
5245 // just a function name, make a copy
5246 dest->cb_name = vim_strsave(src->cb_name);
5247 dest->cb_free_name = TRUE;
5248 }
5249 else
5250 {
5251 // cb_name is a pointer into cb_partial
5252 dest->cb_name = src->cb_name;
5253 dest->cb_free_name = FALSE;
5254 }
5255 dest->cb_partial = src->cb_partial;
5256}
5257
5258/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02005259 * Copy callback from "src" to "dest", incrementing the refcounts.
5260 */
5261 void
5262copy_callback(callback_T *dest, callback_T *src)
5263{
5264 dest->cb_partial = src->cb_partial;
5265 if (dest->cb_partial != NULL)
5266 {
5267 dest->cb_name = src->cb_name;
5268 dest->cb_free_name = FALSE;
5269 ++dest->cb_partial->pt_refcount;
5270 }
5271 else
5272 {
5273 dest->cb_name = vim_strsave(src->cb_name);
5274 dest->cb_free_name = TRUE;
5275 func_ref(src->cb_name);
5276 }
5277}
5278
5279/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005280 * When a callback refers to an autoload import, change the function name to
5281 * the "path#name" form. Uses the current script context.
5282 * Only works when the name is allocated.
5283 */
5284 void
5285expand_autload_callback(callback_T *cb)
5286{
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005287 char_u *name;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005288 char_u *p;
5289 imported_T *import;
5290
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005291 if (!in_vim9script() || cb->cb_name == NULL
5292 || (!cb->cb_free_name
5293 && (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005294 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005295 if (cb->cb_partial != NULL)
5296 name = cb->cb_partial->pt_name;
5297 else
5298 name = cb->cb_name;
5299 p = vim_strchr(name, '.');
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005300 if (p == NULL)
5301 return;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005302
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005303 import = find_imported(name, p - name, FALSE);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005304 if (import == NULL || !SCRIPT_ID_VALID(import->imp_sid))
5305 return;
5306
5307 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
5308 if (si->sn_autoload_prefix == NULL)
5309 return;
5310
5311 char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
5312 if (newname == NULL)
5313 return;
5314
5315 if (cb->cb_partial != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005316 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005317 if (cb->cb_name == cb->cb_partial->pt_name)
5318 cb->cb_name = newname;
5319 vim_free(cb->cb_partial->pt_name);
5320 cb->cb_partial->pt_name = newname;
5321 }
5322 else
5323 {
5324 vim_free(cb->cb_name);
5325 cb->cb_name = newname;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005326 }
5327}
5328
5329/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005330 * Unref/free "callback" returned by get_callback() or set_callback().
5331 */
5332 void
5333free_callback(callback_T *callback)
5334{
5335 if (callback->cb_partial != NULL)
5336 {
5337 partial_unref(callback->cb_partial);
5338 callback->cb_partial = NULL;
5339 }
5340 else if (callback->cb_name != NULL)
5341 func_unref(callback->cb_name);
5342 if (callback->cb_free_name)
5343 {
5344 vim_free(callback->cb_name);
5345 callback->cb_free_name = FALSE;
5346 }
5347 callback->cb_name = NULL;
5348}
5349
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005350#endif // FEAT_EVAL