blob: f16d4757f2de8da10c4283d86c2e3e23e20a6a2a [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
Bram Moolenaard787e402021-12-24 21:36:12 +000048 type_T *vv_type; // type or NULL
Bram Moolenaare5cdf152019-08-29 22:09:46 +020049 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
50} vimvars[VV_LEN] =
51{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020052 // The order here must match the VV_ defines in vim.h!
53 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaard787e402021-12-24 21:36:12 +000054 {VV_NAME("count", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
55 {VV_NAME("count1", VAR_NUMBER), NULL, VV_RO},
56 {VV_NAME("prevcount", VAR_NUMBER), NULL, VV_RO},
57 {VV_NAME("errmsg", VAR_STRING), NULL, VV_COMPAT},
58 {VV_NAME("warningmsg", VAR_STRING), NULL, 0},
59 {VV_NAME("statusmsg", VAR_STRING), NULL, 0},
60 {VV_NAME("shell_error", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
61 {VV_NAME("this_session", VAR_STRING), NULL, VV_COMPAT},
62 {VV_NAME("version", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
63 {VV_NAME("lnum", VAR_NUMBER), NULL, VV_RO_SBX},
64 {VV_NAME("termresponse", VAR_STRING), NULL, VV_RO},
65 {VV_NAME("fname", VAR_STRING), NULL, VV_RO},
66 {VV_NAME("lang", VAR_STRING), NULL, VV_RO},
67 {VV_NAME("lc_time", VAR_STRING), NULL, VV_RO},
68 {VV_NAME("ctype", VAR_STRING), NULL, VV_RO},
69 {VV_NAME("charconvert_from", VAR_STRING), NULL, VV_RO},
70 {VV_NAME("charconvert_to", VAR_STRING), NULL, VV_RO},
71 {VV_NAME("fname_in", VAR_STRING), NULL, VV_RO},
72 {VV_NAME("fname_out", VAR_STRING), NULL, VV_RO},
73 {VV_NAME("fname_new", VAR_STRING), NULL, VV_RO},
74 {VV_NAME("fname_diff", VAR_STRING), NULL, VV_RO},
75 {VV_NAME("cmdarg", VAR_STRING), NULL, VV_RO},
76 {VV_NAME("foldstart", VAR_NUMBER), NULL, VV_RO_SBX},
77 {VV_NAME("foldend", VAR_NUMBER), NULL, VV_RO_SBX},
78 {VV_NAME("folddashes", VAR_STRING), NULL, VV_RO_SBX},
79 {VV_NAME("foldlevel", VAR_NUMBER), NULL, VV_RO_SBX},
80 {VV_NAME("progname", VAR_STRING), NULL, VV_RO},
81 {VV_NAME("servername", VAR_STRING), NULL, VV_RO},
82 {VV_NAME("dying", VAR_NUMBER), NULL, VV_RO},
83 {VV_NAME("exception", VAR_STRING), NULL, VV_RO},
84 {VV_NAME("throwpoint", VAR_STRING), NULL, VV_RO},
85 {VV_NAME("register", VAR_STRING), NULL, VV_RO},
86 {VV_NAME("cmdbang", VAR_NUMBER), NULL, VV_RO},
87 {VV_NAME("insertmode", VAR_STRING), NULL, VV_RO},
88 {VV_NAME("val", VAR_UNKNOWN), NULL, VV_RO},
89 {VV_NAME("key", VAR_UNKNOWN), NULL, VV_RO},
90 {VV_NAME("profiling", VAR_NUMBER), NULL, VV_RO},
91 {VV_NAME("fcs_reason", VAR_STRING), NULL, VV_RO},
92 {VV_NAME("fcs_choice", VAR_STRING), NULL, 0},
93 {VV_NAME("beval_bufnr", VAR_NUMBER), NULL, VV_RO},
94 {VV_NAME("beval_winnr", VAR_NUMBER), NULL, VV_RO},
95 {VV_NAME("beval_winid", VAR_NUMBER), NULL, VV_RO},
96 {VV_NAME("beval_lnum", VAR_NUMBER), NULL, VV_RO},
97 {VV_NAME("beval_col", VAR_NUMBER), NULL, VV_RO},
98 {VV_NAME("beval_text", VAR_STRING), NULL, VV_RO},
99 {VV_NAME("scrollstart", VAR_STRING), NULL, 0},
100 {VV_NAME("swapname", VAR_STRING), NULL, VV_RO},
101 {VV_NAME("swapchoice", VAR_STRING), NULL, 0},
102 {VV_NAME("swapcommand", VAR_STRING), NULL, VV_RO},
103 {VV_NAME("char", VAR_STRING), NULL, 0},
104 {VV_NAME("mouse_win", VAR_NUMBER), NULL, 0},
105 {VV_NAME("mouse_winid", VAR_NUMBER), NULL, 0},
106 {VV_NAME("mouse_lnum", VAR_NUMBER), NULL, 0},
107 {VV_NAME("mouse_col", VAR_NUMBER), NULL, 0},
108 {VV_NAME("operator", VAR_STRING), NULL, VV_RO},
109 {VV_NAME("searchforward", VAR_NUMBER), NULL, 0},
110 {VV_NAME("hlsearch", VAR_NUMBER), NULL, 0},
111 {VV_NAME("oldfiles", VAR_LIST), &t_list_string, 0},
112 {VV_NAME("windowid", VAR_NUMBER), NULL, VV_RO},
113 {VV_NAME("progpath", VAR_STRING), NULL, VV_RO},
Shougo Matsushita61021aa2022-07-27 14:40:00 +0100114 {VV_NAME("completed_item", VAR_DICT), &t_dict_string, 0},
Bram Moolenaard787e402021-12-24 21:36:12 +0000115 {VV_NAME("option_new", VAR_STRING), NULL, VV_RO},
116 {VV_NAME("option_old", VAR_STRING), NULL, VV_RO},
117 {VV_NAME("option_oldlocal", VAR_STRING), NULL, VV_RO},
118 {VV_NAME("option_oldglobal", VAR_STRING), NULL, VV_RO},
119 {VV_NAME("option_command", VAR_STRING), NULL, VV_RO},
120 {VV_NAME("option_type", VAR_STRING), NULL, VV_RO},
121 {VV_NAME("errors", VAR_LIST), &t_list_string, 0},
122 {VV_NAME("false", VAR_BOOL), NULL, VV_RO},
123 {VV_NAME("true", VAR_BOOL), NULL, VV_RO},
124 {VV_NAME("none", VAR_SPECIAL), NULL, VV_RO},
125 {VV_NAME("null", VAR_SPECIAL), NULL, VV_RO},
126 {VV_NAME("numbermax", VAR_NUMBER), NULL, VV_RO},
127 {VV_NAME("numbermin", VAR_NUMBER), NULL, VV_RO},
128 {VV_NAME("numbersize", VAR_NUMBER), NULL, VV_RO},
129 {VV_NAME("vim_did_enter", VAR_NUMBER), NULL, VV_RO},
130 {VV_NAME("testing", VAR_NUMBER), NULL, 0},
131 {VV_NAME("t_number", VAR_NUMBER), NULL, VV_RO},
132 {VV_NAME("t_string", VAR_NUMBER), NULL, VV_RO},
133 {VV_NAME("t_func", VAR_NUMBER), NULL, VV_RO},
134 {VV_NAME("t_list", VAR_NUMBER), NULL, VV_RO},
135 {VV_NAME("t_dict", VAR_NUMBER), NULL, VV_RO},
136 {VV_NAME("t_float", VAR_NUMBER), NULL, VV_RO},
137 {VV_NAME("t_bool", VAR_NUMBER), NULL, VV_RO},
138 {VV_NAME("t_none", VAR_NUMBER), NULL, VV_RO},
139 {VV_NAME("t_job", VAR_NUMBER), NULL, VV_RO},
140 {VV_NAME("t_channel", VAR_NUMBER), NULL, VV_RO},
141 {VV_NAME("t_blob", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaarc0c2c262023-01-12 21:08:53 +0000142 {VV_NAME("t_class", VAR_NUMBER), NULL, VV_RO},
143 {VV_NAME("t_object", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaard787e402021-12-24 21:36:12 +0000144 {VV_NAME("termrfgresp", VAR_STRING), NULL, VV_RO},
145 {VV_NAME("termrbgresp", VAR_STRING), NULL, VV_RO},
146 {VV_NAME("termu7resp", VAR_STRING), NULL, VV_RO},
147 {VV_NAME("termstyleresp", VAR_STRING), NULL, VV_RO},
148 {VV_NAME("termblinkresp", VAR_STRING), NULL, VV_RO},
149 {VV_NAME("event", VAR_DICT), NULL, VV_RO},
150 {VV_NAME("versionlong", VAR_NUMBER), NULL, VV_RO},
151 {VV_NAME("echospace", VAR_NUMBER), NULL, VV_RO},
152 {VV_NAME("argv", VAR_LIST), &t_list_string, VV_RO},
153 {VV_NAME("collate", VAR_STRING), NULL, VV_RO},
154 {VV_NAME("exiting", VAR_SPECIAL), NULL, VV_RO},
155 {VV_NAME("colornames", VAR_DICT), &t_dict_string, VV_RO},
156 {VV_NAME("sizeofint", VAR_NUMBER), NULL, VV_RO},
157 {VV_NAME("sizeoflong", VAR_NUMBER), NULL, VV_RO},
158 {VV_NAME("sizeofpointer", VAR_NUMBER), NULL, VV_RO},
naohiro ono56200ee2022-01-01 14:59:44 +0000159 {VV_NAME("maxcol", VAR_NUMBER), NULL, VV_RO},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200160 {VV_NAME("python3_version", VAR_NUMBER), NULL, VV_RO},
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200161 {VV_NAME("t_typealias", VAR_NUMBER), NULL, VV_RO},
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100162 {VV_NAME("t_enum", VAR_NUMBER), NULL, VV_RO},
163 {VV_NAME("t_enumvalue", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200164};
165
166// shorthand
Bram Moolenaard787e402021-12-24 21:36:12 +0000167#define vv_tv_type vv_di.di_tv.v_type
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200168#define vv_nr vv_di.di_tv.vval.v_number
169#define vv_float vv_di.di_tv.vval.v_float
170#define vv_str vv_di.di_tv.vval.v_string
171#define vv_list vv_di.di_tv.vval.v_list
172#define vv_dict vv_di.di_tv.vval.v_dict
173#define vv_blob vv_di.di_tv.vval.v_blob
174#define vv_tv vv_di.di_tv
175
176static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200177static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200178#define vimvarht vimvardict.dv_hashtab
179
180// for VIM_VERSION_ defines
181#include "version.h"
182
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200183static void list_glob_vars(int *first);
184static void list_buf_vars(int *first);
185static void list_win_vars(int *first);
186static void list_tab_vars(int *first);
187static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100188static 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 +0200189static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
190static 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 +0200191static void list_one_var(dictitem_T *v, char *prefix, int *first);
192static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
193
194/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200195 * Initialize global and vim special variables
196 */
197 void
198evalvars_init(void)
199{
200 int i;
201 struct vimvar *p;
202
203 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
204 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
205 vimvardict.dv_lock = VAR_FIXED;
206 hash_init(&compat_hashtab);
207
208 for (i = 0; i < VV_LEN; ++i)
209 {
210 p = &vimvars[i];
211 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
212 {
Bram Moolenaar097c5372023-05-24 21:02:24 +0100213 iemsg("Name too long, increase size of dictitem16_T");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200214 getout(1);
215 }
216 STRCPY(p->vv_di.di_key, p->vv_name);
217 if (p->vv_flags & VV_RO)
218 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
219 else if (p->vv_flags & VV_RO_SBX)
220 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
221 else
222 p->vv_di.di_flags = DI_FLAGS_FIX;
223
224 // add to v: scope dict, unless the value is not always available
Bram Moolenaard787e402021-12-24 21:36:12 +0000225 if (p->vv_tv_type != VAR_UNKNOWN)
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000226 hash_add(&vimvarht, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200227 if (p->vv_flags & VV_COMPAT)
228 // add to compat scope dict
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000229 hash_add(&compat_hashtab, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200230 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200231 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
232 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200233
234 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
235 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100236 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200237 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
238 set_vim_var_list(VV_ERRORS, list_alloc());
239 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
240
241 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
242 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
243 set_vim_var_nr(VV_NONE, VVAL_NONE);
244 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100245 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
246 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100247 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaar69b30722021-11-02 21:39:49 +0000248 set_vim_var_nr(VV_SIZEOFINT, sizeof(int));
249 set_vim_var_nr(VV_SIZEOFLONG, sizeof(long));
250 set_vim_var_nr(VV_SIZEOFPOINTER, sizeof(char *));
naohiro ono56200ee2022-01-01 14:59:44 +0000251 set_vim_var_nr(VV_MAXCOL, MAXCOL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200252
253 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
254 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
255 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
256 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
257 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
258 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
259 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
260 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
261 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
262 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
263 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarc0c2c262023-01-12 21:08:53 +0000264 set_vim_var_nr(VV_TYPE_CLASS, VAR_TYPE_CLASS);
265 set_vim_var_nr(VV_TYPE_OBJECT, VAR_TYPE_OBJECT);
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200266 set_vim_var_nr(VV_TYPE_TYPEALIAS, VAR_TYPE_TYPEALIAS);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100267 set_vim_var_nr(VV_TYPE_ENUM, VAR_TYPE_ENUM);
268 set_vim_var_nr(VV_TYPE_ENUMVALUE, VAR_TYPE_ENUMVALUE);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200269
270 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
271
Drew Vogele30d1022021-10-24 20:35:07 +0100272 set_vim_var_dict(VV_COLORNAMES, dict_alloc());
273
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200274#ifdef FEAT_PYTHON3
275 set_vim_var_nr(VV_PYTHON3_VERSION, python3_version());
276#endif
277
Bram Moolenaar439c0362020-06-06 15:58:03 +0200278 // Default for v:register is not 0 but '"'. This is adjusted once the
279 // clipboard has been setup by calling reset_reg_var().
280 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200281}
282
283#if defined(EXITFREE) || defined(PROTO)
284/*
285 * Free all vim variables information on exit
286 */
287 void
288evalvars_clear(void)
289{
290 int i;
291 struct vimvar *p;
292
293 for (i = 0; i < VV_LEN; ++i)
294 {
295 p = &vimvars[i];
296 if (p->vv_di.di_tv.v_type == VAR_STRING)
297 VIM_CLEAR(p->vv_str);
298 else if (p->vv_di.di_tv.v_type == VAR_LIST)
299 {
300 list_unref(p->vv_list);
301 p->vv_list = NULL;
302 }
303 }
304 hash_clear(&vimvarht);
305 hash_init(&vimvarht); // garbage_collect() will access it
306 hash_clear(&compat_hashtab);
307
308 // global variables
309 vars_clear(&globvarht);
310
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100311 // Script-local variables. Clear all the variables here.
312 // The scriptvar_T is cleared later in free_scriptnames(), because a
313 // variable in one script might hold a reference to the whole scope of
314 // another script.
315 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200316 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200317}
318#endif
319
320 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200321garbage_collect_globvars(int copyID)
322{
323 return set_ref_in_ht(&globvarht, copyID, NULL);
324}
325
326 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200327garbage_collect_vimvars(int copyID)
328{
329 return set_ref_in_ht(&vimvarht, copyID, NULL);
330}
331
332 int
333garbage_collect_scriptvars(int copyID)
334{
Bram Moolenaared234f22020-10-15 20:42:20 +0200335 int i;
336 int idx;
337 int abort = FALSE;
338 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200339
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100340 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200341 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200342 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
343
Bram Moolenaared234f22020-10-15 20:42:20 +0200344 si = SCRIPT_ITEM(i);
345 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
346 {
347 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
348
Bram Moolenaard00a7fb2021-03-08 20:47:14 +0100349 if (sv->sv_name != NULL)
350 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
Bram Moolenaared234f22020-10-15 20:42:20 +0200351 }
352 }
353
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200354 return abort;
355}
356
357/*
358 * Set an internal variable to a string value. Creates the variable if it does
359 * not already exist.
360 */
361 void
362set_internal_string_var(char_u *name, char_u *value)
363{
364 char_u *val;
365 typval_T *tvp;
366
367 val = vim_strsave(value);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000368 if (val == NULL)
369 return;
370
371 tvp = alloc_string_tv(val);
372 if (tvp == NULL)
373 return;
374
375 set_var(name, tvp, FALSE);
376 free_tv(tvp);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200377}
378
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200379 int
380eval_charconvert(
381 char_u *enc_from,
382 char_u *enc_to,
383 char_u *fname_from,
384 char_u *fname_to)
385{
386 int err = FALSE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000387 sctx_T saved_sctx = current_sctx;
388 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200389
390 set_vim_var_string(VV_CC_FROM, enc_from, -1);
391 set_vim_var_string(VV_CC_TO, enc_to, -1);
392 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
393 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000394 ctx = get_option_sctx("charconvert");
395 if (ctx != NULL)
396 current_sctx = *ctx;
397
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100398 if (eval_to_bool(p_ccv, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200399 err = TRUE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000400
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200401 set_vim_var_string(VV_CC_FROM, NULL, -1);
402 set_vim_var_string(VV_CC_TO, NULL, -1);
403 set_vim_var_string(VV_FNAME_IN, NULL, -1);
404 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000405 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200406
407 if (err)
408 return FAIL;
409 return OK;
410}
411
412# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
413 int
414eval_printexpr(char_u *fname, char_u *args)
415{
416 int err = FALSE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000417 sctx_T saved_sctx = current_sctx;
418 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200419
420 set_vim_var_string(VV_FNAME_IN, fname, -1);
421 set_vim_var_string(VV_CMDARG, args, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000422 ctx = get_option_sctx("printexpr");
423 if (ctx != NULL)
424 current_sctx = *ctx;
425
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100426 if (eval_to_bool(p_pexpr, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200427 err = TRUE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000428
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200429 set_vim_var_string(VV_FNAME_IN, NULL, -1);
430 set_vim_var_string(VV_CMDARG, NULL, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000431 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200432
433 if (err)
434 {
435 mch_remove(fname);
436 return FAIL;
437 }
438 return OK;
439}
440# endif
441
442# if defined(FEAT_DIFF) || defined(PROTO)
443 void
444eval_diff(
445 char_u *origfile,
446 char_u *newfile,
447 char_u *outfile)
448{
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000449 sctx_T saved_sctx = current_sctx;
450 sctx_T *ctx;
451 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200452
453 set_vim_var_string(VV_FNAME_IN, origfile, -1);
454 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
455 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000456
457 ctx = get_option_sctx("diffexpr");
458 if (ctx != NULL)
459 current_sctx = *ctx;
460
461 // errors are ignored
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100462 tv = eval_expr_ext(p_dex, NULL, TRUE);
Bram Moolenaar39b89442022-01-22 18:21:36 +0000463 free_tv(tv);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000464
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200465 set_vim_var_string(VV_FNAME_IN, NULL, -1);
466 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
467 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000468 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200469}
470
471 void
472eval_patch(
473 char_u *origfile,
474 char_u *difffile,
475 char_u *outfile)
476{
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000477 sctx_T saved_sctx = current_sctx;
478 sctx_T *ctx;
479 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200480
481 set_vim_var_string(VV_FNAME_IN, origfile, -1);
482 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
483 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000484
485 ctx = get_option_sctx("patchexpr");
486 if (ctx != NULL)
487 current_sctx = *ctx;
488
489 // errors are ignored
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100490 tv = eval_expr_ext(p_pex, NULL, TRUE);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000491 free_tv(tv);
492
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200493 set_vim_var_string(VV_FNAME_IN, NULL, -1);
494 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
495 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000496 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200497}
498# endif
499
500#if defined(FEAT_SPELL) || defined(PROTO)
501/*
502 * Evaluate an expression to a list with suggestions.
503 * For the "expr:" part of 'spellsuggest'.
504 * Returns NULL when there is an error.
505 */
506 list_T *
507eval_spell_expr(char_u *badword, char_u *expr)
508{
509 typval_T save_val;
510 typval_T rettv;
511 list_T *list = NULL;
512 char_u *p = skipwhite(expr);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000513 sctx_T saved_sctx = current_sctx;
514 sctx_T *ctx;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100515 int r;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200516
517 // Set "v:val" to the bad word.
518 prepare_vimvar(VV_VAL, &save_val);
519 set_vim_var_string(VV_VAL, badword, -1);
520 if (p_verbose == 0)
521 ++emsg_off;
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000522 ctx = get_option_sctx("spellsuggest");
523 if (ctx != NULL)
524 current_sctx = *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200525
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100526 r = may_call_simple_func(p, &rettv);
527 if (r == NOTDONE)
528 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
529 if (r == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200530 {
531 if (rettv.v_type != VAR_LIST)
532 clear_tv(&rettv);
533 else
534 list = rettv.vval.v_list;
535 }
536
537 if (p_verbose == 0)
538 --emsg_off;
539 clear_tv(get_vim_var_tv(VV_VAL));
540 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000541 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200542
543 return list;
544}
545
546/*
547 * "list" is supposed to contain two items: a word and a number. Return the
548 * word in "pp" and the number as the return value.
549 * Return -1 if anything isn't right.
550 * Used to get the good word and score from the eval_spell_expr() result.
551 */
552 int
553get_spellword(list_T *list, char_u **pp)
554{
555 listitem_T *li;
556
557 li = list->lv_first;
558 if (li == NULL)
559 return -1;
560 *pp = tv_get_string(&li->li_tv);
561
562 li = li->li_next;
563 if (li == NULL)
564 return -1;
565 return (int)tv_get_number(&li->li_tv);
566}
567#endif
568
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200569/*
570 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200571 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200572 * When not used yet add the variable to the v: hashtable.
573 */
574 void
575prepare_vimvar(int idx, typval_T *save_tv)
576{
577 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200578 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaard787e402021-12-24 21:36:12 +0000579 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000580 hash_add(&vimvarht, vimvars[idx].vv_di.di_key, "prepare vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200581}
582
583/*
584 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200585 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200586 * When no longer defined, remove the variable from the v: hashtable.
587 */
588 void
589restore_vimvar(int idx, typval_T *save_tv)
590{
591 hashitem_T *hi;
592
593 vimvars[idx].vv_tv = *save_tv;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000594 if (vimvars[idx].vv_tv_type != VAR_UNKNOWN)
595 return;
596
597 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
598 if (HASHITEM_EMPTY(hi))
599 internal_error("restore_vimvar()");
600 else
601 hash_remove(&vimvarht, hi, "restore vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200602}
603
604/*
605 * List Vim variables.
606 */
607 static void
608list_vim_vars(int *first)
609{
610 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
611}
612
613/*
614 * List script-local variables, if there is a script.
615 */
616 static void
617list_script_vars(int *first)
618{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200619 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200620 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
621 "s:", FALSE, first);
622}
623
624/*
Bram Moolenaar9510d222022-09-11 15:14:05 +0100625 * Return TRUE if "name" starts with "g:", "w:", "t:" or "b:".
626 * But only when an identifier character follows.
627 */
628 int
629is_scoped_variable(char_u *name)
630{
631 return vim_strchr((char_u *)"gwbt", name[0]) != NULL
632 && name[1] == ':'
633 && eval_isnamec(name[2]);
634}
635
636/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100637 * Evaluate one Vim expression {expr} in string "p" and append the
638 * resulting string to "gap". "p" points to the opening "{".
Bram Moolenaar70c41242022-05-10 18:11:43 +0100639 * When "evaluate" is FALSE only skip over the expression.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100640 * Return a pointer to the character after "}", NULL for an error.
641 */
642 char_u *
Bram Moolenaar70c41242022-05-10 18:11:43 +0100643eval_one_expr_in_str(char_u *p, garray_T *gap, int evaluate)
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100644{
645 char_u *block_start = skipwhite(p + 1); // skip the opening {
646 char_u *block_end = block_start;
647 char_u *expr_val;
648
649 if (*block_start == NUL)
650 {
651 semsg(_(e_missing_close_curly_str), p);
652 return NULL;
653 }
654 if (skip_expr(&block_end, NULL) == FAIL)
655 return NULL;
656 block_end = skipwhite(block_end);
657 if (*block_end != '}')
658 {
659 semsg(_(e_missing_close_curly_str), p);
660 return NULL;
661 }
Bram Moolenaar70c41242022-05-10 18:11:43 +0100662 if (evaluate)
663 {
664 *block_end = NUL;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100665 expr_val = eval_to_string(block_start, TRUE, FALSE);
Bram Moolenaar70c41242022-05-10 18:11:43 +0100666 *block_end = '}';
667 if (expr_val == NULL)
668 return NULL;
669 ga_concat(gap, expr_val);
670 vim_free(expr_val);
671 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100672
673 return block_end + 1;
674}
675
676/*
677 * Evaluate all the Vim expressions {expr} in "str" and return the resulting
678 * string in allocated memory. "{{" is reduced to "{" and "}}" to "}".
679 * Used for a heredoc assignment.
680 * Returns NULL for an error.
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100681 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +0100682 static char_u *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100683eval_all_expr_in_str(char_u *str)
684{
685 garray_T ga;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100686 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100687
688 ga_init2(&ga, 1, 80);
689 p = str;
690
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100691 while (*p != NUL)
692 {
LemonBoy2eaef102022-05-06 13:14:50 +0100693 char_u *lit_start;
LemonBoy2eaef102022-05-06 13:14:50 +0100694 int escaped_brace = FALSE;
695
696 // Look for a block start.
697 lit_start = p;
698 while (*p != '{' && *p != '}' && *p != NUL)
699 ++p;
700
701 if (*p != NUL && *p == p[1])
702 {
703 // Escaped brace, unescape and continue.
704 // Include the brace in the literal string.
705 ++p;
706 escaped_brace = TRUE;
707 }
708 else if (*p == '}')
709 {
710 semsg(_(e_stray_closing_curly_str), str);
711 ga_clear(&ga);
712 return NULL;
713 }
714
715 // Append the literal part.
716 ga_concat_len(&ga, lit_start, (size_t)(p - lit_start));
717
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100718 if (*p == NUL)
LemonBoy2eaef102022-05-06 13:14:50 +0100719 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100720
LemonBoy2eaef102022-05-06 13:14:50 +0100721 if (escaped_brace)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100722 {
LemonBoy2eaef102022-05-06 13:14:50 +0100723 // Skip the second brace.
724 ++p;
725 continue;
726 }
727
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100728 // Evaluate the expression and append the result.
Bram Moolenaar70c41242022-05-10 18:11:43 +0100729 p = eval_one_expr_in_str(p, &ga, TRUE);
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100730 if (p == NULL)
LemonBoy2eaef102022-05-06 13:14:50 +0100731 {
732 ga_clear(&ga);
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100733 return NULL;
734 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100735 }
736 ga_append(&ga, NUL);
737
738 return ga.ga_data;
739}
740
741/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200742 * Get a list of lines from a HERE document. The here document is a list of
743 * lines surrounded by a marker.
744 * cmd << {marker}
745 * {line1}
746 * {line2}
747 * ....
748 * {marker}
749 *
750 * The {marker} is a string. If the optional 'trim' word is supplied before the
751 * marker, then the leading indentation before the lines (matching the
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100752 * indentation in the "cmd" line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200753 *
754 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100755 * tcl, mzscheme), "script_get" is set to TRUE. In this case, if the marker is
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200756 * missing, then '.' is accepted as a marker.
757 *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100758 * When compiling a heredoc assignment to a variable in a Vim9 def function,
759 * "vim9compile" is set to TRUE. In this case, instead of generating a list of
760 * string values from the heredoc, vim9 instructions are generated. On success
761 * the returned list will be empty.
762 *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100763 * Returns a List with {lines} or NULL on failure.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200764 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765 list_T *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100766heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200767{
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100768 char_u *theline = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200769 char_u *marker;
770 list_T *l;
771 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100772 char_u *str;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200773 int marker_indent_len = 0;
774 int text_indent_len = 0;
775 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200776 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200777 int comment_char = in_vim9script() ? '#' : '"';
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100778 int evalstr = FALSE;
779 int eval_failed = FALSE;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100780 cctx_T *cctx = vim9compile ? eap->cookie : NULL;
781 int count = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200782
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100783 if (eap->ea_getline == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200784 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000785 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200786 return NULL;
787 }
788
789 // Check for the optional 'trim' word before the marker
790 cmd = skipwhite(cmd);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200791
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100792 while (TRUE)
793 {
794 if (STRNCMP(cmd, "trim", 4) == 0
795 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200796 {
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100797 cmd = skipwhite(cmd + 4);
798
799 // Trim the indentation from all the lines in the here document.
800 // The amount of indentation trimmed is the same as the indentation
801 // of the first line after the :let command line. To find the end
802 // marker the indent of the :let command line is trimmed.
803 p = *eap->cmdlinep;
804 while (VIM_ISWHITE(*p))
805 {
806 p++;
807 marker_indent_len++;
808 }
809 text_indent_len = -1;
810
811 continue;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200812 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100813 if (STRNCMP(cmd, "eval", 4) == 0
814 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
815 {
816 cmd = skipwhite(cmd + 4);
817 evalstr = TRUE;
818 continue;
819 }
820 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200821 }
822
823 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200824 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200825 {
826 marker = skipwhite(cmd);
827 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200828 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200829 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000830 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200831 return NULL;
832 }
833 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200834 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200835 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000836 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200837 return NULL;
838 }
839 }
840 else
841 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200842 // When getting lines for an embedded script, if the marker is missing,
843 // accept '.' as the marker.
844 if (script_get)
845 marker = dot;
846 else
847 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000848 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200849 return NULL;
850 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200851 }
852
853 l = list_alloc();
854 if (l == NULL)
855 return NULL;
856
857 for (;;)
858 {
859 int mi = 0;
860 int ti = 0;
861
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100862 vim_free(theline);
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100863 theline = eap->ea_getline(NUL, eap->cookie, 0, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200864 if (theline == NULL)
865 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000866 semsg(_(e_missing_end_marker_str), marker);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200867 break;
868 }
869
870 // with "trim": skip the indent matching the :let line to find the
871 // marker
872 if (marker_indent_len > 0
873 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
874 mi = marker_indent_len;
875 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200876 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200877
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100878 // If expression evaluation failed in the heredoc, then skip till the
879 // end marker.
880 if (eval_failed)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100881 continue;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100882
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200883 if (text_indent_len == -1 && *theline != NUL)
884 {
885 // set the text indent from the first line.
886 p = theline;
887 text_indent_len = 0;
888 while (VIM_ISWHITE(*p))
889 {
890 p++;
891 text_indent_len++;
892 }
893 text_indent = vim_strnsave(theline, text_indent_len);
894 }
895 // with "trim": skip the indent matching the first line
896 if (text_indent != NULL)
897 for (ti = 0; ti < text_indent_len; ++ti)
898 if (theline[ti] != text_indent[ti])
899 break;
900
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100901 str = theline + ti;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100902 if (vim9compile)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100903 {
LemonBoy2eaef102022-05-06 13:14:50 +0100904 if (compile_all_expr_in_str(str, evalstr, cctx) == FAIL)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100905 {
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100906 vim_free(theline);
907 vim_free(text_indent);
908 return FAIL;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100909 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100910 count++;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100911 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100912 else
913 {
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100914 if (evalstr && !eap->skip)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100915 {
916 str = eval_all_expr_in_str(str);
917 if (str == NULL)
918 {
919 // expression evaluation failed
920 eval_failed = TRUE;
921 continue;
922 }
923 vim_free(theline);
924 theline = str;
925 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100926
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100927 if (list_append_string(l, str, -1) == FAIL)
928 break;
929 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200930 }
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100931 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200932 vim_free(text_indent);
933
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100934 if (vim9compile && cctx->ctx_skip != SKIP_YES && !eval_failed)
935 generate_NEWLIST(cctx, count, FALSE);
936
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100937 if (eval_failed)
938 {
939 // expression evaluation in the heredoc failed
940 list_free(l);
941 return NULL;
942 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200943 return l;
944}
945
946/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200947 * Vim9 variable declaration:
948 * ":var name"
949 * ":var name: type"
950 * ":var name = expr"
951 * ":var name: type = expr"
952 * etc.
953 */
954 void
955ex_var(exarg_T *eap)
956{
Bram Moolenaar330a3882022-03-05 11:05:57 +0000957 char_u *p = eap->cmd;
Bram Moolenaare1d12112022-03-05 11:37:48 +0000958 int has_var;
Bram Moolenaar330a3882022-03-05 11:05:57 +0000959
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200960 if (!in_vim9script())
961 {
962 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
963 return;
964 }
Bram Moolenaare1d12112022-03-05 11:37:48 +0000965 has_var = checkforcmd_noparen(&p, "var", 3);
966 if (current_sctx.sc_sid == 0 && has_var)
Bram Moolenaar0e1574c2022-03-03 17:05:35 +0000967 {
968 emsg(_(e_cannot_declare_variable_on_command_line));
969 return;
970 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200971 ex_let(eap);
972}
973
974/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200975 * ":let" list all variable values
976 * ":let var1 var2" list variable values
977 * ":let var = expr" assignment command.
978 * ":let var += expr" assignment command.
979 * ":let var -= expr" assignment command.
980 * ":let var *= expr" assignment command.
981 * ":let var /= expr" assignment command.
982 * ":let var %= expr" assignment command.
983 * ":let var .= expr" assignment command.
984 * ":let var ..= expr" assignment command.
985 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100986 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100987 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200988 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200989 * ":final var = expr" assignment command.
990 * ":final [var1, var2] = expr" unpack list.
991 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200992 * ":const" list all variable values
993 * ":const var1 var2" list variable values
994 * ":const var = expr" assignment command.
995 * ":const [var1, var2] = expr" unpack list.
996 */
997 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200998ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200999{
1000 char_u *arg = eap->arg;
1001 char_u *expr = NULL;
1002 typval_T rettv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001003 int var_count = 0;
1004 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001005 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001006 char_u *argend;
1007 int first = TRUE;
1008 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001009 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001010 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001011 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001012
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001013 if (eap->cmdidx == CMD_final && !vim9script)
1014 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001015 // In legacy Vim script ":final" is short for ":finally".
1016 ex_finally(eap);
1017 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001018 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +02001019 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001020 {
1021 emsg(_(e_cannot_use_let_in_vim9_script));
1022 return;
1023 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001024
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001025 if (eap->cmdidx == CMD_const)
1026 flags |= ASSIGN_CONST;
1027 else if (eap->cmdidx == CMD_final)
1028 flags |= ASSIGN_FINAL;
1029
1030 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001031 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001032 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001034 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001035 if (argend == NULL)
1036 return;
1037 if (argend > arg && argend[-1] == '.') // for var.='str'
1038 --argend;
1039 expr = skipwhite(argend);
1040 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02001041 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001042 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +02001043 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
1044 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +02001045 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001046 {
1047 // ":let" without "=": list variables
1048 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001049 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001050 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001051 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001052 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001053 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001054 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001055 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001056 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001057 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001058 else
1059 // Vim9 declaration ":var name: type"
1060 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001061 }
1062 else
1063 {
1064 // ":let var1 var2" - list values
1065 arg = list_arg_vars(eap, arg, &first);
1066 }
1067 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001068 else if (!eap->skip)
1069 {
1070 // ":let"
1071 list_glob_vars(&first);
1072 list_buf_vars(&first);
1073 list_win_vars(&first);
1074 list_tab_vars(&first);
1075 list_script_vars(&first);
1076 list_func_vars(&first);
1077 list_vim_vars(&first);
1078 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02001079 set_nextcmd(eap, arg);
zeertzjq474891b2023-04-12 21:36:03 +01001080 return;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001081 }
zeertzjq474891b2023-04-12 21:36:03 +01001082
1083 if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001084 {
Bram Moolenaard9876422022-10-12 12:58:54 +01001085 list_T *l = NULL;
Bram Moolenaar81530e32021-07-28 21:25:49 +02001086 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001087
Bram Moolenaard9876422022-10-12 12:58:54 +01001088 // :let text =<< [trim] [eval] END
1089 // :var text =<< [trim] [eval] END
1090 if (vim9script && !eap->skip && (!VIM_ISWHITE(expr[-1])
1091 || !IS_WHITE_OR_NUL(expr[3])))
1092 semsg(_(e_white_space_required_before_and_after_str_at_str),
1093 "=<<", expr);
1094 else
1095 l = heredoc_get(eap, expr + 3, FALSE, FALSE);
1096
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001097 if (l != NULL)
1098 {
1099 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001100 if (!eap->skip)
1101 {
Bram Moolenaar81530e32021-07-28 21:25:49 +02001102 // errors are for the assignment, not the end marker
1103 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001104 op[0] = '=';
1105 op[1] = NUL;
1106 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001107 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001108 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001109 clear_tv(&rettv);
1110 }
zeertzjq474891b2023-04-12 21:36:03 +01001111 return;
1112 }
1113
1114 evalarg_T evalarg;
1115 int len = 1;
1116
1117 CLEAR_FIELD(rettv);
1118
1119 int cur_lnum;
1120
1121 op[0] = '=';
1122 op[1] = NUL;
1123 if (*expr != '=')
1124 {
1125 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
1126 {
1127 // +=, /=, etc. require an existing variable
1128 semsg(_(e_cannot_use_operator_on_new_variable_str), eap->arg);
1129 }
1130 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
1131 {
1132 op[0] = *expr; // +=, -=, *=, /=, %= or .=
1133 ++len;
1134 if (expr[0] == '.' && expr[1] == '.') // ..=
1135 {
1136 ++expr;
1137 ++len;
1138 }
1139 }
1140 expr += 2;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001141 }
1142 else
zeertzjq474891b2023-04-12 21:36:03 +01001143 ++expr;
1144
1145 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
1146 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001147 {
zeertzjq474891b2023-04-12 21:36:03 +01001148 vim_strncpy(op, expr - len, len);
1149 semsg(_(e_white_space_required_before_and_after_str_at_str),
1150 op, argend);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001151 }
zeertzjq474891b2023-04-12 21:36:03 +01001152
1153 if (eap->skip)
1154 ++emsg_skip;
1155 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
1156 expr = skipwhite_and_linebreak(expr, &evalarg);
1157 cur_lnum = SOURCING_LNUM;
1158 int eval_res = eval0(expr, &rettv, eap, &evalarg);
1159 if (eap->skip)
1160 --emsg_skip;
1161 clear_evalarg(&evalarg, eap);
1162
1163 // Restore the line number so that any type error is given for the
1164 // declaration, not the expression.
1165 SOURCING_LNUM = cur_lnum;
1166
1167 if (!eap->skip && eval_res != FAIL)
1168 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
1169 flags, op);
1170 if (eval_res != FAIL)
1171 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001172}
1173
1174/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001175 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001176 * Handles both "var" with any type and "[var, var; var]" with a list type.
1177 * When "op" is not NULL it points to a string with characters that
1178 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1179 * or concatenate.
1180 * Returns OK or FAIL;
1181 */
1182 int
1183ex_let_vars(
1184 char_u *arg_start,
1185 typval_T *tv,
1186 int copy, // copy values from "tv", don't move
1187 int semicolon, // from skip_var_list()
1188 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001189 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001190 char_u *op)
1191{
1192 char_u *arg = arg_start;
1193 list_T *l;
1194 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001195 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001196 listitem_T *item;
1197 typval_T ltv;
1198
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00001199 if (tv->v_type == VAR_VOID)
1200 {
1201 emsg(_(e_cannot_use_void_value));
1202 return FAIL;
1203 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001204 if (*arg != '[')
1205 {
1206 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001207 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001208 return FAIL;
1209 return OK;
1210 }
1211
1212 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1213 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
1214 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001215 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001216 return FAIL;
1217 }
1218
1219 i = list_len(l);
1220 if (semicolon == 0 && var_count < i)
1221 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001222 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001223 return FAIL;
1224 }
1225 if (var_count - semicolon > i)
1226 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001227 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001228 return FAIL;
1229 }
1230
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001231 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001232 item = l->lv_first;
1233 while (*arg != ']')
1234 {
1235 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001236 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001237 arg = ex_let_one(arg, &item->li_tv, TRUE,
1238 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001239 item = item->li_next;
1240 if (arg == NULL)
1241 return FAIL;
1242
1243 arg = skipwhite(arg);
1244 if (*arg == ';')
1245 {
1246 // Put the rest of the list (may be empty) in the var after ';'.
1247 // Create a new list for this.
1248 l = list_alloc();
1249 if (l == NULL)
1250 return FAIL;
1251 while (item != NULL)
1252 {
1253 list_append_tv(l, &item->li_tv);
1254 item = item->li_next;
1255 }
1256
1257 ltv.v_type = VAR_LIST;
1258 ltv.v_lock = 0;
1259 ltv.vval.v_list = l;
1260 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001261 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001262
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001263 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1264 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001265 clear_tv(&ltv);
1266 if (arg == NULL)
1267 return FAIL;
1268 break;
1269 }
1270 else if (*arg != ',' && *arg != ']')
1271 {
1272 internal_error("ex_let_vars()");
1273 return FAIL;
1274 }
1275 }
1276
1277 return OK;
1278}
1279
1280/*
1281 * Skip over assignable variable "var" or list of variables "[var, var]".
1282 * Used for ":let varvar = expr" and ":for varvar in expr".
1283 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001284 * for "[var, var; var]" set "semicolon" to 1.
1285 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001286 * Return NULL for an error.
1287 */
1288 char_u *
1289skip_var_list(
1290 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001292 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001293 int *semicolon,
1294 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001295{
1296 char_u *p, *s;
1297
1298 if (*arg == '[')
1299 {
1300 // "[var, var]": find the matching ']'.
1301 p = arg;
1302 for (;;)
1303 {
1304 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001305 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001306 if (s == p)
1307 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001308 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001309 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001310 return NULL;
1311 }
1312 ++*var_count;
1313
1314 p = skipwhite(s);
1315 if (*p == ']')
1316 break;
1317 else if (*p == ';')
1318 {
1319 if (*semicolon == 1)
1320 {
Bram Moolenaar8b716f52022-02-15 21:17:56 +00001321 if (!silent)
1322 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001323 return NULL;
1324 }
1325 *semicolon = 1;
1326 }
1327 else if (*p != ',')
1328 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001329 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001330 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001331 return NULL;
1332 }
1333 }
1334 return p + 1;
1335 }
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01001336
Bram Moolenaare8e369a2022-09-21 18:59:14 +01001337 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001338}
1339
1340/*
1341 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1342 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001344 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001345 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001347{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001348 char_u *end;
1349 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001351 if (*arg == '@' && arg[1] != NUL)
1352 return arg + 2;
Bram Moolenaar1573e732022-11-16 20:33:21 +00001353
1354 // termcap option name may have non-alpha characters
1355 if (STRNCMP(arg, "&t_", 3) == 0 && arg[3] != NUL && arg[4] != NUL)
1356 return arg + 5;
1357
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001358 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001359 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001360
1361 // "a: type" is declaring variable "a" with a type, not "a:".
1362 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001363 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001364 --end;
1365
Bram Moolenaar585587d2021-01-17 20:52:13 +01001366 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001367 {
Bram Moolenaarce93d162023-01-30 21:12:34 +00001368 if (*skipwhite(end) == ':')
1369 end = skip_type(skipwhite(skipwhite(end) + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001370 }
1371 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001372}
1373
1374/*
1375 * List variables for hashtab "ht" with prefix "prefix".
1376 * If "empty" is TRUE also list NULL strings as empty strings.
1377 */
1378 void
1379list_hashtable_vars(
1380 hashtab_T *ht,
1381 char *prefix,
1382 int empty,
1383 int *first)
1384{
1385 hashitem_T *hi;
1386 dictitem_T *di;
1387 int todo;
1388 char_u buf[IOSIZE];
1389
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001390 int save_ht_flags = ht->ht_flags;
1391 ht->ht_flags |= HTFLAGS_FROZEN;
1392
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001393 todo = (int)ht->ht_used;
1394 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1395 {
1396 if (!HASHITEM_EMPTY(hi))
1397 {
1398 --todo;
1399 di = HI2DI(hi);
1400
1401 // apply :filter /pat/ to variable name
1402 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1403 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1404 if (message_filtered(buf))
1405 continue;
1406
1407 if (empty || di->di_tv.v_type != VAR_STRING
1408 || di->di_tv.vval.v_string != NULL)
1409 list_one_var(di, prefix, first);
1410 }
1411 }
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001412
1413 ht->ht_flags = save_ht_flags;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001414}
1415
1416/*
1417 * List global variables.
1418 */
1419 static void
1420list_glob_vars(int *first)
1421{
1422 list_hashtable_vars(&globvarht, "", TRUE, first);
1423}
1424
1425/*
1426 * List buffer variables.
1427 */
1428 static void
1429list_buf_vars(int *first)
1430{
1431 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1432}
1433
1434/*
1435 * List window variables.
1436 */
1437 static void
1438list_win_vars(int *first)
1439{
1440 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1441}
1442
1443/*
1444 * List tab page variables.
1445 */
1446 static void
1447list_tab_vars(int *first)
1448{
1449 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1450}
1451
1452/*
1453 * List variables in "arg".
1454 */
1455 static char_u *
1456list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1457{
1458 int error = FALSE;
1459 int len;
1460 char_u *name;
1461 char_u *name_start;
1462 char_u *arg_subsc;
1463 char_u *tofree;
1464 typval_T tv;
1465
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001466 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001467 {
1468 if (error || eap->skip)
1469 {
1470 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1471 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1472 {
1473 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001474 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001475 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001476 break;
1477 }
1478 }
1479 else
1480 {
1481 // get_name_len() takes care of expanding curly braces
1482 name_start = name = arg;
1483 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1484 if (len <= 0)
1485 {
1486 // This is mainly to keep test 49 working: when expanding
1487 // curly braces fails overrule the exception error message.
1488 if (len < 0 && !aborting())
1489 {
1490 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001491 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001492 break;
1493 }
1494 error = TRUE;
1495 }
1496 else
1497 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001498 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001499 if (tofree != NULL)
1500 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001501 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001502 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001503 error = TRUE;
1504 else
1505 {
1506 // handle d.key, l[idx], f(expr)
1507 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001508 if (handle_subscript(&arg, name_start, &tv,
1509 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001510 error = TRUE;
1511 else
1512 {
1513 if (arg == arg_subsc && len == 2 && name[1] == ':')
1514 {
1515 switch (*name)
1516 {
1517 case 'g': list_glob_vars(first); break;
1518 case 'b': list_buf_vars(first); break;
1519 case 'w': list_win_vars(first); break;
1520 case 't': list_tab_vars(first); break;
1521 case 'v': list_vim_vars(first); break;
1522 case 's': list_script_vars(first); break;
1523 case 'l': list_func_vars(first); break;
1524 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001525 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001526 }
1527 }
1528 else
1529 {
1530 char_u numbuf[NUMBUFLEN];
1531 char_u *tf;
1532 int c;
1533 char_u *s;
1534
1535 s = echo_string(&tv, &tf, numbuf, 0);
1536 c = *arg;
1537 *arg = NUL;
1538 list_one_var_a("",
1539 arg == arg_subsc ? name : name_start,
1540 tv.v_type,
1541 s == NULL ? (char_u *)"" : s,
1542 first);
1543 *arg = c;
1544 vim_free(tf);
1545 }
1546 clear_tv(&tv);
1547 }
1548 }
1549 }
1550
1551 vim_free(tofree);
1552 }
1553
1554 arg = skipwhite(arg);
1555 }
1556
1557 return arg;
1558}
1559
1560/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001561 * Set an environment variable, part of ex_let_one().
1562 */
1563 static char_u *
1564ex_let_env(
1565 char_u *arg,
1566 typval_T *tv,
1567 int flags,
1568 char_u *endchars,
1569 char_u *op)
1570{
1571 char_u *arg_end = NULL;
1572 char_u *name;
1573 int len;
1574
1575 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1576 && (flags & ASSIGN_FOR_LOOP) == 0)
1577 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001578 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001579 return NULL;
1580 }
1581
1582 // Find the end of the name.
1583 ++arg;
1584 name = arg;
1585 len = get_env_len(&arg);
1586 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001587 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001588 else
1589 {
1590 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001591 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001592 else if (endchars != NULL
1593 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1594 emsg(_(e_unexpected_characters_in_let));
1595 else if (!check_secure())
1596 {
1597 char_u *tofree = NULL;
1598 int c1 = name[len];
1599 char_u *p;
1600
1601 name[len] = NUL;
1602 p = tv_get_string_chk(tv);
1603 if (p != NULL && op != NULL && *op == '.')
1604 {
1605 int mustfree = FALSE;
1606 char_u *s = vim_getenv(name, &mustfree);
1607
1608 if (s != NULL)
1609 {
1610 p = tofree = concat_str(s, p);
1611 if (mustfree)
1612 vim_free(s);
1613 }
1614 }
1615 if (p != NULL)
1616 {
1617 vim_setenv_ext(name, p);
1618 arg_end = arg;
1619 }
1620 name[len] = c1;
1621 vim_free(tofree);
1622 }
1623 }
1624 return arg_end;
1625}
1626
1627/*
1628 * Set an option, part of ex_let_one().
1629 */
1630 static char_u *
1631ex_let_option(
1632 char_u *arg,
1633 typval_T *tv,
1634 int flags,
1635 char_u *endchars,
1636 char_u *op)
1637{
1638 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001639 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001640 char_u *arg_end = NULL;
1641
1642 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1643 && (flags & ASSIGN_FOR_LOOP) == 0)
1644 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001645 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001646 return NULL;
1647 }
1648
1649 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001650 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001651 if (p == NULL || (endchars != NULL
1652 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001653 {
zeertzjq4c7cb372023-06-14 16:39:54 +01001654 emsg(_(e_unexpected_characters_in_let));
1655 return NULL;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001656 }
zeertzjq4c7cb372023-06-14 16:39:54 +01001657
1658 int c1;
1659 long n = 0;
1660 getoption_T opt_type;
1661 long numval;
1662 char_u *stringval = NULL;
1663 char_u *s = NULL;
1664 int failed = FALSE;
1665 int opt_p_flags;
1666 char_u *tofree = NULL;
1667 char_u numbuf[NUMBUFLEN];
1668
1669 c1 = *p;
1670 *p = NUL;
1671
1672 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags, scope);
1673 if (opt_type == gov_unknown && arg[0] != 't' && arg[1] != '_')
1674 {
1675 semsg(_(e_unknown_option_str_2), arg);
1676 goto theend;
1677 }
1678 if (op != NULL && *op != '='
1679 && (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1680 || (opt_type == gov_string && *op != '.')))
1681 {
1682 semsg(_(e_wrong_variable_type_for_str_equal), op);
1683 goto theend;
1684 }
1685
1686 if ((opt_type == gov_bool
1687 || opt_type == gov_number
1688 || opt_type == gov_hidden_bool
1689 || opt_type == gov_hidden_number)
1690 && (tv->v_type != VAR_STRING || !in_vim9script()))
1691 {
1692 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1693 // bool, possibly hidden
1694 n = (long)tv_get_bool_chk(tv, &failed);
1695 else
1696 // number, possibly hidden
1697 n = (long)tv_get_number_chk(tv, &failed);
1698 if (failed)
1699 goto theend;
1700 }
1701
1702 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
1703 || tv->v_type == VAR_FUNC))
1704 {
1705 // If the option can be set to a function reference or a lambda
1706 // and the passed value is a function reference, then convert it to
1707 // the name (string) of the function reference.
1708 s = tv2string(tv, &tofree, numbuf, 0);
1709 if (s == NULL)
1710 goto theend;
1711 }
1712 // Avoid setting a string option to the text "v:false" or similar.
1713 // In Vim9 script also don't convert a number to string.
1714 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1715 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1716 {
1717 s = tv_get_string_chk(tv);
1718 if (s == NULL)
1719 goto theend;
1720 }
1721 else if (opt_type == gov_string || opt_type == gov_hidden_string)
1722 {
1723 emsg(_(e_string_required));
1724 goto theend;
1725 }
1726
1727 if (op != NULL && *op != '=')
1728 {
1729 // number, in legacy script also bool
1730 if (opt_type == gov_number
1731 || (opt_type == gov_bool && !in_vim9script()))
1732 {
1733 switch (*op)
1734 {
1735 case '+': n = numval + n; break;
1736 case '-': n = numval - n; break;
1737 case '*': n = numval * n; break;
1738 case '/': n = (long)num_divide(numval, n, &failed); break;
1739 case '%': n = (long)num_modulus(numval, n, &failed); break;
1740 }
1741 s = NULL;
1742 if (failed)
1743 goto theend;
1744 }
1745 else if (opt_type == gov_string && stringval != NULL && s != NULL)
1746 {
1747 // string
1748 s = concat_str(stringval, s);
1749 vim_free(stringval);
1750 stringval = s;
1751 }
1752 }
1753
1754 char *err = set_option_value(arg, n, s, scope);
1755 arg_end = p;
1756 if (err != NULL)
1757 emsg(_(err));
1758
1759theend:
1760 *p = c1;
1761 vim_free(stringval);
1762 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001763 return arg_end;
1764}
1765
1766/*
1767 * Set a register, part of ex_let_one().
1768 */
1769 static char_u *
1770ex_let_register(
1771 char_u *arg,
1772 typval_T *tv,
1773 int flags,
1774 char_u *endchars,
1775 char_u *op)
1776{
1777 char_u *arg_end = NULL;
1778
1779 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1780 && (flags & ASSIGN_FOR_LOOP) == 0)
1781 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001782 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001783 return NULL;
1784 }
1785 ++arg;
1786 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001787 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001788 else if (endchars != NULL
1789 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1790 emsg(_(e_unexpected_characters_in_let));
1791 else
1792 {
1793 char_u *ptofree = NULL;
1794 char_u *p;
1795
1796 p = tv_get_string_chk(tv);
1797 if (p != NULL && op != NULL && *op == '.')
1798 {
1799 char_u *s = get_reg_contents(*arg == '@'
1800 ? '"' : *arg, GREG_EXPR_SRC);
1801
1802 if (s != NULL)
1803 {
1804 p = ptofree = concat_str(s, p);
1805 vim_free(s);
1806 }
1807 }
1808 if (p != NULL)
1809 {
1810 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1811 arg_end = arg + 1;
1812 }
1813 vim_free(ptofree);
1814 }
1815 return arg_end;
1816}
1817
1818/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001819 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1820 * Returns a pointer to the char just after the var name.
1821 * Returns NULL if there is an error.
1822 */
1823 static char_u *
1824ex_let_one(
1825 char_u *arg, // points to variable name
1826 typval_T *tv, // value to assign to variable
1827 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001828 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001829 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001830 char_u *op, // "+", "-", "." or NULL
1831 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001832{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001833 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001834
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001835 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001836 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001837 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1838 {
1839 vim9_declare_error(arg);
1840 return NULL;
1841 }
1842
Ernie Rael9ed53752023-12-11 17:40:46 +01001843 if (check_typval_is_value(tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001844 return NULL;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001845
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001846 if (*arg == '$')
1847 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001848 // ":let $VAR = expr": Set environment variable.
1849 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001850 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001851 else if (*arg == '&')
1852 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001853 // ":let &option = expr": Set option value.
1854 // ":let &l:option = expr": Set local option value.
1855 // ":let &g:option = expr": Set global option value.
1856 // ":for &ts in range(8)": Set option value for for loop
1857 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001858 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001859 else if (*arg == '@')
1860 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001861 // ":let @r = expr": Set register contents.
1862 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001863 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001864 else if (eval_isnamec1(*arg) || *arg == '{')
1865 {
1866 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001867 char_u *p;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001868 int lval_flags = (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1869 ? GLV_NO_DECL : 0;
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02001870 lval_flags |= (flags & ASSIGN_FOR_LOOP) ? GLV_FOR_LOOP : 0;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001871 if (op != NULL && *op != '=')
1872 lval_flags |= GLV_ASSIGN_WITH_OP;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001873
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001874 // ":let var = expr": Set internal variable.
1875 // ":let var: type = expr": Set internal variable with type.
1876 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001877 p = get_lval(arg, tv, &lv, FALSE, FALSE, lval_flags, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001878 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001879 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001880 if (endchars != NULL && vim_strchr(endchars,
1881 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001882 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001883 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001884 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001885 else
1886 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001887 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001888 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001889 }
1890 }
1891 clear_lval(&lv);
1892 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001893 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001894 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001895
1896 return arg_end;
1897}
1898
1899/*
1900 * ":unlet[!] var1 ... " command.
1901 */
1902 void
1903ex_unlet(exarg_T *eap)
1904{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001905 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001906}
1907
1908/*
1909 * ":lockvar" and ":unlockvar" commands
1910 */
1911 void
1912ex_lockvar(exarg_T *eap)
1913{
1914 char_u *arg = eap->arg;
1915 int deep = 2;
1916
1917 if (eap->forceit)
1918 deep = -1;
1919 else if (vim_isdigit(*arg))
1920 {
1921 deep = getdigits(&arg);
1922 arg = skipwhite(arg);
1923 }
1924
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001925 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001926}
1927
1928/*
1929 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001930 * Also used for Vim9 script. "callback" is invoked as:
1931 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001932 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001933 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001934ex_unletlock(
1935 exarg_T *eap,
1936 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001937 int deep,
1938 int glv_flags,
1939 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1940 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001941{
1942 char_u *arg = argstart;
1943 char_u *name_end;
1944 int error = FALSE;
1945 lval_T lv;
1946
1947 do
1948 {
1949 if (*arg == '$')
1950 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001951 lv.ll_name = arg;
1952 lv.ll_tv = NULL;
1953 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001954 if (get_env_len(&arg) == 0)
1955 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001956 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001957 return;
1958 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001959 if (!error && !eap->skip
1960 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1961 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001962 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001963 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001964 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001965 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001966 // Parse the name and find the end.
1967 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001968 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001969 if (lv.ll_name == NULL)
1970 error = TRUE; // error but continue parsing
1971 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1972 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001973 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001974 if (name_end != NULL)
1975 {
1976 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00001977 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001978 }
1979 if (!(eap->skip || error))
1980 clear_lval(&lv);
1981 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001982 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001983
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001984 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001985 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001986 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001987
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001988 if (!eap->skip)
1989 clear_lval(&lv);
1990 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001991
1992 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001993 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001994
Bram Moolenaar63b91732021-08-05 20:40:03 +02001995 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001996}
1997
1998 static int
1999do_unlet_var(
2000 lval_T *lp,
2001 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002002 exarg_T *eap,
2003 int deep UNUSED,
2004 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002005{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002006 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002007 int ret = OK;
2008 int cc;
2009
2010 if (lp->ll_tv == NULL)
2011 {
2012 cc = *name_end;
2013 *name_end = NUL;
2014
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002015 // Environment variable, normal name or expanded name.
2016 if (*lp->ll_name == '$')
LemonBoy77142312022-04-15 20:50:46 +01002017 vim_unsetenv_ext(lp->ll_name + 1);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002018 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002019 ret = FAIL;
2020 *name_end = cc;
2021 }
2022 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002023 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002024 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002025 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002026 return FAIL;
2027 else if (lp->ll_range)
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002028 list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_n1,
2029 !lp->ll_empty2, lp->ll_n2);
2030 else if (lp->ll_list != NULL)
2031 // unlet a List item.
2032 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002033 else
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002034 // unlet a Dictionary item.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002035 dictitem_remove(lp->ll_dict, lp->ll_di, "unlet");
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002036
2037 return ret;
2038}
2039
2040/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002041 * Unlet one item or a range of items from a list.
2042 * Return OK or FAIL.
2043 */
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002044 void
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002045list_unlet_range(
2046 list_T *l,
2047 listitem_T *li_first,
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002048 long n1_arg,
2049 int has_n2,
2050 long n2)
2051{
2052 listitem_T *li = li_first;
2053 int n1 = n1_arg;
2054
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002055 // Delete a range of List items.
2056 li = li_first;
2057 n1 = n1_arg;
2058 while (li != NULL && (!has_n2 || n2 >= n1))
2059 {
2060 listitem_T *next = li->li_next;
2061
2062 listitem_remove(l, li);
2063 li = next;
2064 ++n1;
2065 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002066}
2067/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002068 * "unlet" a variable. Return OK if it existed, FAIL if not.
2069 * When "forceit" is TRUE don't complain if the variable doesn't exist.
2070 */
2071 int
2072do_unlet(char_u *name, int forceit)
2073{
2074 hashtab_T *ht;
2075 hashitem_T *hi;
mityu8be36ee2022-05-25 17:29:46 +01002076 char_u *varname = NULL; // init to shut up gcc
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002077 dict_T *d;
2078 dictitem_T *di;
2079
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002080 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002081 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002082 return FAIL;
2083
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002084 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002085
2086 // can't :unlet a script variable in Vim9 script from a function
2087 if (ht == get_script_local_ht()
2088 && SCRIPT_ID_VALID(current_sctx.sc_sid)
2089 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2090 == SCRIPT_VERSION_VIM9
2091 && check_vim9_unlet(name) == FAIL)
2092 return FAIL;
2093
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002094 if (ht != NULL && *varname != NUL)
2095 {
2096 d = get_current_funccal_dict(ht);
2097 if (d == NULL)
2098 {
2099 if (ht == &globvarht)
2100 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002101 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002102 d = &vimvardict;
2103 else
2104 {
2105 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2106 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2107 }
2108 if (d == NULL)
2109 {
2110 internal_error("do_unlet()");
2111 return FAIL;
2112 }
2113 }
2114 hi = hash_find(ht, varname);
2115 if (HASHITEM_EMPTY(hi))
2116 hi = find_hi_in_scoped_ht(name, &ht);
2117 if (hi != NULL && !HASHITEM_EMPTY(hi))
2118 {
2119 di = HI2DI(hi);
2120 if (var_check_fixed(di->di_flags, name, FALSE)
2121 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002122 || value_check_lock(d->dv_lock, name, FALSE)
2123 || check_hashtab_frozen(ht, "unlet"))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002124 return FAIL;
2125
2126 delete_var(ht, hi);
2127 return OK;
2128 }
2129 }
2130 if (forceit)
2131 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00002132 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002133 return FAIL;
2134}
2135
Ernie Raelee865f32023-09-29 19:53:55 +02002136 static void
2137report_lockvar_member(char *msg, lval_T *lp)
2138{
2139 int did_alloc = FALSE;
2140 char_u *vname = (char_u *)"";
2141 char_u *class_name = lp->ll_class != NULL
2142 ? lp->ll_class->class_name : (char_u *)"";
2143 if (lp->ll_name != NULL)
2144 {
2145 if (lp->ll_name_end == NULL)
2146 vname = lp->ll_name;
2147 else
2148 {
2149 vname = vim_strnsave(lp->ll_name, lp->ll_name_end - lp->ll_name);
2150 if (vname == NULL)
2151 return;
2152 did_alloc = TRUE;
2153 }
2154 }
2155 semsg(_(msg), vname, class_name);
2156 if (did_alloc)
2157 vim_free(vname);
2158}
2159
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002160/*
2161 * Lock or unlock variable indicated by "lp".
2162 * "deep" is the levels to go (-1 for unlimited);
2163 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2164 */
2165 static int
2166do_lock_var(
2167 lval_T *lp,
2168 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002169 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002170 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002171 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002172{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002173 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002174 int ret = OK;
2175 int cc;
2176 dictitem_T *di;
2177
Ernie Raelee865f32023-09-29 19:53:55 +02002178#ifdef LOG_LOCKVAR
2179 ch_log(NULL, "LKVAR: do_lock_var(): name %s, is_root %d", lp->ll_name, lp->ll_is_root);
2180#endif
2181
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002182 if (lp->ll_tv == NULL)
2183 {
2184 cc = *name_end;
2185 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002186 if (*lp->ll_name == '$')
2187 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002188 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002189 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002190 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002191 else
2192 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002193 // Normal name or expanded name.
2194 di = find_var(lp->ll_name, NULL, TRUE);
2195 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002196 {
2197 if (in_vim9script())
2198 semsg(_(e_cannot_find_variable_to_unlock_str),
2199 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002200 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002201 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002202 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002203 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002204 if ((di->di_flags & DI_FLAGS_FIX)
2205 && di->di_tv.v_type != VAR_DICT
2206 && di->di_tv.v_type != VAR_LIST)
2207 {
2208 // For historic reasons this error is not given for a list
2209 // or dict. E.g., the b: dict could be locked/unlocked.
2210 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
2211 ret = FAIL;
2212 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002213 else
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002214 {
2215 if (in_vim9script())
2216 {
2217 svar_T *sv = find_typval_in_script(&di->di_tv,
2218 0, FALSE);
2219
2220 if (sv != NULL && sv->sv_const != 0)
2221 {
2222 semsg(_(e_cannot_change_readonly_variable_str),
2223 lp->ll_name);
2224 ret = FAIL;
2225 }
2226 }
2227
2228 if (ret == OK)
2229 {
2230 if (lock)
2231 di->di_flags |= DI_FLAGS_LOCK;
2232 else
2233 di->di_flags &= ~DI_FLAGS_LOCK;
2234 if (deep != 0)
2235 item_lock(&di->di_tv, deep, lock, FALSE);
2236 }
2237 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002238 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002239 }
2240 *name_end = cc;
2241 }
Ernie Raelee865f32023-09-29 19:53:55 +02002242 else if (deep == 0 && lp->ll_object == NULL && lp->ll_class == NULL)
Bram Moolenaara187c432020-09-16 21:08:28 +02002243 {
2244 // nothing to do
2245 }
Ernie Raelee865f32023-09-29 19:53:55 +02002246 else if (lp->ll_is_root)
2247 // (un)lock the item.
2248 item_lock(lp->ll_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002249 else if (lp->ll_range)
2250 {
2251 listitem_T *li = lp->ll_li;
2252
2253 // (un)lock a range of List items.
2254 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2255 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02002256 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002257 li = li->li_next;
2258 ++lp->ll_n1;
2259 }
2260 }
2261 else if (lp->ll_list != NULL)
2262 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002263 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Ernie Raelee865f32023-09-29 19:53:55 +02002264 else if (lp->ll_object != NULL) // This check must be before ll_class.
2265 {
2266 // (un)lock an object variable.
2267 report_lockvar_member(e_cannot_lock_object_variable_str, lp);
2268 ret = FAIL;
2269 }
2270 else if (lp->ll_class != NULL)
2271 {
2272 // (un)lock a class variable.
2273 report_lockvar_member(e_cannot_lock_class_variable_str, lp);
2274 ret = FAIL;
2275 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002276 else
Ernie Raelee865f32023-09-29 19:53:55 +02002277 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002278 // (un)lock a Dictionary item.
Ernie Raelee865f32023-09-29 19:53:55 +02002279 if (lp->ll_di == NULL)
2280 {
2281 emsg(_(e_dictionary_required));
2282 ret = FAIL;
2283 }
2284 else
2285 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
2286 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002287
2288 return ret;
2289}
2290
2291/*
2292 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002293 * When "check_refcount" is TRUE do not lock a list or dict with a reference
2294 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002295 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002296 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02002297item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002298{
2299 static int recurse = 0;
2300 list_T *l;
2301 listitem_T *li;
2302 dict_T *d;
2303 blob_T *b;
2304 hashitem_T *hi;
2305 int todo;
2306
Ernie Raelee865f32023-09-29 19:53:55 +02002307#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02002308 ch_log(NULL, "LKVAR: item_lock(): type %s", vartype_name(tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02002309#endif
2310
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002311 if (recurse >= DICT_MAXNEST)
2312 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002313 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002314 return;
2315 }
2316 if (deep == 0)
2317 return;
2318 ++recurse;
2319
2320 // lock/unlock the item itself
2321 if (lock)
2322 tv->v_lock |= VAR_LOCKED;
2323 else
2324 tv->v_lock &= ~VAR_LOCKED;
2325
2326 switch (tv->v_type)
2327 {
2328 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002329 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002331 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002333 case VAR_STRING:
2334 case VAR_FUNC:
2335 case VAR_PARTIAL:
2336 case VAR_FLOAT:
2337 case VAR_SPECIAL:
2338 case VAR_JOB:
2339 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002340 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002341 case VAR_CLASS:
2342 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002343 case VAR_TYPEALIAS:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002344 break;
2345
2346 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002347 if ((b = tv->vval.v_blob) != NULL
2348 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002349 {
2350 if (lock)
2351 b->bv_lock |= VAR_LOCKED;
2352 else
2353 b->bv_lock &= ~VAR_LOCKED;
2354 }
2355 break;
2356 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002357 if ((l = tv->vval.v_list) != NULL
2358 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002359 {
2360 if (lock)
2361 l->lv_lock |= VAR_LOCKED;
2362 else
2363 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002364 if (deep < 0 || deep > 1)
2365 {
2366 if (l->lv_first == &range_list_item)
2367 l->lv_lock |= VAR_ITEMS_LOCKED;
2368 else
2369 {
2370 // recursive: lock/unlock the items the List contains
2371 CHECK_LIST_MATERIALIZE(l);
2372 FOR_ALL_LIST_ITEMS(l, li) item_lock(&li->li_tv,
2373 deep - 1, lock, check_refcount);
2374 }
2375 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002376 }
2377 break;
2378 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002379 if ((d = tv->vval.v_dict) != NULL
2380 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002381 {
2382 if (lock)
2383 d->dv_lock |= VAR_LOCKED;
2384 else
2385 d->dv_lock &= ~VAR_LOCKED;
2386 if (deep < 0 || deep > 1)
2387 {
2388 // recursive: lock/unlock the items the List contains
2389 todo = (int)d->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002390 FOR_ALL_HASHTAB_ITEMS(&d->dv_hashtab, hi, todo)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002391 {
2392 if (!HASHITEM_EMPTY(hi))
2393 {
2394 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002395 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2396 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002397 }
2398 }
2399 }
2400 }
2401 }
2402 --recurse;
2403}
2404
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002405#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2406/*
2407 * Delete all "menutrans_" variables.
2408 */
2409 void
2410del_menutrans_vars(void)
2411{
2412 hashitem_T *hi;
2413 int todo;
2414
2415 hash_lock(&globvarht);
2416 todo = (int)globvarht.ht_used;
2417 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2418 {
2419 if (!HASHITEM_EMPTY(hi))
2420 {
2421 --todo;
2422 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2423 delete_var(&globvarht, hi);
2424 }
2425 }
2426 hash_unlock(&globvarht);
2427}
2428#endif
2429
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002430/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002431 * Local string buffer for the next two functions to store a variable name
2432 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2433 * get_user_var_name().
2434 */
2435
2436static char_u *varnamebuf = NULL;
2437static int varnamebuflen = 0;
2438
2439/*
2440 * Function to concatenate a prefix and a variable name.
2441 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002442 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002443cat_prefix_varname(int prefix, char_u *name)
2444{
2445 int len;
2446
2447 len = (int)STRLEN(name) + 3;
2448 if (len > varnamebuflen)
2449 {
2450 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002451 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002452 varnamebuf = alloc(len);
2453 if (varnamebuf == NULL)
2454 {
2455 varnamebuflen = 0;
2456 return NULL;
2457 }
2458 varnamebuflen = len;
2459 }
2460 *varnamebuf = prefix;
2461 varnamebuf[1] = ':';
2462 STRCPY(varnamebuf + 2, name);
2463 return varnamebuf;
2464}
2465
2466/*
2467 * Function given to ExpandGeneric() to obtain the list of user defined
2468 * (global/buffer/window/built-in) variable names.
2469 */
2470 char_u *
2471get_user_var_name(expand_T *xp, int idx)
2472{
2473 static long_u gdone;
2474 static long_u bdone;
2475 static long_u wdone;
2476 static long_u tdone;
2477 static int vidx;
2478 static hashitem_T *hi;
2479 hashtab_T *ht;
2480
2481 if (idx == 0)
2482 {
2483 gdone = bdone = wdone = vidx = 0;
2484 tdone = 0;
2485 }
2486
2487 // Global variables
2488 if (gdone < globvarht.ht_used)
2489 {
2490 if (gdone++ == 0)
2491 hi = globvarht.ht_array;
2492 else
2493 ++hi;
2494 while (HASHITEM_EMPTY(hi))
2495 ++hi;
2496 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2497 return cat_prefix_varname('g', hi->hi_key);
2498 return hi->hi_key;
2499 }
2500
2501 // b: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002502 ht = &prevwin_curwin()->w_buffer->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002503 if (bdone < ht->ht_used)
2504 {
2505 if (bdone++ == 0)
2506 hi = ht->ht_array;
2507 else
2508 ++hi;
2509 while (HASHITEM_EMPTY(hi))
2510 ++hi;
2511 return cat_prefix_varname('b', hi->hi_key);
2512 }
2513
2514 // w: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002515 ht = &prevwin_curwin()->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002516 if (wdone < ht->ht_used)
2517 {
2518 if (wdone++ == 0)
2519 hi = ht->ht_array;
2520 else
2521 ++hi;
2522 while (HASHITEM_EMPTY(hi))
2523 ++hi;
2524 return cat_prefix_varname('w', hi->hi_key);
2525 }
2526
2527 // t: variables
2528 ht = &curtab->tp_vars->dv_hashtab;
2529 if (tdone < ht->ht_used)
2530 {
2531 if (tdone++ == 0)
2532 hi = ht->ht_array;
2533 else
2534 ++hi;
2535 while (HASHITEM_EMPTY(hi))
2536 ++hi;
2537 return cat_prefix_varname('t', hi->hi_key);
2538 }
2539
2540 // v: variables
2541 if (vidx < VV_LEN)
2542 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2543
2544 VIM_CLEAR(varnamebuf);
2545 varnamebuflen = 0;
2546 return NULL;
2547}
2548
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002549 char *
2550get_var_special_name(int nr)
2551{
2552 switch (nr)
2553 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002554 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2555 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002556 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002557 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002558 }
2559 internal_error("get_var_special_name()");
2560 return "42";
2561}
2562
2563/*
2564 * Returns the global variable dictionary
2565 */
2566 dict_T *
2567get_globvar_dict(void)
2568{
2569 return &globvardict;
2570}
2571
2572/*
2573 * Returns the global variable hash table
2574 */
2575 hashtab_T *
2576get_globvar_ht(void)
2577{
2578 return &globvarht;
2579}
2580
2581/*
2582 * Returns the v: variable dictionary
2583 */
2584 dict_T *
2585get_vimvar_dict(void)
2586{
2587 return &vimvardict;
2588}
2589
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002590/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002592 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 */
2594 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002595find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002597 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2598 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599
2600 if (di == NULL)
2601 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002602 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2604 return (int)(vv - vimvars);
2605}
2606
2607
2608/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002609 * Set type of v: variable to "type".
2610 */
2611 void
2612set_vim_var_type(int idx, vartype_T type)
2613{
Bram Moolenaard787e402021-12-24 21:36:12 +00002614 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002615}
2616
2617/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002618 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002619 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002620 */
2621 void
2622set_vim_var_nr(int idx, varnumber_T val)
2623{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002624 vimvars[idx].vv_nr = val;
2625}
2626
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002627 char *
2628get_vim_var_name(int idx)
2629{
2630 return vimvars[idx].vv_name;
2631}
2632
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002633/*
2634 * Get typval_T v: variable value.
2635 */
2636 typval_T *
2637get_vim_var_tv(int idx)
2638{
2639 return &vimvars[idx].vv_tv;
2640}
2641
Bram Moolenaard787e402021-12-24 21:36:12 +00002642 type_T *
2643get_vim_var_type(int idx, garray_T *type_list)
2644{
2645 if (vimvars[idx].vv_type != NULL)
2646 return vimvars[idx].vv_type;
2647 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2648}
2649
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002650/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002651 * Set v: variable to "tv". Only accepts the same type.
2652 * Takes over the value of "tv".
2653 */
2654 int
2655set_vim_var_tv(int idx, typval_T *tv)
2656{
Bram Moolenaard787e402021-12-24 21:36:12 +00002657 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002658 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002659 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002660 clear_tv(tv);
2661 return FAIL;
2662 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002663 // VV_RO is also checked when compiling, but let's check here as well.
2664 if (vimvars[idx].vv_flags & VV_RO)
2665 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002666 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002667 return FAIL;
2668 }
2669 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2670 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002671 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002672 return FAIL;
2673 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002674 clear_tv(&vimvars[idx].vv_di.di_tv);
2675 vimvars[idx].vv_di.di_tv = *tv;
2676 return OK;
2677}
2678
2679/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002680 * Get number v: variable value.
2681 */
2682 varnumber_T
2683get_vim_var_nr(int idx)
2684{
2685 return vimvars[idx].vv_nr;
2686}
2687
2688/*
2689 * Get string v: variable value. Uses a static buffer, can only be used once.
2690 * If the String variable has never been set, return an empty string.
2691 * Never returns NULL;
2692 */
2693 char_u *
2694get_vim_var_str(int idx)
2695{
2696 return tv_get_string(&vimvars[idx].vv_tv);
2697}
2698
2699/*
2700 * Get List v: variable value. Caller must take care of reference count when
2701 * needed.
2702 */
2703 list_T *
2704get_vim_var_list(int idx)
2705{
2706 return vimvars[idx].vv_list;
2707}
2708
2709/*
2710 * Get Dict v: variable value. Caller must take care of reference count when
2711 * needed.
2712 */
2713 dict_T *
2714get_vim_var_dict(int idx)
2715{
2716 return vimvars[idx].vv_dict;
2717}
2718
2719/*
2720 * Set v:char to character "c".
2721 */
2722 void
2723set_vim_var_char(int c)
2724{
2725 char_u buf[MB_MAXBYTES + 1];
2726
2727 if (has_mbyte)
2728 buf[(*mb_char2bytes)(c, buf)] = NUL;
2729 else
2730 {
2731 buf[0] = c;
2732 buf[1] = NUL;
2733 }
2734 set_vim_var_string(VV_CHAR, buf, -1);
2735}
2736
2737/*
2738 * Set v:count to "count" and v:count1 to "count1".
2739 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2740 */
2741 void
2742set_vcount(
2743 long count,
2744 long count1,
2745 int set_prevcount)
2746{
2747 if (set_prevcount)
2748 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2749 vimvars[VV_COUNT].vv_nr = count;
2750 vimvars[VV_COUNT1].vv_nr = count1;
2751}
2752
2753/*
2754 * Save variables that might be changed as a side effect. Used when executing
2755 * a timer callback.
2756 */
2757 void
2758save_vimvars(vimvars_save_T *vvsave)
2759{
2760 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2761 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2762 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2763}
2764
2765/*
2766 * Restore variables saved by save_vimvars().
2767 */
2768 void
2769restore_vimvars(vimvars_save_T *vvsave)
2770{
2771 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2772 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2773 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2774}
2775
2776/*
2777 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2778 * value.
2779 */
2780 void
2781set_vim_var_string(
2782 int idx,
2783 char_u *val,
2784 int len) // length of "val" to use or -1 (whole string)
2785{
2786 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002787 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002788 if (val == NULL)
2789 vimvars[idx].vv_str = NULL;
2790 else if (len == -1)
2791 vimvars[idx].vv_str = vim_strsave(val);
2792 else
2793 vimvars[idx].vv_str = vim_strnsave(val, len);
2794}
2795
2796/*
2797 * Set List v: variable to "val".
2798 */
2799 void
2800set_vim_var_list(int idx, list_T *val)
2801{
2802 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002803 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002804 vimvars[idx].vv_list = val;
2805 if (val != NULL)
2806 ++val->lv_refcount;
2807}
2808
2809/*
2810 * Set Dictionary v: variable to "val".
2811 */
2812 void
2813set_vim_var_dict(int idx, dict_T *val)
2814{
2815 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002816 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002817 vimvars[idx].vv_dict = val;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002818 if (val == NULL)
2819 return;
2820
2821 ++val->dv_refcount;
2822 dict_set_items_ro(val);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002823}
2824
2825/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002826 * Set the v:argv list.
2827 */
2828 void
2829set_argv_var(char **argv, int argc)
2830{
2831 list_T *l = list_alloc();
2832 int i;
2833
2834 if (l == NULL)
2835 getout(1);
2836 l->lv_lock = VAR_FIXED;
2837 for (i = 0; i < argc; ++i)
2838 {
2839 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2840 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002841 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002842 }
2843 set_vim_var_list(VV_ARGV, l);
2844}
2845
2846/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002847 * Reset v:register, taking the 'clipboard' setting into account.
2848 */
2849 void
2850reset_reg_var(void)
2851{
2852 int regname = 0;
2853
2854 // Adjust the register according to 'clipboard', so that when
2855 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2856#ifdef FEAT_CLIPBOARD
2857 adjust_clip_reg(&regname);
2858#endif
2859 set_reg_var(regname);
2860}
2861
2862/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002863 * Set v:register if needed.
2864 */
2865 void
2866set_reg_var(int c)
2867{
2868 char_u regname;
2869
2870 if (c == 0 || c == ' ')
2871 regname = '"';
2872 else
2873 regname = c;
2874 // Avoid free/alloc when the value is already right.
2875 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2876 set_vim_var_string(VV_REG, &regname, 1);
2877}
2878
2879/*
2880 * Get or set v:exception. If "oldval" == NULL, return the current value.
2881 * Otherwise, restore the value to "oldval" and return NULL.
2882 * Must always be called in pairs to save and restore v:exception! Does not
2883 * take care of memory allocations.
2884 */
2885 char_u *
2886v_exception(char_u *oldval)
2887{
2888 if (oldval == NULL)
2889 return vimvars[VV_EXCEPTION].vv_str;
2890
2891 vimvars[VV_EXCEPTION].vv_str = oldval;
2892 return NULL;
2893}
2894
2895/*
2896 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2897 * Otherwise, restore the value to "oldval" and return NULL.
2898 * Must always be called in pairs to save and restore v:throwpoint! Does not
2899 * take care of memory allocations.
2900 */
2901 char_u *
2902v_throwpoint(char_u *oldval)
2903{
2904 if (oldval == NULL)
2905 return vimvars[VV_THROWPOINT].vv_str;
2906
2907 vimvars[VV_THROWPOINT].vv_str = oldval;
2908 return NULL;
2909}
2910
2911/*
2912 * Set v:cmdarg.
2913 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2914 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2915 * Must always be called in pairs!
2916 */
2917 char_u *
2918set_cmdarg(exarg_T *eap, char_u *oldarg)
2919{
2920 char_u *oldval;
2921 char_u *newval;
2922 unsigned len;
2923
2924 oldval = vimvars[VV_CMDARG].vv_str;
2925 if (eap == NULL)
2926 {
2927 vim_free(oldval);
2928 vimvars[VV_CMDARG].vv_str = oldarg;
2929 return NULL;
2930 }
2931
2932 if (eap->force_bin == FORCE_BIN)
2933 len = 6;
2934 else if (eap->force_bin == FORCE_NOBIN)
2935 len = 8;
2936 else
2937 len = 0;
2938
2939 if (eap->read_edit)
2940 len += 7;
2941
2942 if (eap->force_ff != 0)
2943 len += 10; // " ++ff=unix"
2944 if (eap->force_enc != 0)
2945 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2946 if (eap->bad_char != 0)
2947 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2948
2949 newval = alloc(len + 1);
2950 if (newval == NULL)
2951 return NULL;
2952
2953 if (eap->force_bin == FORCE_BIN)
2954 sprintf((char *)newval, " ++bin");
2955 else if (eap->force_bin == FORCE_NOBIN)
2956 sprintf((char *)newval, " ++nobin");
2957 else
2958 *newval = NUL;
2959
2960 if (eap->read_edit)
2961 STRCAT(newval, " ++edit");
2962
2963 if (eap->force_ff != 0)
2964 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2965 eap->force_ff == 'u' ? "unix"
2966 : eap->force_ff == 'd' ? "dos"
2967 : "mac");
2968 if (eap->force_enc != 0)
2969 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2970 eap->cmd + eap->force_enc);
2971 if (eap->bad_char == BAD_KEEP)
2972 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2973 else if (eap->bad_char == BAD_DROP)
2974 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2975 else if (eap->bad_char != 0)
2976 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2977 vimvars[VV_CMDARG].vv_str = newval;
2978 return oldval;
2979}
2980
2981/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002982 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002983 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2984 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002985 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2986 */
2987 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002988eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002989 char_u *name,
Bram Moolenaar94674f22023-01-06 18:42:20 +00002990 int len, // length of "name" or zero
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002991 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002992 typval_T *rettv, // NULL when only checking existence
2993 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002994 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002995{
2996 int ret = OK;
2997 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002998 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002999 hashtab_T *ht = NULL;
Bram Moolenaar94674f22023-01-06 18:42:20 +00003000 int cc = 0;
Bram Moolenaarc967d572021-07-08 21:38:50 +02003001 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003002
Bram Moolenaar94674f22023-01-06 18:42:20 +00003003 if (len > 0)
3004 {
3005 // truncate the name, so that we can use strcmp()
3006 cc = name[len];
3007 name[len] = NUL;
3008 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003009
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003010 // Check for local variable when debugging.
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003011 if ((sid == 0) && (tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003012 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003013 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02003014 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
3015
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003016 if (v != NULL)
3017 {
3018 tv = &v->di_tv;
3019 if (dip != NULL)
3020 *dip = v;
3021 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02003022 else
3023 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003024 }
3025
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003026 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003028 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003029 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
3030
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003031 if (sid == 0)
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003032 import = find_imported(p, 0, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033
3034 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003035 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003037 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02003038 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00003039 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02003040 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003041 ht = &SCRIPT_VARS(sid);
3042 if (ht != NULL)
3043 {
3044 dictitem_T *v = find_var_in_ht(ht, 0, name,
3045 flags & EVAL_VAR_NOAUTOLOAD);
3046
3047 if (v != NULL)
3048 {
3049 tv = &v->di_tv;
3050 if (dip != NULL)
3051 *dip = v;
3052 }
3053 else
3054 ht = NULL;
3055 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003056 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003057 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003058 {
3059 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00003060 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003061 ret = FAIL;
3062 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02003063 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003064 else
3065 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003066 if (rettv != NULL)
3067 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01003068 // special value that is used in handle_subscript()
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003069 rettv->v_type = VAR_ANY;
3070 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
3071 }
3072 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02003073 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00003075 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003076 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003077 int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003078 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003079
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003080 // In Vim9 script we can get a function reference by using the
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003081 // function name. For a global non-autoload function "g:" is
3082 // required.
3083 if (ufunc != NULL && (has_g_prefix
3084 || !func_requires_g_prefix(ufunc)))
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003085 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003086 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003087 if (rettv != NULL)
3088 {
3089 rettv->v_type = VAR_FUNC;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003090 if (has_g_prefix)
Bram Moolenaaref082e12021-12-12 21:02:03 +00003091 // Keep the "g:", otherwise script-local may be
3092 // assumed.
3093 rettv->vval.v_string = vim_strsave(name);
3094 else
3095 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003096 if (rettv->vval.v_string != NULL)
3097 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003098 }
3099 }
3100 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 }
3102
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003103 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003104 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02003105 if (tv == NULL)
3106 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003107 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003108 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02003109 ret = FAIL;
3110 }
3111 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003112 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003113 svar_T *sv = NULL;
3114 int was_assigned = FALSE;
3115
Bram Moolenaar11005b02021-07-11 20:59:00 +02003116 if (ht != NULL && ht == get_script_local_ht()
3117 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02003118 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003119 sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaarf055d452021-07-08 20:57:24 +02003120 if (sv != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003121 {
Bram Moolenaarf055d452021-07-08 20:57:24 +02003122 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003123 was_assigned = sv->sv_flags & SVFLAG_ASSIGNED;
3124 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003125 }
3126
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003127 if ((tv->v_type == VAR_TYPEALIAS || tv->v_type == VAR_CLASS)
3128 && sid != 0)
3129 {
3130 // type alias or class imported from another script. Check
3131 // whether it is exported from the other script.
3132 sv = find_typval_in_script(tv, sid, TRUE);
3133 if (sv == NULL)
3134 {
3135 ret = FAIL;
3136 goto done;
3137 }
3138 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
3139 {
3140 semsg(_(e_item_not_exported_in_script_str), name);
3141 ret = FAIL;
3142 goto done;
3143 }
3144 }
3145
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003146 // If a list or dict variable wasn't initialized and has meaningful
3147 // type, do it now. Not for global variables, they are not
3148 // declared.
Bram Moolenaar7a222242022-03-01 19:23:24 +00003149 if (ht != &globvarht)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003150 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003151 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003152 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003153 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003154 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003155 tv->vval.v_dict = dict_alloc();
3156 if (tv->vval.v_dict != NULL)
3157 {
3158 ++tv->vval.v_dict->dv_refcount;
3159 tv->vval.v_dict->dv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003160 if (sv != NULL)
3161 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003162 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003163 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003164 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003165 && ((type != NULL && !was_assigned)
Bram Moolenaar501f9782022-03-27 16:51:04 +01003166 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003167 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003168 tv->vval.v_list = list_alloc();
3169 if (tv->vval.v_list != NULL)
3170 {
3171 ++tv->vval.v_list->lv_refcount;
3172 tv->vval.v_list->lv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003173 if (sv != NULL)
3174 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003175 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003176 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003177 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003178 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003179 || !in_vim9script()))
3180 {
3181 tv->vval.v_blob = blob_alloc();
3182 if (tv->vval.v_blob != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003183 {
Bram Moolenaar859cc212022-03-28 15:22:35 +01003184 ++tv->vval.v_blob->bv_refcount;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003185 if (sv != NULL)
3186 sv->sv_flags |= SVFLAG_ASSIGNED;
3187 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003188 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02003189 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003190 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003191 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003192 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003193
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003194done:
Bram Moolenaar94674f22023-01-06 18:42:20 +00003195 if (len > 0)
3196 name[len] = cc;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003197
3198 return ret;
3199}
3200
3201/*
Bram Moolenaara86655a2023-01-12 17:06:27 +00003202 * Get the value of internal variable "name", also handling "import.name".
3203 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
3204 */
3205 int
3206eval_variable_import(
3207 char_u *name,
3208 typval_T *rettv)
3209{
3210 char_u *s = name;
3211 while (ASCII_ISALNUM(*s) || *s == '_')
3212 ++s;
3213 int len = (int)(s - name);
3214
3215 if (eval_variable(name, len, 0, rettv, NULL, EVAL_VAR_IMPORT) == FAIL)
3216 return FAIL;
3217 if (rettv->v_type == VAR_ANY && *s == '.')
3218 {
Bram Moolenaar40594002023-01-12 20:04:51 +00003219 char_u *ns = s + 1;
3220 s = ns;
3221 while (ASCII_ISALNUM(*s) || *s == '_')
3222 ++s;
Bram Moolenaara86655a2023-01-12 17:06:27 +00003223 int sid = rettv->vval.v_number;
Bram Moolenaar40594002023-01-12 20:04:51 +00003224 return eval_variable(ns, (int)(s - ns), sid, rettv, NULL, 0);
Bram Moolenaara86655a2023-01-12 17:06:27 +00003225 }
3226 return OK;
3227}
3228
3229
3230/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003231 * Check if variable "name[len]" is a local variable or an argument.
3232 * If so, "*eval_lavars_used" is set to TRUE.
3233 */
3234 void
3235check_vars(char_u *name, int len)
3236{
3237 int cc;
3238 char_u *varname;
3239 hashtab_T *ht;
3240
3241 if (eval_lavars_used == NULL)
3242 return;
3243
3244 // truncate the name, so that we can use strcmp()
3245 cc = name[len];
3246 name[len] = NUL;
3247
3248 ht = find_var_ht(name, &varname);
3249 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
3250 {
3251 if (find_var(name, NULL, TRUE) != NULL)
3252 *eval_lavars_used = TRUE;
3253 }
3254
3255 name[len] = cc;
3256}
3257
3258/*
3259 * Find variable "name" in the list of variables.
3260 * Return a pointer to it if found, NULL if not found.
3261 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003262 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003263 */
3264 dictitem_T *
3265find_var(char_u *name, hashtab_T **htp, int no_autoload)
3266{
3267 char_u *varname;
3268 hashtab_T *ht;
3269 dictitem_T *ret = NULL;
3270
3271 ht = find_var_ht(name, &varname);
3272 if (htp != NULL)
3273 *htp = ht;
3274 if (ht == NULL)
3275 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003276 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003277 if (ret != NULL)
3278 return ret;
3279
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003280 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003281 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003282 if (ret != NULL)
3283 return ret;
3284
3285 // in Vim9 script items without a scope can be script-local
3286 if (in_vim9script() && name[0] != NUL && name[1] != ':')
3287 {
3288 ht = get_script_local_ht();
3289 if (ht != NULL)
3290 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003291 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003292 if (ret != NULL)
3293 {
3294 if (htp != NULL)
3295 *htp = ht;
3296 return ret;
3297 }
3298 }
3299 }
3300
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003301 // When using "vim9script autoload" script-local items are prefixed but can
3302 // be used with s:name.
3303 if (SCRIPT_ID_VALID(current_sctx.sc_sid)
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003304 && (in_vim9script() || (name[0] == 's' && name[1] == ':')))
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003305 {
3306 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
3307
3308 if (si->sn_autoload_prefix != NULL)
3309 {
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003310 char_u *base_name = (name[0] == 's' && name[1] == ':')
3311 ? name + 2 : name;
3312 char_u *auto_name = concat_str(si->sn_autoload_prefix, base_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003313
3314 if (auto_name != NULL)
3315 {
3316 ht = &globvarht;
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003317 ret = find_var_in_ht(ht, 'g', auto_name, TRUE);
Bram Moolenaar130f65d2022-01-13 20:39:41 +00003318 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003319 if (ret != NULL)
3320 {
3321 if (htp != NULL)
3322 *htp = ht;
3323 return ret;
3324 }
3325 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003326 }
3327 }
3328
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003329 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003330}
3331
3332/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00003333 * Like find_var() but if the name starts with <SNR>99_ then look in the
3334 * referenced script (used for a funcref).
3335 */
3336 dictitem_T *
3337find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
3338{
Keith Thompson184f71c2024-01-04 21:19:04 +01003339 if (STRNCMP(name, "<SNR>", 5) == 0 && SAFE_isdigit(name[5]))
Bram Moolenaar71f21932022-01-07 18:20:55 +00003340 {
3341 char_u *p = name + 5;
3342 int sid = getdigits(&p);
3343
3344 if (SCRIPT_ID_VALID(sid) && *p == '_')
3345 {
3346 hashtab_T *ht = &SCRIPT_VARS(sid);
3347
3348 if (ht != NULL)
3349 {
3350 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
3351
3352 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003353 {
3354 if (htp != NULL)
3355 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00003356 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003357 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00003358 }
3359 }
3360 }
3361
3362 return find_var(name, htp, no_autoload);
3363}
3364
3365/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003366 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02003367 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003368 * Returns NULL if not found.
3369 */
3370 dictitem_T *
3371find_var_in_ht(
3372 hashtab_T *ht,
3373 int htname,
3374 char_u *varname,
3375 int no_autoload)
3376{
3377 hashitem_T *hi;
3378
3379 if (*varname == NUL)
3380 {
3381 // Must be something like "s:", otherwise "ht" would be NULL.
3382 switch (htname)
3383 {
3384 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
3385 case 'g': return &globvars_var;
3386 case 'v': return &vimvars_var;
3387 case 'b': return &curbuf->b_bufvar;
3388 case 'w': return &curwin->w_winvar;
3389 case 't': return &curtab->tp_winvar;
3390 case 'l': return get_funccal_local_var();
3391 case 'a': return get_funccal_args_var();
3392 }
3393 return NULL;
3394 }
3395
3396 hi = hash_find(ht, varname);
3397 if (HASHITEM_EMPTY(hi))
3398 {
3399 // For global variables we may try auto-loading the script. If it
3400 // worked find the variable again. Don't auto-load a script if it was
3401 // loaded already, otherwise it would be loaded every time when
3402 // checking if a function name is a Funcref variable.
3403 if (ht == &globvarht && !no_autoload)
3404 {
3405 // Note: script_autoload() may make "hi" invalid. It must either
3406 // be obtained again or not used.
3407 if (!script_autoload(varname, FALSE) || aborting())
3408 return NULL;
3409 hi = hash_find(ht, varname);
3410 }
3411 if (HASHITEM_EMPTY(hi))
3412 return NULL;
3413 }
3414 return HI2DI(hi);
3415}
3416
3417/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 * Get the script-local hashtab. NULL if not in a script context.
3419 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003420 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421get_script_local_ht(void)
3422{
3423 scid_T sid = current_sctx.sc_sid;
3424
Bram Moolenaare3d46852020-08-29 13:39:17 +02003425 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 return &SCRIPT_VARS(sid);
3427 return NULL;
3428}
3429
3430/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003431 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003432 * When "cmd" is TRUE it must look like a command, a function must be followed
3433 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003434 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003436 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003437lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003438 char_u *name,
3439 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003440 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003441 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442{
3443 hashtab_T *ht = get_script_local_ht();
3444 char_u buffer[30];
3445 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003446 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003448 int is_global = FALSE;
3449 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450
3451 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003452 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 if (len < sizeof(buffer) - 1)
3454 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003455 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 vim_strncpy(buffer, name, len);
3457 p = buffer;
3458 }
3459 else
3460 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003461 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003463 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464 }
3465
3466 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003467 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468
3469 // if not script-local, then perhaps imported
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003470 if (res == FAIL && find_imported(p, 0, FALSE) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003471 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003472 if (p != buffer)
3473 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003474
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003475 // Find a function, so that a following "->" works.
3476 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3477 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003478 if (res != OK)
3479 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003480 p = skipwhite(name + len);
3481
3482 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003483 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003484 // Do not check for an internal function, since it might also be a
3485 // valid command, such as ":split" versus "split()".
3486 // Skip "g:" before a function name.
3487 if (name[0] == 'g' && name[1] == ':')
3488 {
3489 is_global = TRUE;
3490 fname = name + 2;
3491 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003492 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003493 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003494 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003495 }
3496
Bram Moolenaar709664c2020-12-12 14:33:41 +01003497 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498}
3499
3500/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003501 * Find the hashtab used for a variable name.
3502 * Return NULL if the name is not valid.
3503 * Set "varname" to the start of name without ':'.
3504 */
3505 hashtab_T *
3506find_var_ht(char_u *name, char_u **varname)
3507{
3508 hashitem_T *hi;
3509 hashtab_T *ht;
3510
3511 if (name[0] == NUL)
3512 return NULL;
3513 if (name[1] != ':')
3514 {
3515 // The name must not start with a colon or #.
3516 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3517 return NULL;
3518 *varname = name;
3519
3520 // "version" is "v:version" in all scopes if scriptversion < 3.
3521 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003522 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003523 {
3524 hi = hash_find(&compat_hashtab, name);
3525 if (!HASHITEM_EMPTY(hi))
3526 return &compat_hashtab;
3527 }
3528
3529 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530 if (ht != NULL)
3531 return ht; // local variable
3532
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003533 // In Vim9 script items at the script level are script-local, except
3534 // for autoload names.
3535 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536 {
3537 ht = get_script_local_ht();
3538 if (ht != NULL)
3539 return ht;
3540 }
3541
3542 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003543 }
3544 *varname = name + 2;
3545 if (*name == 'g') // global variable
3546 return &globvarht;
3547 // There must be no ':' or '#' in the rest of the name, unless g: is used
3548 if (vim_strchr(name + 2, ':') != NULL
3549 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3550 return NULL;
3551 if (*name == 'b') // buffer variable
3552 return &curbuf->b_vars->dv_hashtab;
3553 if (*name == 'w') // window variable
3554 return &curwin->w_vars->dv_hashtab;
3555 if (*name == 't') // tab page variable
3556 return &curtab->tp_vars->dv_hashtab;
3557 if (*name == 'v') // v: variable
3558 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003559 if (get_current_funccal() != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01003560 && get_current_funccal()->fc_func->uf_def_status
3561 == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003563 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 if (*name == 'a') // a: function argument
3565 return get_funccal_args_ht();
3566 if (*name == 'l') // l: local function variable
3567 return get_funccal_local_ht();
3568 }
3569 if (*name == 's') // script variable
3570 {
3571 ht = get_script_local_ht();
3572 if (ht != NULL)
3573 return ht;
3574 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003575 return NULL;
3576}
3577
3578/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003579 * Get the string value of a (global/local) variable.
3580 * Note: see tv_get_string() for how long the pointer remains valid.
3581 * Returns NULL when it doesn't exist.
3582 */
3583 char_u *
3584get_var_value(char_u *name)
3585{
3586 dictitem_T *v;
3587
3588 v = find_var(name, NULL, FALSE);
3589 if (v == NULL)
3590 return NULL;
3591 return tv_get_string(&v->di_tv);
3592}
3593
3594/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003595 * Allocate a new hashtab for a sourced script. It will be used while
3596 * sourcing this script and when executing functions defined in the script.
3597 */
3598 void
3599new_script_vars(scid_T id)
3600{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003601 scriptvar_T *sv;
3602
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003603 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3604 if (sv == NULL)
3605 return;
3606 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003607 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003608}
3609
3610/*
3611 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3612 * point to it.
3613 */
3614 void
3615init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3616{
3617 hash_init(&dict->dv_hashtab);
3618 dict->dv_lock = 0;
3619 dict->dv_scope = scope;
3620 dict->dv_refcount = DO_NOT_FREE_CNT;
3621 dict->dv_copyID = 0;
3622 dict_var->di_tv.vval.v_dict = dict;
3623 dict_var->di_tv.v_type = VAR_DICT;
3624 dict_var->di_tv.v_lock = VAR_FIXED;
3625 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3626 dict_var->di_key[0] = NUL;
3627}
3628
3629/*
3630 * Unreference a dictionary initialized by init_var_dict().
3631 */
3632 void
3633unref_var_dict(dict_T *dict)
3634{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003635 // Now the dict needs to be freed if no one else is using it, go back to
3636 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003637 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3638 dict_unref(dict);
3639}
3640
3641/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003642 * Clean up a list of internal variables.
3643 * Frees all allocated variables and the value they contain.
3644 * Clears hashtab "ht", does not free it.
3645 */
3646 void
3647vars_clear(hashtab_T *ht)
3648{
3649 vars_clear_ext(ht, TRUE);
3650}
3651
3652/*
3653 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3654 */
3655 void
3656vars_clear_ext(hashtab_T *ht, int free_val)
3657{
3658 int todo;
3659 hashitem_T *hi;
3660 dictitem_T *v;
3661
3662 hash_lock(ht);
3663 todo = (int)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003664 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003665 {
3666 if (!HASHITEM_EMPTY(hi))
3667 {
3668 --todo;
3669
3670 // Free the variable. Don't remove it from the hashtab,
3671 // ht_array might change then. hash_clear() takes care of it
3672 // later.
3673 v = HI2DI(hi);
3674 if (free_val)
3675 clear_tv(&v->di_tv);
3676 if (v->di_flags & DI_FLAGS_ALLOC)
3677 vim_free(v);
3678 }
3679 }
3680 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003681 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003682}
3683
3684/*
3685 * Delete a variable from hashtab "ht" at item "hi".
3686 * Clear the variable value and free the dictitem.
3687 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003688 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003689delete_var(hashtab_T *ht, hashitem_T *hi)
3690{
3691 dictitem_T *di = HI2DI(hi);
3692
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003693 if (hash_remove(ht, hi, "delete variable") != OK)
3694 return;
3695
3696 clear_tv(&di->di_tv);
3697 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003698}
3699
3700/*
3701 * List the value of one internal variable.
3702 */
3703 static void
3704list_one_var(dictitem_T *v, char *prefix, int *first)
3705{
3706 char_u *tofree;
3707 char_u *s;
3708 char_u numbuf[NUMBUFLEN];
3709
3710 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3711 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3712 s == NULL ? (char_u *)"" : s, first);
3713 vim_free(tofree);
3714}
3715
3716 static void
3717list_one_var_a(
3718 char *prefix,
3719 char_u *name,
3720 int type,
3721 char_u *string,
3722 int *first) // when TRUE clear rest of screen and set to FALSE
3723{
3724 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3725 msg_start();
3726 msg_puts(prefix);
3727 if (name != NULL) // "a:" vars don't have a name stored
3728 msg_puts((char *)name);
3729 msg_putchar(' ');
3730 msg_advance(22);
3731 if (type == VAR_NUMBER)
3732 msg_putchar('#');
3733 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3734 msg_putchar('*');
3735 else if (type == VAR_LIST)
3736 {
3737 msg_putchar('[');
3738 if (*string == '[')
3739 ++string;
3740 }
3741 else if (type == VAR_DICT)
3742 {
3743 msg_putchar('{');
3744 if (*string == '{')
3745 ++string;
3746 }
3747 else
3748 msg_putchar(' ');
3749
3750 msg_outtrans(string);
3751
3752 if (type == VAR_FUNC || type == VAR_PARTIAL)
3753 msg_puts("()");
3754 if (*first)
3755 {
3756 msg_clr_eos();
3757 *first = FALSE;
3758 }
3759}
3760
3761/*
zeertzjqedcba962023-09-24 23:13:51 +02003762 * Addition handling for setting a v: variable.
3763 * Return TRUE if the variable should be set normally,
3764 * FALSE if nothing else needs to be done.
3765 */
3766 int
3767before_set_vvar(
3768 char_u *varname,
3769 dictitem_T *di,
3770 typval_T *tv,
3771 int copy,
3772 int *type_error)
3773{
3774 if (di->di_tv.v_type == VAR_STRING)
3775 {
3776 VIM_CLEAR(di->di_tv.vval.v_string);
3777 if (copy || tv->v_type != VAR_STRING)
3778 {
3779 char_u *val = tv_get_string(tv);
3780
3781 // Careful: when assigning to v:errmsg and
3782 // tv_get_string() causes an error message the variable
3783 // will already be set.
3784 if (di->di_tv.vval.v_string == NULL)
3785 di->di_tv.vval.v_string = vim_strsave(val);
3786 }
3787 else
3788 {
3789 // Take over the string to avoid an extra alloc/free.
3790 di->di_tv.vval.v_string = tv->vval.v_string;
3791 tv->vval.v_string = NULL;
3792 }
3793 return FALSE;
3794 }
3795 else if (di->di_tv.v_type == VAR_NUMBER)
3796 {
3797 di->di_tv.vval.v_number = tv_get_number(tv);
3798 if (STRCMP(varname, "searchforward") == 0)
3799 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
3800#ifdef FEAT_SEARCH_EXTRA
3801 else if (STRCMP(varname, "hlsearch") == 0)
3802 {
3803 no_hlsearch = !di->di_tv.vval.v_number;
3804 redraw_all_later(UPD_SOME_VALID);
3805 }
3806#endif
3807 return FALSE;
3808 }
3809 else if (di->di_tv.v_type != tv->v_type)
3810 {
3811 *type_error = TRUE;
3812 return FALSE;
3813 }
3814 return TRUE;
3815}
3816
3817/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003818 * Set variable "name" to value in "tv".
3819 * If the variable already exists, the value is updated.
3820 * Otherwise the variable is created.
3821 */
3822 void
3823set_var(
3824 char_u *name,
3825 typval_T *tv,
3826 int copy) // make copy of value in "tv"
3827{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003828 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003829}
3830
3831/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003832 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003833 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003834 * If the variable already exists and "is_const" is FALSE the value is updated.
3835 * Otherwise the variable is created.
3836 */
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01003837 int
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003838set_var_const(
3839 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003840 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003841 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003842 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003843 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003844 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003845 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003846{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003847 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003848 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003849 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 dictitem_T *di;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003851 typval_T *dest_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003852 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003853 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003854 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003856 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003857 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003858 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003859 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003860 int free_tv_arg = !copy; // free tv_arg if not used
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01003861 int rc = FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003862
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003863 if (sid != 0)
3864 {
3865 if (SCRIPT_ID_VALID(sid))
3866 ht = &SCRIPT_VARS(sid);
3867 varname = name;
3868 }
3869 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003870 {
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003871 scriptitem_T *si;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003872
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003873 if (in_vim9script() && is_export
3874 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3875 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
Bram Moolenaar0e1574c2022-03-03 17:05:35 +00003876 ->sn_autoload_prefix != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003877 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003878 // In a vim9 autoload script an exported variable is put in the
3879 // global namespace with the autoload prefix.
3880 var_in_autoload = TRUE;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003881 varname = concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003882 if (varname == NULL)
3883 goto failed;
3884 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003885 ht = &globvarht;
3886 }
3887 else
3888 ht = find_var_ht(name, &varname);
3889 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003890 if (ht == NULL || *varname == NUL)
3891 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003892 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003893 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003894 }
Bram Moolenaar54969f42022-02-07 13:56:44 +00003895 is_script_local = ht == get_script_local_ht() || sid != 0
3896 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003897
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003898 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003899 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003900 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003901 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003902 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003903 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003904 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003905 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003906 }
Bram Moolenaar9510d222022-09-11 15:14:05 +01003907 if ((flags & ASSIGN_FOR_LOOP) && is_scoped_variable(name))
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003908 // Do not make g:var, w:var, b:var or t:var final.
3909 flags &= ~ASSIGN_FINAL;
3910
Bram Moolenaare535db82021-03-31 21:07:24 +02003911 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003912 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3913 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003914 // For "[a, _] = list" the underscore is ignored.
3915 if ((flags & ASSIGN_UNPACK) == 0)
3916 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003917 goto failed;
3918 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003919
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003921
Bram Moolenaar24e93162021-07-18 20:40:33 +02003922 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003923 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003924 imported_T *import = find_imported(varname, 0, FALSE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003925
Bram Moolenaar24e93162021-07-18 20:40:33 +02003926 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003927 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003928 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02003929 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003931 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003932 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003934 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3935 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003936 }
Bram Moolenaar75e27d72022-02-13 13:56:29 +00003937 if (!in_vim9script())
3938 {
3939 semsg(_(e_cannot_create_vim9_script_variable_in_function_str),
3940 name);
3941 goto failed;
3942 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003943 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944
Bram Moolenaar993faa32022-02-21 15:59:11 +00003945 // Search in parent scope which is possible to reference from lambda
3946 if (di == NULL)
3947 di = find_var_in_scoped_ht(name, TRUE);
3948
3949 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
3950 && var_wrong_func_name(name, di == NULL))
3951 goto failed;
3952
3953 if (need_convert_to_bool(type, tv))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003954 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003955 // Destination is a bool and the value is not, but it can be
3956 // converted.
3957 CLEAR_FIELD(bool_tv);
3958 bool_tv.v_type = VAR_BOOL;
3959 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3960 tv = &bool_tv;
3961 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003962
Bram Moolenaar993faa32022-02-21 15:59:11 +00003963 if (di != NULL)
3964 {
3965 // Item already exists. Allowed to replace when reloading.
3966 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003967 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003968 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
3969 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003970 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003971 emsg(_(e_cannot_modify_existing_variable));
3972 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003973 }
3974
Bram Moolenaar993faa32022-02-21 15:59:11 +00003975 if (is_script_local && vim9script
3976 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003977 {
3978 semsg(_(e_redefining_script_item_str), name);
3979 goto failed;
3980 }
3981
Ernie Raele75fde62023-12-21 17:18:54 +01003982 if (check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003983 goto failed;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003984
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01003985 // List and Blob types can be modified in-place using the "+="
3986 // compound operator. For other types, this is not allowed.
3987 int type_inplace_modifiable =
3988 (di->di_tv.v_type == VAR_LIST || di->di_tv.v_type == VAR_BLOB);
3989
3990 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0
3991 && ((flags & ASSIGN_COMPOUND_OP) == 0
3992 || !type_inplace_modifiable))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003993 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003994 where_T where = WHERE_INIT;
Bram Moolenaar7a411a32022-04-04 14:58:06 +01003995 svar_T *sv = find_typval_in_script(&di->di_tv, sid, TRUE);
Bram Moolenaar993faa32022-02-21 15:59:11 +00003996
3997 if (sv != NULL)
3998 {
3999 // check the type and adjust to bool if needed
LemonBoyc5d27442023-08-19 13:02:35 +02004000 if (var_idx > 0)
4001 {
4002 where.wt_index = var_idx;
4003 where.wt_kind = WT_VARIABLE;
4004 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00004005 if (check_script_var_type(sv, tv, name, where) == FAIL)
4006 goto failed;
4007 if (type == NULL)
4008 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01004009 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004010 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02004011 }
4012
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01004013 // Modifying a final variable with a List value using the "+="
4014 // operator is allowed. For other types, it is not allowed.
4015 if (((flags & ASSIGN_FOR_LOOP) == 0
4016 && ((flags & ASSIGN_COMPOUND_OP) == 0
4017 || !type_inplace_modifiable))
Bram Moolenaar16d2c022023-06-05 19:46:18 +01004018 ? var_check_permission(di, name) == FAIL
4019 : var_check_ro(di->di_flags, name, FALSE))
Bram Moolenaar24e93162021-07-18 20:40:33 +02004020 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004021 }
4022 else
4023 {
4024 // can only redefine once
4025 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar24e93162021-07-18 20:40:33 +02004026
Bram Moolenaar993faa32022-02-21 15:59:11 +00004027 // A Vim9 script-local variable is also present in sn_all_vars
4028 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004029 if (var_in_vim9script || var_in_autoload)
Bram Moolenaar993faa32022-02-21 15:59:11 +00004030 update_vim9_script_var(FALSE, di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004031 var_in_autoload ? name : di->di_key, flags,
Bram Moolenaar993faa32022-02-21 15:59:11 +00004032 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01004033 }
4034
Bram Moolenaar993faa32022-02-21 15:59:11 +00004035 // existing variable, need to clear the value
4036
zeertzjqedcba962023-09-24 23:13:51 +02004037 // Handle setting internal v: variables separately where needed to
Bram Moolenaar993faa32022-02-21 15:59:11 +00004038 // prevent changing the type.
zeertzjqedcba962023-09-24 23:13:51 +02004039 int type_error = FALSE;
4040 if (ht == &vimvarht
4041 && !before_set_vvar(varname, di, tv, copy, &type_error))
Bram Moolenaar993faa32022-02-21 15:59:11 +00004042 {
zeertzjqedcba962023-09-24 23:13:51 +02004043 if (type_error)
4044 semsg(_(e_setting_v_str_to_value_with_wrong_type), varname);
4045 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004046 }
4047
4048 clear_tv(&di->di_tv);
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01004049
4050 if ((flags & ASSIGN_UPDATE_BLOCK_ID)
4051 && SCRIPT_ID_VALID(current_sctx.sc_sid))
4052 {
4053 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
4054
4055 update_script_var_block_id(name, si->sn_current_block_id);
4056 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00004057 }
4058 else
4059 {
4060 // Item not found, check if a function already exists.
4061 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
4062 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
4063 {
4064 semsg(_(e_redefining_script_item_str), name);
4065 goto failed;
4066 }
4067
4068 // add a new variable
4069 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
4070 {
4071 semsg(_(e_unknown_variable_str), name);
4072 goto failed;
4073 }
4074
Bram Moolenaaref2c3252022-11-25 16:31:51 +00004075 if (check_hashtab_frozen(ht, "add variable"))
4076 goto failed;
4077
Bram Moolenaar993faa32022-02-21 15:59:11 +00004078 // Can't add "v:" or "a:" variable.
4079 if (ht == &vimvarht || ht == get_funccal_args_ht())
4080 {
4081 semsg(_(e_illegal_variable_name_str), name);
4082 goto failed;
4083 }
4084
4085 // Make sure the variable name is valid. In Vim9 script an
4086 // autoload variable must be prefixed with "g:" unless in an
4087 // autoload script.
4088 if (!valid_varname(varname, -1, !vim9script
4089 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
4090 goto failed;
4091
zeertzjq1b438a82023-02-01 13:11:15 +00004092 di = alloc(offsetof(dictitem_T, di_key) + STRLEN(varname) + 1);
Bram Moolenaar993faa32022-02-21 15:59:11 +00004093 if (di == NULL)
4094 goto failed;
4095 STRCPY(di->di_key, varname);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00004096 if (hash_add(ht, DI2HIKEY(di), "add variable") == FAIL)
Bram Moolenaar993faa32022-02-21 15:59:11 +00004097 {
4098 vim_free(di);
4099 goto failed;
4100 }
4101 di->di_flags = DI_FLAGS_ALLOC;
4102 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
4103 di->di_flags |= DI_FLAGS_LOCK;
4104
4105 // A Vim9 script-local variable is also added to sn_all_vars and
4106 // sn_var_vals. It may set "type" from "tv".
4107 if (var_in_vim9script || var_in_autoload)
4108 update_vim9_script_var(TRUE, di,
4109 var_in_autoload ? name : di->di_key, flags,
4110 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004111 }
4112
Bram Moolenaar993faa32022-02-21 15:59:11 +00004113 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004114 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02004115 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004116 else
4117 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02004118 *dest_tv = *tv;
4119 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004120 init_tv(tv);
4121 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004122 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004123
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004124 if (vim9script && type != NULL)
Bram Moolenaar381692b2022-02-02 20:01:27 +00004125 set_tv_type(dest_tv, type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004126
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01004127 // ":const var = value" locks the value
4128 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004129 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02004130 // Like :lockvar! name: lock the value and what it contains, but only
4131 // if the reference count is up to one. That locks only literal
4132 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02004133 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02004134
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01004135 rc = OK;
4136
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004137failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004138 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004139 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004140 clear_tv(tv_arg);
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01004141
4142 return rc;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004143}
4144
4145/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004146 * Check in this order for backwards compatibility:
4147 * - Whether the variable is read-only
4148 * - Whether the variable value is locked
4149 * - Whether the variable is locked
4150 */
4151 int
4152var_check_permission(dictitem_T *di, char_u *name)
4153{
4154 if (var_check_ro(di->di_flags, name, FALSE)
4155 || value_check_lock(di->di_tv.v_lock, name, FALSE)
4156 || var_check_lock(di->di_flags, name, FALSE))
4157 return FAIL;
4158 return OK;
4159}
4160
4161/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004162 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
4163 * Also give an error message.
4164 */
4165 int
4166var_check_ro(int flags, char_u *name, int use_gettext)
4167{
4168 if (flags & DI_FLAGS_RO)
4169 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004170 if (name == NULL)
4171 emsg(_(e_cannot_change_readonly_variable));
4172 else
4173 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02004174 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004175 return TRUE;
4176 }
4177 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
4178 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004179 if (name == NULL)
4180 emsg(_(e_cannot_set_variable_in_sandbox));
4181 else
4182 semsg(_(e_cannot_set_variable_in_sandbox_str),
4183 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004184 return TRUE;
4185 }
4186 return FALSE;
4187}
4188
4189/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004190 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
4191 * Also give an error message.
4192 */
4193 int
4194var_check_lock(int flags, char_u *name, int use_gettext)
4195{
4196 if (flags & DI_FLAGS_LOCK)
4197 {
4198 semsg(_(e_variable_is_locked_str),
4199 use_gettext ? (char_u *)_(name) : name);
4200 return TRUE;
4201 }
4202 return FALSE;
4203}
4204
4205/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004206 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
4207 * Also give an error message.
4208 */
4209 int
4210var_check_fixed(int flags, char_u *name, int use_gettext)
4211{
4212 if (flags & DI_FLAGS_FIX)
4213 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004214 if (name == NULL)
4215 emsg(_(e_cannot_delete_variable));
4216 else
4217 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004218 use_gettext ? (char_u *)_(name) : name);
4219 return TRUE;
4220 }
4221 return FALSE;
4222}
4223
4224/*
4225 * Check if a funcref is assigned to a valid variable name.
4226 * Return TRUE and give an error if not.
4227 */
4228 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004229var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004230 char_u *name, // points to start of variable name
4231 int new_var) // TRUE when creating the variable
4232{
Bram Moolenaar32154662021-03-28 21:14:06 +02004233 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
4234 // the name can be used without the s: prefix.
thinca6c667bd2022-09-02 11:25:37 +01004235 // Allow autoload variable.
Bram Moolenaar32154662021-03-28 21:14:06 +02004236 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
4237 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004238 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
thinca6c667bd2022-09-02 11:25:37 +01004239 ? name[2] : name[0])
4240 && vim_strchr(name, '#') == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004241 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004242 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004243 return TRUE;
4244 }
4245 // Don't allow hiding a function. When "v" is not NULL we might be
4246 // assigning another function to the same var, the type is checked
4247 // below.
4248 if (new_var && function_exists(name, FALSE))
4249 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004250 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004251 name);
4252 return TRUE;
4253 }
4254 return FALSE;
4255}
4256
4257/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004258 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
4259 * value. Also give an error message, using "name" or _("name") when
4260 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004261 */
4262 int
Bram Moolenaara187c432020-09-16 21:08:28 +02004263value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004264{
4265 if (lock & VAR_LOCKED)
4266 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004267 if (name == NULL)
4268 emsg(_(e_value_is_locked));
4269 else
4270 semsg(_(e_value_is_locked_str),
4271 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004272 return TRUE;
4273 }
4274 if (lock & VAR_FIXED)
4275 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004276 if (name == NULL)
4277 emsg(_(e_cannot_change_value));
4278 else
4279 semsg(_(e_cannot_change_value_of_str),
4280 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004281 return TRUE;
4282 }
4283 return FALSE;
4284}
4285
4286/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01004287 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004288 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004289 * Return FALSE and give an error if not.
4290 */
4291 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004292valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004293{
4294 char_u *p;
4295
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004296 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004297 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01004298 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004299 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004300 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004301 return FALSE;
4302 }
4303 return TRUE;
4304}
4305
4306/*
LemonBoy47d4e312022-05-04 18:12:55 +01004307 * Implements the logic to retrieve local variable and option values.
4308 * Used by "getwinvar()" "gettabvar()" "gettabwinvar()" "getbufvar()".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004309 */
4310 static void
LemonBoy47d4e312022-05-04 18:12:55 +01004311get_var_from(
4312 char_u *varname,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004313 typval_T *rettv,
LemonBoy47d4e312022-05-04 18:12:55 +01004314 typval_T *deftv, // Default value if not found.
4315 int htname, // 't'ab, 'w'indow or 'b'uffer local.
4316 tabpage_T *tp, // can be NULL
4317 win_T *win,
4318 buf_T *buf) // Ignored if htname is not 'b'.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004319{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004320 dictitem_T *v;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004321 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004322 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004323 int need_switch_win;
zeertzjqcd6ad642022-07-25 12:28:09 +01004324 int do_change_curbuf = buf != NULL && htname == 'b';
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004325
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004326 ++emsg_off;
4327
4328 rettv->v_type = VAR_STRING;
4329 rettv->vval.v_string = NULL;
4330
LemonBoy47d4e312022-05-04 18:12:55 +01004331 if (varname != NULL && tp != NULL && win != NULL
4332 && (htname != 'b' || buf != NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004333 {
4334 // Set curwin to be our win, temporarily. Also set the tabpage,
4335 // otherwise the window is not valid. Only do this when needed,
4336 // autocommands get blocked.
LemonBoy47d4e312022-05-04 18:12:55 +01004337 // If we have a buffer reference avoid the switching, we're saving and
4338 // restoring curbuf directly.
zeertzjqcd6ad642022-07-25 12:28:09 +01004339 need_switch_win = !(tp == curtab && win == curwin) && !do_change_curbuf;
LemonBoy47d4e312022-05-04 18:12:55 +01004340 if (!need_switch_win || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004341 {
LemonBoy47d4e312022-05-04 18:12:55 +01004342 // Handle options. There are no tab-local options.
4343 if (*varname == '&' && htname != 't')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004344 {
LemonBoy47d4e312022-05-04 18:12:55 +01004345 buf_T *save_curbuf = curbuf;
4346
4347 // Change curbuf so the option is read from the correct buffer.
zeertzjqcd6ad642022-07-25 12:28:09 +01004348 if (do_change_curbuf)
LemonBoy47d4e312022-05-04 18:12:55 +01004349 curbuf = buf;
4350
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004351 if (varname[1] == NUL)
4352 {
zeertzjqcd6ad642022-07-25 12:28:09 +01004353 // get all window-local or buffer-local options in a dict
LemonBoy47d4e312022-05-04 18:12:55 +01004354 dict_T *opts = get_winbuf_options(htname == 'b');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004355
4356 if (opts != NULL)
4357 {
4358 rettv_dict_set(rettv, opts);
4359 done = TRUE;
4360 }
4361 }
LemonBoy47d4e312022-05-04 18:12:55 +01004362 else if (eval_option(&varname, rettv, TRUE) == OK)
4363 // Local option
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004364 done = TRUE;
LemonBoy47d4e312022-05-04 18:12:55 +01004365
4366 curbuf = save_curbuf;
4367 }
4368 else if (*varname == NUL)
4369 {
4370 // Empty string: return a dict with all the local variables.
4371 if (htname == 'b')
4372 v = &buf->b_bufvar;
4373 else if (htname == 'w')
4374 v = &win->w_winvar;
4375 else
4376 v = &tp->tp_winvar;
4377 copy_tv(&v->di_tv, rettv);
4378 done = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004379 }
4380 else
4381 {
LemonBoy47d4e312022-05-04 18:12:55 +01004382 hashtab_T *ht;
4383
4384 if (htname == 'b')
4385 ht = &buf->b_vars->dv_hashtab;
4386 else if (htname == 'w')
4387 ht = &win->w_vars->dv_hashtab;
4388 else
4389 ht = &tp->tp_vars->dv_hashtab;
4390
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004391 // Look up the variable.
LemonBoy47d4e312022-05-04 18:12:55 +01004392 v = find_var_in_ht(ht, htname, varname, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004393 if (v != NULL)
4394 {
4395 copy_tv(&v->di_tv, rettv);
4396 done = TRUE;
4397 }
4398 }
4399 }
4400
4401 if (need_switch_win)
4402 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004403 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004404 }
4405
LemonBoy47d4e312022-05-04 18:12:55 +01004406 if (!done && deftv->v_type != VAR_UNKNOWN)
4407 // use the default value
4408 copy_tv(deftv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004409
4410 --emsg_off;
4411}
4412
4413/*
LemonBoy47d4e312022-05-04 18:12:55 +01004414 * getwinvar() and gettabwinvar()
4415 */
4416 static void
4417getwinvar(
4418 typval_T *argvars,
4419 typval_T *rettv,
4420 int off) // 1 for gettabwinvar()
4421{
4422 char_u *varname;
4423 tabpage_T *tp;
4424 win_T *win;
4425
4426 if (off == 1)
4427 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4428 else
4429 tp = curtab;
4430 win = find_win_by_nr(&argvars[off], tp);
4431 varname = tv_get_string_chk(&argvars[off + 1]);
4432
4433 get_var_from(varname, rettv, &argvars[off + 2], 'w', tp, win, NULL);
4434}
4435
4436/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02004437 * Set option "varname" to the value of "varp" for the current buffer/window.
4438 */
4439 static void
4440set_option_from_tv(char_u *varname, typval_T *varp)
4441{
4442 long numval = 0;
4443 char_u *strval;
4444 char_u nbuf[NUMBUFLEN];
4445 int error = FALSE;
4446
zeertzjq4c7cb372023-06-14 16:39:54 +01004447 int opt_idx = findoption(varname);
4448 if (opt_idx < 0)
4449 {
4450 semsg(_(e_unknown_option_str_2), varname);
4451 return;
4452 }
4453 int opt_p_flags = get_option_flags(opt_idx);
4454
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004455 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004456 {
zeertzjq4c7cb372023-06-14 16:39:54 +01004457 if (opt_p_flags & P_STRING)
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004458 {
4459 emsg(_(e_string_required));
4460 return;
4461 }
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004462 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004463 strval = (char_u *)"0"; // avoid using "false"
4464 }
4465 else
4466 {
zeertzjq4c7cb372023-06-14 16:39:54 +01004467 if ((opt_p_flags & (P_NUM|P_BOOL))
4468 && (!in_vim9script() || varp->v_type != VAR_STRING))
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004469 numval = (long)tv_get_number_chk(varp, &error);
zeertzjq4c7cb372023-06-14 16:39:54 +01004470 if (!error)
4471 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004472 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02004473 if (!error && strval != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004474 set_option_value_give_err(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar191929b2020-08-19 21:20:49 +02004475}
4476
4477/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004478 * "setwinvar()" and "settabwinvar()" functions
4479 */
4480 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004481setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004482{
4483 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004484 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004485 int need_switch_win;
4486 char_u *varname, *winvarname;
4487 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004488 tabpage_T *tp = NULL;
4489
4490 if (check_secure())
4491 return;
4492
4493 if (off == 1)
4494 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4495 else
4496 tp = curtab;
4497 win = find_win_by_nr(&argvars[off], tp);
4498 varname = tv_get_string_chk(&argvars[off + 1]);
4499 varp = &argvars[off + 2];
4500
zeertzjqea720ae2023-01-03 10:54:09 +00004501 if (win == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004502 return;
4503
4504 need_switch_win = !(tp == curtab && win == curwin);
4505 if (!need_switch_win
4506 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004507 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004508 if (*varname == '&')
4509 set_option_from_tv(varname + 1, varp);
4510 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004511 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004512 winvarname = alloc(STRLEN(varname) + 3);
4513 if (winvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004514 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004515 STRCPY(winvarname, "w:");
4516 STRCPY(winvarname + 2, varname);
4517 set_var(winvarname, varp, TRUE);
4518 vim_free(winvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004519 }
4520 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004521 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004522 if (need_switch_win)
4523 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004524}
4525
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004526/*
4527 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4528 * v:option_type, and v:option_command.
4529 */
4530 void
4531reset_v_option_vars(void)
4532{
4533 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4534 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4535 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4536 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4537 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4538 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4539}
4540
4541/*
4542 * Add an assert error to v:errors.
4543 */
4544 void
4545assert_error(garray_T *gap)
4546{
4547 struct vimvar *vp = &vimvars[VV_ERRORS];
4548
Bram Moolenaard787e402021-12-24 21:36:12 +00004549 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004550 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004551 set_vim_var_list(VV_ERRORS, list_alloc());
4552 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4553}
4554
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004555 int
4556var_exists(char_u *var)
4557{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004558 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004559 char_u *name;
4560 char_u *tofree;
4561 typval_T tv;
4562 int len = 0;
4563 int n = FALSE;
4564
4565 // get_name_len() takes care of expanding curly braces
4566 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004567 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004568 if (len > 0)
4569 {
4570 if (tofree != NULL)
4571 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004572 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004573 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004574 if (n)
4575 {
4576 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004577 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004578 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4579 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004580 if (n)
4581 clear_tv(&tv);
4582 }
4583 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004584 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004585 n = FALSE;
4586
4587 vim_free(tofree);
4588 return n;
4589}
4590
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004591static lval_T *redir_lval = NULL;
4592#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4593static garray_T redir_ga; // only valid when redir_lval is not NULL
4594static char_u *redir_endp = NULL;
4595static char_u *redir_varname = NULL;
4596
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004597 int
4598alloc_redir_lval(void)
4599{
4600 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4601 if (redir_lval == NULL)
4602 return FAIL;
4603 return OK;
4604}
4605
4606 void
4607clear_redir_lval(void)
4608{
4609 VIM_CLEAR(redir_lval);
4610}
4611
4612 void
4613init_redir_ga(void)
4614{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004615 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004616}
4617
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004618/*
4619 * Start recording command output to a variable
4620 * When "append" is TRUE append to an existing variable.
4621 * Returns OK if successfully completed the setup. FAIL otherwise.
4622 */
4623 int
4624var_redir_start(char_u *name, int append)
4625{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004626 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004627 typval_T tv;
4628
4629 // Catch a bad name early.
4630 if (!eval_isnamec1(*name))
4631 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004632 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004633 return FAIL;
4634 }
4635
4636 // Make a copy of the name, it is used in redir_lval until redir ends.
4637 redir_varname = vim_strsave(name);
4638 if (redir_varname == NULL)
4639 return FAIL;
4640
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004641 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004642 {
4643 var_redir_stop();
4644 return FAIL;
4645 }
4646
4647 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004648 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004649
4650 // Parse the variable name (can be a dict or list entry).
4651 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4652 FNE_CHECK_START);
4653 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4654 {
4655 clear_lval(redir_lval);
4656 if (redir_endp != NULL && *redir_endp != NUL)
4657 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004658 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004659 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004660 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004661 redir_endp = NULL; // don't store a value, only cleanup
4662 var_redir_stop();
4663 return FAIL;
4664 }
4665
4666 // check if we can write to the variable: set it to or append an empty
4667 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004668 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004669 tv.v_type = VAR_STRING;
4670 tv.vval.v_string = (char_u *)"";
4671 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004672 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004673 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004674 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004675 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004676 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004677 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004678 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004679 {
4680 redir_endp = NULL; // don't store a value, only cleanup
4681 var_redir_stop();
4682 return FAIL;
4683 }
4684
4685 return OK;
4686}
4687
4688/*
4689 * Append "value[value_len]" to the variable set by var_redir_start().
4690 * The actual appending is postponed until redirection ends, because the value
4691 * appended may in fact be the string we write to, changing it may cause freed
4692 * memory to be used:
4693 * :redir => foo
4694 * :let foo
4695 * :redir END
4696 */
4697 void
4698var_redir_str(char_u *value, int value_len)
4699{
4700 int len;
4701
4702 if (redir_lval == NULL)
4703 return;
4704
4705 if (value_len == -1)
4706 len = (int)STRLEN(value); // Append the entire string
4707 else
4708 len = value_len; // Append only "value_len" characters
4709
4710 if (ga_grow(&redir_ga, len) == OK)
4711 {
4712 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4713 redir_ga.ga_len += len;
4714 }
4715 else
4716 var_redir_stop();
4717}
4718
4719/*
4720 * Stop redirecting command output to a variable.
4721 * Frees the allocated memory.
4722 */
4723 void
4724var_redir_stop(void)
4725{
4726 typval_T tv;
4727
4728 if (EVALCMD_BUSY)
4729 {
4730 redir_lval = NULL;
4731 return;
4732 }
4733
4734 if (redir_lval != NULL)
4735 {
4736 // If there was no error: assign the text to the variable.
4737 if (redir_endp != NULL)
4738 {
4739 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4740 tv.v_type = VAR_STRING;
4741 tv.vval.v_string = redir_ga.ga_data;
4742 // Call get_lval() again, if it's inside a Dict or List it may
4743 // have changed.
4744 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4745 FALSE, FALSE, 0, FNE_CHECK_START);
4746 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004747 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004748 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004749 clear_lval(redir_lval);
4750 }
4751
4752 // free the collected output
4753 VIM_CLEAR(redir_ga.ga_data);
4754
4755 VIM_CLEAR(redir_lval);
4756 }
4757 VIM_CLEAR(redir_varname);
4758}
4759
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004760/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004761 * Get the collected redirected text and clear redir_ga.
4762 */
4763 char_u *
4764get_clear_redir_ga(void)
4765{
4766 char_u *res;
4767
4768 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4769 res = redir_ga.ga_data;
4770 redir_ga.ga_data = NULL;
4771 return res;
4772}
4773
4774/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004775 * "gettabvar()" function
4776 */
4777 void
4778f_gettabvar(typval_T *argvars, typval_T *rettv)
4779{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004780 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004781 tabpage_T *tp;
4782 win_T *win = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004783
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004784 if (in_vim9script()
4785 && (check_for_number_arg(argvars, 0) == FAIL
4786 || check_for_string_arg(argvars, 1) == FAIL))
4787 return;
4788
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004789 varname = tv_get_string_chk(&argvars[1]);
4790 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
LemonBoy47d4e312022-05-04 18:12:55 +01004791 if (tp != NULL)
4792 win = tp == curtab || tp->tp_firstwin == NULL ? firstwin
4793 : tp->tp_firstwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004794
LemonBoy47d4e312022-05-04 18:12:55 +01004795 get_var_from(varname, rettv, &argvars[2], 't', tp, win, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004796}
4797
4798/*
4799 * "gettabwinvar()" function
4800 */
4801 void
4802f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4803{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004804 if (in_vim9script()
4805 && (check_for_number_arg(argvars, 0) == FAIL
4806 || check_for_number_arg(argvars, 1) == FAIL
4807 || check_for_string_arg(argvars, 2) == FAIL))
4808 return;
4809
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004810 getwinvar(argvars, rettv, 1);
4811}
4812
4813/*
4814 * "getwinvar()" function
4815 */
4816 void
4817f_getwinvar(typval_T *argvars, typval_T *rettv)
4818{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004819 if (in_vim9script()
4820 && (check_for_number_arg(argvars, 0) == FAIL
4821 || check_for_string_arg(argvars, 1) == FAIL))
4822 return;
4823
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004824 getwinvar(argvars, rettv, 0);
4825}
4826
4827/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004828 * "getbufvar()" function
4829 */
4830 void
4831f_getbufvar(typval_T *argvars, typval_T *rettv)
4832{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004833 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004834 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004835
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004836 if (in_vim9script()
4837 && (check_for_buffer_arg(argvars, 0) == FAIL
4838 || check_for_string_arg(argvars, 1) == FAIL))
4839 return;
4840
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004841 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004842 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004843
LemonBoy47d4e312022-05-04 18:12:55 +01004844 get_var_from(varname, rettv, &argvars[2], 'b', curtab, curwin, buf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004845}
4846
4847/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004848 * "settabvar()" function
4849 */
4850 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004851f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004852{
4853 tabpage_T *save_curtab;
4854 tabpage_T *tp;
zeertzjqb47fbb42024-02-12 22:50:26 +01004855 tabpage_T *save_lu_tp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004856 char_u *varname, *tabvarname;
4857 typval_T *varp;
4858
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004859 if (check_secure())
4860 return;
4861
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004862 if (in_vim9script()
4863 && (check_for_number_arg(argvars, 0) == FAIL
4864 || check_for_string_arg(argvars, 1) == FAIL))
4865 return;
4866
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004867 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4868 varname = tv_get_string_chk(&argvars[1]);
4869 varp = &argvars[2];
4870
zeertzjqea720ae2023-01-03 10:54:09 +00004871 if (varname == NULL || tp == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004872 return;
4873
4874 save_curtab = curtab;
zeertzjqb47fbb42024-02-12 22:50:26 +01004875 save_lu_tp = lastused_tabpage;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004876 goto_tabpage_tp(tp, FALSE, FALSE);
4877
4878 tabvarname = alloc(STRLEN(varname) + 3);
4879 if (tabvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004880 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004881 STRCPY(tabvarname, "t:");
4882 STRCPY(tabvarname + 2, varname);
4883 set_var(tabvarname, varp, TRUE);
4884 vim_free(tabvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004885 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004886
zeertzjqb47fbb42024-02-12 22:50:26 +01004887 // Restore current tabpage and last accessed tabpage.
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004888 if (valid_tabpage(save_curtab))
zeertzjqb47fbb42024-02-12 22:50:26 +01004889 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004890 goto_tabpage_tp(save_curtab, FALSE, FALSE);
zeertzjqb47fbb42024-02-12 22:50:26 +01004891 if (valid_tabpage(save_lu_tp))
4892 lastused_tabpage = save_lu_tp;
4893 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004894}
4895
4896/*
4897 * "settabwinvar()" function
4898 */
4899 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004900f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004901{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004902 if (in_vim9script()
4903 && (check_for_number_arg(argvars, 0) == FAIL
4904 || check_for_number_arg(argvars, 1) == FAIL
4905 || check_for_string_arg(argvars, 2) == FAIL))
4906 return;
4907
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004908 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004909}
4910
4911/*
4912 * "setwinvar()" function
4913 */
4914 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004915f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004916{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004917 if (in_vim9script()
4918 && (check_for_number_arg(argvars, 0) == FAIL
4919 || check_for_string_arg(argvars, 1) == FAIL))
4920 return;
4921
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004922 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004923}
4924
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004925/*
4926 * "setbufvar()" function
4927 */
4928 void
4929f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4930{
4931 buf_T *buf;
4932 char_u *varname, *bufvarname;
4933 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004934
4935 if (check_secure())
4936 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004937
4938 if (in_vim9script()
4939 && (check_for_buffer_arg(argvars, 0) == FAIL
4940 || check_for_string_arg(argvars, 1) == FAIL))
4941 return;
4942
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004943 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004944 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004945 varp = &argvars[2];
4946
zeertzjqea720ae2023-01-03 10:54:09 +00004947 if (buf == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004948 return;
4949
4950 if (*varname == '&')
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004951 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004952 aco_save_T aco;
Christian Brabandtac4cffc2024-01-16 17:22:38 +01004953 // safe the current window position, it could
4954 // change because of 'scrollbind' window-local
4955 // options
4956 linenr_T old_topline = curwin->w_topline;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004957
4958 // Set curbuf to be our buf, temporarily.
4959 aucmd_prepbuf(&aco, buf);
4960 if (curbuf == buf)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004961 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004962 // Only when it worked to set "curbuf".
4963 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004964
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004965 // reset notion of buffer
4966 aucmd_restbuf(&aco);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004967 }
Christian Brabandtac4cffc2024-01-16 17:22:38 +01004968 curwin->w_topline = old_topline;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004969 }
4970 else
4971 {
4972 bufvarname = alloc(STRLEN(varname) + 3);
4973 if (bufvarname != NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004974 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004975 buf_T *save_curbuf = curbuf;
Bram Moolenaar86015452020-03-29 15:12:15 +02004976
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004977 curbuf = buf;
4978 STRCPY(bufvarname, "b:");
4979 STRCPY(bufvarname + 2, varname);
4980 set_var(bufvarname, varp, TRUE);
4981 vim_free(bufvarname);
4982 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004983 }
4984 }
4985}
4986
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004987/*
4988 * Get a callback from "arg". It can be a Funcref or a function name.
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004989 * When "arg" is zero "res.cb_name" is set to an empty string.
4990 * If "res.cb_name" is allocated then "res.cb_free_name" is set to TRUE.
4991 * "res.cb_name" is set to NULL for an invalid argument.
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004992 */
4993 callback_T
4994get_callback(typval_T *arg)
4995{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004996 callback_T res;
4997 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004998
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004999 CLEAR_FIELD(res);
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005000 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
5001 {
5002 res.cb_partial = arg->vval.v_partial;
5003 ++res.cb_partial->pt_refcount;
5004 res.cb_name = partial_name(res.cb_partial);
5005 }
5006 else
5007 {
Bram Moolenaar14e579092020-03-07 16:59:25 +01005008 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
Keith Thompson184f71c2024-01-04 21:19:04 +01005009 && SAFE_isdigit(*arg->vval.v_string))
Bram Moolenaar14e579092020-03-07 16:59:25 +01005010 r = FAIL;
5011 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005012 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005013 res.cb_name = arg->vval.v_string;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00005014 if (arg->v_type == VAR_STRING)
5015 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005016 char_u *name = get_scriptlocal_funcname(arg->vval.v_string);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00005017 if (name != NULL)
5018 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005019 res.cb_name = name;
5020 res.cb_free_name = TRUE;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00005021 }
5022 }
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005023 func_ref(res.cb_name);
5024 }
5025 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005026 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005027 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01005028 r = FAIL;
5029
5030 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005031 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005032 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005033 res.cb_name = NULL;
5034 }
5035 }
5036 return res;
5037}
5038
5039/*
5040 * Copy a callback into a typval_T.
5041 */
5042 void
5043put_callback(callback_T *cb, typval_T *tv)
5044{
5045 if (cb->cb_partial != NULL)
5046 {
5047 tv->v_type = VAR_PARTIAL;
5048 tv->vval.v_partial = cb->cb_partial;
5049 ++tv->vval.v_partial->pt_refcount;
5050 }
5051 else
5052 {
5053 tv->v_type = VAR_FUNC;
5054 tv->vval.v_string = vim_strsave(cb->cb_name);
5055 func_ref(cb->cb_name);
5056 }
5057}
5058
5059/*
5060 * Make a copy of "src" into "dest", allocating the function name if needed,
5061 * without incrementing the refcount.
5062 */
5063 void
5064set_callback(callback_T *dest, callback_T *src)
5065{
5066 if (src->cb_partial == NULL)
5067 {
5068 // just a function name, make a copy
5069 dest->cb_name = vim_strsave(src->cb_name);
5070 dest->cb_free_name = TRUE;
5071 }
5072 else
5073 {
5074 // cb_name is a pointer into cb_partial
5075 dest->cb_name = src->cb_name;
5076 dest->cb_free_name = FALSE;
5077 }
5078 dest->cb_partial = src->cb_partial;
5079}
5080
5081/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02005082 * Copy callback from "src" to "dest", incrementing the refcounts.
5083 */
5084 void
5085copy_callback(callback_T *dest, callback_T *src)
5086{
5087 dest->cb_partial = src->cb_partial;
5088 if (dest->cb_partial != NULL)
5089 {
5090 dest->cb_name = src->cb_name;
5091 dest->cb_free_name = FALSE;
5092 ++dest->cb_partial->pt_refcount;
5093 }
5094 else
5095 {
5096 dest->cb_name = vim_strsave(src->cb_name);
5097 dest->cb_free_name = TRUE;
5098 func_ref(src->cb_name);
5099 }
5100}
5101
5102/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005103 * When a callback refers to an autoload import, change the function name to
5104 * the "path#name" form. Uses the current script context.
5105 * Only works when the name is allocated.
5106 */
5107 void
5108expand_autload_callback(callback_T *cb)
5109{
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005110 char_u *name;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005111 char_u *p;
5112 imported_T *import;
5113
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005114 if (!in_vim9script() || cb->cb_name == NULL
5115 || (!cb->cb_free_name
5116 && (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005117 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005118 if (cb->cb_partial != NULL)
5119 name = cb->cb_partial->pt_name;
5120 else
5121 name = cb->cb_name;
5122 p = vim_strchr(name, '.');
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005123 if (p == NULL)
5124 return;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005125
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005126 import = find_imported(name, p - name, FALSE);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005127 if (import == NULL || !SCRIPT_ID_VALID(import->imp_sid))
5128 return;
5129
5130 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
5131 if (si->sn_autoload_prefix == NULL)
5132 return;
5133
5134 char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
5135 if (newname == NULL)
5136 return;
5137
5138 if (cb->cb_partial != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005139 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005140 if (cb->cb_name == cb->cb_partial->pt_name)
5141 cb->cb_name = newname;
5142 vim_free(cb->cb_partial->pt_name);
5143 cb->cb_partial->pt_name = newname;
5144 }
5145 else
5146 {
5147 vim_free(cb->cb_name);
5148 cb->cb_name = newname;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005149 }
5150}
5151
5152/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005153 * Unref/free "callback" returned by get_callback() or set_callback().
5154 */
5155 void
5156free_callback(callback_T *callback)
5157{
5158 if (callback->cb_partial != NULL)
5159 {
5160 partial_unref(callback->cb_partial);
5161 callback->cb_partial = NULL;
5162 }
5163 else if (callback->cb_name != NULL)
5164 func_unref(callback->cb_name);
5165 if (callback->cb_free_name)
5166 {
5167 vim_free(callback->cb_name);
5168 callback->cb_free_name = FALSE;
5169 }
5170 callback->cb_name = NULL;
5171}
5172
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005173#endif // FEAT_EVAL