blob: bebee2a5bcf802f70d6c1ae7d1927c9506126344 [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},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200160};
161
162// shorthand
Bram Moolenaard787e402021-12-24 21:36:12 +0000163#define vv_tv_type vv_di.di_tv.v_type
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200164#define vv_nr vv_di.di_tv.vval.v_number
165#define vv_float vv_di.di_tv.vval.v_float
166#define vv_str vv_di.di_tv.vval.v_string
167#define vv_list vv_di.di_tv.vval.v_list
168#define vv_dict vv_di.di_tv.vval.v_dict
169#define vv_blob vv_di.di_tv.vval.v_blob
170#define vv_tv vv_di.di_tv
171
172static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200173static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200174#define vimvarht vimvardict.dv_hashtab
175
176// for VIM_VERSION_ defines
177#include "version.h"
178
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200179static void list_glob_vars(int *first);
180static void list_buf_vars(int *first);
181static void list_win_vars(int *first);
182static void list_tab_vars(int *first);
183static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100184static 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 +0200185static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
186static 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 +0200187static void list_one_var(dictitem_T *v, char *prefix, int *first);
188static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
189
190/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200191 * Initialize global and vim special variables
192 */
193 void
194evalvars_init(void)
195{
196 int i;
197 struct vimvar *p;
198
199 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
200 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
201 vimvardict.dv_lock = VAR_FIXED;
202 hash_init(&compat_hashtab);
203
204 for (i = 0; i < VV_LEN; ++i)
205 {
206 p = &vimvars[i];
207 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
208 {
Bram Moolenaar097c5372023-05-24 21:02:24 +0100209 iemsg("Name too long, increase size of dictitem16_T");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200210 getout(1);
211 }
212 STRCPY(p->vv_di.di_key, p->vv_name);
213 if (p->vv_flags & VV_RO)
214 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
215 else if (p->vv_flags & VV_RO_SBX)
216 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
217 else
218 p->vv_di.di_flags = DI_FLAGS_FIX;
219
220 // add to v: scope dict, unless the value is not always available
Bram Moolenaard787e402021-12-24 21:36:12 +0000221 if (p->vv_tv_type != VAR_UNKNOWN)
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000222 hash_add(&vimvarht, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200223 if (p->vv_flags & VV_COMPAT)
224 // add to compat scope dict
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000225 hash_add(&compat_hashtab, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200226 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200227 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
228 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200229
230 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
231 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100232 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200233 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
234 set_vim_var_list(VV_ERRORS, list_alloc());
235 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
236
237 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
238 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
239 set_vim_var_nr(VV_NONE, VVAL_NONE);
240 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100241 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
242 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100243 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaar69b30722021-11-02 21:39:49 +0000244 set_vim_var_nr(VV_SIZEOFINT, sizeof(int));
245 set_vim_var_nr(VV_SIZEOFLONG, sizeof(long));
246 set_vim_var_nr(VV_SIZEOFPOINTER, sizeof(char *));
naohiro ono56200ee2022-01-01 14:59:44 +0000247 set_vim_var_nr(VV_MAXCOL, MAXCOL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200248
249 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
250 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
251 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
252 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
253 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
254 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
255 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
256 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
257 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
258 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
259 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarc0c2c262023-01-12 21:08:53 +0000260 set_vim_var_nr(VV_TYPE_CLASS, VAR_TYPE_CLASS);
261 set_vim_var_nr(VV_TYPE_OBJECT, VAR_TYPE_OBJECT);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200262
263 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
264
Drew Vogele30d1022021-10-24 20:35:07 +0100265 set_vim_var_dict(VV_COLORNAMES, dict_alloc());
266
Bram Moolenaar439c0362020-06-06 15:58:03 +0200267 // Default for v:register is not 0 but '"'. This is adjusted once the
268 // clipboard has been setup by calling reset_reg_var().
269 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200270}
271
272#if defined(EXITFREE) || defined(PROTO)
273/*
274 * Free all vim variables information on exit
275 */
276 void
277evalvars_clear(void)
278{
279 int i;
280 struct vimvar *p;
281
282 for (i = 0; i < VV_LEN; ++i)
283 {
284 p = &vimvars[i];
285 if (p->vv_di.di_tv.v_type == VAR_STRING)
286 VIM_CLEAR(p->vv_str);
287 else if (p->vv_di.di_tv.v_type == VAR_LIST)
288 {
289 list_unref(p->vv_list);
290 p->vv_list = NULL;
291 }
292 }
293 hash_clear(&vimvarht);
294 hash_init(&vimvarht); // garbage_collect() will access it
295 hash_clear(&compat_hashtab);
296
297 // global variables
298 vars_clear(&globvarht);
299
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100300 // Script-local variables. Clear all the variables here.
301 // The scriptvar_T is cleared later in free_scriptnames(), because a
302 // variable in one script might hold a reference to the whole scope of
303 // another script.
304 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200305 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200306}
307#endif
308
309 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200310garbage_collect_globvars(int copyID)
311{
312 return set_ref_in_ht(&globvarht, copyID, NULL);
313}
314
315 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200316garbage_collect_vimvars(int copyID)
317{
318 return set_ref_in_ht(&vimvarht, copyID, NULL);
319}
320
321 int
322garbage_collect_scriptvars(int copyID)
323{
Bram Moolenaared234f22020-10-15 20:42:20 +0200324 int i;
325 int idx;
326 int abort = FALSE;
327 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200328
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100329 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200330 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200331 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
332
Bram Moolenaared234f22020-10-15 20:42:20 +0200333 si = SCRIPT_ITEM(i);
334 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
335 {
336 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
337
Bram Moolenaard00a7fb2021-03-08 20:47:14 +0100338 if (sv->sv_name != NULL)
339 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
Bram Moolenaared234f22020-10-15 20:42:20 +0200340 }
341 }
342
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200343 return abort;
344}
345
346/*
347 * Set an internal variable to a string value. Creates the variable if it does
348 * not already exist.
349 */
350 void
351set_internal_string_var(char_u *name, char_u *value)
352{
353 char_u *val;
354 typval_T *tvp;
355
356 val = vim_strsave(value);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000357 if (val == NULL)
358 return;
359
360 tvp = alloc_string_tv(val);
361 if (tvp == NULL)
362 return;
363
364 set_var(name, tvp, FALSE);
365 free_tv(tvp);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200366}
367
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200368 int
369eval_charconvert(
370 char_u *enc_from,
371 char_u *enc_to,
372 char_u *fname_from,
373 char_u *fname_to)
374{
375 int err = FALSE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000376 sctx_T saved_sctx = current_sctx;
377 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200378
379 set_vim_var_string(VV_CC_FROM, enc_from, -1);
380 set_vim_var_string(VV_CC_TO, enc_to, -1);
381 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
382 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000383 ctx = get_option_sctx("charconvert");
384 if (ctx != NULL)
385 current_sctx = *ctx;
386
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100387 if (eval_to_bool(p_ccv, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200388 err = TRUE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000389
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200390 set_vim_var_string(VV_CC_FROM, NULL, -1);
391 set_vim_var_string(VV_CC_TO, NULL, -1);
392 set_vim_var_string(VV_FNAME_IN, NULL, -1);
393 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000394 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200395
396 if (err)
397 return FAIL;
398 return OK;
399}
400
401# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
402 int
403eval_printexpr(char_u *fname, char_u *args)
404{
405 int err = FALSE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000406 sctx_T saved_sctx = current_sctx;
407 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200408
409 set_vim_var_string(VV_FNAME_IN, fname, -1);
410 set_vim_var_string(VV_CMDARG, args, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000411 ctx = get_option_sctx("printexpr");
412 if (ctx != NULL)
413 current_sctx = *ctx;
414
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100415 if (eval_to_bool(p_pexpr, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200416 err = TRUE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000417
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200418 set_vim_var_string(VV_FNAME_IN, NULL, -1);
419 set_vim_var_string(VV_CMDARG, NULL, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000420 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200421
422 if (err)
423 {
424 mch_remove(fname);
425 return FAIL;
426 }
427 return OK;
428}
429# endif
430
431# if defined(FEAT_DIFF) || defined(PROTO)
432 void
433eval_diff(
434 char_u *origfile,
435 char_u *newfile,
436 char_u *outfile)
437{
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000438 sctx_T saved_sctx = current_sctx;
439 sctx_T *ctx;
440 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200441
442 set_vim_var_string(VV_FNAME_IN, origfile, -1);
443 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
444 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000445
446 ctx = get_option_sctx("diffexpr");
447 if (ctx != NULL)
448 current_sctx = *ctx;
449
450 // errors are ignored
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100451 tv = eval_expr_ext(p_dex, NULL, TRUE);
Bram Moolenaar39b89442022-01-22 18:21:36 +0000452 free_tv(tv);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000453
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200454 set_vim_var_string(VV_FNAME_IN, NULL, -1);
455 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
456 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000457 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200458}
459
460 void
461eval_patch(
462 char_u *origfile,
463 char_u *difffile,
464 char_u *outfile)
465{
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000466 sctx_T saved_sctx = current_sctx;
467 sctx_T *ctx;
468 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200469
470 set_vim_var_string(VV_FNAME_IN, origfile, -1);
471 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
472 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000473
474 ctx = get_option_sctx("patchexpr");
475 if (ctx != NULL)
476 current_sctx = *ctx;
477
478 // errors are ignored
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100479 tv = eval_expr_ext(p_pex, NULL, TRUE);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000480 free_tv(tv);
481
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200482 set_vim_var_string(VV_FNAME_IN, NULL, -1);
483 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
484 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000485 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200486}
487# endif
488
489#if defined(FEAT_SPELL) || defined(PROTO)
490/*
491 * Evaluate an expression to a list with suggestions.
492 * For the "expr:" part of 'spellsuggest'.
493 * Returns NULL when there is an error.
494 */
495 list_T *
496eval_spell_expr(char_u *badword, char_u *expr)
497{
498 typval_T save_val;
499 typval_T rettv;
500 list_T *list = NULL;
501 char_u *p = skipwhite(expr);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000502 sctx_T saved_sctx = current_sctx;
503 sctx_T *ctx;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100504 int r;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200505
506 // Set "v:val" to the bad word.
507 prepare_vimvar(VV_VAL, &save_val);
508 set_vim_var_string(VV_VAL, badword, -1);
509 if (p_verbose == 0)
510 ++emsg_off;
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000511 ctx = get_option_sctx("spellsuggest");
512 if (ctx != NULL)
513 current_sctx = *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200514
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100515 r = may_call_simple_func(p, &rettv);
516 if (r == NOTDONE)
517 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
518 if (r == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200519 {
520 if (rettv.v_type != VAR_LIST)
521 clear_tv(&rettv);
522 else
523 list = rettv.vval.v_list;
524 }
525
526 if (p_verbose == 0)
527 --emsg_off;
528 clear_tv(get_vim_var_tv(VV_VAL));
529 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000530 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200531
532 return list;
533}
534
535/*
536 * "list" is supposed to contain two items: a word and a number. Return the
537 * word in "pp" and the number as the return value.
538 * Return -1 if anything isn't right.
539 * Used to get the good word and score from the eval_spell_expr() result.
540 */
541 int
542get_spellword(list_T *list, char_u **pp)
543{
544 listitem_T *li;
545
546 li = list->lv_first;
547 if (li == NULL)
548 return -1;
549 *pp = tv_get_string(&li->li_tv);
550
551 li = li->li_next;
552 if (li == NULL)
553 return -1;
554 return (int)tv_get_number(&li->li_tv);
555}
556#endif
557
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200558/*
559 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200560 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200561 * When not used yet add the variable to the v: hashtable.
562 */
563 void
564prepare_vimvar(int idx, typval_T *save_tv)
565{
566 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200567 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaard787e402021-12-24 21:36:12 +0000568 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000569 hash_add(&vimvarht, vimvars[idx].vv_di.di_key, "prepare vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200570}
571
572/*
573 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200574 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200575 * When no longer defined, remove the variable from the v: hashtable.
576 */
577 void
578restore_vimvar(int idx, typval_T *save_tv)
579{
580 hashitem_T *hi;
581
582 vimvars[idx].vv_tv = *save_tv;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000583 if (vimvars[idx].vv_tv_type != VAR_UNKNOWN)
584 return;
585
586 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
587 if (HASHITEM_EMPTY(hi))
588 internal_error("restore_vimvar()");
589 else
590 hash_remove(&vimvarht, hi, "restore vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200591}
592
593/*
594 * List Vim variables.
595 */
596 static void
597list_vim_vars(int *first)
598{
599 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
600}
601
602/*
603 * List script-local variables, if there is a script.
604 */
605 static void
606list_script_vars(int *first)
607{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200608 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200609 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
610 "s:", FALSE, first);
611}
612
613/*
Bram Moolenaar9510d222022-09-11 15:14:05 +0100614 * Return TRUE if "name" starts with "g:", "w:", "t:" or "b:".
615 * But only when an identifier character follows.
616 */
617 int
618is_scoped_variable(char_u *name)
619{
620 return vim_strchr((char_u *)"gwbt", name[0]) != NULL
621 && name[1] == ':'
622 && eval_isnamec(name[2]);
623}
624
625/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100626 * Evaluate one Vim expression {expr} in string "p" and append the
627 * resulting string to "gap". "p" points to the opening "{".
Bram Moolenaar70c41242022-05-10 18:11:43 +0100628 * When "evaluate" is FALSE only skip over the expression.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100629 * Return a pointer to the character after "}", NULL for an error.
630 */
631 char_u *
Bram Moolenaar70c41242022-05-10 18:11:43 +0100632eval_one_expr_in_str(char_u *p, garray_T *gap, int evaluate)
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100633{
634 char_u *block_start = skipwhite(p + 1); // skip the opening {
635 char_u *block_end = block_start;
636 char_u *expr_val;
637
638 if (*block_start == NUL)
639 {
640 semsg(_(e_missing_close_curly_str), p);
641 return NULL;
642 }
643 if (skip_expr(&block_end, NULL) == FAIL)
644 return NULL;
645 block_end = skipwhite(block_end);
646 if (*block_end != '}')
647 {
648 semsg(_(e_missing_close_curly_str), p);
649 return NULL;
650 }
Bram Moolenaar70c41242022-05-10 18:11:43 +0100651 if (evaluate)
652 {
653 *block_end = NUL;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100654 expr_val = eval_to_string(block_start, TRUE, FALSE);
Bram Moolenaar70c41242022-05-10 18:11:43 +0100655 *block_end = '}';
656 if (expr_val == NULL)
657 return NULL;
658 ga_concat(gap, expr_val);
659 vim_free(expr_val);
660 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100661
662 return block_end + 1;
663}
664
665/*
666 * Evaluate all the Vim expressions {expr} in "str" and return the resulting
667 * string in allocated memory. "{{" is reduced to "{" and "}}" to "}".
668 * Used for a heredoc assignment.
669 * Returns NULL for an error.
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100670 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +0100671 static char_u *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100672eval_all_expr_in_str(char_u *str)
673{
674 garray_T ga;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100675 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100676
677 ga_init2(&ga, 1, 80);
678 p = str;
679
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100680 while (*p != NUL)
681 {
LemonBoy2eaef102022-05-06 13:14:50 +0100682 char_u *lit_start;
LemonBoy2eaef102022-05-06 13:14:50 +0100683 int escaped_brace = FALSE;
684
685 // Look for a block start.
686 lit_start = p;
687 while (*p != '{' && *p != '}' && *p != NUL)
688 ++p;
689
690 if (*p != NUL && *p == p[1])
691 {
692 // Escaped brace, unescape and continue.
693 // Include the brace in the literal string.
694 ++p;
695 escaped_brace = TRUE;
696 }
697 else if (*p == '}')
698 {
699 semsg(_(e_stray_closing_curly_str), str);
700 ga_clear(&ga);
701 return NULL;
702 }
703
704 // Append the literal part.
705 ga_concat_len(&ga, lit_start, (size_t)(p - lit_start));
706
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100707 if (*p == NUL)
LemonBoy2eaef102022-05-06 13:14:50 +0100708 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100709
LemonBoy2eaef102022-05-06 13:14:50 +0100710 if (escaped_brace)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100711 {
LemonBoy2eaef102022-05-06 13:14:50 +0100712 // Skip the second brace.
713 ++p;
714 continue;
715 }
716
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100717 // Evaluate the expression and append the result.
Bram Moolenaar70c41242022-05-10 18:11:43 +0100718 p = eval_one_expr_in_str(p, &ga, TRUE);
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100719 if (p == NULL)
LemonBoy2eaef102022-05-06 13:14:50 +0100720 {
721 ga_clear(&ga);
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100722 return NULL;
723 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100724 }
725 ga_append(&ga, NUL);
726
727 return ga.ga_data;
728}
729
730/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200731 * Get a list of lines from a HERE document. The here document is a list of
732 * lines surrounded by a marker.
733 * cmd << {marker}
734 * {line1}
735 * {line2}
736 * ....
737 * {marker}
738 *
739 * The {marker} is a string. If the optional 'trim' word is supplied before the
740 * marker, then the leading indentation before the lines (matching the
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100741 * indentation in the "cmd" line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200742 *
743 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100744 * tcl, mzscheme), "script_get" is set to TRUE. In this case, if the marker is
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200745 * missing, then '.' is accepted as a marker.
746 *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100747 * When compiling a heredoc assignment to a variable in a Vim9 def function,
748 * "vim9compile" is set to TRUE. In this case, instead of generating a list of
749 * string values from the heredoc, vim9 instructions are generated. On success
750 * the returned list will be empty.
751 *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100752 * Returns a List with {lines} or NULL on failure.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200753 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100754 list_T *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100755heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200756{
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100757 char_u *theline = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200758 char_u *marker;
759 list_T *l;
760 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100761 char_u *str;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200762 int marker_indent_len = 0;
763 int text_indent_len = 0;
764 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200765 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200766 int comment_char = in_vim9script() ? '#' : '"';
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100767 int evalstr = FALSE;
768 int eval_failed = FALSE;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100769 cctx_T *cctx = vim9compile ? eap->cookie : NULL;
770 int count = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200771
772 if (eap->getline == NULL)
773 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000774 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200775 return NULL;
776 }
777
778 // Check for the optional 'trim' word before the marker
779 cmd = skipwhite(cmd);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200780
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100781 while (TRUE)
782 {
783 if (STRNCMP(cmd, "trim", 4) == 0
784 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200785 {
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100786 cmd = skipwhite(cmd + 4);
787
788 // Trim the indentation from all the lines in the here document.
789 // The amount of indentation trimmed is the same as the indentation
790 // of the first line after the :let command line. To find the end
791 // marker the indent of the :let command line is trimmed.
792 p = *eap->cmdlinep;
793 while (VIM_ISWHITE(*p))
794 {
795 p++;
796 marker_indent_len++;
797 }
798 text_indent_len = -1;
799
800 continue;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200801 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100802 if (STRNCMP(cmd, "eval", 4) == 0
803 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
804 {
805 cmd = skipwhite(cmd + 4);
806 evalstr = TRUE;
807 continue;
808 }
809 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200810 }
811
812 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200813 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200814 {
815 marker = skipwhite(cmd);
816 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200817 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200818 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000819 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200820 return NULL;
821 }
822 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200823 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200824 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000825 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200826 return NULL;
827 }
828 }
829 else
830 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200831 // When getting lines for an embedded script, if the marker is missing,
832 // accept '.' as the marker.
833 if (script_get)
834 marker = dot;
835 else
836 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000837 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200838 return NULL;
839 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200840 }
841
842 l = list_alloc();
843 if (l == NULL)
844 return NULL;
845
846 for (;;)
847 {
848 int mi = 0;
849 int ti = 0;
850
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100851 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200852 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
853 if (theline == NULL)
854 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000855 semsg(_(e_missing_end_marker_str), marker);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200856 break;
857 }
858
859 // with "trim": skip the indent matching the :let line to find the
860 // marker
861 if (marker_indent_len > 0
862 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
863 mi = marker_indent_len;
864 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200865 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200866
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100867 // If expression evaluation failed in the heredoc, then skip till the
868 // end marker.
869 if (eval_failed)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100870 continue;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100871
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200872 if (text_indent_len == -1 && *theline != NUL)
873 {
874 // set the text indent from the first line.
875 p = theline;
876 text_indent_len = 0;
877 while (VIM_ISWHITE(*p))
878 {
879 p++;
880 text_indent_len++;
881 }
882 text_indent = vim_strnsave(theline, text_indent_len);
883 }
884 // with "trim": skip the indent matching the first line
885 if (text_indent != NULL)
886 for (ti = 0; ti < text_indent_len; ++ti)
887 if (theline[ti] != text_indent[ti])
888 break;
889
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100890 str = theline + ti;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100891 if (vim9compile)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100892 {
LemonBoy2eaef102022-05-06 13:14:50 +0100893 if (compile_all_expr_in_str(str, evalstr, cctx) == FAIL)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100894 {
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100895 vim_free(theline);
896 vim_free(text_indent);
897 return FAIL;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100898 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100899 count++;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100900 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100901 else
902 {
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100903 if (evalstr && !eap->skip)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100904 {
905 str = eval_all_expr_in_str(str);
906 if (str == NULL)
907 {
908 // expression evaluation failed
909 eval_failed = TRUE;
910 continue;
911 }
912 vim_free(theline);
913 theline = str;
914 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100915
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100916 if (list_append_string(l, str, -1) == FAIL)
917 break;
918 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200919 }
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100920 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200921 vim_free(text_indent);
922
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100923 if (vim9compile && cctx->ctx_skip != SKIP_YES && !eval_failed)
924 generate_NEWLIST(cctx, count, FALSE);
925
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100926 if (eval_failed)
927 {
928 // expression evaluation in the heredoc failed
929 list_free(l);
930 return NULL;
931 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200932 return l;
933}
934
935/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200936 * Vim9 variable declaration:
937 * ":var name"
938 * ":var name: type"
939 * ":var name = expr"
940 * ":var name: type = expr"
941 * etc.
942 */
943 void
944ex_var(exarg_T *eap)
945{
Bram Moolenaar330a3882022-03-05 11:05:57 +0000946 char_u *p = eap->cmd;
Bram Moolenaare1d12112022-03-05 11:37:48 +0000947 int has_var;
Bram Moolenaar330a3882022-03-05 11:05:57 +0000948
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200949 if (!in_vim9script())
950 {
951 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
952 return;
953 }
Bram Moolenaare1d12112022-03-05 11:37:48 +0000954 has_var = checkforcmd_noparen(&p, "var", 3);
955 if (current_sctx.sc_sid == 0 && has_var)
Bram Moolenaar0e1574c2022-03-03 17:05:35 +0000956 {
957 emsg(_(e_cannot_declare_variable_on_command_line));
958 return;
959 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200960 ex_let(eap);
961}
962
963/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200964 * ":let" list all variable values
965 * ":let var1 var2" list variable values
966 * ":let var = expr" assignment command.
967 * ":let var += expr" assignment command.
968 * ":let var -= expr" assignment command.
969 * ":let var *= expr" assignment command.
970 * ":let var /= expr" assignment command.
971 * ":let var %= expr" assignment command.
972 * ":let var .= expr" assignment command.
973 * ":let var ..= expr" assignment command.
974 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100975 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100976 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200977 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200978 * ":final var = expr" assignment command.
979 * ":final [var1, var2] = expr" unpack list.
980 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200981 * ":const" list all variable values
982 * ":const var1 var2" list variable values
983 * ":const var = expr" assignment command.
984 * ":const [var1, var2] = expr" unpack list.
985 */
986 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200987ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200988{
989 char_u *arg = eap->arg;
990 char_u *expr = NULL;
991 typval_T rettv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200992 int var_count = 0;
993 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200994 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200995 char_u *argend;
996 int first = TRUE;
997 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200998 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100999 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001000 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001001
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001002 if (eap->cmdidx == CMD_final && !vim9script)
1003 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001004 // In legacy Vim script ":final" is short for ":finally".
1005 ex_finally(eap);
1006 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001007 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +02001008 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001009 {
1010 emsg(_(e_cannot_use_let_in_vim9_script));
1011 return;
1012 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001013
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001014 if (eap->cmdidx == CMD_const)
1015 flags |= ASSIGN_CONST;
1016 else if (eap->cmdidx == CMD_final)
1017 flags |= ASSIGN_FINAL;
1018
1019 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001020 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001021 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001022
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001023 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001024 if (argend == NULL)
1025 return;
1026 if (argend > arg && argend[-1] == '.') // for var.='str'
1027 --argend;
1028 expr = skipwhite(argend);
1029 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02001030 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001031 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +02001032 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
1033 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +02001034 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001035 {
1036 // ":let" without "=": list variables
1037 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001038 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001039 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001040 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001041 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001042 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001043 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001044 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001045 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001046 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001047 else
1048 // Vim9 declaration ":var name: type"
1049 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001050 }
1051 else
1052 {
1053 // ":let var1 var2" - list values
1054 arg = list_arg_vars(eap, arg, &first);
1055 }
1056 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001057 else if (!eap->skip)
1058 {
1059 // ":let"
1060 list_glob_vars(&first);
1061 list_buf_vars(&first);
1062 list_win_vars(&first);
1063 list_tab_vars(&first);
1064 list_script_vars(&first);
1065 list_func_vars(&first);
1066 list_vim_vars(&first);
1067 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02001068 set_nextcmd(eap, arg);
zeertzjq474891b2023-04-12 21:36:03 +01001069 return;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001070 }
zeertzjq474891b2023-04-12 21:36:03 +01001071
1072 if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001073 {
Bram Moolenaard9876422022-10-12 12:58:54 +01001074 list_T *l = NULL;
Bram Moolenaar81530e32021-07-28 21:25:49 +02001075 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001076
Bram Moolenaard9876422022-10-12 12:58:54 +01001077 // :let text =<< [trim] [eval] END
1078 // :var text =<< [trim] [eval] END
1079 if (vim9script && !eap->skip && (!VIM_ISWHITE(expr[-1])
1080 || !IS_WHITE_OR_NUL(expr[3])))
1081 semsg(_(e_white_space_required_before_and_after_str_at_str),
1082 "=<<", expr);
1083 else
1084 l = heredoc_get(eap, expr + 3, FALSE, FALSE);
1085
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001086 if (l != NULL)
1087 {
1088 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001089 if (!eap->skip)
1090 {
Bram Moolenaar81530e32021-07-28 21:25:49 +02001091 // errors are for the assignment, not the end marker
1092 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001093 op[0] = '=';
1094 op[1] = NUL;
1095 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001097 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001098 clear_tv(&rettv);
1099 }
zeertzjq474891b2023-04-12 21:36:03 +01001100 return;
1101 }
1102
1103 evalarg_T evalarg;
1104 int len = 1;
1105
1106 CLEAR_FIELD(rettv);
1107
1108 int cur_lnum;
1109
1110 op[0] = '=';
1111 op[1] = NUL;
1112 if (*expr != '=')
1113 {
1114 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
1115 {
1116 // +=, /=, etc. require an existing variable
1117 semsg(_(e_cannot_use_operator_on_new_variable_str), eap->arg);
1118 }
1119 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
1120 {
1121 op[0] = *expr; // +=, -=, *=, /=, %= or .=
1122 ++len;
1123 if (expr[0] == '.' && expr[1] == '.') // ..=
1124 {
1125 ++expr;
1126 ++len;
1127 }
1128 }
1129 expr += 2;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001130 }
1131 else
zeertzjq474891b2023-04-12 21:36:03 +01001132 ++expr;
1133
1134 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
1135 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001136 {
zeertzjq474891b2023-04-12 21:36:03 +01001137 vim_strncpy(op, expr - len, len);
1138 semsg(_(e_white_space_required_before_and_after_str_at_str),
1139 op, argend);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001140 }
zeertzjq474891b2023-04-12 21:36:03 +01001141
1142 if (eap->skip)
1143 ++emsg_skip;
1144 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
1145 expr = skipwhite_and_linebreak(expr, &evalarg);
1146 cur_lnum = SOURCING_LNUM;
1147 int eval_res = eval0(expr, &rettv, eap, &evalarg);
1148 if (eap->skip)
1149 --emsg_skip;
1150 clear_evalarg(&evalarg, eap);
1151
1152 // Restore the line number so that any type error is given for the
1153 // declaration, not the expression.
1154 SOURCING_LNUM = cur_lnum;
1155
1156 if (!eap->skip && eval_res != FAIL)
1157 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
1158 flags, op);
1159 if (eval_res != FAIL)
1160 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001161}
1162
1163/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001164 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001165 * Handles both "var" with any type and "[var, var; var]" with a list type.
1166 * When "op" is not NULL it points to a string with characters that
1167 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1168 * or concatenate.
1169 * Returns OK or FAIL;
1170 */
1171 int
1172ex_let_vars(
1173 char_u *arg_start,
1174 typval_T *tv,
1175 int copy, // copy values from "tv", don't move
1176 int semicolon, // from skip_var_list()
1177 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001178 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001179 char_u *op)
1180{
1181 char_u *arg = arg_start;
1182 list_T *l;
1183 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001184 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001185 listitem_T *item;
1186 typval_T ltv;
1187
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00001188 if (tv->v_type == VAR_VOID)
1189 {
1190 emsg(_(e_cannot_use_void_value));
1191 return FAIL;
1192 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001193 if (*arg != '[')
1194 {
1195 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001196 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001197 return FAIL;
1198 return OK;
1199 }
1200
1201 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1202 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
1203 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001204 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001205 return FAIL;
1206 }
1207
1208 i = list_len(l);
1209 if (semicolon == 0 && var_count < i)
1210 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001211 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001212 return FAIL;
1213 }
1214 if (var_count - semicolon > i)
1215 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001216 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001217 return FAIL;
1218 }
1219
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001220 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001221 item = l->lv_first;
1222 while (*arg != ']')
1223 {
1224 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001225 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001226 arg = ex_let_one(arg, &item->li_tv, TRUE,
1227 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001228 item = item->li_next;
1229 if (arg == NULL)
1230 return FAIL;
1231
1232 arg = skipwhite(arg);
1233 if (*arg == ';')
1234 {
1235 // Put the rest of the list (may be empty) in the var after ';'.
1236 // Create a new list for this.
1237 l = list_alloc();
1238 if (l == NULL)
1239 return FAIL;
1240 while (item != NULL)
1241 {
1242 list_append_tv(l, &item->li_tv);
1243 item = item->li_next;
1244 }
1245
1246 ltv.v_type = VAR_LIST;
1247 ltv.v_lock = 0;
1248 ltv.vval.v_list = l;
1249 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001250 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001251
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001252 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1253 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001254 clear_tv(&ltv);
1255 if (arg == NULL)
1256 return FAIL;
1257 break;
1258 }
1259 else if (*arg != ',' && *arg != ']')
1260 {
1261 internal_error("ex_let_vars()");
1262 return FAIL;
1263 }
1264 }
1265
1266 return OK;
1267}
1268
1269/*
1270 * Skip over assignable variable "var" or list of variables "[var, var]".
1271 * Used for ":let varvar = expr" and ":for varvar in expr".
1272 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001273 * for "[var, var; var]" set "semicolon" to 1.
1274 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001275 * Return NULL for an error.
1276 */
1277 char_u *
1278skip_var_list(
1279 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001280 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001281 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001282 int *semicolon,
1283 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001284{
1285 char_u *p, *s;
1286
1287 if (*arg == '[')
1288 {
1289 // "[var, var]": find the matching ']'.
1290 p = arg;
1291 for (;;)
1292 {
1293 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001294 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001295 if (s == p)
1296 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001297 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001298 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001299 return NULL;
1300 }
1301 ++*var_count;
1302
1303 p = skipwhite(s);
1304 if (*p == ']')
1305 break;
1306 else if (*p == ';')
1307 {
1308 if (*semicolon == 1)
1309 {
Bram Moolenaar8b716f52022-02-15 21:17:56 +00001310 if (!silent)
1311 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001312 return NULL;
1313 }
1314 *semicolon = 1;
1315 }
1316 else if (*p != ',')
1317 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001318 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001319 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001320 return NULL;
1321 }
1322 }
1323 return p + 1;
1324 }
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01001325
Bram Moolenaare8e369a2022-09-21 18:59:14 +01001326 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001327}
1328
1329/*
1330 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1331 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001333 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001334 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001336{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001337 char_u *end;
1338 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001340 if (*arg == '@' && arg[1] != NUL)
1341 return arg + 2;
Bram Moolenaar1573e732022-11-16 20:33:21 +00001342
1343 // termcap option name may have non-alpha characters
1344 if (STRNCMP(arg, "&t_", 3) == 0 && arg[3] != NUL && arg[4] != NUL)
1345 return arg + 5;
1346
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001348 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001349
1350 // "a: type" is declaring variable "a" with a type, not "a:".
1351 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001352 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001353 --end;
1354
Bram Moolenaar585587d2021-01-17 20:52:13 +01001355 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 {
Bram Moolenaarce93d162023-01-30 21:12:34 +00001357 if (*skipwhite(end) == ':')
1358 end = skip_type(skipwhite(skipwhite(end) + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 }
1360 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001361}
1362
1363/*
1364 * List variables for hashtab "ht" with prefix "prefix".
1365 * If "empty" is TRUE also list NULL strings as empty strings.
1366 */
1367 void
1368list_hashtable_vars(
1369 hashtab_T *ht,
1370 char *prefix,
1371 int empty,
1372 int *first)
1373{
1374 hashitem_T *hi;
1375 dictitem_T *di;
1376 int todo;
1377 char_u buf[IOSIZE];
1378
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001379 int save_ht_flags = ht->ht_flags;
1380 ht->ht_flags |= HTFLAGS_FROZEN;
1381
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001382 todo = (int)ht->ht_used;
1383 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1384 {
1385 if (!HASHITEM_EMPTY(hi))
1386 {
1387 --todo;
1388 di = HI2DI(hi);
1389
1390 // apply :filter /pat/ to variable name
1391 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1392 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1393 if (message_filtered(buf))
1394 continue;
1395
1396 if (empty || di->di_tv.v_type != VAR_STRING
1397 || di->di_tv.vval.v_string != NULL)
1398 list_one_var(di, prefix, first);
1399 }
1400 }
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001401
1402 ht->ht_flags = save_ht_flags;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001403}
1404
1405/*
1406 * List global variables.
1407 */
1408 static void
1409list_glob_vars(int *first)
1410{
1411 list_hashtable_vars(&globvarht, "", TRUE, first);
1412}
1413
1414/*
1415 * List buffer variables.
1416 */
1417 static void
1418list_buf_vars(int *first)
1419{
1420 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1421}
1422
1423/*
1424 * List window variables.
1425 */
1426 static void
1427list_win_vars(int *first)
1428{
1429 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1430}
1431
1432/*
1433 * List tab page variables.
1434 */
1435 static void
1436list_tab_vars(int *first)
1437{
1438 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1439}
1440
1441/*
1442 * List variables in "arg".
1443 */
1444 static char_u *
1445list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1446{
1447 int error = FALSE;
1448 int len;
1449 char_u *name;
1450 char_u *name_start;
1451 char_u *arg_subsc;
1452 char_u *tofree;
1453 typval_T tv;
1454
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001455 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001456 {
1457 if (error || eap->skip)
1458 {
1459 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1460 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1461 {
1462 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001463 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001464 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001465 break;
1466 }
1467 }
1468 else
1469 {
1470 // get_name_len() takes care of expanding curly braces
1471 name_start = name = arg;
1472 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1473 if (len <= 0)
1474 {
1475 // This is mainly to keep test 49 working: when expanding
1476 // curly braces fails overrule the exception error message.
1477 if (len < 0 && !aborting())
1478 {
1479 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001480 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001481 break;
1482 }
1483 error = TRUE;
1484 }
1485 else
1486 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001487 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001488 if (tofree != NULL)
1489 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001490 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001491 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001492 error = TRUE;
1493 else
1494 {
1495 // handle d.key, l[idx], f(expr)
1496 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001497 if (handle_subscript(&arg, name_start, &tv,
1498 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001499 error = TRUE;
1500 else
1501 {
1502 if (arg == arg_subsc && len == 2 && name[1] == ':')
1503 {
1504 switch (*name)
1505 {
1506 case 'g': list_glob_vars(first); break;
1507 case 'b': list_buf_vars(first); break;
1508 case 'w': list_win_vars(first); break;
1509 case 't': list_tab_vars(first); break;
1510 case 'v': list_vim_vars(first); break;
1511 case 's': list_script_vars(first); break;
1512 case 'l': list_func_vars(first); break;
1513 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001514 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001515 }
1516 }
1517 else
1518 {
1519 char_u numbuf[NUMBUFLEN];
1520 char_u *tf;
1521 int c;
1522 char_u *s;
1523
1524 s = echo_string(&tv, &tf, numbuf, 0);
1525 c = *arg;
1526 *arg = NUL;
1527 list_one_var_a("",
1528 arg == arg_subsc ? name : name_start,
1529 tv.v_type,
1530 s == NULL ? (char_u *)"" : s,
1531 first);
1532 *arg = c;
1533 vim_free(tf);
1534 }
1535 clear_tv(&tv);
1536 }
1537 }
1538 }
1539
1540 vim_free(tofree);
1541 }
1542
1543 arg = skipwhite(arg);
1544 }
1545
1546 return arg;
1547}
1548
1549/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001550 * Set an environment variable, part of ex_let_one().
1551 */
1552 static char_u *
1553ex_let_env(
1554 char_u *arg,
1555 typval_T *tv,
1556 int flags,
1557 char_u *endchars,
1558 char_u *op)
1559{
1560 char_u *arg_end = NULL;
1561 char_u *name;
1562 int len;
1563
1564 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1565 && (flags & ASSIGN_FOR_LOOP) == 0)
1566 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001567 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001568 return NULL;
1569 }
1570
1571 // Find the end of the name.
1572 ++arg;
1573 name = arg;
1574 len = get_env_len(&arg);
1575 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001576 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001577 else
1578 {
1579 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001580 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001581 else if (endchars != NULL
1582 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1583 emsg(_(e_unexpected_characters_in_let));
1584 else if (!check_secure())
1585 {
1586 char_u *tofree = NULL;
1587 int c1 = name[len];
1588 char_u *p;
1589
1590 name[len] = NUL;
1591 p = tv_get_string_chk(tv);
1592 if (p != NULL && op != NULL && *op == '.')
1593 {
1594 int mustfree = FALSE;
1595 char_u *s = vim_getenv(name, &mustfree);
1596
1597 if (s != NULL)
1598 {
1599 p = tofree = concat_str(s, p);
1600 if (mustfree)
1601 vim_free(s);
1602 }
1603 }
1604 if (p != NULL)
1605 {
1606 vim_setenv_ext(name, p);
1607 arg_end = arg;
1608 }
1609 name[len] = c1;
1610 vim_free(tofree);
1611 }
1612 }
1613 return arg_end;
1614}
1615
1616/*
1617 * Set an option, part of ex_let_one().
1618 */
1619 static char_u *
1620ex_let_option(
1621 char_u *arg,
1622 typval_T *tv,
1623 int flags,
1624 char_u *endchars,
1625 char_u *op)
1626{
1627 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001628 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001629 char_u *arg_end = NULL;
1630
1631 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1632 && (flags & ASSIGN_FOR_LOOP) == 0)
1633 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001634 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001635 return NULL;
1636 }
1637
1638 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001639 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001640 if (p == NULL || (endchars != NULL
1641 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1642 emsg(_(e_unexpected_characters_in_let));
1643 else
1644 {
1645 int c1;
1646 long n = 0;
1647 getoption_T opt_type;
1648 long numval;
1649 char_u *stringval = NULL;
1650 char_u *s = NULL;
1651 int failed = FALSE;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001652 int opt_p_flags;
1653 char_u *tofree = NULL;
Bram Moolenaar92c33eb2021-12-07 11:03:39 +00001654 char_u numbuf[NUMBUFLEN];
1655
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001656 c1 = *p;
1657 *p = NUL;
1658
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001659 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags,
1660 scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001661 if ((opt_type == gov_bool
1662 || opt_type == gov_number
1663 || opt_type == gov_hidden_bool
1664 || opt_type == gov_hidden_number)
1665 && (tv->v_type != VAR_STRING || !in_vim9script()))
1666 {
1667 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1668 // bool, possibly hidden
1669 n = (long)tv_get_bool(tv);
1670 else
1671 // number, possibly hidden
1672 n = (long)tv_get_number(tv);
1673 }
1674
Bram Moolenaaref082e12021-12-12 21:02:03 +00001675 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001676 || tv->v_type == VAR_FUNC))
1677 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001678 // If the option can be set to a function reference or a lambda
1679 // and the passed value is a function reference, then convert it to
1680 // the name (string) of the function reference.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001681 s = tv2string(tv, &tofree, numbuf, 0);
1682 }
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001683 // Avoid setting a string option to the text "v:false" or similar.
1684 // In Vim9 script also don't convert a number to string.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001685 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001686 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1687 s = tv_get_string_chk(tv);
1688
1689 if (op != NULL && *op != '=')
1690 {
1691 if (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1692 || (opt_type == gov_string && *op != '.'))
1693 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001694 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001695 failed = TRUE; // don't set the value
1696
1697 }
1698 else
1699 {
1700 // number, in legacy script also bool
1701 if (opt_type == gov_number
1702 || (opt_type == gov_bool && !in_vim9script()))
1703 {
1704 switch (*op)
1705 {
1706 case '+': n = numval + n; break;
1707 case '-': n = numval - n; break;
1708 case '*': n = numval * n; break;
1709 case '/': n = (long)num_divide(numval, n,
1710 &failed); break;
1711 case '%': n = (long)num_modulus(numval, n,
1712 &failed); break;
1713 }
1714 s = NULL;
1715 }
1716 else if (opt_type == gov_string
1717 && stringval != NULL && s != NULL)
1718 {
1719 // string
1720 s = concat_str(stringval, s);
1721 vim_free(stringval);
1722 stringval = s;
1723 }
1724 }
1725 }
1726
1727 if (!failed)
1728 {
1729 if (opt_type != gov_string || s != NULL)
1730 {
Bram Moolenaar8ccbbeb2022-03-02 19:49:38 +00001731 char *err = set_option_value(arg, n, s, scope);
1732
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001733 arg_end = p;
Bram Moolenaar8ccbbeb2022-03-02 19:49:38 +00001734 if (err != NULL)
1735 emsg(_(err));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001736 }
1737 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001738 emsg(_(e_string_required));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001739 }
1740 *p = c1;
1741 vim_free(stringval);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001742 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001743 }
1744 return arg_end;
1745}
1746
1747/*
1748 * Set a register, part of ex_let_one().
1749 */
1750 static char_u *
1751ex_let_register(
1752 char_u *arg,
1753 typval_T *tv,
1754 int flags,
1755 char_u *endchars,
1756 char_u *op)
1757{
1758 char_u *arg_end = NULL;
1759
1760 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1761 && (flags & ASSIGN_FOR_LOOP) == 0)
1762 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001763 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001764 return NULL;
1765 }
1766 ++arg;
1767 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001768 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001769 else if (endchars != NULL
1770 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1771 emsg(_(e_unexpected_characters_in_let));
1772 else
1773 {
1774 char_u *ptofree = NULL;
1775 char_u *p;
1776
1777 p = tv_get_string_chk(tv);
1778 if (p != NULL && op != NULL && *op == '.')
1779 {
1780 char_u *s = get_reg_contents(*arg == '@'
1781 ? '"' : *arg, GREG_EXPR_SRC);
1782
1783 if (s != NULL)
1784 {
1785 p = ptofree = concat_str(s, p);
1786 vim_free(s);
1787 }
1788 }
1789 if (p != NULL)
1790 {
1791 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1792 arg_end = arg + 1;
1793 }
1794 vim_free(ptofree);
1795 }
1796 return arg_end;
1797}
1798
1799/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001800 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1801 * Returns a pointer to the char just after the var name.
1802 * Returns NULL if there is an error.
1803 */
1804 static char_u *
1805ex_let_one(
1806 char_u *arg, // points to variable name
1807 typval_T *tv, // value to assign to variable
1808 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001809 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001810 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001811 char_u *op, // "+", "-", "." or NULL
1812 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001813{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001814 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001815
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001816 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001817 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001818 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1819 {
1820 vim9_declare_error(arg);
1821 return NULL;
1822 }
1823
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001824 if (*arg == '$')
1825 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001826 // ":let $VAR = expr": Set environment variable.
1827 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001828 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001829 else if (*arg == '&')
1830 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001831 // ":let &option = expr": Set option value.
1832 // ":let &l:option = expr": Set local option value.
1833 // ":let &g:option = expr": Set global option value.
1834 // ":for &ts in range(8)": Set option value for for loop
1835 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001836 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001837 else if (*arg == '@')
1838 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001839 // ":let @r = expr": Set register contents.
1840 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001841 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001842 else if (eval_isnamec1(*arg) || *arg == '{')
1843 {
1844 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001845 char_u *p;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001846 int lval_flags = (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1847 ? GLV_NO_DECL : 0;
1848 if (op != NULL && *op != '=')
1849 lval_flags |= GLV_ASSIGN_WITH_OP;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001850
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001851 // ":let var = expr": Set internal variable.
1852 // ":let var: type = expr": Set internal variable with type.
1853 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001854 p = get_lval(arg, tv, &lv, FALSE, FALSE, lval_flags, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001855 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001856 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001857 if (endchars != NULL && vim_strchr(endchars,
1858 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001859 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001860 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001861 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001862 else
1863 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001864 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001865 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001866 }
1867 }
1868 clear_lval(&lv);
1869 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001870 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001871 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001872
1873 return arg_end;
1874}
1875
1876/*
1877 * ":unlet[!] var1 ... " command.
1878 */
1879 void
1880ex_unlet(exarg_T *eap)
1881{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001882 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001883}
1884
1885/*
1886 * ":lockvar" and ":unlockvar" commands
1887 */
1888 void
1889ex_lockvar(exarg_T *eap)
1890{
1891 char_u *arg = eap->arg;
1892 int deep = 2;
1893
1894 if (eap->forceit)
1895 deep = -1;
1896 else if (vim_isdigit(*arg))
1897 {
1898 deep = getdigits(&arg);
1899 arg = skipwhite(arg);
1900 }
1901
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001902 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001903}
1904
1905/*
1906 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001907 * Also used for Vim9 script. "callback" is invoked as:
1908 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001909 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001910 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001911ex_unletlock(
1912 exarg_T *eap,
1913 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001914 int deep,
1915 int glv_flags,
1916 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1917 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001918{
1919 char_u *arg = argstart;
1920 char_u *name_end;
1921 int error = FALSE;
1922 lval_T lv;
1923
1924 do
1925 {
1926 if (*arg == '$')
1927 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001928 lv.ll_name = arg;
1929 lv.ll_tv = NULL;
1930 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001931 if (get_env_len(&arg) == 0)
1932 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001933 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001934 return;
1935 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001936 if (!error && !eap->skip
1937 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1938 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001939 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001940 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001941 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001942 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001943 // Parse the name and find the end.
1944 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001945 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001946 if (lv.ll_name == NULL)
1947 error = TRUE; // error but continue parsing
1948 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1949 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001950 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001951 if (name_end != NULL)
1952 {
1953 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00001954 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001955 }
1956 if (!(eap->skip || error))
1957 clear_lval(&lv);
1958 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001959 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001960
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001961 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001962 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001963 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001964
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001965 if (!eap->skip)
1966 clear_lval(&lv);
1967 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001968
1969 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001970 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001971
Bram Moolenaar63b91732021-08-05 20:40:03 +02001972 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001973}
1974
1975 static int
1976do_unlet_var(
1977 lval_T *lp,
1978 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001979 exarg_T *eap,
1980 int deep UNUSED,
1981 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001982{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001983 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001984 int ret = OK;
1985 int cc;
1986
1987 if (lp->ll_tv == NULL)
1988 {
1989 cc = *name_end;
1990 *name_end = NUL;
1991
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001992 // Environment variable, normal name or expanded name.
1993 if (*lp->ll_name == '$')
LemonBoy77142312022-04-15 20:50:46 +01001994 vim_unsetenv_ext(lp->ll_name + 1);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001995 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001996 ret = FAIL;
1997 *name_end = cc;
1998 }
1999 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002000 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002001 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002002 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002003 return FAIL;
2004 else if (lp->ll_range)
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002005 list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_n1,
2006 !lp->ll_empty2, lp->ll_n2);
2007 else if (lp->ll_list != NULL)
2008 // unlet a List item.
2009 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002010 else
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002011 // unlet a Dictionary item.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002012 dictitem_remove(lp->ll_dict, lp->ll_di, "unlet");
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002013
2014 return ret;
2015}
2016
2017/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002018 * Unlet one item or a range of items from a list.
2019 * Return OK or FAIL.
2020 */
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002021 void
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002022list_unlet_range(
2023 list_T *l,
2024 listitem_T *li_first,
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002025 long n1_arg,
2026 int has_n2,
2027 long n2)
2028{
2029 listitem_T *li = li_first;
2030 int n1 = n1_arg;
2031
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002032 // Delete a range of List items.
2033 li = li_first;
2034 n1 = n1_arg;
2035 while (li != NULL && (!has_n2 || n2 >= n1))
2036 {
2037 listitem_T *next = li->li_next;
2038
2039 listitem_remove(l, li);
2040 li = next;
2041 ++n1;
2042 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002043}
2044/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002045 * "unlet" a variable. Return OK if it existed, FAIL if not.
2046 * When "forceit" is TRUE don't complain if the variable doesn't exist.
2047 */
2048 int
2049do_unlet(char_u *name, int forceit)
2050{
2051 hashtab_T *ht;
2052 hashitem_T *hi;
mityu8be36ee2022-05-25 17:29:46 +01002053 char_u *varname = NULL; // init to shut up gcc
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002054 dict_T *d;
2055 dictitem_T *di;
2056
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002057 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002058 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002059 return FAIL;
2060
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002061 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002062
2063 // can't :unlet a script variable in Vim9 script from a function
2064 if (ht == get_script_local_ht()
2065 && SCRIPT_ID_VALID(current_sctx.sc_sid)
2066 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2067 == SCRIPT_VERSION_VIM9
2068 && check_vim9_unlet(name) == FAIL)
2069 return FAIL;
2070
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002071 if (ht != NULL && *varname != NUL)
2072 {
2073 d = get_current_funccal_dict(ht);
2074 if (d == NULL)
2075 {
2076 if (ht == &globvarht)
2077 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002078 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002079 d = &vimvardict;
2080 else
2081 {
2082 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2083 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2084 }
2085 if (d == NULL)
2086 {
2087 internal_error("do_unlet()");
2088 return FAIL;
2089 }
2090 }
2091 hi = hash_find(ht, varname);
2092 if (HASHITEM_EMPTY(hi))
2093 hi = find_hi_in_scoped_ht(name, &ht);
2094 if (hi != NULL && !HASHITEM_EMPTY(hi))
2095 {
2096 di = HI2DI(hi);
2097 if (var_check_fixed(di->di_flags, name, FALSE)
2098 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002099 || value_check_lock(d->dv_lock, name, FALSE)
2100 || check_hashtab_frozen(ht, "unlet"))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002101 return FAIL;
2102
2103 delete_var(ht, hi);
2104 return OK;
2105 }
2106 }
2107 if (forceit)
2108 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00002109 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002110 return FAIL;
2111}
2112
2113/*
2114 * Lock or unlock variable indicated by "lp".
2115 * "deep" is the levels to go (-1 for unlimited);
2116 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2117 */
2118 static int
2119do_lock_var(
2120 lval_T *lp,
2121 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002122 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002123 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002124 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002125{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002126 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002127 int ret = OK;
2128 int cc;
2129 dictitem_T *di;
2130
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002131 if (lp->ll_tv == NULL)
2132 {
2133 cc = *name_end;
2134 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002135 if (*lp->ll_name == '$')
2136 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002137 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002138 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002139 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002140 else
2141 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002142 // Normal name or expanded name.
2143 di = find_var(lp->ll_name, NULL, TRUE);
2144 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002145 {
2146 if (in_vim9script())
2147 semsg(_(e_cannot_find_variable_to_unlock_str),
2148 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002149 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002150 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002151 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002152 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002153 if ((di->di_flags & DI_FLAGS_FIX)
2154 && di->di_tv.v_type != VAR_DICT
2155 && di->di_tv.v_type != VAR_LIST)
2156 {
2157 // For historic reasons this error is not given for a list
2158 // or dict. E.g., the b: dict could be locked/unlocked.
2159 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
2160 ret = FAIL;
2161 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002162 else
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002163 {
2164 if (in_vim9script())
2165 {
2166 svar_T *sv = find_typval_in_script(&di->di_tv,
2167 0, FALSE);
2168
2169 if (sv != NULL && sv->sv_const != 0)
2170 {
2171 semsg(_(e_cannot_change_readonly_variable_str),
2172 lp->ll_name);
2173 ret = FAIL;
2174 }
2175 }
2176
2177 if (ret == OK)
2178 {
2179 if (lock)
2180 di->di_flags |= DI_FLAGS_LOCK;
2181 else
2182 di->di_flags &= ~DI_FLAGS_LOCK;
2183 if (deep != 0)
2184 item_lock(&di->di_tv, deep, lock, FALSE);
2185 }
2186 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002187 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002188 }
2189 *name_end = cc;
2190 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002191 else if (deep == 0)
2192 {
2193 // nothing to do
2194 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002195 else if (lp->ll_range)
2196 {
2197 listitem_T *li = lp->ll_li;
2198
2199 // (un)lock a range of List items.
2200 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2201 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02002202 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002203 li = li->li_next;
2204 ++lp->ll_n1;
2205 }
2206 }
2207 else if (lp->ll_list != NULL)
2208 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002209 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002210 else
2211 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002212 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002213
2214 return ret;
2215}
2216
2217/*
2218 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002219 * When "check_refcount" is TRUE do not lock a list or dict with a reference
2220 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002221 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002222 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02002223item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002224{
2225 static int recurse = 0;
2226 list_T *l;
2227 listitem_T *li;
2228 dict_T *d;
2229 blob_T *b;
2230 hashitem_T *hi;
2231 int todo;
2232
2233 if (recurse >= DICT_MAXNEST)
2234 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002235 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002236 return;
2237 }
2238 if (deep == 0)
2239 return;
2240 ++recurse;
2241
2242 // lock/unlock the item itself
2243 if (lock)
2244 tv->v_lock |= VAR_LOCKED;
2245 else
2246 tv->v_lock &= ~VAR_LOCKED;
2247
2248 switch (tv->v_type)
2249 {
2250 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002251 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002253 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002254 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002255 case VAR_STRING:
2256 case VAR_FUNC:
2257 case VAR_PARTIAL:
2258 case VAR_FLOAT:
2259 case VAR_SPECIAL:
2260 case VAR_JOB:
2261 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002262 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002263 case VAR_CLASS:
2264 case VAR_OBJECT:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002265 break;
2266
2267 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002268 if ((b = tv->vval.v_blob) != NULL
2269 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002270 {
2271 if (lock)
2272 b->bv_lock |= VAR_LOCKED;
2273 else
2274 b->bv_lock &= ~VAR_LOCKED;
2275 }
2276 break;
2277 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002278 if ((l = tv->vval.v_list) != NULL
2279 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002280 {
2281 if (lock)
2282 l->lv_lock |= VAR_LOCKED;
2283 else
2284 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002285 if (deep < 0 || deep > 1)
2286 {
2287 if (l->lv_first == &range_list_item)
2288 l->lv_lock |= VAR_ITEMS_LOCKED;
2289 else
2290 {
2291 // recursive: lock/unlock the items the List contains
2292 CHECK_LIST_MATERIALIZE(l);
2293 FOR_ALL_LIST_ITEMS(l, li) item_lock(&li->li_tv,
2294 deep - 1, lock, check_refcount);
2295 }
2296 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002297 }
2298 break;
2299 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002300 if ((d = tv->vval.v_dict) != NULL
2301 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002302 {
2303 if (lock)
2304 d->dv_lock |= VAR_LOCKED;
2305 else
2306 d->dv_lock &= ~VAR_LOCKED;
2307 if (deep < 0 || deep > 1)
2308 {
2309 // recursive: lock/unlock the items the List contains
2310 todo = (int)d->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002311 FOR_ALL_HASHTAB_ITEMS(&d->dv_hashtab, hi, todo)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002312 {
2313 if (!HASHITEM_EMPTY(hi))
2314 {
2315 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002316 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2317 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002318 }
2319 }
2320 }
2321 }
2322 }
2323 --recurse;
2324}
2325
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002326#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2327/*
2328 * Delete all "menutrans_" variables.
2329 */
2330 void
2331del_menutrans_vars(void)
2332{
2333 hashitem_T *hi;
2334 int todo;
2335
2336 hash_lock(&globvarht);
2337 todo = (int)globvarht.ht_used;
2338 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2339 {
2340 if (!HASHITEM_EMPTY(hi))
2341 {
2342 --todo;
2343 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2344 delete_var(&globvarht, hi);
2345 }
2346 }
2347 hash_unlock(&globvarht);
2348}
2349#endif
2350
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002351/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002352 * Local string buffer for the next two functions to store a variable name
2353 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2354 * get_user_var_name().
2355 */
2356
2357static char_u *varnamebuf = NULL;
2358static int varnamebuflen = 0;
2359
2360/*
2361 * Function to concatenate a prefix and a variable name.
2362 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002363 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002364cat_prefix_varname(int prefix, char_u *name)
2365{
2366 int len;
2367
2368 len = (int)STRLEN(name) + 3;
2369 if (len > varnamebuflen)
2370 {
2371 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002372 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002373 varnamebuf = alloc(len);
2374 if (varnamebuf == NULL)
2375 {
2376 varnamebuflen = 0;
2377 return NULL;
2378 }
2379 varnamebuflen = len;
2380 }
2381 *varnamebuf = prefix;
2382 varnamebuf[1] = ':';
2383 STRCPY(varnamebuf + 2, name);
2384 return varnamebuf;
2385}
2386
2387/*
2388 * Function given to ExpandGeneric() to obtain the list of user defined
2389 * (global/buffer/window/built-in) variable names.
2390 */
2391 char_u *
2392get_user_var_name(expand_T *xp, int idx)
2393{
2394 static long_u gdone;
2395 static long_u bdone;
2396 static long_u wdone;
2397 static long_u tdone;
2398 static int vidx;
2399 static hashitem_T *hi;
2400 hashtab_T *ht;
2401
2402 if (idx == 0)
2403 {
2404 gdone = bdone = wdone = vidx = 0;
2405 tdone = 0;
2406 }
2407
2408 // Global variables
2409 if (gdone < globvarht.ht_used)
2410 {
2411 if (gdone++ == 0)
2412 hi = globvarht.ht_array;
2413 else
2414 ++hi;
2415 while (HASHITEM_EMPTY(hi))
2416 ++hi;
2417 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2418 return cat_prefix_varname('g', hi->hi_key);
2419 return hi->hi_key;
2420 }
2421
2422 // b: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002423 ht = &prevwin_curwin()->w_buffer->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002424 if (bdone < ht->ht_used)
2425 {
2426 if (bdone++ == 0)
2427 hi = ht->ht_array;
2428 else
2429 ++hi;
2430 while (HASHITEM_EMPTY(hi))
2431 ++hi;
2432 return cat_prefix_varname('b', hi->hi_key);
2433 }
2434
2435 // w: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002436 ht = &prevwin_curwin()->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002437 if (wdone < ht->ht_used)
2438 {
2439 if (wdone++ == 0)
2440 hi = ht->ht_array;
2441 else
2442 ++hi;
2443 while (HASHITEM_EMPTY(hi))
2444 ++hi;
2445 return cat_prefix_varname('w', hi->hi_key);
2446 }
2447
2448 // t: variables
2449 ht = &curtab->tp_vars->dv_hashtab;
2450 if (tdone < ht->ht_used)
2451 {
2452 if (tdone++ == 0)
2453 hi = ht->ht_array;
2454 else
2455 ++hi;
2456 while (HASHITEM_EMPTY(hi))
2457 ++hi;
2458 return cat_prefix_varname('t', hi->hi_key);
2459 }
2460
2461 // v: variables
2462 if (vidx < VV_LEN)
2463 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2464
2465 VIM_CLEAR(varnamebuf);
2466 varnamebuflen = 0;
2467 return NULL;
2468}
2469
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002470 char *
2471get_var_special_name(int nr)
2472{
2473 switch (nr)
2474 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002475 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2476 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002477 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002478 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002479 }
2480 internal_error("get_var_special_name()");
2481 return "42";
2482}
2483
2484/*
2485 * Returns the global variable dictionary
2486 */
2487 dict_T *
2488get_globvar_dict(void)
2489{
2490 return &globvardict;
2491}
2492
2493/*
2494 * Returns the global variable hash table
2495 */
2496 hashtab_T *
2497get_globvar_ht(void)
2498{
2499 return &globvarht;
2500}
2501
2502/*
2503 * Returns the v: variable dictionary
2504 */
2505 dict_T *
2506get_vimvar_dict(void)
2507{
2508 return &vimvardict;
2509}
2510
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002511/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002513 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 */
2515 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002516find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002518 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2519 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520
2521 if (di == NULL)
2522 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002523 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2525 return (int)(vv - vimvars);
2526}
2527
2528
2529/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002530 * Set type of v: variable to "type".
2531 */
2532 void
2533set_vim_var_type(int idx, vartype_T type)
2534{
Bram Moolenaard787e402021-12-24 21:36:12 +00002535 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002536}
2537
2538/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002539 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002540 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002541 */
2542 void
2543set_vim_var_nr(int idx, varnumber_T val)
2544{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002545 vimvars[idx].vv_nr = val;
2546}
2547
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002548 char *
2549get_vim_var_name(int idx)
2550{
2551 return vimvars[idx].vv_name;
2552}
2553
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002554/*
2555 * Get typval_T v: variable value.
2556 */
2557 typval_T *
2558get_vim_var_tv(int idx)
2559{
2560 return &vimvars[idx].vv_tv;
2561}
2562
Bram Moolenaard787e402021-12-24 21:36:12 +00002563 type_T *
2564get_vim_var_type(int idx, garray_T *type_list)
2565{
2566 if (vimvars[idx].vv_type != NULL)
2567 return vimvars[idx].vv_type;
2568 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2569}
2570
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002571/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002572 * Set v: variable to "tv". Only accepts the same type.
2573 * Takes over the value of "tv".
2574 */
2575 int
2576set_vim_var_tv(int idx, typval_T *tv)
2577{
Bram Moolenaard787e402021-12-24 21:36:12 +00002578 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002579 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002580 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002581 clear_tv(tv);
2582 return FAIL;
2583 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002584 // VV_RO is also checked when compiling, but let's check here as well.
2585 if (vimvars[idx].vv_flags & VV_RO)
2586 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002587 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002588 return FAIL;
2589 }
2590 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2591 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002592 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002593 return FAIL;
2594 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002595 clear_tv(&vimvars[idx].vv_di.di_tv);
2596 vimvars[idx].vv_di.di_tv = *tv;
2597 return OK;
2598}
2599
2600/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002601 * Get number v: variable value.
2602 */
2603 varnumber_T
2604get_vim_var_nr(int idx)
2605{
2606 return vimvars[idx].vv_nr;
2607}
2608
2609/*
2610 * Get string v: variable value. Uses a static buffer, can only be used once.
2611 * If the String variable has never been set, return an empty string.
2612 * Never returns NULL;
2613 */
2614 char_u *
2615get_vim_var_str(int idx)
2616{
2617 return tv_get_string(&vimvars[idx].vv_tv);
2618}
2619
2620/*
2621 * Get List v: variable value. Caller must take care of reference count when
2622 * needed.
2623 */
2624 list_T *
2625get_vim_var_list(int idx)
2626{
2627 return vimvars[idx].vv_list;
2628}
2629
2630/*
2631 * Get Dict v: variable value. Caller must take care of reference count when
2632 * needed.
2633 */
2634 dict_T *
2635get_vim_var_dict(int idx)
2636{
2637 return vimvars[idx].vv_dict;
2638}
2639
2640/*
2641 * Set v:char to character "c".
2642 */
2643 void
2644set_vim_var_char(int c)
2645{
2646 char_u buf[MB_MAXBYTES + 1];
2647
2648 if (has_mbyte)
2649 buf[(*mb_char2bytes)(c, buf)] = NUL;
2650 else
2651 {
2652 buf[0] = c;
2653 buf[1] = NUL;
2654 }
2655 set_vim_var_string(VV_CHAR, buf, -1);
2656}
2657
2658/*
2659 * Set v:count to "count" and v:count1 to "count1".
2660 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2661 */
2662 void
2663set_vcount(
2664 long count,
2665 long count1,
2666 int set_prevcount)
2667{
2668 if (set_prevcount)
2669 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2670 vimvars[VV_COUNT].vv_nr = count;
2671 vimvars[VV_COUNT1].vv_nr = count1;
2672}
2673
2674/*
2675 * Save variables that might be changed as a side effect. Used when executing
2676 * a timer callback.
2677 */
2678 void
2679save_vimvars(vimvars_save_T *vvsave)
2680{
2681 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2682 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2683 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2684}
2685
2686/*
2687 * Restore variables saved by save_vimvars().
2688 */
2689 void
2690restore_vimvars(vimvars_save_T *vvsave)
2691{
2692 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2693 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2694 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2695}
2696
2697/*
2698 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2699 * value.
2700 */
2701 void
2702set_vim_var_string(
2703 int idx,
2704 char_u *val,
2705 int len) // length of "val" to use or -1 (whole string)
2706{
2707 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002708 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002709 if (val == NULL)
2710 vimvars[idx].vv_str = NULL;
2711 else if (len == -1)
2712 vimvars[idx].vv_str = vim_strsave(val);
2713 else
2714 vimvars[idx].vv_str = vim_strnsave(val, len);
2715}
2716
2717/*
2718 * Set List v: variable to "val".
2719 */
2720 void
2721set_vim_var_list(int idx, list_T *val)
2722{
2723 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002724 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002725 vimvars[idx].vv_list = val;
2726 if (val != NULL)
2727 ++val->lv_refcount;
2728}
2729
2730/*
2731 * Set Dictionary v: variable to "val".
2732 */
2733 void
2734set_vim_var_dict(int idx, dict_T *val)
2735{
2736 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002737 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002738 vimvars[idx].vv_dict = val;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002739 if (val == NULL)
2740 return;
2741
2742 ++val->dv_refcount;
2743 dict_set_items_ro(val);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002744}
2745
2746/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002747 * Set the v:argv list.
2748 */
2749 void
2750set_argv_var(char **argv, int argc)
2751{
2752 list_T *l = list_alloc();
2753 int i;
2754
2755 if (l == NULL)
2756 getout(1);
2757 l->lv_lock = VAR_FIXED;
2758 for (i = 0; i < argc; ++i)
2759 {
2760 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2761 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002762 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002763 }
2764 set_vim_var_list(VV_ARGV, l);
2765}
2766
2767/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002768 * Reset v:register, taking the 'clipboard' setting into account.
2769 */
2770 void
2771reset_reg_var(void)
2772{
2773 int regname = 0;
2774
2775 // Adjust the register according to 'clipboard', so that when
2776 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2777#ifdef FEAT_CLIPBOARD
2778 adjust_clip_reg(&regname);
2779#endif
2780 set_reg_var(regname);
2781}
2782
2783/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002784 * Set v:register if needed.
2785 */
2786 void
2787set_reg_var(int c)
2788{
2789 char_u regname;
2790
2791 if (c == 0 || c == ' ')
2792 regname = '"';
2793 else
2794 regname = c;
2795 // Avoid free/alloc when the value is already right.
2796 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2797 set_vim_var_string(VV_REG, &regname, 1);
2798}
2799
2800/*
2801 * Get or set v:exception. If "oldval" == NULL, return the current value.
2802 * Otherwise, restore the value to "oldval" and return NULL.
2803 * Must always be called in pairs to save and restore v:exception! Does not
2804 * take care of memory allocations.
2805 */
2806 char_u *
2807v_exception(char_u *oldval)
2808{
2809 if (oldval == NULL)
2810 return vimvars[VV_EXCEPTION].vv_str;
2811
2812 vimvars[VV_EXCEPTION].vv_str = oldval;
2813 return NULL;
2814}
2815
2816/*
2817 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2818 * Otherwise, restore the value to "oldval" and return NULL.
2819 * Must always be called in pairs to save and restore v:throwpoint! Does not
2820 * take care of memory allocations.
2821 */
2822 char_u *
2823v_throwpoint(char_u *oldval)
2824{
2825 if (oldval == NULL)
2826 return vimvars[VV_THROWPOINT].vv_str;
2827
2828 vimvars[VV_THROWPOINT].vv_str = oldval;
2829 return NULL;
2830}
2831
2832/*
2833 * Set v:cmdarg.
2834 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2835 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2836 * Must always be called in pairs!
2837 */
2838 char_u *
2839set_cmdarg(exarg_T *eap, char_u *oldarg)
2840{
2841 char_u *oldval;
2842 char_u *newval;
2843 unsigned len;
2844
2845 oldval = vimvars[VV_CMDARG].vv_str;
2846 if (eap == NULL)
2847 {
2848 vim_free(oldval);
2849 vimvars[VV_CMDARG].vv_str = oldarg;
2850 return NULL;
2851 }
2852
2853 if (eap->force_bin == FORCE_BIN)
2854 len = 6;
2855 else if (eap->force_bin == FORCE_NOBIN)
2856 len = 8;
2857 else
2858 len = 0;
2859
2860 if (eap->read_edit)
2861 len += 7;
2862
2863 if (eap->force_ff != 0)
2864 len += 10; // " ++ff=unix"
2865 if (eap->force_enc != 0)
2866 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2867 if (eap->bad_char != 0)
2868 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2869
2870 newval = alloc(len + 1);
2871 if (newval == NULL)
2872 return NULL;
2873
2874 if (eap->force_bin == FORCE_BIN)
2875 sprintf((char *)newval, " ++bin");
2876 else if (eap->force_bin == FORCE_NOBIN)
2877 sprintf((char *)newval, " ++nobin");
2878 else
2879 *newval = NUL;
2880
2881 if (eap->read_edit)
2882 STRCAT(newval, " ++edit");
2883
2884 if (eap->force_ff != 0)
2885 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2886 eap->force_ff == 'u' ? "unix"
2887 : eap->force_ff == 'd' ? "dos"
2888 : "mac");
2889 if (eap->force_enc != 0)
2890 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2891 eap->cmd + eap->force_enc);
2892 if (eap->bad_char == BAD_KEEP)
2893 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2894 else if (eap->bad_char == BAD_DROP)
2895 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2896 else if (eap->bad_char != 0)
2897 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2898 vimvars[VV_CMDARG].vv_str = newval;
2899 return oldval;
2900}
2901
2902/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002903 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002904 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2905 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002906 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2907 */
2908 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002909eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002910 char_u *name,
Bram Moolenaar94674f22023-01-06 18:42:20 +00002911 int len, // length of "name" or zero
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002912 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002913 typval_T *rettv, // NULL when only checking existence
2914 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002915 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002916{
2917 int ret = OK;
2918 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002919 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002920 hashtab_T *ht = NULL;
Bram Moolenaar94674f22023-01-06 18:42:20 +00002921 int cc = 0;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002922 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002923
Bram Moolenaar94674f22023-01-06 18:42:20 +00002924 if (len > 0)
2925 {
2926 // truncate the name, so that we can use strcmp()
2927 cc = name[len];
2928 name[len] = NUL;
2929 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002930
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002931 // Check for local variable when debugging.
2932 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002933 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002934 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002935 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2936
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002937 if (v != NULL)
2938 {
2939 tv = &v->di_tv;
2940 if (dip != NULL)
2941 *dip = v;
2942 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002943 else
2944 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002945 }
2946
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002947 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002949 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002950 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2951
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002952 if (sid == 0)
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00002953 import = find_imported(p, 0, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954
2955 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002956 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002958 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002959 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00002960 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02002961 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002962 ht = &SCRIPT_VARS(sid);
2963 if (ht != NULL)
2964 {
2965 dictitem_T *v = find_var_in_ht(ht, 0, name,
2966 flags & EVAL_VAR_NOAUTOLOAD);
2967
2968 if (v != NULL)
2969 {
2970 tv = &v->di_tv;
2971 if (dip != NULL)
2972 *dip = v;
2973 }
2974 else
2975 ht = NULL;
2976 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002977 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002978 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002979 {
2980 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00002981 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002982 ret = FAIL;
2983 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002984 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002985 else
2986 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002987 if (rettv != NULL)
2988 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01002989 // special value that is used in handle_subscript()
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002990 rettv->v_type = VAR_ANY;
2991 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
2992 }
2993 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002994 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00002996 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002997 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002998 int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002999 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003000
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003001 // In Vim9 script we can get a function reference by using the
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003002 // function name. For a global non-autoload function "g:" is
3003 // required.
3004 if (ufunc != NULL && (has_g_prefix
3005 || !func_requires_g_prefix(ufunc)))
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003006 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003007 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003008 if (rettv != NULL)
3009 {
3010 rettv->v_type = VAR_FUNC;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003011 if (has_g_prefix)
Bram Moolenaaref082e12021-12-12 21:02:03 +00003012 // Keep the "g:", otherwise script-local may be
3013 // assumed.
3014 rettv->vval.v_string = vim_strsave(name);
3015 else
3016 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003017 if (rettv->vval.v_string != NULL)
3018 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003019 }
3020 }
3021 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 }
3023
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003024 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003025 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02003026 if (tv == NULL)
3027 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003028 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003029 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02003030 ret = FAIL;
3031 }
3032 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003033 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003034 svar_T *sv = NULL;
3035 int was_assigned = FALSE;
3036
Bram Moolenaar11005b02021-07-11 20:59:00 +02003037 if (ht != NULL && ht == get_script_local_ht()
3038 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02003039 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003040 sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaarf055d452021-07-08 20:57:24 +02003041 if (sv != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003042 {
Bram Moolenaarf055d452021-07-08 20:57:24 +02003043 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003044 was_assigned = sv->sv_flags & SVFLAG_ASSIGNED;
3045 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003046 }
3047
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003048 // If a list or dict variable wasn't initialized and has meaningful
3049 // type, do it now. Not for global variables, they are not
3050 // declared.
Bram Moolenaar7a222242022-03-01 19:23:24 +00003051 if (ht != &globvarht)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003052 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003053 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003054 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003055 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003056 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003057 tv->vval.v_dict = dict_alloc();
3058 if (tv->vval.v_dict != NULL)
3059 {
3060 ++tv->vval.v_dict->dv_refcount;
3061 tv->vval.v_dict->dv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003062 if (sv != NULL)
3063 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003064 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003065 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003066 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003067 && ((type != NULL && !was_assigned)
Bram Moolenaar501f9782022-03-27 16:51:04 +01003068 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003069 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003070 tv->vval.v_list = list_alloc();
3071 if (tv->vval.v_list != NULL)
3072 {
3073 ++tv->vval.v_list->lv_refcount;
3074 tv->vval.v_list->lv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003075 if (sv != NULL)
3076 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003077 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003078 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003079 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003080 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003081 || !in_vim9script()))
3082 {
3083 tv->vval.v_blob = blob_alloc();
3084 if (tv->vval.v_blob != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003085 {
Bram Moolenaar859cc212022-03-28 15:22:35 +01003086 ++tv->vval.v_blob->bv_refcount;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003087 if (sv != NULL)
3088 sv->sv_flags |= SVFLAG_ASSIGNED;
3089 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003090 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02003091 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003092 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003093 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003094 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003095
Bram Moolenaar94674f22023-01-06 18:42:20 +00003096 if (len > 0)
3097 name[len] = cc;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003098
3099 return ret;
3100}
3101
3102/*
Bram Moolenaara86655a2023-01-12 17:06:27 +00003103 * Get the value of internal variable "name", also handling "import.name".
3104 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
3105 */
3106 int
3107eval_variable_import(
3108 char_u *name,
3109 typval_T *rettv)
3110{
3111 char_u *s = name;
3112 while (ASCII_ISALNUM(*s) || *s == '_')
3113 ++s;
3114 int len = (int)(s - name);
3115
3116 if (eval_variable(name, len, 0, rettv, NULL, EVAL_VAR_IMPORT) == FAIL)
3117 return FAIL;
3118 if (rettv->v_type == VAR_ANY && *s == '.')
3119 {
Bram Moolenaar40594002023-01-12 20:04:51 +00003120 char_u *ns = s + 1;
3121 s = ns;
3122 while (ASCII_ISALNUM(*s) || *s == '_')
3123 ++s;
Bram Moolenaara86655a2023-01-12 17:06:27 +00003124 int sid = rettv->vval.v_number;
Bram Moolenaar40594002023-01-12 20:04:51 +00003125 return eval_variable(ns, (int)(s - ns), sid, rettv, NULL, 0);
Bram Moolenaara86655a2023-01-12 17:06:27 +00003126 }
3127 return OK;
3128}
3129
3130
3131/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003132 * Check if variable "name[len]" is a local variable or an argument.
3133 * If so, "*eval_lavars_used" is set to TRUE.
3134 */
3135 void
3136check_vars(char_u *name, int len)
3137{
3138 int cc;
3139 char_u *varname;
3140 hashtab_T *ht;
3141
3142 if (eval_lavars_used == NULL)
3143 return;
3144
3145 // truncate the name, so that we can use strcmp()
3146 cc = name[len];
3147 name[len] = NUL;
3148
3149 ht = find_var_ht(name, &varname);
3150 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
3151 {
3152 if (find_var(name, NULL, TRUE) != NULL)
3153 *eval_lavars_used = TRUE;
3154 }
3155
3156 name[len] = cc;
3157}
3158
3159/*
3160 * Find variable "name" in the list of variables.
3161 * Return a pointer to it if found, NULL if not found.
3162 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003163 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003164 */
3165 dictitem_T *
3166find_var(char_u *name, hashtab_T **htp, int no_autoload)
3167{
3168 char_u *varname;
3169 hashtab_T *ht;
3170 dictitem_T *ret = NULL;
3171
3172 ht = find_var_ht(name, &varname);
3173 if (htp != NULL)
3174 *htp = ht;
3175 if (ht == NULL)
3176 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003177 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003178 if (ret != NULL)
3179 return ret;
3180
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003181 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003182 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003183 if (ret != NULL)
3184 return ret;
3185
3186 // in Vim9 script items without a scope can be script-local
3187 if (in_vim9script() && name[0] != NUL && name[1] != ':')
3188 {
3189 ht = get_script_local_ht();
3190 if (ht != NULL)
3191 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003192 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003193 if (ret != NULL)
3194 {
3195 if (htp != NULL)
3196 *htp = ht;
3197 return ret;
3198 }
3199 }
3200 }
3201
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003202 // When using "vim9script autoload" script-local items are prefixed but can
3203 // be used with s:name.
3204 if (SCRIPT_ID_VALID(current_sctx.sc_sid)
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003205 && (in_vim9script() || (name[0] == 's' && name[1] == ':')))
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003206 {
3207 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
3208
3209 if (si->sn_autoload_prefix != NULL)
3210 {
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003211 char_u *base_name = (name[0] == 's' && name[1] == ':')
3212 ? name + 2 : name;
3213 char_u *auto_name = concat_str(si->sn_autoload_prefix, base_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003214
3215 if (auto_name != NULL)
3216 {
3217 ht = &globvarht;
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003218 ret = find_var_in_ht(ht, 'g', auto_name, TRUE);
Bram Moolenaar130f65d2022-01-13 20:39:41 +00003219 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003220 if (ret != NULL)
3221 {
3222 if (htp != NULL)
3223 *htp = ht;
3224 return ret;
3225 }
3226 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003227 }
3228 }
3229
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003230 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003231}
3232
3233/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00003234 * Like find_var() but if the name starts with <SNR>99_ then look in the
3235 * referenced script (used for a funcref).
3236 */
3237 dictitem_T *
3238find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
3239{
3240 if (STRNCMP(name, "<SNR>", 5) == 0 && isdigit(name[5]))
3241 {
3242 char_u *p = name + 5;
3243 int sid = getdigits(&p);
3244
3245 if (SCRIPT_ID_VALID(sid) && *p == '_')
3246 {
3247 hashtab_T *ht = &SCRIPT_VARS(sid);
3248
3249 if (ht != NULL)
3250 {
3251 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
3252
3253 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003254 {
3255 if (htp != NULL)
3256 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00003257 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003258 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00003259 }
3260 }
3261 }
3262
3263 return find_var(name, htp, no_autoload);
3264}
3265
3266/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003267 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02003268 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003269 * Returns NULL if not found.
3270 */
3271 dictitem_T *
3272find_var_in_ht(
3273 hashtab_T *ht,
3274 int htname,
3275 char_u *varname,
3276 int no_autoload)
3277{
3278 hashitem_T *hi;
3279
3280 if (*varname == NUL)
3281 {
3282 // Must be something like "s:", otherwise "ht" would be NULL.
3283 switch (htname)
3284 {
3285 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
3286 case 'g': return &globvars_var;
3287 case 'v': return &vimvars_var;
3288 case 'b': return &curbuf->b_bufvar;
3289 case 'w': return &curwin->w_winvar;
3290 case 't': return &curtab->tp_winvar;
3291 case 'l': return get_funccal_local_var();
3292 case 'a': return get_funccal_args_var();
3293 }
3294 return NULL;
3295 }
3296
3297 hi = hash_find(ht, varname);
3298 if (HASHITEM_EMPTY(hi))
3299 {
3300 // For global variables we may try auto-loading the script. If it
3301 // worked find the variable again. Don't auto-load a script if it was
3302 // loaded already, otherwise it would be loaded every time when
3303 // checking if a function name is a Funcref variable.
3304 if (ht == &globvarht && !no_autoload)
3305 {
3306 // Note: script_autoload() may make "hi" invalid. It must either
3307 // be obtained again or not used.
3308 if (!script_autoload(varname, FALSE) || aborting())
3309 return NULL;
3310 hi = hash_find(ht, varname);
3311 }
3312 if (HASHITEM_EMPTY(hi))
3313 return NULL;
3314 }
3315 return HI2DI(hi);
3316}
3317
3318/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 * Get the script-local hashtab. NULL if not in a script context.
3320 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003321 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003322get_script_local_ht(void)
3323{
3324 scid_T sid = current_sctx.sc_sid;
3325
Bram Moolenaare3d46852020-08-29 13:39:17 +02003326 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 return &SCRIPT_VARS(sid);
3328 return NULL;
3329}
3330
3331/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003332 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003333 * When "cmd" is TRUE it must look like a command, a function must be followed
3334 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003335 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003337 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003338lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003339 char_u *name,
3340 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003341 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003342 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343{
3344 hashtab_T *ht = get_script_local_ht();
3345 char_u buffer[30];
3346 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003347 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003349 int is_global = FALSE;
3350 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351
3352 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003353 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 if (len < sizeof(buffer) - 1)
3355 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003356 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 vim_strncpy(buffer, name, len);
3358 p = buffer;
3359 }
3360 else
3361 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003362 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003364 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365 }
3366
3367 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003368 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003369
3370 // if not script-local, then perhaps imported
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003371 if (res == FAIL && find_imported(p, 0, FALSE) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003372 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373 if (p != buffer)
3374 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003375
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003376 // Find a function, so that a following "->" works.
3377 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3378 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003379 if (res != OK)
3380 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003381 p = skipwhite(name + len);
3382
3383 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003384 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003385 // Do not check for an internal function, since it might also be a
3386 // valid command, such as ":split" versus "split()".
3387 // Skip "g:" before a function name.
3388 if (name[0] == 'g' && name[1] == ':')
3389 {
3390 is_global = TRUE;
3391 fname = name + 2;
3392 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003393 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003394 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003395 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003396 }
3397
Bram Moolenaar709664c2020-12-12 14:33:41 +01003398 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399}
3400
3401/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003402 * Find the hashtab used for a variable name.
3403 * Return NULL if the name is not valid.
3404 * Set "varname" to the start of name without ':'.
3405 */
3406 hashtab_T *
3407find_var_ht(char_u *name, char_u **varname)
3408{
3409 hashitem_T *hi;
3410 hashtab_T *ht;
3411
3412 if (name[0] == NUL)
3413 return NULL;
3414 if (name[1] != ':')
3415 {
3416 // The name must not start with a colon or #.
3417 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3418 return NULL;
3419 *varname = name;
3420
3421 // "version" is "v:version" in all scopes if scriptversion < 3.
3422 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003423 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003424 {
3425 hi = hash_find(&compat_hashtab, name);
3426 if (!HASHITEM_EMPTY(hi))
3427 return &compat_hashtab;
3428 }
3429
3430 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 if (ht != NULL)
3432 return ht; // local variable
3433
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003434 // In Vim9 script items at the script level are script-local, except
3435 // for autoload names.
3436 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 {
3438 ht = get_script_local_ht();
3439 if (ht != NULL)
3440 return ht;
3441 }
3442
3443 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003444 }
3445 *varname = name + 2;
3446 if (*name == 'g') // global variable
3447 return &globvarht;
3448 // There must be no ':' or '#' in the rest of the name, unless g: is used
3449 if (vim_strchr(name + 2, ':') != NULL
3450 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3451 return NULL;
3452 if (*name == 'b') // buffer variable
3453 return &curbuf->b_vars->dv_hashtab;
3454 if (*name == 'w') // window variable
3455 return &curwin->w_vars->dv_hashtab;
3456 if (*name == 't') // tab page variable
3457 return &curtab->tp_vars->dv_hashtab;
3458 if (*name == 'v') // v: variable
3459 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003460 if (get_current_funccal() != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01003461 && get_current_funccal()->fc_func->uf_def_status
3462 == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003464 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465 if (*name == 'a') // a: function argument
3466 return get_funccal_args_ht();
3467 if (*name == 'l') // l: local function variable
3468 return get_funccal_local_ht();
3469 }
3470 if (*name == 's') // script variable
3471 {
3472 ht = get_script_local_ht();
3473 if (ht != NULL)
3474 return ht;
3475 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003476 return NULL;
3477}
3478
3479/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003480 * Get the string value of a (global/local) variable.
3481 * Note: see tv_get_string() for how long the pointer remains valid.
3482 * Returns NULL when it doesn't exist.
3483 */
3484 char_u *
3485get_var_value(char_u *name)
3486{
3487 dictitem_T *v;
3488
3489 v = find_var(name, NULL, FALSE);
3490 if (v == NULL)
3491 return NULL;
3492 return tv_get_string(&v->di_tv);
3493}
3494
3495/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003496 * Allocate a new hashtab for a sourced script. It will be used while
3497 * sourcing this script and when executing functions defined in the script.
3498 */
3499 void
3500new_script_vars(scid_T id)
3501{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003502 scriptvar_T *sv;
3503
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003504 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3505 if (sv == NULL)
3506 return;
3507 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003508 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003509}
3510
3511/*
3512 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3513 * point to it.
3514 */
3515 void
3516init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3517{
3518 hash_init(&dict->dv_hashtab);
3519 dict->dv_lock = 0;
3520 dict->dv_scope = scope;
3521 dict->dv_refcount = DO_NOT_FREE_CNT;
3522 dict->dv_copyID = 0;
3523 dict_var->di_tv.vval.v_dict = dict;
3524 dict_var->di_tv.v_type = VAR_DICT;
3525 dict_var->di_tv.v_lock = VAR_FIXED;
3526 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3527 dict_var->di_key[0] = NUL;
3528}
3529
3530/*
3531 * Unreference a dictionary initialized by init_var_dict().
3532 */
3533 void
3534unref_var_dict(dict_T *dict)
3535{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003536 // Now the dict needs to be freed if no one else is using it, go back to
3537 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003538 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3539 dict_unref(dict);
3540}
3541
3542/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003543 * Clean up a list of internal variables.
3544 * Frees all allocated variables and the value they contain.
3545 * Clears hashtab "ht", does not free it.
3546 */
3547 void
3548vars_clear(hashtab_T *ht)
3549{
3550 vars_clear_ext(ht, TRUE);
3551}
3552
3553/*
3554 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3555 */
3556 void
3557vars_clear_ext(hashtab_T *ht, int free_val)
3558{
3559 int todo;
3560 hashitem_T *hi;
3561 dictitem_T *v;
3562
3563 hash_lock(ht);
3564 todo = (int)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003565 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003566 {
3567 if (!HASHITEM_EMPTY(hi))
3568 {
3569 --todo;
3570
3571 // Free the variable. Don't remove it from the hashtab,
3572 // ht_array might change then. hash_clear() takes care of it
3573 // later.
3574 v = HI2DI(hi);
3575 if (free_val)
3576 clear_tv(&v->di_tv);
3577 if (v->di_flags & DI_FLAGS_ALLOC)
3578 vim_free(v);
3579 }
3580 }
3581 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003582 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003583}
3584
3585/*
3586 * Delete a variable from hashtab "ht" at item "hi".
3587 * Clear the variable value and free the dictitem.
3588 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003589 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003590delete_var(hashtab_T *ht, hashitem_T *hi)
3591{
3592 dictitem_T *di = HI2DI(hi);
3593
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003594 if (hash_remove(ht, hi, "delete variable") != OK)
3595 return;
3596
3597 clear_tv(&di->di_tv);
3598 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003599}
3600
3601/*
3602 * List the value of one internal variable.
3603 */
3604 static void
3605list_one_var(dictitem_T *v, char *prefix, int *first)
3606{
3607 char_u *tofree;
3608 char_u *s;
3609 char_u numbuf[NUMBUFLEN];
3610
3611 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3612 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3613 s == NULL ? (char_u *)"" : s, first);
3614 vim_free(tofree);
3615}
3616
3617 static void
3618list_one_var_a(
3619 char *prefix,
3620 char_u *name,
3621 int type,
3622 char_u *string,
3623 int *first) // when TRUE clear rest of screen and set to FALSE
3624{
3625 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3626 msg_start();
3627 msg_puts(prefix);
3628 if (name != NULL) // "a:" vars don't have a name stored
3629 msg_puts((char *)name);
3630 msg_putchar(' ');
3631 msg_advance(22);
3632 if (type == VAR_NUMBER)
3633 msg_putchar('#');
3634 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3635 msg_putchar('*');
3636 else if (type == VAR_LIST)
3637 {
3638 msg_putchar('[');
3639 if (*string == '[')
3640 ++string;
3641 }
3642 else if (type == VAR_DICT)
3643 {
3644 msg_putchar('{');
3645 if (*string == '{')
3646 ++string;
3647 }
3648 else
3649 msg_putchar(' ');
3650
3651 msg_outtrans(string);
3652
3653 if (type == VAR_FUNC || type == VAR_PARTIAL)
3654 msg_puts("()");
3655 if (*first)
3656 {
3657 msg_clr_eos();
3658 *first = FALSE;
3659 }
3660}
3661
3662/*
3663 * Set variable "name" to value in "tv".
3664 * If the variable already exists, the value is updated.
3665 * Otherwise the variable is created.
3666 */
3667 void
3668set_var(
3669 char_u *name,
3670 typval_T *tv,
3671 int copy) // make copy of value in "tv"
3672{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003673 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003674}
3675
3676/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003677 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003678 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003679 * If the variable already exists and "is_const" is FALSE the value is updated.
3680 * Otherwise the variable is created.
3681 */
3682 void
3683set_var_const(
3684 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003685 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003686 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003687 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003688 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003689 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003690 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003691{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003692 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003693 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003694 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 dictitem_T *di;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003696 typval_T *dest_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003697 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003698 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003699 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003701 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003702 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003703 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003704 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003705 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003706
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003707 if (sid != 0)
3708 {
3709 if (SCRIPT_ID_VALID(sid))
3710 ht = &SCRIPT_VARS(sid);
3711 varname = name;
3712 }
3713 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003714 {
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003715 scriptitem_T *si;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003716
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003717 if (in_vim9script() && is_export
3718 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3719 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
Bram Moolenaar0e1574c2022-03-03 17:05:35 +00003720 ->sn_autoload_prefix != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003721 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003722 // In a vim9 autoload script an exported variable is put in the
3723 // global namespace with the autoload prefix.
3724 var_in_autoload = TRUE;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003725 varname = concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003726 if (varname == NULL)
3727 goto failed;
3728 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003729 ht = &globvarht;
3730 }
3731 else
3732 ht = find_var_ht(name, &varname);
3733 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003734 if (ht == NULL || *varname == NUL)
3735 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003736 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003737 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003738 }
Bram Moolenaar54969f42022-02-07 13:56:44 +00003739 is_script_local = ht == get_script_local_ht() || sid != 0
3740 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003741
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003742 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003743 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003744 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003745 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003746 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003747 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003748 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003749 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003750 }
Bram Moolenaar9510d222022-09-11 15:14:05 +01003751 if ((flags & ASSIGN_FOR_LOOP) && is_scoped_variable(name))
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003752 // Do not make g:var, w:var, b:var or t:var final.
3753 flags &= ~ASSIGN_FINAL;
3754
Bram Moolenaare535db82021-03-31 21:07:24 +02003755 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003756 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3757 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003758 // For "[a, _] = list" the underscore is ignored.
3759 if ((flags & ASSIGN_UNPACK) == 0)
3760 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003761 goto failed;
3762 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003763
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003765
Bram Moolenaar24e93162021-07-18 20:40:33 +02003766 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003767 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003768 imported_T *import = find_imported(varname, 0, FALSE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003769
Bram Moolenaar24e93162021-07-18 20:40:33 +02003770 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003771 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003772 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02003773 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003775 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003776 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003778 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3779 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003780 }
Bram Moolenaar75e27d72022-02-13 13:56:29 +00003781 if (!in_vim9script())
3782 {
3783 semsg(_(e_cannot_create_vim9_script_variable_in_function_str),
3784 name);
3785 goto failed;
3786 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003787 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788
Bram Moolenaar993faa32022-02-21 15:59:11 +00003789 // Search in parent scope which is possible to reference from lambda
3790 if (di == NULL)
3791 di = find_var_in_scoped_ht(name, TRUE);
3792
3793 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
3794 && var_wrong_func_name(name, di == NULL))
3795 goto failed;
3796
3797 if (need_convert_to_bool(type, tv))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003798 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003799 // Destination is a bool and the value is not, but it can be
3800 // converted.
3801 CLEAR_FIELD(bool_tv);
3802 bool_tv.v_type = VAR_BOOL;
3803 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3804 tv = &bool_tv;
3805 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003806
Bram Moolenaar993faa32022-02-21 15:59:11 +00003807 if (di != NULL)
3808 {
3809 // Item already exists. Allowed to replace when reloading.
3810 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003811 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003812 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
3813 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003814 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003815 emsg(_(e_cannot_modify_existing_variable));
3816 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003817 }
3818
Bram Moolenaar993faa32022-02-21 15:59:11 +00003819 if (is_script_local && vim9script
3820 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003821 {
3822 semsg(_(e_redefining_script_item_str), name);
3823 goto failed;
3824 }
3825
Bram Moolenaar993faa32022-02-21 15:59:11 +00003826 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003827 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003828 where_T where = WHERE_INIT;
Bram Moolenaar7a411a32022-04-04 14:58:06 +01003829 svar_T *sv = find_typval_in_script(&di->di_tv, sid, TRUE);
Bram Moolenaar993faa32022-02-21 15:59:11 +00003830
3831 if (sv != NULL)
3832 {
3833 // check the type and adjust to bool if needed
3834 where.wt_index = var_idx;
3835 where.wt_variable = TRUE;
3836 if (check_script_var_type(sv, tv, name, where) == FAIL)
3837 goto failed;
3838 if (type == NULL)
3839 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003840 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003841 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003842 }
3843
Bram Moolenaar993faa32022-02-21 15:59:11 +00003844 if ((flags & ASSIGN_FOR_LOOP) == 0
3845 && var_check_permission(di, name) == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003846 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003847 }
3848 else
3849 {
3850 // can only redefine once
3851 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003852
Bram Moolenaar993faa32022-02-21 15:59:11 +00003853 // A Vim9 script-local variable is also present in sn_all_vars
3854 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003855 if (var_in_vim9script || var_in_autoload)
Bram Moolenaar993faa32022-02-21 15:59:11 +00003856 update_vim9_script_var(FALSE, di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003857 var_in_autoload ? name : di->di_key, flags,
Bram Moolenaar993faa32022-02-21 15:59:11 +00003858 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003859 }
3860
Bram Moolenaar993faa32022-02-21 15:59:11 +00003861 // existing variable, need to clear the value
3862
3863 // Handle setting internal di: variables separately where needed to
3864 // prevent changing the type.
3865 if (ht == &vimvarht)
3866 {
3867 if (di->di_tv.v_type == VAR_STRING)
3868 {
3869 VIM_CLEAR(di->di_tv.vval.v_string);
3870 if (copy || tv->v_type != VAR_STRING)
3871 {
3872 char_u *val = tv_get_string(tv);
3873
3874 // Careful: when assigning to v:errmsg and
3875 // tv_get_string() causes an error message the variable
3876 // will already be set.
3877 if (di->di_tv.vval.v_string == NULL)
3878 di->di_tv.vval.v_string = vim_strsave(val);
3879 }
3880 else
3881 {
3882 // Take over the string to avoid an extra alloc/free.
3883 di->di_tv.vval.v_string = tv->vval.v_string;
3884 tv->vval.v_string = NULL;
3885 }
3886 goto failed;
3887 }
3888 else if (di->di_tv.v_type == VAR_NUMBER)
3889 {
3890 di->di_tv.vval.v_number = tv_get_number(tv);
3891 if (STRCMP(varname, "searchforward") == 0)
3892 set_search_direction(di->di_tv.vval.v_number
3893 ? '/' : '?');
3894#ifdef FEAT_SEARCH_EXTRA
3895 else if (STRCMP(varname, "hlsearch") == 0)
3896 {
3897 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003898 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar993faa32022-02-21 15:59:11 +00003899 }
3900#endif
3901 goto failed;
3902 }
3903 else if (di->di_tv.v_type != tv->v_type)
3904 {
3905 semsg(_(e_setting_str_to_value_with_wrong_type), name);
3906 goto failed;
3907 }
3908 }
3909
3910 clear_tv(&di->di_tv);
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01003911
3912 if ((flags & ASSIGN_UPDATE_BLOCK_ID)
3913 && SCRIPT_ID_VALID(current_sctx.sc_sid))
3914 {
3915 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
3916
3917 update_script_var_block_id(name, si->sn_current_block_id);
3918 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00003919 }
3920 else
3921 {
3922 // Item not found, check if a function already exists.
3923 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
3924 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
3925 {
3926 semsg(_(e_redefining_script_item_str), name);
3927 goto failed;
3928 }
3929
3930 // add a new variable
3931 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3932 {
3933 semsg(_(e_unknown_variable_str), name);
3934 goto failed;
3935 }
3936
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003937 if (check_hashtab_frozen(ht, "add variable"))
3938 goto failed;
3939
Bram Moolenaar993faa32022-02-21 15:59:11 +00003940 // Can't add "v:" or "a:" variable.
3941 if (ht == &vimvarht || ht == get_funccal_args_ht())
3942 {
3943 semsg(_(e_illegal_variable_name_str), name);
3944 goto failed;
3945 }
3946
3947 // Make sure the variable name is valid. In Vim9 script an
3948 // autoload variable must be prefixed with "g:" unless in an
3949 // autoload script.
3950 if (!valid_varname(varname, -1, !vim9script
3951 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
3952 goto failed;
3953
zeertzjq1b438a82023-02-01 13:11:15 +00003954 di = alloc(offsetof(dictitem_T, di_key) + STRLEN(varname) + 1);
Bram Moolenaar993faa32022-02-21 15:59:11 +00003955 if (di == NULL)
3956 goto failed;
3957 STRCPY(di->di_key, varname);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003958 if (hash_add(ht, DI2HIKEY(di), "add variable") == FAIL)
Bram Moolenaar993faa32022-02-21 15:59:11 +00003959 {
3960 vim_free(di);
3961 goto failed;
3962 }
3963 di->di_flags = DI_FLAGS_ALLOC;
3964 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3965 di->di_flags |= DI_FLAGS_LOCK;
3966
3967 // A Vim9 script-local variable is also added to sn_all_vars and
3968 // sn_var_vals. It may set "type" from "tv".
3969 if (var_in_vim9script || var_in_autoload)
3970 update_vim9_script_var(TRUE, di,
3971 var_in_autoload ? name : di->di_key, flags,
3972 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003973 }
3974
Bram Moolenaar993faa32022-02-21 15:59:11 +00003975 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003976 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003977 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003978 else
3979 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003980 *dest_tv = *tv;
3981 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003982 init_tv(tv);
3983 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003984 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003985
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003986 if (vim9script && type != NULL)
Bram Moolenaar381692b2022-02-02 20:01:27 +00003987 set_tv_type(dest_tv, type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003988
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003989 // ":const var = value" locks the value
3990 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003991 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003992 // Like :lockvar! name: lock the value and what it contains, but only
3993 // if the reference count is up to one. That locks only literal
3994 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003995 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003996
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003997failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003998 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003999 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004000 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004001}
4002
4003/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004004 * Check in this order for backwards compatibility:
4005 * - Whether the variable is read-only
4006 * - Whether the variable value is locked
4007 * - Whether the variable is locked
4008 */
4009 int
4010var_check_permission(dictitem_T *di, char_u *name)
4011{
4012 if (var_check_ro(di->di_flags, name, FALSE)
4013 || value_check_lock(di->di_tv.v_lock, name, FALSE)
4014 || var_check_lock(di->di_flags, name, FALSE))
4015 return FAIL;
4016 return OK;
4017}
4018
4019/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004020 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
4021 * Also give an error message.
4022 */
4023 int
4024var_check_ro(int flags, char_u *name, int use_gettext)
4025{
4026 if (flags & DI_FLAGS_RO)
4027 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004028 if (name == NULL)
4029 emsg(_(e_cannot_change_readonly_variable));
4030 else
4031 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02004032 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004033 return TRUE;
4034 }
4035 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
4036 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004037 if (name == NULL)
4038 emsg(_(e_cannot_set_variable_in_sandbox));
4039 else
4040 semsg(_(e_cannot_set_variable_in_sandbox_str),
4041 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004042 return TRUE;
4043 }
4044 return FALSE;
4045}
4046
4047/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004048 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
4049 * Also give an error message.
4050 */
4051 int
4052var_check_lock(int flags, char_u *name, int use_gettext)
4053{
4054 if (flags & DI_FLAGS_LOCK)
4055 {
4056 semsg(_(e_variable_is_locked_str),
4057 use_gettext ? (char_u *)_(name) : name);
4058 return TRUE;
4059 }
4060 return FALSE;
4061}
4062
4063/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004064 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
4065 * Also give an error message.
4066 */
4067 int
4068var_check_fixed(int flags, char_u *name, int use_gettext)
4069{
4070 if (flags & DI_FLAGS_FIX)
4071 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004072 if (name == NULL)
4073 emsg(_(e_cannot_delete_variable));
4074 else
4075 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004076 use_gettext ? (char_u *)_(name) : name);
4077 return TRUE;
4078 }
4079 return FALSE;
4080}
4081
4082/*
4083 * Check if a funcref is assigned to a valid variable name.
4084 * Return TRUE and give an error if not.
4085 */
4086 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004087var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004088 char_u *name, // points to start of variable name
4089 int new_var) // TRUE when creating the variable
4090{
Bram Moolenaar32154662021-03-28 21:14:06 +02004091 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
4092 // the name can be used without the s: prefix.
thinca6c667bd2022-09-02 11:25:37 +01004093 // Allow autoload variable.
Bram Moolenaar32154662021-03-28 21:14:06 +02004094 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
4095 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004096 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
thinca6c667bd2022-09-02 11:25:37 +01004097 ? name[2] : name[0])
4098 && vim_strchr(name, '#') == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004099 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004100 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004101 return TRUE;
4102 }
4103 // Don't allow hiding a function. When "v" is not NULL we might be
4104 // assigning another function to the same var, the type is checked
4105 // below.
4106 if (new_var && function_exists(name, FALSE))
4107 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004108 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004109 name);
4110 return TRUE;
4111 }
4112 return FALSE;
4113}
4114
4115/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004116 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
4117 * value. Also give an error message, using "name" or _("name") when
4118 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004119 */
4120 int
Bram Moolenaara187c432020-09-16 21:08:28 +02004121value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004122{
4123 if (lock & VAR_LOCKED)
4124 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004125 if (name == NULL)
4126 emsg(_(e_value_is_locked));
4127 else
4128 semsg(_(e_value_is_locked_str),
4129 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004130 return TRUE;
4131 }
4132 if (lock & VAR_FIXED)
4133 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004134 if (name == NULL)
4135 emsg(_(e_cannot_change_value));
4136 else
4137 semsg(_(e_cannot_change_value_of_str),
4138 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004139 return TRUE;
4140 }
4141 return FALSE;
4142}
4143
4144/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01004145 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004146 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004147 * Return FALSE and give an error if not.
4148 */
4149 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004150valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004151{
4152 char_u *p;
4153
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004154 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004155 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01004156 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004157 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004158 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004159 return FALSE;
4160 }
4161 return TRUE;
4162}
4163
4164/*
LemonBoy47d4e312022-05-04 18:12:55 +01004165 * Implements the logic to retrieve local variable and option values.
4166 * Used by "getwinvar()" "gettabvar()" "gettabwinvar()" "getbufvar()".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004167 */
4168 static void
LemonBoy47d4e312022-05-04 18:12:55 +01004169get_var_from(
4170 char_u *varname,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004171 typval_T *rettv,
LemonBoy47d4e312022-05-04 18:12:55 +01004172 typval_T *deftv, // Default value if not found.
4173 int htname, // 't'ab, 'w'indow or 'b'uffer local.
4174 tabpage_T *tp, // can be NULL
4175 win_T *win,
4176 buf_T *buf) // Ignored if htname is not 'b'.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004177{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004178 dictitem_T *v;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004179 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004180 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004181 int need_switch_win;
zeertzjqcd6ad642022-07-25 12:28:09 +01004182 int do_change_curbuf = buf != NULL && htname == 'b';
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004183
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004184 ++emsg_off;
4185
4186 rettv->v_type = VAR_STRING;
4187 rettv->vval.v_string = NULL;
4188
LemonBoy47d4e312022-05-04 18:12:55 +01004189 if (varname != NULL && tp != NULL && win != NULL
4190 && (htname != 'b' || buf != NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004191 {
4192 // Set curwin to be our win, temporarily. Also set the tabpage,
4193 // otherwise the window is not valid. Only do this when needed,
4194 // autocommands get blocked.
LemonBoy47d4e312022-05-04 18:12:55 +01004195 // If we have a buffer reference avoid the switching, we're saving and
4196 // restoring curbuf directly.
zeertzjqcd6ad642022-07-25 12:28:09 +01004197 need_switch_win = !(tp == curtab && win == curwin) && !do_change_curbuf;
LemonBoy47d4e312022-05-04 18:12:55 +01004198 if (!need_switch_win || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004199 {
LemonBoy47d4e312022-05-04 18:12:55 +01004200 // Handle options. There are no tab-local options.
4201 if (*varname == '&' && htname != 't')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004202 {
LemonBoy47d4e312022-05-04 18:12:55 +01004203 buf_T *save_curbuf = curbuf;
4204
4205 // Change curbuf so the option is read from the correct buffer.
zeertzjqcd6ad642022-07-25 12:28:09 +01004206 if (do_change_curbuf)
LemonBoy47d4e312022-05-04 18:12:55 +01004207 curbuf = buf;
4208
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004209 if (varname[1] == NUL)
4210 {
zeertzjqcd6ad642022-07-25 12:28:09 +01004211 // get all window-local or buffer-local options in a dict
LemonBoy47d4e312022-05-04 18:12:55 +01004212 dict_T *opts = get_winbuf_options(htname == 'b');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004213
4214 if (opts != NULL)
4215 {
4216 rettv_dict_set(rettv, opts);
4217 done = TRUE;
4218 }
4219 }
LemonBoy47d4e312022-05-04 18:12:55 +01004220 else if (eval_option(&varname, rettv, TRUE) == OK)
4221 // Local option
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004222 done = TRUE;
LemonBoy47d4e312022-05-04 18:12:55 +01004223
4224 curbuf = save_curbuf;
4225 }
4226 else if (*varname == NUL)
4227 {
4228 // Empty string: return a dict with all the local variables.
4229 if (htname == 'b')
4230 v = &buf->b_bufvar;
4231 else if (htname == 'w')
4232 v = &win->w_winvar;
4233 else
4234 v = &tp->tp_winvar;
4235 copy_tv(&v->di_tv, rettv);
4236 done = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004237 }
4238 else
4239 {
LemonBoy47d4e312022-05-04 18:12:55 +01004240 hashtab_T *ht;
4241
4242 if (htname == 'b')
4243 ht = &buf->b_vars->dv_hashtab;
4244 else if (htname == 'w')
4245 ht = &win->w_vars->dv_hashtab;
4246 else
4247 ht = &tp->tp_vars->dv_hashtab;
4248
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004249 // Look up the variable.
LemonBoy47d4e312022-05-04 18:12:55 +01004250 v = find_var_in_ht(ht, htname, varname, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004251 if (v != NULL)
4252 {
4253 copy_tv(&v->di_tv, rettv);
4254 done = TRUE;
4255 }
4256 }
4257 }
4258
4259 if (need_switch_win)
4260 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004261 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004262 }
4263
LemonBoy47d4e312022-05-04 18:12:55 +01004264 if (!done && deftv->v_type != VAR_UNKNOWN)
4265 // use the default value
4266 copy_tv(deftv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004267
4268 --emsg_off;
4269}
4270
4271/*
LemonBoy47d4e312022-05-04 18:12:55 +01004272 * getwinvar() and gettabwinvar()
4273 */
4274 static void
4275getwinvar(
4276 typval_T *argvars,
4277 typval_T *rettv,
4278 int off) // 1 for gettabwinvar()
4279{
4280 char_u *varname;
4281 tabpage_T *tp;
4282 win_T *win;
4283
4284 if (off == 1)
4285 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4286 else
4287 tp = curtab;
4288 win = find_win_by_nr(&argvars[off], tp);
4289 varname = tv_get_string_chk(&argvars[off + 1]);
4290
4291 get_var_from(varname, rettv, &argvars[off + 2], 'w', tp, win, NULL);
4292}
4293
4294/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02004295 * Set option "varname" to the value of "varp" for the current buffer/window.
4296 */
4297 static void
4298set_option_from_tv(char_u *varname, typval_T *varp)
4299{
4300 long numval = 0;
4301 char_u *strval;
4302 char_u nbuf[NUMBUFLEN];
4303 int error = FALSE;
4304
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004305 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004306 {
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004307 if (is_string_option(varname))
4308 {
4309 emsg(_(e_string_required));
4310 return;
4311 }
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004312 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004313 strval = (char_u *)"0"; // avoid using "false"
4314 }
4315 else
4316 {
4317 if (!in_vim9script() || varp->v_type != VAR_STRING)
4318 numval = (long)tv_get_number_chk(varp, &error);
4319 strval = tv_get_string_buf_chk(varp, nbuf);
4320 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02004321 if (!error && strval != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004322 set_option_value_give_err(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar191929b2020-08-19 21:20:49 +02004323}
4324
4325/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004326 * "setwinvar()" and "settabwinvar()" functions
4327 */
4328 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004329setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004330{
4331 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004332 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004333 int need_switch_win;
4334 char_u *varname, *winvarname;
4335 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004336 tabpage_T *tp = NULL;
4337
4338 if (check_secure())
4339 return;
4340
4341 if (off == 1)
4342 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4343 else
4344 tp = curtab;
4345 win = find_win_by_nr(&argvars[off], tp);
4346 varname = tv_get_string_chk(&argvars[off + 1]);
4347 varp = &argvars[off + 2];
4348
zeertzjqea720ae2023-01-03 10:54:09 +00004349 if (win == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004350 return;
4351
4352 need_switch_win = !(tp == curtab && win == curwin);
4353 if (!need_switch_win
4354 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004355 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004356 if (*varname == '&')
4357 set_option_from_tv(varname + 1, varp);
4358 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004359 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004360 winvarname = alloc(STRLEN(varname) + 3);
4361 if (winvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004362 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004363 STRCPY(winvarname, "w:");
4364 STRCPY(winvarname + 2, varname);
4365 set_var(winvarname, varp, TRUE);
4366 vim_free(winvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004367 }
4368 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004369 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004370 if (need_switch_win)
4371 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004372}
4373
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004374/*
4375 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4376 * v:option_type, and v:option_command.
4377 */
4378 void
4379reset_v_option_vars(void)
4380{
4381 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4382 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4383 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4384 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4385 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4386 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4387}
4388
4389/*
4390 * Add an assert error to v:errors.
4391 */
4392 void
4393assert_error(garray_T *gap)
4394{
4395 struct vimvar *vp = &vimvars[VV_ERRORS];
4396
Bram Moolenaard787e402021-12-24 21:36:12 +00004397 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004398 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004399 set_vim_var_list(VV_ERRORS, list_alloc());
4400 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4401}
4402
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004403 int
4404var_exists(char_u *var)
4405{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004406 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004407 char_u *name;
4408 char_u *tofree;
4409 typval_T tv;
4410 int len = 0;
4411 int n = FALSE;
4412
4413 // get_name_len() takes care of expanding curly braces
4414 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004415 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004416 if (len > 0)
4417 {
4418 if (tofree != NULL)
4419 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004420 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004421 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004422 if (n)
4423 {
4424 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004425 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004426 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4427 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004428 if (n)
4429 clear_tv(&tv);
4430 }
4431 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004432 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004433 n = FALSE;
4434
4435 vim_free(tofree);
4436 return n;
4437}
4438
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004439static lval_T *redir_lval = NULL;
4440#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4441static garray_T redir_ga; // only valid when redir_lval is not NULL
4442static char_u *redir_endp = NULL;
4443static char_u *redir_varname = NULL;
4444
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004445 int
4446alloc_redir_lval(void)
4447{
4448 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4449 if (redir_lval == NULL)
4450 return FAIL;
4451 return OK;
4452}
4453
4454 void
4455clear_redir_lval(void)
4456{
4457 VIM_CLEAR(redir_lval);
4458}
4459
4460 void
4461init_redir_ga(void)
4462{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004463 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004464}
4465
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004466/*
4467 * Start recording command output to a variable
4468 * When "append" is TRUE append to an existing variable.
4469 * Returns OK if successfully completed the setup. FAIL otherwise.
4470 */
4471 int
4472var_redir_start(char_u *name, int append)
4473{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004474 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004475 typval_T tv;
4476
4477 // Catch a bad name early.
4478 if (!eval_isnamec1(*name))
4479 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004480 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004481 return FAIL;
4482 }
4483
4484 // Make a copy of the name, it is used in redir_lval until redir ends.
4485 redir_varname = vim_strsave(name);
4486 if (redir_varname == NULL)
4487 return FAIL;
4488
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004489 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004490 {
4491 var_redir_stop();
4492 return FAIL;
4493 }
4494
4495 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004496 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004497
4498 // Parse the variable name (can be a dict or list entry).
4499 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4500 FNE_CHECK_START);
4501 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4502 {
4503 clear_lval(redir_lval);
4504 if (redir_endp != NULL && *redir_endp != NUL)
4505 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004506 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004507 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004508 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004509 redir_endp = NULL; // don't store a value, only cleanup
4510 var_redir_stop();
4511 return FAIL;
4512 }
4513
4514 // check if we can write to the variable: set it to or append an empty
4515 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004516 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004517 tv.v_type = VAR_STRING;
4518 tv.vval.v_string = (char_u *)"";
4519 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004520 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004521 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004522 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004523 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004524 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004525 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004526 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004527 {
4528 redir_endp = NULL; // don't store a value, only cleanup
4529 var_redir_stop();
4530 return FAIL;
4531 }
4532
4533 return OK;
4534}
4535
4536/*
4537 * Append "value[value_len]" to the variable set by var_redir_start().
4538 * The actual appending is postponed until redirection ends, because the value
4539 * appended may in fact be the string we write to, changing it may cause freed
4540 * memory to be used:
4541 * :redir => foo
4542 * :let foo
4543 * :redir END
4544 */
4545 void
4546var_redir_str(char_u *value, int value_len)
4547{
4548 int len;
4549
4550 if (redir_lval == NULL)
4551 return;
4552
4553 if (value_len == -1)
4554 len = (int)STRLEN(value); // Append the entire string
4555 else
4556 len = value_len; // Append only "value_len" characters
4557
4558 if (ga_grow(&redir_ga, len) == OK)
4559 {
4560 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4561 redir_ga.ga_len += len;
4562 }
4563 else
4564 var_redir_stop();
4565}
4566
4567/*
4568 * Stop redirecting command output to a variable.
4569 * Frees the allocated memory.
4570 */
4571 void
4572var_redir_stop(void)
4573{
4574 typval_T tv;
4575
4576 if (EVALCMD_BUSY)
4577 {
4578 redir_lval = NULL;
4579 return;
4580 }
4581
4582 if (redir_lval != NULL)
4583 {
4584 // If there was no error: assign the text to the variable.
4585 if (redir_endp != NULL)
4586 {
4587 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4588 tv.v_type = VAR_STRING;
4589 tv.vval.v_string = redir_ga.ga_data;
4590 // Call get_lval() again, if it's inside a Dict or List it may
4591 // have changed.
4592 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4593 FALSE, FALSE, 0, FNE_CHECK_START);
4594 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004596 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004597 clear_lval(redir_lval);
4598 }
4599
4600 // free the collected output
4601 VIM_CLEAR(redir_ga.ga_data);
4602
4603 VIM_CLEAR(redir_lval);
4604 }
4605 VIM_CLEAR(redir_varname);
4606}
4607
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004608/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004609 * Get the collected redirected text and clear redir_ga.
4610 */
4611 char_u *
4612get_clear_redir_ga(void)
4613{
4614 char_u *res;
4615
4616 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4617 res = redir_ga.ga_data;
4618 redir_ga.ga_data = NULL;
4619 return res;
4620}
4621
4622/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004623 * "gettabvar()" function
4624 */
4625 void
4626f_gettabvar(typval_T *argvars, typval_T *rettv)
4627{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004628 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004629 tabpage_T *tp;
4630 win_T *win = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004631
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004632 if (in_vim9script()
4633 && (check_for_number_arg(argvars, 0) == FAIL
4634 || check_for_string_arg(argvars, 1) == FAIL))
4635 return;
4636
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004637 varname = tv_get_string_chk(&argvars[1]);
4638 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
LemonBoy47d4e312022-05-04 18:12:55 +01004639 if (tp != NULL)
4640 win = tp == curtab || tp->tp_firstwin == NULL ? firstwin
4641 : tp->tp_firstwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004642
LemonBoy47d4e312022-05-04 18:12:55 +01004643 get_var_from(varname, rettv, &argvars[2], 't', tp, win, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004644}
4645
4646/*
4647 * "gettabwinvar()" function
4648 */
4649 void
4650f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4651{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004652 if (in_vim9script()
4653 && (check_for_number_arg(argvars, 0) == FAIL
4654 || check_for_number_arg(argvars, 1) == FAIL
4655 || check_for_string_arg(argvars, 2) == FAIL))
4656 return;
4657
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004658 getwinvar(argvars, rettv, 1);
4659}
4660
4661/*
4662 * "getwinvar()" function
4663 */
4664 void
4665f_getwinvar(typval_T *argvars, typval_T *rettv)
4666{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004667 if (in_vim9script()
4668 && (check_for_number_arg(argvars, 0) == FAIL
4669 || check_for_string_arg(argvars, 1) == FAIL))
4670 return;
4671
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004672 getwinvar(argvars, rettv, 0);
4673}
4674
4675/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004676 * "getbufvar()" function
4677 */
4678 void
4679f_getbufvar(typval_T *argvars, typval_T *rettv)
4680{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004681 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004682 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004683
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004684 if (in_vim9script()
4685 && (check_for_buffer_arg(argvars, 0) == FAIL
4686 || check_for_string_arg(argvars, 1) == FAIL))
4687 return;
4688
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004689 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004690 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004691
LemonBoy47d4e312022-05-04 18:12:55 +01004692 get_var_from(varname, rettv, &argvars[2], 'b', curtab, curwin, buf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004693}
4694
4695/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004696 * "settabvar()" function
4697 */
4698 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004699f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004700{
4701 tabpage_T *save_curtab;
4702 tabpage_T *tp;
4703 char_u *varname, *tabvarname;
4704 typval_T *varp;
4705
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004706 if (check_secure())
4707 return;
4708
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004709 if (in_vim9script()
4710 && (check_for_number_arg(argvars, 0) == FAIL
4711 || check_for_string_arg(argvars, 1) == FAIL))
4712 return;
4713
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004714 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4715 varname = tv_get_string_chk(&argvars[1]);
4716 varp = &argvars[2];
4717
zeertzjqea720ae2023-01-03 10:54:09 +00004718 if (varname == NULL || tp == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004719 return;
4720
4721 save_curtab = curtab;
4722 goto_tabpage_tp(tp, FALSE, FALSE);
4723
4724 tabvarname = alloc(STRLEN(varname) + 3);
4725 if (tabvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004726 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004727 STRCPY(tabvarname, "t:");
4728 STRCPY(tabvarname + 2, varname);
4729 set_var(tabvarname, varp, TRUE);
4730 vim_free(tabvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004731 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004732
4733 // Restore current tabpage
4734 if (valid_tabpage(save_curtab))
4735 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004736}
4737
4738/*
4739 * "settabwinvar()" function
4740 */
4741 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004742f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004743{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004744 if (in_vim9script()
4745 && (check_for_number_arg(argvars, 0) == FAIL
4746 || check_for_number_arg(argvars, 1) == FAIL
4747 || check_for_string_arg(argvars, 2) == FAIL))
4748 return;
4749
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004750 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004751}
4752
4753/*
4754 * "setwinvar()" function
4755 */
4756 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004757f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004758{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004759 if (in_vim9script()
4760 && (check_for_number_arg(argvars, 0) == FAIL
4761 || check_for_string_arg(argvars, 1) == FAIL))
4762 return;
4763
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004764 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004765}
4766
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004767/*
4768 * "setbufvar()" function
4769 */
4770 void
4771f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4772{
4773 buf_T *buf;
4774 char_u *varname, *bufvarname;
4775 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004776
4777 if (check_secure())
4778 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004779
4780 if (in_vim9script()
4781 && (check_for_buffer_arg(argvars, 0) == FAIL
4782 || check_for_string_arg(argvars, 1) == FAIL))
4783 return;
4784
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004785 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004786 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004787 varp = &argvars[2];
4788
zeertzjqea720ae2023-01-03 10:54:09 +00004789 if (buf == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004790 return;
4791
4792 if (*varname == '&')
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004793 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004794 aco_save_T aco;
4795
4796 // Set curbuf to be our buf, temporarily.
4797 aucmd_prepbuf(&aco, buf);
4798 if (curbuf == buf)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004799 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004800 // Only when it worked to set "curbuf".
4801 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004802
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004803 // reset notion of buffer
4804 aucmd_restbuf(&aco);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004805 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004806 }
4807 else
4808 {
4809 bufvarname = alloc(STRLEN(varname) + 3);
4810 if (bufvarname != NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004811 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004812 buf_T *save_curbuf = curbuf;
Bram Moolenaar86015452020-03-29 15:12:15 +02004813
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004814 curbuf = buf;
4815 STRCPY(bufvarname, "b:");
4816 STRCPY(bufvarname + 2, varname);
4817 set_var(bufvarname, varp, TRUE);
4818 vim_free(bufvarname);
4819 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004820 }
4821 }
4822}
4823
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004824/*
4825 * Get a callback from "arg". It can be a Funcref or a function name.
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004826 * When "arg" is zero "res.cb_name" is set to an empty string.
4827 * If "res.cb_name" is allocated then "res.cb_free_name" is set to TRUE.
4828 * "res.cb_name" is set to NULL for an invalid argument.
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004829 */
4830 callback_T
4831get_callback(typval_T *arg)
4832{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004833 callback_T res;
4834 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004835
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004836 CLEAR_FIELD(res);
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004837 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4838 {
4839 res.cb_partial = arg->vval.v_partial;
4840 ++res.cb_partial->pt_refcount;
4841 res.cb_name = partial_name(res.cb_partial);
4842 }
4843 else
4844 {
Bram Moolenaar14e579092020-03-07 16:59:25 +01004845 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4846 && isdigit(*arg->vval.v_string))
4847 r = FAIL;
4848 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004849 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004850 res.cb_name = arg->vval.v_string;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004851 if (arg->v_type == VAR_STRING)
4852 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004853 char_u *name = get_scriptlocal_funcname(arg->vval.v_string);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004854 if (name != NULL)
4855 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004856 res.cb_name = name;
4857 res.cb_free_name = TRUE;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004858 }
4859 }
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004860 func_ref(res.cb_name);
4861 }
4862 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004863 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004864 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004865 r = FAIL;
4866
4867 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004868 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004869 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004870 res.cb_name = NULL;
4871 }
4872 }
4873 return res;
4874}
4875
4876/*
4877 * Copy a callback into a typval_T.
4878 */
4879 void
4880put_callback(callback_T *cb, typval_T *tv)
4881{
4882 if (cb->cb_partial != NULL)
4883 {
4884 tv->v_type = VAR_PARTIAL;
4885 tv->vval.v_partial = cb->cb_partial;
4886 ++tv->vval.v_partial->pt_refcount;
4887 }
4888 else
4889 {
4890 tv->v_type = VAR_FUNC;
4891 tv->vval.v_string = vim_strsave(cb->cb_name);
4892 func_ref(cb->cb_name);
4893 }
4894}
4895
4896/*
4897 * Make a copy of "src" into "dest", allocating the function name if needed,
4898 * without incrementing the refcount.
4899 */
4900 void
4901set_callback(callback_T *dest, callback_T *src)
4902{
4903 if (src->cb_partial == NULL)
4904 {
4905 // just a function name, make a copy
4906 dest->cb_name = vim_strsave(src->cb_name);
4907 dest->cb_free_name = TRUE;
4908 }
4909 else
4910 {
4911 // cb_name is a pointer into cb_partial
4912 dest->cb_name = src->cb_name;
4913 dest->cb_free_name = FALSE;
4914 }
4915 dest->cb_partial = src->cb_partial;
4916}
4917
4918/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004919 * Copy callback from "src" to "dest", incrementing the refcounts.
4920 */
4921 void
4922copy_callback(callback_T *dest, callback_T *src)
4923{
4924 dest->cb_partial = src->cb_partial;
4925 if (dest->cb_partial != NULL)
4926 {
4927 dest->cb_name = src->cb_name;
4928 dest->cb_free_name = FALSE;
4929 ++dest->cb_partial->pt_refcount;
4930 }
4931 else
4932 {
4933 dest->cb_name = vim_strsave(src->cb_name);
4934 dest->cb_free_name = TRUE;
4935 func_ref(src->cb_name);
4936 }
4937}
4938
4939/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004940 * When a callback refers to an autoload import, change the function name to
4941 * the "path#name" form. Uses the current script context.
4942 * Only works when the name is allocated.
4943 */
4944 void
4945expand_autload_callback(callback_T *cb)
4946{
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004947 char_u *name;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004948 char_u *p;
4949 imported_T *import;
4950
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004951 if (!in_vim9script() || cb->cb_name == NULL
4952 || (!cb->cb_free_name
4953 && (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004954 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004955 if (cb->cb_partial != NULL)
4956 name = cb->cb_partial->pt_name;
4957 else
4958 name = cb->cb_name;
4959 p = vim_strchr(name, '.');
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004960 if (p == NULL)
4961 return;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004962
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004963 import = find_imported(name, p - name, FALSE);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004964 if (import == NULL || !SCRIPT_ID_VALID(import->imp_sid))
4965 return;
4966
4967 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
4968 if (si->sn_autoload_prefix == NULL)
4969 return;
4970
4971 char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
4972 if (newname == NULL)
4973 return;
4974
4975 if (cb->cb_partial != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004976 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004977 if (cb->cb_name == cb->cb_partial->pt_name)
4978 cb->cb_name = newname;
4979 vim_free(cb->cb_partial->pt_name);
4980 cb->cb_partial->pt_name = newname;
4981 }
4982 else
4983 {
4984 vim_free(cb->cb_name);
4985 cb->cb_name = newname;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004986 }
4987}
4988
4989/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004990 * Unref/free "callback" returned by get_callback() or set_callback().
4991 */
4992 void
4993free_callback(callback_T *callback)
4994{
4995 if (callback->cb_partial != NULL)
4996 {
4997 partial_unref(callback->cb_partial);
4998 callback->cb_partial = NULL;
4999 }
5000 else if (callback->cb_name != NULL)
5001 func_unref(callback->cb_name);
5002 if (callback->cb_free_name)
5003 {
5004 vim_free(callback->cb_name);
5005 callback->cb_free_name = FALSE;
5006 }
5007 callback->cb_name = NULL;
5008}
5009
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005010#endif // FEAT_EVAL